Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig);
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv);
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 if (err)
995 goto done;
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 blob_orig);
998 if (err)
999 goto done;
1000 free(base_path);
1001 } else {
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1010 goto done;
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 base_path = NULL;
1017 goto done;
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 if (err)
1022 goto done;
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 blob_deriv);
1025 if (err)
1026 goto done;
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (err)
1030 goto done;
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 if (err)
1044 goto done;
1045 } else {
1046 int fd;
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 if (fd == -1) {
1049 err = got_error_from_errno2("open", ondisk_path);
1050 goto done;
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1055 close(fd);
1056 goto done;
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 done:
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1079 free(id_str);
1080 free(label_deriv);
1081 free(parent);
1082 return err;
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1093 *new_iep = NULL;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 else
1109 *new_iep = new_ie;
1110 return err;
1113 static mode_t
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1118 if (executable) {
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1121 xbits |= S_IXGRP;
1122 if (st_mode & S_IROTH)
1123 xbits |= S_IXOTH;
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1148 ssize_t elen;
1149 char etarget[PATH_MAX];
1150 int fd;
1152 *did_something = 0;
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 if (fd == -1) {
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 if (elen == -1)
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1177 *did_something = 1;
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1181 return err;
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1196 return NULL;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1209 if (err)
1210 return err;
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 free(parent);
1213 return got_error_from_errno("asprintf");
1215 free(parent);
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 free(abspath);
1219 return err;
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 free(abspath);
1223 if (err)
1224 return err;
1225 } else {
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 if (err)
1228 return err;
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1234 return NULL;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1244 free(path_got);
1245 return NULL;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1271 do {
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 goto done;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 goto done;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 goto done;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 goto done;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 goto done;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 done:
1374 free(path_got);
1375 return err;
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1387 int fd = -1;
1388 size_t len, hdrlen;
1389 int update = 0;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 if (fd == -1) {
1395 if (errno == ENOENT) {
1396 char *parent;
1397 err = got_path_dirname(&parent, path);
1398 if (err)
1399 return err;
1400 err = add_dir_on_disk(worktree, parent);
1401 free(parent);
1402 if (err)
1403 return err;
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1407 if (fd == -1)
1408 return got_error_from_errno2("open",
1409 ondisk_path);
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1414 goto done;
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 goto done;
1422 } else {
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1424 ondisk_path);
1425 if (err)
1426 goto done;
1427 update = 1;
1429 } else
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1436 goto done;
1439 if (progress_cb) {
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 path);
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 path);
1446 else
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 if (err)
1450 goto done;
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1454 do {
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 break;
1459 if (len > 0) {
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 if (outlen == -1) {
1463 err = got_error_from_errno("write");
1464 goto done;
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1467 goto done;
1469 hdrlen = 0;
1471 } while (len != 0);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1475 goto done;
1478 if (update) {
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1481 goto done;
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1485 ondisk_path);
1486 goto done;
1488 free(tmppath);
1489 tmppath = NULL;
1492 done:
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1497 free(tmppath);
1498 return err;
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1511 int i = 0;
1512 char *line = NULL;
1513 size_t linesize = 0;
1514 ssize_t linelen;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1519 if (feof(f))
1520 break;
1521 err = got_ferror(f, GOT_ERR_IO);
1522 break;
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 == 0)
1528 *status = GOT_STATUS_CONFLICT;
1529 else
1530 i++;
1533 free(line);
1535 return err;
1538 static int
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 static int
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1566 default:
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1579 ssize_t elen;
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1587 do {
1588 err = got_object_blob_read_block(&len, blob);
1589 if (err)
1590 return err;
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1598 if (len > 0) {
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1601 len - hdrlen);
1602 target_len += len - hdrlen;
1603 hdrlen = 0;
1605 } while (len != 0);
1606 target_path[target_len] = '\0';
1608 if (dirfd != -1) {
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 if (elen == -1)
1611 return got_error_from_errno2("readlinkat", abspath);
1612 } else {
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1614 if (elen == -1)
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1621 return NULL;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1631 size_t hdrlen;
1632 int fd = -1;
1633 FILE *f = NULL;
1634 uint8_t fbuf[8192];
1635 struct got_blob_object *blob = NULL;
1636 size_t flen, blen;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1647 if (dirfd != -1) {
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1652 else
1653 *status = GOT_STATUS_DELETE;
1654 goto done;
1656 err = got_error_from_errno2("fstatat", abspath);
1657 goto done;
1659 } else {
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1671 else
1672 *status = GOT_STATUS_DELETE;
1673 goto done;
1675 err = got_error_from_errno2("fstat", abspath);
1676 goto done;
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1682 goto done;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1691 goto done;
1694 if (!stat_info_differs(ie, sb))
1695 goto done;
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1700 goto done;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 else
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (err)
1711 goto done;
1713 if (S_ISLNK(sb->st_mode)) {
1714 err = get_symlink_modification_status(status, ie,
1715 abspath, dirfd, de_name, blob);
1716 goto done;
1719 if (dirfd != -1) {
1720 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 if (fd == -1) {
1722 err = got_error_from_errno2("openat", abspath);
1723 goto done;
1727 f = fdopen(fd, "r");
1728 if (f == NULL) {
1729 err = got_error_from_errno2("fdopen", abspath);
1730 goto done;
1732 fd = -1;
1733 hdrlen = got_object_blob_get_hdrlen(blob);
1734 for (;;) {
1735 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 err = got_object_blob_read_block(&blen, blob);
1737 if (err)
1738 goto done;
1739 /* Skip length of blob object header first time around. */
1740 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 if (flen == 0 && ferror(f)) {
1742 err = got_error_from_errno("fread");
1743 goto done;
1745 if (blen - hdrlen == 0) {
1746 if (flen != 0)
1747 *status = GOT_STATUS_MODIFY;
1748 break;
1749 } else if (flen == 0) {
1750 if (blen - hdrlen != 0)
1751 *status = GOT_STATUS_MODIFY;
1752 break;
1753 } else if (blen - hdrlen == flen) {
1754 /* Skip blob object header first time around. */
1755 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1759 } else {
1760 *status = GOT_STATUS_MODIFY;
1761 break;
1763 hdrlen = 0;
1766 if (*status == GOT_STATUS_MODIFY) {
1767 rewind(f);
1768 err = get_modified_file_content_status(status, f);
1769 } else if (xbit_differs(ie, sb->st_mode))
1770 *status = GOT_STATUS_MODE_CHANGE;
1771 done:
1772 if (blob)
1773 got_object_blob_close(blob);
1774 if (f != NULL && fclose(f) == EOF && err == NULL)
1775 err = got_error_from_errno2("fclose", abspath);
1776 if (fd != -1 && close(fd) == -1 && err == NULL)
1777 err = got_error_from_errno2("close", abspath);
1778 return err;
1782 * Update timestamps in the file index if a file is unmodified and
1783 * we had to run a full content comparison to find out.
1785 static const struct got_error *
1786 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 struct got_fileindex_entry *ie, struct stat *sb)
1789 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 return got_fileindex_entry_update(ie, wt_fd, path,
1791 ie->blob_sha1, ie->commit_sha1, 1);
1793 return NULL;
1796 static const struct got_error *
1797 update_blob(struct got_worktree *worktree,
1798 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 struct got_tree_entry *te, const char *path,
1800 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 void *progress_arg)
1803 const struct got_error *err = NULL;
1804 struct got_blob_object *blob = NULL;
1805 char *ondisk_path;
1806 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 struct stat sb;
1809 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 return got_error_from_errno("asprintf");
1812 if (ie) {
1813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 goto done;
1817 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 repo);
1819 if (err)
1820 goto done;
1821 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 sb.st_mode = got_fileindex_perms_to_st(ie);
1823 } else {
1824 if (stat(ondisk_path, &sb) == -1) {
1825 if (errno != ENOENT) {
1826 err = got_error_from_errno2("stat",
1827 ondisk_path);
1828 goto done;
1830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 status = GOT_STATUS_UNVERSIONED;
1832 } else {
1833 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 status = GOT_STATUS_UNVERSIONED;
1835 else
1836 status = GOT_STATUS_OBSTRUCTED;
1840 if (status == GOT_STATUS_OBSTRUCTED) {
1841 if (ie)
1842 got_fileindex_entry_mark_skipped(ie);
1843 err = (*progress_cb)(progress_arg, status, path);
1844 goto done;
1846 if (status == GOT_STATUS_CONFLICT) {
1847 if (ie)
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 path);
1851 goto done;
1854 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 (S_ISLNK(te->mode) ||
1856 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 * This is a regular file or an installed bad symlink.
1859 * If the file index indicates that this file is already
1860 * up-to-date with respect to the repository we can skip
1861 * updating contents of this file.
1863 if (got_fileindex_entry_has_commit(ie) &&
1864 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH) == 0) {
1866 /* Same commit. */
1867 err = sync_timestamps(worktree->root_fd,
1868 path, status, ie, &sb);
1869 if (err)
1870 goto done;
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 path);
1873 goto done;
1875 if (got_fileindex_entry_has_blob(ie) &&
1876 memcmp(ie->blob_sha1, te->id.sha1,
1877 SHA1_DIGEST_LENGTH) == 0) {
1878 /* Different commit but the same blob. */
1879 err = sync_timestamps(worktree->root_fd,
1880 path, status, ie, &sb);
1881 if (err)
1882 goto done;
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 path);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 char *link_target;
1920 err = got_object_blob_read_to_str(&link_target, blob);
1921 if (err)
1922 goto done;
1923 err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 label_orig, link_target, worktree->base_commit_id,
1925 repo, progress_cb, progress_arg);
1926 free(link_target);
1927 } else {
1928 err = merge_blob(&update_timestamps, worktree, blob2,
1929 ondisk_path, path, sb.st_mode, label_orig, blob,
1930 worktree->base_commit_id, repo,
1931 progress_cb, progress_arg);
1933 free(label_orig);
1934 if (blob2)
1935 got_object_blob_close(blob2);
1936 if (err)
1937 goto done;
1939 * Do not update timestamps of files with local changes.
1940 * Otherwise, a future status walk would treat them as
1941 * unmodified files again.
1943 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 blob->id.sha1, worktree->base_commit_id->sha1,
1945 update_timestamps);
1946 } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 } else if (status == GOT_STATUS_DELETE) {
1950 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 if (err)
1952 goto done;
1953 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 if (err)
1956 goto done;
1957 } else {
1958 int is_bad_symlink = 0;
1959 if (S_ISLNK(te->mode)) {
1960 err = install_symlink(&is_bad_symlink, worktree,
1961 ondisk_path, path, blob,
1962 status == GOT_STATUS_MISSING, 0,
1963 status == GOT_STATUS_UNVERSIONED, 0,
1964 repo, progress_cb, progress_arg);
1965 } else {
1966 err = install_blob(worktree, ondisk_path, path,
1967 te->mode, sb.st_mode, blob,
1968 status == GOT_STATUS_MISSING, 0, 0,
1969 status == GOT_STATUS_UNVERSIONED, repo,
1970 progress_cb, progress_arg);
1972 if (err)
1973 goto done;
1975 if (ie) {
1976 err = got_fileindex_entry_update(ie,
1977 worktree->root_fd, path, blob->id.sha1,
1978 worktree->base_commit_id->sha1, 1);
1979 } else {
1980 err = create_fileindex_entry(&ie, fileindex,
1981 worktree->base_commit_id, worktree->root_fd, path,
1982 &blob->id);
1984 if (err)
1985 goto done;
1987 if (is_bad_symlink) {
1988 got_fileindex_entry_filetype_set(ie,
1989 GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 got_object_blob_close(blob);
1993 done:
1994 free(ondisk_path);
1995 return err;
1998 static const struct got_error *
1999 remove_ondisk_file(const char *root_path, const char *path)
2001 const struct got_error *err = NULL;
2002 char *ondisk_path = NULL, *parent = NULL;
2004 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (unlink(ondisk_path) == -1) {
2008 if (errno != ENOENT)
2009 err = got_error_from_errno2("unlink", ondisk_path);
2010 } else {
2011 size_t root_len = strlen(root_path);
2012 err = got_path_dirname(&parent, ondisk_path);
2013 if (err)
2014 goto done;
2015 while (got_path_cmp(parent, root_path,
2016 strlen(parent), root_len) != 0) {
2017 free(ondisk_path);
2018 ondisk_path = parent;
2019 parent = NULL;
2020 if (rmdir(ondisk_path) == -1) {
2021 if (errno != ENOTEMPTY)
2022 err = got_error_from_errno2("rmdir",
2023 ondisk_path);
2024 break;
2026 err = got_path_dirname(&parent, ondisk_path);
2027 if (err)
2028 break;
2031 done:
2032 free(ondisk_path);
2033 free(parent);
2034 return err;
2037 static const struct got_error *
2038 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 struct got_fileindex_entry *ie, struct got_repository *repo,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 const struct got_error *err = NULL;
2043 unsigned char status;
2044 struct stat sb;
2045 char *ondisk_path;
2047 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 == -1)
2052 return got_error_from_errno("asprintf");
2054 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (err)
2056 goto done;
2058 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 char ondisk_target[PATH_MAX];
2060 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 sizeof(ondisk_target));
2062 if (ondisk_len == -1) {
2063 err = got_error_from_errno2("readlink", ondisk_path);
2064 goto done;
2066 ondisk_target[ondisk_len] = '\0';
2067 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 NULL, NULL, /* XXX pass common ancestor info? */
2069 ondisk_target, ondisk_path);
2070 if (err)
2071 goto done;
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 ie->path);
2074 goto done;
2077 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 status == GOT_STATUS_ADD) {
2079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 if (err)
2081 goto done;
2083 * Preserve the working file and change the deleted blob's
2084 * entry into a schedule-add entry.
2086 err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 ie->path, NULL, NULL, 0);
2088 } else {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (err)
2091 goto done;
2092 if (status == GOT_STATUS_NO_CHANGE) {
2093 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 if (err)
2095 goto done;
2097 got_fileindex_entry_remove(fileindex, ie);
2099 done:
2100 free(ondisk_path);
2101 return err;
2104 struct diff_cb_arg {
2105 struct got_fileindex *fileindex;
2106 struct got_worktree *worktree;
2107 struct got_repository *repo;
2108 got_worktree_checkout_cb progress_cb;
2109 void *progress_arg;
2110 got_cancel_cb cancel_cb;
2111 void *cancel_arg;
2114 static const struct got_error *
2115 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 struct got_tree_entry *te, const char *parent_path)
2118 struct diff_cb_arg *a = arg;
2120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 return got_error(GOT_ERR_CANCELLED);
2123 return update_blob(a->worktree, a->fileindex, ie, te,
2124 ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 static const struct got_error *
2128 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 struct diff_cb_arg *a = arg;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 return delete_blob(a->worktree, a->fileindex, ie,
2136 a->repo, a->progress_cb, a->progress_arg);
2139 static const struct got_error *
2140 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2143 const struct got_error *err;
2144 char *path;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (got_object_tree_entry_is_submodule(te))
2150 return NULL;
2152 if (asprintf(&path, "%s%s%s", parent_path,
2153 parent_path[0] ? "/" : "", te->name)
2154 == -1)
2155 return got_error_from_errno("asprintf");
2157 if (S_ISDIR(te->mode))
2158 err = add_dir_on_disk(a->worktree, path);
2159 else
2160 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 a->repo, a->progress_cb, a->progress_arg);
2163 free(path);
2164 return err;
2167 const struct got_error *
2168 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 uint32_t uuid_status;
2172 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 if (uuid_status != uuid_s_ok) {
2174 *uuidstr = NULL;
2175 return got_error_uuid(uuid_status, "uuid_to_string");
2178 return NULL;
2181 static const struct got_error *
2182 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 const struct got_error *err = NULL;
2185 char *uuidstr = NULL;
2187 *refname = NULL;
2189 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (err)
2191 return err;
2193 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 err = got_error_from_errno("asprintf");
2195 *refname = NULL;
2197 free(uuidstr);
2198 return err;
2201 const struct got_error *
2202 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 static const struct got_error *
2208 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 return get_ref_name(refname, worktree,
2211 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 static const struct got_error *
2215 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 static const struct got_error *
2221 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 static const struct got_error *
2242 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 static const struct got_error *
2249 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 static const struct got_error *
2256 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 const struct got_error *
2263 got_worktree_get_histedit_script_path(char **path,
2264 struct got_worktree *worktree)
2266 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 *path = NULL;
2269 return got_error_from_errno("asprintf");
2271 return NULL;
2274 static const struct got_error *
2275 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 * Prevent Git's garbage collector from deleting our base commit by
2290 * setting a reference to our base commit's ID.
2292 static const struct got_error *
2293 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_reference *ref = NULL;
2297 char *refname;
2299 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 if (err)
2301 return err;
2303 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 if (err)
2305 goto done;
2307 err = got_ref_write(ref, repo);
2308 done:
2309 free(refname);
2310 if (ref)
2311 got_ref_close(ref);
2312 return err;
2315 static const struct got_error *
2316 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2320 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 err = got_error_from_errno("asprintf");
2323 *fileindex_path = NULL;
2325 return err;
2329 static const struct got_error *
2330 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 struct got_worktree *worktree)
2333 const struct got_error *err = NULL;
2334 FILE *index = NULL;
2336 *fileindex_path = NULL;
2337 *fileindex = got_fileindex_alloc();
2338 if (*fileindex == NULL)
2339 return got_error_from_errno("got_fileindex_alloc");
2341 err = get_fileindex_path(fileindex_path, worktree);
2342 if (err)
2343 goto done;
2345 index = fopen(*fileindex_path, "rbe");
2346 if (index == NULL) {
2347 if (errno != ENOENT)
2348 err = got_error_from_errno2("fopen", *fileindex_path);
2349 } else {
2350 err = got_fileindex_read(*fileindex, index);
2351 if (fclose(index) == EOF && err == NULL)
2352 err = got_error_from_errno("fclose");
2354 done:
2355 if (err) {
2356 free(*fileindex_path);
2357 *fileindex_path = NULL;
2358 got_fileindex_free(*fileindex);
2359 *fileindex = NULL;
2361 return err;
2364 struct bump_base_commit_id_arg {
2365 struct got_object_id *base_commit_id;
2366 const char *path;
2367 size_t path_len;
2368 const char *entry_name;
2369 got_worktree_checkout_cb progress_cb;
2370 void *progress_arg;
2373 /* Bump base commit ID of all files within an updated part of the work tree. */
2374 static const struct got_error *
2375 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 const struct got_error *err;
2378 struct bump_base_commit_id_arg *a = arg;
2380 if (a->entry_name) {
2381 if (strcmp(ie->path, a->path) != 0)
2382 return NULL;
2383 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 return NULL;
2386 if (got_fileindex_entry_was_skipped(ie))
2387 return NULL;
2389 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 SHA1_DIGEST_LENGTH) == 0)
2391 return NULL;
2393 if (a->progress_cb) {
2394 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 ie->path);
2396 if (err)
2397 return err;
2399 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 return NULL;
2403 static const struct got_error *
2404 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 struct got_fileindex *fileindex,
2406 got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 struct bump_base_commit_id_arg bbc_arg;
2410 bbc_arg.base_commit_id = worktree->base_commit_id;
2411 bbc_arg.entry_name = NULL;
2412 bbc_arg.path = "";
2413 bbc_arg.path_len = 0;
2414 bbc_arg.progress_cb = progress_cb;
2415 bbc_arg.progress_arg = progress_arg;
2417 return got_fileindex_for_each_entry_safe(fileindex,
2418 bump_base_commit_id, &bbc_arg);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 fileindex_path);
2431 if (err)
2432 goto done;
2434 err = got_fileindex_write(fileindex, new_index);
2435 if (err)
2436 goto done;
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2440 fileindex_path);
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2449 timeout.tv_sec = 0;
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2452 done:
2453 if (new_index)
2454 fclose(new_index);
2455 free(new_fileindex_path);
2456 return err;
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2471 *tree_id = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2483 if (err)
2484 goto done;
2485 return NULL;
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2493 goto done;
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 in_repo_path);
2498 if (err)
2499 goto done;
2501 free(in_repo_path);
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2505 if (err)
2506 goto done;
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 } else {
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (err)
2526 return err;
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(tree_id, repo,
2535 worktree->base_commit_id, in_repo_path);
2536 } else {
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2541 goto done;
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2549 done:
2550 free(id);
2551 free(in_repo_path);
2552 if (err) {
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2556 free(*tree_id);
2557 *tree_id = NULL;
2559 return err;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2575 if (err) {
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2578 goto done;
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 if (err)
2582 return err;
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2587 if (err)
2588 goto done;
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2591 if (err)
2592 goto done;
2594 if (entry_name &&
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 goto done;
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2605 arg.repo = repo;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2612 done:
2613 if (tree)
2614 got_object_tree_close(tree);
2615 if (commit)
2616 got_object_commit_close(commit);
2617 return err;
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 STAILQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2635 int entry_type;
2636 char *relpath;
2637 char *entry_name;
2638 } *tpd = NULL;
2639 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 STAILQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2644 if (err)
2645 return err;
2647 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2650 if (tpd == NULL) {
2651 err = got_error_from_errno("malloc");
2652 goto done;
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 if (err) {
2658 free(tpd);
2659 goto done;
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2664 if (err) {
2665 free(tpd->relpath);
2666 free(tpd->tree_id);
2667 free(tpd);
2668 goto done;
2670 } else
2671 tpd->entry_name = NULL;
2673 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 if (err)
2683 goto done;
2685 tpd = STAILQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 if (err)
2693 break;
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2703 if (err)
2704 break;
2706 tpd = STAILQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2710 err = sync_err;
2711 done:
2712 free(fileindex_path);
2713 if (tree)
2714 got_object_tree_close(tree);
2715 if (commit)
2716 got_object_commit_close(commit);
2717 if (fileindex)
2718 got_fileindex_free(fileindex);
2719 while (!STAILQ_EMPTY(&tree_paths)) {
2720 tpd = STAILQ_FIRST(&tree_paths);
2721 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2728 err = unlockerr;
2729 return err;
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2736 void *progress_arg;
2737 got_cancel_cb cancel_cb;
2738 void *cancel_arg;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2741 int allow_bad_symlinks;
2744 static const struct got_error *
2745 merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 struct got_blob_object *blob2, struct got_object_id *id1,
2747 struct got_object_id *id2, const char *path1, const char *path2,
2748 mode_t mode1, mode_t mode2, struct got_repository *repo)
2750 static const struct got_error *err = NULL;
2751 struct merge_file_cb_arg *a = arg;
2752 struct got_fileindex_entry *ie;
2753 char *ondisk_path = NULL;
2754 struct stat sb;
2755 unsigned char status;
2756 int local_changes_subsumed;
2757 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 char *id_str = NULL, *label_deriv2 = NULL;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 strlen(path2));
2763 if (ie == NULL)
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 path2) == -1)
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 repo);
2773 if (err)
2774 goto done;
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2779 goto done;
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 goto done;
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 char *link_target2;
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 if (err)
2793 goto done;
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2797 free(link_target2);
2798 } else {
2799 int fd;
2801 f_orig = got_opentemp();
2802 if (f_orig == NULL) {
2803 err = got_error_from_errno("got_opentemp");
2804 goto done;
2806 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2807 f_orig, blob1);
2808 if (err)
2809 goto done;
2811 f_deriv2 = got_opentemp();
2812 if (f_deriv2 == NULL)
2813 goto done;
2814 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2815 f_deriv2, blob2);
2816 if (err)
2817 goto done;
2819 fd = open(ondisk_path,
2820 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2821 if (fd == -1) {
2822 err = got_error_from_errno2("open",
2823 ondisk_path);
2824 goto done;
2826 f_deriv = fdopen(fd, "r");
2827 if (f_deriv == NULL) {
2828 err = got_error_from_errno2("fdopen",
2829 ondisk_path);
2830 close(fd);
2831 goto done;
2833 err = got_object_id_str(&id_str, a->commit_id2);
2834 if (err)
2835 goto done;
2836 if (asprintf(&label_deriv2, "%s: commit %s",
2837 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2838 err = got_error_from_errno("asprintf");
2839 goto done;
2841 err = merge_file(&local_changes_subsumed, a->worktree,
2842 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2843 sb.st_mode, a->label_orig, NULL, label_deriv2,
2844 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2845 a->progress_cb, a->progress_arg);
2847 } else if (blob1) {
2848 ie = got_fileindex_entry_get(a->fileindex, path1,
2849 strlen(path1));
2850 if (ie == NULL)
2851 return (*a->progress_cb)(a->progress_arg,
2852 GOT_STATUS_MISSING, path1);
2854 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 path1) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2859 repo);
2860 if (err)
2861 goto done;
2863 switch (status) {
2864 case GOT_STATUS_NO_CHANGE:
2865 err = (*a->progress_cb)(a->progress_arg,
2866 GOT_STATUS_DELETE, path1);
2867 if (err)
2868 goto done;
2869 err = remove_ondisk_file(a->worktree->root_path, path1);
2870 if (err)
2871 goto done;
2872 if (ie)
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2874 break;
2875 case GOT_STATUS_DELETE:
2876 case GOT_STATUS_MISSING:
2877 err = (*a->progress_cb)(a->progress_arg,
2878 GOT_STATUS_DELETE, path1);
2879 if (err)
2880 goto done;
2881 if (ie)
2882 got_fileindex_entry_mark_deleted_from_disk(ie);
2883 break;
2884 case GOT_STATUS_ADD: {
2885 struct got_object_id *id;
2886 FILE *blob1_f;
2887 off_t blob1_size;
2889 * Delete the added file only if its content already
2890 * exists in the repository.
2892 err = got_object_blob_file_create(&id, &blob1_f,
2893 &blob1_size, path1);
2894 if (err)
2895 goto done;
2896 if (got_object_id_cmp(id, id1) == 0) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_DELETE, path1);
2899 if (err)
2900 goto done;
2901 err = remove_ondisk_file(a->worktree->root_path,
2902 path1);
2903 if (err)
2904 goto done;
2905 if (ie)
2906 got_fileindex_entry_remove(a->fileindex,
2907 ie);
2908 } else {
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_CANNOT_DELETE, path1);
2912 if (fclose(blob1_f) == EOF && err == NULL)
2913 err = got_error_from_errno("fclose");
2914 free(id);
2915 if (err)
2916 goto done;
2917 break;
2919 case GOT_STATUS_MODIFY:
2920 case GOT_STATUS_CONFLICT:
2921 err = (*a->progress_cb)(a->progress_arg,
2922 GOT_STATUS_CANNOT_DELETE, path1);
2923 if (err)
2924 goto done;
2925 break;
2926 case GOT_STATUS_OBSTRUCTED:
2927 err = (*a->progress_cb)(a->progress_arg, status, path1);
2928 if (err)
2929 goto done;
2930 break;
2931 default:
2932 break;
2934 } else if (blob2) {
2935 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2936 path2) == -1)
2937 return got_error_from_errno("asprintf");
2938 ie = got_fileindex_entry_get(a->fileindex, path2,
2939 strlen(path2));
2940 if (ie) {
2941 err = get_file_status(&status, &sb, ie, ondisk_path,
2942 -1, NULL, repo);
2943 if (err)
2944 goto done;
2945 if (status != GOT_STATUS_NO_CHANGE &&
2946 status != GOT_STATUS_MODIFY &&
2947 status != GOT_STATUS_CONFLICT &&
2948 status != GOT_STATUS_ADD) {
2949 err = (*a->progress_cb)(a->progress_arg,
2950 status, path2);
2951 goto done;
2953 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2954 char *link_target2;
2955 err = got_object_blob_read_to_str(&link_target2,
2956 blob2);
2957 if (err)
2958 goto done;
2959 err = merge_symlink(a->worktree, NULL,
2960 ondisk_path, path2, a->label_orig,
2961 link_target2, a->commit_id2, repo,
2962 a->progress_cb, a->progress_arg);
2963 free(link_target2);
2964 } else if (S_ISREG(sb.st_mode)) {
2965 err = merge_blob(&local_changes_subsumed,
2966 a->worktree, NULL, ondisk_path, path2,
2967 sb.st_mode, a->label_orig, blob2,
2968 a->commit_id2, repo, a->progress_cb,
2969 a->progress_arg);
2970 } else {
2971 err = got_error_path(ondisk_path,
2972 GOT_ERR_FILE_OBSTRUCTED);
2974 if (err)
2975 goto done;
2976 if (status == GOT_STATUS_DELETE) {
2977 err = got_fileindex_entry_update(ie,
2978 a->worktree->root_fd, path2, blob2->id.sha1,
2979 a->worktree->base_commit_id->sha1, 0);
2980 if (err)
2981 goto done;
2983 } else {
2984 int is_bad_symlink = 0;
2985 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2986 if (S_ISLNK(mode2)) {
2987 err = install_symlink(&is_bad_symlink,
2988 a->worktree, ondisk_path, path2, blob2, 0,
2989 0, 1, a->allow_bad_symlinks, repo,
2990 a->progress_cb, a->progress_arg);
2991 } else {
2992 err = install_blob(a->worktree, ondisk_path, path2,
2993 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2994 a->progress_cb, a->progress_arg);
2996 if (err)
2997 goto done;
2998 err = got_fileindex_entry_alloc(&ie, path2);
2999 if (err)
3000 goto done;
3001 err = got_fileindex_entry_update(ie,
3002 a->worktree->root_fd, path2, NULL, NULL, 1);
3003 if (err) {
3004 got_fileindex_entry_free(ie);
3005 goto done;
3007 err = got_fileindex_entry_add(a->fileindex, ie);
3008 if (err) {
3009 got_fileindex_entry_free(ie);
3010 goto done;
3012 if (is_bad_symlink) {
3013 got_fileindex_entry_filetype_set(ie,
3014 GOT_FILEIDX_MODE_BAD_SYMLINK);
3018 done:
3019 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3024 err = got_error_from_errno("fclose");
3025 free(id_str);
3026 free(label_deriv2);
3027 free(ondisk_path);
3028 return err;
3031 static const struct got_error *
3032 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3034 struct got_worktree *worktree = arg;
3036 /* Reject merges into a work tree with mixed base commits. */
3037 if (got_fileindex_entry_has_commit(ie) &&
3038 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3039 SHA1_DIGEST_LENGTH) != 0)
3040 return got_error(GOT_ERR_MIXED_COMMITS);
3042 return NULL;
3045 struct check_merge_conflicts_arg {
3046 struct got_worktree *worktree;
3047 struct got_fileindex *fileindex;
3048 struct got_repository *repo;
3051 static const struct got_error *
3052 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3053 struct got_blob_object *blob2, struct got_object_id *id1,
3054 struct got_object_id *id2, const char *path1, const char *path2,
3055 mode_t mode1, mode_t mode2, struct got_repository *repo)
3057 const struct got_error *err = NULL;
3058 struct check_merge_conflicts_arg *a = arg;
3059 unsigned char status;
3060 struct stat sb;
3061 struct got_fileindex_entry *ie;
3062 const char *path = path2 ? path2 : path1;
3063 struct got_object_id *id = id2 ? id2 : id1;
3064 char *ondisk_path;
3066 if (id == NULL)
3067 return NULL;
3069 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3070 if (ie == NULL)
3071 return NULL;
3073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3074 == -1)
3075 return got_error_from_errno("asprintf");
3077 /* Reject merges into a work tree with conflicted files. */
3078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3079 free(ondisk_path);
3080 if (err)
3081 return err;
3082 if (status == GOT_STATUS_CONFLICT)
3083 return got_error(GOT_ERR_CONFLICTS);
3085 return NULL;
3088 static const struct got_error *
3089 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3090 const char *fileindex_path, struct got_object_id *commit_id1,
3091 struct got_object_id *commit_id2, struct got_repository *repo,
3092 got_worktree_checkout_cb progress_cb, void *progress_arg,
3093 got_cancel_cb cancel_cb, void *cancel_arg)
3095 const struct got_error *err = NULL, *sync_err;
3096 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 struct check_merge_conflicts_arg cmc_arg;
3099 struct merge_file_cb_arg arg;
3100 char *label_orig = NULL;
3102 if (commit_id1) {
3103 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3104 worktree->path_prefix);
3105 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3106 goto done;
3108 if (tree_id1) {
3109 char *id_str;
3111 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3112 if (err)
3113 goto done;
3115 err = got_object_id_str(&id_str, commit_id1);
3116 if (err)
3117 goto done;
3119 if (asprintf(&label_orig, "%s: commit %s",
3120 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3121 err = got_error_from_errno("asprintf");
3122 free(id_str);
3123 goto done;
3125 free(id_str);
3128 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3129 worktree->path_prefix);
3130 if (err)
3131 goto done;
3133 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3134 if (err)
3135 goto done;
3137 cmc_arg.worktree = worktree;
3138 cmc_arg.fileindex = fileindex;
3139 cmc_arg.repo = repo;
3140 err = got_diff_tree(tree1, tree2, "", "", repo,
3141 check_merge_conflicts, &cmc_arg, 0);
3142 if (err)
3143 goto done;
3145 arg.worktree = worktree;
3146 arg.fileindex = fileindex;
3147 arg.progress_cb = progress_cb;
3148 arg.progress_arg = progress_arg;
3149 arg.cancel_cb = cancel_cb;
3150 arg.cancel_arg = cancel_arg;
3151 arg.label_orig = label_orig;
3152 arg.commit_id2 = commit_id2;
3153 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3154 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3155 sync_err = sync_fileindex(fileindex, fileindex_path);
3156 if (sync_err && err == NULL)
3157 err = sync_err;
3158 done:
3159 if (tree1)
3160 got_object_tree_close(tree1);
3161 if (tree2)
3162 got_object_tree_close(tree2);
3163 free(label_orig);
3164 return err;
3167 const struct got_error *
3168 got_worktree_merge_files(struct got_worktree *worktree,
3169 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3170 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3171 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3173 const struct got_error *err, *unlockerr;
3174 char *fileindex_path = NULL;
3175 struct got_fileindex *fileindex = NULL;
3177 err = lock_worktree(worktree, LOCK_EX);
3178 if (err)
3179 return err;
3181 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3182 if (err)
3183 goto done;
3185 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3186 worktree);
3187 if (err)
3188 goto done;
3190 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3191 commit_id2, repo, progress_cb, progress_arg,
3192 cancel_cb, cancel_arg);
3193 done:
3194 if (fileindex)
3195 got_fileindex_free(fileindex);
3196 free(fileindex_path);
3197 unlockerr = lock_worktree(worktree, LOCK_SH);
3198 if (unlockerr && err == NULL)
3199 err = unlockerr;
3200 return err;
3203 struct diff_dir_cb_arg {
3204 struct got_fileindex *fileindex;
3205 struct got_worktree *worktree;
3206 const char *status_path;
3207 size_t status_path_len;
3208 struct got_repository *repo;
3209 got_worktree_status_cb status_cb;
3210 void *status_arg;
3211 got_cancel_cb cancel_cb;
3212 void *cancel_arg;
3213 /* A pathlist containing per-directory pathlists of ignore patterns. */
3214 struct got_pathlist_head *ignores;
3215 int report_unchanged;
3216 int no_ignores;
3219 static const struct got_error *
3220 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3221 int dirfd, const char *de_name,
3222 got_worktree_status_cb status_cb, void *status_arg,
3223 struct got_repository *repo, int report_unchanged)
3225 const struct got_error *err = NULL;
3226 unsigned char status = GOT_STATUS_NO_CHANGE;
3227 unsigned char staged_status = get_staged_status(ie);
3228 struct stat sb;
3229 struct got_object_id blob_id, commit_id, staged_blob_id;
3230 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3231 struct got_object_id *staged_blob_idp = NULL;
3233 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3234 if (err)
3235 return err;
3237 if (status == GOT_STATUS_NO_CHANGE &&
3238 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3239 return NULL;
3241 if (got_fileindex_entry_has_blob(ie)) {
3242 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3243 blob_idp = &blob_id;
3245 if (got_fileindex_entry_has_commit(ie)) {
3246 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3247 commit_idp = &commit_id;
3249 if (staged_status == GOT_STATUS_ADD ||
3250 staged_status == GOT_STATUS_MODIFY) {
3251 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3252 SHA1_DIGEST_LENGTH);
3253 staged_blob_idp = &staged_blob_id;
3256 return (*status_cb)(status_arg, status, staged_status,
3257 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3260 static const struct got_error *
3261 status_old_new(void *arg, struct got_fileindex_entry *ie,
3262 struct dirent *de, const char *parent_path, int dirfd)
3264 const struct got_error *err = NULL;
3265 struct diff_dir_cb_arg *a = arg;
3266 char *abspath;
3268 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3269 return got_error(GOT_ERR_CANCELLED);
3271 if (got_path_cmp(parent_path, a->status_path,
3272 strlen(parent_path), a->status_path_len) != 0 &&
3273 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3274 return NULL;
3276 if (parent_path[0]) {
3277 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3278 parent_path, de->d_name) == -1)
3279 return got_error_from_errno("asprintf");
3280 } else {
3281 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3282 de->d_name) == -1)
3283 return got_error_from_errno("asprintf");
3286 err = report_file_status(ie, abspath, dirfd, de->d_name,
3287 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3288 free(abspath);
3289 return err;
3292 static const struct got_error *
3293 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3295 struct diff_dir_cb_arg *a = arg;
3296 struct got_object_id blob_id, commit_id;
3297 unsigned char status;
3299 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3300 return got_error(GOT_ERR_CANCELLED);
3302 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3303 return NULL;
3305 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3307 if (got_fileindex_entry_has_file_on_disk(ie))
3308 status = GOT_STATUS_MISSING;
3309 else
3310 status = GOT_STATUS_DELETE;
3311 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3312 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3315 void
3316 free_ignorelist(struct got_pathlist_head *ignorelist)
3318 struct got_pathlist_entry *pe;
3320 TAILQ_FOREACH(pe, ignorelist, entry)
3321 free((char *)pe->path);
3322 got_pathlist_free(ignorelist);
3325 void
3326 free_ignores(struct got_pathlist_head *ignores)
3328 struct got_pathlist_entry *pe;
3330 TAILQ_FOREACH(pe, ignores, entry) {
3331 struct got_pathlist_head *ignorelist = pe->data;
3332 free_ignorelist(ignorelist);
3333 free((char *)pe->path);
3335 got_pathlist_free(ignores);
3338 static const struct got_error *
3339 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3341 const struct got_error *err = NULL;
3342 struct got_pathlist_entry *pe = NULL;
3343 struct got_pathlist_head *ignorelist;
3344 char *line = NULL, *pattern, *dirpath = NULL;
3345 size_t linesize = 0;
3346 ssize_t linelen;
3348 ignorelist = calloc(1, sizeof(*ignorelist));
3349 if (ignorelist == NULL)
3350 return got_error_from_errno("calloc");
3351 TAILQ_INIT(ignorelist);
3353 while ((linelen = getline(&line, &linesize, f)) != -1) {
3354 if (linelen > 0 && line[linelen - 1] == '\n')
3355 line[linelen - 1] = '\0';
3357 /* Git's ignores may contain comments. */
3358 if (line[0] == '#')
3359 continue;
3361 /* Git's negated patterns are not (yet?) supported. */
3362 if (line[0] == '!')
3363 continue;
3365 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3366 line) == -1) {
3367 err = got_error_from_errno("asprintf");
3368 goto done;
3370 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3371 if (err)
3372 goto done;
3374 if (ferror(f)) {
3375 err = got_error_from_errno("getline");
3376 goto done;
3379 dirpath = strdup(path);
3380 if (dirpath == NULL) {
3381 err = got_error_from_errno("strdup");
3382 goto done;
3384 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3385 done:
3386 free(line);
3387 if (err || pe == NULL) {
3388 free(dirpath);
3389 free_ignorelist(ignorelist);
3391 return err;
3394 int
3395 match_ignores(struct got_pathlist_head *ignores, const char *path)
3397 struct got_pathlist_entry *pe;
3399 /* Handle patterns which match in all directories. */
3400 TAILQ_FOREACH(pe, ignores, entry) {
3401 struct got_pathlist_head *ignorelist = pe->data;
3402 struct got_pathlist_entry *pi;
3404 TAILQ_FOREACH(pi, ignorelist, entry) {
3405 const char *p, *pattern = pi->path;
3407 if (strncmp(pattern, "**/", 3) != 0)
3408 continue;
3409 pattern += 3;
3410 p = path;
3411 while (*p) {
3412 if (fnmatch(pattern, p,
3413 FNM_PATHNAME | FNM_LEADING_DIR)) {
3414 /* Retry in next directory. */
3415 while (*p && *p != '/')
3416 p++;
3417 while (*p == '/')
3418 p++;
3419 continue;
3421 return 1;
3427 * The ignores pathlist contains ignore lists from children before
3428 * parents, so we can find the most specific ignorelist by walking
3429 * ignores backwards.
3431 pe = TAILQ_LAST(ignores, got_pathlist_head);
3432 while (pe) {
3433 if (got_path_is_child(path, pe->path, pe->path_len)) {
3434 struct got_pathlist_head *ignorelist = pe->data;
3435 struct got_pathlist_entry *pi;
3436 TAILQ_FOREACH(pi, ignorelist, entry) {
3437 const char *pattern = pi->path;
3438 int flags = FNM_LEADING_DIR;
3439 if (strstr(pattern, "/**/") == NULL)
3440 flags |= FNM_PATHNAME;
3441 if (fnmatch(pattern, path, flags))
3442 continue;
3443 return 1;
3446 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3449 return 0;
3452 static const struct got_error *
3453 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3454 const char *path, int dirfd, const char *ignores_filename)
3456 const struct got_error *err = NULL;
3457 char *ignorespath;
3458 int fd = -1;
3459 FILE *ignoresfile = NULL;
3461 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3462 path[0] ? "/" : "", ignores_filename) == -1)
3463 return got_error_from_errno("asprintf");
3465 if (dirfd != -1) {
3466 fd = openat(dirfd, ignores_filename,
3467 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3468 if (fd == -1) {
3469 if (errno != ENOENT && errno != EACCES)
3470 err = got_error_from_errno2("openat",
3471 ignorespath);
3472 } else {
3473 ignoresfile = fdopen(fd, "r");
3474 if (ignoresfile == NULL)
3475 err = got_error_from_errno2("fdopen",
3476 ignorespath);
3477 else {
3478 fd = -1;
3479 err = read_ignores(ignores, path, ignoresfile);
3482 } else {
3483 ignoresfile = fopen(ignorespath, "re");
3484 if (ignoresfile == NULL) {
3485 if (errno != ENOENT && errno != EACCES)
3486 err = got_error_from_errno2("fopen",
3487 ignorespath);
3488 } else
3489 err = read_ignores(ignores, path, ignoresfile);
3492 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3493 err = got_error_from_errno2("fclose", path);
3494 if (fd != -1 && close(fd) == -1 && err == NULL)
3495 err = got_error_from_errno2("close", path);
3496 free(ignorespath);
3497 return err;
3500 static const struct got_error *
3501 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3502 int dirfd)
3504 const struct got_error *err = NULL;
3505 struct diff_dir_cb_arg *a = arg;
3506 char *path = NULL;
3508 if (ignore != NULL)
3509 *ignore = 0;
3511 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3512 return got_error(GOT_ERR_CANCELLED);
3514 if (parent_path[0]) {
3515 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3517 } else {
3518 path = de->d_name;
3521 if (de->d_type == DT_DIR) {
3522 if (!a->no_ignores && ignore != NULL &&
3523 match_ignores(a->ignores, path))
3524 *ignore = 1;
3525 } else if (!match_ignores(a->ignores, path) &&
3526 got_path_is_child(path, a->status_path, a->status_path_len))
3527 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3528 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3529 if (parent_path[0])
3530 free(path);
3531 return err;
3534 static const struct got_error *
3535 status_traverse(void *arg, const char *path, int dirfd)
3537 const struct got_error *err = NULL;
3538 struct diff_dir_cb_arg *a = arg;
3540 if (a->no_ignores)
3541 return NULL;
3543 err = add_ignores(a->ignores, a->worktree->root_path,
3544 path, dirfd, ".cvsignore");
3545 if (err)
3546 return err;
3548 err = add_ignores(a->ignores, a->worktree->root_path, path,
3549 dirfd, ".gitignore");
3551 return err;
3554 static const struct got_error *
3555 report_single_file_status(const char *path, const char *ondisk_path,
3556 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3557 void *status_arg, struct got_repository *repo, int report_unchanged,
3558 struct got_pathlist_head *ignores, int no_ignores)
3560 struct got_fileindex_entry *ie;
3561 struct stat sb;
3563 if (!no_ignores && match_ignores(ignores, path))
3564 return NULL;
3566 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3567 if (ie)
3568 return report_file_status(ie, ondisk_path, -1, NULL,
3569 status_cb, status_arg, repo, report_unchanged);
3571 if (lstat(ondisk_path, &sb) == -1) {
3572 if (errno != ENOENT)
3573 return got_error_from_errno2("lstat", ondisk_path);
3574 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3575 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3576 return NULL;
3579 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3580 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3581 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3583 return NULL;
3586 static const struct got_error *
3587 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3588 const char *root_path, const char *path)
3590 const struct got_error *err;
3591 char *parent_path, *next_parent_path = NULL;
3593 err = add_ignores(ignores, root_path, "", -1,
3594 ".cvsignore");
3595 if (err)
3596 return err;
3598 err = add_ignores(ignores, root_path, "", -1,
3599 ".gitignore");
3600 if (err)
3601 return err;
3603 err = got_path_dirname(&parent_path, path);
3604 if (err) {
3605 if (err->code == GOT_ERR_BAD_PATH)
3606 return NULL; /* cannot traverse parent */
3607 return err;
3609 for (;;) {
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3611 ".cvsignore");
3612 if (err)
3613 break;
3614 err = add_ignores(ignores, root_path, parent_path, -1,
3615 ".gitignore");
3616 if (err)
3617 break;
3618 err = got_path_dirname(&next_parent_path, parent_path);
3619 if (err) {
3620 if (err->code == GOT_ERR_BAD_PATH)
3621 err = NULL; /* traversed everything */
3622 break;
3624 if (got_path_is_root_dir(parent_path))
3625 break;
3626 free(parent_path);
3627 parent_path = next_parent_path;
3628 next_parent_path = NULL;
3631 free(parent_path);
3632 free(next_parent_path);
3633 return err;
3636 static const struct got_error *
3637 worktree_status(struct got_worktree *worktree, const char *path,
3638 struct got_fileindex *fileindex, struct got_repository *repo,
3639 got_worktree_status_cb status_cb, void *status_arg,
3640 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3641 int report_unchanged)
3643 const struct got_error *err = NULL;
3644 int fd = -1;
3645 struct got_fileindex_diff_dir_cb fdiff_cb;
3646 struct diff_dir_cb_arg arg;
3647 char *ondisk_path = NULL;
3648 struct got_pathlist_head ignores;
3650 TAILQ_INIT(&ignores);
3652 if (asprintf(&ondisk_path, "%s%s%s",
3653 worktree->root_path, path[0] ? "/" : "", path) == -1)
3654 return got_error_from_errno("asprintf");
3656 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3657 if (fd == -1) {
3658 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3659 !got_err_open_nofollow_on_symlink())
3660 err = got_error_from_errno2("open", ondisk_path);
3661 else {
3662 if (!no_ignores) {
3663 err = add_ignores_from_parent_paths(&ignores,
3664 worktree->root_path, ondisk_path);
3665 if (err)
3666 goto done;
3668 err = report_single_file_status(path, ondisk_path,
3669 fileindex, status_cb, status_arg, repo,
3670 report_unchanged, &ignores, no_ignores);
3672 } else {
3673 fdiff_cb.diff_old_new = status_old_new;
3674 fdiff_cb.diff_old = status_old;
3675 fdiff_cb.diff_new = status_new;
3676 fdiff_cb.diff_traverse = status_traverse;
3677 arg.fileindex = fileindex;
3678 arg.worktree = worktree;
3679 arg.status_path = path;
3680 arg.status_path_len = strlen(path);
3681 arg.repo = repo;
3682 arg.status_cb = status_cb;
3683 arg.status_arg = status_arg;
3684 arg.cancel_cb = cancel_cb;
3685 arg.cancel_arg = cancel_arg;
3686 arg.report_unchanged = report_unchanged;
3687 arg.no_ignores = no_ignores;
3688 if (!no_ignores) {
3689 err = add_ignores_from_parent_paths(&ignores,
3690 worktree->root_path, path);
3691 if (err)
3692 goto done;
3694 arg.ignores = &ignores;
3695 err = got_fileindex_diff_dir(fileindex, fd,
3696 worktree->root_path, path, repo, &fdiff_cb, &arg);
3698 done:
3699 free_ignores(&ignores);
3700 if (fd != -1 && close(fd) == -1 && err == NULL)
3701 err = got_error_from_errno("close");
3702 free(ondisk_path);
3703 return err;
3706 const struct got_error *
3707 got_worktree_status(struct got_worktree *worktree,
3708 struct got_pathlist_head *paths, struct got_repository *repo,
3709 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3710 got_cancel_cb cancel_cb, void *cancel_arg)
3712 const struct got_error *err = NULL;
3713 char *fileindex_path = NULL;
3714 struct got_fileindex *fileindex = NULL;
3715 struct got_pathlist_entry *pe;
3717 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3718 if (err)
3719 return err;
3721 TAILQ_FOREACH(pe, paths, entry) {
3722 err = worktree_status(worktree, pe->path, fileindex, repo,
3723 status_cb, status_arg, cancel_cb, cancel_arg,
3724 no_ignores, 0);
3725 if (err)
3726 break;
3728 free(fileindex_path);
3729 got_fileindex_free(fileindex);
3730 return err;
3733 const struct got_error *
3734 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3735 const char *arg)
3737 const struct got_error *err = NULL;
3738 char *resolved = NULL, *cwd = NULL, *path = NULL;
3739 size_t len;
3740 struct stat sb;
3741 char *abspath = NULL;
3742 char canonpath[PATH_MAX];
3744 *wt_path = NULL;
3746 cwd = getcwd(NULL, 0);
3747 if (cwd == NULL)
3748 return got_error_from_errno("getcwd");
3750 if (lstat(arg, &sb) == -1) {
3751 if (errno != ENOENT) {
3752 err = got_error_from_errno2("lstat", arg);
3753 goto done;
3755 sb.st_mode = 0;
3757 if (S_ISLNK(sb.st_mode)) {
3759 * We cannot use realpath(3) with symlinks since we want to
3760 * operate on the symlink itself.
3761 * But we can make the path absolute, assuming it is relative
3762 * to the current working directory, and then canonicalize it.
3764 if (!got_path_is_absolute(arg)) {
3765 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3766 err = got_error_from_errno("asprintf");
3767 goto done;
3771 err = got_canonpath(abspath ? abspath : arg, canonpath,
3772 sizeof(canonpath));
3773 if (err)
3774 goto done;
3775 resolved = strdup(canonpath);
3776 if (resolved == NULL) {
3777 err = got_error_from_errno("strdup");
3778 goto done;
3780 } else {
3781 resolved = realpath(arg, NULL);
3782 if (resolved == NULL) {
3783 if (errno != ENOENT) {
3784 err = got_error_from_errno2("realpath", arg);
3785 goto done;
3787 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3788 err = got_error_from_errno("asprintf");
3789 goto done;
3791 err = got_canonpath(abspath, canonpath,
3792 sizeof(canonpath));
3793 if (err)
3794 goto done;
3795 resolved = strdup(canonpath);
3796 if (resolved == NULL) {
3797 err = got_error_from_errno("strdup");
3798 goto done;
3803 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3804 strlen(got_worktree_get_root_path(worktree)))) {
3805 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3806 goto done;
3809 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3810 err = got_path_skip_common_ancestor(&path,
3811 got_worktree_get_root_path(worktree), resolved);
3812 if (err)
3813 goto done;
3814 } else {
3815 path = strdup("");
3816 if (path == NULL) {
3817 err = got_error_from_errno("strdup");
3818 goto done;
3822 /* XXX status walk can't deal with trailing slash! */
3823 len = strlen(path);
3824 while (len > 0 && path[len - 1] == '/') {
3825 path[len - 1] = '\0';
3826 len--;
3828 done:
3829 free(abspath);
3830 free(resolved);
3831 free(cwd);
3832 if (err == NULL)
3833 *wt_path = path;
3834 else
3835 free(path);
3836 return err;
3839 struct schedule_addition_args {
3840 struct got_worktree *worktree;
3841 struct got_fileindex *fileindex;
3842 got_worktree_checkout_cb progress_cb;
3843 void *progress_arg;
3844 struct got_repository *repo;
3847 static const struct got_error *
3848 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3849 const char *relpath, struct got_object_id *blob_id,
3850 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3851 int dirfd, const char *de_name)
3853 struct schedule_addition_args *a = arg;
3854 const struct got_error *err = NULL;
3855 struct got_fileindex_entry *ie;
3856 struct stat sb;
3857 char *ondisk_path;
3859 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3860 relpath) == -1)
3861 return got_error_from_errno("asprintf");
3863 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3864 if (ie) {
3865 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3866 de_name, a->repo);
3867 if (err)
3868 goto done;
3869 /* Re-adding an existing entry is a no-op. */
3870 if (status == GOT_STATUS_ADD)
3871 goto done;
3872 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3873 if (err)
3874 goto done;
3877 if (status != GOT_STATUS_UNVERSIONED) {
3878 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3879 goto done;
3882 err = got_fileindex_entry_alloc(&ie, relpath);
3883 if (err)
3884 goto done;
3885 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3886 relpath, NULL, NULL, 1);
3887 if (err) {
3888 got_fileindex_entry_free(ie);
3889 goto done;
3891 err = got_fileindex_entry_add(a->fileindex, ie);
3892 if (err) {
3893 got_fileindex_entry_free(ie);
3894 goto done;
3896 done:
3897 free(ondisk_path);
3898 if (err)
3899 return err;
3900 if (status == GOT_STATUS_ADD)
3901 return NULL;
3902 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3905 const struct got_error *
3906 got_worktree_schedule_add(struct got_worktree *worktree,
3907 struct got_pathlist_head *paths,
3908 got_worktree_checkout_cb progress_cb, void *progress_arg,
3909 struct got_repository *repo, int no_ignores)
3911 struct got_fileindex *fileindex = NULL;
3912 char *fileindex_path = NULL;
3913 const struct got_error *err = NULL, *sync_err, *unlockerr;
3914 struct got_pathlist_entry *pe;
3915 struct schedule_addition_args saa;
3917 err = lock_worktree(worktree, LOCK_EX);
3918 if (err)
3919 return err;
3921 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 if (err)
3923 goto done;
3925 saa.worktree = worktree;
3926 saa.fileindex = fileindex;
3927 saa.progress_cb = progress_cb;
3928 saa.progress_arg = progress_arg;
3929 saa.repo = repo;
3931 TAILQ_FOREACH(pe, paths, entry) {
3932 err = worktree_status(worktree, pe->path, fileindex, repo,
3933 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3934 if (err)
3935 break;
3937 sync_err = sync_fileindex(fileindex, fileindex_path);
3938 if (sync_err && err == NULL)
3939 err = sync_err;
3940 done:
3941 free(fileindex_path);
3942 if (fileindex)
3943 got_fileindex_free(fileindex);
3944 unlockerr = lock_worktree(worktree, LOCK_SH);
3945 if (unlockerr && err == NULL)
3946 err = unlockerr;
3947 return err;
3950 struct schedule_deletion_args {
3951 struct got_worktree *worktree;
3952 struct got_fileindex *fileindex;
3953 got_worktree_delete_cb progress_cb;
3954 void *progress_arg;
3955 struct got_repository *repo;
3956 int delete_local_mods;
3957 int keep_on_disk;
3958 const char *status_codes;
3961 static const struct got_error *
3962 schedule_for_deletion(void *arg, unsigned char status,
3963 unsigned char staged_status, const char *relpath,
3964 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3965 struct got_object_id *commit_id, int dirfd, const char *de_name)
3967 struct schedule_deletion_args *a = arg;
3968 const struct got_error *err = NULL;
3969 struct got_fileindex_entry *ie = NULL;
3970 struct stat sb;
3971 char *ondisk_path;
3973 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3974 if (ie == NULL)
3975 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3977 staged_status = get_staged_status(ie);
3978 if (staged_status != GOT_STATUS_NO_CHANGE) {
3979 if (staged_status == GOT_STATUS_DELETE)
3980 return NULL;
3981 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3984 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3985 relpath) == -1)
3986 return got_error_from_errno("asprintf");
3988 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3989 a->repo);
3990 if (err)
3991 goto done;
3993 if (a->status_codes) {
3994 size_t ncodes = strlen(a->status_codes);
3995 int i;
3996 for (i = 0; i < ncodes ; i++) {
3997 if (status == a->status_codes[i])
3998 break;
4000 if (i == ncodes) {
4001 /* Do not delete files in non-matching status. */
4002 free(ondisk_path);
4003 return NULL;
4005 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4006 a->status_codes[i] != GOT_STATUS_MISSING) {
4007 static char msg[64];
4008 snprintf(msg, sizeof(msg),
4009 "invalid status code '%c'", a->status_codes[i]);
4010 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4011 goto done;
4015 if (status != GOT_STATUS_NO_CHANGE) {
4016 if (status == GOT_STATUS_DELETE)
4017 goto done;
4018 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4019 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4020 goto done;
4022 if (status != GOT_STATUS_MODIFY &&
4023 status != GOT_STATUS_MISSING) {
4024 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4025 goto done;
4029 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4030 size_t root_len;
4032 if (dirfd != -1) {
4033 if (unlinkat(dirfd, de_name, 0) != 0) {
4034 err = got_error_from_errno2("unlinkat",
4035 ondisk_path);
4036 goto done;
4038 } else if (unlink(ondisk_path) != 0) {
4039 err = got_error_from_errno2("unlink", ondisk_path);
4040 goto done;
4043 root_len = strlen(a->worktree->root_path);
4044 do {
4045 char *parent;
4046 err = got_path_dirname(&parent, ondisk_path);
4047 if (err)
4048 goto done;
4049 free(ondisk_path);
4050 ondisk_path = parent;
4051 if (rmdir(ondisk_path) == -1) {
4052 if (errno != ENOTEMPTY)
4053 err = got_error_from_errno2("rmdir",
4054 ondisk_path);
4055 break;
4057 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4058 strlen(ondisk_path), root_len) != 0);
4061 got_fileindex_entry_mark_deleted_from_disk(ie);
4062 done:
4063 free(ondisk_path);
4064 if (err)
4065 return err;
4066 if (status == GOT_STATUS_DELETE)
4067 return NULL;
4068 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4069 staged_status, relpath);
4072 const struct got_error *
4073 got_worktree_schedule_delete(struct got_worktree *worktree,
4074 struct got_pathlist_head *paths, int delete_local_mods,
4075 const char *status_codes,
4076 got_worktree_delete_cb progress_cb, void *progress_arg,
4077 struct got_repository *repo, int keep_on_disk)
4079 struct got_fileindex *fileindex = NULL;
4080 char *fileindex_path = NULL;
4081 const struct got_error *err = NULL, *sync_err, *unlockerr;
4082 struct got_pathlist_entry *pe;
4083 struct schedule_deletion_args sda;
4085 err = lock_worktree(worktree, LOCK_EX);
4086 if (err)
4087 return err;
4089 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4090 if (err)
4091 goto done;
4093 sda.worktree = worktree;
4094 sda.fileindex = fileindex;
4095 sda.progress_cb = progress_cb;
4096 sda.progress_arg = progress_arg;
4097 sda.repo = repo;
4098 sda.delete_local_mods = delete_local_mods;
4099 sda.keep_on_disk = keep_on_disk;
4100 sda.status_codes = status_codes;
4102 TAILQ_FOREACH(pe, paths, entry) {
4103 err = worktree_status(worktree, pe->path, fileindex, repo,
4104 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4105 if (err)
4106 break;
4108 sync_err = sync_fileindex(fileindex, fileindex_path);
4109 if (sync_err && err == NULL)
4110 err = sync_err;
4111 done:
4112 free(fileindex_path);
4113 if (fileindex)
4114 got_fileindex_free(fileindex);
4115 unlockerr = lock_worktree(worktree, LOCK_SH);
4116 if (unlockerr && err == NULL)
4117 err = unlockerr;
4118 return err;
4121 static const struct got_error *
4122 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4124 const struct got_error *err = NULL;
4125 char *line = NULL;
4126 size_t linesize = 0, n;
4127 ssize_t linelen;
4129 linelen = getline(&line, &linesize, infile);
4130 if (linelen == -1) {
4131 if (ferror(infile)) {
4132 err = got_error_from_errno("getline");
4133 goto done;
4135 return NULL;
4137 if (outfile) {
4138 n = fwrite(line, 1, linelen, outfile);
4139 if (n != linelen) {
4140 err = got_ferror(outfile, GOT_ERR_IO);
4141 goto done;
4144 if (rejectfile) {
4145 n = fwrite(line, 1, linelen, rejectfile);
4146 if (n != linelen)
4147 err = got_ferror(outfile, GOT_ERR_IO);
4149 done:
4150 free(line);
4151 return err;
4154 static const struct got_error *
4155 skip_one_line(FILE *f)
4157 char *line = NULL;
4158 size_t linesize = 0;
4159 ssize_t linelen;
4161 linelen = getline(&line, &linesize, f);
4162 if (linelen == -1) {
4163 if (ferror(f))
4164 return got_error_from_errno("getline");
4165 return NULL;
4167 free(line);
4168 return NULL;
4171 static const struct got_error *
4172 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4173 int start_old, int end_old, int start_new, int end_new,
4174 FILE *outfile, FILE *rejectfile)
4176 const struct got_error *err;
4178 /* Copy old file's lines leading up to patch. */
4179 while (!feof(f1) && *line_cur1 < start_old) {
4180 err = copy_one_line(f1, outfile, NULL);
4181 if (err)
4182 return err;
4183 (*line_cur1)++;
4185 /* Skip new file's lines leading up to patch. */
4186 while (!feof(f2) && *line_cur2 < start_new) {
4187 if (rejectfile)
4188 err = copy_one_line(f2, NULL, rejectfile);
4189 else
4190 err = skip_one_line(f2);
4191 if (err)
4192 return err;
4193 (*line_cur2)++;
4195 /* Copy patched lines. */
4196 while (!feof(f2) && *line_cur2 <= end_new) {
4197 err = copy_one_line(f2, outfile, NULL);
4198 if (err)
4199 return err;
4200 (*line_cur2)++;
4202 /* Skip over old file's replaced lines. */
4203 while (!feof(f1) && *line_cur1 <= end_old) {
4204 if (rejectfile)
4205 err = copy_one_line(f1, NULL, rejectfile);
4206 else
4207 err = skip_one_line(f1);
4208 if (err)
4209 return err;
4210 (*line_cur1)++;
4213 return NULL;
4216 static const struct got_error *
4217 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4218 FILE *outfile, FILE *rejectfile)
4220 const struct got_error *err;
4222 if (outfile) {
4223 /* Copy old file's lines until EOF. */
4224 while (!feof(f1)) {
4225 err = copy_one_line(f1, outfile, NULL);
4226 if (err)
4227 return err;
4228 (*line_cur1)++;
4231 if (rejectfile) {
4232 /* Copy new file's lines until EOF. */
4233 while (!feof(f2)) {
4234 err = copy_one_line(f2, NULL, rejectfile);
4235 if (err)
4236 return err;
4237 (*line_cur2)++;
4241 return NULL;
4244 static const struct got_error *
4245 apply_or_reject_change(int *choice, int *nchunks_used,
4246 struct diff_result *diff_result, int n,
4247 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4248 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4249 got_worktree_patch_cb patch_cb, void *patch_arg)
4251 const struct got_error *err = NULL;
4252 struct diff_chunk_context cc = {};
4253 int start_old, end_old, start_new, end_new;
4254 FILE *hunkfile;
4255 struct diff_output_unidiff_state *diff_state;
4256 struct diff_input_info diff_info;
4257 int rc;
4259 *choice = GOT_PATCH_CHOICE_NONE;
4261 /* Get changed line numbers without context lines for copy_change(). */
4262 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4263 start_old = cc.left.start;
4264 end_old = cc.left.end;
4265 start_new = cc.right.start;
4266 end_new = cc.right.end;
4268 /* Get the same change with context lines for display. */
4269 memset(&cc, 0, sizeof(cc));
4270 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4272 memset(&diff_info, 0, sizeof(diff_info));
4273 diff_info.left_path = relpath;
4274 diff_info.right_path = relpath;
4276 diff_state = diff_output_unidiff_state_alloc();
4277 if (diff_state == NULL)
4278 return got_error_set_errno(ENOMEM,
4279 "diff_output_unidiff_state_alloc");
4281 hunkfile = got_opentemp();
4282 if (hunkfile == NULL) {
4283 err = got_error_from_errno("got_opentemp");
4284 goto done;
4287 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4288 diff_result, &cc);
4289 if (rc != DIFF_RC_OK) {
4290 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4291 goto done;
4294 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4295 err = got_ferror(hunkfile, GOT_ERR_IO);
4296 goto done;
4299 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4300 hunkfile, changeno, nchanges);
4301 if (err)
4302 goto done;
4304 switch (*choice) {
4305 case GOT_PATCH_CHOICE_YES:
4306 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 end_old, start_new, end_new, outfile, rejectfile);
4308 break;
4309 case GOT_PATCH_CHOICE_NO:
4310 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4311 end_old, start_new, end_new, rejectfile, outfile);
4312 break;
4313 case GOT_PATCH_CHOICE_QUIT:
4314 break;
4315 default:
4316 err = got_error(GOT_ERR_PATCH_CHOICE);
4317 break;
4319 done:
4320 diff_output_unidiff_state_free(diff_state);
4321 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4322 err = got_error_from_errno("fclose");
4323 return err;
4326 struct revert_file_args {
4327 struct got_worktree *worktree;
4328 struct got_fileindex *fileindex;
4329 got_worktree_checkout_cb progress_cb;
4330 void *progress_arg;
4331 got_worktree_patch_cb patch_cb;
4332 void *patch_arg;
4333 struct got_repository *repo;
4334 int unlink_added_files;
4337 static const struct got_error *
4338 create_patched_content(char **path_outfile, int reverse_patch,
4339 struct got_object_id *blob_id, const char *path2,
4340 int dirfd2, const char *de_name2,
4341 const char *relpath, struct got_repository *repo,
4342 got_worktree_patch_cb patch_cb, void *patch_arg)
4344 const struct got_error *err, *free_err;
4345 struct got_blob_object *blob = NULL;
4346 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4347 int fd2 = -1;
4348 char link_target[PATH_MAX];
4349 ssize_t link_len = 0;
4350 char *path1 = NULL, *id_str = NULL;
4351 struct stat sb2;
4352 struct got_diffreg_result *diffreg_result = NULL;
4353 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4354 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4356 *path_outfile = NULL;
4358 err = got_object_id_str(&id_str, blob_id);
4359 if (err)
4360 return err;
4362 if (dirfd2 != -1) {
4363 fd2 = openat(dirfd2, de_name2,
4364 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4365 if (fd2 == -1) {
4366 if (!got_err_open_nofollow_on_symlink()) {
4367 err = got_error_from_errno2("openat", path2);
4368 goto done;
4370 link_len = readlinkat(dirfd2, de_name2,
4371 link_target, sizeof(link_target));
4372 if (link_len == -1) {
4373 return got_error_from_errno2("readlinkat",
4374 path2);
4376 sb2.st_mode = S_IFLNK;
4377 sb2.st_size = link_len;
4379 } else {
4380 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4381 if (fd2 == -1) {
4382 if (!got_err_open_nofollow_on_symlink()) {
4383 err = got_error_from_errno2("open", path2);
4384 goto done;
4386 link_len = readlink(path2, link_target,
4387 sizeof(link_target));
4388 if (link_len == -1)
4389 return got_error_from_errno2("readlink", path2);
4390 sb2.st_mode = S_IFLNK;
4391 sb2.st_size = link_len;
4394 if (fd2 != -1) {
4395 if (fstat(fd2, &sb2) == -1) {
4396 err = got_error_from_errno2("fstat", path2);
4397 goto done;
4400 f2 = fdopen(fd2, "r");
4401 if (f2 == NULL) {
4402 err = got_error_from_errno2("fdopen", path2);
4403 goto done;
4405 fd2 = -1;
4406 } else {
4407 size_t n;
4408 f2 = got_opentemp();
4409 if (f2 == NULL) {
4410 err = got_error_from_errno2("got_opentemp", path2);
4411 goto done;
4413 n = fwrite(link_target, 1, link_len, f2);
4414 if (n != link_len) {
4415 err = got_ferror(f2, GOT_ERR_IO);
4416 goto done;
4418 if (fflush(f2) == EOF) {
4419 err = got_error_from_errno("fflush");
4420 goto done;
4422 rewind(f2);
4425 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4426 if (err)
4427 goto done;
4429 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4430 if (err)
4431 goto done;
4433 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4434 if (err)
4435 goto done;
4437 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4438 NULL);
4439 if (err)
4440 goto done;
4442 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4443 if (err)
4444 goto done;
4446 if (fseek(f1, 0L, SEEK_SET) == -1)
4447 return got_ferror(f1, GOT_ERR_IO);
4448 if (fseek(f2, 0L, SEEK_SET) == -1)
4449 return got_ferror(f2, GOT_ERR_IO);
4451 /* Count the number of actual changes in the diff result. */
4452 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4453 struct diff_chunk_context cc = {};
4454 diff_chunk_context_load_change(&cc, &nchunks_used,
4455 diffreg_result->result, n, 0);
4456 nchanges++;
4458 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4459 int choice;
4460 err = apply_or_reject_change(&choice, &nchunks_used,
4461 diffreg_result->result, n, relpath, f1, f2,
4462 &line_cur1, &line_cur2,
4463 reverse_patch ? NULL : outfile,
4464 reverse_patch ? outfile : NULL,
4465 ++i, nchanges, patch_cb, patch_arg);
4466 if (err)
4467 goto done;
4468 if (choice == GOT_PATCH_CHOICE_YES)
4469 have_content = 1;
4470 else if (choice == GOT_PATCH_CHOICE_QUIT)
4471 break;
4473 if (have_content) {
4474 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4475 reverse_patch ? NULL : outfile,
4476 reverse_patch ? outfile : NULL);
4477 if (err)
4478 goto done;
4480 if (!S_ISLNK(sb2.st_mode)) {
4481 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4482 err = got_error_from_errno2("fchmod", path2);
4483 goto done;
4487 done:
4488 free(id_str);
4489 if (blob)
4490 got_object_blob_close(blob);
4491 free_err = got_diffreg_result_free(diffreg_result);
4492 if (err == NULL)
4493 err = free_err;
4494 if (f1 && fclose(f1) == EOF && err == NULL)
4495 err = got_error_from_errno2("fclose", path1);
4496 if (f2 && fclose(f2) == EOF && err == NULL)
4497 err = got_error_from_errno2("fclose", path2);
4498 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4499 err = got_error_from_errno2("close", path2);
4500 if (outfile && fclose(outfile) == EOF && err == NULL)
4501 err = got_error_from_errno2("fclose", *path_outfile);
4502 if (path1 && unlink(path1) == -1 && err == NULL)
4503 err = got_error_from_errno2("unlink", path1);
4504 if (err || !have_content) {
4505 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4506 err = got_error_from_errno2("unlink", *path_outfile);
4507 free(*path_outfile);
4508 *path_outfile = NULL;
4510 free(path1);
4511 return err;
4514 static const struct got_error *
4515 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4516 const char *relpath, struct got_object_id *blob_id,
4517 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4518 int dirfd, const char *de_name)
4520 struct revert_file_args *a = arg;
4521 const struct got_error *err = NULL;
4522 char *parent_path = NULL;
4523 struct got_fileindex_entry *ie;
4524 struct got_tree_object *tree = NULL;
4525 struct got_object_id *tree_id = NULL;
4526 const struct got_tree_entry *te = NULL;
4527 char *tree_path = NULL, *te_name;
4528 char *ondisk_path = NULL, *path_content = NULL;
4529 struct got_blob_object *blob = NULL;
4531 /* Reverting a staged deletion is a no-op. */
4532 if (status == GOT_STATUS_DELETE &&
4533 staged_status != GOT_STATUS_NO_CHANGE)
4534 return NULL;
4536 if (status == GOT_STATUS_UNVERSIONED)
4537 return (*a->progress_cb)(a->progress_arg,
4538 GOT_STATUS_UNVERSIONED, relpath);
4540 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4541 if (ie == NULL)
4542 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4544 /* Construct in-repository path of tree which contains this blob. */
4545 err = got_path_dirname(&parent_path, ie->path);
4546 if (err) {
4547 if (err->code != GOT_ERR_BAD_PATH)
4548 goto done;
4549 parent_path = strdup("/");
4550 if (parent_path == NULL) {
4551 err = got_error_from_errno("strdup");
4552 goto done;
4555 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4556 tree_path = strdup(parent_path);
4557 if (tree_path == NULL) {
4558 err = got_error_from_errno("strdup");
4559 goto done;
4561 } else {
4562 if (got_path_is_root_dir(parent_path)) {
4563 tree_path = strdup(a->worktree->path_prefix);
4564 if (tree_path == NULL) {
4565 err = got_error_from_errno("strdup");
4566 goto done;
4568 } else {
4569 if (asprintf(&tree_path, "%s/%s",
4570 a->worktree->path_prefix, parent_path) == -1) {
4571 err = got_error_from_errno("asprintf");
4572 goto done;
4577 err = got_object_id_by_path(&tree_id, a->repo,
4578 a->worktree->base_commit_id, tree_path);
4579 if (err) {
4580 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4581 (status == GOT_STATUS_ADD ||
4582 staged_status == GOT_STATUS_ADD)))
4583 goto done;
4584 } else {
4585 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4586 if (err)
4587 goto done;
4589 err = got_path_basename(&te_name, ie->path);
4590 if (err)
4591 goto done;
4593 te = got_object_tree_find_entry(tree, te_name);
4594 free(te_name);
4595 if (te == NULL && status != GOT_STATUS_ADD &&
4596 staged_status != GOT_STATUS_ADD) {
4597 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4598 goto done;
4602 switch (status) {
4603 case GOT_STATUS_ADD:
4604 if (a->patch_cb) {
4605 int choice = GOT_PATCH_CHOICE_NONE;
4606 err = (*a->patch_cb)(&choice, a->patch_arg,
4607 status, ie->path, NULL, 1, 1);
4608 if (err)
4609 goto done;
4610 if (choice != GOT_PATCH_CHOICE_YES)
4611 break;
4613 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4614 ie->path);
4615 if (err)
4616 goto done;
4617 got_fileindex_entry_remove(a->fileindex, ie);
4618 if (a->unlink_added_files) {
4619 if (asprintf(&ondisk_path, "%s/%s",
4620 got_worktree_get_root_path(a->worktree),
4621 relpath) == -1) {
4622 err = got_error_from_errno("asprintf");
4623 goto done;
4625 if (unlink(ondisk_path) == -1) {
4626 err = got_error_from_errno2("unlink",
4627 ondisk_path);
4628 break;
4631 break;
4632 case GOT_STATUS_DELETE:
4633 if (a->patch_cb) {
4634 int choice = GOT_PATCH_CHOICE_NONE;
4635 err = (*a->patch_cb)(&choice, a->patch_arg,
4636 status, ie->path, NULL, 1, 1);
4637 if (err)
4638 goto done;
4639 if (choice != GOT_PATCH_CHOICE_YES)
4640 break;
4642 /* fall through */
4643 case GOT_STATUS_MODIFY:
4644 case GOT_STATUS_MODE_CHANGE:
4645 case GOT_STATUS_CONFLICT:
4646 case GOT_STATUS_MISSING: {
4647 struct got_object_id id;
4648 if (staged_status == GOT_STATUS_ADD ||
4649 staged_status == GOT_STATUS_MODIFY) {
4650 memcpy(id.sha1, ie->staged_blob_sha1,
4651 SHA1_DIGEST_LENGTH);
4652 } else
4653 memcpy(id.sha1, ie->blob_sha1,
4654 SHA1_DIGEST_LENGTH);
4655 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4656 if (err)
4657 goto done;
4659 if (asprintf(&ondisk_path, "%s/%s",
4660 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4661 err = got_error_from_errno("asprintf");
4662 goto done;
4665 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4666 status == GOT_STATUS_CONFLICT)) {
4667 int is_bad_symlink = 0;
4668 err = create_patched_content(&path_content, 1, &id,
4669 ondisk_path, dirfd, de_name, ie->path, a->repo,
4670 a->patch_cb, a->patch_arg);
4671 if (err || path_content == NULL)
4672 break;
4673 if (te && S_ISLNK(te->mode)) {
4674 if (unlink(path_content) == -1) {
4675 err = got_error_from_errno2("unlink",
4676 path_content);
4677 break;
4679 err = install_symlink(&is_bad_symlink,
4680 a->worktree, ondisk_path, ie->path,
4681 blob, 0, 1, 0, 0, a->repo,
4682 a->progress_cb, a->progress_arg);
4683 } else {
4684 if (rename(path_content, ondisk_path) == -1) {
4685 err = got_error_from_errno3("rename",
4686 path_content, ondisk_path);
4687 goto done;
4690 } else {
4691 int is_bad_symlink = 0;
4692 if (te && S_ISLNK(te->mode)) {
4693 err = install_symlink(&is_bad_symlink,
4694 a->worktree, ondisk_path, ie->path,
4695 blob, 0, 1, 0, 0, a->repo,
4696 a->progress_cb, a->progress_arg);
4697 } else {
4698 err = install_blob(a->worktree, ondisk_path,
4699 ie->path,
4700 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4701 got_fileindex_perms_to_st(ie), blob,
4702 0, 1, 0, 0, a->repo,
4703 a->progress_cb, a->progress_arg);
4705 if (err)
4706 goto done;
4707 if (status == GOT_STATUS_DELETE ||
4708 status == GOT_STATUS_MODE_CHANGE) {
4709 err = got_fileindex_entry_update(ie,
4710 a->worktree->root_fd, relpath,
4711 blob->id.sha1,
4712 a->worktree->base_commit_id->sha1, 1);
4713 if (err)
4714 goto done;
4716 if (is_bad_symlink) {
4717 got_fileindex_entry_filetype_set(ie,
4718 GOT_FILEIDX_MODE_BAD_SYMLINK);
4721 break;
4723 default:
4724 break;
4726 done:
4727 free(ondisk_path);
4728 free(path_content);
4729 free(parent_path);
4730 free(tree_path);
4731 if (blob)
4732 got_object_blob_close(blob);
4733 if (tree)
4734 got_object_tree_close(tree);
4735 free(tree_id);
4736 return err;
4739 const struct got_error *
4740 got_worktree_revert(struct got_worktree *worktree,
4741 struct got_pathlist_head *paths,
4742 got_worktree_checkout_cb progress_cb, void *progress_arg,
4743 got_worktree_patch_cb patch_cb, void *patch_arg,
4744 struct got_repository *repo)
4746 struct got_fileindex *fileindex = NULL;
4747 char *fileindex_path = NULL;
4748 const struct got_error *err = NULL, *unlockerr = NULL;
4749 const struct got_error *sync_err = NULL;
4750 struct got_pathlist_entry *pe;
4751 struct revert_file_args rfa;
4753 err = lock_worktree(worktree, LOCK_EX);
4754 if (err)
4755 return err;
4757 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4758 if (err)
4759 goto done;
4761 rfa.worktree = worktree;
4762 rfa.fileindex = fileindex;
4763 rfa.progress_cb = progress_cb;
4764 rfa.progress_arg = progress_arg;
4765 rfa.patch_cb = patch_cb;
4766 rfa.patch_arg = patch_arg;
4767 rfa.repo = repo;
4768 rfa.unlink_added_files = 0;
4769 TAILQ_FOREACH(pe, paths, entry) {
4770 err = worktree_status(worktree, pe->path, fileindex, repo,
4771 revert_file, &rfa, NULL, NULL, 1, 0);
4772 if (err)
4773 break;
4775 sync_err = sync_fileindex(fileindex, fileindex_path);
4776 if (sync_err && err == NULL)
4777 err = sync_err;
4778 done:
4779 free(fileindex_path);
4780 if (fileindex)
4781 got_fileindex_free(fileindex);
4782 unlockerr = lock_worktree(worktree, LOCK_SH);
4783 if (unlockerr && err == NULL)
4784 err = unlockerr;
4785 return err;
4788 static void
4789 free_commitable(struct got_commitable *ct)
4791 free(ct->path);
4792 free(ct->in_repo_path);
4793 free(ct->ondisk_path);
4794 free(ct->blob_id);
4795 free(ct->base_blob_id);
4796 free(ct->staged_blob_id);
4797 free(ct->base_commit_id);
4798 free(ct);
4801 struct collect_commitables_arg {
4802 struct got_pathlist_head *commitable_paths;
4803 struct got_repository *repo;
4804 struct got_worktree *worktree;
4805 struct got_fileindex *fileindex;
4806 int have_staged_files;
4807 int allow_bad_symlinks;
4810 static const struct got_error *
4811 collect_commitables(void *arg, unsigned char status,
4812 unsigned char staged_status, const char *relpath,
4813 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4814 struct got_object_id *commit_id, int dirfd, const char *de_name)
4816 struct collect_commitables_arg *a = arg;
4817 const struct got_error *err = NULL;
4818 struct got_commitable *ct = NULL;
4819 struct got_pathlist_entry *new = NULL;
4820 char *parent_path = NULL, *path = NULL;
4821 struct stat sb;
4823 if (a->have_staged_files) {
4824 if (staged_status != GOT_STATUS_MODIFY &&
4825 staged_status != GOT_STATUS_ADD &&
4826 staged_status != GOT_STATUS_DELETE)
4827 return NULL;
4828 } else {
4829 if (status == GOT_STATUS_CONFLICT)
4830 return got_error(GOT_ERR_COMMIT_CONFLICT);
4832 if (status != GOT_STATUS_MODIFY &&
4833 status != GOT_STATUS_MODE_CHANGE &&
4834 status != GOT_STATUS_ADD &&
4835 status != GOT_STATUS_DELETE)
4836 return NULL;
4839 if (asprintf(&path, "/%s", relpath) == -1) {
4840 err = got_error_from_errno("asprintf");
4841 goto done;
4843 if (strcmp(path, "/") == 0) {
4844 parent_path = strdup("");
4845 if (parent_path == NULL)
4846 return got_error_from_errno("strdup");
4847 } else {
4848 err = got_path_dirname(&parent_path, path);
4849 if (err)
4850 return err;
4853 ct = calloc(1, sizeof(*ct));
4854 if (ct == NULL) {
4855 err = got_error_from_errno("calloc");
4856 goto done;
4859 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4860 relpath) == -1) {
4861 err = got_error_from_errno("asprintf");
4862 goto done;
4865 if (staged_status == GOT_STATUS_ADD ||
4866 staged_status == GOT_STATUS_MODIFY) {
4867 struct got_fileindex_entry *ie;
4868 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4869 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4870 case GOT_FILEIDX_MODE_REGULAR_FILE:
4871 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4872 ct->mode = S_IFREG;
4873 break;
4874 case GOT_FILEIDX_MODE_SYMLINK:
4875 ct->mode = S_IFLNK;
4876 break;
4877 default:
4878 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4879 goto done;
4881 ct->mode |= got_fileindex_entry_perms_get(ie);
4882 } else if (status != GOT_STATUS_DELETE &&
4883 staged_status != GOT_STATUS_DELETE) {
4884 if (dirfd != -1) {
4885 if (fstatat(dirfd, de_name, &sb,
4886 AT_SYMLINK_NOFOLLOW) == -1) {
4887 err = got_error_from_errno2("fstatat",
4888 ct->ondisk_path);
4889 goto done;
4891 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4892 err = got_error_from_errno2("lstat", ct->ondisk_path);
4893 goto done;
4895 ct->mode = sb.st_mode;
4898 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4899 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4900 relpath) == -1) {
4901 err = got_error_from_errno("asprintf");
4902 goto done;
4905 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4906 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4907 int is_bad_symlink;
4908 char target_path[PATH_MAX];
4909 ssize_t target_len;
4910 target_len = readlink(ct->ondisk_path, target_path,
4911 sizeof(target_path));
4912 if (target_len == -1) {
4913 err = got_error_from_errno2("readlink",
4914 ct->ondisk_path);
4915 goto done;
4917 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4918 target_len, ct->ondisk_path, a->worktree->root_path);
4919 if (err)
4920 goto done;
4921 if (is_bad_symlink) {
4922 err = got_error_path(ct->ondisk_path,
4923 GOT_ERR_BAD_SYMLINK);
4924 goto done;
4929 ct->status = status;
4930 ct->staged_status = staged_status;
4931 ct->blob_id = NULL; /* will be filled in when blob gets created */
4932 if (ct->status != GOT_STATUS_ADD &&
4933 ct->staged_status != GOT_STATUS_ADD) {
4934 ct->base_blob_id = got_object_id_dup(blob_id);
4935 if (ct->base_blob_id == NULL) {
4936 err = got_error_from_errno("got_object_id_dup");
4937 goto done;
4939 ct->base_commit_id = got_object_id_dup(commit_id);
4940 if (ct->base_commit_id == NULL) {
4941 err = got_error_from_errno("got_object_id_dup");
4942 goto done;
4945 if (ct->staged_status == GOT_STATUS_ADD ||
4946 ct->staged_status == GOT_STATUS_MODIFY) {
4947 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4948 if (ct->staged_blob_id == NULL) {
4949 err = got_error_from_errno("got_object_id_dup");
4950 goto done;
4953 ct->path = strdup(path);
4954 if (ct->path == NULL) {
4955 err = got_error_from_errno("strdup");
4956 goto done;
4958 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4959 done:
4960 if (ct && (err || new == NULL))
4961 free_commitable(ct);
4962 free(parent_path);
4963 free(path);
4964 return err;
4967 static const struct got_error *write_tree(struct got_object_id **, int *,
4968 struct got_tree_object *, const char *, struct got_pathlist_head *,
4969 got_worktree_status_cb status_cb, void *status_arg,
4970 struct got_repository *);
4972 static const struct got_error *
4973 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4974 struct got_tree_entry *te, const char *parent_path,
4975 struct got_pathlist_head *commitable_paths,
4976 got_worktree_status_cb status_cb, void *status_arg,
4977 struct got_repository *repo)
4979 const struct got_error *err = NULL;
4980 struct got_tree_object *subtree;
4981 char *subpath;
4983 if (asprintf(&subpath, "%s%s%s", parent_path,
4984 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4985 return got_error_from_errno("asprintf");
4987 err = got_object_open_as_tree(&subtree, repo, &te->id);
4988 if (err)
4989 return err;
4991 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4992 commitable_paths, status_cb, status_arg, repo);
4993 got_object_tree_close(subtree);
4994 free(subpath);
4995 return err;
4998 static const struct got_error *
4999 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5001 const struct got_error *err = NULL;
5002 char *ct_parent_path = NULL;
5004 *match = 0;
5006 if (strchr(ct->in_repo_path, '/') == NULL) {
5007 *match = got_path_is_root_dir(path);
5008 return NULL;
5011 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5012 if (err)
5013 return err;
5014 *match = (strcmp(path, ct_parent_path) == 0);
5015 free(ct_parent_path);
5016 return err;
5019 static mode_t
5020 get_ct_file_mode(struct got_commitable *ct)
5022 if (S_ISLNK(ct->mode))
5023 return S_IFLNK;
5025 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5028 static const struct got_error *
5029 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5030 struct got_tree_entry *te, struct got_commitable *ct)
5032 const struct got_error *err = NULL;
5034 *new_te = NULL;
5036 err = got_object_tree_entry_dup(new_te, te);
5037 if (err)
5038 goto done;
5040 (*new_te)->mode = get_ct_file_mode(ct);
5042 if (ct->staged_status == GOT_STATUS_MODIFY)
5043 memcpy(&(*new_te)->id, ct->staged_blob_id,
5044 sizeof((*new_te)->id));
5045 else
5046 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5047 done:
5048 if (err && *new_te) {
5049 free(*new_te);
5050 *new_te = NULL;
5052 return err;
5055 static const struct got_error *
5056 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5057 struct got_commitable *ct)
5059 const struct got_error *err = NULL;
5060 char *ct_name = NULL;
5062 *new_te = NULL;
5064 *new_te = calloc(1, sizeof(**new_te));
5065 if (*new_te == NULL)
5066 return got_error_from_errno("calloc");
5068 err = got_path_basename(&ct_name, ct->path);
5069 if (err)
5070 goto done;
5071 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5072 sizeof((*new_te)->name)) {
5073 err = got_error(GOT_ERR_NO_SPACE);
5074 goto done;
5077 (*new_te)->mode = get_ct_file_mode(ct);
5079 if (ct->staged_status == GOT_STATUS_ADD)
5080 memcpy(&(*new_te)->id, ct->staged_blob_id,
5081 sizeof((*new_te)->id));
5082 else
5083 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5084 done:
5085 free(ct_name);
5086 if (err && *new_te) {
5087 free(*new_te);
5088 *new_te = NULL;
5090 return err;
5093 static const struct got_error *
5094 insert_tree_entry(struct got_tree_entry *new_te,
5095 struct got_pathlist_head *paths)
5097 const struct got_error *err = NULL;
5098 struct got_pathlist_entry *new_pe;
5100 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5101 if (err)
5102 return err;
5103 if (new_pe == NULL)
5104 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5105 return NULL;
5108 static const struct got_error *
5109 report_ct_status(struct got_commitable *ct,
5110 got_worktree_status_cb status_cb, void *status_arg)
5112 const char *ct_path = ct->path;
5113 unsigned char status;
5115 if (status_cb == NULL) /* no commit progress output desired */
5116 return NULL;
5118 while (ct_path[0] == '/')
5119 ct_path++;
5121 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5122 status = ct->staged_status;
5123 else
5124 status = ct->status;
5126 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5127 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5130 static const struct got_error *
5131 match_modified_subtree(int *modified, struct got_tree_entry *te,
5132 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5134 const struct got_error *err = NULL;
5135 struct got_pathlist_entry *pe;
5136 char *te_path;
5138 *modified = 0;
5140 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5141 got_path_is_root_dir(base_tree_path) ? "" : "/",
5142 te->name) == -1)
5143 return got_error_from_errno("asprintf");
5145 TAILQ_FOREACH(pe, commitable_paths, entry) {
5146 struct got_commitable *ct = pe->data;
5147 *modified = got_path_is_child(ct->in_repo_path, te_path,
5148 strlen(te_path));
5149 if (*modified)
5150 break;
5153 free(te_path);
5154 return err;
5157 static const struct got_error *
5158 match_deleted_or_modified_ct(struct got_commitable **ctp,
5159 struct got_tree_entry *te, const char *base_tree_path,
5160 struct got_pathlist_head *commitable_paths)
5162 const struct got_error *err = NULL;
5163 struct got_pathlist_entry *pe;
5165 *ctp = NULL;
5167 TAILQ_FOREACH(pe, commitable_paths, entry) {
5168 struct got_commitable *ct = pe->data;
5169 char *ct_name = NULL;
5170 int path_matches;
5172 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5173 if (ct->status != GOT_STATUS_MODIFY &&
5174 ct->status != GOT_STATUS_MODE_CHANGE &&
5175 ct->status != GOT_STATUS_DELETE)
5176 continue;
5177 } else {
5178 if (ct->staged_status != GOT_STATUS_MODIFY &&
5179 ct->staged_status != GOT_STATUS_DELETE)
5180 continue;
5183 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5184 continue;
5186 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5187 if (err)
5188 return err;
5189 if (!path_matches)
5190 continue;
5192 err = got_path_basename(&ct_name, pe->path);
5193 if (err)
5194 return err;
5196 if (strcmp(te->name, ct_name) != 0) {
5197 free(ct_name);
5198 continue;
5200 free(ct_name);
5202 *ctp = ct;
5203 break;
5206 return err;
5209 static const struct got_error *
5210 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5211 const char *child_path, const char *path_base_tree,
5212 struct got_pathlist_head *commitable_paths,
5213 got_worktree_status_cb status_cb, void *status_arg,
5214 struct got_repository *repo)
5216 const struct got_error *err = NULL;
5217 struct got_tree_entry *new_te;
5218 char *subtree_path;
5219 struct got_object_id *id = NULL;
5220 int nentries;
5222 *new_tep = NULL;
5224 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5225 got_path_is_root_dir(path_base_tree) ? "" : "/",
5226 child_path) == -1)
5227 return got_error_from_errno("asprintf");
5229 new_te = calloc(1, sizeof(*new_te));
5230 if (new_te == NULL)
5231 return got_error_from_errno("calloc");
5232 new_te->mode = S_IFDIR;
5234 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5235 sizeof(new_te->name)) {
5236 err = got_error(GOT_ERR_NO_SPACE);
5237 goto done;
5239 err = write_tree(&id, &nentries, NULL, subtree_path,
5240 commitable_paths, status_cb, status_arg, repo);
5241 if (err) {
5242 free(new_te);
5243 goto done;
5245 memcpy(&new_te->id, id, sizeof(new_te->id));
5246 done:
5247 free(id);
5248 free(subtree_path);
5249 if (err == NULL)
5250 *new_tep = new_te;
5251 return err;
5254 static const struct got_error *
5255 write_tree(struct got_object_id **new_tree_id, int *nentries,
5256 struct got_tree_object *base_tree, const char *path_base_tree,
5257 struct got_pathlist_head *commitable_paths,
5258 got_worktree_status_cb status_cb, void *status_arg,
5259 struct got_repository *repo)
5261 const struct got_error *err = NULL;
5262 struct got_pathlist_head paths;
5263 struct got_tree_entry *te, *new_te = NULL;
5264 struct got_pathlist_entry *pe;
5266 TAILQ_INIT(&paths);
5267 *nentries = 0;
5269 /* Insert, and recurse into, newly added entries first. */
5270 TAILQ_FOREACH(pe, commitable_paths, entry) {
5271 struct got_commitable *ct = pe->data;
5272 char *child_path = NULL, *slash;
5274 if ((ct->status != GOT_STATUS_ADD &&
5275 ct->staged_status != GOT_STATUS_ADD) ||
5276 (ct->flags & GOT_COMMITABLE_ADDED))
5277 continue;
5279 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5280 strlen(path_base_tree)))
5281 continue;
5283 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5284 ct->in_repo_path);
5285 if (err)
5286 goto done;
5288 slash = strchr(child_path, '/');
5289 if (slash == NULL) {
5290 err = alloc_added_blob_tree_entry(&new_te, ct);
5291 if (err)
5292 goto done;
5293 err = report_ct_status(ct, status_cb, status_arg);
5294 if (err)
5295 goto done;
5296 ct->flags |= GOT_COMMITABLE_ADDED;
5297 err = insert_tree_entry(new_te, &paths);
5298 if (err)
5299 goto done;
5300 (*nentries)++;
5301 } else {
5302 *slash = '\0'; /* trim trailing path components */
5303 if (base_tree == NULL ||
5304 got_object_tree_find_entry(base_tree, child_path)
5305 == NULL) {
5306 err = make_subtree_for_added_blob(&new_te,
5307 child_path, path_base_tree,
5308 commitable_paths, status_cb, status_arg,
5309 repo);
5310 if (err)
5311 goto done;
5312 err = insert_tree_entry(new_te, &paths);
5313 if (err)
5314 goto done;
5315 (*nentries)++;
5320 if (base_tree) {
5321 int i, nbase_entries;
5322 /* Handle modified and deleted entries. */
5323 nbase_entries = got_object_tree_get_nentries(base_tree);
5324 for (i = 0; i < nbase_entries; i++) {
5325 struct got_commitable *ct = NULL;
5327 te = got_object_tree_get_entry(base_tree, i);
5328 if (got_object_tree_entry_is_submodule(te)) {
5329 /* Entry is a submodule; just copy it. */
5330 err = got_object_tree_entry_dup(&new_te, te);
5331 if (err)
5332 goto done;
5333 err = insert_tree_entry(new_te, &paths);
5334 if (err)
5335 goto done;
5336 (*nentries)++;
5337 continue;
5340 if (S_ISDIR(te->mode)) {
5341 int modified;
5342 err = got_object_tree_entry_dup(&new_te, te);
5343 if (err)
5344 goto done;
5345 err = match_modified_subtree(&modified, te,
5346 path_base_tree, commitable_paths);
5347 if (err)
5348 goto done;
5349 /* Avoid recursion into unmodified subtrees. */
5350 if (modified) {
5351 struct got_object_id *new_id;
5352 int nsubentries;
5353 err = write_subtree(&new_id,
5354 &nsubentries, te,
5355 path_base_tree, commitable_paths,
5356 status_cb, status_arg, repo);
5357 if (err)
5358 goto done;
5359 if (nsubentries == 0) {
5360 /* All entries were deleted. */
5361 free(new_id);
5362 continue;
5364 memcpy(&new_te->id, new_id,
5365 sizeof(new_te->id));
5366 free(new_id);
5368 err = insert_tree_entry(new_te, &paths);
5369 if (err)
5370 goto done;
5371 (*nentries)++;
5372 continue;
5375 err = match_deleted_or_modified_ct(&ct, te,
5376 path_base_tree, commitable_paths);
5377 if (err)
5378 goto done;
5379 if (ct) {
5380 /* NB: Deleted entries get dropped here. */
5381 if (ct->status == GOT_STATUS_MODIFY ||
5382 ct->status == GOT_STATUS_MODE_CHANGE ||
5383 ct->staged_status == GOT_STATUS_MODIFY) {
5384 err = alloc_modified_blob_tree_entry(
5385 &new_te, te, ct);
5386 if (err)
5387 goto done;
5388 err = insert_tree_entry(new_te, &paths);
5389 if (err)
5390 goto done;
5391 (*nentries)++;
5393 err = report_ct_status(ct, status_cb,
5394 status_arg);
5395 if (err)
5396 goto done;
5397 } else {
5398 /* Entry is unchanged; just copy it. */
5399 err = got_object_tree_entry_dup(&new_te, te);
5400 if (err)
5401 goto done;
5402 err = insert_tree_entry(new_te, &paths);
5403 if (err)
5404 goto done;
5405 (*nentries)++;
5410 /* Write new list of entries; deleted entries have been dropped. */
5411 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5412 done:
5413 got_pathlist_free(&paths);
5414 return err;
5417 static const struct got_error *
5418 update_fileindex_after_commit(struct got_worktree *worktree,
5419 struct got_pathlist_head *commitable_paths,
5420 struct got_object_id *new_base_commit_id,
5421 struct got_fileindex *fileindex, int have_staged_files)
5423 const struct got_error *err = NULL;
5424 struct got_pathlist_entry *pe;
5425 char *relpath = NULL;
5427 TAILQ_FOREACH(pe, commitable_paths, entry) {
5428 struct got_fileindex_entry *ie;
5429 struct got_commitable *ct = pe->data;
5431 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5433 err = got_path_skip_common_ancestor(&relpath,
5434 worktree->root_path, ct->ondisk_path);
5435 if (err)
5436 goto done;
5438 if (ie) {
5439 if (ct->status == GOT_STATUS_DELETE ||
5440 ct->staged_status == GOT_STATUS_DELETE) {
5441 got_fileindex_entry_remove(fileindex, ie);
5442 } else if (ct->staged_status == GOT_STATUS_ADD ||
5443 ct->staged_status == GOT_STATUS_MODIFY) {
5444 got_fileindex_entry_stage_set(ie,
5445 GOT_FILEIDX_STAGE_NONE);
5446 got_fileindex_entry_staged_filetype_set(ie, 0);
5448 err = got_fileindex_entry_update(ie,
5449 worktree->root_fd, relpath,
5450 ct->staged_blob_id->sha1,
5451 new_base_commit_id->sha1,
5452 !have_staged_files);
5453 } else
5454 err = got_fileindex_entry_update(ie,
5455 worktree->root_fd, relpath,
5456 ct->blob_id->sha1,
5457 new_base_commit_id->sha1,
5458 !have_staged_files);
5459 } else {
5460 err = got_fileindex_entry_alloc(&ie, pe->path);
5461 if (err)
5462 goto done;
5463 err = got_fileindex_entry_update(ie,
5464 worktree->root_fd, relpath, ct->blob_id->sha1,
5465 new_base_commit_id->sha1, 1);
5466 if (err) {
5467 got_fileindex_entry_free(ie);
5468 goto done;
5470 err = got_fileindex_entry_add(fileindex, ie);
5471 if (err) {
5472 got_fileindex_entry_free(ie);
5473 goto done;
5476 free(relpath);
5477 relpath = NULL;
5479 done:
5480 free(relpath);
5481 return err;
5485 static const struct got_error *
5486 check_out_of_date(const char *in_repo_path, unsigned char status,
5487 unsigned char staged_status, struct got_object_id *base_blob_id,
5488 struct got_object_id *base_commit_id,
5489 struct got_object_id *head_commit_id, struct got_repository *repo,
5490 int ood_errcode)
5492 const struct got_error *err = NULL;
5493 struct got_object_id *id = NULL;
5495 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5496 /* Trivial case: base commit == head commit */
5497 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5498 return NULL;
5500 * Ensure file content which local changes were based
5501 * on matches file content in the branch head.
5503 err = got_object_id_by_path(&id, repo, head_commit_id,
5504 in_repo_path);
5505 if (err) {
5506 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5507 err = got_error(ood_errcode);
5508 goto done;
5509 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5510 err = got_error(ood_errcode);
5511 } else {
5512 /* Require that added files don't exist in the branch head. */
5513 err = got_object_id_by_path(&id, repo, head_commit_id,
5514 in_repo_path);
5515 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5516 goto done;
5517 err = id ? got_error(ood_errcode) : NULL;
5519 done:
5520 free(id);
5521 return err;
5524 const struct got_error *
5525 commit_worktree(struct got_object_id **new_commit_id,
5526 struct got_pathlist_head *commitable_paths,
5527 struct got_object_id *head_commit_id,
5528 struct got_object_id *parent_id2,
5529 struct got_worktree *worktree,
5530 const char *author, const char *committer,
5531 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5532 got_worktree_status_cb status_cb, void *status_arg,
5533 struct got_repository *repo)
5535 const struct got_error *err = NULL, *unlockerr = NULL;
5536 struct got_pathlist_entry *pe;
5537 const char *head_ref_name = NULL;
5538 struct got_commit_object *head_commit = NULL;
5539 struct got_reference *head_ref2 = NULL;
5540 struct got_object_id *head_commit_id2 = NULL;
5541 struct got_tree_object *head_tree = NULL;
5542 struct got_object_id *new_tree_id = NULL;
5543 int nentries, nparents = 0;
5544 struct got_object_id_queue parent_ids;
5545 struct got_object_qid *pid = NULL;
5546 char *logmsg = NULL;
5548 *new_commit_id = NULL;
5550 STAILQ_INIT(&parent_ids);
5552 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5553 if (err)
5554 goto done;
5556 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5557 if (err)
5558 goto done;
5560 if (commit_msg_cb != NULL) {
5561 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5562 if (err)
5563 goto done;
5566 if (logmsg == NULL || strlen(logmsg) == 0) {
5567 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5568 goto done;
5571 /* Create blobs from added and modified files and record their IDs. */
5572 TAILQ_FOREACH(pe, commitable_paths, entry) {
5573 struct got_commitable *ct = pe->data;
5574 char *ondisk_path;
5576 /* Blobs for staged files already exist. */
5577 if (ct->staged_status == GOT_STATUS_ADD ||
5578 ct->staged_status == GOT_STATUS_MODIFY)
5579 continue;
5581 if (ct->status != GOT_STATUS_ADD &&
5582 ct->status != GOT_STATUS_MODIFY &&
5583 ct->status != GOT_STATUS_MODE_CHANGE)
5584 continue;
5586 if (asprintf(&ondisk_path, "%s/%s",
5587 worktree->root_path, pe->path) == -1) {
5588 err = got_error_from_errno("asprintf");
5589 goto done;
5591 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5592 free(ondisk_path);
5593 if (err)
5594 goto done;
5597 /* Recursively write new tree objects. */
5598 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5599 commitable_paths, status_cb, status_arg, repo);
5600 if (err)
5601 goto done;
5603 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5604 if (err)
5605 goto done;
5606 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5607 nparents++;
5608 if (parent_id2) {
5609 err = got_object_qid_alloc(&pid, parent_id2);
5610 if (err)
5611 goto done;
5612 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5613 nparents++;
5615 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5616 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5617 if (logmsg != NULL)
5618 free(logmsg);
5619 if (err)
5620 goto done;
5622 /* Check if a concurrent commit to our branch has occurred. */
5623 head_ref_name = got_worktree_get_head_ref_name(worktree);
5624 if (head_ref_name == NULL) {
5625 err = got_error_from_errno("got_worktree_get_head_ref_name");
5626 goto done;
5628 /* Lock the reference here to prevent concurrent modification. */
5629 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5630 if (err)
5631 goto done;
5632 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5633 if (err)
5634 goto done;
5635 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5636 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5637 goto done;
5639 /* Update branch head in repository. */
5640 err = got_ref_change_ref(head_ref2, *new_commit_id);
5641 if (err)
5642 goto done;
5643 err = got_ref_write(head_ref2, repo);
5644 if (err)
5645 goto done;
5647 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5648 if (err)
5649 goto done;
5651 err = ref_base_commit(worktree, repo);
5652 if (err)
5653 goto done;
5654 done:
5655 got_object_id_queue_free(&parent_ids);
5656 if (head_tree)
5657 got_object_tree_close(head_tree);
5658 if (head_commit)
5659 got_object_commit_close(head_commit);
5660 free(head_commit_id2);
5661 if (head_ref2) {
5662 unlockerr = got_ref_unlock(head_ref2);
5663 if (unlockerr && err == NULL)
5664 err = unlockerr;
5665 got_ref_close(head_ref2);
5667 return err;
5670 static const struct got_error *
5671 check_path_is_commitable(const char *path,
5672 struct got_pathlist_head *commitable_paths)
5674 struct got_pathlist_entry *cpe = NULL;
5675 size_t path_len = strlen(path);
5677 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5678 struct got_commitable *ct = cpe->data;
5679 const char *ct_path = ct->path;
5681 while (ct_path[0] == '/')
5682 ct_path++;
5684 if (strcmp(path, ct_path) == 0 ||
5685 got_path_is_child(ct_path, path, path_len))
5686 break;
5689 if (cpe == NULL)
5690 return got_error_path(path, GOT_ERR_BAD_PATH);
5692 return NULL;
5695 static const struct got_error *
5696 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5698 int *have_staged_files = arg;
5700 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5701 *have_staged_files = 1;
5702 return got_error(GOT_ERR_CANCELLED);
5705 return NULL;
5708 static const struct got_error *
5709 check_non_staged_files(struct got_fileindex *fileindex,
5710 struct got_pathlist_head *paths)
5712 struct got_pathlist_entry *pe;
5713 struct got_fileindex_entry *ie;
5715 TAILQ_FOREACH(pe, paths, entry) {
5716 if (pe->path[0] == '\0')
5717 continue;
5718 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5719 if (ie == NULL)
5720 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5721 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5722 return got_error_path(pe->path,
5723 GOT_ERR_FILE_NOT_STAGED);
5726 return NULL;
5729 const struct got_error *
5730 got_worktree_commit(struct got_object_id **new_commit_id,
5731 struct got_worktree *worktree, struct got_pathlist_head *paths,
5732 const char *author, const char *committer, int allow_bad_symlinks,
5733 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5734 got_worktree_status_cb status_cb, void *status_arg,
5735 struct got_repository *repo)
5737 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5738 struct got_fileindex *fileindex = NULL;
5739 char *fileindex_path = NULL;
5740 struct got_pathlist_head commitable_paths;
5741 struct collect_commitables_arg cc_arg;
5742 struct got_pathlist_entry *pe;
5743 struct got_reference *head_ref = NULL;
5744 struct got_object_id *head_commit_id = NULL;
5745 int have_staged_files = 0;
5747 *new_commit_id = NULL;
5749 TAILQ_INIT(&commitable_paths);
5751 err = lock_worktree(worktree, LOCK_EX);
5752 if (err)
5753 goto done;
5755 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5756 if (err)
5757 goto done;
5759 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5760 if (err)
5761 goto done;
5763 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5764 if (err)
5765 goto done;
5767 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5768 &have_staged_files);
5769 if (err && err->code != GOT_ERR_CANCELLED)
5770 goto done;
5771 if (have_staged_files) {
5772 err = check_non_staged_files(fileindex, paths);
5773 if (err)
5774 goto done;
5777 cc_arg.commitable_paths = &commitable_paths;
5778 cc_arg.worktree = worktree;
5779 cc_arg.fileindex = fileindex;
5780 cc_arg.repo = repo;
5781 cc_arg.have_staged_files = have_staged_files;
5782 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5783 TAILQ_FOREACH(pe, paths, entry) {
5784 err = worktree_status(worktree, pe->path, fileindex, repo,
5785 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5786 if (err)
5787 goto done;
5790 if (TAILQ_EMPTY(&commitable_paths)) {
5791 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5792 goto done;
5795 TAILQ_FOREACH(pe, paths, entry) {
5796 err = check_path_is_commitable(pe->path, &commitable_paths);
5797 if (err)
5798 goto done;
5801 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5802 struct got_commitable *ct = pe->data;
5803 const char *ct_path = ct->in_repo_path;
5805 while (ct_path[0] == '/')
5806 ct_path++;
5807 err = check_out_of_date(ct_path, ct->status,
5808 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5809 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5810 if (err)
5811 goto done;
5815 err = commit_worktree(new_commit_id, &commitable_paths,
5816 head_commit_id, NULL, worktree, author, committer,
5817 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5818 if (err)
5819 goto done;
5821 err = update_fileindex_after_commit(worktree, &commitable_paths,
5822 *new_commit_id, fileindex, have_staged_files);
5823 sync_err = sync_fileindex(fileindex, fileindex_path);
5824 if (sync_err && err == NULL)
5825 err = sync_err;
5826 done:
5827 if (fileindex)
5828 got_fileindex_free(fileindex);
5829 free(fileindex_path);
5830 unlockerr = lock_worktree(worktree, LOCK_SH);
5831 if (unlockerr && err == NULL)
5832 err = unlockerr;
5833 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5834 struct got_commitable *ct = pe->data;
5835 free_commitable(ct);
5837 got_pathlist_free(&commitable_paths);
5838 return err;
5841 const char *
5842 got_commitable_get_path(struct got_commitable *ct)
5844 return ct->path;
5847 unsigned int
5848 got_commitable_get_status(struct got_commitable *ct)
5850 return ct->status;
5853 struct check_rebase_ok_arg {
5854 struct got_worktree *worktree;
5855 struct got_repository *repo;
5858 static const struct got_error *
5859 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5861 const struct got_error *err = NULL;
5862 struct check_rebase_ok_arg *a = arg;
5863 unsigned char status;
5864 struct stat sb;
5865 char *ondisk_path;
5867 /* Reject rebase of a work tree with mixed base commits. */
5868 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5869 SHA1_DIGEST_LENGTH))
5870 return got_error(GOT_ERR_MIXED_COMMITS);
5872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5873 == -1)
5874 return got_error_from_errno("asprintf");
5876 /* Reject rebase of a work tree with modified or staged files. */
5877 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5878 free(ondisk_path);
5879 if (err)
5880 return err;
5882 if (status != GOT_STATUS_NO_CHANGE)
5883 return got_error(GOT_ERR_MODIFIED);
5884 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5885 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5887 return NULL;
5890 const struct got_error *
5891 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5892 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5893 struct got_worktree *worktree, struct got_reference *branch,
5894 struct got_repository *repo)
5896 const struct got_error *err = NULL;
5897 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5898 char *branch_ref_name = NULL;
5899 char *fileindex_path = NULL;
5900 struct check_rebase_ok_arg ok_arg;
5901 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5902 struct got_object_id *wt_branch_tip = NULL;
5904 *new_base_branch_ref = NULL;
5905 *tmp_branch = NULL;
5906 *fileindex = NULL;
5908 err = lock_worktree(worktree, LOCK_EX);
5909 if (err)
5910 return err;
5912 err = open_fileindex(fileindex, &fileindex_path, worktree);
5913 if (err)
5914 goto done;
5916 ok_arg.worktree = worktree;
5917 ok_arg.repo = repo;
5918 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5919 &ok_arg);
5920 if (err)
5921 goto done;
5923 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5924 if (err)
5925 goto done;
5927 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5928 if (err)
5929 goto done;
5931 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5932 if (err)
5933 goto done;
5935 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5936 0);
5937 if (err)
5938 goto done;
5940 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5941 if (err)
5942 goto done;
5943 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5944 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5945 goto done;
5948 err = got_ref_alloc_symref(new_base_branch_ref,
5949 new_base_branch_ref_name, wt_branch);
5950 if (err)
5951 goto done;
5952 err = got_ref_write(*new_base_branch_ref, repo);
5953 if (err)
5954 goto done;
5956 /* TODO Lock original branch's ref while rebasing? */
5958 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5959 if (err)
5960 goto done;
5962 err = got_ref_write(branch_ref, repo);
5963 if (err)
5964 goto done;
5966 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5967 worktree->base_commit_id);
5968 if (err)
5969 goto done;
5970 err = got_ref_write(*tmp_branch, repo);
5971 if (err)
5972 goto done;
5974 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5975 if (err)
5976 goto done;
5977 done:
5978 free(fileindex_path);
5979 free(tmp_branch_name);
5980 free(new_base_branch_ref_name);
5981 free(branch_ref_name);
5982 if (branch_ref)
5983 got_ref_close(branch_ref);
5984 if (wt_branch)
5985 got_ref_close(wt_branch);
5986 free(wt_branch_tip);
5987 if (err) {
5988 if (*new_base_branch_ref) {
5989 got_ref_close(*new_base_branch_ref);
5990 *new_base_branch_ref = NULL;
5992 if (*tmp_branch) {
5993 got_ref_close(*tmp_branch);
5994 *tmp_branch = NULL;
5996 if (*fileindex) {
5997 got_fileindex_free(*fileindex);
5998 *fileindex = NULL;
6000 lock_worktree(worktree, LOCK_SH);
6002 return err;
6005 const struct got_error *
6006 got_worktree_rebase_continue(struct got_object_id **commit_id,
6007 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6008 struct got_reference **branch, struct got_fileindex **fileindex,
6009 struct got_worktree *worktree, struct got_repository *repo)
6011 const struct got_error *err;
6012 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6013 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6014 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6015 char *fileindex_path = NULL;
6016 int have_staged_files = 0;
6018 *commit_id = NULL;
6019 *new_base_branch = NULL;
6020 *tmp_branch = NULL;
6021 *branch = NULL;
6022 *fileindex = NULL;
6024 err = lock_worktree(worktree, LOCK_EX);
6025 if (err)
6026 return err;
6028 err = open_fileindex(fileindex, &fileindex_path, worktree);
6029 if (err)
6030 goto done;
6032 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6033 &have_staged_files);
6034 if (err && err->code != GOT_ERR_CANCELLED)
6035 goto done;
6036 if (have_staged_files) {
6037 err = got_error(GOT_ERR_STAGED_PATHS);
6038 goto done;
6041 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6042 if (err)
6043 goto done;
6045 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6046 if (err)
6047 goto done;
6049 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6050 if (err)
6051 goto done;
6053 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6054 if (err)
6055 goto done;
6057 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6058 if (err)
6059 goto done;
6061 err = got_ref_open(branch, repo,
6062 got_ref_get_symref_target(branch_ref), 0);
6063 if (err)
6064 goto done;
6066 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6067 if (err)
6068 goto done;
6070 err = got_ref_resolve(commit_id, repo, commit_ref);
6071 if (err)
6072 goto done;
6074 err = got_ref_open(new_base_branch, repo,
6075 new_base_branch_ref_name, 0);
6076 if (err)
6077 goto done;
6079 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6080 if (err)
6081 goto done;
6082 done:
6083 free(commit_ref_name);
6084 free(branch_ref_name);
6085 free(fileindex_path);
6086 if (commit_ref)
6087 got_ref_close(commit_ref);
6088 if (branch_ref)
6089 got_ref_close(branch_ref);
6090 if (err) {
6091 free(*commit_id);
6092 *commit_id = NULL;
6093 if (*tmp_branch) {
6094 got_ref_close(*tmp_branch);
6095 *tmp_branch = NULL;
6097 if (*new_base_branch) {
6098 got_ref_close(*new_base_branch);
6099 *new_base_branch = NULL;
6101 if (*branch) {
6102 got_ref_close(*branch);
6103 *branch = NULL;
6105 if (*fileindex) {
6106 got_fileindex_free(*fileindex);
6107 *fileindex = NULL;
6109 lock_worktree(worktree, LOCK_SH);
6111 return err;
6114 const struct got_error *
6115 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6117 const struct got_error *err;
6118 char *tmp_branch_name = NULL;
6120 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6121 if (err)
6122 return err;
6124 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6125 free(tmp_branch_name);
6126 return NULL;
6129 static const struct got_error *
6130 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6131 char **logmsg, void *arg)
6133 *logmsg = arg;
6134 return NULL;
6137 static const struct got_error *
6138 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6139 const char *path, struct got_object_id *blob_id,
6140 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6141 int dirfd, const char *de_name)
6143 return NULL;
6146 struct collect_merged_paths_arg {
6147 got_worktree_checkout_cb progress_cb;
6148 void *progress_arg;
6149 struct got_pathlist_head *merged_paths;
6152 static const struct got_error *
6153 collect_merged_paths(void *arg, unsigned char status, const char *path)
6155 const struct got_error *err;
6156 struct collect_merged_paths_arg *a = arg;
6157 char *p;
6158 struct got_pathlist_entry *new;
6160 err = (*a->progress_cb)(a->progress_arg, status, path);
6161 if (err)
6162 return err;
6164 if (status != GOT_STATUS_MERGE &&
6165 status != GOT_STATUS_ADD &&
6166 status != GOT_STATUS_DELETE &&
6167 status != GOT_STATUS_CONFLICT)
6168 return NULL;
6170 p = strdup(path);
6171 if (p == NULL)
6172 return got_error_from_errno("strdup");
6174 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6175 if (err || new == NULL)
6176 free(p);
6177 return err;
6180 void
6181 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6183 struct got_pathlist_entry *pe;
6185 TAILQ_FOREACH(pe, merged_paths, entry)
6186 free((char *)pe->path);
6188 got_pathlist_free(merged_paths);
6191 static const struct got_error *
6192 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6193 int is_rebase, struct got_repository *repo)
6195 const struct got_error *err;
6196 struct got_reference *commit_ref = NULL;
6198 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6199 if (err) {
6200 if (err->code != GOT_ERR_NOT_REF)
6201 goto done;
6202 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6203 if (err)
6204 goto done;
6205 err = got_ref_write(commit_ref, repo);
6206 if (err)
6207 goto done;
6208 } else if (is_rebase) {
6209 struct got_object_id *stored_id;
6210 int cmp;
6212 err = got_ref_resolve(&stored_id, repo, commit_ref);
6213 if (err)
6214 goto done;
6215 cmp = got_object_id_cmp(commit_id, stored_id);
6216 free(stored_id);
6217 if (cmp != 0) {
6218 err = got_error(GOT_ERR_REBASE_COMMITID);
6219 goto done;
6222 done:
6223 if (commit_ref)
6224 got_ref_close(commit_ref);
6225 return err;
6228 static const struct got_error *
6229 rebase_merge_files(struct got_pathlist_head *merged_paths,
6230 const char *commit_ref_name, struct got_worktree *worktree,
6231 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6232 struct got_object_id *commit_id, struct got_repository *repo,
6233 got_worktree_checkout_cb progress_cb, void *progress_arg,
6234 got_cancel_cb cancel_cb, void *cancel_arg)
6236 const struct got_error *err;
6237 struct got_reference *commit_ref = NULL;
6238 struct collect_merged_paths_arg cmp_arg;
6239 char *fileindex_path;
6241 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6243 err = get_fileindex_path(&fileindex_path, worktree);
6244 if (err)
6245 return err;
6247 cmp_arg.progress_cb = progress_cb;
6248 cmp_arg.progress_arg = progress_arg;
6249 cmp_arg.merged_paths = merged_paths;
6250 err = merge_files(worktree, fileindex, fileindex_path,
6251 parent_commit_id, commit_id, repo, collect_merged_paths,
6252 &cmp_arg, cancel_cb, cancel_arg);
6253 if (commit_ref)
6254 got_ref_close(commit_ref);
6255 return err;
6258 const struct got_error *
6259 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6260 struct got_worktree *worktree, struct got_fileindex *fileindex,
6261 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6262 struct got_repository *repo,
6263 got_worktree_checkout_cb progress_cb, void *progress_arg,
6264 got_cancel_cb cancel_cb, void *cancel_arg)
6266 const struct got_error *err;
6267 char *commit_ref_name;
6269 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6270 if (err)
6271 return err;
6273 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6274 if (err)
6275 goto done;
6277 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6278 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6279 progress_arg, cancel_cb, cancel_arg);
6280 done:
6281 free(commit_ref_name);
6282 return err;
6285 const struct got_error *
6286 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6287 struct got_worktree *worktree, struct got_fileindex *fileindex,
6288 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6289 struct got_repository *repo,
6290 got_worktree_checkout_cb progress_cb, void *progress_arg,
6291 got_cancel_cb cancel_cb, void *cancel_arg)
6293 const struct got_error *err;
6294 char *commit_ref_name;
6296 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6297 if (err)
6298 return err;
6300 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6301 if (err)
6302 goto done;
6304 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6305 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6306 progress_arg, cancel_cb, cancel_arg);
6307 done:
6308 free(commit_ref_name);
6309 return err;
6312 static const struct got_error *
6313 rebase_commit(struct got_object_id **new_commit_id,
6314 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6315 struct got_worktree *worktree, struct got_fileindex *fileindex,
6316 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6317 const char *new_logmsg, struct got_repository *repo)
6319 const struct got_error *err, *sync_err;
6320 struct got_pathlist_head commitable_paths;
6321 struct collect_commitables_arg cc_arg;
6322 char *fileindex_path = NULL;
6323 struct got_reference *head_ref = NULL;
6324 struct got_object_id *head_commit_id = NULL;
6325 char *logmsg = NULL;
6327 TAILQ_INIT(&commitable_paths);
6328 *new_commit_id = NULL;
6330 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6332 err = get_fileindex_path(&fileindex_path, worktree);
6333 if (err)
6334 return err;
6336 cc_arg.commitable_paths = &commitable_paths;
6337 cc_arg.worktree = worktree;
6338 cc_arg.repo = repo;
6339 cc_arg.have_staged_files = 0;
6341 * If possible get the status of individual files directly to
6342 * avoid crawling the entire work tree once per rebased commit.
6344 * Ideally, merged_paths would contain a list of commitables
6345 * we could use so we could skip worktree_status() entirely.
6346 * However, we would then need carefully keep track of cumulative
6347 * effects of operations such as file additions and deletions
6348 * in 'got histedit -f' (folding multiple commits into one),
6349 * and this extra complexity is not really worth it.
6351 if (merged_paths) {
6352 struct got_pathlist_entry *pe;
6353 TAILQ_FOREACH(pe, merged_paths, entry) {
6354 err = worktree_status(worktree, pe->path, fileindex,
6355 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6356 0);
6357 if (err)
6358 goto done;
6360 } else {
6361 err = worktree_status(worktree, "", fileindex, repo,
6362 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6363 if (err)
6364 goto done;
6367 if (TAILQ_EMPTY(&commitable_paths)) {
6368 /* No-op change; commit will be elided. */
6369 err = got_ref_delete(commit_ref, repo);
6370 if (err)
6371 goto done;
6372 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6373 goto done;
6376 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6377 if (err)
6378 goto done;
6380 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6381 if (err)
6382 goto done;
6384 if (new_logmsg) {
6385 logmsg = strdup(new_logmsg);
6386 if (logmsg == NULL) {
6387 err = got_error_from_errno("strdup");
6388 goto done;
6390 } else {
6391 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6392 if (err)
6393 goto done;
6396 /* NB: commit_worktree will call free(logmsg) */
6397 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6398 NULL, worktree, got_object_commit_get_author(orig_commit),
6399 got_object_commit_get_committer(orig_commit),
6400 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6401 if (err)
6402 goto done;
6404 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6405 if (err)
6406 goto done;
6408 err = got_ref_delete(commit_ref, repo);
6409 if (err)
6410 goto done;
6412 err = update_fileindex_after_commit(worktree, &commitable_paths,
6413 *new_commit_id, fileindex, 0);
6414 sync_err = sync_fileindex(fileindex, fileindex_path);
6415 if (sync_err && err == NULL)
6416 err = sync_err;
6417 done:
6418 free(fileindex_path);
6419 free(head_commit_id);
6420 if (head_ref)
6421 got_ref_close(head_ref);
6422 if (err) {
6423 free(*new_commit_id);
6424 *new_commit_id = NULL;
6426 return err;
6429 const struct got_error *
6430 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6431 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6432 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6433 struct got_commit_object *orig_commit,
6434 struct got_object_id *orig_commit_id, struct got_repository *repo)
6436 const struct got_error *err;
6437 char *commit_ref_name;
6438 struct got_reference *commit_ref = NULL;
6439 struct got_object_id *commit_id = NULL;
6441 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6442 if (err)
6443 return err;
6445 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6446 if (err)
6447 goto done;
6448 err = got_ref_resolve(&commit_id, repo, commit_ref);
6449 if (err)
6450 goto done;
6451 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6452 err = got_error(GOT_ERR_REBASE_COMMITID);
6453 goto done;
6456 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6457 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6458 done:
6459 if (commit_ref)
6460 got_ref_close(commit_ref);
6461 free(commit_ref_name);
6462 free(commit_id);
6463 return err;
6466 const struct got_error *
6467 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6468 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6469 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6470 struct got_commit_object *orig_commit,
6471 struct got_object_id *orig_commit_id, const char *new_logmsg,
6472 struct got_repository *repo)
6474 const struct got_error *err;
6475 char *commit_ref_name;
6476 struct got_reference *commit_ref = NULL;
6478 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6479 if (err)
6480 return err;
6482 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6483 if (err)
6484 goto done;
6486 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6487 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6488 done:
6489 if (commit_ref)
6490 got_ref_close(commit_ref);
6491 free(commit_ref_name);
6492 return err;
6495 const struct got_error *
6496 got_worktree_rebase_postpone(struct got_worktree *worktree,
6497 struct got_fileindex *fileindex)
6499 if (fileindex)
6500 got_fileindex_free(fileindex);
6501 return lock_worktree(worktree, LOCK_SH);
6504 static const struct got_error *
6505 delete_ref(const char *name, struct got_repository *repo)
6507 const struct got_error *err;
6508 struct got_reference *ref;
6510 err = got_ref_open(&ref, repo, name, 0);
6511 if (err) {
6512 if (err->code == GOT_ERR_NOT_REF)
6513 return NULL;
6514 return err;
6517 err = got_ref_delete(ref, repo);
6518 got_ref_close(ref);
6519 return err;
6522 static const struct got_error *
6523 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6525 const struct got_error *err;
6526 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6527 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6529 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6530 if (err)
6531 goto done;
6532 err = delete_ref(tmp_branch_name, repo);
6533 if (err)
6534 goto done;
6536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6537 if (err)
6538 goto done;
6539 err = delete_ref(new_base_branch_ref_name, repo);
6540 if (err)
6541 goto done;
6543 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6544 if (err)
6545 goto done;
6546 err = delete_ref(branch_ref_name, repo);
6547 if (err)
6548 goto done;
6550 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6551 if (err)
6552 goto done;
6553 err = delete_ref(commit_ref_name, repo);
6554 if (err)
6555 goto done;
6557 done:
6558 free(tmp_branch_name);
6559 free(new_base_branch_ref_name);
6560 free(branch_ref_name);
6561 free(commit_ref_name);
6562 return err;
6565 const struct got_error *
6566 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6567 struct got_object_id *new_commit_id, struct got_repository *repo)
6569 const struct got_error *err;
6570 struct got_reference *ref = NULL;
6571 struct got_object_id *old_commit_id = NULL;
6572 const char *branch_name = NULL;
6573 char *new_id_str = NULL;
6574 char *refname = NULL;
6576 branch_name = got_ref_get_name(branch);
6577 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6578 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6579 branch_name += 11;
6581 err = got_object_id_str(&new_id_str, new_commit_id);
6582 if (err)
6583 return err;
6585 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6586 new_id_str) == -1) {
6587 err = got_error_from_errno("asprintf");
6588 goto done;
6591 err = got_ref_resolve(&old_commit_id, repo, branch);
6592 if (err)
6593 goto done;
6595 err = got_ref_alloc(&ref, refname, old_commit_id);
6596 if (err)
6597 goto done;
6599 err = got_ref_write(ref, repo);
6600 done:
6601 free(new_id_str);
6602 free(refname);
6603 free(old_commit_id);
6604 if (ref)
6605 got_ref_close(ref);
6606 return err;
6609 const struct got_error *
6610 got_worktree_rebase_complete(struct got_worktree *worktree,
6611 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6612 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6613 struct got_repository *repo, int create_backup)
6615 const struct got_error *err, *unlockerr, *sync_err;
6616 struct got_object_id *new_head_commit_id = NULL;
6617 char *fileindex_path = NULL;
6619 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6620 if (err)
6621 return err;
6623 if (create_backup) {
6624 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6625 rebased_branch, new_head_commit_id, repo);
6626 if (err)
6627 goto done;
6630 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6631 if (err)
6632 goto done;
6634 err = got_ref_write(rebased_branch, repo);
6635 if (err)
6636 goto done;
6638 err = got_worktree_set_head_ref(worktree, rebased_branch);
6639 if (err)
6640 goto done;
6642 err = delete_rebase_refs(worktree, repo);
6643 if (err)
6644 goto done;
6646 err = get_fileindex_path(&fileindex_path, worktree);
6647 if (err)
6648 goto done;
6649 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6650 sync_err = sync_fileindex(fileindex, fileindex_path);
6651 if (sync_err && err == NULL)
6652 err = sync_err;
6653 done:
6654 got_fileindex_free(fileindex);
6655 free(fileindex_path);
6656 free(new_head_commit_id);
6657 unlockerr = lock_worktree(worktree, LOCK_SH);
6658 if (unlockerr && err == NULL)
6659 err = unlockerr;
6660 return err;
6663 const struct got_error *
6664 got_worktree_rebase_abort(struct got_worktree *worktree,
6665 struct got_fileindex *fileindex, struct got_repository *repo,
6666 struct got_reference *new_base_branch,
6667 got_worktree_checkout_cb progress_cb, void *progress_arg)
6669 const struct got_error *err, *unlockerr, *sync_err;
6670 struct got_reference *resolved = NULL;
6671 struct got_object_id *commit_id = NULL;
6672 char *fileindex_path = NULL;
6673 struct revert_file_args rfa;
6674 struct got_object_id *tree_id = NULL;
6676 err = lock_worktree(worktree, LOCK_EX);
6677 if (err)
6678 return err;
6680 err = got_ref_open(&resolved, repo,
6681 got_ref_get_symref_target(new_base_branch), 0);
6682 if (err)
6683 goto done;
6685 err = got_worktree_set_head_ref(worktree, resolved);
6686 if (err)
6687 goto done;
6690 * XXX commits to the base branch could have happened while
6691 * we were busy rebasing; should we store the original commit ID
6692 * when rebase begins and read it back here?
6694 err = got_ref_resolve(&commit_id, repo, resolved);
6695 if (err)
6696 goto done;
6698 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6699 if (err)
6700 goto done;
6702 err = got_object_id_by_path(&tree_id, repo,
6703 worktree->base_commit_id, worktree->path_prefix);
6704 if (err)
6705 goto done;
6707 err = delete_rebase_refs(worktree, repo);
6708 if (err)
6709 goto done;
6711 err = get_fileindex_path(&fileindex_path, worktree);
6712 if (err)
6713 goto done;
6715 rfa.worktree = worktree;
6716 rfa.fileindex = fileindex;
6717 rfa.progress_cb = progress_cb;
6718 rfa.progress_arg = progress_arg;
6719 rfa.patch_cb = NULL;
6720 rfa.patch_arg = NULL;
6721 rfa.repo = repo;
6722 rfa.unlink_added_files = 0;
6723 err = worktree_status(worktree, "", fileindex, repo,
6724 revert_file, &rfa, NULL, NULL, 1, 0);
6725 if (err)
6726 goto sync;
6728 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6729 repo, progress_cb, progress_arg, NULL, NULL);
6730 sync:
6731 sync_err = sync_fileindex(fileindex, fileindex_path);
6732 if (sync_err && err == NULL)
6733 err = sync_err;
6734 done:
6735 got_ref_close(resolved);
6736 free(tree_id);
6737 free(commit_id);
6738 if (fileindex)
6739 got_fileindex_free(fileindex);
6740 free(fileindex_path);
6742 unlockerr = lock_worktree(worktree, LOCK_SH);
6743 if (unlockerr && err == NULL)
6744 err = unlockerr;
6745 return err;
6748 const struct got_error *
6749 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6750 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6751 struct got_fileindex **fileindex, struct got_worktree *worktree,
6752 struct got_repository *repo)
6754 const struct got_error *err = NULL;
6755 char *tmp_branch_name = NULL;
6756 char *branch_ref_name = NULL;
6757 char *base_commit_ref_name = NULL;
6758 char *fileindex_path = NULL;
6759 struct check_rebase_ok_arg ok_arg;
6760 struct got_reference *wt_branch = NULL;
6761 struct got_reference *base_commit_ref = NULL;
6763 *tmp_branch = NULL;
6764 *branch_ref = NULL;
6765 *base_commit_id = NULL;
6766 *fileindex = NULL;
6768 err = lock_worktree(worktree, LOCK_EX);
6769 if (err)
6770 return err;
6772 err = open_fileindex(fileindex, &fileindex_path, worktree);
6773 if (err)
6774 goto done;
6776 ok_arg.worktree = worktree;
6777 ok_arg.repo = repo;
6778 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6779 &ok_arg);
6780 if (err)
6781 goto done;
6783 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6784 if (err)
6785 goto done;
6787 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6788 if (err)
6789 goto done;
6791 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6792 worktree);
6793 if (err)
6794 goto done;
6796 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6797 0);
6798 if (err)
6799 goto done;
6801 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6802 if (err)
6803 goto done;
6805 err = got_ref_write(*branch_ref, repo);
6806 if (err)
6807 goto done;
6809 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6810 worktree->base_commit_id);
6811 if (err)
6812 goto done;
6813 err = got_ref_write(base_commit_ref, repo);
6814 if (err)
6815 goto done;
6816 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6817 if (*base_commit_id == NULL) {
6818 err = got_error_from_errno("got_object_id_dup");
6819 goto done;
6822 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6823 worktree->base_commit_id);
6824 if (err)
6825 goto done;
6826 err = got_ref_write(*tmp_branch, repo);
6827 if (err)
6828 goto done;
6830 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6831 if (err)
6832 goto done;
6833 done:
6834 free(fileindex_path);
6835 free(tmp_branch_name);
6836 free(branch_ref_name);
6837 free(base_commit_ref_name);
6838 if (wt_branch)
6839 got_ref_close(wt_branch);
6840 if (err) {
6841 if (*branch_ref) {
6842 got_ref_close(*branch_ref);
6843 *branch_ref = NULL;
6845 if (*tmp_branch) {
6846 got_ref_close(*tmp_branch);
6847 *tmp_branch = NULL;
6849 free(*base_commit_id);
6850 if (*fileindex) {
6851 got_fileindex_free(*fileindex);
6852 *fileindex = NULL;
6854 lock_worktree(worktree, LOCK_SH);
6856 return err;
6859 const struct got_error *
6860 got_worktree_histedit_postpone(struct got_worktree *worktree,
6861 struct got_fileindex *fileindex)
6863 if (fileindex)
6864 got_fileindex_free(fileindex);
6865 return lock_worktree(worktree, LOCK_SH);
6868 const struct got_error *
6869 got_worktree_histedit_in_progress(int *in_progress,
6870 struct got_worktree *worktree)
6872 const struct got_error *err;
6873 char *tmp_branch_name = NULL;
6875 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6876 if (err)
6877 return err;
6879 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6880 free(tmp_branch_name);
6881 return NULL;
6884 const struct got_error *
6885 got_worktree_histedit_continue(struct got_object_id **commit_id,
6886 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6887 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6888 struct got_worktree *worktree, struct got_repository *repo)
6890 const struct got_error *err;
6891 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6892 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6893 struct got_reference *commit_ref = NULL;
6894 struct got_reference *base_commit_ref = NULL;
6895 char *fileindex_path = NULL;
6896 int have_staged_files = 0;
6898 *commit_id = NULL;
6899 *tmp_branch = NULL;
6900 *base_commit_id = NULL;
6901 *fileindex = NULL;
6903 err = lock_worktree(worktree, LOCK_EX);
6904 if (err)
6905 return err;
6907 err = open_fileindex(fileindex, &fileindex_path, worktree);
6908 if (err)
6909 goto done;
6911 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6912 &have_staged_files);
6913 if (err && err->code != GOT_ERR_CANCELLED)
6914 goto done;
6915 if (have_staged_files) {
6916 err = got_error(GOT_ERR_STAGED_PATHS);
6917 goto done;
6920 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6921 if (err)
6922 goto done;
6924 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6925 if (err)
6926 goto done;
6928 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6929 if (err)
6930 goto done;
6932 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6933 worktree);
6934 if (err)
6935 goto done;
6937 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6942 if (err)
6943 goto done;
6944 err = got_ref_resolve(commit_id, repo, commit_ref);
6945 if (err)
6946 goto done;
6948 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6949 if (err)
6950 goto done;
6951 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6952 if (err)
6953 goto done;
6955 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6956 if (err)
6957 goto done;
6958 done:
6959 free(commit_ref_name);
6960 free(branch_ref_name);
6961 free(fileindex_path);
6962 if (commit_ref)
6963 got_ref_close(commit_ref);
6964 if (base_commit_ref)
6965 got_ref_close(base_commit_ref);
6966 if (err) {
6967 free(*commit_id);
6968 *commit_id = NULL;
6969 free(*base_commit_id);
6970 *base_commit_id = NULL;
6971 if (*tmp_branch) {
6972 got_ref_close(*tmp_branch);
6973 *tmp_branch = NULL;
6975 if (*fileindex) {
6976 got_fileindex_free(*fileindex);
6977 *fileindex = NULL;
6979 lock_worktree(worktree, LOCK_EX);
6981 return err;
6984 static const struct got_error *
6985 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6987 const struct got_error *err;
6988 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6989 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6991 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6992 if (err)
6993 goto done;
6994 err = delete_ref(tmp_branch_name, repo);
6995 if (err)
6996 goto done;
6998 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6999 worktree);
7000 if (err)
7001 goto done;
7002 err = delete_ref(base_commit_ref_name, repo);
7003 if (err)
7004 goto done;
7006 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7007 if (err)
7008 goto done;
7009 err = delete_ref(branch_ref_name, repo);
7010 if (err)
7011 goto done;
7013 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7014 if (err)
7015 goto done;
7016 err = delete_ref(commit_ref_name, repo);
7017 if (err)
7018 goto done;
7019 done:
7020 free(tmp_branch_name);
7021 free(base_commit_ref_name);
7022 free(branch_ref_name);
7023 free(commit_ref_name);
7024 return err;
7027 const struct got_error *
7028 got_worktree_histedit_abort(struct got_worktree *worktree,
7029 struct got_fileindex *fileindex, struct got_repository *repo,
7030 struct got_reference *branch, struct got_object_id *base_commit_id,
7031 got_worktree_checkout_cb progress_cb, void *progress_arg)
7033 const struct got_error *err, *unlockerr, *sync_err;
7034 struct got_reference *resolved = NULL;
7035 char *fileindex_path = NULL;
7036 struct got_object_id *tree_id = NULL;
7037 struct revert_file_args rfa;
7039 err = lock_worktree(worktree, LOCK_EX);
7040 if (err)
7041 return err;
7043 err = got_ref_open(&resolved, repo,
7044 got_ref_get_symref_target(branch), 0);
7045 if (err)
7046 goto done;
7048 err = got_worktree_set_head_ref(worktree, resolved);
7049 if (err)
7050 goto done;
7052 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7053 if (err)
7054 goto done;
7056 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7057 worktree->path_prefix);
7058 if (err)
7059 goto done;
7061 err = delete_histedit_refs(worktree, repo);
7062 if (err)
7063 goto done;
7065 err = get_fileindex_path(&fileindex_path, worktree);
7066 if (err)
7067 goto done;
7069 rfa.worktree = worktree;
7070 rfa.fileindex = fileindex;
7071 rfa.progress_cb = progress_cb;
7072 rfa.progress_arg = progress_arg;
7073 rfa.patch_cb = NULL;
7074 rfa.patch_arg = NULL;
7075 rfa.repo = repo;
7076 rfa.unlink_added_files = 0;
7077 err = worktree_status(worktree, "", fileindex, repo,
7078 revert_file, &rfa, NULL, NULL, 1, 0);
7079 if (err)
7080 goto sync;
7082 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7083 repo, progress_cb, progress_arg, NULL, NULL);
7084 sync:
7085 sync_err = sync_fileindex(fileindex, fileindex_path);
7086 if (sync_err && err == NULL)
7087 err = sync_err;
7088 done:
7089 got_ref_close(resolved);
7090 free(tree_id);
7091 free(fileindex_path);
7093 unlockerr = lock_worktree(worktree, LOCK_SH);
7094 if (unlockerr && err == NULL)
7095 err = unlockerr;
7096 return err;
7099 const struct got_error *
7100 got_worktree_histedit_complete(struct got_worktree *worktree,
7101 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7102 struct got_reference *edited_branch, struct got_repository *repo)
7104 const struct got_error *err, *unlockerr, *sync_err;
7105 struct got_object_id *new_head_commit_id = NULL;
7106 struct got_reference *resolved = NULL;
7107 char *fileindex_path = NULL;
7109 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7110 if (err)
7111 return err;
7113 err = got_ref_open(&resolved, repo,
7114 got_ref_get_symref_target(edited_branch), 0);
7115 if (err)
7116 goto done;
7118 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7119 resolved, new_head_commit_id, repo);
7120 if (err)
7121 goto done;
7123 err = got_ref_change_ref(resolved, new_head_commit_id);
7124 if (err)
7125 goto done;
7127 err = got_ref_write(resolved, repo);
7128 if (err)
7129 goto done;
7131 err = got_worktree_set_head_ref(worktree, resolved);
7132 if (err)
7133 goto done;
7135 err = delete_histedit_refs(worktree, repo);
7136 if (err)
7137 goto done;
7139 err = get_fileindex_path(&fileindex_path, worktree);
7140 if (err)
7141 goto done;
7142 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7143 sync_err = sync_fileindex(fileindex, fileindex_path);
7144 if (sync_err && err == NULL)
7145 err = sync_err;
7146 done:
7147 got_fileindex_free(fileindex);
7148 free(fileindex_path);
7149 free(new_head_commit_id);
7150 unlockerr = lock_worktree(worktree, LOCK_SH);
7151 if (unlockerr && err == NULL)
7152 err = unlockerr;
7153 return err;
7156 const struct got_error *
7157 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7158 struct got_object_id *commit_id, struct got_repository *repo)
7160 const struct got_error *err;
7161 char *commit_ref_name;
7163 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7164 if (err)
7165 return err;
7167 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7168 if (err)
7169 goto done;
7171 err = delete_ref(commit_ref_name, repo);
7172 done:
7173 free(commit_ref_name);
7174 return err;
7177 const struct got_error *
7178 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7179 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7180 struct got_worktree *worktree, const char *refname,
7181 struct got_repository *repo)
7183 const struct got_error *err = NULL;
7184 char *fileindex_path = NULL;
7185 struct check_rebase_ok_arg ok_arg;
7187 *fileindex = NULL;
7188 *branch_ref = NULL;
7189 *base_branch_ref = NULL;
7191 err = lock_worktree(worktree, LOCK_EX);
7192 if (err)
7193 return err;
7195 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7196 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7197 "cannot integrate a branch into itself; "
7198 "update -b or different branch name required");
7199 goto done;
7202 err = open_fileindex(fileindex, &fileindex_path, worktree);
7203 if (err)
7204 goto done;
7206 /* Preconditions are the same as for rebase. */
7207 ok_arg.worktree = worktree;
7208 ok_arg.repo = repo;
7209 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7210 &ok_arg);
7211 if (err)
7212 goto done;
7214 err = got_ref_open(branch_ref, repo, refname, 1);
7215 if (err)
7216 goto done;
7218 err = got_ref_open(base_branch_ref, repo,
7219 got_worktree_get_head_ref_name(worktree), 1);
7220 done:
7221 if (err) {
7222 if (*branch_ref) {
7223 got_ref_close(*branch_ref);
7224 *branch_ref = NULL;
7226 if (*base_branch_ref) {
7227 got_ref_close(*base_branch_ref);
7228 *base_branch_ref = NULL;
7230 if (*fileindex) {
7231 got_fileindex_free(*fileindex);
7232 *fileindex = NULL;
7234 lock_worktree(worktree, LOCK_SH);
7236 return err;
7239 const struct got_error *
7240 got_worktree_integrate_continue(struct got_worktree *worktree,
7241 struct got_fileindex *fileindex, struct got_repository *repo,
7242 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7243 got_worktree_checkout_cb progress_cb, void *progress_arg,
7244 got_cancel_cb cancel_cb, void *cancel_arg)
7246 const struct got_error *err = NULL, *sync_err, *unlockerr;
7247 char *fileindex_path = NULL;
7248 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7250 err = get_fileindex_path(&fileindex_path, worktree);
7251 if (err)
7252 goto done;
7254 err = got_ref_resolve(&commit_id, repo, branch_ref);
7255 if (err)
7256 goto done;
7258 err = got_object_id_by_path(&tree_id, repo, commit_id,
7259 worktree->path_prefix);
7260 if (err)
7261 goto done;
7263 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7264 if (err)
7265 goto done;
7267 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7268 progress_cb, progress_arg, cancel_cb, cancel_arg);
7269 if (err)
7270 goto sync;
7272 err = got_ref_change_ref(base_branch_ref, commit_id);
7273 if (err)
7274 goto sync;
7276 err = got_ref_write(base_branch_ref, repo);
7277 if (err)
7278 goto sync;
7280 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7281 sync:
7282 sync_err = sync_fileindex(fileindex, fileindex_path);
7283 if (sync_err && err == NULL)
7284 err = sync_err;
7286 done:
7287 unlockerr = got_ref_unlock(branch_ref);
7288 if (unlockerr && err == NULL)
7289 err = unlockerr;
7290 got_ref_close(branch_ref);
7292 unlockerr = got_ref_unlock(base_branch_ref);
7293 if (unlockerr && err == NULL)
7294 err = unlockerr;
7295 got_ref_close(base_branch_ref);
7297 got_fileindex_free(fileindex);
7298 free(fileindex_path);
7299 free(tree_id);
7301 unlockerr = lock_worktree(worktree, LOCK_SH);
7302 if (unlockerr && err == NULL)
7303 err = unlockerr;
7304 return err;
7307 const struct got_error *
7308 got_worktree_integrate_abort(struct got_worktree *worktree,
7309 struct got_fileindex *fileindex, struct got_repository *repo,
7310 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7312 const struct got_error *err = NULL, *unlockerr = NULL;
7314 got_fileindex_free(fileindex);
7316 err = lock_worktree(worktree, LOCK_SH);
7318 unlockerr = got_ref_unlock(branch_ref);
7319 if (unlockerr && err == NULL)
7320 err = unlockerr;
7321 got_ref_close(branch_ref);
7323 unlockerr = got_ref_unlock(base_branch_ref);
7324 if (unlockerr && err == NULL)
7325 err = unlockerr;
7326 got_ref_close(base_branch_ref);
7328 return err;
7331 const struct got_error *
7332 got_worktree_merge_postpone(struct got_worktree *worktree,
7333 struct got_fileindex *fileindex)
7335 const struct got_error *err, *sync_err;
7336 char *fileindex_path = NULL;
7338 err = get_fileindex_path(&fileindex_path, worktree);
7339 if (err)
7340 goto done;
7342 sync_err = sync_fileindex(fileindex, fileindex_path);
7344 err = lock_worktree(worktree, LOCK_SH);
7345 if (sync_err && err == NULL)
7346 err = sync_err;
7347 done:
7348 got_fileindex_free(fileindex);
7349 free(fileindex_path);
7350 return err;
7353 static const struct got_error *
7354 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7356 const struct got_error *err;
7357 char *branch_refname = NULL, *commit_refname = NULL;
7359 err = get_merge_branch_ref_name(&branch_refname, worktree);
7360 if (err)
7361 goto done;
7362 err = delete_ref(branch_refname, repo);
7363 if (err)
7364 goto done;
7366 err = get_merge_commit_ref_name(&commit_refname, worktree);
7367 if (err)
7368 goto done;
7369 err = delete_ref(commit_refname, repo);
7370 if (err)
7371 goto done;
7373 done:
7374 free(branch_refname);
7375 free(commit_refname);
7376 return err;
7379 struct merge_commit_msg_arg {
7380 struct got_worktree *worktree;
7381 const char *branch_name;
7384 static const struct got_error *
7385 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7386 void *arg)
7388 struct merge_commit_msg_arg *a = arg;
7390 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7391 got_worktree_get_head_ref_name(a->worktree)) == -1)
7392 return got_error_from_errno("asprintf");
7394 return NULL;
7398 const struct got_error *
7399 got_worktree_merge_branch(struct got_worktree *worktree,
7400 struct got_fileindex *fileindex,
7401 struct got_object_id *yca_commit_id,
7402 struct got_object_id *branch_tip,
7403 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7404 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7406 const struct got_error *err;
7407 char *fileindex_path = NULL;
7409 err = get_fileindex_path(&fileindex_path, worktree);
7410 if (err)
7411 goto done;
7413 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7414 worktree);
7415 if (err)
7416 goto done;
7418 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7419 branch_tip, repo, progress_cb, progress_arg,
7420 cancel_cb, cancel_arg);
7421 done:
7422 free(fileindex_path);
7423 return err;
7426 const struct got_error *
7427 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7428 struct got_worktree *worktree, struct got_fileindex *fileindex,
7429 const char *author, const char *committer, int allow_bad_symlinks,
7430 struct got_object_id *branch_tip, const char *branch_name,
7431 struct got_repository *repo,
7432 got_worktree_status_cb status_cb, void *status_arg)
7435 const struct got_error *err = NULL, *sync_err;
7436 struct got_pathlist_head commitable_paths;
7437 struct collect_commitables_arg cc_arg;
7438 struct got_pathlist_entry *pe;
7439 struct got_reference *head_ref = NULL;
7440 struct got_object_id *head_commit_id = NULL;
7441 int have_staged_files = 0;
7442 struct merge_commit_msg_arg mcm_arg;
7443 char *fileindex_path = NULL;
7445 *new_commit_id = NULL;
7447 TAILQ_INIT(&commitable_paths);
7449 err = get_fileindex_path(&fileindex_path, worktree);
7450 if (err)
7451 goto done;
7453 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7454 if (err)
7455 goto done;
7457 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7458 if (err)
7459 goto done;
7461 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7462 &have_staged_files);
7463 if (err && err->code != GOT_ERR_CANCELLED)
7464 goto done;
7465 if (have_staged_files) {
7466 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7467 goto done;
7470 cc_arg.commitable_paths = &commitable_paths;
7471 cc_arg.worktree = worktree;
7472 cc_arg.fileindex = fileindex;
7473 cc_arg.repo = repo;
7474 cc_arg.have_staged_files = have_staged_files;
7475 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7476 err = worktree_status(worktree, "", fileindex, repo,
7477 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7478 if (err)
7479 goto done;
7481 if (TAILQ_EMPTY(&commitable_paths)) {
7482 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7483 "merge of %s cannot proceed", branch_name);
7484 goto done;
7487 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7488 struct got_commitable *ct = pe->data;
7489 const char *ct_path = ct->in_repo_path;
7491 while (ct_path[0] == '/')
7492 ct_path++;
7493 err = check_out_of_date(ct_path, ct->status,
7494 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7495 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7496 if (err)
7497 goto done;
7501 mcm_arg.worktree = worktree;
7502 mcm_arg.branch_name = branch_name;
7503 err = commit_worktree(new_commit_id, &commitable_paths,
7504 head_commit_id, branch_tip, worktree, author, committer,
7505 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7506 if (err)
7507 goto done;
7509 err = update_fileindex_after_commit(worktree, &commitable_paths,
7510 *new_commit_id, fileindex, have_staged_files);
7511 sync_err = sync_fileindex(fileindex, fileindex_path);
7512 if (sync_err && err == NULL)
7513 err = sync_err;
7514 done:
7515 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7516 struct got_commitable *ct = pe->data;
7517 free_commitable(ct);
7519 got_pathlist_free(&commitable_paths);
7520 free(fileindex_path);
7521 return err;
7524 const struct got_error *
7525 got_worktree_merge_complete(struct got_worktree *worktree,
7526 struct got_fileindex *fileindex, struct got_repository *repo)
7528 const struct got_error *err, *unlockerr, *sync_err;
7529 char *fileindex_path = NULL;
7531 err = delete_merge_refs(worktree, repo);
7532 if (err)
7533 goto done;
7535 err = get_fileindex_path(&fileindex_path, worktree);
7536 if (err)
7537 goto done;
7538 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7539 sync_err = sync_fileindex(fileindex, fileindex_path);
7540 if (sync_err && err == NULL)
7541 err = sync_err;
7542 done:
7543 got_fileindex_free(fileindex);
7544 free(fileindex_path);
7545 unlockerr = lock_worktree(worktree, LOCK_SH);
7546 if (unlockerr && err == NULL)
7547 err = unlockerr;
7548 return err;
7551 const struct got_error *
7552 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7553 struct got_repository *repo)
7555 const struct got_error *err;
7556 char *branch_refname = NULL;
7557 struct got_reference *branch_ref = NULL;
7559 *in_progress = 0;
7561 err = get_merge_branch_ref_name(&branch_refname, worktree);
7562 if (err)
7563 return err;
7564 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7565 free(branch_refname);
7566 if (err) {
7567 if (err->code != GOT_ERR_NOT_REF)
7568 return err;
7569 } else
7570 *in_progress = 1;
7572 return NULL;
7575 const struct got_error *got_worktree_merge_prepare(
7576 struct got_fileindex **fileindex, struct got_worktree *worktree,
7577 struct got_reference *branch, struct got_repository *repo)
7579 const struct got_error *err = NULL;
7580 char *fileindex_path = NULL;
7581 char *branch_refname = NULL, *commit_refname = NULL;
7582 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7583 struct got_reference *commit_ref = NULL;
7584 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7585 struct check_rebase_ok_arg ok_arg;
7587 *fileindex = NULL;
7589 err = lock_worktree(worktree, LOCK_EX);
7590 if (err)
7591 return err;
7593 err = open_fileindex(fileindex, &fileindex_path, worktree);
7594 if (err)
7595 goto done;
7597 /* Preconditions are the same as for rebase. */
7598 ok_arg.worktree = worktree;
7599 ok_arg.repo = repo;
7600 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7601 &ok_arg);
7602 if (err)
7603 goto done;
7605 err = get_merge_branch_ref_name(&branch_refname, worktree);
7606 if (err)
7607 return err;
7609 err = get_merge_commit_ref_name(&commit_refname, worktree);
7610 if (err)
7611 return err;
7613 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7614 0);
7615 if (err)
7616 goto done;
7618 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7619 if (err)
7620 goto done;
7622 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7623 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7624 goto done;
7627 err = got_ref_resolve(&branch_tip, repo, branch);
7628 if (err)
7629 goto done;
7631 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7632 if (err)
7633 goto done;
7634 err = got_ref_write(branch_ref, repo);
7635 if (err)
7636 goto done;
7638 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7639 if (err)
7640 goto done;
7641 err = got_ref_write(commit_ref, repo);
7642 if (err)
7643 goto done;
7645 done:
7646 free(branch_refname);
7647 free(commit_refname);
7648 free(fileindex_path);
7649 if (branch_ref)
7650 got_ref_close(branch_ref);
7651 if (commit_ref)
7652 got_ref_close(commit_ref);
7653 if (wt_branch)
7654 got_ref_close(wt_branch);
7655 free(wt_branch_tip);
7656 if (err) {
7657 if (*fileindex) {
7658 got_fileindex_free(*fileindex);
7659 *fileindex = NULL;
7661 lock_worktree(worktree, LOCK_SH);
7663 return err;
7666 const struct got_error *
7667 got_worktree_merge_continue(char **branch_name,
7668 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7669 struct got_worktree *worktree, struct got_repository *repo)
7671 const struct got_error *err;
7672 char *commit_refname = NULL, *branch_refname = NULL;
7673 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7674 char *fileindex_path = NULL;
7675 int have_staged_files = 0;
7677 *branch_name = NULL;
7678 *branch_tip = NULL;
7679 *fileindex = NULL;
7681 err = lock_worktree(worktree, LOCK_EX);
7682 if (err)
7683 return err;
7685 err = open_fileindex(fileindex, &fileindex_path, worktree);
7686 if (err)
7687 goto done;
7689 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7690 &have_staged_files);
7691 if (err && err->code != GOT_ERR_CANCELLED)
7692 goto done;
7693 if (have_staged_files) {
7694 err = got_error(GOT_ERR_STAGED_PATHS);
7695 goto done;
7698 err = get_merge_branch_ref_name(&branch_refname, worktree);
7699 if (err)
7700 goto done;
7702 err = get_merge_commit_ref_name(&commit_refname, worktree);
7703 if (err)
7704 goto done;
7706 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7707 if (err)
7708 goto done;
7710 if (!got_ref_is_symbolic(branch_ref)) {
7711 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7712 "%s is not a symbolic reference",
7713 got_ref_get_name(branch_ref));
7714 goto done;
7716 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7717 if (*branch_name == NULL) {
7718 err = got_error_from_errno("strdup");
7719 goto done;
7722 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7723 if (err)
7724 goto done;
7726 err = got_ref_resolve(branch_tip, repo, commit_ref);
7727 if (err)
7728 goto done;
7729 done:
7730 free(commit_refname);
7731 free(branch_refname);
7732 free(fileindex_path);
7733 if (commit_ref)
7734 got_ref_close(commit_ref);
7735 if (branch_ref)
7736 got_ref_close(branch_ref);
7737 if (err) {
7738 if (*branch_name) {
7739 free(*branch_name);
7740 *branch_name = NULL;
7742 free(*branch_tip);
7743 *branch_tip = NULL;
7744 if (*fileindex) {
7745 got_fileindex_free(*fileindex);
7746 *fileindex = NULL;
7748 lock_worktree(worktree, LOCK_SH);
7750 return err;
7753 const struct got_error *
7754 got_worktree_merge_abort(struct got_worktree *worktree,
7755 struct got_fileindex *fileindex, struct got_repository *repo,
7756 got_worktree_checkout_cb progress_cb, void *progress_arg)
7758 const struct got_error *err, *unlockerr, *sync_err;
7759 struct got_object_id *commit_id = NULL;
7760 char *fileindex_path = NULL;
7761 struct revert_file_args rfa;
7762 struct got_object_id *tree_id = NULL;
7764 err = got_object_id_by_path(&tree_id, repo,
7765 worktree->base_commit_id, worktree->path_prefix);
7766 if (err)
7767 goto done;
7769 err = delete_merge_refs(worktree, repo);
7770 if (err)
7771 goto done;
7773 err = get_fileindex_path(&fileindex_path, worktree);
7774 if (err)
7775 goto done;
7777 rfa.worktree = worktree;
7778 rfa.fileindex = fileindex;
7779 rfa.progress_cb = progress_cb;
7780 rfa.progress_arg = progress_arg;
7781 rfa.patch_cb = NULL;
7782 rfa.patch_arg = NULL;
7783 rfa.repo = repo;
7784 rfa.unlink_added_files = 1;
7785 err = worktree_status(worktree, "", fileindex, repo,
7786 revert_file, &rfa, NULL, NULL, 1, 0);
7787 if (err)
7788 goto sync;
7790 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7791 repo, progress_cb, progress_arg, NULL, NULL);
7792 sync:
7793 sync_err = sync_fileindex(fileindex, fileindex_path);
7794 if (sync_err && err == NULL)
7795 err = sync_err;
7796 done:
7797 free(tree_id);
7798 free(commit_id);
7799 if (fileindex)
7800 got_fileindex_free(fileindex);
7801 free(fileindex_path);
7803 unlockerr = lock_worktree(worktree, LOCK_SH);
7804 if (unlockerr && err == NULL)
7805 err = unlockerr;
7806 return err;
7809 struct check_stage_ok_arg {
7810 struct got_object_id *head_commit_id;
7811 struct got_worktree *worktree;
7812 struct got_fileindex *fileindex;
7813 struct got_repository *repo;
7814 int have_changes;
7817 const struct got_error *
7818 check_stage_ok(void *arg, unsigned char status,
7819 unsigned char staged_status, const char *relpath,
7820 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7821 struct got_object_id *commit_id, int dirfd, const char *de_name)
7823 struct check_stage_ok_arg *a = arg;
7824 const struct got_error *err = NULL;
7825 struct got_fileindex_entry *ie;
7826 struct got_object_id base_commit_id;
7827 struct got_object_id *base_commit_idp = NULL;
7828 char *in_repo_path = NULL, *p;
7830 if (status == GOT_STATUS_UNVERSIONED ||
7831 status == GOT_STATUS_NO_CHANGE)
7832 return NULL;
7833 if (status == GOT_STATUS_NONEXISTENT)
7834 return got_error_set_errno(ENOENT, relpath);
7836 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7837 if (ie == NULL)
7838 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7840 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7841 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7842 relpath) == -1)
7843 return got_error_from_errno("asprintf");
7845 if (got_fileindex_entry_has_commit(ie)) {
7846 memcpy(base_commit_id.sha1, ie->commit_sha1,
7847 SHA1_DIGEST_LENGTH);
7848 base_commit_idp = &base_commit_id;
7851 if (status == GOT_STATUS_CONFLICT) {
7852 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7853 goto done;
7854 } else if (status != GOT_STATUS_ADD &&
7855 status != GOT_STATUS_MODIFY &&
7856 status != GOT_STATUS_DELETE) {
7857 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7858 goto done;
7861 a->have_changes = 1;
7863 p = in_repo_path;
7864 while (p[0] == '/')
7865 p++;
7866 err = check_out_of_date(p, status, staged_status,
7867 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7868 GOT_ERR_STAGE_OUT_OF_DATE);
7869 done:
7870 free(in_repo_path);
7871 return err;
7874 struct stage_path_arg {
7875 struct got_worktree *worktree;
7876 struct got_fileindex *fileindex;
7877 struct got_repository *repo;
7878 got_worktree_status_cb status_cb;
7879 void *status_arg;
7880 got_worktree_patch_cb patch_cb;
7881 void *patch_arg;
7882 int staged_something;
7883 int allow_bad_symlinks;
7886 static const struct got_error *
7887 stage_path(void *arg, unsigned char status,
7888 unsigned char staged_status, const char *relpath,
7889 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7890 struct got_object_id *commit_id, int dirfd, const char *de_name)
7892 struct stage_path_arg *a = arg;
7893 const struct got_error *err = NULL;
7894 struct got_fileindex_entry *ie;
7895 char *ondisk_path = NULL, *path_content = NULL;
7896 uint32_t stage;
7897 struct got_object_id *new_staged_blob_id = NULL;
7898 struct stat sb;
7900 if (status == GOT_STATUS_UNVERSIONED)
7901 return NULL;
7903 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7904 if (ie == NULL)
7905 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7908 relpath)== -1)
7909 return got_error_from_errno("asprintf");
7911 switch (status) {
7912 case GOT_STATUS_ADD:
7913 case GOT_STATUS_MODIFY:
7914 /* XXX could sb.st_mode be passed in by our caller? */
7915 if (lstat(ondisk_path, &sb) == -1) {
7916 err = got_error_from_errno2("lstat", ondisk_path);
7917 break;
7919 if (a->patch_cb) {
7920 if (status == GOT_STATUS_ADD) {
7921 int choice = GOT_PATCH_CHOICE_NONE;
7922 err = (*a->patch_cb)(&choice, a->patch_arg,
7923 status, ie->path, NULL, 1, 1);
7924 if (err)
7925 break;
7926 if (choice != GOT_PATCH_CHOICE_YES)
7927 break;
7928 } else {
7929 err = create_patched_content(&path_content, 0,
7930 staged_blob_id ? staged_blob_id : blob_id,
7931 ondisk_path, dirfd, de_name, ie->path,
7932 a->repo, a->patch_cb, a->patch_arg);
7933 if (err || path_content == NULL)
7934 break;
7937 err = got_object_blob_create(&new_staged_blob_id,
7938 path_content ? path_content : ondisk_path, a->repo);
7939 if (err)
7940 break;
7941 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7942 SHA1_DIGEST_LENGTH);
7943 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7944 stage = GOT_FILEIDX_STAGE_ADD;
7945 else
7946 stage = GOT_FILEIDX_STAGE_MODIFY;
7947 got_fileindex_entry_stage_set(ie, stage);
7948 if (S_ISLNK(sb.st_mode)) {
7949 int is_bad_symlink = 0;
7950 if (!a->allow_bad_symlinks) {
7951 char target_path[PATH_MAX];
7952 ssize_t target_len;
7953 target_len = readlink(ondisk_path, target_path,
7954 sizeof(target_path));
7955 if (target_len == -1) {
7956 err = got_error_from_errno2("readlink",
7957 ondisk_path);
7958 break;
7960 err = is_bad_symlink_target(&is_bad_symlink,
7961 target_path, target_len, ondisk_path,
7962 a->worktree->root_path);
7963 if (err)
7964 break;
7965 if (is_bad_symlink) {
7966 err = got_error_path(ondisk_path,
7967 GOT_ERR_BAD_SYMLINK);
7968 break;
7971 if (is_bad_symlink)
7972 got_fileindex_entry_staged_filetype_set(ie,
7973 GOT_FILEIDX_MODE_BAD_SYMLINK);
7974 else
7975 got_fileindex_entry_staged_filetype_set(ie,
7976 GOT_FILEIDX_MODE_SYMLINK);
7977 } else {
7978 got_fileindex_entry_staged_filetype_set(ie,
7979 GOT_FILEIDX_MODE_REGULAR_FILE);
7981 a->staged_something = 1;
7982 if (a->status_cb == NULL)
7983 break;
7984 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7985 get_staged_status(ie), relpath, blob_id,
7986 new_staged_blob_id, NULL, dirfd, de_name);
7987 break;
7988 case GOT_STATUS_DELETE:
7989 if (staged_status == GOT_STATUS_DELETE)
7990 break;
7991 if (a->patch_cb) {
7992 int choice = GOT_PATCH_CHOICE_NONE;
7993 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7994 ie->path, NULL, 1, 1);
7995 if (err)
7996 break;
7997 if (choice == GOT_PATCH_CHOICE_NO)
7998 break;
7999 if (choice != GOT_PATCH_CHOICE_YES) {
8000 err = got_error(GOT_ERR_PATCH_CHOICE);
8001 break;
8004 stage = GOT_FILEIDX_STAGE_DELETE;
8005 got_fileindex_entry_stage_set(ie, stage);
8006 a->staged_something = 1;
8007 if (a->status_cb == NULL)
8008 break;
8009 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8010 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8011 de_name);
8012 break;
8013 case GOT_STATUS_NO_CHANGE:
8014 break;
8015 case GOT_STATUS_CONFLICT:
8016 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8017 break;
8018 case GOT_STATUS_NONEXISTENT:
8019 err = got_error_set_errno(ENOENT, relpath);
8020 break;
8021 default:
8022 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8023 break;
8026 if (path_content && unlink(path_content) == -1 && err == NULL)
8027 err = got_error_from_errno2("unlink", path_content);
8028 free(path_content);
8029 free(ondisk_path);
8030 free(new_staged_blob_id);
8031 return err;
8034 const struct got_error *
8035 got_worktree_stage(struct got_worktree *worktree,
8036 struct got_pathlist_head *paths,
8037 got_worktree_status_cb status_cb, void *status_arg,
8038 got_worktree_patch_cb patch_cb, void *patch_arg,
8039 int allow_bad_symlinks, struct got_repository *repo)
8041 const struct got_error *err = NULL, *sync_err, *unlockerr;
8042 struct got_pathlist_entry *pe;
8043 struct got_fileindex *fileindex = NULL;
8044 char *fileindex_path = NULL;
8045 struct got_reference *head_ref = NULL;
8046 struct got_object_id *head_commit_id = NULL;
8047 struct check_stage_ok_arg oka;
8048 struct stage_path_arg spa;
8050 err = lock_worktree(worktree, LOCK_EX);
8051 if (err)
8052 return err;
8054 err = got_ref_open(&head_ref, repo,
8055 got_worktree_get_head_ref_name(worktree), 0);
8056 if (err)
8057 goto done;
8058 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8059 if (err)
8060 goto done;
8061 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8062 if (err)
8063 goto done;
8065 /* Check pre-conditions before staging anything. */
8066 oka.head_commit_id = head_commit_id;
8067 oka.worktree = worktree;
8068 oka.fileindex = fileindex;
8069 oka.repo = repo;
8070 oka.have_changes = 0;
8071 TAILQ_FOREACH(pe, paths, entry) {
8072 err = worktree_status(worktree, pe->path, fileindex, repo,
8073 check_stage_ok, &oka, NULL, NULL, 1, 0);
8074 if (err)
8075 goto done;
8077 if (!oka.have_changes) {
8078 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8079 goto done;
8082 spa.worktree = worktree;
8083 spa.fileindex = fileindex;
8084 spa.repo = repo;
8085 spa.patch_cb = patch_cb;
8086 spa.patch_arg = patch_arg;
8087 spa.status_cb = status_cb;
8088 spa.status_arg = status_arg;
8089 spa.staged_something = 0;
8090 spa.allow_bad_symlinks = allow_bad_symlinks;
8091 TAILQ_FOREACH(pe, paths, entry) {
8092 err = worktree_status(worktree, pe->path, fileindex, repo,
8093 stage_path, &spa, NULL, NULL, 1, 0);
8094 if (err)
8095 goto done;
8097 if (!spa.staged_something) {
8098 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8099 goto done;
8102 sync_err = sync_fileindex(fileindex, fileindex_path);
8103 if (sync_err && err == NULL)
8104 err = sync_err;
8105 done:
8106 if (head_ref)
8107 got_ref_close(head_ref);
8108 free(head_commit_id);
8109 free(fileindex_path);
8110 if (fileindex)
8111 got_fileindex_free(fileindex);
8112 unlockerr = lock_worktree(worktree, LOCK_SH);
8113 if (unlockerr && err == NULL)
8114 err = unlockerr;
8115 return err;
8118 struct unstage_path_arg {
8119 struct got_worktree *worktree;
8120 struct got_fileindex *fileindex;
8121 struct got_repository *repo;
8122 got_worktree_checkout_cb progress_cb;
8123 void *progress_arg;
8124 got_worktree_patch_cb patch_cb;
8125 void *patch_arg;
8128 static const struct got_error *
8129 create_unstaged_content(char **path_unstaged_content,
8130 char **path_new_staged_content, struct got_object_id *blob_id,
8131 struct got_object_id *staged_blob_id, const char *relpath,
8132 struct got_repository *repo,
8133 got_worktree_patch_cb patch_cb, void *patch_arg)
8135 const struct got_error *err, *free_err;
8136 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8137 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8138 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8139 struct got_diffreg_result *diffreg_result = NULL;
8140 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8141 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8143 *path_unstaged_content = NULL;
8144 *path_new_staged_content = NULL;
8146 err = got_object_id_str(&label1, blob_id);
8147 if (err)
8148 return err;
8149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8150 if (err)
8151 goto done;
8153 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8154 if (err)
8155 goto done;
8157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8158 if (err)
8159 goto done;
8161 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8162 if (err)
8163 goto done;
8165 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8166 if (err)
8167 goto done;
8169 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8170 if (err)
8171 goto done;
8173 err = got_diff_files(&diffreg_result, f1, label1, f2,
8174 path2, 3, 0, 1, NULL);
8175 if (err)
8176 goto done;
8178 err = got_opentemp_named(path_unstaged_content, &outfile,
8179 "got-unstaged-content");
8180 if (err)
8181 goto done;
8182 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8183 "got-new-staged-content");
8184 if (err)
8185 goto done;
8187 if (fseek(f1, 0L, SEEK_SET) == -1) {
8188 err = got_ferror(f1, GOT_ERR_IO);
8189 goto done;
8191 if (fseek(f2, 0L, SEEK_SET) == -1) {
8192 err = got_ferror(f2, GOT_ERR_IO);
8193 goto done;
8195 /* Count the number of actual changes in the diff result. */
8196 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8197 struct diff_chunk_context cc = {};
8198 diff_chunk_context_load_change(&cc, &nchunks_used,
8199 diffreg_result->result, n, 0);
8200 nchanges++;
8202 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8203 int choice;
8204 err = apply_or_reject_change(&choice, &nchunks_used,
8205 diffreg_result->result, n, relpath, f1, f2,
8206 &line_cur1, &line_cur2,
8207 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8208 if (err)
8209 goto done;
8210 if (choice == GOT_PATCH_CHOICE_YES)
8211 have_content = 1;
8212 else
8213 have_rejected_content = 1;
8214 if (choice == GOT_PATCH_CHOICE_QUIT)
8215 break;
8217 if (have_content || have_rejected_content)
8218 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8219 outfile, rejectfile);
8220 done:
8221 free(label1);
8222 if (blob)
8223 got_object_blob_close(blob);
8224 if (staged_blob)
8225 got_object_blob_close(staged_blob);
8226 free_err = got_diffreg_result_free(diffreg_result);
8227 if (free_err && err == NULL)
8228 err = free_err;
8229 if (f1 && fclose(f1) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", path1);
8231 if (f2 && fclose(f2) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", path2);
8233 if (outfile && fclose(outfile) == EOF && err == NULL)
8234 err = got_error_from_errno2("fclose", *path_unstaged_content);
8235 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8236 err = got_error_from_errno2("fclose", *path_new_staged_content);
8237 if (path1 && unlink(path1) == -1 && err == NULL)
8238 err = got_error_from_errno2("unlink", path1);
8239 if (path2 && unlink(path2) == -1 && err == NULL)
8240 err = got_error_from_errno2("unlink", path2);
8241 if (err || !have_content) {
8242 if (*path_unstaged_content &&
8243 unlink(*path_unstaged_content) == -1 && err == NULL)
8244 err = got_error_from_errno2("unlink",
8245 *path_unstaged_content);
8246 free(*path_unstaged_content);
8247 *path_unstaged_content = NULL;
8249 if (err || !have_content || !have_rejected_content) {
8250 if (*path_new_staged_content &&
8251 unlink(*path_new_staged_content) == -1 && err == NULL)
8252 err = got_error_from_errno2("unlink",
8253 *path_new_staged_content);
8254 free(*path_new_staged_content);
8255 *path_new_staged_content = NULL;
8257 free(path1);
8258 free(path2);
8259 return err;
8262 static const struct got_error *
8263 unstage_hunks(struct got_object_id *staged_blob_id,
8264 struct got_blob_object *blob_base,
8265 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8266 const char *ondisk_path, const char *label_orig,
8267 struct got_worktree *worktree, struct got_repository *repo,
8268 got_worktree_patch_cb patch_cb, void *patch_arg,
8269 got_worktree_checkout_cb progress_cb, void *progress_arg)
8271 const struct got_error *err = NULL;
8272 char *path_unstaged_content = NULL;
8273 char *path_new_staged_content = NULL;
8274 char *parent = NULL, *base_path = NULL;
8275 char *blob_base_path = NULL;
8276 struct got_object_id *new_staged_blob_id = NULL;
8277 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8278 struct stat sb;
8280 err = create_unstaged_content(&path_unstaged_content,
8281 &path_new_staged_content, blob_id, staged_blob_id,
8282 ie->path, repo, patch_cb, patch_arg);
8283 if (err)
8284 return err;
8286 if (path_unstaged_content == NULL)
8287 return NULL;
8289 if (path_new_staged_content) {
8290 err = got_object_blob_create(&new_staged_blob_id,
8291 path_new_staged_content, repo);
8292 if (err)
8293 goto done;
8296 f = fopen(path_unstaged_content, "re");
8297 if (f == NULL) {
8298 err = got_error_from_errno2("fopen",
8299 path_unstaged_content);
8300 goto done;
8302 if (fstat(fileno(f), &sb) == -1) {
8303 err = got_error_from_errno2("fstat", path_unstaged_content);
8304 goto done;
8306 if (got_fileindex_entry_staged_filetype_get(ie) ==
8307 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8308 char link_target[PATH_MAX];
8309 size_t r;
8310 r = fread(link_target, 1, sizeof(link_target), f);
8311 if (r == 0 && ferror(f)) {
8312 err = got_error_from_errno("fread");
8313 goto done;
8315 if (r >= sizeof(link_target)) { /* should not happen */
8316 err = got_error(GOT_ERR_NO_SPACE);
8317 goto done;
8319 link_target[r] = '\0';
8320 err = merge_symlink(worktree, blob_base,
8321 ondisk_path, ie->path, label_orig, link_target,
8322 worktree->base_commit_id, repo, progress_cb,
8323 progress_arg);
8324 } else {
8325 int local_changes_subsumed;
8327 err = got_path_dirname(&parent, ondisk_path);
8328 if (err)
8329 return err;
8331 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8332 parent) == -1) {
8333 err = got_error_from_errno("asprintf");
8334 base_path = NULL;
8335 goto done;
8338 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8339 if (err)
8340 goto done;
8341 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8342 blob_base);
8343 if (err)
8344 goto done;
8347 * In order the run a 3-way merge with a symlink we copy the symlink's
8348 * target path into a temporary file and use that file with diff3.
8350 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8351 err = dump_symlink_target_path_to_file(&f_deriv2,
8352 ondisk_path);
8353 if (err)
8354 goto done;
8355 } else {
8356 int fd;
8357 fd = open(ondisk_path,
8358 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8359 if (fd == -1) {
8360 err = got_error_from_errno2("open", ondisk_path);
8361 goto done;
8363 f_deriv2 = fdopen(fd, "r");
8364 if (f_deriv2 == NULL) {
8365 err = got_error_from_errno2("fdopen", ondisk_path);
8366 close(fd);
8367 goto done;
8371 err = merge_file(&local_changes_subsumed, worktree,
8372 f_base, f, f_deriv2, ondisk_path, ie->path,
8373 got_fileindex_perms_to_st(ie),
8374 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8375 repo, progress_cb, progress_arg);
8377 if (err)
8378 goto done;
8380 if (new_staged_blob_id) {
8381 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8382 SHA1_DIGEST_LENGTH);
8383 } else {
8384 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8385 got_fileindex_entry_staged_filetype_set(ie, 0);
8387 done:
8388 free(new_staged_blob_id);
8389 if (path_unstaged_content &&
8390 unlink(path_unstaged_content) == -1 && err == NULL)
8391 err = got_error_from_errno2("unlink", path_unstaged_content);
8392 if (path_new_staged_content &&
8393 unlink(path_new_staged_content) == -1 && err == NULL)
8394 err = got_error_from_errno2("unlink", path_new_staged_content);
8395 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8396 err = got_error_from_errno2("unlink", blob_base_path);
8397 if (f_base && fclose(f_base) == EOF && err == NULL)
8398 err = got_error_from_errno2("fclose", path_unstaged_content);
8399 if (f && fclose(f) == EOF && err == NULL)
8400 err = got_error_from_errno2("fclose", path_unstaged_content);
8401 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8402 err = got_error_from_errno2("fclose", ondisk_path);
8403 free(path_unstaged_content);
8404 free(path_new_staged_content);
8405 free(blob_base_path);
8406 free(parent);
8407 free(base_path);
8408 return err;
8411 static const struct got_error *
8412 unstage_path(void *arg, unsigned char status,
8413 unsigned char staged_status, const char *relpath,
8414 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8415 struct got_object_id *commit_id, int dirfd, const char *de_name)
8417 const struct got_error *err = NULL;
8418 struct unstage_path_arg *a = arg;
8419 struct got_fileindex_entry *ie;
8420 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8421 char *ondisk_path = NULL;
8422 char *id_str = NULL, *label_orig = NULL;
8423 int local_changes_subsumed;
8424 struct stat sb;
8426 if (staged_status != GOT_STATUS_ADD &&
8427 staged_status != GOT_STATUS_MODIFY &&
8428 staged_status != GOT_STATUS_DELETE)
8429 return NULL;
8431 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8432 if (ie == NULL)
8433 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8435 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8436 == -1)
8437 return got_error_from_errno("asprintf");
8439 err = got_object_id_str(&id_str,
8440 commit_id ? commit_id : a->worktree->base_commit_id);
8441 if (err)
8442 goto done;
8443 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8444 id_str) == -1) {
8445 err = got_error_from_errno("asprintf");
8446 goto done;
8449 switch (staged_status) {
8450 case GOT_STATUS_MODIFY:
8451 err = got_object_open_as_blob(&blob_base, a->repo,
8452 blob_id, 8192);
8453 if (err)
8454 break;
8455 /* fall through */
8456 case GOT_STATUS_ADD:
8457 if (a->patch_cb) {
8458 if (staged_status == GOT_STATUS_ADD) {
8459 int choice = GOT_PATCH_CHOICE_NONE;
8460 err = (*a->patch_cb)(&choice, a->patch_arg,
8461 staged_status, ie->path, NULL, 1, 1);
8462 if (err)
8463 break;
8464 if (choice != GOT_PATCH_CHOICE_YES)
8465 break;
8466 } else {
8467 err = unstage_hunks(staged_blob_id,
8468 blob_base, blob_id, ie, ondisk_path,
8469 label_orig, a->worktree, a->repo,
8470 a->patch_cb, a->patch_arg,
8471 a->progress_cb, a->progress_arg);
8472 break; /* Done with this file. */
8475 err = got_object_open_as_blob(&blob_staged, a->repo,
8476 staged_blob_id, 8192);
8477 if (err)
8478 break;
8479 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8480 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8481 case GOT_FILEIDX_MODE_REGULAR_FILE:
8482 err = merge_blob(&local_changes_subsumed, a->worktree,
8483 blob_base, ondisk_path, relpath,
8484 got_fileindex_perms_to_st(ie), label_orig,
8485 blob_staged, commit_id ? commit_id :
8486 a->worktree->base_commit_id, a->repo,
8487 a->progress_cb, a->progress_arg);
8488 break;
8489 case GOT_FILEIDX_MODE_SYMLINK:
8490 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8491 char *staged_target;
8492 err = got_object_blob_read_to_str(
8493 &staged_target, blob_staged);
8494 if (err)
8495 goto done;
8496 err = merge_symlink(a->worktree, blob_base,
8497 ondisk_path, relpath, label_orig,
8498 staged_target, commit_id ? commit_id :
8499 a->worktree->base_commit_id,
8500 a->repo, a->progress_cb, a->progress_arg);
8501 free(staged_target);
8502 } else {
8503 err = merge_blob(&local_changes_subsumed,
8504 a->worktree, blob_base, ondisk_path,
8505 relpath, got_fileindex_perms_to_st(ie),
8506 label_orig, blob_staged,
8507 commit_id ? commit_id :
8508 a->worktree->base_commit_id, a->repo,
8509 a->progress_cb, a->progress_arg);
8511 break;
8512 default:
8513 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8514 break;
8516 if (err == NULL) {
8517 got_fileindex_entry_stage_set(ie,
8518 GOT_FILEIDX_STAGE_NONE);
8519 got_fileindex_entry_staged_filetype_set(ie, 0);
8521 break;
8522 case GOT_STATUS_DELETE:
8523 if (a->patch_cb) {
8524 int choice = GOT_PATCH_CHOICE_NONE;
8525 err = (*a->patch_cb)(&choice, a->patch_arg,
8526 staged_status, ie->path, NULL, 1, 1);
8527 if (err)
8528 break;
8529 if (choice == GOT_PATCH_CHOICE_NO)
8530 break;
8531 if (choice != GOT_PATCH_CHOICE_YES) {
8532 err = got_error(GOT_ERR_PATCH_CHOICE);
8533 break;
8536 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8537 got_fileindex_entry_staged_filetype_set(ie, 0);
8538 err = get_file_status(&status, &sb, ie, ondisk_path,
8539 dirfd, de_name, a->repo);
8540 if (err)
8541 break;
8542 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8543 break;
8545 done:
8546 free(ondisk_path);
8547 if (blob_base)
8548 got_object_blob_close(blob_base);
8549 if (blob_staged)
8550 got_object_blob_close(blob_staged);
8551 free(id_str);
8552 free(label_orig);
8553 return err;
8556 const struct got_error *
8557 got_worktree_unstage(struct got_worktree *worktree,
8558 struct got_pathlist_head *paths,
8559 got_worktree_checkout_cb progress_cb, void *progress_arg,
8560 got_worktree_patch_cb patch_cb, void *patch_arg,
8561 struct got_repository *repo)
8563 const struct got_error *err = NULL, *sync_err, *unlockerr;
8564 struct got_pathlist_entry *pe;
8565 struct got_fileindex *fileindex = NULL;
8566 char *fileindex_path = NULL;
8567 struct unstage_path_arg upa;
8569 err = lock_worktree(worktree, LOCK_EX);
8570 if (err)
8571 return err;
8573 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8574 if (err)
8575 goto done;
8577 upa.worktree = worktree;
8578 upa.fileindex = fileindex;
8579 upa.repo = repo;
8580 upa.progress_cb = progress_cb;
8581 upa.progress_arg = progress_arg;
8582 upa.patch_cb = patch_cb;
8583 upa.patch_arg = patch_arg;
8584 TAILQ_FOREACH(pe, paths, entry) {
8585 err = worktree_status(worktree, pe->path, fileindex, repo,
8586 unstage_path, &upa, NULL, NULL, 1, 0);
8587 if (err)
8588 goto done;
8591 sync_err = sync_fileindex(fileindex, fileindex_path);
8592 if (sync_err && err == NULL)
8593 err = sync_err;
8594 done:
8595 free(fileindex_path);
8596 if (fileindex)
8597 got_fileindex_free(fileindex);
8598 unlockerr = lock_worktree(worktree, LOCK_SH);
8599 if (unlockerr && err == NULL)
8600 err = unlockerr;
8601 return err;
8604 struct report_file_info_arg {
8605 struct got_worktree *worktree;
8606 got_worktree_path_info_cb info_cb;
8607 void *info_arg;
8608 struct got_pathlist_head *paths;
8609 got_cancel_cb cancel_cb;
8610 void *cancel_arg;
8613 static const struct got_error *
8614 report_file_info(void *arg, struct got_fileindex_entry *ie)
8616 struct report_file_info_arg *a = arg;
8617 struct got_pathlist_entry *pe;
8618 struct got_object_id blob_id, staged_blob_id, commit_id;
8619 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8620 struct got_object_id *commit_idp = NULL;
8621 int stage;
8623 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8624 return got_error(GOT_ERR_CANCELLED);
8626 TAILQ_FOREACH(pe, a->paths, entry) {
8627 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8628 got_path_is_child(ie->path, pe->path, pe->path_len))
8629 break;
8631 if (pe == NULL) /* not found */
8632 return NULL;
8634 if (got_fileindex_entry_has_blob(ie)) {
8635 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8636 blob_idp = &blob_id;
8638 stage = got_fileindex_entry_stage_get(ie);
8639 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8640 stage == GOT_FILEIDX_STAGE_ADD) {
8641 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8642 SHA1_DIGEST_LENGTH);
8643 staged_blob_idp = &staged_blob_id;
8646 if (got_fileindex_entry_has_commit(ie)) {
8647 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8648 commit_idp = &commit_id;
8651 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8652 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8655 const struct got_error *
8656 got_worktree_path_info(struct got_worktree *worktree,
8657 struct got_pathlist_head *paths,
8658 got_worktree_path_info_cb info_cb, void *info_arg,
8659 got_cancel_cb cancel_cb, void *cancel_arg)
8662 const struct got_error *err = NULL, *unlockerr;
8663 struct got_fileindex *fileindex = NULL;
8664 char *fileindex_path = NULL;
8665 struct report_file_info_arg arg;
8667 err = lock_worktree(worktree, LOCK_SH);
8668 if (err)
8669 return err;
8671 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8672 if (err)
8673 goto done;
8675 arg.worktree = worktree;
8676 arg.info_cb = info_cb;
8677 arg.info_arg = info_arg;
8678 arg.paths = paths;
8679 arg.cancel_cb = cancel_cb;
8680 arg.cancel_arg = cancel_arg;
8681 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8682 &arg);
8683 done:
8684 free(fileindex_path);
8685 if (fileindex)
8686 got_fileindex_free(fileindex);
8687 unlockerr = lock_worktree(worktree, LOCK_UN);
8688 if (unlockerr && err == NULL)
8689 err = unlockerr;
8690 return err;