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, fd1 = -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 fd1 = got_opentempfd();
1710 if (fd1 == -1) {
1711 err = got_error_from_errno("got_opentempfd");
1712 goto done;
1714 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1715 if (err)
1716 goto done;
1718 if (S_ISLNK(sb->st_mode)) {
1719 err = get_symlink_modification_status(status, ie,
1720 abspath, dirfd, de_name, blob);
1721 goto done;
1724 if (dirfd != -1) {
1725 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1726 if (fd == -1) {
1727 err = got_error_from_errno2("openat", abspath);
1728 goto done;
1732 f = fdopen(fd, "r");
1733 if (f == NULL) {
1734 err = got_error_from_errno2("fdopen", abspath);
1735 goto done;
1737 fd = -1;
1738 hdrlen = got_object_blob_get_hdrlen(blob);
1739 for (;;) {
1740 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1741 err = got_object_blob_read_block(&blen, blob);
1742 if (err)
1743 goto done;
1744 /* Skip length of blob object header first time around. */
1745 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1746 if (flen == 0 && ferror(f)) {
1747 err = got_error_from_errno("fread");
1748 goto done;
1750 if (blen - hdrlen == 0) {
1751 if (flen != 0)
1752 *status = GOT_STATUS_MODIFY;
1753 break;
1754 } else if (flen == 0) {
1755 if (blen - hdrlen != 0)
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1758 } else if (blen - hdrlen == flen) {
1759 /* Skip blob object header first time around. */
1760 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1761 *status = GOT_STATUS_MODIFY;
1762 break;
1764 } else {
1765 *status = GOT_STATUS_MODIFY;
1766 break;
1768 hdrlen = 0;
1771 if (*status == GOT_STATUS_MODIFY) {
1772 rewind(f);
1773 err = get_modified_file_content_status(status, f);
1774 } else if (xbit_differs(ie, sb->st_mode))
1775 *status = GOT_STATUS_MODE_CHANGE;
1776 done:
1777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1778 err = got_error_from_errno("close");
1779 if (blob)
1780 got_object_blob_close(blob);
1781 if (f != NULL && fclose(f) == EOF && err == NULL)
1782 err = got_error_from_errno2("fclose", abspath);
1783 if (fd != -1 && close(fd) == -1 && err == NULL)
1784 err = got_error_from_errno2("close", abspath);
1785 return err;
1789 * Update timestamps in the file index if a file is unmodified and
1790 * we had to run a full content comparison to find out.
1792 static const struct got_error *
1793 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1794 struct got_fileindex_entry *ie, struct stat *sb)
1796 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1797 return got_fileindex_entry_update(ie, wt_fd, path,
1798 ie->blob_sha1, ie->commit_sha1, 1);
1800 return NULL;
1803 static const struct got_error *
1804 update_blob(struct got_worktree *worktree,
1805 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1806 struct got_tree_entry *te, const char *path,
1807 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1808 void *progress_arg)
1810 const struct got_error *err = NULL;
1811 struct got_blob_object *blob = NULL;
1812 char *ondisk_path = NULL;
1813 unsigned char status = GOT_STATUS_NO_CHANGE;
1814 struct stat sb;
1815 int fd1 = -1, fd2 = -1;
1817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1818 return got_error_from_errno("asprintf");
1820 if (ie) {
1821 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1822 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1823 goto done;
1825 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1826 repo);
1827 if (err)
1828 goto done;
1829 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1830 sb.st_mode = got_fileindex_perms_to_st(ie);
1831 } else {
1832 if (stat(ondisk_path, &sb) == -1) {
1833 if (errno != ENOENT) {
1834 err = got_error_from_errno2("stat",
1835 ondisk_path);
1836 goto done;
1838 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1839 status = GOT_STATUS_UNVERSIONED;
1840 } else {
1841 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1842 status = GOT_STATUS_UNVERSIONED;
1843 else
1844 status = GOT_STATUS_OBSTRUCTED;
1848 if (status == GOT_STATUS_OBSTRUCTED) {
1849 if (ie)
1850 got_fileindex_entry_mark_skipped(ie);
1851 err = (*progress_cb)(progress_arg, status, path);
1852 goto done;
1854 if (status == GOT_STATUS_CONFLICT) {
1855 if (ie)
1856 got_fileindex_entry_mark_skipped(ie);
1857 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1858 path);
1859 goto done;
1862 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1863 (S_ISLNK(te->mode) ||
1864 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1866 * This is a regular file or an installed bad symlink.
1867 * If the file index indicates that this file is already
1868 * up-to-date with respect to the repository we can skip
1869 * updating contents of this file.
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1874 /* Same commit. */
1875 err = sync_timestamps(worktree->root_fd,
1876 path, status, ie, &sb);
1877 if (err)
1878 goto done;
1879 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1880 path);
1881 goto done;
1883 if (got_fileindex_entry_has_blob(ie) &&
1884 memcmp(ie->blob_sha1, te->id.sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1886 /* Different commit but the same blob. */
1887 err = sync_timestamps(worktree->root_fd,
1888 path, status, ie, &sb);
1889 if (err)
1890 goto done;
1891 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1892 path);
1893 goto done;
1897 fd1 = got_opentempfd();
1898 if (fd1 == -1) {
1899 err = got_error_from_errno("got_opentempfd");
1900 goto done;
1902 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1903 if (err)
1904 goto done;
1906 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1907 int update_timestamps;
1908 struct got_blob_object *blob2 = NULL;
1909 char *label_orig = NULL;
1910 if (got_fileindex_entry_has_blob(ie)) {
1911 fd2 = got_opentempfd();
1912 if (fd2 == -1) {
1913 err = got_error_from_errno("got_opentempfd");
1914 goto done;
1916 struct got_object_id id2;
1917 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1918 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1919 fd2);
1920 if (err)
1921 goto done;
1923 if (got_fileindex_entry_has_commit(ie)) {
1924 char id_str[SHA1_DIGEST_STRING_LENGTH];
1925 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1926 sizeof(id_str)) == NULL) {
1927 err = got_error_path(id_str,
1928 GOT_ERR_BAD_OBJ_ID_STR);
1929 goto done;
1931 if (asprintf(&label_orig, "%s: commit %s",
1932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1933 err = got_error_from_errno("asprintf");
1934 goto done;
1937 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1938 char *link_target;
1939 err = got_object_blob_read_to_str(&link_target, blob);
1940 if (err)
1941 goto done;
1942 err = merge_symlink(worktree, blob2, ondisk_path, path,
1943 label_orig, link_target, worktree->base_commit_id,
1944 repo, progress_cb, progress_arg);
1945 free(link_target);
1946 } else {
1947 err = merge_blob(&update_timestamps, worktree, blob2,
1948 ondisk_path, path, sb.st_mode, label_orig, blob,
1949 worktree->base_commit_id, repo,
1950 progress_cb, progress_arg);
1952 free(label_orig);
1953 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1954 err = got_error_from_errno("close");
1955 goto done;
1957 if (blob2)
1958 got_object_blob_close(blob2);
1959 if (err)
1960 goto done;
1962 * Do not update timestamps of files with local changes.
1963 * Otherwise, a future status walk would treat them as
1964 * unmodified files again.
1966 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1967 blob->id.sha1, worktree->base_commit_id->sha1,
1968 update_timestamps);
1969 } else if (status == GOT_STATUS_MODE_CHANGE) {
1970 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1971 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1972 } else if (status == GOT_STATUS_DELETE) {
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1974 if (err)
1975 goto done;
1976 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1977 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1978 if (err)
1979 goto done;
1980 } else {
1981 int is_bad_symlink = 0;
1982 if (S_ISLNK(te->mode)) {
1983 err = install_symlink(&is_bad_symlink, worktree,
1984 ondisk_path, path, blob,
1985 status == GOT_STATUS_MISSING, 0,
1986 status == GOT_STATUS_UNVERSIONED, 0,
1987 repo, progress_cb, progress_arg);
1988 } else {
1989 err = install_blob(worktree, ondisk_path, path,
1990 te->mode, sb.st_mode, blob,
1991 status == GOT_STATUS_MISSING, 0, 0,
1992 status == GOT_STATUS_UNVERSIONED, repo,
1993 progress_cb, progress_arg);
1995 if (err)
1996 goto done;
1998 if (ie) {
1999 err = got_fileindex_entry_update(ie,
2000 worktree->root_fd, path, blob->id.sha1,
2001 worktree->base_commit_id->sha1, 1);
2002 } else {
2003 err = create_fileindex_entry(&ie, fileindex,
2004 worktree->base_commit_id, worktree->root_fd, path,
2005 &blob->id);
2007 if (err)
2008 goto done;
2010 if (is_bad_symlink) {
2011 got_fileindex_entry_filetype_set(ie,
2012 GOT_FILEIDX_MODE_BAD_SYMLINK);
2016 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2017 err = got_error_from_errno("close");
2018 goto done;
2020 got_object_blob_close(blob);
2021 done:
2022 free(ondisk_path);
2023 return err;
2026 static const struct got_error *
2027 remove_ondisk_file(const char *root_path, const char *path)
2029 const struct got_error *err = NULL;
2030 char *ondisk_path = NULL, *parent = NULL;
2032 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2033 return got_error_from_errno("asprintf");
2035 if (unlink(ondisk_path) == -1) {
2036 if (errno != ENOENT)
2037 err = got_error_from_errno2("unlink", ondisk_path);
2038 } else {
2039 size_t root_len = strlen(root_path);
2040 err = got_path_dirname(&parent, ondisk_path);
2041 if (err)
2042 goto done;
2043 while (got_path_cmp(parent, root_path,
2044 strlen(parent), root_len) != 0) {
2045 free(ondisk_path);
2046 ondisk_path = parent;
2047 parent = NULL;
2048 if (rmdir(ondisk_path) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2051 ondisk_path);
2052 break;
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 break;
2059 done:
2060 free(ondisk_path);
2061 free(parent);
2062 return err;
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2072 struct stat sb;
2073 char *ondisk_path;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2079 == -1)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 if (err)
2084 goto done;
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2092 goto done;
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2098 if (err)
2099 goto done;
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 ie->path);
2102 goto done;
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 if (err)
2109 goto done;
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, worktree->root_fd,
2115 ie->path, NULL, NULL, 0);
2116 } else {
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2118 if (err)
2119 goto done;
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2122 if (err)
2123 goto done;
2125 got_fileindex_entry_remove(fileindex, ie);
2127 done:
2128 free(ondisk_path);
2129 return err;
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2137 void *progress_arg;
2138 got_cancel_cb cancel_cb;
2139 void *cancel_arg;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2172 char *path;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2178 return NULL;
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2182 == -1)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2187 else
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2191 free(path);
2192 return err;
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2202 *uuidstr = NULL;
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2206 return NULL;
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2215 *refname = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2218 if (err)
2219 return err;
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 *refname = NULL;
2225 free(uuidstr);
2226 return err;
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2296 *path = NULL;
2297 return got_error_from_errno("asprintf");
2299 return NULL;
2302 static const struct got_error *
2303 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2309 static const struct got_error *
2310 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2317 * Prevent Git's garbage collector from deleting our base commit by
2318 * setting a reference to our base commit's ID.
2320 static const struct got_error *
2321 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2323 const struct got_error *err = NULL;
2324 struct got_reference *ref = NULL;
2325 char *refname;
2327 err = got_worktree_get_base_ref_name(&refname, worktree);
2328 if (err)
2329 return err;
2331 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2332 if (err)
2333 goto done;
2335 err = got_ref_write(ref, repo);
2336 done:
2337 free(refname);
2338 if (ref)
2339 got_ref_close(ref);
2340 return err;
2343 static const struct got_error *
2344 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2346 const struct got_error *err = NULL;
2348 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2349 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2350 err = got_error_from_errno("asprintf");
2351 *fileindex_path = NULL;
2353 return err;
2357 static const struct got_error *
2358 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2359 struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2362 FILE *index = NULL;
2364 *fileindex_path = NULL;
2365 *fileindex = got_fileindex_alloc();
2366 if (*fileindex == NULL)
2367 return got_error_from_errno("got_fileindex_alloc");
2369 err = get_fileindex_path(fileindex_path, worktree);
2370 if (err)
2371 goto done;
2373 index = fopen(*fileindex_path, "rbe");
2374 if (index == NULL) {
2375 if (errno != ENOENT)
2376 err = got_error_from_errno2("fopen", *fileindex_path);
2377 } else {
2378 err = got_fileindex_read(*fileindex, index);
2379 if (fclose(index) == EOF && err == NULL)
2380 err = got_error_from_errno("fclose");
2382 done:
2383 if (err) {
2384 free(*fileindex_path);
2385 *fileindex_path = NULL;
2386 got_fileindex_free(*fileindex);
2387 *fileindex = NULL;
2389 return err;
2392 struct bump_base_commit_id_arg {
2393 struct got_object_id *base_commit_id;
2394 const char *path;
2395 size_t path_len;
2396 const char *entry_name;
2397 got_worktree_checkout_cb progress_cb;
2398 void *progress_arg;
2401 /* Bump base commit ID of all files within an updated part of the work tree. */
2402 static const struct got_error *
2403 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2405 const struct got_error *err;
2406 struct bump_base_commit_id_arg *a = arg;
2408 if (a->entry_name) {
2409 if (strcmp(ie->path, a->path) != 0)
2410 return NULL;
2411 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2412 return NULL;
2414 if (got_fileindex_entry_was_skipped(ie))
2415 return NULL;
2417 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2418 SHA1_DIGEST_LENGTH) == 0)
2419 return NULL;
2421 if (a->progress_cb) {
2422 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2423 ie->path);
2424 if (err)
2425 return err;
2427 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2428 return NULL;
2431 static const struct got_error *
2432 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2433 struct got_fileindex *fileindex,
2434 got_worktree_checkout_cb progress_cb, void *progress_arg)
2436 struct bump_base_commit_id_arg bbc_arg;
2438 bbc_arg.base_commit_id = worktree->base_commit_id;
2439 bbc_arg.entry_name = NULL;
2440 bbc_arg.path = "";
2441 bbc_arg.path_len = 0;
2442 bbc_arg.progress_cb = progress_cb;
2443 bbc_arg.progress_arg = progress_arg;
2445 return got_fileindex_for_each_entry_safe(fileindex,
2446 bump_base_commit_id, &bbc_arg);
2449 static const struct got_error *
2450 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2452 const struct got_error *err = NULL;
2453 char *new_fileindex_path = NULL;
2454 FILE *new_index = NULL;
2455 struct timespec timeout;
2457 err = got_opentemp_named(&new_fileindex_path, &new_index,
2458 fileindex_path);
2459 if (err)
2460 goto done;
2462 err = got_fileindex_write(fileindex, new_index);
2463 if (err)
2464 goto done;
2466 if (rename(new_fileindex_path, fileindex_path) != 0) {
2467 err = got_error_from_errno3("rename", new_fileindex_path,
2468 fileindex_path);
2469 unlink(new_fileindex_path);
2473 * Sleep for a short amount of time to ensure that files modified after
2474 * this program exits have a different time stamp from the one which
2475 * was recorded in the file index.
2477 timeout.tv_sec = 0;
2478 timeout.tv_nsec = 1;
2479 nanosleep(&timeout, NULL);
2480 done:
2481 if (new_index)
2482 fclose(new_index);
2483 free(new_fileindex_path);
2484 return err;
2487 static const struct got_error *
2488 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2489 struct got_object_id **tree_id, const char *wt_relpath,
2490 struct got_commit_object *base_commit, struct got_worktree *worktree,
2491 struct got_repository *repo)
2493 const struct got_error *err = NULL;
2494 struct got_object_id *id = NULL;
2495 char *in_repo_path = NULL;
2496 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2498 *entry_type = GOT_OBJ_TYPE_ANY;
2499 *tree_relpath = NULL;
2500 *tree_id = NULL;
2502 if (wt_relpath[0] == '\0') {
2503 /* Check out all files within the work tree. */
2504 *entry_type = GOT_OBJ_TYPE_TREE;
2505 *tree_relpath = strdup("");
2506 if (*tree_relpath == NULL) {
2507 err = got_error_from_errno("strdup");
2508 goto done;
2510 err = got_object_id_by_path(tree_id, repo, base_commit,
2511 worktree->path_prefix);
2512 if (err)
2513 goto done;
2514 return NULL;
2517 /* Check out a subset of files in the work tree. */
2519 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2520 is_root_wt ? "" : "/", wt_relpath) == -1) {
2521 err = got_error_from_errno("asprintf");
2522 goto done;
2525 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2526 if (err)
2527 goto done;
2529 free(in_repo_path);
2530 in_repo_path = NULL;
2532 err = got_object_get_type(entry_type, repo, id);
2533 if (err)
2534 goto done;
2536 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2537 /* Check out a single file. */
2538 if (strchr(wt_relpath, '/') == NULL) {
2539 /* Check out a single file in work tree's root dir. */
2540 in_repo_path = strdup(worktree->path_prefix);
2541 if (in_repo_path == NULL) {
2542 err = got_error_from_errno("strdup");
2543 goto done;
2545 *tree_relpath = strdup("");
2546 if (*tree_relpath == NULL) {
2547 err = got_error_from_errno("strdup");
2548 goto done;
2550 } else {
2551 /* Check out a single file in a subdirectory. */
2552 err = got_path_dirname(tree_relpath, wt_relpath);
2553 if (err)
2554 return err;
2555 if (asprintf(&in_repo_path, "%s%s%s",
2556 worktree->path_prefix, is_root_wt ? "" : "/",
2557 *tree_relpath) == -1) {
2558 err = got_error_from_errno("asprintf");
2559 goto done;
2562 err = got_object_id_by_path(tree_id, repo,
2563 base_commit, in_repo_path);
2564 } else {
2565 /* Check out all files within a subdirectory. */
2566 *tree_id = got_object_id_dup(id);
2567 if (*tree_id == NULL) {
2568 err = got_error_from_errno("got_object_id_dup");
2569 goto done;
2571 *tree_relpath = strdup(wt_relpath);
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2574 goto done;
2577 done:
2578 free(id);
2579 free(in_repo_path);
2580 if (err) {
2581 *entry_type = GOT_OBJ_TYPE_ANY;
2582 free(*tree_relpath);
2583 *tree_relpath = NULL;
2584 free(*tree_id);
2585 *tree_id = NULL;
2587 return err;
2590 static const struct got_error *
2591 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2593 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2594 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2596 const struct got_error *err = NULL;
2597 struct got_commit_object *commit = NULL;
2598 struct got_tree_object *tree = NULL;
2599 struct got_fileindex_diff_tree_cb diff_cb;
2600 struct diff_cb_arg arg;
2602 err = ref_base_commit(worktree, repo);
2603 if (err) {
2604 if (!(err->code == GOT_ERR_ERRNO &&
2605 (errno == EACCES || errno == EROFS)))
2606 goto done;
2607 err = (*progress_cb)(progress_arg,
2608 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2609 if (err)
2610 return err;
2613 err = got_object_open_as_commit(&commit, repo,
2614 worktree->base_commit_id);
2615 if (err)
2616 goto done;
2618 err = got_object_open_as_tree(&tree, repo, tree_id);
2619 if (err)
2620 goto done;
2622 if (entry_name &&
2623 got_object_tree_find_entry(tree, entry_name) == NULL) {
2624 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2625 goto done;
2628 diff_cb.diff_old_new = diff_old_new;
2629 diff_cb.diff_old = diff_old;
2630 diff_cb.diff_new = diff_new;
2631 arg.fileindex = fileindex;
2632 arg.worktree = worktree;
2633 arg.repo = repo;
2634 arg.progress_cb = progress_cb;
2635 arg.progress_arg = progress_arg;
2636 arg.cancel_cb = cancel_cb;
2637 arg.cancel_arg = cancel_arg;
2638 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2639 entry_name, repo, &diff_cb, &arg);
2640 done:
2641 if (tree)
2642 got_object_tree_close(tree);
2643 if (commit)
2644 got_object_commit_close(commit);
2645 return err;
2648 const struct got_error *
2649 got_worktree_checkout_files(struct got_worktree *worktree,
2650 struct got_pathlist_head *paths, struct got_repository *repo,
2651 got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 got_cancel_cb cancel_cb, void *cancel_arg)
2654 const struct got_error *err = NULL, *sync_err, *unlockerr;
2655 struct got_commit_object *commit = NULL;
2656 struct got_tree_object *tree = NULL;
2657 struct got_fileindex *fileindex = NULL;
2658 char *fileindex_path = NULL;
2659 struct got_pathlist_entry *pe;
2660 struct tree_path_data {
2661 STAILQ_ENTRY(tree_path_data) entry;
2662 struct got_object_id *tree_id;
2663 int entry_type;
2664 char *relpath;
2665 char *entry_name;
2666 } *tpd = NULL;
2667 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2669 STAILQ_INIT(&tree_paths);
2671 err = lock_worktree(worktree, LOCK_EX);
2672 if (err)
2673 return err;
2675 err = got_object_open_as_commit(&commit, repo,
2676 worktree->base_commit_id);
2677 if (err)
2678 goto done;
2680 /* Map all specified paths to in-repository trees. */
2681 TAILQ_FOREACH(pe, paths, entry) {
2682 tpd = malloc(sizeof(*tpd));
2683 if (tpd == NULL) {
2684 err = got_error_from_errno("malloc");
2685 goto done;
2688 err = find_tree_entry_for_checkout(&tpd->entry_type,
2689 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2690 worktree, repo);
2691 if (err) {
2692 free(tpd);
2693 goto done;
2696 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2697 err = got_path_basename(&tpd->entry_name, pe->path);
2698 if (err) {
2699 free(tpd->relpath);
2700 free(tpd->tree_id);
2701 free(tpd);
2702 goto done;
2704 } else
2705 tpd->entry_name = NULL;
2707 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2711 * Read the file index.
2712 * Checking out files is supposed to be an idempotent operation.
2713 * If the on-disk file index is incomplete we will try to complete it.
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 if (err)
2717 goto done;
2719 tpd = STAILQ_FIRST(&tree_paths);
2720 TAILQ_FOREACH(pe, paths, entry) {
2721 struct bump_base_commit_id_arg bbc_arg;
2723 err = checkout_files(worktree, fileindex, tpd->relpath,
2724 tpd->tree_id, tpd->entry_name, repo,
2725 progress_cb, progress_arg, cancel_cb, cancel_arg);
2726 if (err)
2727 break;
2729 bbc_arg.base_commit_id = worktree->base_commit_id;
2730 bbc_arg.entry_name = tpd->entry_name;
2731 bbc_arg.path = pe->path;
2732 bbc_arg.path_len = pe->path_len;
2733 bbc_arg.progress_cb = progress_cb;
2734 bbc_arg.progress_arg = progress_arg;
2735 err = got_fileindex_for_each_entry_safe(fileindex,
2736 bump_base_commit_id, &bbc_arg);
2737 if (err)
2738 break;
2740 tpd = STAILQ_NEXT(tpd, entry);
2742 sync_err = sync_fileindex(fileindex, fileindex_path);
2743 if (sync_err && err == NULL)
2744 err = sync_err;
2745 done:
2746 free(fileindex_path);
2747 if (tree)
2748 got_object_tree_close(tree);
2749 if (commit)
2750 got_object_commit_close(commit);
2751 if (fileindex)
2752 got_fileindex_free(fileindex);
2753 while (!STAILQ_EMPTY(&tree_paths)) {
2754 tpd = STAILQ_FIRST(&tree_paths);
2755 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2756 free(tpd->relpath);
2757 free(tpd->tree_id);
2758 free(tpd);
2760 unlockerr = lock_worktree(worktree, LOCK_SH);
2761 if (unlockerr && err == NULL)
2762 err = unlockerr;
2763 return err;
2766 struct merge_file_cb_arg {
2767 struct got_worktree *worktree;
2768 struct got_fileindex *fileindex;
2769 got_worktree_checkout_cb progress_cb;
2770 void *progress_arg;
2771 got_cancel_cb cancel_cb;
2772 void *cancel_arg;
2773 const char *label_orig;
2774 struct got_object_id *commit_id2;
2775 int allow_bad_symlinks;
2778 static const struct got_error *
2779 merge_file_cb(void *arg, struct got_blob_object *blob1,
2780 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2781 struct got_object_id *id1, struct got_object_id *id2,
2782 const char *path1, const char *path2,
2783 mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 static const struct got_error *err = NULL;
2786 struct merge_file_cb_arg *a = arg;
2787 struct got_fileindex_entry *ie;
2788 char *ondisk_path = NULL;
2789 struct stat sb;
2790 unsigned char status;
2791 int local_changes_subsumed;
2792 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2793 char *id_str = NULL, *label_deriv2 = NULL;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2797 strlen(path2));
2798 if (ie == NULL)
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 path2) == -1)
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 repo);
2808 if (err)
2809 goto done;
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2814 goto done;
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 goto done;
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 char *link_target2;
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2827 if (err)
2828 goto done;
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2832 free(link_target2);
2833 } else {
2834 int fd;
2836 f_orig = got_opentemp();
2837 if (f_orig == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2839 goto done;
2841 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2842 f_orig, blob1);
2843 if (err)
2844 goto done;
2846 f_deriv2 = got_opentemp();
2847 if (f_deriv2 == NULL)
2848 goto done;
2849 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2850 f_deriv2, blob2);
2851 if (err)
2852 goto done;
2854 fd = open(ondisk_path,
2855 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2856 if (fd == -1) {
2857 err = got_error_from_errno2("open",
2858 ondisk_path);
2859 goto done;
2861 f_deriv = fdopen(fd, "r");
2862 if (f_deriv == NULL) {
2863 err = got_error_from_errno2("fdopen",
2864 ondisk_path);
2865 close(fd);
2866 goto done;
2868 err = got_object_id_str(&id_str, a->commit_id2);
2869 if (err)
2870 goto done;
2871 if (asprintf(&label_deriv2, "%s: commit %s",
2872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2873 err = got_error_from_errno("asprintf");
2874 goto done;
2876 err = merge_file(&local_changes_subsumed, a->worktree,
2877 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2878 sb.st_mode, a->label_orig, NULL, label_deriv2,
2879 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2880 a->progress_cb, a->progress_arg);
2882 } else if (blob1) {
2883 ie = got_fileindex_entry_get(a->fileindex, path1,
2884 strlen(path1));
2885 if (ie == NULL)
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path1);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 path1) == -1)
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 repo);
2895 if (err)
2896 goto done;
2898 switch (status) {
2899 case GOT_STATUS_NO_CHANGE:
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2902 if (err)
2903 goto done;
2904 err = remove_ondisk_file(a->worktree->root_path, path1);
2905 if (err)
2906 goto done;
2907 if (ie)
2908 got_fileindex_entry_mark_deleted_from_disk(ie);
2909 break;
2910 case GOT_STATUS_DELETE:
2911 case GOT_STATUS_MISSING:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2914 if (err)
2915 goto done;
2916 if (ie)
2917 got_fileindex_entry_mark_deleted_from_disk(ie);
2918 break;
2919 case GOT_STATUS_ADD: {
2920 struct got_object_id *id;
2921 FILE *blob1_f;
2922 off_t blob1_size;
2924 * Delete the added file only if its content already
2925 * exists in the repository.
2927 err = got_object_blob_file_create(&id, &blob1_f,
2928 &blob1_size, path1);
2929 if (err)
2930 goto done;
2931 if (got_object_id_cmp(id, id1) == 0) {
2932 err = (*a->progress_cb)(a->progress_arg,
2933 GOT_STATUS_DELETE, path1);
2934 if (err)
2935 goto done;
2936 err = remove_ondisk_file(a->worktree->root_path,
2937 path1);
2938 if (err)
2939 goto done;
2940 if (ie)
2941 got_fileindex_entry_remove(a->fileindex,
2942 ie);
2943 } else {
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_CANNOT_DELETE, path1);
2947 if (fclose(blob1_f) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2949 free(id);
2950 if (err)
2951 goto done;
2952 break;
2954 case GOT_STATUS_MODIFY:
2955 case GOT_STATUS_CONFLICT:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2958 if (err)
2959 goto done;
2960 break;
2961 case GOT_STATUS_OBSTRUCTED:
2962 err = (*a->progress_cb)(a->progress_arg, status, path1);
2963 if (err)
2964 goto done;
2965 break;
2966 default:
2967 break;
2969 } else if (blob2) {
2970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2971 path2) == -1)
2972 return got_error_from_errno("asprintf");
2973 ie = got_fileindex_entry_get(a->fileindex, path2,
2974 strlen(path2));
2975 if (ie) {
2976 err = get_file_status(&status, &sb, ie, ondisk_path,
2977 -1, NULL, repo);
2978 if (err)
2979 goto done;
2980 if (status != GOT_STATUS_NO_CHANGE &&
2981 status != GOT_STATUS_MODIFY &&
2982 status != GOT_STATUS_CONFLICT &&
2983 status != GOT_STATUS_ADD) {
2984 err = (*a->progress_cb)(a->progress_arg,
2985 status, path2);
2986 goto done;
2988 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2989 char *link_target2;
2990 err = got_object_blob_read_to_str(&link_target2,
2991 blob2);
2992 if (err)
2993 goto done;
2994 err = merge_symlink(a->worktree, NULL,
2995 ondisk_path, path2, a->label_orig,
2996 link_target2, a->commit_id2, repo,
2997 a->progress_cb, a->progress_arg);
2998 free(link_target2);
2999 } else if (S_ISREG(sb.st_mode)) {
3000 err = merge_blob(&local_changes_subsumed,
3001 a->worktree, NULL, ondisk_path, path2,
3002 sb.st_mode, a->label_orig, blob2,
3003 a->commit_id2, repo, a->progress_cb,
3004 a->progress_arg);
3005 } else {
3006 err = got_error_path(ondisk_path,
3007 GOT_ERR_FILE_OBSTRUCTED);
3009 if (err)
3010 goto done;
3011 if (status == GOT_STATUS_DELETE) {
3012 err = got_fileindex_entry_update(ie,
3013 a->worktree->root_fd, path2, blob2->id.sha1,
3014 a->worktree->base_commit_id->sha1, 0);
3015 if (err)
3016 goto done;
3018 } else {
3019 int is_bad_symlink = 0;
3020 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3021 if (S_ISLNK(mode2)) {
3022 err = install_symlink(&is_bad_symlink,
3023 a->worktree, ondisk_path, path2, blob2, 0,
3024 0, 1, a->allow_bad_symlinks, repo,
3025 a->progress_cb, a->progress_arg);
3026 } else {
3027 err = install_blob(a->worktree, ondisk_path, path2,
3028 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3029 a->progress_cb, a->progress_arg);
3031 if (err)
3032 goto done;
3033 err = got_fileindex_entry_alloc(&ie, path2);
3034 if (err)
3035 goto done;
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, NULL, NULL, 1);
3038 if (err) {
3039 got_fileindex_entry_free(ie);
3040 goto done;
3042 err = got_fileindex_entry_add(a->fileindex, ie);
3043 if (err) {
3044 got_fileindex_entry_free(ie);
3045 goto done;
3047 if (is_bad_symlink) {
3048 got_fileindex_entry_filetype_set(ie,
3049 GOT_FILEIDX_MODE_BAD_SYMLINK);
3053 done:
3054 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3055 err = got_error_from_errno("fclose");
3056 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3057 err = got_error_from_errno("fclose");
3058 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3059 err = got_error_from_errno("fclose");
3060 free(id_str);
3061 free(label_deriv2);
3062 free(ondisk_path);
3063 return err;
3066 static const struct got_error *
3067 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3069 struct got_worktree *worktree = arg;
3071 /* Reject merges into a work tree with mixed base commits. */
3072 if (got_fileindex_entry_has_commit(ie) &&
3073 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3074 SHA1_DIGEST_LENGTH) != 0)
3075 return got_error(GOT_ERR_MIXED_COMMITS);
3077 return NULL;
3080 struct check_merge_conflicts_arg {
3081 struct got_worktree *worktree;
3082 struct got_fileindex *fileindex;
3083 struct got_repository *repo;
3086 static const struct got_error *
3087 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3088 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3089 struct got_object_id *id1, struct got_object_id *id2,
3090 const char *path1, const char *path2,
3091 mode_t mode1, mode_t mode2, struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct check_merge_conflicts_arg *a = arg;
3095 unsigned char status;
3096 struct stat sb;
3097 struct got_fileindex_entry *ie;
3098 const char *path = path2 ? path2 : path1;
3099 struct got_object_id *id = id2 ? id2 : id1;
3100 char *ondisk_path;
3102 if (id == NULL)
3103 return NULL;
3105 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3106 if (ie == NULL)
3107 return NULL;
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3110 == -1)
3111 return got_error_from_errno("asprintf");
3113 /* Reject merges into a work tree with conflicted files. */
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3115 free(ondisk_path);
3116 if (err)
3117 return err;
3118 if (status == GOT_STATUS_CONFLICT)
3119 return got_error(GOT_ERR_CONFLICTS);
3121 return NULL;
3124 static const struct got_error *
3125 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3126 const char *fileindex_path, struct got_object_id *commit_id1,
3127 struct got_object_id *commit_id2, struct got_repository *repo,
3128 got_worktree_checkout_cb progress_cb, void *progress_arg,
3129 got_cancel_cb cancel_cb, void *cancel_arg)
3131 const struct got_error *err = NULL, *sync_err;
3132 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3133 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3134 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3135 struct check_merge_conflicts_arg cmc_arg;
3136 struct merge_file_cb_arg arg;
3137 char *label_orig = NULL;
3138 FILE *f1 = NULL, *f2 = NULL;
3140 if (commit_id1) {
3141 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3142 if (err)
3143 goto done;
3144 err = got_object_id_by_path(&tree_id1, repo, commit1,
3145 worktree->path_prefix);
3146 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3147 goto done;
3149 if (tree_id1) {
3150 char *id_str;
3152 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3153 if (err)
3154 goto done;
3156 err = got_object_id_str(&id_str, commit_id1);
3157 if (err)
3158 goto done;
3160 if (asprintf(&label_orig, "%s: commit %s",
3161 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3162 err = got_error_from_errno("asprintf");
3163 free(id_str);
3164 goto done;
3166 free(id_str);
3168 f1 = got_opentemp();
3169 if (f1 == NULL) {
3170 err = got_error_from_errno("got_opentemp");
3171 goto done;
3175 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3176 if (err)
3177 goto done;
3179 err = got_object_id_by_path(&tree_id2, repo, commit2,
3180 worktree->path_prefix);
3181 if (err)
3182 goto done;
3184 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3185 if (err)
3186 goto done;
3188 f2 = got_opentemp();
3189 if (f2 == NULL) {
3190 err = got_error_from_errno("got_opentemp");
3191 goto done;
3194 cmc_arg.worktree = worktree;
3195 cmc_arg.fileindex = fileindex;
3196 cmc_arg.repo = repo;
3197 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3198 check_merge_conflicts, &cmc_arg, 0);
3199 if (err)
3200 goto done;
3202 arg.worktree = worktree;
3203 arg.fileindex = fileindex;
3204 arg.progress_cb = progress_cb;
3205 arg.progress_arg = progress_arg;
3206 arg.cancel_cb = cancel_cb;
3207 arg.cancel_arg = cancel_arg;
3208 arg.label_orig = label_orig;
3209 arg.commit_id2 = commit_id2;
3210 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3211 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3212 merge_file_cb, &arg, 1);
3213 sync_err = sync_fileindex(fileindex, fileindex_path);
3214 if (sync_err && err == NULL)
3215 err = sync_err;
3216 done:
3217 if (commit1)
3218 got_object_commit_close(commit1);
3219 if (commit2)
3220 got_object_commit_close(commit2);
3221 if (tree1)
3222 got_object_tree_close(tree1);
3223 if (tree2)
3224 got_object_tree_close(tree2);
3225 if (f1 && fclose(f1) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 if (f2 && fclose(f2) == EOF && err == NULL)
3228 err = got_error_from_errno("fclose");
3229 free(label_orig);
3230 return err;
3233 const struct got_error *
3234 got_worktree_merge_files(struct got_worktree *worktree,
3235 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3236 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3237 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3239 const struct got_error *err, *unlockerr;
3240 char *fileindex_path = NULL;
3241 struct got_fileindex *fileindex = NULL;
3243 err = lock_worktree(worktree, LOCK_EX);
3244 if (err)
3245 return err;
3247 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3248 if (err)
3249 goto done;
3251 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3252 worktree);
3253 if (err)
3254 goto done;
3256 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3257 commit_id2, repo, progress_cb, progress_arg,
3258 cancel_cb, cancel_arg);
3259 done:
3260 if (fileindex)
3261 got_fileindex_free(fileindex);
3262 free(fileindex_path);
3263 unlockerr = lock_worktree(worktree, LOCK_SH);
3264 if (unlockerr && err == NULL)
3265 err = unlockerr;
3266 return err;
3269 struct diff_dir_cb_arg {
3270 struct got_fileindex *fileindex;
3271 struct got_worktree *worktree;
3272 const char *status_path;
3273 size_t status_path_len;
3274 struct got_repository *repo;
3275 got_worktree_status_cb status_cb;
3276 void *status_arg;
3277 got_cancel_cb cancel_cb;
3278 void *cancel_arg;
3279 /* A pathlist containing per-directory pathlists of ignore patterns. */
3280 struct got_pathlist_head *ignores;
3281 int report_unchanged;
3282 int no_ignores;
3285 static const struct got_error *
3286 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3287 int dirfd, const char *de_name,
3288 got_worktree_status_cb status_cb, void *status_arg,
3289 struct got_repository *repo, int report_unchanged)
3291 const struct got_error *err = NULL;
3292 unsigned char status = GOT_STATUS_NO_CHANGE;
3293 unsigned char staged_status = get_staged_status(ie);
3294 struct stat sb;
3295 struct got_object_id blob_id, commit_id, staged_blob_id;
3296 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3297 struct got_object_id *staged_blob_idp = NULL;
3299 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3300 if (err)
3301 return err;
3303 if (status == GOT_STATUS_NO_CHANGE &&
3304 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3305 return NULL;
3307 if (got_fileindex_entry_has_blob(ie)) {
3308 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3309 blob_idp = &blob_id;
3311 if (got_fileindex_entry_has_commit(ie)) {
3312 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3313 commit_idp = &commit_id;
3315 if (staged_status == GOT_STATUS_ADD ||
3316 staged_status == GOT_STATUS_MODIFY) {
3317 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3318 SHA1_DIGEST_LENGTH);
3319 staged_blob_idp = &staged_blob_id;
3322 return (*status_cb)(status_arg, status, staged_status,
3323 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3326 static const struct got_error *
3327 status_old_new(void *arg, struct got_fileindex_entry *ie,
3328 struct dirent *de, const char *parent_path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3332 char *abspath;
3334 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3335 return got_error(GOT_ERR_CANCELLED);
3337 if (got_path_cmp(parent_path, a->status_path,
3338 strlen(parent_path), a->status_path_len) != 0 &&
3339 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3340 return NULL;
3342 if (parent_path[0]) {
3343 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3344 parent_path, de->d_name) == -1)
3345 return got_error_from_errno("asprintf");
3346 } else {
3347 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3348 de->d_name) == -1)
3349 return got_error_from_errno("asprintf");
3352 err = report_file_status(ie, abspath, dirfd, de->d_name,
3353 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3354 free(abspath);
3355 return err;
3358 static const struct got_error *
3359 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3361 struct diff_dir_cb_arg *a = arg;
3362 struct got_object_id blob_id, commit_id;
3363 unsigned char status;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3369 return NULL;
3371 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3372 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3373 if (got_fileindex_entry_has_file_on_disk(ie))
3374 status = GOT_STATUS_MISSING;
3375 else
3376 status = GOT_STATUS_DELETE;
3377 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3378 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3381 static void
3382 free_ignorelist(struct got_pathlist_head *ignorelist)
3384 struct got_pathlist_entry *pe;
3386 TAILQ_FOREACH(pe, ignorelist, entry)
3387 free((char *)pe->path);
3388 got_pathlist_free(ignorelist);
3391 static void
3392 free_ignores(struct got_pathlist_head *ignores)
3394 struct got_pathlist_entry *pe;
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 free_ignorelist(ignorelist);
3399 free((char *)pe->path);
3401 got_pathlist_free(ignores);
3404 static const struct got_error *
3405 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3407 const struct got_error *err = NULL;
3408 struct got_pathlist_entry *pe = NULL;
3409 struct got_pathlist_head *ignorelist;
3410 char *line = NULL, *pattern, *dirpath = NULL;
3411 size_t linesize = 0;
3412 ssize_t linelen;
3414 ignorelist = calloc(1, sizeof(*ignorelist));
3415 if (ignorelist == NULL)
3416 return got_error_from_errno("calloc");
3417 TAILQ_INIT(ignorelist);
3419 while ((linelen = getline(&line, &linesize, f)) != -1) {
3420 if (linelen > 0 && line[linelen - 1] == '\n')
3421 line[linelen - 1] = '\0';
3423 /* Git's ignores may contain comments. */
3424 if (line[0] == '#')
3425 continue;
3427 /* Git's negated patterns are not (yet?) supported. */
3428 if (line[0] == '!')
3429 continue;
3431 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3432 line) == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto done;
3436 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3437 if (err)
3438 goto done;
3440 if (ferror(f)) {
3441 err = got_error_from_errno("getline");
3442 goto done;
3445 dirpath = strdup(path);
3446 if (dirpath == NULL) {
3447 err = got_error_from_errno("strdup");
3448 goto done;
3450 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3451 done:
3452 free(line);
3453 if (err || pe == NULL) {
3454 free(dirpath);
3455 free_ignorelist(ignorelist);
3457 return err;
3460 static int
3461 match_ignores(struct got_pathlist_head *ignores, const char *path)
3463 struct got_pathlist_entry *pe;
3465 /* Handle patterns which match in all directories. */
3466 TAILQ_FOREACH(pe, ignores, entry) {
3467 struct got_pathlist_head *ignorelist = pe->data;
3468 struct got_pathlist_entry *pi;
3470 TAILQ_FOREACH(pi, ignorelist, entry) {
3471 const char *p, *pattern = pi->path;
3473 if (strncmp(pattern, "**/", 3) != 0)
3474 continue;
3475 pattern += 3;
3476 p = path;
3477 while (*p) {
3478 if (fnmatch(pattern, p,
3479 FNM_PATHNAME | FNM_LEADING_DIR)) {
3480 /* Retry in next directory. */
3481 while (*p && *p != '/')
3482 p++;
3483 while (*p == '/')
3484 p++;
3485 continue;
3487 return 1;
3493 * The ignores pathlist contains ignore lists from children before
3494 * parents, so we can find the most specific ignorelist by walking
3495 * ignores backwards.
3497 pe = TAILQ_LAST(ignores, got_pathlist_head);
3498 while (pe) {
3499 if (got_path_is_child(path, pe->path, pe->path_len)) {
3500 struct got_pathlist_head *ignorelist = pe->data;
3501 struct got_pathlist_entry *pi;
3502 TAILQ_FOREACH(pi, ignorelist, entry) {
3503 const char *pattern = pi->path;
3504 int flags = FNM_LEADING_DIR;
3505 if (strstr(pattern, "/**/") == NULL)
3506 flags |= FNM_PATHNAME;
3507 if (fnmatch(pattern, path, flags))
3508 continue;
3509 return 1;
3512 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3515 return 0;
3518 static const struct got_error *
3519 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3520 const char *path, int dirfd, const char *ignores_filename)
3522 const struct got_error *err = NULL;
3523 char *ignorespath;
3524 int fd = -1;
3525 FILE *ignoresfile = NULL;
3527 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3528 path[0] ? "/" : "", ignores_filename) == -1)
3529 return got_error_from_errno("asprintf");
3531 if (dirfd != -1) {
3532 fd = openat(dirfd, ignores_filename,
3533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3534 if (fd == -1) {
3535 if (errno != ENOENT && errno != EACCES)
3536 err = got_error_from_errno2("openat",
3537 ignorespath);
3538 } else {
3539 ignoresfile = fdopen(fd, "r");
3540 if (ignoresfile == NULL)
3541 err = got_error_from_errno2("fdopen",
3542 ignorespath);
3543 else {
3544 fd = -1;
3545 err = read_ignores(ignores, path, ignoresfile);
3548 } else {
3549 ignoresfile = fopen(ignorespath, "re");
3550 if (ignoresfile == NULL) {
3551 if (errno != ENOENT && errno != EACCES)
3552 err = got_error_from_errno2("fopen",
3553 ignorespath);
3554 } else
3555 err = read_ignores(ignores, path, ignoresfile);
3558 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3559 err = got_error_from_errno2("fclose", path);
3560 if (fd != -1 && close(fd) == -1 && err == NULL)
3561 err = got_error_from_errno2("close", path);
3562 free(ignorespath);
3563 return err;
3566 static const struct got_error *
3567 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3568 int dirfd)
3570 const struct got_error *err = NULL;
3571 struct diff_dir_cb_arg *a = arg;
3572 char *path = NULL;
3574 if (ignore != NULL)
3575 *ignore = 0;
3577 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3578 return got_error(GOT_ERR_CANCELLED);
3580 if (parent_path[0]) {
3581 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3582 return got_error_from_errno("asprintf");
3583 } else {
3584 path = de->d_name;
3587 if (de->d_type == DT_DIR) {
3588 if (!a->no_ignores && ignore != NULL &&
3589 match_ignores(a->ignores, path))
3590 *ignore = 1;
3591 } else if (!match_ignores(a->ignores, path) &&
3592 got_path_is_child(path, a->status_path, a->status_path_len))
3593 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3594 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3595 if (parent_path[0])
3596 free(path);
3597 return err;
3600 static const struct got_error *
3601 status_traverse(void *arg, const char *path, int dirfd)
3603 const struct got_error *err = NULL;
3604 struct diff_dir_cb_arg *a = arg;
3606 if (a->no_ignores)
3607 return NULL;
3609 err = add_ignores(a->ignores, a->worktree->root_path,
3610 path, dirfd, ".cvsignore");
3611 if (err)
3612 return err;
3614 err = add_ignores(a->ignores, a->worktree->root_path, path,
3615 dirfd, ".gitignore");
3617 return err;
3620 static const struct got_error *
3621 report_single_file_status(const char *path, const char *ondisk_path,
3622 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3623 void *status_arg, struct got_repository *repo, int report_unchanged,
3624 struct got_pathlist_head *ignores, int no_ignores)
3626 struct got_fileindex_entry *ie;
3627 struct stat sb;
3629 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3630 if (ie)
3631 return report_file_status(ie, ondisk_path, -1, NULL,
3632 status_cb, status_arg, repo, report_unchanged);
3634 if (lstat(ondisk_path, &sb) == -1) {
3635 if (errno != ENOENT)
3636 return got_error_from_errno2("lstat", ondisk_path);
3637 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3638 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3641 if (!no_ignores && match_ignores(ignores, path))
3642 return NULL;
3644 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3645 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3646 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3648 return NULL;
3651 static const struct got_error *
3652 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3653 const char *root_path, const char *path)
3655 const struct got_error *err;
3656 char *parent_path, *next_parent_path = NULL;
3658 err = add_ignores(ignores, root_path, "", -1,
3659 ".cvsignore");
3660 if (err)
3661 return err;
3663 err = add_ignores(ignores, root_path, "", -1,
3664 ".gitignore");
3665 if (err)
3666 return err;
3668 err = got_path_dirname(&parent_path, path);
3669 if (err) {
3670 if (err->code == GOT_ERR_BAD_PATH)
3671 return NULL; /* cannot traverse parent */
3672 return err;
3674 for (;;) {
3675 err = add_ignores(ignores, root_path, parent_path, -1,
3676 ".cvsignore");
3677 if (err)
3678 break;
3679 err = add_ignores(ignores, root_path, parent_path, -1,
3680 ".gitignore");
3681 if (err)
3682 break;
3683 err = got_path_dirname(&next_parent_path, parent_path);
3684 if (err) {
3685 if (err->code == GOT_ERR_BAD_PATH)
3686 err = NULL; /* traversed everything */
3687 break;
3689 if (got_path_is_root_dir(parent_path))
3690 break;
3691 free(parent_path);
3692 parent_path = next_parent_path;
3693 next_parent_path = NULL;
3696 free(parent_path);
3697 free(next_parent_path);
3698 return err;
3701 static const struct got_error *
3702 worktree_status(struct got_worktree *worktree, const char *path,
3703 struct got_fileindex *fileindex, struct got_repository *repo,
3704 got_worktree_status_cb status_cb, void *status_arg,
3705 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3706 int report_unchanged)
3708 const struct got_error *err = NULL;
3709 int fd = -1;
3710 struct got_fileindex_diff_dir_cb fdiff_cb;
3711 struct diff_dir_cb_arg arg;
3712 char *ondisk_path = NULL;
3713 struct got_pathlist_head ignores;
3714 struct got_fileindex_entry *ie;
3716 TAILQ_INIT(&ignores);
3718 if (asprintf(&ondisk_path, "%s%s%s",
3719 worktree->root_path, path[0] ? "/" : "", path) == -1)
3720 return got_error_from_errno("asprintf");
3722 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3723 if (ie) {
3724 err = report_single_file_status(path, ondisk_path,
3725 fileindex, status_cb, status_arg, repo,
3726 report_unchanged, &ignores, no_ignores);
3727 goto done;
3730 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3731 if (fd == -1) {
3732 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3733 !got_err_open_nofollow_on_symlink())
3734 err = got_error_from_errno2("open", ondisk_path);
3735 else {
3736 if (!no_ignores) {
3737 err = add_ignores_from_parent_paths(&ignores,
3738 worktree->root_path, ondisk_path);
3739 if (err)
3740 goto done;
3742 err = report_single_file_status(path, ondisk_path,
3743 fileindex, status_cb, status_arg, repo,
3744 report_unchanged, &ignores, no_ignores);
3746 } else {
3747 fdiff_cb.diff_old_new = status_old_new;
3748 fdiff_cb.diff_old = status_old;
3749 fdiff_cb.diff_new = status_new;
3750 fdiff_cb.diff_traverse = status_traverse;
3751 arg.fileindex = fileindex;
3752 arg.worktree = worktree;
3753 arg.status_path = path;
3754 arg.status_path_len = strlen(path);
3755 arg.repo = repo;
3756 arg.status_cb = status_cb;
3757 arg.status_arg = status_arg;
3758 arg.cancel_cb = cancel_cb;
3759 arg.cancel_arg = cancel_arg;
3760 arg.report_unchanged = report_unchanged;
3761 arg.no_ignores = no_ignores;
3762 if (!no_ignores) {
3763 err = add_ignores_from_parent_paths(&ignores,
3764 worktree->root_path, path);
3765 if (err)
3766 goto done;
3768 arg.ignores = &ignores;
3769 err = got_fileindex_diff_dir(fileindex, fd,
3770 worktree->root_path, path, repo, &fdiff_cb, &arg);
3772 done:
3773 free_ignores(&ignores);
3774 if (fd != -1 && close(fd) == -1 && err == NULL)
3775 err = got_error_from_errno("close");
3776 free(ondisk_path);
3777 return err;
3780 const struct got_error *
3781 got_worktree_status(struct got_worktree *worktree,
3782 struct got_pathlist_head *paths, struct got_repository *repo,
3783 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3784 got_cancel_cb cancel_cb, void *cancel_arg)
3786 const struct got_error *err = NULL;
3787 char *fileindex_path = NULL;
3788 struct got_fileindex *fileindex = NULL;
3789 struct got_pathlist_entry *pe;
3791 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3792 if (err)
3793 return err;
3795 TAILQ_FOREACH(pe, paths, entry) {
3796 err = worktree_status(worktree, pe->path, fileindex, repo,
3797 status_cb, status_arg, cancel_cb, cancel_arg,
3798 no_ignores, 0);
3799 if (err)
3800 break;
3802 free(fileindex_path);
3803 got_fileindex_free(fileindex);
3804 return err;
3807 const struct got_error *
3808 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3809 const char *arg)
3811 const struct got_error *err = NULL;
3812 char *resolved = NULL, *cwd = NULL, *path = NULL;
3813 size_t len;
3814 struct stat sb;
3815 char *abspath = NULL;
3816 char canonpath[PATH_MAX];
3818 *wt_path = NULL;
3820 cwd = getcwd(NULL, 0);
3821 if (cwd == NULL)
3822 return got_error_from_errno("getcwd");
3824 if (lstat(arg, &sb) == -1) {
3825 if (errno != ENOENT) {
3826 err = got_error_from_errno2("lstat", arg);
3827 goto done;
3829 sb.st_mode = 0;
3831 if (S_ISLNK(sb.st_mode)) {
3833 * We cannot use realpath(3) with symlinks since we want to
3834 * operate on the symlink itself.
3835 * But we can make the path absolute, assuming it is relative
3836 * to the current working directory, and then canonicalize it.
3838 if (!got_path_is_absolute(arg)) {
3839 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3840 err = got_error_from_errno("asprintf");
3841 goto done;
3845 err = got_canonpath(abspath ? abspath : arg, canonpath,
3846 sizeof(canonpath));
3847 if (err)
3848 goto done;
3849 resolved = strdup(canonpath);
3850 if (resolved == NULL) {
3851 err = got_error_from_errno("strdup");
3852 goto done;
3854 } else {
3855 resolved = realpath(arg, NULL);
3856 if (resolved == NULL) {
3857 if (errno != ENOENT) {
3858 err = got_error_from_errno2("realpath", arg);
3859 goto done;
3861 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3862 err = got_error_from_errno("asprintf");
3863 goto done;
3865 err = got_canonpath(abspath, canonpath,
3866 sizeof(canonpath));
3867 if (err)
3868 goto done;
3869 resolved = strdup(canonpath);
3870 if (resolved == NULL) {
3871 err = got_error_from_errno("strdup");
3872 goto done;
3877 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3878 strlen(got_worktree_get_root_path(worktree)))) {
3879 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3880 goto done;
3883 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3884 err = got_path_skip_common_ancestor(&path,
3885 got_worktree_get_root_path(worktree), resolved);
3886 if (err)
3887 goto done;
3888 } else {
3889 path = strdup("");
3890 if (path == NULL) {
3891 err = got_error_from_errno("strdup");
3892 goto done;
3896 /* XXX status walk can't deal with trailing slash! */
3897 len = strlen(path);
3898 while (len > 0 && path[len - 1] == '/') {
3899 path[len - 1] = '\0';
3900 len--;
3902 done:
3903 free(abspath);
3904 free(resolved);
3905 free(cwd);
3906 if (err == NULL)
3907 *wt_path = path;
3908 else
3909 free(path);
3910 return err;
3913 struct schedule_addition_args {
3914 struct got_worktree *worktree;
3915 struct got_fileindex *fileindex;
3916 got_worktree_checkout_cb progress_cb;
3917 void *progress_arg;
3918 struct got_repository *repo;
3921 static const struct got_error *
3922 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3923 const char *relpath, struct got_object_id *blob_id,
3924 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3925 int dirfd, const char *de_name)
3927 struct schedule_addition_args *a = arg;
3928 const struct got_error *err = NULL;
3929 struct got_fileindex_entry *ie;
3930 struct stat sb;
3931 char *ondisk_path;
3933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3934 relpath) == -1)
3935 return got_error_from_errno("asprintf");
3937 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3938 if (ie) {
3939 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3940 de_name, a->repo);
3941 if (err)
3942 goto done;
3943 /* Re-adding an existing entry is a no-op. */
3944 if (status == GOT_STATUS_ADD)
3945 goto done;
3946 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3947 if (err)
3948 goto done;
3951 if (status != GOT_STATUS_UNVERSIONED) {
3952 if (status == GOT_STATUS_NONEXISTENT)
3953 err = got_error_set_errno(ENOENT, ondisk_path);
3954 else
3955 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3956 goto done;
3959 err = got_fileindex_entry_alloc(&ie, relpath);
3960 if (err)
3961 goto done;
3962 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3963 relpath, NULL, NULL, 1);
3964 if (err) {
3965 got_fileindex_entry_free(ie);
3966 goto done;
3968 err = got_fileindex_entry_add(a->fileindex, ie);
3969 if (err) {
3970 got_fileindex_entry_free(ie);
3971 goto done;
3973 done:
3974 free(ondisk_path);
3975 if (err)
3976 return err;
3977 if (status == GOT_STATUS_ADD)
3978 return NULL;
3979 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3982 const struct got_error *
3983 got_worktree_schedule_add(struct got_worktree *worktree,
3984 struct got_pathlist_head *paths,
3985 got_worktree_checkout_cb progress_cb, void *progress_arg,
3986 struct got_repository *repo, int no_ignores)
3988 struct got_fileindex *fileindex = NULL;
3989 char *fileindex_path = NULL;
3990 const struct got_error *err = NULL, *sync_err, *unlockerr;
3991 struct got_pathlist_entry *pe;
3992 struct schedule_addition_args saa;
3994 err = lock_worktree(worktree, LOCK_EX);
3995 if (err)
3996 return err;
3998 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3999 if (err)
4000 goto done;
4002 saa.worktree = worktree;
4003 saa.fileindex = fileindex;
4004 saa.progress_cb = progress_cb;
4005 saa.progress_arg = progress_arg;
4006 saa.repo = repo;
4008 TAILQ_FOREACH(pe, paths, entry) {
4009 err = worktree_status(worktree, pe->path, fileindex, repo,
4010 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4011 if (err)
4012 break;
4014 sync_err = sync_fileindex(fileindex, fileindex_path);
4015 if (sync_err && err == NULL)
4016 err = sync_err;
4017 done:
4018 free(fileindex_path);
4019 if (fileindex)
4020 got_fileindex_free(fileindex);
4021 unlockerr = lock_worktree(worktree, LOCK_SH);
4022 if (unlockerr && err == NULL)
4023 err = unlockerr;
4024 return err;
4027 struct schedule_deletion_args {
4028 struct got_worktree *worktree;
4029 struct got_fileindex *fileindex;
4030 got_worktree_delete_cb progress_cb;
4031 void *progress_arg;
4032 struct got_repository *repo;
4033 int delete_local_mods;
4034 int keep_on_disk;
4035 int ignore_missing_paths;
4036 const char *status_codes;
4039 static const struct got_error *
4040 schedule_for_deletion(void *arg, unsigned char status,
4041 unsigned char staged_status, const char *relpath,
4042 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 struct got_object_id *commit_id, int dirfd, const char *de_name)
4045 struct schedule_deletion_args *a = arg;
4046 const struct got_error *err = NULL;
4047 struct got_fileindex_entry *ie = NULL;
4048 struct stat sb;
4049 char *ondisk_path;
4051 if (status == GOT_STATUS_NONEXISTENT) {
4052 if (a->ignore_missing_paths)
4053 return NULL;
4054 return got_error_set_errno(ENOENT, relpath);
4057 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4058 if (ie == NULL)
4059 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4061 staged_status = get_staged_status(ie);
4062 if (staged_status != GOT_STATUS_NO_CHANGE) {
4063 if (staged_status == GOT_STATUS_DELETE)
4064 return NULL;
4065 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4068 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4069 relpath) == -1)
4070 return got_error_from_errno("asprintf");
4072 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4073 a->repo);
4074 if (err)
4075 goto done;
4077 if (a->status_codes) {
4078 size_t ncodes = strlen(a->status_codes);
4079 int i;
4080 for (i = 0; i < ncodes ; i++) {
4081 if (status == a->status_codes[i])
4082 break;
4084 if (i == ncodes) {
4085 /* Do not delete files in non-matching status. */
4086 free(ondisk_path);
4087 return NULL;
4089 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4090 a->status_codes[i] != GOT_STATUS_MISSING) {
4091 static char msg[64];
4092 snprintf(msg, sizeof(msg),
4093 "invalid status code '%c'", a->status_codes[i]);
4094 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4095 goto done;
4099 if (status != GOT_STATUS_NO_CHANGE) {
4100 if (status == GOT_STATUS_DELETE)
4101 goto done;
4102 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4103 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4104 goto done;
4106 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4107 err = got_error_set_errno(ENOENT, relpath);
4108 goto done;
4110 if (status != GOT_STATUS_MODIFY &&
4111 status != GOT_STATUS_MISSING) {
4112 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4113 goto done;
4117 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4118 size_t root_len;
4120 if (dirfd != -1) {
4121 if (unlinkat(dirfd, de_name, 0) != 0) {
4122 err = got_error_from_errno2("unlinkat",
4123 ondisk_path);
4124 goto done;
4126 } else if (unlink(ondisk_path) != 0) {
4127 err = got_error_from_errno2("unlink", ondisk_path);
4128 goto done;
4131 root_len = strlen(a->worktree->root_path);
4132 do {
4133 char *parent;
4134 err = got_path_dirname(&parent, ondisk_path);
4135 if (err)
4136 goto done;
4137 free(ondisk_path);
4138 ondisk_path = parent;
4139 if (rmdir(ondisk_path) == -1) {
4140 if (errno != ENOTEMPTY)
4141 err = got_error_from_errno2("rmdir",
4142 ondisk_path);
4143 break;
4145 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4146 strlen(ondisk_path), root_len) != 0);
4149 got_fileindex_entry_mark_deleted_from_disk(ie);
4150 done:
4151 free(ondisk_path);
4152 if (err)
4153 return err;
4154 if (status == GOT_STATUS_DELETE)
4155 return NULL;
4156 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4157 staged_status, relpath);
4160 const struct got_error *
4161 got_worktree_schedule_delete(struct got_worktree *worktree,
4162 struct got_pathlist_head *paths, int delete_local_mods,
4163 const char *status_codes,
4164 got_worktree_delete_cb progress_cb, void *progress_arg,
4165 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4167 struct got_fileindex *fileindex = NULL;
4168 char *fileindex_path = NULL;
4169 const struct got_error *err = NULL, *sync_err, *unlockerr;
4170 struct got_pathlist_entry *pe;
4171 struct schedule_deletion_args sda;
4173 err = lock_worktree(worktree, LOCK_EX);
4174 if (err)
4175 return err;
4177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4178 if (err)
4179 goto done;
4181 sda.worktree = worktree;
4182 sda.fileindex = fileindex;
4183 sda.progress_cb = progress_cb;
4184 sda.progress_arg = progress_arg;
4185 sda.repo = repo;
4186 sda.delete_local_mods = delete_local_mods;
4187 sda.keep_on_disk = keep_on_disk;
4188 sda.ignore_missing_paths = ignore_missing_paths;
4189 sda.status_codes = status_codes;
4191 TAILQ_FOREACH(pe, paths, entry) {
4192 err = worktree_status(worktree, pe->path, fileindex, repo,
4193 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4194 if (err)
4195 break;
4197 sync_err = sync_fileindex(fileindex, fileindex_path);
4198 if (sync_err && err == NULL)
4199 err = sync_err;
4200 done:
4201 free(fileindex_path);
4202 if (fileindex)
4203 got_fileindex_free(fileindex);
4204 unlockerr = lock_worktree(worktree, LOCK_SH);
4205 if (unlockerr && err == NULL)
4206 err = unlockerr;
4207 return err;
4210 static const struct got_error *
4211 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4213 const struct got_error *err = NULL;
4214 char *line = NULL;
4215 size_t linesize = 0, n;
4216 ssize_t linelen;
4218 linelen = getline(&line, &linesize, infile);
4219 if (linelen == -1) {
4220 if (ferror(infile)) {
4221 err = got_error_from_errno("getline");
4222 goto done;
4224 return NULL;
4226 if (outfile) {
4227 n = fwrite(line, 1, linelen, outfile);
4228 if (n != linelen) {
4229 err = got_ferror(outfile, GOT_ERR_IO);
4230 goto done;
4233 if (rejectfile) {
4234 n = fwrite(line, 1, linelen, rejectfile);
4235 if (n != linelen)
4236 err = got_ferror(outfile, GOT_ERR_IO);
4238 done:
4239 free(line);
4240 return err;
4243 static const struct got_error *
4244 skip_one_line(FILE *f)
4246 char *line = NULL;
4247 size_t linesize = 0;
4248 ssize_t linelen;
4250 linelen = getline(&line, &linesize, f);
4251 if (linelen == -1) {
4252 if (ferror(f))
4253 return got_error_from_errno("getline");
4254 return NULL;
4256 free(line);
4257 return NULL;
4260 static const struct got_error *
4261 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4262 int start_old, int end_old, int start_new, int end_new,
4263 FILE *outfile, FILE *rejectfile)
4265 const struct got_error *err;
4267 /* Copy old file's lines leading up to patch. */
4268 while (!feof(f1) && *line_cur1 < start_old) {
4269 err = copy_one_line(f1, outfile, NULL);
4270 if (err)
4271 return err;
4272 (*line_cur1)++;
4274 /* Skip new file's lines leading up to patch. */
4275 while (!feof(f2) && *line_cur2 < start_new) {
4276 if (rejectfile)
4277 err = copy_one_line(f2, NULL, rejectfile);
4278 else
4279 err = skip_one_line(f2);
4280 if (err)
4281 return err;
4282 (*line_cur2)++;
4284 /* Copy patched lines. */
4285 while (!feof(f2) && *line_cur2 <= end_new) {
4286 err = copy_one_line(f2, outfile, NULL);
4287 if (err)
4288 return err;
4289 (*line_cur2)++;
4291 /* Skip over old file's replaced lines. */
4292 while (!feof(f1) && *line_cur1 <= end_old) {
4293 if (rejectfile)
4294 err = copy_one_line(f1, NULL, rejectfile);
4295 else
4296 err = skip_one_line(f1);
4297 if (err)
4298 return err;
4299 (*line_cur1)++;
4302 return NULL;
4305 static const struct got_error *
4306 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4307 FILE *outfile, FILE *rejectfile)
4309 const struct got_error *err;
4311 if (outfile) {
4312 /* Copy old file's lines until EOF. */
4313 while (!feof(f1)) {
4314 err = copy_one_line(f1, outfile, NULL);
4315 if (err)
4316 return err;
4317 (*line_cur1)++;
4320 if (rejectfile) {
4321 /* Copy new file's lines until EOF. */
4322 while (!feof(f2)) {
4323 err = copy_one_line(f2, NULL, rejectfile);
4324 if (err)
4325 return err;
4326 (*line_cur2)++;
4330 return NULL;
4333 static const struct got_error *
4334 apply_or_reject_change(int *choice, int *nchunks_used,
4335 struct diff_result *diff_result, int n,
4336 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err = NULL;
4341 struct diff_chunk_context cc = {};
4342 int start_old, end_old, start_new, end_new;
4343 FILE *hunkfile;
4344 struct diff_output_unidiff_state *diff_state;
4345 struct diff_input_info diff_info;
4346 int rc;
4348 *choice = GOT_PATCH_CHOICE_NONE;
4350 /* Get changed line numbers without context lines for copy_change(). */
4351 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4352 start_old = cc.left.start;
4353 end_old = cc.left.end;
4354 start_new = cc.right.start;
4355 end_new = cc.right.end;
4357 /* Get the same change with context lines for display. */
4358 memset(&cc, 0, sizeof(cc));
4359 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4361 memset(&diff_info, 0, sizeof(diff_info));
4362 diff_info.left_path = relpath;
4363 diff_info.right_path = relpath;
4365 diff_state = diff_output_unidiff_state_alloc();
4366 if (diff_state == NULL)
4367 return got_error_set_errno(ENOMEM,
4368 "diff_output_unidiff_state_alloc");
4370 hunkfile = got_opentemp();
4371 if (hunkfile == NULL) {
4372 err = got_error_from_errno("got_opentemp");
4373 goto done;
4376 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4377 diff_result, &cc);
4378 if (rc != DIFF_RC_OK) {
4379 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4380 goto done;
4383 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4384 err = got_ferror(hunkfile, GOT_ERR_IO);
4385 goto done;
4388 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4389 hunkfile, changeno, nchanges);
4390 if (err)
4391 goto done;
4393 switch (*choice) {
4394 case GOT_PATCH_CHOICE_YES:
4395 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4396 end_old, start_new, end_new, outfile, rejectfile);
4397 break;
4398 case GOT_PATCH_CHOICE_NO:
4399 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4400 end_old, start_new, end_new, rejectfile, outfile);
4401 break;
4402 case GOT_PATCH_CHOICE_QUIT:
4403 break;
4404 default:
4405 err = got_error(GOT_ERR_PATCH_CHOICE);
4406 break;
4408 done:
4409 diff_output_unidiff_state_free(diff_state);
4410 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4411 err = got_error_from_errno("fclose");
4412 return err;
4415 struct revert_file_args {
4416 struct got_worktree *worktree;
4417 struct got_fileindex *fileindex;
4418 got_worktree_checkout_cb progress_cb;
4419 void *progress_arg;
4420 got_worktree_patch_cb patch_cb;
4421 void *patch_arg;
4422 struct got_repository *repo;
4423 int unlink_added_files;
4426 static const struct got_error *
4427 create_patched_content(char **path_outfile, int reverse_patch,
4428 struct got_object_id *blob_id, const char *path2,
4429 int dirfd2, const char *de_name2,
4430 const char *relpath, struct got_repository *repo,
4431 got_worktree_patch_cb patch_cb, void *patch_arg)
4433 const struct got_error *err, *free_err;
4434 struct got_blob_object *blob = NULL;
4435 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4436 int fd = -1, fd2 = -1;
4437 char link_target[PATH_MAX];
4438 ssize_t link_len = 0;
4439 char *path1 = NULL, *id_str = NULL;
4440 struct stat sb2;
4441 struct got_diffreg_result *diffreg_result = NULL;
4442 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4443 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4445 *path_outfile = NULL;
4447 err = got_object_id_str(&id_str, blob_id);
4448 if (err)
4449 return err;
4451 if (dirfd2 != -1) {
4452 fd2 = openat(dirfd2, de_name2,
4453 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4454 if (fd2 == -1) {
4455 if (!got_err_open_nofollow_on_symlink()) {
4456 err = got_error_from_errno2("openat", path2);
4457 goto done;
4459 link_len = readlinkat(dirfd2, de_name2,
4460 link_target, sizeof(link_target));
4461 if (link_len == -1) {
4462 return got_error_from_errno2("readlinkat",
4463 path2);
4465 sb2.st_mode = S_IFLNK;
4466 sb2.st_size = link_len;
4468 } else {
4469 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4470 if (fd2 == -1) {
4471 if (!got_err_open_nofollow_on_symlink()) {
4472 err = got_error_from_errno2("open", path2);
4473 goto done;
4475 link_len = readlink(path2, link_target,
4476 sizeof(link_target));
4477 if (link_len == -1)
4478 return got_error_from_errno2("readlink", path2);
4479 sb2.st_mode = S_IFLNK;
4480 sb2.st_size = link_len;
4483 if (fd2 != -1) {
4484 if (fstat(fd2, &sb2) == -1) {
4485 err = got_error_from_errno2("fstat", path2);
4486 goto done;
4489 f2 = fdopen(fd2, "r");
4490 if (f2 == NULL) {
4491 err = got_error_from_errno2("fdopen", path2);
4492 goto done;
4494 fd2 = -1;
4495 } else {
4496 size_t n;
4497 f2 = got_opentemp();
4498 if (f2 == NULL) {
4499 err = got_error_from_errno2("got_opentemp", path2);
4500 goto done;
4502 n = fwrite(link_target, 1, link_len, f2);
4503 if (n != link_len) {
4504 err = got_ferror(f2, GOT_ERR_IO);
4505 goto done;
4507 if (fflush(f2) == EOF) {
4508 err = got_error_from_errno("fflush");
4509 goto done;
4511 rewind(f2);
4514 fd = got_opentempfd();
4515 if (fd == -1) {
4516 err = got_error_from_errno("got_opentempfd");
4517 goto done;
4520 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4521 if (err)
4522 goto done;
4524 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4525 if (err)
4526 goto done;
4528 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4529 if (err)
4530 goto done;
4532 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4533 NULL);
4534 if (err)
4535 goto done;
4537 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4538 if (err)
4539 goto done;
4541 if (fseek(f1, 0L, SEEK_SET) == -1)
4542 return got_ferror(f1, GOT_ERR_IO);
4543 if (fseek(f2, 0L, SEEK_SET) == -1)
4544 return got_ferror(f2, GOT_ERR_IO);
4546 /* Count the number of actual changes in the diff result. */
4547 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4548 struct diff_chunk_context cc = {};
4549 diff_chunk_context_load_change(&cc, &nchunks_used,
4550 diffreg_result->result, n, 0);
4551 nchanges++;
4553 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4554 int choice;
4555 err = apply_or_reject_change(&choice, &nchunks_used,
4556 diffreg_result->result, n, relpath, f1, f2,
4557 &line_cur1, &line_cur2,
4558 reverse_patch ? NULL : outfile,
4559 reverse_patch ? outfile : NULL,
4560 ++i, nchanges, patch_cb, patch_arg);
4561 if (err)
4562 goto done;
4563 if (choice == GOT_PATCH_CHOICE_YES)
4564 have_content = 1;
4565 else if (choice == GOT_PATCH_CHOICE_QUIT)
4566 break;
4568 if (have_content) {
4569 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4570 reverse_patch ? NULL : outfile,
4571 reverse_patch ? outfile : NULL);
4572 if (err)
4573 goto done;
4575 if (!S_ISLNK(sb2.st_mode)) {
4576 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4577 err = got_error_from_errno2("fchmod", path2);
4578 goto done;
4582 done:
4583 free(id_str);
4584 if (fd != -1 && close(fd) == -1 && err == NULL)
4585 err = got_error_from_errno("close");
4586 if (blob)
4587 got_object_blob_close(blob);
4588 free_err = got_diffreg_result_free(diffreg_result);
4589 if (err == NULL)
4590 err = free_err;
4591 if (f1 && fclose(f1) == EOF && err == NULL)
4592 err = got_error_from_errno2("fclose", path1);
4593 if (f2 && fclose(f2) == EOF && err == NULL)
4594 err = got_error_from_errno2("fclose", path2);
4595 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4596 err = got_error_from_errno2("close", path2);
4597 if (outfile && fclose(outfile) == EOF && err == NULL)
4598 err = got_error_from_errno2("fclose", *path_outfile);
4599 if (path1 && unlink(path1) == -1 && err == NULL)
4600 err = got_error_from_errno2("unlink", path1);
4601 if (err || !have_content) {
4602 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4603 err = got_error_from_errno2("unlink", *path_outfile);
4604 free(*path_outfile);
4605 *path_outfile = NULL;
4607 free(path1);
4608 return err;
4611 static const struct got_error *
4612 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4613 const char *relpath, struct got_object_id *blob_id,
4614 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4615 int dirfd, const char *de_name)
4617 struct revert_file_args *a = arg;
4618 const struct got_error *err = NULL;
4619 char *parent_path = NULL;
4620 struct got_fileindex_entry *ie;
4621 struct got_commit_object *base_commit = NULL;
4622 struct got_tree_object *tree = NULL;
4623 struct got_object_id *tree_id = NULL;
4624 const struct got_tree_entry *te = NULL;
4625 char *tree_path = NULL, *te_name;
4626 char *ondisk_path = NULL, *path_content = NULL;
4627 struct got_blob_object *blob = NULL;
4628 int fd = -1;
4630 /* Reverting a staged deletion is a no-op. */
4631 if (status == GOT_STATUS_DELETE &&
4632 staged_status != GOT_STATUS_NO_CHANGE)
4633 return NULL;
4635 if (status == GOT_STATUS_UNVERSIONED)
4636 return (*a->progress_cb)(a->progress_arg,
4637 GOT_STATUS_UNVERSIONED, relpath);
4639 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4640 if (ie == NULL)
4641 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4643 /* Construct in-repository path of tree which contains this blob. */
4644 err = got_path_dirname(&parent_path, ie->path);
4645 if (err) {
4646 if (err->code != GOT_ERR_BAD_PATH)
4647 goto done;
4648 parent_path = strdup("/");
4649 if (parent_path == NULL) {
4650 err = got_error_from_errno("strdup");
4651 goto done;
4654 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4655 tree_path = strdup(parent_path);
4656 if (tree_path == NULL) {
4657 err = got_error_from_errno("strdup");
4658 goto done;
4660 } else {
4661 if (got_path_is_root_dir(parent_path)) {
4662 tree_path = strdup(a->worktree->path_prefix);
4663 if (tree_path == NULL) {
4664 err = got_error_from_errno("strdup");
4665 goto done;
4667 } else {
4668 if (asprintf(&tree_path, "%s/%s",
4669 a->worktree->path_prefix, parent_path) == -1) {
4670 err = got_error_from_errno("asprintf");
4671 goto done;
4676 err = got_object_open_as_commit(&base_commit, a->repo,
4677 a->worktree->base_commit_id);
4678 if (err)
4679 goto done;
4681 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4682 if (err) {
4683 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4684 (status == GOT_STATUS_ADD ||
4685 staged_status == GOT_STATUS_ADD)))
4686 goto done;
4687 } else {
4688 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4689 if (err)
4690 goto done;
4692 err = got_path_basename(&te_name, ie->path);
4693 if (err)
4694 goto done;
4696 te = got_object_tree_find_entry(tree, te_name);
4697 free(te_name);
4698 if (te == NULL && status != GOT_STATUS_ADD &&
4699 staged_status != GOT_STATUS_ADD) {
4700 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4701 goto done;
4705 switch (status) {
4706 case GOT_STATUS_ADD:
4707 if (a->patch_cb) {
4708 int choice = GOT_PATCH_CHOICE_NONE;
4709 err = (*a->patch_cb)(&choice, a->patch_arg,
4710 status, ie->path, NULL, 1, 1);
4711 if (err)
4712 goto done;
4713 if (choice != GOT_PATCH_CHOICE_YES)
4714 break;
4716 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4717 ie->path);
4718 if (err)
4719 goto done;
4720 got_fileindex_entry_remove(a->fileindex, ie);
4721 if (a->unlink_added_files) {
4722 if (asprintf(&ondisk_path, "%s/%s",
4723 got_worktree_get_root_path(a->worktree),
4724 relpath) == -1) {
4725 err = got_error_from_errno("asprintf");
4726 goto done;
4728 if (unlink(ondisk_path) == -1) {
4729 err = got_error_from_errno2("unlink",
4730 ondisk_path);
4731 break;
4734 break;
4735 case GOT_STATUS_DELETE:
4736 if (a->patch_cb) {
4737 int choice = GOT_PATCH_CHOICE_NONE;
4738 err = (*a->patch_cb)(&choice, a->patch_arg,
4739 status, ie->path, NULL, 1, 1);
4740 if (err)
4741 goto done;
4742 if (choice != GOT_PATCH_CHOICE_YES)
4743 break;
4745 /* fall through */
4746 case GOT_STATUS_MODIFY:
4747 case GOT_STATUS_MODE_CHANGE:
4748 case GOT_STATUS_CONFLICT:
4749 case GOT_STATUS_MISSING: {
4750 struct got_object_id id;
4751 if (staged_status == GOT_STATUS_ADD ||
4752 staged_status == GOT_STATUS_MODIFY) {
4753 memcpy(id.sha1, ie->staged_blob_sha1,
4754 SHA1_DIGEST_LENGTH);
4755 } else
4756 memcpy(id.sha1, ie->blob_sha1,
4757 SHA1_DIGEST_LENGTH);
4758 fd = got_opentempfd();
4759 if (fd == -1) {
4760 err = got_error_from_errno("got_opentempfd");
4761 goto done;
4764 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4765 if (err)
4766 goto done;
4768 if (asprintf(&ondisk_path, "%s/%s",
4769 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4770 err = got_error_from_errno("asprintf");
4771 goto done;
4774 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4775 status == GOT_STATUS_CONFLICT)) {
4776 int is_bad_symlink = 0;
4777 err = create_patched_content(&path_content, 1, &id,
4778 ondisk_path, dirfd, de_name, ie->path, a->repo,
4779 a->patch_cb, a->patch_arg);
4780 if (err || path_content == NULL)
4781 break;
4782 if (te && S_ISLNK(te->mode)) {
4783 if (unlink(path_content) == -1) {
4784 err = got_error_from_errno2("unlink",
4785 path_content);
4786 break;
4788 err = install_symlink(&is_bad_symlink,
4789 a->worktree, ondisk_path, ie->path,
4790 blob, 0, 1, 0, 0, a->repo,
4791 a->progress_cb, a->progress_arg);
4792 } else {
4793 if (rename(path_content, ondisk_path) == -1) {
4794 err = got_error_from_errno3("rename",
4795 path_content, ondisk_path);
4796 goto done;
4799 } else {
4800 int is_bad_symlink = 0;
4801 if (te && S_ISLNK(te->mode)) {
4802 err = install_symlink(&is_bad_symlink,
4803 a->worktree, ondisk_path, ie->path,
4804 blob, 0, 1, 0, 0, a->repo,
4805 a->progress_cb, a->progress_arg);
4806 } else {
4807 err = install_blob(a->worktree, ondisk_path,
4808 ie->path,
4809 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4810 got_fileindex_perms_to_st(ie), blob,
4811 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4814 if (err)
4815 goto done;
4816 if (status == GOT_STATUS_DELETE ||
4817 status == GOT_STATUS_MODE_CHANGE) {
4818 err = got_fileindex_entry_update(ie,
4819 a->worktree->root_fd, relpath,
4820 blob->id.sha1,
4821 a->worktree->base_commit_id->sha1, 1);
4822 if (err)
4823 goto done;
4825 if (is_bad_symlink) {
4826 got_fileindex_entry_filetype_set(ie,
4827 GOT_FILEIDX_MODE_BAD_SYMLINK);
4830 break;
4832 default:
4833 break;
4835 done:
4836 free(ondisk_path);
4837 free(path_content);
4838 free(parent_path);
4839 free(tree_path);
4840 if (fd != -1 && close(fd) == -1 && err == NULL)
4841 err = got_error_from_errno("close");
4842 if (blob)
4843 got_object_blob_close(blob);
4844 if (tree)
4845 got_object_tree_close(tree);
4846 free(tree_id);
4847 if (base_commit)
4848 got_object_commit_close(base_commit);
4849 return err;
4852 const struct got_error *
4853 got_worktree_revert(struct got_worktree *worktree,
4854 struct got_pathlist_head *paths,
4855 got_worktree_checkout_cb progress_cb, void *progress_arg,
4856 got_worktree_patch_cb patch_cb, void *patch_arg,
4857 struct got_repository *repo)
4859 struct got_fileindex *fileindex = NULL;
4860 char *fileindex_path = NULL;
4861 const struct got_error *err = NULL, *unlockerr = NULL;
4862 const struct got_error *sync_err = NULL;
4863 struct got_pathlist_entry *pe;
4864 struct revert_file_args rfa;
4866 err = lock_worktree(worktree, LOCK_EX);
4867 if (err)
4868 return err;
4870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4871 if (err)
4872 goto done;
4874 rfa.worktree = worktree;
4875 rfa.fileindex = fileindex;
4876 rfa.progress_cb = progress_cb;
4877 rfa.progress_arg = progress_arg;
4878 rfa.patch_cb = patch_cb;
4879 rfa.patch_arg = patch_arg;
4880 rfa.repo = repo;
4881 rfa.unlink_added_files = 0;
4882 TAILQ_FOREACH(pe, paths, entry) {
4883 err = worktree_status(worktree, pe->path, fileindex, repo,
4884 revert_file, &rfa, NULL, NULL, 1, 0);
4885 if (err)
4886 break;
4888 sync_err = sync_fileindex(fileindex, fileindex_path);
4889 if (sync_err && err == NULL)
4890 err = sync_err;
4891 done:
4892 free(fileindex_path);
4893 if (fileindex)
4894 got_fileindex_free(fileindex);
4895 unlockerr = lock_worktree(worktree, LOCK_SH);
4896 if (unlockerr && err == NULL)
4897 err = unlockerr;
4898 return err;
4901 static void
4902 free_commitable(struct got_commitable *ct)
4904 free(ct->path);
4905 free(ct->in_repo_path);
4906 free(ct->ondisk_path);
4907 free(ct->blob_id);
4908 free(ct->base_blob_id);
4909 free(ct->staged_blob_id);
4910 free(ct->base_commit_id);
4911 free(ct);
4914 struct collect_commitables_arg {
4915 struct got_pathlist_head *commitable_paths;
4916 struct got_repository *repo;
4917 struct got_worktree *worktree;
4918 struct got_fileindex *fileindex;
4919 int have_staged_files;
4920 int allow_bad_symlinks;
4923 static const struct got_error *
4924 collect_commitables(void *arg, unsigned char status,
4925 unsigned char staged_status, const char *relpath,
4926 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4927 struct got_object_id *commit_id, int dirfd, const char *de_name)
4929 struct collect_commitables_arg *a = arg;
4930 const struct got_error *err = NULL;
4931 struct got_commitable *ct = NULL;
4932 struct got_pathlist_entry *new = NULL;
4933 char *parent_path = NULL, *path = NULL;
4934 struct stat sb;
4936 if (a->have_staged_files) {
4937 if (staged_status != GOT_STATUS_MODIFY &&
4938 staged_status != GOT_STATUS_ADD &&
4939 staged_status != GOT_STATUS_DELETE)
4940 return NULL;
4941 } else {
4942 if (status == GOT_STATUS_CONFLICT)
4943 return got_error(GOT_ERR_COMMIT_CONFLICT);
4945 if (status != GOT_STATUS_MODIFY &&
4946 status != GOT_STATUS_MODE_CHANGE &&
4947 status != GOT_STATUS_ADD &&
4948 status != GOT_STATUS_DELETE)
4949 return NULL;
4952 if (asprintf(&path, "/%s", relpath) == -1) {
4953 err = got_error_from_errno("asprintf");
4954 goto done;
4956 if (strcmp(path, "/") == 0) {
4957 parent_path = strdup("");
4958 if (parent_path == NULL)
4959 return got_error_from_errno("strdup");
4960 } else {
4961 err = got_path_dirname(&parent_path, path);
4962 if (err)
4963 return err;
4966 ct = calloc(1, sizeof(*ct));
4967 if (ct == NULL) {
4968 err = got_error_from_errno("calloc");
4969 goto done;
4972 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4973 relpath) == -1) {
4974 err = got_error_from_errno("asprintf");
4975 goto done;
4978 if (staged_status == GOT_STATUS_ADD ||
4979 staged_status == GOT_STATUS_MODIFY) {
4980 struct got_fileindex_entry *ie;
4981 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4982 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4983 case GOT_FILEIDX_MODE_REGULAR_FILE:
4984 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4985 ct->mode = S_IFREG;
4986 break;
4987 case GOT_FILEIDX_MODE_SYMLINK:
4988 ct->mode = S_IFLNK;
4989 break;
4990 default:
4991 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4992 goto done;
4994 ct->mode |= got_fileindex_entry_perms_get(ie);
4995 } else if (status != GOT_STATUS_DELETE &&
4996 staged_status != GOT_STATUS_DELETE) {
4997 if (dirfd != -1) {
4998 if (fstatat(dirfd, de_name, &sb,
4999 AT_SYMLINK_NOFOLLOW) == -1) {
5000 err = got_error_from_errno2("fstatat",
5001 ct->ondisk_path);
5002 goto done;
5004 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5005 err = got_error_from_errno2("lstat", ct->ondisk_path);
5006 goto done;
5008 ct->mode = sb.st_mode;
5011 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5012 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5013 relpath) == -1) {
5014 err = got_error_from_errno("asprintf");
5015 goto done;
5018 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5019 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5020 int is_bad_symlink;
5021 char target_path[PATH_MAX];
5022 ssize_t target_len;
5023 target_len = readlink(ct->ondisk_path, target_path,
5024 sizeof(target_path));
5025 if (target_len == -1) {
5026 err = got_error_from_errno2("readlink",
5027 ct->ondisk_path);
5028 goto done;
5030 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5031 target_len, ct->ondisk_path, a->worktree->root_path);
5032 if (err)
5033 goto done;
5034 if (is_bad_symlink) {
5035 err = got_error_path(ct->ondisk_path,
5036 GOT_ERR_BAD_SYMLINK);
5037 goto done;
5042 ct->status = status;
5043 ct->staged_status = staged_status;
5044 ct->blob_id = NULL; /* will be filled in when blob gets created */
5045 if (ct->status != GOT_STATUS_ADD &&
5046 ct->staged_status != GOT_STATUS_ADD) {
5047 ct->base_blob_id = got_object_id_dup(blob_id);
5048 if (ct->base_blob_id == NULL) {
5049 err = got_error_from_errno("got_object_id_dup");
5050 goto done;
5052 ct->base_commit_id = got_object_id_dup(commit_id);
5053 if (ct->base_commit_id == NULL) {
5054 err = got_error_from_errno("got_object_id_dup");
5055 goto done;
5058 if (ct->staged_status == GOT_STATUS_ADD ||
5059 ct->staged_status == GOT_STATUS_MODIFY) {
5060 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5061 if (ct->staged_blob_id == NULL) {
5062 err = got_error_from_errno("got_object_id_dup");
5063 goto done;
5066 ct->path = strdup(path);
5067 if (ct->path == NULL) {
5068 err = got_error_from_errno("strdup");
5069 goto done;
5071 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5072 done:
5073 if (ct && (err || new == NULL))
5074 free_commitable(ct);
5075 free(parent_path);
5076 free(path);
5077 return err;
5080 static const struct got_error *write_tree(struct got_object_id **, int *,
5081 struct got_tree_object *, const char *, struct got_pathlist_head *,
5082 got_worktree_status_cb status_cb, void *status_arg,
5083 struct got_repository *);
5085 static const struct got_error *
5086 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5087 struct got_tree_entry *te, const char *parent_path,
5088 struct got_pathlist_head *commitable_paths,
5089 got_worktree_status_cb status_cb, void *status_arg,
5090 struct got_repository *repo)
5092 const struct got_error *err = NULL;
5093 struct got_tree_object *subtree;
5094 char *subpath;
5096 if (asprintf(&subpath, "%s%s%s", parent_path,
5097 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5098 return got_error_from_errno("asprintf");
5100 err = got_object_open_as_tree(&subtree, repo, &te->id);
5101 if (err)
5102 return err;
5104 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5105 commitable_paths, status_cb, status_arg, repo);
5106 got_object_tree_close(subtree);
5107 free(subpath);
5108 return err;
5111 static const struct got_error *
5112 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5114 const struct got_error *err = NULL;
5115 char *ct_parent_path = NULL;
5117 *match = 0;
5119 if (strchr(ct->in_repo_path, '/') == NULL) {
5120 *match = got_path_is_root_dir(path);
5121 return NULL;
5124 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5125 if (err)
5126 return err;
5127 *match = (strcmp(path, ct_parent_path) == 0);
5128 free(ct_parent_path);
5129 return err;
5132 static mode_t
5133 get_ct_file_mode(struct got_commitable *ct)
5135 if (S_ISLNK(ct->mode))
5136 return S_IFLNK;
5138 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5141 static const struct got_error *
5142 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5143 struct got_tree_entry *te, struct got_commitable *ct)
5145 const struct got_error *err = NULL;
5147 *new_te = NULL;
5149 err = got_object_tree_entry_dup(new_te, te);
5150 if (err)
5151 goto done;
5153 (*new_te)->mode = get_ct_file_mode(ct);
5155 if (ct->staged_status == GOT_STATUS_MODIFY)
5156 memcpy(&(*new_te)->id, ct->staged_blob_id,
5157 sizeof((*new_te)->id));
5158 else
5159 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5160 done:
5161 if (err && *new_te) {
5162 free(*new_te);
5163 *new_te = NULL;
5165 return err;
5168 static const struct got_error *
5169 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5170 struct got_commitable *ct)
5172 const struct got_error *err = NULL;
5173 char *ct_name = NULL;
5175 *new_te = NULL;
5177 *new_te = calloc(1, sizeof(**new_te));
5178 if (*new_te == NULL)
5179 return got_error_from_errno("calloc");
5181 err = got_path_basename(&ct_name, ct->path);
5182 if (err)
5183 goto done;
5184 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5185 sizeof((*new_te)->name)) {
5186 err = got_error(GOT_ERR_NO_SPACE);
5187 goto done;
5190 (*new_te)->mode = get_ct_file_mode(ct);
5192 if (ct->staged_status == GOT_STATUS_ADD)
5193 memcpy(&(*new_te)->id, ct->staged_blob_id,
5194 sizeof((*new_te)->id));
5195 else
5196 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5197 done:
5198 free(ct_name);
5199 if (err && *new_te) {
5200 free(*new_te);
5201 *new_te = NULL;
5203 return err;
5206 static const struct got_error *
5207 insert_tree_entry(struct got_tree_entry *new_te,
5208 struct got_pathlist_head *paths)
5210 const struct got_error *err = NULL;
5211 struct got_pathlist_entry *new_pe;
5213 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5214 if (err)
5215 return err;
5216 if (new_pe == NULL)
5217 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5218 return NULL;
5221 static const struct got_error *
5222 report_ct_status(struct got_commitable *ct,
5223 got_worktree_status_cb status_cb, void *status_arg)
5225 const char *ct_path = ct->path;
5226 unsigned char status;
5228 if (status_cb == NULL) /* no commit progress output desired */
5229 return NULL;
5231 while (ct_path[0] == '/')
5232 ct_path++;
5234 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5235 status = ct->staged_status;
5236 else
5237 status = ct->status;
5239 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5240 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5243 static const struct got_error *
5244 match_modified_subtree(int *modified, struct got_tree_entry *te,
5245 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5247 const struct got_error *err = NULL;
5248 struct got_pathlist_entry *pe;
5249 char *te_path;
5251 *modified = 0;
5253 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5254 got_path_is_root_dir(base_tree_path) ? "" : "/",
5255 te->name) == -1)
5256 return got_error_from_errno("asprintf");
5258 TAILQ_FOREACH(pe, commitable_paths, entry) {
5259 struct got_commitable *ct = pe->data;
5260 *modified = got_path_is_child(ct->in_repo_path, te_path,
5261 strlen(te_path));
5262 if (*modified)
5263 break;
5266 free(te_path);
5267 return err;
5270 static const struct got_error *
5271 match_deleted_or_modified_ct(struct got_commitable **ctp,
5272 struct got_tree_entry *te, const char *base_tree_path,
5273 struct got_pathlist_head *commitable_paths)
5275 const struct got_error *err = NULL;
5276 struct got_pathlist_entry *pe;
5278 *ctp = NULL;
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_commitable *ct = pe->data;
5282 char *ct_name = NULL;
5283 int path_matches;
5285 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5286 if (ct->status != GOT_STATUS_MODIFY &&
5287 ct->status != GOT_STATUS_MODE_CHANGE &&
5288 ct->status != GOT_STATUS_DELETE)
5289 continue;
5290 } else {
5291 if (ct->staged_status != GOT_STATUS_MODIFY &&
5292 ct->staged_status != GOT_STATUS_DELETE)
5293 continue;
5296 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5297 continue;
5299 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5300 if (err)
5301 return err;
5302 if (!path_matches)
5303 continue;
5305 err = got_path_basename(&ct_name, pe->path);
5306 if (err)
5307 return err;
5309 if (strcmp(te->name, ct_name) != 0) {
5310 free(ct_name);
5311 continue;
5313 free(ct_name);
5315 *ctp = ct;
5316 break;
5319 return err;
5322 static const struct got_error *
5323 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5324 const char *child_path, const char *path_base_tree,
5325 struct got_pathlist_head *commitable_paths,
5326 got_worktree_status_cb status_cb, void *status_arg,
5327 struct got_repository *repo)
5329 const struct got_error *err = NULL;
5330 struct got_tree_entry *new_te;
5331 char *subtree_path;
5332 struct got_object_id *id = NULL;
5333 int nentries;
5335 *new_tep = NULL;
5337 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5338 got_path_is_root_dir(path_base_tree) ? "" : "/",
5339 child_path) == -1)
5340 return got_error_from_errno("asprintf");
5342 new_te = calloc(1, sizeof(*new_te));
5343 if (new_te == NULL)
5344 return got_error_from_errno("calloc");
5345 new_te->mode = S_IFDIR;
5347 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5348 sizeof(new_te->name)) {
5349 err = got_error(GOT_ERR_NO_SPACE);
5350 goto done;
5352 err = write_tree(&id, &nentries, NULL, subtree_path,
5353 commitable_paths, status_cb, status_arg, repo);
5354 if (err) {
5355 free(new_te);
5356 goto done;
5358 memcpy(&new_te->id, id, sizeof(new_te->id));
5359 done:
5360 free(id);
5361 free(subtree_path);
5362 if (err == NULL)
5363 *new_tep = new_te;
5364 return err;
5367 static const struct got_error *
5368 write_tree(struct got_object_id **new_tree_id, int *nentries,
5369 struct got_tree_object *base_tree, const char *path_base_tree,
5370 struct got_pathlist_head *commitable_paths,
5371 got_worktree_status_cb status_cb, void *status_arg,
5372 struct got_repository *repo)
5374 const struct got_error *err = NULL;
5375 struct got_pathlist_head paths;
5376 struct got_tree_entry *te, *new_te = NULL;
5377 struct got_pathlist_entry *pe;
5379 TAILQ_INIT(&paths);
5380 *nentries = 0;
5382 /* Insert, and recurse into, newly added entries first. */
5383 TAILQ_FOREACH(pe, commitable_paths, entry) {
5384 struct got_commitable *ct = pe->data;
5385 char *child_path = NULL, *slash;
5387 if ((ct->status != GOT_STATUS_ADD &&
5388 ct->staged_status != GOT_STATUS_ADD) ||
5389 (ct->flags & GOT_COMMITABLE_ADDED))
5390 continue;
5392 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5393 strlen(path_base_tree)))
5394 continue;
5396 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5397 ct->in_repo_path);
5398 if (err)
5399 goto done;
5401 slash = strchr(child_path, '/');
5402 if (slash == NULL) {
5403 err = alloc_added_blob_tree_entry(&new_te, ct);
5404 if (err)
5405 goto done;
5406 err = report_ct_status(ct, status_cb, status_arg);
5407 if (err)
5408 goto done;
5409 ct->flags |= GOT_COMMITABLE_ADDED;
5410 err = insert_tree_entry(new_te, &paths);
5411 if (err)
5412 goto done;
5413 (*nentries)++;
5414 } else {
5415 *slash = '\0'; /* trim trailing path components */
5416 if (base_tree == NULL ||
5417 got_object_tree_find_entry(base_tree, child_path)
5418 == NULL) {
5419 err = make_subtree_for_added_blob(&new_te,
5420 child_path, path_base_tree,
5421 commitable_paths, status_cb, status_arg,
5422 repo);
5423 if (err)
5424 goto done;
5425 err = insert_tree_entry(new_te, &paths);
5426 if (err)
5427 goto done;
5428 (*nentries)++;
5433 if (base_tree) {
5434 int i, nbase_entries;
5435 /* Handle modified and deleted entries. */
5436 nbase_entries = got_object_tree_get_nentries(base_tree);
5437 for (i = 0; i < nbase_entries; i++) {
5438 struct got_commitable *ct = NULL;
5440 te = got_object_tree_get_entry(base_tree, i);
5441 if (got_object_tree_entry_is_submodule(te)) {
5442 /* Entry is a submodule; just copy it. */
5443 err = got_object_tree_entry_dup(&new_te, te);
5444 if (err)
5445 goto done;
5446 err = insert_tree_entry(new_te, &paths);
5447 if (err)
5448 goto done;
5449 (*nentries)++;
5450 continue;
5453 if (S_ISDIR(te->mode)) {
5454 int modified;
5455 err = got_object_tree_entry_dup(&new_te, te);
5456 if (err)
5457 goto done;
5458 err = match_modified_subtree(&modified, te,
5459 path_base_tree, commitable_paths);
5460 if (err)
5461 goto done;
5462 /* Avoid recursion into unmodified subtrees. */
5463 if (modified) {
5464 struct got_object_id *new_id;
5465 int nsubentries;
5466 err = write_subtree(&new_id,
5467 &nsubentries, te,
5468 path_base_tree, commitable_paths,
5469 status_cb, status_arg, repo);
5470 if (err)
5471 goto done;
5472 if (nsubentries == 0) {
5473 /* All entries were deleted. */
5474 free(new_id);
5475 continue;
5477 memcpy(&new_te->id, new_id,
5478 sizeof(new_te->id));
5479 free(new_id);
5481 err = insert_tree_entry(new_te, &paths);
5482 if (err)
5483 goto done;
5484 (*nentries)++;
5485 continue;
5488 err = match_deleted_or_modified_ct(&ct, te,
5489 path_base_tree, commitable_paths);
5490 if (err)
5491 goto done;
5492 if (ct) {
5493 /* NB: Deleted entries get dropped here. */
5494 if (ct->status == GOT_STATUS_MODIFY ||
5495 ct->status == GOT_STATUS_MODE_CHANGE ||
5496 ct->staged_status == GOT_STATUS_MODIFY) {
5497 err = alloc_modified_blob_tree_entry(
5498 &new_te, te, ct);
5499 if (err)
5500 goto done;
5501 err = insert_tree_entry(new_te, &paths);
5502 if (err)
5503 goto done;
5504 (*nentries)++;
5506 err = report_ct_status(ct, status_cb,
5507 status_arg);
5508 if (err)
5509 goto done;
5510 } else {
5511 /* Entry is unchanged; just copy it. */
5512 err = got_object_tree_entry_dup(&new_te, te);
5513 if (err)
5514 goto done;
5515 err = insert_tree_entry(new_te, &paths);
5516 if (err)
5517 goto done;
5518 (*nentries)++;
5523 /* Write new list of entries; deleted entries have been dropped. */
5524 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5525 done:
5526 got_pathlist_free(&paths);
5527 return err;
5530 static const struct got_error *
5531 update_fileindex_after_commit(struct got_worktree *worktree,
5532 struct got_pathlist_head *commitable_paths,
5533 struct got_object_id *new_base_commit_id,
5534 struct got_fileindex *fileindex, int have_staged_files)
5536 const struct got_error *err = NULL;
5537 struct got_pathlist_entry *pe;
5538 char *relpath = NULL;
5540 TAILQ_FOREACH(pe, commitable_paths, entry) {
5541 struct got_fileindex_entry *ie;
5542 struct got_commitable *ct = pe->data;
5544 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5546 err = got_path_skip_common_ancestor(&relpath,
5547 worktree->root_path, ct->ondisk_path);
5548 if (err)
5549 goto done;
5551 if (ie) {
5552 if (ct->status == GOT_STATUS_DELETE ||
5553 ct->staged_status == GOT_STATUS_DELETE) {
5554 got_fileindex_entry_remove(fileindex, ie);
5555 } else if (ct->staged_status == GOT_STATUS_ADD ||
5556 ct->staged_status == GOT_STATUS_MODIFY) {
5557 got_fileindex_entry_stage_set(ie,
5558 GOT_FILEIDX_STAGE_NONE);
5559 got_fileindex_entry_staged_filetype_set(ie, 0);
5561 err = got_fileindex_entry_update(ie,
5562 worktree->root_fd, relpath,
5563 ct->staged_blob_id->sha1,
5564 new_base_commit_id->sha1,
5565 !have_staged_files);
5566 } else
5567 err = got_fileindex_entry_update(ie,
5568 worktree->root_fd, relpath,
5569 ct->blob_id->sha1,
5570 new_base_commit_id->sha1,
5571 !have_staged_files);
5572 } else {
5573 err = got_fileindex_entry_alloc(&ie, pe->path);
5574 if (err)
5575 goto done;
5576 err = got_fileindex_entry_update(ie,
5577 worktree->root_fd, relpath, ct->blob_id->sha1,
5578 new_base_commit_id->sha1, 1);
5579 if (err) {
5580 got_fileindex_entry_free(ie);
5581 goto done;
5583 err = got_fileindex_entry_add(fileindex, ie);
5584 if (err) {
5585 got_fileindex_entry_free(ie);
5586 goto done;
5589 free(relpath);
5590 relpath = NULL;
5592 done:
5593 free(relpath);
5594 return err;
5598 static const struct got_error *
5599 check_out_of_date(const char *in_repo_path, unsigned char status,
5600 unsigned char staged_status, struct got_object_id *base_blob_id,
5601 struct got_object_id *base_commit_id,
5602 struct got_object_id *head_commit_id, struct got_repository *repo,
5603 int ood_errcode)
5605 const struct got_error *err = NULL;
5606 struct got_commit_object *commit = NULL;
5607 struct got_object_id *id = NULL;
5609 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5610 /* Trivial case: base commit == head commit */
5611 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5612 return NULL;
5614 * Ensure file content which local changes were based
5615 * on matches file content in the branch head.
5617 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5618 if (err)
5619 goto done;
5620 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5621 if (err) {
5622 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 err = got_error(ood_errcode);
5624 goto done;
5625 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 err = got_error(ood_errcode);
5627 } else {
5628 /* Require that added files don't exist in the branch head. */
5629 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5630 if (err)
5631 goto done;
5632 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5633 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5634 goto done;
5635 err = id ? got_error(ood_errcode) : NULL;
5637 done:
5638 free(id);
5639 if (commit)
5640 got_object_commit_close(commit);
5641 return err;
5644 static const struct got_error *
5645 commit_worktree(struct got_object_id **new_commit_id,
5646 struct got_pathlist_head *commitable_paths,
5647 struct got_object_id *head_commit_id,
5648 struct got_object_id *parent_id2,
5649 struct got_worktree *worktree,
5650 const char *author, const char *committer,
5651 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5652 got_worktree_status_cb status_cb, void *status_arg,
5653 struct got_repository *repo)
5655 const struct got_error *err = NULL, *unlockerr = NULL;
5656 struct got_pathlist_entry *pe;
5657 const char *head_ref_name = NULL;
5658 struct got_commit_object *head_commit = NULL;
5659 struct got_reference *head_ref2 = NULL;
5660 struct got_object_id *head_commit_id2 = NULL;
5661 struct got_tree_object *head_tree = NULL;
5662 struct got_object_id *new_tree_id = NULL;
5663 int nentries, nparents = 0;
5664 struct got_object_id_queue parent_ids;
5665 struct got_object_qid *pid = NULL;
5666 char *logmsg = NULL;
5668 *new_commit_id = NULL;
5670 STAILQ_INIT(&parent_ids);
5672 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5673 if (err)
5674 goto done;
5676 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5677 if (err)
5678 goto done;
5680 if (commit_msg_cb != NULL) {
5681 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5682 if (err)
5683 goto done;
5686 if (logmsg == NULL || strlen(logmsg) == 0) {
5687 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5688 goto done;
5691 /* Create blobs from added and modified files and record their IDs. */
5692 TAILQ_FOREACH(pe, commitable_paths, entry) {
5693 struct got_commitable *ct = pe->data;
5694 char *ondisk_path;
5696 /* Blobs for staged files already exist. */
5697 if (ct->staged_status == GOT_STATUS_ADD ||
5698 ct->staged_status == GOT_STATUS_MODIFY)
5699 continue;
5701 if (ct->status != GOT_STATUS_ADD &&
5702 ct->status != GOT_STATUS_MODIFY &&
5703 ct->status != GOT_STATUS_MODE_CHANGE)
5704 continue;
5706 if (asprintf(&ondisk_path, "%s/%s",
5707 worktree->root_path, pe->path) == -1) {
5708 err = got_error_from_errno("asprintf");
5709 goto done;
5711 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5712 free(ondisk_path);
5713 if (err)
5714 goto done;
5717 /* Recursively write new tree objects. */
5718 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5719 commitable_paths, status_cb, status_arg, repo);
5720 if (err)
5721 goto done;
5723 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5724 if (err)
5725 goto done;
5726 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5727 nparents++;
5728 if (parent_id2) {
5729 err = got_object_qid_alloc(&pid, parent_id2);
5730 if (err)
5731 goto done;
5732 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5733 nparents++;
5735 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5736 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5737 if (logmsg != NULL)
5738 free(logmsg);
5739 if (err)
5740 goto done;
5742 /* Check if a concurrent commit to our branch has occurred. */
5743 head_ref_name = got_worktree_get_head_ref_name(worktree);
5744 if (head_ref_name == NULL) {
5745 err = got_error_from_errno("got_worktree_get_head_ref_name");
5746 goto done;
5748 /* Lock the reference here to prevent concurrent modification. */
5749 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5750 if (err)
5751 goto done;
5752 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5753 if (err)
5754 goto done;
5755 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5756 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5757 goto done;
5759 /* Update branch head in repository. */
5760 err = got_ref_change_ref(head_ref2, *new_commit_id);
5761 if (err)
5762 goto done;
5763 err = got_ref_write(head_ref2, repo);
5764 if (err)
5765 goto done;
5767 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5768 if (err)
5769 goto done;
5771 err = ref_base_commit(worktree, repo);
5772 if (err)
5773 goto done;
5774 done:
5775 got_object_id_queue_free(&parent_ids);
5776 if (head_tree)
5777 got_object_tree_close(head_tree);
5778 if (head_commit)
5779 got_object_commit_close(head_commit);
5780 free(head_commit_id2);
5781 if (head_ref2) {
5782 unlockerr = got_ref_unlock(head_ref2);
5783 if (unlockerr && err == NULL)
5784 err = unlockerr;
5785 got_ref_close(head_ref2);
5787 return err;
5790 static const struct got_error *
5791 check_path_is_commitable(const char *path,
5792 struct got_pathlist_head *commitable_paths)
5794 struct got_pathlist_entry *cpe = NULL;
5795 size_t path_len = strlen(path);
5797 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5798 struct got_commitable *ct = cpe->data;
5799 const char *ct_path = ct->path;
5801 while (ct_path[0] == '/')
5802 ct_path++;
5804 if (strcmp(path, ct_path) == 0 ||
5805 got_path_is_child(ct_path, path, path_len))
5806 break;
5809 if (cpe == NULL)
5810 return got_error_path(path, GOT_ERR_BAD_PATH);
5812 return NULL;
5815 static const struct got_error *
5816 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5818 int *have_staged_files = arg;
5820 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5821 *have_staged_files = 1;
5822 return got_error(GOT_ERR_CANCELLED);
5825 return NULL;
5828 static const struct got_error *
5829 check_non_staged_files(struct got_fileindex *fileindex,
5830 struct got_pathlist_head *paths)
5832 struct got_pathlist_entry *pe;
5833 struct got_fileindex_entry *ie;
5835 TAILQ_FOREACH(pe, paths, entry) {
5836 if (pe->path[0] == '\0')
5837 continue;
5838 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5839 if (ie == NULL)
5840 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5841 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5842 return got_error_path(pe->path,
5843 GOT_ERR_FILE_NOT_STAGED);
5846 return NULL;
5849 const struct got_error *
5850 got_worktree_commit(struct got_object_id **new_commit_id,
5851 struct got_worktree *worktree, struct got_pathlist_head *paths,
5852 const char *author, const char *committer, int allow_bad_symlinks,
5853 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5854 got_worktree_status_cb status_cb, void *status_arg,
5855 struct got_repository *repo)
5857 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5858 struct got_fileindex *fileindex = NULL;
5859 char *fileindex_path = NULL;
5860 struct got_pathlist_head commitable_paths;
5861 struct collect_commitables_arg cc_arg;
5862 struct got_pathlist_entry *pe;
5863 struct got_reference *head_ref = NULL;
5864 struct got_object_id *head_commit_id = NULL;
5865 int have_staged_files = 0;
5867 *new_commit_id = NULL;
5869 TAILQ_INIT(&commitable_paths);
5871 err = lock_worktree(worktree, LOCK_EX);
5872 if (err)
5873 goto done;
5875 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5876 if (err)
5877 goto done;
5879 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5880 if (err)
5881 goto done;
5883 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5884 if (err)
5885 goto done;
5887 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5888 &have_staged_files);
5889 if (err && err->code != GOT_ERR_CANCELLED)
5890 goto done;
5891 if (have_staged_files) {
5892 err = check_non_staged_files(fileindex, paths);
5893 if (err)
5894 goto done;
5897 cc_arg.commitable_paths = &commitable_paths;
5898 cc_arg.worktree = worktree;
5899 cc_arg.fileindex = fileindex;
5900 cc_arg.repo = repo;
5901 cc_arg.have_staged_files = have_staged_files;
5902 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5903 TAILQ_FOREACH(pe, paths, entry) {
5904 err = worktree_status(worktree, pe->path, fileindex, repo,
5905 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5906 if (err)
5907 goto done;
5910 if (TAILQ_EMPTY(&commitable_paths)) {
5911 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5912 goto done;
5915 TAILQ_FOREACH(pe, paths, entry) {
5916 err = check_path_is_commitable(pe->path, &commitable_paths);
5917 if (err)
5918 goto done;
5921 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5922 struct got_commitable *ct = pe->data;
5923 const char *ct_path = ct->in_repo_path;
5925 while (ct_path[0] == '/')
5926 ct_path++;
5927 err = check_out_of_date(ct_path, ct->status,
5928 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5929 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5930 if (err)
5931 goto done;
5935 err = commit_worktree(new_commit_id, &commitable_paths,
5936 head_commit_id, NULL, worktree, author, committer,
5937 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5938 if (err)
5939 goto done;
5941 err = update_fileindex_after_commit(worktree, &commitable_paths,
5942 *new_commit_id, fileindex, have_staged_files);
5943 sync_err = sync_fileindex(fileindex, fileindex_path);
5944 if (sync_err && err == NULL)
5945 err = sync_err;
5946 done:
5947 if (fileindex)
5948 got_fileindex_free(fileindex);
5949 free(fileindex_path);
5950 unlockerr = lock_worktree(worktree, LOCK_SH);
5951 if (unlockerr && err == NULL)
5952 err = unlockerr;
5953 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5954 struct got_commitable *ct = pe->data;
5955 free_commitable(ct);
5957 got_pathlist_free(&commitable_paths);
5958 return err;
5961 const char *
5962 got_commitable_get_path(struct got_commitable *ct)
5964 return ct->path;
5967 unsigned int
5968 got_commitable_get_status(struct got_commitable *ct)
5970 return ct->status;
5973 struct check_rebase_ok_arg {
5974 struct got_worktree *worktree;
5975 struct got_repository *repo;
5978 static const struct got_error *
5979 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5981 const struct got_error *err = NULL;
5982 struct check_rebase_ok_arg *a = arg;
5983 unsigned char status;
5984 struct stat sb;
5985 char *ondisk_path;
5987 /* Reject rebase of a work tree with mixed base commits. */
5988 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5989 SHA1_DIGEST_LENGTH))
5990 return got_error(GOT_ERR_MIXED_COMMITS);
5992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5993 == -1)
5994 return got_error_from_errno("asprintf");
5996 /* Reject rebase of a work tree with modified or staged files. */
5997 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5998 free(ondisk_path);
5999 if (err)
6000 return err;
6002 if (status != GOT_STATUS_NO_CHANGE)
6003 return got_error(GOT_ERR_MODIFIED);
6004 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6005 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6007 return NULL;
6010 const struct got_error *
6011 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6012 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6013 struct got_worktree *worktree, struct got_reference *branch,
6014 struct got_repository *repo)
6016 const struct got_error *err = NULL;
6017 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6018 char *branch_ref_name = NULL;
6019 char *fileindex_path = NULL;
6020 struct check_rebase_ok_arg ok_arg;
6021 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6022 struct got_object_id *wt_branch_tip = NULL;
6024 *new_base_branch_ref = NULL;
6025 *tmp_branch = NULL;
6026 *fileindex = NULL;
6028 err = lock_worktree(worktree, LOCK_EX);
6029 if (err)
6030 return err;
6032 err = open_fileindex(fileindex, &fileindex_path, worktree);
6033 if (err)
6034 goto done;
6036 ok_arg.worktree = worktree;
6037 ok_arg.repo = repo;
6038 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6039 &ok_arg);
6040 if (err)
6041 goto done;
6043 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 if (err)
6045 goto done;
6047 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6048 if (err)
6049 goto done;
6051 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6052 if (err)
6053 goto done;
6055 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6056 0);
6057 if (err)
6058 goto done;
6060 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6061 if (err)
6062 goto done;
6063 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6064 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6065 goto done;
6068 err = got_ref_alloc_symref(new_base_branch_ref,
6069 new_base_branch_ref_name, wt_branch);
6070 if (err)
6071 goto done;
6072 err = got_ref_write(*new_base_branch_ref, repo);
6073 if (err)
6074 goto done;
6076 /* TODO Lock original branch's ref while rebasing? */
6078 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6079 if (err)
6080 goto done;
6082 err = got_ref_write(branch_ref, repo);
6083 if (err)
6084 goto done;
6086 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6087 worktree->base_commit_id);
6088 if (err)
6089 goto done;
6090 err = got_ref_write(*tmp_branch, repo);
6091 if (err)
6092 goto done;
6094 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6095 if (err)
6096 goto done;
6097 done:
6098 free(fileindex_path);
6099 free(tmp_branch_name);
6100 free(new_base_branch_ref_name);
6101 free(branch_ref_name);
6102 if (branch_ref)
6103 got_ref_close(branch_ref);
6104 if (wt_branch)
6105 got_ref_close(wt_branch);
6106 free(wt_branch_tip);
6107 if (err) {
6108 if (*new_base_branch_ref) {
6109 got_ref_close(*new_base_branch_ref);
6110 *new_base_branch_ref = NULL;
6112 if (*tmp_branch) {
6113 got_ref_close(*tmp_branch);
6114 *tmp_branch = NULL;
6116 if (*fileindex) {
6117 got_fileindex_free(*fileindex);
6118 *fileindex = NULL;
6120 lock_worktree(worktree, LOCK_SH);
6122 return err;
6125 const struct got_error *
6126 got_worktree_rebase_continue(struct got_object_id **commit_id,
6127 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6128 struct got_reference **branch, struct got_fileindex **fileindex,
6129 struct got_worktree *worktree, struct got_repository *repo)
6131 const struct got_error *err;
6132 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6133 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6134 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6135 char *fileindex_path = NULL;
6136 int have_staged_files = 0;
6138 *commit_id = NULL;
6139 *new_base_branch = NULL;
6140 *tmp_branch = NULL;
6141 *branch = NULL;
6142 *fileindex = NULL;
6144 err = lock_worktree(worktree, LOCK_EX);
6145 if (err)
6146 return err;
6148 err = open_fileindex(fileindex, &fileindex_path, worktree);
6149 if (err)
6150 goto done;
6152 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6153 &have_staged_files);
6154 if (err && err->code != GOT_ERR_CANCELLED)
6155 goto done;
6156 if (have_staged_files) {
6157 err = got_error(GOT_ERR_STAGED_PATHS);
6158 goto done;
6161 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6162 if (err)
6163 goto done;
6165 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6166 if (err)
6167 goto done;
6169 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6170 if (err)
6171 goto done;
6173 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6174 if (err)
6175 goto done;
6177 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6178 if (err)
6179 goto done;
6181 err = got_ref_open(branch, repo,
6182 got_ref_get_symref_target(branch_ref), 0);
6183 if (err)
6184 goto done;
6186 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6187 if (err)
6188 goto done;
6190 err = got_ref_resolve(commit_id, repo, commit_ref);
6191 if (err)
6192 goto done;
6194 err = got_ref_open(new_base_branch, repo,
6195 new_base_branch_ref_name, 0);
6196 if (err)
6197 goto done;
6199 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6200 if (err)
6201 goto done;
6202 done:
6203 free(commit_ref_name);
6204 free(branch_ref_name);
6205 free(fileindex_path);
6206 if (commit_ref)
6207 got_ref_close(commit_ref);
6208 if (branch_ref)
6209 got_ref_close(branch_ref);
6210 if (err) {
6211 free(*commit_id);
6212 *commit_id = NULL;
6213 if (*tmp_branch) {
6214 got_ref_close(*tmp_branch);
6215 *tmp_branch = NULL;
6217 if (*new_base_branch) {
6218 got_ref_close(*new_base_branch);
6219 *new_base_branch = NULL;
6221 if (*branch) {
6222 got_ref_close(*branch);
6223 *branch = NULL;
6225 if (*fileindex) {
6226 got_fileindex_free(*fileindex);
6227 *fileindex = NULL;
6229 lock_worktree(worktree, LOCK_SH);
6231 return err;
6234 const struct got_error *
6235 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6237 const struct got_error *err;
6238 char *tmp_branch_name = NULL;
6240 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6241 if (err)
6242 return err;
6244 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6245 free(tmp_branch_name);
6246 return NULL;
6249 static const struct got_error *
6250 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6251 char **logmsg, void *arg)
6253 *logmsg = arg;
6254 return NULL;
6257 static const struct got_error *
6258 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6259 const char *path, struct got_object_id *blob_id,
6260 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6261 int dirfd, const char *de_name)
6263 return NULL;
6266 struct collect_merged_paths_arg {
6267 got_worktree_checkout_cb progress_cb;
6268 void *progress_arg;
6269 struct got_pathlist_head *merged_paths;
6272 static const struct got_error *
6273 collect_merged_paths(void *arg, unsigned char status, const char *path)
6275 const struct got_error *err;
6276 struct collect_merged_paths_arg *a = arg;
6277 char *p;
6278 struct got_pathlist_entry *new;
6280 err = (*a->progress_cb)(a->progress_arg, status, path);
6281 if (err)
6282 return err;
6284 if (status != GOT_STATUS_MERGE &&
6285 status != GOT_STATUS_ADD &&
6286 status != GOT_STATUS_DELETE &&
6287 status != GOT_STATUS_CONFLICT)
6288 return NULL;
6290 p = strdup(path);
6291 if (p == NULL)
6292 return got_error_from_errno("strdup");
6294 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6295 if (err || new == NULL)
6296 free(p);
6297 return err;
6300 void
6301 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6303 struct got_pathlist_entry *pe;
6305 TAILQ_FOREACH(pe, merged_paths, entry)
6306 free((char *)pe->path);
6308 got_pathlist_free(merged_paths);
6311 static const struct got_error *
6312 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6313 int is_rebase, struct got_repository *repo)
6315 const struct got_error *err;
6316 struct got_reference *commit_ref = NULL;
6318 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6319 if (err) {
6320 if (err->code != GOT_ERR_NOT_REF)
6321 goto done;
6322 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6323 if (err)
6324 goto done;
6325 err = got_ref_write(commit_ref, repo);
6326 if (err)
6327 goto done;
6328 } else if (is_rebase) {
6329 struct got_object_id *stored_id;
6330 int cmp;
6332 err = got_ref_resolve(&stored_id, repo, commit_ref);
6333 if (err)
6334 goto done;
6335 cmp = got_object_id_cmp(commit_id, stored_id);
6336 free(stored_id);
6337 if (cmp != 0) {
6338 err = got_error(GOT_ERR_REBASE_COMMITID);
6339 goto done;
6342 done:
6343 if (commit_ref)
6344 got_ref_close(commit_ref);
6345 return err;
6348 static const struct got_error *
6349 rebase_merge_files(struct got_pathlist_head *merged_paths,
6350 const char *commit_ref_name, struct got_worktree *worktree,
6351 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6352 struct got_object_id *commit_id, struct got_repository *repo,
6353 got_worktree_checkout_cb progress_cb, void *progress_arg,
6354 got_cancel_cb cancel_cb, void *cancel_arg)
6356 const struct got_error *err;
6357 struct got_reference *commit_ref = NULL;
6358 struct collect_merged_paths_arg cmp_arg;
6359 char *fileindex_path;
6361 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6363 err = get_fileindex_path(&fileindex_path, worktree);
6364 if (err)
6365 return err;
6367 cmp_arg.progress_cb = progress_cb;
6368 cmp_arg.progress_arg = progress_arg;
6369 cmp_arg.merged_paths = merged_paths;
6370 err = merge_files(worktree, fileindex, fileindex_path,
6371 parent_commit_id, commit_id, repo, collect_merged_paths,
6372 &cmp_arg, cancel_cb, cancel_arg);
6373 if (commit_ref)
6374 got_ref_close(commit_ref);
6375 return err;
6378 const struct got_error *
6379 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6380 struct got_worktree *worktree, struct got_fileindex *fileindex,
6381 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6382 struct got_repository *repo,
6383 got_worktree_checkout_cb progress_cb, void *progress_arg,
6384 got_cancel_cb cancel_cb, void *cancel_arg)
6386 const struct got_error *err;
6387 char *commit_ref_name;
6389 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6390 if (err)
6391 return err;
6393 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6394 if (err)
6395 goto done;
6397 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6398 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6399 progress_arg, cancel_cb, cancel_arg);
6400 done:
6401 free(commit_ref_name);
6402 return err;
6405 const struct got_error *
6406 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6407 struct got_worktree *worktree, struct got_fileindex *fileindex,
6408 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6409 struct got_repository *repo,
6410 got_worktree_checkout_cb progress_cb, void *progress_arg,
6411 got_cancel_cb cancel_cb, void *cancel_arg)
6413 const struct got_error *err;
6414 char *commit_ref_name;
6416 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6417 if (err)
6418 return err;
6420 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6421 if (err)
6422 goto done;
6424 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6425 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6426 progress_arg, cancel_cb, cancel_arg);
6427 done:
6428 free(commit_ref_name);
6429 return err;
6432 static const struct got_error *
6433 rebase_commit(struct got_object_id **new_commit_id,
6434 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6435 struct got_worktree *worktree, struct got_fileindex *fileindex,
6436 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6437 const char *new_logmsg, struct got_repository *repo)
6439 const struct got_error *err, *sync_err;
6440 struct got_pathlist_head commitable_paths;
6441 struct collect_commitables_arg cc_arg;
6442 char *fileindex_path = NULL;
6443 struct got_reference *head_ref = NULL;
6444 struct got_object_id *head_commit_id = NULL;
6445 char *logmsg = NULL;
6447 TAILQ_INIT(&commitable_paths);
6448 *new_commit_id = NULL;
6450 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6452 err = get_fileindex_path(&fileindex_path, worktree);
6453 if (err)
6454 return err;
6456 cc_arg.commitable_paths = &commitable_paths;
6457 cc_arg.worktree = worktree;
6458 cc_arg.repo = repo;
6459 cc_arg.have_staged_files = 0;
6461 * If possible get the status of individual files directly to
6462 * avoid crawling the entire work tree once per rebased commit.
6464 * Ideally, merged_paths would contain a list of commitables
6465 * we could use so we could skip worktree_status() entirely.
6466 * However, we would then need carefully keep track of cumulative
6467 * effects of operations such as file additions and deletions
6468 * in 'got histedit -f' (folding multiple commits into one),
6469 * and this extra complexity is not really worth it.
6471 if (merged_paths) {
6472 struct got_pathlist_entry *pe;
6473 TAILQ_FOREACH(pe, merged_paths, entry) {
6474 err = worktree_status(worktree, pe->path, fileindex,
6475 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6476 0);
6477 if (err)
6478 goto done;
6480 } else {
6481 err = worktree_status(worktree, "", fileindex, repo,
6482 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6483 if (err)
6484 goto done;
6487 if (TAILQ_EMPTY(&commitable_paths)) {
6488 /* No-op change; commit will be elided. */
6489 err = got_ref_delete(commit_ref, repo);
6490 if (err)
6491 goto done;
6492 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6493 goto done;
6496 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6497 if (err)
6498 goto done;
6500 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6501 if (err)
6502 goto done;
6504 if (new_logmsg) {
6505 logmsg = strdup(new_logmsg);
6506 if (logmsg == NULL) {
6507 err = got_error_from_errno("strdup");
6508 goto done;
6510 } else {
6511 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6512 if (err)
6513 goto done;
6516 /* NB: commit_worktree will call free(logmsg) */
6517 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6518 NULL, worktree, got_object_commit_get_author(orig_commit),
6519 got_object_commit_get_committer(orig_commit),
6520 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6521 if (err)
6522 goto done;
6524 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6525 if (err)
6526 goto done;
6528 err = got_ref_delete(commit_ref, repo);
6529 if (err)
6530 goto done;
6532 err = update_fileindex_after_commit(worktree, &commitable_paths,
6533 *new_commit_id, fileindex, 0);
6534 sync_err = sync_fileindex(fileindex, fileindex_path);
6535 if (sync_err && err == NULL)
6536 err = sync_err;
6537 done:
6538 free(fileindex_path);
6539 free(head_commit_id);
6540 if (head_ref)
6541 got_ref_close(head_ref);
6542 if (err) {
6543 free(*new_commit_id);
6544 *new_commit_id = NULL;
6546 return err;
6549 const struct got_error *
6550 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6551 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6552 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6553 struct got_commit_object *orig_commit,
6554 struct got_object_id *orig_commit_id, struct got_repository *repo)
6556 const struct got_error *err;
6557 char *commit_ref_name;
6558 struct got_reference *commit_ref = NULL;
6559 struct got_object_id *commit_id = NULL;
6561 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6562 if (err)
6563 return err;
6565 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6566 if (err)
6567 goto done;
6568 err = got_ref_resolve(&commit_id, repo, commit_ref);
6569 if (err)
6570 goto done;
6571 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6572 err = got_error(GOT_ERR_REBASE_COMMITID);
6573 goto done;
6576 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6577 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6578 done:
6579 if (commit_ref)
6580 got_ref_close(commit_ref);
6581 free(commit_ref_name);
6582 free(commit_id);
6583 return err;
6586 const struct got_error *
6587 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6588 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6589 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6590 struct got_commit_object *orig_commit,
6591 struct got_object_id *orig_commit_id, const char *new_logmsg,
6592 struct got_repository *repo)
6594 const struct got_error *err;
6595 char *commit_ref_name;
6596 struct got_reference *commit_ref = NULL;
6598 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6599 if (err)
6600 return err;
6602 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6603 if (err)
6604 goto done;
6606 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6607 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6608 done:
6609 if (commit_ref)
6610 got_ref_close(commit_ref);
6611 free(commit_ref_name);
6612 return err;
6615 const struct got_error *
6616 got_worktree_rebase_postpone(struct got_worktree *worktree,
6617 struct got_fileindex *fileindex)
6619 if (fileindex)
6620 got_fileindex_free(fileindex);
6621 return lock_worktree(worktree, LOCK_SH);
6624 static const struct got_error *
6625 delete_ref(const char *name, struct got_repository *repo)
6627 const struct got_error *err;
6628 struct got_reference *ref;
6630 err = got_ref_open(&ref, repo, name, 0);
6631 if (err) {
6632 if (err->code == GOT_ERR_NOT_REF)
6633 return NULL;
6634 return err;
6637 err = got_ref_delete(ref, repo);
6638 got_ref_close(ref);
6639 return err;
6642 static const struct got_error *
6643 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6645 const struct got_error *err;
6646 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6647 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6649 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6650 if (err)
6651 goto done;
6652 err = delete_ref(tmp_branch_name, repo);
6653 if (err)
6654 goto done;
6656 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6657 if (err)
6658 goto done;
6659 err = delete_ref(new_base_branch_ref_name, repo);
6660 if (err)
6661 goto done;
6663 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6664 if (err)
6665 goto done;
6666 err = delete_ref(branch_ref_name, repo);
6667 if (err)
6668 goto done;
6670 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6671 if (err)
6672 goto done;
6673 err = delete_ref(commit_ref_name, repo);
6674 if (err)
6675 goto done;
6677 done:
6678 free(tmp_branch_name);
6679 free(new_base_branch_ref_name);
6680 free(branch_ref_name);
6681 free(commit_ref_name);
6682 return err;
6685 static const struct got_error *
6686 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6687 struct got_object_id *new_commit_id, struct got_repository *repo)
6689 const struct got_error *err;
6690 struct got_reference *ref = NULL;
6691 struct got_object_id *old_commit_id = NULL;
6692 const char *branch_name = NULL;
6693 char *new_id_str = NULL;
6694 char *refname = NULL;
6696 branch_name = got_ref_get_name(branch);
6697 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6698 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6699 branch_name += 11;
6701 err = got_object_id_str(&new_id_str, new_commit_id);
6702 if (err)
6703 return err;
6705 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6706 new_id_str) == -1) {
6707 err = got_error_from_errno("asprintf");
6708 goto done;
6711 err = got_ref_resolve(&old_commit_id, repo, branch);
6712 if (err)
6713 goto done;
6715 err = got_ref_alloc(&ref, refname, old_commit_id);
6716 if (err)
6717 goto done;
6719 err = got_ref_write(ref, repo);
6720 done:
6721 free(new_id_str);
6722 free(refname);
6723 free(old_commit_id);
6724 if (ref)
6725 got_ref_close(ref);
6726 return err;
6729 const struct got_error *
6730 got_worktree_rebase_complete(struct got_worktree *worktree,
6731 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6732 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6733 struct got_repository *repo, int create_backup)
6735 const struct got_error *err, *unlockerr, *sync_err;
6736 struct got_object_id *new_head_commit_id = NULL;
6737 char *fileindex_path = NULL;
6739 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6740 if (err)
6741 return err;
6743 if (create_backup) {
6744 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6745 rebased_branch, new_head_commit_id, repo);
6746 if (err)
6747 goto done;
6750 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6751 if (err)
6752 goto done;
6754 err = got_ref_write(rebased_branch, repo);
6755 if (err)
6756 goto done;
6758 err = got_worktree_set_head_ref(worktree, rebased_branch);
6759 if (err)
6760 goto done;
6762 err = delete_rebase_refs(worktree, repo);
6763 if (err)
6764 goto done;
6766 err = get_fileindex_path(&fileindex_path, worktree);
6767 if (err)
6768 goto done;
6769 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6770 sync_err = sync_fileindex(fileindex, fileindex_path);
6771 if (sync_err && err == NULL)
6772 err = sync_err;
6773 done:
6774 got_fileindex_free(fileindex);
6775 free(fileindex_path);
6776 free(new_head_commit_id);
6777 unlockerr = lock_worktree(worktree, LOCK_SH);
6778 if (unlockerr && err == NULL)
6779 err = unlockerr;
6780 return err;
6783 const struct got_error *
6784 got_worktree_rebase_abort(struct got_worktree *worktree,
6785 struct got_fileindex *fileindex, struct got_repository *repo,
6786 struct got_reference *new_base_branch,
6787 got_worktree_checkout_cb progress_cb, void *progress_arg)
6789 const struct got_error *err, *unlockerr, *sync_err;
6790 struct got_reference *resolved = NULL;
6791 struct got_object_id *commit_id = NULL;
6792 struct got_commit_object *commit = NULL;
6793 char *fileindex_path = NULL;
6794 struct revert_file_args rfa;
6795 struct got_object_id *tree_id = NULL;
6797 err = lock_worktree(worktree, LOCK_EX);
6798 if (err)
6799 return err;
6801 err = got_object_open_as_commit(&commit, repo,
6802 worktree->base_commit_id);
6803 if (err)
6804 goto done;
6806 err = got_ref_open(&resolved, repo,
6807 got_ref_get_symref_target(new_base_branch), 0);
6808 if (err)
6809 goto done;
6811 err = got_worktree_set_head_ref(worktree, resolved);
6812 if (err)
6813 goto done;
6816 * XXX commits to the base branch could have happened while
6817 * we were busy rebasing; should we store the original commit ID
6818 * when rebase begins and read it back here?
6820 err = got_ref_resolve(&commit_id, repo, resolved);
6821 if (err)
6822 goto done;
6824 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6825 if (err)
6826 goto done;
6828 err = got_object_id_by_path(&tree_id, repo, commit,
6829 worktree->path_prefix);
6830 if (err)
6831 goto done;
6833 err = delete_rebase_refs(worktree, repo);
6834 if (err)
6835 goto done;
6837 err = get_fileindex_path(&fileindex_path, worktree);
6838 if (err)
6839 goto done;
6841 rfa.worktree = worktree;
6842 rfa.fileindex = fileindex;
6843 rfa.progress_cb = progress_cb;
6844 rfa.progress_arg = progress_arg;
6845 rfa.patch_cb = NULL;
6846 rfa.patch_arg = NULL;
6847 rfa.repo = repo;
6848 rfa.unlink_added_files = 0;
6849 err = worktree_status(worktree, "", fileindex, repo,
6850 revert_file, &rfa, NULL, NULL, 1, 0);
6851 if (err)
6852 goto sync;
6854 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6855 repo, progress_cb, progress_arg, NULL, NULL);
6856 sync:
6857 sync_err = sync_fileindex(fileindex, fileindex_path);
6858 if (sync_err && err == NULL)
6859 err = sync_err;
6860 done:
6861 got_ref_close(resolved);
6862 free(tree_id);
6863 free(commit_id);
6864 if (commit)
6865 got_object_commit_close(commit);
6866 if (fileindex)
6867 got_fileindex_free(fileindex);
6868 free(fileindex_path);
6870 unlockerr = lock_worktree(worktree, LOCK_SH);
6871 if (unlockerr && err == NULL)
6872 err = unlockerr;
6873 return err;
6876 const struct got_error *
6877 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6878 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6879 struct got_fileindex **fileindex, struct got_worktree *worktree,
6880 struct got_repository *repo)
6882 const struct got_error *err = NULL;
6883 char *tmp_branch_name = NULL;
6884 char *branch_ref_name = NULL;
6885 char *base_commit_ref_name = NULL;
6886 char *fileindex_path = NULL;
6887 struct check_rebase_ok_arg ok_arg;
6888 struct got_reference *wt_branch = NULL;
6889 struct got_reference *base_commit_ref = NULL;
6891 *tmp_branch = NULL;
6892 *branch_ref = NULL;
6893 *base_commit_id = NULL;
6894 *fileindex = NULL;
6896 err = lock_worktree(worktree, LOCK_EX);
6897 if (err)
6898 return err;
6900 err = open_fileindex(fileindex, &fileindex_path, worktree);
6901 if (err)
6902 goto done;
6904 ok_arg.worktree = worktree;
6905 ok_arg.repo = repo;
6906 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6907 &ok_arg);
6908 if (err)
6909 goto done;
6911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 if (err)
6913 goto done;
6915 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 if (err)
6917 goto done;
6919 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6920 worktree);
6921 if (err)
6922 goto done;
6924 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6925 0);
6926 if (err)
6927 goto done;
6929 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6930 if (err)
6931 goto done;
6933 err = got_ref_write(*branch_ref, repo);
6934 if (err)
6935 goto done;
6937 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6938 worktree->base_commit_id);
6939 if (err)
6940 goto done;
6941 err = got_ref_write(base_commit_ref, repo);
6942 if (err)
6943 goto done;
6944 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6945 if (*base_commit_id == NULL) {
6946 err = got_error_from_errno("got_object_id_dup");
6947 goto done;
6950 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6951 worktree->base_commit_id);
6952 if (err)
6953 goto done;
6954 err = got_ref_write(*tmp_branch, repo);
6955 if (err)
6956 goto done;
6958 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6959 if (err)
6960 goto done;
6961 done:
6962 free(fileindex_path);
6963 free(tmp_branch_name);
6964 free(branch_ref_name);
6965 free(base_commit_ref_name);
6966 if (wt_branch)
6967 got_ref_close(wt_branch);
6968 if (err) {
6969 if (*branch_ref) {
6970 got_ref_close(*branch_ref);
6971 *branch_ref = NULL;
6973 if (*tmp_branch) {
6974 got_ref_close(*tmp_branch);
6975 *tmp_branch = NULL;
6977 free(*base_commit_id);
6978 if (*fileindex) {
6979 got_fileindex_free(*fileindex);
6980 *fileindex = NULL;
6982 lock_worktree(worktree, LOCK_SH);
6984 return err;
6987 const struct got_error *
6988 got_worktree_histedit_postpone(struct got_worktree *worktree,
6989 struct got_fileindex *fileindex)
6991 if (fileindex)
6992 got_fileindex_free(fileindex);
6993 return lock_worktree(worktree, LOCK_SH);
6996 const struct got_error *
6997 got_worktree_histedit_in_progress(int *in_progress,
6998 struct got_worktree *worktree)
7000 const struct got_error *err;
7001 char *tmp_branch_name = NULL;
7003 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 if (err)
7005 return err;
7007 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 free(tmp_branch_name);
7009 return NULL;
7012 const struct got_error *
7013 got_worktree_histedit_continue(struct got_object_id **commit_id,
7014 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7015 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7016 struct got_worktree *worktree, struct got_repository *repo)
7018 const struct got_error *err;
7019 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7020 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7021 struct got_reference *commit_ref = NULL;
7022 struct got_reference *base_commit_ref = NULL;
7023 char *fileindex_path = NULL;
7024 int have_staged_files = 0;
7026 *commit_id = NULL;
7027 *tmp_branch = NULL;
7028 *base_commit_id = NULL;
7029 *fileindex = NULL;
7031 err = lock_worktree(worktree, LOCK_EX);
7032 if (err)
7033 return err;
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 if (err)
7037 goto done;
7039 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7040 &have_staged_files);
7041 if (err && err->code != GOT_ERR_CANCELLED)
7042 goto done;
7043 if (have_staged_files) {
7044 err = got_error(GOT_ERR_STAGED_PATHS);
7045 goto done;
7048 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7049 if (err)
7050 goto done;
7052 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7053 if (err)
7054 goto done;
7056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7057 if (err)
7058 goto done;
7060 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7061 worktree);
7062 if (err)
7063 goto done;
7065 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7066 if (err)
7067 goto done;
7069 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7070 if (err)
7071 goto done;
7072 err = got_ref_resolve(commit_id, repo, commit_ref);
7073 if (err)
7074 goto done;
7076 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7077 if (err)
7078 goto done;
7079 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7080 if (err)
7081 goto done;
7083 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7084 if (err)
7085 goto done;
7086 done:
7087 free(commit_ref_name);
7088 free(branch_ref_name);
7089 free(fileindex_path);
7090 if (commit_ref)
7091 got_ref_close(commit_ref);
7092 if (base_commit_ref)
7093 got_ref_close(base_commit_ref);
7094 if (err) {
7095 free(*commit_id);
7096 *commit_id = NULL;
7097 free(*base_commit_id);
7098 *base_commit_id = NULL;
7099 if (*tmp_branch) {
7100 got_ref_close(*tmp_branch);
7101 *tmp_branch = NULL;
7103 if (*fileindex) {
7104 got_fileindex_free(*fileindex);
7105 *fileindex = NULL;
7107 lock_worktree(worktree, LOCK_EX);
7109 return err;
7112 static const struct got_error *
7113 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7115 const struct got_error *err;
7116 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7117 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7119 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7120 if (err)
7121 goto done;
7122 err = delete_ref(tmp_branch_name, repo);
7123 if (err)
7124 goto done;
7126 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7127 worktree);
7128 if (err)
7129 goto done;
7130 err = delete_ref(base_commit_ref_name, repo);
7131 if (err)
7132 goto done;
7134 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7135 if (err)
7136 goto done;
7137 err = delete_ref(branch_ref_name, repo);
7138 if (err)
7139 goto done;
7141 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7142 if (err)
7143 goto done;
7144 err = delete_ref(commit_ref_name, repo);
7145 if (err)
7146 goto done;
7147 done:
7148 free(tmp_branch_name);
7149 free(base_commit_ref_name);
7150 free(branch_ref_name);
7151 free(commit_ref_name);
7152 return err;
7155 const struct got_error *
7156 got_worktree_histedit_abort(struct got_worktree *worktree,
7157 struct got_fileindex *fileindex, struct got_repository *repo,
7158 struct got_reference *branch, struct got_object_id *base_commit_id,
7159 got_worktree_checkout_cb progress_cb, void *progress_arg)
7161 const struct got_error *err, *unlockerr, *sync_err;
7162 struct got_reference *resolved = NULL;
7163 char *fileindex_path = NULL;
7164 struct got_commit_object *commit = NULL;
7165 struct got_object_id *tree_id = NULL;
7166 struct revert_file_args rfa;
7168 err = lock_worktree(worktree, LOCK_EX);
7169 if (err)
7170 return err;
7172 err = got_object_open_as_commit(&commit, repo,
7173 worktree->base_commit_id);
7174 if (err)
7175 goto done;
7177 err = got_ref_open(&resolved, repo,
7178 got_ref_get_symref_target(branch), 0);
7179 if (err)
7180 goto done;
7182 err = got_worktree_set_head_ref(worktree, resolved);
7183 if (err)
7184 goto done;
7186 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7187 if (err)
7188 goto done;
7190 err = got_object_id_by_path(&tree_id, repo, commit,
7191 worktree->path_prefix);
7192 if (err)
7193 goto done;
7195 err = delete_histedit_refs(worktree, repo);
7196 if (err)
7197 goto done;
7199 err = get_fileindex_path(&fileindex_path, worktree);
7200 if (err)
7201 goto done;
7203 rfa.worktree = worktree;
7204 rfa.fileindex = fileindex;
7205 rfa.progress_cb = progress_cb;
7206 rfa.progress_arg = progress_arg;
7207 rfa.patch_cb = NULL;
7208 rfa.patch_arg = NULL;
7209 rfa.repo = repo;
7210 rfa.unlink_added_files = 0;
7211 err = worktree_status(worktree, "", fileindex, repo,
7212 revert_file, &rfa, NULL, NULL, 1, 0);
7213 if (err)
7214 goto sync;
7216 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7217 repo, progress_cb, progress_arg, NULL, NULL);
7218 sync:
7219 sync_err = sync_fileindex(fileindex, fileindex_path);
7220 if (sync_err && err == NULL)
7221 err = sync_err;
7222 done:
7223 got_ref_close(resolved);
7224 free(tree_id);
7225 free(fileindex_path);
7227 unlockerr = lock_worktree(worktree, LOCK_SH);
7228 if (unlockerr && err == NULL)
7229 err = unlockerr;
7230 return err;
7233 const struct got_error *
7234 got_worktree_histedit_complete(struct got_worktree *worktree,
7235 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7236 struct got_reference *edited_branch, struct got_repository *repo)
7238 const struct got_error *err, *unlockerr, *sync_err;
7239 struct got_object_id *new_head_commit_id = NULL;
7240 struct got_reference *resolved = NULL;
7241 char *fileindex_path = NULL;
7243 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7244 if (err)
7245 return err;
7247 err = got_ref_open(&resolved, repo,
7248 got_ref_get_symref_target(edited_branch), 0);
7249 if (err)
7250 goto done;
7252 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7253 resolved, new_head_commit_id, repo);
7254 if (err)
7255 goto done;
7257 err = got_ref_change_ref(resolved, new_head_commit_id);
7258 if (err)
7259 goto done;
7261 err = got_ref_write(resolved, repo);
7262 if (err)
7263 goto done;
7265 err = got_worktree_set_head_ref(worktree, resolved);
7266 if (err)
7267 goto done;
7269 err = delete_histedit_refs(worktree, repo);
7270 if (err)
7271 goto done;
7273 err = get_fileindex_path(&fileindex_path, worktree);
7274 if (err)
7275 goto done;
7276 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7279 err = sync_err;
7280 done:
7281 got_fileindex_free(fileindex);
7282 free(fileindex_path);
7283 free(new_head_commit_id);
7284 unlockerr = lock_worktree(worktree, LOCK_SH);
7285 if (unlockerr && err == NULL)
7286 err = unlockerr;
7287 return err;
7290 const struct got_error *
7291 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7292 struct got_object_id *commit_id, struct got_repository *repo)
7294 const struct got_error *err;
7295 char *commit_ref_name;
7297 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7298 if (err)
7299 return err;
7301 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7302 if (err)
7303 goto done;
7305 err = delete_ref(commit_ref_name, repo);
7306 done:
7307 free(commit_ref_name);
7308 return err;
7311 const struct got_error *
7312 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7313 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7314 struct got_worktree *worktree, const char *refname,
7315 struct got_repository *repo)
7317 const struct got_error *err = NULL;
7318 char *fileindex_path = NULL;
7319 struct check_rebase_ok_arg ok_arg;
7321 *fileindex = NULL;
7322 *branch_ref = NULL;
7323 *base_branch_ref = NULL;
7325 err = lock_worktree(worktree, LOCK_EX);
7326 if (err)
7327 return err;
7329 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7330 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7331 "cannot integrate a branch into itself; "
7332 "update -b or different branch name required");
7333 goto done;
7336 err = open_fileindex(fileindex, &fileindex_path, worktree);
7337 if (err)
7338 goto done;
7340 /* Preconditions are the same as for rebase. */
7341 ok_arg.worktree = worktree;
7342 ok_arg.repo = repo;
7343 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7344 &ok_arg);
7345 if (err)
7346 goto done;
7348 err = got_ref_open(branch_ref, repo, refname, 1);
7349 if (err)
7350 goto done;
7352 err = got_ref_open(base_branch_ref, repo,
7353 got_worktree_get_head_ref_name(worktree), 1);
7354 done:
7355 if (err) {
7356 if (*branch_ref) {
7357 got_ref_close(*branch_ref);
7358 *branch_ref = NULL;
7360 if (*base_branch_ref) {
7361 got_ref_close(*base_branch_ref);
7362 *base_branch_ref = NULL;
7364 if (*fileindex) {
7365 got_fileindex_free(*fileindex);
7366 *fileindex = NULL;
7368 lock_worktree(worktree, LOCK_SH);
7370 return err;
7373 const struct got_error *
7374 got_worktree_integrate_continue(struct got_worktree *worktree,
7375 struct got_fileindex *fileindex, struct got_repository *repo,
7376 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7377 got_worktree_checkout_cb progress_cb, void *progress_arg,
7378 got_cancel_cb cancel_cb, void *cancel_arg)
7380 const struct got_error *err = NULL, *sync_err, *unlockerr;
7381 char *fileindex_path = NULL;
7382 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7383 struct got_commit_object *commit = NULL;
7385 err = get_fileindex_path(&fileindex_path, worktree);
7386 if (err)
7387 goto done;
7389 err = got_ref_resolve(&commit_id, repo, branch_ref);
7390 if (err)
7391 goto done;
7393 err = got_object_open_as_commit(&commit, repo, commit_id);
7394 if (err)
7395 goto done;
7397 err = got_object_id_by_path(&tree_id, repo, commit,
7398 worktree->path_prefix);
7399 if (err)
7400 goto done;
7402 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7403 if (err)
7404 goto done;
7406 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7407 progress_cb, progress_arg, cancel_cb, cancel_arg);
7408 if (err)
7409 goto sync;
7411 err = got_ref_change_ref(base_branch_ref, commit_id);
7412 if (err)
7413 goto sync;
7415 err = got_ref_write(base_branch_ref, repo);
7416 if (err)
7417 goto sync;
7419 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7420 sync:
7421 sync_err = sync_fileindex(fileindex, fileindex_path);
7422 if (sync_err && err == NULL)
7423 err = sync_err;
7425 done:
7426 unlockerr = got_ref_unlock(branch_ref);
7427 if (unlockerr && err == NULL)
7428 err = unlockerr;
7429 got_ref_close(branch_ref);
7431 unlockerr = got_ref_unlock(base_branch_ref);
7432 if (unlockerr && err == NULL)
7433 err = unlockerr;
7434 got_ref_close(base_branch_ref);
7436 got_fileindex_free(fileindex);
7437 free(fileindex_path);
7438 free(tree_id);
7439 if (commit)
7440 got_object_commit_close(commit);
7442 unlockerr = lock_worktree(worktree, LOCK_SH);
7443 if (unlockerr && err == NULL)
7444 err = unlockerr;
7445 return err;
7448 const struct got_error *
7449 got_worktree_integrate_abort(struct got_worktree *worktree,
7450 struct got_fileindex *fileindex, struct got_repository *repo,
7451 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7453 const struct got_error *err = NULL, *unlockerr = NULL;
7455 got_fileindex_free(fileindex);
7457 err = lock_worktree(worktree, LOCK_SH);
7459 unlockerr = got_ref_unlock(branch_ref);
7460 if (unlockerr && err == NULL)
7461 err = unlockerr;
7462 got_ref_close(branch_ref);
7464 unlockerr = got_ref_unlock(base_branch_ref);
7465 if (unlockerr && err == NULL)
7466 err = unlockerr;
7467 got_ref_close(base_branch_ref);
7469 return err;
7472 const struct got_error *
7473 got_worktree_merge_postpone(struct got_worktree *worktree,
7474 struct got_fileindex *fileindex)
7476 const struct got_error *err, *sync_err;
7477 char *fileindex_path = NULL;
7479 err = get_fileindex_path(&fileindex_path, worktree);
7480 if (err)
7481 goto done;
7483 sync_err = sync_fileindex(fileindex, fileindex_path);
7485 err = lock_worktree(worktree, LOCK_SH);
7486 if (sync_err && err == NULL)
7487 err = sync_err;
7488 done:
7489 got_fileindex_free(fileindex);
7490 free(fileindex_path);
7491 return err;
7494 static const struct got_error *
7495 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7497 const struct got_error *err;
7498 char *branch_refname = NULL, *commit_refname = NULL;
7500 err = get_merge_branch_ref_name(&branch_refname, worktree);
7501 if (err)
7502 goto done;
7503 err = delete_ref(branch_refname, repo);
7504 if (err)
7505 goto done;
7507 err = get_merge_commit_ref_name(&commit_refname, worktree);
7508 if (err)
7509 goto done;
7510 err = delete_ref(commit_refname, repo);
7511 if (err)
7512 goto done;
7514 done:
7515 free(branch_refname);
7516 free(commit_refname);
7517 return err;
7520 struct merge_commit_msg_arg {
7521 struct got_worktree *worktree;
7522 const char *branch_name;
7525 static const struct got_error *
7526 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7527 void *arg)
7529 struct merge_commit_msg_arg *a = arg;
7531 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7532 got_worktree_get_head_ref_name(a->worktree)) == -1)
7533 return got_error_from_errno("asprintf");
7535 return NULL;
7539 const struct got_error *
7540 got_worktree_merge_branch(struct got_worktree *worktree,
7541 struct got_fileindex *fileindex,
7542 struct got_object_id *yca_commit_id,
7543 struct got_object_id *branch_tip,
7544 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7545 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7547 const struct got_error *err;
7548 char *fileindex_path = NULL;
7550 err = get_fileindex_path(&fileindex_path, worktree);
7551 if (err)
7552 goto done;
7554 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7555 worktree);
7556 if (err)
7557 goto done;
7559 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7560 branch_tip, repo, progress_cb, progress_arg,
7561 cancel_cb, cancel_arg);
7562 done:
7563 free(fileindex_path);
7564 return err;
7567 const struct got_error *
7568 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7569 struct got_worktree *worktree, struct got_fileindex *fileindex,
7570 const char *author, const char *committer, int allow_bad_symlinks,
7571 struct got_object_id *branch_tip, const char *branch_name,
7572 struct got_repository *repo,
7573 got_worktree_status_cb status_cb, void *status_arg)
7576 const struct got_error *err = NULL, *sync_err;
7577 struct got_pathlist_head commitable_paths;
7578 struct collect_commitables_arg cc_arg;
7579 struct got_pathlist_entry *pe;
7580 struct got_reference *head_ref = NULL;
7581 struct got_object_id *head_commit_id = NULL;
7582 int have_staged_files = 0;
7583 struct merge_commit_msg_arg mcm_arg;
7584 char *fileindex_path = NULL;
7586 *new_commit_id = NULL;
7588 TAILQ_INIT(&commitable_paths);
7590 err = get_fileindex_path(&fileindex_path, worktree);
7591 if (err)
7592 goto done;
7594 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7595 if (err)
7596 goto done;
7598 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7599 if (err)
7600 goto done;
7602 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7603 &have_staged_files);
7604 if (err && err->code != GOT_ERR_CANCELLED)
7605 goto done;
7606 if (have_staged_files) {
7607 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7608 goto done;
7611 cc_arg.commitable_paths = &commitable_paths;
7612 cc_arg.worktree = worktree;
7613 cc_arg.fileindex = fileindex;
7614 cc_arg.repo = repo;
7615 cc_arg.have_staged_files = have_staged_files;
7616 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7617 err = worktree_status(worktree, "", fileindex, repo,
7618 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7619 if (err)
7620 goto done;
7622 if (TAILQ_EMPTY(&commitable_paths)) {
7623 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7624 "merge of %s cannot proceed", branch_name);
7625 goto done;
7628 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7629 struct got_commitable *ct = pe->data;
7630 const char *ct_path = ct->in_repo_path;
7632 while (ct_path[0] == '/')
7633 ct_path++;
7634 err = check_out_of_date(ct_path, ct->status,
7635 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7636 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7637 if (err)
7638 goto done;
7642 mcm_arg.worktree = worktree;
7643 mcm_arg.branch_name = branch_name;
7644 err = commit_worktree(new_commit_id, &commitable_paths,
7645 head_commit_id, branch_tip, worktree, author, committer,
7646 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7647 if (err)
7648 goto done;
7650 err = update_fileindex_after_commit(worktree, &commitable_paths,
7651 *new_commit_id, fileindex, have_staged_files);
7652 sync_err = sync_fileindex(fileindex, fileindex_path);
7653 if (sync_err && err == NULL)
7654 err = sync_err;
7655 done:
7656 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7657 struct got_commitable *ct = pe->data;
7658 free_commitable(ct);
7660 got_pathlist_free(&commitable_paths);
7661 free(fileindex_path);
7662 return err;
7665 const struct got_error *
7666 got_worktree_merge_complete(struct got_worktree *worktree,
7667 struct got_fileindex *fileindex, struct got_repository *repo)
7669 const struct got_error *err, *unlockerr, *sync_err;
7670 char *fileindex_path = NULL;
7672 err = delete_merge_refs(worktree, repo);
7673 if (err)
7674 goto done;
7676 err = get_fileindex_path(&fileindex_path, worktree);
7677 if (err)
7678 goto done;
7679 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7680 sync_err = sync_fileindex(fileindex, fileindex_path);
7681 if (sync_err && err == NULL)
7682 err = sync_err;
7683 done:
7684 got_fileindex_free(fileindex);
7685 free(fileindex_path);
7686 unlockerr = lock_worktree(worktree, LOCK_SH);
7687 if (unlockerr && err == NULL)
7688 err = unlockerr;
7689 return err;
7692 const struct got_error *
7693 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7694 struct got_repository *repo)
7696 const struct got_error *err;
7697 char *branch_refname = NULL;
7698 struct got_reference *branch_ref = NULL;
7700 *in_progress = 0;
7702 err = get_merge_branch_ref_name(&branch_refname, worktree);
7703 if (err)
7704 return err;
7705 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7706 free(branch_refname);
7707 if (err) {
7708 if (err->code != GOT_ERR_NOT_REF)
7709 return err;
7710 } else
7711 *in_progress = 1;
7713 return NULL;
7716 const struct got_error *got_worktree_merge_prepare(
7717 struct got_fileindex **fileindex, struct got_worktree *worktree,
7718 struct got_reference *branch, struct got_repository *repo)
7720 const struct got_error *err = NULL;
7721 char *fileindex_path = NULL;
7722 char *branch_refname = NULL, *commit_refname = NULL;
7723 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7724 struct got_reference *commit_ref = NULL;
7725 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7726 struct check_rebase_ok_arg ok_arg;
7728 *fileindex = NULL;
7730 err = lock_worktree(worktree, LOCK_EX);
7731 if (err)
7732 return err;
7734 err = open_fileindex(fileindex, &fileindex_path, worktree);
7735 if (err)
7736 goto done;
7738 /* Preconditions are the same as for rebase. */
7739 ok_arg.worktree = worktree;
7740 ok_arg.repo = repo;
7741 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7742 &ok_arg);
7743 if (err)
7744 goto done;
7746 err = get_merge_branch_ref_name(&branch_refname, worktree);
7747 if (err)
7748 return err;
7750 err = get_merge_commit_ref_name(&commit_refname, worktree);
7751 if (err)
7752 return err;
7754 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7755 0);
7756 if (err)
7757 goto done;
7759 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7760 if (err)
7761 goto done;
7763 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7764 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7765 goto done;
7768 err = got_ref_resolve(&branch_tip, repo, branch);
7769 if (err)
7770 goto done;
7772 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7773 if (err)
7774 goto done;
7775 err = got_ref_write(branch_ref, repo);
7776 if (err)
7777 goto done;
7779 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7780 if (err)
7781 goto done;
7782 err = got_ref_write(commit_ref, repo);
7783 if (err)
7784 goto done;
7786 done:
7787 free(branch_refname);
7788 free(commit_refname);
7789 free(fileindex_path);
7790 if (branch_ref)
7791 got_ref_close(branch_ref);
7792 if (commit_ref)
7793 got_ref_close(commit_ref);
7794 if (wt_branch)
7795 got_ref_close(wt_branch);
7796 free(wt_branch_tip);
7797 if (err) {
7798 if (*fileindex) {
7799 got_fileindex_free(*fileindex);
7800 *fileindex = NULL;
7802 lock_worktree(worktree, LOCK_SH);
7804 return err;
7807 const struct got_error *
7808 got_worktree_merge_continue(char **branch_name,
7809 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7810 struct got_worktree *worktree, struct got_repository *repo)
7812 const struct got_error *err;
7813 char *commit_refname = NULL, *branch_refname = NULL;
7814 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7815 char *fileindex_path = NULL;
7816 int have_staged_files = 0;
7818 *branch_name = NULL;
7819 *branch_tip = NULL;
7820 *fileindex = NULL;
7822 err = lock_worktree(worktree, LOCK_EX);
7823 if (err)
7824 return err;
7826 err = open_fileindex(fileindex, &fileindex_path, worktree);
7827 if (err)
7828 goto done;
7830 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7831 &have_staged_files);
7832 if (err && err->code != GOT_ERR_CANCELLED)
7833 goto done;
7834 if (have_staged_files) {
7835 err = got_error(GOT_ERR_STAGED_PATHS);
7836 goto done;
7839 err = get_merge_branch_ref_name(&branch_refname, worktree);
7840 if (err)
7841 goto done;
7843 err = get_merge_commit_ref_name(&commit_refname, worktree);
7844 if (err)
7845 goto done;
7847 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7848 if (err)
7849 goto done;
7851 if (!got_ref_is_symbolic(branch_ref)) {
7852 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7853 "%s is not a symbolic reference",
7854 got_ref_get_name(branch_ref));
7855 goto done;
7857 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7858 if (*branch_name == NULL) {
7859 err = got_error_from_errno("strdup");
7860 goto done;
7863 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7864 if (err)
7865 goto done;
7867 err = got_ref_resolve(branch_tip, repo, commit_ref);
7868 if (err)
7869 goto done;
7870 done:
7871 free(commit_refname);
7872 free(branch_refname);
7873 free(fileindex_path);
7874 if (commit_ref)
7875 got_ref_close(commit_ref);
7876 if (branch_ref)
7877 got_ref_close(branch_ref);
7878 if (err) {
7879 if (*branch_name) {
7880 free(*branch_name);
7881 *branch_name = NULL;
7883 free(*branch_tip);
7884 *branch_tip = NULL;
7885 if (*fileindex) {
7886 got_fileindex_free(*fileindex);
7887 *fileindex = NULL;
7889 lock_worktree(worktree, LOCK_SH);
7891 return err;
7894 const struct got_error *
7895 got_worktree_merge_abort(struct got_worktree *worktree,
7896 struct got_fileindex *fileindex, struct got_repository *repo,
7897 got_worktree_checkout_cb progress_cb, void *progress_arg)
7899 const struct got_error *err, *unlockerr, *sync_err;
7900 struct got_object_id *commit_id = NULL;
7901 struct got_commit_object *commit = NULL;
7902 char *fileindex_path = NULL;
7903 struct revert_file_args rfa;
7904 struct got_object_id *tree_id = NULL;
7906 err = got_object_open_as_commit(&commit, repo,
7907 worktree->base_commit_id);
7908 if (err)
7909 goto done;
7911 err = got_object_id_by_path(&tree_id, repo, commit,
7912 worktree->path_prefix);
7913 if (err)
7914 goto done;
7916 err = delete_merge_refs(worktree, repo);
7917 if (err)
7918 goto done;
7920 err = get_fileindex_path(&fileindex_path, worktree);
7921 if (err)
7922 goto done;
7924 rfa.worktree = worktree;
7925 rfa.fileindex = fileindex;
7926 rfa.progress_cb = progress_cb;
7927 rfa.progress_arg = progress_arg;
7928 rfa.patch_cb = NULL;
7929 rfa.patch_arg = NULL;
7930 rfa.repo = repo;
7931 rfa.unlink_added_files = 1;
7932 err = worktree_status(worktree, "", fileindex, repo,
7933 revert_file, &rfa, NULL, NULL, 1, 0);
7934 if (err)
7935 goto sync;
7937 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7938 repo, progress_cb, progress_arg, NULL, NULL);
7939 sync:
7940 sync_err = sync_fileindex(fileindex, fileindex_path);
7941 if (sync_err && err == NULL)
7942 err = sync_err;
7943 done:
7944 free(tree_id);
7945 free(commit_id);
7946 if (commit)
7947 got_object_commit_close(commit);
7948 if (fileindex)
7949 got_fileindex_free(fileindex);
7950 free(fileindex_path);
7952 unlockerr = lock_worktree(worktree, LOCK_SH);
7953 if (unlockerr && err == NULL)
7954 err = unlockerr;
7955 return err;
7958 struct check_stage_ok_arg {
7959 struct got_object_id *head_commit_id;
7960 struct got_worktree *worktree;
7961 struct got_fileindex *fileindex;
7962 struct got_repository *repo;
7963 int have_changes;
7966 static const struct got_error *
7967 check_stage_ok(void *arg, unsigned char status,
7968 unsigned char staged_status, const char *relpath,
7969 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7970 struct got_object_id *commit_id, int dirfd, const char *de_name)
7972 struct check_stage_ok_arg *a = arg;
7973 const struct got_error *err = NULL;
7974 struct got_fileindex_entry *ie;
7975 struct got_object_id base_commit_id;
7976 struct got_object_id *base_commit_idp = NULL;
7977 char *in_repo_path = NULL, *p;
7979 if (status == GOT_STATUS_UNVERSIONED ||
7980 status == GOT_STATUS_NO_CHANGE)
7981 return NULL;
7982 if (status == GOT_STATUS_NONEXISTENT)
7983 return got_error_set_errno(ENOENT, relpath);
7985 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7986 if (ie == NULL)
7987 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7989 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7990 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7991 relpath) == -1)
7992 return got_error_from_errno("asprintf");
7994 if (got_fileindex_entry_has_commit(ie)) {
7995 memcpy(base_commit_id.sha1, ie->commit_sha1,
7996 SHA1_DIGEST_LENGTH);
7997 base_commit_idp = &base_commit_id;
8000 if (status == GOT_STATUS_CONFLICT) {
8001 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8002 goto done;
8003 } else if (status != GOT_STATUS_ADD &&
8004 status != GOT_STATUS_MODIFY &&
8005 status != GOT_STATUS_DELETE) {
8006 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8007 goto done;
8010 a->have_changes = 1;
8012 p = in_repo_path;
8013 while (p[0] == '/')
8014 p++;
8015 err = check_out_of_date(p, status, staged_status,
8016 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8017 GOT_ERR_STAGE_OUT_OF_DATE);
8018 done:
8019 free(in_repo_path);
8020 return err;
8023 struct stage_path_arg {
8024 struct got_worktree *worktree;
8025 struct got_fileindex *fileindex;
8026 struct got_repository *repo;
8027 got_worktree_status_cb status_cb;
8028 void *status_arg;
8029 got_worktree_patch_cb patch_cb;
8030 void *patch_arg;
8031 int staged_something;
8032 int allow_bad_symlinks;
8035 static const struct got_error *
8036 stage_path(void *arg, unsigned char status,
8037 unsigned char staged_status, const char *relpath,
8038 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8039 struct got_object_id *commit_id, int dirfd, const char *de_name)
8041 struct stage_path_arg *a = arg;
8042 const struct got_error *err = NULL;
8043 struct got_fileindex_entry *ie;
8044 char *ondisk_path = NULL, *path_content = NULL;
8045 uint32_t stage;
8046 struct got_object_id *new_staged_blob_id = NULL;
8047 struct stat sb;
8049 if (status == GOT_STATUS_UNVERSIONED)
8050 return NULL;
8052 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8053 if (ie == NULL)
8054 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8057 relpath)== -1)
8058 return got_error_from_errno("asprintf");
8060 switch (status) {
8061 case GOT_STATUS_ADD:
8062 case GOT_STATUS_MODIFY:
8063 /* XXX could sb.st_mode be passed in by our caller? */
8064 if (lstat(ondisk_path, &sb) == -1) {
8065 err = got_error_from_errno2("lstat", ondisk_path);
8066 break;
8068 if (a->patch_cb) {
8069 if (status == GOT_STATUS_ADD) {
8070 int choice = GOT_PATCH_CHOICE_NONE;
8071 err = (*a->patch_cb)(&choice, a->patch_arg,
8072 status, ie->path, NULL, 1, 1);
8073 if (err)
8074 break;
8075 if (choice != GOT_PATCH_CHOICE_YES)
8076 break;
8077 } else {
8078 err = create_patched_content(&path_content, 0,
8079 staged_blob_id ? staged_blob_id : blob_id,
8080 ondisk_path, dirfd, de_name, ie->path,
8081 a->repo, a->patch_cb, a->patch_arg);
8082 if (err || path_content == NULL)
8083 break;
8086 err = got_object_blob_create(&new_staged_blob_id,
8087 path_content ? path_content : ondisk_path, a->repo);
8088 if (err)
8089 break;
8090 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8091 SHA1_DIGEST_LENGTH);
8092 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8093 stage = GOT_FILEIDX_STAGE_ADD;
8094 else
8095 stage = GOT_FILEIDX_STAGE_MODIFY;
8096 got_fileindex_entry_stage_set(ie, stage);
8097 if (S_ISLNK(sb.st_mode)) {
8098 int is_bad_symlink = 0;
8099 if (!a->allow_bad_symlinks) {
8100 char target_path[PATH_MAX];
8101 ssize_t target_len;
8102 target_len = readlink(ondisk_path, target_path,
8103 sizeof(target_path));
8104 if (target_len == -1) {
8105 err = got_error_from_errno2("readlink",
8106 ondisk_path);
8107 break;
8109 err = is_bad_symlink_target(&is_bad_symlink,
8110 target_path, target_len, ondisk_path,
8111 a->worktree->root_path);
8112 if (err)
8113 break;
8114 if (is_bad_symlink) {
8115 err = got_error_path(ondisk_path,
8116 GOT_ERR_BAD_SYMLINK);
8117 break;
8120 if (is_bad_symlink)
8121 got_fileindex_entry_staged_filetype_set(ie,
8122 GOT_FILEIDX_MODE_BAD_SYMLINK);
8123 else
8124 got_fileindex_entry_staged_filetype_set(ie,
8125 GOT_FILEIDX_MODE_SYMLINK);
8126 } else {
8127 got_fileindex_entry_staged_filetype_set(ie,
8128 GOT_FILEIDX_MODE_REGULAR_FILE);
8130 a->staged_something = 1;
8131 if (a->status_cb == NULL)
8132 break;
8133 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8134 get_staged_status(ie), relpath, blob_id,
8135 new_staged_blob_id, NULL, dirfd, de_name);
8136 if (err)
8137 break;
8139 * When staging the reverse of the staged diff,
8140 * implicitly unstage the file.
8142 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8143 sizeof(ie->blob_sha1)) == 0) {
8144 got_fileindex_entry_stage_set(ie,
8145 GOT_FILEIDX_STAGE_NONE);
8147 break;
8148 case GOT_STATUS_DELETE:
8149 if (staged_status == GOT_STATUS_DELETE)
8150 break;
8151 if (a->patch_cb) {
8152 int choice = GOT_PATCH_CHOICE_NONE;
8153 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8154 ie->path, NULL, 1, 1);
8155 if (err)
8156 break;
8157 if (choice == GOT_PATCH_CHOICE_NO)
8158 break;
8159 if (choice != GOT_PATCH_CHOICE_YES) {
8160 err = got_error(GOT_ERR_PATCH_CHOICE);
8161 break;
8164 stage = GOT_FILEIDX_STAGE_DELETE;
8165 got_fileindex_entry_stage_set(ie, stage);
8166 a->staged_something = 1;
8167 if (a->status_cb == NULL)
8168 break;
8169 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8170 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8171 de_name);
8172 break;
8173 case GOT_STATUS_NO_CHANGE:
8174 break;
8175 case GOT_STATUS_CONFLICT:
8176 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8177 break;
8178 case GOT_STATUS_NONEXISTENT:
8179 err = got_error_set_errno(ENOENT, relpath);
8180 break;
8181 default:
8182 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8183 break;
8186 if (path_content && unlink(path_content) == -1 && err == NULL)
8187 err = got_error_from_errno2("unlink", path_content);
8188 free(path_content);
8189 free(ondisk_path);
8190 free(new_staged_blob_id);
8191 return err;
8194 const struct got_error *
8195 got_worktree_stage(struct got_worktree *worktree,
8196 struct got_pathlist_head *paths,
8197 got_worktree_status_cb status_cb, void *status_arg,
8198 got_worktree_patch_cb patch_cb, void *patch_arg,
8199 int allow_bad_symlinks, struct got_repository *repo)
8201 const struct got_error *err = NULL, *sync_err, *unlockerr;
8202 struct got_pathlist_entry *pe;
8203 struct got_fileindex *fileindex = NULL;
8204 char *fileindex_path = NULL;
8205 struct got_reference *head_ref = NULL;
8206 struct got_object_id *head_commit_id = NULL;
8207 struct check_stage_ok_arg oka;
8208 struct stage_path_arg spa;
8210 err = lock_worktree(worktree, LOCK_EX);
8211 if (err)
8212 return err;
8214 err = got_ref_open(&head_ref, repo,
8215 got_worktree_get_head_ref_name(worktree), 0);
8216 if (err)
8217 goto done;
8218 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8219 if (err)
8220 goto done;
8221 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8222 if (err)
8223 goto done;
8225 /* Check pre-conditions before staging anything. */
8226 oka.head_commit_id = head_commit_id;
8227 oka.worktree = worktree;
8228 oka.fileindex = fileindex;
8229 oka.repo = repo;
8230 oka.have_changes = 0;
8231 TAILQ_FOREACH(pe, paths, entry) {
8232 err = worktree_status(worktree, pe->path, fileindex, repo,
8233 check_stage_ok, &oka, NULL, NULL, 1, 0);
8234 if (err)
8235 goto done;
8237 if (!oka.have_changes) {
8238 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8239 goto done;
8242 spa.worktree = worktree;
8243 spa.fileindex = fileindex;
8244 spa.repo = repo;
8245 spa.patch_cb = patch_cb;
8246 spa.patch_arg = patch_arg;
8247 spa.status_cb = status_cb;
8248 spa.status_arg = status_arg;
8249 spa.staged_something = 0;
8250 spa.allow_bad_symlinks = allow_bad_symlinks;
8251 TAILQ_FOREACH(pe, paths, entry) {
8252 err = worktree_status(worktree, pe->path, fileindex, repo,
8253 stage_path, &spa, NULL, NULL, 1, 0);
8254 if (err)
8255 goto done;
8257 if (!spa.staged_something) {
8258 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8259 goto done;
8262 sync_err = sync_fileindex(fileindex, fileindex_path);
8263 if (sync_err && err == NULL)
8264 err = sync_err;
8265 done:
8266 if (head_ref)
8267 got_ref_close(head_ref);
8268 free(head_commit_id);
8269 free(fileindex_path);
8270 if (fileindex)
8271 got_fileindex_free(fileindex);
8272 unlockerr = lock_worktree(worktree, LOCK_SH);
8273 if (unlockerr && err == NULL)
8274 err = unlockerr;
8275 return err;
8278 struct unstage_path_arg {
8279 struct got_worktree *worktree;
8280 struct got_fileindex *fileindex;
8281 struct got_repository *repo;
8282 got_worktree_checkout_cb progress_cb;
8283 void *progress_arg;
8284 got_worktree_patch_cb patch_cb;
8285 void *patch_arg;
8288 static const struct got_error *
8289 create_unstaged_content(char **path_unstaged_content,
8290 char **path_new_staged_content, struct got_object_id *blob_id,
8291 struct got_object_id *staged_blob_id, const char *relpath,
8292 struct got_repository *repo,
8293 got_worktree_patch_cb patch_cb, void *patch_arg)
8295 const struct got_error *err, *free_err;
8296 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8297 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8298 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8299 struct got_diffreg_result *diffreg_result = NULL;
8300 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8301 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8302 int fd1 = -1, fd2 = -1;
8304 *path_unstaged_content = NULL;
8305 *path_new_staged_content = NULL;
8307 err = got_object_id_str(&label1, blob_id);
8308 if (err)
8309 return err;
8311 fd1 = got_opentempfd();
8312 if (fd1 == -1) {
8313 err = got_error_from_errno("got_opentempfd");
8314 goto done;
8316 fd2 = got_opentempfd();
8317 if (fd2 == -1) {
8318 err = got_error_from_errno("got_opentempfd");
8319 goto done;
8322 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8323 if (err)
8324 goto done;
8326 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8327 if (err)
8328 goto done;
8330 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8331 if (err)
8332 goto done;
8334 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8335 fd2);
8336 if (err)
8337 goto done;
8339 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8340 if (err)
8341 goto done;
8343 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8344 if (err)
8345 goto done;
8347 err = got_diff_files(&diffreg_result, f1, label1, f2,
8348 path2, 3, 0, 1, NULL);
8349 if (err)
8350 goto done;
8352 err = got_opentemp_named(path_unstaged_content, &outfile,
8353 "got-unstaged-content");
8354 if (err)
8355 goto done;
8356 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8357 "got-new-staged-content");
8358 if (err)
8359 goto done;
8361 if (fseek(f1, 0L, SEEK_SET) == -1) {
8362 err = got_ferror(f1, GOT_ERR_IO);
8363 goto done;
8365 if (fseek(f2, 0L, SEEK_SET) == -1) {
8366 err = got_ferror(f2, GOT_ERR_IO);
8367 goto done;
8369 /* Count the number of actual changes in the diff result. */
8370 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8371 struct diff_chunk_context cc = {};
8372 diff_chunk_context_load_change(&cc, &nchunks_used,
8373 diffreg_result->result, n, 0);
8374 nchanges++;
8376 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8377 int choice;
8378 err = apply_or_reject_change(&choice, &nchunks_used,
8379 diffreg_result->result, n, relpath, f1, f2,
8380 &line_cur1, &line_cur2,
8381 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8382 if (err)
8383 goto done;
8384 if (choice == GOT_PATCH_CHOICE_YES)
8385 have_content = 1;
8386 else
8387 have_rejected_content = 1;
8388 if (choice == GOT_PATCH_CHOICE_QUIT)
8389 break;
8391 if (have_content || have_rejected_content)
8392 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8393 outfile, rejectfile);
8394 done:
8395 free(label1);
8396 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8397 err = got_error_from_errno("close");
8398 if (blob)
8399 got_object_blob_close(blob);
8400 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8401 err = got_error_from_errno("close");
8402 if (staged_blob)
8403 got_object_blob_close(staged_blob);
8404 free_err = got_diffreg_result_free(diffreg_result);
8405 if (free_err && err == NULL)
8406 err = free_err;
8407 if (f1 && fclose(f1) == EOF && err == NULL)
8408 err = got_error_from_errno2("fclose", path1);
8409 if (f2 && fclose(f2) == EOF && err == NULL)
8410 err = got_error_from_errno2("fclose", path2);
8411 if (outfile && fclose(outfile) == EOF && err == NULL)
8412 err = got_error_from_errno2("fclose", *path_unstaged_content);
8413 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8414 err = got_error_from_errno2("fclose", *path_new_staged_content);
8415 if (path1 && unlink(path1) == -1 && err == NULL)
8416 err = got_error_from_errno2("unlink", path1);
8417 if (path2 && unlink(path2) == -1 && err == NULL)
8418 err = got_error_from_errno2("unlink", path2);
8419 if (err || !have_content) {
8420 if (*path_unstaged_content &&
8421 unlink(*path_unstaged_content) == -1 && err == NULL)
8422 err = got_error_from_errno2("unlink",
8423 *path_unstaged_content);
8424 free(*path_unstaged_content);
8425 *path_unstaged_content = NULL;
8427 if (err || !have_content || !have_rejected_content) {
8428 if (*path_new_staged_content &&
8429 unlink(*path_new_staged_content) == -1 && err == NULL)
8430 err = got_error_from_errno2("unlink",
8431 *path_new_staged_content);
8432 free(*path_new_staged_content);
8433 *path_new_staged_content = NULL;
8435 free(path1);
8436 free(path2);
8437 return err;
8440 static const struct got_error *
8441 unstage_hunks(struct got_object_id *staged_blob_id,
8442 struct got_blob_object *blob_base,
8443 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8444 const char *ondisk_path, const char *label_orig,
8445 struct got_worktree *worktree, struct got_repository *repo,
8446 got_worktree_patch_cb patch_cb, void *patch_arg,
8447 got_worktree_checkout_cb progress_cb, void *progress_arg)
8449 const struct got_error *err = NULL;
8450 char *path_unstaged_content = NULL;
8451 char *path_new_staged_content = NULL;
8452 char *parent = NULL, *base_path = NULL;
8453 char *blob_base_path = NULL;
8454 struct got_object_id *new_staged_blob_id = NULL;
8455 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8456 struct stat sb;
8458 err = create_unstaged_content(&path_unstaged_content,
8459 &path_new_staged_content, blob_id, staged_blob_id,
8460 ie->path, repo, patch_cb, patch_arg);
8461 if (err)
8462 return err;
8464 if (path_unstaged_content == NULL)
8465 return NULL;
8467 if (path_new_staged_content) {
8468 err = got_object_blob_create(&new_staged_blob_id,
8469 path_new_staged_content, repo);
8470 if (err)
8471 goto done;
8474 f = fopen(path_unstaged_content, "re");
8475 if (f == NULL) {
8476 err = got_error_from_errno2("fopen",
8477 path_unstaged_content);
8478 goto done;
8480 if (fstat(fileno(f), &sb) == -1) {
8481 err = got_error_from_errno2("fstat", path_unstaged_content);
8482 goto done;
8484 if (got_fileindex_entry_staged_filetype_get(ie) ==
8485 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8486 char link_target[PATH_MAX];
8487 size_t r;
8488 r = fread(link_target, 1, sizeof(link_target), f);
8489 if (r == 0 && ferror(f)) {
8490 err = got_error_from_errno("fread");
8491 goto done;
8493 if (r >= sizeof(link_target)) { /* should not happen */
8494 err = got_error(GOT_ERR_NO_SPACE);
8495 goto done;
8497 link_target[r] = '\0';
8498 err = merge_symlink(worktree, blob_base,
8499 ondisk_path, ie->path, label_orig, link_target,
8500 worktree->base_commit_id, repo, progress_cb,
8501 progress_arg);
8502 } else {
8503 int local_changes_subsumed;
8505 err = got_path_dirname(&parent, ondisk_path);
8506 if (err)
8507 return err;
8509 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8510 parent) == -1) {
8511 err = got_error_from_errno("asprintf");
8512 base_path = NULL;
8513 goto done;
8516 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8517 if (err)
8518 goto done;
8519 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8520 blob_base);
8521 if (err)
8522 goto done;
8525 * In order the run a 3-way merge with a symlink we copy the symlink's
8526 * target path into a temporary file and use that file with diff3.
8528 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8529 err = dump_symlink_target_path_to_file(&f_deriv2,
8530 ondisk_path);
8531 if (err)
8532 goto done;
8533 } else {
8534 int fd;
8535 fd = open(ondisk_path,
8536 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8537 if (fd == -1) {
8538 err = got_error_from_errno2("open", ondisk_path);
8539 goto done;
8541 f_deriv2 = fdopen(fd, "r");
8542 if (f_deriv2 == NULL) {
8543 err = got_error_from_errno2("fdopen", ondisk_path);
8544 close(fd);
8545 goto done;
8549 err = merge_file(&local_changes_subsumed, worktree,
8550 f_base, f, f_deriv2, ondisk_path, ie->path,
8551 got_fileindex_perms_to_st(ie),
8552 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8553 repo, progress_cb, progress_arg);
8555 if (err)
8556 goto done;
8558 if (new_staged_blob_id) {
8559 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8560 SHA1_DIGEST_LENGTH);
8561 } else {
8562 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8563 got_fileindex_entry_staged_filetype_set(ie, 0);
8565 done:
8566 free(new_staged_blob_id);
8567 if (path_unstaged_content &&
8568 unlink(path_unstaged_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_unstaged_content);
8570 if (path_new_staged_content &&
8571 unlink(path_new_staged_content) == -1 && err == NULL)
8572 err = got_error_from_errno2("unlink", path_new_staged_content);
8573 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8574 err = got_error_from_errno2("unlink", blob_base_path);
8575 if (f_base && fclose(f_base) == EOF && err == NULL)
8576 err = got_error_from_errno2("fclose", path_unstaged_content);
8577 if (f && fclose(f) == EOF && err == NULL)
8578 err = got_error_from_errno2("fclose", path_unstaged_content);
8579 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8580 err = got_error_from_errno2("fclose", ondisk_path);
8581 free(path_unstaged_content);
8582 free(path_new_staged_content);
8583 free(blob_base_path);
8584 free(parent);
8585 free(base_path);
8586 return err;
8589 static const struct got_error *
8590 unstage_path(void *arg, unsigned char status,
8591 unsigned char staged_status, const char *relpath,
8592 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8593 struct got_object_id *commit_id, int dirfd, const char *de_name)
8595 const struct got_error *err = NULL;
8596 struct unstage_path_arg *a = arg;
8597 struct got_fileindex_entry *ie;
8598 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8599 char *ondisk_path = NULL;
8600 char *id_str = NULL, *label_orig = NULL;
8601 int local_changes_subsumed;
8602 struct stat sb;
8603 int fd1 = -1, fd2 = -1;
8605 if (staged_status != GOT_STATUS_ADD &&
8606 staged_status != GOT_STATUS_MODIFY &&
8607 staged_status != GOT_STATUS_DELETE)
8608 return NULL;
8610 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8611 if (ie == NULL)
8612 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8615 == -1)
8616 return got_error_from_errno("asprintf");
8618 err = got_object_id_str(&id_str,
8619 commit_id ? commit_id : a->worktree->base_commit_id);
8620 if (err)
8621 goto done;
8622 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8623 id_str) == -1) {
8624 err = got_error_from_errno("asprintf");
8625 goto done;
8628 fd1 = got_opentempfd();
8629 if (fd1 == -1) {
8630 err = got_error_from_errno("got_opentempfd");
8631 goto done;
8633 fd2 = got_opentempfd();
8634 if (fd2 == -1) {
8635 err = got_error_from_errno("got_opentempfd");
8636 goto done;
8639 switch (staged_status) {
8640 case GOT_STATUS_MODIFY:
8641 err = got_object_open_as_blob(&blob_base, a->repo,
8642 blob_id, 8192, fd1);
8643 if (err)
8644 break;
8645 /* fall through */
8646 case GOT_STATUS_ADD:
8647 if (a->patch_cb) {
8648 if (staged_status == GOT_STATUS_ADD) {
8649 int choice = GOT_PATCH_CHOICE_NONE;
8650 err = (*a->patch_cb)(&choice, a->patch_arg,
8651 staged_status, ie->path, NULL, 1, 1);
8652 if (err)
8653 break;
8654 if (choice != GOT_PATCH_CHOICE_YES)
8655 break;
8656 } else {
8657 err = unstage_hunks(staged_blob_id,
8658 blob_base, blob_id, ie, ondisk_path,
8659 label_orig, a->worktree, a->repo,
8660 a->patch_cb, a->patch_arg,
8661 a->progress_cb, a->progress_arg);
8662 break; /* Done with this file. */
8665 err = got_object_open_as_blob(&blob_staged, a->repo,
8666 staged_blob_id, 8192, fd2);
8667 if (err)
8668 break;
8669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8671 case GOT_FILEIDX_MODE_REGULAR_FILE:
8672 err = merge_blob(&local_changes_subsumed, a->worktree,
8673 blob_base, ondisk_path, relpath,
8674 got_fileindex_perms_to_st(ie), label_orig,
8675 blob_staged, commit_id ? commit_id :
8676 a->worktree->base_commit_id, a->repo,
8677 a->progress_cb, a->progress_arg);
8678 break;
8679 case GOT_FILEIDX_MODE_SYMLINK:
8680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8681 char *staged_target;
8682 err = got_object_blob_read_to_str(
8683 &staged_target, blob_staged);
8684 if (err)
8685 goto done;
8686 err = merge_symlink(a->worktree, blob_base,
8687 ondisk_path, relpath, label_orig,
8688 staged_target, commit_id ? commit_id :
8689 a->worktree->base_commit_id,
8690 a->repo, a->progress_cb, a->progress_arg);
8691 free(staged_target);
8692 } else {
8693 err = merge_blob(&local_changes_subsumed,
8694 a->worktree, blob_base, ondisk_path,
8695 relpath, got_fileindex_perms_to_st(ie),
8696 label_orig, blob_staged,
8697 commit_id ? commit_id :
8698 a->worktree->base_commit_id, a->repo,
8699 a->progress_cb, a->progress_arg);
8701 break;
8702 default:
8703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8704 break;
8706 if (err == NULL) {
8707 got_fileindex_entry_stage_set(ie,
8708 GOT_FILEIDX_STAGE_NONE);
8709 got_fileindex_entry_staged_filetype_set(ie, 0);
8711 break;
8712 case GOT_STATUS_DELETE:
8713 if (a->patch_cb) {
8714 int choice = GOT_PATCH_CHOICE_NONE;
8715 err = (*a->patch_cb)(&choice, a->patch_arg,
8716 staged_status, ie->path, NULL, 1, 1);
8717 if (err)
8718 break;
8719 if (choice == GOT_PATCH_CHOICE_NO)
8720 break;
8721 if (choice != GOT_PATCH_CHOICE_YES) {
8722 err = got_error(GOT_ERR_PATCH_CHOICE);
8723 break;
8726 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8727 got_fileindex_entry_staged_filetype_set(ie, 0);
8728 err = get_file_status(&status, &sb, ie, ondisk_path,
8729 dirfd, de_name, a->repo);
8730 if (err)
8731 break;
8732 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8733 break;
8735 done:
8736 free(ondisk_path);
8737 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8738 err = got_error_from_errno("close");
8739 if (blob_base)
8740 got_object_blob_close(blob_base);
8741 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8742 err = got_error_from_errno("close");
8743 if (blob_staged)
8744 got_object_blob_close(blob_staged);
8745 free(id_str);
8746 free(label_orig);
8747 return err;
8750 const struct got_error *
8751 got_worktree_unstage(struct got_worktree *worktree,
8752 struct got_pathlist_head *paths,
8753 got_worktree_checkout_cb progress_cb, void *progress_arg,
8754 got_worktree_patch_cb patch_cb, void *patch_arg,
8755 struct got_repository *repo)
8757 const struct got_error *err = NULL, *sync_err, *unlockerr;
8758 struct got_pathlist_entry *pe;
8759 struct got_fileindex *fileindex = NULL;
8760 char *fileindex_path = NULL;
8761 struct unstage_path_arg upa;
8763 err = lock_worktree(worktree, LOCK_EX);
8764 if (err)
8765 return err;
8767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8768 if (err)
8769 goto done;
8771 upa.worktree = worktree;
8772 upa.fileindex = fileindex;
8773 upa.repo = repo;
8774 upa.progress_cb = progress_cb;
8775 upa.progress_arg = progress_arg;
8776 upa.patch_cb = patch_cb;
8777 upa.patch_arg = patch_arg;
8778 TAILQ_FOREACH(pe, paths, entry) {
8779 err = worktree_status(worktree, pe->path, fileindex, repo,
8780 unstage_path, &upa, NULL, NULL, 1, 0);
8781 if (err)
8782 goto done;
8785 sync_err = sync_fileindex(fileindex, fileindex_path);
8786 if (sync_err && err == NULL)
8787 err = sync_err;
8788 done:
8789 free(fileindex_path);
8790 if (fileindex)
8791 got_fileindex_free(fileindex);
8792 unlockerr = lock_worktree(worktree, LOCK_SH);
8793 if (unlockerr && err == NULL)
8794 err = unlockerr;
8795 return err;
8798 struct report_file_info_arg {
8799 struct got_worktree *worktree;
8800 got_worktree_path_info_cb info_cb;
8801 void *info_arg;
8802 struct got_pathlist_head *paths;
8803 got_cancel_cb cancel_cb;
8804 void *cancel_arg;
8807 static const struct got_error *
8808 report_file_info(void *arg, struct got_fileindex_entry *ie)
8810 struct report_file_info_arg *a = arg;
8811 struct got_pathlist_entry *pe;
8812 struct got_object_id blob_id, staged_blob_id, commit_id;
8813 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8814 struct got_object_id *commit_idp = NULL;
8815 int stage;
8817 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8818 return got_error(GOT_ERR_CANCELLED);
8820 TAILQ_FOREACH(pe, a->paths, entry) {
8821 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8822 got_path_is_child(ie->path, pe->path, pe->path_len))
8823 break;
8825 if (pe == NULL) /* not found */
8826 return NULL;
8828 if (got_fileindex_entry_has_blob(ie)) {
8829 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8830 blob_idp = &blob_id;
8832 stage = got_fileindex_entry_stage_get(ie);
8833 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8834 stage == GOT_FILEIDX_STAGE_ADD) {
8835 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8836 SHA1_DIGEST_LENGTH);
8837 staged_blob_idp = &staged_blob_id;
8840 if (got_fileindex_entry_has_commit(ie)) {
8841 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8842 commit_idp = &commit_id;
8845 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8846 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8849 const struct got_error *
8850 got_worktree_path_info(struct got_worktree *worktree,
8851 struct got_pathlist_head *paths,
8852 got_worktree_path_info_cb info_cb, void *info_arg,
8853 got_cancel_cb cancel_cb, void *cancel_arg)
8856 const struct got_error *err = NULL, *unlockerr;
8857 struct got_fileindex *fileindex = NULL;
8858 char *fileindex_path = NULL;
8859 struct report_file_info_arg arg;
8861 err = lock_worktree(worktree, LOCK_SH);
8862 if (err)
8863 return err;
8865 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8866 if (err)
8867 goto done;
8869 arg.worktree = worktree;
8870 arg.info_cb = info_cb;
8871 arg.info_arg = info_arg;
8872 arg.paths = paths;
8873 arg.cancel_cb = cancel_cb;
8874 arg.cancel_arg = cancel_arg;
8875 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8876 &arg);
8877 done:
8878 free(fileindex_path);
8879 if (fileindex)
8880 got_fileindex_free(fileindex);
8881 unlockerr = lock_worktree(worktree, LOCK_UN);
8882 if (unlockerr && err == NULL)
8883 err = unlockerr;
8884 return err;
8887 static const struct got_error *
8888 patch_check_path(const char *p, char **path, unsigned char *status,
8889 unsigned char *staged_status, struct got_fileindex *fileindex,
8890 struct got_worktree *worktree, struct got_repository *repo)
8892 const struct got_error *err;
8893 struct got_fileindex_entry *ie;
8894 struct stat sb;
8895 char *ondisk_path = NULL;
8897 err = got_worktree_resolve_path(path, worktree, p);
8898 if (err)
8899 return err;
8901 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8902 *path[0] ? "/" : "", *path) == -1)
8903 return got_error_from_errno("asprintf");
8905 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8906 if (ie) {
8907 *staged_status = get_staged_status(ie);
8908 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8909 repo);
8910 if (err)
8911 goto done;
8912 } else {
8913 *staged_status = GOT_STATUS_NO_CHANGE;
8914 *status = GOT_STATUS_UNVERSIONED;
8915 if (lstat(ondisk_path, &sb) == -1) {
8916 if (errno != ENOENT) {
8917 err = got_error_from_errno2("lstat",
8918 ondisk_path);
8919 goto done;
8921 *status = GOT_STATUS_NONEXISTENT;
8925 done:
8926 free(ondisk_path);
8927 return err;
8930 static const struct got_error *
8931 patch_can_rm(const char *path, unsigned char status,
8932 unsigned char staged_status)
8934 if (status == GOT_STATUS_NONEXISTENT)
8935 return got_error_set_errno(ENOENT, path);
8936 if (status != GOT_STATUS_NO_CHANGE &&
8937 status != GOT_STATUS_ADD &&
8938 status != GOT_STATUS_MODIFY &&
8939 status != GOT_STATUS_MODE_CHANGE)
8940 return got_error_path(path, GOT_ERR_FILE_STATUS);
8941 if (staged_status == GOT_STATUS_DELETE)
8942 return got_error_path(path, GOT_ERR_FILE_STATUS);
8943 return NULL;
8946 static const struct got_error *
8947 patch_can_add(const char *path, unsigned char status)
8949 if (status != GOT_STATUS_NONEXISTENT)
8950 return got_error_path(path, GOT_ERR_FILE_STATUS);
8951 return NULL;
8954 static const struct got_error *
8955 patch_can_edit(const char *path, unsigned char status,
8956 unsigned char staged_status)
8958 if (status == GOT_STATUS_NONEXISTENT)
8959 return got_error_set_errno(ENOENT, path);
8960 if (status != GOT_STATUS_NO_CHANGE &&
8961 status != GOT_STATUS_ADD &&
8962 status != GOT_STATUS_MODIFY)
8963 return got_error_path(path, GOT_ERR_FILE_STATUS);
8964 if (staged_status == GOT_STATUS_DELETE)
8965 return got_error_path(path, GOT_ERR_FILE_STATUS);
8966 return NULL;
8969 const struct got_error *
8970 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8971 char **fileindex_path, struct got_worktree *worktree)
8973 return open_fileindex(fileindex, fileindex_path, worktree);
8976 const struct got_error *
8977 got_worktree_patch_check_path(const char *old, const char *new,
8978 char **oldpath, char **newpath, struct got_worktree *worktree,
8979 struct got_repository *repo, struct got_fileindex *fileindex)
8981 const struct got_error *err = NULL;
8982 int file_renamed = 0;
8983 unsigned char status_old, staged_status_old;
8984 unsigned char status_new, staged_status_new;
8986 *oldpath = NULL;
8987 *newpath = NULL;
8989 err = patch_check_path(old != NULL ? old : new, oldpath,
8990 &status_old, &staged_status_old, fileindex, worktree, repo);
8991 if (err)
8992 goto done;
8994 err = patch_check_path(new != NULL ? new : old, newpath,
8995 &status_new, &staged_status_new, fileindex, worktree, repo);
8996 if (err)
8997 goto done;
8999 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9000 file_renamed = 1;
9002 if (old != NULL && new == NULL)
9003 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9004 else if (file_renamed) {
9005 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9006 if (err == NULL)
9007 err = patch_can_add(*newpath, status_new);
9008 } else if (old == NULL)
9009 err = patch_can_add(*newpath, status_new);
9010 else
9011 err = patch_can_edit(*newpath, status_new, staged_status_new);
9013 done:
9014 if (err) {
9015 free(*oldpath);
9016 *oldpath = NULL;
9017 free(*newpath);
9018 *newpath = NULL;
9020 return err;
9023 const struct got_error *
9024 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9025 struct got_worktree *worktree, struct got_fileindex *fileindex,
9026 got_worktree_checkout_cb progress_cb, void *progress_arg)
9028 struct schedule_addition_args saa;
9030 memset(&saa, 0, sizeof(saa));
9031 saa.worktree = worktree;
9032 saa.fileindex = fileindex;
9033 saa.progress_cb = progress_cb;
9034 saa.progress_arg = progress_arg;
9035 saa.repo = repo;
9037 return worktree_status(worktree, path, fileindex, repo,
9038 schedule_addition, &saa, NULL, NULL, 1, 0);
9041 const struct got_error *
9042 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9043 struct got_worktree *worktree, struct got_fileindex *fileindex,
9044 got_worktree_delete_cb progress_cb, void *progress_arg)
9046 struct schedule_deletion_args sda;
9048 memset(&sda, 0, sizeof(sda));
9049 sda.worktree = worktree;
9050 sda.fileindex = fileindex;
9051 sda.progress_cb = progress_cb;
9052 sda.progress_arg = progress_arg;
9053 sda.repo = repo;
9054 sda.delete_local_mods = 0;
9055 sda.keep_on_disk = 0;
9056 sda.ignore_missing_paths = 0;
9057 sda.status_codes = NULL;
9059 return worktree_status(worktree, path, fileindex, repo,
9060 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9063 const struct got_error *
9064 got_worktree_patch_complete(struct got_fileindex *fileindex,
9065 const char *fileindex_path)
9067 const struct got_error *err = NULL;
9069 err = sync_fileindex(fileindex, fileindex_path);
9070 got_fileindex_free(fileindex);
9072 return err;