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 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1260 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1262 *is_bad_symlink = 0;
1265 * Blob object content specifies the target path of the link.
1266 * If a symbolic link cannot be installed we instead create
1267 * a regular file which contains the link target path stored
1268 * in the blob object.
1270 do {
1271 err = got_object_blob_read_block(&len, blob);
1272 if (err)
1273 return err;
1275 if (len + target_len >= sizeof(target_path)) {
1276 /* Path too long; install as a regular file. */
1277 *is_bad_symlink = 1;
1278 got_object_blob_rewind(blob);
1279 return install_blob(worktree, ondisk_path, path,
1280 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1281 restoring_missing_file, reverting_versioned_file,
1282 1, path_is_unversioned, repo, progress_cb,
1283 progress_arg);
1285 if (len > 0) {
1286 /* Skip blob object header first time around. */
1287 memcpy(target_path + target_len, buf + hdrlen,
1288 len - hdrlen);
1289 target_len += len - hdrlen;
1290 hdrlen = 0;
1292 } while (len != 0);
1293 target_path[target_len] = '\0';
1295 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1296 ondisk_path, worktree->root_path);
1297 if (err)
1298 return err;
1300 if (*is_bad_symlink && !allow_bad_symlinks) {
1301 /* install as a regular file */
1302 got_object_blob_rewind(blob);
1303 err = install_blob(worktree, ondisk_path, path,
1304 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1305 restoring_missing_file, reverting_versioned_file, 1,
1306 path_is_unversioned, repo, progress_cb, progress_arg);
1307 return err;
1310 if (symlink(target_path, ondisk_path) == -1) {
1311 if (errno == EEXIST) {
1312 int symlink_replaced;
1313 if (path_is_unversioned) {
1314 err = (*progress_cb)(progress_arg,
1315 GOT_STATUS_UNVERSIONED, path);
1316 return err;
1318 err = replace_existing_symlink(&symlink_replaced,
1319 ondisk_path, target_path, target_len);
1320 if (err)
1321 return err;
1322 if (progress_cb) {
1323 if (symlink_replaced) {
1324 err = (*progress_cb)(progress_arg,
1325 reverting_versioned_file ?
1326 GOT_STATUS_REVERT :
1327 GOT_STATUS_UPDATE, path);
1328 } else {
1329 err = (*progress_cb)(progress_arg,
1330 GOT_STATUS_EXISTS, path);
1333 return err; /* Nothing else to do. */
1336 if (errno == ENOENT) {
1337 char *parent;
1338 err = got_path_dirname(&parent, ondisk_path);
1339 if (err)
1340 return err;
1341 err = add_dir_on_disk(worktree, parent);
1342 free(parent);
1343 if (err)
1344 return err;
1346 * Retry, and fall through to error handling
1347 * below if this second attempt fails.
1349 if (symlink(target_path, ondisk_path) != -1) {
1350 err = NULL; /* success */
1351 return err;
1355 /* Handle errors from first or second creation attempt. */
1356 if (errno == ENAMETOOLONG) {
1357 /* bad target path; install as a regular file */
1358 *is_bad_symlink = 1;
1359 got_object_blob_rewind(blob);
1360 err = install_blob(worktree, ondisk_path, path,
1361 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1362 restoring_missing_file, reverting_versioned_file, 1,
1363 path_is_unversioned, repo,
1364 progress_cb, progress_arg);
1365 } else if (errno == ENOTDIR) {
1366 err = got_error_path(ondisk_path,
1367 GOT_ERR_FILE_OBSTRUCTED);
1368 } else {
1369 err = got_error_from_errno3("symlink",
1370 target_path, ondisk_path);
1372 } else if (progress_cb)
1373 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1374 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
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;
3139 int fd1 = -1, fd2 = -1;
3141 if (commit_id1) {
3142 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3143 if (err)
3144 goto done;
3145 err = got_object_id_by_path(&tree_id1, repo, commit1,
3146 worktree->path_prefix);
3147 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3148 goto done;
3150 if (tree_id1) {
3151 char *id_str;
3153 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3154 if (err)
3155 goto done;
3157 err = got_object_id_str(&id_str, commit_id1);
3158 if (err)
3159 goto done;
3161 if (asprintf(&label_orig, "%s: commit %s",
3162 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3163 err = got_error_from_errno("asprintf");
3164 free(id_str);
3165 goto done;
3167 free(id_str);
3169 f1 = got_opentemp();
3170 if (f1 == NULL) {
3171 err = got_error_from_errno("got_opentemp");
3172 goto done;
3176 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3177 if (err)
3178 goto done;
3180 err = got_object_id_by_path(&tree_id2, repo, commit2,
3181 worktree->path_prefix);
3182 if (err)
3183 goto done;
3185 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3186 if (err)
3187 goto done;
3189 f2 = got_opentemp();
3190 if (f2 == NULL) {
3191 err = got_error_from_errno("got_opentemp");
3192 goto done;
3195 fd1 = got_opentempfd();
3196 if (fd1 == -1) {
3197 err = got_error_from_errno("got_opentempfd");
3198 goto done;
3201 fd2 = got_opentempfd();
3202 if (fd2 == -1) {
3203 err = got_error_from_errno("got_opentempfd");
3204 goto done;
3207 cmc_arg.worktree = worktree;
3208 cmc_arg.fileindex = fileindex;
3209 cmc_arg.repo = repo;
3210 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3211 check_merge_conflicts, &cmc_arg, 0);
3212 if (err)
3213 goto done;
3215 arg.worktree = worktree;
3216 arg.fileindex = fileindex;
3217 arg.progress_cb = progress_cb;
3218 arg.progress_arg = progress_arg;
3219 arg.cancel_cb = cancel_cb;
3220 arg.cancel_arg = cancel_arg;
3221 arg.label_orig = label_orig;
3222 arg.commit_id2 = commit_id2;
3223 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 merge_file_cb, &arg, 1);
3226 sync_err = sync_fileindex(fileindex, fileindex_path);
3227 if (sync_err && err == NULL)
3228 err = sync_err;
3229 done:
3230 if (commit1)
3231 got_object_commit_close(commit1);
3232 if (commit2)
3233 got_object_commit_close(commit2);
3234 if (tree1)
3235 got_object_tree_close(tree1);
3236 if (tree2)
3237 got_object_tree_close(tree2);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno("fclose");
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno("fclose");
3242 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 err = got_error_from_errno("close");
3246 free(label_orig);
3247 return err;
3250 const struct got_error *
3251 got_worktree_merge_files(struct got_worktree *worktree,
3252 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3253 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3254 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3256 const struct got_error *err, *unlockerr;
3257 char *fileindex_path = NULL;
3258 struct got_fileindex *fileindex = NULL;
3260 err = lock_worktree(worktree, LOCK_EX);
3261 if (err)
3262 return err;
3264 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3265 if (err)
3266 goto done;
3268 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3269 worktree);
3270 if (err)
3271 goto done;
3273 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3274 commit_id2, repo, progress_cb, progress_arg,
3275 cancel_cb, cancel_arg);
3276 done:
3277 if (fileindex)
3278 got_fileindex_free(fileindex);
3279 free(fileindex_path);
3280 unlockerr = lock_worktree(worktree, LOCK_SH);
3281 if (unlockerr && err == NULL)
3282 err = unlockerr;
3283 return err;
3286 struct diff_dir_cb_arg {
3287 struct got_fileindex *fileindex;
3288 struct got_worktree *worktree;
3289 const char *status_path;
3290 size_t status_path_len;
3291 struct got_repository *repo;
3292 got_worktree_status_cb status_cb;
3293 void *status_arg;
3294 got_cancel_cb cancel_cb;
3295 void *cancel_arg;
3296 /* A pathlist containing per-directory pathlists of ignore patterns. */
3297 struct got_pathlist_head *ignores;
3298 int report_unchanged;
3299 int no_ignores;
3302 static const struct got_error *
3303 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3304 int dirfd, const char *de_name,
3305 got_worktree_status_cb status_cb, void *status_arg,
3306 struct got_repository *repo, int report_unchanged)
3308 const struct got_error *err = NULL;
3309 unsigned char status = GOT_STATUS_NO_CHANGE;
3310 unsigned char staged_status = get_staged_status(ie);
3311 struct stat sb;
3312 struct got_object_id blob_id, commit_id, staged_blob_id;
3313 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3314 struct got_object_id *staged_blob_idp = NULL;
3316 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3317 if (err)
3318 return err;
3320 if (status == GOT_STATUS_NO_CHANGE &&
3321 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3322 return NULL;
3324 if (got_fileindex_entry_has_blob(ie)) {
3325 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3326 blob_idp = &blob_id;
3328 if (got_fileindex_entry_has_commit(ie)) {
3329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3330 commit_idp = &commit_id;
3332 if (staged_status == GOT_STATUS_ADD ||
3333 staged_status == GOT_STATUS_MODIFY) {
3334 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3335 SHA1_DIGEST_LENGTH);
3336 staged_blob_idp = &staged_blob_id;
3339 return (*status_cb)(status_arg, status, staged_status,
3340 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3343 static const struct got_error *
3344 status_old_new(void *arg, struct got_fileindex_entry *ie,
3345 struct dirent *de, const char *parent_path, int dirfd)
3347 const struct got_error *err = NULL;
3348 struct diff_dir_cb_arg *a = arg;
3349 char *abspath;
3351 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3352 return got_error(GOT_ERR_CANCELLED);
3354 if (got_path_cmp(parent_path, a->status_path,
3355 strlen(parent_path), a->status_path_len) != 0 &&
3356 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3357 return NULL;
3359 if (parent_path[0]) {
3360 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3361 parent_path, de->d_name) == -1)
3362 return got_error_from_errno("asprintf");
3363 } else {
3364 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3365 de->d_name) == -1)
3366 return got_error_from_errno("asprintf");
3369 err = report_file_status(ie, abspath, dirfd, de->d_name,
3370 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3371 free(abspath);
3372 return err;
3375 static const struct got_error *
3376 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3378 struct diff_dir_cb_arg *a = arg;
3379 struct got_object_id blob_id, commit_id;
3380 unsigned char status;
3382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3383 return got_error(GOT_ERR_CANCELLED);
3385 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3386 return NULL;
3388 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3389 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3390 if (got_fileindex_entry_has_file_on_disk(ie))
3391 status = GOT_STATUS_MISSING;
3392 else
3393 status = GOT_STATUS_DELETE;
3394 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3395 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3398 static void
3399 free_ignorelist(struct got_pathlist_head *ignorelist)
3401 struct got_pathlist_entry *pe;
3403 TAILQ_FOREACH(pe, ignorelist, entry)
3404 free((char *)pe->path);
3405 got_pathlist_free(ignorelist);
3408 static void
3409 free_ignores(struct got_pathlist_head *ignores)
3411 struct got_pathlist_entry *pe;
3413 TAILQ_FOREACH(pe, ignores, entry) {
3414 struct got_pathlist_head *ignorelist = pe->data;
3415 free_ignorelist(ignorelist);
3416 free((char *)pe->path);
3418 got_pathlist_free(ignores);
3421 static const struct got_error *
3422 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3424 const struct got_error *err = NULL;
3425 struct got_pathlist_entry *pe = NULL;
3426 struct got_pathlist_head *ignorelist;
3427 char *line = NULL, *pattern, *dirpath = NULL;
3428 size_t linesize = 0;
3429 ssize_t linelen;
3431 ignorelist = calloc(1, sizeof(*ignorelist));
3432 if (ignorelist == NULL)
3433 return got_error_from_errno("calloc");
3434 TAILQ_INIT(ignorelist);
3436 while ((linelen = getline(&line, &linesize, f)) != -1) {
3437 if (linelen > 0 && line[linelen - 1] == '\n')
3438 line[linelen - 1] = '\0';
3440 /* Git's ignores may contain comments. */
3441 if (line[0] == '#')
3442 continue;
3444 /* Git's negated patterns are not (yet?) supported. */
3445 if (line[0] == '!')
3446 continue;
3448 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3449 line) == -1) {
3450 err = got_error_from_errno("asprintf");
3451 goto done;
3453 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3454 if (err)
3455 goto done;
3457 if (ferror(f)) {
3458 err = got_error_from_errno("getline");
3459 goto done;
3462 dirpath = strdup(path);
3463 if (dirpath == NULL) {
3464 err = got_error_from_errno("strdup");
3465 goto done;
3467 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3468 done:
3469 free(line);
3470 if (err || pe == NULL) {
3471 free(dirpath);
3472 free_ignorelist(ignorelist);
3474 return err;
3477 static int
3478 match_ignores(struct got_pathlist_head *ignores, const char *path)
3480 struct got_pathlist_entry *pe;
3482 /* Handle patterns which match in all directories. */
3483 TAILQ_FOREACH(pe, ignores, entry) {
3484 struct got_pathlist_head *ignorelist = pe->data;
3485 struct got_pathlist_entry *pi;
3487 TAILQ_FOREACH(pi, ignorelist, entry) {
3488 const char *p, *pattern = pi->path;
3490 if (strncmp(pattern, "**/", 3) != 0)
3491 continue;
3492 pattern += 3;
3493 p = path;
3494 while (*p) {
3495 if (fnmatch(pattern, p,
3496 FNM_PATHNAME | FNM_LEADING_DIR)) {
3497 /* Retry in next directory. */
3498 while (*p && *p != '/')
3499 p++;
3500 while (*p == '/')
3501 p++;
3502 continue;
3504 return 1;
3510 * The ignores pathlist contains ignore lists from children before
3511 * parents, so we can find the most specific ignorelist by walking
3512 * ignores backwards.
3514 pe = TAILQ_LAST(ignores, got_pathlist_head);
3515 while (pe) {
3516 if (got_path_is_child(path, pe->path, pe->path_len)) {
3517 struct got_pathlist_head *ignorelist = pe->data;
3518 struct got_pathlist_entry *pi;
3519 TAILQ_FOREACH(pi, ignorelist, entry) {
3520 const char *pattern = pi->path;
3521 int flags = FNM_LEADING_DIR;
3522 if (strstr(pattern, "/**/") == NULL)
3523 flags |= FNM_PATHNAME;
3524 if (fnmatch(pattern, path, flags))
3525 continue;
3526 return 1;
3529 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3532 return 0;
3535 static const struct got_error *
3536 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3537 const char *path, int dirfd, const char *ignores_filename)
3539 const struct got_error *err = NULL;
3540 char *ignorespath;
3541 int fd = -1;
3542 FILE *ignoresfile = NULL;
3544 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3545 path[0] ? "/" : "", ignores_filename) == -1)
3546 return got_error_from_errno("asprintf");
3548 if (dirfd != -1) {
3549 fd = openat(dirfd, ignores_filename,
3550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3551 if (fd == -1) {
3552 if (errno != ENOENT && errno != EACCES)
3553 err = got_error_from_errno2("openat",
3554 ignorespath);
3555 } else {
3556 ignoresfile = fdopen(fd, "r");
3557 if (ignoresfile == NULL)
3558 err = got_error_from_errno2("fdopen",
3559 ignorespath);
3560 else {
3561 fd = -1;
3562 err = read_ignores(ignores, path, ignoresfile);
3565 } else {
3566 ignoresfile = fopen(ignorespath, "re");
3567 if (ignoresfile == NULL) {
3568 if (errno != ENOENT && errno != EACCES)
3569 err = got_error_from_errno2("fopen",
3570 ignorespath);
3571 } else
3572 err = read_ignores(ignores, path, ignoresfile);
3575 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3576 err = got_error_from_errno2("fclose", path);
3577 if (fd != -1 && close(fd) == -1 && err == NULL)
3578 err = got_error_from_errno2("close", path);
3579 free(ignorespath);
3580 return err;
3583 static const struct got_error *
3584 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3585 int dirfd)
3587 const struct got_error *err = NULL;
3588 struct diff_dir_cb_arg *a = arg;
3589 char *path = NULL;
3591 if (ignore != NULL)
3592 *ignore = 0;
3594 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 return got_error(GOT_ERR_CANCELLED);
3597 if (parent_path[0]) {
3598 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 return got_error_from_errno("asprintf");
3600 } else {
3601 path = de->d_name;
3604 if (de->d_type == DT_DIR) {
3605 if (!a->no_ignores && ignore != NULL &&
3606 match_ignores(a->ignores, path))
3607 *ignore = 1;
3608 } else if (!match_ignores(a->ignores, path) &&
3609 got_path_is_child(path, a->status_path, a->status_path_len))
3610 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3611 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3612 if (parent_path[0])
3613 free(path);
3614 return err;
3617 static const struct got_error *
3618 status_traverse(void *arg, const char *path, int dirfd)
3620 const struct got_error *err = NULL;
3621 struct diff_dir_cb_arg *a = arg;
3623 if (a->no_ignores)
3624 return NULL;
3626 err = add_ignores(a->ignores, a->worktree->root_path,
3627 path, dirfd, ".cvsignore");
3628 if (err)
3629 return err;
3631 err = add_ignores(a->ignores, a->worktree->root_path, path,
3632 dirfd, ".gitignore");
3634 return err;
3637 static const struct got_error *
3638 report_single_file_status(const char *path, const char *ondisk_path,
3639 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3640 void *status_arg, struct got_repository *repo, int report_unchanged,
3641 struct got_pathlist_head *ignores, int no_ignores)
3643 struct got_fileindex_entry *ie;
3644 struct stat sb;
3646 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3647 if (ie)
3648 return report_file_status(ie, ondisk_path, -1, NULL,
3649 status_cb, status_arg, repo, report_unchanged);
3651 if (lstat(ondisk_path, &sb) == -1) {
3652 if (errno != ENOENT)
3653 return got_error_from_errno2("lstat", ondisk_path);
3654 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3658 if (!no_ignores && match_ignores(ignores, path))
3659 return NULL;
3661 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3662 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3663 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3665 return NULL;
3668 static const struct got_error *
3669 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3670 const char *root_path, const char *path)
3672 const struct got_error *err;
3673 char *parent_path, *next_parent_path = NULL;
3675 err = add_ignores(ignores, root_path, "", -1,
3676 ".cvsignore");
3677 if (err)
3678 return err;
3680 err = add_ignores(ignores, root_path, "", -1,
3681 ".gitignore");
3682 if (err)
3683 return err;
3685 err = got_path_dirname(&parent_path, path);
3686 if (err) {
3687 if (err->code == GOT_ERR_BAD_PATH)
3688 return NULL; /* cannot traverse parent */
3689 return err;
3691 for (;;) {
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3693 ".cvsignore");
3694 if (err)
3695 break;
3696 err = add_ignores(ignores, root_path, parent_path, -1,
3697 ".gitignore");
3698 if (err)
3699 break;
3700 err = got_path_dirname(&next_parent_path, parent_path);
3701 if (err) {
3702 if (err->code == GOT_ERR_BAD_PATH)
3703 err = NULL; /* traversed everything */
3704 break;
3706 if (got_path_is_root_dir(parent_path))
3707 break;
3708 free(parent_path);
3709 parent_path = next_parent_path;
3710 next_parent_path = NULL;
3713 free(parent_path);
3714 free(next_parent_path);
3715 return err;
3718 static const struct got_error *
3719 worktree_status(struct got_worktree *worktree, const char *path,
3720 struct got_fileindex *fileindex, struct got_repository *repo,
3721 got_worktree_status_cb status_cb, void *status_arg,
3722 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3723 int report_unchanged)
3725 const struct got_error *err = NULL;
3726 int fd = -1;
3727 struct got_fileindex_diff_dir_cb fdiff_cb;
3728 struct diff_dir_cb_arg arg;
3729 char *ondisk_path = NULL;
3730 struct got_pathlist_head ignores;
3731 struct got_fileindex_entry *ie;
3733 TAILQ_INIT(&ignores);
3735 if (asprintf(&ondisk_path, "%s%s%s",
3736 worktree->root_path, path[0] ? "/" : "", path) == -1)
3737 return got_error_from_errno("asprintf");
3739 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3740 if (ie) {
3741 err = report_single_file_status(path, ondisk_path,
3742 fileindex, status_cb, status_arg, repo,
3743 report_unchanged, &ignores, no_ignores);
3744 goto done;
3747 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3748 if (fd == -1) {
3749 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3750 !got_err_open_nofollow_on_symlink())
3751 err = got_error_from_errno2("open", ondisk_path);
3752 else {
3753 if (!no_ignores) {
3754 err = add_ignores_from_parent_paths(&ignores,
3755 worktree->root_path, ondisk_path);
3756 if (err)
3757 goto done;
3759 err = report_single_file_status(path, ondisk_path,
3760 fileindex, status_cb, status_arg, repo,
3761 report_unchanged, &ignores, no_ignores);
3763 } else {
3764 fdiff_cb.diff_old_new = status_old_new;
3765 fdiff_cb.diff_old = status_old;
3766 fdiff_cb.diff_new = status_new;
3767 fdiff_cb.diff_traverse = status_traverse;
3768 arg.fileindex = fileindex;
3769 arg.worktree = worktree;
3770 arg.status_path = path;
3771 arg.status_path_len = strlen(path);
3772 arg.repo = repo;
3773 arg.status_cb = status_cb;
3774 arg.status_arg = status_arg;
3775 arg.cancel_cb = cancel_cb;
3776 arg.cancel_arg = cancel_arg;
3777 arg.report_unchanged = report_unchanged;
3778 arg.no_ignores = no_ignores;
3779 if (!no_ignores) {
3780 err = add_ignores_from_parent_paths(&ignores,
3781 worktree->root_path, path);
3782 if (err)
3783 goto done;
3785 arg.ignores = &ignores;
3786 err = got_fileindex_diff_dir(fileindex, fd,
3787 worktree->root_path, path, repo, &fdiff_cb, &arg);
3789 done:
3790 free_ignores(&ignores);
3791 if (fd != -1 && close(fd) == -1 && err == NULL)
3792 err = got_error_from_errno("close");
3793 free(ondisk_path);
3794 return err;
3797 const struct got_error *
3798 got_worktree_status(struct got_worktree *worktree,
3799 struct got_pathlist_head *paths, struct got_repository *repo,
3800 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3801 got_cancel_cb cancel_cb, void *cancel_arg)
3803 const struct got_error *err = NULL;
3804 char *fileindex_path = NULL;
3805 struct got_fileindex *fileindex = NULL;
3806 struct got_pathlist_entry *pe;
3808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3809 if (err)
3810 return err;
3812 TAILQ_FOREACH(pe, paths, entry) {
3813 err = worktree_status(worktree, pe->path, fileindex, repo,
3814 status_cb, status_arg, cancel_cb, cancel_arg,
3815 no_ignores, 0);
3816 if (err)
3817 break;
3819 free(fileindex_path);
3820 got_fileindex_free(fileindex);
3821 return err;
3824 const struct got_error *
3825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3826 const char *arg)
3828 const struct got_error *err = NULL;
3829 char *resolved = NULL, *cwd = NULL, *path = NULL;
3830 size_t len;
3831 struct stat sb;
3832 char *abspath = NULL;
3833 char canonpath[PATH_MAX];
3835 *wt_path = NULL;
3837 cwd = getcwd(NULL, 0);
3838 if (cwd == NULL)
3839 return got_error_from_errno("getcwd");
3841 if (lstat(arg, &sb) == -1) {
3842 if (errno != ENOENT) {
3843 err = got_error_from_errno2("lstat", arg);
3844 goto done;
3846 sb.st_mode = 0;
3848 if (S_ISLNK(sb.st_mode)) {
3850 * We cannot use realpath(3) with symlinks since we want to
3851 * operate on the symlink itself.
3852 * But we can make the path absolute, assuming it is relative
3853 * to the current working directory, and then canonicalize it.
3855 if (!got_path_is_absolute(arg)) {
3856 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3857 err = got_error_from_errno("asprintf");
3858 goto done;
3862 err = got_canonpath(abspath ? abspath : arg, canonpath,
3863 sizeof(canonpath));
3864 if (err)
3865 goto done;
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3869 goto done;
3871 } else {
3872 resolved = realpath(arg, NULL);
3873 if (resolved == NULL) {
3874 if (errno != ENOENT) {
3875 err = got_error_from_errno2("realpath", arg);
3876 goto done;
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3880 goto done;
3882 err = got_canonpath(abspath, canonpath,
3883 sizeof(canonpath));
3884 if (err)
3885 goto done;
3886 resolved = strdup(canonpath);
3887 if (resolved == NULL) {
3888 err = got_error_from_errno("strdup");
3889 goto done;
3894 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3895 strlen(got_worktree_get_root_path(worktree)))) {
3896 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3897 goto done;
3900 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3901 err = got_path_skip_common_ancestor(&path,
3902 got_worktree_get_root_path(worktree), resolved);
3903 if (err)
3904 goto done;
3905 } else {
3906 path = strdup("");
3907 if (path == NULL) {
3908 err = got_error_from_errno("strdup");
3909 goto done;
3913 /* XXX status walk can't deal with trailing slash! */
3914 len = strlen(path);
3915 while (len > 0 && path[len - 1] == '/') {
3916 path[len - 1] = '\0';
3917 len--;
3919 done:
3920 free(abspath);
3921 free(resolved);
3922 free(cwd);
3923 if (err == NULL)
3924 *wt_path = path;
3925 else
3926 free(path);
3927 return err;
3930 struct schedule_addition_args {
3931 struct got_worktree *worktree;
3932 struct got_fileindex *fileindex;
3933 got_worktree_checkout_cb progress_cb;
3934 void *progress_arg;
3935 struct got_repository *repo;
3938 static const struct got_error *
3939 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3940 const char *relpath, struct got_object_id *blob_id,
3941 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3942 int dirfd, const char *de_name)
3944 struct schedule_addition_args *a = arg;
3945 const struct got_error *err = NULL;
3946 struct got_fileindex_entry *ie;
3947 struct stat sb;
3948 char *ondisk_path;
3950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3951 relpath) == -1)
3952 return got_error_from_errno("asprintf");
3954 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3955 if (ie) {
3956 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3957 de_name, a->repo);
3958 if (err)
3959 goto done;
3960 /* Re-adding an existing entry is a no-op. */
3961 if (status == GOT_STATUS_ADD)
3962 goto done;
3963 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3964 if (err)
3965 goto done;
3968 if (status != GOT_STATUS_UNVERSIONED) {
3969 if (status == GOT_STATUS_NONEXISTENT)
3970 err = got_error_set_errno(ENOENT, ondisk_path);
3971 else
3972 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3973 goto done;
3976 err = got_fileindex_entry_alloc(&ie, relpath);
3977 if (err)
3978 goto done;
3979 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3980 relpath, NULL, NULL, 1);
3981 if (err) {
3982 got_fileindex_entry_free(ie);
3983 goto done;
3985 err = got_fileindex_entry_add(a->fileindex, ie);
3986 if (err) {
3987 got_fileindex_entry_free(ie);
3988 goto done;
3990 done:
3991 free(ondisk_path);
3992 if (err)
3993 return err;
3994 if (status == GOT_STATUS_ADD)
3995 return NULL;
3996 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_add(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 struct got_repository *repo, int no_ignores)
4005 struct got_fileindex *fileindex = NULL;
4006 char *fileindex_path = NULL;
4007 const struct got_error *err = NULL, *sync_err, *unlockerr;
4008 struct got_pathlist_entry *pe;
4009 struct schedule_addition_args saa;
4011 err = lock_worktree(worktree, LOCK_EX);
4012 if (err)
4013 return err;
4015 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4016 if (err)
4017 goto done;
4019 saa.worktree = worktree;
4020 saa.fileindex = fileindex;
4021 saa.progress_cb = progress_cb;
4022 saa.progress_arg = progress_arg;
4023 saa.repo = repo;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4028 if (err)
4029 break;
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4033 err = sync_err;
4034 done:
4035 free(fileindex_path);
4036 if (fileindex)
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4040 err = unlockerr;
4041 return err;
4044 struct schedule_deletion_args {
4045 struct got_worktree *worktree;
4046 struct got_fileindex *fileindex;
4047 got_worktree_delete_cb progress_cb;
4048 void *progress_arg;
4049 struct got_repository *repo;
4050 int delete_local_mods;
4051 int keep_on_disk;
4052 int ignore_missing_paths;
4053 const char *status_codes;
4056 static const struct got_error *
4057 schedule_for_deletion(void *arg, unsigned char status,
4058 unsigned char staged_status, const char *relpath,
4059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 struct got_object_id *commit_id, int dirfd, const char *de_name)
4062 struct schedule_deletion_args *a = arg;
4063 const struct got_error *err = NULL;
4064 struct got_fileindex_entry *ie = NULL;
4065 struct stat sb;
4066 char *ondisk_path;
4068 if (status == GOT_STATUS_NONEXISTENT) {
4069 if (a->ignore_missing_paths)
4070 return NULL;
4071 return got_error_set_errno(ENOENT, relpath);
4074 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4075 if (ie == NULL)
4076 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4078 staged_status = get_staged_status(ie);
4079 if (staged_status != GOT_STATUS_NO_CHANGE) {
4080 if (staged_status == GOT_STATUS_DELETE)
4081 return NULL;
4082 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4085 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4086 relpath) == -1)
4087 return got_error_from_errno("asprintf");
4089 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4090 a->repo);
4091 if (err)
4092 goto done;
4094 if (a->status_codes) {
4095 size_t ncodes = strlen(a->status_codes);
4096 int i;
4097 for (i = 0; i < ncodes ; i++) {
4098 if (status == a->status_codes[i])
4099 break;
4101 if (i == ncodes) {
4102 /* Do not delete files in non-matching status. */
4103 free(ondisk_path);
4104 return NULL;
4106 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4107 a->status_codes[i] != GOT_STATUS_MISSING) {
4108 static char msg[64];
4109 snprintf(msg, sizeof(msg),
4110 "invalid status code '%c'", a->status_codes[i]);
4111 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4112 goto done;
4116 if (status != GOT_STATUS_NO_CHANGE) {
4117 if (status == GOT_STATUS_DELETE)
4118 goto done;
4119 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4120 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4121 goto done;
4123 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4124 err = got_error_set_errno(ENOENT, relpath);
4125 goto done;
4127 if (status != GOT_STATUS_MODIFY &&
4128 status != GOT_STATUS_MISSING) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4130 goto done;
4134 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4135 size_t root_len;
4137 if (dirfd != -1) {
4138 if (unlinkat(dirfd, de_name, 0) == -1) {
4139 err = got_error_from_errno2("unlinkat",
4140 ondisk_path);
4141 goto done;
4143 } else if (unlink(ondisk_path) == -1) {
4144 err = got_error_from_errno2("unlink", ondisk_path);
4145 goto done;
4148 root_len = strlen(a->worktree->root_path);
4149 do {
4150 char *parent;
4151 err = got_path_dirname(&parent, ondisk_path);
4152 if (err)
4153 goto done;
4154 free(ondisk_path);
4155 ondisk_path = parent;
4156 if (rmdir(ondisk_path) == -1) {
4157 if (errno != ENOTEMPTY)
4158 err = got_error_from_errno2("rmdir",
4159 ondisk_path);
4160 break;
4162 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 strlen(ondisk_path), root_len) != 0);
4166 got_fileindex_entry_mark_deleted_from_disk(ie);
4167 done:
4168 free(ondisk_path);
4169 if (err)
4170 return err;
4171 if (status == GOT_STATUS_DELETE)
4172 return NULL;
4173 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 staged_status, relpath);
4177 const struct got_error *
4178 got_worktree_schedule_delete(struct got_worktree *worktree,
4179 struct got_pathlist_head *paths, int delete_local_mods,
4180 const char *status_codes,
4181 got_worktree_delete_cb progress_cb, void *progress_arg,
4182 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4184 struct got_fileindex *fileindex = NULL;
4185 char *fileindex_path = NULL;
4186 const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 struct got_pathlist_entry *pe;
4188 struct schedule_deletion_args sda;
4190 err = lock_worktree(worktree, LOCK_EX);
4191 if (err)
4192 return err;
4194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4195 if (err)
4196 goto done;
4198 sda.worktree = worktree;
4199 sda.fileindex = fileindex;
4200 sda.progress_cb = progress_cb;
4201 sda.progress_arg = progress_arg;
4202 sda.repo = repo;
4203 sda.delete_local_mods = delete_local_mods;
4204 sda.keep_on_disk = keep_on_disk;
4205 sda.ignore_missing_paths = ignore_missing_paths;
4206 sda.status_codes = status_codes;
4208 TAILQ_FOREACH(pe, paths, entry) {
4209 err = worktree_status(worktree, pe->path, fileindex, repo,
4210 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4211 if (err)
4212 break;
4214 sync_err = sync_fileindex(fileindex, fileindex_path);
4215 if (sync_err && err == NULL)
4216 err = sync_err;
4217 done:
4218 free(fileindex_path);
4219 if (fileindex)
4220 got_fileindex_free(fileindex);
4221 unlockerr = lock_worktree(worktree, LOCK_SH);
4222 if (unlockerr && err == NULL)
4223 err = unlockerr;
4224 return err;
4227 static const struct got_error *
4228 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4230 const struct got_error *err = NULL;
4231 char *line = NULL;
4232 size_t linesize = 0, n;
4233 ssize_t linelen;
4235 linelen = getline(&line, &linesize, infile);
4236 if (linelen == -1) {
4237 if (ferror(infile)) {
4238 err = got_error_from_errno("getline");
4239 goto done;
4241 return NULL;
4243 if (outfile) {
4244 n = fwrite(line, 1, linelen, outfile);
4245 if (n != linelen) {
4246 err = got_ferror(outfile, GOT_ERR_IO);
4247 goto done;
4250 if (rejectfile) {
4251 n = fwrite(line, 1, linelen, rejectfile);
4252 if (n != linelen)
4253 err = got_ferror(outfile, GOT_ERR_IO);
4255 done:
4256 free(line);
4257 return err;
4260 static const struct got_error *
4261 skip_one_line(FILE *f)
4263 char *line = NULL;
4264 size_t linesize = 0;
4265 ssize_t linelen;
4267 linelen = getline(&line, &linesize, f);
4268 if (linelen == -1) {
4269 if (ferror(f))
4270 return got_error_from_errno("getline");
4271 return NULL;
4273 free(line);
4274 return NULL;
4277 static const struct got_error *
4278 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 int start_old, int end_old, int start_new, int end_new,
4280 FILE *outfile, FILE *rejectfile)
4282 const struct got_error *err;
4284 /* Copy old file's lines leading up to patch. */
4285 while (!feof(f1) && *line_cur1 < start_old) {
4286 err = copy_one_line(f1, outfile, NULL);
4287 if (err)
4288 return err;
4289 (*line_cur1)++;
4291 /* Skip new file's lines leading up to patch. */
4292 while (!feof(f2) && *line_cur2 < start_new) {
4293 if (rejectfile)
4294 err = copy_one_line(f2, NULL, rejectfile);
4295 else
4296 err = skip_one_line(f2);
4297 if (err)
4298 return err;
4299 (*line_cur2)++;
4301 /* Copy patched lines. */
4302 while (!feof(f2) && *line_cur2 <= end_new) {
4303 err = copy_one_line(f2, outfile, NULL);
4304 if (err)
4305 return err;
4306 (*line_cur2)++;
4308 /* Skip over old file's replaced lines. */
4309 while (!feof(f1) && *line_cur1 <= end_old) {
4310 if (rejectfile)
4311 err = copy_one_line(f1, NULL, rejectfile);
4312 else
4313 err = skip_one_line(f1);
4314 if (err)
4315 return err;
4316 (*line_cur1)++;
4319 return NULL;
4322 static const struct got_error *
4323 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4324 FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err;
4328 if (outfile) {
4329 /* Copy old file's lines until EOF. */
4330 while (!feof(f1)) {
4331 err = copy_one_line(f1, outfile, NULL);
4332 if (err)
4333 return err;
4334 (*line_cur1)++;
4337 if (rejectfile) {
4338 /* Copy new file's lines until EOF. */
4339 while (!feof(f2)) {
4340 err = copy_one_line(f2, NULL, rejectfile);
4341 if (err)
4342 return err;
4343 (*line_cur2)++;
4347 return NULL;
4350 static const struct got_error *
4351 apply_or_reject_change(int *choice, int *nchunks_used,
4352 struct diff_result *diff_result, int n,
4353 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4354 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4355 got_worktree_patch_cb patch_cb, void *patch_arg)
4357 const struct got_error *err = NULL;
4358 struct diff_chunk_context cc = {};
4359 int start_old, end_old, start_new, end_new;
4360 FILE *hunkfile;
4361 struct diff_output_unidiff_state *diff_state;
4362 struct diff_input_info diff_info;
4363 int rc;
4365 *choice = GOT_PATCH_CHOICE_NONE;
4367 /* Get changed line numbers without context lines for copy_change(). */
4368 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4369 start_old = cc.left.start;
4370 end_old = cc.left.end;
4371 start_new = cc.right.start;
4372 end_new = cc.right.end;
4374 /* Get the same change with context lines for display. */
4375 memset(&cc, 0, sizeof(cc));
4376 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4378 memset(&diff_info, 0, sizeof(diff_info));
4379 diff_info.left_path = relpath;
4380 diff_info.right_path = relpath;
4382 diff_state = diff_output_unidiff_state_alloc();
4383 if (diff_state == NULL)
4384 return got_error_set_errno(ENOMEM,
4385 "diff_output_unidiff_state_alloc");
4387 hunkfile = got_opentemp();
4388 if (hunkfile == NULL) {
4389 err = got_error_from_errno("got_opentemp");
4390 goto done;
4393 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4394 diff_result, &cc);
4395 if (rc != DIFF_RC_OK) {
4396 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4397 goto done;
4400 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4401 err = got_ferror(hunkfile, GOT_ERR_IO);
4402 goto done;
4405 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4406 hunkfile, changeno, nchanges);
4407 if (err)
4408 goto done;
4410 switch (*choice) {
4411 case GOT_PATCH_CHOICE_YES:
4412 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4413 end_old, start_new, end_new, outfile, rejectfile);
4414 break;
4415 case GOT_PATCH_CHOICE_NO:
4416 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 end_old, start_new, end_new, rejectfile, outfile);
4418 break;
4419 case GOT_PATCH_CHOICE_QUIT:
4420 break;
4421 default:
4422 err = got_error(GOT_ERR_PATCH_CHOICE);
4423 break;
4425 done:
4426 diff_output_unidiff_state_free(diff_state);
4427 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4429 return err;
4432 struct revert_file_args {
4433 struct got_worktree *worktree;
4434 struct got_fileindex *fileindex;
4435 got_worktree_checkout_cb progress_cb;
4436 void *progress_arg;
4437 got_worktree_patch_cb patch_cb;
4438 void *patch_arg;
4439 struct got_repository *repo;
4440 int unlink_added_files;
4443 static const struct got_error *
4444 create_patched_content(char **path_outfile, int reverse_patch,
4445 struct got_object_id *blob_id, const char *path2,
4446 int dirfd2, const char *de_name2,
4447 const char *relpath, struct got_repository *repo,
4448 got_worktree_patch_cb patch_cb, void *patch_arg)
4450 const struct got_error *err, *free_err;
4451 struct got_blob_object *blob = NULL;
4452 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4453 int fd = -1, fd2 = -1;
4454 char link_target[PATH_MAX];
4455 ssize_t link_len = 0;
4456 char *path1 = NULL, *id_str = NULL;
4457 struct stat sb2;
4458 struct got_diffreg_result *diffreg_result = NULL;
4459 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4460 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4462 *path_outfile = NULL;
4464 err = got_object_id_str(&id_str, blob_id);
4465 if (err)
4466 return err;
4468 if (dirfd2 != -1) {
4469 fd2 = openat(dirfd2, de_name2,
4470 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4471 if (fd2 == -1) {
4472 if (!got_err_open_nofollow_on_symlink()) {
4473 err = got_error_from_errno2("openat", path2);
4474 goto done;
4476 link_len = readlinkat(dirfd2, de_name2,
4477 link_target, sizeof(link_target));
4478 if (link_len == -1) {
4479 return got_error_from_errno2("readlinkat",
4480 path2);
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4485 } else {
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4487 if (fd2 == -1) {
4488 if (!got_err_open_nofollow_on_symlink()) {
4489 err = got_error_from_errno2("open", path2);
4490 goto done;
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4494 if (link_len == -1)
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 if (fd2 != -1) {
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4503 goto done;
4506 f2 = fdopen(fd2, "r");
4507 if (f2 == NULL) {
4508 err = got_error_from_errno2("fdopen", path2);
4509 goto done;
4511 fd2 = -1;
4512 } else {
4513 size_t n;
4514 f2 = got_opentemp();
4515 if (f2 == NULL) {
4516 err = got_error_from_errno2("got_opentemp", path2);
4517 goto done;
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4522 goto done;
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4526 goto done;
4528 rewind(f2);
4531 fd = got_opentempfd();
4532 if (fd == -1) {
4533 err = got_error_from_errno("got_opentempfd");
4534 goto done;
4537 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4538 if (err)
4539 goto done;
4541 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4542 if (err)
4543 goto done;
4545 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4546 if (err)
4547 goto done;
4549 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4550 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4551 if (err)
4552 goto done;
4554 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4555 if (err)
4556 goto done;
4558 if (fseek(f1, 0L, SEEK_SET) == -1)
4559 return got_ferror(f1, GOT_ERR_IO);
4560 if (fseek(f2, 0L, SEEK_SET) == -1)
4561 return got_ferror(f2, GOT_ERR_IO);
4563 /* Count the number of actual changes in the diff result. */
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 struct diff_chunk_context cc = {};
4566 diff_chunk_context_load_change(&cc, &nchunks_used,
4567 diffreg_result->result, n, 0);
4568 nchanges++;
4570 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4571 int choice;
4572 err = apply_or_reject_change(&choice, &nchunks_used,
4573 diffreg_result->result, n, relpath, f1, f2,
4574 &line_cur1, &line_cur2,
4575 reverse_patch ? NULL : outfile,
4576 reverse_patch ? outfile : NULL,
4577 ++i, nchanges, patch_cb, patch_arg);
4578 if (err)
4579 goto done;
4580 if (choice == GOT_PATCH_CHOICE_YES)
4581 have_content = 1;
4582 else if (choice == GOT_PATCH_CHOICE_QUIT)
4583 break;
4585 if (have_content) {
4586 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL);
4589 if (err)
4590 goto done;
4592 if (!S_ISLNK(sb2.st_mode)) {
4593 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4594 err = got_error_from_errno2("fchmod", path2);
4595 goto done;
4599 done:
4600 free(id_str);
4601 if (fd != -1 && close(fd) == -1 && err == NULL)
4602 err = got_error_from_errno("close");
4603 if (blob)
4604 got_object_blob_close(blob);
4605 free_err = got_diffreg_result_free(diffreg_result);
4606 if (err == NULL)
4607 err = free_err;
4608 if (f1 && fclose(f1) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path1);
4610 if (f2 && fclose(f2) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path2);
4612 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 err = got_error_from_errno2("close", path2);
4614 if (outfile && fclose(outfile) == EOF && err == NULL)
4615 err = got_error_from_errno2("fclose", *path_outfile);
4616 if (path1 && unlink(path1) == -1 && err == NULL)
4617 err = got_error_from_errno2("unlink", path1);
4618 if (err || !have_content) {
4619 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 err = got_error_from_errno2("unlink", *path_outfile);
4621 free(*path_outfile);
4622 *path_outfile = NULL;
4624 free(path1);
4625 return err;
4628 static const struct got_error *
4629 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *relpath, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct revert_file_args *a = arg;
4635 const struct got_error *err = NULL;
4636 char *parent_path = NULL;
4637 struct got_fileindex_entry *ie;
4638 struct got_commit_object *base_commit = NULL;
4639 struct got_tree_object *tree = NULL;
4640 struct got_object_id *tree_id = NULL;
4641 const struct got_tree_entry *te = NULL;
4642 char *tree_path = NULL, *te_name;
4643 char *ondisk_path = NULL, *path_content = NULL;
4644 struct got_blob_object *blob = NULL;
4645 int fd = -1;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4650 return NULL;
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4657 if (ie == NULL)
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4662 if (err) {
4663 if (err->code != GOT_ERR_BAD_PATH)
4664 goto done;
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4668 goto done;
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4677 } else {
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4682 goto done;
4684 } else {
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4688 goto done;
4693 err = got_object_open_as_commit(&base_commit, a->repo,
4694 a->worktree->base_commit_id);
4695 if (err)
4696 goto done;
4698 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4699 if (err) {
4700 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4701 (status == GOT_STATUS_ADD ||
4702 staged_status == GOT_STATUS_ADD)))
4703 goto done;
4704 } else {
4705 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4706 if (err)
4707 goto done;
4709 err = got_path_basename(&te_name, ie->path);
4710 if (err)
4711 goto done;
4713 te = got_object_tree_find_entry(tree, te_name);
4714 free(te_name);
4715 if (te == NULL && status != GOT_STATUS_ADD &&
4716 staged_status != GOT_STATUS_ADD) {
4717 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4718 goto done;
4722 switch (status) {
4723 case GOT_STATUS_ADD:
4724 if (a->patch_cb) {
4725 int choice = GOT_PATCH_CHOICE_NONE;
4726 err = (*a->patch_cb)(&choice, a->patch_arg,
4727 status, ie->path, NULL, 1, 1);
4728 if (err)
4729 goto done;
4730 if (choice != GOT_PATCH_CHOICE_YES)
4731 break;
4733 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4734 ie->path);
4735 if (err)
4736 goto done;
4737 got_fileindex_entry_remove(a->fileindex, ie);
4738 if (a->unlink_added_files) {
4739 if (asprintf(&ondisk_path, "%s/%s",
4740 got_worktree_get_root_path(a->worktree),
4741 relpath) == -1) {
4742 err = got_error_from_errno("asprintf");
4743 goto done;
4745 if (unlink(ondisk_path) == -1) {
4746 err = got_error_from_errno2("unlink",
4747 ondisk_path);
4748 break;
4751 break;
4752 case GOT_STATUS_DELETE:
4753 if (a->patch_cb) {
4754 int choice = GOT_PATCH_CHOICE_NONE;
4755 err = (*a->patch_cb)(&choice, a->patch_arg,
4756 status, ie->path, NULL, 1, 1);
4757 if (err)
4758 goto done;
4759 if (choice != GOT_PATCH_CHOICE_YES)
4760 break;
4762 /* fall through */
4763 case GOT_STATUS_MODIFY:
4764 case GOT_STATUS_MODE_CHANGE:
4765 case GOT_STATUS_CONFLICT:
4766 case GOT_STATUS_MISSING: {
4767 struct got_object_id id;
4768 if (staged_status == GOT_STATUS_ADD ||
4769 staged_status == GOT_STATUS_MODIFY) {
4770 memcpy(id.sha1, ie->staged_blob_sha1,
4771 SHA1_DIGEST_LENGTH);
4772 } else
4773 memcpy(id.sha1, ie->blob_sha1,
4774 SHA1_DIGEST_LENGTH);
4775 fd = got_opentempfd();
4776 if (fd == -1) {
4777 err = got_error_from_errno("got_opentempfd");
4778 goto done;
4781 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4782 if (err)
4783 goto done;
4785 if (asprintf(&ondisk_path, "%s/%s",
4786 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4787 err = got_error_from_errno("asprintf");
4788 goto done;
4791 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4792 status == GOT_STATUS_CONFLICT)) {
4793 int is_bad_symlink = 0;
4794 err = create_patched_content(&path_content, 1, &id,
4795 ondisk_path, dirfd, de_name, ie->path, a->repo,
4796 a->patch_cb, a->patch_arg);
4797 if (err || path_content == NULL)
4798 break;
4799 if (te && S_ISLNK(te->mode)) {
4800 if (unlink(path_content) == -1) {
4801 err = got_error_from_errno2("unlink",
4802 path_content);
4803 break;
4805 err = install_symlink(&is_bad_symlink,
4806 a->worktree, ondisk_path, ie->path,
4807 blob, 0, 1, 0, 0, a->repo,
4808 a->progress_cb, a->progress_arg);
4809 } else {
4810 if (rename(path_content, ondisk_path) == -1) {
4811 err = got_error_from_errno3("rename",
4812 path_content, ondisk_path);
4813 goto done;
4816 } else {
4817 int is_bad_symlink = 0;
4818 if (te && S_ISLNK(te->mode)) {
4819 err = install_symlink(&is_bad_symlink,
4820 a->worktree, ondisk_path, ie->path,
4821 blob, 0, 1, 0, 0, a->repo,
4822 a->progress_cb, a->progress_arg);
4823 } else {
4824 err = install_blob(a->worktree, ondisk_path,
4825 ie->path,
4826 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4827 got_fileindex_perms_to_st(ie), blob,
4828 0, 1, 0, 0, a->repo,
4829 a->progress_cb, a->progress_arg);
4831 if (err)
4832 goto done;
4833 if (status == GOT_STATUS_DELETE ||
4834 status == GOT_STATUS_MODE_CHANGE) {
4835 err = got_fileindex_entry_update(ie,
4836 a->worktree->root_fd, relpath,
4837 blob->id.sha1,
4838 a->worktree->base_commit_id->sha1, 1);
4839 if (err)
4840 goto done;
4842 if (is_bad_symlink) {
4843 got_fileindex_entry_filetype_set(ie,
4844 GOT_FILEIDX_MODE_BAD_SYMLINK);
4847 break;
4849 default:
4850 break;
4852 done:
4853 free(ondisk_path);
4854 free(path_content);
4855 free(parent_path);
4856 free(tree_path);
4857 if (fd != -1 && close(fd) == -1 && err == NULL)
4858 err = got_error_from_errno("close");
4859 if (blob)
4860 got_object_blob_close(blob);
4861 if (tree)
4862 got_object_tree_close(tree);
4863 free(tree_id);
4864 if (base_commit)
4865 got_object_commit_close(base_commit);
4866 return err;
4869 const struct got_error *
4870 got_worktree_revert(struct got_worktree *worktree,
4871 struct got_pathlist_head *paths,
4872 got_worktree_checkout_cb progress_cb, void *progress_arg,
4873 got_worktree_patch_cb patch_cb, void *patch_arg,
4874 struct got_repository *repo)
4876 struct got_fileindex *fileindex = NULL;
4877 char *fileindex_path = NULL;
4878 const struct got_error *err = NULL, *unlockerr = NULL;
4879 const struct got_error *sync_err = NULL;
4880 struct got_pathlist_entry *pe;
4881 struct revert_file_args rfa;
4883 err = lock_worktree(worktree, LOCK_EX);
4884 if (err)
4885 return err;
4887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4888 if (err)
4889 goto done;
4891 rfa.worktree = worktree;
4892 rfa.fileindex = fileindex;
4893 rfa.progress_cb = progress_cb;
4894 rfa.progress_arg = progress_arg;
4895 rfa.patch_cb = patch_cb;
4896 rfa.patch_arg = patch_arg;
4897 rfa.repo = repo;
4898 rfa.unlink_added_files = 0;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 err = worktree_status(worktree, pe->path, fileindex, repo,
4901 revert_file, &rfa, NULL, NULL, 1, 0);
4902 if (err)
4903 break;
4905 sync_err = sync_fileindex(fileindex, fileindex_path);
4906 if (sync_err && err == NULL)
4907 err = sync_err;
4908 done:
4909 free(fileindex_path);
4910 if (fileindex)
4911 got_fileindex_free(fileindex);
4912 unlockerr = lock_worktree(worktree, LOCK_SH);
4913 if (unlockerr && err == NULL)
4914 err = unlockerr;
4915 return err;
4918 static void
4919 free_commitable(struct got_commitable *ct)
4921 free(ct->path);
4922 free(ct->in_repo_path);
4923 free(ct->ondisk_path);
4924 free(ct->blob_id);
4925 free(ct->base_blob_id);
4926 free(ct->staged_blob_id);
4927 free(ct->base_commit_id);
4928 free(ct);
4931 struct collect_commitables_arg {
4932 struct got_pathlist_head *commitable_paths;
4933 struct got_repository *repo;
4934 struct got_worktree *worktree;
4935 struct got_fileindex *fileindex;
4936 int have_staged_files;
4937 int allow_bad_symlinks;
4940 static const struct got_error *
4941 collect_commitables(void *arg, unsigned char status,
4942 unsigned char staged_status, const char *relpath,
4943 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4944 struct got_object_id *commit_id, int dirfd, const char *de_name)
4946 struct collect_commitables_arg *a = arg;
4947 const struct got_error *err = NULL;
4948 struct got_commitable *ct = NULL;
4949 struct got_pathlist_entry *new = NULL;
4950 char *parent_path = NULL, *path = NULL;
4951 struct stat sb;
4953 if (a->have_staged_files) {
4954 if (staged_status != GOT_STATUS_MODIFY &&
4955 staged_status != GOT_STATUS_ADD &&
4956 staged_status != GOT_STATUS_DELETE)
4957 return NULL;
4958 } else {
4959 if (status == GOT_STATUS_CONFLICT)
4960 return got_error(GOT_ERR_COMMIT_CONFLICT);
4962 if (status != GOT_STATUS_MODIFY &&
4963 status != GOT_STATUS_MODE_CHANGE &&
4964 status != GOT_STATUS_ADD &&
4965 status != GOT_STATUS_DELETE)
4966 return NULL;
4969 if (asprintf(&path, "/%s", relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4971 goto done;
4973 if (strcmp(path, "/") == 0) {
4974 parent_path = strdup("");
4975 if (parent_path == NULL)
4976 return got_error_from_errno("strdup");
4977 } else {
4978 err = got_path_dirname(&parent_path, path);
4979 if (err)
4980 return err;
4983 ct = calloc(1, sizeof(*ct));
4984 if (ct == NULL) {
4985 err = got_error_from_errno("calloc");
4986 goto done;
4989 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4990 relpath) == -1) {
4991 err = got_error_from_errno("asprintf");
4992 goto done;
4995 if (staged_status == GOT_STATUS_ADD ||
4996 staged_status == GOT_STATUS_MODIFY) {
4997 struct got_fileindex_entry *ie;
4998 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4999 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5000 case GOT_FILEIDX_MODE_REGULAR_FILE:
5001 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5002 ct->mode = S_IFREG;
5003 break;
5004 case GOT_FILEIDX_MODE_SYMLINK:
5005 ct->mode = S_IFLNK;
5006 break;
5007 default:
5008 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5009 goto done;
5011 ct->mode |= got_fileindex_entry_perms_get(ie);
5012 } else if (status != GOT_STATUS_DELETE &&
5013 staged_status != GOT_STATUS_DELETE) {
5014 if (dirfd != -1) {
5015 if (fstatat(dirfd, de_name, &sb,
5016 AT_SYMLINK_NOFOLLOW) == -1) {
5017 err = got_error_from_errno2("fstatat",
5018 ct->ondisk_path);
5019 goto done;
5021 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5022 err = got_error_from_errno2("lstat", ct->ondisk_path);
5023 goto done;
5025 ct->mode = sb.st_mode;
5028 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5029 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5030 relpath) == -1) {
5031 err = got_error_from_errno("asprintf");
5032 goto done;
5035 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5036 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5037 int is_bad_symlink;
5038 char target_path[PATH_MAX];
5039 ssize_t target_len;
5040 target_len = readlink(ct->ondisk_path, target_path,
5041 sizeof(target_path));
5042 if (target_len == -1) {
5043 err = got_error_from_errno2("readlink",
5044 ct->ondisk_path);
5045 goto done;
5047 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5048 target_len, ct->ondisk_path, a->worktree->root_path);
5049 if (err)
5050 goto done;
5051 if (is_bad_symlink) {
5052 err = got_error_path(ct->ondisk_path,
5053 GOT_ERR_BAD_SYMLINK);
5054 goto done;
5059 ct->status = status;
5060 ct->staged_status = staged_status;
5061 ct->blob_id = NULL; /* will be filled in when blob gets created */
5062 if (ct->status != GOT_STATUS_ADD &&
5063 ct->staged_status != GOT_STATUS_ADD) {
5064 ct->base_blob_id = got_object_id_dup(blob_id);
5065 if (ct->base_blob_id == NULL) {
5066 err = got_error_from_errno("got_object_id_dup");
5067 goto done;
5069 ct->base_commit_id = got_object_id_dup(commit_id);
5070 if (ct->base_commit_id == NULL) {
5071 err = got_error_from_errno("got_object_id_dup");
5072 goto done;
5075 if (ct->staged_status == GOT_STATUS_ADD ||
5076 ct->staged_status == GOT_STATUS_MODIFY) {
5077 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5078 if (ct->staged_blob_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5080 goto done;
5083 ct->path = strdup(path);
5084 if (ct->path == NULL) {
5085 err = got_error_from_errno("strdup");
5086 goto done;
5088 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5089 done:
5090 if (ct && (err || new == NULL))
5091 free_commitable(ct);
5092 free(parent_path);
5093 free(path);
5094 return err;
5097 static const struct got_error *write_tree(struct got_object_id **, int *,
5098 struct got_tree_object *, const char *, struct got_pathlist_head *,
5099 got_worktree_status_cb status_cb, void *status_arg,
5100 struct got_repository *);
5102 static const struct got_error *
5103 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5104 struct got_tree_entry *te, const char *parent_path,
5105 struct got_pathlist_head *commitable_paths,
5106 got_worktree_status_cb status_cb, void *status_arg,
5107 struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 struct got_tree_object *subtree;
5111 char *subpath;
5113 if (asprintf(&subpath, "%s%s%s", parent_path,
5114 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5115 return got_error_from_errno("asprintf");
5117 err = got_object_open_as_tree(&subtree, repo, &te->id);
5118 if (err)
5119 return err;
5121 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5122 commitable_paths, status_cb, status_arg, repo);
5123 got_object_tree_close(subtree);
5124 free(subpath);
5125 return err;
5128 static const struct got_error *
5129 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5131 const struct got_error *err = NULL;
5132 char *ct_parent_path = NULL;
5134 *match = 0;
5136 if (strchr(ct->in_repo_path, '/') == NULL) {
5137 *match = got_path_is_root_dir(path);
5138 return NULL;
5141 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5142 if (err)
5143 return err;
5144 *match = (strcmp(path, ct_parent_path) == 0);
5145 free(ct_parent_path);
5146 return err;
5149 static mode_t
5150 get_ct_file_mode(struct got_commitable *ct)
5152 if (S_ISLNK(ct->mode))
5153 return S_IFLNK;
5155 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5158 static const struct got_error *
5159 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5160 struct got_tree_entry *te, struct got_commitable *ct)
5162 const struct got_error *err = NULL;
5164 *new_te = NULL;
5166 err = got_object_tree_entry_dup(new_te, te);
5167 if (err)
5168 goto done;
5170 (*new_te)->mode = get_ct_file_mode(ct);
5172 if (ct->staged_status == GOT_STATUS_MODIFY)
5173 memcpy(&(*new_te)->id, ct->staged_blob_id,
5174 sizeof((*new_te)->id));
5175 else
5176 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5177 done:
5178 if (err && *new_te) {
5179 free(*new_te);
5180 *new_te = NULL;
5182 return err;
5185 static const struct got_error *
5186 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5187 struct got_commitable *ct)
5189 const struct got_error *err = NULL;
5190 char *ct_name = NULL;
5192 *new_te = NULL;
5194 *new_te = calloc(1, sizeof(**new_te));
5195 if (*new_te == NULL)
5196 return got_error_from_errno("calloc");
5198 err = got_path_basename(&ct_name, ct->path);
5199 if (err)
5200 goto done;
5201 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5202 sizeof((*new_te)->name)) {
5203 err = got_error(GOT_ERR_NO_SPACE);
5204 goto done;
5207 (*new_te)->mode = get_ct_file_mode(ct);
5209 if (ct->staged_status == GOT_STATUS_ADD)
5210 memcpy(&(*new_te)->id, ct->staged_blob_id,
5211 sizeof((*new_te)->id));
5212 else
5213 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5214 done:
5215 free(ct_name);
5216 if (err && *new_te) {
5217 free(*new_te);
5218 *new_te = NULL;
5220 return err;
5223 static const struct got_error *
5224 insert_tree_entry(struct got_tree_entry *new_te,
5225 struct got_pathlist_head *paths)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *new_pe;
5230 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5231 if (err)
5232 return err;
5233 if (new_pe == NULL)
5234 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5235 return NULL;
5238 static const struct got_error *
5239 report_ct_status(struct got_commitable *ct,
5240 got_worktree_status_cb status_cb, void *status_arg)
5242 const char *ct_path = ct->path;
5243 unsigned char status;
5245 if (status_cb == NULL) /* no commit progress output desired */
5246 return NULL;
5248 while (ct_path[0] == '/')
5249 ct_path++;
5251 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5252 status = ct->staged_status;
5253 else
5254 status = ct->status;
5256 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5257 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5260 static const struct got_error *
5261 match_modified_subtree(int *modified, struct got_tree_entry *te,
5262 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5264 const struct got_error *err = NULL;
5265 struct got_pathlist_entry *pe;
5266 char *te_path;
5268 *modified = 0;
5270 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5271 got_path_is_root_dir(base_tree_path) ? "" : "/",
5272 te->name) == -1)
5273 return got_error_from_errno("asprintf");
5275 TAILQ_FOREACH(pe, commitable_paths, entry) {
5276 struct got_commitable *ct = pe->data;
5277 *modified = got_path_is_child(ct->in_repo_path, te_path,
5278 strlen(te_path));
5279 if (*modified)
5280 break;
5283 free(te_path);
5284 return err;
5287 static const struct got_error *
5288 match_deleted_or_modified_ct(struct got_commitable **ctp,
5289 struct got_tree_entry *te, const char *base_tree_path,
5290 struct got_pathlist_head *commitable_paths)
5292 const struct got_error *err = NULL;
5293 struct got_pathlist_entry *pe;
5295 *ctp = NULL;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_commitable *ct = pe->data;
5299 char *ct_name = NULL;
5300 int path_matches;
5302 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5303 if (ct->status != GOT_STATUS_MODIFY &&
5304 ct->status != GOT_STATUS_MODE_CHANGE &&
5305 ct->status != GOT_STATUS_DELETE)
5306 continue;
5307 } else {
5308 if (ct->staged_status != GOT_STATUS_MODIFY &&
5309 ct->staged_status != GOT_STATUS_DELETE)
5310 continue;
5313 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5314 continue;
5316 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5317 if (err)
5318 return err;
5319 if (!path_matches)
5320 continue;
5322 err = got_path_basename(&ct_name, pe->path);
5323 if (err)
5324 return err;
5326 if (strcmp(te->name, ct_name) != 0) {
5327 free(ct_name);
5328 continue;
5330 free(ct_name);
5332 *ctp = ct;
5333 break;
5336 return err;
5339 static const struct got_error *
5340 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5341 const char *child_path, const char *path_base_tree,
5342 struct got_pathlist_head *commitable_paths,
5343 got_worktree_status_cb status_cb, void *status_arg,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 struct got_tree_entry *new_te;
5348 char *subtree_path;
5349 struct got_object_id *id = NULL;
5350 int nentries;
5352 *new_tep = NULL;
5354 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5355 got_path_is_root_dir(path_base_tree) ? "" : "/",
5356 child_path) == -1)
5357 return got_error_from_errno("asprintf");
5359 new_te = calloc(1, sizeof(*new_te));
5360 if (new_te == NULL)
5361 return got_error_from_errno("calloc");
5362 new_te->mode = S_IFDIR;
5364 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5365 sizeof(new_te->name)) {
5366 err = got_error(GOT_ERR_NO_SPACE);
5367 goto done;
5369 err = write_tree(&id, &nentries, NULL, subtree_path,
5370 commitable_paths, status_cb, status_arg, repo);
5371 if (err) {
5372 free(new_te);
5373 goto done;
5375 memcpy(&new_te->id, id, sizeof(new_te->id));
5376 done:
5377 free(id);
5378 free(subtree_path);
5379 if (err == NULL)
5380 *new_tep = new_te;
5381 return err;
5384 static const struct got_error *
5385 write_tree(struct got_object_id **new_tree_id, int *nentries,
5386 struct got_tree_object *base_tree, const char *path_base_tree,
5387 struct got_pathlist_head *commitable_paths,
5388 got_worktree_status_cb status_cb, void *status_arg,
5389 struct got_repository *repo)
5391 const struct got_error *err = NULL;
5392 struct got_pathlist_head paths;
5393 struct got_tree_entry *te, *new_te = NULL;
5394 struct got_pathlist_entry *pe;
5396 TAILQ_INIT(&paths);
5397 *nentries = 0;
5399 /* Insert, and recurse into, newly added entries first. */
5400 TAILQ_FOREACH(pe, commitable_paths, entry) {
5401 struct got_commitable *ct = pe->data;
5402 char *child_path = NULL, *slash;
5404 if ((ct->status != GOT_STATUS_ADD &&
5405 ct->staged_status != GOT_STATUS_ADD) ||
5406 (ct->flags & GOT_COMMITABLE_ADDED))
5407 continue;
5409 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5410 strlen(path_base_tree)))
5411 continue;
5413 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5414 ct->in_repo_path);
5415 if (err)
5416 goto done;
5418 slash = strchr(child_path, '/');
5419 if (slash == NULL) {
5420 err = alloc_added_blob_tree_entry(&new_te, ct);
5421 if (err)
5422 goto done;
5423 err = report_ct_status(ct, status_cb, status_arg);
5424 if (err)
5425 goto done;
5426 ct->flags |= GOT_COMMITABLE_ADDED;
5427 err = insert_tree_entry(new_te, &paths);
5428 if (err)
5429 goto done;
5430 (*nentries)++;
5431 } else {
5432 *slash = '\0'; /* trim trailing path components */
5433 if (base_tree == NULL ||
5434 got_object_tree_find_entry(base_tree, child_path)
5435 == NULL) {
5436 err = make_subtree_for_added_blob(&new_te,
5437 child_path, path_base_tree,
5438 commitable_paths, status_cb, status_arg,
5439 repo);
5440 if (err)
5441 goto done;
5442 err = insert_tree_entry(new_te, &paths);
5443 if (err)
5444 goto done;
5445 (*nentries)++;
5450 if (base_tree) {
5451 int i, nbase_entries;
5452 /* Handle modified and deleted entries. */
5453 nbase_entries = got_object_tree_get_nentries(base_tree);
5454 for (i = 0; i < nbase_entries; i++) {
5455 struct got_commitable *ct = NULL;
5457 te = got_object_tree_get_entry(base_tree, i);
5458 if (got_object_tree_entry_is_submodule(te)) {
5459 /* Entry is a submodule; just copy it. */
5460 err = got_object_tree_entry_dup(&new_te, te);
5461 if (err)
5462 goto done;
5463 err = insert_tree_entry(new_te, &paths);
5464 if (err)
5465 goto done;
5466 (*nentries)++;
5467 continue;
5470 if (S_ISDIR(te->mode)) {
5471 int modified;
5472 err = got_object_tree_entry_dup(&new_te, te);
5473 if (err)
5474 goto done;
5475 err = match_modified_subtree(&modified, te,
5476 path_base_tree, commitable_paths);
5477 if (err)
5478 goto done;
5479 /* Avoid recursion into unmodified subtrees. */
5480 if (modified) {
5481 struct got_object_id *new_id;
5482 int nsubentries;
5483 err = write_subtree(&new_id,
5484 &nsubentries, te,
5485 path_base_tree, commitable_paths,
5486 status_cb, status_arg, repo);
5487 if (err)
5488 goto done;
5489 if (nsubentries == 0) {
5490 /* All entries were deleted. */
5491 free(new_id);
5492 continue;
5494 memcpy(&new_te->id, new_id,
5495 sizeof(new_te->id));
5496 free(new_id);
5498 err = insert_tree_entry(new_te, &paths);
5499 if (err)
5500 goto done;
5501 (*nentries)++;
5502 continue;
5505 err = match_deleted_or_modified_ct(&ct, te,
5506 path_base_tree, commitable_paths);
5507 if (err)
5508 goto done;
5509 if (ct) {
5510 /* NB: Deleted entries get dropped here. */
5511 if (ct->status == GOT_STATUS_MODIFY ||
5512 ct->status == GOT_STATUS_MODE_CHANGE ||
5513 ct->staged_status == GOT_STATUS_MODIFY) {
5514 err = alloc_modified_blob_tree_entry(
5515 &new_te, te, ct);
5516 if (err)
5517 goto done;
5518 err = insert_tree_entry(new_te, &paths);
5519 if (err)
5520 goto done;
5521 (*nentries)++;
5523 err = report_ct_status(ct, status_cb,
5524 status_arg);
5525 if (err)
5526 goto done;
5527 } else {
5528 /* Entry is unchanged; just copy it. */
5529 err = got_object_tree_entry_dup(&new_te, te);
5530 if (err)
5531 goto done;
5532 err = insert_tree_entry(new_te, &paths);
5533 if (err)
5534 goto done;
5535 (*nentries)++;
5540 /* Write new list of entries; deleted entries have been dropped. */
5541 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5542 done:
5543 got_pathlist_free(&paths);
5544 return err;
5547 static const struct got_error *
5548 update_fileindex_after_commit(struct got_worktree *worktree,
5549 struct got_pathlist_head *commitable_paths,
5550 struct got_object_id *new_base_commit_id,
5551 struct got_fileindex *fileindex, int have_staged_files)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *pe;
5555 char *relpath = NULL;
5557 TAILQ_FOREACH(pe, commitable_paths, entry) {
5558 struct got_fileindex_entry *ie;
5559 struct got_commitable *ct = pe->data;
5561 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5563 err = got_path_skip_common_ancestor(&relpath,
5564 worktree->root_path, ct->ondisk_path);
5565 if (err)
5566 goto done;
5568 if (ie) {
5569 if (ct->status == GOT_STATUS_DELETE ||
5570 ct->staged_status == GOT_STATUS_DELETE) {
5571 got_fileindex_entry_remove(fileindex, ie);
5572 } else if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY) {
5574 got_fileindex_entry_stage_set(ie,
5575 GOT_FILEIDX_STAGE_NONE);
5576 got_fileindex_entry_staged_filetype_set(ie, 0);
5578 err = got_fileindex_entry_update(ie,
5579 worktree->root_fd, relpath,
5580 ct->staged_blob_id->sha1,
5581 new_base_commit_id->sha1,
5582 !have_staged_files);
5583 } else
5584 err = got_fileindex_entry_update(ie,
5585 worktree->root_fd, relpath,
5586 ct->blob_id->sha1,
5587 new_base_commit_id->sha1,
5588 !have_staged_files);
5589 } else {
5590 err = got_fileindex_entry_alloc(&ie, pe->path);
5591 if (err)
5592 goto done;
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath, ct->blob_id->sha1,
5595 new_base_commit_id->sha1, 1);
5596 if (err) {
5597 got_fileindex_entry_free(ie);
5598 goto done;
5600 err = got_fileindex_entry_add(fileindex, ie);
5601 if (err) {
5602 got_fileindex_entry_free(ie);
5603 goto done;
5606 free(relpath);
5607 relpath = NULL;
5609 done:
5610 free(relpath);
5611 return err;
5615 static const struct got_error *
5616 check_out_of_date(const char *in_repo_path, unsigned char status,
5617 unsigned char staged_status, struct got_object_id *base_blob_id,
5618 struct got_object_id *base_commit_id,
5619 struct got_object_id *head_commit_id, struct got_repository *repo,
5620 int ood_errcode)
5622 const struct got_error *err = NULL;
5623 struct got_commit_object *commit = NULL;
5624 struct got_object_id *id = NULL;
5626 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5627 /* Trivial case: base commit == head commit */
5628 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5629 return NULL;
5631 * Ensure file content which local changes were based
5632 * on matches file content in the branch head.
5634 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5635 if (err)
5636 goto done;
5637 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5638 if (err) {
5639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5640 err = got_error(ood_errcode);
5641 goto done;
5642 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5643 err = got_error(ood_errcode);
5644 } else {
5645 /* Require that added files don't exist in the branch head. */
5646 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5647 if (err)
5648 goto done;
5649 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5650 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5651 goto done;
5652 err = id ? got_error(ood_errcode) : NULL;
5654 done:
5655 free(id);
5656 if (commit)
5657 got_object_commit_close(commit);
5658 return err;
5661 static const struct got_error *
5662 commit_worktree(struct got_object_id **new_commit_id,
5663 struct got_pathlist_head *commitable_paths,
5664 struct got_object_id *head_commit_id,
5665 struct got_object_id *parent_id2,
5666 struct got_worktree *worktree,
5667 const char *author, const char *committer,
5668 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL, *unlockerr = NULL;
5673 struct got_pathlist_entry *pe;
5674 const char *head_ref_name = NULL;
5675 struct got_commit_object *head_commit = NULL;
5676 struct got_reference *head_ref2 = NULL;
5677 struct got_object_id *head_commit_id2 = NULL;
5678 struct got_tree_object *head_tree = NULL;
5679 struct got_object_id *new_tree_id = NULL;
5680 int nentries, nparents = 0;
5681 struct got_object_id_queue parent_ids;
5682 struct got_object_qid *pid = NULL;
5683 char *logmsg = NULL;
5684 time_t timestamp;
5686 *new_commit_id = NULL;
5688 STAILQ_INIT(&parent_ids);
5690 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5691 if (err)
5692 goto done;
5694 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5695 if (err)
5696 goto done;
5698 if (commit_msg_cb != NULL) {
5699 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5700 if (err)
5701 goto done;
5704 if (logmsg == NULL || strlen(logmsg) == 0) {
5705 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5706 goto done;
5709 /* Create blobs from added and modified files and record their IDs. */
5710 TAILQ_FOREACH(pe, commitable_paths, entry) {
5711 struct got_commitable *ct = pe->data;
5712 char *ondisk_path;
5714 /* Blobs for staged files already exist. */
5715 if (ct->staged_status == GOT_STATUS_ADD ||
5716 ct->staged_status == GOT_STATUS_MODIFY)
5717 continue;
5719 if (ct->status != GOT_STATUS_ADD &&
5720 ct->status != GOT_STATUS_MODIFY &&
5721 ct->status != GOT_STATUS_MODE_CHANGE)
5722 continue;
5724 if (asprintf(&ondisk_path, "%s/%s",
5725 worktree->root_path, pe->path) == -1) {
5726 err = got_error_from_errno("asprintf");
5727 goto done;
5729 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5730 free(ondisk_path);
5731 if (err)
5732 goto done;
5735 /* Recursively write new tree objects. */
5736 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5737 commitable_paths, status_cb, status_arg, repo);
5738 if (err)
5739 goto done;
5741 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5742 if (err)
5743 goto done;
5744 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5745 nparents++;
5746 if (parent_id2) {
5747 err = got_object_qid_alloc(&pid, parent_id2);
5748 if (err)
5749 goto done;
5750 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5751 nparents++;
5753 timestamp = time(NULL);
5754 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5755 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5756 if (logmsg != NULL)
5757 free(logmsg);
5758 if (err)
5759 goto done;
5761 /* Check if a concurrent commit to our branch has occurred. */
5762 head_ref_name = got_worktree_get_head_ref_name(worktree);
5763 if (head_ref_name == NULL) {
5764 err = got_error_from_errno("got_worktree_get_head_ref_name");
5765 goto done;
5767 /* Lock the reference here to prevent concurrent modification. */
5768 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5769 if (err)
5770 goto done;
5771 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5772 if (err)
5773 goto done;
5774 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5775 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5776 goto done;
5778 /* Update branch head in repository. */
5779 err = got_ref_change_ref(head_ref2, *new_commit_id);
5780 if (err)
5781 goto done;
5782 err = got_ref_write(head_ref2, repo);
5783 if (err)
5784 goto done;
5786 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5787 if (err)
5788 goto done;
5790 err = ref_base_commit(worktree, repo);
5791 if (err)
5792 goto done;
5793 done:
5794 got_object_id_queue_free(&parent_ids);
5795 if (head_tree)
5796 got_object_tree_close(head_tree);
5797 if (head_commit)
5798 got_object_commit_close(head_commit);
5799 free(head_commit_id2);
5800 if (head_ref2) {
5801 unlockerr = got_ref_unlock(head_ref2);
5802 if (unlockerr && err == NULL)
5803 err = unlockerr;
5804 got_ref_close(head_ref2);
5806 return err;
5809 static const struct got_error *
5810 check_path_is_commitable(const char *path,
5811 struct got_pathlist_head *commitable_paths)
5813 struct got_pathlist_entry *cpe = NULL;
5814 size_t path_len = strlen(path);
5816 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5817 struct got_commitable *ct = cpe->data;
5818 const char *ct_path = ct->path;
5820 while (ct_path[0] == '/')
5821 ct_path++;
5823 if (strcmp(path, ct_path) == 0 ||
5824 got_path_is_child(ct_path, path, path_len))
5825 break;
5828 if (cpe == NULL)
5829 return got_error_path(path, GOT_ERR_BAD_PATH);
5831 return NULL;
5834 static const struct got_error *
5835 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5837 int *have_staged_files = arg;
5839 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5840 *have_staged_files = 1;
5841 return got_error(GOT_ERR_CANCELLED);
5844 return NULL;
5847 static const struct got_error *
5848 check_non_staged_files(struct got_fileindex *fileindex,
5849 struct got_pathlist_head *paths)
5851 struct got_pathlist_entry *pe;
5852 struct got_fileindex_entry *ie;
5854 TAILQ_FOREACH(pe, paths, entry) {
5855 if (pe->path[0] == '\0')
5856 continue;
5857 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5858 if (ie == NULL)
5859 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5860 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5861 return got_error_path(pe->path,
5862 GOT_ERR_FILE_NOT_STAGED);
5865 return NULL;
5868 const struct got_error *
5869 got_worktree_commit(struct got_object_id **new_commit_id,
5870 struct got_worktree *worktree, struct got_pathlist_head *paths,
5871 const char *author, const char *committer, int allow_bad_symlinks,
5872 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5873 got_worktree_status_cb status_cb, void *status_arg,
5874 struct got_repository *repo)
5876 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5877 struct got_fileindex *fileindex = NULL;
5878 char *fileindex_path = NULL;
5879 struct got_pathlist_head commitable_paths;
5880 struct collect_commitables_arg cc_arg;
5881 struct got_pathlist_entry *pe;
5882 struct got_reference *head_ref = NULL;
5883 struct got_object_id *head_commit_id = NULL;
5884 int have_staged_files = 0;
5886 *new_commit_id = NULL;
5888 TAILQ_INIT(&commitable_paths);
5890 err = lock_worktree(worktree, LOCK_EX);
5891 if (err)
5892 goto done;
5894 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5895 if (err)
5896 goto done;
5898 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5899 if (err)
5900 goto done;
5902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5903 if (err)
5904 goto done;
5906 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5907 &have_staged_files);
5908 if (err && err->code != GOT_ERR_CANCELLED)
5909 goto done;
5910 if (have_staged_files) {
5911 err = check_non_staged_files(fileindex, paths);
5912 if (err)
5913 goto done;
5916 cc_arg.commitable_paths = &commitable_paths;
5917 cc_arg.worktree = worktree;
5918 cc_arg.fileindex = fileindex;
5919 cc_arg.repo = repo;
5920 cc_arg.have_staged_files = have_staged_files;
5921 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5922 TAILQ_FOREACH(pe, paths, entry) {
5923 err = worktree_status(worktree, pe->path, fileindex, repo,
5924 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5925 if (err)
5926 goto done;
5929 if (TAILQ_EMPTY(&commitable_paths)) {
5930 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5931 goto done;
5934 TAILQ_FOREACH(pe, paths, entry) {
5935 err = check_path_is_commitable(pe->path, &commitable_paths);
5936 if (err)
5937 goto done;
5940 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5941 struct got_commitable *ct = pe->data;
5942 const char *ct_path = ct->in_repo_path;
5944 while (ct_path[0] == '/')
5945 ct_path++;
5946 err = check_out_of_date(ct_path, ct->status,
5947 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5948 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5949 if (err)
5950 goto done;
5954 err = commit_worktree(new_commit_id, &commitable_paths,
5955 head_commit_id, NULL, worktree, author, committer,
5956 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5957 if (err)
5958 goto done;
5960 err = update_fileindex_after_commit(worktree, &commitable_paths,
5961 *new_commit_id, fileindex, have_staged_files);
5962 sync_err = sync_fileindex(fileindex, fileindex_path);
5963 if (sync_err && err == NULL)
5964 err = sync_err;
5965 done:
5966 if (fileindex)
5967 got_fileindex_free(fileindex);
5968 free(fileindex_path);
5969 unlockerr = lock_worktree(worktree, LOCK_SH);
5970 if (unlockerr && err == NULL)
5971 err = unlockerr;
5972 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5973 struct got_commitable *ct = pe->data;
5974 free_commitable(ct);
5976 got_pathlist_free(&commitable_paths);
5977 return err;
5980 const char *
5981 got_commitable_get_path(struct got_commitable *ct)
5983 return ct->path;
5986 unsigned int
5987 got_commitable_get_status(struct got_commitable *ct)
5989 return ct->status;
5992 struct check_rebase_ok_arg {
5993 struct got_worktree *worktree;
5994 struct got_repository *repo;
5997 static const struct got_error *
5998 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6000 const struct got_error *err = NULL;
6001 struct check_rebase_ok_arg *a = arg;
6002 unsigned char status;
6003 struct stat sb;
6004 char *ondisk_path;
6006 /* Reject rebase of a work tree with mixed base commits. */
6007 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6008 SHA1_DIGEST_LENGTH))
6009 return got_error(GOT_ERR_MIXED_COMMITS);
6011 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6012 == -1)
6013 return got_error_from_errno("asprintf");
6015 /* Reject rebase of a work tree with modified or staged files. */
6016 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6017 free(ondisk_path);
6018 if (err)
6019 return err;
6021 if (status != GOT_STATUS_NO_CHANGE)
6022 return got_error(GOT_ERR_MODIFIED);
6023 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6024 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6026 return NULL;
6029 const struct got_error *
6030 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6031 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6032 struct got_worktree *worktree, struct got_reference *branch,
6033 struct got_repository *repo)
6035 const struct got_error *err = NULL;
6036 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6037 char *branch_ref_name = NULL;
6038 char *fileindex_path = NULL;
6039 struct check_rebase_ok_arg ok_arg;
6040 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6041 struct got_object_id *wt_branch_tip = NULL;
6043 *new_base_branch_ref = NULL;
6044 *tmp_branch = NULL;
6045 *fileindex = NULL;
6047 err = lock_worktree(worktree, LOCK_EX);
6048 if (err)
6049 return err;
6051 err = open_fileindex(fileindex, &fileindex_path, worktree);
6052 if (err)
6053 goto done;
6055 ok_arg.worktree = worktree;
6056 ok_arg.repo = repo;
6057 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6058 &ok_arg);
6059 if (err)
6060 goto done;
6062 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6063 if (err)
6064 goto done;
6066 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6067 if (err)
6068 goto done;
6070 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6071 if (err)
6072 goto done;
6074 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6075 0);
6076 if (err)
6077 goto done;
6079 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6080 if (err)
6081 goto done;
6082 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6083 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6084 goto done;
6087 err = got_ref_alloc_symref(new_base_branch_ref,
6088 new_base_branch_ref_name, wt_branch);
6089 if (err)
6090 goto done;
6091 err = got_ref_write(*new_base_branch_ref, repo);
6092 if (err)
6093 goto done;
6095 /* TODO Lock original branch's ref while rebasing? */
6097 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6098 if (err)
6099 goto done;
6101 err = got_ref_write(branch_ref, repo);
6102 if (err)
6103 goto done;
6105 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6106 worktree->base_commit_id);
6107 if (err)
6108 goto done;
6109 err = got_ref_write(*tmp_branch, repo);
6110 if (err)
6111 goto done;
6113 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6114 if (err)
6115 goto done;
6116 done:
6117 free(fileindex_path);
6118 free(tmp_branch_name);
6119 free(new_base_branch_ref_name);
6120 free(branch_ref_name);
6121 if (branch_ref)
6122 got_ref_close(branch_ref);
6123 if (wt_branch)
6124 got_ref_close(wt_branch);
6125 free(wt_branch_tip);
6126 if (err) {
6127 if (*new_base_branch_ref) {
6128 got_ref_close(*new_base_branch_ref);
6129 *new_base_branch_ref = NULL;
6131 if (*tmp_branch) {
6132 got_ref_close(*tmp_branch);
6133 *tmp_branch = NULL;
6135 if (*fileindex) {
6136 got_fileindex_free(*fileindex);
6137 *fileindex = NULL;
6139 lock_worktree(worktree, LOCK_SH);
6141 return err;
6144 const struct got_error *
6145 got_worktree_rebase_continue(struct got_object_id **commit_id,
6146 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6147 struct got_reference **branch, struct got_fileindex **fileindex,
6148 struct got_worktree *worktree, struct got_repository *repo)
6150 const struct got_error *err;
6151 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6152 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6153 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6154 char *fileindex_path = NULL;
6155 int have_staged_files = 0;
6157 *commit_id = NULL;
6158 *new_base_branch = NULL;
6159 *tmp_branch = NULL;
6160 *branch = NULL;
6161 *fileindex = NULL;
6163 err = lock_worktree(worktree, LOCK_EX);
6164 if (err)
6165 return err;
6167 err = open_fileindex(fileindex, &fileindex_path, worktree);
6168 if (err)
6169 goto done;
6171 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6172 &have_staged_files);
6173 if (err && err->code != GOT_ERR_CANCELLED)
6174 goto done;
6175 if (have_staged_files) {
6176 err = got_error(GOT_ERR_STAGED_PATHS);
6177 goto done;
6180 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6181 if (err)
6182 goto done;
6184 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6185 if (err)
6186 goto done;
6188 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6189 if (err)
6190 goto done;
6192 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6193 if (err)
6194 goto done;
6196 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6197 if (err)
6198 goto done;
6200 err = got_ref_open(branch, repo,
6201 got_ref_get_symref_target(branch_ref), 0);
6202 if (err)
6203 goto done;
6205 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6206 if (err)
6207 goto done;
6209 err = got_ref_resolve(commit_id, repo, commit_ref);
6210 if (err)
6211 goto done;
6213 err = got_ref_open(new_base_branch, repo,
6214 new_base_branch_ref_name, 0);
6215 if (err)
6216 goto done;
6218 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6219 if (err)
6220 goto done;
6221 done:
6222 free(commit_ref_name);
6223 free(branch_ref_name);
6224 free(fileindex_path);
6225 if (commit_ref)
6226 got_ref_close(commit_ref);
6227 if (branch_ref)
6228 got_ref_close(branch_ref);
6229 if (err) {
6230 free(*commit_id);
6231 *commit_id = NULL;
6232 if (*tmp_branch) {
6233 got_ref_close(*tmp_branch);
6234 *tmp_branch = NULL;
6236 if (*new_base_branch) {
6237 got_ref_close(*new_base_branch);
6238 *new_base_branch = NULL;
6240 if (*branch) {
6241 got_ref_close(*branch);
6242 *branch = NULL;
6244 if (*fileindex) {
6245 got_fileindex_free(*fileindex);
6246 *fileindex = NULL;
6248 lock_worktree(worktree, LOCK_SH);
6250 return err;
6253 const struct got_error *
6254 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6256 const struct got_error *err;
6257 char *tmp_branch_name = NULL;
6259 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6260 if (err)
6261 return err;
6263 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6264 free(tmp_branch_name);
6265 return NULL;
6268 static const struct got_error *
6269 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6270 char **logmsg, void *arg)
6272 *logmsg = arg;
6273 return NULL;
6276 static const struct got_error *
6277 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6278 const char *path, struct got_object_id *blob_id,
6279 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6280 int dirfd, const char *de_name)
6282 return NULL;
6285 struct collect_merged_paths_arg {
6286 got_worktree_checkout_cb progress_cb;
6287 void *progress_arg;
6288 struct got_pathlist_head *merged_paths;
6291 static const struct got_error *
6292 collect_merged_paths(void *arg, unsigned char status, const char *path)
6294 const struct got_error *err;
6295 struct collect_merged_paths_arg *a = arg;
6296 char *p;
6297 struct got_pathlist_entry *new;
6299 err = (*a->progress_cb)(a->progress_arg, status, path);
6300 if (err)
6301 return err;
6303 if (status != GOT_STATUS_MERGE &&
6304 status != GOT_STATUS_ADD &&
6305 status != GOT_STATUS_DELETE &&
6306 status != GOT_STATUS_CONFLICT)
6307 return NULL;
6309 p = strdup(path);
6310 if (p == NULL)
6311 return got_error_from_errno("strdup");
6313 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6314 if (err || new == NULL)
6315 free(p);
6316 return err;
6319 void
6320 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6322 struct got_pathlist_entry *pe;
6324 TAILQ_FOREACH(pe, merged_paths, entry)
6325 free((char *)pe->path);
6327 got_pathlist_free(merged_paths);
6330 static const struct got_error *
6331 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6332 int is_rebase, struct got_repository *repo)
6334 const struct got_error *err;
6335 struct got_reference *commit_ref = NULL;
6337 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6338 if (err) {
6339 if (err->code != GOT_ERR_NOT_REF)
6340 goto done;
6341 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6342 if (err)
6343 goto done;
6344 err = got_ref_write(commit_ref, repo);
6345 if (err)
6346 goto done;
6347 } else if (is_rebase) {
6348 struct got_object_id *stored_id;
6349 int cmp;
6351 err = got_ref_resolve(&stored_id, repo, commit_ref);
6352 if (err)
6353 goto done;
6354 cmp = got_object_id_cmp(commit_id, stored_id);
6355 free(stored_id);
6356 if (cmp != 0) {
6357 err = got_error(GOT_ERR_REBASE_COMMITID);
6358 goto done;
6361 done:
6362 if (commit_ref)
6363 got_ref_close(commit_ref);
6364 return err;
6367 static const struct got_error *
6368 rebase_merge_files(struct got_pathlist_head *merged_paths,
6369 const char *commit_ref_name, struct got_worktree *worktree,
6370 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6371 struct got_object_id *commit_id, struct got_repository *repo,
6372 got_worktree_checkout_cb progress_cb, void *progress_arg,
6373 got_cancel_cb cancel_cb, void *cancel_arg)
6375 const struct got_error *err;
6376 struct got_reference *commit_ref = NULL;
6377 struct collect_merged_paths_arg cmp_arg;
6378 char *fileindex_path;
6380 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6382 err = get_fileindex_path(&fileindex_path, worktree);
6383 if (err)
6384 return err;
6386 cmp_arg.progress_cb = progress_cb;
6387 cmp_arg.progress_arg = progress_arg;
6388 cmp_arg.merged_paths = merged_paths;
6389 err = merge_files(worktree, fileindex, fileindex_path,
6390 parent_commit_id, commit_id, repo, collect_merged_paths,
6391 &cmp_arg, cancel_cb, cancel_arg);
6392 if (commit_ref)
6393 got_ref_close(commit_ref);
6394 return err;
6397 const struct got_error *
6398 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6399 struct got_worktree *worktree, struct got_fileindex *fileindex,
6400 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6401 struct got_repository *repo,
6402 got_worktree_checkout_cb progress_cb, void *progress_arg,
6403 got_cancel_cb cancel_cb, void *cancel_arg)
6405 const struct got_error *err;
6406 char *commit_ref_name;
6408 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6409 if (err)
6410 return err;
6412 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6413 if (err)
6414 goto done;
6416 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6417 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6418 progress_arg, cancel_cb, cancel_arg);
6419 done:
6420 free(commit_ref_name);
6421 return err;
6424 const struct got_error *
6425 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6426 struct got_worktree *worktree, struct got_fileindex *fileindex,
6427 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6428 struct got_repository *repo,
6429 got_worktree_checkout_cb progress_cb, void *progress_arg,
6430 got_cancel_cb cancel_cb, void *cancel_arg)
6432 const struct got_error *err;
6433 char *commit_ref_name;
6435 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6436 if (err)
6437 return err;
6439 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6440 if (err)
6441 goto done;
6443 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6444 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6445 progress_arg, cancel_cb, cancel_arg);
6446 done:
6447 free(commit_ref_name);
6448 return err;
6451 static const struct got_error *
6452 rebase_commit(struct got_object_id **new_commit_id,
6453 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6454 struct got_worktree *worktree, struct got_fileindex *fileindex,
6455 struct got_reference *tmp_branch, const char *committer,
6456 struct got_commit_object *orig_commit, const char *new_logmsg,
6457 struct got_repository *repo)
6459 const struct got_error *err, *sync_err;
6460 struct got_pathlist_head commitable_paths;
6461 struct collect_commitables_arg cc_arg;
6462 char *fileindex_path = NULL;
6463 struct got_reference *head_ref = NULL;
6464 struct got_object_id *head_commit_id = NULL;
6465 char *logmsg = NULL;
6467 TAILQ_INIT(&commitable_paths);
6468 *new_commit_id = NULL;
6470 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6472 err = get_fileindex_path(&fileindex_path, worktree);
6473 if (err)
6474 return err;
6476 cc_arg.commitable_paths = &commitable_paths;
6477 cc_arg.worktree = worktree;
6478 cc_arg.repo = repo;
6479 cc_arg.have_staged_files = 0;
6481 * If possible get the status of individual files directly to
6482 * avoid crawling the entire work tree once per rebased commit.
6484 * Ideally, merged_paths would contain a list of commitables
6485 * we could use so we could skip worktree_status() entirely.
6486 * However, we would then need carefully keep track of cumulative
6487 * effects of operations such as file additions and deletions
6488 * in 'got histedit -f' (folding multiple commits into one),
6489 * and this extra complexity is not really worth it.
6491 if (merged_paths) {
6492 struct got_pathlist_entry *pe;
6493 TAILQ_FOREACH(pe, merged_paths, entry) {
6494 err = worktree_status(worktree, pe->path, fileindex,
6495 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6496 0);
6497 if (err)
6498 goto done;
6500 } else {
6501 err = worktree_status(worktree, "", fileindex, repo,
6502 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6503 if (err)
6504 goto done;
6507 if (TAILQ_EMPTY(&commitable_paths)) {
6508 /* No-op change; commit will be elided. */
6509 err = got_ref_delete(commit_ref, repo);
6510 if (err)
6511 goto done;
6512 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6513 goto done;
6516 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6517 if (err)
6518 goto done;
6520 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6521 if (err)
6522 goto done;
6524 if (new_logmsg) {
6525 logmsg = strdup(new_logmsg);
6526 if (logmsg == NULL) {
6527 err = got_error_from_errno("strdup");
6528 goto done;
6530 } else {
6531 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6532 if (err)
6533 goto done;
6536 /* NB: commit_worktree will call free(logmsg) */
6537 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6538 NULL, worktree, got_object_commit_get_author(orig_commit),
6539 committer ? committer :
6540 got_object_commit_get_committer(orig_commit),
6541 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6542 if (err)
6543 goto done;
6545 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6546 if (err)
6547 goto done;
6549 err = got_ref_delete(commit_ref, repo);
6550 if (err)
6551 goto done;
6553 err = update_fileindex_after_commit(worktree, &commitable_paths,
6554 *new_commit_id, fileindex, 0);
6555 sync_err = sync_fileindex(fileindex, fileindex_path);
6556 if (sync_err && err == NULL)
6557 err = sync_err;
6558 done:
6559 free(fileindex_path);
6560 free(head_commit_id);
6561 if (head_ref)
6562 got_ref_close(head_ref);
6563 if (err) {
6564 free(*new_commit_id);
6565 *new_commit_id = NULL;
6567 return err;
6570 const struct got_error *
6571 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6572 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6573 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6574 const char *committer, struct got_commit_object *orig_commit,
6575 struct got_object_id *orig_commit_id, struct got_repository *repo)
6577 const struct got_error *err;
6578 char *commit_ref_name;
6579 struct got_reference *commit_ref = NULL;
6580 struct got_object_id *commit_id = NULL;
6582 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6583 if (err)
6584 return err;
6586 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6587 if (err)
6588 goto done;
6589 err = got_ref_resolve(&commit_id, repo, commit_ref);
6590 if (err)
6591 goto done;
6592 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6593 err = got_error(GOT_ERR_REBASE_COMMITID);
6594 goto done;
6597 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6598 worktree, fileindex, tmp_branch, committer, orig_commit,
6599 NULL, repo);
6600 done:
6601 if (commit_ref)
6602 got_ref_close(commit_ref);
6603 free(commit_ref_name);
6604 free(commit_id);
6605 return err;
6608 const struct got_error *
6609 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6610 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6611 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6612 const char *committer, struct got_commit_object *orig_commit,
6613 struct got_object_id *orig_commit_id, const char *new_logmsg,
6614 struct got_repository *repo)
6616 const struct got_error *err;
6617 char *commit_ref_name;
6618 struct got_reference *commit_ref = NULL;
6620 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6621 if (err)
6622 return err;
6624 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6625 if (err)
6626 goto done;
6628 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6629 worktree, fileindex, tmp_branch, committer, orig_commit,
6630 new_logmsg, repo);
6631 done:
6632 if (commit_ref)
6633 got_ref_close(commit_ref);
6634 free(commit_ref_name);
6635 return err;
6638 const struct got_error *
6639 got_worktree_rebase_postpone(struct got_worktree *worktree,
6640 struct got_fileindex *fileindex)
6642 if (fileindex)
6643 got_fileindex_free(fileindex);
6644 return lock_worktree(worktree, LOCK_SH);
6647 static const struct got_error *
6648 delete_ref(const char *name, struct got_repository *repo)
6650 const struct got_error *err;
6651 struct got_reference *ref;
6653 err = got_ref_open(&ref, repo, name, 0);
6654 if (err) {
6655 if (err->code == GOT_ERR_NOT_REF)
6656 return NULL;
6657 return err;
6660 err = got_ref_delete(ref, repo);
6661 got_ref_close(ref);
6662 return err;
6665 static const struct got_error *
6666 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6668 const struct got_error *err;
6669 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6670 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6672 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6673 if (err)
6674 goto done;
6675 err = delete_ref(tmp_branch_name, repo);
6676 if (err)
6677 goto done;
6679 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6680 if (err)
6681 goto done;
6682 err = delete_ref(new_base_branch_ref_name, repo);
6683 if (err)
6684 goto done;
6686 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6687 if (err)
6688 goto done;
6689 err = delete_ref(branch_ref_name, repo);
6690 if (err)
6691 goto done;
6693 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6694 if (err)
6695 goto done;
6696 err = delete_ref(commit_ref_name, repo);
6697 if (err)
6698 goto done;
6700 done:
6701 free(tmp_branch_name);
6702 free(new_base_branch_ref_name);
6703 free(branch_ref_name);
6704 free(commit_ref_name);
6705 return err;
6708 static const struct got_error *
6709 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6710 struct got_object_id *new_commit_id, struct got_repository *repo)
6712 const struct got_error *err;
6713 struct got_reference *ref = NULL;
6714 struct got_object_id *old_commit_id = NULL;
6715 const char *branch_name = NULL;
6716 char *new_id_str = NULL;
6717 char *refname = NULL;
6719 branch_name = got_ref_get_name(branch);
6720 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6721 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6722 branch_name += 11;
6724 err = got_object_id_str(&new_id_str, new_commit_id);
6725 if (err)
6726 return err;
6728 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6729 new_id_str) == -1) {
6730 err = got_error_from_errno("asprintf");
6731 goto done;
6734 err = got_ref_resolve(&old_commit_id, repo, branch);
6735 if (err)
6736 goto done;
6738 err = got_ref_alloc(&ref, refname, old_commit_id);
6739 if (err)
6740 goto done;
6742 err = got_ref_write(ref, repo);
6743 done:
6744 free(new_id_str);
6745 free(refname);
6746 free(old_commit_id);
6747 if (ref)
6748 got_ref_close(ref);
6749 return err;
6752 const struct got_error *
6753 got_worktree_rebase_complete(struct got_worktree *worktree,
6754 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6755 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6756 struct got_repository *repo, int create_backup)
6758 const struct got_error *err, *unlockerr, *sync_err;
6759 struct got_object_id *new_head_commit_id = NULL;
6760 char *fileindex_path = NULL;
6762 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6763 if (err)
6764 return err;
6766 if (create_backup) {
6767 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6768 rebased_branch, new_head_commit_id, repo);
6769 if (err)
6770 goto done;
6773 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6774 if (err)
6775 goto done;
6777 err = got_ref_write(rebased_branch, repo);
6778 if (err)
6779 goto done;
6781 err = got_worktree_set_head_ref(worktree, rebased_branch);
6782 if (err)
6783 goto done;
6785 err = delete_rebase_refs(worktree, repo);
6786 if (err)
6787 goto done;
6789 err = get_fileindex_path(&fileindex_path, worktree);
6790 if (err)
6791 goto done;
6792 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6793 sync_err = sync_fileindex(fileindex, fileindex_path);
6794 if (sync_err && err == NULL)
6795 err = sync_err;
6796 done:
6797 got_fileindex_free(fileindex);
6798 free(fileindex_path);
6799 free(new_head_commit_id);
6800 unlockerr = lock_worktree(worktree, LOCK_SH);
6801 if (unlockerr && err == NULL)
6802 err = unlockerr;
6803 return err;
6806 const struct got_error *
6807 got_worktree_rebase_abort(struct got_worktree *worktree,
6808 struct got_fileindex *fileindex, struct got_repository *repo,
6809 struct got_reference *new_base_branch,
6810 got_worktree_checkout_cb progress_cb, void *progress_arg)
6812 const struct got_error *err, *unlockerr, *sync_err;
6813 struct got_reference *resolved = NULL;
6814 struct got_object_id *commit_id = NULL;
6815 struct got_commit_object *commit = NULL;
6816 char *fileindex_path = NULL;
6817 struct revert_file_args rfa;
6818 struct got_object_id *tree_id = NULL;
6820 err = lock_worktree(worktree, LOCK_EX);
6821 if (err)
6822 return err;
6824 err = got_object_open_as_commit(&commit, repo,
6825 worktree->base_commit_id);
6826 if (err)
6827 goto done;
6829 err = got_ref_open(&resolved, repo,
6830 got_ref_get_symref_target(new_base_branch), 0);
6831 if (err)
6832 goto done;
6834 err = got_worktree_set_head_ref(worktree, resolved);
6835 if (err)
6836 goto done;
6839 * XXX commits to the base branch could have happened while
6840 * we were busy rebasing; should we store the original commit ID
6841 * when rebase begins and read it back here?
6843 err = got_ref_resolve(&commit_id, repo, resolved);
6844 if (err)
6845 goto done;
6847 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6848 if (err)
6849 goto done;
6851 err = got_object_id_by_path(&tree_id, repo, commit,
6852 worktree->path_prefix);
6853 if (err)
6854 goto done;
6856 err = delete_rebase_refs(worktree, repo);
6857 if (err)
6858 goto done;
6860 err = get_fileindex_path(&fileindex_path, worktree);
6861 if (err)
6862 goto done;
6864 rfa.worktree = worktree;
6865 rfa.fileindex = fileindex;
6866 rfa.progress_cb = progress_cb;
6867 rfa.progress_arg = progress_arg;
6868 rfa.patch_cb = NULL;
6869 rfa.patch_arg = NULL;
6870 rfa.repo = repo;
6871 rfa.unlink_added_files = 0;
6872 err = worktree_status(worktree, "", fileindex, repo,
6873 revert_file, &rfa, NULL, NULL, 1, 0);
6874 if (err)
6875 goto sync;
6877 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6878 repo, progress_cb, progress_arg, NULL, NULL);
6879 sync:
6880 sync_err = sync_fileindex(fileindex, fileindex_path);
6881 if (sync_err && err == NULL)
6882 err = sync_err;
6883 done:
6884 got_ref_close(resolved);
6885 free(tree_id);
6886 free(commit_id);
6887 if (commit)
6888 got_object_commit_close(commit);
6889 if (fileindex)
6890 got_fileindex_free(fileindex);
6891 free(fileindex_path);
6893 unlockerr = lock_worktree(worktree, LOCK_SH);
6894 if (unlockerr && err == NULL)
6895 err = unlockerr;
6896 return err;
6899 const struct got_error *
6900 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6901 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6902 struct got_fileindex **fileindex, struct got_worktree *worktree,
6903 struct got_repository *repo)
6905 const struct got_error *err = NULL;
6906 char *tmp_branch_name = NULL;
6907 char *branch_ref_name = NULL;
6908 char *base_commit_ref_name = NULL;
6909 char *fileindex_path = NULL;
6910 struct check_rebase_ok_arg ok_arg;
6911 struct got_reference *wt_branch = NULL;
6912 struct got_reference *base_commit_ref = NULL;
6914 *tmp_branch = NULL;
6915 *branch_ref = NULL;
6916 *base_commit_id = NULL;
6917 *fileindex = NULL;
6919 err = lock_worktree(worktree, LOCK_EX);
6920 if (err)
6921 return err;
6923 err = open_fileindex(fileindex, &fileindex_path, worktree);
6924 if (err)
6925 goto done;
6927 ok_arg.worktree = worktree;
6928 ok_arg.repo = repo;
6929 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6930 &ok_arg);
6931 if (err)
6932 goto done;
6934 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6935 if (err)
6936 goto done;
6938 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6939 if (err)
6940 goto done;
6942 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6943 worktree);
6944 if (err)
6945 goto done;
6947 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6948 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6953 if (err)
6954 goto done;
6956 err = got_ref_write(*branch_ref, repo);
6957 if (err)
6958 goto done;
6960 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6961 worktree->base_commit_id);
6962 if (err)
6963 goto done;
6964 err = got_ref_write(base_commit_ref, repo);
6965 if (err)
6966 goto done;
6967 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6968 if (*base_commit_id == NULL) {
6969 err = got_error_from_errno("got_object_id_dup");
6970 goto done;
6973 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6974 worktree->base_commit_id);
6975 if (err)
6976 goto done;
6977 err = got_ref_write(*tmp_branch, repo);
6978 if (err)
6979 goto done;
6981 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6982 if (err)
6983 goto done;
6984 done:
6985 free(fileindex_path);
6986 free(tmp_branch_name);
6987 free(branch_ref_name);
6988 free(base_commit_ref_name);
6989 if (wt_branch)
6990 got_ref_close(wt_branch);
6991 if (err) {
6992 if (*branch_ref) {
6993 got_ref_close(*branch_ref);
6994 *branch_ref = NULL;
6996 if (*tmp_branch) {
6997 got_ref_close(*tmp_branch);
6998 *tmp_branch = NULL;
7000 free(*base_commit_id);
7001 if (*fileindex) {
7002 got_fileindex_free(*fileindex);
7003 *fileindex = NULL;
7005 lock_worktree(worktree, LOCK_SH);
7007 return err;
7010 const struct got_error *
7011 got_worktree_histedit_postpone(struct got_worktree *worktree,
7012 struct got_fileindex *fileindex)
7014 if (fileindex)
7015 got_fileindex_free(fileindex);
7016 return lock_worktree(worktree, LOCK_SH);
7019 const struct got_error *
7020 got_worktree_histedit_in_progress(int *in_progress,
7021 struct got_worktree *worktree)
7023 const struct got_error *err;
7024 char *tmp_branch_name = NULL;
7026 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7027 if (err)
7028 return err;
7030 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7031 free(tmp_branch_name);
7032 return NULL;
7035 const struct got_error *
7036 got_worktree_histedit_continue(struct got_object_id **commit_id,
7037 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7038 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7039 struct got_worktree *worktree, struct got_repository *repo)
7041 const struct got_error *err;
7042 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7043 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7044 struct got_reference *commit_ref = NULL;
7045 struct got_reference *base_commit_ref = NULL;
7046 char *fileindex_path = NULL;
7047 int have_staged_files = 0;
7049 *commit_id = NULL;
7050 *tmp_branch = NULL;
7051 *base_commit_id = NULL;
7052 *fileindex = NULL;
7054 err = lock_worktree(worktree, LOCK_EX);
7055 if (err)
7056 return err;
7058 err = open_fileindex(fileindex, &fileindex_path, worktree);
7059 if (err)
7060 goto done;
7062 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7063 &have_staged_files);
7064 if (err && err->code != GOT_ERR_CANCELLED)
7065 goto done;
7066 if (have_staged_files) {
7067 err = got_error(GOT_ERR_STAGED_PATHS);
7068 goto done;
7071 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7072 if (err)
7073 goto done;
7075 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7076 if (err)
7077 goto done;
7079 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7080 if (err)
7081 goto done;
7083 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7084 worktree);
7085 if (err)
7086 goto done;
7088 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7089 if (err)
7090 goto done;
7092 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7093 if (err)
7094 goto done;
7095 err = got_ref_resolve(commit_id, repo, commit_ref);
7096 if (err)
7097 goto done;
7099 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7100 if (err)
7101 goto done;
7102 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7103 if (err)
7104 goto done;
7106 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7107 if (err)
7108 goto done;
7109 done:
7110 free(commit_ref_name);
7111 free(branch_ref_name);
7112 free(fileindex_path);
7113 if (commit_ref)
7114 got_ref_close(commit_ref);
7115 if (base_commit_ref)
7116 got_ref_close(base_commit_ref);
7117 if (err) {
7118 free(*commit_id);
7119 *commit_id = NULL;
7120 free(*base_commit_id);
7121 *base_commit_id = NULL;
7122 if (*tmp_branch) {
7123 got_ref_close(*tmp_branch);
7124 *tmp_branch = NULL;
7126 if (*fileindex) {
7127 got_fileindex_free(*fileindex);
7128 *fileindex = NULL;
7130 lock_worktree(worktree, LOCK_EX);
7132 return err;
7135 static const struct got_error *
7136 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7138 const struct got_error *err;
7139 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7140 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7142 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7143 if (err)
7144 goto done;
7145 err = delete_ref(tmp_branch_name, repo);
7146 if (err)
7147 goto done;
7149 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7150 worktree);
7151 if (err)
7152 goto done;
7153 err = delete_ref(base_commit_ref_name, repo);
7154 if (err)
7155 goto done;
7157 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7158 if (err)
7159 goto done;
7160 err = delete_ref(branch_ref_name, repo);
7161 if (err)
7162 goto done;
7164 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7165 if (err)
7166 goto done;
7167 err = delete_ref(commit_ref_name, repo);
7168 if (err)
7169 goto done;
7170 done:
7171 free(tmp_branch_name);
7172 free(base_commit_ref_name);
7173 free(branch_ref_name);
7174 free(commit_ref_name);
7175 return err;
7178 const struct got_error *
7179 got_worktree_histedit_abort(struct got_worktree *worktree,
7180 struct got_fileindex *fileindex, struct got_repository *repo,
7181 struct got_reference *branch, struct got_object_id *base_commit_id,
7182 got_worktree_checkout_cb progress_cb, void *progress_arg)
7184 const struct got_error *err, *unlockerr, *sync_err;
7185 struct got_reference *resolved = NULL;
7186 char *fileindex_path = NULL;
7187 struct got_commit_object *commit = NULL;
7188 struct got_object_id *tree_id = NULL;
7189 struct revert_file_args rfa;
7191 err = lock_worktree(worktree, LOCK_EX);
7192 if (err)
7193 return err;
7195 err = got_object_open_as_commit(&commit, repo,
7196 worktree->base_commit_id);
7197 if (err)
7198 goto done;
7200 err = got_ref_open(&resolved, repo,
7201 got_ref_get_symref_target(branch), 0);
7202 if (err)
7203 goto done;
7205 err = got_worktree_set_head_ref(worktree, resolved);
7206 if (err)
7207 goto done;
7209 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7210 if (err)
7211 goto done;
7213 err = got_object_id_by_path(&tree_id, repo, commit,
7214 worktree->path_prefix);
7215 if (err)
7216 goto done;
7218 err = delete_histedit_refs(worktree, repo);
7219 if (err)
7220 goto done;
7222 err = get_fileindex_path(&fileindex_path, worktree);
7223 if (err)
7224 goto done;
7226 rfa.worktree = worktree;
7227 rfa.fileindex = fileindex;
7228 rfa.progress_cb = progress_cb;
7229 rfa.progress_arg = progress_arg;
7230 rfa.patch_cb = NULL;
7231 rfa.patch_arg = NULL;
7232 rfa.repo = repo;
7233 rfa.unlink_added_files = 0;
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 revert_file, &rfa, NULL, NULL, 1, 0);
7236 if (err)
7237 goto sync;
7239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 repo, progress_cb, progress_arg, NULL, NULL);
7241 sync:
7242 sync_err = sync_fileindex(fileindex, fileindex_path);
7243 if (sync_err && err == NULL)
7244 err = sync_err;
7245 done:
7246 got_ref_close(resolved);
7247 free(tree_id);
7248 free(fileindex_path);
7250 unlockerr = lock_worktree(worktree, LOCK_SH);
7251 if (unlockerr && err == NULL)
7252 err = unlockerr;
7253 return err;
7256 const struct got_error *
7257 got_worktree_histedit_complete(struct got_worktree *worktree,
7258 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7259 struct got_reference *edited_branch, struct got_repository *repo)
7261 const struct got_error *err, *unlockerr, *sync_err;
7262 struct got_object_id *new_head_commit_id = NULL;
7263 struct got_reference *resolved = NULL;
7264 char *fileindex_path = NULL;
7266 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7267 if (err)
7268 return err;
7270 err = got_ref_open(&resolved, repo,
7271 got_ref_get_symref_target(edited_branch), 0);
7272 if (err)
7273 goto done;
7275 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7276 resolved, new_head_commit_id, repo);
7277 if (err)
7278 goto done;
7280 err = got_ref_change_ref(resolved, new_head_commit_id);
7281 if (err)
7282 goto done;
7284 err = got_ref_write(resolved, repo);
7285 if (err)
7286 goto done;
7288 err = got_worktree_set_head_ref(worktree, resolved);
7289 if (err)
7290 goto done;
7292 err = delete_histedit_refs(worktree, repo);
7293 if (err)
7294 goto done;
7296 err = get_fileindex_path(&fileindex_path, worktree);
7297 if (err)
7298 goto done;
7299 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7300 sync_err = sync_fileindex(fileindex, fileindex_path);
7301 if (sync_err && err == NULL)
7302 err = sync_err;
7303 done:
7304 got_fileindex_free(fileindex);
7305 free(fileindex_path);
7306 free(new_head_commit_id);
7307 unlockerr = lock_worktree(worktree, LOCK_SH);
7308 if (unlockerr && err == NULL)
7309 err = unlockerr;
7310 return err;
7313 const struct got_error *
7314 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7315 struct got_object_id *commit_id, struct got_repository *repo)
7317 const struct got_error *err;
7318 char *commit_ref_name;
7320 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7321 if (err)
7322 return err;
7324 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7325 if (err)
7326 goto done;
7328 err = delete_ref(commit_ref_name, repo);
7329 done:
7330 free(commit_ref_name);
7331 return err;
7334 const struct got_error *
7335 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7336 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7337 struct got_worktree *worktree, const char *refname,
7338 struct got_repository *repo)
7340 const struct got_error *err = NULL;
7341 char *fileindex_path = NULL;
7342 struct check_rebase_ok_arg ok_arg;
7344 *fileindex = NULL;
7345 *branch_ref = NULL;
7346 *base_branch_ref = NULL;
7348 err = lock_worktree(worktree, LOCK_EX);
7349 if (err)
7350 return err;
7352 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7353 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7354 "cannot integrate a branch into itself; "
7355 "update -b or different branch name required");
7356 goto done;
7359 err = open_fileindex(fileindex, &fileindex_path, worktree);
7360 if (err)
7361 goto done;
7363 /* Preconditions are the same as for rebase. */
7364 ok_arg.worktree = worktree;
7365 ok_arg.repo = repo;
7366 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7367 &ok_arg);
7368 if (err)
7369 goto done;
7371 err = got_ref_open(branch_ref, repo, refname, 1);
7372 if (err)
7373 goto done;
7375 err = got_ref_open(base_branch_ref, repo,
7376 got_worktree_get_head_ref_name(worktree), 1);
7377 done:
7378 if (err) {
7379 if (*branch_ref) {
7380 got_ref_close(*branch_ref);
7381 *branch_ref = NULL;
7383 if (*base_branch_ref) {
7384 got_ref_close(*base_branch_ref);
7385 *base_branch_ref = NULL;
7387 if (*fileindex) {
7388 got_fileindex_free(*fileindex);
7389 *fileindex = NULL;
7391 lock_worktree(worktree, LOCK_SH);
7393 return err;
7396 const struct got_error *
7397 got_worktree_integrate_continue(struct got_worktree *worktree,
7398 struct got_fileindex *fileindex, struct got_repository *repo,
7399 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7400 got_worktree_checkout_cb progress_cb, void *progress_arg,
7401 got_cancel_cb cancel_cb, void *cancel_arg)
7403 const struct got_error *err = NULL, *sync_err, *unlockerr;
7404 char *fileindex_path = NULL;
7405 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7406 struct got_commit_object *commit = NULL;
7408 err = get_fileindex_path(&fileindex_path, worktree);
7409 if (err)
7410 goto done;
7412 err = got_ref_resolve(&commit_id, repo, branch_ref);
7413 if (err)
7414 goto done;
7416 err = got_object_open_as_commit(&commit, repo, commit_id);
7417 if (err)
7418 goto done;
7420 err = got_object_id_by_path(&tree_id, repo, commit,
7421 worktree->path_prefix);
7422 if (err)
7423 goto done;
7425 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7426 if (err)
7427 goto done;
7429 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7430 progress_cb, progress_arg, cancel_cb, cancel_arg);
7431 if (err)
7432 goto sync;
7434 err = got_ref_change_ref(base_branch_ref, commit_id);
7435 if (err)
7436 goto sync;
7438 err = got_ref_write(base_branch_ref, repo);
7439 if (err)
7440 goto sync;
7442 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7443 sync:
7444 sync_err = sync_fileindex(fileindex, fileindex_path);
7445 if (sync_err && err == NULL)
7446 err = sync_err;
7448 done:
7449 unlockerr = got_ref_unlock(branch_ref);
7450 if (unlockerr && err == NULL)
7451 err = unlockerr;
7452 got_ref_close(branch_ref);
7454 unlockerr = got_ref_unlock(base_branch_ref);
7455 if (unlockerr && err == NULL)
7456 err = unlockerr;
7457 got_ref_close(base_branch_ref);
7459 got_fileindex_free(fileindex);
7460 free(fileindex_path);
7461 free(tree_id);
7462 if (commit)
7463 got_object_commit_close(commit);
7465 unlockerr = lock_worktree(worktree, LOCK_SH);
7466 if (unlockerr && err == NULL)
7467 err = unlockerr;
7468 return err;
7471 const struct got_error *
7472 got_worktree_integrate_abort(struct got_worktree *worktree,
7473 struct got_fileindex *fileindex, struct got_repository *repo,
7474 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7476 const struct got_error *err = NULL, *unlockerr = NULL;
7478 got_fileindex_free(fileindex);
7480 err = lock_worktree(worktree, LOCK_SH);
7482 unlockerr = got_ref_unlock(branch_ref);
7483 if (unlockerr && err == NULL)
7484 err = unlockerr;
7485 got_ref_close(branch_ref);
7487 unlockerr = got_ref_unlock(base_branch_ref);
7488 if (unlockerr && err == NULL)
7489 err = unlockerr;
7490 got_ref_close(base_branch_ref);
7492 return err;
7495 const struct got_error *
7496 got_worktree_merge_postpone(struct got_worktree *worktree,
7497 struct got_fileindex *fileindex)
7499 const struct got_error *err, *sync_err;
7500 char *fileindex_path = NULL;
7502 err = get_fileindex_path(&fileindex_path, worktree);
7503 if (err)
7504 goto done;
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7508 err = lock_worktree(worktree, LOCK_SH);
7509 if (sync_err && err == NULL)
7510 err = sync_err;
7511 done:
7512 got_fileindex_free(fileindex);
7513 free(fileindex_path);
7514 return err;
7517 static const struct got_error *
7518 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7520 const struct got_error *err;
7521 char *branch_refname = NULL, *commit_refname = NULL;
7523 err = get_merge_branch_ref_name(&branch_refname, worktree);
7524 if (err)
7525 goto done;
7526 err = delete_ref(branch_refname, repo);
7527 if (err)
7528 goto done;
7530 err = get_merge_commit_ref_name(&commit_refname, worktree);
7531 if (err)
7532 goto done;
7533 err = delete_ref(commit_refname, repo);
7534 if (err)
7535 goto done;
7537 done:
7538 free(branch_refname);
7539 free(commit_refname);
7540 return err;
7543 struct merge_commit_msg_arg {
7544 struct got_worktree *worktree;
7545 const char *branch_name;
7548 static const struct got_error *
7549 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7550 void *arg)
7552 struct merge_commit_msg_arg *a = arg;
7554 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7555 got_worktree_get_head_ref_name(a->worktree)) == -1)
7556 return got_error_from_errno("asprintf");
7558 return NULL;
7562 const struct got_error *
7563 got_worktree_merge_branch(struct got_worktree *worktree,
7564 struct got_fileindex *fileindex,
7565 struct got_object_id *yca_commit_id,
7566 struct got_object_id *branch_tip,
7567 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7568 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7570 const struct got_error *err;
7571 char *fileindex_path = NULL;
7573 err = get_fileindex_path(&fileindex_path, worktree);
7574 if (err)
7575 goto done;
7577 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7578 worktree);
7579 if (err)
7580 goto done;
7582 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7583 branch_tip, repo, progress_cb, progress_arg,
7584 cancel_cb, cancel_arg);
7585 done:
7586 free(fileindex_path);
7587 return err;
7590 const struct got_error *
7591 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7592 struct got_worktree *worktree, struct got_fileindex *fileindex,
7593 const char *author, const char *committer, int allow_bad_symlinks,
7594 struct got_object_id *branch_tip, const char *branch_name,
7595 struct got_repository *repo,
7596 got_worktree_status_cb status_cb, void *status_arg)
7599 const struct got_error *err = NULL, *sync_err;
7600 struct got_pathlist_head commitable_paths;
7601 struct collect_commitables_arg cc_arg;
7602 struct got_pathlist_entry *pe;
7603 struct got_reference *head_ref = NULL;
7604 struct got_object_id *head_commit_id = NULL;
7605 int have_staged_files = 0;
7606 struct merge_commit_msg_arg mcm_arg;
7607 char *fileindex_path = NULL;
7609 *new_commit_id = NULL;
7611 TAILQ_INIT(&commitable_paths);
7613 err = get_fileindex_path(&fileindex_path, worktree);
7614 if (err)
7615 goto done;
7617 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7618 if (err)
7619 goto done;
7621 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7622 if (err)
7623 goto done;
7625 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7626 &have_staged_files);
7627 if (err && err->code != GOT_ERR_CANCELLED)
7628 goto done;
7629 if (have_staged_files) {
7630 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7631 goto done;
7634 cc_arg.commitable_paths = &commitable_paths;
7635 cc_arg.worktree = worktree;
7636 cc_arg.fileindex = fileindex;
7637 cc_arg.repo = repo;
7638 cc_arg.have_staged_files = have_staged_files;
7639 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7640 err = worktree_status(worktree, "", fileindex, repo,
7641 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7642 if (err)
7643 goto done;
7645 if (TAILQ_EMPTY(&commitable_paths)) {
7646 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7647 "merge of %s cannot proceed", branch_name);
7648 goto done;
7651 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7652 struct got_commitable *ct = pe->data;
7653 const char *ct_path = ct->in_repo_path;
7655 while (ct_path[0] == '/')
7656 ct_path++;
7657 err = check_out_of_date(ct_path, ct->status,
7658 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7659 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7660 if (err)
7661 goto done;
7665 mcm_arg.worktree = worktree;
7666 mcm_arg.branch_name = branch_name;
7667 err = commit_worktree(new_commit_id, &commitable_paths,
7668 head_commit_id, branch_tip, worktree, author, committer,
7669 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7670 if (err)
7671 goto done;
7673 err = update_fileindex_after_commit(worktree, &commitable_paths,
7674 *new_commit_id, fileindex, have_staged_files);
7675 sync_err = sync_fileindex(fileindex, fileindex_path);
7676 if (sync_err && err == NULL)
7677 err = sync_err;
7678 done:
7679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7680 struct got_commitable *ct = pe->data;
7681 free_commitable(ct);
7683 got_pathlist_free(&commitable_paths);
7684 free(fileindex_path);
7685 return err;
7688 const struct got_error *
7689 got_worktree_merge_complete(struct got_worktree *worktree,
7690 struct got_fileindex *fileindex, struct got_repository *repo)
7692 const struct got_error *err, *unlockerr, *sync_err;
7693 char *fileindex_path = NULL;
7695 err = delete_merge_refs(worktree, repo);
7696 if (err)
7697 goto done;
7699 err = get_fileindex_path(&fileindex_path, worktree);
7700 if (err)
7701 goto done;
7702 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7703 sync_err = sync_fileindex(fileindex, fileindex_path);
7704 if (sync_err && err == NULL)
7705 err = sync_err;
7706 done:
7707 got_fileindex_free(fileindex);
7708 free(fileindex_path);
7709 unlockerr = lock_worktree(worktree, LOCK_SH);
7710 if (unlockerr && err == NULL)
7711 err = unlockerr;
7712 return err;
7715 const struct got_error *
7716 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7717 struct got_repository *repo)
7719 const struct got_error *err;
7720 char *branch_refname = NULL;
7721 struct got_reference *branch_ref = NULL;
7723 *in_progress = 0;
7725 err = get_merge_branch_ref_name(&branch_refname, worktree);
7726 if (err)
7727 return err;
7728 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7729 free(branch_refname);
7730 if (err) {
7731 if (err->code != GOT_ERR_NOT_REF)
7732 return err;
7733 } else
7734 *in_progress = 1;
7736 return NULL;
7739 const struct got_error *got_worktree_merge_prepare(
7740 struct got_fileindex **fileindex, struct got_worktree *worktree,
7741 struct got_reference *branch, struct got_repository *repo)
7743 const struct got_error *err = NULL;
7744 char *fileindex_path = NULL;
7745 char *branch_refname = NULL, *commit_refname = NULL;
7746 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7747 struct got_reference *commit_ref = NULL;
7748 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7749 struct check_rebase_ok_arg ok_arg;
7751 *fileindex = NULL;
7753 err = lock_worktree(worktree, LOCK_EX);
7754 if (err)
7755 return err;
7757 err = open_fileindex(fileindex, &fileindex_path, worktree);
7758 if (err)
7759 goto done;
7761 /* Preconditions are the same as for rebase. */
7762 ok_arg.worktree = worktree;
7763 ok_arg.repo = repo;
7764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7765 &ok_arg);
7766 if (err)
7767 goto done;
7769 err = get_merge_branch_ref_name(&branch_refname, worktree);
7770 if (err)
7771 return err;
7773 err = get_merge_commit_ref_name(&commit_refname, worktree);
7774 if (err)
7775 return err;
7777 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7778 0);
7779 if (err)
7780 goto done;
7782 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7783 if (err)
7784 goto done;
7786 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7787 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7788 goto done;
7791 err = got_ref_resolve(&branch_tip, repo, branch);
7792 if (err)
7793 goto done;
7795 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7796 if (err)
7797 goto done;
7798 err = got_ref_write(branch_ref, repo);
7799 if (err)
7800 goto done;
7802 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7803 if (err)
7804 goto done;
7805 err = got_ref_write(commit_ref, repo);
7806 if (err)
7807 goto done;
7809 done:
7810 free(branch_refname);
7811 free(commit_refname);
7812 free(fileindex_path);
7813 if (branch_ref)
7814 got_ref_close(branch_ref);
7815 if (commit_ref)
7816 got_ref_close(commit_ref);
7817 if (wt_branch)
7818 got_ref_close(wt_branch);
7819 free(wt_branch_tip);
7820 if (err) {
7821 if (*fileindex) {
7822 got_fileindex_free(*fileindex);
7823 *fileindex = NULL;
7825 lock_worktree(worktree, LOCK_SH);
7827 return err;
7830 const struct got_error *
7831 got_worktree_merge_continue(char **branch_name,
7832 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7833 struct got_worktree *worktree, struct got_repository *repo)
7835 const struct got_error *err;
7836 char *commit_refname = NULL, *branch_refname = NULL;
7837 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7838 char *fileindex_path = NULL;
7839 int have_staged_files = 0;
7841 *branch_name = NULL;
7842 *branch_tip = NULL;
7843 *fileindex = NULL;
7845 err = lock_worktree(worktree, LOCK_EX);
7846 if (err)
7847 return err;
7849 err = open_fileindex(fileindex, &fileindex_path, worktree);
7850 if (err)
7851 goto done;
7853 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7854 &have_staged_files);
7855 if (err && err->code != GOT_ERR_CANCELLED)
7856 goto done;
7857 if (have_staged_files) {
7858 err = got_error(GOT_ERR_STAGED_PATHS);
7859 goto done;
7862 err = get_merge_branch_ref_name(&branch_refname, worktree);
7863 if (err)
7864 goto done;
7866 err = get_merge_commit_ref_name(&commit_refname, worktree);
7867 if (err)
7868 goto done;
7870 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7871 if (err)
7872 goto done;
7874 if (!got_ref_is_symbolic(branch_ref)) {
7875 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7876 "%s is not a symbolic reference",
7877 got_ref_get_name(branch_ref));
7878 goto done;
7880 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7881 if (*branch_name == NULL) {
7882 err = got_error_from_errno("strdup");
7883 goto done;
7886 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7887 if (err)
7888 goto done;
7890 err = got_ref_resolve(branch_tip, repo, commit_ref);
7891 if (err)
7892 goto done;
7893 done:
7894 free(commit_refname);
7895 free(branch_refname);
7896 free(fileindex_path);
7897 if (commit_ref)
7898 got_ref_close(commit_ref);
7899 if (branch_ref)
7900 got_ref_close(branch_ref);
7901 if (err) {
7902 if (*branch_name) {
7903 free(*branch_name);
7904 *branch_name = NULL;
7906 free(*branch_tip);
7907 *branch_tip = NULL;
7908 if (*fileindex) {
7909 got_fileindex_free(*fileindex);
7910 *fileindex = NULL;
7912 lock_worktree(worktree, LOCK_SH);
7914 return err;
7917 const struct got_error *
7918 got_worktree_merge_abort(struct got_worktree *worktree,
7919 struct got_fileindex *fileindex, struct got_repository *repo,
7920 got_worktree_checkout_cb progress_cb, void *progress_arg)
7922 const struct got_error *err, *unlockerr, *sync_err;
7923 struct got_object_id *commit_id = NULL;
7924 struct got_commit_object *commit = NULL;
7925 char *fileindex_path = NULL;
7926 struct revert_file_args rfa;
7927 struct got_object_id *tree_id = NULL;
7929 err = got_object_open_as_commit(&commit, repo,
7930 worktree->base_commit_id);
7931 if (err)
7932 goto done;
7934 err = got_object_id_by_path(&tree_id, repo, commit,
7935 worktree->path_prefix);
7936 if (err)
7937 goto done;
7939 err = delete_merge_refs(worktree, repo);
7940 if (err)
7941 goto done;
7943 err = get_fileindex_path(&fileindex_path, worktree);
7944 if (err)
7945 goto done;
7947 rfa.worktree = worktree;
7948 rfa.fileindex = fileindex;
7949 rfa.progress_cb = progress_cb;
7950 rfa.progress_arg = progress_arg;
7951 rfa.patch_cb = NULL;
7952 rfa.patch_arg = NULL;
7953 rfa.repo = repo;
7954 rfa.unlink_added_files = 1;
7955 err = worktree_status(worktree, "", fileindex, repo,
7956 revert_file, &rfa, NULL, NULL, 1, 0);
7957 if (err)
7958 goto sync;
7960 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7961 repo, progress_cb, progress_arg, NULL, NULL);
7962 sync:
7963 sync_err = sync_fileindex(fileindex, fileindex_path);
7964 if (sync_err && err == NULL)
7965 err = sync_err;
7966 done:
7967 free(tree_id);
7968 free(commit_id);
7969 if (commit)
7970 got_object_commit_close(commit);
7971 if (fileindex)
7972 got_fileindex_free(fileindex);
7973 free(fileindex_path);
7975 unlockerr = lock_worktree(worktree, LOCK_SH);
7976 if (unlockerr && err == NULL)
7977 err = unlockerr;
7978 return err;
7981 struct check_stage_ok_arg {
7982 struct got_object_id *head_commit_id;
7983 struct got_worktree *worktree;
7984 struct got_fileindex *fileindex;
7985 struct got_repository *repo;
7986 int have_changes;
7989 static const struct got_error *
7990 check_stage_ok(void *arg, unsigned char status,
7991 unsigned char staged_status, const char *relpath,
7992 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7993 struct got_object_id *commit_id, int dirfd, const char *de_name)
7995 struct check_stage_ok_arg *a = arg;
7996 const struct got_error *err = NULL;
7997 struct got_fileindex_entry *ie;
7998 struct got_object_id base_commit_id;
7999 struct got_object_id *base_commit_idp = NULL;
8000 char *in_repo_path = NULL, *p;
8002 if (status == GOT_STATUS_UNVERSIONED ||
8003 status == GOT_STATUS_NO_CHANGE)
8004 return NULL;
8005 if (status == GOT_STATUS_NONEXISTENT)
8006 return got_error_set_errno(ENOENT, relpath);
8008 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8009 if (ie == NULL)
8010 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8012 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8013 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8014 relpath) == -1)
8015 return got_error_from_errno("asprintf");
8017 if (got_fileindex_entry_has_commit(ie)) {
8018 memcpy(base_commit_id.sha1, ie->commit_sha1,
8019 SHA1_DIGEST_LENGTH);
8020 base_commit_idp = &base_commit_id;
8023 if (status == GOT_STATUS_CONFLICT) {
8024 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8025 goto done;
8026 } else if (status != GOT_STATUS_ADD &&
8027 status != GOT_STATUS_MODIFY &&
8028 status != GOT_STATUS_DELETE) {
8029 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8030 goto done;
8033 a->have_changes = 1;
8035 p = in_repo_path;
8036 while (p[0] == '/')
8037 p++;
8038 err = check_out_of_date(p, status, staged_status,
8039 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8040 GOT_ERR_STAGE_OUT_OF_DATE);
8041 done:
8042 free(in_repo_path);
8043 return err;
8046 struct stage_path_arg {
8047 struct got_worktree *worktree;
8048 struct got_fileindex *fileindex;
8049 struct got_repository *repo;
8050 got_worktree_status_cb status_cb;
8051 void *status_arg;
8052 got_worktree_patch_cb patch_cb;
8053 void *patch_arg;
8054 int staged_something;
8055 int allow_bad_symlinks;
8058 static const struct got_error *
8059 stage_path(void *arg, unsigned char status,
8060 unsigned char staged_status, const char *relpath,
8061 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8062 struct got_object_id *commit_id, int dirfd, const char *de_name)
8064 struct stage_path_arg *a = arg;
8065 const struct got_error *err = NULL;
8066 struct got_fileindex_entry *ie;
8067 char *ondisk_path = NULL, *path_content = NULL;
8068 uint32_t stage;
8069 struct got_object_id *new_staged_blob_id = NULL;
8070 struct stat sb;
8072 if (status == GOT_STATUS_UNVERSIONED)
8073 return NULL;
8075 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8076 if (ie == NULL)
8077 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8079 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8080 relpath)== -1)
8081 return got_error_from_errno("asprintf");
8083 switch (status) {
8084 case GOT_STATUS_ADD:
8085 case GOT_STATUS_MODIFY:
8086 /* XXX could sb.st_mode be passed in by our caller? */
8087 if (lstat(ondisk_path, &sb) == -1) {
8088 err = got_error_from_errno2("lstat", ondisk_path);
8089 break;
8091 if (a->patch_cb) {
8092 if (status == GOT_STATUS_ADD) {
8093 int choice = GOT_PATCH_CHOICE_NONE;
8094 err = (*a->patch_cb)(&choice, a->patch_arg,
8095 status, ie->path, NULL, 1, 1);
8096 if (err)
8097 break;
8098 if (choice != GOT_PATCH_CHOICE_YES)
8099 break;
8100 } else {
8101 err = create_patched_content(&path_content, 0,
8102 staged_blob_id ? staged_blob_id : blob_id,
8103 ondisk_path, dirfd, de_name, ie->path,
8104 a->repo, a->patch_cb, a->patch_arg);
8105 if (err || path_content == NULL)
8106 break;
8109 err = got_object_blob_create(&new_staged_blob_id,
8110 path_content ? path_content : ondisk_path, a->repo);
8111 if (err)
8112 break;
8113 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8114 SHA1_DIGEST_LENGTH);
8115 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8116 stage = GOT_FILEIDX_STAGE_ADD;
8117 else
8118 stage = GOT_FILEIDX_STAGE_MODIFY;
8119 got_fileindex_entry_stage_set(ie, stage);
8120 if (S_ISLNK(sb.st_mode)) {
8121 int is_bad_symlink = 0;
8122 if (!a->allow_bad_symlinks) {
8123 char target_path[PATH_MAX];
8124 ssize_t target_len;
8125 target_len = readlink(ondisk_path, target_path,
8126 sizeof(target_path));
8127 if (target_len == -1) {
8128 err = got_error_from_errno2("readlink",
8129 ondisk_path);
8130 break;
8132 err = is_bad_symlink_target(&is_bad_symlink,
8133 target_path, target_len, ondisk_path,
8134 a->worktree->root_path);
8135 if (err)
8136 break;
8137 if (is_bad_symlink) {
8138 err = got_error_path(ondisk_path,
8139 GOT_ERR_BAD_SYMLINK);
8140 break;
8143 if (is_bad_symlink)
8144 got_fileindex_entry_staged_filetype_set(ie,
8145 GOT_FILEIDX_MODE_BAD_SYMLINK);
8146 else
8147 got_fileindex_entry_staged_filetype_set(ie,
8148 GOT_FILEIDX_MODE_SYMLINK);
8149 } else {
8150 got_fileindex_entry_staged_filetype_set(ie,
8151 GOT_FILEIDX_MODE_REGULAR_FILE);
8153 a->staged_something = 1;
8154 if (a->status_cb == NULL)
8155 break;
8156 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8157 get_staged_status(ie), relpath, blob_id,
8158 new_staged_blob_id, NULL, dirfd, de_name);
8159 if (err)
8160 break;
8162 * When staging the reverse of the staged diff,
8163 * implicitly unstage the file.
8165 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8166 sizeof(ie->blob_sha1)) == 0) {
8167 got_fileindex_entry_stage_set(ie,
8168 GOT_FILEIDX_STAGE_NONE);
8170 break;
8171 case GOT_STATUS_DELETE:
8172 if (staged_status == GOT_STATUS_DELETE)
8173 break;
8174 if (a->patch_cb) {
8175 int choice = GOT_PATCH_CHOICE_NONE;
8176 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8177 ie->path, NULL, 1, 1);
8178 if (err)
8179 break;
8180 if (choice == GOT_PATCH_CHOICE_NO)
8181 break;
8182 if (choice != GOT_PATCH_CHOICE_YES) {
8183 err = got_error(GOT_ERR_PATCH_CHOICE);
8184 break;
8187 stage = GOT_FILEIDX_STAGE_DELETE;
8188 got_fileindex_entry_stage_set(ie, stage);
8189 a->staged_something = 1;
8190 if (a->status_cb == NULL)
8191 break;
8192 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8193 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8194 de_name);
8195 break;
8196 case GOT_STATUS_NO_CHANGE:
8197 break;
8198 case GOT_STATUS_CONFLICT:
8199 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8200 break;
8201 case GOT_STATUS_NONEXISTENT:
8202 err = got_error_set_errno(ENOENT, relpath);
8203 break;
8204 default:
8205 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8206 break;
8209 if (path_content && unlink(path_content) == -1 && err == NULL)
8210 err = got_error_from_errno2("unlink", path_content);
8211 free(path_content);
8212 free(ondisk_path);
8213 free(new_staged_blob_id);
8214 return err;
8217 const struct got_error *
8218 got_worktree_stage(struct got_worktree *worktree,
8219 struct got_pathlist_head *paths,
8220 got_worktree_status_cb status_cb, void *status_arg,
8221 got_worktree_patch_cb patch_cb, void *patch_arg,
8222 int allow_bad_symlinks, struct got_repository *repo)
8224 const struct got_error *err = NULL, *sync_err, *unlockerr;
8225 struct got_pathlist_entry *pe;
8226 struct got_fileindex *fileindex = NULL;
8227 char *fileindex_path = NULL;
8228 struct got_reference *head_ref = NULL;
8229 struct got_object_id *head_commit_id = NULL;
8230 struct check_stage_ok_arg oka;
8231 struct stage_path_arg spa;
8233 err = lock_worktree(worktree, LOCK_EX);
8234 if (err)
8235 return err;
8237 err = got_ref_open(&head_ref, repo,
8238 got_worktree_get_head_ref_name(worktree), 0);
8239 if (err)
8240 goto done;
8241 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8242 if (err)
8243 goto done;
8244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8245 if (err)
8246 goto done;
8248 /* Check pre-conditions before staging anything. */
8249 oka.head_commit_id = head_commit_id;
8250 oka.worktree = worktree;
8251 oka.fileindex = fileindex;
8252 oka.repo = repo;
8253 oka.have_changes = 0;
8254 TAILQ_FOREACH(pe, paths, entry) {
8255 err = worktree_status(worktree, pe->path, fileindex, repo,
8256 check_stage_ok, &oka, NULL, NULL, 1, 0);
8257 if (err)
8258 goto done;
8260 if (!oka.have_changes) {
8261 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8262 goto done;
8265 spa.worktree = worktree;
8266 spa.fileindex = fileindex;
8267 spa.repo = repo;
8268 spa.patch_cb = patch_cb;
8269 spa.patch_arg = patch_arg;
8270 spa.status_cb = status_cb;
8271 spa.status_arg = status_arg;
8272 spa.staged_something = 0;
8273 spa.allow_bad_symlinks = allow_bad_symlinks;
8274 TAILQ_FOREACH(pe, paths, entry) {
8275 err = worktree_status(worktree, pe->path, fileindex, repo,
8276 stage_path, &spa, NULL, NULL, 1, 0);
8277 if (err)
8278 goto done;
8280 if (!spa.staged_something) {
8281 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8282 goto done;
8285 sync_err = sync_fileindex(fileindex, fileindex_path);
8286 if (sync_err && err == NULL)
8287 err = sync_err;
8288 done:
8289 if (head_ref)
8290 got_ref_close(head_ref);
8291 free(head_commit_id);
8292 free(fileindex_path);
8293 if (fileindex)
8294 got_fileindex_free(fileindex);
8295 unlockerr = lock_worktree(worktree, LOCK_SH);
8296 if (unlockerr && err == NULL)
8297 err = unlockerr;
8298 return err;
8301 struct unstage_path_arg {
8302 struct got_worktree *worktree;
8303 struct got_fileindex *fileindex;
8304 struct got_repository *repo;
8305 got_worktree_checkout_cb progress_cb;
8306 void *progress_arg;
8307 got_worktree_patch_cb patch_cb;
8308 void *patch_arg;
8311 static const struct got_error *
8312 create_unstaged_content(char **path_unstaged_content,
8313 char **path_new_staged_content, struct got_object_id *blob_id,
8314 struct got_object_id *staged_blob_id, const char *relpath,
8315 struct got_repository *repo,
8316 got_worktree_patch_cb patch_cb, void *patch_arg)
8318 const struct got_error *err, *free_err;
8319 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8320 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8321 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8322 struct got_diffreg_result *diffreg_result = NULL;
8323 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8324 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8325 int fd1 = -1, fd2 = -1;
8327 *path_unstaged_content = NULL;
8328 *path_new_staged_content = NULL;
8330 err = got_object_id_str(&label1, blob_id);
8331 if (err)
8332 return err;
8334 fd1 = got_opentempfd();
8335 if (fd1 == -1) {
8336 err = got_error_from_errno("got_opentempfd");
8337 goto done;
8339 fd2 = got_opentempfd();
8340 if (fd2 == -1) {
8341 err = got_error_from_errno("got_opentempfd");
8342 goto done;
8345 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8346 if (err)
8347 goto done;
8349 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8350 if (err)
8351 goto done;
8353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8354 if (err)
8355 goto done;
8357 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8358 fd2);
8359 if (err)
8360 goto done;
8362 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8363 if (err)
8364 goto done;
8366 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8367 if (err)
8368 goto done;
8370 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8371 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8372 if (err)
8373 goto done;
8375 err = got_opentemp_named(path_unstaged_content, &outfile,
8376 "got-unstaged-content");
8377 if (err)
8378 goto done;
8379 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8380 "got-new-staged-content");
8381 if (err)
8382 goto done;
8384 if (fseek(f1, 0L, SEEK_SET) == -1) {
8385 err = got_ferror(f1, GOT_ERR_IO);
8386 goto done;
8388 if (fseek(f2, 0L, SEEK_SET) == -1) {
8389 err = got_ferror(f2, GOT_ERR_IO);
8390 goto done;
8392 /* Count the number of actual changes in the diff result. */
8393 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8394 struct diff_chunk_context cc = {};
8395 diff_chunk_context_load_change(&cc, &nchunks_used,
8396 diffreg_result->result, n, 0);
8397 nchanges++;
8399 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8400 int choice;
8401 err = apply_or_reject_change(&choice, &nchunks_used,
8402 diffreg_result->result, n, relpath, f1, f2,
8403 &line_cur1, &line_cur2,
8404 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8405 if (err)
8406 goto done;
8407 if (choice == GOT_PATCH_CHOICE_YES)
8408 have_content = 1;
8409 else
8410 have_rejected_content = 1;
8411 if (choice == GOT_PATCH_CHOICE_QUIT)
8412 break;
8414 if (have_content || have_rejected_content)
8415 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8416 outfile, rejectfile);
8417 done:
8418 free(label1);
8419 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8420 err = got_error_from_errno("close");
8421 if (blob)
8422 got_object_blob_close(blob);
8423 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8424 err = got_error_from_errno("close");
8425 if (staged_blob)
8426 got_object_blob_close(staged_blob);
8427 free_err = got_diffreg_result_free(diffreg_result);
8428 if (free_err && err == NULL)
8429 err = free_err;
8430 if (f1 && fclose(f1) == EOF && err == NULL)
8431 err = got_error_from_errno2("fclose", path1);
8432 if (f2 && fclose(f2) == EOF && err == NULL)
8433 err = got_error_from_errno2("fclose", path2);
8434 if (outfile && fclose(outfile) == EOF && err == NULL)
8435 err = got_error_from_errno2("fclose", *path_unstaged_content);
8436 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8437 err = got_error_from_errno2("fclose", *path_new_staged_content);
8438 if (path1 && unlink(path1) == -1 && err == NULL)
8439 err = got_error_from_errno2("unlink", path1);
8440 if (path2 && unlink(path2) == -1 && err == NULL)
8441 err = got_error_from_errno2("unlink", path2);
8442 if (err || !have_content) {
8443 if (*path_unstaged_content &&
8444 unlink(*path_unstaged_content) == -1 && err == NULL)
8445 err = got_error_from_errno2("unlink",
8446 *path_unstaged_content);
8447 free(*path_unstaged_content);
8448 *path_unstaged_content = NULL;
8450 if (err || !have_content || !have_rejected_content) {
8451 if (*path_new_staged_content &&
8452 unlink(*path_new_staged_content) == -1 && err == NULL)
8453 err = got_error_from_errno2("unlink",
8454 *path_new_staged_content);
8455 free(*path_new_staged_content);
8456 *path_new_staged_content = NULL;
8458 free(path1);
8459 free(path2);
8460 return err;
8463 static const struct got_error *
8464 unstage_hunks(struct got_object_id *staged_blob_id,
8465 struct got_blob_object *blob_base,
8466 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8467 const char *ondisk_path, const char *label_orig,
8468 struct got_worktree *worktree, struct got_repository *repo,
8469 got_worktree_patch_cb patch_cb, void *patch_arg,
8470 got_worktree_checkout_cb progress_cb, void *progress_arg)
8472 const struct got_error *err = NULL;
8473 char *path_unstaged_content = NULL;
8474 char *path_new_staged_content = NULL;
8475 char *parent = NULL, *base_path = NULL;
8476 char *blob_base_path = NULL;
8477 struct got_object_id *new_staged_blob_id = NULL;
8478 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8479 struct stat sb;
8481 err = create_unstaged_content(&path_unstaged_content,
8482 &path_new_staged_content, blob_id, staged_blob_id,
8483 ie->path, repo, patch_cb, patch_arg);
8484 if (err)
8485 return err;
8487 if (path_unstaged_content == NULL)
8488 return NULL;
8490 if (path_new_staged_content) {
8491 err = got_object_blob_create(&new_staged_blob_id,
8492 path_new_staged_content, repo);
8493 if (err)
8494 goto done;
8497 f = fopen(path_unstaged_content, "re");
8498 if (f == NULL) {
8499 err = got_error_from_errno2("fopen",
8500 path_unstaged_content);
8501 goto done;
8503 if (fstat(fileno(f), &sb) == -1) {
8504 err = got_error_from_errno2("fstat", path_unstaged_content);
8505 goto done;
8507 if (got_fileindex_entry_staged_filetype_get(ie) ==
8508 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8509 char link_target[PATH_MAX];
8510 size_t r;
8511 r = fread(link_target, 1, sizeof(link_target), f);
8512 if (r == 0 && ferror(f)) {
8513 err = got_error_from_errno("fread");
8514 goto done;
8516 if (r >= sizeof(link_target)) { /* should not happen */
8517 err = got_error(GOT_ERR_NO_SPACE);
8518 goto done;
8520 link_target[r] = '\0';
8521 err = merge_symlink(worktree, blob_base,
8522 ondisk_path, ie->path, label_orig, link_target,
8523 worktree->base_commit_id, repo, progress_cb,
8524 progress_arg);
8525 } else {
8526 int local_changes_subsumed;
8528 err = got_path_dirname(&parent, ondisk_path);
8529 if (err)
8530 return err;
8532 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8533 parent) == -1) {
8534 err = got_error_from_errno("asprintf");
8535 base_path = NULL;
8536 goto done;
8539 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8540 if (err)
8541 goto done;
8542 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8543 blob_base);
8544 if (err)
8545 goto done;
8548 * In order the run a 3-way merge with a symlink we copy the symlink's
8549 * target path into a temporary file and use that file with diff3.
8551 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8552 err = dump_symlink_target_path_to_file(&f_deriv2,
8553 ondisk_path);
8554 if (err)
8555 goto done;
8556 } else {
8557 int fd;
8558 fd = open(ondisk_path,
8559 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8560 if (fd == -1) {
8561 err = got_error_from_errno2("open", ondisk_path);
8562 goto done;
8564 f_deriv2 = fdopen(fd, "r");
8565 if (f_deriv2 == NULL) {
8566 err = got_error_from_errno2("fdopen", ondisk_path);
8567 close(fd);
8568 goto done;
8572 err = merge_file(&local_changes_subsumed, worktree,
8573 f_base, f, f_deriv2, ondisk_path, ie->path,
8574 got_fileindex_perms_to_st(ie),
8575 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8576 repo, progress_cb, progress_arg);
8578 if (err)
8579 goto done;
8581 if (new_staged_blob_id) {
8582 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8583 SHA1_DIGEST_LENGTH);
8584 } else {
8585 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8586 got_fileindex_entry_staged_filetype_set(ie, 0);
8588 done:
8589 free(new_staged_blob_id);
8590 if (path_unstaged_content &&
8591 unlink(path_unstaged_content) == -1 && err == NULL)
8592 err = got_error_from_errno2("unlink", path_unstaged_content);
8593 if (path_new_staged_content &&
8594 unlink(path_new_staged_content) == -1 && err == NULL)
8595 err = got_error_from_errno2("unlink", path_new_staged_content);
8596 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8597 err = got_error_from_errno2("unlink", blob_base_path);
8598 if (f_base && fclose(f_base) == EOF && err == NULL)
8599 err = got_error_from_errno2("fclose", path_unstaged_content);
8600 if (f && fclose(f) == EOF && err == NULL)
8601 err = got_error_from_errno2("fclose", path_unstaged_content);
8602 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8603 err = got_error_from_errno2("fclose", ondisk_path);
8604 free(path_unstaged_content);
8605 free(path_new_staged_content);
8606 free(blob_base_path);
8607 free(parent);
8608 free(base_path);
8609 return err;
8612 static const struct got_error *
8613 unstage_path(void *arg, unsigned char status,
8614 unsigned char staged_status, const char *relpath,
8615 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8616 struct got_object_id *commit_id, int dirfd, const char *de_name)
8618 const struct got_error *err = NULL;
8619 struct unstage_path_arg *a = arg;
8620 struct got_fileindex_entry *ie;
8621 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8622 char *ondisk_path = NULL;
8623 char *id_str = NULL, *label_orig = NULL;
8624 int local_changes_subsumed;
8625 struct stat sb;
8626 int fd1 = -1, fd2 = -1;
8628 if (staged_status != GOT_STATUS_ADD &&
8629 staged_status != GOT_STATUS_MODIFY &&
8630 staged_status != GOT_STATUS_DELETE)
8631 return NULL;
8633 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8634 if (ie == NULL)
8635 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8637 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8638 == -1)
8639 return got_error_from_errno("asprintf");
8641 err = got_object_id_str(&id_str,
8642 commit_id ? commit_id : a->worktree->base_commit_id);
8643 if (err)
8644 goto done;
8645 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8646 id_str) == -1) {
8647 err = got_error_from_errno("asprintf");
8648 goto done;
8651 fd1 = got_opentempfd();
8652 if (fd1 == -1) {
8653 err = got_error_from_errno("got_opentempfd");
8654 goto done;
8656 fd2 = got_opentempfd();
8657 if (fd2 == -1) {
8658 err = got_error_from_errno("got_opentempfd");
8659 goto done;
8662 switch (staged_status) {
8663 case GOT_STATUS_MODIFY:
8664 err = got_object_open_as_blob(&blob_base, a->repo,
8665 blob_id, 8192, fd1);
8666 if (err)
8667 break;
8668 /* fall through */
8669 case GOT_STATUS_ADD:
8670 if (a->patch_cb) {
8671 if (staged_status == GOT_STATUS_ADD) {
8672 int choice = GOT_PATCH_CHOICE_NONE;
8673 err = (*a->patch_cb)(&choice, a->patch_arg,
8674 staged_status, ie->path, NULL, 1, 1);
8675 if (err)
8676 break;
8677 if (choice != GOT_PATCH_CHOICE_YES)
8678 break;
8679 } else {
8680 err = unstage_hunks(staged_blob_id,
8681 blob_base, blob_id, ie, ondisk_path,
8682 label_orig, a->worktree, a->repo,
8683 a->patch_cb, a->patch_arg,
8684 a->progress_cb, a->progress_arg);
8685 break; /* Done with this file. */
8688 err = got_object_open_as_blob(&blob_staged, a->repo,
8689 staged_blob_id, 8192, fd2);
8690 if (err)
8691 break;
8692 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8693 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8694 case GOT_FILEIDX_MODE_REGULAR_FILE:
8695 err = merge_blob(&local_changes_subsumed, a->worktree,
8696 blob_base, ondisk_path, relpath,
8697 got_fileindex_perms_to_st(ie), label_orig,
8698 blob_staged, commit_id ? commit_id :
8699 a->worktree->base_commit_id, a->repo,
8700 a->progress_cb, a->progress_arg);
8701 break;
8702 case GOT_FILEIDX_MODE_SYMLINK:
8703 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8704 char *staged_target;
8705 err = got_object_blob_read_to_str(
8706 &staged_target, blob_staged);
8707 if (err)
8708 goto done;
8709 err = merge_symlink(a->worktree, blob_base,
8710 ondisk_path, relpath, label_orig,
8711 staged_target, commit_id ? commit_id :
8712 a->worktree->base_commit_id,
8713 a->repo, a->progress_cb, a->progress_arg);
8714 free(staged_target);
8715 } else {
8716 err = merge_blob(&local_changes_subsumed,
8717 a->worktree, blob_base, ondisk_path,
8718 relpath, got_fileindex_perms_to_st(ie),
8719 label_orig, blob_staged,
8720 commit_id ? commit_id :
8721 a->worktree->base_commit_id, a->repo,
8722 a->progress_cb, a->progress_arg);
8724 break;
8725 default:
8726 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8727 break;
8729 if (err == NULL) {
8730 got_fileindex_entry_stage_set(ie,
8731 GOT_FILEIDX_STAGE_NONE);
8732 got_fileindex_entry_staged_filetype_set(ie, 0);
8734 break;
8735 case GOT_STATUS_DELETE:
8736 if (a->patch_cb) {
8737 int choice = GOT_PATCH_CHOICE_NONE;
8738 err = (*a->patch_cb)(&choice, a->patch_arg,
8739 staged_status, ie->path, NULL, 1, 1);
8740 if (err)
8741 break;
8742 if (choice == GOT_PATCH_CHOICE_NO)
8743 break;
8744 if (choice != GOT_PATCH_CHOICE_YES) {
8745 err = got_error(GOT_ERR_PATCH_CHOICE);
8746 break;
8749 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8750 got_fileindex_entry_staged_filetype_set(ie, 0);
8751 err = get_file_status(&status, &sb, ie, ondisk_path,
8752 dirfd, de_name, a->repo);
8753 if (err)
8754 break;
8755 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8756 break;
8758 done:
8759 free(ondisk_path);
8760 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8761 err = got_error_from_errno("close");
8762 if (blob_base)
8763 got_object_blob_close(blob_base);
8764 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8765 err = got_error_from_errno("close");
8766 if (blob_staged)
8767 got_object_blob_close(blob_staged);
8768 free(id_str);
8769 free(label_orig);
8770 return err;
8773 const struct got_error *
8774 got_worktree_unstage(struct got_worktree *worktree,
8775 struct got_pathlist_head *paths,
8776 got_worktree_checkout_cb progress_cb, void *progress_arg,
8777 got_worktree_patch_cb patch_cb, void *patch_arg,
8778 struct got_repository *repo)
8780 const struct got_error *err = NULL, *sync_err, *unlockerr;
8781 struct got_pathlist_entry *pe;
8782 struct got_fileindex *fileindex = NULL;
8783 char *fileindex_path = NULL;
8784 struct unstage_path_arg upa;
8786 err = lock_worktree(worktree, LOCK_EX);
8787 if (err)
8788 return err;
8790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8791 if (err)
8792 goto done;
8794 upa.worktree = worktree;
8795 upa.fileindex = fileindex;
8796 upa.repo = repo;
8797 upa.progress_cb = progress_cb;
8798 upa.progress_arg = progress_arg;
8799 upa.patch_cb = patch_cb;
8800 upa.patch_arg = patch_arg;
8801 TAILQ_FOREACH(pe, paths, entry) {
8802 err = worktree_status(worktree, pe->path, fileindex, repo,
8803 unstage_path, &upa, NULL, NULL, 1, 0);
8804 if (err)
8805 goto done;
8808 sync_err = sync_fileindex(fileindex, fileindex_path);
8809 if (sync_err && err == NULL)
8810 err = sync_err;
8811 done:
8812 free(fileindex_path);
8813 if (fileindex)
8814 got_fileindex_free(fileindex);
8815 unlockerr = lock_worktree(worktree, LOCK_SH);
8816 if (unlockerr && err == NULL)
8817 err = unlockerr;
8818 return err;
8821 struct report_file_info_arg {
8822 struct got_worktree *worktree;
8823 got_worktree_path_info_cb info_cb;
8824 void *info_arg;
8825 struct got_pathlist_head *paths;
8826 got_cancel_cb cancel_cb;
8827 void *cancel_arg;
8830 static const struct got_error *
8831 report_file_info(void *arg, struct got_fileindex_entry *ie)
8833 struct report_file_info_arg *a = arg;
8834 struct got_pathlist_entry *pe;
8835 struct got_object_id blob_id, staged_blob_id, commit_id;
8836 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8837 struct got_object_id *commit_idp = NULL;
8838 int stage;
8840 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8841 return got_error(GOT_ERR_CANCELLED);
8843 TAILQ_FOREACH(pe, a->paths, entry) {
8844 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8845 got_path_is_child(ie->path, pe->path, pe->path_len))
8846 break;
8848 if (pe == NULL) /* not found */
8849 return NULL;
8851 if (got_fileindex_entry_has_blob(ie)) {
8852 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8853 blob_idp = &blob_id;
8855 stage = got_fileindex_entry_stage_get(ie);
8856 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8857 stage == GOT_FILEIDX_STAGE_ADD) {
8858 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8859 SHA1_DIGEST_LENGTH);
8860 staged_blob_idp = &staged_blob_id;
8863 if (got_fileindex_entry_has_commit(ie)) {
8864 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8865 commit_idp = &commit_id;
8868 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8869 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8872 const struct got_error *
8873 got_worktree_path_info(struct got_worktree *worktree,
8874 struct got_pathlist_head *paths,
8875 got_worktree_path_info_cb info_cb, void *info_arg,
8876 got_cancel_cb cancel_cb, void *cancel_arg)
8879 const struct got_error *err = NULL, *unlockerr;
8880 struct got_fileindex *fileindex = NULL;
8881 char *fileindex_path = NULL;
8882 struct report_file_info_arg arg;
8884 err = lock_worktree(worktree, LOCK_SH);
8885 if (err)
8886 return err;
8888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8889 if (err)
8890 goto done;
8892 arg.worktree = worktree;
8893 arg.info_cb = info_cb;
8894 arg.info_arg = info_arg;
8895 arg.paths = paths;
8896 arg.cancel_cb = cancel_cb;
8897 arg.cancel_arg = cancel_arg;
8898 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8899 &arg);
8900 done:
8901 free(fileindex_path);
8902 if (fileindex)
8903 got_fileindex_free(fileindex);
8904 unlockerr = lock_worktree(worktree, LOCK_UN);
8905 if (unlockerr && err == NULL)
8906 err = unlockerr;
8907 return err;
8910 static const struct got_error *
8911 patch_check_path(const char *p, char **path, unsigned char *status,
8912 unsigned char *staged_status, struct got_fileindex *fileindex,
8913 struct got_worktree *worktree, struct got_repository *repo)
8915 const struct got_error *err;
8916 struct got_fileindex_entry *ie;
8917 struct stat sb;
8918 char *ondisk_path = NULL;
8920 err = got_worktree_resolve_path(path, worktree, p);
8921 if (err)
8922 return err;
8924 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8925 *path[0] ? "/" : "", *path) == -1)
8926 return got_error_from_errno("asprintf");
8928 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8929 if (ie) {
8930 *staged_status = get_staged_status(ie);
8931 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8932 repo);
8933 if (err)
8934 goto done;
8935 } else {
8936 *staged_status = GOT_STATUS_NO_CHANGE;
8937 *status = GOT_STATUS_UNVERSIONED;
8938 if (lstat(ondisk_path, &sb) == -1) {
8939 if (errno != ENOENT) {
8940 err = got_error_from_errno2("lstat",
8941 ondisk_path);
8942 goto done;
8944 *status = GOT_STATUS_NONEXISTENT;
8948 done:
8949 free(ondisk_path);
8950 return err;
8953 static const struct got_error *
8954 patch_can_rm(const char *path, unsigned char status,
8955 unsigned char staged_status)
8957 if (status == GOT_STATUS_NONEXISTENT)
8958 return got_error_set_errno(ENOENT, path);
8959 if (status != GOT_STATUS_NO_CHANGE &&
8960 status != GOT_STATUS_ADD &&
8961 status != GOT_STATUS_MODIFY &&
8962 status != GOT_STATUS_MODE_CHANGE)
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 static const struct got_error *
8970 patch_can_add(const char *path, unsigned char status)
8972 if (status != GOT_STATUS_NONEXISTENT)
8973 return got_error_path(path, GOT_ERR_FILE_STATUS);
8974 return NULL;
8977 static const struct got_error *
8978 patch_can_edit(const char *path, unsigned char status,
8979 unsigned char staged_status)
8981 if (status == GOT_STATUS_NONEXISTENT)
8982 return got_error_set_errno(ENOENT, path);
8983 if (status != GOT_STATUS_NO_CHANGE &&
8984 status != GOT_STATUS_ADD &&
8985 status != GOT_STATUS_MODIFY)
8986 return got_error_path(path, GOT_ERR_FILE_STATUS);
8987 if (staged_status == GOT_STATUS_DELETE)
8988 return got_error_path(path, GOT_ERR_FILE_STATUS);
8989 return NULL;
8992 const struct got_error *
8993 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8994 char **fileindex_path, struct got_worktree *worktree)
8996 return open_fileindex(fileindex, fileindex_path, worktree);
8999 const struct got_error *
9000 got_worktree_patch_check_path(const char *old, const char *new,
9001 char **oldpath, char **newpath, struct got_worktree *worktree,
9002 struct got_repository *repo, struct got_fileindex *fileindex)
9004 const struct got_error *err = NULL;
9005 int file_renamed = 0;
9006 unsigned char status_old, staged_status_old;
9007 unsigned char status_new, staged_status_new;
9009 *oldpath = NULL;
9010 *newpath = NULL;
9012 err = patch_check_path(old != NULL ? old : new, oldpath,
9013 &status_old, &staged_status_old, fileindex, worktree, repo);
9014 if (err)
9015 goto done;
9017 err = patch_check_path(new != NULL ? new : old, newpath,
9018 &status_new, &staged_status_new, fileindex, worktree, repo);
9019 if (err)
9020 goto done;
9022 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9023 file_renamed = 1;
9025 if (old != NULL && new == NULL)
9026 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9027 else if (file_renamed) {
9028 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9029 if (err == NULL)
9030 err = patch_can_add(*newpath, status_new);
9031 } else if (old == NULL)
9032 err = patch_can_add(*newpath, status_new);
9033 else
9034 err = patch_can_edit(*newpath, status_new, staged_status_new);
9036 done:
9037 if (err) {
9038 free(*oldpath);
9039 *oldpath = NULL;
9040 free(*newpath);
9041 *newpath = NULL;
9043 return err;
9046 const struct got_error *
9047 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9048 struct got_worktree *worktree, struct got_fileindex *fileindex,
9049 got_worktree_checkout_cb progress_cb, void *progress_arg)
9051 struct schedule_addition_args saa;
9053 memset(&saa, 0, sizeof(saa));
9054 saa.worktree = worktree;
9055 saa.fileindex = fileindex;
9056 saa.progress_cb = progress_cb;
9057 saa.progress_arg = progress_arg;
9058 saa.repo = repo;
9060 return worktree_status(worktree, path, fileindex, repo,
9061 schedule_addition, &saa, NULL, NULL, 1, 0);
9064 const struct got_error *
9065 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9066 struct got_worktree *worktree, struct got_fileindex *fileindex,
9067 got_worktree_delete_cb progress_cb, void *progress_arg)
9069 struct schedule_deletion_args sda;
9071 memset(&sda, 0, sizeof(sda));
9072 sda.worktree = worktree;
9073 sda.fileindex = fileindex;
9074 sda.progress_cb = progress_cb;
9075 sda.progress_arg = progress_arg;
9076 sda.repo = repo;
9077 sda.delete_local_mods = 0;
9078 sda.keep_on_disk = 0;
9079 sda.ignore_missing_paths = 0;
9080 sda.status_codes = NULL;
9082 return worktree_status(worktree, path, fileindex, repo,
9083 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9086 const struct got_error *
9087 got_worktree_patch_complete(struct got_fileindex *fileindex,
9088 const char *fileindex_path)
9090 const struct got_error *err = NULL;
9092 err = sync_fileindex(fileindex, fileindex_path);
9093 got_fileindex_free(fileindex);
9095 return err;