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);
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);
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 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,
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);
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);
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, "rb");
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);
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, O_RDONLY | O_NOFOLLOW);
3464 if (fd == -1) {
3465 if (errno != ENOENT && errno != EACCES)
3466 err = got_error_from_errno2("openat",
3467 ignorespath);
3468 } else {
3469 ignoresfile = fdopen(fd, "r");
3470 if (ignoresfile == NULL)
3471 err = got_error_from_errno2("fdopen",
3472 ignorespath);
3473 else {
3474 fd = -1;
3475 err = read_ignores(ignores, path, ignoresfile);
3478 } else {
3479 ignoresfile = fopen(ignorespath, "r");
3480 if (ignoresfile == NULL) {
3481 if (errno != ENOENT && errno != EACCES)
3482 err = got_error_from_errno2("fopen",
3483 ignorespath);
3484 } else
3485 err = read_ignores(ignores, path, ignoresfile);
3488 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3489 err = got_error_from_errno2("fclose", path);
3490 if (fd != -1 && close(fd) == -1 && err == NULL)
3491 err = got_error_from_errno2("close", path);
3492 free(ignorespath);
3493 return err;
3496 static const struct got_error *
3497 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3498 int dirfd)
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3502 char *path = NULL;
3504 if (ignore != NULL)
3505 *ignore = 0;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (parent_path[0]) {
3511 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3512 return got_error_from_errno("asprintf");
3513 } else {
3514 path = de->d_name;
3517 if (de->d_type == DT_DIR) {
3518 if (!a->no_ignores && ignore != NULL &&
3519 match_ignores(a->ignores, path))
3520 *ignore = 1;
3521 } else if (!match_ignores(a->ignores, path) &&
3522 got_path_is_child(path, a->status_path, a->status_path_len))
3523 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3525 if (parent_path[0])
3526 free(path);
3527 return err;
3530 static const struct got_error *
3531 status_traverse(void *arg, const char *path, int dirfd)
3533 const struct got_error *err = NULL;
3534 struct diff_dir_cb_arg *a = arg;
3536 if (a->no_ignores)
3537 return NULL;
3539 err = add_ignores(a->ignores, a->worktree->root_path,
3540 path, dirfd, ".cvsignore");
3541 if (err)
3542 return err;
3544 err = add_ignores(a->ignores, a->worktree->root_path, path,
3545 dirfd, ".gitignore");
3547 return err;
3550 static const struct got_error *
3551 report_single_file_status(const char *path, const char *ondisk_path,
3552 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3553 void *status_arg, struct got_repository *repo, int report_unchanged,
3554 struct got_pathlist_head *ignores, int no_ignores)
3556 struct got_fileindex_entry *ie;
3557 struct stat sb;
3559 if (!no_ignores && match_ignores(ignores, path))
3560 return NULL;
3562 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3563 if (ie)
3564 return report_file_status(ie, ondisk_path, -1, NULL,
3565 status_cb, status_arg, repo, report_unchanged);
3567 if (lstat(ondisk_path, &sb) == -1) {
3568 if (errno != ENOENT)
3569 return got_error_from_errno2("lstat", ondisk_path);
3570 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3571 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 return NULL;
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3579 return NULL;
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3590 ".cvsignore");
3591 if (err)
3592 return err;
3594 err = add_ignores(ignores, root_path, "", -1,
3595 ".gitignore");
3596 if (err)
3597 return err;
3599 err = got_path_dirname(&parent_path, path);
3600 if (err) {
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3603 return err;
3605 for (;;) {
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3607 ".cvsignore");
3608 if (err)
3609 break;
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3611 ".gitignore");
3612 if (err)
3613 break;
3614 err = got_path_dirname(&next_parent_path, parent_path);
3615 if (err) {
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3618 break;
3620 if (got_path_is_root_dir(parent_path))
3621 break;
3622 free(parent_path);
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3627 free(parent_path);
3628 free(next_parent_path);
3629 return err;
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3640 int fd = -1;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3646 TAILQ_INIT(&ignores);
3648 if (asprintf(&ondisk_path, "%s%s%s",
3649 worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 return got_error_from_errno("asprintf");
3652 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3653 if (fd == -1) {
3654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 !got_err_open_nofollow_on_symlink())
3656 err = got_error_from_errno2("open", ondisk_path);
3657 else {
3658 if (!no_ignores) {
3659 err = add_ignores_from_parent_paths(&ignores,
3660 worktree->root_path, ondisk_path);
3661 if (err)
3662 goto done;
3664 err = report_single_file_status(path, ondisk_path,
3665 fileindex, status_cb, status_arg, repo,
3666 report_unchanged, &ignores, no_ignores);
3668 } else {
3669 fdiff_cb.diff_old_new = status_old_new;
3670 fdiff_cb.diff_old = status_old;
3671 fdiff_cb.diff_new = status_new;
3672 fdiff_cb.diff_traverse = status_traverse;
3673 arg.fileindex = fileindex;
3674 arg.worktree = worktree;
3675 arg.status_path = path;
3676 arg.status_path_len = strlen(path);
3677 arg.repo = repo;
3678 arg.status_cb = status_cb;
3679 arg.status_arg = status_arg;
3680 arg.cancel_cb = cancel_cb;
3681 arg.cancel_arg = cancel_arg;
3682 arg.report_unchanged = report_unchanged;
3683 arg.no_ignores = no_ignores;
3684 if (!no_ignores) {
3685 err = add_ignores_from_parent_paths(&ignores,
3686 worktree->root_path, path);
3687 if (err)
3688 goto done;
3690 arg.ignores = &ignores;
3691 err = got_fileindex_diff_dir(fileindex, fd,
3692 worktree->root_path, path, repo, &fdiff_cb, &arg);
3694 done:
3695 free_ignores(&ignores);
3696 if (fd != -1 && close(fd) == -1 && err == NULL)
3697 err = got_error_from_errno("close");
3698 free(ondisk_path);
3699 return err;
3702 const struct got_error *
3703 got_worktree_status(struct got_worktree *worktree,
3704 struct got_pathlist_head *paths, struct got_repository *repo,
3705 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 got_cancel_cb cancel_cb, void *cancel_arg)
3708 const struct got_error *err = NULL;
3709 char *fileindex_path = NULL;
3710 struct got_fileindex *fileindex = NULL;
3711 struct got_pathlist_entry *pe;
3713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3714 if (err)
3715 return err;
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 status_cb, status_arg, cancel_cb, cancel_arg,
3720 no_ignores, 0);
3721 if (err)
3722 break;
3724 free(fileindex_path);
3725 got_fileindex_free(fileindex);
3726 return err;
3729 const struct got_error *
3730 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3731 const char *arg)
3733 const struct got_error *err = NULL;
3734 char *resolved = NULL, *cwd = NULL, *path = NULL;
3735 size_t len;
3736 struct stat sb;
3737 char *abspath = NULL;
3738 char canonpath[PATH_MAX];
3740 *wt_path = NULL;
3742 cwd = getcwd(NULL, 0);
3743 if (cwd == NULL)
3744 return got_error_from_errno("getcwd");
3746 if (lstat(arg, &sb) == -1) {
3747 if (errno != ENOENT) {
3748 err = got_error_from_errno2("lstat", arg);
3749 goto done;
3751 sb.st_mode = 0;
3753 if (S_ISLNK(sb.st_mode)) {
3755 * We cannot use realpath(3) with symlinks since we want to
3756 * operate on the symlink itself.
3757 * But we can make the path absolute, assuming it is relative
3758 * to the current working directory, and then canonicalize it.
3760 if (!got_path_is_absolute(arg)) {
3761 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 err = got_error_from_errno("asprintf");
3763 goto done;
3767 err = got_canonpath(abspath ? abspath : arg, canonpath,
3768 sizeof(canonpath));
3769 if (err)
3770 goto done;
3771 resolved = strdup(canonpath);
3772 if (resolved == NULL) {
3773 err = got_error_from_errno("strdup");
3774 goto done;
3776 } else {
3777 resolved = realpath(arg, NULL);
3778 if (resolved == NULL) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("realpath", arg);
3781 goto done;
3783 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 err = got_error_from_errno("asprintf");
3785 goto done;
3787 err = got_canonpath(abspath, canonpath,
3788 sizeof(canonpath));
3789 if (err)
3790 goto done;
3791 resolved = strdup(canonpath);
3792 if (resolved == NULL) {
3793 err = got_error_from_errno("strdup");
3794 goto done;
3799 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 strlen(got_worktree_get_root_path(worktree)))) {
3801 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3802 goto done;
3805 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 err = got_path_skip_common_ancestor(&path,
3807 got_worktree_get_root_path(worktree), resolved);
3808 if (err)
3809 goto done;
3810 } else {
3811 path = strdup("");
3812 if (path == NULL) {
3813 err = got_error_from_errno("strdup");
3814 goto done;
3818 /* XXX status walk can't deal with trailing slash! */
3819 len = strlen(path);
3820 while (len > 0 && path[len - 1] == '/') {
3821 path[len - 1] = '\0';
3822 len--;
3824 done:
3825 free(abspath);
3826 free(resolved);
3827 free(cwd);
3828 if (err == NULL)
3829 *wt_path = path;
3830 else
3831 free(path);
3832 return err;
3835 struct schedule_addition_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_checkout_cb progress_cb;
3839 void *progress_arg;
3840 struct got_repository *repo;
3843 static const struct got_error *
3844 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 const char *relpath, struct got_object_id *blob_id,
3846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 int dirfd, const char *de_name)
3849 struct schedule_addition_args *a = arg;
3850 const struct got_error *err = NULL;
3851 struct got_fileindex_entry *ie;
3852 struct stat sb;
3853 char *ondisk_path;
3855 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3856 relpath) == -1)
3857 return got_error_from_errno("asprintf");
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3860 if (ie) {
3861 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3862 de_name, a->repo);
3863 if (err)
3864 goto done;
3865 /* Re-adding an existing entry is a no-op. */
3866 if (status == GOT_STATUS_ADD)
3867 goto done;
3868 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3869 if (err)
3870 goto done;
3873 if (status != GOT_STATUS_UNVERSIONED) {
3874 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3875 goto done;
3878 err = got_fileindex_entry_alloc(&ie, relpath);
3879 if (err)
3880 goto done;
3881 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3882 relpath, NULL, NULL, 1);
3883 if (err) {
3884 got_fileindex_entry_free(ie);
3885 goto done;
3887 err = got_fileindex_entry_add(a->fileindex, ie);
3888 if (err) {
3889 got_fileindex_entry_free(ie);
3890 goto done;
3892 done:
3893 free(ondisk_path);
3894 if (err)
3895 return err;
3896 if (status == GOT_STATUS_ADD)
3897 return NULL;
3898 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3901 const struct got_error *
3902 got_worktree_schedule_add(struct got_worktree *worktree,
3903 struct got_pathlist_head *paths,
3904 got_worktree_checkout_cb progress_cb, void *progress_arg,
3905 struct got_repository *repo, int no_ignores)
3907 struct got_fileindex *fileindex = NULL;
3908 char *fileindex_path = NULL;
3909 const struct got_error *err = NULL, *sync_err, *unlockerr;
3910 struct got_pathlist_entry *pe;
3911 struct schedule_addition_args saa;
3913 err = lock_worktree(worktree, LOCK_EX);
3914 if (err)
3915 return err;
3917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3918 if (err)
3919 goto done;
3921 saa.worktree = worktree;
3922 saa.fileindex = fileindex;
3923 saa.progress_cb = progress_cb;
3924 saa.progress_arg = progress_arg;
3925 saa.repo = repo;
3927 TAILQ_FOREACH(pe, paths, entry) {
3928 err = worktree_status(worktree, pe->path, fileindex, repo,
3929 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3930 if (err)
3931 break;
3933 sync_err = sync_fileindex(fileindex, fileindex_path);
3934 if (sync_err && err == NULL)
3935 err = sync_err;
3936 done:
3937 free(fileindex_path);
3938 if (fileindex)
3939 got_fileindex_free(fileindex);
3940 unlockerr = lock_worktree(worktree, LOCK_SH);
3941 if (unlockerr && err == NULL)
3942 err = unlockerr;
3943 return err;
3946 struct schedule_deletion_args {
3947 struct got_worktree *worktree;
3948 struct got_fileindex *fileindex;
3949 got_worktree_delete_cb progress_cb;
3950 void *progress_arg;
3951 struct got_repository *repo;
3952 int delete_local_mods;
3953 int keep_on_disk;
3954 const char *status_codes;
3957 static const struct got_error *
3958 schedule_for_deletion(void *arg, unsigned char status,
3959 unsigned char staged_status, const char *relpath,
3960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3961 struct got_object_id *commit_id, int dirfd, const char *de_name)
3963 struct schedule_deletion_args *a = arg;
3964 const struct got_error *err = NULL;
3965 struct got_fileindex_entry *ie = NULL;
3966 struct stat sb;
3967 char *ondisk_path;
3969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 if (ie == NULL)
3971 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3973 staged_status = get_staged_status(ie);
3974 if (staged_status != GOT_STATUS_NO_CHANGE) {
3975 if (staged_status == GOT_STATUS_DELETE)
3976 return NULL;
3977 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3981 relpath) == -1)
3982 return got_error_from_errno("asprintf");
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3985 a->repo);
3986 if (err)
3987 goto done;
3989 if (a->status_codes) {
3990 size_t ncodes = strlen(a->status_codes);
3991 int i;
3992 for (i = 0; i < ncodes ; i++) {
3993 if (status == a->status_codes[i])
3994 break;
3996 if (i == ncodes) {
3997 /* Do not delete files in non-matching status. */
3998 free(ondisk_path);
3999 return NULL;
4001 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4002 a->status_codes[i] != GOT_STATUS_MISSING) {
4003 static char msg[64];
4004 snprintf(msg, sizeof(msg),
4005 "invalid status code '%c'", a->status_codes[i]);
4006 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4007 goto done;
4011 if (status != GOT_STATUS_NO_CHANGE) {
4012 if (status == GOT_STATUS_DELETE)
4013 goto done;
4014 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4015 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4016 goto done;
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MISSING) {
4020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4021 goto done;
4025 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4026 size_t root_len;
4028 if (dirfd != -1) {
4029 if (unlinkat(dirfd, de_name, 0) != 0) {
4030 err = got_error_from_errno2("unlinkat",
4031 ondisk_path);
4032 goto done;
4034 } else if (unlink(ondisk_path) != 0) {
4035 err = got_error_from_errno2("unlink", ondisk_path);
4036 goto done;
4039 root_len = strlen(a->worktree->root_path);
4040 do {
4041 char *parent;
4042 err = got_path_dirname(&parent, ondisk_path);
4043 if (err)
4044 goto done;
4045 free(ondisk_path);
4046 ondisk_path = parent;
4047 if (rmdir(ondisk_path) == -1) {
4048 if (errno != ENOTEMPTY)
4049 err = got_error_from_errno2("rmdir",
4050 ondisk_path);
4051 break;
4053 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4054 strlen(ondisk_path), root_len) != 0);
4057 got_fileindex_entry_mark_deleted_from_disk(ie);
4058 done:
4059 free(ondisk_path);
4060 if (err)
4061 return err;
4062 if (status == GOT_STATUS_DELETE)
4063 return NULL;
4064 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4065 staged_status, relpath);
4068 const struct got_error *
4069 got_worktree_schedule_delete(struct got_worktree *worktree,
4070 struct got_pathlist_head *paths, int delete_local_mods,
4071 const char *status_codes,
4072 got_worktree_delete_cb progress_cb, void *progress_arg,
4073 struct got_repository *repo, int keep_on_disk)
4075 struct got_fileindex *fileindex = NULL;
4076 char *fileindex_path = NULL;
4077 const struct got_error *err = NULL, *sync_err, *unlockerr;
4078 struct got_pathlist_entry *pe;
4079 struct schedule_deletion_args sda;
4081 err = lock_worktree(worktree, LOCK_EX);
4082 if (err)
4083 return err;
4085 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4086 if (err)
4087 goto done;
4089 sda.worktree = worktree;
4090 sda.fileindex = fileindex;
4091 sda.progress_cb = progress_cb;
4092 sda.progress_arg = progress_arg;
4093 sda.repo = repo;
4094 sda.delete_local_mods = delete_local_mods;
4095 sda.keep_on_disk = keep_on_disk;
4096 sda.status_codes = status_codes;
4098 TAILQ_FOREACH(pe, paths, entry) {
4099 err = worktree_status(worktree, pe->path, fileindex, repo,
4100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4101 if (err)
4102 break;
4104 sync_err = sync_fileindex(fileindex, fileindex_path);
4105 if (sync_err && err == NULL)
4106 err = sync_err;
4107 done:
4108 free(fileindex_path);
4109 if (fileindex)
4110 got_fileindex_free(fileindex);
4111 unlockerr = lock_worktree(worktree, LOCK_SH);
4112 if (unlockerr && err == NULL)
4113 err = unlockerr;
4114 return err;
4117 static const struct got_error *
4118 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4120 const struct got_error *err = NULL;
4121 char *line = NULL;
4122 size_t linesize = 0, n;
4123 ssize_t linelen;
4125 linelen = getline(&line, &linesize, infile);
4126 if (linelen == -1) {
4127 if (ferror(infile)) {
4128 err = got_error_from_errno("getline");
4129 goto done;
4131 return NULL;
4133 if (outfile) {
4134 n = fwrite(line, 1, linelen, outfile);
4135 if (n != linelen) {
4136 err = got_ferror(outfile, GOT_ERR_IO);
4137 goto done;
4140 if (rejectfile) {
4141 n = fwrite(line, 1, linelen, rejectfile);
4142 if (n != linelen)
4143 err = got_ferror(outfile, GOT_ERR_IO);
4145 done:
4146 free(line);
4147 return err;
4150 static const struct got_error *
4151 skip_one_line(FILE *f)
4153 char *line = NULL;
4154 size_t linesize = 0;
4155 ssize_t linelen;
4157 linelen = getline(&line, &linesize, f);
4158 if (linelen == -1) {
4159 if (ferror(f))
4160 return got_error_from_errno("getline");
4161 return NULL;
4163 free(line);
4164 return NULL;
4167 static const struct got_error *
4168 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4169 int start_old, int end_old, int start_new, int end_new,
4170 FILE *outfile, FILE *rejectfile)
4172 const struct got_error *err;
4174 /* Copy old file's lines leading up to patch. */
4175 while (!feof(f1) && *line_cur1 < start_old) {
4176 err = copy_one_line(f1, outfile, NULL);
4177 if (err)
4178 return err;
4179 (*line_cur1)++;
4181 /* Skip new file's lines leading up to patch. */
4182 while (!feof(f2) && *line_cur2 < start_new) {
4183 if (rejectfile)
4184 err = copy_one_line(f2, NULL, rejectfile);
4185 else
4186 err = skip_one_line(f2);
4187 if (err)
4188 return err;
4189 (*line_cur2)++;
4191 /* Copy patched lines. */
4192 while (!feof(f2) && *line_cur2 <= end_new) {
4193 err = copy_one_line(f2, outfile, NULL);
4194 if (err)
4195 return err;
4196 (*line_cur2)++;
4198 /* Skip over old file's replaced lines. */
4199 while (!feof(f1) && *line_cur1 <= end_old) {
4200 if (rejectfile)
4201 err = copy_one_line(f1, NULL, rejectfile);
4202 else
4203 err = skip_one_line(f1);
4204 if (err)
4205 return err;
4206 (*line_cur1)++;
4209 return NULL;
4212 static const struct got_error *
4213 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 FILE *outfile, FILE *rejectfile)
4216 const struct got_error *err;
4218 if (outfile) {
4219 /* Copy old file's lines until EOF. */
4220 while (!feof(f1)) {
4221 err = copy_one_line(f1, outfile, NULL);
4222 if (err)
4223 return err;
4224 (*line_cur1)++;
4227 if (rejectfile) {
4228 /* Copy new file's lines until EOF. */
4229 while (!feof(f2)) {
4230 err = copy_one_line(f2, NULL, rejectfile);
4231 if (err)
4232 return err;
4233 (*line_cur2)++;
4237 return NULL;
4240 static const struct got_error *
4241 apply_or_reject_change(int *choice, int *nchunks_used,
4242 struct diff_result *diff_result, int n,
4243 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4244 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4245 got_worktree_patch_cb patch_cb, void *patch_arg)
4247 const struct got_error *err = NULL;
4248 struct diff_chunk_context cc = {};
4249 int start_old, end_old, start_new, end_new;
4250 FILE *hunkfile;
4251 struct diff_output_unidiff_state *diff_state;
4252 struct diff_input_info diff_info;
4253 int rc;
4255 *choice = GOT_PATCH_CHOICE_NONE;
4257 /* Get changed line numbers without context lines for copy_change(). */
4258 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4259 start_old = cc.left.start;
4260 end_old = cc.left.end;
4261 start_new = cc.right.start;
4262 end_new = cc.right.end;
4264 /* Get the same change with context lines for display. */
4265 memset(&cc, 0, sizeof(cc));
4266 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4268 memset(&diff_info, 0, sizeof(diff_info));
4269 diff_info.left_path = relpath;
4270 diff_info.right_path = relpath;
4272 diff_state = diff_output_unidiff_state_alloc();
4273 if (diff_state == NULL)
4274 return got_error_set_errno(ENOMEM,
4275 "diff_output_unidiff_state_alloc");
4277 hunkfile = got_opentemp();
4278 if (hunkfile == NULL) {
4279 err = got_error_from_errno("got_opentemp");
4280 goto done;
4283 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4284 diff_result, &cc);
4285 if (rc != DIFF_RC_OK) {
4286 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4287 goto done;
4290 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4291 err = got_ferror(hunkfile, GOT_ERR_IO);
4292 goto done;
4295 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4296 hunkfile, changeno, nchanges);
4297 if (err)
4298 goto done;
4300 switch (*choice) {
4301 case GOT_PATCH_CHOICE_YES:
4302 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4303 end_old, start_new, end_new, outfile, rejectfile);
4304 break;
4305 case GOT_PATCH_CHOICE_NO:
4306 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 end_old, start_new, end_new, rejectfile, outfile);
4308 break;
4309 case GOT_PATCH_CHOICE_QUIT:
4310 break;
4311 default:
4312 err = got_error(GOT_ERR_PATCH_CHOICE);
4313 break;
4315 done:
4316 diff_output_unidiff_state_free(diff_state);
4317 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4318 err = got_error_from_errno("fclose");
4319 return err;
4322 struct revert_file_args {
4323 struct got_worktree *worktree;
4324 struct got_fileindex *fileindex;
4325 got_worktree_checkout_cb progress_cb;
4326 void *progress_arg;
4327 got_worktree_patch_cb patch_cb;
4328 void *patch_arg;
4329 struct got_repository *repo;
4330 int unlink_added_files;
4333 static const struct got_error *
4334 create_patched_content(char **path_outfile, int reverse_patch,
4335 struct got_object_id *blob_id, const char *path2,
4336 int dirfd2, const char *de_name2,
4337 const char *relpath, struct got_repository *repo,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err, *free_err;
4341 struct got_blob_object *blob = NULL;
4342 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4343 int fd2 = -1;
4344 char link_target[PATH_MAX];
4345 ssize_t link_len = 0;
4346 char *path1 = NULL, *id_str = NULL;
4347 struct stat sb2;
4348 struct got_diffreg_result *diffreg_result = NULL;
4349 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4350 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4352 *path_outfile = NULL;
4354 err = got_object_id_str(&id_str, blob_id);
4355 if (err)
4356 return err;
4358 if (dirfd2 != -1) {
4359 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4360 if (fd2 == -1) {
4361 if (!got_err_open_nofollow_on_symlink()) {
4362 err = got_error_from_errno2("openat", path2);
4363 goto done;
4365 link_len = readlinkat(dirfd2, de_name2,
4366 link_target, sizeof(link_target));
4367 if (link_len == -1)
4368 return got_error_from_errno2("readlinkat", path2);
4369 sb2.st_mode = S_IFLNK;
4370 sb2.st_size = link_len;
4372 } else {
4373 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4374 if (fd2 == -1) {
4375 if (!got_err_open_nofollow_on_symlink()) {
4376 err = got_error_from_errno2("open", path2);
4377 goto done;
4379 link_len = readlink(path2, link_target,
4380 sizeof(link_target));
4381 if (link_len == -1)
4382 return got_error_from_errno2("readlink", path2);
4383 sb2.st_mode = S_IFLNK;
4384 sb2.st_size = link_len;
4387 if (fd2 != -1) {
4388 if (fstat(fd2, &sb2) == -1) {
4389 err = got_error_from_errno2("fstat", path2);
4390 goto done;
4393 f2 = fdopen(fd2, "r");
4394 if (f2 == NULL) {
4395 err = got_error_from_errno2("fdopen", path2);
4396 goto done;
4398 fd2 = -1;
4399 } else {
4400 size_t n;
4401 f2 = got_opentemp();
4402 if (f2 == NULL) {
4403 err = got_error_from_errno2("got_opentemp", path2);
4404 goto done;
4406 n = fwrite(link_target, 1, link_len, f2);
4407 if (n != link_len) {
4408 err = got_ferror(f2, GOT_ERR_IO);
4409 goto done;
4411 if (fflush(f2) == EOF) {
4412 err = got_error_from_errno("fflush");
4413 goto done;
4415 rewind(f2);
4418 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4419 if (err)
4420 goto done;
4422 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4423 if (err)
4424 goto done;
4426 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4427 if (err)
4428 goto done;
4430 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4431 NULL);
4432 if (err)
4433 goto done;
4435 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4436 if (err)
4437 goto done;
4439 if (fseek(f1, 0L, SEEK_SET) == -1)
4440 return got_ferror(f1, GOT_ERR_IO);
4441 if (fseek(f2, 0L, SEEK_SET) == -1)
4442 return got_ferror(f2, GOT_ERR_IO);
4444 /* Count the number of actual changes in the diff result. */
4445 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4446 struct diff_chunk_context cc = {};
4447 diff_chunk_context_load_change(&cc, &nchunks_used,
4448 diffreg_result->result, n, 0);
4449 nchanges++;
4451 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4452 int choice;
4453 err = apply_or_reject_change(&choice, &nchunks_used,
4454 diffreg_result->result, n, relpath, f1, f2,
4455 &line_cur1, &line_cur2,
4456 reverse_patch ? NULL : outfile,
4457 reverse_patch ? outfile : NULL,
4458 ++i, nchanges, patch_cb, patch_arg);
4459 if (err)
4460 goto done;
4461 if (choice == GOT_PATCH_CHOICE_YES)
4462 have_content = 1;
4463 else if (choice == GOT_PATCH_CHOICE_QUIT)
4464 break;
4466 if (have_content) {
4467 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4468 reverse_patch ? NULL : outfile,
4469 reverse_patch ? outfile : NULL);
4470 if (err)
4471 goto done;
4473 if (!S_ISLNK(sb2.st_mode)) {
4474 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4475 err = got_error_from_errno2("fchmod", path2);
4476 goto done;
4480 done:
4481 free(id_str);
4482 if (blob)
4483 got_object_blob_close(blob);
4484 free_err = got_diffreg_result_free(diffreg_result);
4485 if (err == NULL)
4486 err = free_err;
4487 if (f1 && fclose(f1) == EOF && err == NULL)
4488 err = got_error_from_errno2("fclose", path1);
4489 if (f2 && fclose(f2) == EOF && err == NULL)
4490 err = got_error_from_errno2("fclose", path2);
4491 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4492 err = got_error_from_errno2("close", path2);
4493 if (outfile && fclose(outfile) == EOF && err == NULL)
4494 err = got_error_from_errno2("fclose", *path_outfile);
4495 if (path1 && unlink(path1) == -1 && err == NULL)
4496 err = got_error_from_errno2("unlink", path1);
4497 if (err || !have_content) {
4498 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4499 err = got_error_from_errno2("unlink", *path_outfile);
4500 free(*path_outfile);
4501 *path_outfile = NULL;
4503 free(path1);
4504 return err;
4507 static const struct got_error *
4508 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4509 const char *relpath, struct got_object_id *blob_id,
4510 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4511 int dirfd, const char *de_name)
4513 struct revert_file_args *a = arg;
4514 const struct got_error *err = NULL;
4515 char *parent_path = NULL;
4516 struct got_fileindex_entry *ie;
4517 struct got_tree_object *tree = NULL;
4518 struct got_object_id *tree_id = NULL;
4519 const struct got_tree_entry *te = NULL;
4520 char *tree_path = NULL, *te_name;
4521 char *ondisk_path = NULL, *path_content = NULL;
4522 struct got_blob_object *blob = NULL;
4524 /* Reverting a staged deletion is a no-op. */
4525 if (status == GOT_STATUS_DELETE &&
4526 staged_status != GOT_STATUS_NO_CHANGE)
4527 return NULL;
4529 if (status == GOT_STATUS_UNVERSIONED)
4530 return (*a->progress_cb)(a->progress_arg,
4531 GOT_STATUS_UNVERSIONED, relpath);
4533 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4534 if (ie == NULL)
4535 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4537 /* Construct in-repository path of tree which contains this blob. */
4538 err = got_path_dirname(&parent_path, ie->path);
4539 if (err) {
4540 if (err->code != GOT_ERR_BAD_PATH)
4541 goto done;
4542 parent_path = strdup("/");
4543 if (parent_path == NULL) {
4544 err = got_error_from_errno("strdup");
4545 goto done;
4548 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4549 tree_path = strdup(parent_path);
4550 if (tree_path == NULL) {
4551 err = got_error_from_errno("strdup");
4552 goto done;
4554 } else {
4555 if (got_path_is_root_dir(parent_path)) {
4556 tree_path = strdup(a->worktree->path_prefix);
4557 if (tree_path == NULL) {
4558 err = got_error_from_errno("strdup");
4559 goto done;
4561 } else {
4562 if (asprintf(&tree_path, "%s/%s",
4563 a->worktree->path_prefix, parent_path) == -1) {
4564 err = got_error_from_errno("asprintf");
4565 goto done;
4570 err = got_object_id_by_path(&tree_id, a->repo,
4571 a->worktree->base_commit_id, tree_path);
4572 if (err) {
4573 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4574 (status == GOT_STATUS_ADD ||
4575 staged_status == GOT_STATUS_ADD)))
4576 goto done;
4577 } else {
4578 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4579 if (err)
4580 goto done;
4582 err = got_path_basename(&te_name, ie->path);
4583 if (err)
4584 goto done;
4586 te = got_object_tree_find_entry(tree, te_name);
4587 free(te_name);
4588 if (te == NULL && status != GOT_STATUS_ADD &&
4589 staged_status != GOT_STATUS_ADD) {
4590 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4591 goto done;
4595 switch (status) {
4596 case GOT_STATUS_ADD:
4597 if (a->patch_cb) {
4598 int choice = GOT_PATCH_CHOICE_NONE;
4599 err = (*a->patch_cb)(&choice, a->patch_arg,
4600 status, ie->path, NULL, 1, 1);
4601 if (err)
4602 goto done;
4603 if (choice != GOT_PATCH_CHOICE_YES)
4604 break;
4606 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4607 ie->path);
4608 if (err)
4609 goto done;
4610 got_fileindex_entry_remove(a->fileindex, ie);
4611 if (a->unlink_added_files) {
4612 if (asprintf(&ondisk_path, "%s/%s",
4613 got_worktree_get_root_path(a->worktree),
4614 relpath) == -1) {
4615 err = got_error_from_errno("asprintf");
4616 goto done;
4618 if (unlink(ondisk_path) == -1) {
4619 err = got_error_from_errno2("unlink",
4620 ondisk_path);
4621 break;
4624 break;
4625 case GOT_STATUS_DELETE:
4626 if (a->patch_cb) {
4627 int choice = GOT_PATCH_CHOICE_NONE;
4628 err = (*a->patch_cb)(&choice, a->patch_arg,
4629 status, ie->path, NULL, 1, 1);
4630 if (err)
4631 goto done;
4632 if (choice != GOT_PATCH_CHOICE_YES)
4633 break;
4635 /* fall through */
4636 case GOT_STATUS_MODIFY:
4637 case GOT_STATUS_MODE_CHANGE:
4638 case GOT_STATUS_CONFLICT:
4639 case GOT_STATUS_MISSING: {
4640 struct got_object_id id;
4641 if (staged_status == GOT_STATUS_ADD ||
4642 staged_status == GOT_STATUS_MODIFY) {
4643 memcpy(id.sha1, ie->staged_blob_sha1,
4644 SHA1_DIGEST_LENGTH);
4645 } else
4646 memcpy(id.sha1, ie->blob_sha1,
4647 SHA1_DIGEST_LENGTH);
4648 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4649 if (err)
4650 goto done;
4652 if (asprintf(&ondisk_path, "%s/%s",
4653 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4654 err = got_error_from_errno("asprintf");
4655 goto done;
4658 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4659 status == GOT_STATUS_CONFLICT)) {
4660 int is_bad_symlink = 0;
4661 err = create_patched_content(&path_content, 1, &id,
4662 ondisk_path, dirfd, de_name, ie->path, a->repo,
4663 a->patch_cb, a->patch_arg);
4664 if (err || path_content == NULL)
4665 break;
4666 if (te && S_ISLNK(te->mode)) {
4667 if (unlink(path_content) == -1) {
4668 err = got_error_from_errno2("unlink",
4669 path_content);
4670 break;
4672 err = install_symlink(&is_bad_symlink,
4673 a->worktree, ondisk_path, ie->path,
4674 blob, 0, 1, 0, 0, a->repo,
4675 a->progress_cb, a->progress_arg);
4676 } else {
4677 if (rename(path_content, ondisk_path) == -1) {
4678 err = got_error_from_errno3("rename",
4679 path_content, ondisk_path);
4680 goto done;
4683 } else {
4684 int is_bad_symlink = 0;
4685 if (te && S_ISLNK(te->mode)) {
4686 err = install_symlink(&is_bad_symlink,
4687 a->worktree, ondisk_path, ie->path,
4688 blob, 0, 1, 0, 0, a->repo,
4689 a->progress_cb, a->progress_arg);
4690 } else {
4691 err = install_blob(a->worktree, ondisk_path,
4692 ie->path,
4693 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4694 got_fileindex_perms_to_st(ie), blob,
4695 0, 1, 0, 0, a->repo,
4696 a->progress_cb, a->progress_arg);
4698 if (err)
4699 goto done;
4700 if (status == GOT_STATUS_DELETE ||
4701 status == GOT_STATUS_MODE_CHANGE) {
4702 err = got_fileindex_entry_update(ie,
4703 a->worktree->root_fd, relpath,
4704 blob->id.sha1,
4705 a->worktree->base_commit_id->sha1, 1);
4706 if (err)
4707 goto done;
4709 if (is_bad_symlink) {
4710 got_fileindex_entry_filetype_set(ie,
4711 GOT_FILEIDX_MODE_BAD_SYMLINK);
4714 break;
4716 default:
4717 break;
4719 done:
4720 free(ondisk_path);
4721 free(path_content);
4722 free(parent_path);
4723 free(tree_path);
4724 if (blob)
4725 got_object_blob_close(blob);
4726 if (tree)
4727 got_object_tree_close(tree);
4728 free(tree_id);
4729 return err;
4732 const struct got_error *
4733 got_worktree_revert(struct got_worktree *worktree,
4734 struct got_pathlist_head *paths,
4735 got_worktree_checkout_cb progress_cb, void *progress_arg,
4736 got_worktree_patch_cb patch_cb, void *patch_arg,
4737 struct got_repository *repo)
4739 struct got_fileindex *fileindex = NULL;
4740 char *fileindex_path = NULL;
4741 const struct got_error *err = NULL, *unlockerr = NULL;
4742 const struct got_error *sync_err = NULL;
4743 struct got_pathlist_entry *pe;
4744 struct revert_file_args rfa;
4746 err = lock_worktree(worktree, LOCK_EX);
4747 if (err)
4748 return err;
4750 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4751 if (err)
4752 goto done;
4754 rfa.worktree = worktree;
4755 rfa.fileindex = fileindex;
4756 rfa.progress_cb = progress_cb;
4757 rfa.progress_arg = progress_arg;
4758 rfa.patch_cb = patch_cb;
4759 rfa.patch_arg = patch_arg;
4760 rfa.repo = repo;
4761 rfa.unlink_added_files = 0;
4762 TAILQ_FOREACH(pe, paths, entry) {
4763 err = worktree_status(worktree, pe->path, fileindex, repo,
4764 revert_file, &rfa, NULL, NULL, 1, 0);
4765 if (err)
4766 break;
4768 sync_err = sync_fileindex(fileindex, fileindex_path);
4769 if (sync_err && err == NULL)
4770 err = sync_err;
4771 done:
4772 free(fileindex_path);
4773 if (fileindex)
4774 got_fileindex_free(fileindex);
4775 unlockerr = lock_worktree(worktree, LOCK_SH);
4776 if (unlockerr && err == NULL)
4777 err = unlockerr;
4778 return err;
4781 static void
4782 free_commitable(struct got_commitable *ct)
4784 free(ct->path);
4785 free(ct->in_repo_path);
4786 free(ct->ondisk_path);
4787 free(ct->blob_id);
4788 free(ct->base_blob_id);
4789 free(ct->staged_blob_id);
4790 free(ct->base_commit_id);
4791 free(ct);
4794 struct collect_commitables_arg {
4795 struct got_pathlist_head *commitable_paths;
4796 struct got_repository *repo;
4797 struct got_worktree *worktree;
4798 struct got_fileindex *fileindex;
4799 int have_staged_files;
4800 int allow_bad_symlinks;
4803 static const struct got_error *
4804 collect_commitables(void *arg, unsigned char status,
4805 unsigned char staged_status, const char *relpath,
4806 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4807 struct got_object_id *commit_id, int dirfd, const char *de_name)
4809 struct collect_commitables_arg *a = arg;
4810 const struct got_error *err = NULL;
4811 struct got_commitable *ct = NULL;
4812 struct got_pathlist_entry *new = NULL;
4813 char *parent_path = NULL, *path = NULL;
4814 struct stat sb;
4816 if (a->have_staged_files) {
4817 if (staged_status != GOT_STATUS_MODIFY &&
4818 staged_status != GOT_STATUS_ADD &&
4819 staged_status != GOT_STATUS_DELETE)
4820 return NULL;
4821 } else {
4822 if (status == GOT_STATUS_CONFLICT)
4823 return got_error(GOT_ERR_COMMIT_CONFLICT);
4825 if (status != GOT_STATUS_MODIFY &&
4826 status != GOT_STATUS_MODE_CHANGE &&
4827 status != GOT_STATUS_ADD &&
4828 status != GOT_STATUS_DELETE)
4829 return NULL;
4832 if (asprintf(&path, "/%s", relpath) == -1) {
4833 err = got_error_from_errno("asprintf");
4834 goto done;
4836 if (strcmp(path, "/") == 0) {
4837 parent_path = strdup("");
4838 if (parent_path == NULL)
4839 return got_error_from_errno("strdup");
4840 } else {
4841 err = got_path_dirname(&parent_path, path);
4842 if (err)
4843 return err;
4846 ct = calloc(1, sizeof(*ct));
4847 if (ct == NULL) {
4848 err = got_error_from_errno("calloc");
4849 goto done;
4852 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4853 relpath) == -1) {
4854 err = got_error_from_errno("asprintf");
4855 goto done;
4858 if (staged_status == GOT_STATUS_ADD ||
4859 staged_status == GOT_STATUS_MODIFY) {
4860 struct got_fileindex_entry *ie;
4861 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4862 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4863 case GOT_FILEIDX_MODE_REGULAR_FILE:
4864 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4865 ct->mode = S_IFREG;
4866 break;
4867 case GOT_FILEIDX_MODE_SYMLINK:
4868 ct->mode = S_IFLNK;
4869 break;
4870 default:
4871 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4872 goto done;
4874 ct->mode |= got_fileindex_entry_perms_get(ie);
4875 } else if (status != GOT_STATUS_DELETE &&
4876 staged_status != GOT_STATUS_DELETE) {
4877 if (dirfd != -1) {
4878 if (fstatat(dirfd, de_name, &sb,
4879 AT_SYMLINK_NOFOLLOW) == -1) {
4880 err = got_error_from_errno2("fstatat",
4881 ct->ondisk_path);
4882 goto done;
4884 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4885 err = got_error_from_errno2("lstat", ct->ondisk_path);
4886 goto done;
4888 ct->mode = sb.st_mode;
4891 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4892 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4893 relpath) == -1) {
4894 err = got_error_from_errno("asprintf");
4895 goto done;
4898 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4899 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4900 int is_bad_symlink;
4901 char target_path[PATH_MAX];
4902 ssize_t target_len;
4903 target_len = readlink(ct->ondisk_path, target_path,
4904 sizeof(target_path));
4905 if (target_len == -1) {
4906 err = got_error_from_errno2("readlink",
4907 ct->ondisk_path);
4908 goto done;
4910 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4911 target_len, ct->ondisk_path, a->worktree->root_path);
4912 if (err)
4913 goto done;
4914 if (is_bad_symlink) {
4915 err = got_error_path(ct->ondisk_path,
4916 GOT_ERR_BAD_SYMLINK);
4917 goto done;
4922 ct->status = status;
4923 ct->staged_status = staged_status;
4924 ct->blob_id = NULL; /* will be filled in when blob gets created */
4925 if (ct->status != GOT_STATUS_ADD &&
4926 ct->staged_status != GOT_STATUS_ADD) {
4927 ct->base_blob_id = got_object_id_dup(blob_id);
4928 if (ct->base_blob_id == NULL) {
4929 err = got_error_from_errno("got_object_id_dup");
4930 goto done;
4932 ct->base_commit_id = got_object_id_dup(commit_id);
4933 if (ct->base_commit_id == NULL) {
4934 err = got_error_from_errno("got_object_id_dup");
4935 goto done;
4938 if (ct->staged_status == GOT_STATUS_ADD ||
4939 ct->staged_status == GOT_STATUS_MODIFY) {
4940 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4941 if (ct->staged_blob_id == NULL) {
4942 err = got_error_from_errno("got_object_id_dup");
4943 goto done;
4946 ct->path = strdup(path);
4947 if (ct->path == NULL) {
4948 err = got_error_from_errno("strdup");
4949 goto done;
4951 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4952 done:
4953 if (ct && (err || new == NULL))
4954 free_commitable(ct);
4955 free(parent_path);
4956 free(path);
4957 return err;
4960 static const struct got_error *write_tree(struct got_object_id **, int *,
4961 struct got_tree_object *, const char *, struct got_pathlist_head *,
4962 got_worktree_status_cb status_cb, void *status_arg,
4963 struct got_repository *);
4965 static const struct got_error *
4966 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4967 struct got_tree_entry *te, const char *parent_path,
4968 struct got_pathlist_head *commitable_paths,
4969 got_worktree_status_cb status_cb, void *status_arg,
4970 struct got_repository *repo)
4972 const struct got_error *err = NULL;
4973 struct got_tree_object *subtree;
4974 char *subpath;
4976 if (asprintf(&subpath, "%s%s%s", parent_path,
4977 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4978 return got_error_from_errno("asprintf");
4980 err = got_object_open_as_tree(&subtree, repo, &te->id);
4981 if (err)
4982 return err;
4984 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4985 commitable_paths, status_cb, status_arg, repo);
4986 got_object_tree_close(subtree);
4987 free(subpath);
4988 return err;
4991 static const struct got_error *
4992 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4994 const struct got_error *err = NULL;
4995 char *ct_parent_path = NULL;
4997 *match = 0;
4999 if (strchr(ct->in_repo_path, '/') == NULL) {
5000 *match = got_path_is_root_dir(path);
5001 return NULL;
5004 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5005 if (err)
5006 return err;
5007 *match = (strcmp(path, ct_parent_path) == 0);
5008 free(ct_parent_path);
5009 return err;
5012 static mode_t
5013 get_ct_file_mode(struct got_commitable *ct)
5015 if (S_ISLNK(ct->mode))
5016 return S_IFLNK;
5018 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5021 static const struct got_error *
5022 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5023 struct got_tree_entry *te, struct got_commitable *ct)
5025 const struct got_error *err = NULL;
5027 *new_te = NULL;
5029 err = got_object_tree_entry_dup(new_te, te);
5030 if (err)
5031 goto done;
5033 (*new_te)->mode = get_ct_file_mode(ct);
5035 if (ct->staged_status == GOT_STATUS_MODIFY)
5036 memcpy(&(*new_te)->id, ct->staged_blob_id,
5037 sizeof((*new_te)->id));
5038 else
5039 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5040 done:
5041 if (err && *new_te) {
5042 free(*new_te);
5043 *new_te = NULL;
5045 return err;
5048 static const struct got_error *
5049 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5050 struct got_commitable *ct)
5052 const struct got_error *err = NULL;
5053 char *ct_name = NULL;
5055 *new_te = NULL;
5057 *new_te = calloc(1, sizeof(**new_te));
5058 if (*new_te == NULL)
5059 return got_error_from_errno("calloc");
5061 err = got_path_basename(&ct_name, ct->path);
5062 if (err)
5063 goto done;
5064 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5065 sizeof((*new_te)->name)) {
5066 err = got_error(GOT_ERR_NO_SPACE);
5067 goto done;
5070 (*new_te)->mode = get_ct_file_mode(ct);
5072 if (ct->staged_status == GOT_STATUS_ADD)
5073 memcpy(&(*new_te)->id, ct->staged_blob_id,
5074 sizeof((*new_te)->id));
5075 else
5076 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5077 done:
5078 free(ct_name);
5079 if (err && *new_te) {
5080 free(*new_te);
5081 *new_te = NULL;
5083 return err;
5086 static const struct got_error *
5087 insert_tree_entry(struct got_tree_entry *new_te,
5088 struct got_pathlist_head *paths)
5090 const struct got_error *err = NULL;
5091 struct got_pathlist_entry *new_pe;
5093 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5094 if (err)
5095 return err;
5096 if (new_pe == NULL)
5097 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5098 return NULL;
5101 static const struct got_error *
5102 report_ct_status(struct got_commitable *ct,
5103 got_worktree_status_cb status_cb, void *status_arg)
5105 const char *ct_path = ct->path;
5106 unsigned char status;
5108 if (status_cb == NULL) /* no commit progress output desired */
5109 return NULL;
5111 while (ct_path[0] == '/')
5112 ct_path++;
5114 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5115 status = ct->staged_status;
5116 else
5117 status = ct->status;
5119 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5120 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5123 static const struct got_error *
5124 match_modified_subtree(int *modified, struct got_tree_entry *te,
5125 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5127 const struct got_error *err = NULL;
5128 struct got_pathlist_entry *pe;
5129 char *te_path;
5131 *modified = 0;
5133 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5134 got_path_is_root_dir(base_tree_path) ? "" : "/",
5135 te->name) == -1)
5136 return got_error_from_errno("asprintf");
5138 TAILQ_FOREACH(pe, commitable_paths, entry) {
5139 struct got_commitable *ct = pe->data;
5140 *modified = got_path_is_child(ct->in_repo_path, te_path,
5141 strlen(te_path));
5142 if (*modified)
5143 break;
5146 free(te_path);
5147 return err;
5150 static const struct got_error *
5151 match_deleted_or_modified_ct(struct got_commitable **ctp,
5152 struct got_tree_entry *te, const char *base_tree_path,
5153 struct got_pathlist_head *commitable_paths)
5155 const struct got_error *err = NULL;
5156 struct got_pathlist_entry *pe;
5158 *ctp = NULL;
5160 TAILQ_FOREACH(pe, commitable_paths, entry) {
5161 struct got_commitable *ct = pe->data;
5162 char *ct_name = NULL;
5163 int path_matches;
5165 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5166 if (ct->status != GOT_STATUS_MODIFY &&
5167 ct->status != GOT_STATUS_MODE_CHANGE &&
5168 ct->status != GOT_STATUS_DELETE)
5169 continue;
5170 } else {
5171 if (ct->staged_status != GOT_STATUS_MODIFY &&
5172 ct->staged_status != GOT_STATUS_DELETE)
5173 continue;
5176 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5177 continue;
5179 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5180 if (err)
5181 return err;
5182 if (!path_matches)
5183 continue;
5185 err = got_path_basename(&ct_name, pe->path);
5186 if (err)
5187 return err;
5189 if (strcmp(te->name, ct_name) != 0) {
5190 free(ct_name);
5191 continue;
5193 free(ct_name);
5195 *ctp = ct;
5196 break;
5199 return err;
5202 static const struct got_error *
5203 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5204 const char *child_path, const char *path_base_tree,
5205 struct got_pathlist_head *commitable_paths,
5206 got_worktree_status_cb status_cb, void *status_arg,
5207 struct got_repository *repo)
5209 const struct got_error *err = NULL;
5210 struct got_tree_entry *new_te;
5211 char *subtree_path;
5212 struct got_object_id *id = NULL;
5213 int nentries;
5215 *new_tep = NULL;
5217 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5218 got_path_is_root_dir(path_base_tree) ? "" : "/",
5219 child_path) == -1)
5220 return got_error_from_errno("asprintf");
5222 new_te = calloc(1, sizeof(*new_te));
5223 if (new_te == NULL)
5224 return got_error_from_errno("calloc");
5225 new_te->mode = S_IFDIR;
5227 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5228 sizeof(new_te->name)) {
5229 err = got_error(GOT_ERR_NO_SPACE);
5230 goto done;
5232 err = write_tree(&id, &nentries, NULL, subtree_path,
5233 commitable_paths, status_cb, status_arg, repo);
5234 if (err) {
5235 free(new_te);
5236 goto done;
5238 memcpy(&new_te->id, id, sizeof(new_te->id));
5239 done:
5240 free(id);
5241 free(subtree_path);
5242 if (err == NULL)
5243 *new_tep = new_te;
5244 return err;
5247 static const struct got_error *
5248 write_tree(struct got_object_id **new_tree_id, int *nentries,
5249 struct got_tree_object *base_tree, const char *path_base_tree,
5250 struct got_pathlist_head *commitable_paths,
5251 got_worktree_status_cb status_cb, void *status_arg,
5252 struct got_repository *repo)
5254 const struct got_error *err = NULL;
5255 struct got_pathlist_head paths;
5256 struct got_tree_entry *te, *new_te = NULL;
5257 struct got_pathlist_entry *pe;
5259 TAILQ_INIT(&paths);
5260 *nentries = 0;
5262 /* Insert, and recurse into, newly added entries first. */
5263 TAILQ_FOREACH(pe, commitable_paths, entry) {
5264 struct got_commitable *ct = pe->data;
5265 char *child_path = NULL, *slash;
5267 if ((ct->status != GOT_STATUS_ADD &&
5268 ct->staged_status != GOT_STATUS_ADD) ||
5269 (ct->flags & GOT_COMMITABLE_ADDED))
5270 continue;
5272 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5273 strlen(path_base_tree)))
5274 continue;
5276 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5277 ct->in_repo_path);
5278 if (err)
5279 goto done;
5281 slash = strchr(child_path, '/');
5282 if (slash == NULL) {
5283 err = alloc_added_blob_tree_entry(&new_te, ct);
5284 if (err)
5285 goto done;
5286 err = report_ct_status(ct, status_cb, status_arg);
5287 if (err)
5288 goto done;
5289 ct->flags |= GOT_COMMITABLE_ADDED;
5290 err = insert_tree_entry(new_te, &paths);
5291 if (err)
5292 goto done;
5293 (*nentries)++;
5294 } else {
5295 *slash = '\0'; /* trim trailing path components */
5296 if (base_tree == NULL ||
5297 got_object_tree_find_entry(base_tree, child_path)
5298 == NULL) {
5299 err = make_subtree_for_added_blob(&new_te,
5300 child_path, path_base_tree,
5301 commitable_paths, status_cb, status_arg,
5302 repo);
5303 if (err)
5304 goto done;
5305 err = insert_tree_entry(new_te, &paths);
5306 if (err)
5307 goto done;
5308 (*nentries)++;
5313 if (base_tree) {
5314 int i, nbase_entries;
5315 /* Handle modified and deleted entries. */
5316 nbase_entries = got_object_tree_get_nentries(base_tree);
5317 for (i = 0; i < nbase_entries; i++) {
5318 struct got_commitable *ct = NULL;
5320 te = got_object_tree_get_entry(base_tree, i);
5321 if (got_object_tree_entry_is_submodule(te)) {
5322 /* Entry is a submodule; just copy it. */
5323 err = got_object_tree_entry_dup(&new_te, te);
5324 if (err)
5325 goto done;
5326 err = insert_tree_entry(new_te, &paths);
5327 if (err)
5328 goto done;
5329 (*nentries)++;
5330 continue;
5333 if (S_ISDIR(te->mode)) {
5334 int modified;
5335 err = got_object_tree_entry_dup(&new_te, te);
5336 if (err)
5337 goto done;
5338 err = match_modified_subtree(&modified, te,
5339 path_base_tree, commitable_paths);
5340 if (err)
5341 goto done;
5342 /* Avoid recursion into unmodified subtrees. */
5343 if (modified) {
5344 struct got_object_id *new_id;
5345 int nsubentries;
5346 err = write_subtree(&new_id,
5347 &nsubentries, te,
5348 path_base_tree, commitable_paths,
5349 status_cb, status_arg, repo);
5350 if (err)
5351 goto done;
5352 if (nsubentries == 0) {
5353 /* All entries were deleted. */
5354 free(new_id);
5355 continue;
5357 memcpy(&new_te->id, new_id,
5358 sizeof(new_te->id));
5359 free(new_id);
5361 err = insert_tree_entry(new_te, &paths);
5362 if (err)
5363 goto done;
5364 (*nentries)++;
5365 continue;
5368 err = match_deleted_or_modified_ct(&ct, te,
5369 path_base_tree, commitable_paths);
5370 if (err)
5371 goto done;
5372 if (ct) {
5373 /* NB: Deleted entries get dropped here. */
5374 if (ct->status == GOT_STATUS_MODIFY ||
5375 ct->status == GOT_STATUS_MODE_CHANGE ||
5376 ct->staged_status == GOT_STATUS_MODIFY) {
5377 err = alloc_modified_blob_tree_entry(
5378 &new_te, te, ct);
5379 if (err)
5380 goto done;
5381 err = insert_tree_entry(new_te, &paths);
5382 if (err)
5383 goto done;
5384 (*nentries)++;
5386 err = report_ct_status(ct, status_cb,
5387 status_arg);
5388 if (err)
5389 goto done;
5390 } else {
5391 /* Entry is unchanged; just copy it. */
5392 err = got_object_tree_entry_dup(&new_te, te);
5393 if (err)
5394 goto done;
5395 err = insert_tree_entry(new_te, &paths);
5396 if (err)
5397 goto done;
5398 (*nentries)++;
5403 /* Write new list of entries; deleted entries have been dropped. */
5404 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5405 done:
5406 got_pathlist_free(&paths);
5407 return err;
5410 static const struct got_error *
5411 update_fileindex_after_commit(struct got_worktree *worktree,
5412 struct got_pathlist_head *commitable_paths,
5413 struct got_object_id *new_base_commit_id,
5414 struct got_fileindex *fileindex, int have_staged_files)
5416 const struct got_error *err = NULL;
5417 struct got_pathlist_entry *pe;
5418 char *relpath = NULL;
5420 TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 struct got_fileindex_entry *ie;
5422 struct got_commitable *ct = pe->data;
5424 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5426 err = got_path_skip_common_ancestor(&relpath,
5427 worktree->root_path, ct->ondisk_path);
5428 if (err)
5429 goto done;
5431 if (ie) {
5432 if (ct->status == GOT_STATUS_DELETE ||
5433 ct->staged_status == GOT_STATUS_DELETE) {
5434 got_fileindex_entry_remove(fileindex, ie);
5435 } else if (ct->staged_status == GOT_STATUS_ADD ||
5436 ct->staged_status == GOT_STATUS_MODIFY) {
5437 got_fileindex_entry_stage_set(ie,
5438 GOT_FILEIDX_STAGE_NONE);
5439 got_fileindex_entry_staged_filetype_set(ie, 0);
5441 err = got_fileindex_entry_update(ie,
5442 worktree->root_fd, relpath,
5443 ct->staged_blob_id->sha1,
5444 new_base_commit_id->sha1,
5445 !have_staged_files);
5446 } else
5447 err = got_fileindex_entry_update(ie,
5448 worktree->root_fd, relpath,
5449 ct->blob_id->sha1,
5450 new_base_commit_id->sha1,
5451 !have_staged_files);
5452 } else {
5453 err = got_fileindex_entry_alloc(&ie, pe->path);
5454 if (err)
5455 goto done;
5456 err = got_fileindex_entry_update(ie,
5457 worktree->root_fd, relpath, ct->blob_id->sha1,
5458 new_base_commit_id->sha1, 1);
5459 if (err) {
5460 got_fileindex_entry_free(ie);
5461 goto done;
5463 err = got_fileindex_entry_add(fileindex, ie);
5464 if (err) {
5465 got_fileindex_entry_free(ie);
5466 goto done;
5469 free(relpath);
5470 relpath = NULL;
5472 done:
5473 free(relpath);
5474 return err;
5478 static const struct got_error *
5479 check_out_of_date(const char *in_repo_path, unsigned char status,
5480 unsigned char staged_status, struct got_object_id *base_blob_id,
5481 struct got_object_id *base_commit_id,
5482 struct got_object_id *head_commit_id, struct got_repository *repo,
5483 int ood_errcode)
5485 const struct got_error *err = NULL;
5486 struct got_object_id *id = NULL;
5488 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5489 /* Trivial case: base commit == head commit */
5490 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5491 return NULL;
5493 * Ensure file content which local changes were based
5494 * on matches file content in the branch head.
5496 err = got_object_id_by_path(&id, repo, head_commit_id,
5497 in_repo_path);
5498 if (err) {
5499 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5500 err = got_error(ood_errcode);
5501 goto done;
5502 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5503 err = got_error(ood_errcode);
5504 } else {
5505 /* Require that added files don't exist in the branch head. */
5506 err = got_object_id_by_path(&id, repo, head_commit_id,
5507 in_repo_path);
5508 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5509 goto done;
5510 err = id ? got_error(ood_errcode) : NULL;
5512 done:
5513 free(id);
5514 return err;
5517 const struct got_error *
5518 commit_worktree(struct got_object_id **new_commit_id,
5519 struct got_pathlist_head *commitable_paths,
5520 struct got_object_id *head_commit_id,
5521 struct got_object_id *parent_id2,
5522 struct got_worktree *worktree,
5523 const char *author, const char *committer,
5524 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5525 got_worktree_status_cb status_cb, void *status_arg,
5526 struct got_repository *repo)
5528 const struct got_error *err = NULL, *unlockerr = NULL;
5529 struct got_pathlist_entry *pe;
5530 const char *head_ref_name = NULL;
5531 struct got_commit_object *head_commit = NULL;
5532 struct got_reference *head_ref2 = NULL;
5533 struct got_object_id *head_commit_id2 = NULL;
5534 struct got_tree_object *head_tree = NULL;
5535 struct got_object_id *new_tree_id = NULL;
5536 int nentries, nparents = 0;
5537 struct got_object_id_queue parent_ids;
5538 struct got_object_qid *pid = NULL;
5539 char *logmsg = NULL;
5541 *new_commit_id = NULL;
5543 STAILQ_INIT(&parent_ids);
5545 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5546 if (err)
5547 goto done;
5549 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5550 if (err)
5551 goto done;
5553 if (commit_msg_cb != NULL) {
5554 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5555 if (err)
5556 goto done;
5559 if (logmsg == NULL || strlen(logmsg) == 0) {
5560 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5561 goto done;
5564 /* Create blobs from added and modified files and record their IDs. */
5565 TAILQ_FOREACH(pe, commitable_paths, entry) {
5566 struct got_commitable *ct = pe->data;
5567 char *ondisk_path;
5569 /* Blobs for staged files already exist. */
5570 if (ct->staged_status == GOT_STATUS_ADD ||
5571 ct->staged_status == GOT_STATUS_MODIFY)
5572 continue;
5574 if (ct->status != GOT_STATUS_ADD &&
5575 ct->status != GOT_STATUS_MODIFY &&
5576 ct->status != GOT_STATUS_MODE_CHANGE)
5577 continue;
5579 if (asprintf(&ondisk_path, "%s/%s",
5580 worktree->root_path, pe->path) == -1) {
5581 err = got_error_from_errno("asprintf");
5582 goto done;
5584 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5585 free(ondisk_path);
5586 if (err)
5587 goto done;
5590 /* Recursively write new tree objects. */
5591 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5592 commitable_paths, status_cb, status_arg, repo);
5593 if (err)
5594 goto done;
5596 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5597 if (err)
5598 goto done;
5599 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5600 nparents++;
5601 if (parent_id2) {
5602 err = got_object_qid_alloc(&pid, parent_id2);
5603 if (err)
5604 goto done;
5605 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5606 nparents++;
5608 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5609 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5610 if (logmsg != NULL)
5611 free(logmsg);
5612 if (err)
5613 goto done;
5615 /* Check if a concurrent commit to our branch has occurred. */
5616 head_ref_name = got_worktree_get_head_ref_name(worktree);
5617 if (head_ref_name == NULL) {
5618 err = got_error_from_errno("got_worktree_get_head_ref_name");
5619 goto done;
5621 /* Lock the reference here to prevent concurrent modification. */
5622 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5623 if (err)
5624 goto done;
5625 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5626 if (err)
5627 goto done;
5628 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5629 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5630 goto done;
5632 /* Update branch head in repository. */
5633 err = got_ref_change_ref(head_ref2, *new_commit_id);
5634 if (err)
5635 goto done;
5636 err = got_ref_write(head_ref2, repo);
5637 if (err)
5638 goto done;
5640 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5641 if (err)
5642 goto done;
5644 err = ref_base_commit(worktree, repo);
5645 if (err)
5646 goto done;
5647 done:
5648 got_object_id_queue_free(&parent_ids);
5649 if (head_tree)
5650 got_object_tree_close(head_tree);
5651 if (head_commit)
5652 got_object_commit_close(head_commit);
5653 free(head_commit_id2);
5654 if (head_ref2) {
5655 unlockerr = got_ref_unlock(head_ref2);
5656 if (unlockerr && err == NULL)
5657 err = unlockerr;
5658 got_ref_close(head_ref2);
5660 return err;
5663 static const struct got_error *
5664 check_path_is_commitable(const char *path,
5665 struct got_pathlist_head *commitable_paths)
5667 struct got_pathlist_entry *cpe = NULL;
5668 size_t path_len = strlen(path);
5670 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5671 struct got_commitable *ct = cpe->data;
5672 const char *ct_path = ct->path;
5674 while (ct_path[0] == '/')
5675 ct_path++;
5677 if (strcmp(path, ct_path) == 0 ||
5678 got_path_is_child(ct_path, path, path_len))
5679 break;
5682 if (cpe == NULL)
5683 return got_error_path(path, GOT_ERR_BAD_PATH);
5685 return NULL;
5688 static const struct got_error *
5689 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5691 int *have_staged_files = arg;
5693 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5694 *have_staged_files = 1;
5695 return got_error(GOT_ERR_CANCELLED);
5698 return NULL;
5701 static const struct got_error *
5702 check_non_staged_files(struct got_fileindex *fileindex,
5703 struct got_pathlist_head *paths)
5705 struct got_pathlist_entry *pe;
5706 struct got_fileindex_entry *ie;
5708 TAILQ_FOREACH(pe, paths, entry) {
5709 if (pe->path[0] == '\0')
5710 continue;
5711 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5712 if (ie == NULL)
5713 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5714 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5715 return got_error_path(pe->path,
5716 GOT_ERR_FILE_NOT_STAGED);
5719 return NULL;
5722 const struct got_error *
5723 got_worktree_commit(struct got_object_id **new_commit_id,
5724 struct got_worktree *worktree, struct got_pathlist_head *paths,
5725 const char *author, const char *committer, int allow_bad_symlinks,
5726 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5727 got_worktree_status_cb status_cb, void *status_arg,
5728 struct got_repository *repo)
5730 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5731 struct got_fileindex *fileindex = NULL;
5732 char *fileindex_path = NULL;
5733 struct got_pathlist_head commitable_paths;
5734 struct collect_commitables_arg cc_arg;
5735 struct got_pathlist_entry *pe;
5736 struct got_reference *head_ref = NULL;
5737 struct got_object_id *head_commit_id = NULL;
5738 int have_staged_files = 0;
5740 *new_commit_id = NULL;
5742 TAILQ_INIT(&commitable_paths);
5744 err = lock_worktree(worktree, LOCK_EX);
5745 if (err)
5746 goto done;
5748 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5749 if (err)
5750 goto done;
5752 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5753 if (err)
5754 goto done;
5756 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5757 if (err)
5758 goto done;
5760 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5761 &have_staged_files);
5762 if (err && err->code != GOT_ERR_CANCELLED)
5763 goto done;
5764 if (have_staged_files) {
5765 err = check_non_staged_files(fileindex, paths);
5766 if (err)
5767 goto done;
5770 cc_arg.commitable_paths = &commitable_paths;
5771 cc_arg.worktree = worktree;
5772 cc_arg.fileindex = fileindex;
5773 cc_arg.repo = repo;
5774 cc_arg.have_staged_files = have_staged_files;
5775 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5776 TAILQ_FOREACH(pe, paths, entry) {
5777 err = worktree_status(worktree, pe->path, fileindex, repo,
5778 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5779 if (err)
5780 goto done;
5783 if (TAILQ_EMPTY(&commitable_paths)) {
5784 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5785 goto done;
5788 TAILQ_FOREACH(pe, paths, entry) {
5789 err = check_path_is_commitable(pe->path, &commitable_paths);
5790 if (err)
5791 goto done;
5794 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5795 struct got_commitable *ct = pe->data;
5796 const char *ct_path = ct->in_repo_path;
5798 while (ct_path[0] == '/')
5799 ct_path++;
5800 err = check_out_of_date(ct_path, ct->status,
5801 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5802 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5803 if (err)
5804 goto done;
5808 err = commit_worktree(new_commit_id, &commitable_paths,
5809 head_commit_id, NULL, worktree, author, committer,
5810 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5811 if (err)
5812 goto done;
5814 err = update_fileindex_after_commit(worktree, &commitable_paths,
5815 *new_commit_id, fileindex, have_staged_files);
5816 sync_err = sync_fileindex(fileindex, fileindex_path);
5817 if (sync_err && err == NULL)
5818 err = sync_err;
5819 done:
5820 if (fileindex)
5821 got_fileindex_free(fileindex);
5822 free(fileindex_path);
5823 unlockerr = lock_worktree(worktree, LOCK_SH);
5824 if (unlockerr && err == NULL)
5825 err = unlockerr;
5826 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5827 struct got_commitable *ct = pe->data;
5828 free_commitable(ct);
5830 got_pathlist_free(&commitable_paths);
5831 return err;
5834 const char *
5835 got_commitable_get_path(struct got_commitable *ct)
5837 return ct->path;
5840 unsigned int
5841 got_commitable_get_status(struct got_commitable *ct)
5843 return ct->status;
5846 struct check_rebase_ok_arg {
5847 struct got_worktree *worktree;
5848 struct got_repository *repo;
5851 static const struct got_error *
5852 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5854 const struct got_error *err = NULL;
5855 struct check_rebase_ok_arg *a = arg;
5856 unsigned char status;
5857 struct stat sb;
5858 char *ondisk_path;
5860 /* Reject rebase of a work tree with mixed base commits. */
5861 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5862 SHA1_DIGEST_LENGTH))
5863 return got_error(GOT_ERR_MIXED_COMMITS);
5865 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5866 == -1)
5867 return got_error_from_errno("asprintf");
5869 /* Reject rebase of a work tree with modified or staged files. */
5870 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5871 free(ondisk_path);
5872 if (err)
5873 return err;
5875 if (status != GOT_STATUS_NO_CHANGE)
5876 return got_error(GOT_ERR_MODIFIED);
5877 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5878 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5880 return NULL;
5883 const struct got_error *
5884 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5885 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5886 struct got_worktree *worktree, struct got_reference *branch,
5887 struct got_repository *repo)
5889 const struct got_error *err = NULL;
5890 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5891 char *branch_ref_name = NULL;
5892 char *fileindex_path = NULL;
5893 struct check_rebase_ok_arg ok_arg;
5894 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5895 struct got_object_id *wt_branch_tip = NULL;
5897 *new_base_branch_ref = NULL;
5898 *tmp_branch = NULL;
5899 *fileindex = NULL;
5901 err = lock_worktree(worktree, LOCK_EX);
5902 if (err)
5903 return err;
5905 err = open_fileindex(fileindex, &fileindex_path, worktree);
5906 if (err)
5907 goto done;
5909 ok_arg.worktree = worktree;
5910 ok_arg.repo = repo;
5911 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5912 &ok_arg);
5913 if (err)
5914 goto done;
5916 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5917 if (err)
5918 goto done;
5920 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5921 if (err)
5922 goto done;
5924 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5925 if (err)
5926 goto done;
5928 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5929 0);
5930 if (err)
5931 goto done;
5933 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5934 if (err)
5935 goto done;
5936 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5937 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5938 goto done;
5941 err = got_ref_alloc_symref(new_base_branch_ref,
5942 new_base_branch_ref_name, wt_branch);
5943 if (err)
5944 goto done;
5945 err = got_ref_write(*new_base_branch_ref, repo);
5946 if (err)
5947 goto done;
5949 /* TODO Lock original branch's ref while rebasing? */
5951 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5952 if (err)
5953 goto done;
5955 err = got_ref_write(branch_ref, repo);
5956 if (err)
5957 goto done;
5959 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5960 worktree->base_commit_id);
5961 if (err)
5962 goto done;
5963 err = got_ref_write(*tmp_branch, repo);
5964 if (err)
5965 goto done;
5967 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5968 if (err)
5969 goto done;
5970 done:
5971 free(fileindex_path);
5972 free(tmp_branch_name);
5973 free(new_base_branch_ref_name);
5974 free(branch_ref_name);
5975 if (branch_ref)
5976 got_ref_close(branch_ref);
5977 if (wt_branch)
5978 got_ref_close(wt_branch);
5979 free(wt_branch_tip);
5980 if (err) {
5981 if (*new_base_branch_ref) {
5982 got_ref_close(*new_base_branch_ref);
5983 *new_base_branch_ref = NULL;
5985 if (*tmp_branch) {
5986 got_ref_close(*tmp_branch);
5987 *tmp_branch = NULL;
5989 if (*fileindex) {
5990 got_fileindex_free(*fileindex);
5991 *fileindex = NULL;
5993 lock_worktree(worktree, LOCK_SH);
5995 return err;
5998 const struct got_error *
5999 got_worktree_rebase_continue(struct got_object_id **commit_id,
6000 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6001 struct got_reference **branch, struct got_fileindex **fileindex,
6002 struct got_worktree *worktree, struct got_repository *repo)
6004 const struct got_error *err;
6005 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6006 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6007 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6008 char *fileindex_path = NULL;
6009 int have_staged_files = 0;
6011 *commit_id = NULL;
6012 *new_base_branch = NULL;
6013 *tmp_branch = NULL;
6014 *branch = NULL;
6015 *fileindex = NULL;
6017 err = lock_worktree(worktree, LOCK_EX);
6018 if (err)
6019 return err;
6021 err = open_fileindex(fileindex, &fileindex_path, worktree);
6022 if (err)
6023 goto done;
6025 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6026 &have_staged_files);
6027 if (err && err->code != GOT_ERR_CANCELLED)
6028 goto done;
6029 if (have_staged_files) {
6030 err = got_error(GOT_ERR_STAGED_PATHS);
6031 goto done;
6034 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6035 if (err)
6036 goto done;
6038 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6039 if (err)
6040 goto done;
6042 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6043 if (err)
6044 goto done;
6046 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6047 if (err)
6048 goto done;
6050 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6051 if (err)
6052 goto done;
6054 err = got_ref_open(branch, repo,
6055 got_ref_get_symref_target(branch_ref), 0);
6056 if (err)
6057 goto done;
6059 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6060 if (err)
6061 goto done;
6063 err = got_ref_resolve(commit_id, repo, commit_ref);
6064 if (err)
6065 goto done;
6067 err = got_ref_open(new_base_branch, repo,
6068 new_base_branch_ref_name, 0);
6069 if (err)
6070 goto done;
6072 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6073 if (err)
6074 goto done;
6075 done:
6076 free(commit_ref_name);
6077 free(branch_ref_name);
6078 free(fileindex_path);
6079 if (commit_ref)
6080 got_ref_close(commit_ref);
6081 if (branch_ref)
6082 got_ref_close(branch_ref);
6083 if (err) {
6084 free(*commit_id);
6085 *commit_id = NULL;
6086 if (*tmp_branch) {
6087 got_ref_close(*tmp_branch);
6088 *tmp_branch = NULL;
6090 if (*new_base_branch) {
6091 got_ref_close(*new_base_branch);
6092 *new_base_branch = NULL;
6094 if (*branch) {
6095 got_ref_close(*branch);
6096 *branch = NULL;
6098 if (*fileindex) {
6099 got_fileindex_free(*fileindex);
6100 *fileindex = NULL;
6102 lock_worktree(worktree, LOCK_SH);
6104 return err;
6107 const struct got_error *
6108 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6110 const struct got_error *err;
6111 char *tmp_branch_name = NULL;
6113 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6114 if (err)
6115 return err;
6117 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6118 free(tmp_branch_name);
6119 return NULL;
6122 static const struct got_error *
6123 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6124 char **logmsg, void *arg)
6126 *logmsg = arg;
6127 return NULL;
6130 static const struct got_error *
6131 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6132 const char *path, struct got_object_id *blob_id,
6133 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6134 int dirfd, const char *de_name)
6136 return NULL;
6139 struct collect_merged_paths_arg {
6140 got_worktree_checkout_cb progress_cb;
6141 void *progress_arg;
6142 struct got_pathlist_head *merged_paths;
6145 static const struct got_error *
6146 collect_merged_paths(void *arg, unsigned char status, const char *path)
6148 const struct got_error *err;
6149 struct collect_merged_paths_arg *a = arg;
6150 char *p;
6151 struct got_pathlist_entry *new;
6153 err = (*a->progress_cb)(a->progress_arg, status, path);
6154 if (err)
6155 return err;
6157 if (status != GOT_STATUS_MERGE &&
6158 status != GOT_STATUS_ADD &&
6159 status != GOT_STATUS_DELETE &&
6160 status != GOT_STATUS_CONFLICT)
6161 return NULL;
6163 p = strdup(path);
6164 if (p == NULL)
6165 return got_error_from_errno("strdup");
6167 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6168 if (err || new == NULL)
6169 free(p);
6170 return err;
6173 void
6174 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6176 struct got_pathlist_entry *pe;
6178 TAILQ_FOREACH(pe, merged_paths, entry)
6179 free((char *)pe->path);
6181 got_pathlist_free(merged_paths);
6184 static const struct got_error *
6185 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6186 int is_rebase, struct got_repository *repo)
6188 const struct got_error *err;
6189 struct got_reference *commit_ref = NULL;
6191 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6192 if (err) {
6193 if (err->code != GOT_ERR_NOT_REF)
6194 goto done;
6195 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6196 if (err)
6197 goto done;
6198 err = got_ref_write(commit_ref, repo);
6199 if (err)
6200 goto done;
6201 } else if (is_rebase) {
6202 struct got_object_id *stored_id;
6203 int cmp;
6205 err = got_ref_resolve(&stored_id, repo, commit_ref);
6206 if (err)
6207 goto done;
6208 cmp = got_object_id_cmp(commit_id, stored_id);
6209 free(stored_id);
6210 if (cmp != 0) {
6211 err = got_error(GOT_ERR_REBASE_COMMITID);
6212 goto done;
6215 done:
6216 if (commit_ref)
6217 got_ref_close(commit_ref);
6218 return err;
6221 static const struct got_error *
6222 rebase_merge_files(struct got_pathlist_head *merged_paths,
6223 const char *commit_ref_name, struct got_worktree *worktree,
6224 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6225 struct got_object_id *commit_id, struct got_repository *repo,
6226 got_worktree_checkout_cb progress_cb, void *progress_arg,
6227 got_cancel_cb cancel_cb, void *cancel_arg)
6229 const struct got_error *err;
6230 struct got_reference *commit_ref = NULL;
6231 struct collect_merged_paths_arg cmp_arg;
6232 char *fileindex_path;
6234 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6236 err = get_fileindex_path(&fileindex_path, worktree);
6237 if (err)
6238 return err;
6240 cmp_arg.progress_cb = progress_cb;
6241 cmp_arg.progress_arg = progress_arg;
6242 cmp_arg.merged_paths = merged_paths;
6243 err = merge_files(worktree, fileindex, fileindex_path,
6244 parent_commit_id, commit_id, repo, collect_merged_paths,
6245 &cmp_arg, cancel_cb, cancel_arg);
6246 if (commit_ref)
6247 got_ref_close(commit_ref);
6248 return err;
6251 const struct got_error *
6252 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6253 struct got_worktree *worktree, struct got_fileindex *fileindex,
6254 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6255 struct got_repository *repo,
6256 got_worktree_checkout_cb progress_cb, void *progress_arg,
6257 got_cancel_cb cancel_cb, void *cancel_arg)
6259 const struct got_error *err;
6260 char *commit_ref_name;
6262 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6263 if (err)
6264 return err;
6266 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6267 if (err)
6268 goto done;
6270 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6271 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6272 progress_arg, cancel_cb, cancel_arg);
6273 done:
6274 free(commit_ref_name);
6275 return err;
6278 const struct got_error *
6279 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6280 struct got_worktree *worktree, struct got_fileindex *fileindex,
6281 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6282 struct got_repository *repo,
6283 got_worktree_checkout_cb progress_cb, void *progress_arg,
6284 got_cancel_cb cancel_cb, void *cancel_arg)
6286 const struct got_error *err;
6287 char *commit_ref_name;
6289 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6290 if (err)
6291 return err;
6293 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6294 if (err)
6295 goto done;
6297 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6298 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6299 progress_arg, cancel_cb, cancel_arg);
6300 done:
6301 free(commit_ref_name);
6302 return err;
6305 static const struct got_error *
6306 rebase_commit(struct got_object_id **new_commit_id,
6307 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6308 struct got_worktree *worktree, struct got_fileindex *fileindex,
6309 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6310 const char *new_logmsg, struct got_repository *repo)
6312 const struct got_error *err, *sync_err;
6313 struct got_pathlist_head commitable_paths;
6314 struct collect_commitables_arg cc_arg;
6315 char *fileindex_path = NULL;
6316 struct got_reference *head_ref = NULL;
6317 struct got_object_id *head_commit_id = NULL;
6318 char *logmsg = NULL;
6320 TAILQ_INIT(&commitable_paths);
6321 *new_commit_id = NULL;
6323 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6325 err = get_fileindex_path(&fileindex_path, worktree);
6326 if (err)
6327 return err;
6329 cc_arg.commitable_paths = &commitable_paths;
6330 cc_arg.worktree = worktree;
6331 cc_arg.repo = repo;
6332 cc_arg.have_staged_files = 0;
6334 * If possible get the status of individual files directly to
6335 * avoid crawling the entire work tree once per rebased commit.
6337 * Ideally, merged_paths would contain a list of commitables
6338 * we could use so we could skip worktree_status() entirely.
6339 * However, we would then need carefully keep track of cumulative
6340 * effects of operations such as file additions and deletions
6341 * in 'got histedit -f' (folding multiple commits into one),
6342 * and this extra complexity is not really worth it.
6344 if (merged_paths) {
6345 struct got_pathlist_entry *pe;
6346 TAILQ_FOREACH(pe, merged_paths, entry) {
6347 err = worktree_status(worktree, pe->path, fileindex,
6348 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6349 0);
6350 if (err)
6351 goto done;
6353 } else {
6354 err = worktree_status(worktree, "", fileindex, repo,
6355 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6356 if (err)
6357 goto done;
6360 if (TAILQ_EMPTY(&commitable_paths)) {
6361 /* No-op change; commit will be elided. */
6362 err = got_ref_delete(commit_ref, repo);
6363 if (err)
6364 goto done;
6365 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6366 goto done;
6369 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6370 if (err)
6371 goto done;
6373 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6374 if (err)
6375 goto done;
6377 if (new_logmsg) {
6378 logmsg = strdup(new_logmsg);
6379 if (logmsg == NULL) {
6380 err = got_error_from_errno("strdup");
6381 goto done;
6383 } else {
6384 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6385 if (err)
6386 goto done;
6389 /* NB: commit_worktree will call free(logmsg) */
6390 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6391 NULL, worktree, got_object_commit_get_author(orig_commit),
6392 got_object_commit_get_committer(orig_commit),
6393 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6394 if (err)
6395 goto done;
6397 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6398 if (err)
6399 goto done;
6401 err = got_ref_delete(commit_ref, repo);
6402 if (err)
6403 goto done;
6405 err = update_fileindex_after_commit(worktree, &commitable_paths,
6406 *new_commit_id, fileindex, 0);
6407 sync_err = sync_fileindex(fileindex, fileindex_path);
6408 if (sync_err && err == NULL)
6409 err = sync_err;
6410 done:
6411 free(fileindex_path);
6412 free(head_commit_id);
6413 if (head_ref)
6414 got_ref_close(head_ref);
6415 if (err) {
6416 free(*new_commit_id);
6417 *new_commit_id = NULL;
6419 return err;
6422 const struct got_error *
6423 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6424 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6425 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6426 struct got_commit_object *orig_commit,
6427 struct got_object_id *orig_commit_id, struct got_repository *repo)
6429 const struct got_error *err;
6430 char *commit_ref_name;
6431 struct got_reference *commit_ref = NULL;
6432 struct got_object_id *commit_id = NULL;
6434 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6435 if (err)
6436 return err;
6438 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6439 if (err)
6440 goto done;
6441 err = got_ref_resolve(&commit_id, repo, commit_ref);
6442 if (err)
6443 goto done;
6444 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6445 err = got_error(GOT_ERR_REBASE_COMMITID);
6446 goto done;
6449 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6450 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6451 done:
6452 if (commit_ref)
6453 got_ref_close(commit_ref);
6454 free(commit_ref_name);
6455 free(commit_id);
6456 return err;
6459 const struct got_error *
6460 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6461 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6462 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6463 struct got_commit_object *orig_commit,
6464 struct got_object_id *orig_commit_id, const char *new_logmsg,
6465 struct got_repository *repo)
6467 const struct got_error *err;
6468 char *commit_ref_name;
6469 struct got_reference *commit_ref = NULL;
6471 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6472 if (err)
6473 return err;
6475 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6476 if (err)
6477 goto done;
6479 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6480 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6481 done:
6482 if (commit_ref)
6483 got_ref_close(commit_ref);
6484 free(commit_ref_name);
6485 return err;
6488 const struct got_error *
6489 got_worktree_rebase_postpone(struct got_worktree *worktree,
6490 struct got_fileindex *fileindex)
6492 if (fileindex)
6493 got_fileindex_free(fileindex);
6494 return lock_worktree(worktree, LOCK_SH);
6497 static const struct got_error *
6498 delete_ref(const char *name, struct got_repository *repo)
6500 const struct got_error *err;
6501 struct got_reference *ref;
6503 err = got_ref_open(&ref, repo, name, 0);
6504 if (err) {
6505 if (err->code == GOT_ERR_NOT_REF)
6506 return NULL;
6507 return err;
6510 err = got_ref_delete(ref, repo);
6511 got_ref_close(ref);
6512 return err;
6515 static const struct got_error *
6516 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6518 const struct got_error *err;
6519 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6520 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6522 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6523 if (err)
6524 goto done;
6525 err = delete_ref(tmp_branch_name, repo);
6526 if (err)
6527 goto done;
6529 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6530 if (err)
6531 goto done;
6532 err = delete_ref(new_base_branch_ref_name, repo);
6533 if (err)
6534 goto done;
6536 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6537 if (err)
6538 goto done;
6539 err = delete_ref(branch_ref_name, repo);
6540 if (err)
6541 goto done;
6543 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6544 if (err)
6545 goto done;
6546 err = delete_ref(commit_ref_name, repo);
6547 if (err)
6548 goto done;
6550 done:
6551 free(tmp_branch_name);
6552 free(new_base_branch_ref_name);
6553 free(branch_ref_name);
6554 free(commit_ref_name);
6555 return err;
6558 const struct got_error *
6559 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6560 struct got_object_id *new_commit_id, struct got_repository *repo)
6562 const struct got_error *err;
6563 struct got_reference *ref = NULL;
6564 struct got_object_id *old_commit_id = NULL;
6565 const char *branch_name = NULL;
6566 char *new_id_str = NULL;
6567 char *refname = NULL;
6569 branch_name = got_ref_get_name(branch);
6570 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6571 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6572 branch_name += 11;
6574 err = got_object_id_str(&new_id_str, new_commit_id);
6575 if (err)
6576 return err;
6578 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6579 new_id_str) == -1) {
6580 err = got_error_from_errno("asprintf");
6581 goto done;
6584 err = got_ref_resolve(&old_commit_id, repo, branch);
6585 if (err)
6586 goto done;
6588 err = got_ref_alloc(&ref, refname, old_commit_id);
6589 if (err)
6590 goto done;
6592 err = got_ref_write(ref, repo);
6593 done:
6594 free(new_id_str);
6595 free(refname);
6596 free(old_commit_id);
6597 if (ref)
6598 got_ref_close(ref);
6599 return err;
6602 const struct got_error *
6603 got_worktree_rebase_complete(struct got_worktree *worktree,
6604 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6605 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6606 struct got_repository *repo, int create_backup)
6608 const struct got_error *err, *unlockerr, *sync_err;
6609 struct got_object_id *new_head_commit_id = NULL;
6610 char *fileindex_path = NULL;
6612 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6613 if (err)
6614 return err;
6616 if (create_backup) {
6617 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6618 rebased_branch, new_head_commit_id, repo);
6619 if (err)
6620 goto done;
6623 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6624 if (err)
6625 goto done;
6627 err = got_ref_write(rebased_branch, repo);
6628 if (err)
6629 goto done;
6631 err = got_worktree_set_head_ref(worktree, rebased_branch);
6632 if (err)
6633 goto done;
6635 err = delete_rebase_refs(worktree, repo);
6636 if (err)
6637 goto done;
6639 err = get_fileindex_path(&fileindex_path, worktree);
6640 if (err)
6641 goto done;
6642 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6643 sync_err = sync_fileindex(fileindex, fileindex_path);
6644 if (sync_err && err == NULL)
6645 err = sync_err;
6646 done:
6647 got_fileindex_free(fileindex);
6648 free(fileindex_path);
6649 free(new_head_commit_id);
6650 unlockerr = lock_worktree(worktree, LOCK_SH);
6651 if (unlockerr && err == NULL)
6652 err = unlockerr;
6653 return err;
6656 const struct got_error *
6657 got_worktree_rebase_abort(struct got_worktree *worktree,
6658 struct got_fileindex *fileindex, struct got_repository *repo,
6659 struct got_reference *new_base_branch,
6660 got_worktree_checkout_cb progress_cb, void *progress_arg)
6662 const struct got_error *err, *unlockerr, *sync_err;
6663 struct got_reference *resolved = NULL;
6664 struct got_object_id *commit_id = NULL;
6665 char *fileindex_path = NULL;
6666 struct revert_file_args rfa;
6667 struct got_object_id *tree_id = NULL;
6669 err = lock_worktree(worktree, LOCK_EX);
6670 if (err)
6671 return err;
6673 err = got_ref_open(&resolved, repo,
6674 got_ref_get_symref_target(new_base_branch), 0);
6675 if (err)
6676 goto done;
6678 err = got_worktree_set_head_ref(worktree, resolved);
6679 if (err)
6680 goto done;
6683 * XXX commits to the base branch could have happened while
6684 * we were busy rebasing; should we store the original commit ID
6685 * when rebase begins and read it back here?
6687 err = got_ref_resolve(&commit_id, repo, resolved);
6688 if (err)
6689 goto done;
6691 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6692 if (err)
6693 goto done;
6695 err = got_object_id_by_path(&tree_id, repo,
6696 worktree->base_commit_id, worktree->path_prefix);
6697 if (err)
6698 goto done;
6700 err = delete_rebase_refs(worktree, repo);
6701 if (err)
6702 goto done;
6704 err = get_fileindex_path(&fileindex_path, worktree);
6705 if (err)
6706 goto done;
6708 rfa.worktree = worktree;
6709 rfa.fileindex = fileindex;
6710 rfa.progress_cb = progress_cb;
6711 rfa.progress_arg = progress_arg;
6712 rfa.patch_cb = NULL;
6713 rfa.patch_arg = NULL;
6714 rfa.repo = repo;
6715 rfa.unlink_added_files = 0;
6716 err = worktree_status(worktree, "", fileindex, repo,
6717 revert_file, &rfa, NULL, NULL, 1, 0);
6718 if (err)
6719 goto sync;
6721 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6722 repo, progress_cb, progress_arg, NULL, NULL);
6723 sync:
6724 sync_err = sync_fileindex(fileindex, fileindex_path);
6725 if (sync_err && err == NULL)
6726 err = sync_err;
6727 done:
6728 got_ref_close(resolved);
6729 free(tree_id);
6730 free(commit_id);
6731 if (fileindex)
6732 got_fileindex_free(fileindex);
6733 free(fileindex_path);
6735 unlockerr = lock_worktree(worktree, LOCK_SH);
6736 if (unlockerr && err == NULL)
6737 err = unlockerr;
6738 return err;
6741 const struct got_error *
6742 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6743 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6744 struct got_fileindex **fileindex, struct got_worktree *worktree,
6745 struct got_repository *repo)
6747 const struct got_error *err = NULL;
6748 char *tmp_branch_name = NULL;
6749 char *branch_ref_name = NULL;
6750 char *base_commit_ref_name = NULL;
6751 char *fileindex_path = NULL;
6752 struct check_rebase_ok_arg ok_arg;
6753 struct got_reference *wt_branch = NULL;
6754 struct got_reference *base_commit_ref = NULL;
6756 *tmp_branch = NULL;
6757 *branch_ref = NULL;
6758 *base_commit_id = NULL;
6759 *fileindex = NULL;
6761 err = lock_worktree(worktree, LOCK_EX);
6762 if (err)
6763 return err;
6765 err = open_fileindex(fileindex, &fileindex_path, worktree);
6766 if (err)
6767 goto done;
6769 ok_arg.worktree = worktree;
6770 ok_arg.repo = repo;
6771 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6772 &ok_arg);
6773 if (err)
6774 goto done;
6776 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6777 if (err)
6778 goto done;
6780 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6781 if (err)
6782 goto done;
6784 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6785 worktree);
6786 if (err)
6787 goto done;
6789 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6790 0);
6791 if (err)
6792 goto done;
6794 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6795 if (err)
6796 goto done;
6798 err = got_ref_write(*branch_ref, repo);
6799 if (err)
6800 goto done;
6802 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6803 worktree->base_commit_id);
6804 if (err)
6805 goto done;
6806 err = got_ref_write(base_commit_ref, repo);
6807 if (err)
6808 goto done;
6809 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6810 if (*base_commit_id == NULL) {
6811 err = got_error_from_errno("got_object_id_dup");
6812 goto done;
6815 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6816 worktree->base_commit_id);
6817 if (err)
6818 goto done;
6819 err = got_ref_write(*tmp_branch, repo);
6820 if (err)
6821 goto done;
6823 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6824 if (err)
6825 goto done;
6826 done:
6827 free(fileindex_path);
6828 free(tmp_branch_name);
6829 free(branch_ref_name);
6830 free(base_commit_ref_name);
6831 if (wt_branch)
6832 got_ref_close(wt_branch);
6833 if (err) {
6834 if (*branch_ref) {
6835 got_ref_close(*branch_ref);
6836 *branch_ref = NULL;
6838 if (*tmp_branch) {
6839 got_ref_close(*tmp_branch);
6840 *tmp_branch = NULL;
6842 free(*base_commit_id);
6843 if (*fileindex) {
6844 got_fileindex_free(*fileindex);
6845 *fileindex = NULL;
6847 lock_worktree(worktree, LOCK_SH);
6849 return err;
6852 const struct got_error *
6853 got_worktree_histedit_postpone(struct got_worktree *worktree,
6854 struct got_fileindex *fileindex)
6856 if (fileindex)
6857 got_fileindex_free(fileindex);
6858 return lock_worktree(worktree, LOCK_SH);
6861 const struct got_error *
6862 got_worktree_histedit_in_progress(int *in_progress,
6863 struct got_worktree *worktree)
6865 const struct got_error *err;
6866 char *tmp_branch_name = NULL;
6868 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6869 if (err)
6870 return err;
6872 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6873 free(tmp_branch_name);
6874 return NULL;
6877 const struct got_error *
6878 got_worktree_histedit_continue(struct got_object_id **commit_id,
6879 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6880 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6881 struct got_worktree *worktree, struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6885 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6886 struct got_reference *commit_ref = NULL;
6887 struct got_reference *base_commit_ref = NULL;
6888 char *fileindex_path = NULL;
6889 int have_staged_files = 0;
6891 *commit_id = NULL;
6892 *tmp_branch = NULL;
6893 *base_commit_id = NULL;
6894 *fileindex = NULL;
6896 err = lock_worktree(worktree, LOCK_EX);
6897 if (err)
6898 return err;
6900 err = open_fileindex(fileindex, &fileindex_path, worktree);
6901 if (err)
6902 goto done;
6904 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6905 &have_staged_files);
6906 if (err && err->code != GOT_ERR_CANCELLED)
6907 goto done;
6908 if (have_staged_files) {
6909 err = got_error(GOT_ERR_STAGED_PATHS);
6910 goto done;
6913 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6914 if (err)
6915 goto done;
6917 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6918 if (err)
6919 goto done;
6921 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6922 if (err)
6923 goto done;
6925 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6926 worktree);
6927 if (err)
6928 goto done;
6930 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6931 if (err)
6932 goto done;
6934 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6935 if (err)
6936 goto done;
6937 err = got_ref_resolve(commit_id, repo, commit_ref);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6942 if (err)
6943 goto done;
6944 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6945 if (err)
6946 goto done;
6948 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6949 if (err)
6950 goto done;
6951 done:
6952 free(commit_ref_name);
6953 free(branch_ref_name);
6954 free(fileindex_path);
6955 if (commit_ref)
6956 got_ref_close(commit_ref);
6957 if (base_commit_ref)
6958 got_ref_close(base_commit_ref);
6959 if (err) {
6960 free(*commit_id);
6961 *commit_id = NULL;
6962 free(*base_commit_id);
6963 *base_commit_id = NULL;
6964 if (*tmp_branch) {
6965 got_ref_close(*tmp_branch);
6966 *tmp_branch = NULL;
6968 if (*fileindex) {
6969 got_fileindex_free(*fileindex);
6970 *fileindex = NULL;
6972 lock_worktree(worktree, LOCK_EX);
6974 return err;
6977 static const struct got_error *
6978 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6980 const struct got_error *err;
6981 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6982 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6984 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6985 if (err)
6986 goto done;
6987 err = delete_ref(tmp_branch_name, repo);
6988 if (err)
6989 goto done;
6991 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6992 worktree);
6993 if (err)
6994 goto done;
6995 err = delete_ref(base_commit_ref_name, repo);
6996 if (err)
6997 goto done;
6999 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7000 if (err)
7001 goto done;
7002 err = delete_ref(branch_ref_name, repo);
7003 if (err)
7004 goto done;
7006 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7007 if (err)
7008 goto done;
7009 err = delete_ref(commit_ref_name, repo);
7010 if (err)
7011 goto done;
7012 done:
7013 free(tmp_branch_name);
7014 free(base_commit_ref_name);
7015 free(branch_ref_name);
7016 free(commit_ref_name);
7017 return err;
7020 const struct got_error *
7021 got_worktree_histedit_abort(struct got_worktree *worktree,
7022 struct got_fileindex *fileindex, struct got_repository *repo,
7023 struct got_reference *branch, struct got_object_id *base_commit_id,
7024 got_worktree_checkout_cb progress_cb, void *progress_arg)
7026 const struct got_error *err, *unlockerr, *sync_err;
7027 struct got_reference *resolved = NULL;
7028 char *fileindex_path = NULL;
7029 struct got_object_id *tree_id = NULL;
7030 struct revert_file_args rfa;
7032 err = lock_worktree(worktree, LOCK_EX);
7033 if (err)
7034 return err;
7036 err = got_ref_open(&resolved, repo,
7037 got_ref_get_symref_target(branch), 0);
7038 if (err)
7039 goto done;
7041 err = got_worktree_set_head_ref(worktree, resolved);
7042 if (err)
7043 goto done;
7045 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7046 if (err)
7047 goto done;
7049 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7050 worktree->path_prefix);
7051 if (err)
7052 goto done;
7054 err = delete_histedit_refs(worktree, repo);
7055 if (err)
7056 goto done;
7058 err = get_fileindex_path(&fileindex_path, worktree);
7059 if (err)
7060 goto done;
7062 rfa.worktree = worktree;
7063 rfa.fileindex = fileindex;
7064 rfa.progress_cb = progress_cb;
7065 rfa.progress_arg = progress_arg;
7066 rfa.patch_cb = NULL;
7067 rfa.patch_arg = NULL;
7068 rfa.repo = repo;
7069 rfa.unlink_added_files = 0;
7070 err = worktree_status(worktree, "", fileindex, repo,
7071 revert_file, &rfa, NULL, NULL, 1, 0);
7072 if (err)
7073 goto sync;
7075 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7076 repo, progress_cb, progress_arg, NULL, NULL);
7077 sync:
7078 sync_err = sync_fileindex(fileindex, fileindex_path);
7079 if (sync_err && err == NULL)
7080 err = sync_err;
7081 done:
7082 got_ref_close(resolved);
7083 free(tree_id);
7084 free(fileindex_path);
7086 unlockerr = lock_worktree(worktree, LOCK_SH);
7087 if (unlockerr && err == NULL)
7088 err = unlockerr;
7089 return err;
7092 const struct got_error *
7093 got_worktree_histedit_complete(struct got_worktree *worktree,
7094 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7095 struct got_reference *edited_branch, struct got_repository *repo)
7097 const struct got_error *err, *unlockerr, *sync_err;
7098 struct got_object_id *new_head_commit_id = NULL;
7099 struct got_reference *resolved = NULL;
7100 char *fileindex_path = NULL;
7102 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7103 if (err)
7104 return err;
7106 err = got_ref_open(&resolved, repo,
7107 got_ref_get_symref_target(edited_branch), 0);
7108 if (err)
7109 goto done;
7111 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7112 resolved, new_head_commit_id, repo);
7113 if (err)
7114 goto done;
7116 err = got_ref_change_ref(resolved, new_head_commit_id);
7117 if (err)
7118 goto done;
7120 err = got_ref_write(resolved, repo);
7121 if (err)
7122 goto done;
7124 err = got_worktree_set_head_ref(worktree, resolved);
7125 if (err)
7126 goto done;
7128 err = delete_histedit_refs(worktree, repo);
7129 if (err)
7130 goto done;
7132 err = get_fileindex_path(&fileindex_path, worktree);
7133 if (err)
7134 goto done;
7135 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7138 err = sync_err;
7139 done:
7140 got_fileindex_free(fileindex);
7141 free(fileindex_path);
7142 free(new_head_commit_id);
7143 unlockerr = lock_worktree(worktree, LOCK_SH);
7144 if (unlockerr && err == NULL)
7145 err = unlockerr;
7146 return err;
7149 const struct got_error *
7150 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7151 struct got_object_id *commit_id, struct got_repository *repo)
7153 const struct got_error *err;
7154 char *commit_ref_name;
7156 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7157 if (err)
7158 return err;
7160 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7161 if (err)
7162 goto done;
7164 err = delete_ref(commit_ref_name, repo);
7165 done:
7166 free(commit_ref_name);
7167 return err;
7170 const struct got_error *
7171 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7172 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7173 struct got_worktree *worktree, const char *refname,
7174 struct got_repository *repo)
7176 const struct got_error *err = NULL;
7177 char *fileindex_path = NULL;
7178 struct check_rebase_ok_arg ok_arg;
7180 *fileindex = NULL;
7181 *branch_ref = NULL;
7182 *base_branch_ref = NULL;
7184 err = lock_worktree(worktree, LOCK_EX);
7185 if (err)
7186 return err;
7188 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7189 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7190 "cannot integrate a branch into itself; "
7191 "update -b or different branch name required");
7192 goto done;
7195 err = open_fileindex(fileindex, &fileindex_path, worktree);
7196 if (err)
7197 goto done;
7199 /* Preconditions are the same as for rebase. */
7200 ok_arg.worktree = worktree;
7201 ok_arg.repo = repo;
7202 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7203 &ok_arg);
7204 if (err)
7205 goto done;
7207 err = got_ref_open(branch_ref, repo, refname, 1);
7208 if (err)
7209 goto done;
7211 err = got_ref_open(base_branch_ref, repo,
7212 got_worktree_get_head_ref_name(worktree), 1);
7213 done:
7214 if (err) {
7215 if (*branch_ref) {
7216 got_ref_close(*branch_ref);
7217 *branch_ref = NULL;
7219 if (*base_branch_ref) {
7220 got_ref_close(*base_branch_ref);
7221 *base_branch_ref = NULL;
7223 if (*fileindex) {
7224 got_fileindex_free(*fileindex);
7225 *fileindex = NULL;
7227 lock_worktree(worktree, LOCK_SH);
7229 return err;
7232 const struct got_error *
7233 got_worktree_integrate_continue(struct got_worktree *worktree,
7234 struct got_fileindex *fileindex, struct got_repository *repo,
7235 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7236 got_worktree_checkout_cb progress_cb, void *progress_arg,
7237 got_cancel_cb cancel_cb, void *cancel_arg)
7239 const struct got_error *err = NULL, *sync_err, *unlockerr;
7240 char *fileindex_path = NULL;
7241 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7243 err = get_fileindex_path(&fileindex_path, worktree);
7244 if (err)
7245 goto done;
7247 err = got_ref_resolve(&commit_id, repo, branch_ref);
7248 if (err)
7249 goto done;
7251 err = got_object_id_by_path(&tree_id, repo, commit_id,
7252 worktree->path_prefix);
7253 if (err)
7254 goto done;
7256 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7257 if (err)
7258 goto done;
7260 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7261 progress_cb, progress_arg, cancel_cb, cancel_arg);
7262 if (err)
7263 goto sync;
7265 err = got_ref_change_ref(base_branch_ref, commit_id);
7266 if (err)
7267 goto sync;
7269 err = got_ref_write(base_branch_ref, repo);
7270 if (err)
7271 goto sync;
7273 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7274 sync:
7275 sync_err = sync_fileindex(fileindex, fileindex_path);
7276 if (sync_err && err == NULL)
7277 err = sync_err;
7279 done:
7280 unlockerr = got_ref_unlock(branch_ref);
7281 if (unlockerr && err == NULL)
7282 err = unlockerr;
7283 got_ref_close(branch_ref);
7285 unlockerr = got_ref_unlock(base_branch_ref);
7286 if (unlockerr && err == NULL)
7287 err = unlockerr;
7288 got_ref_close(base_branch_ref);
7290 got_fileindex_free(fileindex);
7291 free(fileindex_path);
7292 free(tree_id);
7294 unlockerr = lock_worktree(worktree, LOCK_SH);
7295 if (unlockerr && err == NULL)
7296 err = unlockerr;
7297 return err;
7300 const struct got_error *
7301 got_worktree_integrate_abort(struct got_worktree *worktree,
7302 struct got_fileindex *fileindex, struct got_repository *repo,
7303 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7305 const struct got_error *err = NULL, *unlockerr = NULL;
7307 got_fileindex_free(fileindex);
7309 err = lock_worktree(worktree, LOCK_SH);
7311 unlockerr = got_ref_unlock(branch_ref);
7312 if (unlockerr && err == NULL)
7313 err = unlockerr;
7314 got_ref_close(branch_ref);
7316 unlockerr = got_ref_unlock(base_branch_ref);
7317 if (unlockerr && err == NULL)
7318 err = unlockerr;
7319 got_ref_close(base_branch_ref);
7321 return err;
7324 const struct got_error *
7325 got_worktree_merge_postpone(struct got_worktree *worktree,
7326 struct got_fileindex *fileindex)
7328 const struct got_error *err, *sync_err;
7329 char *fileindex_path = NULL;
7331 err = get_fileindex_path(&fileindex_path, worktree);
7332 if (err)
7333 goto done;
7335 sync_err = sync_fileindex(fileindex, fileindex_path);
7337 err = lock_worktree(worktree, LOCK_SH);
7338 if (sync_err && err == NULL)
7339 err = sync_err;
7340 done:
7341 got_fileindex_free(fileindex);
7342 free(fileindex_path);
7343 return err;
7346 static const struct got_error *
7347 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7349 const struct got_error *err;
7350 char *branch_refname = NULL, *commit_refname = NULL;
7352 err = get_merge_branch_ref_name(&branch_refname, worktree);
7353 if (err)
7354 goto done;
7355 err = delete_ref(branch_refname, repo);
7356 if (err)
7357 goto done;
7359 err = get_merge_commit_ref_name(&commit_refname, worktree);
7360 if (err)
7361 goto done;
7362 err = delete_ref(commit_refname, repo);
7363 if (err)
7364 goto done;
7366 done:
7367 free(branch_refname);
7368 free(commit_refname);
7369 return err;
7372 struct merge_commit_msg_arg {
7373 struct got_worktree *worktree;
7374 const char *branch_name;
7377 static const struct got_error *
7378 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7379 void *arg)
7381 struct merge_commit_msg_arg *a = arg;
7383 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7384 got_worktree_get_head_ref_name(a->worktree)) == -1)
7385 return got_error_from_errno("asprintf");
7387 return NULL;
7391 const struct got_error *
7392 got_worktree_merge_branch(struct got_worktree *worktree,
7393 struct got_fileindex *fileindex,
7394 struct got_object_id *yca_commit_id,
7395 struct got_object_id *branch_tip,
7396 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7397 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7399 const struct got_error *err;
7400 char *fileindex_path = NULL;
7402 err = get_fileindex_path(&fileindex_path, worktree);
7403 if (err)
7404 goto done;
7406 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7407 worktree);
7408 if (err)
7409 goto done;
7411 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7412 branch_tip, repo, progress_cb, progress_arg,
7413 cancel_cb, cancel_arg);
7414 done:
7415 free(fileindex_path);
7416 return err;
7419 const struct got_error *
7420 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7421 struct got_worktree *worktree, struct got_fileindex *fileindex,
7422 const char *author, const char *committer, int allow_bad_symlinks,
7423 struct got_object_id *branch_tip, const char *branch_name,
7424 struct got_repository *repo,
7425 got_worktree_status_cb status_cb, void *status_arg)
7428 const struct got_error *err = NULL, *sync_err;
7429 struct got_pathlist_head commitable_paths;
7430 struct collect_commitables_arg cc_arg;
7431 struct got_pathlist_entry *pe;
7432 struct got_reference *head_ref = NULL;
7433 struct got_object_id *head_commit_id = NULL;
7434 int have_staged_files = 0;
7435 struct merge_commit_msg_arg mcm_arg;
7436 char *fileindex_path = NULL;
7438 *new_commit_id = NULL;
7440 TAILQ_INIT(&commitable_paths);
7442 err = get_fileindex_path(&fileindex_path, worktree);
7443 if (err)
7444 goto done;
7446 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7447 if (err)
7448 goto done;
7450 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7451 if (err)
7452 goto done;
7454 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7455 &have_staged_files);
7456 if (err && err->code != GOT_ERR_CANCELLED)
7457 goto done;
7458 if (have_staged_files) {
7459 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7460 goto done;
7463 cc_arg.commitable_paths = &commitable_paths;
7464 cc_arg.worktree = worktree;
7465 cc_arg.fileindex = fileindex;
7466 cc_arg.repo = repo;
7467 cc_arg.have_staged_files = have_staged_files;
7468 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7469 err = worktree_status(worktree, "", fileindex, repo,
7470 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7471 if (err)
7472 goto done;
7474 if (TAILQ_EMPTY(&commitable_paths)) {
7475 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7476 "merge of %s cannot proceed", branch_name);
7477 goto done;
7480 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7481 struct got_commitable *ct = pe->data;
7482 const char *ct_path = ct->in_repo_path;
7484 while (ct_path[0] == '/')
7485 ct_path++;
7486 err = check_out_of_date(ct_path, ct->status,
7487 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7488 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7489 if (err)
7490 goto done;
7494 mcm_arg.worktree = worktree;
7495 mcm_arg.branch_name = branch_name;
7496 err = commit_worktree(new_commit_id, &commitable_paths,
7497 head_commit_id, branch_tip, worktree, author, committer,
7498 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7499 if (err)
7500 goto done;
7502 err = update_fileindex_after_commit(worktree, &commitable_paths,
7503 *new_commit_id, fileindex, have_staged_files);
7504 sync_err = sync_fileindex(fileindex, fileindex_path);
7505 if (sync_err && err == NULL)
7506 err = sync_err;
7507 done:
7508 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7509 struct got_commitable *ct = pe->data;
7510 free_commitable(ct);
7512 got_pathlist_free(&commitable_paths);
7513 free(fileindex_path);
7514 return err;
7517 const struct got_error *
7518 got_worktree_merge_complete(struct got_worktree *worktree,
7519 struct got_fileindex *fileindex, struct got_repository *repo)
7521 const struct got_error *err, *unlockerr, *sync_err;
7522 char *fileindex_path = NULL;
7524 err = delete_merge_refs(worktree, repo);
7525 if (err)
7526 goto done;
7528 err = get_fileindex_path(&fileindex_path, worktree);
7529 if (err)
7530 goto done;
7531 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 sync_err = sync_fileindex(fileindex, fileindex_path);
7533 if (sync_err && err == NULL)
7534 err = sync_err;
7535 done:
7536 got_fileindex_free(fileindex);
7537 free(fileindex_path);
7538 unlockerr = lock_worktree(worktree, LOCK_SH);
7539 if (unlockerr && err == NULL)
7540 err = unlockerr;
7541 return err;
7544 const struct got_error *
7545 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7546 struct got_repository *repo)
7548 const struct got_error *err;
7549 char *branch_refname = NULL;
7550 struct got_reference *branch_ref = NULL;
7552 *in_progress = 0;
7554 err = get_merge_branch_ref_name(&branch_refname, worktree);
7555 if (err)
7556 return err;
7557 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7558 free(branch_refname);
7559 if (err) {
7560 if (err->code != GOT_ERR_NOT_REF)
7561 return err;
7562 } else
7563 *in_progress = 1;
7565 return NULL;
7568 const struct got_error *got_worktree_merge_prepare(
7569 struct got_fileindex **fileindex, struct got_worktree *worktree,
7570 struct got_reference *branch, struct got_repository *repo)
7572 const struct got_error *err = NULL;
7573 char *fileindex_path = NULL;
7574 char *branch_refname = NULL, *commit_refname = NULL;
7575 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7576 struct got_reference *commit_ref = NULL;
7577 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7578 struct check_rebase_ok_arg ok_arg;
7580 *fileindex = NULL;
7582 err = lock_worktree(worktree, LOCK_EX);
7583 if (err)
7584 return err;
7586 err = open_fileindex(fileindex, &fileindex_path, worktree);
7587 if (err)
7588 goto done;
7590 /* Preconditions are the same as for rebase. */
7591 ok_arg.worktree = worktree;
7592 ok_arg.repo = repo;
7593 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7594 &ok_arg);
7595 if (err)
7596 goto done;
7598 err = get_merge_branch_ref_name(&branch_refname, worktree);
7599 if (err)
7600 return err;
7602 err = get_merge_commit_ref_name(&commit_refname, worktree);
7603 if (err)
7604 return err;
7606 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7607 0);
7608 if (err)
7609 goto done;
7611 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7612 if (err)
7613 goto done;
7615 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7616 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7617 goto done;
7620 err = got_ref_resolve(&branch_tip, repo, branch);
7621 if (err)
7622 goto done;
7624 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7625 if (err)
7626 goto done;
7627 err = got_ref_write(branch_ref, repo);
7628 if (err)
7629 goto done;
7631 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7632 if (err)
7633 goto done;
7634 err = got_ref_write(commit_ref, repo);
7635 if (err)
7636 goto done;
7638 done:
7639 free(branch_refname);
7640 free(commit_refname);
7641 free(fileindex_path);
7642 if (branch_ref)
7643 got_ref_close(branch_ref);
7644 if (commit_ref)
7645 got_ref_close(commit_ref);
7646 if (wt_branch)
7647 got_ref_close(wt_branch);
7648 free(wt_branch_tip);
7649 if (err) {
7650 if (*fileindex) {
7651 got_fileindex_free(*fileindex);
7652 *fileindex = NULL;
7654 lock_worktree(worktree, LOCK_SH);
7656 return err;
7659 const struct got_error *
7660 got_worktree_merge_continue(char **branch_name,
7661 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7662 struct got_worktree *worktree, struct got_repository *repo)
7664 const struct got_error *err;
7665 char *commit_refname = NULL, *branch_refname = NULL;
7666 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7667 char *fileindex_path = NULL;
7668 int have_staged_files = 0;
7670 *branch_name = NULL;
7671 *branch_tip = NULL;
7672 *fileindex = NULL;
7674 err = lock_worktree(worktree, LOCK_EX);
7675 if (err)
7676 return err;
7678 err = open_fileindex(fileindex, &fileindex_path, worktree);
7679 if (err)
7680 goto done;
7682 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7683 &have_staged_files);
7684 if (err && err->code != GOT_ERR_CANCELLED)
7685 goto done;
7686 if (have_staged_files) {
7687 err = got_error(GOT_ERR_STAGED_PATHS);
7688 goto done;
7691 err = get_merge_branch_ref_name(&branch_refname, worktree);
7692 if (err)
7693 goto done;
7695 err = get_merge_commit_ref_name(&commit_refname, worktree);
7696 if (err)
7697 goto done;
7699 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7700 if (err)
7701 goto done;
7703 if (!got_ref_is_symbolic(branch_ref)) {
7704 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7705 "%s is not a symbolic reference",
7706 got_ref_get_name(branch_ref));
7707 goto done;
7709 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7710 if (*branch_name == NULL) {
7711 err = got_error_from_errno("strdup");
7712 goto done;
7715 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7716 if (err)
7717 goto done;
7719 err = got_ref_resolve(branch_tip, repo, commit_ref);
7720 if (err)
7721 goto done;
7722 done:
7723 free(commit_refname);
7724 free(branch_refname);
7725 free(fileindex_path);
7726 if (commit_ref)
7727 got_ref_close(commit_ref);
7728 if (branch_ref)
7729 got_ref_close(branch_ref);
7730 if (err) {
7731 if (*branch_name) {
7732 free(*branch_name);
7733 *branch_name = NULL;
7735 free(*branch_tip);
7736 *branch_tip = NULL;
7737 if (*fileindex) {
7738 got_fileindex_free(*fileindex);
7739 *fileindex = NULL;
7741 lock_worktree(worktree, LOCK_SH);
7743 return err;
7746 const struct got_error *
7747 got_worktree_merge_abort(struct got_worktree *worktree,
7748 struct got_fileindex *fileindex, struct got_repository *repo,
7749 got_worktree_checkout_cb progress_cb, void *progress_arg)
7751 const struct got_error *err, *unlockerr, *sync_err;
7752 struct got_object_id *commit_id = NULL;
7753 char *fileindex_path = NULL;
7754 struct revert_file_args rfa;
7755 struct got_object_id *tree_id = NULL;
7757 err = got_object_id_by_path(&tree_id, repo,
7758 worktree->base_commit_id, worktree->path_prefix);
7759 if (err)
7760 goto done;
7762 err = delete_merge_refs(worktree, repo);
7763 if (err)
7764 goto done;
7766 err = get_fileindex_path(&fileindex_path, worktree);
7767 if (err)
7768 goto done;
7770 rfa.worktree = worktree;
7771 rfa.fileindex = fileindex;
7772 rfa.progress_cb = progress_cb;
7773 rfa.progress_arg = progress_arg;
7774 rfa.patch_cb = NULL;
7775 rfa.patch_arg = NULL;
7776 rfa.repo = repo;
7777 rfa.unlink_added_files = 1;
7778 err = worktree_status(worktree, "", fileindex, repo,
7779 revert_file, &rfa, NULL, NULL, 1, 0);
7780 if (err)
7781 goto sync;
7783 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7784 repo, progress_cb, progress_arg, NULL, NULL);
7785 sync:
7786 sync_err = sync_fileindex(fileindex, fileindex_path);
7787 if (sync_err && err == NULL)
7788 err = sync_err;
7789 done:
7790 free(tree_id);
7791 free(commit_id);
7792 if (fileindex)
7793 got_fileindex_free(fileindex);
7794 free(fileindex_path);
7796 unlockerr = lock_worktree(worktree, LOCK_SH);
7797 if (unlockerr && err == NULL)
7798 err = unlockerr;
7799 return err;
7802 struct check_stage_ok_arg {
7803 struct got_object_id *head_commit_id;
7804 struct got_worktree *worktree;
7805 struct got_fileindex *fileindex;
7806 struct got_repository *repo;
7807 int have_changes;
7810 const struct got_error *
7811 check_stage_ok(void *arg, unsigned char status,
7812 unsigned char staged_status, const char *relpath,
7813 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7814 struct got_object_id *commit_id, int dirfd, const char *de_name)
7816 struct check_stage_ok_arg *a = arg;
7817 const struct got_error *err = NULL;
7818 struct got_fileindex_entry *ie;
7819 struct got_object_id base_commit_id;
7820 struct got_object_id *base_commit_idp = NULL;
7821 char *in_repo_path = NULL, *p;
7823 if (status == GOT_STATUS_UNVERSIONED ||
7824 status == GOT_STATUS_NO_CHANGE)
7825 return NULL;
7826 if (status == GOT_STATUS_NONEXISTENT)
7827 return got_error_set_errno(ENOENT, relpath);
7829 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7830 if (ie == NULL)
7831 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7833 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7834 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7835 relpath) == -1)
7836 return got_error_from_errno("asprintf");
7838 if (got_fileindex_entry_has_commit(ie)) {
7839 memcpy(base_commit_id.sha1, ie->commit_sha1,
7840 SHA1_DIGEST_LENGTH);
7841 base_commit_idp = &base_commit_id;
7844 if (status == GOT_STATUS_CONFLICT) {
7845 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7846 goto done;
7847 } else if (status != GOT_STATUS_ADD &&
7848 status != GOT_STATUS_MODIFY &&
7849 status != GOT_STATUS_DELETE) {
7850 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7851 goto done;
7854 a->have_changes = 1;
7856 p = in_repo_path;
7857 while (p[0] == '/')
7858 p++;
7859 err = check_out_of_date(p, status, staged_status,
7860 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7861 GOT_ERR_STAGE_OUT_OF_DATE);
7862 done:
7863 free(in_repo_path);
7864 return err;
7867 struct stage_path_arg {
7868 struct got_worktree *worktree;
7869 struct got_fileindex *fileindex;
7870 struct got_repository *repo;
7871 got_worktree_status_cb status_cb;
7872 void *status_arg;
7873 got_worktree_patch_cb patch_cb;
7874 void *patch_arg;
7875 int staged_something;
7876 int allow_bad_symlinks;
7879 static const struct got_error *
7880 stage_path(void *arg, unsigned char status,
7881 unsigned char staged_status, const char *relpath,
7882 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7883 struct got_object_id *commit_id, int dirfd, const char *de_name)
7885 struct stage_path_arg *a = arg;
7886 const struct got_error *err = NULL;
7887 struct got_fileindex_entry *ie;
7888 char *ondisk_path = NULL, *path_content = NULL;
7889 uint32_t stage;
7890 struct got_object_id *new_staged_blob_id = NULL;
7891 struct stat sb;
7893 if (status == GOT_STATUS_UNVERSIONED)
7894 return NULL;
7896 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7897 if (ie == NULL)
7898 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7900 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7901 relpath)== -1)
7902 return got_error_from_errno("asprintf");
7904 switch (status) {
7905 case GOT_STATUS_ADD:
7906 case GOT_STATUS_MODIFY:
7907 /* XXX could sb.st_mode be passed in by our caller? */
7908 if (lstat(ondisk_path, &sb) == -1) {
7909 err = got_error_from_errno2("lstat", ondisk_path);
7910 break;
7912 if (a->patch_cb) {
7913 if (status == GOT_STATUS_ADD) {
7914 int choice = GOT_PATCH_CHOICE_NONE;
7915 err = (*a->patch_cb)(&choice, a->patch_arg,
7916 status, ie->path, NULL, 1, 1);
7917 if (err)
7918 break;
7919 if (choice != GOT_PATCH_CHOICE_YES)
7920 break;
7921 } else {
7922 err = create_patched_content(&path_content, 0,
7923 staged_blob_id ? staged_blob_id : blob_id,
7924 ondisk_path, dirfd, de_name, ie->path,
7925 a->repo, a->patch_cb, a->patch_arg);
7926 if (err || path_content == NULL)
7927 break;
7930 err = got_object_blob_create(&new_staged_blob_id,
7931 path_content ? path_content : ondisk_path, a->repo);
7932 if (err)
7933 break;
7934 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7935 SHA1_DIGEST_LENGTH);
7936 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7937 stage = GOT_FILEIDX_STAGE_ADD;
7938 else
7939 stage = GOT_FILEIDX_STAGE_MODIFY;
7940 got_fileindex_entry_stage_set(ie, stage);
7941 if (S_ISLNK(sb.st_mode)) {
7942 int is_bad_symlink = 0;
7943 if (!a->allow_bad_symlinks) {
7944 char target_path[PATH_MAX];
7945 ssize_t target_len;
7946 target_len = readlink(ondisk_path, target_path,
7947 sizeof(target_path));
7948 if (target_len == -1) {
7949 err = got_error_from_errno2("readlink",
7950 ondisk_path);
7951 break;
7953 err = is_bad_symlink_target(&is_bad_symlink,
7954 target_path, target_len, ondisk_path,
7955 a->worktree->root_path);
7956 if (err)
7957 break;
7958 if (is_bad_symlink) {
7959 err = got_error_path(ondisk_path,
7960 GOT_ERR_BAD_SYMLINK);
7961 break;
7964 if (is_bad_symlink)
7965 got_fileindex_entry_staged_filetype_set(ie,
7966 GOT_FILEIDX_MODE_BAD_SYMLINK);
7967 else
7968 got_fileindex_entry_staged_filetype_set(ie,
7969 GOT_FILEIDX_MODE_SYMLINK);
7970 } else {
7971 got_fileindex_entry_staged_filetype_set(ie,
7972 GOT_FILEIDX_MODE_REGULAR_FILE);
7974 a->staged_something = 1;
7975 if (a->status_cb == NULL)
7976 break;
7977 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7978 get_staged_status(ie), relpath, blob_id,
7979 new_staged_blob_id, NULL, dirfd, de_name);
7980 break;
7981 case GOT_STATUS_DELETE:
7982 if (staged_status == GOT_STATUS_DELETE)
7983 break;
7984 if (a->patch_cb) {
7985 int choice = GOT_PATCH_CHOICE_NONE;
7986 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7987 ie->path, NULL, 1, 1);
7988 if (err)
7989 break;
7990 if (choice == GOT_PATCH_CHOICE_NO)
7991 break;
7992 if (choice != GOT_PATCH_CHOICE_YES) {
7993 err = got_error(GOT_ERR_PATCH_CHOICE);
7994 break;
7997 stage = GOT_FILEIDX_STAGE_DELETE;
7998 got_fileindex_entry_stage_set(ie, stage);
7999 a->staged_something = 1;
8000 if (a->status_cb == NULL)
8001 break;
8002 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8003 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8004 de_name);
8005 break;
8006 case GOT_STATUS_NO_CHANGE:
8007 break;
8008 case GOT_STATUS_CONFLICT:
8009 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8010 break;
8011 case GOT_STATUS_NONEXISTENT:
8012 err = got_error_set_errno(ENOENT, relpath);
8013 break;
8014 default:
8015 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8016 break;
8019 if (path_content && unlink(path_content) == -1 && err == NULL)
8020 err = got_error_from_errno2("unlink", path_content);
8021 free(path_content);
8022 free(ondisk_path);
8023 free(new_staged_blob_id);
8024 return err;
8027 const struct got_error *
8028 got_worktree_stage(struct got_worktree *worktree,
8029 struct got_pathlist_head *paths,
8030 got_worktree_status_cb status_cb, void *status_arg,
8031 got_worktree_patch_cb patch_cb, void *patch_arg,
8032 int allow_bad_symlinks, struct got_repository *repo)
8034 const struct got_error *err = NULL, *sync_err, *unlockerr;
8035 struct got_pathlist_entry *pe;
8036 struct got_fileindex *fileindex = NULL;
8037 char *fileindex_path = NULL;
8038 struct got_reference *head_ref = NULL;
8039 struct got_object_id *head_commit_id = NULL;
8040 struct check_stage_ok_arg oka;
8041 struct stage_path_arg spa;
8043 err = lock_worktree(worktree, LOCK_EX);
8044 if (err)
8045 return err;
8047 err = got_ref_open(&head_ref, repo,
8048 got_worktree_get_head_ref_name(worktree), 0);
8049 if (err)
8050 goto done;
8051 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8052 if (err)
8053 goto done;
8054 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8055 if (err)
8056 goto done;
8058 /* Check pre-conditions before staging anything. */
8059 oka.head_commit_id = head_commit_id;
8060 oka.worktree = worktree;
8061 oka.fileindex = fileindex;
8062 oka.repo = repo;
8063 oka.have_changes = 0;
8064 TAILQ_FOREACH(pe, paths, entry) {
8065 err = worktree_status(worktree, pe->path, fileindex, repo,
8066 check_stage_ok, &oka, NULL, NULL, 1, 0);
8067 if (err)
8068 goto done;
8070 if (!oka.have_changes) {
8071 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8072 goto done;
8075 spa.worktree = worktree;
8076 spa.fileindex = fileindex;
8077 spa.repo = repo;
8078 spa.patch_cb = patch_cb;
8079 spa.patch_arg = patch_arg;
8080 spa.status_cb = status_cb;
8081 spa.status_arg = status_arg;
8082 spa.staged_something = 0;
8083 spa.allow_bad_symlinks = allow_bad_symlinks;
8084 TAILQ_FOREACH(pe, paths, entry) {
8085 err = worktree_status(worktree, pe->path, fileindex, repo,
8086 stage_path, &spa, NULL, NULL, 1, 0);
8087 if (err)
8088 goto done;
8090 if (!spa.staged_something) {
8091 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8092 goto done;
8095 sync_err = sync_fileindex(fileindex, fileindex_path);
8096 if (sync_err && err == NULL)
8097 err = sync_err;
8098 done:
8099 if (head_ref)
8100 got_ref_close(head_ref);
8101 free(head_commit_id);
8102 free(fileindex_path);
8103 if (fileindex)
8104 got_fileindex_free(fileindex);
8105 unlockerr = lock_worktree(worktree, LOCK_SH);
8106 if (unlockerr && err == NULL)
8107 err = unlockerr;
8108 return err;
8111 struct unstage_path_arg {
8112 struct got_worktree *worktree;
8113 struct got_fileindex *fileindex;
8114 struct got_repository *repo;
8115 got_worktree_checkout_cb progress_cb;
8116 void *progress_arg;
8117 got_worktree_patch_cb patch_cb;
8118 void *patch_arg;
8121 static const struct got_error *
8122 create_unstaged_content(char **path_unstaged_content,
8123 char **path_new_staged_content, struct got_object_id *blob_id,
8124 struct got_object_id *staged_blob_id, const char *relpath,
8125 struct got_repository *repo,
8126 got_worktree_patch_cb patch_cb, void *patch_arg)
8128 const struct got_error *err, *free_err;
8129 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8130 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8131 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8132 struct got_diffreg_result *diffreg_result = NULL;
8133 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8134 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8136 *path_unstaged_content = NULL;
8137 *path_new_staged_content = NULL;
8139 err = got_object_id_str(&label1, blob_id);
8140 if (err)
8141 return err;
8142 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8143 if (err)
8144 goto done;
8146 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8147 if (err)
8148 goto done;
8150 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8151 if (err)
8152 goto done;
8154 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8155 if (err)
8156 goto done;
8158 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8159 if (err)
8160 goto done;
8162 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8163 if (err)
8164 goto done;
8166 err = got_diff_files(&diffreg_result, f1, label1, f2,
8167 path2, 3, 0, 1, NULL);
8168 if (err)
8169 goto done;
8171 err = got_opentemp_named(path_unstaged_content, &outfile,
8172 "got-unstaged-content");
8173 if (err)
8174 goto done;
8175 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8176 "got-new-staged-content");
8177 if (err)
8178 goto done;
8180 if (fseek(f1, 0L, SEEK_SET) == -1) {
8181 err = got_ferror(f1, GOT_ERR_IO);
8182 goto done;
8184 if (fseek(f2, 0L, SEEK_SET) == -1) {
8185 err = got_ferror(f2, GOT_ERR_IO);
8186 goto done;
8188 /* Count the number of actual changes in the diff result. */
8189 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8190 struct diff_chunk_context cc = {};
8191 diff_chunk_context_load_change(&cc, &nchunks_used,
8192 diffreg_result->result, n, 0);
8193 nchanges++;
8195 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8196 int choice;
8197 err = apply_or_reject_change(&choice, &nchunks_used,
8198 diffreg_result->result, n, relpath, f1, f2,
8199 &line_cur1, &line_cur2,
8200 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8201 if (err)
8202 goto done;
8203 if (choice == GOT_PATCH_CHOICE_YES)
8204 have_content = 1;
8205 else
8206 have_rejected_content = 1;
8207 if (choice == GOT_PATCH_CHOICE_QUIT)
8208 break;
8210 if (have_content || have_rejected_content)
8211 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8212 outfile, rejectfile);
8213 done:
8214 free(label1);
8215 if (blob)
8216 got_object_blob_close(blob);
8217 if (staged_blob)
8218 got_object_blob_close(staged_blob);
8219 free_err = got_diffreg_result_free(diffreg_result);
8220 if (free_err && err == NULL)
8221 err = free_err;
8222 if (f1 && fclose(f1) == EOF && err == NULL)
8223 err = got_error_from_errno2("fclose", path1);
8224 if (f2 && fclose(f2) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", path2);
8226 if (outfile && fclose(outfile) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", *path_unstaged_content);
8228 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8229 err = got_error_from_errno2("fclose", *path_new_staged_content);
8230 if (path1 && unlink(path1) == -1 && err == NULL)
8231 err = got_error_from_errno2("unlink", path1);
8232 if (path2 && unlink(path2) == -1 && err == NULL)
8233 err = got_error_from_errno2("unlink", path2);
8234 if (err || !have_content) {
8235 if (*path_unstaged_content &&
8236 unlink(*path_unstaged_content) == -1 && err == NULL)
8237 err = got_error_from_errno2("unlink",
8238 *path_unstaged_content);
8239 free(*path_unstaged_content);
8240 *path_unstaged_content = NULL;
8242 if (err || !have_content || !have_rejected_content) {
8243 if (*path_new_staged_content &&
8244 unlink(*path_new_staged_content) == -1 && err == NULL)
8245 err = got_error_from_errno2("unlink",
8246 *path_new_staged_content);
8247 free(*path_new_staged_content);
8248 *path_new_staged_content = NULL;
8250 free(path1);
8251 free(path2);
8252 return err;
8255 static const struct got_error *
8256 unstage_hunks(struct got_object_id *staged_blob_id,
8257 struct got_blob_object *blob_base,
8258 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8259 const char *ondisk_path, const char *label_orig,
8260 struct got_worktree *worktree, struct got_repository *repo,
8261 got_worktree_patch_cb patch_cb, void *patch_arg,
8262 got_worktree_checkout_cb progress_cb, void *progress_arg)
8264 const struct got_error *err = NULL;
8265 char *path_unstaged_content = NULL;
8266 char *path_new_staged_content = NULL;
8267 char *parent = NULL, *base_path = NULL;
8268 char *blob_base_path = NULL;
8269 struct got_object_id *new_staged_blob_id = NULL;
8270 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8271 struct stat sb;
8273 err = create_unstaged_content(&path_unstaged_content,
8274 &path_new_staged_content, blob_id, staged_blob_id,
8275 ie->path, repo, patch_cb, patch_arg);
8276 if (err)
8277 return err;
8279 if (path_unstaged_content == NULL)
8280 return NULL;
8282 if (path_new_staged_content) {
8283 err = got_object_blob_create(&new_staged_blob_id,
8284 path_new_staged_content, repo);
8285 if (err)
8286 goto done;
8289 f = fopen(path_unstaged_content, "r");
8290 if (f == NULL) {
8291 err = got_error_from_errno2("fopen",
8292 path_unstaged_content);
8293 goto done;
8295 if (fstat(fileno(f), &sb) == -1) {
8296 err = got_error_from_errno2("fstat", path_unstaged_content);
8297 goto done;
8299 if (got_fileindex_entry_staged_filetype_get(ie) ==
8300 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8301 char link_target[PATH_MAX];
8302 size_t r;
8303 r = fread(link_target, 1, sizeof(link_target), f);
8304 if (r == 0 && ferror(f)) {
8305 err = got_error_from_errno("fread");
8306 goto done;
8308 if (r >= sizeof(link_target)) { /* should not happen */
8309 err = got_error(GOT_ERR_NO_SPACE);
8310 goto done;
8312 link_target[r] = '\0';
8313 err = merge_symlink(worktree, blob_base,
8314 ondisk_path, ie->path, label_orig, link_target,
8315 worktree->base_commit_id, repo, progress_cb,
8316 progress_arg);
8317 } else {
8318 int local_changes_subsumed;
8320 err = got_path_dirname(&parent, ondisk_path);
8321 if (err)
8322 return err;
8324 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8325 parent) == -1) {
8326 err = got_error_from_errno("asprintf");
8327 base_path = NULL;
8328 goto done;
8331 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8332 if (err)
8333 goto done;
8334 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8335 blob_base);
8336 if (err)
8337 goto done;
8340 * In order the run a 3-way merge with a symlink we copy the symlink's
8341 * target path into a temporary file and use that file with diff3.
8343 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8344 err = dump_symlink_target_path_to_file(&f_deriv2,
8345 ondisk_path);
8346 if (err)
8347 goto done;
8348 } else {
8349 int fd;
8350 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8351 if (fd == -1) {
8352 err = got_error_from_errno2("open", ondisk_path);
8353 goto done;
8355 f_deriv2 = fdopen(fd, "r");
8356 if (f_deriv2 == NULL) {
8357 err = got_error_from_errno2("fdopen", ondisk_path);
8358 close(fd);
8359 goto done;
8363 err = merge_file(&local_changes_subsumed, worktree,
8364 f_base, f, f_deriv2, ondisk_path, ie->path,
8365 got_fileindex_perms_to_st(ie),
8366 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8367 repo, progress_cb, progress_arg);
8369 if (err)
8370 goto done;
8372 if (new_staged_blob_id) {
8373 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8374 SHA1_DIGEST_LENGTH);
8375 } else {
8376 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8377 got_fileindex_entry_staged_filetype_set(ie, 0);
8379 done:
8380 free(new_staged_blob_id);
8381 if (path_unstaged_content &&
8382 unlink(path_unstaged_content) == -1 && err == NULL)
8383 err = got_error_from_errno2("unlink", path_unstaged_content);
8384 if (path_new_staged_content &&
8385 unlink(path_new_staged_content) == -1 && err == NULL)
8386 err = got_error_from_errno2("unlink", path_new_staged_content);
8387 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8388 err = got_error_from_errno2("unlink", blob_base_path);
8389 if (f_base && fclose(f_base) == EOF && err == NULL)
8390 err = got_error_from_errno2("fclose", path_unstaged_content);
8391 if (f && fclose(f) == EOF && err == NULL)
8392 err = got_error_from_errno2("fclose", path_unstaged_content);
8393 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8394 err = got_error_from_errno2("fclose", ondisk_path);
8395 free(path_unstaged_content);
8396 free(path_new_staged_content);
8397 free(blob_base_path);
8398 free(parent);
8399 free(base_path);
8400 return err;
8403 static const struct got_error *
8404 unstage_path(void *arg, unsigned char status,
8405 unsigned char staged_status, const char *relpath,
8406 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8407 struct got_object_id *commit_id, int dirfd, const char *de_name)
8409 const struct got_error *err = NULL;
8410 struct unstage_path_arg *a = arg;
8411 struct got_fileindex_entry *ie;
8412 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8413 char *ondisk_path = NULL;
8414 char *id_str = NULL, *label_orig = NULL;
8415 int local_changes_subsumed;
8416 struct stat sb;
8418 if (staged_status != GOT_STATUS_ADD &&
8419 staged_status != GOT_STATUS_MODIFY &&
8420 staged_status != GOT_STATUS_DELETE)
8421 return NULL;
8423 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8424 if (ie == NULL)
8425 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8427 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8428 == -1)
8429 return got_error_from_errno("asprintf");
8431 err = got_object_id_str(&id_str,
8432 commit_id ? commit_id : a->worktree->base_commit_id);
8433 if (err)
8434 goto done;
8435 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8436 id_str) == -1) {
8437 err = got_error_from_errno("asprintf");
8438 goto done;
8441 switch (staged_status) {
8442 case GOT_STATUS_MODIFY:
8443 err = got_object_open_as_blob(&blob_base, a->repo,
8444 blob_id, 8192);
8445 if (err)
8446 break;
8447 /* fall through */
8448 case GOT_STATUS_ADD:
8449 if (a->patch_cb) {
8450 if (staged_status == GOT_STATUS_ADD) {
8451 int choice = GOT_PATCH_CHOICE_NONE;
8452 err = (*a->patch_cb)(&choice, a->patch_arg,
8453 staged_status, ie->path, NULL, 1, 1);
8454 if (err)
8455 break;
8456 if (choice != GOT_PATCH_CHOICE_YES)
8457 break;
8458 } else {
8459 err = unstage_hunks(staged_blob_id,
8460 blob_base, blob_id, ie, ondisk_path,
8461 label_orig, a->worktree, a->repo,
8462 a->patch_cb, a->patch_arg,
8463 a->progress_cb, a->progress_arg);
8464 break; /* Done with this file. */
8467 err = got_object_open_as_blob(&blob_staged, a->repo,
8468 staged_blob_id, 8192);
8469 if (err)
8470 break;
8471 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8472 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8473 case GOT_FILEIDX_MODE_REGULAR_FILE:
8474 err = merge_blob(&local_changes_subsumed, a->worktree,
8475 blob_base, ondisk_path, relpath,
8476 got_fileindex_perms_to_st(ie), label_orig,
8477 blob_staged, commit_id ? commit_id :
8478 a->worktree->base_commit_id, a->repo,
8479 a->progress_cb, a->progress_arg);
8480 break;
8481 case GOT_FILEIDX_MODE_SYMLINK:
8482 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8483 char *staged_target;
8484 err = got_object_blob_read_to_str(
8485 &staged_target, blob_staged);
8486 if (err)
8487 goto done;
8488 err = merge_symlink(a->worktree, blob_base,
8489 ondisk_path, relpath, label_orig,
8490 staged_target, commit_id ? commit_id :
8491 a->worktree->base_commit_id,
8492 a->repo, a->progress_cb, a->progress_arg);
8493 free(staged_target);
8494 } else {
8495 err = merge_blob(&local_changes_subsumed,
8496 a->worktree, blob_base, ondisk_path,
8497 relpath, got_fileindex_perms_to_st(ie),
8498 label_orig, blob_staged,
8499 commit_id ? commit_id :
8500 a->worktree->base_commit_id, a->repo,
8501 a->progress_cb, a->progress_arg);
8503 break;
8504 default:
8505 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8506 break;
8508 if (err == NULL) {
8509 got_fileindex_entry_stage_set(ie,
8510 GOT_FILEIDX_STAGE_NONE);
8511 got_fileindex_entry_staged_filetype_set(ie, 0);
8513 break;
8514 case GOT_STATUS_DELETE:
8515 if (a->patch_cb) {
8516 int choice = GOT_PATCH_CHOICE_NONE;
8517 err = (*a->patch_cb)(&choice, a->patch_arg,
8518 staged_status, ie->path, NULL, 1, 1);
8519 if (err)
8520 break;
8521 if (choice == GOT_PATCH_CHOICE_NO)
8522 break;
8523 if (choice != GOT_PATCH_CHOICE_YES) {
8524 err = got_error(GOT_ERR_PATCH_CHOICE);
8525 break;
8528 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8529 got_fileindex_entry_staged_filetype_set(ie, 0);
8530 err = get_file_status(&status, &sb, ie, ondisk_path,
8531 dirfd, de_name, a->repo);
8532 if (err)
8533 break;
8534 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8535 break;
8537 done:
8538 free(ondisk_path);
8539 if (blob_base)
8540 got_object_blob_close(blob_base);
8541 if (blob_staged)
8542 got_object_blob_close(blob_staged);
8543 free(id_str);
8544 free(label_orig);
8545 return err;
8548 const struct got_error *
8549 got_worktree_unstage(struct got_worktree *worktree,
8550 struct got_pathlist_head *paths,
8551 got_worktree_checkout_cb progress_cb, void *progress_arg,
8552 got_worktree_patch_cb patch_cb, void *patch_arg,
8553 struct got_repository *repo)
8555 const struct got_error *err = NULL, *sync_err, *unlockerr;
8556 struct got_pathlist_entry *pe;
8557 struct got_fileindex *fileindex = NULL;
8558 char *fileindex_path = NULL;
8559 struct unstage_path_arg upa;
8561 err = lock_worktree(worktree, LOCK_EX);
8562 if (err)
8563 return err;
8565 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8566 if (err)
8567 goto done;
8569 upa.worktree = worktree;
8570 upa.fileindex = fileindex;
8571 upa.repo = repo;
8572 upa.progress_cb = progress_cb;
8573 upa.progress_arg = progress_arg;
8574 upa.patch_cb = patch_cb;
8575 upa.patch_arg = patch_arg;
8576 TAILQ_FOREACH(pe, paths, entry) {
8577 err = worktree_status(worktree, pe->path, fileindex, repo,
8578 unstage_path, &upa, NULL, NULL, 1, 0);
8579 if (err)
8580 goto done;
8583 sync_err = sync_fileindex(fileindex, fileindex_path);
8584 if (sync_err && err == NULL)
8585 err = sync_err;
8586 done:
8587 free(fileindex_path);
8588 if (fileindex)
8589 got_fileindex_free(fileindex);
8590 unlockerr = lock_worktree(worktree, LOCK_SH);
8591 if (unlockerr && err == NULL)
8592 err = unlockerr;
8593 return err;
8596 struct report_file_info_arg {
8597 struct got_worktree *worktree;
8598 got_worktree_path_info_cb info_cb;
8599 void *info_arg;
8600 struct got_pathlist_head *paths;
8601 got_cancel_cb cancel_cb;
8602 void *cancel_arg;
8605 static const struct got_error *
8606 report_file_info(void *arg, struct got_fileindex_entry *ie)
8608 struct report_file_info_arg *a = arg;
8609 struct got_pathlist_entry *pe;
8610 struct got_object_id blob_id, staged_blob_id, commit_id;
8611 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8612 struct got_object_id *commit_idp = NULL;
8613 int stage;
8615 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8616 return got_error(GOT_ERR_CANCELLED);
8618 TAILQ_FOREACH(pe, a->paths, entry) {
8619 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8620 got_path_is_child(ie->path, pe->path, pe->path_len))
8621 break;
8623 if (pe == NULL) /* not found */
8624 return NULL;
8626 if (got_fileindex_entry_has_blob(ie)) {
8627 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8628 blob_idp = &blob_id;
8630 stage = got_fileindex_entry_stage_get(ie);
8631 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8632 stage == GOT_FILEIDX_STAGE_ADD) {
8633 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8634 SHA1_DIGEST_LENGTH);
8635 staged_blob_idp = &staged_blob_id;
8638 if (got_fileindex_entry_has_commit(ie)) {
8639 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8640 commit_idp = &commit_id;
8643 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8644 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8647 const struct got_error *
8648 got_worktree_path_info(struct got_worktree *worktree,
8649 struct got_pathlist_head *paths,
8650 got_worktree_path_info_cb info_cb, void *info_arg,
8651 got_cancel_cb cancel_cb, void *cancel_arg)
8654 const struct got_error *err = NULL, *unlockerr;
8655 struct got_fileindex *fileindex = NULL;
8656 char *fileindex_path = NULL;
8657 struct report_file_info_arg arg;
8659 err = lock_worktree(worktree, LOCK_SH);
8660 if (err)
8661 return err;
8663 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8664 if (err)
8665 goto done;
8667 arg.worktree = worktree;
8668 arg.info_cb = info_cb;
8669 arg.info_arg = info_arg;
8670 arg.paths = paths;
8671 arg.cancel_cb = cancel_cb;
8672 arg.cancel_arg = cancel_arg;
8673 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8674 &arg);
8675 done:
8676 free(fileindex_path);
8677 if (fileindex)
8678 got_fileindex_free(fileindex);
8679 unlockerr = lock_worktree(worktree, LOCK_UN);
8680 if (unlockerr && err == NULL)
8681 err = unlockerr;
8682 return err;