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>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
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), apply_umask(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), apply_umask(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,
994 base_path, "");
995 if (err)
996 goto done;
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 blob_orig);
999 if (err)
1000 goto done;
1001 free(base_path);
1002 } else {
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1011 goto done;
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 base_path = NULL;
1018 goto done;
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 if (err)
1023 goto done;
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 blob_deriv);
1026 if (err)
1027 goto done;
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 if (err)
1031 goto done;
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 goto done;
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 if (err)
1045 goto done;
1046 } else {
1047 int fd;
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 if (fd == -1) {
1050 err = got_error_from_errno2("open", ondisk_path);
1051 goto done;
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1056 close(fd);
1057 goto done;
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 done:
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 free(base_path);
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1080 free(id_str);
1081 free(label_deriv);
1082 free(parent);
1083 return err;
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1094 *new_iep = NULL;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1097 if (err)
1098 return err;
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1102 if (err)
1103 goto done;
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1106 done:
1107 if (err)
1108 got_fileindex_entry_free(new_ie);
1109 else
1110 *new_iep = new_ie;
1111 return err;
1114 static mode_t
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1119 if (executable) {
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1122 xbits |= S_IXGRP;
1123 if (st_mode & S_IROTH)
1124 xbits |= S_IXOTH;
1125 return st_mode | xbits;
1128 return st_mode;
1131 static mode_t
1132 apply_umask(mode_t mode)
1134 mode_t um;
1136 um = umask(000);
1137 umask(um);
1138 return mode & ~um;
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1159 ssize_t elen;
1160 char etarget[PATH_MAX];
1161 int fd;
1163 *did_something = 0;
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 if (fd == -1) {
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 if (elen == -1)
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1188 *did_something = 1;
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1192 return err;
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1207 return NULL;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1220 if (err)
1221 return err;
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 free(parent);
1224 return got_error_from_errno("asprintf");
1226 free(parent);
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 free(abspath);
1230 return err;
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 free(abspath);
1234 if (err)
1235 return err;
1236 } else {
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 if (err)
1239 return err;
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1245 return NULL;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1255 free(path_got);
1256 return NULL;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1281 do {
1282 err = got_object_blob_read_block(&len, blob);
1283 if (err)
1284 return err;
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1294 progress_arg);
1296 if (len > 0) {
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1299 len - hdrlen);
1300 target_len += len - hdrlen;
1301 hdrlen = 0;
1303 } while (len != 0);
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1308 if (err)
1309 return err;
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1318 return err;
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1327 return err;
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1331 if (err)
1332 return err;
1333 if (progress_cb) {
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1337 GOT_STATUS_REVERT :
1338 GOT_STATUS_UPDATE, path);
1339 } else {
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1348 char *parent;
1349 err = got_path_dirname(&parent, ondisk_path);
1350 if (err)
1351 return err;
1352 err = add_dir_on_disk(worktree, parent);
1353 free(parent);
1354 if (err)
1355 return err;
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1362 return err;
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1379 } else {
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 return err;
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1398 int fd = -1;
1399 size_t len, hdrlen;
1400 int update = 0;
1401 char *tmppath = NULL;
1402 mode_t mode;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 O_CLOEXEC, mode);
1407 if (fd == -1) {
1408 if (errno == ENOENT) {
1409 char *parent;
1410 err = got_path_dirname(&parent, path);
1411 if (err)
1412 return err;
1413 err = add_dir_on_disk(worktree, parent);
1414 free(parent);
1415 if (err)
1416 return err;
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 mode);
1420 if (fd == -1)
1421 return got_error_from_errno2("open",
1422 ondisk_path);
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1427 goto done;
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1434 goto done;
1435 } else {
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1437 ondisk_path, "");
1438 if (err)
1439 goto done;
1440 update = 1;
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1444 tmppath);
1445 goto done;
1448 } else
1449 return got_error_from_errno2("open", ondisk_path);
1452 if (progress_cb) {
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 path);
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 path);
1459 else
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 if (err)
1463 goto done;
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1467 do {
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1470 if (err)
1471 break;
1472 if (len > 0) {
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 if (outlen == -1) {
1476 err = got_error_from_errno("write");
1477 goto done;
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1480 goto done;
1482 hdrlen = 0;
1484 } while (len != 0);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1488 goto done;
1491 if (update) {
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1494 goto done;
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1498 ondisk_path);
1499 goto done;
1501 free(tmppath);
1502 tmppath = NULL;
1505 done:
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1510 free(tmppath);
1511 return err;
1514 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1515 static const struct got_error *
1516 get_modified_file_content_status(unsigned char *status, FILE *f)
1518 const struct got_error *err = NULL;
1519 const char *markers[3] = {
1520 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1521 GOT_DIFF_CONFLICT_MARKER_SEP,
1522 GOT_DIFF_CONFLICT_MARKER_END
1524 int i = 0;
1525 char *line = NULL;
1526 size_t linesize = 0;
1527 ssize_t linelen;
1529 while (*status == GOT_STATUS_MODIFY) {
1530 linelen = getline(&line, &linesize, f);
1531 if (linelen == -1) {
1532 if (feof(f))
1533 break;
1534 err = got_ferror(f, GOT_ERR_IO);
1535 break;
1538 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1539 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1540 == 0)
1541 *status = GOT_STATUS_CONFLICT;
1542 else
1543 i++;
1546 free(line);
1548 return err;
1551 static int
1552 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1554 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1555 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 static int
1559 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1561 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1562 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1563 ie->mtime_sec == sb->st_mtim.tv_sec &&
1564 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1565 ie->size == (sb->st_size & 0xffffffff) &&
1566 !xbit_differs(ie, sb->st_mode));
1569 static unsigned char
1570 get_staged_status(struct got_fileindex_entry *ie)
1572 switch (got_fileindex_entry_stage_get(ie)) {
1573 case GOT_FILEIDX_STAGE_ADD:
1574 return GOT_STATUS_ADD;
1575 case GOT_FILEIDX_STAGE_DELETE:
1576 return GOT_STATUS_DELETE;
1577 case GOT_FILEIDX_STAGE_MODIFY:
1578 return GOT_STATUS_MODIFY;
1579 default:
1580 return GOT_STATUS_NO_CHANGE;
1584 static const struct got_error *
1585 get_symlink_modification_status(unsigned char *status,
1586 struct got_fileindex_entry *ie, const char *abspath,
1587 int dirfd, const char *de_name, struct got_blob_object *blob)
1589 const struct got_error *err = NULL;
1590 char target_path[PATH_MAX];
1591 char etarget[PATH_MAX];
1592 ssize_t elen;
1593 size_t len, target_len = 0;
1594 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1595 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1597 *status = GOT_STATUS_NO_CHANGE;
1599 /* Blob object content specifies the target path of the link. */
1600 do {
1601 err = got_object_blob_read_block(&len, blob);
1602 if (err)
1603 return err;
1604 if (len + target_len >= sizeof(target_path)) {
1606 * Should not happen. The blob contents were OK
1607 * when this symlink was installed.
1609 return got_error(GOT_ERR_NO_SPACE);
1611 if (len > 0) {
1612 /* Skip blob object header first time around. */
1613 memcpy(target_path + target_len, buf + hdrlen,
1614 len - hdrlen);
1615 target_len += len - hdrlen;
1616 hdrlen = 0;
1618 } while (len != 0);
1619 target_path[target_len] = '\0';
1621 if (dirfd != -1) {
1622 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 if (elen == -1)
1624 return got_error_from_errno2("readlinkat", abspath);
1625 } else {
1626 elen = readlink(abspath, etarget, sizeof(etarget));
1627 if (elen == -1)
1628 return got_error_from_errno2("readlink", abspath);
1631 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1632 *status = GOT_STATUS_MODIFY;
1634 return NULL;
1637 static const struct got_error *
1638 get_file_status(unsigned char *status, struct stat *sb,
1639 struct got_fileindex_entry *ie, const char *abspath,
1640 int dirfd, const char *de_name, struct got_repository *repo)
1642 const struct got_error *err = NULL;
1643 struct got_object_id id;
1644 size_t hdrlen;
1645 int fd = -1, fd1 = -1;
1646 FILE *f = NULL;
1647 uint8_t fbuf[8192];
1648 struct got_blob_object *blob = NULL;
1649 size_t flen, blen;
1650 unsigned char staged_status;
1652 staged_status = get_staged_status(ie);
1653 *status = GOT_STATUS_NO_CHANGE;
1654 memset(sb, 0, sizeof(*sb));
1657 * Whenever the caller provides a directory descriptor and a
1658 * directory entry name for the file, use them! This prevents
1659 * race conditions if filesystem paths change beneath our feet.
1661 if (dirfd != -1) {
1662 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 if (errno == ENOENT) {
1664 if (got_fileindex_entry_has_file_on_disk(ie))
1665 *status = GOT_STATUS_MISSING;
1666 else
1667 *status = GOT_STATUS_DELETE;
1668 goto done;
1670 err = got_error_from_errno2("fstatat", abspath);
1671 goto done;
1673 } else {
1674 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 if (fd == -1 && errno != ENOENT &&
1676 !got_err_open_nofollow_on_symlink())
1677 return got_error_from_errno2("open", abspath);
1678 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 if (lstat(abspath, sb) == -1)
1680 return got_error_from_errno2("lstat", abspath);
1681 } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 if (errno == ENOENT) {
1683 if (got_fileindex_entry_has_file_on_disk(ie))
1684 *status = GOT_STATUS_MISSING;
1685 else
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1689 err = got_error_from_errno2("fstat", abspath);
1690 goto done;
1694 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 *status = GOT_STATUS_OBSTRUCTED;
1696 goto done;
1699 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 *status = GOT_STATUS_DELETE;
1701 goto done;
1702 } else if (!got_fileindex_entry_has_blob(ie) &&
1703 staged_status != GOT_STATUS_ADD) {
1704 *status = GOT_STATUS_ADD;
1705 goto done;
1708 if (!stat_info_differs(ie, sb))
1709 goto done;
1711 if (S_ISLNK(sb->st_mode) &&
1712 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 *status = GOT_STATUS_MODIFY;
1714 goto done;
1717 if (staged_status == GOT_STATUS_MODIFY ||
1718 staged_status == GOT_STATUS_ADD)
1719 got_fileindex_entry_get_staged_blob_id(&id, ie);
1720 else
1721 got_fileindex_entry_get_blob_id(&id, ie);
1723 fd1 = got_opentempfd();
1724 if (fd1 == -1) {
1725 err = got_error_from_errno("got_opentempfd");
1726 goto done;
1728 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1729 if (err)
1730 goto done;
1732 if (S_ISLNK(sb->st_mode)) {
1733 err = get_symlink_modification_status(status, ie,
1734 abspath, dirfd, de_name, blob);
1735 goto done;
1738 if (dirfd != -1) {
1739 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1) {
1741 err = got_error_from_errno2("openat", abspath);
1742 goto done;
1746 f = fdopen(fd, "r");
1747 if (f == NULL) {
1748 err = got_error_from_errno2("fdopen", abspath);
1749 goto done;
1751 fd = -1;
1752 hdrlen = got_object_blob_get_hdrlen(blob);
1753 for (;;) {
1754 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 err = got_object_blob_read_block(&blen, blob);
1756 if (err)
1757 goto done;
1758 /* Skip length of blob object header first time around. */
1759 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 if (flen == 0 && ferror(f)) {
1761 err = got_error_from_errno("fread");
1762 goto done;
1764 if (blen - hdrlen == 0) {
1765 if (flen != 0)
1766 *status = GOT_STATUS_MODIFY;
1767 break;
1768 } else if (flen == 0) {
1769 if (blen - hdrlen != 0)
1770 *status = GOT_STATUS_MODIFY;
1771 break;
1772 } else if (blen - hdrlen == flen) {
1773 /* Skip blob object header first time around. */
1774 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1778 } else {
1779 *status = GOT_STATUS_MODIFY;
1780 break;
1782 hdrlen = 0;
1785 if (*status == GOT_STATUS_MODIFY) {
1786 rewind(f);
1787 err = get_modified_file_content_status(status, f);
1788 } else if (xbit_differs(ie, sb->st_mode))
1789 *status = GOT_STATUS_MODE_CHANGE;
1790 done:
1791 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 err = got_error_from_errno("close");
1793 if (blob)
1794 got_object_blob_close(blob);
1795 if (f != NULL && fclose(f) == EOF && err == NULL)
1796 err = got_error_from_errno2("fclose", abspath);
1797 if (fd != -1 && close(fd) == -1 && err == NULL)
1798 err = got_error_from_errno2("close", abspath);
1799 return err;
1803 * Update timestamps in the file index if a file is unmodified and
1804 * we had to run a full content comparison to find out.
1806 static const struct got_error *
1807 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 struct got_fileindex_entry *ie, struct stat *sb)
1810 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 return got_fileindex_entry_update(ie, wt_fd, path,
1812 ie->blob_sha1, ie->commit_sha1, 1);
1814 return NULL;
1817 static const struct got_error *
1818 update_blob(struct got_worktree *worktree,
1819 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 struct got_tree_entry *te, const char *path,
1821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1822 void *progress_arg)
1824 const struct got_error *err = NULL;
1825 struct got_blob_object *blob = NULL;
1826 char *ondisk_path = NULL;
1827 unsigned char status = GOT_STATUS_NO_CHANGE;
1828 struct stat sb;
1829 int fd1 = -1, fd2 = -1;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1834 if (ie) {
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 goto done;
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 repo);
1841 if (err)
1842 goto done;
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1845 } else {
1846 if (stat(ondisk_path, &sb) == -1) {
1847 if (errno != ENOENT) {
1848 err = got_error_from_errno2("stat",
1849 ondisk_path);
1850 goto done;
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1854 } else {
1855 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 status = GOT_STATUS_UNVERSIONED;
1857 else
1858 status = GOT_STATUS_OBSTRUCTED;
1862 if (status == GOT_STATUS_OBSTRUCTED) {
1863 if (ie)
1864 got_fileindex_entry_mark_skipped(ie);
1865 err = (*progress_cb)(progress_arg, status, path);
1866 goto done;
1868 if (status == GOT_STATUS_CONFLICT) {
1869 if (ie)
1870 got_fileindex_entry_mark_skipped(ie);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1872 path);
1873 goto done;
1876 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 (S_ISLNK(te->mode) ||
1878 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 * This is a regular file or an installed bad symlink.
1881 * If the file index indicates that this file is already
1882 * up-to-date with respect to the repository we can skip
1883 * updating contents of this file.
1885 if (got_fileindex_entry_has_commit(ie) &&
1886 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 SHA1_DIGEST_LENGTH) == 0) {
1888 /* Same commit. */
1889 err = sync_timestamps(worktree->root_fd,
1890 path, status, ie, &sb);
1891 if (err)
1892 goto done;
1893 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 path);
1895 goto done;
1897 if (got_fileindex_entry_has_blob(ie) &&
1898 memcmp(ie->blob_sha1, te->id.sha1,
1899 SHA1_DIGEST_LENGTH) == 0) {
1900 /* Different commit but the same blob. */
1901 err = sync_timestamps(worktree->root_fd,
1902 path, status, ie, &sb);
1903 if (err)
1904 goto done;
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1906 path);
1907 goto done;
1911 fd1 = got_opentempfd();
1912 if (fd1 == -1) {
1913 err = got_error_from_errno("got_opentempfd");
1914 goto done;
1916 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1917 if (err)
1918 goto done;
1920 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 int update_timestamps;
1922 struct got_blob_object *blob2 = NULL;
1923 char *label_orig = NULL;
1924 if (got_fileindex_entry_has_blob(ie)) {
1925 fd2 = got_opentempfd();
1926 if (fd2 == -1) {
1927 err = got_error_from_errno("got_opentempfd");
1928 goto done;
1930 struct got_object_id id2;
1931 got_fileindex_entry_get_blob_id(&id2, ie);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1933 fd2);
1934 if (err)
1935 goto done;
1937 if (got_fileindex_entry_has_commit(ie)) {
1938 char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 sizeof(id_str)) == NULL) {
1941 err = got_error_path(id_str,
1942 GOT_ERR_BAD_OBJ_ID_STR);
1943 goto done;
1945 if (asprintf(&label_orig, "%s: commit %s",
1946 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 err = got_error_from_errno("asprintf");
1948 goto done;
1951 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1952 char *link_target;
1953 err = got_object_blob_read_to_str(&link_target, blob);
1954 if (err)
1955 goto done;
1956 err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 label_orig, link_target, worktree->base_commit_id,
1958 repo, progress_cb, progress_arg);
1959 free(link_target);
1960 } else {
1961 err = merge_blob(&update_timestamps, worktree, blob2,
1962 ondisk_path, path, sb.st_mode, label_orig, blob,
1963 worktree->base_commit_id, repo,
1964 progress_cb, progress_arg);
1966 free(label_orig);
1967 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 err = got_error_from_errno("close");
1969 goto done;
1971 if (blob2)
1972 got_object_blob_close(blob2);
1973 if (err)
1974 goto done;
1976 * Do not update timestamps of files with local changes.
1977 * Otherwise, a future status walk would treat them as
1978 * unmodified files again.
1980 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 blob->id.sha1, worktree->base_commit_id->sha1,
1982 update_timestamps);
1983 } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 } else if (status == GOT_STATUS_DELETE) {
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 if (err)
1989 goto done;
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1992 if (err)
1993 goto done;
1994 } else {
1995 int is_bad_symlink = 0;
1996 if (S_ISLNK(te->mode)) {
1997 err = install_symlink(&is_bad_symlink, worktree,
1998 ondisk_path, path, blob,
1999 status == GOT_STATUS_MISSING, 0,
2000 status == GOT_STATUS_UNVERSIONED, 0,
2001 repo, progress_cb, progress_arg);
2002 } else {
2003 err = install_blob(worktree, ondisk_path, path,
2004 te->mode, sb.st_mode, blob,
2005 status == GOT_STATUS_MISSING, 0, 0,
2006 status == GOT_STATUS_UNVERSIONED, repo,
2007 progress_cb, progress_arg);
2009 if (err)
2010 goto done;
2012 if (ie) {
2013 err = got_fileindex_entry_update(ie,
2014 worktree->root_fd, path, blob->id.sha1,
2015 worktree->base_commit_id->sha1, 1);
2016 } else {
2017 err = create_fileindex_entry(&ie, fileindex,
2018 worktree->base_commit_id, worktree->root_fd, path,
2019 &blob->id);
2021 if (err)
2022 goto done;
2024 if (is_bad_symlink) {
2025 got_fileindex_entry_filetype_set(ie,
2026 GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 err = got_error_from_errno("close");
2032 goto done;
2034 got_object_blob_close(blob);
2035 done:
2036 free(ondisk_path);
2037 return err;
2040 static const struct got_error *
2041 remove_ondisk_file(const char *root_path, const char *path)
2043 const struct got_error *err = NULL;
2044 char *ondisk_path = NULL, *parent = NULL;
2046 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 return got_error_from_errno("asprintf");
2049 if (unlink(ondisk_path) == -1) {
2050 if (errno != ENOENT)
2051 err = got_error_from_errno2("unlink", ondisk_path);
2052 } else {
2053 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 goto done;
2057 while (got_path_cmp(parent, root_path,
2058 strlen(parent), root_len) != 0) {
2059 free(ondisk_path);
2060 ondisk_path = parent;
2061 parent = NULL;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2065 ondisk_path);
2066 break;
2068 err = got_path_dirname(&parent, ondisk_path);
2069 if (err)
2070 break;
2073 done:
2074 free(ondisk_path);
2075 free(parent);
2076 return err;
2079 static const struct got_error *
2080 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 struct got_fileindex_entry *ie, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 unsigned char status;
2086 struct stat sb;
2087 char *ondisk_path;
2089 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2093 == -1)
2094 return got_error_from_errno("asprintf");
2096 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2097 if (err)
2098 goto done;
2100 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 char ondisk_target[PATH_MAX];
2102 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 sizeof(ondisk_target));
2104 if (ondisk_len == -1) {
2105 err = got_error_from_errno2("readlink", ondisk_path);
2106 goto done;
2108 ondisk_target[ondisk_len] = '\0';
2109 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 NULL, NULL, /* XXX pass common ancestor info? */
2111 ondisk_target, ondisk_path);
2112 if (err)
2113 goto done;
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2115 ie->path);
2116 goto done;
2119 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 status == GOT_STATUS_ADD) {
2121 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2122 if (err)
2123 goto done;
2125 * Preserve the working file and change the deleted blob's
2126 * entry into a schedule-add entry.
2128 err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 ie->path, NULL, NULL, 0);
2130 } else {
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 if (err)
2133 goto done;
2134 if (status == GOT_STATUS_NO_CHANGE) {
2135 err = remove_ondisk_file(worktree->root_path, ie->path);
2136 if (err)
2137 goto done;
2139 got_fileindex_entry_remove(fileindex, ie);
2141 done:
2142 free(ondisk_path);
2143 return err;
2146 struct diff_cb_arg {
2147 struct got_fileindex *fileindex;
2148 struct got_worktree *worktree;
2149 struct got_repository *repo;
2150 got_worktree_checkout_cb progress_cb;
2151 void *progress_arg;
2152 got_cancel_cb cancel_cb;
2153 void *cancel_arg;
2156 static const struct got_error *
2157 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 struct got_tree_entry *te, const char *parent_path)
2160 struct diff_cb_arg *a = arg;
2162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 return got_error(GOT_ERR_CANCELLED);
2165 return update_blob(a->worktree, a->fileindex, ie, te,
2166 ie->path, a->repo, a->progress_cb, a->progress_arg);
2169 static const struct got_error *
2170 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return delete_blob(a->worktree, a->fileindex, ie,
2178 a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2185 const struct got_error *err;
2186 char *path;
2188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 return got_error(GOT_ERR_CANCELLED);
2191 if (got_object_tree_entry_is_submodule(te))
2192 return NULL;
2194 if (asprintf(&path, "%s%s%s", parent_path,
2195 parent_path[0] ? "/" : "", te->name)
2196 == -1)
2197 return got_error_from_errno("asprintf");
2199 if (S_ISDIR(te->mode))
2200 err = add_dir_on_disk(a->worktree, path);
2201 else
2202 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 a->repo, a->progress_cb, a->progress_arg);
2205 free(path);
2206 return err;
2209 const struct got_error *
2210 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 uint32_t uuid_status;
2214 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 if (uuid_status != uuid_s_ok) {
2216 *uuidstr = NULL;
2217 return got_error_uuid(uuid_status, "uuid_to_string");
2220 return NULL;
2223 static const struct got_error *
2224 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 const struct got_error *err = NULL;
2227 char *uuidstr = NULL;
2229 *refname = NULL;
2231 err = got_worktree_get_uuid(&uuidstr, worktree);
2232 if (err)
2233 return err;
2235 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 err = got_error_from_errno("asprintf");
2237 *refname = NULL;
2239 free(uuidstr);
2240 return err;
2243 const struct got_error *
2244 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2245 const char *prefix)
2247 return get_ref_name(refname, worktree, prefix);
2250 const struct got_error *
2251 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2253 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2256 static const struct got_error *
2257 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree,
2260 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2263 static const struct got_error *
2264 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2266 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2269 static const struct got_error *
2270 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2297 static const struct got_error *
2298 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2304 static const struct got_error *
2305 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2307 return get_ref_name(refname, worktree,
2308 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2311 const struct got_error *
2312 got_worktree_get_histedit_script_path(char **path,
2313 struct got_worktree *worktree)
2315 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2316 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2317 *path = NULL;
2318 return got_error_from_errno("asprintf");
2320 return NULL;
2323 static const struct got_error *
2324 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2330 static const struct got_error *
2331 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree,
2334 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2338 * Prevent Git's garbage collector from deleting our base commit by
2339 * setting a reference to our base commit's ID.
2341 static const struct got_error *
2342 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2344 const struct got_error *err = NULL;
2345 struct got_reference *ref = NULL;
2346 char *refname;
2348 err = got_worktree_get_base_ref_name(&refname, worktree);
2349 if (err)
2350 return err;
2352 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2353 if (err)
2354 goto done;
2356 err = got_ref_write(ref, repo);
2357 done:
2358 free(refname);
2359 if (ref)
2360 got_ref_close(ref);
2361 return err;
2364 static const struct got_error *
2365 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2367 const struct got_error *err = NULL;
2369 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2370 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 *fileindex_path = NULL;
2374 return err;
2378 static const struct got_error *
2379 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2380 struct got_worktree *worktree)
2382 const struct got_error *err = NULL;
2383 FILE *index = NULL;
2385 *fileindex_path = NULL;
2386 *fileindex = got_fileindex_alloc();
2387 if (*fileindex == NULL)
2388 return got_error_from_errno("got_fileindex_alloc");
2390 err = get_fileindex_path(fileindex_path, worktree);
2391 if (err)
2392 goto done;
2394 index = fopen(*fileindex_path, "rbe");
2395 if (index == NULL) {
2396 if (errno != ENOENT)
2397 err = got_error_from_errno2("fopen", *fileindex_path);
2398 } else {
2399 err = got_fileindex_read(*fileindex, index);
2400 if (fclose(index) == EOF && err == NULL)
2401 err = got_error_from_errno("fclose");
2403 done:
2404 if (err) {
2405 free(*fileindex_path);
2406 *fileindex_path = NULL;
2407 got_fileindex_free(*fileindex);
2408 *fileindex = NULL;
2410 return err;
2413 struct bump_base_commit_id_arg {
2414 struct got_object_id *base_commit_id;
2415 const char *path;
2416 size_t path_len;
2417 const char *entry_name;
2418 got_worktree_checkout_cb progress_cb;
2419 void *progress_arg;
2422 static const struct got_error *
2423 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2425 const struct got_error *err;
2426 struct bump_base_commit_id_arg *a = arg;
2428 if (a->entry_name) {
2429 if (strcmp(ie->path, a->path) != 0)
2430 return NULL;
2431 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2432 return NULL;
2434 if (got_fileindex_entry_was_skipped(ie))
2435 return NULL;
2437 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2438 SHA1_DIGEST_LENGTH) == 0)
2439 return NULL;
2441 if (a->progress_cb) {
2442 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2443 ie->path);
2444 if (err)
2445 return err;
2447 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2448 return NULL;
2451 /* Bump base commit ID of all files within an updated part of the work tree. */
2452 static const struct got_error *
2453 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2454 struct got_fileindex *fileindex,
2455 got_worktree_checkout_cb progress_cb, void *progress_arg)
2457 struct bump_base_commit_id_arg bbc_arg;
2459 bbc_arg.base_commit_id = worktree->base_commit_id;
2460 bbc_arg.entry_name = NULL;
2461 bbc_arg.path = "";
2462 bbc_arg.path_len = 0;
2463 bbc_arg.progress_cb = progress_cb;
2464 bbc_arg.progress_arg = progress_arg;
2466 return got_fileindex_for_each_entry_safe(fileindex,
2467 bump_base_commit_id, &bbc_arg);
2470 static const struct got_error *
2471 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2473 const struct got_error *err = NULL;
2474 char *new_fileindex_path = NULL;
2475 FILE *new_index = NULL;
2476 struct timespec timeout;
2478 err = got_opentemp_named(&new_fileindex_path, &new_index,
2479 fileindex_path, "");
2480 if (err)
2481 goto done;
2483 err = got_fileindex_write(fileindex, new_index);
2484 if (err)
2485 goto done;
2487 if (rename(new_fileindex_path, fileindex_path) != 0) {
2488 err = got_error_from_errno3("rename", new_fileindex_path,
2489 fileindex_path);
2490 unlink(new_fileindex_path);
2494 * Sleep for a short amount of time to ensure that files modified after
2495 * this program exits have a different time stamp from the one which
2496 * was recorded in the file index.
2498 timeout.tv_sec = 0;
2499 timeout.tv_nsec = 1;
2500 nanosleep(&timeout, NULL);
2501 done:
2502 if (new_index)
2503 fclose(new_index);
2504 free(new_fileindex_path);
2505 return err;
2508 static const struct got_error *
2509 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2510 struct got_object_id **tree_id, const char *wt_relpath,
2511 struct got_commit_object *base_commit, struct got_worktree *worktree,
2512 struct got_repository *repo)
2514 const struct got_error *err = NULL;
2515 struct got_object_id *id = NULL;
2516 char *in_repo_path = NULL;
2517 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2519 *entry_type = GOT_OBJ_TYPE_ANY;
2520 *tree_relpath = NULL;
2521 *tree_id = NULL;
2523 if (wt_relpath[0] == '\0') {
2524 /* Check out all files within the work tree. */
2525 *entry_type = GOT_OBJ_TYPE_TREE;
2526 *tree_relpath = strdup("");
2527 if (*tree_relpath == NULL) {
2528 err = got_error_from_errno("strdup");
2529 goto done;
2531 err = got_object_id_by_path(tree_id, repo, base_commit,
2532 worktree->path_prefix);
2533 if (err)
2534 goto done;
2535 return NULL;
2538 /* Check out a subset of files in the work tree. */
2540 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2541 is_root_wt ? "" : "/", wt_relpath) == -1) {
2542 err = got_error_from_errno("asprintf");
2543 goto done;
2546 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2547 if (err)
2548 goto done;
2550 free(in_repo_path);
2551 in_repo_path = NULL;
2553 err = got_object_get_type(entry_type, repo, id);
2554 if (err)
2555 goto done;
2557 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2558 /* Check out a single file. */
2559 if (strchr(wt_relpath, '/') == NULL) {
2560 /* Check out a single file in work tree's root dir. */
2561 in_repo_path = strdup(worktree->path_prefix);
2562 if (in_repo_path == NULL) {
2563 err = got_error_from_errno("strdup");
2564 goto done;
2566 *tree_relpath = strdup("");
2567 if (*tree_relpath == NULL) {
2568 err = got_error_from_errno("strdup");
2569 goto done;
2571 } else {
2572 /* Check out a single file in a subdirectory. */
2573 err = got_path_dirname(tree_relpath, wt_relpath);
2574 if (err)
2575 return err;
2576 if (asprintf(&in_repo_path, "%s%s%s",
2577 worktree->path_prefix, is_root_wt ? "" : "/",
2578 *tree_relpath) == -1) {
2579 err = got_error_from_errno("asprintf");
2580 goto done;
2583 err = got_object_id_by_path(tree_id, repo,
2584 base_commit, in_repo_path);
2585 } else {
2586 /* Check out all files within a subdirectory. */
2587 *tree_id = got_object_id_dup(id);
2588 if (*tree_id == NULL) {
2589 err = got_error_from_errno("got_object_id_dup");
2590 goto done;
2592 *tree_relpath = strdup(wt_relpath);
2593 if (*tree_relpath == NULL) {
2594 err = got_error_from_errno("strdup");
2595 goto done;
2598 done:
2599 free(id);
2600 free(in_repo_path);
2601 if (err) {
2602 *entry_type = GOT_OBJ_TYPE_ANY;
2603 free(*tree_relpath);
2604 *tree_relpath = NULL;
2605 free(*tree_id);
2606 *tree_id = NULL;
2608 return err;
2611 static const struct got_error *
2612 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2613 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2614 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2615 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2617 const struct got_error *err = NULL;
2618 struct got_commit_object *commit = NULL;
2619 struct got_tree_object *tree = NULL;
2620 struct got_fileindex_diff_tree_cb diff_cb;
2621 struct diff_cb_arg arg;
2623 err = ref_base_commit(worktree, repo);
2624 if (err) {
2625 if (!(err->code == GOT_ERR_ERRNO &&
2626 (errno == EACCES || errno == EROFS)))
2627 goto done;
2628 err = (*progress_cb)(progress_arg,
2629 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2630 if (err)
2631 return err;
2634 err = got_object_open_as_commit(&commit, repo,
2635 worktree->base_commit_id);
2636 if (err)
2637 goto done;
2639 err = got_object_open_as_tree(&tree, repo, tree_id);
2640 if (err)
2641 goto done;
2643 if (entry_name &&
2644 got_object_tree_find_entry(tree, entry_name) == NULL) {
2645 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2646 goto done;
2649 diff_cb.diff_old_new = diff_old_new;
2650 diff_cb.diff_old = diff_old;
2651 diff_cb.diff_new = diff_new;
2652 arg.fileindex = fileindex;
2653 arg.worktree = worktree;
2654 arg.repo = repo;
2655 arg.progress_cb = progress_cb;
2656 arg.progress_arg = progress_arg;
2657 arg.cancel_cb = cancel_cb;
2658 arg.cancel_arg = cancel_arg;
2659 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2660 entry_name, repo, &diff_cb, &arg);
2661 done:
2662 if (tree)
2663 got_object_tree_close(tree);
2664 if (commit)
2665 got_object_commit_close(commit);
2666 return err;
2669 const struct got_error *
2670 got_worktree_checkout_files(struct got_worktree *worktree,
2671 struct got_pathlist_head *paths, struct got_repository *repo,
2672 got_worktree_checkout_cb progress_cb, void *progress_arg,
2673 got_cancel_cb cancel_cb, void *cancel_arg)
2675 const struct got_error *err = NULL, *sync_err, *unlockerr;
2676 struct got_commit_object *commit = NULL;
2677 struct got_tree_object *tree = NULL;
2678 struct got_fileindex *fileindex = NULL;
2679 char *fileindex_path = NULL;
2680 struct got_pathlist_entry *pe;
2681 struct tree_path_data {
2682 STAILQ_ENTRY(tree_path_data) entry;
2683 struct got_object_id *tree_id;
2684 int entry_type;
2685 char *relpath;
2686 char *entry_name;
2687 } *tpd = NULL;
2688 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2690 STAILQ_INIT(&tree_paths);
2692 err = lock_worktree(worktree, LOCK_EX);
2693 if (err)
2694 return err;
2696 err = got_object_open_as_commit(&commit, repo,
2697 worktree->base_commit_id);
2698 if (err)
2699 goto done;
2701 /* Map all specified paths to in-repository trees. */
2702 TAILQ_FOREACH(pe, paths, entry) {
2703 tpd = malloc(sizeof(*tpd));
2704 if (tpd == NULL) {
2705 err = got_error_from_errno("malloc");
2706 goto done;
2709 err = find_tree_entry_for_checkout(&tpd->entry_type,
2710 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2711 worktree, repo);
2712 if (err) {
2713 free(tpd);
2714 goto done;
2717 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2718 err = got_path_basename(&tpd->entry_name, pe->path);
2719 if (err) {
2720 free(tpd->relpath);
2721 free(tpd->tree_id);
2722 free(tpd);
2723 goto done;
2725 } else
2726 tpd->entry_name = NULL;
2728 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2732 * Read the file index.
2733 * Checking out files is supposed to be an idempotent operation.
2734 * If the on-disk file index is incomplete we will try to complete it.
2736 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2737 if (err)
2738 goto done;
2740 tpd = STAILQ_FIRST(&tree_paths);
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 struct bump_base_commit_id_arg bbc_arg;
2744 err = checkout_files(worktree, fileindex, tpd->relpath,
2745 tpd->tree_id, tpd->entry_name, repo,
2746 progress_cb, progress_arg, cancel_cb, cancel_arg);
2747 if (err)
2748 break;
2750 bbc_arg.base_commit_id = worktree->base_commit_id;
2751 bbc_arg.entry_name = tpd->entry_name;
2752 bbc_arg.path = pe->path;
2753 bbc_arg.path_len = pe->path_len;
2754 bbc_arg.progress_cb = progress_cb;
2755 bbc_arg.progress_arg = progress_arg;
2756 err = got_fileindex_for_each_entry_safe(fileindex,
2757 bump_base_commit_id, &bbc_arg);
2758 if (err)
2759 break;
2761 tpd = STAILQ_NEXT(tpd, entry);
2763 sync_err = sync_fileindex(fileindex, fileindex_path);
2764 if (sync_err && err == NULL)
2765 err = sync_err;
2766 done:
2767 free(fileindex_path);
2768 if (tree)
2769 got_object_tree_close(tree);
2770 if (commit)
2771 got_object_commit_close(commit);
2772 if (fileindex)
2773 got_fileindex_free(fileindex);
2774 while (!STAILQ_EMPTY(&tree_paths)) {
2775 tpd = STAILQ_FIRST(&tree_paths);
2776 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2777 free(tpd->relpath);
2778 free(tpd->tree_id);
2779 free(tpd);
2781 unlockerr = lock_worktree(worktree, LOCK_SH);
2782 if (unlockerr && err == NULL)
2783 err = unlockerr;
2784 return err;
2787 struct merge_file_cb_arg {
2788 struct got_worktree *worktree;
2789 struct got_fileindex *fileindex;
2790 got_worktree_checkout_cb progress_cb;
2791 void *progress_arg;
2792 got_cancel_cb cancel_cb;
2793 void *cancel_arg;
2794 const char *label_orig;
2795 struct got_object_id *commit_id2;
2796 int allow_bad_symlinks;
2799 static const struct got_error *
2800 merge_file_cb(void *arg, struct got_blob_object *blob1,
2801 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2802 struct got_object_id *id1, struct got_object_id *id2,
2803 const char *path1, const char *path2,
2804 mode_t mode1, mode_t mode2, struct got_repository *repo)
2806 static const struct got_error *err = NULL;
2807 struct merge_file_cb_arg *a = arg;
2808 struct got_fileindex_entry *ie;
2809 char *ondisk_path = NULL;
2810 struct stat sb;
2811 unsigned char status;
2812 int local_changes_subsumed;
2813 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2814 char *id_str = NULL, *label_deriv2 = NULL;
2816 if (blob1 && blob2) {
2817 ie = got_fileindex_entry_get(a->fileindex, path2,
2818 strlen(path2));
2819 if (ie == NULL)
2820 return (*a->progress_cb)(a->progress_arg,
2821 GOT_STATUS_MISSING, path2);
2823 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2824 path2) == -1)
2825 return got_error_from_errno("asprintf");
2827 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2828 repo);
2829 if (err)
2830 goto done;
2832 if (status == GOT_STATUS_DELETE) {
2833 err = (*a->progress_cb)(a->progress_arg,
2834 GOT_STATUS_MERGE, path2);
2835 goto done;
2837 if (status != GOT_STATUS_NO_CHANGE &&
2838 status != GOT_STATUS_MODIFY &&
2839 status != GOT_STATUS_CONFLICT &&
2840 status != GOT_STATUS_ADD) {
2841 err = (*a->progress_cb)(a->progress_arg, status, path2);
2842 goto done;
2845 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2846 char *link_target2;
2847 err = got_object_blob_read_to_str(&link_target2, blob2);
2848 if (err)
2849 goto done;
2850 err = merge_symlink(a->worktree, blob1, ondisk_path,
2851 path2, a->label_orig, link_target2, a->commit_id2,
2852 repo, a->progress_cb, a->progress_arg);
2853 free(link_target2);
2854 } else {
2855 int fd;
2857 f_orig = got_opentemp();
2858 if (f_orig == NULL) {
2859 err = got_error_from_errno("got_opentemp");
2860 goto done;
2862 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2863 f_orig, blob1);
2864 if (err)
2865 goto done;
2867 f_deriv2 = got_opentemp();
2868 if (f_deriv2 == NULL)
2869 goto done;
2870 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2871 f_deriv2, blob2);
2872 if (err)
2873 goto done;
2875 fd = open(ondisk_path,
2876 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2877 if (fd == -1) {
2878 err = got_error_from_errno2("open",
2879 ondisk_path);
2880 goto done;
2882 f_deriv = fdopen(fd, "r");
2883 if (f_deriv == NULL) {
2884 err = got_error_from_errno2("fdopen",
2885 ondisk_path);
2886 close(fd);
2887 goto done;
2889 err = got_object_id_str(&id_str, a->commit_id2);
2890 if (err)
2891 goto done;
2892 if (asprintf(&label_deriv2, "%s: commit %s",
2893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2894 err = got_error_from_errno("asprintf");
2895 goto done;
2897 err = merge_file(&local_changes_subsumed, a->worktree,
2898 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2899 mode2, a->label_orig, NULL, label_deriv2,
2900 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2901 a->progress_cb, a->progress_arg);
2903 } else if (blob1) {
2904 ie = got_fileindex_entry_get(a->fileindex, path1,
2905 strlen(path1));
2906 if (ie == NULL)
2907 return (*a->progress_cb)(a->progress_arg,
2908 GOT_STATUS_MISSING, path1);
2910 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2911 path1) == -1)
2912 return got_error_from_errno("asprintf");
2914 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2915 repo);
2916 if (err)
2917 goto done;
2919 switch (status) {
2920 case GOT_STATUS_NO_CHANGE:
2921 err = (*a->progress_cb)(a->progress_arg,
2922 GOT_STATUS_DELETE, path1);
2923 if (err)
2924 goto done;
2925 err = remove_ondisk_file(a->worktree->root_path, path1);
2926 if (err)
2927 goto done;
2928 if (ie)
2929 got_fileindex_entry_mark_deleted_from_disk(ie);
2930 break;
2931 case GOT_STATUS_DELETE:
2932 case GOT_STATUS_MISSING:
2933 err = (*a->progress_cb)(a->progress_arg,
2934 GOT_STATUS_DELETE, path1);
2935 if (err)
2936 goto done;
2937 if (ie)
2938 got_fileindex_entry_mark_deleted_from_disk(ie);
2939 break;
2940 case GOT_STATUS_ADD: {
2941 struct got_object_id *id;
2942 FILE *blob1_f;
2943 off_t blob1_size;
2945 * Delete the added file only if its content already
2946 * exists in the repository.
2948 err = got_object_blob_file_create(&id, &blob1_f,
2949 &blob1_size, path1);
2950 if (err)
2951 goto done;
2952 if (got_object_id_cmp(id, id1) == 0) {
2953 err = (*a->progress_cb)(a->progress_arg,
2954 GOT_STATUS_DELETE, path1);
2955 if (err)
2956 goto done;
2957 err = remove_ondisk_file(a->worktree->root_path,
2958 path1);
2959 if (err)
2960 goto done;
2961 if (ie)
2962 got_fileindex_entry_remove(a->fileindex,
2963 ie);
2964 } else {
2965 err = (*a->progress_cb)(a->progress_arg,
2966 GOT_STATUS_CANNOT_DELETE, path1);
2968 if (fclose(blob1_f) == EOF && err == NULL)
2969 err = got_error_from_errno("fclose");
2970 free(id);
2971 if (err)
2972 goto done;
2973 break;
2975 case GOT_STATUS_MODIFY:
2976 case GOT_STATUS_CONFLICT:
2977 err = (*a->progress_cb)(a->progress_arg,
2978 GOT_STATUS_CANNOT_DELETE, path1);
2979 if (err)
2980 goto done;
2981 break;
2982 case GOT_STATUS_OBSTRUCTED:
2983 err = (*a->progress_cb)(a->progress_arg, status, path1);
2984 if (err)
2985 goto done;
2986 break;
2987 default:
2988 break;
2990 } else if (blob2) {
2991 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2992 path2) == -1)
2993 return got_error_from_errno("asprintf");
2994 ie = got_fileindex_entry_get(a->fileindex, path2,
2995 strlen(path2));
2996 if (ie) {
2997 err = get_file_status(&status, &sb, ie, ondisk_path,
2998 -1, NULL, repo);
2999 if (err)
3000 goto done;
3001 if (status != GOT_STATUS_NO_CHANGE &&
3002 status != GOT_STATUS_MODIFY &&
3003 status != GOT_STATUS_CONFLICT &&
3004 status != GOT_STATUS_ADD) {
3005 err = (*a->progress_cb)(a->progress_arg,
3006 status, path2);
3007 goto done;
3009 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3010 char *link_target2;
3011 err = got_object_blob_read_to_str(&link_target2,
3012 blob2);
3013 if (err)
3014 goto done;
3015 err = merge_symlink(a->worktree, NULL,
3016 ondisk_path, path2, a->label_orig,
3017 link_target2, a->commit_id2, repo,
3018 a->progress_cb, a->progress_arg);
3019 free(link_target2);
3020 } else if (S_ISREG(sb.st_mode)) {
3021 err = merge_blob(&local_changes_subsumed,
3022 a->worktree, NULL, ondisk_path, path2,
3023 sb.st_mode, a->label_orig, blob2,
3024 a->commit_id2, repo, a->progress_cb,
3025 a->progress_arg);
3026 } else {
3027 err = got_error_path(ondisk_path,
3028 GOT_ERR_FILE_OBSTRUCTED);
3030 if (err)
3031 goto done;
3032 if (status == GOT_STATUS_DELETE) {
3033 err = got_fileindex_entry_update(ie,
3034 a->worktree->root_fd, path2, blob2->id.sha1,
3035 a->worktree->base_commit_id->sha1, 0);
3036 if (err)
3037 goto done;
3039 } else {
3040 int is_bad_symlink = 0;
3041 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3042 if (S_ISLNK(mode2)) {
3043 err = install_symlink(&is_bad_symlink,
3044 a->worktree, ondisk_path, path2, blob2, 0,
3045 0, 1, a->allow_bad_symlinks, repo,
3046 a->progress_cb, a->progress_arg);
3047 } else {
3048 err = install_blob(a->worktree, ondisk_path, path2,
3049 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3050 a->progress_cb, a->progress_arg);
3052 if (err)
3053 goto done;
3054 err = got_fileindex_entry_alloc(&ie, path2);
3055 if (err)
3056 goto done;
3057 err = got_fileindex_entry_update(ie,
3058 a->worktree->root_fd, path2, NULL, NULL, 1);
3059 if (err) {
3060 got_fileindex_entry_free(ie);
3061 goto done;
3063 err = got_fileindex_entry_add(a->fileindex, ie);
3064 if (err) {
3065 got_fileindex_entry_free(ie);
3066 goto done;
3068 if (is_bad_symlink) {
3069 got_fileindex_entry_filetype_set(ie,
3070 GOT_FILEIDX_MODE_BAD_SYMLINK);
3074 done:
3075 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3076 err = got_error_from_errno("fclose");
3077 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 free(id_str);
3082 free(label_deriv2);
3083 free(ondisk_path);
3084 return err;
3087 static const struct got_error *
3088 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3090 struct got_worktree *worktree = arg;
3092 /* Reject merges into a work tree with mixed base commits. */
3093 if (got_fileindex_entry_has_commit(ie) &&
3094 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3095 SHA1_DIGEST_LENGTH) != 0)
3096 return got_error(GOT_ERR_MIXED_COMMITS);
3098 return NULL;
3101 struct check_merge_conflicts_arg {
3102 struct got_worktree *worktree;
3103 struct got_fileindex *fileindex;
3104 struct got_repository *repo;
3107 static const struct got_error *
3108 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3109 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3110 struct got_object_id *id1, struct got_object_id *id2,
3111 const char *path1, const char *path2,
3112 mode_t mode1, mode_t mode2, struct got_repository *repo)
3114 const struct got_error *err = NULL;
3115 struct check_merge_conflicts_arg *a = arg;
3116 unsigned char status;
3117 struct stat sb;
3118 struct got_fileindex_entry *ie;
3119 const char *path = path2 ? path2 : path1;
3120 struct got_object_id *id = id2 ? id2 : id1;
3121 char *ondisk_path;
3123 if (id == NULL)
3124 return NULL;
3126 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3127 if (ie == NULL)
3128 return NULL;
3130 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3131 == -1)
3132 return got_error_from_errno("asprintf");
3134 /* Reject merges into a work tree with conflicted files. */
3135 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3136 free(ondisk_path);
3137 if (err)
3138 return err;
3139 if (status == GOT_STATUS_CONFLICT)
3140 return got_error(GOT_ERR_CONFLICTS);
3142 return NULL;
3145 static const struct got_error *
3146 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3147 const char *fileindex_path, struct got_object_id *commit_id1,
3148 struct got_object_id *commit_id2, struct got_repository *repo,
3149 got_worktree_checkout_cb progress_cb, void *progress_arg,
3150 got_cancel_cb cancel_cb, void *cancel_arg)
3152 const struct got_error *err = NULL, *sync_err;
3153 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3154 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3155 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3156 struct check_merge_conflicts_arg cmc_arg;
3157 struct merge_file_cb_arg arg;
3158 char *label_orig = NULL;
3159 FILE *f1 = NULL, *f2 = NULL;
3160 int fd1 = -1, fd2 = -1;
3162 if (commit_id1) {
3163 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3164 if (err)
3165 goto done;
3166 err = got_object_id_by_path(&tree_id1, repo, commit1,
3167 worktree->path_prefix);
3168 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3169 goto done;
3171 if (tree_id1) {
3172 char *id_str;
3174 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3175 if (err)
3176 goto done;
3178 err = got_object_id_str(&id_str, commit_id1);
3179 if (err)
3180 goto done;
3182 if (asprintf(&label_orig, "%s: commit %s",
3183 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3184 err = got_error_from_errno("asprintf");
3185 free(id_str);
3186 goto done;
3188 free(id_str);
3190 f1 = got_opentemp();
3191 if (f1 == NULL) {
3192 err = got_error_from_errno("got_opentemp");
3193 goto done;
3197 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3198 if (err)
3199 goto done;
3201 err = got_object_id_by_path(&tree_id2, repo, commit2,
3202 worktree->path_prefix);
3203 if (err)
3204 goto done;
3206 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3207 if (err)
3208 goto done;
3210 f2 = got_opentemp();
3211 if (f2 == NULL) {
3212 err = got_error_from_errno("got_opentemp");
3213 goto done;
3216 fd1 = got_opentempfd();
3217 if (fd1 == -1) {
3218 err = got_error_from_errno("got_opentempfd");
3219 goto done;
3222 fd2 = got_opentempfd();
3223 if (fd2 == -1) {
3224 err = got_error_from_errno("got_opentempfd");
3225 goto done;
3228 cmc_arg.worktree = worktree;
3229 cmc_arg.fileindex = fileindex;
3230 cmc_arg.repo = repo;
3231 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3232 check_merge_conflicts, &cmc_arg, 0);
3233 if (err)
3234 goto done;
3236 arg.worktree = worktree;
3237 arg.fileindex = fileindex;
3238 arg.progress_cb = progress_cb;
3239 arg.progress_arg = progress_arg;
3240 arg.cancel_cb = cancel_cb;
3241 arg.cancel_arg = cancel_arg;
3242 arg.label_orig = label_orig;
3243 arg.commit_id2 = commit_id2;
3244 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3245 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3246 merge_file_cb, &arg, 1);
3247 sync_err = sync_fileindex(fileindex, fileindex_path);
3248 if (sync_err && err == NULL)
3249 err = sync_err;
3250 done:
3251 if (commit1)
3252 got_object_commit_close(commit1);
3253 if (commit2)
3254 got_object_commit_close(commit2);
3255 if (tree1)
3256 got_object_tree_close(tree1);
3257 if (tree2)
3258 got_object_tree_close(tree2);
3259 if (f1 && fclose(f1) == EOF && err == NULL)
3260 err = got_error_from_errno("fclose");
3261 if (f2 && fclose(f2) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3264 err = got_error_from_errno("close");
3265 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 free(label_orig);
3268 return err;
3271 const struct got_error *
3272 got_worktree_merge_files(struct got_worktree *worktree,
3273 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3274 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3275 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3277 const struct got_error *err, *unlockerr;
3278 char *fileindex_path = NULL;
3279 struct got_fileindex *fileindex = NULL;
3281 err = lock_worktree(worktree, LOCK_EX);
3282 if (err)
3283 return err;
3285 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3286 if (err)
3287 goto done;
3289 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3290 worktree);
3291 if (err)
3292 goto done;
3294 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3295 commit_id2, repo, progress_cb, progress_arg,
3296 cancel_cb, cancel_arg);
3297 done:
3298 if (fileindex)
3299 got_fileindex_free(fileindex);
3300 free(fileindex_path);
3301 unlockerr = lock_worktree(worktree, LOCK_SH);
3302 if (unlockerr && err == NULL)
3303 err = unlockerr;
3304 return err;
3307 struct diff_dir_cb_arg {
3308 struct got_fileindex *fileindex;
3309 struct got_worktree *worktree;
3310 const char *status_path;
3311 size_t status_path_len;
3312 struct got_repository *repo;
3313 got_worktree_status_cb status_cb;
3314 void *status_arg;
3315 got_cancel_cb cancel_cb;
3316 void *cancel_arg;
3317 /* A pathlist containing per-directory pathlists of ignore patterns. */
3318 struct got_pathlist_head *ignores;
3319 int report_unchanged;
3320 int no_ignores;
3323 static const struct got_error *
3324 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3325 int dirfd, const char *de_name,
3326 got_worktree_status_cb status_cb, void *status_arg,
3327 struct got_repository *repo, int report_unchanged)
3329 const struct got_error *err = NULL;
3330 unsigned char status = GOT_STATUS_NO_CHANGE;
3331 unsigned char staged_status;
3332 struct stat sb;
3333 struct got_object_id blob_id, commit_id, staged_blob_id;
3334 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3335 struct got_object_id *staged_blob_idp = NULL;
3337 staged_status = get_staged_status(ie);
3338 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3339 if (err)
3340 return err;
3342 if (status == GOT_STATUS_NO_CHANGE &&
3343 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3344 return NULL;
3346 if (got_fileindex_entry_has_blob(ie))
3347 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3348 if (got_fileindex_entry_has_commit(ie))
3349 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3350 if (staged_status == GOT_STATUS_ADD ||
3351 staged_status == GOT_STATUS_MODIFY) {
3352 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3353 &staged_blob_id, ie);
3356 return (*status_cb)(status_arg, status, staged_status,
3357 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3360 static const struct got_error *
3361 status_old_new(void *arg, struct got_fileindex_entry *ie,
3362 struct dirent *de, const char *parent_path, int dirfd)
3364 const struct got_error *err = NULL;
3365 struct diff_dir_cb_arg *a = arg;
3366 char *abspath;
3368 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3369 return got_error(GOT_ERR_CANCELLED);
3371 if (got_path_cmp(parent_path, a->status_path,
3372 strlen(parent_path), a->status_path_len) != 0 &&
3373 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3374 return NULL;
3376 if (parent_path[0]) {
3377 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3378 parent_path, de->d_name) == -1)
3379 return got_error_from_errno("asprintf");
3380 } else {
3381 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3382 de->d_name) == -1)
3383 return got_error_from_errno("asprintf");
3386 err = report_file_status(ie, abspath, dirfd, de->d_name,
3387 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3388 free(abspath);
3389 return err;
3392 static const struct got_error *
3393 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3395 struct diff_dir_cb_arg *a = arg;
3396 struct got_object_id blob_id, commit_id;
3397 unsigned char status;
3399 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3400 return got_error(GOT_ERR_CANCELLED);
3402 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3403 return NULL;
3405 got_fileindex_entry_get_blob_id(&blob_id, ie);
3406 got_fileindex_entry_get_commit_id(&commit_id, ie);
3407 if (got_fileindex_entry_has_file_on_disk(ie))
3408 status = GOT_STATUS_MISSING;
3409 else
3410 status = GOT_STATUS_DELETE;
3411 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3412 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3415 static void
3416 free_ignores(struct got_pathlist_head *ignores)
3418 struct got_pathlist_entry *pe;
3420 TAILQ_FOREACH(pe, ignores, entry) {
3421 struct got_pathlist_head *ignorelist = pe->data;
3423 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3425 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3428 static const struct got_error *
3429 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3431 const struct got_error *err = NULL;
3432 struct got_pathlist_entry *pe = NULL;
3433 struct got_pathlist_head *ignorelist;
3434 char *line = NULL, *pattern, *dirpath = NULL;
3435 size_t linesize = 0;
3436 ssize_t linelen;
3438 ignorelist = calloc(1, sizeof(*ignorelist));
3439 if (ignorelist == NULL)
3440 return got_error_from_errno("calloc");
3441 TAILQ_INIT(ignorelist);
3443 while ((linelen = getline(&line, &linesize, f)) != -1) {
3444 if (linelen > 0 && line[linelen - 1] == '\n')
3445 line[linelen - 1] = '\0';
3447 /* Git's ignores may contain comments. */
3448 if (line[0] == '#')
3449 continue;
3451 /* Git's negated patterns are not (yet?) supported. */
3452 if (line[0] == '!')
3453 continue;
3455 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3456 line) == -1) {
3457 err = got_error_from_errno("asprintf");
3458 goto done;
3460 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3461 if (err)
3462 goto done;
3464 if (ferror(f)) {
3465 err = got_error_from_errno("getline");
3466 goto done;
3469 dirpath = strdup(path);
3470 if (dirpath == NULL) {
3471 err = got_error_from_errno("strdup");
3472 goto done;
3474 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3475 done:
3476 free(line);
3477 if (err || pe == NULL) {
3478 free(dirpath);
3479 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3481 return err;
3484 static int
3485 match_ignores(struct got_pathlist_head *ignores, const char *path)
3487 struct got_pathlist_entry *pe;
3489 /* Handle patterns which match in all directories. */
3490 TAILQ_FOREACH(pe, ignores, entry) {
3491 struct got_pathlist_head *ignorelist = pe->data;
3492 struct got_pathlist_entry *pi;
3494 TAILQ_FOREACH(pi, ignorelist, entry) {
3495 const char *p, *pattern = pi->path;
3497 if (strncmp(pattern, "**/", 3) != 0)
3498 continue;
3499 pattern += 3;
3500 p = path;
3501 while (*p) {
3502 if (fnmatch(pattern, p,
3503 FNM_PATHNAME | FNM_LEADING_DIR)) {
3504 /* Retry in next directory. */
3505 while (*p && *p != '/')
3506 p++;
3507 while (*p == '/')
3508 p++;
3509 continue;
3511 return 1;
3517 * The ignores pathlist contains ignore lists from children before
3518 * parents, so we can find the most specific ignorelist by walking
3519 * ignores backwards.
3521 pe = TAILQ_LAST(ignores, got_pathlist_head);
3522 while (pe) {
3523 if (got_path_is_child(path, pe->path, pe->path_len)) {
3524 struct got_pathlist_head *ignorelist = pe->data;
3525 struct got_pathlist_entry *pi;
3526 TAILQ_FOREACH(pi, ignorelist, entry) {
3527 const char *pattern = pi->path;
3528 int flags = FNM_LEADING_DIR;
3529 if (strstr(pattern, "/**/") == NULL)
3530 flags |= FNM_PATHNAME;
3531 if (fnmatch(pattern, path, flags))
3532 continue;
3533 return 1;
3536 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3539 return 0;
3542 static const struct got_error *
3543 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3544 const char *path, int dirfd, const char *ignores_filename)
3546 const struct got_error *err = NULL;
3547 char *ignorespath;
3548 int fd = -1;
3549 FILE *ignoresfile = NULL;
3551 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3552 path[0] ? "/" : "", ignores_filename) == -1)
3553 return got_error_from_errno("asprintf");
3555 if (dirfd != -1) {
3556 fd = openat(dirfd, ignores_filename,
3557 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3558 if (fd == -1) {
3559 if (errno != ENOENT && errno != EACCES)
3560 err = got_error_from_errno2("openat",
3561 ignorespath);
3562 } else {
3563 ignoresfile = fdopen(fd, "r");
3564 if (ignoresfile == NULL)
3565 err = got_error_from_errno2("fdopen",
3566 ignorespath);
3567 else {
3568 fd = -1;
3569 err = read_ignores(ignores, path, ignoresfile);
3572 } else {
3573 ignoresfile = fopen(ignorespath, "re");
3574 if (ignoresfile == NULL) {
3575 if (errno != ENOENT && errno != EACCES)
3576 err = got_error_from_errno2("fopen",
3577 ignorespath);
3578 } else
3579 err = read_ignores(ignores, path, ignoresfile);
3582 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3583 err = got_error_from_errno2("fclose", path);
3584 if (fd != -1 && close(fd) == -1 && err == NULL)
3585 err = got_error_from_errno2("close", path);
3586 free(ignorespath);
3587 return err;
3590 static const struct got_error *
3591 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3592 int dirfd)
3594 const struct got_error *err = NULL;
3595 struct diff_dir_cb_arg *a = arg;
3596 char *path = NULL;
3598 if (ignore != NULL)
3599 *ignore = 0;
3601 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3602 return got_error(GOT_ERR_CANCELLED);
3604 if (parent_path[0]) {
3605 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3606 return got_error_from_errno("asprintf");
3607 } else {
3608 path = de->d_name;
3611 if (de->d_type == DT_DIR) {
3612 if (!a->no_ignores && ignore != NULL &&
3613 match_ignores(a->ignores, path))
3614 *ignore = 1;
3615 } else if (!match_ignores(a->ignores, path) &&
3616 got_path_is_child(path, a->status_path, a->status_path_len))
3617 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3618 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3619 if (parent_path[0])
3620 free(path);
3621 return err;
3624 static const struct got_error *
3625 status_traverse(void *arg, const char *path, int dirfd)
3627 const struct got_error *err = NULL;
3628 struct diff_dir_cb_arg *a = arg;
3630 if (a->no_ignores)
3631 return NULL;
3633 err = add_ignores(a->ignores, a->worktree->root_path,
3634 path, dirfd, ".cvsignore");
3635 if (err)
3636 return err;
3638 err = add_ignores(a->ignores, a->worktree->root_path, path,
3639 dirfd, ".gitignore");
3641 return err;
3644 static const struct got_error *
3645 report_single_file_status(const char *path, const char *ondisk_path,
3646 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3647 void *status_arg, struct got_repository *repo, int report_unchanged,
3648 struct got_pathlist_head *ignores, int no_ignores)
3650 struct got_fileindex_entry *ie;
3651 struct stat sb;
3653 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3654 if (ie)
3655 return report_file_status(ie, ondisk_path, -1, NULL,
3656 status_cb, status_arg, repo, report_unchanged);
3658 if (lstat(ondisk_path, &sb) == -1) {
3659 if (errno != ENOENT)
3660 return got_error_from_errno2("lstat", ondisk_path);
3661 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3662 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3665 if (!no_ignores && match_ignores(ignores, path))
3666 return NULL;
3668 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3669 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3670 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 return NULL;
3675 static const struct got_error *
3676 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3677 const char *root_path, const char *path)
3679 const struct got_error *err;
3680 char *parent_path, *next_parent_path = NULL;
3682 err = add_ignores(ignores, root_path, "", -1,
3683 ".cvsignore");
3684 if (err)
3685 return err;
3687 err = add_ignores(ignores, root_path, "", -1,
3688 ".gitignore");
3689 if (err)
3690 return err;
3692 err = got_path_dirname(&parent_path, path);
3693 if (err) {
3694 if (err->code == GOT_ERR_BAD_PATH)
3695 return NULL; /* cannot traverse parent */
3696 return err;
3698 for (;;) {
3699 err = add_ignores(ignores, root_path, parent_path, -1,
3700 ".cvsignore");
3701 if (err)
3702 break;
3703 err = add_ignores(ignores, root_path, parent_path, -1,
3704 ".gitignore");
3705 if (err)
3706 break;
3707 err = got_path_dirname(&next_parent_path, parent_path);
3708 if (err) {
3709 if (err->code == GOT_ERR_BAD_PATH)
3710 err = NULL; /* traversed everything */
3711 break;
3713 if (got_path_is_root_dir(parent_path))
3714 break;
3715 free(parent_path);
3716 parent_path = next_parent_path;
3717 next_parent_path = NULL;
3720 free(parent_path);
3721 free(next_parent_path);
3722 return err;
3725 static const struct got_error *
3726 worktree_status(struct got_worktree *worktree, const char *path,
3727 struct got_fileindex *fileindex, struct got_repository *repo,
3728 got_worktree_status_cb status_cb, void *status_arg,
3729 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3730 int report_unchanged)
3732 const struct got_error *err = NULL;
3733 int fd = -1;
3734 struct got_fileindex_diff_dir_cb fdiff_cb;
3735 struct diff_dir_cb_arg arg;
3736 char *ondisk_path = NULL;
3737 struct got_pathlist_head ignores;
3738 struct got_fileindex_entry *ie;
3740 TAILQ_INIT(&ignores);
3742 if (asprintf(&ondisk_path, "%s%s%s",
3743 worktree->root_path, path[0] ? "/" : "", path) == -1)
3744 return got_error_from_errno("asprintf");
3746 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3747 if (ie) {
3748 err = report_single_file_status(path, ondisk_path,
3749 fileindex, status_cb, status_arg, repo,
3750 report_unchanged, &ignores, no_ignores);
3751 goto done;
3754 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3755 if (fd == -1) {
3756 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3757 !got_err_open_nofollow_on_symlink())
3758 err = got_error_from_errno2("open", ondisk_path);
3759 else {
3760 if (!no_ignores) {
3761 err = add_ignores_from_parent_paths(&ignores,
3762 worktree->root_path, ondisk_path);
3763 if (err)
3764 goto done;
3766 err = report_single_file_status(path, ondisk_path,
3767 fileindex, status_cb, status_arg, repo,
3768 report_unchanged, &ignores, no_ignores);
3770 } else {
3771 fdiff_cb.diff_old_new = status_old_new;
3772 fdiff_cb.diff_old = status_old;
3773 fdiff_cb.diff_new = status_new;
3774 fdiff_cb.diff_traverse = status_traverse;
3775 arg.fileindex = fileindex;
3776 arg.worktree = worktree;
3777 arg.status_path = path;
3778 arg.status_path_len = strlen(path);
3779 arg.repo = repo;
3780 arg.status_cb = status_cb;
3781 arg.status_arg = status_arg;
3782 arg.cancel_cb = cancel_cb;
3783 arg.cancel_arg = cancel_arg;
3784 arg.report_unchanged = report_unchanged;
3785 arg.no_ignores = no_ignores;
3786 if (!no_ignores) {
3787 err = add_ignores_from_parent_paths(&ignores,
3788 worktree->root_path, path);
3789 if (err)
3790 goto done;
3792 arg.ignores = &ignores;
3793 err = got_fileindex_diff_dir(fileindex, fd,
3794 worktree->root_path, path, repo, &fdiff_cb, &arg);
3796 done:
3797 free_ignores(&ignores);
3798 if (fd != -1 && close(fd) == -1 && err == NULL)
3799 err = got_error_from_errno("close");
3800 free(ondisk_path);
3801 return err;
3804 const struct got_error *
3805 got_worktree_status(struct got_worktree *worktree,
3806 struct got_pathlist_head *paths, struct got_repository *repo,
3807 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3808 got_cancel_cb cancel_cb, void *cancel_arg)
3810 const struct got_error *err = NULL;
3811 char *fileindex_path = NULL;
3812 struct got_fileindex *fileindex = NULL;
3813 struct got_pathlist_entry *pe;
3815 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3816 if (err)
3817 return err;
3819 TAILQ_FOREACH(pe, paths, entry) {
3820 err = worktree_status(worktree, pe->path, fileindex, repo,
3821 status_cb, status_arg, cancel_cb, cancel_arg,
3822 no_ignores, 0);
3823 if (err)
3824 break;
3826 free(fileindex_path);
3827 got_fileindex_free(fileindex);
3828 return err;
3831 const struct got_error *
3832 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3833 const char *arg)
3835 const struct got_error *err = NULL;
3836 char *resolved = NULL, *cwd = NULL, *path = NULL;
3837 size_t len;
3838 struct stat sb;
3839 char *abspath = NULL;
3840 char canonpath[PATH_MAX];
3842 *wt_path = NULL;
3844 cwd = getcwd(NULL, 0);
3845 if (cwd == NULL)
3846 return got_error_from_errno("getcwd");
3848 if (lstat(arg, &sb) == -1) {
3849 if (errno != ENOENT) {
3850 err = got_error_from_errno2("lstat", arg);
3851 goto done;
3853 sb.st_mode = 0;
3855 if (S_ISLNK(sb.st_mode)) {
3857 * We cannot use realpath(3) with symlinks since we want to
3858 * operate on the symlink itself.
3859 * But we can make the path absolute, assuming it is relative
3860 * to the current working directory, and then canonicalize it.
3862 if (!got_path_is_absolute(arg)) {
3863 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3864 err = got_error_from_errno("asprintf");
3865 goto done;
3869 err = got_canonpath(abspath ? abspath : arg, canonpath,
3870 sizeof(canonpath));
3871 if (err)
3872 goto done;
3873 resolved = strdup(canonpath);
3874 if (resolved == NULL) {
3875 err = got_error_from_errno("strdup");
3876 goto done;
3878 } else {
3879 resolved = realpath(arg, NULL);
3880 if (resolved == NULL) {
3881 if (errno != ENOENT) {
3882 err = got_error_from_errno2("realpath", arg);
3883 goto done;
3885 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3886 err = got_error_from_errno("asprintf");
3887 goto done;
3889 err = got_canonpath(abspath, canonpath,
3890 sizeof(canonpath));
3891 if (err)
3892 goto done;
3893 resolved = strdup(canonpath);
3894 if (resolved == NULL) {
3895 err = got_error_from_errno("strdup");
3896 goto done;
3901 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3902 strlen(got_worktree_get_root_path(worktree)))) {
3903 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3904 goto done;
3907 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3908 err = got_path_skip_common_ancestor(&path,
3909 got_worktree_get_root_path(worktree), resolved);
3910 if (err)
3911 goto done;
3912 } else {
3913 path = strdup("");
3914 if (path == NULL) {
3915 err = got_error_from_errno("strdup");
3916 goto done;
3920 /* XXX status walk can't deal with trailing slash! */
3921 len = strlen(path);
3922 while (len > 0 && path[len - 1] == '/') {
3923 path[len - 1] = '\0';
3924 len--;
3926 done:
3927 free(abspath);
3928 free(resolved);
3929 free(cwd);
3930 if (err == NULL)
3931 *wt_path = path;
3932 else
3933 free(path);
3934 return err;
3937 struct schedule_addition_args {
3938 struct got_worktree *worktree;
3939 struct got_fileindex *fileindex;
3940 got_worktree_checkout_cb progress_cb;
3941 void *progress_arg;
3942 struct got_repository *repo;
3945 static const struct got_error *
3946 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3947 const char *relpath, struct got_object_id *blob_id,
3948 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3949 int dirfd, const char *de_name)
3951 struct schedule_addition_args *a = arg;
3952 const struct got_error *err = NULL;
3953 struct got_fileindex_entry *ie;
3954 struct stat sb;
3955 char *ondisk_path;
3957 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3958 relpath) == -1)
3959 return got_error_from_errno("asprintf");
3961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3962 if (ie) {
3963 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3964 de_name, a->repo);
3965 if (err)
3966 goto done;
3967 /* Re-adding an existing entry is a no-op. */
3968 if (status == GOT_STATUS_ADD)
3969 goto done;
3970 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3971 if (err)
3972 goto done;
3975 if (status != GOT_STATUS_UNVERSIONED) {
3976 if (status == GOT_STATUS_NONEXISTENT)
3977 err = got_error_set_errno(ENOENT, ondisk_path);
3978 else
3979 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3980 goto done;
3983 err = got_fileindex_entry_alloc(&ie, relpath);
3984 if (err)
3985 goto done;
3986 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3987 relpath, NULL, NULL, 1);
3988 if (err) {
3989 got_fileindex_entry_free(ie);
3990 goto done;
3992 err = got_fileindex_entry_add(a->fileindex, ie);
3993 if (err) {
3994 got_fileindex_entry_free(ie);
3995 goto done;
3997 done:
3998 free(ondisk_path);
3999 if (err)
4000 return err;
4001 if (status == GOT_STATUS_ADD)
4002 return NULL;
4003 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4006 const struct got_error *
4007 got_worktree_schedule_add(struct got_worktree *worktree,
4008 struct got_pathlist_head *paths,
4009 got_worktree_checkout_cb progress_cb, void *progress_arg,
4010 struct got_repository *repo, int no_ignores)
4012 struct got_fileindex *fileindex = NULL;
4013 char *fileindex_path = NULL;
4014 const struct got_error *err = NULL, *sync_err, *unlockerr;
4015 struct got_pathlist_entry *pe;
4016 struct schedule_addition_args saa;
4018 err = lock_worktree(worktree, LOCK_EX);
4019 if (err)
4020 return err;
4022 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4023 if (err)
4024 goto done;
4026 saa.worktree = worktree;
4027 saa.fileindex = fileindex;
4028 saa.progress_cb = progress_cb;
4029 saa.progress_arg = progress_arg;
4030 saa.repo = repo;
4032 TAILQ_FOREACH(pe, paths, entry) {
4033 err = worktree_status(worktree, pe->path, fileindex, repo,
4034 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4035 if (err)
4036 break;
4038 sync_err = sync_fileindex(fileindex, fileindex_path);
4039 if (sync_err && err == NULL)
4040 err = sync_err;
4041 done:
4042 free(fileindex_path);
4043 if (fileindex)
4044 got_fileindex_free(fileindex);
4045 unlockerr = lock_worktree(worktree, LOCK_SH);
4046 if (unlockerr && err == NULL)
4047 err = unlockerr;
4048 return err;
4051 struct schedule_deletion_args {
4052 struct got_worktree *worktree;
4053 struct got_fileindex *fileindex;
4054 got_worktree_delete_cb progress_cb;
4055 void *progress_arg;
4056 struct got_repository *repo;
4057 int delete_local_mods;
4058 int keep_on_disk;
4059 int ignore_missing_paths;
4060 const char *status_codes;
4063 static const struct got_error *
4064 schedule_for_deletion(void *arg, unsigned char status,
4065 unsigned char staged_status, const char *relpath,
4066 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4067 struct got_object_id *commit_id, int dirfd, const char *de_name)
4069 struct schedule_deletion_args *a = arg;
4070 const struct got_error *err = NULL;
4071 struct got_fileindex_entry *ie = NULL;
4072 struct stat sb;
4073 char *ondisk_path;
4075 if (status == GOT_STATUS_NONEXISTENT) {
4076 if (a->ignore_missing_paths)
4077 return NULL;
4078 return got_error_set_errno(ENOENT, relpath);
4081 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4082 if (ie == NULL)
4083 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4085 staged_status = get_staged_status(ie);
4086 if (staged_status != GOT_STATUS_NO_CHANGE) {
4087 if (staged_status == GOT_STATUS_DELETE)
4088 return NULL;
4089 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4092 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4093 relpath) == -1)
4094 return got_error_from_errno("asprintf");
4096 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4097 a->repo);
4098 if (err)
4099 goto done;
4101 if (a->status_codes) {
4102 size_t ncodes = strlen(a->status_codes);
4103 int i;
4104 for (i = 0; i < ncodes ; i++) {
4105 if (status == a->status_codes[i])
4106 break;
4108 if (i == ncodes) {
4109 /* Do not delete files in non-matching status. */
4110 free(ondisk_path);
4111 return NULL;
4113 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4114 a->status_codes[i] != GOT_STATUS_MISSING) {
4115 static char msg[64];
4116 snprintf(msg, sizeof(msg),
4117 "invalid status code '%c'", a->status_codes[i]);
4118 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4119 goto done;
4123 if (status != GOT_STATUS_NO_CHANGE) {
4124 if (status == GOT_STATUS_DELETE)
4125 goto done;
4126 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4127 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4128 goto done;
4130 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4131 err = got_error_set_errno(ENOENT, relpath);
4132 goto done;
4134 if (status != GOT_STATUS_MODIFY &&
4135 status != GOT_STATUS_MISSING) {
4136 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4137 goto done;
4141 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4142 size_t root_len;
4144 if (dirfd != -1) {
4145 if (unlinkat(dirfd, de_name, 0) == -1) {
4146 err = got_error_from_errno2("unlinkat",
4147 ondisk_path);
4148 goto done;
4150 } else if (unlink(ondisk_path) == -1) {
4151 err = got_error_from_errno2("unlink", ondisk_path);
4152 goto done;
4155 root_len = strlen(a->worktree->root_path);
4156 do {
4157 char *parent;
4158 err = got_path_dirname(&parent, ondisk_path);
4159 if (err)
4160 goto done;
4161 free(ondisk_path);
4162 ondisk_path = parent;
4163 if (rmdir(ondisk_path) == -1) {
4164 if (errno != ENOTEMPTY)
4165 err = got_error_from_errno2("rmdir",
4166 ondisk_path);
4167 break;
4169 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4170 strlen(ondisk_path), root_len) != 0);
4173 got_fileindex_entry_mark_deleted_from_disk(ie);
4174 done:
4175 free(ondisk_path);
4176 if (err)
4177 return err;
4178 if (status == GOT_STATUS_DELETE)
4179 return NULL;
4180 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4181 staged_status, relpath);
4184 const struct got_error *
4185 got_worktree_schedule_delete(struct got_worktree *worktree,
4186 struct got_pathlist_head *paths, int delete_local_mods,
4187 const char *status_codes,
4188 got_worktree_delete_cb progress_cb, void *progress_arg,
4189 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4191 struct got_fileindex *fileindex = NULL;
4192 char *fileindex_path = NULL;
4193 const struct got_error *err = NULL, *sync_err, *unlockerr;
4194 struct got_pathlist_entry *pe;
4195 struct schedule_deletion_args sda;
4197 err = lock_worktree(worktree, LOCK_EX);
4198 if (err)
4199 return err;
4201 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4202 if (err)
4203 goto done;
4205 sda.worktree = worktree;
4206 sda.fileindex = fileindex;
4207 sda.progress_cb = progress_cb;
4208 sda.progress_arg = progress_arg;
4209 sda.repo = repo;
4210 sda.delete_local_mods = delete_local_mods;
4211 sda.keep_on_disk = keep_on_disk;
4212 sda.ignore_missing_paths = ignore_missing_paths;
4213 sda.status_codes = status_codes;
4215 TAILQ_FOREACH(pe, paths, entry) {
4216 err = worktree_status(worktree, pe->path, fileindex, repo,
4217 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4218 if (err)
4219 break;
4221 sync_err = sync_fileindex(fileindex, fileindex_path);
4222 if (sync_err && err == NULL)
4223 err = sync_err;
4224 done:
4225 free(fileindex_path);
4226 if (fileindex)
4227 got_fileindex_free(fileindex);
4228 unlockerr = lock_worktree(worktree, LOCK_SH);
4229 if (unlockerr && err == NULL)
4230 err = unlockerr;
4231 return err;
4234 static const struct got_error *
4235 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4237 const struct got_error *err = NULL;
4238 char *line = NULL;
4239 size_t linesize = 0, n;
4240 ssize_t linelen;
4242 linelen = getline(&line, &linesize, infile);
4243 if (linelen == -1) {
4244 if (ferror(infile)) {
4245 err = got_error_from_errno("getline");
4246 goto done;
4248 return NULL;
4250 if (outfile) {
4251 n = fwrite(line, 1, linelen, outfile);
4252 if (n != linelen) {
4253 err = got_ferror(outfile, GOT_ERR_IO);
4254 goto done;
4257 if (rejectfile) {
4258 n = fwrite(line, 1, linelen, rejectfile);
4259 if (n != linelen)
4260 err = got_ferror(rejectfile, GOT_ERR_IO);
4262 done:
4263 free(line);
4264 return err;
4267 static const struct got_error *
4268 skip_one_line(FILE *f)
4270 char *line = NULL;
4271 size_t linesize = 0;
4272 ssize_t linelen;
4274 linelen = getline(&line, &linesize, f);
4275 if (linelen == -1) {
4276 if (ferror(f))
4277 return got_error_from_errno("getline");
4278 return NULL;
4280 free(line);
4281 return NULL;
4284 static const struct got_error *
4285 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4286 int start_old, int end_old, int start_new, int end_new,
4287 FILE *outfile, FILE *rejectfile)
4289 const struct got_error *err;
4291 /* Copy old file's lines leading up to patch. */
4292 while (!feof(f1) && *line_cur1 < start_old) {
4293 err = copy_one_line(f1, outfile, NULL);
4294 if (err)
4295 return err;
4296 (*line_cur1)++;
4298 /* Skip new file's lines leading up to patch. */
4299 while (!feof(f2) && *line_cur2 < start_new) {
4300 if (rejectfile)
4301 err = copy_one_line(f2, NULL, rejectfile);
4302 else
4303 err = skip_one_line(f2);
4304 if (err)
4305 return err;
4306 (*line_cur2)++;
4308 /* Copy patched lines. */
4309 while (!feof(f2) && *line_cur2 <= end_new) {
4310 err = copy_one_line(f2, outfile, NULL);
4311 if (err)
4312 return err;
4313 (*line_cur2)++;
4315 /* Skip over old file's replaced lines. */
4316 while (!feof(f1) && *line_cur1 <= end_old) {
4317 if (rejectfile)
4318 err = copy_one_line(f1, NULL, rejectfile);
4319 else
4320 err = skip_one_line(f1);
4321 if (err)
4322 return err;
4323 (*line_cur1)++;
4326 return NULL;
4329 static const struct got_error *
4330 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4331 FILE *outfile, FILE *rejectfile)
4333 const struct got_error *err;
4335 if (outfile) {
4336 /* Copy old file's lines until EOF. */
4337 while (!feof(f1)) {
4338 err = copy_one_line(f1, outfile, NULL);
4339 if (err)
4340 return err;
4341 (*line_cur1)++;
4344 if (rejectfile) {
4345 /* Copy new file's lines until EOF. */
4346 while (!feof(f2)) {
4347 err = copy_one_line(f2, NULL, rejectfile);
4348 if (err)
4349 return err;
4350 (*line_cur2)++;
4354 return NULL;
4357 static const struct got_error *
4358 apply_or_reject_change(int *choice, int *nchunks_used,
4359 struct diff_result *diff_result, int n,
4360 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4361 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4362 got_worktree_patch_cb patch_cb, void *patch_arg)
4364 const struct got_error *err = NULL;
4365 struct diff_chunk_context cc = {};
4366 int start_old, end_old, start_new, end_new;
4367 FILE *hunkfile;
4368 struct diff_output_unidiff_state *diff_state;
4369 struct diff_input_info diff_info;
4370 int rc;
4372 *choice = GOT_PATCH_CHOICE_NONE;
4374 /* Get changed line numbers without context lines for copy_change(). */
4375 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4376 start_old = cc.left.start;
4377 end_old = cc.left.end;
4378 start_new = cc.right.start;
4379 end_new = cc.right.end;
4381 /* Get the same change with context lines for display. */
4382 memset(&cc, 0, sizeof(cc));
4383 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4385 memset(&diff_info, 0, sizeof(diff_info));
4386 diff_info.left_path = relpath;
4387 diff_info.right_path = relpath;
4389 diff_state = diff_output_unidiff_state_alloc();
4390 if (diff_state == NULL)
4391 return got_error_set_errno(ENOMEM,
4392 "diff_output_unidiff_state_alloc");
4394 hunkfile = got_opentemp();
4395 if (hunkfile == NULL) {
4396 err = got_error_from_errno("got_opentemp");
4397 goto done;
4400 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4401 diff_result, &cc);
4402 if (rc != DIFF_RC_OK) {
4403 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4404 goto done;
4407 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4408 err = got_ferror(hunkfile, GOT_ERR_IO);
4409 goto done;
4412 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4413 hunkfile, changeno, nchanges);
4414 if (err)
4415 goto done;
4417 switch (*choice) {
4418 case GOT_PATCH_CHOICE_YES:
4419 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4420 end_old, start_new, end_new, outfile, rejectfile);
4421 break;
4422 case GOT_PATCH_CHOICE_NO:
4423 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4424 end_old, start_new, end_new, rejectfile, outfile);
4425 break;
4426 case GOT_PATCH_CHOICE_QUIT:
4427 break;
4428 default:
4429 err = got_error(GOT_ERR_PATCH_CHOICE);
4430 break;
4432 done:
4433 diff_output_unidiff_state_free(diff_state);
4434 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4435 err = got_error_from_errno("fclose");
4436 return err;
4439 struct revert_file_args {
4440 struct got_worktree *worktree;
4441 struct got_fileindex *fileindex;
4442 got_worktree_checkout_cb progress_cb;
4443 void *progress_arg;
4444 got_worktree_patch_cb patch_cb;
4445 void *patch_arg;
4446 struct got_repository *repo;
4447 int unlink_added_files;
4450 static const struct got_error *
4451 create_patched_content(char **path_outfile, int reverse_patch,
4452 struct got_object_id *blob_id, const char *path2,
4453 int dirfd2, const char *de_name2,
4454 const char *relpath, struct got_repository *repo,
4455 got_worktree_patch_cb patch_cb, void *patch_arg)
4457 const struct got_error *err, *free_err;
4458 struct got_blob_object *blob = NULL;
4459 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4460 int fd = -1, fd2 = -1;
4461 char link_target[PATH_MAX];
4462 ssize_t link_len = 0;
4463 char *path1 = NULL, *id_str = NULL;
4464 struct stat sb2;
4465 struct got_diffreg_result *diffreg_result = NULL;
4466 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4467 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4469 *path_outfile = NULL;
4471 err = got_object_id_str(&id_str, blob_id);
4472 if (err)
4473 return err;
4475 if (dirfd2 != -1) {
4476 fd2 = openat(dirfd2, de_name2,
4477 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4478 if (fd2 == -1) {
4479 if (!got_err_open_nofollow_on_symlink()) {
4480 err = got_error_from_errno2("openat", path2);
4481 goto done;
4483 link_len = readlinkat(dirfd2, de_name2,
4484 link_target, sizeof(link_target));
4485 if (link_len == -1) {
4486 return got_error_from_errno2("readlinkat",
4487 path2);
4489 sb2.st_mode = S_IFLNK;
4490 sb2.st_size = link_len;
4492 } else {
4493 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4494 if (fd2 == -1) {
4495 if (!got_err_open_nofollow_on_symlink()) {
4496 err = got_error_from_errno2("open", path2);
4497 goto done;
4499 link_len = readlink(path2, link_target,
4500 sizeof(link_target));
4501 if (link_len == -1)
4502 return got_error_from_errno2("readlink", path2);
4503 sb2.st_mode = S_IFLNK;
4504 sb2.st_size = link_len;
4507 if (fd2 != -1) {
4508 if (fstat(fd2, &sb2) == -1) {
4509 err = got_error_from_errno2("fstat", path2);
4510 goto done;
4513 f2 = fdopen(fd2, "r");
4514 if (f2 == NULL) {
4515 err = got_error_from_errno2("fdopen", path2);
4516 goto done;
4518 fd2 = -1;
4519 } else {
4520 size_t n;
4521 f2 = got_opentemp();
4522 if (f2 == NULL) {
4523 err = got_error_from_errno2("got_opentemp", path2);
4524 goto done;
4526 n = fwrite(link_target, 1, link_len, f2);
4527 if (n != link_len) {
4528 err = got_ferror(f2, GOT_ERR_IO);
4529 goto done;
4531 if (fflush(f2) == EOF) {
4532 err = got_error_from_errno("fflush");
4533 goto done;
4535 rewind(f2);
4538 fd = got_opentempfd();
4539 if (fd == -1) {
4540 err = got_error_from_errno("got_opentempfd");
4541 goto done;
4544 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4545 if (err)
4546 goto done;
4548 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4549 if (err)
4550 goto done;
4552 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4553 if (err)
4554 goto done;
4556 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4557 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4558 if (err)
4559 goto done;
4561 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4562 "");
4563 if (err)
4564 goto done;
4566 if (fseek(f1, 0L, SEEK_SET) == -1)
4567 return got_ferror(f1, GOT_ERR_IO);
4568 if (fseek(f2, 0L, SEEK_SET) == -1)
4569 return got_ferror(f2, GOT_ERR_IO);
4571 /* Count the number of actual changes in the diff result. */
4572 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4573 struct diff_chunk_context cc = {};
4574 diff_chunk_context_load_change(&cc, &nchunks_used,
4575 diffreg_result->result, n, 0);
4576 nchanges++;
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 int choice;
4580 err = apply_or_reject_change(&choice, &nchunks_used,
4581 diffreg_result->result, n, relpath, f1, f2,
4582 &line_cur1, &line_cur2,
4583 reverse_patch ? NULL : outfile,
4584 reverse_patch ? outfile : NULL,
4585 ++i, nchanges, patch_cb, patch_arg);
4586 if (err)
4587 goto done;
4588 if (choice == GOT_PATCH_CHOICE_YES)
4589 have_content = 1;
4590 else if (choice == GOT_PATCH_CHOICE_QUIT)
4591 break;
4593 if (have_content) {
4594 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4595 reverse_patch ? NULL : outfile,
4596 reverse_patch ? outfile : NULL);
4597 if (err)
4598 goto done;
4600 if (!S_ISLNK(sb2.st_mode)) {
4601 mode_t mode;
4603 mode = apply_umask(sb2.st_mode);
4604 if (fchmod(fileno(outfile), mode) == -1) {
4605 err = got_error_from_errno2("fchmod", path2);
4606 goto done;
4610 done:
4611 free(id_str);
4612 if (fd != -1 && close(fd) == -1 && err == NULL)
4613 err = got_error_from_errno("close");
4614 if (blob)
4615 got_object_blob_close(blob);
4616 free_err = got_diffreg_result_free(diffreg_result);
4617 if (err == NULL)
4618 err = free_err;
4619 if (f1 && fclose(f1) == EOF && err == NULL)
4620 err = got_error_from_errno2("fclose", path1);
4621 if (f2 && fclose(f2) == EOF && err == NULL)
4622 err = got_error_from_errno2("fclose", path2);
4623 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4624 err = got_error_from_errno2("close", path2);
4625 if (outfile && fclose(outfile) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", *path_outfile);
4627 if (path1 && unlink(path1) == -1 && err == NULL)
4628 err = got_error_from_errno2("unlink", path1);
4629 if (err || !have_content) {
4630 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4631 err = got_error_from_errno2("unlink", *path_outfile);
4632 free(*path_outfile);
4633 *path_outfile = NULL;
4635 free(path1);
4636 return err;
4639 static const struct got_error *
4640 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4641 const char *relpath, struct got_object_id *blob_id,
4642 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4643 int dirfd, const char *de_name)
4645 struct revert_file_args *a = arg;
4646 const struct got_error *err = NULL;
4647 char *parent_path = NULL;
4648 struct got_fileindex_entry *ie;
4649 struct got_commit_object *base_commit = NULL;
4650 struct got_tree_object *tree = NULL;
4651 struct got_object_id *tree_id = NULL;
4652 const struct got_tree_entry *te = NULL;
4653 char *tree_path = NULL, *te_name;
4654 char *ondisk_path = NULL, *path_content = NULL;
4655 struct got_blob_object *blob = NULL;
4656 int fd = -1;
4658 /* Reverting a staged deletion is a no-op. */
4659 if (status == GOT_STATUS_DELETE &&
4660 staged_status != GOT_STATUS_NO_CHANGE)
4661 return NULL;
4663 if (status == GOT_STATUS_UNVERSIONED)
4664 return (*a->progress_cb)(a->progress_arg,
4665 GOT_STATUS_UNVERSIONED, relpath);
4667 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4668 if (ie == NULL)
4669 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4671 /* Construct in-repository path of tree which contains this blob. */
4672 err = got_path_dirname(&parent_path, ie->path);
4673 if (err) {
4674 if (err->code != GOT_ERR_BAD_PATH)
4675 goto done;
4676 parent_path = strdup("/");
4677 if (parent_path == NULL) {
4678 err = got_error_from_errno("strdup");
4679 goto done;
4682 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4683 tree_path = strdup(parent_path);
4684 if (tree_path == NULL) {
4685 err = got_error_from_errno("strdup");
4686 goto done;
4688 } else {
4689 if (got_path_is_root_dir(parent_path)) {
4690 tree_path = strdup(a->worktree->path_prefix);
4691 if (tree_path == NULL) {
4692 err = got_error_from_errno("strdup");
4693 goto done;
4695 } else {
4696 if (asprintf(&tree_path, "%s/%s",
4697 a->worktree->path_prefix, parent_path) == -1) {
4698 err = got_error_from_errno("asprintf");
4699 goto done;
4704 err = got_object_open_as_commit(&base_commit, a->repo,
4705 a->worktree->base_commit_id);
4706 if (err)
4707 goto done;
4709 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4710 if (err) {
4711 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4712 (status == GOT_STATUS_ADD ||
4713 staged_status == GOT_STATUS_ADD)))
4714 goto done;
4715 } else {
4716 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4717 if (err)
4718 goto done;
4720 err = got_path_basename(&te_name, ie->path);
4721 if (err)
4722 goto done;
4724 te = got_object_tree_find_entry(tree, te_name);
4725 free(te_name);
4726 if (te == NULL && status != GOT_STATUS_ADD &&
4727 staged_status != GOT_STATUS_ADD) {
4728 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4729 goto done;
4733 switch (status) {
4734 case GOT_STATUS_ADD:
4735 if (a->patch_cb) {
4736 int choice = GOT_PATCH_CHOICE_NONE;
4737 err = (*a->patch_cb)(&choice, a->patch_arg,
4738 status, ie->path, NULL, 1, 1);
4739 if (err)
4740 goto done;
4741 if (choice != GOT_PATCH_CHOICE_YES)
4742 break;
4744 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4745 ie->path);
4746 if (err)
4747 goto done;
4748 got_fileindex_entry_remove(a->fileindex, ie);
4749 if (a->unlink_added_files) {
4750 if (asprintf(&ondisk_path, "%s/%s",
4751 got_worktree_get_root_path(a->worktree),
4752 relpath) == -1) {
4753 err = got_error_from_errno("asprintf");
4754 goto done;
4756 if (unlink(ondisk_path) == -1) {
4757 err = got_error_from_errno2("unlink",
4758 ondisk_path);
4759 break;
4762 break;
4763 case GOT_STATUS_DELETE:
4764 if (a->patch_cb) {
4765 int choice = GOT_PATCH_CHOICE_NONE;
4766 err = (*a->patch_cb)(&choice, a->patch_arg,
4767 status, ie->path, NULL, 1, 1);
4768 if (err)
4769 goto done;
4770 if (choice != GOT_PATCH_CHOICE_YES)
4771 break;
4773 /* fall through */
4774 case GOT_STATUS_MODIFY:
4775 case GOT_STATUS_MODE_CHANGE:
4776 case GOT_STATUS_CONFLICT:
4777 case GOT_STATUS_MISSING: {
4778 struct got_object_id id;
4779 if (staged_status == GOT_STATUS_ADD ||
4780 staged_status == GOT_STATUS_MODIFY)
4781 got_fileindex_entry_get_staged_blob_id(&id, ie);
4782 else
4783 got_fileindex_entry_get_blob_id(&id, ie);
4784 fd = got_opentempfd();
4785 if (fd == -1) {
4786 err = got_error_from_errno("got_opentempfd");
4787 goto done;
4790 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4791 if (err)
4792 goto done;
4794 if (asprintf(&ondisk_path, "%s/%s",
4795 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4796 err = got_error_from_errno("asprintf");
4797 goto done;
4800 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4801 status == GOT_STATUS_CONFLICT)) {
4802 int is_bad_symlink = 0;
4803 err = create_patched_content(&path_content, 1, &id,
4804 ondisk_path, dirfd, de_name, ie->path, a->repo,
4805 a->patch_cb, a->patch_arg);
4806 if (err || path_content == NULL)
4807 break;
4808 if (te && S_ISLNK(te->mode)) {
4809 if (unlink(path_content) == -1) {
4810 err = got_error_from_errno2("unlink",
4811 path_content);
4812 break;
4814 err = install_symlink(&is_bad_symlink,
4815 a->worktree, ondisk_path, ie->path,
4816 blob, 0, 1, 0, 0, a->repo,
4817 a->progress_cb, a->progress_arg);
4818 } else {
4819 if (rename(path_content, ondisk_path) == -1) {
4820 err = got_error_from_errno3("rename",
4821 path_content, ondisk_path);
4822 goto done;
4825 } else {
4826 int is_bad_symlink = 0;
4827 if (te && S_ISLNK(te->mode)) {
4828 err = install_symlink(&is_bad_symlink,
4829 a->worktree, ondisk_path, ie->path,
4830 blob, 0, 1, 0, 0, a->repo,
4831 a->progress_cb, a->progress_arg);
4832 } else {
4833 err = install_blob(a->worktree, ondisk_path,
4834 ie->path,
4835 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4836 got_fileindex_perms_to_st(ie), blob,
4837 0, 1, 0, 0, a->repo,
4838 a->progress_cb, a->progress_arg);
4840 if (err)
4841 goto done;
4842 if (status == GOT_STATUS_DELETE ||
4843 status == GOT_STATUS_MODE_CHANGE) {
4844 err = got_fileindex_entry_update(ie,
4845 a->worktree->root_fd, relpath,
4846 blob->id.sha1,
4847 a->worktree->base_commit_id->sha1, 1);
4848 if (err)
4849 goto done;
4851 if (is_bad_symlink) {
4852 got_fileindex_entry_filetype_set(ie,
4853 GOT_FILEIDX_MODE_BAD_SYMLINK);
4856 break;
4858 default:
4859 break;
4861 done:
4862 free(ondisk_path);
4863 free(path_content);
4864 free(parent_path);
4865 free(tree_path);
4866 if (fd != -1 && close(fd) == -1 && err == NULL)
4867 err = got_error_from_errno("close");
4868 if (blob)
4869 got_object_blob_close(blob);
4870 if (tree)
4871 got_object_tree_close(tree);
4872 free(tree_id);
4873 if (base_commit)
4874 got_object_commit_close(base_commit);
4875 return err;
4878 const struct got_error *
4879 got_worktree_revert(struct got_worktree *worktree,
4880 struct got_pathlist_head *paths,
4881 got_worktree_checkout_cb progress_cb, void *progress_arg,
4882 got_worktree_patch_cb patch_cb, void *patch_arg,
4883 struct got_repository *repo)
4885 struct got_fileindex *fileindex = NULL;
4886 char *fileindex_path = NULL;
4887 const struct got_error *err = NULL, *unlockerr = NULL;
4888 const struct got_error *sync_err = NULL;
4889 struct got_pathlist_entry *pe;
4890 struct revert_file_args rfa;
4892 err = lock_worktree(worktree, LOCK_EX);
4893 if (err)
4894 return err;
4896 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4897 if (err)
4898 goto done;
4900 rfa.worktree = worktree;
4901 rfa.fileindex = fileindex;
4902 rfa.progress_cb = progress_cb;
4903 rfa.progress_arg = progress_arg;
4904 rfa.patch_cb = patch_cb;
4905 rfa.patch_arg = patch_arg;
4906 rfa.repo = repo;
4907 rfa.unlink_added_files = 0;
4908 TAILQ_FOREACH(pe, paths, entry) {
4909 err = worktree_status(worktree, pe->path, fileindex, repo,
4910 revert_file, &rfa, NULL, NULL, 1, 0);
4911 if (err)
4912 break;
4914 sync_err = sync_fileindex(fileindex, fileindex_path);
4915 if (sync_err && err == NULL)
4916 err = sync_err;
4917 done:
4918 free(fileindex_path);
4919 if (fileindex)
4920 got_fileindex_free(fileindex);
4921 unlockerr = lock_worktree(worktree, LOCK_SH);
4922 if (unlockerr && err == NULL)
4923 err = unlockerr;
4924 return err;
4927 static void
4928 free_commitable(struct got_commitable *ct)
4930 free(ct->path);
4931 free(ct->in_repo_path);
4932 free(ct->ondisk_path);
4933 free(ct->blob_id);
4934 free(ct->base_blob_id);
4935 free(ct->staged_blob_id);
4936 free(ct->base_commit_id);
4937 free(ct);
4940 struct collect_commitables_arg {
4941 struct got_pathlist_head *commitable_paths;
4942 struct got_repository *repo;
4943 struct got_worktree *worktree;
4944 struct got_fileindex *fileindex;
4945 int have_staged_files;
4946 int allow_bad_symlinks;
4947 int diff_header_shown;
4948 FILE *diff_outfile;
4949 FILE *f1;
4950 FILE *f2;
4954 * Create a file which contains the target path of a symlink so we can feed
4955 * it as content to the diff engine.
4957 static const struct got_error *
4958 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4959 const char *abspath)
4961 const struct got_error *err = NULL;
4962 char target_path[PATH_MAX];
4963 ssize_t target_len, outlen;
4965 *fd = -1;
4967 if (dirfd != -1) {
4968 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4969 if (target_len == -1)
4970 return got_error_from_errno2("readlinkat", abspath);
4971 } else {
4972 target_len = readlink(abspath, target_path, PATH_MAX);
4973 if (target_len == -1)
4974 return got_error_from_errno2("readlink", abspath);
4977 *fd = got_opentempfd();
4978 if (*fd == -1)
4979 return got_error_from_errno("got_opentempfd");
4981 outlen = write(*fd, target_path, target_len);
4982 if (outlen == -1) {
4983 err = got_error_from_errno("got_opentempfd");
4984 goto done;
4987 if (lseek(*fd, 0, SEEK_SET) == -1) {
4988 err = got_error_from_errno2("lseek", abspath);
4989 goto done;
4991 done:
4992 if (err) {
4993 close(*fd);
4994 *fd = -1;
4996 return err;
4999 static const struct got_error *
5000 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5001 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5002 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5004 const struct got_error *err = NULL;
5005 struct got_blob_object *blob1 = NULL;
5006 int fd = -1, fd1 = -1, fd2 = -1;
5007 FILE *ondisk_file = NULL;
5008 char *label1 = NULL;
5009 struct stat sb;
5010 off_t size1 = 0;
5011 int f2_exists = 0;
5012 char *id_str = NULL;
5014 memset(&sb, 0, sizeof(sb));
5016 if (diff_staged) {
5017 if (ct->staged_status != GOT_STATUS_MODIFY &&
5018 ct->staged_status != GOT_STATUS_ADD &&
5019 ct->staged_status != GOT_STATUS_DELETE)
5020 return NULL;
5021 } else {
5022 if (ct->status != GOT_STATUS_MODIFY &&
5023 ct->status != GOT_STATUS_ADD &&
5024 ct->status != GOT_STATUS_DELETE)
5025 return NULL;
5028 err = got_opentemp_truncate(f1);
5029 if (err)
5030 return got_error_from_errno("got_opentemp_truncate");
5031 err = got_opentemp_truncate(f2);
5032 if (err)
5033 return got_error_from_errno("got_opentemp_truncate");
5035 if (!*diff_header_shown) {
5036 err = got_object_id_str(&id_str, worktree->base_commit_id);
5037 if (err)
5038 return err;
5039 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5040 got_worktree_get_root_path(worktree));
5041 fprintf(diff_outfile, "commit - %s\n", id_str);
5042 fprintf(diff_outfile, "path + %s%s\n",
5043 got_worktree_get_root_path(worktree),
5044 diff_staged ? " (staged changes)" : "");
5045 *diff_header_shown = 1;
5048 if (diff_staged) {
5049 const char *label1 = NULL, *label2 = NULL;
5050 switch (ct->staged_status) {
5051 case GOT_STATUS_MODIFY:
5052 label1 = ct->path;
5053 label2 = ct->path;
5054 break;
5055 case GOT_STATUS_ADD:
5056 label2 = ct->path;
5057 break;
5058 case GOT_STATUS_DELETE:
5059 label1 = ct->path;
5060 break;
5061 default:
5062 return got_error(GOT_ERR_FILE_STATUS);
5064 fd1 = got_opentempfd();
5065 if (fd1 == -1) {
5066 err = got_error_from_errno("got_opentempfd");
5067 goto done;
5069 fd2 = got_opentempfd();
5070 if (fd2 == -1) {
5071 err = got_error_from_errno("got_opentempfd");
5072 goto done;
5074 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5075 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5076 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5077 NULL, repo, diff_outfile);
5078 goto done;
5081 fd1 = got_opentempfd();
5082 if (fd1 == -1) {
5083 err = got_error_from_errno("got_opentempfd");
5084 goto done;
5087 if (ct->status != GOT_STATUS_ADD) {
5088 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5089 8192, fd1);
5090 if (err)
5091 goto done;
5094 if (ct->status != GOT_STATUS_DELETE) {
5095 if (dirfd != -1) {
5096 fd = openat(dirfd, de_name,
5097 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5098 if (fd == -1) {
5099 if (!got_err_open_nofollow_on_symlink()) {
5100 err = got_error_from_errno2("openat",
5101 ct->ondisk_path);
5102 goto done;
5104 err = get_symlink_target_file(&fd, dirfd,
5105 de_name, ct->ondisk_path);
5106 if (err)
5107 goto done;
5109 } else {
5110 fd = open(ct->ondisk_path,
5111 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5112 if (fd == -1) {
5113 if (!got_err_open_nofollow_on_symlink()) {
5114 err = got_error_from_errno2("open",
5115 ct->ondisk_path);
5116 goto done;
5118 err = get_symlink_target_file(&fd, dirfd,
5119 de_name, ct->ondisk_path);
5120 if (err)
5121 goto done;
5124 if (fstatat(fd, ct->ondisk_path, &sb,
5125 AT_SYMLINK_NOFOLLOW) == -1) {
5126 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5127 goto done;
5129 ondisk_file = fdopen(fd, "r");
5130 if (ondisk_file == NULL) {
5131 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5132 goto done;
5134 fd = -1;
5135 f2_exists = 1;
5138 if (blob1) {
5139 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5140 f1, blob1);
5141 if (err)
5142 goto done;
5145 err = got_diff_blob_file(blob1, f1, size1, label1,
5146 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5147 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5148 done:
5149 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5150 err = got_error_from_errno("close");
5151 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5152 err = got_error_from_errno("close");
5153 if (blob1)
5154 got_object_blob_close(blob1);
5155 if (fd != -1 && close(fd) == -1 && err == NULL)
5156 err = got_error_from_errno("close");
5157 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5158 err = got_error_from_errno("fclose");
5159 return err;
5162 static const struct got_error *
5163 collect_commitables(void *arg, unsigned char status,
5164 unsigned char staged_status, const char *relpath,
5165 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5166 struct got_object_id *commit_id, int dirfd, const char *de_name)
5168 struct collect_commitables_arg *a = arg;
5169 const struct got_error *err = NULL;
5170 struct got_commitable *ct = NULL;
5171 struct got_pathlist_entry *new = NULL;
5172 char *parent_path = NULL, *path = NULL;
5173 struct stat sb;
5175 if (a->have_staged_files) {
5176 if (staged_status != GOT_STATUS_MODIFY &&
5177 staged_status != GOT_STATUS_ADD &&
5178 staged_status != GOT_STATUS_DELETE)
5179 return NULL;
5180 } else {
5181 if (status == GOT_STATUS_CONFLICT)
5182 return got_error(GOT_ERR_COMMIT_CONFLICT);
5184 if (status != GOT_STATUS_MODIFY &&
5185 status != GOT_STATUS_MODE_CHANGE &&
5186 status != GOT_STATUS_ADD &&
5187 status != GOT_STATUS_DELETE)
5188 return NULL;
5191 if (asprintf(&path, "/%s", relpath) == -1) {
5192 err = got_error_from_errno("asprintf");
5193 goto done;
5195 if (strcmp(path, "/") == 0) {
5196 parent_path = strdup("");
5197 if (parent_path == NULL)
5198 return got_error_from_errno("strdup");
5199 } else {
5200 err = got_path_dirname(&parent_path, path);
5201 if (err)
5202 return err;
5205 ct = calloc(1, sizeof(*ct));
5206 if (ct == NULL) {
5207 err = got_error_from_errno("calloc");
5208 goto done;
5211 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5212 relpath) == -1) {
5213 err = got_error_from_errno("asprintf");
5214 goto done;
5217 if (staged_status == GOT_STATUS_ADD ||
5218 staged_status == GOT_STATUS_MODIFY) {
5219 struct got_fileindex_entry *ie;
5220 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5221 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5222 case GOT_FILEIDX_MODE_REGULAR_FILE:
5223 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5224 ct->mode = S_IFREG;
5225 break;
5226 case GOT_FILEIDX_MODE_SYMLINK:
5227 ct->mode = S_IFLNK;
5228 break;
5229 default:
5230 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5231 goto done;
5233 ct->mode |= got_fileindex_entry_perms_get(ie);
5234 } else if (status != GOT_STATUS_DELETE &&
5235 staged_status != GOT_STATUS_DELETE) {
5236 if (dirfd != -1) {
5237 if (fstatat(dirfd, de_name, &sb,
5238 AT_SYMLINK_NOFOLLOW) == -1) {
5239 err = got_error_from_errno2("fstatat",
5240 ct->ondisk_path);
5241 goto done;
5243 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5244 err = got_error_from_errno2("lstat", ct->ondisk_path);
5245 goto done;
5247 ct->mode = sb.st_mode;
5250 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5251 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5252 relpath) == -1) {
5253 err = got_error_from_errno("asprintf");
5254 goto done;
5257 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5258 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5259 int is_bad_symlink;
5260 char target_path[PATH_MAX];
5261 ssize_t target_len;
5262 target_len = readlink(ct->ondisk_path, target_path,
5263 sizeof(target_path));
5264 if (target_len == -1) {
5265 err = got_error_from_errno2("readlink",
5266 ct->ondisk_path);
5267 goto done;
5269 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5270 target_len, ct->ondisk_path, a->worktree->root_path);
5271 if (err)
5272 goto done;
5273 if (is_bad_symlink) {
5274 err = got_error_path(ct->ondisk_path,
5275 GOT_ERR_BAD_SYMLINK);
5276 goto done;
5281 ct->status = status;
5282 ct->staged_status = staged_status;
5283 ct->blob_id = NULL; /* will be filled in when blob gets created */
5284 if (ct->status != GOT_STATUS_ADD &&
5285 ct->staged_status != GOT_STATUS_ADD) {
5286 ct->base_blob_id = got_object_id_dup(blob_id);
5287 if (ct->base_blob_id == NULL) {
5288 err = got_error_from_errno("got_object_id_dup");
5289 goto done;
5291 ct->base_commit_id = got_object_id_dup(commit_id);
5292 if (ct->base_commit_id == NULL) {
5293 err = got_error_from_errno("got_object_id_dup");
5294 goto done;
5297 if (ct->staged_status == GOT_STATUS_ADD ||
5298 ct->staged_status == GOT_STATUS_MODIFY) {
5299 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5300 if (ct->staged_blob_id == NULL) {
5301 err = got_error_from_errno("got_object_id_dup");
5302 goto done;
5305 ct->path = strdup(path);
5306 if (ct->path == NULL) {
5307 err = got_error_from_errno("strdup");
5308 goto done;
5310 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5311 if (err)
5312 goto done;
5314 if (a->diff_outfile && ct && new != NULL) {
5315 err = append_ct_diff(ct, &a->diff_header_shown,
5316 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5317 a->have_staged_files, a->repo, a->worktree);
5318 if (err)
5319 goto done;
5321 done:
5322 if (ct && (err || new == NULL))
5323 free_commitable(ct);
5324 free(parent_path);
5325 free(path);
5326 return err;
5329 static const struct got_error *write_tree(struct got_object_id **, int *,
5330 struct got_tree_object *, const char *, struct got_pathlist_head *,
5331 got_worktree_status_cb status_cb, void *status_arg,
5332 struct got_repository *);
5334 static const struct got_error *
5335 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5336 struct got_tree_entry *te, const char *parent_path,
5337 struct got_pathlist_head *commitable_paths,
5338 got_worktree_status_cb status_cb, void *status_arg,
5339 struct got_repository *repo)
5341 const struct got_error *err = NULL;
5342 struct got_tree_object *subtree;
5343 char *subpath;
5345 if (asprintf(&subpath, "%s%s%s", parent_path,
5346 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5347 return got_error_from_errno("asprintf");
5349 err = got_object_open_as_tree(&subtree, repo, &te->id);
5350 if (err)
5351 return err;
5353 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5354 commitable_paths, status_cb, status_arg, repo);
5355 got_object_tree_close(subtree);
5356 free(subpath);
5357 return err;
5360 static const struct got_error *
5361 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5363 const struct got_error *err = NULL;
5364 char *ct_parent_path = NULL;
5366 *match = 0;
5368 if (strchr(ct->in_repo_path, '/') == NULL) {
5369 *match = got_path_is_root_dir(path);
5370 return NULL;
5373 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5374 if (err)
5375 return err;
5376 *match = (strcmp(path, ct_parent_path) == 0);
5377 free(ct_parent_path);
5378 return err;
5381 static mode_t
5382 get_ct_file_mode(struct got_commitable *ct)
5384 if (S_ISLNK(ct->mode))
5385 return S_IFLNK;
5387 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5390 static const struct got_error *
5391 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5392 struct got_tree_entry *te, struct got_commitable *ct)
5394 const struct got_error *err = NULL;
5396 *new_te = NULL;
5398 err = got_object_tree_entry_dup(new_te, te);
5399 if (err)
5400 goto done;
5402 (*new_te)->mode = get_ct_file_mode(ct);
5404 if (ct->staged_status == GOT_STATUS_MODIFY)
5405 memcpy(&(*new_te)->id, ct->staged_blob_id,
5406 sizeof((*new_te)->id));
5407 else
5408 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5409 done:
5410 if (err && *new_te) {
5411 free(*new_te);
5412 *new_te = NULL;
5414 return err;
5417 static const struct got_error *
5418 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5419 struct got_commitable *ct)
5421 const struct got_error *err = NULL;
5422 char *ct_name = NULL;
5424 *new_te = NULL;
5426 *new_te = calloc(1, sizeof(**new_te));
5427 if (*new_te == NULL)
5428 return got_error_from_errno("calloc");
5430 err = got_path_basename(&ct_name, ct->path);
5431 if (err)
5432 goto done;
5433 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5434 sizeof((*new_te)->name)) {
5435 err = got_error(GOT_ERR_NO_SPACE);
5436 goto done;
5439 (*new_te)->mode = get_ct_file_mode(ct);
5441 if (ct->staged_status == GOT_STATUS_ADD)
5442 memcpy(&(*new_te)->id, ct->staged_blob_id,
5443 sizeof((*new_te)->id));
5444 else
5445 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5446 done:
5447 free(ct_name);
5448 if (err && *new_te) {
5449 free(*new_te);
5450 *new_te = NULL;
5452 return err;
5455 static const struct got_error *
5456 insert_tree_entry(struct got_tree_entry *new_te,
5457 struct got_pathlist_head *paths)
5459 const struct got_error *err = NULL;
5460 struct got_pathlist_entry *new_pe;
5462 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5463 if (err)
5464 return err;
5465 if (new_pe == NULL)
5466 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5467 return NULL;
5470 static const struct got_error *
5471 report_ct_status(struct got_commitable *ct,
5472 got_worktree_status_cb status_cb, void *status_arg)
5474 const char *ct_path = ct->path;
5475 unsigned char status;
5477 if (status_cb == NULL) /* no commit progress output desired */
5478 return NULL;
5480 while (ct_path[0] == '/')
5481 ct_path++;
5483 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5484 status = ct->staged_status;
5485 else
5486 status = ct->status;
5488 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5489 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5492 static const struct got_error *
5493 match_modified_subtree(int *modified, struct got_tree_entry *te,
5494 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5496 const struct got_error *err = NULL;
5497 struct got_pathlist_entry *pe;
5498 char *te_path;
5500 *modified = 0;
5502 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5503 got_path_is_root_dir(base_tree_path) ? "" : "/",
5504 te->name) == -1)
5505 return got_error_from_errno("asprintf");
5507 TAILQ_FOREACH(pe, commitable_paths, entry) {
5508 struct got_commitable *ct = pe->data;
5509 *modified = got_path_is_child(ct->in_repo_path, te_path,
5510 strlen(te_path));
5511 if (*modified)
5512 break;
5515 free(te_path);
5516 return err;
5519 static const struct got_error *
5520 match_deleted_or_modified_ct(struct got_commitable **ctp,
5521 struct got_tree_entry *te, const char *base_tree_path,
5522 struct got_pathlist_head *commitable_paths)
5524 const struct got_error *err = NULL;
5525 struct got_pathlist_entry *pe;
5527 *ctp = NULL;
5529 TAILQ_FOREACH(pe, commitable_paths, entry) {
5530 struct got_commitable *ct = pe->data;
5531 char *ct_name = NULL;
5532 int path_matches;
5534 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5535 if (ct->status != GOT_STATUS_MODIFY &&
5536 ct->status != GOT_STATUS_MODE_CHANGE &&
5537 ct->status != GOT_STATUS_DELETE)
5538 continue;
5539 } else {
5540 if (ct->staged_status != GOT_STATUS_MODIFY &&
5541 ct->staged_status != GOT_STATUS_DELETE)
5542 continue;
5545 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5546 continue;
5548 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5549 if (err)
5550 return err;
5551 if (!path_matches)
5552 continue;
5554 err = got_path_basename(&ct_name, pe->path);
5555 if (err)
5556 return err;
5558 if (strcmp(te->name, ct_name) != 0) {
5559 free(ct_name);
5560 continue;
5562 free(ct_name);
5564 *ctp = ct;
5565 break;
5568 return err;
5571 static const struct got_error *
5572 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5573 const char *child_path, const char *path_base_tree,
5574 struct got_pathlist_head *commitable_paths,
5575 got_worktree_status_cb status_cb, void *status_arg,
5576 struct got_repository *repo)
5578 const struct got_error *err = NULL;
5579 struct got_tree_entry *new_te;
5580 char *subtree_path;
5581 struct got_object_id *id = NULL;
5582 int nentries;
5584 *new_tep = NULL;
5586 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5587 got_path_is_root_dir(path_base_tree) ? "" : "/",
5588 child_path) == -1)
5589 return got_error_from_errno("asprintf");
5591 new_te = calloc(1, sizeof(*new_te));
5592 if (new_te == NULL)
5593 return got_error_from_errno("calloc");
5594 new_te->mode = S_IFDIR;
5596 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5597 sizeof(new_te->name)) {
5598 err = got_error(GOT_ERR_NO_SPACE);
5599 goto done;
5601 err = write_tree(&id, &nentries, NULL, subtree_path,
5602 commitable_paths, status_cb, status_arg, repo);
5603 if (err) {
5604 free(new_te);
5605 goto done;
5607 memcpy(&new_te->id, id, sizeof(new_te->id));
5608 done:
5609 free(id);
5610 free(subtree_path);
5611 if (err == NULL)
5612 *new_tep = new_te;
5613 return err;
5616 static const struct got_error *
5617 write_tree(struct got_object_id **new_tree_id, int *nentries,
5618 struct got_tree_object *base_tree, const char *path_base_tree,
5619 struct got_pathlist_head *commitable_paths,
5620 got_worktree_status_cb status_cb, void *status_arg,
5621 struct got_repository *repo)
5623 const struct got_error *err = NULL;
5624 struct got_pathlist_head paths;
5625 struct got_tree_entry *te, *new_te = NULL;
5626 struct got_pathlist_entry *pe;
5628 TAILQ_INIT(&paths);
5629 *nentries = 0;
5631 /* Insert, and recurse into, newly added entries first. */
5632 TAILQ_FOREACH(pe, commitable_paths, entry) {
5633 struct got_commitable *ct = pe->data;
5634 char *child_path = NULL, *slash;
5636 if ((ct->status != GOT_STATUS_ADD &&
5637 ct->staged_status != GOT_STATUS_ADD) ||
5638 (ct->flags & GOT_COMMITABLE_ADDED))
5639 continue;
5641 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5642 strlen(path_base_tree)))
5643 continue;
5645 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5646 ct->in_repo_path);
5647 if (err)
5648 goto done;
5650 slash = strchr(child_path, '/');
5651 if (slash == NULL) {
5652 err = alloc_added_blob_tree_entry(&new_te, ct);
5653 if (err)
5654 goto done;
5655 err = report_ct_status(ct, status_cb, status_arg);
5656 if (err)
5657 goto done;
5658 ct->flags |= GOT_COMMITABLE_ADDED;
5659 err = insert_tree_entry(new_te, &paths);
5660 if (err)
5661 goto done;
5662 (*nentries)++;
5663 } else {
5664 *slash = '\0'; /* trim trailing path components */
5665 if (base_tree == NULL ||
5666 got_object_tree_find_entry(base_tree, child_path)
5667 == NULL) {
5668 err = make_subtree_for_added_blob(&new_te,
5669 child_path, path_base_tree,
5670 commitable_paths, status_cb, status_arg,
5671 repo);
5672 if (err)
5673 goto done;
5674 err = insert_tree_entry(new_te, &paths);
5675 if (err)
5676 goto done;
5677 (*nentries)++;
5682 if (base_tree) {
5683 int i, nbase_entries;
5684 /* Handle modified and deleted entries. */
5685 nbase_entries = got_object_tree_get_nentries(base_tree);
5686 for (i = 0; i < nbase_entries; i++) {
5687 struct got_commitable *ct = NULL;
5689 te = got_object_tree_get_entry(base_tree, i);
5690 if (got_object_tree_entry_is_submodule(te)) {
5691 /* Entry is a submodule; just copy it. */
5692 err = got_object_tree_entry_dup(&new_te, te);
5693 if (err)
5694 goto done;
5695 err = insert_tree_entry(new_te, &paths);
5696 if (err)
5697 goto done;
5698 (*nentries)++;
5699 continue;
5702 if (S_ISDIR(te->mode)) {
5703 int modified;
5704 err = got_object_tree_entry_dup(&new_te, te);
5705 if (err)
5706 goto done;
5707 err = match_modified_subtree(&modified, te,
5708 path_base_tree, commitable_paths);
5709 if (err)
5710 goto done;
5711 /* Avoid recursion into unmodified subtrees. */
5712 if (modified) {
5713 struct got_object_id *new_id;
5714 int nsubentries;
5715 err = write_subtree(&new_id,
5716 &nsubentries, te,
5717 path_base_tree, commitable_paths,
5718 status_cb, status_arg, repo);
5719 if (err)
5720 goto done;
5721 if (nsubentries == 0) {
5722 /* All entries were deleted. */
5723 free(new_id);
5724 continue;
5726 memcpy(&new_te->id, new_id,
5727 sizeof(new_te->id));
5728 free(new_id);
5730 err = insert_tree_entry(new_te, &paths);
5731 if (err)
5732 goto done;
5733 (*nentries)++;
5734 continue;
5737 err = match_deleted_or_modified_ct(&ct, te,
5738 path_base_tree, commitable_paths);
5739 if (err)
5740 goto done;
5741 if (ct) {
5742 /* NB: Deleted entries get dropped here. */
5743 if (ct->status == GOT_STATUS_MODIFY ||
5744 ct->status == GOT_STATUS_MODE_CHANGE ||
5745 ct->staged_status == GOT_STATUS_MODIFY) {
5746 err = alloc_modified_blob_tree_entry(
5747 &new_te, te, ct);
5748 if (err)
5749 goto done;
5750 err = insert_tree_entry(new_te, &paths);
5751 if (err)
5752 goto done;
5753 (*nentries)++;
5755 err = report_ct_status(ct, status_cb,
5756 status_arg);
5757 if (err)
5758 goto done;
5759 } else {
5760 /* Entry is unchanged; just copy it. */
5761 err = got_object_tree_entry_dup(&new_te, te);
5762 if (err)
5763 goto done;
5764 err = insert_tree_entry(new_te, &paths);
5765 if (err)
5766 goto done;
5767 (*nentries)++;
5772 /* Write new list of entries; deleted entries have been dropped. */
5773 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5774 done:
5775 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5776 return err;
5779 static const struct got_error *
5780 update_fileindex_after_commit(struct got_worktree *worktree,
5781 struct got_pathlist_head *commitable_paths,
5782 struct got_object_id *new_base_commit_id,
5783 struct got_fileindex *fileindex, int have_staged_files)
5785 const struct got_error *err = NULL;
5786 struct got_pathlist_entry *pe;
5787 char *relpath = NULL;
5789 TAILQ_FOREACH(pe, commitable_paths, entry) {
5790 struct got_fileindex_entry *ie;
5791 struct got_commitable *ct = pe->data;
5793 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5795 err = got_path_skip_common_ancestor(&relpath,
5796 worktree->root_path, ct->ondisk_path);
5797 if (err)
5798 goto done;
5800 if (ie) {
5801 if (ct->status == GOT_STATUS_DELETE ||
5802 ct->staged_status == GOT_STATUS_DELETE) {
5803 got_fileindex_entry_remove(fileindex, ie);
5804 } else if (ct->staged_status == GOT_STATUS_ADD ||
5805 ct->staged_status == GOT_STATUS_MODIFY) {
5806 got_fileindex_entry_stage_set(ie,
5807 GOT_FILEIDX_STAGE_NONE);
5808 got_fileindex_entry_staged_filetype_set(ie, 0);
5810 err = got_fileindex_entry_update(ie,
5811 worktree->root_fd, relpath,
5812 ct->staged_blob_id->sha1,
5813 new_base_commit_id->sha1,
5814 !have_staged_files);
5815 } else
5816 err = got_fileindex_entry_update(ie,
5817 worktree->root_fd, relpath,
5818 ct->blob_id->sha1,
5819 new_base_commit_id->sha1,
5820 !have_staged_files);
5821 } else {
5822 err = got_fileindex_entry_alloc(&ie, pe->path);
5823 if (err)
5824 goto done;
5825 err = got_fileindex_entry_update(ie,
5826 worktree->root_fd, relpath, ct->blob_id->sha1,
5827 new_base_commit_id->sha1, 1);
5828 if (err) {
5829 got_fileindex_entry_free(ie);
5830 goto done;
5832 err = got_fileindex_entry_add(fileindex, ie);
5833 if (err) {
5834 got_fileindex_entry_free(ie);
5835 goto done;
5838 free(relpath);
5839 relpath = NULL;
5841 done:
5842 free(relpath);
5843 return err;
5847 static const struct got_error *
5848 check_out_of_date(const char *in_repo_path, unsigned char status,
5849 unsigned char staged_status, struct got_object_id *base_blob_id,
5850 struct got_object_id *base_commit_id,
5851 struct got_object_id *head_commit_id, struct got_repository *repo,
5852 int ood_errcode)
5854 const struct got_error *err = NULL;
5855 struct got_commit_object *commit = NULL;
5856 struct got_object_id *id = NULL;
5858 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5859 /* Trivial case: base commit == head commit */
5860 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5861 return NULL;
5863 * Ensure file content which local changes were based
5864 * on matches file content in the branch head.
5866 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5867 if (err)
5868 goto done;
5869 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5870 if (err) {
5871 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5872 err = got_error(ood_errcode);
5873 goto done;
5874 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5875 err = got_error(ood_errcode);
5876 } else {
5877 /* Require that added files don't exist in the branch head. */
5878 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5879 if (err)
5880 goto done;
5881 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5882 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5883 goto done;
5884 err = id ? got_error(ood_errcode) : NULL;
5886 done:
5887 free(id);
5888 if (commit)
5889 got_object_commit_close(commit);
5890 return err;
5893 static const struct got_error *
5894 commit_worktree(struct got_object_id **new_commit_id,
5895 struct got_pathlist_head *commitable_paths,
5896 struct got_object_id *head_commit_id,
5897 struct got_object_id *parent_id2,
5898 struct got_worktree *worktree,
5899 const char *author, const char *committer, char *diff_path,
5900 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5901 got_worktree_status_cb status_cb, void *status_arg,
5902 struct got_repository *repo)
5904 const struct got_error *err = NULL, *unlockerr = NULL;
5905 struct got_pathlist_entry *pe;
5906 const char *head_ref_name = NULL;
5907 struct got_commit_object *head_commit = NULL;
5908 struct got_reference *head_ref2 = NULL;
5909 struct got_object_id *head_commit_id2 = NULL;
5910 struct got_tree_object *head_tree = NULL;
5911 struct got_object_id *new_tree_id = NULL;
5912 int nentries, nparents = 0;
5913 struct got_object_id_queue parent_ids;
5914 struct got_object_qid *pid = NULL;
5915 char *logmsg = NULL;
5916 time_t timestamp;
5918 *new_commit_id = NULL;
5920 STAILQ_INIT(&parent_ids);
5922 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5923 if (err)
5924 goto done;
5926 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5927 if (err)
5928 goto done;
5930 if (commit_msg_cb != NULL) {
5931 err = commit_msg_cb(commitable_paths, diff_path,
5932 &logmsg, commit_arg);
5933 if (err)
5934 goto done;
5937 if (logmsg == NULL || strlen(logmsg) == 0) {
5938 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5939 goto done;
5942 /* Create blobs from added and modified files and record their IDs. */
5943 TAILQ_FOREACH(pe, commitable_paths, entry) {
5944 struct got_commitable *ct = pe->data;
5945 char *ondisk_path;
5947 /* Blobs for staged files already exist. */
5948 if (ct->staged_status == GOT_STATUS_ADD ||
5949 ct->staged_status == GOT_STATUS_MODIFY)
5950 continue;
5952 if (ct->status != GOT_STATUS_ADD &&
5953 ct->status != GOT_STATUS_MODIFY &&
5954 ct->status != GOT_STATUS_MODE_CHANGE)
5955 continue;
5957 if (asprintf(&ondisk_path, "%s/%s",
5958 worktree->root_path, pe->path) == -1) {
5959 err = got_error_from_errno("asprintf");
5960 goto done;
5962 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5963 free(ondisk_path);
5964 if (err)
5965 goto done;
5968 /* Recursively write new tree objects. */
5969 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5970 commitable_paths, status_cb, status_arg, repo);
5971 if (err)
5972 goto done;
5974 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5975 if (err)
5976 goto done;
5977 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5978 nparents++;
5979 if (parent_id2) {
5980 err = got_object_qid_alloc(&pid, parent_id2);
5981 if (err)
5982 goto done;
5983 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5984 nparents++;
5986 timestamp = time(NULL);
5987 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5988 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5989 if (logmsg != NULL)
5990 free(logmsg);
5991 if (err)
5992 goto done;
5994 /* Check if a concurrent commit to our branch has occurred. */
5995 head_ref_name = got_worktree_get_head_ref_name(worktree);
5996 if (head_ref_name == NULL) {
5997 err = got_error_from_errno("got_worktree_get_head_ref_name");
5998 goto done;
6000 /* Lock the reference here to prevent concurrent modification. */
6001 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6002 if (err)
6003 goto done;
6004 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6005 if (err)
6006 goto done;
6007 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6008 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6009 goto done;
6011 /* Update branch head in repository. */
6012 err = got_ref_change_ref(head_ref2, *new_commit_id);
6013 if (err)
6014 goto done;
6015 err = got_ref_write(head_ref2, repo);
6016 if (err)
6017 goto done;
6019 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6020 if (err)
6021 goto done;
6023 err = ref_base_commit(worktree, repo);
6024 if (err)
6025 goto done;
6026 done:
6027 got_object_id_queue_free(&parent_ids);
6028 if (head_tree)
6029 got_object_tree_close(head_tree);
6030 if (head_commit)
6031 got_object_commit_close(head_commit);
6032 free(head_commit_id2);
6033 if (head_ref2) {
6034 unlockerr = got_ref_unlock(head_ref2);
6035 if (unlockerr && err == NULL)
6036 err = unlockerr;
6037 got_ref_close(head_ref2);
6039 return err;
6042 static const struct got_error *
6043 check_path_is_commitable(const char *path,
6044 struct got_pathlist_head *commitable_paths)
6046 struct got_pathlist_entry *cpe = NULL;
6047 size_t path_len = strlen(path);
6049 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6050 struct got_commitable *ct = cpe->data;
6051 const char *ct_path = ct->path;
6053 while (ct_path[0] == '/')
6054 ct_path++;
6056 if (strcmp(path, ct_path) == 0 ||
6057 got_path_is_child(ct_path, path, path_len))
6058 break;
6061 if (cpe == NULL)
6062 return got_error_path(path, GOT_ERR_BAD_PATH);
6064 return NULL;
6067 static const struct got_error *
6068 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6070 int *have_staged_files = arg;
6072 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6073 *have_staged_files = 1;
6074 return got_error(GOT_ERR_CANCELLED);
6077 return NULL;
6080 static const struct got_error *
6081 check_non_staged_files(struct got_fileindex *fileindex,
6082 struct got_pathlist_head *paths)
6084 struct got_pathlist_entry *pe;
6085 struct got_fileindex_entry *ie;
6087 TAILQ_FOREACH(pe, paths, entry) {
6088 if (pe->path[0] == '\0')
6089 continue;
6090 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6091 if (ie == NULL)
6092 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6093 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6094 return got_error_path(pe->path,
6095 GOT_ERR_FILE_NOT_STAGED);
6098 return NULL;
6101 const struct got_error *
6102 got_worktree_commit(struct got_object_id **new_commit_id,
6103 struct got_worktree *worktree, struct got_pathlist_head *paths,
6104 const char *author, const char *committer, int allow_bad_symlinks,
6105 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6106 got_worktree_status_cb status_cb, void *status_arg,
6107 struct got_repository *repo)
6109 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6110 struct got_fileindex *fileindex = NULL;
6111 char *fileindex_path = NULL;
6112 struct got_pathlist_head commitable_paths;
6113 struct collect_commitables_arg cc_arg;
6114 struct got_pathlist_entry *pe;
6115 struct got_reference *head_ref = NULL;
6116 struct got_object_id *head_commit_id = NULL;
6117 char *diff_path = NULL;
6118 int have_staged_files = 0;
6120 *new_commit_id = NULL;
6122 memset(&cc_arg, 0, sizeof(cc_arg));
6123 TAILQ_INIT(&commitable_paths);
6125 err = lock_worktree(worktree, LOCK_EX);
6126 if (err)
6127 goto done;
6129 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6130 if (err)
6131 goto done;
6133 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6134 if (err)
6135 goto done;
6137 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6138 if (err)
6139 goto done;
6141 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6142 &have_staged_files);
6143 if (err && err->code != GOT_ERR_CANCELLED)
6144 goto done;
6145 if (have_staged_files) {
6146 err = check_non_staged_files(fileindex, paths);
6147 if (err)
6148 goto done;
6151 cc_arg.commitable_paths = &commitable_paths;
6152 cc_arg.worktree = worktree;
6153 cc_arg.fileindex = fileindex;
6154 cc_arg.repo = repo;
6155 cc_arg.have_staged_files = have_staged_files;
6156 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6157 cc_arg.diff_header_shown = 0;
6158 if (show_diff) {
6159 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6160 GOT_TMPDIR_STR "/got", ".diff");
6161 if (err)
6162 goto done;
6163 cc_arg.f1 = got_opentemp();
6164 if (cc_arg.f1 == NULL) {
6165 err = got_error_from_errno("got_opentemp");
6166 goto done;
6168 cc_arg.f2 = got_opentemp();
6169 if (cc_arg.f2 == NULL) {
6170 err = got_error_from_errno("got_opentemp");
6171 goto done;
6175 TAILQ_FOREACH(pe, paths, entry) {
6176 err = worktree_status(worktree, pe->path, fileindex, repo,
6177 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6178 if (err)
6179 goto done;
6182 if (show_diff) {
6183 if (fflush(cc_arg.diff_outfile) == EOF) {
6184 err = got_error_from_errno("fflush");
6185 goto done;
6189 if (TAILQ_EMPTY(&commitable_paths)) {
6190 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6191 goto done;
6194 TAILQ_FOREACH(pe, paths, entry) {
6195 err = check_path_is_commitable(pe->path, &commitable_paths);
6196 if (err)
6197 goto done;
6200 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6201 struct got_commitable *ct = pe->data;
6202 const char *ct_path = ct->in_repo_path;
6204 while (ct_path[0] == '/')
6205 ct_path++;
6206 err = check_out_of_date(ct_path, ct->status,
6207 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6208 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6209 if (err)
6210 goto done;
6214 err = commit_worktree(new_commit_id, &commitable_paths,
6215 head_commit_id, NULL, worktree, author, committer,
6216 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6217 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6218 if (err)
6219 goto done;
6221 err = update_fileindex_after_commit(worktree, &commitable_paths,
6222 *new_commit_id, fileindex, have_staged_files);
6223 sync_err = sync_fileindex(fileindex, fileindex_path);
6224 if (sync_err && err == NULL)
6225 err = sync_err;
6226 done:
6227 if (fileindex)
6228 got_fileindex_free(fileindex);
6229 free(fileindex_path);
6230 unlockerr = lock_worktree(worktree, LOCK_SH);
6231 if (unlockerr && err == NULL)
6232 err = unlockerr;
6233 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6234 struct got_commitable *ct = pe->data;
6236 free_commitable(ct);
6238 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6239 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6240 err = got_error_from_errno2("unlink", diff_path);
6241 free(diff_path);
6242 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6243 err == NULL)
6244 err = got_error_from_errno("fclose");
6245 return err;
6248 const char *
6249 got_commitable_get_path(struct got_commitable *ct)
6251 return ct->path;
6254 unsigned int
6255 got_commitable_get_status(struct got_commitable *ct)
6257 return ct->status;
6260 struct check_rebase_ok_arg {
6261 struct got_worktree *worktree;
6262 struct got_repository *repo;
6265 static const struct got_error *
6266 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6268 const struct got_error *err = NULL;
6269 struct check_rebase_ok_arg *a = arg;
6270 unsigned char status;
6271 struct stat sb;
6272 char *ondisk_path;
6274 /* Reject rebase of a work tree with mixed base commits. */
6275 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6276 SHA1_DIGEST_LENGTH))
6277 return got_error(GOT_ERR_MIXED_COMMITS);
6279 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6280 == -1)
6281 return got_error_from_errno("asprintf");
6283 /* Reject rebase of a work tree with modified or staged files. */
6284 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6285 free(ondisk_path);
6286 if (err)
6287 return err;
6289 if (status != GOT_STATUS_NO_CHANGE)
6290 return got_error(GOT_ERR_MODIFIED);
6291 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6292 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6294 return NULL;
6297 const struct got_error *
6298 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6299 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6300 struct got_worktree *worktree, struct got_reference *branch,
6301 struct got_repository *repo)
6303 const struct got_error *err = NULL;
6304 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6305 char *branch_ref_name = NULL;
6306 char *fileindex_path = NULL;
6307 struct check_rebase_ok_arg ok_arg;
6308 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6309 struct got_object_id *wt_branch_tip = NULL;
6311 *new_base_branch_ref = NULL;
6312 *tmp_branch = NULL;
6313 *fileindex = NULL;
6315 err = lock_worktree(worktree, LOCK_EX);
6316 if (err)
6317 return err;
6319 err = open_fileindex(fileindex, &fileindex_path, worktree);
6320 if (err)
6321 goto done;
6323 ok_arg.worktree = worktree;
6324 ok_arg.repo = repo;
6325 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6326 &ok_arg);
6327 if (err)
6328 goto done;
6330 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6331 if (err)
6332 goto done;
6334 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6335 if (err)
6336 goto done;
6338 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6339 if (err)
6340 goto done;
6342 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6343 0);
6344 if (err)
6345 goto done;
6347 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6348 if (err)
6349 goto done;
6350 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6351 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6352 goto done;
6355 err = got_ref_alloc_symref(new_base_branch_ref,
6356 new_base_branch_ref_name, wt_branch);
6357 if (err)
6358 goto done;
6359 err = got_ref_write(*new_base_branch_ref, repo);
6360 if (err)
6361 goto done;
6363 /* TODO Lock original branch's ref while rebasing? */
6365 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6366 if (err)
6367 goto done;
6369 err = got_ref_write(branch_ref, repo);
6370 if (err)
6371 goto done;
6373 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6374 worktree->base_commit_id);
6375 if (err)
6376 goto done;
6377 err = got_ref_write(*tmp_branch, repo);
6378 if (err)
6379 goto done;
6381 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6382 if (err)
6383 goto done;
6384 done:
6385 free(fileindex_path);
6386 free(tmp_branch_name);
6387 free(new_base_branch_ref_name);
6388 free(branch_ref_name);
6389 if (branch_ref)
6390 got_ref_close(branch_ref);
6391 if (wt_branch)
6392 got_ref_close(wt_branch);
6393 free(wt_branch_tip);
6394 if (err) {
6395 if (*new_base_branch_ref) {
6396 got_ref_close(*new_base_branch_ref);
6397 *new_base_branch_ref = NULL;
6399 if (*tmp_branch) {
6400 got_ref_close(*tmp_branch);
6401 *tmp_branch = NULL;
6403 if (*fileindex) {
6404 got_fileindex_free(*fileindex);
6405 *fileindex = NULL;
6407 lock_worktree(worktree, LOCK_SH);
6409 return err;
6412 const struct got_error *
6413 got_worktree_rebase_continue(struct got_object_id **commit_id,
6414 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6415 struct got_reference **branch, struct got_fileindex **fileindex,
6416 struct got_worktree *worktree, struct got_repository *repo)
6418 const struct got_error *err;
6419 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6420 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6421 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6422 char *fileindex_path = NULL;
6423 int have_staged_files = 0;
6425 *commit_id = NULL;
6426 *new_base_branch = NULL;
6427 *tmp_branch = NULL;
6428 *branch = NULL;
6429 *fileindex = NULL;
6431 err = lock_worktree(worktree, LOCK_EX);
6432 if (err)
6433 return err;
6435 err = open_fileindex(fileindex, &fileindex_path, worktree);
6436 if (err)
6437 goto done;
6439 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6440 &have_staged_files);
6441 if (err && err->code != GOT_ERR_CANCELLED)
6442 goto done;
6443 if (have_staged_files) {
6444 err = got_error(GOT_ERR_STAGED_PATHS);
6445 goto done;
6448 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6449 if (err)
6450 goto done;
6452 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6453 if (err)
6454 goto done;
6456 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6457 if (err)
6458 goto done;
6460 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6461 if (err)
6462 goto done;
6464 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6465 if (err)
6466 goto done;
6468 err = got_ref_open(branch, repo,
6469 got_ref_get_symref_target(branch_ref), 0);
6470 if (err)
6471 goto done;
6473 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6474 if (err)
6475 goto done;
6477 err = got_ref_resolve(commit_id, repo, commit_ref);
6478 if (err)
6479 goto done;
6481 err = got_ref_open(new_base_branch, repo,
6482 new_base_branch_ref_name, 0);
6483 if (err)
6484 goto done;
6486 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6487 if (err)
6488 goto done;
6489 done:
6490 free(commit_ref_name);
6491 free(branch_ref_name);
6492 free(fileindex_path);
6493 if (commit_ref)
6494 got_ref_close(commit_ref);
6495 if (branch_ref)
6496 got_ref_close(branch_ref);
6497 if (err) {
6498 free(*commit_id);
6499 *commit_id = NULL;
6500 if (*tmp_branch) {
6501 got_ref_close(*tmp_branch);
6502 *tmp_branch = NULL;
6504 if (*new_base_branch) {
6505 got_ref_close(*new_base_branch);
6506 *new_base_branch = NULL;
6508 if (*branch) {
6509 got_ref_close(*branch);
6510 *branch = NULL;
6512 if (*fileindex) {
6513 got_fileindex_free(*fileindex);
6514 *fileindex = NULL;
6516 lock_worktree(worktree, LOCK_SH);
6518 return err;
6521 const struct got_error *
6522 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6524 const struct got_error *err;
6525 char *tmp_branch_name = NULL;
6527 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6528 if (err)
6529 return err;
6531 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6532 free(tmp_branch_name);
6533 return NULL;
6536 static const struct got_error *
6537 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6538 const char *diff_path, char **logmsg, void *arg)
6540 *logmsg = arg;
6541 return NULL;
6544 static const struct got_error *
6545 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6546 const char *path, struct got_object_id *blob_id,
6547 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6548 int dirfd, const char *de_name)
6550 return NULL;
6553 struct collect_merged_paths_arg {
6554 got_worktree_checkout_cb progress_cb;
6555 void *progress_arg;
6556 struct got_pathlist_head *merged_paths;
6559 static const struct got_error *
6560 collect_merged_paths(void *arg, unsigned char status, const char *path)
6562 const struct got_error *err;
6563 struct collect_merged_paths_arg *a = arg;
6564 char *p;
6565 struct got_pathlist_entry *new;
6567 err = (*a->progress_cb)(a->progress_arg, status, path);
6568 if (err)
6569 return err;
6571 if (status != GOT_STATUS_MERGE &&
6572 status != GOT_STATUS_ADD &&
6573 status != GOT_STATUS_DELETE &&
6574 status != GOT_STATUS_CONFLICT)
6575 return NULL;
6577 p = strdup(path);
6578 if (p == NULL)
6579 return got_error_from_errno("strdup");
6581 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6582 if (err || new == NULL)
6583 free(p);
6584 return err;
6587 static const struct got_error *
6588 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6589 int is_rebase, struct got_repository *repo)
6591 const struct got_error *err;
6592 struct got_reference *commit_ref = NULL;
6594 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6595 if (err) {
6596 if (err->code != GOT_ERR_NOT_REF)
6597 goto done;
6598 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6599 if (err)
6600 goto done;
6601 err = got_ref_write(commit_ref, repo);
6602 if (err)
6603 goto done;
6604 } else if (is_rebase) {
6605 struct got_object_id *stored_id;
6606 int cmp;
6608 err = got_ref_resolve(&stored_id, repo, commit_ref);
6609 if (err)
6610 goto done;
6611 cmp = got_object_id_cmp(commit_id, stored_id);
6612 free(stored_id);
6613 if (cmp != 0) {
6614 err = got_error(GOT_ERR_REBASE_COMMITID);
6615 goto done;
6618 done:
6619 if (commit_ref)
6620 got_ref_close(commit_ref);
6621 return err;
6624 static const struct got_error *
6625 rebase_merge_files(struct got_pathlist_head *merged_paths,
6626 const char *commit_ref_name, struct got_worktree *worktree,
6627 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6628 struct got_object_id *commit_id, struct got_repository *repo,
6629 got_worktree_checkout_cb progress_cb, void *progress_arg,
6630 got_cancel_cb cancel_cb, void *cancel_arg)
6632 const struct got_error *err;
6633 struct got_reference *commit_ref = NULL;
6634 struct collect_merged_paths_arg cmp_arg;
6635 char *fileindex_path;
6637 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6639 err = get_fileindex_path(&fileindex_path, worktree);
6640 if (err)
6641 return err;
6643 cmp_arg.progress_cb = progress_cb;
6644 cmp_arg.progress_arg = progress_arg;
6645 cmp_arg.merged_paths = merged_paths;
6646 err = merge_files(worktree, fileindex, fileindex_path,
6647 parent_commit_id, commit_id, repo, collect_merged_paths,
6648 &cmp_arg, cancel_cb, cancel_arg);
6649 if (commit_ref)
6650 got_ref_close(commit_ref);
6651 return err;
6654 const struct got_error *
6655 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6656 struct got_worktree *worktree, struct got_fileindex *fileindex,
6657 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6658 struct got_repository *repo,
6659 got_worktree_checkout_cb progress_cb, void *progress_arg,
6660 got_cancel_cb cancel_cb, void *cancel_arg)
6662 const struct got_error *err;
6663 char *commit_ref_name;
6665 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6666 if (err)
6667 return err;
6669 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6670 if (err)
6671 goto done;
6673 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6674 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6675 progress_arg, cancel_cb, cancel_arg);
6676 done:
6677 free(commit_ref_name);
6678 return err;
6681 const struct got_error *
6682 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6683 struct got_worktree *worktree, struct got_fileindex *fileindex,
6684 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6685 struct got_repository *repo,
6686 got_worktree_checkout_cb progress_cb, void *progress_arg,
6687 got_cancel_cb cancel_cb, void *cancel_arg)
6689 const struct got_error *err;
6690 char *commit_ref_name;
6692 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6693 if (err)
6694 return err;
6696 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6697 if (err)
6698 goto done;
6700 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6701 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6702 progress_arg, cancel_cb, cancel_arg);
6703 done:
6704 free(commit_ref_name);
6705 return err;
6708 static const struct got_error *
6709 rebase_commit(struct got_object_id **new_commit_id,
6710 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6711 struct got_worktree *worktree, struct got_fileindex *fileindex,
6712 struct got_reference *tmp_branch, const char *committer,
6713 struct got_commit_object *orig_commit, const char *new_logmsg,
6714 struct got_repository *repo)
6716 const struct got_error *err, *sync_err;
6717 struct got_pathlist_head commitable_paths;
6718 struct collect_commitables_arg cc_arg;
6719 char *fileindex_path = NULL;
6720 struct got_reference *head_ref = NULL;
6721 struct got_object_id *head_commit_id = NULL;
6722 char *logmsg = NULL;
6724 memset(&cc_arg, 0, sizeof(cc_arg));
6725 TAILQ_INIT(&commitable_paths);
6726 *new_commit_id = NULL;
6728 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6730 err = get_fileindex_path(&fileindex_path, worktree);
6731 if (err)
6732 return err;
6734 cc_arg.commitable_paths = &commitable_paths;
6735 cc_arg.worktree = worktree;
6736 cc_arg.repo = repo;
6737 cc_arg.have_staged_files = 0;
6739 * If possible get the status of individual files directly to
6740 * avoid crawling the entire work tree once per rebased commit.
6742 * Ideally, merged_paths would contain a list of commitables
6743 * we could use so we could skip worktree_status() entirely.
6744 * However, we would then need carefully keep track of cumulative
6745 * effects of operations such as file additions and deletions
6746 * in 'got histedit -f' (folding multiple commits into one),
6747 * and this extra complexity is not really worth it.
6749 if (merged_paths) {
6750 struct got_pathlist_entry *pe;
6751 TAILQ_FOREACH(pe, merged_paths, entry) {
6752 err = worktree_status(worktree, pe->path, fileindex,
6753 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6754 0);
6755 if (err)
6756 goto done;
6758 } else {
6759 err = worktree_status(worktree, "", fileindex, repo,
6760 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6761 if (err)
6762 goto done;
6765 if (TAILQ_EMPTY(&commitable_paths)) {
6766 /* No-op change; commit will be elided. */
6767 err = got_ref_delete(commit_ref, repo);
6768 if (err)
6769 goto done;
6770 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6771 goto done;
6774 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6775 if (err)
6776 goto done;
6778 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6779 if (err)
6780 goto done;
6782 if (new_logmsg) {
6783 logmsg = strdup(new_logmsg);
6784 if (logmsg == NULL) {
6785 err = got_error_from_errno("strdup");
6786 goto done;
6788 } else {
6789 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6790 if (err)
6791 goto done;
6794 /* NB: commit_worktree will call free(logmsg) */
6795 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6796 NULL, worktree, got_object_commit_get_author(orig_commit),
6797 committer ? committer :
6798 got_object_commit_get_committer(orig_commit), NULL,
6799 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6800 if (err)
6801 goto done;
6803 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6804 if (err)
6805 goto done;
6807 err = got_ref_delete(commit_ref, repo);
6808 if (err)
6809 goto done;
6811 err = update_fileindex_after_commit(worktree, &commitable_paths,
6812 *new_commit_id, fileindex, 0);
6813 sync_err = sync_fileindex(fileindex, fileindex_path);
6814 if (sync_err && err == NULL)
6815 err = sync_err;
6816 done:
6817 free(fileindex_path);
6818 free(head_commit_id);
6819 if (head_ref)
6820 got_ref_close(head_ref);
6821 if (err) {
6822 free(*new_commit_id);
6823 *new_commit_id = NULL;
6825 return err;
6828 const struct got_error *
6829 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6830 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6831 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6832 const char *committer, struct got_commit_object *orig_commit,
6833 struct got_object_id *orig_commit_id, struct got_repository *repo)
6835 const struct got_error *err;
6836 char *commit_ref_name;
6837 struct got_reference *commit_ref = NULL;
6838 struct got_object_id *commit_id = NULL;
6840 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6841 if (err)
6842 return err;
6844 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6845 if (err)
6846 goto done;
6847 err = got_ref_resolve(&commit_id, repo, commit_ref);
6848 if (err)
6849 goto done;
6850 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6851 err = got_error(GOT_ERR_REBASE_COMMITID);
6852 goto done;
6855 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6856 worktree, fileindex, tmp_branch, committer, orig_commit,
6857 NULL, repo);
6858 done:
6859 if (commit_ref)
6860 got_ref_close(commit_ref);
6861 free(commit_ref_name);
6862 free(commit_id);
6863 return err;
6866 const struct got_error *
6867 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6868 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6869 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6870 const char *committer, struct got_commit_object *orig_commit,
6871 struct got_object_id *orig_commit_id, const char *new_logmsg,
6872 struct got_repository *repo)
6874 const struct got_error *err;
6875 char *commit_ref_name;
6876 struct got_reference *commit_ref = NULL;
6878 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6879 if (err)
6880 return err;
6882 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6883 if (err)
6884 goto done;
6886 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6887 worktree, fileindex, tmp_branch, committer, orig_commit,
6888 new_logmsg, repo);
6889 done:
6890 if (commit_ref)
6891 got_ref_close(commit_ref);
6892 free(commit_ref_name);
6893 return err;
6896 const struct got_error *
6897 got_worktree_rebase_postpone(struct got_worktree *worktree,
6898 struct got_fileindex *fileindex)
6900 if (fileindex)
6901 got_fileindex_free(fileindex);
6902 return lock_worktree(worktree, LOCK_SH);
6905 static const struct got_error *
6906 delete_ref(const char *name, struct got_repository *repo)
6908 const struct got_error *err;
6909 struct got_reference *ref;
6911 err = got_ref_open(&ref, repo, name, 0);
6912 if (err) {
6913 if (err->code == GOT_ERR_NOT_REF)
6914 return NULL;
6915 return err;
6918 err = got_ref_delete(ref, repo);
6919 got_ref_close(ref);
6920 return err;
6923 static const struct got_error *
6924 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6926 const struct got_error *err;
6927 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6928 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6930 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6931 if (err)
6932 goto done;
6933 err = delete_ref(tmp_branch_name, repo);
6934 if (err)
6935 goto done;
6937 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6938 if (err)
6939 goto done;
6940 err = delete_ref(new_base_branch_ref_name, repo);
6941 if (err)
6942 goto done;
6944 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6945 if (err)
6946 goto done;
6947 err = delete_ref(branch_ref_name, repo);
6948 if (err)
6949 goto done;
6951 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6952 if (err)
6953 goto done;
6954 err = delete_ref(commit_ref_name, repo);
6955 if (err)
6956 goto done;
6958 done:
6959 free(tmp_branch_name);
6960 free(new_base_branch_ref_name);
6961 free(branch_ref_name);
6962 free(commit_ref_name);
6963 return err;
6966 static const struct got_error *
6967 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6968 struct got_object_id *new_commit_id, struct got_repository *repo)
6970 const struct got_error *err;
6971 struct got_reference *ref = NULL;
6972 struct got_object_id *old_commit_id = NULL;
6973 const char *branch_name = NULL;
6974 char *new_id_str = NULL;
6975 char *refname = NULL;
6977 branch_name = got_ref_get_name(branch);
6978 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6979 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6980 branch_name += 11;
6982 err = got_object_id_str(&new_id_str, new_commit_id);
6983 if (err)
6984 return err;
6986 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6987 new_id_str) == -1) {
6988 err = got_error_from_errno("asprintf");
6989 goto done;
6992 err = got_ref_resolve(&old_commit_id, repo, branch);
6993 if (err)
6994 goto done;
6996 err = got_ref_alloc(&ref, refname, old_commit_id);
6997 if (err)
6998 goto done;
7000 err = got_ref_write(ref, repo);
7001 done:
7002 free(new_id_str);
7003 free(refname);
7004 free(old_commit_id);
7005 if (ref)
7006 got_ref_close(ref);
7007 return err;
7010 const struct got_error *
7011 got_worktree_rebase_complete(struct got_worktree *worktree,
7012 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7013 struct got_reference *rebased_branch, struct got_repository *repo,
7014 int create_backup)
7016 const struct got_error *err, *unlockerr, *sync_err;
7017 struct got_object_id *new_head_commit_id = NULL;
7018 char *fileindex_path = NULL;
7020 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7021 if (err)
7022 return err;
7024 if (create_backup) {
7025 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7026 rebased_branch, new_head_commit_id, repo);
7027 if (err)
7028 goto done;
7031 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7032 if (err)
7033 goto done;
7035 err = got_ref_write(rebased_branch, repo);
7036 if (err)
7037 goto done;
7039 err = got_worktree_set_head_ref(worktree, rebased_branch);
7040 if (err)
7041 goto done;
7043 err = delete_rebase_refs(worktree, repo);
7044 if (err)
7045 goto done;
7047 err = get_fileindex_path(&fileindex_path, worktree);
7048 if (err)
7049 goto done;
7050 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7051 sync_err = sync_fileindex(fileindex, fileindex_path);
7052 if (sync_err && err == NULL)
7053 err = sync_err;
7054 done:
7055 got_fileindex_free(fileindex);
7056 free(fileindex_path);
7057 free(new_head_commit_id);
7058 unlockerr = lock_worktree(worktree, LOCK_SH);
7059 if (unlockerr && err == NULL)
7060 err = unlockerr;
7061 return err;
7064 const struct got_error *
7065 got_worktree_rebase_abort(struct got_worktree *worktree,
7066 struct got_fileindex *fileindex, struct got_repository *repo,
7067 struct got_reference *new_base_branch,
7068 got_worktree_checkout_cb progress_cb, void *progress_arg)
7070 const struct got_error *err, *unlockerr, *sync_err;
7071 struct got_reference *resolved = NULL;
7072 struct got_object_id *commit_id = NULL;
7073 struct got_commit_object *commit = NULL;
7074 char *fileindex_path = NULL;
7075 struct revert_file_args rfa;
7076 struct got_object_id *tree_id = NULL;
7078 err = lock_worktree(worktree, LOCK_EX);
7079 if (err)
7080 return err;
7082 err = got_object_open_as_commit(&commit, repo,
7083 worktree->base_commit_id);
7084 if (err)
7085 goto done;
7087 err = got_ref_open(&resolved, repo,
7088 got_ref_get_symref_target(new_base_branch), 0);
7089 if (err)
7090 goto done;
7092 err = got_worktree_set_head_ref(worktree, resolved);
7093 if (err)
7094 goto done;
7097 * XXX commits to the base branch could have happened while
7098 * we were busy rebasing; should we store the original commit ID
7099 * when rebase begins and read it back here?
7101 err = got_ref_resolve(&commit_id, repo, resolved);
7102 if (err)
7103 goto done;
7105 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7106 if (err)
7107 goto done;
7109 err = got_object_id_by_path(&tree_id, repo, commit,
7110 worktree->path_prefix);
7111 if (err)
7112 goto done;
7114 err = delete_rebase_refs(worktree, repo);
7115 if (err)
7116 goto done;
7118 err = get_fileindex_path(&fileindex_path, worktree);
7119 if (err)
7120 goto done;
7122 rfa.worktree = worktree;
7123 rfa.fileindex = fileindex;
7124 rfa.progress_cb = progress_cb;
7125 rfa.progress_arg = progress_arg;
7126 rfa.patch_cb = NULL;
7127 rfa.patch_arg = NULL;
7128 rfa.repo = repo;
7129 rfa.unlink_added_files = 0;
7130 err = worktree_status(worktree, "", fileindex, repo,
7131 revert_file, &rfa, NULL, NULL, 1, 0);
7132 if (err)
7133 goto sync;
7135 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7136 repo, progress_cb, progress_arg, NULL, NULL);
7137 sync:
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7140 err = sync_err;
7141 done:
7142 got_ref_close(resolved);
7143 free(tree_id);
7144 free(commit_id);
7145 if (commit)
7146 got_object_commit_close(commit);
7147 if (fileindex)
7148 got_fileindex_free(fileindex);
7149 free(fileindex_path);
7151 unlockerr = lock_worktree(worktree, LOCK_SH);
7152 if (unlockerr && err == NULL)
7153 err = unlockerr;
7154 return err;
7157 const struct got_error *
7158 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7159 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7160 struct got_fileindex **fileindex, struct got_worktree *worktree,
7161 struct got_repository *repo)
7163 const struct got_error *err = NULL;
7164 char *tmp_branch_name = NULL;
7165 char *branch_ref_name = NULL;
7166 char *base_commit_ref_name = NULL;
7167 char *fileindex_path = NULL;
7168 struct check_rebase_ok_arg ok_arg;
7169 struct got_reference *wt_branch = NULL;
7170 struct got_reference *base_commit_ref = NULL;
7172 *tmp_branch = NULL;
7173 *branch_ref = NULL;
7174 *base_commit_id = NULL;
7175 *fileindex = NULL;
7177 err = lock_worktree(worktree, LOCK_EX);
7178 if (err)
7179 return err;
7181 err = open_fileindex(fileindex, &fileindex_path, worktree);
7182 if (err)
7183 goto done;
7185 ok_arg.worktree = worktree;
7186 ok_arg.repo = repo;
7187 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7188 &ok_arg);
7189 if (err)
7190 goto done;
7192 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7193 if (err)
7194 goto done;
7196 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7197 if (err)
7198 goto done;
7200 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7201 worktree);
7202 if (err)
7203 goto done;
7205 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7206 0);
7207 if (err)
7208 goto done;
7210 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7211 if (err)
7212 goto done;
7214 err = got_ref_write(*branch_ref, repo);
7215 if (err)
7216 goto done;
7218 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7219 worktree->base_commit_id);
7220 if (err)
7221 goto done;
7222 err = got_ref_write(base_commit_ref, repo);
7223 if (err)
7224 goto done;
7225 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7226 if (*base_commit_id == NULL) {
7227 err = got_error_from_errno("got_object_id_dup");
7228 goto done;
7231 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7232 worktree->base_commit_id);
7233 if (err)
7234 goto done;
7235 err = got_ref_write(*tmp_branch, repo);
7236 if (err)
7237 goto done;
7239 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7240 if (err)
7241 goto done;
7242 done:
7243 free(fileindex_path);
7244 free(tmp_branch_name);
7245 free(branch_ref_name);
7246 free(base_commit_ref_name);
7247 if (wt_branch)
7248 got_ref_close(wt_branch);
7249 if (err) {
7250 if (*branch_ref) {
7251 got_ref_close(*branch_ref);
7252 *branch_ref = NULL;
7254 if (*tmp_branch) {
7255 got_ref_close(*tmp_branch);
7256 *tmp_branch = NULL;
7258 free(*base_commit_id);
7259 if (*fileindex) {
7260 got_fileindex_free(*fileindex);
7261 *fileindex = NULL;
7263 lock_worktree(worktree, LOCK_SH);
7265 return err;
7268 const struct got_error *
7269 got_worktree_histedit_postpone(struct got_worktree *worktree,
7270 struct got_fileindex *fileindex)
7272 if (fileindex)
7273 got_fileindex_free(fileindex);
7274 return lock_worktree(worktree, LOCK_SH);
7277 const struct got_error *
7278 got_worktree_histedit_in_progress(int *in_progress,
7279 struct got_worktree *worktree)
7281 const struct got_error *err;
7282 char *tmp_branch_name = NULL;
7284 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7285 if (err)
7286 return err;
7288 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7289 free(tmp_branch_name);
7290 return NULL;
7293 const struct got_error *
7294 got_worktree_histedit_continue(struct got_object_id **commit_id,
7295 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7296 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7297 struct got_worktree *worktree, struct got_repository *repo)
7299 const struct got_error *err;
7300 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7301 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7302 struct got_reference *commit_ref = NULL;
7303 struct got_reference *base_commit_ref = NULL;
7304 char *fileindex_path = NULL;
7305 int have_staged_files = 0;
7307 *commit_id = NULL;
7308 *tmp_branch = NULL;
7309 *base_commit_id = NULL;
7310 *fileindex = NULL;
7312 err = lock_worktree(worktree, LOCK_EX);
7313 if (err)
7314 return err;
7316 err = open_fileindex(fileindex, &fileindex_path, worktree);
7317 if (err)
7318 goto done;
7320 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7321 &have_staged_files);
7322 if (err && err->code != GOT_ERR_CANCELLED)
7323 goto done;
7324 if (have_staged_files) {
7325 err = got_error(GOT_ERR_STAGED_PATHS);
7326 goto done;
7329 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7330 if (err)
7331 goto done;
7333 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7334 if (err)
7335 goto done;
7337 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7338 if (err)
7339 goto done;
7341 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7342 worktree);
7343 if (err)
7344 goto done;
7346 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7347 if (err)
7348 goto done;
7350 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7351 if (err)
7352 goto done;
7353 err = got_ref_resolve(commit_id, repo, commit_ref);
7354 if (err)
7355 goto done;
7357 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7358 if (err)
7359 goto done;
7360 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7361 if (err)
7362 goto done;
7364 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7365 if (err)
7366 goto done;
7367 done:
7368 free(commit_ref_name);
7369 free(branch_ref_name);
7370 free(fileindex_path);
7371 if (commit_ref)
7372 got_ref_close(commit_ref);
7373 if (base_commit_ref)
7374 got_ref_close(base_commit_ref);
7375 if (err) {
7376 free(*commit_id);
7377 *commit_id = NULL;
7378 free(*base_commit_id);
7379 *base_commit_id = NULL;
7380 if (*tmp_branch) {
7381 got_ref_close(*tmp_branch);
7382 *tmp_branch = NULL;
7384 if (*fileindex) {
7385 got_fileindex_free(*fileindex);
7386 *fileindex = NULL;
7388 lock_worktree(worktree, LOCK_EX);
7390 return err;
7393 static const struct got_error *
7394 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7396 const struct got_error *err;
7397 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7398 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7400 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7401 if (err)
7402 goto done;
7403 err = delete_ref(tmp_branch_name, repo);
7404 if (err)
7405 goto done;
7407 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7408 worktree);
7409 if (err)
7410 goto done;
7411 err = delete_ref(base_commit_ref_name, repo);
7412 if (err)
7413 goto done;
7415 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7416 if (err)
7417 goto done;
7418 err = delete_ref(branch_ref_name, repo);
7419 if (err)
7420 goto done;
7422 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7423 if (err)
7424 goto done;
7425 err = delete_ref(commit_ref_name, repo);
7426 if (err)
7427 goto done;
7428 done:
7429 free(tmp_branch_name);
7430 free(base_commit_ref_name);
7431 free(branch_ref_name);
7432 free(commit_ref_name);
7433 return err;
7436 const struct got_error *
7437 got_worktree_histedit_abort(struct got_worktree *worktree,
7438 struct got_fileindex *fileindex, struct got_repository *repo,
7439 struct got_reference *branch, struct got_object_id *base_commit_id,
7440 got_worktree_checkout_cb progress_cb, void *progress_arg)
7442 const struct got_error *err, *unlockerr, *sync_err;
7443 struct got_reference *resolved = NULL;
7444 char *fileindex_path = NULL;
7445 struct got_commit_object *commit = NULL;
7446 struct got_object_id *tree_id = NULL;
7447 struct revert_file_args rfa;
7449 err = lock_worktree(worktree, LOCK_EX);
7450 if (err)
7451 return err;
7453 err = got_object_open_as_commit(&commit, repo,
7454 worktree->base_commit_id);
7455 if (err)
7456 goto done;
7458 err = got_ref_open(&resolved, repo,
7459 got_ref_get_symref_target(branch), 0);
7460 if (err)
7461 goto done;
7463 err = got_worktree_set_head_ref(worktree, resolved);
7464 if (err)
7465 goto done;
7467 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7468 if (err)
7469 goto done;
7471 err = got_object_id_by_path(&tree_id, repo, commit,
7472 worktree->path_prefix);
7473 if (err)
7474 goto done;
7476 err = delete_histedit_refs(worktree, repo);
7477 if (err)
7478 goto done;
7480 err = get_fileindex_path(&fileindex_path, worktree);
7481 if (err)
7482 goto done;
7484 rfa.worktree = worktree;
7485 rfa.fileindex = fileindex;
7486 rfa.progress_cb = progress_cb;
7487 rfa.progress_arg = progress_arg;
7488 rfa.patch_cb = NULL;
7489 rfa.patch_arg = NULL;
7490 rfa.repo = repo;
7491 rfa.unlink_added_files = 0;
7492 err = worktree_status(worktree, "", fileindex, repo,
7493 revert_file, &rfa, NULL, NULL, 1, 0);
7494 if (err)
7495 goto sync;
7497 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7498 repo, progress_cb, progress_arg, NULL, NULL);
7499 sync:
7500 sync_err = sync_fileindex(fileindex, fileindex_path);
7501 if (sync_err && err == NULL)
7502 err = sync_err;
7503 done:
7504 got_ref_close(resolved);
7505 free(tree_id);
7506 free(fileindex_path);
7508 unlockerr = lock_worktree(worktree, LOCK_SH);
7509 if (unlockerr && err == NULL)
7510 err = unlockerr;
7511 return err;
7514 const struct got_error *
7515 got_worktree_histedit_complete(struct got_worktree *worktree,
7516 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7517 struct got_reference *edited_branch, struct got_repository *repo)
7519 const struct got_error *err, *unlockerr, *sync_err;
7520 struct got_object_id *new_head_commit_id = NULL;
7521 struct got_reference *resolved = NULL;
7522 char *fileindex_path = NULL;
7524 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7525 if (err)
7526 return err;
7528 err = got_ref_open(&resolved, repo,
7529 got_ref_get_symref_target(edited_branch), 0);
7530 if (err)
7531 goto done;
7533 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7534 resolved, new_head_commit_id, repo);
7535 if (err)
7536 goto done;
7538 err = got_ref_change_ref(resolved, new_head_commit_id);
7539 if (err)
7540 goto done;
7542 err = got_ref_write(resolved, repo);
7543 if (err)
7544 goto done;
7546 err = got_worktree_set_head_ref(worktree, resolved);
7547 if (err)
7548 goto done;
7550 err = delete_histedit_refs(worktree, repo);
7551 if (err)
7552 goto done;
7554 err = get_fileindex_path(&fileindex_path, worktree);
7555 if (err)
7556 goto done;
7557 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7558 sync_err = sync_fileindex(fileindex, fileindex_path);
7559 if (sync_err && err == NULL)
7560 err = sync_err;
7561 done:
7562 got_fileindex_free(fileindex);
7563 free(fileindex_path);
7564 free(new_head_commit_id);
7565 unlockerr = lock_worktree(worktree, LOCK_SH);
7566 if (unlockerr && err == NULL)
7567 err = unlockerr;
7568 return err;
7571 const struct got_error *
7572 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7573 struct got_object_id *commit_id, struct got_repository *repo)
7575 const struct got_error *err;
7576 char *commit_ref_name;
7578 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7579 if (err)
7580 return err;
7582 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7583 if (err)
7584 goto done;
7586 err = delete_ref(commit_ref_name, repo);
7587 done:
7588 free(commit_ref_name);
7589 return err;
7592 const struct got_error *
7593 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7594 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7595 struct got_worktree *worktree, const char *refname,
7596 struct got_repository *repo)
7598 const struct got_error *err = NULL;
7599 char *fileindex_path = NULL;
7600 struct check_rebase_ok_arg ok_arg;
7602 *fileindex = NULL;
7603 *branch_ref = NULL;
7604 *base_branch_ref = NULL;
7606 err = lock_worktree(worktree, LOCK_EX);
7607 if (err)
7608 return err;
7610 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7611 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7612 "cannot integrate a branch into itself; "
7613 "update -b or different branch name required");
7614 goto done;
7617 err = open_fileindex(fileindex, &fileindex_path, worktree);
7618 if (err)
7619 goto done;
7621 /* Preconditions are the same as for rebase. */
7622 ok_arg.worktree = worktree;
7623 ok_arg.repo = repo;
7624 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7625 &ok_arg);
7626 if (err)
7627 goto done;
7629 err = got_ref_open(branch_ref, repo, refname, 1);
7630 if (err)
7631 goto done;
7633 err = got_ref_open(base_branch_ref, repo,
7634 got_worktree_get_head_ref_name(worktree), 1);
7635 done:
7636 if (err) {
7637 if (*branch_ref) {
7638 got_ref_close(*branch_ref);
7639 *branch_ref = NULL;
7641 if (*base_branch_ref) {
7642 got_ref_close(*base_branch_ref);
7643 *base_branch_ref = NULL;
7645 if (*fileindex) {
7646 got_fileindex_free(*fileindex);
7647 *fileindex = NULL;
7649 lock_worktree(worktree, LOCK_SH);
7651 return err;
7654 const struct got_error *
7655 got_worktree_integrate_continue(struct got_worktree *worktree,
7656 struct got_fileindex *fileindex, struct got_repository *repo,
7657 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7658 got_worktree_checkout_cb progress_cb, void *progress_arg,
7659 got_cancel_cb cancel_cb, void *cancel_arg)
7661 const struct got_error *err = NULL, *sync_err, *unlockerr;
7662 char *fileindex_path = NULL;
7663 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7664 struct got_commit_object *commit = NULL;
7666 err = get_fileindex_path(&fileindex_path, worktree);
7667 if (err)
7668 goto done;
7670 err = got_ref_resolve(&commit_id, repo, branch_ref);
7671 if (err)
7672 goto done;
7674 err = got_object_open_as_commit(&commit, repo, commit_id);
7675 if (err)
7676 goto done;
7678 err = got_object_id_by_path(&tree_id, repo, commit,
7679 worktree->path_prefix);
7680 if (err)
7681 goto done;
7683 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7684 if (err)
7685 goto done;
7687 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7688 progress_cb, progress_arg, cancel_cb, cancel_arg);
7689 if (err)
7690 goto sync;
7692 err = got_ref_change_ref(base_branch_ref, commit_id);
7693 if (err)
7694 goto sync;
7696 err = got_ref_write(base_branch_ref, repo);
7697 if (err)
7698 goto sync;
7700 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7701 sync:
7702 sync_err = sync_fileindex(fileindex, fileindex_path);
7703 if (sync_err && err == NULL)
7704 err = sync_err;
7706 done:
7707 unlockerr = got_ref_unlock(branch_ref);
7708 if (unlockerr && err == NULL)
7709 err = unlockerr;
7710 got_ref_close(branch_ref);
7712 unlockerr = got_ref_unlock(base_branch_ref);
7713 if (unlockerr && err == NULL)
7714 err = unlockerr;
7715 got_ref_close(base_branch_ref);
7717 got_fileindex_free(fileindex);
7718 free(fileindex_path);
7719 free(tree_id);
7720 if (commit)
7721 got_object_commit_close(commit);
7723 unlockerr = lock_worktree(worktree, LOCK_SH);
7724 if (unlockerr && err == NULL)
7725 err = unlockerr;
7726 return err;
7729 const struct got_error *
7730 got_worktree_integrate_abort(struct got_worktree *worktree,
7731 struct got_fileindex *fileindex, struct got_repository *repo,
7732 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7734 const struct got_error *err = NULL, *unlockerr = NULL;
7736 got_fileindex_free(fileindex);
7738 err = lock_worktree(worktree, LOCK_SH);
7740 unlockerr = got_ref_unlock(branch_ref);
7741 if (unlockerr && err == NULL)
7742 err = unlockerr;
7743 got_ref_close(branch_ref);
7745 unlockerr = got_ref_unlock(base_branch_ref);
7746 if (unlockerr && err == NULL)
7747 err = unlockerr;
7748 got_ref_close(base_branch_ref);
7750 return err;
7753 const struct got_error *
7754 got_worktree_merge_postpone(struct got_worktree *worktree,
7755 struct got_fileindex *fileindex)
7757 const struct got_error *err, *sync_err;
7758 char *fileindex_path = NULL;
7760 err = get_fileindex_path(&fileindex_path, worktree);
7761 if (err)
7762 goto done;
7764 sync_err = sync_fileindex(fileindex, fileindex_path);
7766 err = lock_worktree(worktree, LOCK_SH);
7767 if (sync_err && err == NULL)
7768 err = sync_err;
7769 done:
7770 got_fileindex_free(fileindex);
7771 free(fileindex_path);
7772 return err;
7775 static const struct got_error *
7776 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7778 const struct got_error *err;
7779 char *branch_refname = NULL, *commit_refname = NULL;
7781 err = get_merge_branch_ref_name(&branch_refname, worktree);
7782 if (err)
7783 goto done;
7784 err = delete_ref(branch_refname, repo);
7785 if (err)
7786 goto done;
7788 err = get_merge_commit_ref_name(&commit_refname, worktree);
7789 if (err)
7790 goto done;
7791 err = delete_ref(commit_refname, repo);
7792 if (err)
7793 goto done;
7795 done:
7796 free(branch_refname);
7797 free(commit_refname);
7798 return err;
7801 struct merge_commit_msg_arg {
7802 struct got_worktree *worktree;
7803 const char *branch_name;
7806 static const struct got_error *
7807 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7808 const char *diff_path, char **logmsg, void *arg)
7810 struct merge_commit_msg_arg *a = arg;
7812 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7813 got_worktree_get_head_ref_name(a->worktree)) == -1)
7814 return got_error_from_errno("asprintf");
7816 return NULL;
7820 const struct got_error *
7821 got_worktree_merge_branch(struct got_worktree *worktree,
7822 struct got_fileindex *fileindex,
7823 struct got_object_id *yca_commit_id,
7824 struct got_object_id *branch_tip,
7825 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7826 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7828 const struct got_error *err;
7829 char *fileindex_path = NULL;
7831 err = get_fileindex_path(&fileindex_path, worktree);
7832 if (err)
7833 goto done;
7835 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7836 worktree);
7837 if (err)
7838 goto done;
7840 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7841 branch_tip, repo, progress_cb, progress_arg,
7842 cancel_cb, cancel_arg);
7843 done:
7844 free(fileindex_path);
7845 return err;
7848 const struct got_error *
7849 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7850 struct got_worktree *worktree, struct got_fileindex *fileindex,
7851 const char *author, const char *committer, int allow_bad_symlinks,
7852 struct got_object_id *branch_tip, const char *branch_name,
7853 struct got_repository *repo,
7854 got_worktree_status_cb status_cb, void *status_arg)
7857 const struct got_error *err = NULL, *sync_err;
7858 struct got_pathlist_head commitable_paths;
7859 struct collect_commitables_arg cc_arg;
7860 struct got_pathlist_entry *pe;
7861 struct got_reference *head_ref = NULL;
7862 struct got_object_id *head_commit_id = NULL;
7863 int have_staged_files = 0;
7864 struct merge_commit_msg_arg mcm_arg;
7865 char *fileindex_path = NULL;
7867 memset(&cc_arg, 0, sizeof(cc_arg));
7868 *new_commit_id = NULL;
7870 TAILQ_INIT(&commitable_paths);
7872 err = get_fileindex_path(&fileindex_path, worktree);
7873 if (err)
7874 goto done;
7876 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7877 if (err)
7878 goto done;
7880 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7881 if (err)
7882 goto done;
7884 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7885 &have_staged_files);
7886 if (err && err->code != GOT_ERR_CANCELLED)
7887 goto done;
7888 if (have_staged_files) {
7889 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7890 goto done;
7893 cc_arg.commitable_paths = &commitable_paths;
7894 cc_arg.worktree = worktree;
7895 cc_arg.fileindex = fileindex;
7896 cc_arg.repo = repo;
7897 cc_arg.have_staged_files = have_staged_files;
7898 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7899 err = worktree_status(worktree, "", fileindex, repo,
7900 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7901 if (err)
7902 goto done;
7904 if (TAILQ_EMPTY(&commitable_paths)) {
7905 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7906 "merge of %s cannot proceed", branch_name);
7907 goto done;
7910 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7911 struct got_commitable *ct = pe->data;
7912 const char *ct_path = ct->in_repo_path;
7914 while (ct_path[0] == '/')
7915 ct_path++;
7916 err = check_out_of_date(ct_path, ct->status,
7917 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7918 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7919 if (err)
7920 goto done;
7924 mcm_arg.worktree = worktree;
7925 mcm_arg.branch_name = branch_name;
7926 err = commit_worktree(new_commit_id, &commitable_paths,
7927 head_commit_id, branch_tip, worktree, author, committer, NULL,
7928 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7929 if (err)
7930 goto done;
7932 err = update_fileindex_after_commit(worktree, &commitable_paths,
7933 *new_commit_id, fileindex, have_staged_files);
7934 sync_err = sync_fileindex(fileindex, fileindex_path);
7935 if (sync_err && err == NULL)
7936 err = sync_err;
7937 done:
7938 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7939 struct got_commitable *ct = pe->data;
7941 free_commitable(ct);
7943 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7944 free(fileindex_path);
7945 return err;
7948 const struct got_error *
7949 got_worktree_merge_complete(struct got_worktree *worktree,
7950 struct got_fileindex *fileindex, struct got_repository *repo)
7952 const struct got_error *err, *unlockerr, *sync_err;
7953 char *fileindex_path = NULL;
7955 err = delete_merge_refs(worktree, repo);
7956 if (err)
7957 goto done;
7959 err = get_fileindex_path(&fileindex_path, worktree);
7960 if (err)
7961 goto done;
7962 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7963 sync_err = sync_fileindex(fileindex, fileindex_path);
7964 if (sync_err && err == NULL)
7965 err = sync_err;
7966 done:
7967 got_fileindex_free(fileindex);
7968 free(fileindex_path);
7969 unlockerr = lock_worktree(worktree, LOCK_SH);
7970 if (unlockerr && err == NULL)
7971 err = unlockerr;
7972 return err;
7975 const struct got_error *
7976 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7977 struct got_repository *repo)
7979 const struct got_error *err;
7980 char *branch_refname = NULL;
7981 struct got_reference *branch_ref = NULL;
7983 *in_progress = 0;
7985 err = get_merge_branch_ref_name(&branch_refname, worktree);
7986 if (err)
7987 return err;
7988 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7989 free(branch_refname);
7990 if (err) {
7991 if (err->code != GOT_ERR_NOT_REF)
7992 return err;
7993 } else
7994 *in_progress = 1;
7996 return NULL;
7999 const struct got_error *got_worktree_merge_prepare(
8000 struct got_fileindex **fileindex, struct got_worktree *worktree,
8001 struct got_reference *branch, struct got_repository *repo)
8003 const struct got_error *err = NULL;
8004 char *fileindex_path = NULL;
8005 char *branch_refname = NULL, *commit_refname = NULL;
8006 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8007 struct got_reference *commit_ref = NULL;
8008 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8009 struct check_rebase_ok_arg ok_arg;
8011 *fileindex = NULL;
8013 err = lock_worktree(worktree, LOCK_EX);
8014 if (err)
8015 return err;
8017 err = open_fileindex(fileindex, &fileindex_path, worktree);
8018 if (err)
8019 goto done;
8021 /* Preconditions are the same as for rebase. */
8022 ok_arg.worktree = worktree;
8023 ok_arg.repo = repo;
8024 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8025 &ok_arg);
8026 if (err)
8027 goto done;
8029 err = get_merge_branch_ref_name(&branch_refname, worktree);
8030 if (err)
8031 return err;
8033 err = get_merge_commit_ref_name(&commit_refname, worktree);
8034 if (err)
8035 return err;
8037 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8038 0);
8039 if (err)
8040 goto done;
8042 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8043 if (err)
8044 goto done;
8046 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8047 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8048 goto done;
8051 err = got_ref_resolve(&branch_tip, repo, branch);
8052 if (err)
8053 goto done;
8055 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8056 if (err)
8057 goto done;
8058 err = got_ref_write(branch_ref, repo);
8059 if (err)
8060 goto done;
8062 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8063 if (err)
8064 goto done;
8065 err = got_ref_write(commit_ref, repo);
8066 if (err)
8067 goto done;
8069 done:
8070 free(branch_refname);
8071 free(commit_refname);
8072 free(fileindex_path);
8073 if (branch_ref)
8074 got_ref_close(branch_ref);
8075 if (commit_ref)
8076 got_ref_close(commit_ref);
8077 if (wt_branch)
8078 got_ref_close(wt_branch);
8079 free(wt_branch_tip);
8080 if (err) {
8081 if (*fileindex) {
8082 got_fileindex_free(*fileindex);
8083 *fileindex = NULL;
8085 lock_worktree(worktree, LOCK_SH);
8087 return err;
8090 const struct got_error *
8091 got_worktree_merge_continue(char **branch_name,
8092 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8093 struct got_worktree *worktree, struct got_repository *repo)
8095 const struct got_error *err;
8096 char *commit_refname = NULL, *branch_refname = NULL;
8097 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8098 char *fileindex_path = NULL;
8099 int have_staged_files = 0;
8101 *branch_name = NULL;
8102 *branch_tip = NULL;
8103 *fileindex = NULL;
8105 err = lock_worktree(worktree, LOCK_EX);
8106 if (err)
8107 return err;
8109 err = open_fileindex(fileindex, &fileindex_path, worktree);
8110 if (err)
8111 goto done;
8113 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8114 &have_staged_files);
8115 if (err && err->code != GOT_ERR_CANCELLED)
8116 goto done;
8117 if (have_staged_files) {
8118 err = got_error(GOT_ERR_STAGED_PATHS);
8119 goto done;
8122 err = get_merge_branch_ref_name(&branch_refname, worktree);
8123 if (err)
8124 goto done;
8126 err = get_merge_commit_ref_name(&commit_refname, worktree);
8127 if (err)
8128 goto done;
8130 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8131 if (err)
8132 goto done;
8134 if (!got_ref_is_symbolic(branch_ref)) {
8135 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8136 "%s is not a symbolic reference",
8137 got_ref_get_name(branch_ref));
8138 goto done;
8140 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8141 if (*branch_name == NULL) {
8142 err = got_error_from_errno("strdup");
8143 goto done;
8146 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8147 if (err)
8148 goto done;
8150 err = got_ref_resolve(branch_tip, repo, commit_ref);
8151 if (err)
8152 goto done;
8153 done:
8154 free(commit_refname);
8155 free(branch_refname);
8156 free(fileindex_path);
8157 if (commit_ref)
8158 got_ref_close(commit_ref);
8159 if (branch_ref)
8160 got_ref_close(branch_ref);
8161 if (err) {
8162 if (*branch_name) {
8163 free(*branch_name);
8164 *branch_name = NULL;
8166 free(*branch_tip);
8167 *branch_tip = NULL;
8168 if (*fileindex) {
8169 got_fileindex_free(*fileindex);
8170 *fileindex = NULL;
8172 lock_worktree(worktree, LOCK_SH);
8174 return err;
8177 const struct got_error *
8178 got_worktree_merge_abort(struct got_worktree *worktree,
8179 struct got_fileindex *fileindex, struct got_repository *repo,
8180 got_worktree_checkout_cb progress_cb, void *progress_arg)
8182 const struct got_error *err, *unlockerr, *sync_err;
8183 struct got_object_id *commit_id = NULL;
8184 struct got_commit_object *commit = NULL;
8185 char *fileindex_path = NULL;
8186 struct revert_file_args rfa;
8187 struct got_object_id *tree_id = NULL;
8189 err = got_object_open_as_commit(&commit, repo,
8190 worktree->base_commit_id);
8191 if (err)
8192 goto done;
8194 err = got_object_id_by_path(&tree_id, repo, commit,
8195 worktree->path_prefix);
8196 if (err)
8197 goto done;
8199 err = delete_merge_refs(worktree, repo);
8200 if (err)
8201 goto done;
8203 err = get_fileindex_path(&fileindex_path, worktree);
8204 if (err)
8205 goto done;
8207 rfa.worktree = worktree;
8208 rfa.fileindex = fileindex;
8209 rfa.progress_cb = progress_cb;
8210 rfa.progress_arg = progress_arg;
8211 rfa.patch_cb = NULL;
8212 rfa.patch_arg = NULL;
8213 rfa.repo = repo;
8214 rfa.unlink_added_files = 1;
8215 err = worktree_status(worktree, "", fileindex, repo,
8216 revert_file, &rfa, NULL, NULL, 1, 0);
8217 if (err)
8218 goto sync;
8220 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8221 repo, progress_cb, progress_arg, NULL, NULL);
8222 sync:
8223 sync_err = sync_fileindex(fileindex, fileindex_path);
8224 if (sync_err && err == NULL)
8225 err = sync_err;
8226 done:
8227 free(tree_id);
8228 free(commit_id);
8229 if (commit)
8230 got_object_commit_close(commit);
8231 if (fileindex)
8232 got_fileindex_free(fileindex);
8233 free(fileindex_path);
8235 unlockerr = lock_worktree(worktree, LOCK_SH);
8236 if (unlockerr && err == NULL)
8237 err = unlockerr;
8238 return err;
8241 struct check_stage_ok_arg {
8242 struct got_object_id *head_commit_id;
8243 struct got_worktree *worktree;
8244 struct got_fileindex *fileindex;
8245 struct got_repository *repo;
8246 int have_changes;
8249 static const struct got_error *
8250 check_stage_ok(void *arg, unsigned char status,
8251 unsigned char staged_status, const char *relpath,
8252 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8253 struct got_object_id *commit_id, int dirfd, const char *de_name)
8255 struct check_stage_ok_arg *a = arg;
8256 const struct got_error *err = NULL;
8257 struct got_fileindex_entry *ie;
8258 struct got_object_id base_commit_id;
8259 struct got_object_id *base_commit_idp = NULL;
8260 char *in_repo_path = NULL, *p;
8262 if (status == GOT_STATUS_UNVERSIONED ||
8263 status == GOT_STATUS_NO_CHANGE)
8264 return NULL;
8265 if (status == GOT_STATUS_NONEXISTENT)
8266 return got_error_set_errno(ENOENT, relpath);
8268 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8269 if (ie == NULL)
8270 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8272 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8273 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8274 relpath) == -1)
8275 return got_error_from_errno("asprintf");
8277 if (got_fileindex_entry_has_commit(ie)) {
8278 base_commit_idp = got_fileindex_entry_get_commit_id(
8279 &base_commit_id, ie);
8282 if (status == GOT_STATUS_CONFLICT) {
8283 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8284 goto done;
8285 } else if (status != GOT_STATUS_ADD &&
8286 status != GOT_STATUS_MODIFY &&
8287 status != GOT_STATUS_DELETE) {
8288 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8289 goto done;
8292 a->have_changes = 1;
8294 p = in_repo_path;
8295 while (p[0] == '/')
8296 p++;
8297 err = check_out_of_date(p, status, staged_status,
8298 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8299 GOT_ERR_STAGE_OUT_OF_DATE);
8300 done:
8301 free(in_repo_path);
8302 return err;
8305 struct stage_path_arg {
8306 struct got_worktree *worktree;
8307 struct got_fileindex *fileindex;
8308 struct got_repository *repo;
8309 got_worktree_status_cb status_cb;
8310 void *status_arg;
8311 got_worktree_patch_cb patch_cb;
8312 void *patch_arg;
8313 int staged_something;
8314 int allow_bad_symlinks;
8317 static const struct got_error *
8318 stage_path(void *arg, unsigned char status,
8319 unsigned char staged_status, const char *relpath,
8320 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8321 struct got_object_id *commit_id, int dirfd, const char *de_name)
8323 struct stage_path_arg *a = arg;
8324 const struct got_error *err = NULL;
8325 struct got_fileindex_entry *ie;
8326 char *ondisk_path = NULL, *path_content = NULL;
8327 uint32_t stage;
8328 struct got_object_id *new_staged_blob_id = NULL;
8329 struct stat sb;
8331 if (status == GOT_STATUS_UNVERSIONED)
8332 return NULL;
8334 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8335 if (ie == NULL)
8336 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8338 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8339 relpath)== -1)
8340 return got_error_from_errno("asprintf");
8342 switch (status) {
8343 case GOT_STATUS_ADD:
8344 case GOT_STATUS_MODIFY:
8345 /* XXX could sb.st_mode be passed in by our caller? */
8346 if (lstat(ondisk_path, &sb) == -1) {
8347 err = got_error_from_errno2("lstat", ondisk_path);
8348 break;
8350 if (a->patch_cb) {
8351 if (status == GOT_STATUS_ADD) {
8352 int choice = GOT_PATCH_CHOICE_NONE;
8353 err = (*a->patch_cb)(&choice, a->patch_arg,
8354 status, ie->path, NULL, 1, 1);
8355 if (err)
8356 break;
8357 if (choice != GOT_PATCH_CHOICE_YES)
8358 break;
8359 } else {
8360 err = create_patched_content(&path_content, 0,
8361 staged_blob_id ? staged_blob_id : blob_id,
8362 ondisk_path, dirfd, de_name, ie->path,
8363 a->repo, a->patch_cb, a->patch_arg);
8364 if (err || path_content == NULL)
8365 break;
8368 err = got_object_blob_create(&new_staged_blob_id,
8369 path_content ? path_content : ondisk_path, a->repo);
8370 if (err)
8371 break;
8372 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8373 SHA1_DIGEST_LENGTH);
8374 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8375 stage = GOT_FILEIDX_STAGE_ADD;
8376 else
8377 stage = GOT_FILEIDX_STAGE_MODIFY;
8378 got_fileindex_entry_stage_set(ie, stage);
8379 if (S_ISLNK(sb.st_mode)) {
8380 int is_bad_symlink = 0;
8381 if (!a->allow_bad_symlinks) {
8382 char target_path[PATH_MAX];
8383 ssize_t target_len;
8384 target_len = readlink(ondisk_path, target_path,
8385 sizeof(target_path));
8386 if (target_len == -1) {
8387 err = got_error_from_errno2("readlink",
8388 ondisk_path);
8389 break;
8391 err = is_bad_symlink_target(&is_bad_symlink,
8392 target_path, target_len, ondisk_path,
8393 a->worktree->root_path);
8394 if (err)
8395 break;
8396 if (is_bad_symlink) {
8397 err = got_error_path(ondisk_path,
8398 GOT_ERR_BAD_SYMLINK);
8399 break;
8402 if (is_bad_symlink)
8403 got_fileindex_entry_staged_filetype_set(ie,
8404 GOT_FILEIDX_MODE_BAD_SYMLINK);
8405 else
8406 got_fileindex_entry_staged_filetype_set(ie,
8407 GOT_FILEIDX_MODE_SYMLINK);
8408 } else {
8409 got_fileindex_entry_staged_filetype_set(ie,
8410 GOT_FILEIDX_MODE_REGULAR_FILE);
8412 a->staged_something = 1;
8413 if (a->status_cb == NULL)
8414 break;
8415 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8416 get_staged_status(ie), relpath, blob_id,
8417 new_staged_blob_id, NULL, dirfd, de_name);
8418 if (err)
8419 break;
8421 * When staging the reverse of the staged diff,
8422 * implicitly unstage the file.
8424 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8425 sizeof(ie->blob_sha1)) == 0) {
8426 got_fileindex_entry_stage_set(ie,
8427 GOT_FILEIDX_STAGE_NONE);
8429 break;
8430 case GOT_STATUS_DELETE:
8431 if (staged_status == GOT_STATUS_DELETE)
8432 break;
8433 if (a->patch_cb) {
8434 int choice = GOT_PATCH_CHOICE_NONE;
8435 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8436 ie->path, NULL, 1, 1);
8437 if (err)
8438 break;
8439 if (choice == GOT_PATCH_CHOICE_NO)
8440 break;
8441 if (choice != GOT_PATCH_CHOICE_YES) {
8442 err = got_error(GOT_ERR_PATCH_CHOICE);
8443 break;
8446 stage = GOT_FILEIDX_STAGE_DELETE;
8447 got_fileindex_entry_stage_set(ie, stage);
8448 a->staged_something = 1;
8449 if (a->status_cb == NULL)
8450 break;
8451 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8452 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8453 de_name);
8454 break;
8455 case GOT_STATUS_NO_CHANGE:
8456 break;
8457 case GOT_STATUS_CONFLICT:
8458 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8459 break;
8460 case GOT_STATUS_NONEXISTENT:
8461 err = got_error_set_errno(ENOENT, relpath);
8462 break;
8463 default:
8464 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8465 break;
8468 if (path_content && unlink(path_content) == -1 && err == NULL)
8469 err = got_error_from_errno2("unlink", path_content);
8470 free(path_content);
8471 free(ondisk_path);
8472 free(new_staged_blob_id);
8473 return err;
8476 const struct got_error *
8477 got_worktree_stage(struct got_worktree *worktree,
8478 struct got_pathlist_head *paths,
8479 got_worktree_status_cb status_cb, void *status_arg,
8480 got_worktree_patch_cb patch_cb, void *patch_arg,
8481 int allow_bad_symlinks, struct got_repository *repo)
8483 const struct got_error *err = NULL, *sync_err, *unlockerr;
8484 struct got_pathlist_entry *pe;
8485 struct got_fileindex *fileindex = NULL;
8486 char *fileindex_path = NULL;
8487 struct got_reference *head_ref = NULL;
8488 struct got_object_id *head_commit_id = NULL;
8489 struct check_stage_ok_arg oka;
8490 struct stage_path_arg spa;
8492 err = lock_worktree(worktree, LOCK_EX);
8493 if (err)
8494 return err;
8496 err = got_ref_open(&head_ref, repo,
8497 got_worktree_get_head_ref_name(worktree), 0);
8498 if (err)
8499 goto done;
8500 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8501 if (err)
8502 goto done;
8503 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8504 if (err)
8505 goto done;
8507 /* Check pre-conditions before staging anything. */
8508 oka.head_commit_id = head_commit_id;
8509 oka.worktree = worktree;
8510 oka.fileindex = fileindex;
8511 oka.repo = repo;
8512 oka.have_changes = 0;
8513 TAILQ_FOREACH(pe, paths, entry) {
8514 err = worktree_status(worktree, pe->path, fileindex, repo,
8515 check_stage_ok, &oka, NULL, NULL, 1, 0);
8516 if (err)
8517 goto done;
8519 if (!oka.have_changes) {
8520 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8521 goto done;
8524 spa.worktree = worktree;
8525 spa.fileindex = fileindex;
8526 spa.repo = repo;
8527 spa.patch_cb = patch_cb;
8528 spa.patch_arg = patch_arg;
8529 spa.status_cb = status_cb;
8530 spa.status_arg = status_arg;
8531 spa.staged_something = 0;
8532 spa.allow_bad_symlinks = allow_bad_symlinks;
8533 TAILQ_FOREACH(pe, paths, entry) {
8534 err = worktree_status(worktree, pe->path, fileindex, repo,
8535 stage_path, &spa, NULL, NULL, 1, 0);
8536 if (err)
8537 goto done;
8539 if (!spa.staged_something) {
8540 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8541 goto done;
8544 sync_err = sync_fileindex(fileindex, fileindex_path);
8545 if (sync_err && err == NULL)
8546 err = sync_err;
8547 done:
8548 if (head_ref)
8549 got_ref_close(head_ref);
8550 free(head_commit_id);
8551 free(fileindex_path);
8552 if (fileindex)
8553 got_fileindex_free(fileindex);
8554 unlockerr = lock_worktree(worktree, LOCK_SH);
8555 if (unlockerr && err == NULL)
8556 err = unlockerr;
8557 return err;
8560 struct unstage_path_arg {
8561 struct got_worktree *worktree;
8562 struct got_fileindex *fileindex;
8563 struct got_repository *repo;
8564 got_worktree_checkout_cb progress_cb;
8565 void *progress_arg;
8566 got_worktree_patch_cb patch_cb;
8567 void *patch_arg;
8570 static const struct got_error *
8571 create_unstaged_content(char **path_unstaged_content,
8572 char **path_new_staged_content, struct got_object_id *blob_id,
8573 struct got_object_id *staged_blob_id, const char *relpath,
8574 struct got_repository *repo,
8575 got_worktree_patch_cb patch_cb, void *patch_arg)
8577 const struct got_error *err, *free_err;
8578 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8579 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8580 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8581 struct got_diffreg_result *diffreg_result = NULL;
8582 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8583 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8584 int fd1 = -1, fd2 = -1;
8586 *path_unstaged_content = NULL;
8587 *path_new_staged_content = NULL;
8589 err = got_object_id_str(&label1, blob_id);
8590 if (err)
8591 return err;
8593 fd1 = got_opentempfd();
8594 if (fd1 == -1) {
8595 err = got_error_from_errno("got_opentempfd");
8596 goto done;
8598 fd2 = got_opentempfd();
8599 if (fd2 == -1) {
8600 err = got_error_from_errno("got_opentempfd");
8601 goto done;
8604 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8605 if (err)
8606 goto done;
8608 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8609 if (err)
8610 goto done;
8612 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8613 if (err)
8614 goto done;
8616 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8617 fd2);
8618 if (err)
8619 goto done;
8621 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8622 if (err)
8623 goto done;
8625 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8626 if (err)
8627 goto done;
8629 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8630 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8631 if (err)
8632 goto done;
8634 err = got_opentemp_named(path_unstaged_content, &outfile,
8635 "got-unstaged-content", "");
8636 if (err)
8637 goto done;
8638 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8639 "got-new-staged-content", "");
8640 if (err)
8641 goto done;
8643 if (fseek(f1, 0L, SEEK_SET) == -1) {
8644 err = got_ferror(f1, GOT_ERR_IO);
8645 goto done;
8647 if (fseek(f2, 0L, SEEK_SET) == -1) {
8648 err = got_ferror(f2, GOT_ERR_IO);
8649 goto done;
8651 /* Count the number of actual changes in the diff result. */
8652 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8653 struct diff_chunk_context cc = {};
8654 diff_chunk_context_load_change(&cc, &nchunks_used,
8655 diffreg_result->result, n, 0);
8656 nchanges++;
8658 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8659 int choice;
8660 err = apply_or_reject_change(&choice, &nchunks_used,
8661 diffreg_result->result, n, relpath, f1, f2,
8662 &line_cur1, &line_cur2,
8663 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8664 if (err)
8665 goto done;
8666 if (choice == GOT_PATCH_CHOICE_YES)
8667 have_content = 1;
8668 else
8669 have_rejected_content = 1;
8670 if (choice == GOT_PATCH_CHOICE_QUIT)
8671 break;
8673 if (have_content || have_rejected_content)
8674 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8675 outfile, rejectfile);
8676 done:
8677 free(label1);
8678 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8679 err = got_error_from_errno("close");
8680 if (blob)
8681 got_object_blob_close(blob);
8682 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8683 err = got_error_from_errno("close");
8684 if (staged_blob)
8685 got_object_blob_close(staged_blob);
8686 free_err = got_diffreg_result_free(diffreg_result);
8687 if (free_err && err == NULL)
8688 err = free_err;
8689 if (f1 && fclose(f1) == EOF && err == NULL)
8690 err = got_error_from_errno2("fclose", path1);
8691 if (f2 && fclose(f2) == EOF && err == NULL)
8692 err = got_error_from_errno2("fclose", path2);
8693 if (outfile && fclose(outfile) == EOF && err == NULL)
8694 err = got_error_from_errno2("fclose", *path_unstaged_content);
8695 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8696 err = got_error_from_errno2("fclose", *path_new_staged_content);
8697 if (path1 && unlink(path1) == -1 && err == NULL)
8698 err = got_error_from_errno2("unlink", path1);
8699 if (path2 && unlink(path2) == -1 && err == NULL)
8700 err = got_error_from_errno2("unlink", path2);
8701 if (err || !have_content) {
8702 if (*path_unstaged_content &&
8703 unlink(*path_unstaged_content) == -1 && err == NULL)
8704 err = got_error_from_errno2("unlink",
8705 *path_unstaged_content);
8706 free(*path_unstaged_content);
8707 *path_unstaged_content = NULL;
8709 if (err || !have_content || !have_rejected_content) {
8710 if (*path_new_staged_content &&
8711 unlink(*path_new_staged_content) == -1 && err == NULL)
8712 err = got_error_from_errno2("unlink",
8713 *path_new_staged_content);
8714 free(*path_new_staged_content);
8715 *path_new_staged_content = NULL;
8717 free(path1);
8718 free(path2);
8719 return err;
8722 static const struct got_error *
8723 unstage_hunks(struct got_object_id *staged_blob_id,
8724 struct got_blob_object *blob_base,
8725 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8726 const char *ondisk_path, const char *label_orig,
8727 struct got_worktree *worktree, struct got_repository *repo,
8728 got_worktree_patch_cb patch_cb, void *patch_arg,
8729 got_worktree_checkout_cb progress_cb, void *progress_arg)
8731 const struct got_error *err = NULL;
8732 char *path_unstaged_content = NULL;
8733 char *path_new_staged_content = NULL;
8734 char *parent = NULL, *base_path = NULL;
8735 char *blob_base_path = NULL;
8736 struct got_object_id *new_staged_blob_id = NULL;
8737 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8738 struct stat sb;
8740 err = create_unstaged_content(&path_unstaged_content,
8741 &path_new_staged_content, blob_id, staged_blob_id,
8742 ie->path, repo, patch_cb, patch_arg);
8743 if (err)
8744 return err;
8746 if (path_unstaged_content == NULL)
8747 return NULL;
8749 if (path_new_staged_content) {
8750 err = got_object_blob_create(&new_staged_blob_id,
8751 path_new_staged_content, repo);
8752 if (err)
8753 goto done;
8756 f = fopen(path_unstaged_content, "re");
8757 if (f == NULL) {
8758 err = got_error_from_errno2("fopen",
8759 path_unstaged_content);
8760 goto done;
8762 if (fstat(fileno(f), &sb) == -1) {
8763 err = got_error_from_errno2("fstat", path_unstaged_content);
8764 goto done;
8766 if (got_fileindex_entry_staged_filetype_get(ie) ==
8767 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8768 char link_target[PATH_MAX];
8769 size_t r;
8770 r = fread(link_target, 1, sizeof(link_target), f);
8771 if (r == 0 && ferror(f)) {
8772 err = got_error_from_errno("fread");
8773 goto done;
8775 if (r >= sizeof(link_target)) { /* should not happen */
8776 err = got_error(GOT_ERR_NO_SPACE);
8777 goto done;
8779 link_target[r] = '\0';
8780 err = merge_symlink(worktree, blob_base,
8781 ondisk_path, ie->path, label_orig, link_target,
8782 worktree->base_commit_id, repo, progress_cb,
8783 progress_arg);
8784 } else {
8785 int local_changes_subsumed;
8787 err = got_path_dirname(&parent, ondisk_path);
8788 if (err)
8789 return err;
8791 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8792 parent) == -1) {
8793 err = got_error_from_errno("asprintf");
8794 base_path = NULL;
8795 goto done;
8798 err = got_opentemp_named(&blob_base_path, &f_base,
8799 base_path, "");
8800 if (err)
8801 goto done;
8802 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8803 blob_base);
8804 if (err)
8805 goto done;
8808 * In order the run a 3-way merge with a symlink we copy the symlink's
8809 * target path into a temporary file and use that file with diff3.
8811 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8812 err = dump_symlink_target_path_to_file(&f_deriv2,
8813 ondisk_path);
8814 if (err)
8815 goto done;
8816 } else {
8817 int fd;
8818 fd = open(ondisk_path,
8819 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8820 if (fd == -1) {
8821 err = got_error_from_errno2("open", ondisk_path);
8822 goto done;
8824 f_deriv2 = fdopen(fd, "r");
8825 if (f_deriv2 == NULL) {
8826 err = got_error_from_errno2("fdopen", ondisk_path);
8827 close(fd);
8828 goto done;
8832 err = merge_file(&local_changes_subsumed, worktree,
8833 f_base, f, f_deriv2, ondisk_path, ie->path,
8834 got_fileindex_perms_to_st(ie),
8835 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8836 repo, progress_cb, progress_arg);
8838 if (err)
8839 goto done;
8841 if (new_staged_blob_id) {
8842 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8843 SHA1_DIGEST_LENGTH);
8844 } else {
8845 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8846 got_fileindex_entry_staged_filetype_set(ie, 0);
8848 done:
8849 free(new_staged_blob_id);
8850 if (path_unstaged_content &&
8851 unlink(path_unstaged_content) == -1 && err == NULL)
8852 err = got_error_from_errno2("unlink", path_unstaged_content);
8853 if (path_new_staged_content &&
8854 unlink(path_new_staged_content) == -1 && err == NULL)
8855 err = got_error_from_errno2("unlink", path_new_staged_content);
8856 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8857 err = got_error_from_errno2("unlink", blob_base_path);
8858 if (f_base && fclose(f_base) == EOF && err == NULL)
8859 err = got_error_from_errno2("fclose", path_unstaged_content);
8860 if (f && fclose(f) == EOF && err == NULL)
8861 err = got_error_from_errno2("fclose", path_unstaged_content);
8862 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8863 err = got_error_from_errno2("fclose", ondisk_path);
8864 free(path_unstaged_content);
8865 free(path_new_staged_content);
8866 free(blob_base_path);
8867 free(parent);
8868 free(base_path);
8869 return err;
8872 static const struct got_error *
8873 unstage_path(void *arg, unsigned char status,
8874 unsigned char staged_status, const char *relpath,
8875 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8876 struct got_object_id *commit_id, int dirfd, const char *de_name)
8878 const struct got_error *err = NULL;
8879 struct unstage_path_arg *a = arg;
8880 struct got_fileindex_entry *ie;
8881 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8882 char *ondisk_path = NULL;
8883 char *id_str = NULL, *label_orig = NULL;
8884 int local_changes_subsumed;
8885 struct stat sb;
8886 int fd1 = -1, fd2 = -1;
8888 if (staged_status != GOT_STATUS_ADD &&
8889 staged_status != GOT_STATUS_MODIFY &&
8890 staged_status != GOT_STATUS_DELETE)
8891 return NULL;
8893 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8894 if (ie == NULL)
8895 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8897 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8898 == -1)
8899 return got_error_from_errno("asprintf");
8901 err = got_object_id_str(&id_str,
8902 commit_id ? commit_id : a->worktree->base_commit_id);
8903 if (err)
8904 goto done;
8905 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8906 id_str) == -1) {
8907 err = got_error_from_errno("asprintf");
8908 goto done;
8911 fd1 = got_opentempfd();
8912 if (fd1 == -1) {
8913 err = got_error_from_errno("got_opentempfd");
8914 goto done;
8916 fd2 = got_opentempfd();
8917 if (fd2 == -1) {
8918 err = got_error_from_errno("got_opentempfd");
8919 goto done;
8922 switch (staged_status) {
8923 case GOT_STATUS_MODIFY:
8924 err = got_object_open_as_blob(&blob_base, a->repo,
8925 blob_id, 8192, fd1);
8926 if (err)
8927 break;
8928 /* fall through */
8929 case GOT_STATUS_ADD:
8930 if (a->patch_cb) {
8931 if (staged_status == GOT_STATUS_ADD) {
8932 int choice = GOT_PATCH_CHOICE_NONE;
8933 err = (*a->patch_cb)(&choice, a->patch_arg,
8934 staged_status, ie->path, NULL, 1, 1);
8935 if (err)
8936 break;
8937 if (choice != GOT_PATCH_CHOICE_YES)
8938 break;
8939 } else {
8940 err = unstage_hunks(staged_blob_id,
8941 blob_base, blob_id, ie, ondisk_path,
8942 label_orig, a->worktree, a->repo,
8943 a->patch_cb, a->patch_arg,
8944 a->progress_cb, a->progress_arg);
8945 break; /* Done with this file. */
8948 err = got_object_open_as_blob(&blob_staged, a->repo,
8949 staged_blob_id, 8192, fd2);
8950 if (err)
8951 break;
8952 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8953 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8954 case GOT_FILEIDX_MODE_REGULAR_FILE:
8955 err = merge_blob(&local_changes_subsumed, a->worktree,
8956 blob_base, ondisk_path, relpath,
8957 got_fileindex_perms_to_st(ie), label_orig,
8958 blob_staged, commit_id ? commit_id :
8959 a->worktree->base_commit_id, a->repo,
8960 a->progress_cb, a->progress_arg);
8961 break;
8962 case GOT_FILEIDX_MODE_SYMLINK:
8963 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8964 char *staged_target;
8965 err = got_object_blob_read_to_str(
8966 &staged_target, blob_staged);
8967 if (err)
8968 goto done;
8969 err = merge_symlink(a->worktree, blob_base,
8970 ondisk_path, relpath, label_orig,
8971 staged_target, commit_id ? commit_id :
8972 a->worktree->base_commit_id,
8973 a->repo, a->progress_cb, a->progress_arg);
8974 free(staged_target);
8975 } else {
8976 err = merge_blob(&local_changes_subsumed,
8977 a->worktree, blob_base, ondisk_path,
8978 relpath, got_fileindex_perms_to_st(ie),
8979 label_orig, blob_staged,
8980 commit_id ? commit_id :
8981 a->worktree->base_commit_id, a->repo,
8982 a->progress_cb, a->progress_arg);
8984 break;
8985 default:
8986 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8987 break;
8989 if (err == NULL) {
8990 got_fileindex_entry_stage_set(ie,
8991 GOT_FILEIDX_STAGE_NONE);
8992 got_fileindex_entry_staged_filetype_set(ie, 0);
8994 break;
8995 case GOT_STATUS_DELETE:
8996 if (a->patch_cb) {
8997 int choice = GOT_PATCH_CHOICE_NONE;
8998 err = (*a->patch_cb)(&choice, a->patch_arg,
8999 staged_status, ie->path, NULL, 1, 1);
9000 if (err)
9001 break;
9002 if (choice == GOT_PATCH_CHOICE_NO)
9003 break;
9004 if (choice != GOT_PATCH_CHOICE_YES) {
9005 err = got_error(GOT_ERR_PATCH_CHOICE);
9006 break;
9009 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9010 got_fileindex_entry_staged_filetype_set(ie, 0);
9011 err = get_file_status(&status, &sb, ie, ondisk_path,
9012 dirfd, de_name, a->repo);
9013 if (err)
9014 break;
9015 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9016 break;
9018 done:
9019 free(ondisk_path);
9020 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9021 err = got_error_from_errno("close");
9022 if (blob_base)
9023 got_object_blob_close(blob_base);
9024 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9025 err = got_error_from_errno("close");
9026 if (blob_staged)
9027 got_object_blob_close(blob_staged);
9028 free(id_str);
9029 free(label_orig);
9030 return err;
9033 const struct got_error *
9034 got_worktree_unstage(struct got_worktree *worktree,
9035 struct got_pathlist_head *paths,
9036 got_worktree_checkout_cb progress_cb, void *progress_arg,
9037 got_worktree_patch_cb patch_cb, void *patch_arg,
9038 struct got_repository *repo)
9040 const struct got_error *err = NULL, *sync_err, *unlockerr;
9041 struct got_pathlist_entry *pe;
9042 struct got_fileindex *fileindex = NULL;
9043 char *fileindex_path = NULL;
9044 struct unstage_path_arg upa;
9046 err = lock_worktree(worktree, LOCK_EX);
9047 if (err)
9048 return err;
9050 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9051 if (err)
9052 goto done;
9054 upa.worktree = worktree;
9055 upa.fileindex = fileindex;
9056 upa.repo = repo;
9057 upa.progress_cb = progress_cb;
9058 upa.progress_arg = progress_arg;
9059 upa.patch_cb = patch_cb;
9060 upa.patch_arg = patch_arg;
9061 TAILQ_FOREACH(pe, paths, entry) {
9062 err = worktree_status(worktree, pe->path, fileindex, repo,
9063 unstage_path, &upa, NULL, NULL, 1, 0);
9064 if (err)
9065 goto done;
9068 sync_err = sync_fileindex(fileindex, fileindex_path);
9069 if (sync_err && err == NULL)
9070 err = sync_err;
9071 done:
9072 free(fileindex_path);
9073 if (fileindex)
9074 got_fileindex_free(fileindex);
9075 unlockerr = lock_worktree(worktree, LOCK_SH);
9076 if (unlockerr && err == NULL)
9077 err = unlockerr;
9078 return err;
9081 struct report_file_info_arg {
9082 struct got_worktree *worktree;
9083 got_worktree_path_info_cb info_cb;
9084 void *info_arg;
9085 struct got_pathlist_head *paths;
9086 got_cancel_cb cancel_cb;
9087 void *cancel_arg;
9090 static const struct got_error *
9091 report_file_info(void *arg, struct got_fileindex_entry *ie)
9093 struct report_file_info_arg *a = arg;
9094 struct got_pathlist_entry *pe;
9095 struct got_object_id blob_id, staged_blob_id, commit_id;
9096 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9097 struct got_object_id *commit_idp = NULL;
9098 int stage;
9100 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9101 return got_error(GOT_ERR_CANCELLED);
9103 TAILQ_FOREACH(pe, a->paths, entry) {
9104 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9105 got_path_is_child(ie->path, pe->path, pe->path_len))
9106 break;
9108 if (pe == NULL) /* not found */
9109 return NULL;
9111 if (got_fileindex_entry_has_blob(ie))
9112 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9113 stage = got_fileindex_entry_stage_get(ie);
9114 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9115 stage == GOT_FILEIDX_STAGE_ADD) {
9116 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9117 &staged_blob_id, ie);
9120 if (got_fileindex_entry_has_commit(ie))
9121 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9123 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9124 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9127 const struct got_error *
9128 got_worktree_path_info(struct got_worktree *worktree,
9129 struct got_pathlist_head *paths,
9130 got_worktree_path_info_cb info_cb, void *info_arg,
9131 got_cancel_cb cancel_cb, void *cancel_arg)
9134 const struct got_error *err = NULL, *unlockerr;
9135 struct got_fileindex *fileindex = NULL;
9136 char *fileindex_path = NULL;
9137 struct report_file_info_arg arg;
9139 err = lock_worktree(worktree, LOCK_SH);
9140 if (err)
9141 return err;
9143 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9144 if (err)
9145 goto done;
9147 arg.worktree = worktree;
9148 arg.info_cb = info_cb;
9149 arg.info_arg = info_arg;
9150 arg.paths = paths;
9151 arg.cancel_cb = cancel_cb;
9152 arg.cancel_arg = cancel_arg;
9153 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9154 &arg);
9155 done:
9156 free(fileindex_path);
9157 if (fileindex)
9158 got_fileindex_free(fileindex);
9159 unlockerr = lock_worktree(worktree, LOCK_UN);
9160 if (unlockerr && err == NULL)
9161 err = unlockerr;
9162 return err;
9165 static const struct got_error *
9166 patch_check_path(const char *p, char **path, unsigned char *status,
9167 unsigned char *staged_status, struct got_fileindex *fileindex,
9168 struct got_worktree *worktree, struct got_repository *repo)
9170 const struct got_error *err;
9171 struct got_fileindex_entry *ie;
9172 struct stat sb;
9173 char *ondisk_path = NULL;
9175 err = got_worktree_resolve_path(path, worktree, p);
9176 if (err)
9177 return err;
9179 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9180 *path[0] ? "/" : "", *path) == -1)
9181 return got_error_from_errno("asprintf");
9183 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9184 if (ie) {
9185 *staged_status = get_staged_status(ie);
9186 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9187 repo);
9188 if (err)
9189 goto done;
9190 } else {
9191 *staged_status = GOT_STATUS_NO_CHANGE;
9192 *status = GOT_STATUS_UNVERSIONED;
9193 if (lstat(ondisk_path, &sb) == -1) {
9194 if (errno != ENOENT) {
9195 err = got_error_from_errno2("lstat",
9196 ondisk_path);
9197 goto done;
9199 *status = GOT_STATUS_NONEXISTENT;
9203 done:
9204 free(ondisk_path);
9205 return err;
9208 static const struct got_error *
9209 patch_can_rm(const char *path, unsigned char status,
9210 unsigned char staged_status)
9212 if (status == GOT_STATUS_NONEXISTENT)
9213 return got_error_set_errno(ENOENT, path);
9214 if (status != GOT_STATUS_NO_CHANGE &&
9215 status != GOT_STATUS_ADD &&
9216 status != GOT_STATUS_MODIFY &&
9217 status != GOT_STATUS_MODE_CHANGE)
9218 return got_error_path(path, GOT_ERR_FILE_STATUS);
9219 if (staged_status == GOT_STATUS_DELETE)
9220 return got_error_path(path, GOT_ERR_FILE_STATUS);
9221 return NULL;
9224 static const struct got_error *
9225 patch_can_add(const char *path, unsigned char status)
9227 if (status != GOT_STATUS_NONEXISTENT)
9228 return got_error_path(path, GOT_ERR_FILE_STATUS);
9229 return NULL;
9232 static const struct got_error *
9233 patch_can_edit(const char *path, unsigned char status,
9234 unsigned char staged_status)
9236 if (status == GOT_STATUS_NONEXISTENT)
9237 return got_error_set_errno(ENOENT, path);
9238 if (status != GOT_STATUS_NO_CHANGE &&
9239 status != GOT_STATUS_ADD &&
9240 status != GOT_STATUS_MODIFY)
9241 return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 if (staged_status == GOT_STATUS_DELETE)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 return NULL;
9247 const struct got_error *
9248 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9249 char **fileindex_path, struct got_worktree *worktree)
9251 return open_fileindex(fileindex, fileindex_path, worktree);
9254 const struct got_error *
9255 got_worktree_patch_check_path(const char *old, const char *new,
9256 char **oldpath, char **newpath, struct got_worktree *worktree,
9257 struct got_repository *repo, struct got_fileindex *fileindex)
9259 const struct got_error *err = NULL;
9260 int file_renamed = 0;
9261 unsigned char status_old, staged_status_old;
9262 unsigned char status_new, staged_status_new;
9264 *oldpath = NULL;
9265 *newpath = NULL;
9267 err = patch_check_path(old != NULL ? old : new, oldpath,
9268 &status_old, &staged_status_old, fileindex, worktree, repo);
9269 if (err)
9270 goto done;
9272 err = patch_check_path(new != NULL ? new : old, newpath,
9273 &status_new, &staged_status_new, fileindex, worktree, repo);
9274 if (err)
9275 goto done;
9277 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9278 file_renamed = 1;
9280 if (old != NULL && new == NULL)
9281 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9282 else if (file_renamed) {
9283 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9284 if (err == NULL)
9285 err = patch_can_add(*newpath, status_new);
9286 } else if (old == NULL)
9287 err = patch_can_add(*newpath, status_new);
9288 else
9289 err = patch_can_edit(*newpath, status_new, staged_status_new);
9291 done:
9292 if (err) {
9293 free(*oldpath);
9294 *oldpath = NULL;
9295 free(*newpath);
9296 *newpath = NULL;
9298 return err;
9301 const struct got_error *
9302 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9303 struct got_worktree *worktree, struct got_fileindex *fileindex,
9304 got_worktree_checkout_cb progress_cb, void *progress_arg)
9306 struct schedule_addition_args saa;
9308 memset(&saa, 0, sizeof(saa));
9309 saa.worktree = worktree;
9310 saa.fileindex = fileindex;
9311 saa.progress_cb = progress_cb;
9312 saa.progress_arg = progress_arg;
9313 saa.repo = repo;
9315 return worktree_status(worktree, path, fileindex, repo,
9316 schedule_addition, &saa, NULL, NULL, 1, 0);
9319 const struct got_error *
9320 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9321 struct got_worktree *worktree, struct got_fileindex *fileindex,
9322 got_worktree_delete_cb progress_cb, void *progress_arg)
9324 struct schedule_deletion_args sda;
9326 memset(&sda, 0, sizeof(sda));
9327 sda.worktree = worktree;
9328 sda.fileindex = fileindex;
9329 sda.progress_cb = progress_cb;
9330 sda.progress_arg = progress_arg;
9331 sda.repo = repo;
9332 sda.delete_local_mods = 0;
9333 sda.keep_on_disk = 0;
9334 sda.ignore_missing_paths = 0;
9335 sda.status_codes = NULL;
9337 return worktree_status(worktree, path, fileindex, repo,
9338 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9341 const struct got_error *
9342 got_worktree_patch_complete(struct got_fileindex *fileindex,
9343 const char *fileindex_path)
9345 const struct got_error *err = NULL;
9347 err = sync_fileindex(fileindex, fileindex_path);
9348 got_fileindex_free(fileindex);
9350 return err;