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 "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.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_hash.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;
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1521 FILE *ondisk_file)
1523 const struct got_error *err, *free_err;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1529 FILE *f1 = NULL;
1530 struct got_diffreg_result *diffreg_result = NULL;
1531 struct diff_result *r;
1532 int nchunks_parsed, n, i = 0, ln = 0;
1533 char *line = NULL;
1534 size_t linesize = 0;
1535 ssize_t linelen;
1537 if (*status != GOT_STATUS_MODIFY)
1538 return NULL;
1540 f1 = got_opentemp();
1541 if (f1 == NULL)
1542 return got_error_from_errno("got_opentemp");
1544 if (blob) {
1545 got_object_blob_rewind(blob);
1546 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 if (err)
1548 goto done;
1551 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 if (err)
1554 goto done;
1556 r = diffreg_result->result;
1558 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 struct diff_chunk *c;
1560 struct diff_chunk_context cc = {};
1561 off_t pos;
1564 * We can optimise a little by advancing straight
1565 * to the next chunk if this one has no added lines.
1567 c = diff_chunk_get(r, n);
1569 if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 nchunks_parsed = 1;
1571 continue; /* removed or unchanged lines */
1574 pos = diff_chunk_get_right_start_pos(c);
1575 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 goto done;
1580 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 ln = cc.right.start;
1583 while (ln < cc.right.end) {
1584 linelen = getline(&line, &linesize, ondisk_file);
1585 if (linelen == -1) {
1586 if (feof(ondisk_file))
1587 break;
1588 err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 break;
1592 if (line && strncmp(line, markers[i],
1593 strlen(markers[i])) == 0) {
1594 if (strcmp(markers[i],
1595 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 *status = GOT_STATUS_CONFLICT;
1597 goto done;
1598 } else
1599 i++;
1601 ++ln;
1605 done:
1606 free(line);
1607 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 err = got_error_from_errno("fclose");
1609 free_err = got_diffreg_result_free(diffreg_result);
1610 if (err == NULL)
1611 err = free_err;
1613 return err;
1616 static int
1617 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 static int
1624 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 ie->size == (sb->st_size & 0xffffffff) &&
1631 !xbit_differs(ie, sb->st_mode));
1634 static unsigned char
1635 get_staged_status(struct got_fileindex_entry *ie)
1637 switch (got_fileindex_entry_stage_get(ie)) {
1638 case GOT_FILEIDX_STAGE_ADD:
1639 return GOT_STATUS_ADD;
1640 case GOT_FILEIDX_STAGE_DELETE:
1641 return GOT_STATUS_DELETE;
1642 case GOT_FILEIDX_STAGE_MODIFY:
1643 return GOT_STATUS_MODIFY;
1644 default:
1645 return GOT_STATUS_NO_CHANGE;
1649 static const struct got_error *
1650 get_symlink_modification_status(unsigned char *status,
1651 struct got_fileindex_entry *ie, const char *abspath,
1652 int dirfd, const char *de_name, struct got_blob_object *blob)
1654 const struct got_error *err = NULL;
1655 char target_path[PATH_MAX];
1656 char etarget[PATH_MAX];
1657 ssize_t elen;
1658 size_t len, target_len = 0;
1659 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 *status = GOT_STATUS_NO_CHANGE;
1664 /* Blob object content specifies the target path of the link. */
1665 do {
1666 err = got_object_blob_read_block(&len, blob);
1667 if (err)
1668 return err;
1669 if (len + target_len >= sizeof(target_path)) {
1671 * Should not happen. The blob contents were OK
1672 * when this symlink was installed.
1674 return got_error(GOT_ERR_NO_SPACE);
1676 if (len > 0) {
1677 /* Skip blob object header first time around. */
1678 memcpy(target_path + target_len, buf + hdrlen,
1679 len - hdrlen);
1680 target_len += len - hdrlen;
1681 hdrlen = 0;
1683 } while (len != 0);
1684 target_path[target_len] = '\0';
1686 if (dirfd != -1) {
1687 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 if (elen == -1)
1689 return got_error_from_errno2("readlinkat", abspath);
1690 } else {
1691 elen = readlink(abspath, etarget, sizeof(etarget));
1692 if (elen == -1)
1693 return got_error_from_errno2("readlink", abspath);
1696 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 *status = GOT_STATUS_MODIFY;
1699 return NULL;
1702 static const struct got_error *
1703 get_file_status(unsigned char *status, struct stat *sb,
1704 struct got_fileindex_entry *ie, const char *abspath,
1705 int dirfd, const char *de_name, struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_object_id id;
1709 size_t hdrlen;
1710 int fd = -1, fd1 = -1;
1711 FILE *f = NULL;
1712 uint8_t fbuf[8192];
1713 struct got_blob_object *blob = NULL;
1714 size_t flen, blen;
1715 unsigned char staged_status;
1717 staged_status = get_staged_status(ie);
1718 *status = GOT_STATUS_NO_CHANGE;
1719 memset(sb, 0, sizeof(*sb));
1722 * Whenever the caller provides a directory descriptor and a
1723 * directory entry name for the file, use them! This prevents
1724 * race conditions if filesystem paths change beneath our feet.
1726 if (dirfd != -1) {
1727 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 if (errno == ENOENT) {
1729 if (got_fileindex_entry_has_file_on_disk(ie))
1730 *status = GOT_STATUS_MISSING;
1731 else
1732 *status = GOT_STATUS_DELETE;
1733 goto done;
1735 err = got_error_from_errno2("fstatat", abspath);
1736 goto done;
1738 } else {
1739 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1 && errno != ENOENT &&
1741 !got_err_open_nofollow_on_symlink())
1742 return got_error_from_errno2("open", abspath);
1743 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 if (lstat(abspath, sb) == -1)
1745 return got_error_from_errno2("lstat", abspath);
1746 } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 if (errno == ENOENT) {
1748 if (got_fileindex_entry_has_file_on_disk(ie))
1749 *status = GOT_STATUS_MISSING;
1750 else
1751 *status = GOT_STATUS_DELETE;
1752 goto done;
1754 err = got_error_from_errno2("fstat", abspath);
1755 goto done;
1759 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 *status = GOT_STATUS_OBSTRUCTED;
1761 goto done;
1764 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 *status = GOT_STATUS_DELETE;
1766 goto done;
1767 } else if (!got_fileindex_entry_has_blob(ie) &&
1768 staged_status != GOT_STATUS_ADD) {
1769 *status = GOT_STATUS_ADD;
1770 goto done;
1773 if (!stat_info_differs(ie, sb))
1774 goto done;
1776 if (S_ISLNK(sb->st_mode) &&
1777 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 *status = GOT_STATUS_MODIFY;
1779 goto done;
1782 if (staged_status == GOT_STATUS_MODIFY ||
1783 staged_status == GOT_STATUS_ADD)
1784 got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 else
1786 got_fileindex_entry_get_blob_id(&id, ie);
1788 fd1 = got_opentempfd();
1789 if (fd1 == -1) {
1790 err = got_error_from_errno("got_opentempfd");
1791 goto done;
1793 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 if (err)
1795 goto done;
1797 if (S_ISLNK(sb->st_mode)) {
1798 err = get_symlink_modification_status(status, ie,
1799 abspath, dirfd, de_name, blob);
1800 goto done;
1803 if (dirfd != -1) {
1804 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 if (fd == -1) {
1806 err = got_error_from_errno2("openat", abspath);
1807 goto done;
1811 f = fdopen(fd, "r");
1812 if (f == NULL) {
1813 err = got_error_from_errno2("fdopen", abspath);
1814 goto done;
1816 fd = -1;
1817 hdrlen = got_object_blob_get_hdrlen(blob);
1818 for (;;) {
1819 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 err = got_object_blob_read_block(&blen, blob);
1821 if (err)
1822 goto done;
1823 /* Skip length of blob object header first time around. */
1824 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 if (flen == 0 && ferror(f)) {
1826 err = got_error_from_errno("fread");
1827 goto done;
1829 if (blen - hdrlen == 0) {
1830 if (flen != 0)
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1833 } else if (flen == 0) {
1834 if (blen - hdrlen != 0)
1835 *status = GOT_STATUS_MODIFY;
1836 break;
1837 } else if (blen - hdrlen == flen) {
1838 /* Skip blob object header first time around. */
1839 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1843 } else {
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1847 hdrlen = 0;
1850 if (*status == GOT_STATUS_MODIFY) {
1851 rewind(f);
1852 err = get_modified_file_content_status(status, blob, ie->path,
1853 sb, f);
1854 } else if (xbit_differs(ie, sb->st_mode))
1855 *status = GOT_STATUS_MODE_CHANGE;
1856 done:
1857 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 err = got_error_from_errno("close");
1859 if (blob)
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1865 return err;
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1880 return NULL;
1883 static const struct got_error *
1884 update_blob(struct got_worktree *worktree,
1885 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 struct got_tree_entry *te, const char *path,
1887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 void *progress_arg)
1890 const struct got_error *err = NULL;
1891 struct got_blob_object *blob = NULL;
1892 char *ondisk_path = NULL;
1893 unsigned char status = GOT_STATUS_NO_CHANGE;
1894 struct stat sb;
1895 int fd1 = -1, fd2 = -1;
1897 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 return got_error_from_errno("asprintf");
1900 if (ie) {
1901 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 goto done;
1905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 repo);
1907 if (err)
1908 goto done;
1909 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 sb.st_mode = got_fileindex_perms_to_st(ie);
1911 } else {
1912 if (stat(ondisk_path, &sb) == -1) {
1913 if (errno != ENOENT) {
1914 err = got_error_from_errno2("stat",
1915 ondisk_path);
1916 goto done;
1918 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1919 status = GOT_STATUS_UNVERSIONED;
1920 } else {
1921 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1922 status = GOT_STATUS_UNVERSIONED;
1923 else
1924 status = GOT_STATUS_OBSTRUCTED;
1928 if (status == GOT_STATUS_OBSTRUCTED) {
1929 if (ie)
1930 got_fileindex_entry_mark_skipped(ie);
1931 err = (*progress_cb)(progress_arg, status, path);
1932 goto done;
1934 if (status == GOT_STATUS_CONFLICT) {
1935 if (ie)
1936 got_fileindex_entry_mark_skipped(ie);
1937 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1938 path);
1939 goto done;
1942 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1943 (S_ISLNK(te->mode) ||
1944 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1946 * This is a regular file or an installed bad symlink.
1947 * If the file index indicates that this file is already
1948 * up-to-date with respect to the repository we can skip
1949 * updating contents of this file.
1951 if (got_fileindex_entry_has_commit(ie) &&
1952 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1953 SHA1_DIGEST_LENGTH) == 0) {
1954 /* Same commit. */
1955 err = sync_timestamps(worktree->root_fd,
1956 path, status, ie, &sb);
1957 if (err)
1958 goto done;
1959 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1960 path);
1961 goto done;
1963 if (got_fileindex_entry_has_blob(ie) &&
1964 memcmp(ie->blob_sha1, te->id.sha1,
1965 SHA1_DIGEST_LENGTH) == 0) {
1966 /* Different commit but the same blob. */
1967 err = sync_timestamps(worktree->root_fd,
1968 path, status, ie, &sb);
1969 if (err)
1970 goto done;
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1972 path);
1973 goto done;
1977 fd1 = got_opentempfd();
1978 if (fd1 == -1) {
1979 err = got_error_from_errno("got_opentempfd");
1980 goto done;
1982 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1983 if (err)
1984 goto done;
1986 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1987 int update_timestamps;
1988 struct got_blob_object *blob2 = NULL;
1989 char *label_orig = NULL;
1990 if (got_fileindex_entry_has_blob(ie)) {
1991 fd2 = got_opentempfd();
1992 if (fd2 == -1) {
1993 err = got_error_from_errno("got_opentempfd");
1994 goto done;
1996 struct got_object_id id2;
1997 got_fileindex_entry_get_blob_id(&id2, ie);
1998 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1999 fd2);
2000 if (err)
2001 goto done;
2003 if (got_fileindex_entry_has_commit(ie)) {
2004 char id_str[SHA1_DIGEST_STRING_LENGTH];
2005 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2006 sizeof(id_str)) == NULL) {
2007 err = got_error_path(id_str,
2008 GOT_ERR_BAD_OBJ_ID_STR);
2009 goto done;
2011 if (asprintf(&label_orig, "%s: commit %s",
2012 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2013 err = got_error_from_errno("asprintf");
2014 goto done;
2017 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2018 char *link_target;
2019 err = got_object_blob_read_to_str(&link_target, blob);
2020 if (err)
2021 goto done;
2022 err = merge_symlink(worktree, blob2, ondisk_path, path,
2023 label_orig, link_target, worktree->base_commit_id,
2024 repo, progress_cb, progress_arg);
2025 free(link_target);
2026 } else {
2027 err = merge_blob(&update_timestamps, worktree, blob2,
2028 ondisk_path, path, sb.st_mode, label_orig, blob,
2029 worktree->base_commit_id, repo,
2030 progress_cb, progress_arg);
2032 free(label_orig);
2033 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2034 err = got_error_from_errno("close");
2035 goto done;
2037 if (blob2)
2038 got_object_blob_close(blob2);
2039 if (err)
2040 goto done;
2042 * Do not update timestamps of files with local changes.
2043 * Otherwise, a future status walk would treat them as
2044 * unmodified files again.
2046 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2047 blob->id.sha1, worktree->base_commit_id->sha1,
2048 update_timestamps);
2049 } else if (status == GOT_STATUS_MODE_CHANGE) {
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2052 } else if (status == GOT_STATUS_DELETE) {
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2054 if (err)
2055 goto done;
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 if (err)
2059 goto done;
2060 } else {
2061 int is_bad_symlink = 0;
2062 if (S_ISLNK(te->mode)) {
2063 err = install_symlink(&is_bad_symlink, worktree,
2064 ondisk_path, path, blob,
2065 status == GOT_STATUS_MISSING, 0,
2066 status == GOT_STATUS_UNVERSIONED, 0,
2067 repo, progress_cb, progress_arg);
2068 } else {
2069 err = install_blob(worktree, ondisk_path, path,
2070 te->mode, sb.st_mode, blob,
2071 status == GOT_STATUS_MISSING, 0, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2075 if (err)
2076 goto done;
2078 if (ie) {
2079 err = got_fileindex_entry_update(ie,
2080 worktree->root_fd, path, blob->id.sha1,
2081 worktree->base_commit_id->sha1, 1);
2082 } else {
2083 err = create_fileindex_entry(&ie, fileindex,
2084 worktree->base_commit_id, worktree->root_fd, path,
2085 &blob->id);
2087 if (err)
2088 goto done;
2090 if (is_bad_symlink) {
2091 got_fileindex_entry_filetype_set(ie,
2092 GOT_FILEIDX_MODE_BAD_SYMLINK);
2096 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2097 err = got_error_from_errno("close");
2098 goto done;
2100 got_object_blob_close(blob);
2101 done:
2102 free(ondisk_path);
2103 return err;
2106 static const struct got_error *
2107 remove_ondisk_file(const char *root_path, const char *path)
2109 const struct got_error *err = NULL;
2110 char *ondisk_path = NULL, *parent = NULL;
2112 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2113 return got_error_from_errno("asprintf");
2115 if (unlink(ondisk_path) == -1) {
2116 if (errno != ENOENT)
2117 err = got_error_from_errno2("unlink", ondisk_path);
2118 } else {
2119 size_t root_len = strlen(root_path);
2120 err = got_path_dirname(&parent, ondisk_path);
2121 if (err)
2122 goto done;
2123 while (got_path_cmp(parent, root_path,
2124 strlen(parent), root_len) != 0) {
2125 free(ondisk_path);
2126 ondisk_path = parent;
2127 parent = NULL;
2128 if (rmdir(ondisk_path) == -1) {
2129 if (errno != ENOTEMPTY)
2130 err = got_error_from_errno2("rmdir",
2131 ondisk_path);
2132 break;
2134 err = got_path_dirname(&parent, ondisk_path);
2135 if (err)
2136 break;
2139 done:
2140 free(ondisk_path);
2141 free(parent);
2142 return err;
2145 static const struct got_error *
2146 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 struct got_fileindex_entry *ie, struct got_repository *repo,
2148 got_worktree_checkout_cb progress_cb, void *progress_arg)
2150 const struct got_error *err = NULL;
2151 unsigned char status;
2152 struct stat sb;
2153 char *ondisk_path;
2155 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2156 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2158 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2159 == -1)
2160 return got_error_from_errno("asprintf");
2162 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2163 if (err)
2164 goto done;
2166 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2167 char ondisk_target[PATH_MAX];
2168 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2169 sizeof(ondisk_target));
2170 if (ondisk_len == -1) {
2171 err = got_error_from_errno2("readlink", ondisk_path);
2172 goto done;
2174 ondisk_target[ondisk_len] = '\0';
2175 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2176 NULL, NULL, /* XXX pass common ancestor info? */
2177 ondisk_target, ondisk_path);
2178 if (err)
2179 goto done;
2180 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2181 ie->path);
2182 goto done;
2185 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2186 status == GOT_STATUS_ADD) {
2187 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2188 if (err)
2189 goto done;
2191 * Preserve the working file and change the deleted blob's
2192 * entry into a schedule-add entry.
2194 err = got_fileindex_entry_update(ie, worktree->root_fd,
2195 ie->path, NULL, NULL, 0);
2196 } else {
2197 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2198 if (err)
2199 goto done;
2200 if (status == GOT_STATUS_NO_CHANGE) {
2201 err = remove_ondisk_file(worktree->root_path, ie->path);
2202 if (err)
2203 goto done;
2205 got_fileindex_entry_remove(fileindex, ie);
2207 done:
2208 free(ondisk_path);
2209 return err;
2212 struct diff_cb_arg {
2213 struct got_fileindex *fileindex;
2214 struct got_worktree *worktree;
2215 struct got_repository *repo;
2216 got_worktree_checkout_cb progress_cb;
2217 void *progress_arg;
2218 got_cancel_cb cancel_cb;
2219 void *cancel_arg;
2222 static const struct got_error *
2223 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2224 struct got_tree_entry *te, const char *parent_path)
2226 struct diff_cb_arg *a = arg;
2228 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2229 return got_error(GOT_ERR_CANCELLED);
2231 return update_blob(a->worktree, a->fileindex, ie, te,
2232 ie->path, a->repo, a->progress_cb, a->progress_arg);
2235 static const struct got_error *
2236 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2238 struct diff_cb_arg *a = arg;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 return delete_blob(a->worktree, a->fileindex, ie,
2244 a->repo, a->progress_cb, a->progress_arg);
2247 static const struct got_error *
2248 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2250 struct diff_cb_arg *a = arg;
2251 const struct got_error *err;
2252 char *path;
2254 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2255 return got_error(GOT_ERR_CANCELLED);
2257 if (got_object_tree_entry_is_submodule(te))
2258 return NULL;
2260 if (asprintf(&path, "%s%s%s", parent_path,
2261 parent_path[0] ? "/" : "", te->name)
2262 == -1)
2263 return got_error_from_errno("asprintf");
2265 if (S_ISDIR(te->mode))
2266 err = add_dir_on_disk(a->worktree, path);
2267 else
2268 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2269 a->repo, a->progress_cb, a->progress_arg);
2271 free(path);
2272 return err;
2275 const struct got_error *
2276 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2278 uint32_t uuid_status;
2280 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2281 if (uuid_status != uuid_s_ok) {
2282 *uuidstr = NULL;
2283 return got_error_uuid(uuid_status, "uuid_to_string");
2286 return NULL;
2289 static const struct got_error *
2290 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2292 const struct got_error *err = NULL;
2293 char *uuidstr = NULL;
2295 *refname = NULL;
2297 err = got_worktree_get_uuid(&uuidstr, worktree);
2298 if (err)
2299 return err;
2301 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2302 err = got_error_from_errno("asprintf");
2303 *refname = NULL;
2305 free(uuidstr);
2306 return err;
2309 const struct got_error *
2310 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2311 const char *prefix)
2313 return get_ref_name(refname, worktree, prefix);
2316 const struct got_error *
2317 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2322 static const struct got_error *
2323 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2329 static const struct got_error *
2330 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2335 static const struct got_error *
2336 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree,
2339 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2342 static const struct got_error *
2343 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2345 return get_ref_name(refname, worktree,
2346 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2349 static const struct got_error *
2350 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree,
2353 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2356 static const struct got_error *
2357 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2363 static const struct got_error *
2364 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree,
2367 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2370 static const struct got_error *
2371 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 return get_ref_name(refname, worktree,
2374 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2377 const struct got_error *
2378 got_worktree_get_histedit_script_path(char **path,
2379 struct got_worktree *worktree)
2381 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2382 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2383 *path = NULL;
2384 return got_error_from_errno("asprintf");
2386 return NULL;
2389 static const struct got_error *
2390 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2396 static const struct got_error *
2397 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2404 * Prevent Git's garbage collector from deleting our base commit by
2405 * setting a reference to our base commit's ID.
2407 static const struct got_error *
2408 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2410 const struct got_error *err = NULL;
2411 struct got_reference *ref = NULL;
2412 char *refname;
2414 err = got_worktree_get_base_ref_name(&refname, worktree);
2415 if (err)
2416 return err;
2418 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2419 if (err)
2420 goto done;
2422 err = got_ref_write(ref, repo);
2423 done:
2424 free(refname);
2425 if (ref)
2426 got_ref_close(ref);
2427 return err;
2430 static const struct got_error *
2431 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2433 const struct got_error *err = NULL;
2435 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2436 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2437 err = got_error_from_errno("asprintf");
2438 *fileindex_path = NULL;
2440 return err;
2444 static const struct got_error *
2445 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2446 struct got_worktree *worktree)
2448 const struct got_error *err = NULL;
2449 FILE *index = NULL;
2451 *fileindex_path = NULL;
2452 *fileindex = got_fileindex_alloc();
2453 if (*fileindex == NULL)
2454 return got_error_from_errno("got_fileindex_alloc");
2456 err = get_fileindex_path(fileindex_path, worktree);
2457 if (err)
2458 goto done;
2460 index = fopen(*fileindex_path, "rbe");
2461 if (index == NULL) {
2462 if (errno != ENOENT)
2463 err = got_error_from_errno2("fopen", *fileindex_path);
2464 } else {
2465 err = got_fileindex_read(*fileindex, index);
2466 if (fclose(index) == EOF && err == NULL)
2467 err = got_error_from_errno("fclose");
2469 done:
2470 if (err) {
2471 free(*fileindex_path);
2472 *fileindex_path = NULL;
2473 got_fileindex_free(*fileindex);
2474 *fileindex = NULL;
2476 return err;
2479 struct bump_base_commit_id_arg {
2480 struct got_object_id *base_commit_id;
2481 const char *path;
2482 size_t path_len;
2483 const char *entry_name;
2484 got_worktree_checkout_cb progress_cb;
2485 void *progress_arg;
2488 static const struct got_error *
2489 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2491 const struct got_error *err;
2492 struct bump_base_commit_id_arg *a = arg;
2494 if (a->entry_name) {
2495 if (strcmp(ie->path, a->path) != 0)
2496 return NULL;
2497 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2498 return NULL;
2500 if (got_fileindex_entry_was_skipped(ie))
2501 return NULL;
2503 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2504 SHA1_DIGEST_LENGTH) == 0)
2505 return NULL;
2507 if (a->progress_cb) {
2508 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2509 ie->path);
2510 if (err)
2511 return err;
2513 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2514 return NULL;
2517 /* Bump base commit ID of all files within an updated part of the work tree. */
2518 static const struct got_error *
2519 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2520 struct got_fileindex *fileindex,
2521 got_worktree_checkout_cb progress_cb, void *progress_arg)
2523 struct bump_base_commit_id_arg bbc_arg;
2525 bbc_arg.base_commit_id = worktree->base_commit_id;
2526 bbc_arg.entry_name = NULL;
2527 bbc_arg.path = "";
2528 bbc_arg.path_len = 0;
2529 bbc_arg.progress_cb = progress_cb;
2530 bbc_arg.progress_arg = progress_arg;
2532 return got_fileindex_for_each_entry_safe(fileindex,
2533 bump_base_commit_id, &bbc_arg);
2536 static const struct got_error *
2537 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2539 const struct got_error *err = NULL;
2540 char *new_fileindex_path = NULL;
2541 FILE *new_index = NULL;
2542 struct timespec timeout;
2544 err = got_opentemp_named(&new_fileindex_path, &new_index,
2545 fileindex_path, "");
2546 if (err)
2547 goto done;
2549 err = got_fileindex_write(fileindex, new_index);
2550 if (err)
2551 goto done;
2553 if (rename(new_fileindex_path, fileindex_path) != 0) {
2554 err = got_error_from_errno3("rename", new_fileindex_path,
2555 fileindex_path);
2556 unlink(new_fileindex_path);
2560 * Sleep for a short amount of time to ensure that files modified after
2561 * this program exits have a different time stamp from the one which
2562 * was recorded in the file index.
2564 timeout.tv_sec = 0;
2565 timeout.tv_nsec = 1;
2566 nanosleep(&timeout, NULL);
2567 done:
2568 if (new_index)
2569 fclose(new_index);
2570 free(new_fileindex_path);
2571 return err;
2574 static const struct got_error *
2575 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2576 struct got_object_id **tree_id, const char *wt_relpath,
2577 struct got_commit_object *base_commit, struct got_worktree *worktree,
2578 struct got_repository *repo)
2580 const struct got_error *err = NULL;
2581 struct got_object_id *id = NULL;
2582 char *in_repo_path = NULL;
2583 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2585 *entry_type = GOT_OBJ_TYPE_ANY;
2586 *tree_relpath = NULL;
2587 *tree_id = NULL;
2589 if (wt_relpath[0] == '\0') {
2590 /* Check out all files within the work tree. */
2591 *entry_type = GOT_OBJ_TYPE_TREE;
2592 *tree_relpath = strdup("");
2593 if (*tree_relpath == NULL) {
2594 err = got_error_from_errno("strdup");
2595 goto done;
2597 err = got_object_id_by_path(tree_id, repo, base_commit,
2598 worktree->path_prefix);
2599 if (err)
2600 goto done;
2601 return NULL;
2604 /* Check out a subset of files in the work tree. */
2606 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2607 is_root_wt ? "" : "/", wt_relpath) == -1) {
2608 err = got_error_from_errno("asprintf");
2609 goto done;
2612 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2613 if (err)
2614 goto done;
2616 free(in_repo_path);
2617 in_repo_path = NULL;
2619 err = got_object_get_type(entry_type, repo, id);
2620 if (err)
2621 goto done;
2623 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2624 /* Check out a single file. */
2625 if (strchr(wt_relpath, '/') == NULL) {
2626 /* Check out a single file in work tree's root dir. */
2627 in_repo_path = strdup(worktree->path_prefix);
2628 if (in_repo_path == NULL) {
2629 err = got_error_from_errno("strdup");
2630 goto done;
2632 *tree_relpath = strdup("");
2633 if (*tree_relpath == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2637 } else {
2638 /* Check out a single file in a subdirectory. */
2639 err = got_path_dirname(tree_relpath, wt_relpath);
2640 if (err)
2641 return err;
2642 if (asprintf(&in_repo_path, "%s%s%s",
2643 worktree->path_prefix, is_root_wt ? "" : "/",
2644 *tree_relpath) == -1) {
2645 err = got_error_from_errno("asprintf");
2646 goto done;
2649 err = got_object_id_by_path(tree_id, repo,
2650 base_commit, in_repo_path);
2651 } else {
2652 /* Check out all files within a subdirectory. */
2653 *tree_id = got_object_id_dup(id);
2654 if (*tree_id == NULL) {
2655 err = got_error_from_errno("got_object_id_dup");
2656 goto done;
2658 *tree_relpath = strdup(wt_relpath);
2659 if (*tree_relpath == NULL) {
2660 err = got_error_from_errno("strdup");
2661 goto done;
2664 done:
2665 free(id);
2666 free(in_repo_path);
2667 if (err) {
2668 *entry_type = GOT_OBJ_TYPE_ANY;
2669 free(*tree_relpath);
2670 *tree_relpath = NULL;
2671 free(*tree_id);
2672 *tree_id = NULL;
2674 return err;
2677 static const struct got_error *
2678 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2679 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2680 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2681 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2683 const struct got_error *err = NULL;
2684 struct got_commit_object *commit = NULL;
2685 struct got_tree_object *tree = NULL;
2686 struct got_fileindex_diff_tree_cb diff_cb;
2687 struct diff_cb_arg arg;
2689 err = ref_base_commit(worktree, repo);
2690 if (err) {
2691 if (!(err->code == GOT_ERR_ERRNO &&
2692 (errno == EACCES || errno == EROFS)))
2693 goto done;
2694 err = (*progress_cb)(progress_arg,
2695 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2696 if (err)
2697 return err;
2700 err = got_object_open_as_commit(&commit, repo,
2701 worktree->base_commit_id);
2702 if (err)
2703 goto done;
2705 err = got_object_open_as_tree(&tree, repo, tree_id);
2706 if (err)
2707 goto done;
2709 if (entry_name &&
2710 got_object_tree_find_entry(tree, entry_name) == NULL) {
2711 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2712 goto done;
2715 diff_cb.diff_old_new = diff_old_new;
2716 diff_cb.diff_old = diff_old;
2717 diff_cb.diff_new = diff_new;
2718 arg.fileindex = fileindex;
2719 arg.worktree = worktree;
2720 arg.repo = repo;
2721 arg.progress_cb = progress_cb;
2722 arg.progress_arg = progress_arg;
2723 arg.cancel_cb = cancel_cb;
2724 arg.cancel_arg = cancel_arg;
2725 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2726 entry_name, repo, &diff_cb, &arg);
2727 done:
2728 if (tree)
2729 got_object_tree_close(tree);
2730 if (commit)
2731 got_object_commit_close(commit);
2732 return err;
2735 const struct got_error *
2736 got_worktree_checkout_files(struct got_worktree *worktree,
2737 struct got_pathlist_head *paths, struct got_repository *repo,
2738 got_worktree_checkout_cb progress_cb, void *progress_arg,
2739 got_cancel_cb cancel_cb, void *cancel_arg)
2741 const struct got_error *err = NULL, *sync_err, *unlockerr;
2742 struct got_commit_object *commit = NULL;
2743 struct got_tree_object *tree = NULL;
2744 struct got_fileindex *fileindex = NULL;
2745 char *fileindex_path = NULL;
2746 struct got_pathlist_entry *pe;
2747 struct tree_path_data {
2748 STAILQ_ENTRY(tree_path_data) entry;
2749 struct got_object_id *tree_id;
2750 int entry_type;
2751 char *relpath;
2752 char *entry_name;
2753 } *tpd = NULL;
2754 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2756 STAILQ_INIT(&tree_paths);
2758 err = lock_worktree(worktree, LOCK_EX);
2759 if (err)
2760 return err;
2762 err = got_object_open_as_commit(&commit, repo,
2763 worktree->base_commit_id);
2764 if (err)
2765 goto done;
2767 /* Map all specified paths to in-repository trees. */
2768 TAILQ_FOREACH(pe, paths, entry) {
2769 tpd = malloc(sizeof(*tpd));
2770 if (tpd == NULL) {
2771 err = got_error_from_errno("malloc");
2772 goto done;
2775 err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2777 worktree, repo);
2778 if (err) {
2779 free(tpd);
2780 goto done;
2783 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 err = got_path_basename(&tpd->entry_name, pe->path);
2785 if (err) {
2786 free(tpd->relpath);
2787 free(tpd->tree_id);
2788 free(tpd);
2789 goto done;
2791 } else
2792 tpd->entry_name = NULL;
2794 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 * Read the file index.
2799 * Checking out files is supposed to be an idempotent operation.
2800 * If the on-disk file index is incomplete we will try to complete it.
2802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 if (err)
2804 goto done;
2806 tpd = STAILQ_FIRST(&tree_paths);
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 struct bump_base_commit_id_arg bbc_arg;
2810 err = checkout_files(worktree, fileindex, tpd->relpath,
2811 tpd->tree_id, tpd->entry_name, repo,
2812 progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 if (err)
2814 break;
2816 bbc_arg.base_commit_id = worktree->base_commit_id;
2817 bbc_arg.entry_name = tpd->entry_name;
2818 bbc_arg.path = pe->path;
2819 bbc_arg.path_len = pe->path_len;
2820 bbc_arg.progress_cb = progress_cb;
2821 bbc_arg.progress_arg = progress_arg;
2822 err = got_fileindex_for_each_entry_safe(fileindex,
2823 bump_base_commit_id, &bbc_arg);
2824 if (err)
2825 break;
2827 tpd = STAILQ_NEXT(tpd, entry);
2829 sync_err = sync_fileindex(fileindex, fileindex_path);
2830 if (sync_err && err == NULL)
2831 err = sync_err;
2832 done:
2833 free(fileindex_path);
2834 if (tree)
2835 got_object_tree_close(tree);
2836 if (commit)
2837 got_object_commit_close(commit);
2838 if (fileindex)
2839 got_fileindex_free(fileindex);
2840 while (!STAILQ_EMPTY(&tree_paths)) {
2841 tpd = STAILQ_FIRST(&tree_paths);
2842 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 free(tpd->relpath);
2844 free(tpd->tree_id);
2845 free(tpd);
2847 unlockerr = lock_worktree(worktree, LOCK_SH);
2848 if (unlockerr && err == NULL)
2849 err = unlockerr;
2850 return err;
2853 static const struct got_error *
2854 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2855 struct got_fileindex_entry *ie, const char *ondisk_path,
2856 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2857 int restoring_missing_file, int reverting_versioned_file,
2858 int path_is_unversioned, int allow_bad_symlinks,
2859 struct got_repository *repo,
2860 got_worktree_checkout_cb progress_cb, void *progress_arg)
2862 const struct got_error *err = NULL;
2863 int is_bad_symlink = 0;
2865 if (S_ISLNK(mode2)) {
2866 err = install_symlink(&is_bad_symlink,
2867 worktree, ondisk_path, path2, blob2,
2868 restoring_missing_file,
2869 reverting_versioned_file,
2870 path_is_unversioned, allow_bad_symlinks,
2871 repo, progress_cb, progress_arg);
2872 } else {
2873 err = install_blob(worktree, ondisk_path, path2,
2874 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2875 restoring_missing_file, reverting_versioned_file, 0,
2876 path_is_unversioned, repo, progress_cb, progress_arg);
2878 if (err)
2879 return err;
2880 if (ie == NULL) {
2881 /* Adding an unversioned file. */
2882 err = got_fileindex_entry_alloc(&ie, path2);
2883 if (err)
2884 return err;
2885 err = got_fileindex_entry_update(ie,
2886 worktree->root_fd, path2, NULL, NULL, 1);
2887 if (err) {
2888 got_fileindex_entry_free(ie);
2889 return err;
2891 err = got_fileindex_entry_add(fileindex, ie);
2892 if (err) {
2893 got_fileindex_entry_free(ie);
2894 return err;
2896 } else {
2897 /* Re-adding a locally deleted file. */
2898 err = got_fileindex_entry_update(ie,
2899 worktree->root_fd, path2, ie->blob_sha1,
2900 worktree->base_commit_id->sha1, 0);
2901 if (err)
2902 return err;
2905 if (is_bad_symlink) {
2906 got_fileindex_entry_filetype_set(ie,
2907 GOT_FILEIDX_MODE_BAD_SYMLINK);
2910 return NULL;
2913 struct merge_file_cb_arg {
2914 struct got_worktree *worktree;
2915 struct got_fileindex *fileindex;
2916 got_worktree_checkout_cb progress_cb;
2917 void *progress_arg;
2918 got_cancel_cb cancel_cb;
2919 void *cancel_arg;
2920 const char *label_orig;
2921 struct got_object_id *commit_id2;
2922 int allow_bad_symlinks;
2925 static const struct got_error *
2926 merge_file_cb(void *arg, struct got_blob_object *blob1,
2927 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2928 struct got_object_id *id1, struct got_object_id *id2,
2929 const char *path1, const char *path2,
2930 mode_t mode1, mode_t mode2, struct got_repository *repo)
2932 static const struct got_error *err = NULL;
2933 struct merge_file_cb_arg *a = arg;
2934 struct got_fileindex_entry *ie;
2935 char *ondisk_path = NULL;
2936 struct stat sb;
2937 unsigned char status;
2938 int local_changes_subsumed;
2939 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2940 char *id_str = NULL, *label_deriv2 = NULL;
2942 if (blob1 && blob2) {
2943 ie = got_fileindex_entry_get(a->fileindex, path2,
2944 strlen(path2));
2945 if (ie == NULL)
2946 return (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_MISSING, path2);
2949 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2950 path2) == -1)
2951 return got_error_from_errno("asprintf");
2953 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2954 repo);
2955 if (err)
2956 goto done;
2958 if (status == GOT_STATUS_DELETE) {
2959 err = (*a->progress_cb)(a->progress_arg,
2960 GOT_STATUS_MERGE, path2);
2961 goto done;
2963 if (status != GOT_STATUS_NO_CHANGE &&
2964 status != GOT_STATUS_MODIFY &&
2965 status != GOT_STATUS_CONFLICT &&
2966 status != GOT_STATUS_ADD) {
2967 err = (*a->progress_cb)(a->progress_arg, status, path2);
2968 goto done;
2971 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2972 char *link_target2;
2973 err = got_object_blob_read_to_str(&link_target2, blob2);
2974 if (err)
2975 goto done;
2976 err = merge_symlink(a->worktree, blob1, ondisk_path,
2977 path2, a->label_orig, link_target2, a->commit_id2,
2978 repo, a->progress_cb, a->progress_arg);
2979 free(link_target2);
2980 } else {
2981 int fd;
2983 f_orig = got_opentemp();
2984 if (f_orig == NULL) {
2985 err = got_error_from_errno("got_opentemp");
2986 goto done;
2988 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2989 f_orig, blob1);
2990 if (err)
2991 goto done;
2993 f_deriv2 = got_opentemp();
2994 if (f_deriv2 == NULL)
2995 goto done;
2996 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2997 f_deriv2, blob2);
2998 if (err)
2999 goto done;
3001 fd = open(ondisk_path,
3002 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3003 if (fd == -1) {
3004 err = got_error_from_errno2("open",
3005 ondisk_path);
3006 goto done;
3008 f_deriv = fdopen(fd, "r");
3009 if (f_deriv == NULL) {
3010 err = got_error_from_errno2("fdopen",
3011 ondisk_path);
3012 close(fd);
3013 goto done;
3015 err = got_object_id_str(&id_str, a->commit_id2);
3016 if (err)
3017 goto done;
3018 if (asprintf(&label_deriv2, "%s: commit %s",
3019 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3020 err = got_error_from_errno("asprintf");
3021 goto done;
3023 err = merge_file(&local_changes_subsumed, a->worktree,
3024 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3025 mode2, a->label_orig, NULL, label_deriv2,
3026 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3027 a->progress_cb, a->progress_arg);
3029 } else if (blob1) {
3030 ie = got_fileindex_entry_get(a->fileindex, path1,
3031 strlen(path1));
3032 if (ie == NULL)
3033 return (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_MISSING, path1);
3036 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3037 path1) == -1)
3038 return got_error_from_errno("asprintf");
3040 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3041 repo);
3042 if (err)
3043 goto done;
3045 switch (status) {
3046 case GOT_STATUS_NO_CHANGE:
3047 err = (*a->progress_cb)(a->progress_arg,
3048 GOT_STATUS_DELETE, path1);
3049 if (err)
3050 goto done;
3051 err = remove_ondisk_file(a->worktree->root_path, path1);
3052 if (err)
3053 goto done;
3054 if (ie)
3055 got_fileindex_entry_mark_deleted_from_disk(ie);
3056 break;
3057 case GOT_STATUS_DELETE:
3058 case GOT_STATUS_MISSING:
3059 err = (*a->progress_cb)(a->progress_arg,
3060 GOT_STATUS_DELETE, path1);
3061 if (err)
3062 goto done;
3063 if (ie)
3064 got_fileindex_entry_mark_deleted_from_disk(ie);
3065 break;
3066 case GOT_STATUS_ADD: {
3067 struct got_object_id *id;
3068 FILE *blob1_f;
3069 off_t blob1_size;
3071 * Delete the added file only if its content already
3072 * exists in the repository.
3074 err = got_object_blob_file_create(&id, &blob1_f,
3075 &blob1_size, path1);
3076 if (err)
3077 goto done;
3078 if (got_object_id_cmp(id, id1) == 0) {
3079 err = (*a->progress_cb)(a->progress_arg,
3080 GOT_STATUS_DELETE, path1);
3081 if (err)
3082 goto done;
3083 err = remove_ondisk_file(a->worktree->root_path,
3084 path1);
3085 if (err)
3086 goto done;
3087 if (ie)
3088 got_fileindex_entry_remove(a->fileindex,
3089 ie);
3090 } else {
3091 err = (*a->progress_cb)(a->progress_arg,
3092 GOT_STATUS_CANNOT_DELETE, path1);
3094 if (fclose(blob1_f) == EOF && err == NULL)
3095 err = got_error_from_errno("fclose");
3096 free(id);
3097 if (err)
3098 goto done;
3099 break;
3101 case GOT_STATUS_MODIFY:
3102 case GOT_STATUS_CONFLICT:
3103 err = (*a->progress_cb)(a->progress_arg,
3104 GOT_STATUS_CANNOT_DELETE, path1);
3105 if (err)
3106 goto done;
3107 break;
3108 case GOT_STATUS_OBSTRUCTED:
3109 err = (*a->progress_cb)(a->progress_arg, status, path1);
3110 if (err)
3111 goto done;
3112 break;
3113 default:
3114 break;
3116 } else if (blob2) {
3117 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3118 path2) == -1)
3119 return got_error_from_errno("asprintf");
3120 ie = got_fileindex_entry_get(a->fileindex, path2,
3121 strlen(path2));
3122 if (ie) {
3123 err = get_file_status(&status, &sb, ie, ondisk_path,
3124 -1, NULL, repo);
3125 if (err)
3126 goto done;
3127 if (status != GOT_STATUS_NO_CHANGE &&
3128 status != GOT_STATUS_MODIFY &&
3129 status != GOT_STATUS_CONFLICT &&
3130 status != GOT_STATUS_ADD &&
3131 status != GOT_STATUS_DELETE) {
3132 err = (*a->progress_cb)(a->progress_arg,
3133 status, path2);
3134 goto done;
3136 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3137 char *link_target2;
3138 err = got_object_blob_read_to_str(&link_target2,
3139 blob2);
3140 if (err)
3141 goto done;
3142 err = merge_symlink(a->worktree, NULL,
3143 ondisk_path, path2, a->label_orig,
3144 link_target2, a->commit_id2, repo,
3145 a->progress_cb, a->progress_arg);
3146 free(link_target2);
3147 } else if (S_ISREG(sb.st_mode)) {
3148 err = merge_blob(&local_changes_subsumed,
3149 a->worktree, NULL, ondisk_path, path2,
3150 sb.st_mode, a->label_orig, blob2,
3151 a->commit_id2, repo, a->progress_cb,
3152 a->progress_arg);
3153 } else if (status != GOT_STATUS_DELETE) {
3154 err = got_error_path(ondisk_path,
3155 GOT_ERR_FILE_OBSTRUCTED);
3157 if (err)
3158 goto done;
3159 if (status == GOT_STATUS_DELETE) {
3160 /* Re-add file with content from new blob. */
3161 err = add_file(a->worktree, a->fileindex, ie,
3162 ondisk_path, path2, blob2, mode2,
3163 0, 0, 0, a->allow_bad_symlinks,
3164 repo, a->progress_cb, a->progress_arg);
3165 if (err)
3166 goto done;
3168 } else {
3169 err = add_file(a->worktree, a->fileindex, NULL,
3170 ondisk_path, path2, blob2, mode2,
3171 0, 0, 1, a->allow_bad_symlinks,
3172 repo, a->progress_cb, a->progress_arg);
3173 if (err)
3174 goto done;
3177 done:
3178 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3179 err = got_error_from_errno("fclose");
3180 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3181 err = got_error_from_errno("fclose");
3182 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3183 err = got_error_from_errno("fclose");
3184 free(id_str);
3185 free(label_deriv2);
3186 free(ondisk_path);
3187 return err;
3190 static const struct got_error *
3191 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3193 struct got_worktree *worktree = arg;
3195 /* Reject merges into a work tree with mixed base commits. */
3196 if (got_fileindex_entry_has_commit(ie) &&
3197 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3198 SHA1_DIGEST_LENGTH) != 0)
3199 return got_error(GOT_ERR_MIXED_COMMITS);
3201 return NULL;
3204 struct check_merge_conflicts_arg {
3205 struct got_worktree *worktree;
3206 struct got_fileindex *fileindex;
3207 struct got_repository *repo;
3210 static const struct got_error *
3211 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3212 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3213 struct got_object_id *id1, struct got_object_id *id2,
3214 const char *path1, const char *path2,
3215 mode_t mode1, mode_t mode2, struct got_repository *repo)
3217 const struct got_error *err = NULL;
3218 struct check_merge_conflicts_arg *a = arg;
3219 unsigned char status;
3220 struct stat sb;
3221 struct got_fileindex_entry *ie;
3222 const char *path = path2 ? path2 : path1;
3223 struct got_object_id *id = id2 ? id2 : id1;
3224 char *ondisk_path;
3226 if (id == NULL)
3227 return NULL;
3229 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3230 if (ie == NULL)
3231 return NULL;
3233 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3234 == -1)
3235 return got_error_from_errno("asprintf");
3237 /* Reject merges into a work tree with conflicted files. */
3238 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3239 free(ondisk_path);
3240 if (err)
3241 return err;
3242 if (status == GOT_STATUS_CONFLICT)
3243 return got_error(GOT_ERR_CONFLICTS);
3245 return NULL;
3248 static const struct got_error *
3249 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3250 const char *fileindex_path, struct got_object_id *commit_id1,
3251 struct got_object_id *commit_id2, struct got_repository *repo,
3252 got_worktree_checkout_cb progress_cb, void *progress_arg,
3253 got_cancel_cb cancel_cb, void *cancel_arg)
3255 const struct got_error *err = NULL, *sync_err;
3256 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3257 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3258 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3259 struct check_merge_conflicts_arg cmc_arg;
3260 struct merge_file_cb_arg arg;
3261 char *label_orig = NULL;
3262 FILE *f1 = NULL, *f2 = NULL;
3263 int fd1 = -1, fd2 = -1;
3265 if (commit_id1) {
3266 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3267 if (err)
3268 goto done;
3269 err = got_object_id_by_path(&tree_id1, repo, commit1,
3270 worktree->path_prefix);
3271 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3272 goto done;
3274 if (tree_id1) {
3275 char *id_str;
3277 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3278 if (err)
3279 goto done;
3281 err = got_object_id_str(&id_str, commit_id1);
3282 if (err)
3283 goto done;
3285 if (asprintf(&label_orig, "%s: commit %s",
3286 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3287 err = got_error_from_errno("asprintf");
3288 free(id_str);
3289 goto done;
3291 free(id_str);
3293 f1 = got_opentemp();
3294 if (f1 == NULL) {
3295 err = got_error_from_errno("got_opentemp");
3296 goto done;
3300 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3301 if (err)
3302 goto done;
3304 err = got_object_id_by_path(&tree_id2, repo, commit2,
3305 worktree->path_prefix);
3306 if (err)
3307 goto done;
3309 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3310 if (err)
3311 goto done;
3313 f2 = got_opentemp();
3314 if (f2 == NULL) {
3315 err = got_error_from_errno("got_opentemp");
3316 goto done;
3319 fd1 = got_opentempfd();
3320 if (fd1 == -1) {
3321 err = got_error_from_errno("got_opentempfd");
3322 goto done;
3325 fd2 = got_opentempfd();
3326 if (fd2 == -1) {
3327 err = got_error_from_errno("got_opentempfd");
3328 goto done;
3331 cmc_arg.worktree = worktree;
3332 cmc_arg.fileindex = fileindex;
3333 cmc_arg.repo = repo;
3334 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3335 check_merge_conflicts, &cmc_arg, 0);
3336 if (err)
3337 goto done;
3339 arg.worktree = worktree;
3340 arg.fileindex = fileindex;
3341 arg.progress_cb = progress_cb;
3342 arg.progress_arg = progress_arg;
3343 arg.cancel_cb = cancel_cb;
3344 arg.cancel_arg = cancel_arg;
3345 arg.label_orig = label_orig;
3346 arg.commit_id2 = commit_id2;
3347 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3348 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3349 merge_file_cb, &arg, 1);
3350 sync_err = sync_fileindex(fileindex, fileindex_path);
3351 if (sync_err && err == NULL)
3352 err = sync_err;
3353 done:
3354 if (commit1)
3355 got_object_commit_close(commit1);
3356 if (commit2)
3357 got_object_commit_close(commit2);
3358 if (tree1)
3359 got_object_tree_close(tree1);
3360 if (tree2)
3361 got_object_tree_close(tree2);
3362 if (f1 && fclose(f1) == EOF && err == NULL)
3363 err = got_error_from_errno("fclose");
3364 if (f2 && fclose(f2) == EOF && err == NULL)
3365 err = got_error_from_errno("fclose");
3366 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3367 err = got_error_from_errno("close");
3368 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3369 err = got_error_from_errno("close");
3370 free(label_orig);
3371 return err;
3374 const struct got_error *
3375 got_worktree_merge_files(struct got_worktree *worktree,
3376 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3377 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3378 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3380 const struct got_error *err, *unlockerr;
3381 char *fileindex_path = NULL;
3382 struct got_fileindex *fileindex = NULL;
3384 err = lock_worktree(worktree, LOCK_EX);
3385 if (err)
3386 return err;
3388 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3389 if (err)
3390 goto done;
3392 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3393 worktree);
3394 if (err)
3395 goto done;
3397 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3398 commit_id2, repo, progress_cb, progress_arg,
3399 cancel_cb, cancel_arg);
3400 done:
3401 if (fileindex)
3402 got_fileindex_free(fileindex);
3403 free(fileindex_path);
3404 unlockerr = lock_worktree(worktree, LOCK_SH);
3405 if (unlockerr && err == NULL)
3406 err = unlockerr;
3407 return err;
3410 struct diff_dir_cb_arg {
3411 struct got_fileindex *fileindex;
3412 struct got_worktree *worktree;
3413 const char *status_path;
3414 size_t status_path_len;
3415 struct got_repository *repo;
3416 got_worktree_status_cb status_cb;
3417 void *status_arg;
3418 got_cancel_cb cancel_cb;
3419 void *cancel_arg;
3420 /* A pathlist containing per-directory pathlists of ignore patterns. */
3421 struct got_pathlist_head *ignores;
3422 int report_unchanged;
3423 int no_ignores;
3426 static const struct got_error *
3427 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3428 int dirfd, const char *de_name,
3429 got_worktree_status_cb status_cb, void *status_arg,
3430 struct got_repository *repo, int report_unchanged)
3432 const struct got_error *err = NULL;
3433 unsigned char status = GOT_STATUS_NO_CHANGE;
3434 unsigned char staged_status;
3435 struct stat sb;
3436 struct got_object_id blob_id, commit_id, staged_blob_id;
3437 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3438 struct got_object_id *staged_blob_idp = NULL;
3440 staged_status = get_staged_status(ie);
3441 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3442 if (err)
3443 return err;
3445 if (status == GOT_STATUS_NO_CHANGE &&
3446 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3447 return NULL;
3449 if (got_fileindex_entry_has_blob(ie))
3450 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3451 if (got_fileindex_entry_has_commit(ie))
3452 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3453 if (staged_status == GOT_STATUS_ADD ||
3454 staged_status == GOT_STATUS_MODIFY) {
3455 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3456 &staged_blob_id, ie);
3459 return (*status_cb)(status_arg, status, staged_status,
3460 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3463 static const struct got_error *
3464 status_old_new(void *arg, struct got_fileindex_entry *ie,
3465 struct dirent *de, const char *parent_path, int dirfd)
3467 const struct got_error *err = NULL;
3468 struct diff_dir_cb_arg *a = arg;
3469 char *abspath;
3471 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3472 return got_error(GOT_ERR_CANCELLED);
3474 if (got_path_cmp(parent_path, a->status_path,
3475 strlen(parent_path), a->status_path_len) != 0 &&
3476 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3477 return NULL;
3479 if (parent_path[0]) {
3480 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3481 parent_path, de->d_name) == -1)
3482 return got_error_from_errno("asprintf");
3483 } else {
3484 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3485 de->d_name) == -1)
3486 return got_error_from_errno("asprintf");
3489 err = report_file_status(ie, abspath, dirfd, de->d_name,
3490 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3491 free(abspath);
3492 return err;
3495 static const struct got_error *
3496 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3498 struct diff_dir_cb_arg *a = arg;
3499 struct got_object_id blob_id, commit_id;
3500 unsigned char status;
3502 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3503 return got_error(GOT_ERR_CANCELLED);
3505 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3506 return NULL;
3508 got_fileindex_entry_get_blob_id(&blob_id, ie);
3509 got_fileindex_entry_get_commit_id(&commit_id, ie);
3510 if (got_fileindex_entry_has_file_on_disk(ie))
3511 status = GOT_STATUS_MISSING;
3512 else
3513 status = GOT_STATUS_DELETE;
3514 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3515 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3518 static void
3519 free_ignores(struct got_pathlist_head *ignores)
3521 struct got_pathlist_entry *pe;
3523 TAILQ_FOREACH(pe, ignores, entry) {
3524 struct got_pathlist_head *ignorelist = pe->data;
3526 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3528 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3531 static const struct got_error *
3532 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3534 const struct got_error *err = NULL;
3535 struct got_pathlist_entry *pe = NULL;
3536 struct got_pathlist_head *ignorelist;
3537 char *line = NULL, *pattern, *dirpath = NULL;
3538 size_t linesize = 0;
3539 ssize_t linelen;
3541 ignorelist = calloc(1, sizeof(*ignorelist));
3542 if (ignorelist == NULL)
3543 return got_error_from_errno("calloc");
3544 TAILQ_INIT(ignorelist);
3546 while ((linelen = getline(&line, &linesize, f)) != -1) {
3547 if (linelen > 0 && line[linelen - 1] == '\n')
3548 line[linelen - 1] = '\0';
3550 /* Git's ignores may contain comments. */
3551 if (line[0] == '#')
3552 continue;
3554 /* Git's negated patterns are not (yet?) supported. */
3555 if (line[0] == '!')
3556 continue;
3558 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3559 line) == -1) {
3560 err = got_error_from_errno("asprintf");
3561 goto done;
3563 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3564 if (err)
3565 goto done;
3567 if (ferror(f)) {
3568 err = got_error_from_errno("getline");
3569 goto done;
3572 dirpath = strdup(path);
3573 if (dirpath == NULL) {
3574 err = got_error_from_errno("strdup");
3575 goto done;
3577 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3578 done:
3579 free(line);
3580 if (err || pe == NULL) {
3581 free(dirpath);
3582 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3584 return err;
3587 static int
3588 match_path(const char *pattern, size_t pattern_len, const char *path,
3589 int flags)
3591 char buf[PATH_MAX];
3594 * Trailing slashes signify directories.
3595 * Append a * to make such patterns conform to fnmatch rules.
3597 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3598 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3599 return FNM_NOMATCH; /* XXX */
3601 return fnmatch(buf, path, flags);
3604 return fnmatch(pattern, path, flags);
3607 static int
3608 match_ignores(struct got_pathlist_head *ignores, const char *path)
3610 struct got_pathlist_entry *pe;
3612 /* Handle patterns which match in all directories. */
3613 TAILQ_FOREACH(pe, ignores, entry) {
3614 struct got_pathlist_head *ignorelist = pe->data;
3615 struct got_pathlist_entry *pi;
3617 TAILQ_FOREACH(pi, ignorelist, entry) {
3618 const char *p;
3620 if (pi->path_len < 3 ||
3621 strncmp(pi->path, "**/", 3) != 0)
3622 continue;
3623 p = path;
3624 while (*p) {
3625 if (match_path(pi->path + 3,
3626 pi->path_len - 3, p,
3627 FNM_PATHNAME | FNM_LEADING_DIR)) {
3628 /* Retry in next directory. */
3629 while (*p && *p != '/')
3630 p++;
3631 while (*p == '/')
3632 p++;
3633 continue;
3635 return 1;
3641 * The ignores pathlist contains ignore lists from children before
3642 * parents, so we can find the most specific ignorelist by walking
3643 * ignores backwards.
3645 pe = TAILQ_LAST(ignores, got_pathlist_head);
3646 while (pe) {
3647 if (got_path_is_child(path, pe->path, pe->path_len)) {
3648 struct got_pathlist_head *ignorelist = pe->data;
3649 struct got_pathlist_entry *pi;
3650 TAILQ_FOREACH(pi, ignorelist, entry) {
3651 int flags = FNM_LEADING_DIR;
3652 if (strstr(pi->path, "/**/") == NULL)
3653 flags |= FNM_PATHNAME;
3654 if (match_path(pi->path, pi->path_len,
3655 path, flags))
3656 continue;
3657 return 1;
3660 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3663 return 0;
3666 static const struct got_error *
3667 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3668 const char *path, int dirfd, const char *ignores_filename)
3670 const struct got_error *err = NULL;
3671 char *ignorespath;
3672 int fd = -1;
3673 FILE *ignoresfile = NULL;
3675 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3676 path[0] ? "/" : "", ignores_filename) == -1)
3677 return got_error_from_errno("asprintf");
3679 if (dirfd != -1) {
3680 fd = openat(dirfd, ignores_filename,
3681 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3682 if (fd == -1) {
3683 if (errno != ENOENT && errno != EACCES)
3684 err = got_error_from_errno2("openat",
3685 ignorespath);
3686 } else {
3687 ignoresfile = fdopen(fd, "r");
3688 if (ignoresfile == NULL)
3689 err = got_error_from_errno2("fdopen",
3690 ignorespath);
3691 else {
3692 fd = -1;
3693 err = read_ignores(ignores, path, ignoresfile);
3696 } else {
3697 ignoresfile = fopen(ignorespath, "re");
3698 if (ignoresfile == NULL) {
3699 if (errno != ENOENT && errno != EACCES)
3700 err = got_error_from_errno2("fopen",
3701 ignorespath);
3702 } else
3703 err = read_ignores(ignores, path, ignoresfile);
3706 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3707 err = got_error_from_errno2("fclose", path);
3708 if (fd != -1 && close(fd) == -1 && err == NULL)
3709 err = got_error_from_errno2("close", path);
3710 free(ignorespath);
3711 return err;
3714 static const struct got_error *
3715 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3716 int dirfd)
3718 const struct got_error *err = NULL;
3719 struct diff_dir_cb_arg *a = arg;
3720 char *path = NULL;
3722 if (ignore != NULL)
3723 *ignore = 0;
3725 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3726 return got_error(GOT_ERR_CANCELLED);
3728 if (parent_path[0]) {
3729 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3730 return got_error_from_errno("asprintf");
3731 } else {
3732 path = de->d_name;
3735 if (de->d_type == DT_DIR) {
3736 if (!a->no_ignores && ignore != NULL &&
3737 match_ignores(a->ignores, path))
3738 *ignore = 1;
3739 } else if (!match_ignores(a->ignores, path) &&
3740 got_path_is_child(path, a->status_path, a->status_path_len))
3741 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3742 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3743 if (parent_path[0])
3744 free(path);
3745 return err;
3748 static const struct got_error *
3749 status_traverse(void *arg, const char *path, int dirfd)
3751 const struct got_error *err = NULL;
3752 struct diff_dir_cb_arg *a = arg;
3754 if (a->no_ignores)
3755 return NULL;
3757 err = add_ignores(a->ignores, a->worktree->root_path,
3758 path, dirfd, ".cvsignore");
3759 if (err)
3760 return err;
3762 err = add_ignores(a->ignores, a->worktree->root_path, path,
3763 dirfd, ".gitignore");
3765 return err;
3768 static const struct got_error *
3769 report_single_file_status(const char *path, const char *ondisk_path,
3770 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3771 void *status_arg, struct got_repository *repo, int report_unchanged,
3772 struct got_pathlist_head *ignores, int no_ignores)
3774 struct got_fileindex_entry *ie;
3775 struct stat sb;
3777 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3778 if (ie)
3779 return report_file_status(ie, ondisk_path, -1, NULL,
3780 status_cb, status_arg, repo, report_unchanged);
3782 if (lstat(ondisk_path, &sb) == -1) {
3783 if (errno != ENOENT)
3784 return got_error_from_errno2("lstat", ondisk_path);
3785 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3786 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3789 if (!no_ignores && match_ignores(ignores, path))
3790 return NULL;
3792 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3793 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3794 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3796 return NULL;
3799 static const struct got_error *
3800 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3801 const char *root_path, const char *path)
3803 const struct got_error *err;
3804 char *parent_path, *next_parent_path = NULL;
3806 err = add_ignores(ignores, root_path, "", -1,
3807 ".cvsignore");
3808 if (err)
3809 return err;
3811 err = add_ignores(ignores, root_path, "", -1,
3812 ".gitignore");
3813 if (err)
3814 return err;
3816 err = got_path_dirname(&parent_path, path);
3817 if (err) {
3818 if (err->code == GOT_ERR_BAD_PATH)
3819 return NULL; /* cannot traverse parent */
3820 return err;
3822 for (;;) {
3823 err = add_ignores(ignores, root_path, parent_path, -1,
3824 ".cvsignore");
3825 if (err)
3826 break;
3827 err = add_ignores(ignores, root_path, parent_path, -1,
3828 ".gitignore");
3829 if (err)
3830 break;
3831 err = got_path_dirname(&next_parent_path, parent_path);
3832 if (err) {
3833 if (err->code == GOT_ERR_BAD_PATH)
3834 err = NULL; /* traversed everything */
3835 break;
3837 if (got_path_is_root_dir(parent_path))
3838 break;
3839 free(parent_path);
3840 parent_path = next_parent_path;
3841 next_parent_path = NULL;
3844 free(parent_path);
3845 free(next_parent_path);
3846 return err;
3849 static const struct got_error *
3850 worktree_status(struct got_worktree *worktree, const char *path,
3851 struct got_fileindex *fileindex, struct got_repository *repo,
3852 got_worktree_status_cb status_cb, void *status_arg,
3853 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3854 int report_unchanged)
3856 const struct got_error *err = NULL;
3857 int fd = -1;
3858 struct got_fileindex_diff_dir_cb fdiff_cb;
3859 struct diff_dir_cb_arg arg;
3860 char *ondisk_path = NULL;
3861 struct got_pathlist_head ignores;
3862 struct got_fileindex_entry *ie;
3864 TAILQ_INIT(&ignores);
3866 if (asprintf(&ondisk_path, "%s%s%s",
3867 worktree->root_path, path[0] ? "/" : "", path) == -1)
3868 return got_error_from_errno("asprintf");
3870 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3871 if (ie) {
3872 err = report_single_file_status(path, ondisk_path,
3873 fileindex, status_cb, status_arg, repo,
3874 report_unchanged, &ignores, no_ignores);
3875 goto done;
3878 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3879 if (fd == -1) {
3880 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3881 !got_err_open_nofollow_on_symlink())
3882 err = got_error_from_errno2("open", ondisk_path);
3883 else {
3884 if (!no_ignores) {
3885 err = add_ignores_from_parent_paths(&ignores,
3886 worktree->root_path, ondisk_path);
3887 if (err)
3888 goto done;
3890 err = report_single_file_status(path, ondisk_path,
3891 fileindex, status_cb, status_arg, repo,
3892 report_unchanged, &ignores, no_ignores);
3894 } else {
3895 fdiff_cb.diff_old_new = status_old_new;
3896 fdiff_cb.diff_old = status_old;
3897 fdiff_cb.diff_new = status_new;
3898 fdiff_cb.diff_traverse = status_traverse;
3899 arg.fileindex = fileindex;
3900 arg.worktree = worktree;
3901 arg.status_path = path;
3902 arg.status_path_len = strlen(path);
3903 arg.repo = repo;
3904 arg.status_cb = status_cb;
3905 arg.status_arg = status_arg;
3906 arg.cancel_cb = cancel_cb;
3907 arg.cancel_arg = cancel_arg;
3908 arg.report_unchanged = report_unchanged;
3909 arg.no_ignores = no_ignores;
3910 if (!no_ignores) {
3911 err = add_ignores_from_parent_paths(&ignores,
3912 worktree->root_path, path);
3913 if (err)
3914 goto done;
3916 arg.ignores = &ignores;
3917 err = got_fileindex_diff_dir(fileindex, fd,
3918 worktree->root_path, path, repo, &fdiff_cb, &arg);
3920 done:
3921 free_ignores(&ignores);
3922 if (fd != -1 && close(fd) == -1 && err == NULL)
3923 err = got_error_from_errno("close");
3924 free(ondisk_path);
3925 return err;
3928 const struct got_error *
3929 got_worktree_status(struct got_worktree *worktree,
3930 struct got_pathlist_head *paths, struct got_repository *repo,
3931 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3932 got_cancel_cb cancel_cb, void *cancel_arg)
3934 const struct got_error *err = NULL;
3935 char *fileindex_path = NULL;
3936 struct got_fileindex *fileindex = NULL;
3937 struct got_pathlist_entry *pe;
3939 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3940 if (err)
3941 return err;
3943 TAILQ_FOREACH(pe, paths, entry) {
3944 err = worktree_status(worktree, pe->path, fileindex, repo,
3945 status_cb, status_arg, cancel_cb, cancel_arg,
3946 no_ignores, 0);
3947 if (err)
3948 break;
3950 free(fileindex_path);
3951 got_fileindex_free(fileindex);
3952 return err;
3955 const struct got_error *
3956 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3957 const char *arg)
3959 const struct got_error *err = NULL;
3960 char *resolved = NULL, *cwd = NULL, *path = NULL;
3961 size_t len;
3962 struct stat sb;
3963 char *abspath = NULL;
3964 char canonpath[PATH_MAX];
3966 *wt_path = NULL;
3968 cwd = getcwd(NULL, 0);
3969 if (cwd == NULL)
3970 return got_error_from_errno("getcwd");
3972 if (lstat(arg, &sb) == -1) {
3973 if (errno != ENOENT) {
3974 err = got_error_from_errno2("lstat", arg);
3975 goto done;
3977 sb.st_mode = 0;
3979 if (S_ISLNK(sb.st_mode)) {
3981 * We cannot use realpath(3) with symlinks since we want to
3982 * operate on the symlink itself.
3983 * But we can make the path absolute, assuming it is relative
3984 * to the current working directory, and then canonicalize it.
3986 if (!got_path_is_absolute(arg)) {
3987 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3988 err = got_error_from_errno("asprintf");
3989 goto done;
3993 err = got_canonpath(abspath ? abspath : arg, canonpath,
3994 sizeof(canonpath));
3995 if (err)
3996 goto done;
3997 resolved = strdup(canonpath);
3998 if (resolved == NULL) {
3999 err = got_error_from_errno("strdup");
4000 goto done;
4002 } else {
4003 resolved = realpath(arg, NULL);
4004 if (resolved == NULL) {
4005 if (errno != ENOENT) {
4006 err = got_error_from_errno2("realpath", arg);
4007 goto done;
4009 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4010 err = got_error_from_errno("asprintf");
4011 goto done;
4013 err = got_canonpath(abspath, canonpath,
4014 sizeof(canonpath));
4015 if (err)
4016 goto done;
4017 resolved = strdup(canonpath);
4018 if (resolved == NULL) {
4019 err = got_error_from_errno("strdup");
4020 goto done;
4025 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4026 strlen(got_worktree_get_root_path(worktree)))) {
4027 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4028 goto done;
4031 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4032 err = got_path_skip_common_ancestor(&path,
4033 got_worktree_get_root_path(worktree), resolved);
4034 if (err)
4035 goto done;
4036 } else {
4037 path = strdup("");
4038 if (path == NULL) {
4039 err = got_error_from_errno("strdup");
4040 goto done;
4044 /* XXX status walk can't deal with trailing slash! */
4045 len = strlen(path);
4046 while (len > 0 && path[len - 1] == '/') {
4047 path[len - 1] = '\0';
4048 len--;
4050 done:
4051 free(abspath);
4052 free(resolved);
4053 free(cwd);
4054 if (err == NULL)
4055 *wt_path = path;
4056 else
4057 free(path);
4058 return err;
4061 struct schedule_addition_args {
4062 struct got_worktree *worktree;
4063 struct got_fileindex *fileindex;
4064 got_worktree_checkout_cb progress_cb;
4065 void *progress_arg;
4066 struct got_repository *repo;
4069 static const struct got_error *
4070 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4071 const char *relpath, struct got_object_id *blob_id,
4072 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4073 int dirfd, const char *de_name)
4075 struct schedule_addition_args *a = arg;
4076 const struct got_error *err = NULL;
4077 struct got_fileindex_entry *ie;
4078 struct stat sb;
4079 char *ondisk_path;
4081 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4082 relpath) == -1)
4083 return got_error_from_errno("asprintf");
4085 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4086 if (ie) {
4087 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4088 de_name, a->repo);
4089 if (err)
4090 goto done;
4091 /* Re-adding an existing entry is a no-op. */
4092 if (status == GOT_STATUS_ADD)
4093 goto done;
4094 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4095 if (err)
4096 goto done;
4099 if (status != GOT_STATUS_UNVERSIONED) {
4100 if (status == GOT_STATUS_NONEXISTENT)
4101 err = got_error_set_errno(ENOENT, ondisk_path);
4102 else
4103 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4104 goto done;
4107 err = got_fileindex_entry_alloc(&ie, relpath);
4108 if (err)
4109 goto done;
4110 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4111 relpath, NULL, NULL, 1);
4112 if (err) {
4113 got_fileindex_entry_free(ie);
4114 goto done;
4116 err = got_fileindex_entry_add(a->fileindex, ie);
4117 if (err) {
4118 got_fileindex_entry_free(ie);
4119 goto done;
4121 done:
4122 free(ondisk_path);
4123 if (err)
4124 return err;
4125 if (status == GOT_STATUS_ADD)
4126 return NULL;
4127 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4130 const struct got_error *
4131 got_worktree_schedule_add(struct got_worktree *worktree,
4132 struct got_pathlist_head *paths,
4133 got_worktree_checkout_cb progress_cb, void *progress_arg,
4134 struct got_repository *repo, int no_ignores)
4136 struct got_fileindex *fileindex = NULL;
4137 char *fileindex_path = NULL;
4138 const struct got_error *err = NULL, *sync_err, *unlockerr;
4139 struct got_pathlist_entry *pe;
4140 struct schedule_addition_args saa;
4142 err = lock_worktree(worktree, LOCK_EX);
4143 if (err)
4144 return err;
4146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4147 if (err)
4148 goto done;
4150 saa.worktree = worktree;
4151 saa.fileindex = fileindex;
4152 saa.progress_cb = progress_cb;
4153 saa.progress_arg = progress_arg;
4154 saa.repo = repo;
4156 TAILQ_FOREACH(pe, paths, entry) {
4157 err = worktree_status(worktree, pe->path, fileindex, repo,
4158 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4159 if (err)
4160 break;
4162 sync_err = sync_fileindex(fileindex, fileindex_path);
4163 if (sync_err && err == NULL)
4164 err = sync_err;
4165 done:
4166 free(fileindex_path);
4167 if (fileindex)
4168 got_fileindex_free(fileindex);
4169 unlockerr = lock_worktree(worktree, LOCK_SH);
4170 if (unlockerr && err == NULL)
4171 err = unlockerr;
4172 return err;
4175 struct schedule_deletion_args {
4176 struct got_worktree *worktree;
4177 struct got_fileindex *fileindex;
4178 got_worktree_delete_cb progress_cb;
4179 void *progress_arg;
4180 struct got_repository *repo;
4181 int delete_local_mods;
4182 int keep_on_disk;
4183 int ignore_missing_paths;
4184 const char *status_codes;
4187 static const struct got_error *
4188 schedule_for_deletion(void *arg, unsigned char status,
4189 unsigned char staged_status, const char *relpath,
4190 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4191 struct got_object_id *commit_id, int dirfd, const char *de_name)
4193 struct schedule_deletion_args *a = arg;
4194 const struct got_error *err = NULL;
4195 struct got_fileindex_entry *ie = NULL;
4196 struct stat sb;
4197 char *ondisk_path;
4199 if (status == GOT_STATUS_NONEXISTENT) {
4200 if (a->ignore_missing_paths)
4201 return NULL;
4202 return got_error_set_errno(ENOENT, relpath);
4205 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4206 if (ie == NULL)
4207 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4209 staged_status = get_staged_status(ie);
4210 if (staged_status != GOT_STATUS_NO_CHANGE) {
4211 if (staged_status == GOT_STATUS_DELETE)
4212 return NULL;
4213 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4216 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4217 relpath) == -1)
4218 return got_error_from_errno("asprintf");
4220 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4221 a->repo);
4222 if (err)
4223 goto done;
4225 if (a->status_codes) {
4226 size_t ncodes = strlen(a->status_codes);
4227 int i;
4228 for (i = 0; i < ncodes ; i++) {
4229 if (status == a->status_codes[i])
4230 break;
4232 if (i == ncodes) {
4233 /* Do not delete files in non-matching status. */
4234 free(ondisk_path);
4235 return NULL;
4237 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4238 a->status_codes[i] != GOT_STATUS_MISSING) {
4239 static char msg[64];
4240 snprintf(msg, sizeof(msg),
4241 "invalid status code '%c'", a->status_codes[i]);
4242 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4243 goto done;
4247 if (status != GOT_STATUS_NO_CHANGE) {
4248 if (status == GOT_STATUS_DELETE)
4249 goto done;
4250 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4251 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4252 goto done;
4254 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4255 err = got_error_set_errno(ENOENT, relpath);
4256 goto done;
4258 if (status != GOT_STATUS_MODIFY &&
4259 status != GOT_STATUS_MISSING) {
4260 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4261 goto done;
4265 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4266 size_t root_len;
4268 if (dirfd != -1) {
4269 if (unlinkat(dirfd, de_name, 0) == -1) {
4270 err = got_error_from_errno2("unlinkat",
4271 ondisk_path);
4272 goto done;
4274 } else if (unlink(ondisk_path) == -1) {
4275 err = got_error_from_errno2("unlink", ondisk_path);
4276 goto done;
4279 root_len = strlen(a->worktree->root_path);
4280 do {
4281 char *parent;
4282 err = got_path_dirname(&parent, ondisk_path);
4283 if (err)
4284 goto done;
4285 free(ondisk_path);
4286 ondisk_path = parent;
4287 if (rmdir(ondisk_path) == -1) {
4288 if (errno != ENOTEMPTY)
4289 err = got_error_from_errno2("rmdir",
4290 ondisk_path);
4291 break;
4293 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4294 strlen(ondisk_path), root_len) != 0);
4297 got_fileindex_entry_mark_deleted_from_disk(ie);
4298 done:
4299 free(ondisk_path);
4300 if (err)
4301 return err;
4302 if (status == GOT_STATUS_DELETE)
4303 return NULL;
4304 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4305 staged_status, relpath);
4308 const struct got_error *
4309 got_worktree_schedule_delete(struct got_worktree *worktree,
4310 struct got_pathlist_head *paths, int delete_local_mods,
4311 const char *status_codes,
4312 got_worktree_delete_cb progress_cb, void *progress_arg,
4313 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4315 struct got_fileindex *fileindex = NULL;
4316 char *fileindex_path = NULL;
4317 const struct got_error *err = NULL, *sync_err, *unlockerr;
4318 struct got_pathlist_entry *pe;
4319 struct schedule_deletion_args sda;
4321 err = lock_worktree(worktree, LOCK_EX);
4322 if (err)
4323 return err;
4325 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4326 if (err)
4327 goto done;
4329 sda.worktree = worktree;
4330 sda.fileindex = fileindex;
4331 sda.progress_cb = progress_cb;
4332 sda.progress_arg = progress_arg;
4333 sda.repo = repo;
4334 sda.delete_local_mods = delete_local_mods;
4335 sda.keep_on_disk = keep_on_disk;
4336 sda.ignore_missing_paths = ignore_missing_paths;
4337 sda.status_codes = status_codes;
4339 TAILQ_FOREACH(pe, paths, entry) {
4340 err = worktree_status(worktree, pe->path, fileindex, repo,
4341 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4342 if (err)
4343 break;
4345 sync_err = sync_fileindex(fileindex, fileindex_path);
4346 if (sync_err && err == NULL)
4347 err = sync_err;
4348 done:
4349 free(fileindex_path);
4350 if (fileindex)
4351 got_fileindex_free(fileindex);
4352 unlockerr = lock_worktree(worktree, LOCK_SH);
4353 if (unlockerr && err == NULL)
4354 err = unlockerr;
4355 return err;
4358 static const struct got_error *
4359 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4361 const struct got_error *err = NULL;
4362 char *line = NULL;
4363 size_t linesize = 0, n;
4364 ssize_t linelen;
4366 linelen = getline(&line, &linesize, infile);
4367 if (linelen == -1) {
4368 if (ferror(infile)) {
4369 err = got_error_from_errno("getline");
4370 goto done;
4372 return NULL;
4374 if (outfile) {
4375 n = fwrite(line, 1, linelen, outfile);
4376 if (n != linelen) {
4377 err = got_ferror(outfile, GOT_ERR_IO);
4378 goto done;
4381 if (rejectfile) {
4382 n = fwrite(line, 1, linelen, rejectfile);
4383 if (n != linelen)
4384 err = got_ferror(rejectfile, GOT_ERR_IO);
4386 done:
4387 free(line);
4388 return err;
4391 static const struct got_error *
4392 skip_one_line(FILE *f)
4394 char *line = NULL;
4395 size_t linesize = 0;
4396 ssize_t linelen;
4398 linelen = getline(&line, &linesize, f);
4399 if (linelen == -1) {
4400 if (ferror(f))
4401 return got_error_from_errno("getline");
4402 return NULL;
4404 free(line);
4405 return NULL;
4408 static const struct got_error *
4409 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4410 int start_old, int end_old, int start_new, int end_new,
4411 FILE *outfile, FILE *rejectfile)
4413 const struct got_error *err;
4415 /* Copy old file's lines leading up to patch. */
4416 while (!feof(f1) && *line_cur1 < start_old) {
4417 err = copy_one_line(f1, outfile, NULL);
4418 if (err)
4419 return err;
4420 (*line_cur1)++;
4422 /* Skip new file's lines leading up to patch. */
4423 while (!feof(f2) && *line_cur2 < start_new) {
4424 if (rejectfile)
4425 err = copy_one_line(f2, NULL, rejectfile);
4426 else
4427 err = skip_one_line(f2);
4428 if (err)
4429 return err;
4430 (*line_cur2)++;
4432 /* Copy patched lines. */
4433 while (!feof(f2) && *line_cur2 <= end_new) {
4434 err = copy_one_line(f2, outfile, NULL);
4435 if (err)
4436 return err;
4437 (*line_cur2)++;
4439 /* Skip over old file's replaced lines. */
4440 while (!feof(f1) && *line_cur1 <= end_old) {
4441 if (rejectfile)
4442 err = copy_one_line(f1, NULL, rejectfile);
4443 else
4444 err = skip_one_line(f1);
4445 if (err)
4446 return err;
4447 (*line_cur1)++;
4450 return NULL;
4453 static const struct got_error *
4454 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4455 FILE *outfile, FILE *rejectfile)
4457 const struct got_error *err;
4459 if (outfile) {
4460 /* Copy old file's lines until EOF. */
4461 while (!feof(f1)) {
4462 err = copy_one_line(f1, outfile, NULL);
4463 if (err)
4464 return err;
4465 (*line_cur1)++;
4468 if (rejectfile) {
4469 /* Copy new file's lines until EOF. */
4470 while (!feof(f2)) {
4471 err = copy_one_line(f2, NULL, rejectfile);
4472 if (err)
4473 return err;
4474 (*line_cur2)++;
4478 return NULL;
4481 static const struct got_error *
4482 apply_or_reject_change(int *choice, int *nchunks_used,
4483 struct diff_result *diff_result, int n,
4484 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4485 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4486 got_worktree_patch_cb patch_cb, void *patch_arg)
4488 const struct got_error *err = NULL;
4489 struct diff_chunk_context cc = {};
4490 int start_old, end_old, start_new, end_new;
4491 FILE *hunkfile;
4492 struct diff_output_unidiff_state *diff_state;
4493 struct diff_input_info diff_info;
4494 int rc;
4496 *choice = GOT_PATCH_CHOICE_NONE;
4498 /* Get changed line numbers without context lines for copy_change(). */
4499 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4500 start_old = cc.left.start;
4501 end_old = cc.left.end;
4502 start_new = cc.right.start;
4503 end_new = cc.right.end;
4505 /* Get the same change with context lines for display. */
4506 memset(&cc, 0, sizeof(cc));
4507 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4509 memset(&diff_info, 0, sizeof(diff_info));
4510 diff_info.left_path = relpath;
4511 diff_info.right_path = relpath;
4513 diff_state = diff_output_unidiff_state_alloc();
4514 if (diff_state == NULL)
4515 return got_error_set_errno(ENOMEM,
4516 "diff_output_unidiff_state_alloc");
4518 hunkfile = got_opentemp();
4519 if (hunkfile == NULL) {
4520 err = got_error_from_errno("got_opentemp");
4521 goto done;
4524 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4525 diff_result, &cc);
4526 if (rc != DIFF_RC_OK) {
4527 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4528 goto done;
4531 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4532 err = got_ferror(hunkfile, GOT_ERR_IO);
4533 goto done;
4536 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4537 hunkfile, changeno, nchanges);
4538 if (err)
4539 goto done;
4541 switch (*choice) {
4542 case GOT_PATCH_CHOICE_YES:
4543 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4544 end_old, start_new, end_new, outfile, rejectfile);
4545 break;
4546 case GOT_PATCH_CHOICE_NO:
4547 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4548 end_old, start_new, end_new, rejectfile, outfile);
4549 break;
4550 case GOT_PATCH_CHOICE_QUIT:
4551 break;
4552 default:
4553 err = got_error(GOT_ERR_PATCH_CHOICE);
4554 break;
4556 done:
4557 diff_output_unidiff_state_free(diff_state);
4558 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4559 err = got_error_from_errno("fclose");
4560 return err;
4563 struct revert_file_args {
4564 struct got_worktree *worktree;
4565 struct got_fileindex *fileindex;
4566 got_worktree_checkout_cb progress_cb;
4567 void *progress_arg;
4568 got_worktree_patch_cb patch_cb;
4569 void *patch_arg;
4570 struct got_repository *repo;
4571 int unlink_added_files;
4574 static const struct got_error *
4575 create_patched_content(char **path_outfile, int reverse_patch,
4576 struct got_object_id *blob_id, const char *path2,
4577 int dirfd2, const char *de_name2,
4578 const char *relpath, struct got_repository *repo,
4579 got_worktree_patch_cb patch_cb, void *patch_arg)
4581 const struct got_error *err, *free_err;
4582 struct got_blob_object *blob = NULL;
4583 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4584 int fd = -1, fd2 = -1;
4585 char link_target[PATH_MAX];
4586 ssize_t link_len = 0;
4587 char *path1 = NULL, *id_str = NULL;
4588 struct stat sb2;
4589 struct got_diffreg_result *diffreg_result = NULL;
4590 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4591 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4593 *path_outfile = NULL;
4595 err = got_object_id_str(&id_str, blob_id);
4596 if (err)
4597 return err;
4599 if (dirfd2 != -1) {
4600 fd2 = openat(dirfd2, de_name2,
4601 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4602 if (fd2 == -1) {
4603 if (!got_err_open_nofollow_on_symlink()) {
4604 err = got_error_from_errno2("openat", path2);
4605 goto done;
4607 link_len = readlinkat(dirfd2, de_name2,
4608 link_target, sizeof(link_target));
4609 if (link_len == -1) {
4610 return got_error_from_errno2("readlinkat",
4611 path2);
4613 sb2.st_mode = S_IFLNK;
4614 sb2.st_size = link_len;
4616 } else {
4617 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4618 if (fd2 == -1) {
4619 if (!got_err_open_nofollow_on_symlink()) {
4620 err = got_error_from_errno2("open", path2);
4621 goto done;
4623 link_len = readlink(path2, link_target,
4624 sizeof(link_target));
4625 if (link_len == -1)
4626 return got_error_from_errno2("readlink", path2);
4627 sb2.st_mode = S_IFLNK;
4628 sb2.st_size = link_len;
4631 if (fd2 != -1) {
4632 if (fstat(fd2, &sb2) == -1) {
4633 err = got_error_from_errno2("fstat", path2);
4634 goto done;
4637 f2 = fdopen(fd2, "r");
4638 if (f2 == NULL) {
4639 err = got_error_from_errno2("fdopen", path2);
4640 goto done;
4642 fd2 = -1;
4643 } else {
4644 size_t n;
4645 f2 = got_opentemp();
4646 if (f2 == NULL) {
4647 err = got_error_from_errno2("got_opentemp", path2);
4648 goto done;
4650 n = fwrite(link_target, 1, link_len, f2);
4651 if (n != link_len) {
4652 err = got_ferror(f2, GOT_ERR_IO);
4653 goto done;
4655 if (fflush(f2) == EOF) {
4656 err = got_error_from_errno("fflush");
4657 goto done;
4659 rewind(f2);
4662 fd = got_opentempfd();
4663 if (fd == -1) {
4664 err = got_error_from_errno("got_opentempfd");
4665 goto done;
4668 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4669 if (err)
4670 goto done;
4672 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4673 if (err)
4674 goto done;
4676 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4677 if (err)
4678 goto done;
4680 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4681 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4682 if (err)
4683 goto done;
4685 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4686 "");
4687 if (err)
4688 goto done;
4690 if (fseek(f1, 0L, SEEK_SET) == -1)
4691 return got_ferror(f1, GOT_ERR_IO);
4692 if (fseek(f2, 0L, SEEK_SET) == -1)
4693 return got_ferror(f2, GOT_ERR_IO);
4695 /* Count the number of actual changes in the diff result. */
4696 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4697 struct diff_chunk_context cc = {};
4698 diff_chunk_context_load_change(&cc, &nchunks_used,
4699 diffreg_result->result, n, 0);
4700 nchanges++;
4702 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4703 int choice;
4704 err = apply_or_reject_change(&choice, &nchunks_used,
4705 diffreg_result->result, n, relpath, f1, f2,
4706 &line_cur1, &line_cur2,
4707 reverse_patch ? NULL : outfile,
4708 reverse_patch ? outfile : NULL,
4709 ++i, nchanges, patch_cb, patch_arg);
4710 if (err)
4711 goto done;
4712 if (choice == GOT_PATCH_CHOICE_YES)
4713 have_content = 1;
4714 else if (choice == GOT_PATCH_CHOICE_QUIT)
4715 break;
4717 if (have_content) {
4718 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4719 reverse_patch ? NULL : outfile,
4720 reverse_patch ? outfile : NULL);
4721 if (err)
4722 goto done;
4724 if (!S_ISLNK(sb2.st_mode)) {
4725 mode_t mode;
4727 mode = apply_umask(sb2.st_mode);
4728 if (fchmod(fileno(outfile), mode) == -1) {
4729 err = got_error_from_errno2("fchmod", path2);
4730 goto done;
4734 done:
4735 free(id_str);
4736 if (fd != -1 && close(fd) == -1 && err == NULL)
4737 err = got_error_from_errno("close");
4738 if (blob)
4739 got_object_blob_close(blob);
4740 free_err = got_diffreg_result_free(diffreg_result);
4741 if (err == NULL)
4742 err = free_err;
4743 if (f1 && fclose(f1) == EOF && err == NULL)
4744 err = got_error_from_errno2("fclose", path1);
4745 if (f2 && fclose(f2) == EOF && err == NULL)
4746 err = got_error_from_errno2("fclose", path2);
4747 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4748 err = got_error_from_errno2("close", path2);
4749 if (outfile && fclose(outfile) == EOF && err == NULL)
4750 err = got_error_from_errno2("fclose", *path_outfile);
4751 if (path1 && unlink(path1) == -1 && err == NULL)
4752 err = got_error_from_errno2("unlink", path1);
4753 if (err || !have_content) {
4754 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4755 err = got_error_from_errno2("unlink", *path_outfile);
4756 free(*path_outfile);
4757 *path_outfile = NULL;
4759 free(path1);
4760 return err;
4763 static const struct got_error *
4764 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4765 const char *relpath, struct got_object_id *blob_id,
4766 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4767 int dirfd, const char *de_name)
4769 struct revert_file_args *a = arg;
4770 const struct got_error *err = NULL;
4771 char *parent_path = NULL;
4772 struct got_fileindex_entry *ie;
4773 struct got_commit_object *base_commit = NULL;
4774 struct got_tree_object *tree = NULL;
4775 struct got_object_id *tree_id = NULL;
4776 const struct got_tree_entry *te = NULL;
4777 char *tree_path = NULL, *te_name;
4778 char *ondisk_path = NULL, *path_content = NULL;
4779 struct got_blob_object *blob = NULL;
4780 int fd = -1;
4782 /* Reverting a staged deletion is a no-op. */
4783 if (status == GOT_STATUS_DELETE &&
4784 staged_status != GOT_STATUS_NO_CHANGE)
4785 return NULL;
4787 if (status == GOT_STATUS_UNVERSIONED)
4788 return (*a->progress_cb)(a->progress_arg,
4789 GOT_STATUS_UNVERSIONED, relpath);
4791 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4792 if (ie == NULL)
4793 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4795 /* Construct in-repository path of tree which contains this blob. */
4796 err = got_path_dirname(&parent_path, ie->path);
4797 if (err) {
4798 if (err->code != GOT_ERR_BAD_PATH)
4799 goto done;
4800 parent_path = strdup("/");
4801 if (parent_path == NULL) {
4802 err = got_error_from_errno("strdup");
4803 goto done;
4806 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4807 tree_path = strdup(parent_path);
4808 if (tree_path == NULL) {
4809 err = got_error_from_errno("strdup");
4810 goto done;
4812 } else {
4813 if (got_path_is_root_dir(parent_path)) {
4814 tree_path = strdup(a->worktree->path_prefix);
4815 if (tree_path == NULL) {
4816 err = got_error_from_errno("strdup");
4817 goto done;
4819 } else {
4820 if (asprintf(&tree_path, "%s/%s",
4821 a->worktree->path_prefix, parent_path) == -1) {
4822 err = got_error_from_errno("asprintf");
4823 goto done;
4828 err = got_object_open_as_commit(&base_commit, a->repo,
4829 a->worktree->base_commit_id);
4830 if (err)
4831 goto done;
4833 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4834 if (err) {
4835 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4836 (status == GOT_STATUS_ADD ||
4837 staged_status == GOT_STATUS_ADD)))
4838 goto done;
4839 } else {
4840 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4841 if (err)
4842 goto done;
4844 err = got_path_basename(&te_name, ie->path);
4845 if (err)
4846 goto done;
4848 te = got_object_tree_find_entry(tree, te_name);
4849 free(te_name);
4850 if (te == NULL && status != GOT_STATUS_ADD &&
4851 staged_status != GOT_STATUS_ADD) {
4852 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4853 goto done;
4857 switch (status) {
4858 case GOT_STATUS_ADD:
4859 if (a->patch_cb) {
4860 int choice = GOT_PATCH_CHOICE_NONE;
4861 err = (*a->patch_cb)(&choice, a->patch_arg,
4862 status, ie->path, NULL, 1, 1);
4863 if (err)
4864 goto done;
4865 if (choice != GOT_PATCH_CHOICE_YES)
4866 break;
4868 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4869 ie->path);
4870 if (err)
4871 goto done;
4872 got_fileindex_entry_remove(a->fileindex, ie);
4873 if (a->unlink_added_files) {
4874 if (asprintf(&ondisk_path, "%s/%s",
4875 got_worktree_get_root_path(a->worktree),
4876 relpath) == -1) {
4877 err = got_error_from_errno("asprintf");
4878 goto done;
4880 if (unlink(ondisk_path) == -1) {
4881 err = got_error_from_errno2("unlink",
4882 ondisk_path);
4883 break;
4886 break;
4887 case GOT_STATUS_DELETE:
4888 if (a->patch_cb) {
4889 int choice = GOT_PATCH_CHOICE_NONE;
4890 err = (*a->patch_cb)(&choice, a->patch_arg,
4891 status, ie->path, NULL, 1, 1);
4892 if (err)
4893 goto done;
4894 if (choice != GOT_PATCH_CHOICE_YES)
4895 break;
4897 /* fall through */
4898 case GOT_STATUS_MODIFY:
4899 case GOT_STATUS_MODE_CHANGE:
4900 case GOT_STATUS_CONFLICT:
4901 case GOT_STATUS_MISSING: {
4902 struct got_object_id id;
4903 if (staged_status == GOT_STATUS_ADD ||
4904 staged_status == GOT_STATUS_MODIFY)
4905 got_fileindex_entry_get_staged_blob_id(&id, ie);
4906 else
4907 got_fileindex_entry_get_blob_id(&id, ie);
4908 fd = got_opentempfd();
4909 if (fd == -1) {
4910 err = got_error_from_errno("got_opentempfd");
4911 goto done;
4914 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4915 if (err)
4916 goto done;
4918 if (asprintf(&ondisk_path, "%s/%s",
4919 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4920 err = got_error_from_errno("asprintf");
4921 goto done;
4924 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4925 status == GOT_STATUS_CONFLICT)) {
4926 int is_bad_symlink = 0;
4927 err = create_patched_content(&path_content, 1, &id,
4928 ondisk_path, dirfd, de_name, ie->path, a->repo,
4929 a->patch_cb, a->patch_arg);
4930 if (err || path_content == NULL)
4931 break;
4932 if (te && S_ISLNK(te->mode)) {
4933 if (unlink(path_content) == -1) {
4934 err = got_error_from_errno2("unlink",
4935 path_content);
4936 break;
4938 err = install_symlink(&is_bad_symlink,
4939 a->worktree, ondisk_path, ie->path,
4940 blob, 0, 1, 0, 0, a->repo,
4941 a->progress_cb, a->progress_arg);
4942 } else {
4943 if (rename(path_content, ondisk_path) == -1) {
4944 err = got_error_from_errno3("rename",
4945 path_content, ondisk_path);
4946 goto done;
4949 } else {
4950 int is_bad_symlink = 0;
4951 if (te && S_ISLNK(te->mode)) {
4952 err = install_symlink(&is_bad_symlink,
4953 a->worktree, ondisk_path, ie->path,
4954 blob, 0, 1, 0, 0, a->repo,
4955 a->progress_cb, a->progress_arg);
4956 } else {
4957 err = install_blob(a->worktree, ondisk_path,
4958 ie->path,
4959 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4960 got_fileindex_perms_to_st(ie), blob,
4961 0, 1, 0, 0, a->repo,
4962 a->progress_cb, a->progress_arg);
4964 if (err)
4965 goto done;
4966 if (status == GOT_STATUS_DELETE ||
4967 status == GOT_STATUS_MODE_CHANGE) {
4968 err = got_fileindex_entry_update(ie,
4969 a->worktree->root_fd, relpath,
4970 blob->id.sha1,
4971 a->worktree->base_commit_id->sha1, 1);
4972 if (err)
4973 goto done;
4975 if (is_bad_symlink) {
4976 got_fileindex_entry_filetype_set(ie,
4977 GOT_FILEIDX_MODE_BAD_SYMLINK);
4980 break;
4982 default:
4983 break;
4985 done:
4986 free(ondisk_path);
4987 free(path_content);
4988 free(parent_path);
4989 free(tree_path);
4990 if (fd != -1 && close(fd) == -1 && err == NULL)
4991 err = got_error_from_errno("close");
4992 if (blob)
4993 got_object_blob_close(blob);
4994 if (tree)
4995 got_object_tree_close(tree);
4996 free(tree_id);
4997 if (base_commit)
4998 got_object_commit_close(base_commit);
4999 return err;
5002 const struct got_error *
5003 got_worktree_revert(struct got_worktree *worktree,
5004 struct got_pathlist_head *paths,
5005 got_worktree_checkout_cb progress_cb, void *progress_arg,
5006 got_worktree_patch_cb patch_cb, void *patch_arg,
5007 struct got_repository *repo)
5009 struct got_fileindex *fileindex = NULL;
5010 char *fileindex_path = NULL;
5011 const struct got_error *err = NULL, *unlockerr = NULL;
5012 const struct got_error *sync_err = NULL;
5013 struct got_pathlist_entry *pe;
5014 struct revert_file_args rfa;
5016 err = lock_worktree(worktree, LOCK_EX);
5017 if (err)
5018 return err;
5020 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5021 if (err)
5022 goto done;
5024 rfa.worktree = worktree;
5025 rfa.fileindex = fileindex;
5026 rfa.progress_cb = progress_cb;
5027 rfa.progress_arg = progress_arg;
5028 rfa.patch_cb = patch_cb;
5029 rfa.patch_arg = patch_arg;
5030 rfa.repo = repo;
5031 rfa.unlink_added_files = 0;
5032 TAILQ_FOREACH(pe, paths, entry) {
5033 err = worktree_status(worktree, pe->path, fileindex, repo,
5034 revert_file, &rfa, NULL, NULL, 1, 0);
5035 if (err)
5036 break;
5038 sync_err = sync_fileindex(fileindex, fileindex_path);
5039 if (sync_err && err == NULL)
5040 err = sync_err;
5041 done:
5042 free(fileindex_path);
5043 if (fileindex)
5044 got_fileindex_free(fileindex);
5045 unlockerr = lock_worktree(worktree, LOCK_SH);
5046 if (unlockerr && err == NULL)
5047 err = unlockerr;
5048 return err;
5051 static void
5052 free_commitable(struct got_commitable *ct)
5054 free(ct->path);
5055 free(ct->in_repo_path);
5056 free(ct->ondisk_path);
5057 free(ct->blob_id);
5058 free(ct->base_blob_id);
5059 free(ct->staged_blob_id);
5060 free(ct->base_commit_id);
5061 free(ct);
5064 struct collect_commitables_arg {
5065 struct got_pathlist_head *commitable_paths;
5066 struct got_repository *repo;
5067 struct got_worktree *worktree;
5068 struct got_fileindex *fileindex;
5069 int have_staged_files;
5070 int allow_bad_symlinks;
5071 int diff_header_shown;
5072 int commit_conflicts;
5073 FILE *diff_outfile;
5074 FILE *f1;
5075 FILE *f2;
5079 * Create a file which contains the target path of a symlink so we can feed
5080 * it as content to the diff engine.
5082 static const struct got_error *
5083 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5084 const char *abspath)
5086 const struct got_error *err = NULL;
5087 char target_path[PATH_MAX];
5088 ssize_t target_len, outlen;
5090 *fd = -1;
5092 if (dirfd != -1) {
5093 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5094 if (target_len == -1)
5095 return got_error_from_errno2("readlinkat", abspath);
5096 } else {
5097 target_len = readlink(abspath, target_path, PATH_MAX);
5098 if (target_len == -1)
5099 return got_error_from_errno2("readlink", abspath);
5102 *fd = got_opentempfd();
5103 if (*fd == -1)
5104 return got_error_from_errno("got_opentempfd");
5106 outlen = write(*fd, target_path, target_len);
5107 if (outlen == -1) {
5108 err = got_error_from_errno("got_opentempfd");
5109 goto done;
5112 if (lseek(*fd, 0, SEEK_SET) == -1) {
5113 err = got_error_from_errno2("lseek", abspath);
5114 goto done;
5116 done:
5117 if (err) {
5118 close(*fd);
5119 *fd = -1;
5121 return err;
5124 static const struct got_error *
5125 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5126 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5127 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5129 const struct got_error *err = NULL;
5130 struct got_blob_object *blob1 = NULL;
5131 int fd = -1, fd1 = -1, fd2 = -1;
5132 FILE *ondisk_file = NULL;
5133 char *label1 = NULL;
5134 struct stat sb;
5135 off_t size1 = 0;
5136 int f2_exists = 0;
5137 char *id_str = NULL;
5139 memset(&sb, 0, sizeof(sb));
5141 if (diff_staged) {
5142 if (ct->staged_status != GOT_STATUS_MODIFY &&
5143 ct->staged_status != GOT_STATUS_ADD &&
5144 ct->staged_status != GOT_STATUS_DELETE)
5145 return NULL;
5146 } else {
5147 if (ct->status != GOT_STATUS_MODIFY &&
5148 ct->status != GOT_STATUS_ADD &&
5149 ct->status != GOT_STATUS_DELETE &&
5150 ct->status != GOT_STATUS_CONFLICT)
5151 return NULL;
5154 err = got_opentemp_truncate(f1);
5155 if (err)
5156 return got_error_from_errno("got_opentemp_truncate");
5157 err = got_opentemp_truncate(f2);
5158 if (err)
5159 return got_error_from_errno("got_opentemp_truncate");
5161 if (!*diff_header_shown) {
5162 err = got_object_id_str(&id_str, worktree->base_commit_id);
5163 if (err)
5164 return err;
5165 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5166 got_worktree_get_root_path(worktree));
5167 fprintf(diff_outfile, "commit - %s\n", id_str);
5168 fprintf(diff_outfile, "path + %s%s\n",
5169 got_worktree_get_root_path(worktree),
5170 diff_staged ? " (staged changes)" : "");
5171 *diff_header_shown = 1;
5174 if (diff_staged) {
5175 const char *label1 = NULL, *label2 = NULL;
5176 switch (ct->staged_status) {
5177 case GOT_STATUS_MODIFY:
5178 label1 = ct->path;
5179 label2 = ct->path;
5180 break;
5181 case GOT_STATUS_ADD:
5182 label2 = ct->path;
5183 break;
5184 case GOT_STATUS_DELETE:
5185 label1 = ct->path;
5186 break;
5187 default:
5188 return got_error(GOT_ERR_FILE_STATUS);
5190 fd1 = got_opentempfd();
5191 if (fd1 == -1) {
5192 err = got_error_from_errno("got_opentempfd");
5193 goto done;
5195 fd2 = got_opentempfd();
5196 if (fd2 == -1) {
5197 err = got_error_from_errno("got_opentempfd");
5198 goto done;
5200 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5201 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5202 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5203 NULL, repo, diff_outfile);
5204 goto done;
5207 fd1 = got_opentempfd();
5208 if (fd1 == -1) {
5209 err = got_error_from_errno("got_opentempfd");
5210 goto done;
5213 if (ct->status != GOT_STATUS_ADD) {
5214 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5215 8192, fd1);
5216 if (err)
5217 goto done;
5220 if (ct->status != GOT_STATUS_DELETE) {
5221 if (dirfd != -1) {
5222 fd = openat(dirfd, de_name,
5223 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5224 if (fd == -1) {
5225 if (!got_err_open_nofollow_on_symlink()) {
5226 err = got_error_from_errno2("openat",
5227 ct->ondisk_path);
5228 goto done;
5230 err = get_symlink_target_file(&fd, dirfd,
5231 de_name, ct->ondisk_path);
5232 if (err)
5233 goto done;
5235 } else {
5236 fd = open(ct->ondisk_path,
5237 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5238 if (fd == -1) {
5239 if (!got_err_open_nofollow_on_symlink()) {
5240 err = got_error_from_errno2("open",
5241 ct->ondisk_path);
5242 goto done;
5244 err = get_symlink_target_file(&fd, dirfd,
5245 de_name, ct->ondisk_path);
5246 if (err)
5247 goto done;
5250 if (fstatat(fd, ct->ondisk_path, &sb,
5251 AT_SYMLINK_NOFOLLOW) == -1) {
5252 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5253 goto done;
5255 ondisk_file = fdopen(fd, "r");
5256 if (ondisk_file == NULL) {
5257 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5258 goto done;
5260 fd = -1;
5261 f2_exists = 1;
5264 if (blob1) {
5265 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5266 f1, blob1);
5267 if (err)
5268 goto done;
5271 err = got_diff_blob_file(blob1, f1, size1, label1,
5272 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5273 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5274 done:
5275 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5276 err = got_error_from_errno("close");
5277 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5278 err = got_error_from_errno("close");
5279 if (blob1)
5280 got_object_blob_close(blob1);
5281 if (fd != -1 && close(fd) == -1 && err == NULL)
5282 err = got_error_from_errno("close");
5283 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5284 err = got_error_from_errno("fclose");
5285 return err;
5288 static const struct got_error *
5289 collect_commitables(void *arg, unsigned char status,
5290 unsigned char staged_status, const char *relpath,
5291 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5292 struct got_object_id *commit_id, int dirfd, const char *de_name)
5294 struct collect_commitables_arg *a = arg;
5295 const struct got_error *err = NULL;
5296 struct got_commitable *ct = NULL;
5297 struct got_pathlist_entry *new = NULL;
5298 char *parent_path = NULL, *path = NULL;
5299 struct stat sb;
5301 if (a->have_staged_files) {
5302 if (staged_status != GOT_STATUS_MODIFY &&
5303 staged_status != GOT_STATUS_ADD &&
5304 staged_status != GOT_STATUS_DELETE)
5305 return NULL;
5306 } else {
5307 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5308 printf("C %s\n", relpath);
5309 return got_error(GOT_ERR_COMMIT_CONFLICT);
5312 if (status != GOT_STATUS_MODIFY &&
5313 status != GOT_STATUS_MODE_CHANGE &&
5314 status != GOT_STATUS_ADD &&
5315 status != GOT_STATUS_DELETE &&
5316 status != GOT_STATUS_CONFLICT)
5317 return NULL;
5320 if (asprintf(&path, "/%s", relpath) == -1) {
5321 err = got_error_from_errno("asprintf");
5322 goto done;
5324 if (strcmp(path, "/") == 0) {
5325 parent_path = strdup("");
5326 if (parent_path == NULL)
5327 return got_error_from_errno("strdup");
5328 } else {
5329 err = got_path_dirname(&parent_path, path);
5330 if (err)
5331 return err;
5334 ct = calloc(1, sizeof(*ct));
5335 if (ct == NULL) {
5336 err = got_error_from_errno("calloc");
5337 goto done;
5340 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5341 relpath) == -1) {
5342 err = got_error_from_errno("asprintf");
5343 goto done;
5346 if (staged_status == GOT_STATUS_ADD ||
5347 staged_status == GOT_STATUS_MODIFY) {
5348 struct got_fileindex_entry *ie;
5349 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5350 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5351 case GOT_FILEIDX_MODE_REGULAR_FILE:
5352 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5353 ct->mode = S_IFREG;
5354 break;
5355 case GOT_FILEIDX_MODE_SYMLINK:
5356 ct->mode = S_IFLNK;
5357 break;
5358 default:
5359 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5360 goto done;
5362 ct->mode |= got_fileindex_entry_perms_get(ie);
5363 } else if (status != GOT_STATUS_DELETE &&
5364 staged_status != GOT_STATUS_DELETE) {
5365 if (dirfd != -1) {
5366 if (fstatat(dirfd, de_name, &sb,
5367 AT_SYMLINK_NOFOLLOW) == -1) {
5368 err = got_error_from_errno2("fstatat",
5369 ct->ondisk_path);
5370 goto done;
5372 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5373 err = got_error_from_errno2("lstat", ct->ondisk_path);
5374 goto done;
5376 ct->mode = sb.st_mode;
5379 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5380 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5381 relpath) == -1) {
5382 err = got_error_from_errno("asprintf");
5383 goto done;
5386 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5387 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5388 int is_bad_symlink;
5389 char target_path[PATH_MAX];
5390 ssize_t target_len;
5391 target_len = readlink(ct->ondisk_path, target_path,
5392 sizeof(target_path));
5393 if (target_len == -1) {
5394 err = got_error_from_errno2("readlink",
5395 ct->ondisk_path);
5396 goto done;
5398 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5399 target_len, ct->ondisk_path, a->worktree->root_path);
5400 if (err)
5401 goto done;
5402 if (is_bad_symlink) {
5403 err = got_error_path(ct->ondisk_path,
5404 GOT_ERR_BAD_SYMLINK);
5405 goto done;
5410 ct->status = status;
5411 ct->staged_status = staged_status;
5412 ct->blob_id = NULL; /* will be filled in when blob gets created */
5413 if (ct->status != GOT_STATUS_ADD &&
5414 ct->staged_status != GOT_STATUS_ADD) {
5415 ct->base_blob_id = got_object_id_dup(blob_id);
5416 if (ct->base_blob_id == NULL) {
5417 err = got_error_from_errno("got_object_id_dup");
5418 goto done;
5420 ct->base_commit_id = got_object_id_dup(commit_id);
5421 if (ct->base_commit_id == NULL) {
5422 err = got_error_from_errno("got_object_id_dup");
5423 goto done;
5426 if (ct->staged_status == GOT_STATUS_ADD ||
5427 ct->staged_status == GOT_STATUS_MODIFY) {
5428 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5429 if (ct->staged_blob_id == NULL) {
5430 err = got_error_from_errno("got_object_id_dup");
5431 goto done;
5434 ct->path = strdup(path);
5435 if (ct->path == NULL) {
5436 err = got_error_from_errno("strdup");
5437 goto done;
5439 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5440 if (err)
5441 goto done;
5443 if (a->diff_outfile && ct && new != NULL) {
5444 err = append_ct_diff(ct, &a->diff_header_shown,
5445 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5446 a->have_staged_files, a->repo, a->worktree);
5447 if (err)
5448 goto done;
5450 done:
5451 if (ct && (err || new == NULL))
5452 free_commitable(ct);
5453 free(parent_path);
5454 free(path);
5455 return err;
5458 static const struct got_error *write_tree(struct got_object_id **, int *,
5459 struct got_tree_object *, const char *, struct got_pathlist_head *,
5460 got_worktree_status_cb status_cb, void *status_arg,
5461 struct got_repository *);
5463 static const struct got_error *
5464 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5465 struct got_tree_entry *te, const char *parent_path,
5466 struct got_pathlist_head *commitable_paths,
5467 got_worktree_status_cb status_cb, void *status_arg,
5468 struct got_repository *repo)
5470 const struct got_error *err = NULL;
5471 struct got_tree_object *subtree;
5472 char *subpath;
5474 if (asprintf(&subpath, "%s%s%s", parent_path,
5475 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5476 return got_error_from_errno("asprintf");
5478 err = got_object_open_as_tree(&subtree, repo, &te->id);
5479 if (err)
5480 return err;
5482 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5483 commitable_paths, status_cb, status_arg, repo);
5484 got_object_tree_close(subtree);
5485 free(subpath);
5486 return err;
5489 static const struct got_error *
5490 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5492 const struct got_error *err = NULL;
5493 char *ct_parent_path = NULL;
5495 *match = 0;
5497 if (strchr(ct->in_repo_path, '/') == NULL) {
5498 *match = got_path_is_root_dir(path);
5499 return NULL;
5502 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5503 if (err)
5504 return err;
5505 *match = (strcmp(path, ct_parent_path) == 0);
5506 free(ct_parent_path);
5507 return err;
5510 static mode_t
5511 get_ct_file_mode(struct got_commitable *ct)
5513 if (S_ISLNK(ct->mode))
5514 return S_IFLNK;
5516 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5519 static const struct got_error *
5520 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5521 struct got_tree_entry *te, struct got_commitable *ct)
5523 const struct got_error *err = NULL;
5525 *new_te = NULL;
5527 err = got_object_tree_entry_dup(new_te, te);
5528 if (err)
5529 goto done;
5531 (*new_te)->mode = get_ct_file_mode(ct);
5533 if (ct->staged_status == GOT_STATUS_MODIFY)
5534 memcpy(&(*new_te)->id, ct->staged_blob_id,
5535 sizeof((*new_te)->id));
5536 else
5537 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5538 done:
5539 if (err && *new_te) {
5540 free(*new_te);
5541 *new_te = NULL;
5543 return err;
5546 static const struct got_error *
5547 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5548 struct got_commitable *ct)
5550 const struct got_error *err = NULL;
5551 char *ct_name = NULL;
5553 *new_te = NULL;
5555 *new_te = calloc(1, sizeof(**new_te));
5556 if (*new_te == NULL)
5557 return got_error_from_errno("calloc");
5559 err = got_path_basename(&ct_name, ct->path);
5560 if (err)
5561 goto done;
5562 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5563 sizeof((*new_te)->name)) {
5564 err = got_error(GOT_ERR_NO_SPACE);
5565 goto done;
5568 (*new_te)->mode = get_ct_file_mode(ct);
5570 if (ct->staged_status == GOT_STATUS_ADD)
5571 memcpy(&(*new_te)->id, ct->staged_blob_id,
5572 sizeof((*new_te)->id));
5573 else
5574 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5575 done:
5576 free(ct_name);
5577 if (err && *new_te) {
5578 free(*new_te);
5579 *new_te = NULL;
5581 return err;
5584 static const struct got_error *
5585 insert_tree_entry(struct got_tree_entry *new_te,
5586 struct got_pathlist_head *paths)
5588 const struct got_error *err = NULL;
5589 struct got_pathlist_entry *new_pe;
5591 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5592 if (err)
5593 return err;
5594 if (new_pe == NULL)
5595 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5596 return NULL;
5599 static const struct got_error *
5600 report_ct_status(struct got_commitable *ct,
5601 got_worktree_status_cb status_cb, void *status_arg)
5603 const char *ct_path = ct->path;
5604 unsigned char status;
5606 if (status_cb == NULL) /* no commit progress output desired */
5607 return NULL;
5609 while (ct_path[0] == '/')
5610 ct_path++;
5612 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5613 status = ct->staged_status;
5614 else
5615 status = ct->status;
5617 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5618 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5621 static const struct got_error *
5622 match_modified_subtree(int *modified, struct got_tree_entry *te,
5623 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5625 const struct got_error *err = NULL;
5626 struct got_pathlist_entry *pe;
5627 char *te_path;
5629 *modified = 0;
5631 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5632 got_path_is_root_dir(base_tree_path) ? "" : "/",
5633 te->name) == -1)
5634 return got_error_from_errno("asprintf");
5636 TAILQ_FOREACH(pe, commitable_paths, entry) {
5637 struct got_commitable *ct = pe->data;
5638 *modified = got_path_is_child(ct->in_repo_path, te_path,
5639 strlen(te_path));
5640 if (*modified)
5641 break;
5644 free(te_path);
5645 return err;
5648 static const struct got_error *
5649 match_deleted_or_modified_ct(struct got_commitable **ctp,
5650 struct got_tree_entry *te, const char *base_tree_path,
5651 struct got_pathlist_head *commitable_paths)
5653 const struct got_error *err = NULL;
5654 struct got_pathlist_entry *pe;
5656 *ctp = NULL;
5658 TAILQ_FOREACH(pe, commitable_paths, entry) {
5659 struct got_commitable *ct = pe->data;
5660 char *ct_name = NULL;
5661 int path_matches;
5663 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5664 if (ct->status != GOT_STATUS_MODIFY &&
5665 ct->status != GOT_STATUS_MODE_CHANGE &&
5666 ct->status != GOT_STATUS_DELETE &&
5667 ct->status != GOT_STATUS_CONFLICT)
5668 continue;
5669 } else {
5670 if (ct->staged_status != GOT_STATUS_MODIFY &&
5671 ct->staged_status != GOT_STATUS_DELETE)
5672 continue;
5675 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5676 continue;
5678 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5679 if (err)
5680 return err;
5681 if (!path_matches)
5682 continue;
5684 err = got_path_basename(&ct_name, pe->path);
5685 if (err)
5686 return err;
5688 if (strcmp(te->name, ct_name) != 0) {
5689 free(ct_name);
5690 continue;
5692 free(ct_name);
5694 *ctp = ct;
5695 break;
5698 return err;
5701 static const struct got_error *
5702 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5703 const char *child_path, const char *path_base_tree,
5704 struct got_pathlist_head *commitable_paths,
5705 got_worktree_status_cb status_cb, void *status_arg,
5706 struct got_repository *repo)
5708 const struct got_error *err = NULL;
5709 struct got_tree_entry *new_te;
5710 char *subtree_path;
5711 struct got_object_id *id = NULL;
5712 int nentries;
5714 *new_tep = NULL;
5716 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5717 got_path_is_root_dir(path_base_tree) ? "" : "/",
5718 child_path) == -1)
5719 return got_error_from_errno("asprintf");
5721 new_te = calloc(1, sizeof(*new_te));
5722 if (new_te == NULL)
5723 return got_error_from_errno("calloc");
5724 new_te->mode = S_IFDIR;
5726 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5727 sizeof(new_te->name)) {
5728 err = got_error(GOT_ERR_NO_SPACE);
5729 goto done;
5731 err = write_tree(&id, &nentries, NULL, subtree_path,
5732 commitable_paths, status_cb, status_arg, repo);
5733 if (err) {
5734 free(new_te);
5735 goto done;
5737 memcpy(&new_te->id, id, sizeof(new_te->id));
5738 done:
5739 free(id);
5740 free(subtree_path);
5741 if (err == NULL)
5742 *new_tep = new_te;
5743 return err;
5746 static const struct got_error *
5747 write_tree(struct got_object_id **new_tree_id, int *nentries,
5748 struct got_tree_object *base_tree, const char *path_base_tree,
5749 struct got_pathlist_head *commitable_paths,
5750 got_worktree_status_cb status_cb, void *status_arg,
5751 struct got_repository *repo)
5753 const struct got_error *err = NULL;
5754 struct got_pathlist_head paths;
5755 struct got_tree_entry *te, *new_te = NULL;
5756 struct got_pathlist_entry *pe;
5758 TAILQ_INIT(&paths);
5759 *nentries = 0;
5761 /* Insert, and recurse into, newly added entries first. */
5762 TAILQ_FOREACH(pe, commitable_paths, entry) {
5763 struct got_commitable *ct = pe->data;
5764 char *child_path = NULL, *slash;
5766 if ((ct->status != GOT_STATUS_ADD &&
5767 ct->staged_status != GOT_STATUS_ADD) ||
5768 (ct->flags & GOT_COMMITABLE_ADDED))
5769 continue;
5771 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5772 strlen(path_base_tree)))
5773 continue;
5775 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5776 ct->in_repo_path);
5777 if (err)
5778 goto done;
5780 slash = strchr(child_path, '/');
5781 if (slash == NULL) {
5782 err = alloc_added_blob_tree_entry(&new_te, ct);
5783 if (err)
5784 goto done;
5785 err = report_ct_status(ct, status_cb, status_arg);
5786 if (err)
5787 goto done;
5788 ct->flags |= GOT_COMMITABLE_ADDED;
5789 err = insert_tree_entry(new_te, &paths);
5790 if (err)
5791 goto done;
5792 (*nentries)++;
5793 } else {
5794 *slash = '\0'; /* trim trailing path components */
5795 if (base_tree == NULL ||
5796 got_object_tree_find_entry(base_tree, child_path)
5797 == NULL) {
5798 err = make_subtree_for_added_blob(&new_te,
5799 child_path, path_base_tree,
5800 commitable_paths, status_cb, status_arg,
5801 repo);
5802 if (err)
5803 goto done;
5804 err = insert_tree_entry(new_te, &paths);
5805 if (err)
5806 goto done;
5807 (*nentries)++;
5812 if (base_tree) {
5813 int i, nbase_entries;
5814 /* Handle modified and deleted entries. */
5815 nbase_entries = got_object_tree_get_nentries(base_tree);
5816 for (i = 0; i < nbase_entries; i++) {
5817 struct got_commitable *ct = NULL;
5819 te = got_object_tree_get_entry(base_tree, i);
5820 if (got_object_tree_entry_is_submodule(te)) {
5821 /* Entry is a submodule; just copy it. */
5822 err = got_object_tree_entry_dup(&new_te, te);
5823 if (err)
5824 goto done;
5825 err = insert_tree_entry(new_te, &paths);
5826 if (err)
5827 goto done;
5828 (*nentries)++;
5829 continue;
5832 if (S_ISDIR(te->mode)) {
5833 int modified;
5834 err = got_object_tree_entry_dup(&new_te, te);
5835 if (err)
5836 goto done;
5837 err = match_modified_subtree(&modified, te,
5838 path_base_tree, commitable_paths);
5839 if (err)
5840 goto done;
5841 /* Avoid recursion into unmodified subtrees. */
5842 if (modified) {
5843 struct got_object_id *new_id;
5844 int nsubentries;
5845 err = write_subtree(&new_id,
5846 &nsubentries, te,
5847 path_base_tree, commitable_paths,
5848 status_cb, status_arg, repo);
5849 if (err)
5850 goto done;
5851 if (nsubentries == 0) {
5852 /* All entries were deleted. */
5853 free(new_id);
5854 continue;
5856 memcpy(&new_te->id, new_id,
5857 sizeof(new_te->id));
5858 free(new_id);
5860 err = insert_tree_entry(new_te, &paths);
5861 if (err)
5862 goto done;
5863 (*nentries)++;
5864 continue;
5867 err = match_deleted_or_modified_ct(&ct, te,
5868 path_base_tree, commitable_paths);
5869 if (err)
5870 goto done;
5871 if (ct) {
5872 /* NB: Deleted entries get dropped here. */
5873 if (ct->status == GOT_STATUS_MODIFY ||
5874 ct->status == GOT_STATUS_MODE_CHANGE ||
5875 ct->status == GOT_STATUS_CONFLICT ||
5876 ct->staged_status == GOT_STATUS_MODIFY) {
5877 err = alloc_modified_blob_tree_entry(
5878 &new_te, te, ct);
5879 if (err)
5880 goto done;
5881 err = insert_tree_entry(new_te, &paths);
5882 if (err)
5883 goto done;
5884 (*nentries)++;
5886 err = report_ct_status(ct, status_cb,
5887 status_arg);
5888 if (err)
5889 goto done;
5890 } else {
5891 /* Entry is unchanged; just copy it. */
5892 err = got_object_tree_entry_dup(&new_te, te);
5893 if (err)
5894 goto done;
5895 err = insert_tree_entry(new_te, &paths);
5896 if (err)
5897 goto done;
5898 (*nentries)++;
5903 /* Write new list of entries; deleted entries have been dropped. */
5904 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5905 done:
5906 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5907 return err;
5910 static const struct got_error *
5911 update_fileindex_after_commit(struct got_worktree *worktree,
5912 struct got_pathlist_head *commitable_paths,
5913 struct got_object_id *new_base_commit_id,
5914 struct got_fileindex *fileindex, int have_staged_files)
5916 const struct got_error *err = NULL;
5917 struct got_pathlist_entry *pe;
5918 char *relpath = NULL;
5920 TAILQ_FOREACH(pe, commitable_paths, entry) {
5921 struct got_fileindex_entry *ie;
5922 struct got_commitable *ct = pe->data;
5924 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5926 err = got_path_skip_common_ancestor(&relpath,
5927 worktree->root_path, ct->ondisk_path);
5928 if (err)
5929 goto done;
5931 if (ie) {
5932 if (ct->status == GOT_STATUS_DELETE ||
5933 ct->staged_status == GOT_STATUS_DELETE) {
5934 got_fileindex_entry_remove(fileindex, ie);
5935 } else if (ct->staged_status == GOT_STATUS_ADD ||
5936 ct->staged_status == GOT_STATUS_MODIFY) {
5937 got_fileindex_entry_stage_set(ie,
5938 GOT_FILEIDX_STAGE_NONE);
5939 got_fileindex_entry_staged_filetype_set(ie, 0);
5941 err = got_fileindex_entry_update(ie,
5942 worktree->root_fd, relpath,
5943 ct->staged_blob_id->sha1,
5944 new_base_commit_id->sha1,
5945 !have_staged_files);
5946 } else
5947 err = got_fileindex_entry_update(ie,
5948 worktree->root_fd, relpath,
5949 ct->blob_id->sha1,
5950 new_base_commit_id->sha1,
5951 !have_staged_files);
5952 } else {
5953 err = got_fileindex_entry_alloc(&ie, pe->path);
5954 if (err)
5955 goto done;
5956 err = got_fileindex_entry_update(ie,
5957 worktree->root_fd, relpath, ct->blob_id->sha1,
5958 new_base_commit_id->sha1, 1);
5959 if (err) {
5960 got_fileindex_entry_free(ie);
5961 goto done;
5963 err = got_fileindex_entry_add(fileindex, ie);
5964 if (err) {
5965 got_fileindex_entry_free(ie);
5966 goto done;
5969 free(relpath);
5970 relpath = NULL;
5972 done:
5973 free(relpath);
5974 return err;
5978 static const struct got_error *
5979 check_out_of_date(const char *in_repo_path, unsigned char status,
5980 unsigned char staged_status, struct got_object_id *base_blob_id,
5981 struct got_object_id *base_commit_id,
5982 struct got_object_id *head_commit_id, struct got_repository *repo,
5983 int ood_errcode)
5985 const struct got_error *err = NULL;
5986 struct got_commit_object *commit = NULL;
5987 struct got_object_id *id = NULL;
5989 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5990 /* Trivial case: base commit == head commit */
5991 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5992 return NULL;
5994 * Ensure file content which local changes were based
5995 * on matches file content in the branch head.
5997 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5998 if (err)
5999 goto done;
6000 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6001 if (err) {
6002 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6003 err = got_error(ood_errcode);
6004 goto done;
6005 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6006 err = got_error(ood_errcode);
6007 } else {
6008 /* Require that added files don't exist in the branch head. */
6009 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6010 if (err)
6011 goto done;
6012 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6013 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6014 goto done;
6015 err = id ? got_error(ood_errcode) : NULL;
6017 done:
6018 free(id);
6019 if (commit)
6020 got_object_commit_close(commit);
6021 return err;
6024 static const struct got_error *
6025 commit_worktree(struct got_object_id **new_commit_id,
6026 struct got_pathlist_head *commitable_paths,
6027 struct got_object_id *head_commit_id,
6028 struct got_object_id *parent_id2,
6029 struct got_worktree *worktree,
6030 const char *author, const char *committer, char *diff_path,
6031 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6032 got_worktree_status_cb status_cb, void *status_arg,
6033 struct got_repository *repo)
6035 const struct got_error *err = NULL, *unlockerr = NULL;
6036 struct got_pathlist_entry *pe;
6037 const char *head_ref_name = NULL;
6038 struct got_commit_object *head_commit = NULL;
6039 struct got_reference *head_ref2 = NULL;
6040 struct got_object_id *head_commit_id2 = NULL;
6041 struct got_tree_object *head_tree = NULL;
6042 struct got_object_id *new_tree_id = NULL;
6043 int nentries, nparents = 0;
6044 struct got_object_id_queue parent_ids;
6045 struct got_object_qid *pid = NULL;
6046 char *logmsg = NULL;
6047 time_t timestamp;
6049 *new_commit_id = NULL;
6051 STAILQ_INIT(&parent_ids);
6053 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6054 if (err)
6055 goto done;
6057 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6058 if (err)
6059 goto done;
6061 if (commit_msg_cb != NULL) {
6062 err = commit_msg_cb(commitable_paths, diff_path,
6063 &logmsg, commit_arg);
6064 if (err)
6065 goto done;
6068 if (logmsg == NULL || strlen(logmsg) == 0) {
6069 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6070 goto done;
6073 /* Create blobs from added and modified files and record their IDs. */
6074 TAILQ_FOREACH(pe, commitable_paths, entry) {
6075 struct got_commitable *ct = pe->data;
6076 char *ondisk_path;
6078 /* Blobs for staged files already exist. */
6079 if (ct->staged_status == GOT_STATUS_ADD ||
6080 ct->staged_status == GOT_STATUS_MODIFY)
6081 continue;
6083 if (ct->status != GOT_STATUS_ADD &&
6084 ct->status != GOT_STATUS_MODIFY &&
6085 ct->status != GOT_STATUS_MODE_CHANGE &&
6086 ct->status != GOT_STATUS_CONFLICT)
6087 continue;
6089 if (asprintf(&ondisk_path, "%s/%s",
6090 worktree->root_path, pe->path) == -1) {
6091 err = got_error_from_errno("asprintf");
6092 goto done;
6094 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6095 free(ondisk_path);
6096 if (err)
6097 goto done;
6100 /* Recursively write new tree objects. */
6101 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6102 commitable_paths, status_cb, status_arg, repo);
6103 if (err)
6104 goto done;
6106 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6107 if (err)
6108 goto done;
6109 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6110 nparents++;
6111 if (parent_id2) {
6112 err = got_object_qid_alloc(&pid, parent_id2);
6113 if (err)
6114 goto done;
6115 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6116 nparents++;
6118 timestamp = time(NULL);
6119 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6120 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6121 if (logmsg != NULL)
6122 free(logmsg);
6123 if (err)
6124 goto done;
6126 /* Check if a concurrent commit to our branch has occurred. */
6127 head_ref_name = got_worktree_get_head_ref_name(worktree);
6128 if (head_ref_name == NULL) {
6129 err = got_error_from_errno("got_worktree_get_head_ref_name");
6130 goto done;
6132 /* Lock the reference here to prevent concurrent modification. */
6133 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6134 if (err)
6135 goto done;
6136 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6137 if (err)
6138 goto done;
6139 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6140 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6141 goto done;
6143 /* Update branch head in repository. */
6144 err = got_ref_change_ref(head_ref2, *new_commit_id);
6145 if (err)
6146 goto done;
6147 err = got_ref_write(head_ref2, repo);
6148 if (err)
6149 goto done;
6151 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6152 if (err)
6153 goto done;
6155 err = ref_base_commit(worktree, repo);
6156 if (err)
6157 goto done;
6158 done:
6159 got_object_id_queue_free(&parent_ids);
6160 if (head_tree)
6161 got_object_tree_close(head_tree);
6162 if (head_commit)
6163 got_object_commit_close(head_commit);
6164 free(head_commit_id2);
6165 if (head_ref2) {
6166 unlockerr = got_ref_unlock(head_ref2);
6167 if (unlockerr && err == NULL)
6168 err = unlockerr;
6169 got_ref_close(head_ref2);
6171 return err;
6174 static const struct got_error *
6175 check_path_is_commitable(const char *path,
6176 struct got_pathlist_head *commitable_paths)
6178 struct got_pathlist_entry *cpe = NULL;
6179 size_t path_len = strlen(path);
6181 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6182 struct got_commitable *ct = cpe->data;
6183 const char *ct_path = ct->path;
6185 while (ct_path[0] == '/')
6186 ct_path++;
6188 if (strcmp(path, ct_path) == 0 ||
6189 got_path_is_child(ct_path, path, path_len))
6190 break;
6193 if (cpe == NULL)
6194 return got_error_path(path, GOT_ERR_BAD_PATH);
6196 return NULL;
6199 static const struct got_error *
6200 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6202 int *have_staged_files = arg;
6204 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6205 *have_staged_files = 1;
6206 return got_error(GOT_ERR_CANCELLED);
6209 return NULL;
6212 static const struct got_error *
6213 check_non_staged_files(struct got_fileindex *fileindex,
6214 struct got_pathlist_head *paths)
6216 struct got_pathlist_entry *pe;
6217 struct got_fileindex_entry *ie;
6219 TAILQ_FOREACH(pe, paths, entry) {
6220 if (pe->path[0] == '\0')
6221 continue;
6222 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6223 if (ie == NULL)
6224 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6225 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6226 return got_error_path(pe->path,
6227 GOT_ERR_FILE_NOT_STAGED);
6230 return NULL;
6233 const struct got_error *
6234 got_worktree_commit(struct got_object_id **new_commit_id,
6235 struct got_worktree *worktree, struct got_pathlist_head *paths,
6236 const char *author, const char *committer, int allow_bad_symlinks,
6237 int show_diff, int commit_conflicts,
6238 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6239 got_worktree_status_cb status_cb, void *status_arg,
6240 struct got_repository *repo)
6242 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6243 struct got_fileindex *fileindex = NULL;
6244 char *fileindex_path = NULL;
6245 struct got_pathlist_head commitable_paths;
6246 struct collect_commitables_arg cc_arg;
6247 struct got_pathlist_entry *pe;
6248 struct got_reference *head_ref = NULL;
6249 struct got_object_id *head_commit_id = NULL;
6250 char *diff_path = NULL;
6251 int have_staged_files = 0;
6253 *new_commit_id = NULL;
6255 memset(&cc_arg, 0, sizeof(cc_arg));
6256 TAILQ_INIT(&commitable_paths);
6258 err = lock_worktree(worktree, LOCK_EX);
6259 if (err)
6260 goto done;
6262 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6263 if (err)
6264 goto done;
6266 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6267 if (err)
6268 goto done;
6270 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6271 if (err)
6272 goto done;
6274 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6275 &have_staged_files);
6276 if (err && err->code != GOT_ERR_CANCELLED)
6277 goto done;
6278 if (have_staged_files) {
6279 err = check_non_staged_files(fileindex, paths);
6280 if (err)
6281 goto done;
6284 cc_arg.commitable_paths = &commitable_paths;
6285 cc_arg.worktree = worktree;
6286 cc_arg.fileindex = fileindex;
6287 cc_arg.repo = repo;
6288 cc_arg.have_staged_files = have_staged_files;
6289 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6290 cc_arg.diff_header_shown = 0;
6291 cc_arg.commit_conflicts = commit_conflicts;
6292 if (show_diff) {
6293 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6294 GOT_TMPDIR_STR "/got", ".diff");
6295 if (err)
6296 goto done;
6297 cc_arg.f1 = got_opentemp();
6298 if (cc_arg.f1 == NULL) {
6299 err = got_error_from_errno("got_opentemp");
6300 goto done;
6302 cc_arg.f2 = got_opentemp();
6303 if (cc_arg.f2 == NULL) {
6304 err = got_error_from_errno("got_opentemp");
6305 goto done;
6309 TAILQ_FOREACH(pe, paths, entry) {
6310 err = worktree_status(worktree, pe->path, fileindex, repo,
6311 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6312 if (err)
6313 goto done;
6316 if (show_diff) {
6317 if (fflush(cc_arg.diff_outfile) == EOF) {
6318 err = got_error_from_errno("fflush");
6319 goto done;
6323 if (TAILQ_EMPTY(&commitable_paths)) {
6324 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6325 goto done;
6328 TAILQ_FOREACH(pe, paths, entry) {
6329 err = check_path_is_commitable(pe->path, &commitable_paths);
6330 if (err)
6331 goto done;
6334 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6335 struct got_commitable *ct = pe->data;
6336 const char *ct_path = ct->in_repo_path;
6338 while (ct_path[0] == '/')
6339 ct_path++;
6340 err = check_out_of_date(ct_path, ct->status,
6341 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6342 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6343 if (err)
6344 goto done;
6348 err = commit_worktree(new_commit_id, &commitable_paths,
6349 head_commit_id, NULL, worktree, author, committer,
6350 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6351 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6352 if (err)
6353 goto done;
6355 err = update_fileindex_after_commit(worktree, &commitable_paths,
6356 *new_commit_id, fileindex, have_staged_files);
6357 sync_err = sync_fileindex(fileindex, fileindex_path);
6358 if (sync_err && err == NULL)
6359 err = sync_err;
6360 done:
6361 if (fileindex)
6362 got_fileindex_free(fileindex);
6363 free(fileindex_path);
6364 unlockerr = lock_worktree(worktree, LOCK_SH);
6365 if (unlockerr && err == NULL)
6366 err = unlockerr;
6367 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6368 struct got_commitable *ct = pe->data;
6370 free_commitable(ct);
6372 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6373 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6374 err = got_error_from_errno2("unlink", diff_path);
6375 free(diff_path);
6376 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6377 err == NULL)
6378 err = got_error_from_errno("fclose");
6379 return err;
6382 const char *
6383 got_commitable_get_path(struct got_commitable *ct)
6385 return ct->path;
6388 unsigned int
6389 got_commitable_get_status(struct got_commitable *ct)
6391 return ct->status;
6394 struct check_rebase_ok_arg {
6395 struct got_worktree *worktree;
6396 struct got_repository *repo;
6399 static const struct got_error *
6400 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6402 const struct got_error *err = NULL;
6403 struct check_rebase_ok_arg *a = arg;
6404 unsigned char status;
6405 struct stat sb;
6406 char *ondisk_path;
6408 /* Reject rebase of a work tree with mixed base commits. */
6409 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6410 SHA1_DIGEST_LENGTH))
6411 return got_error(GOT_ERR_MIXED_COMMITS);
6413 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6414 == -1)
6415 return got_error_from_errno("asprintf");
6417 /* Reject rebase of a work tree with modified or staged files. */
6418 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6419 free(ondisk_path);
6420 if (err)
6421 return err;
6423 if (status != GOT_STATUS_NO_CHANGE)
6424 return got_error(GOT_ERR_MODIFIED);
6425 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6426 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6428 return NULL;
6431 const struct got_error *
6432 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6433 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6434 struct got_worktree *worktree, struct got_reference *branch,
6435 struct got_repository *repo)
6437 const struct got_error *err = NULL;
6438 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6439 char *branch_ref_name = NULL;
6440 char *fileindex_path = NULL;
6441 struct check_rebase_ok_arg ok_arg;
6442 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6443 struct got_object_id *wt_branch_tip = NULL;
6445 *new_base_branch_ref = NULL;
6446 *tmp_branch = NULL;
6447 *fileindex = NULL;
6449 err = lock_worktree(worktree, LOCK_EX);
6450 if (err)
6451 return err;
6453 err = open_fileindex(fileindex, &fileindex_path, worktree);
6454 if (err)
6455 goto done;
6457 ok_arg.worktree = worktree;
6458 ok_arg.repo = repo;
6459 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6460 &ok_arg);
6461 if (err)
6462 goto done;
6464 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6465 if (err)
6466 goto done;
6468 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6469 if (err)
6470 goto done;
6472 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6473 if (err)
6474 goto done;
6476 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6477 0);
6478 if (err)
6479 goto done;
6481 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6482 if (err)
6483 goto done;
6484 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6485 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6486 goto done;
6489 err = got_ref_alloc_symref(new_base_branch_ref,
6490 new_base_branch_ref_name, wt_branch);
6491 if (err)
6492 goto done;
6493 err = got_ref_write(*new_base_branch_ref, repo);
6494 if (err)
6495 goto done;
6497 /* TODO Lock original branch's ref while rebasing? */
6499 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6500 if (err)
6501 goto done;
6503 err = got_ref_write(branch_ref, repo);
6504 if (err)
6505 goto done;
6507 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6508 worktree->base_commit_id);
6509 if (err)
6510 goto done;
6511 err = got_ref_write(*tmp_branch, repo);
6512 if (err)
6513 goto done;
6515 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6516 if (err)
6517 goto done;
6518 done:
6519 free(fileindex_path);
6520 free(tmp_branch_name);
6521 free(new_base_branch_ref_name);
6522 free(branch_ref_name);
6523 if (branch_ref)
6524 got_ref_close(branch_ref);
6525 if (wt_branch)
6526 got_ref_close(wt_branch);
6527 free(wt_branch_tip);
6528 if (err) {
6529 if (*new_base_branch_ref) {
6530 got_ref_close(*new_base_branch_ref);
6531 *new_base_branch_ref = NULL;
6533 if (*tmp_branch) {
6534 got_ref_close(*tmp_branch);
6535 *tmp_branch = NULL;
6537 if (*fileindex) {
6538 got_fileindex_free(*fileindex);
6539 *fileindex = NULL;
6541 lock_worktree(worktree, LOCK_SH);
6543 return err;
6546 const struct got_error *
6547 got_worktree_rebase_continue(struct got_object_id **commit_id,
6548 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6549 struct got_reference **branch, struct got_fileindex **fileindex,
6550 struct got_worktree *worktree, struct got_repository *repo)
6552 const struct got_error *err;
6553 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6554 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6555 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6556 char *fileindex_path = NULL;
6557 int have_staged_files = 0;
6559 *commit_id = NULL;
6560 *new_base_branch = NULL;
6561 *tmp_branch = NULL;
6562 *branch = NULL;
6563 *fileindex = NULL;
6565 err = lock_worktree(worktree, LOCK_EX);
6566 if (err)
6567 return err;
6569 err = open_fileindex(fileindex, &fileindex_path, worktree);
6570 if (err)
6571 goto done;
6573 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6574 &have_staged_files);
6575 if (err && err->code != GOT_ERR_CANCELLED)
6576 goto done;
6577 if (have_staged_files) {
6578 err = got_error(GOT_ERR_STAGED_PATHS);
6579 goto done;
6582 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6583 if (err)
6584 goto done;
6586 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6587 if (err)
6588 goto done;
6590 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6591 if (err)
6592 goto done;
6594 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6595 if (err)
6596 goto done;
6598 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6599 if (err)
6600 goto done;
6602 err = got_ref_open(branch, repo,
6603 got_ref_get_symref_target(branch_ref), 0);
6604 if (err)
6605 goto done;
6607 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6608 if (err)
6609 goto done;
6611 err = got_ref_resolve(commit_id, repo, commit_ref);
6612 if (err)
6613 goto done;
6615 err = got_ref_open(new_base_branch, repo,
6616 new_base_branch_ref_name, 0);
6617 if (err)
6618 goto done;
6620 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6621 if (err)
6622 goto done;
6623 done:
6624 free(commit_ref_name);
6625 free(branch_ref_name);
6626 free(fileindex_path);
6627 if (commit_ref)
6628 got_ref_close(commit_ref);
6629 if (branch_ref)
6630 got_ref_close(branch_ref);
6631 if (err) {
6632 free(*commit_id);
6633 *commit_id = NULL;
6634 if (*tmp_branch) {
6635 got_ref_close(*tmp_branch);
6636 *tmp_branch = NULL;
6638 if (*new_base_branch) {
6639 got_ref_close(*new_base_branch);
6640 *new_base_branch = NULL;
6642 if (*branch) {
6643 got_ref_close(*branch);
6644 *branch = NULL;
6646 if (*fileindex) {
6647 got_fileindex_free(*fileindex);
6648 *fileindex = NULL;
6650 lock_worktree(worktree, LOCK_SH);
6652 return err;
6655 const struct got_error *
6656 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6658 const struct got_error *err;
6659 char *tmp_branch_name = NULL;
6661 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6662 if (err)
6663 return err;
6665 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6666 free(tmp_branch_name);
6667 return NULL;
6670 static const struct got_error *
6671 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6672 const char *diff_path, char **logmsg, void *arg)
6674 *logmsg = arg;
6675 return NULL;
6678 static const struct got_error *
6679 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6680 const char *path, struct got_object_id *blob_id,
6681 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6682 int dirfd, const char *de_name)
6684 return NULL;
6687 struct collect_merged_paths_arg {
6688 got_worktree_checkout_cb progress_cb;
6689 void *progress_arg;
6690 struct got_pathlist_head *merged_paths;
6693 static const struct got_error *
6694 collect_merged_paths(void *arg, unsigned char status, const char *path)
6696 const struct got_error *err;
6697 struct collect_merged_paths_arg *a = arg;
6698 char *p;
6699 struct got_pathlist_entry *new;
6701 err = (*a->progress_cb)(a->progress_arg, status, path);
6702 if (err)
6703 return err;
6705 if (status != GOT_STATUS_MERGE &&
6706 status != GOT_STATUS_ADD &&
6707 status != GOT_STATUS_DELETE &&
6708 status != GOT_STATUS_CONFLICT)
6709 return NULL;
6711 p = strdup(path);
6712 if (p == NULL)
6713 return got_error_from_errno("strdup");
6715 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6716 if (err || new == NULL)
6717 free(p);
6718 return err;
6721 static const struct got_error *
6722 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6723 int is_rebase, struct got_repository *repo)
6725 const struct got_error *err;
6726 struct got_reference *commit_ref = NULL;
6728 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6729 if (err) {
6730 if (err->code != GOT_ERR_NOT_REF)
6731 goto done;
6732 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6733 if (err)
6734 goto done;
6735 err = got_ref_write(commit_ref, repo);
6736 if (err)
6737 goto done;
6738 } else if (is_rebase) {
6739 struct got_object_id *stored_id;
6740 int cmp;
6742 err = got_ref_resolve(&stored_id, repo, commit_ref);
6743 if (err)
6744 goto done;
6745 cmp = got_object_id_cmp(commit_id, stored_id);
6746 free(stored_id);
6747 if (cmp != 0) {
6748 err = got_error(GOT_ERR_REBASE_COMMITID);
6749 goto done;
6752 done:
6753 if (commit_ref)
6754 got_ref_close(commit_ref);
6755 return err;
6758 static const struct got_error *
6759 rebase_merge_files(struct got_pathlist_head *merged_paths,
6760 const char *commit_ref_name, struct got_worktree *worktree,
6761 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6762 struct got_object_id *commit_id, struct got_repository *repo,
6763 got_worktree_checkout_cb progress_cb, void *progress_arg,
6764 got_cancel_cb cancel_cb, void *cancel_arg)
6766 const struct got_error *err;
6767 struct got_reference *commit_ref = NULL;
6768 struct collect_merged_paths_arg cmp_arg;
6769 char *fileindex_path;
6771 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6773 err = get_fileindex_path(&fileindex_path, worktree);
6774 if (err)
6775 return err;
6777 cmp_arg.progress_cb = progress_cb;
6778 cmp_arg.progress_arg = progress_arg;
6779 cmp_arg.merged_paths = merged_paths;
6780 err = merge_files(worktree, fileindex, fileindex_path,
6781 parent_commit_id, commit_id, repo, collect_merged_paths,
6782 &cmp_arg, cancel_cb, cancel_arg);
6783 if (commit_ref)
6784 got_ref_close(commit_ref);
6785 return err;
6788 const struct got_error *
6789 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6790 struct got_worktree *worktree, struct got_fileindex *fileindex,
6791 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6792 struct got_repository *repo,
6793 got_worktree_checkout_cb progress_cb, void *progress_arg,
6794 got_cancel_cb cancel_cb, void *cancel_arg)
6796 const struct got_error *err;
6797 char *commit_ref_name;
6799 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6800 if (err)
6801 return err;
6803 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6804 if (err)
6805 goto done;
6807 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6808 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6809 progress_arg, cancel_cb, cancel_arg);
6810 done:
6811 free(commit_ref_name);
6812 return err;
6815 const struct got_error *
6816 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6817 struct got_worktree *worktree, struct got_fileindex *fileindex,
6818 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6819 struct got_repository *repo,
6820 got_worktree_checkout_cb progress_cb, void *progress_arg,
6821 got_cancel_cb cancel_cb, void *cancel_arg)
6823 const struct got_error *err;
6824 char *commit_ref_name;
6826 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6827 if (err)
6828 return err;
6830 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6831 if (err)
6832 goto done;
6834 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6835 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6836 progress_arg, cancel_cb, cancel_arg);
6837 done:
6838 free(commit_ref_name);
6839 return err;
6842 static const struct got_error *
6843 rebase_commit(struct got_object_id **new_commit_id,
6844 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6845 struct got_worktree *worktree, struct got_fileindex *fileindex,
6846 struct got_reference *tmp_branch, const char *committer,
6847 struct got_commit_object *orig_commit, const char *new_logmsg,
6848 int allow_conflict, struct got_repository *repo)
6850 const struct got_error *err, *sync_err;
6851 struct got_pathlist_head commitable_paths;
6852 struct collect_commitables_arg cc_arg;
6853 char *fileindex_path = NULL;
6854 struct got_reference *head_ref = NULL;
6855 struct got_object_id *head_commit_id = NULL;
6856 char *logmsg = NULL;
6858 memset(&cc_arg, 0, sizeof(cc_arg));
6859 TAILQ_INIT(&commitable_paths);
6860 *new_commit_id = NULL;
6862 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6864 err = get_fileindex_path(&fileindex_path, worktree);
6865 if (err)
6866 return err;
6868 cc_arg.commitable_paths = &commitable_paths;
6869 cc_arg.worktree = worktree;
6870 cc_arg.repo = repo;
6871 cc_arg.have_staged_files = 0;
6872 cc_arg.commit_conflicts = allow_conflict;
6874 * If possible get the status of individual files directly to
6875 * avoid crawling the entire work tree once per rebased commit.
6877 * Ideally, merged_paths would contain a list of commitables
6878 * we could use so we could skip worktree_status() entirely.
6879 * However, we would then need carefully keep track of cumulative
6880 * effects of operations such as file additions and deletions
6881 * in 'got histedit -f' (folding multiple commits into one),
6882 * and this extra complexity is not really worth it.
6884 if (merged_paths) {
6885 struct got_pathlist_entry *pe;
6886 TAILQ_FOREACH(pe, merged_paths, entry) {
6887 err = worktree_status(worktree, pe->path, fileindex,
6888 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6889 0);
6890 if (err)
6891 goto done;
6893 } else {
6894 err = worktree_status(worktree, "", fileindex, repo,
6895 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6896 if (err)
6897 goto done;
6900 if (TAILQ_EMPTY(&commitable_paths)) {
6901 /* No-op change; commit will be elided. */
6902 err = got_ref_delete(commit_ref, repo);
6903 if (err)
6904 goto done;
6905 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6906 goto done;
6909 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6910 if (err)
6911 goto done;
6913 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6914 if (err)
6915 goto done;
6917 if (new_logmsg) {
6918 logmsg = strdup(new_logmsg);
6919 if (logmsg == NULL) {
6920 err = got_error_from_errno("strdup");
6921 goto done;
6923 } else {
6924 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6925 if (err)
6926 goto done;
6929 /* NB: commit_worktree will call free(logmsg) */
6930 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6931 NULL, worktree, got_object_commit_get_author(orig_commit),
6932 committer ? committer :
6933 got_object_commit_get_committer(orig_commit), NULL,
6934 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6935 if (err)
6936 goto done;
6938 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6939 if (err)
6940 goto done;
6942 err = got_ref_delete(commit_ref, repo);
6943 if (err)
6944 goto done;
6946 err = update_fileindex_after_commit(worktree, &commitable_paths,
6947 *new_commit_id, fileindex, 0);
6948 sync_err = sync_fileindex(fileindex, fileindex_path);
6949 if (sync_err && err == NULL)
6950 err = sync_err;
6951 done:
6952 free(fileindex_path);
6953 free(head_commit_id);
6954 if (head_ref)
6955 got_ref_close(head_ref);
6956 if (err) {
6957 free(*new_commit_id);
6958 *new_commit_id = NULL;
6960 return err;
6963 const struct got_error *
6964 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6965 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6966 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6967 const char *committer, struct got_commit_object *orig_commit,
6968 struct got_object_id *orig_commit_id, int allow_conflict,
6969 struct got_repository *repo)
6971 const struct got_error *err;
6972 char *commit_ref_name;
6973 struct got_reference *commit_ref = NULL;
6974 struct got_object_id *commit_id = NULL;
6976 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6977 if (err)
6978 return err;
6980 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6981 if (err)
6982 goto done;
6983 err = got_ref_resolve(&commit_id, repo, commit_ref);
6984 if (err)
6985 goto done;
6986 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6987 err = got_error(GOT_ERR_REBASE_COMMITID);
6988 goto done;
6991 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6992 worktree, fileindex, tmp_branch, committer, orig_commit,
6993 NULL, allow_conflict, repo);
6994 done:
6995 if (commit_ref)
6996 got_ref_close(commit_ref);
6997 free(commit_ref_name);
6998 free(commit_id);
6999 return err;
7002 const struct got_error *
7003 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7004 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7005 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7006 const char *committer, struct got_commit_object *orig_commit,
7007 struct got_object_id *orig_commit_id, const char *new_logmsg,
7008 int allow_conflict, struct got_repository *repo)
7010 const struct got_error *err;
7011 char *commit_ref_name;
7012 struct got_reference *commit_ref = NULL;
7014 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7015 if (err)
7016 return err;
7018 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7019 if (err)
7020 goto done;
7022 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7023 worktree, fileindex, tmp_branch, committer, orig_commit,
7024 new_logmsg, allow_conflict, repo);
7025 done:
7026 if (commit_ref)
7027 got_ref_close(commit_ref);
7028 free(commit_ref_name);
7029 return err;
7032 const struct got_error *
7033 got_worktree_rebase_postpone(struct got_worktree *worktree,
7034 struct got_fileindex *fileindex)
7036 if (fileindex)
7037 got_fileindex_free(fileindex);
7038 return lock_worktree(worktree, LOCK_SH);
7041 static const struct got_error *
7042 delete_ref(const char *name, struct got_repository *repo)
7044 const struct got_error *err;
7045 struct got_reference *ref;
7047 err = got_ref_open(&ref, repo, name, 0);
7048 if (err) {
7049 if (err->code == GOT_ERR_NOT_REF)
7050 return NULL;
7051 return err;
7054 err = got_ref_delete(ref, repo);
7055 got_ref_close(ref);
7056 return err;
7059 static const struct got_error *
7060 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7062 const struct got_error *err;
7063 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7064 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7066 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7067 if (err)
7068 goto done;
7069 err = delete_ref(tmp_branch_name, repo);
7070 if (err)
7071 goto done;
7073 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7074 if (err)
7075 goto done;
7076 err = delete_ref(new_base_branch_ref_name, repo);
7077 if (err)
7078 goto done;
7080 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7081 if (err)
7082 goto done;
7083 err = delete_ref(branch_ref_name, repo);
7084 if (err)
7085 goto done;
7087 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7088 if (err)
7089 goto done;
7090 err = delete_ref(commit_ref_name, repo);
7091 if (err)
7092 goto done;
7094 done:
7095 free(tmp_branch_name);
7096 free(new_base_branch_ref_name);
7097 free(branch_ref_name);
7098 free(commit_ref_name);
7099 return err;
7102 static const struct got_error *
7103 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7104 struct got_object_id *new_commit_id, struct got_repository *repo)
7106 const struct got_error *err;
7107 struct got_reference *ref = NULL;
7108 struct got_object_id *old_commit_id = NULL;
7109 const char *branch_name = NULL;
7110 char *new_id_str = NULL;
7111 char *refname = NULL;
7113 branch_name = got_ref_get_name(branch);
7114 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7115 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7116 branch_name += 11;
7118 err = got_object_id_str(&new_id_str, new_commit_id);
7119 if (err)
7120 return err;
7122 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7123 new_id_str) == -1) {
7124 err = got_error_from_errno("asprintf");
7125 goto done;
7128 err = got_ref_resolve(&old_commit_id, repo, branch);
7129 if (err)
7130 goto done;
7132 err = got_ref_alloc(&ref, refname, old_commit_id);
7133 if (err)
7134 goto done;
7136 err = got_ref_write(ref, repo);
7137 done:
7138 free(new_id_str);
7139 free(refname);
7140 free(old_commit_id);
7141 if (ref)
7142 got_ref_close(ref);
7143 return err;
7146 const struct got_error *
7147 got_worktree_rebase_complete(struct got_worktree *worktree,
7148 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7149 struct got_reference *rebased_branch, struct got_repository *repo,
7150 int create_backup)
7152 const struct got_error *err, *unlockerr, *sync_err;
7153 struct got_object_id *new_head_commit_id = NULL;
7154 char *fileindex_path = NULL;
7156 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7157 if (err)
7158 return err;
7160 if (create_backup) {
7161 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7162 rebased_branch, new_head_commit_id, repo);
7163 if (err)
7164 goto done;
7167 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7168 if (err)
7169 goto done;
7171 err = got_ref_write(rebased_branch, repo);
7172 if (err)
7173 goto done;
7175 err = got_worktree_set_head_ref(worktree, rebased_branch);
7176 if (err)
7177 goto done;
7179 err = delete_rebase_refs(worktree, repo);
7180 if (err)
7181 goto done;
7183 err = get_fileindex_path(&fileindex_path, worktree);
7184 if (err)
7185 goto done;
7186 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7187 sync_err = sync_fileindex(fileindex, fileindex_path);
7188 if (sync_err && err == NULL)
7189 err = sync_err;
7190 done:
7191 got_fileindex_free(fileindex);
7192 free(fileindex_path);
7193 free(new_head_commit_id);
7194 unlockerr = lock_worktree(worktree, LOCK_SH);
7195 if (unlockerr && err == NULL)
7196 err = unlockerr;
7197 return err;
7200 const struct got_error *
7201 got_worktree_rebase_abort(struct got_worktree *worktree,
7202 struct got_fileindex *fileindex, struct got_repository *repo,
7203 struct got_reference *new_base_branch,
7204 got_worktree_checkout_cb progress_cb, void *progress_arg)
7206 const struct got_error *err, *unlockerr, *sync_err;
7207 struct got_reference *resolved = NULL;
7208 struct got_object_id *commit_id = NULL;
7209 struct got_commit_object *commit = NULL;
7210 char *fileindex_path = NULL;
7211 struct revert_file_args rfa;
7212 struct got_object_id *tree_id = NULL;
7214 err = lock_worktree(worktree, LOCK_EX);
7215 if (err)
7216 return err;
7218 err = got_object_open_as_commit(&commit, repo,
7219 worktree->base_commit_id);
7220 if (err)
7221 goto done;
7223 err = got_ref_open(&resolved, repo,
7224 got_ref_get_symref_target(new_base_branch), 0);
7225 if (err)
7226 goto done;
7228 err = got_worktree_set_head_ref(worktree, resolved);
7229 if (err)
7230 goto done;
7233 * XXX commits to the base branch could have happened while
7234 * we were busy rebasing; should we store the original commit ID
7235 * when rebase begins and read it back here?
7237 err = got_ref_resolve(&commit_id, repo, resolved);
7238 if (err)
7239 goto done;
7241 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7242 if (err)
7243 goto done;
7245 err = got_object_id_by_path(&tree_id, repo, commit,
7246 worktree->path_prefix);
7247 if (err)
7248 goto done;
7250 err = delete_rebase_refs(worktree, repo);
7251 if (err)
7252 goto done;
7254 err = get_fileindex_path(&fileindex_path, worktree);
7255 if (err)
7256 goto done;
7258 rfa.worktree = worktree;
7259 rfa.fileindex = fileindex;
7260 rfa.progress_cb = progress_cb;
7261 rfa.progress_arg = progress_arg;
7262 rfa.patch_cb = NULL;
7263 rfa.patch_arg = NULL;
7264 rfa.repo = repo;
7265 rfa.unlink_added_files = 0;
7266 err = worktree_status(worktree, "", fileindex, repo,
7267 revert_file, &rfa, NULL, NULL, 1, 0);
7268 if (err)
7269 goto sync;
7271 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7272 repo, progress_cb, progress_arg, NULL, NULL);
7273 sync:
7274 sync_err = sync_fileindex(fileindex, fileindex_path);
7275 if (sync_err && err == NULL)
7276 err = sync_err;
7277 done:
7278 got_ref_close(resolved);
7279 free(tree_id);
7280 free(commit_id);
7281 if (commit)
7282 got_object_commit_close(commit);
7283 if (fileindex)
7284 got_fileindex_free(fileindex);
7285 free(fileindex_path);
7287 unlockerr = lock_worktree(worktree, LOCK_SH);
7288 if (unlockerr && err == NULL)
7289 err = unlockerr;
7290 return err;
7293 const struct got_error *
7294 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7295 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7296 struct got_fileindex **fileindex, struct got_worktree *worktree,
7297 struct got_repository *repo)
7299 const struct got_error *err = NULL;
7300 char *tmp_branch_name = NULL;
7301 char *branch_ref_name = NULL;
7302 char *base_commit_ref_name = NULL;
7303 char *fileindex_path = NULL;
7304 struct check_rebase_ok_arg ok_arg;
7305 struct got_reference *wt_branch = NULL;
7306 struct got_reference *base_commit_ref = NULL;
7308 *tmp_branch = NULL;
7309 *branch_ref = NULL;
7310 *base_commit_id = NULL;
7311 *fileindex = NULL;
7313 err = lock_worktree(worktree, LOCK_EX);
7314 if (err)
7315 return err;
7317 err = open_fileindex(fileindex, &fileindex_path, worktree);
7318 if (err)
7319 goto done;
7321 ok_arg.worktree = worktree;
7322 ok_arg.repo = repo;
7323 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7324 &ok_arg);
7325 if (err)
7326 goto done;
7328 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7329 if (err)
7330 goto done;
7332 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7333 if (err)
7334 goto done;
7336 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7337 worktree);
7338 if (err)
7339 goto done;
7341 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7342 0);
7343 if (err)
7344 goto done;
7346 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7347 if (err)
7348 goto done;
7350 err = got_ref_write(*branch_ref, repo);
7351 if (err)
7352 goto done;
7354 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7355 worktree->base_commit_id);
7356 if (err)
7357 goto done;
7358 err = got_ref_write(base_commit_ref, repo);
7359 if (err)
7360 goto done;
7361 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7362 if (*base_commit_id == NULL) {
7363 err = got_error_from_errno("got_object_id_dup");
7364 goto done;
7367 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7368 worktree->base_commit_id);
7369 if (err)
7370 goto done;
7371 err = got_ref_write(*tmp_branch, repo);
7372 if (err)
7373 goto done;
7375 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7376 if (err)
7377 goto done;
7378 done:
7379 free(fileindex_path);
7380 free(tmp_branch_name);
7381 free(branch_ref_name);
7382 free(base_commit_ref_name);
7383 if (wt_branch)
7384 got_ref_close(wt_branch);
7385 if (err) {
7386 if (*branch_ref) {
7387 got_ref_close(*branch_ref);
7388 *branch_ref = NULL;
7390 if (*tmp_branch) {
7391 got_ref_close(*tmp_branch);
7392 *tmp_branch = NULL;
7394 free(*base_commit_id);
7395 if (*fileindex) {
7396 got_fileindex_free(*fileindex);
7397 *fileindex = NULL;
7399 lock_worktree(worktree, LOCK_SH);
7401 return err;
7404 const struct got_error *
7405 got_worktree_histedit_postpone(struct got_worktree *worktree,
7406 struct got_fileindex *fileindex)
7408 if (fileindex)
7409 got_fileindex_free(fileindex);
7410 return lock_worktree(worktree, LOCK_SH);
7413 const struct got_error *
7414 got_worktree_histedit_in_progress(int *in_progress,
7415 struct got_worktree *worktree)
7417 const struct got_error *err;
7418 char *tmp_branch_name = NULL;
7420 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7421 if (err)
7422 return err;
7424 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7425 free(tmp_branch_name);
7426 return NULL;
7429 const struct got_error *
7430 got_worktree_histedit_continue(struct got_object_id **commit_id,
7431 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7432 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7433 struct got_worktree *worktree, struct got_repository *repo)
7435 const struct got_error *err;
7436 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7437 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7438 struct got_reference *commit_ref = NULL;
7439 struct got_reference *base_commit_ref = NULL;
7440 char *fileindex_path = NULL;
7441 int have_staged_files = 0;
7443 *commit_id = NULL;
7444 *tmp_branch = NULL;
7445 *base_commit_id = NULL;
7446 *fileindex = NULL;
7448 err = lock_worktree(worktree, LOCK_EX);
7449 if (err)
7450 return err;
7452 err = open_fileindex(fileindex, &fileindex_path, worktree);
7453 if (err)
7454 goto done;
7456 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7457 &have_staged_files);
7458 if (err && err->code != GOT_ERR_CANCELLED)
7459 goto done;
7460 if (have_staged_files) {
7461 err = got_error(GOT_ERR_STAGED_PATHS);
7462 goto done;
7465 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7466 if (err)
7467 goto done;
7469 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7470 if (err)
7471 goto done;
7473 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7474 if (err)
7475 goto done;
7477 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7478 worktree);
7479 if (err)
7480 goto done;
7482 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7483 if (err)
7484 goto done;
7486 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7487 if (err)
7488 goto done;
7489 err = got_ref_resolve(commit_id, repo, commit_ref);
7490 if (err)
7491 goto done;
7493 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7494 if (err)
7495 goto done;
7496 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7497 if (err)
7498 goto done;
7500 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7501 if (err)
7502 goto done;
7503 done:
7504 free(commit_ref_name);
7505 free(branch_ref_name);
7506 free(fileindex_path);
7507 if (commit_ref)
7508 got_ref_close(commit_ref);
7509 if (base_commit_ref)
7510 got_ref_close(base_commit_ref);
7511 if (err) {
7512 free(*commit_id);
7513 *commit_id = NULL;
7514 free(*base_commit_id);
7515 *base_commit_id = NULL;
7516 if (*tmp_branch) {
7517 got_ref_close(*tmp_branch);
7518 *tmp_branch = NULL;
7520 if (*fileindex) {
7521 got_fileindex_free(*fileindex);
7522 *fileindex = NULL;
7524 lock_worktree(worktree, LOCK_EX);
7526 return err;
7529 static const struct got_error *
7530 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7532 const struct got_error *err;
7533 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7534 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7536 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7537 if (err)
7538 goto done;
7539 err = delete_ref(tmp_branch_name, repo);
7540 if (err)
7541 goto done;
7543 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7544 worktree);
7545 if (err)
7546 goto done;
7547 err = delete_ref(base_commit_ref_name, repo);
7548 if (err)
7549 goto done;
7551 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7552 if (err)
7553 goto done;
7554 err = delete_ref(branch_ref_name, repo);
7555 if (err)
7556 goto done;
7558 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7559 if (err)
7560 goto done;
7561 err = delete_ref(commit_ref_name, repo);
7562 if (err)
7563 goto done;
7564 done:
7565 free(tmp_branch_name);
7566 free(base_commit_ref_name);
7567 free(branch_ref_name);
7568 free(commit_ref_name);
7569 return err;
7572 const struct got_error *
7573 got_worktree_histedit_abort(struct got_worktree *worktree,
7574 struct got_fileindex *fileindex, struct got_repository *repo,
7575 struct got_reference *branch, struct got_object_id *base_commit_id,
7576 got_worktree_checkout_cb progress_cb, void *progress_arg)
7578 const struct got_error *err, *unlockerr, *sync_err;
7579 struct got_reference *resolved = NULL;
7580 char *fileindex_path = NULL;
7581 struct got_commit_object *commit = NULL;
7582 struct got_object_id *tree_id = NULL;
7583 struct revert_file_args rfa;
7585 err = lock_worktree(worktree, LOCK_EX);
7586 if (err)
7587 return err;
7589 err = got_object_open_as_commit(&commit, repo,
7590 worktree->base_commit_id);
7591 if (err)
7592 goto done;
7594 err = got_ref_open(&resolved, repo,
7595 got_ref_get_symref_target(branch), 0);
7596 if (err)
7597 goto done;
7599 err = got_worktree_set_head_ref(worktree, resolved);
7600 if (err)
7601 goto done;
7603 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7604 if (err)
7605 goto done;
7607 err = got_object_id_by_path(&tree_id, repo, commit,
7608 worktree->path_prefix);
7609 if (err)
7610 goto done;
7612 err = delete_histedit_refs(worktree, repo);
7613 if (err)
7614 goto done;
7616 err = get_fileindex_path(&fileindex_path, worktree);
7617 if (err)
7618 goto done;
7620 rfa.worktree = worktree;
7621 rfa.fileindex = fileindex;
7622 rfa.progress_cb = progress_cb;
7623 rfa.progress_arg = progress_arg;
7624 rfa.patch_cb = NULL;
7625 rfa.patch_arg = NULL;
7626 rfa.repo = repo;
7627 rfa.unlink_added_files = 0;
7628 err = worktree_status(worktree, "", fileindex, repo,
7629 revert_file, &rfa, NULL, NULL, 1, 0);
7630 if (err)
7631 goto sync;
7633 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7634 repo, progress_cb, progress_arg, NULL, NULL);
7635 sync:
7636 sync_err = sync_fileindex(fileindex, fileindex_path);
7637 if (sync_err && err == NULL)
7638 err = sync_err;
7639 done:
7640 got_ref_close(resolved);
7641 free(tree_id);
7642 free(fileindex_path);
7644 unlockerr = lock_worktree(worktree, LOCK_SH);
7645 if (unlockerr && err == NULL)
7646 err = unlockerr;
7647 return err;
7650 const struct got_error *
7651 got_worktree_histedit_complete(struct got_worktree *worktree,
7652 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7653 struct got_reference *edited_branch, struct got_repository *repo)
7655 const struct got_error *err, *unlockerr, *sync_err;
7656 struct got_object_id *new_head_commit_id = NULL;
7657 struct got_reference *resolved = NULL;
7658 char *fileindex_path = NULL;
7660 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7661 if (err)
7662 return err;
7664 err = got_ref_open(&resolved, repo,
7665 got_ref_get_symref_target(edited_branch), 0);
7666 if (err)
7667 goto done;
7669 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7670 resolved, new_head_commit_id, repo);
7671 if (err)
7672 goto done;
7674 err = got_ref_change_ref(resolved, new_head_commit_id);
7675 if (err)
7676 goto done;
7678 err = got_ref_write(resolved, repo);
7679 if (err)
7680 goto done;
7682 err = got_worktree_set_head_ref(worktree, resolved);
7683 if (err)
7684 goto done;
7686 err = delete_histedit_refs(worktree, repo);
7687 if (err)
7688 goto done;
7690 err = get_fileindex_path(&fileindex_path, worktree);
7691 if (err)
7692 goto done;
7693 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7694 sync_err = sync_fileindex(fileindex, fileindex_path);
7695 if (sync_err && err == NULL)
7696 err = sync_err;
7697 done:
7698 got_fileindex_free(fileindex);
7699 free(fileindex_path);
7700 free(new_head_commit_id);
7701 unlockerr = lock_worktree(worktree, LOCK_SH);
7702 if (unlockerr && err == NULL)
7703 err = unlockerr;
7704 return err;
7707 const struct got_error *
7708 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7709 struct got_object_id *commit_id, struct got_repository *repo)
7711 const struct got_error *err;
7712 char *commit_ref_name;
7714 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7715 if (err)
7716 return err;
7718 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7719 if (err)
7720 goto done;
7722 err = delete_ref(commit_ref_name, repo);
7723 done:
7724 free(commit_ref_name);
7725 return err;
7728 const struct got_error *
7729 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7730 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7731 struct got_worktree *worktree, const char *refname,
7732 struct got_repository *repo)
7734 const struct got_error *err = NULL;
7735 char *fileindex_path = NULL;
7736 struct check_rebase_ok_arg ok_arg;
7738 *fileindex = NULL;
7739 *branch_ref = NULL;
7740 *base_branch_ref = NULL;
7742 err = lock_worktree(worktree, LOCK_EX);
7743 if (err)
7744 return err;
7746 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7747 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7748 "cannot integrate a branch into itself; "
7749 "update -b or different branch name required");
7750 goto done;
7753 err = open_fileindex(fileindex, &fileindex_path, worktree);
7754 if (err)
7755 goto done;
7757 /* Preconditions are the same as for rebase. */
7758 ok_arg.worktree = worktree;
7759 ok_arg.repo = repo;
7760 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7761 &ok_arg);
7762 if (err)
7763 goto done;
7765 err = got_ref_open(branch_ref, repo, refname, 1);
7766 if (err)
7767 goto done;
7769 err = got_ref_open(base_branch_ref, repo,
7770 got_worktree_get_head_ref_name(worktree), 1);
7771 done:
7772 if (err) {
7773 if (*branch_ref) {
7774 got_ref_close(*branch_ref);
7775 *branch_ref = NULL;
7777 if (*base_branch_ref) {
7778 got_ref_close(*base_branch_ref);
7779 *base_branch_ref = NULL;
7781 if (*fileindex) {
7782 got_fileindex_free(*fileindex);
7783 *fileindex = NULL;
7785 lock_worktree(worktree, LOCK_SH);
7787 return err;
7790 const struct got_error *
7791 got_worktree_integrate_continue(struct got_worktree *worktree,
7792 struct got_fileindex *fileindex, struct got_repository *repo,
7793 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7794 got_worktree_checkout_cb progress_cb, void *progress_arg,
7795 got_cancel_cb cancel_cb, void *cancel_arg)
7797 const struct got_error *err = NULL, *sync_err, *unlockerr;
7798 char *fileindex_path = NULL;
7799 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7800 struct got_commit_object *commit = NULL;
7802 err = get_fileindex_path(&fileindex_path, worktree);
7803 if (err)
7804 goto done;
7806 err = got_ref_resolve(&commit_id, repo, branch_ref);
7807 if (err)
7808 goto done;
7810 err = got_object_open_as_commit(&commit, repo, commit_id);
7811 if (err)
7812 goto done;
7814 err = got_object_id_by_path(&tree_id, repo, commit,
7815 worktree->path_prefix);
7816 if (err)
7817 goto done;
7819 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7820 if (err)
7821 goto done;
7823 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7824 progress_cb, progress_arg, cancel_cb, cancel_arg);
7825 if (err)
7826 goto sync;
7828 err = got_ref_change_ref(base_branch_ref, commit_id);
7829 if (err)
7830 goto sync;
7832 err = got_ref_write(base_branch_ref, repo);
7833 if (err)
7834 goto sync;
7836 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7837 sync:
7838 sync_err = sync_fileindex(fileindex, fileindex_path);
7839 if (sync_err && err == NULL)
7840 err = sync_err;
7842 done:
7843 unlockerr = got_ref_unlock(branch_ref);
7844 if (unlockerr && err == NULL)
7845 err = unlockerr;
7846 got_ref_close(branch_ref);
7848 unlockerr = got_ref_unlock(base_branch_ref);
7849 if (unlockerr && err == NULL)
7850 err = unlockerr;
7851 got_ref_close(base_branch_ref);
7853 got_fileindex_free(fileindex);
7854 free(fileindex_path);
7855 free(tree_id);
7856 if (commit)
7857 got_object_commit_close(commit);
7859 unlockerr = lock_worktree(worktree, LOCK_SH);
7860 if (unlockerr && err == NULL)
7861 err = unlockerr;
7862 return err;
7865 const struct got_error *
7866 got_worktree_integrate_abort(struct got_worktree *worktree,
7867 struct got_fileindex *fileindex, struct got_repository *repo,
7868 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7870 const struct got_error *err = NULL, *unlockerr = NULL;
7872 got_fileindex_free(fileindex);
7874 err = lock_worktree(worktree, LOCK_SH);
7876 unlockerr = got_ref_unlock(branch_ref);
7877 if (unlockerr && err == NULL)
7878 err = unlockerr;
7879 got_ref_close(branch_ref);
7881 unlockerr = got_ref_unlock(base_branch_ref);
7882 if (unlockerr && err == NULL)
7883 err = unlockerr;
7884 got_ref_close(base_branch_ref);
7886 return err;
7889 const struct got_error *
7890 got_worktree_merge_postpone(struct got_worktree *worktree,
7891 struct got_fileindex *fileindex)
7893 const struct got_error *err, *sync_err;
7894 char *fileindex_path = NULL;
7896 err = get_fileindex_path(&fileindex_path, worktree);
7897 if (err)
7898 goto done;
7900 sync_err = sync_fileindex(fileindex, fileindex_path);
7902 err = lock_worktree(worktree, LOCK_SH);
7903 if (sync_err && err == NULL)
7904 err = sync_err;
7905 done:
7906 got_fileindex_free(fileindex);
7907 free(fileindex_path);
7908 return err;
7911 static const struct got_error *
7912 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7914 const struct got_error *err;
7915 char *branch_refname = NULL, *commit_refname = NULL;
7917 err = get_merge_branch_ref_name(&branch_refname, worktree);
7918 if (err)
7919 goto done;
7920 err = delete_ref(branch_refname, repo);
7921 if (err)
7922 goto done;
7924 err = get_merge_commit_ref_name(&commit_refname, worktree);
7925 if (err)
7926 goto done;
7927 err = delete_ref(commit_refname, repo);
7928 if (err)
7929 goto done;
7931 done:
7932 free(branch_refname);
7933 free(commit_refname);
7934 return err;
7937 struct merge_commit_msg_arg {
7938 struct got_worktree *worktree;
7939 const char *branch_name;
7942 static const struct got_error *
7943 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7944 const char *diff_path, char **logmsg, void *arg)
7946 struct merge_commit_msg_arg *a = arg;
7948 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7949 got_worktree_get_head_ref_name(a->worktree)) == -1)
7950 return got_error_from_errno("asprintf");
7952 return NULL;
7956 const struct got_error *
7957 got_worktree_merge_branch(struct got_worktree *worktree,
7958 struct got_fileindex *fileindex,
7959 struct got_object_id *yca_commit_id,
7960 struct got_object_id *branch_tip,
7961 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7962 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7964 const struct got_error *err;
7965 char *fileindex_path = NULL;
7967 err = get_fileindex_path(&fileindex_path, worktree);
7968 if (err)
7969 goto done;
7971 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7972 worktree);
7973 if (err)
7974 goto done;
7976 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7977 branch_tip, repo, progress_cb, progress_arg,
7978 cancel_cb, cancel_arg);
7979 done:
7980 free(fileindex_path);
7981 return err;
7984 const struct got_error *
7985 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7986 struct got_worktree *worktree, struct got_fileindex *fileindex,
7987 const char *author, const char *committer, int allow_bad_symlinks,
7988 struct got_object_id *branch_tip, const char *branch_name,
7989 int allow_conflict, struct got_repository *repo,
7990 got_worktree_status_cb status_cb, void *status_arg)
7993 const struct got_error *err = NULL, *sync_err;
7994 struct got_pathlist_head commitable_paths;
7995 struct collect_commitables_arg cc_arg;
7996 struct got_pathlist_entry *pe;
7997 struct got_reference *head_ref = NULL;
7998 struct got_object_id *head_commit_id = NULL;
7999 int have_staged_files = 0;
8000 struct merge_commit_msg_arg mcm_arg;
8001 char *fileindex_path = NULL;
8003 memset(&cc_arg, 0, sizeof(cc_arg));
8004 *new_commit_id = NULL;
8006 TAILQ_INIT(&commitable_paths);
8008 err = get_fileindex_path(&fileindex_path, worktree);
8009 if (err)
8010 goto done;
8012 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8013 if (err)
8014 goto done;
8016 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8017 if (err)
8018 goto done;
8020 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8021 &have_staged_files);
8022 if (err && err->code != GOT_ERR_CANCELLED)
8023 goto done;
8024 if (have_staged_files) {
8025 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8026 goto done;
8029 cc_arg.commitable_paths = &commitable_paths;
8030 cc_arg.worktree = worktree;
8031 cc_arg.fileindex = fileindex;
8032 cc_arg.repo = repo;
8033 cc_arg.have_staged_files = have_staged_files;
8034 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8035 cc_arg.commit_conflicts = allow_conflict;
8036 err = worktree_status(worktree, "", fileindex, repo,
8037 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8038 if (err)
8039 goto done;
8041 if (TAILQ_EMPTY(&commitable_paths)) {
8042 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8043 "merge of %s cannot proceed", branch_name);
8044 goto done;
8047 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8048 struct got_commitable *ct = pe->data;
8049 const char *ct_path = ct->in_repo_path;
8051 while (ct_path[0] == '/')
8052 ct_path++;
8053 err = check_out_of_date(ct_path, ct->status,
8054 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8055 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8056 if (err)
8057 goto done;
8061 mcm_arg.worktree = worktree;
8062 mcm_arg.branch_name = branch_name;
8063 err = commit_worktree(new_commit_id, &commitable_paths,
8064 head_commit_id, branch_tip, worktree, author, committer, NULL,
8065 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8066 if (err)
8067 goto done;
8069 err = update_fileindex_after_commit(worktree, &commitable_paths,
8070 *new_commit_id, fileindex, have_staged_files);
8071 sync_err = sync_fileindex(fileindex, fileindex_path);
8072 if (sync_err && err == NULL)
8073 err = sync_err;
8074 done:
8075 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8076 struct got_commitable *ct = pe->data;
8078 free_commitable(ct);
8080 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8081 free(fileindex_path);
8082 return err;
8085 const struct got_error *
8086 got_worktree_merge_complete(struct got_worktree *worktree,
8087 struct got_fileindex *fileindex, struct got_repository *repo)
8089 const struct got_error *err, *unlockerr, *sync_err;
8090 char *fileindex_path = NULL;
8092 err = delete_merge_refs(worktree, repo);
8093 if (err)
8094 goto done;
8096 err = get_fileindex_path(&fileindex_path, worktree);
8097 if (err)
8098 goto done;
8099 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8100 sync_err = sync_fileindex(fileindex, fileindex_path);
8101 if (sync_err && err == NULL)
8102 err = sync_err;
8103 done:
8104 got_fileindex_free(fileindex);
8105 free(fileindex_path);
8106 unlockerr = lock_worktree(worktree, LOCK_SH);
8107 if (unlockerr && err == NULL)
8108 err = unlockerr;
8109 return err;
8112 const struct got_error *
8113 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8114 struct got_repository *repo)
8116 const struct got_error *err;
8117 char *branch_refname = NULL;
8118 struct got_reference *branch_ref = NULL;
8120 *in_progress = 0;
8122 err = get_merge_branch_ref_name(&branch_refname, worktree);
8123 if (err)
8124 return err;
8125 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8126 free(branch_refname);
8127 if (err) {
8128 if (err->code != GOT_ERR_NOT_REF)
8129 return err;
8130 } else
8131 *in_progress = 1;
8133 return NULL;
8136 const struct got_error *got_worktree_merge_prepare(
8137 struct got_fileindex **fileindex, struct got_worktree *worktree,
8138 struct got_reference *branch, struct got_repository *repo)
8140 const struct got_error *err = NULL;
8141 char *fileindex_path = NULL;
8142 char *branch_refname = NULL, *commit_refname = NULL;
8143 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8144 struct got_reference *commit_ref = NULL;
8145 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8146 struct check_rebase_ok_arg ok_arg;
8148 *fileindex = NULL;
8150 err = lock_worktree(worktree, LOCK_EX);
8151 if (err)
8152 return err;
8154 err = open_fileindex(fileindex, &fileindex_path, worktree);
8155 if (err)
8156 goto done;
8158 /* Preconditions are the same as for rebase. */
8159 ok_arg.worktree = worktree;
8160 ok_arg.repo = repo;
8161 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8162 &ok_arg);
8163 if (err)
8164 goto done;
8166 err = get_merge_branch_ref_name(&branch_refname, worktree);
8167 if (err)
8168 return err;
8170 err = get_merge_commit_ref_name(&commit_refname, worktree);
8171 if (err)
8172 return err;
8174 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8175 0);
8176 if (err)
8177 goto done;
8179 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8180 if (err)
8181 goto done;
8183 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8184 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8185 goto done;
8188 err = got_ref_resolve(&branch_tip, repo, branch);
8189 if (err)
8190 goto done;
8192 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8193 if (err)
8194 goto done;
8195 err = got_ref_write(branch_ref, repo);
8196 if (err)
8197 goto done;
8199 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8200 if (err)
8201 goto done;
8202 err = got_ref_write(commit_ref, repo);
8203 if (err)
8204 goto done;
8206 done:
8207 free(branch_refname);
8208 free(commit_refname);
8209 free(fileindex_path);
8210 if (branch_ref)
8211 got_ref_close(branch_ref);
8212 if (commit_ref)
8213 got_ref_close(commit_ref);
8214 if (wt_branch)
8215 got_ref_close(wt_branch);
8216 free(wt_branch_tip);
8217 if (err) {
8218 if (*fileindex) {
8219 got_fileindex_free(*fileindex);
8220 *fileindex = NULL;
8222 lock_worktree(worktree, LOCK_SH);
8224 return err;
8227 const struct got_error *
8228 got_worktree_merge_continue(char **branch_name,
8229 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8230 struct got_worktree *worktree, struct got_repository *repo)
8232 const struct got_error *err;
8233 char *commit_refname = NULL, *branch_refname = NULL;
8234 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8235 char *fileindex_path = NULL;
8236 int have_staged_files = 0;
8238 *branch_name = NULL;
8239 *branch_tip = NULL;
8240 *fileindex = NULL;
8242 err = lock_worktree(worktree, LOCK_EX);
8243 if (err)
8244 return err;
8246 err = open_fileindex(fileindex, &fileindex_path, worktree);
8247 if (err)
8248 goto done;
8250 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8251 &have_staged_files);
8252 if (err && err->code != GOT_ERR_CANCELLED)
8253 goto done;
8254 if (have_staged_files) {
8255 err = got_error(GOT_ERR_STAGED_PATHS);
8256 goto done;
8259 err = get_merge_branch_ref_name(&branch_refname, worktree);
8260 if (err)
8261 goto done;
8263 err = get_merge_commit_ref_name(&commit_refname, worktree);
8264 if (err)
8265 goto done;
8267 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8268 if (err)
8269 goto done;
8271 if (!got_ref_is_symbolic(branch_ref)) {
8272 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8273 "%s is not a symbolic reference",
8274 got_ref_get_name(branch_ref));
8275 goto done;
8277 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8278 if (*branch_name == NULL) {
8279 err = got_error_from_errno("strdup");
8280 goto done;
8283 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8284 if (err)
8285 goto done;
8287 err = got_ref_resolve(branch_tip, repo, commit_ref);
8288 if (err)
8289 goto done;
8290 done:
8291 free(commit_refname);
8292 free(branch_refname);
8293 free(fileindex_path);
8294 if (commit_ref)
8295 got_ref_close(commit_ref);
8296 if (branch_ref)
8297 got_ref_close(branch_ref);
8298 if (err) {
8299 if (*branch_name) {
8300 free(*branch_name);
8301 *branch_name = NULL;
8303 free(*branch_tip);
8304 *branch_tip = NULL;
8305 if (*fileindex) {
8306 got_fileindex_free(*fileindex);
8307 *fileindex = NULL;
8309 lock_worktree(worktree, LOCK_SH);
8311 return err;
8314 const struct got_error *
8315 got_worktree_merge_abort(struct got_worktree *worktree,
8316 struct got_fileindex *fileindex, struct got_repository *repo,
8317 got_worktree_checkout_cb progress_cb, void *progress_arg)
8319 const struct got_error *err, *unlockerr, *sync_err;
8320 struct got_object_id *commit_id = NULL;
8321 struct got_commit_object *commit = NULL;
8322 char *fileindex_path = NULL;
8323 struct revert_file_args rfa;
8324 struct got_object_id *tree_id = NULL;
8326 err = got_object_open_as_commit(&commit, repo,
8327 worktree->base_commit_id);
8328 if (err)
8329 goto done;
8331 err = got_object_id_by_path(&tree_id, repo, commit,
8332 worktree->path_prefix);
8333 if (err)
8334 goto done;
8336 err = delete_merge_refs(worktree, repo);
8337 if (err)
8338 goto done;
8340 err = get_fileindex_path(&fileindex_path, worktree);
8341 if (err)
8342 goto done;
8344 rfa.worktree = worktree;
8345 rfa.fileindex = fileindex;
8346 rfa.progress_cb = progress_cb;
8347 rfa.progress_arg = progress_arg;
8348 rfa.patch_cb = NULL;
8349 rfa.patch_arg = NULL;
8350 rfa.repo = repo;
8351 rfa.unlink_added_files = 1;
8352 err = worktree_status(worktree, "", fileindex, repo,
8353 revert_file, &rfa, NULL, NULL, 1, 0);
8354 if (err)
8355 goto sync;
8357 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8358 repo, progress_cb, progress_arg, NULL, NULL);
8359 sync:
8360 sync_err = sync_fileindex(fileindex, fileindex_path);
8361 if (sync_err && err == NULL)
8362 err = sync_err;
8363 done:
8364 free(tree_id);
8365 free(commit_id);
8366 if (commit)
8367 got_object_commit_close(commit);
8368 if (fileindex)
8369 got_fileindex_free(fileindex);
8370 free(fileindex_path);
8372 unlockerr = lock_worktree(worktree, LOCK_SH);
8373 if (unlockerr && err == NULL)
8374 err = unlockerr;
8375 return err;
8378 struct check_stage_ok_arg {
8379 struct got_object_id *head_commit_id;
8380 struct got_worktree *worktree;
8381 struct got_fileindex *fileindex;
8382 struct got_repository *repo;
8383 int have_changes;
8386 static const struct got_error *
8387 check_stage_ok(void *arg, unsigned char status,
8388 unsigned char staged_status, const char *relpath,
8389 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8390 struct got_object_id *commit_id, int dirfd, const char *de_name)
8392 struct check_stage_ok_arg *a = arg;
8393 const struct got_error *err = NULL;
8394 struct got_fileindex_entry *ie;
8395 struct got_object_id base_commit_id;
8396 struct got_object_id *base_commit_idp = NULL;
8397 char *in_repo_path = NULL, *p;
8399 if (status == GOT_STATUS_UNVERSIONED ||
8400 status == GOT_STATUS_NO_CHANGE)
8401 return NULL;
8402 if (status == GOT_STATUS_NONEXISTENT)
8403 return got_error_set_errno(ENOENT, relpath);
8405 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8406 if (ie == NULL)
8407 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8409 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8410 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8411 relpath) == -1)
8412 return got_error_from_errno("asprintf");
8414 if (got_fileindex_entry_has_commit(ie)) {
8415 base_commit_idp = got_fileindex_entry_get_commit_id(
8416 &base_commit_id, ie);
8419 if (status == GOT_STATUS_CONFLICT) {
8420 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8421 goto done;
8422 } else if (status != GOT_STATUS_ADD &&
8423 status != GOT_STATUS_MODIFY &&
8424 status != GOT_STATUS_DELETE) {
8425 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8426 goto done;
8429 a->have_changes = 1;
8431 p = in_repo_path;
8432 while (p[0] == '/')
8433 p++;
8434 err = check_out_of_date(p, status, staged_status,
8435 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8436 GOT_ERR_STAGE_OUT_OF_DATE);
8437 done:
8438 free(in_repo_path);
8439 return err;
8442 struct stage_path_arg {
8443 struct got_worktree *worktree;
8444 struct got_fileindex *fileindex;
8445 struct got_repository *repo;
8446 got_worktree_status_cb status_cb;
8447 void *status_arg;
8448 got_worktree_patch_cb patch_cb;
8449 void *patch_arg;
8450 int staged_something;
8451 int allow_bad_symlinks;
8454 static const struct got_error *
8455 stage_path(void *arg, unsigned char status,
8456 unsigned char staged_status, const char *relpath,
8457 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8458 struct got_object_id *commit_id, int dirfd, const char *de_name)
8460 struct stage_path_arg *a = arg;
8461 const struct got_error *err = NULL;
8462 struct got_fileindex_entry *ie;
8463 char *ondisk_path = NULL, *path_content = NULL;
8464 uint32_t stage;
8465 struct got_object_id *new_staged_blob_id = NULL;
8466 struct stat sb;
8468 if (status == GOT_STATUS_UNVERSIONED)
8469 return NULL;
8471 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8472 if (ie == NULL)
8473 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8475 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8476 relpath)== -1)
8477 return got_error_from_errno("asprintf");
8479 switch (status) {
8480 case GOT_STATUS_ADD:
8481 case GOT_STATUS_MODIFY:
8482 /* XXX could sb.st_mode be passed in by our caller? */
8483 if (lstat(ondisk_path, &sb) == -1) {
8484 err = got_error_from_errno2("lstat", ondisk_path);
8485 break;
8487 if (a->patch_cb) {
8488 if (status == GOT_STATUS_ADD) {
8489 int choice = GOT_PATCH_CHOICE_NONE;
8490 err = (*a->patch_cb)(&choice, a->patch_arg,
8491 status, ie->path, NULL, 1, 1);
8492 if (err)
8493 break;
8494 if (choice != GOT_PATCH_CHOICE_YES)
8495 break;
8496 } else {
8497 err = create_patched_content(&path_content, 0,
8498 staged_blob_id ? staged_blob_id : blob_id,
8499 ondisk_path, dirfd, de_name, ie->path,
8500 a->repo, a->patch_cb, a->patch_arg);
8501 if (err || path_content == NULL)
8502 break;
8505 err = got_object_blob_create(&new_staged_blob_id,
8506 path_content ? path_content : ondisk_path, a->repo);
8507 if (err)
8508 break;
8509 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8510 SHA1_DIGEST_LENGTH);
8511 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8512 stage = GOT_FILEIDX_STAGE_ADD;
8513 else
8514 stage = GOT_FILEIDX_STAGE_MODIFY;
8515 got_fileindex_entry_stage_set(ie, stage);
8516 if (S_ISLNK(sb.st_mode)) {
8517 int is_bad_symlink = 0;
8518 if (!a->allow_bad_symlinks) {
8519 char target_path[PATH_MAX];
8520 ssize_t target_len;
8521 target_len = readlink(ondisk_path, target_path,
8522 sizeof(target_path));
8523 if (target_len == -1) {
8524 err = got_error_from_errno2("readlink",
8525 ondisk_path);
8526 break;
8528 err = is_bad_symlink_target(&is_bad_symlink,
8529 target_path, target_len, ondisk_path,
8530 a->worktree->root_path);
8531 if (err)
8532 break;
8533 if (is_bad_symlink) {
8534 err = got_error_path(ondisk_path,
8535 GOT_ERR_BAD_SYMLINK);
8536 break;
8539 if (is_bad_symlink)
8540 got_fileindex_entry_staged_filetype_set(ie,
8541 GOT_FILEIDX_MODE_BAD_SYMLINK);
8542 else
8543 got_fileindex_entry_staged_filetype_set(ie,
8544 GOT_FILEIDX_MODE_SYMLINK);
8545 } else {
8546 got_fileindex_entry_staged_filetype_set(ie,
8547 GOT_FILEIDX_MODE_REGULAR_FILE);
8549 a->staged_something = 1;
8550 if (a->status_cb == NULL)
8551 break;
8552 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 get_staged_status(ie), relpath, blob_id,
8554 new_staged_blob_id, NULL, dirfd, de_name);
8555 if (err)
8556 break;
8558 * When staging the reverse of the staged diff,
8559 * implicitly unstage the file.
8561 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8562 sizeof(ie->blob_sha1)) == 0) {
8563 got_fileindex_entry_stage_set(ie,
8564 GOT_FILEIDX_STAGE_NONE);
8566 break;
8567 case GOT_STATUS_DELETE:
8568 if (staged_status == GOT_STATUS_DELETE)
8569 break;
8570 if (a->patch_cb) {
8571 int choice = GOT_PATCH_CHOICE_NONE;
8572 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8573 ie->path, NULL, 1, 1);
8574 if (err)
8575 break;
8576 if (choice == GOT_PATCH_CHOICE_NO)
8577 break;
8578 if (choice != GOT_PATCH_CHOICE_YES) {
8579 err = got_error(GOT_ERR_PATCH_CHOICE);
8580 break;
8583 stage = GOT_FILEIDX_STAGE_DELETE;
8584 got_fileindex_entry_stage_set(ie, stage);
8585 a->staged_something = 1;
8586 if (a->status_cb == NULL)
8587 break;
8588 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8589 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8590 de_name);
8591 break;
8592 case GOT_STATUS_NO_CHANGE:
8593 break;
8594 case GOT_STATUS_CONFLICT:
8595 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8596 break;
8597 case GOT_STATUS_NONEXISTENT:
8598 err = got_error_set_errno(ENOENT, relpath);
8599 break;
8600 default:
8601 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8602 break;
8605 if (path_content && unlink(path_content) == -1 && err == NULL)
8606 err = got_error_from_errno2("unlink", path_content);
8607 free(path_content);
8608 free(ondisk_path);
8609 free(new_staged_blob_id);
8610 return err;
8613 const struct got_error *
8614 got_worktree_stage(struct got_worktree *worktree,
8615 struct got_pathlist_head *paths,
8616 got_worktree_status_cb status_cb, void *status_arg,
8617 got_worktree_patch_cb patch_cb, void *patch_arg,
8618 int allow_bad_symlinks, struct got_repository *repo)
8620 const struct got_error *err = NULL, *sync_err, *unlockerr;
8621 struct got_pathlist_entry *pe;
8622 struct got_fileindex *fileindex = NULL;
8623 char *fileindex_path = NULL;
8624 struct got_reference *head_ref = NULL;
8625 struct got_object_id *head_commit_id = NULL;
8626 struct check_stage_ok_arg oka;
8627 struct stage_path_arg spa;
8629 err = lock_worktree(worktree, LOCK_EX);
8630 if (err)
8631 return err;
8633 err = got_ref_open(&head_ref, repo,
8634 got_worktree_get_head_ref_name(worktree), 0);
8635 if (err)
8636 goto done;
8637 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8638 if (err)
8639 goto done;
8640 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8641 if (err)
8642 goto done;
8644 /* Check pre-conditions before staging anything. */
8645 oka.head_commit_id = head_commit_id;
8646 oka.worktree = worktree;
8647 oka.fileindex = fileindex;
8648 oka.repo = repo;
8649 oka.have_changes = 0;
8650 TAILQ_FOREACH(pe, paths, entry) {
8651 err = worktree_status(worktree, pe->path, fileindex, repo,
8652 check_stage_ok, &oka, NULL, NULL, 1, 0);
8653 if (err)
8654 goto done;
8656 if (!oka.have_changes) {
8657 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8658 goto done;
8661 spa.worktree = worktree;
8662 spa.fileindex = fileindex;
8663 spa.repo = repo;
8664 spa.patch_cb = patch_cb;
8665 spa.patch_arg = patch_arg;
8666 spa.status_cb = status_cb;
8667 spa.status_arg = status_arg;
8668 spa.staged_something = 0;
8669 spa.allow_bad_symlinks = allow_bad_symlinks;
8670 TAILQ_FOREACH(pe, paths, entry) {
8671 err = worktree_status(worktree, pe->path, fileindex, repo,
8672 stage_path, &spa, NULL, NULL, 1, 0);
8673 if (err)
8674 goto done;
8676 if (!spa.staged_something) {
8677 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8678 goto done;
8681 sync_err = sync_fileindex(fileindex, fileindex_path);
8682 if (sync_err && err == NULL)
8683 err = sync_err;
8684 done:
8685 if (head_ref)
8686 got_ref_close(head_ref);
8687 free(head_commit_id);
8688 free(fileindex_path);
8689 if (fileindex)
8690 got_fileindex_free(fileindex);
8691 unlockerr = lock_worktree(worktree, LOCK_SH);
8692 if (unlockerr && err == NULL)
8693 err = unlockerr;
8694 return err;
8697 struct unstage_path_arg {
8698 struct got_worktree *worktree;
8699 struct got_fileindex *fileindex;
8700 struct got_repository *repo;
8701 got_worktree_checkout_cb progress_cb;
8702 void *progress_arg;
8703 got_worktree_patch_cb patch_cb;
8704 void *patch_arg;
8707 static const struct got_error *
8708 create_unstaged_content(char **path_unstaged_content,
8709 char **path_new_staged_content, struct got_object_id *blob_id,
8710 struct got_object_id *staged_blob_id, const char *relpath,
8711 struct got_repository *repo,
8712 got_worktree_patch_cb patch_cb, void *patch_arg)
8714 const struct got_error *err, *free_err;
8715 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8716 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8717 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8718 struct got_diffreg_result *diffreg_result = NULL;
8719 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8720 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8721 int fd1 = -1, fd2 = -1;
8723 *path_unstaged_content = NULL;
8724 *path_new_staged_content = NULL;
8726 err = got_object_id_str(&label1, blob_id);
8727 if (err)
8728 return err;
8730 fd1 = got_opentempfd();
8731 if (fd1 == -1) {
8732 err = got_error_from_errno("got_opentempfd");
8733 goto done;
8735 fd2 = got_opentempfd();
8736 if (fd2 == -1) {
8737 err = got_error_from_errno("got_opentempfd");
8738 goto done;
8741 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8742 if (err)
8743 goto done;
8745 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8746 if (err)
8747 goto done;
8749 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8750 if (err)
8751 goto done;
8753 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8754 fd2);
8755 if (err)
8756 goto done;
8758 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8759 if (err)
8760 goto done;
8762 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8763 if (err)
8764 goto done;
8766 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8767 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8768 if (err)
8769 goto done;
8771 err = got_opentemp_named(path_unstaged_content, &outfile,
8772 "got-unstaged-content", "");
8773 if (err)
8774 goto done;
8775 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8776 "got-new-staged-content", "");
8777 if (err)
8778 goto done;
8780 if (fseek(f1, 0L, SEEK_SET) == -1) {
8781 err = got_ferror(f1, GOT_ERR_IO);
8782 goto done;
8784 if (fseek(f2, 0L, SEEK_SET) == -1) {
8785 err = got_ferror(f2, GOT_ERR_IO);
8786 goto done;
8788 /* Count the number of actual changes in the diff result. */
8789 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8790 struct diff_chunk_context cc = {};
8791 diff_chunk_context_load_change(&cc, &nchunks_used,
8792 diffreg_result->result, n, 0);
8793 nchanges++;
8795 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8796 int choice;
8797 err = apply_or_reject_change(&choice, &nchunks_used,
8798 diffreg_result->result, n, relpath, f1, f2,
8799 &line_cur1, &line_cur2,
8800 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8801 if (err)
8802 goto done;
8803 if (choice == GOT_PATCH_CHOICE_YES)
8804 have_content = 1;
8805 else
8806 have_rejected_content = 1;
8807 if (choice == GOT_PATCH_CHOICE_QUIT)
8808 break;
8810 if (have_content || have_rejected_content)
8811 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8812 outfile, rejectfile);
8813 done:
8814 free(label1);
8815 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8816 err = got_error_from_errno("close");
8817 if (blob)
8818 got_object_blob_close(blob);
8819 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8820 err = got_error_from_errno("close");
8821 if (staged_blob)
8822 got_object_blob_close(staged_blob);
8823 free_err = got_diffreg_result_free(diffreg_result);
8824 if (free_err && err == NULL)
8825 err = free_err;
8826 if (f1 && fclose(f1) == EOF && err == NULL)
8827 err = got_error_from_errno2("fclose", path1);
8828 if (f2 && fclose(f2) == EOF && err == NULL)
8829 err = got_error_from_errno2("fclose", path2);
8830 if (outfile && fclose(outfile) == EOF && err == NULL)
8831 err = got_error_from_errno2("fclose", *path_unstaged_content);
8832 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8833 err = got_error_from_errno2("fclose", *path_new_staged_content);
8834 if (path1 && unlink(path1) == -1 && err == NULL)
8835 err = got_error_from_errno2("unlink", path1);
8836 if (path2 && unlink(path2) == -1 && err == NULL)
8837 err = got_error_from_errno2("unlink", path2);
8838 if (err || !have_content) {
8839 if (*path_unstaged_content &&
8840 unlink(*path_unstaged_content) == -1 && err == NULL)
8841 err = got_error_from_errno2("unlink",
8842 *path_unstaged_content);
8843 free(*path_unstaged_content);
8844 *path_unstaged_content = NULL;
8846 if (err || !have_content || !have_rejected_content) {
8847 if (*path_new_staged_content &&
8848 unlink(*path_new_staged_content) == -1 && err == NULL)
8849 err = got_error_from_errno2("unlink",
8850 *path_new_staged_content);
8851 free(*path_new_staged_content);
8852 *path_new_staged_content = NULL;
8854 free(path1);
8855 free(path2);
8856 return err;
8859 static const struct got_error *
8860 unstage_hunks(struct got_object_id *staged_blob_id,
8861 struct got_blob_object *blob_base,
8862 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8863 const char *ondisk_path, const char *label_orig,
8864 struct got_worktree *worktree, struct got_repository *repo,
8865 got_worktree_patch_cb patch_cb, void *patch_arg,
8866 got_worktree_checkout_cb progress_cb, void *progress_arg)
8868 const struct got_error *err = NULL;
8869 char *path_unstaged_content = NULL;
8870 char *path_new_staged_content = NULL;
8871 char *parent = NULL, *base_path = NULL;
8872 char *blob_base_path = NULL;
8873 struct got_object_id *new_staged_blob_id = NULL;
8874 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8875 struct stat sb;
8877 err = create_unstaged_content(&path_unstaged_content,
8878 &path_new_staged_content, blob_id, staged_blob_id,
8879 ie->path, repo, patch_cb, patch_arg);
8880 if (err)
8881 return err;
8883 if (path_unstaged_content == NULL)
8884 return NULL;
8886 if (path_new_staged_content) {
8887 err = got_object_blob_create(&new_staged_blob_id,
8888 path_new_staged_content, repo);
8889 if (err)
8890 goto done;
8893 f = fopen(path_unstaged_content, "re");
8894 if (f == NULL) {
8895 err = got_error_from_errno2("fopen",
8896 path_unstaged_content);
8897 goto done;
8899 if (fstat(fileno(f), &sb) == -1) {
8900 err = got_error_from_errno2("fstat", path_unstaged_content);
8901 goto done;
8903 if (got_fileindex_entry_staged_filetype_get(ie) ==
8904 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8905 char link_target[PATH_MAX];
8906 size_t r;
8907 r = fread(link_target, 1, sizeof(link_target), f);
8908 if (r == 0 && ferror(f)) {
8909 err = got_error_from_errno("fread");
8910 goto done;
8912 if (r >= sizeof(link_target)) { /* should not happen */
8913 err = got_error(GOT_ERR_NO_SPACE);
8914 goto done;
8916 link_target[r] = '\0';
8917 err = merge_symlink(worktree, blob_base,
8918 ondisk_path, ie->path, label_orig, link_target,
8919 worktree->base_commit_id, repo, progress_cb,
8920 progress_arg);
8921 } else {
8922 int local_changes_subsumed;
8924 err = got_path_dirname(&parent, ondisk_path);
8925 if (err)
8926 return err;
8928 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8929 parent) == -1) {
8930 err = got_error_from_errno("asprintf");
8931 base_path = NULL;
8932 goto done;
8935 err = got_opentemp_named(&blob_base_path, &f_base,
8936 base_path, "");
8937 if (err)
8938 goto done;
8939 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8940 blob_base);
8941 if (err)
8942 goto done;
8945 * In order the run a 3-way merge with a symlink we copy the symlink's
8946 * target path into a temporary file and use that file with diff3.
8948 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8949 err = dump_symlink_target_path_to_file(&f_deriv2,
8950 ondisk_path);
8951 if (err)
8952 goto done;
8953 } else {
8954 int fd;
8955 fd = open(ondisk_path,
8956 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8957 if (fd == -1) {
8958 err = got_error_from_errno2("open", ondisk_path);
8959 goto done;
8961 f_deriv2 = fdopen(fd, "r");
8962 if (f_deriv2 == NULL) {
8963 err = got_error_from_errno2("fdopen", ondisk_path);
8964 close(fd);
8965 goto done;
8969 err = merge_file(&local_changes_subsumed, worktree,
8970 f_base, f, f_deriv2, ondisk_path, ie->path,
8971 got_fileindex_perms_to_st(ie),
8972 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8973 repo, progress_cb, progress_arg);
8975 if (err)
8976 goto done;
8978 if (new_staged_blob_id) {
8979 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8980 SHA1_DIGEST_LENGTH);
8981 } else {
8982 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8983 got_fileindex_entry_staged_filetype_set(ie, 0);
8985 done:
8986 free(new_staged_blob_id);
8987 if (path_unstaged_content &&
8988 unlink(path_unstaged_content) == -1 && err == NULL)
8989 err = got_error_from_errno2("unlink", path_unstaged_content);
8990 if (path_new_staged_content &&
8991 unlink(path_new_staged_content) == -1 && err == NULL)
8992 err = got_error_from_errno2("unlink", path_new_staged_content);
8993 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8994 err = got_error_from_errno2("unlink", blob_base_path);
8995 if (f_base && fclose(f_base) == EOF && err == NULL)
8996 err = got_error_from_errno2("fclose", path_unstaged_content);
8997 if (f && fclose(f) == EOF && err == NULL)
8998 err = got_error_from_errno2("fclose", path_unstaged_content);
8999 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9000 err = got_error_from_errno2("fclose", ondisk_path);
9001 free(path_unstaged_content);
9002 free(path_new_staged_content);
9003 free(blob_base_path);
9004 free(parent);
9005 free(base_path);
9006 return err;
9009 static const struct got_error *
9010 unstage_path(void *arg, unsigned char status,
9011 unsigned char staged_status, const char *relpath,
9012 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9013 struct got_object_id *commit_id, int dirfd, const char *de_name)
9015 const struct got_error *err = NULL;
9016 struct unstage_path_arg *a = arg;
9017 struct got_fileindex_entry *ie;
9018 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9019 char *ondisk_path = NULL;
9020 char *id_str = NULL, *label_orig = NULL;
9021 int local_changes_subsumed;
9022 struct stat sb;
9023 int fd1 = -1, fd2 = -1;
9025 if (staged_status != GOT_STATUS_ADD &&
9026 staged_status != GOT_STATUS_MODIFY &&
9027 staged_status != GOT_STATUS_DELETE)
9028 return NULL;
9030 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9031 if (ie == NULL)
9032 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9034 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9035 == -1)
9036 return got_error_from_errno("asprintf");
9038 err = got_object_id_str(&id_str,
9039 commit_id ? commit_id : a->worktree->base_commit_id);
9040 if (err)
9041 goto done;
9042 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9043 id_str) == -1) {
9044 err = got_error_from_errno("asprintf");
9045 goto done;
9048 fd1 = got_opentempfd();
9049 if (fd1 == -1) {
9050 err = got_error_from_errno("got_opentempfd");
9051 goto done;
9053 fd2 = got_opentempfd();
9054 if (fd2 == -1) {
9055 err = got_error_from_errno("got_opentempfd");
9056 goto done;
9059 switch (staged_status) {
9060 case GOT_STATUS_MODIFY:
9061 err = got_object_open_as_blob(&blob_base, a->repo,
9062 blob_id, 8192, fd1);
9063 if (err)
9064 break;
9065 /* fall through */
9066 case GOT_STATUS_ADD:
9067 if (a->patch_cb) {
9068 if (staged_status == GOT_STATUS_ADD) {
9069 int choice = GOT_PATCH_CHOICE_NONE;
9070 err = (*a->patch_cb)(&choice, a->patch_arg,
9071 staged_status, ie->path, NULL, 1, 1);
9072 if (err)
9073 break;
9074 if (choice != GOT_PATCH_CHOICE_YES)
9075 break;
9076 } else {
9077 err = unstage_hunks(staged_blob_id,
9078 blob_base, blob_id, ie, ondisk_path,
9079 label_orig, a->worktree, a->repo,
9080 a->patch_cb, a->patch_arg,
9081 a->progress_cb, a->progress_arg);
9082 break; /* Done with this file. */
9085 err = got_object_open_as_blob(&blob_staged, a->repo,
9086 staged_blob_id, 8192, fd2);
9087 if (err)
9088 break;
9089 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9090 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9091 case GOT_FILEIDX_MODE_REGULAR_FILE:
9092 err = merge_blob(&local_changes_subsumed, a->worktree,
9093 blob_base, ondisk_path, relpath,
9094 got_fileindex_perms_to_st(ie), label_orig,
9095 blob_staged, commit_id ? commit_id :
9096 a->worktree->base_commit_id, a->repo,
9097 a->progress_cb, a->progress_arg);
9098 break;
9099 case GOT_FILEIDX_MODE_SYMLINK:
9100 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9101 char *staged_target;
9102 err = got_object_blob_read_to_str(
9103 &staged_target, blob_staged);
9104 if (err)
9105 goto done;
9106 err = merge_symlink(a->worktree, blob_base,
9107 ondisk_path, relpath, label_orig,
9108 staged_target, commit_id ? commit_id :
9109 a->worktree->base_commit_id,
9110 a->repo, a->progress_cb, a->progress_arg);
9111 free(staged_target);
9112 } else {
9113 err = merge_blob(&local_changes_subsumed,
9114 a->worktree, blob_base, ondisk_path,
9115 relpath, got_fileindex_perms_to_st(ie),
9116 label_orig, blob_staged,
9117 commit_id ? commit_id :
9118 a->worktree->base_commit_id, a->repo,
9119 a->progress_cb, a->progress_arg);
9121 break;
9122 default:
9123 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9124 break;
9126 if (err == NULL) {
9127 got_fileindex_entry_stage_set(ie,
9128 GOT_FILEIDX_STAGE_NONE);
9129 got_fileindex_entry_staged_filetype_set(ie, 0);
9131 break;
9132 case GOT_STATUS_DELETE:
9133 if (a->patch_cb) {
9134 int choice = GOT_PATCH_CHOICE_NONE;
9135 err = (*a->patch_cb)(&choice, a->patch_arg,
9136 staged_status, ie->path, NULL, 1, 1);
9137 if (err)
9138 break;
9139 if (choice == GOT_PATCH_CHOICE_NO)
9140 break;
9141 if (choice != GOT_PATCH_CHOICE_YES) {
9142 err = got_error(GOT_ERR_PATCH_CHOICE);
9143 break;
9146 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9147 got_fileindex_entry_staged_filetype_set(ie, 0);
9148 err = get_file_status(&status, &sb, ie, ondisk_path,
9149 dirfd, de_name, a->repo);
9150 if (err)
9151 break;
9152 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9153 break;
9155 done:
9156 free(ondisk_path);
9157 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9158 err = got_error_from_errno("close");
9159 if (blob_base)
9160 got_object_blob_close(blob_base);
9161 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9162 err = got_error_from_errno("close");
9163 if (blob_staged)
9164 got_object_blob_close(blob_staged);
9165 free(id_str);
9166 free(label_orig);
9167 return err;
9170 const struct got_error *
9171 got_worktree_unstage(struct got_worktree *worktree,
9172 struct got_pathlist_head *paths,
9173 got_worktree_checkout_cb progress_cb, void *progress_arg,
9174 got_worktree_patch_cb patch_cb, void *patch_arg,
9175 struct got_repository *repo)
9177 const struct got_error *err = NULL, *sync_err, *unlockerr;
9178 struct got_pathlist_entry *pe;
9179 struct got_fileindex *fileindex = NULL;
9180 char *fileindex_path = NULL;
9181 struct unstage_path_arg upa;
9183 err = lock_worktree(worktree, LOCK_EX);
9184 if (err)
9185 return err;
9187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9188 if (err)
9189 goto done;
9191 upa.worktree = worktree;
9192 upa.fileindex = fileindex;
9193 upa.repo = repo;
9194 upa.progress_cb = progress_cb;
9195 upa.progress_arg = progress_arg;
9196 upa.patch_cb = patch_cb;
9197 upa.patch_arg = patch_arg;
9198 TAILQ_FOREACH(pe, paths, entry) {
9199 err = worktree_status(worktree, pe->path, fileindex, repo,
9200 unstage_path, &upa, NULL, NULL, 1, 0);
9201 if (err)
9202 goto done;
9205 sync_err = sync_fileindex(fileindex, fileindex_path);
9206 if (sync_err && err == NULL)
9207 err = sync_err;
9208 done:
9209 free(fileindex_path);
9210 if (fileindex)
9211 got_fileindex_free(fileindex);
9212 unlockerr = lock_worktree(worktree, LOCK_SH);
9213 if (unlockerr && err == NULL)
9214 err = unlockerr;
9215 return err;
9218 struct report_file_info_arg {
9219 struct got_worktree *worktree;
9220 got_worktree_path_info_cb info_cb;
9221 void *info_arg;
9222 struct got_pathlist_head *paths;
9223 got_cancel_cb cancel_cb;
9224 void *cancel_arg;
9227 static const struct got_error *
9228 report_file_info(void *arg, struct got_fileindex_entry *ie)
9230 struct report_file_info_arg *a = arg;
9231 struct got_pathlist_entry *pe;
9232 struct got_object_id blob_id, staged_blob_id, commit_id;
9233 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9234 struct got_object_id *commit_idp = NULL;
9235 int stage;
9237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9238 return got_error(GOT_ERR_CANCELLED);
9240 TAILQ_FOREACH(pe, a->paths, entry) {
9241 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9242 got_path_is_child(ie->path, pe->path, pe->path_len))
9243 break;
9245 if (pe == NULL) /* not found */
9246 return NULL;
9248 if (got_fileindex_entry_has_blob(ie))
9249 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9250 stage = got_fileindex_entry_stage_get(ie);
9251 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9252 stage == GOT_FILEIDX_STAGE_ADD) {
9253 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9254 &staged_blob_id, ie);
9257 if (got_fileindex_entry_has_commit(ie))
9258 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9260 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9261 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9264 const struct got_error *
9265 got_worktree_path_info(struct got_worktree *worktree,
9266 struct got_pathlist_head *paths,
9267 got_worktree_path_info_cb info_cb, void *info_arg,
9268 got_cancel_cb cancel_cb, void *cancel_arg)
9271 const struct got_error *err = NULL, *unlockerr;
9272 struct got_fileindex *fileindex = NULL;
9273 char *fileindex_path = NULL;
9274 struct report_file_info_arg arg;
9276 err = lock_worktree(worktree, LOCK_SH);
9277 if (err)
9278 return err;
9280 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9281 if (err)
9282 goto done;
9284 arg.worktree = worktree;
9285 arg.info_cb = info_cb;
9286 arg.info_arg = info_arg;
9287 arg.paths = paths;
9288 arg.cancel_cb = cancel_cb;
9289 arg.cancel_arg = cancel_arg;
9290 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9291 &arg);
9292 done:
9293 free(fileindex_path);
9294 if (fileindex)
9295 got_fileindex_free(fileindex);
9296 unlockerr = lock_worktree(worktree, LOCK_UN);
9297 if (unlockerr && err == NULL)
9298 err = unlockerr;
9299 return err;
9302 static const struct got_error *
9303 patch_check_path(const char *p, char **path, unsigned char *status,
9304 unsigned char *staged_status, struct got_fileindex *fileindex,
9305 struct got_worktree *worktree, struct got_repository *repo)
9307 const struct got_error *err;
9308 struct got_fileindex_entry *ie;
9309 struct stat sb;
9310 char *ondisk_path = NULL;
9312 err = got_worktree_resolve_path(path, worktree, p);
9313 if (err)
9314 return err;
9316 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9317 *path[0] ? "/" : "", *path) == -1)
9318 return got_error_from_errno("asprintf");
9320 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9321 if (ie) {
9322 *staged_status = get_staged_status(ie);
9323 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9324 repo);
9325 if (err)
9326 goto done;
9327 } else {
9328 *staged_status = GOT_STATUS_NO_CHANGE;
9329 *status = GOT_STATUS_UNVERSIONED;
9330 if (lstat(ondisk_path, &sb) == -1) {
9331 if (errno != ENOENT) {
9332 err = got_error_from_errno2("lstat",
9333 ondisk_path);
9334 goto done;
9336 *status = GOT_STATUS_NONEXISTENT;
9340 done:
9341 free(ondisk_path);
9342 return err;
9345 static const struct got_error *
9346 patch_can_rm(const char *path, unsigned char status,
9347 unsigned char staged_status)
9349 if (status == GOT_STATUS_NONEXISTENT)
9350 return got_error_set_errno(ENOENT, path);
9351 if (status != GOT_STATUS_NO_CHANGE &&
9352 status != GOT_STATUS_ADD &&
9353 status != GOT_STATUS_MODIFY &&
9354 status != GOT_STATUS_MODE_CHANGE)
9355 return got_error_path(path, GOT_ERR_FILE_STATUS);
9356 if (staged_status == GOT_STATUS_DELETE)
9357 return got_error_path(path, GOT_ERR_FILE_STATUS);
9358 return NULL;
9361 static const struct got_error *
9362 patch_can_add(const char *path, unsigned char status)
9364 if (status != GOT_STATUS_NONEXISTENT)
9365 return got_error_path(path, GOT_ERR_FILE_STATUS);
9366 return NULL;
9369 static const struct got_error *
9370 patch_can_edit(const char *path, unsigned char status,
9371 unsigned char staged_status)
9373 if (status == GOT_STATUS_NONEXISTENT)
9374 return got_error_set_errno(ENOENT, path);
9375 if (status != GOT_STATUS_NO_CHANGE &&
9376 status != GOT_STATUS_ADD &&
9377 status != GOT_STATUS_MODIFY)
9378 return got_error_path(path, GOT_ERR_FILE_STATUS);
9379 if (staged_status == GOT_STATUS_DELETE)
9380 return got_error_path(path, GOT_ERR_FILE_STATUS);
9381 return NULL;
9384 const struct got_error *
9385 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9386 char **fileindex_path, struct got_worktree *worktree)
9388 return open_fileindex(fileindex, fileindex_path, worktree);
9391 const struct got_error *
9392 got_worktree_patch_check_path(const char *old, const char *new,
9393 char **oldpath, char **newpath, struct got_worktree *worktree,
9394 struct got_repository *repo, struct got_fileindex *fileindex)
9396 const struct got_error *err = NULL;
9397 int file_renamed = 0;
9398 unsigned char status_old, staged_status_old;
9399 unsigned char status_new, staged_status_new;
9401 *oldpath = NULL;
9402 *newpath = NULL;
9404 err = patch_check_path(old != NULL ? old : new, oldpath,
9405 &status_old, &staged_status_old, fileindex, worktree, repo);
9406 if (err)
9407 goto done;
9409 err = patch_check_path(new != NULL ? new : old, newpath,
9410 &status_new, &staged_status_new, fileindex, worktree, repo);
9411 if (err)
9412 goto done;
9414 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9415 file_renamed = 1;
9417 if (old != NULL && new == NULL)
9418 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9419 else if (file_renamed) {
9420 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9421 if (err == NULL)
9422 err = patch_can_add(*newpath, status_new);
9423 } else if (old == NULL)
9424 err = patch_can_add(*newpath, status_new);
9425 else
9426 err = patch_can_edit(*newpath, status_new, staged_status_new);
9428 done:
9429 if (err) {
9430 free(*oldpath);
9431 *oldpath = NULL;
9432 free(*newpath);
9433 *newpath = NULL;
9435 return err;
9438 const struct got_error *
9439 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9440 struct got_worktree *worktree, struct got_fileindex *fileindex,
9441 got_worktree_checkout_cb progress_cb, void *progress_arg)
9443 struct schedule_addition_args saa;
9445 memset(&saa, 0, sizeof(saa));
9446 saa.worktree = worktree;
9447 saa.fileindex = fileindex;
9448 saa.progress_cb = progress_cb;
9449 saa.progress_arg = progress_arg;
9450 saa.repo = repo;
9452 return worktree_status(worktree, path, fileindex, repo,
9453 schedule_addition, &saa, NULL, NULL, 1, 0);
9456 const struct got_error *
9457 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9458 struct got_worktree *worktree, struct got_fileindex *fileindex,
9459 got_worktree_delete_cb progress_cb, void *progress_arg)
9461 struct schedule_deletion_args sda;
9463 memset(&sda, 0, sizeof(sda));
9464 sda.worktree = worktree;
9465 sda.fileindex = fileindex;
9466 sda.progress_cb = progress_cb;
9467 sda.progress_arg = progress_arg;
9468 sda.repo = repo;
9469 sda.delete_local_mods = 0;
9470 sda.keep_on_disk = 0;
9471 sda.ignore_missing_paths = 0;
9472 sda.status_codes = NULL;
9474 return worktree_status(worktree, path, fileindex, repo,
9475 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9478 const struct got_error *
9479 got_worktree_patch_complete(struct got_fileindex *fileindex,
9480 const char *fileindex_path)
9482 const struct got_error *err = NULL;
9484 err = sync_fileindex(fileindex, fileindex_path);
9485 got_fileindex_free(fileindex);
9487 return err;