Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
32 #include <uuid.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
67 {
68 const struct got_error *err = NULL;
69 char *path;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
75 free(path);
76 return err;
77 }
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
81 {
82 const struct got_error *err = NULL;
83 FILE *tmpfile = NULL;
84 char *tmppath = NULL;
85 char *path = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
89 path = NULL;
90 goto done;
91 }
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 if (err)
95 goto done;
97 if (content) {
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
101 goto done;
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
107 unlink(tmppath);
108 goto done;
111 done:
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
114 free(tmppath);
115 return err;
118 static const struct got_error *
119 read_meta_file(char **content, const char *path_got, const char *name)
121 const struct got_error *err = NULL;
122 char *path;
123 int fd = -1;
124 ssize_t n;
125 struct stat sb;
127 *content = NULL;
129 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
130 err = got_error_from_errno("asprintf");
131 path = NULL;
132 goto done;
135 fd = open(path, O_RDONLY | O_NOFOLLOW);
136 if (fd == -1) {
137 if (errno == ENOENT)
138 err = got_error_path(path, GOT_ERR_WORKTREE_META);
139 else
140 err = got_error_from_errno2("open", path);
141 goto done;
143 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
144 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 : got_error_from_errno2("flock", path));
146 goto done;
149 if (fstat(fd, &sb) != 0) {
150 err = got_error_from_errno2("fstat", path);
151 goto done;
153 *content = calloc(1, sb.st_size);
154 if (*content == NULL) {
155 err = got_error_from_errno("calloc");
156 goto done;
159 n = read(fd, *content, sb.st_size);
160 if (n != sb.st_size) {
161 err = (n == -1 ? got_error_from_errno2("read", path) :
162 got_error_path(path, GOT_ERR_WORKTREE_META));
163 goto done;
165 if ((*content)[sb.st_size - 1] != '\n') {
166 err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 goto done;
169 (*content)[sb.st_size - 1] = '\0';
171 done:
172 if (fd != -1 && close(fd) == -1 && err == NULL)
173 err = got_error_from_errno2("close", path_got);
174 free(path);
175 if (err) {
176 free(*content);
177 *content = NULL;
179 return err;
182 static const struct got_error *
183 write_head_ref(const char *path_got, struct got_reference *head_ref)
185 const struct got_error *err = NULL;
186 char *refstr = NULL;
188 if (got_ref_is_symbolic(head_ref)) {
189 refstr = got_ref_to_str(head_ref);
190 if (refstr == NULL)
191 return got_error_from_errno("got_ref_to_str");
192 } else {
193 refstr = strdup(got_ref_get_name(head_ref));
194 if (refstr == NULL)
195 return got_error_from_errno("strdup");
197 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 free(refstr);
199 return err;
202 const struct got_error *
203 got_worktree_init(const char *path, struct got_reference *head_ref,
204 const char *prefix, struct got_repository *repo)
206 const struct got_error *err = NULL;
207 struct got_object_id *commit_id = NULL;
208 uuid_t uuid;
209 uint32_t uuid_status;
210 int obj_type;
211 char *path_got = NULL;
212 char *formatstr = NULL;
213 char *absprefix = NULL;
214 char *basestr = NULL;
215 char *uuidstr = NULL;
217 if (strcmp(path, got_repo_get_path(repo)) == 0) {
218 err = got_error(GOT_ERR_WORKTREE_REPO);
219 goto done;
222 err = got_ref_resolve(&commit_id, repo, head_ref);
223 if (err)
224 return err;
225 err = got_object_get_type(&obj_type, repo, commit_id);
226 if (err)
227 return err;
228 if (obj_type != GOT_OBJ_TYPE_COMMIT)
229 return got_error(GOT_ERR_OBJ_TYPE);
231 if (!got_path_is_absolute(prefix)) {
232 if (asprintf(&absprefix, "/%s", prefix) == -1)
233 return got_error_from_errno("asprintf");
236 /* Create top-level directory (may already exist). */
237 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
238 err = got_error_from_errno2("mkdir", path);
239 goto done;
242 /* Create .got directory (may already exist). */
243 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
248 err = got_error_from_errno2("mkdir", path_got);
249 goto done;
252 /* Create an empty lock file. */
253 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 if (err)
255 goto done;
257 /* Create an empty file index. */
258 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 if (err)
260 goto done;
262 /* Write the HEAD reference. */
263 err = write_head_ref(path_got, head_ref);
264 if (err)
265 goto done;
267 /* Record our base commit. */
268 err = got_object_id_str(&basestr, commit_id);
269 if (err)
270 goto done;
271 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 if (err)
273 goto done;
275 /* Store path to repository. */
276 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 got_repo_get_path(repo));
278 if (err)
279 goto done;
281 /* Store in-repository path prefix. */
282 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 absprefix ? absprefix : prefix);
284 if (err)
285 goto done;
287 /* Generate UUID. */
288 uuid_create(&uuid, &uuid_status);
289 if (uuid_status != uuid_s_ok) {
290 err = got_error_uuid(uuid_status, "uuid_create");
291 goto done;
293 uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 if (uuid_status != uuid_s_ok) {
295 err = got_error_uuid(uuid_status, "uuid_to_string");
296 goto done;
298 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 if (err)
300 goto done;
302 /* Stamp work tree with format file. */
303 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 err = got_error_from_errno("asprintf");
305 goto done;
307 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 if (err)
309 goto done;
311 done:
312 free(commit_id);
313 free(path_got);
314 free(formatstr);
315 free(absprefix);
316 free(basestr);
317 free(uuidstr);
318 return err;
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
325 char *path_got;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 int version, fd = -1;
331 const char *errstr;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
335 *worktree = NULL;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno("asprintf");
339 path_got = NULL;
340 goto done;
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno("asprintf");
345 path_lock = NULL;
346 goto done;
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 if (fd == -1) {
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno2("open", path_lock));
353 goto done;
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 if (err)
358 goto done;
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 if (errstr) {
362 err = got_error_msg(GOT_ERR_WORKTREE_META,
363 "could not parse work tree format version number");
364 goto done;
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
368 goto done;
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno("calloc");
374 goto done;
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = realpath(path, NULL);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno2("realpath", path);
381 goto done;
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
385 if (err)
386 goto done;
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
390 if (err)
391 goto done;
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
395 if (err)
396 goto done;
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 if (err)
400 goto done;
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status, "uuid_from_string");
404 goto done;
407 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
408 if (err)
409 goto done;
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 base_commit_id_str);
413 if (err)
414 goto done;
416 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
417 GOT_WORKTREE_HEAD_REF);
418 if (err)
419 goto done;
421 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
422 (*worktree)->root_path,
423 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
424 err = got_error_from_errno("asprintf");
425 goto done;
428 err = got_gotconfig_read(&(*worktree)->gotconfig,
429 (*worktree)->gotconfig_path);
431 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
432 if ((*worktree)->root_fd == -1) {
433 err = got_error_from_errno2("open", (*worktree)->root_path);
434 goto done;
436 done:
437 if (repo) {
438 const struct got_error *close_err = got_repo_close(repo);
439 if (err == NULL)
440 err = close_err;
442 free(path_got);
443 free(path_lock);
444 free(base_commit_id_str);
445 free(uuidstr);
446 free(formatstr);
447 if (err) {
448 if (fd != -1)
449 close(fd);
450 if (*worktree != NULL)
451 got_worktree_close(*worktree);
452 *worktree = NULL;
453 } else
454 (*worktree)->lockfd = fd;
456 return err;
459 const struct got_error *
460 got_worktree_open(struct got_worktree **worktree, const char *path)
462 const struct got_error *err = NULL;
463 char *worktree_path;
465 worktree_path = strdup(path);
466 if (worktree_path == NULL)
467 return got_error_from_errno("strdup");
469 for (;;) {
470 char *parent_path;
472 err = open_worktree(worktree, worktree_path);
473 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 free(worktree_path);
475 return err;
477 if (*worktree) {
478 free(worktree_path);
479 return NULL;
481 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 break;
483 err = got_path_dirname(&parent_path, worktree_path);
484 if (err) {
485 if (err->code != GOT_ERR_BAD_PATH) {
486 free(worktree_path);
487 return err;
489 break;
491 free(worktree_path);
492 worktree_path = parent_path;
495 free(worktree_path);
496 return got_error(GOT_ERR_NOT_WORKTREE);
499 const struct got_error *
500 got_worktree_close(struct got_worktree *worktree)
502 const struct got_error *err = NULL;
504 if (worktree->lockfd != -1) {
505 if (close(worktree->lockfd) == -1)
506 err = got_error_from_errno2("close",
507 got_worktree_get_root_path(worktree));
509 if (close(worktree->root_fd) == -1 && err == NULL)
510 err = got_error_from_errno2("close",
511 got_worktree_get_root_path(worktree));
512 free(worktree->repo_path);
513 free(worktree->path_prefix);
514 free(worktree->base_commit_id);
515 free(worktree->head_ref_name);
516 free(worktree->root_path);
517 free(worktree->gotconfig_path);
518 got_gotconfig_free(worktree->gotconfig);
519 free(worktree);
520 return err;
523 const char *
524 got_worktree_get_root_path(struct got_worktree *worktree)
526 return worktree->root_path;
529 const char *
530 got_worktree_get_repo_path(struct got_worktree *worktree)
532 return worktree->repo_path;
534 const char *
535 got_worktree_get_path_prefix(struct got_worktree *worktree)
537 return worktree->path_prefix;
540 const struct got_error *
541 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
542 const char *path_prefix)
544 char *absprefix = NULL;
546 if (!got_path_is_absolute(path_prefix)) {
547 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
548 return got_error_from_errno("asprintf");
550 *match = (strcmp(absprefix ? absprefix : path_prefix,
551 worktree->path_prefix) == 0);
552 free(absprefix);
553 return NULL;
556 const char *
557 got_worktree_get_head_ref_name(struct got_worktree *worktree)
559 return worktree->head_ref_name;
562 const struct got_error *
563 got_worktree_set_head_ref(struct got_worktree *worktree,
564 struct got_reference *head_ref)
566 const struct got_error *err = NULL;
567 char *path_got = NULL, *head_ref_name = NULL;
569 if (asprintf(&path_got, "%s/%s", worktree->root_path,
570 GOT_WORKTREE_GOT_DIR) == -1) {
571 err = got_error_from_errno("asprintf");
572 path_got = NULL;
573 goto done;
576 head_ref_name = strdup(got_ref_get_name(head_ref));
577 if (head_ref_name == NULL) {
578 err = got_error_from_errno("strdup");
579 goto done;
582 err = write_head_ref(path_got, head_ref);
583 if (err)
584 goto done;
586 free(worktree->head_ref_name);
587 worktree->head_ref_name = head_ref_name;
588 done:
589 free(path_got);
590 if (err)
591 free(head_ref_name);
592 return err;
595 struct got_object_id *
596 got_worktree_get_base_commit_id(struct got_worktree *worktree)
598 return worktree->base_commit_id;
601 const struct got_error *
602 got_worktree_set_base_commit_id(struct got_worktree *worktree,
603 struct got_repository *repo, struct got_object_id *commit_id)
605 const struct got_error *err;
606 struct got_object *obj = NULL;
607 char *id_str = NULL;
608 char *path_got = NULL;
610 if (asprintf(&path_got, "%s/%s", worktree->root_path,
611 GOT_WORKTREE_GOT_DIR) == -1) {
612 err = got_error_from_errno("asprintf");
613 path_got = NULL;
614 goto done;
617 err = got_object_open(&obj, repo, commit_id);
618 if (err)
619 return err;
621 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
622 err = got_error(GOT_ERR_OBJ_TYPE);
623 goto done;
626 /* Record our base commit. */
627 err = got_object_id_str(&id_str, commit_id);
628 if (err)
629 goto done;
630 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
631 if (err)
632 goto done;
634 free(worktree->base_commit_id);
635 worktree->base_commit_id = got_object_id_dup(commit_id);
636 if (worktree->base_commit_id == NULL) {
637 err = got_error_from_errno("got_object_id_dup");
638 goto done;
640 done:
641 if (obj)
642 got_object_close(obj);
643 free(id_str);
644 free(path_got);
645 return err;
648 const struct got_gotconfig *
649 got_worktree_get_gotconfig(struct got_worktree *worktree)
651 return worktree->gotconfig;
654 static const struct got_error *
655 lock_worktree(struct got_worktree *worktree, int operation)
657 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
658 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
659 : got_error_from_errno2("flock",
660 got_worktree_get_root_path(worktree)));
661 return NULL;
664 static const struct got_error *
665 add_dir_on_disk(struct got_worktree *worktree, const char *path)
667 const struct got_error *err = NULL;
668 char *abspath;
670 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
671 return got_error_from_errno("asprintf");
673 err = got_path_mkdir(abspath);
674 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
675 struct stat sb;
676 err = NULL;
677 if (lstat(abspath, &sb) == -1) {
678 err = got_error_from_errno2("lstat", abspath);
679 } else if (!S_ISDIR(sb.st_mode)) {
680 /* TODO directory is obstructed; do something */
681 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 free(abspath);
685 return err;
688 static const struct got_error *
689 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
691 const struct got_error *err = NULL;
692 uint8_t fbuf1[8192];
693 uint8_t fbuf2[8192];
694 size_t flen1 = 0, flen2 = 0;
696 *same = 1;
698 for (;;) {
699 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
700 if (flen1 == 0 && ferror(f1)) {
701 err = got_error_from_errno("fread");
702 break;
704 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
705 if (flen2 == 0 && ferror(f2)) {
706 err = got_error_from_errno("fread");
707 break;
709 if (flen1 == 0) {
710 if (flen2 != 0)
711 *same = 0;
712 break;
713 } else if (flen2 == 0) {
714 if (flen1 != 0)
715 *same = 0;
716 break;
717 } else if (flen1 == flen2) {
718 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 *same = 0;
720 break;
722 } else {
723 *same = 0;
724 break;
728 return err;
731 static const struct got_error *
732 check_files_equal(int *same, FILE *f1, FILE *f2)
734 struct stat sb;
735 size_t size1, size2;
737 *same = 1;
739 if (fstat(fileno(f1), &sb) != 0)
740 return got_error_from_errno("fstat");
741 size1 = sb.st_size;
743 if (fstat(fileno(f2), &sb) != 0)
744 return got_error_from_errno("fstat");
745 size2 = sb.st_size;
747 if (size1 != size2) {
748 *same = 0;
749 return NULL;
752 if (fseek(f1, 0L, SEEK_SET) == -1)
753 return got_ferror(f1, GOT_ERR_IO);
754 if (fseek(f2, 0L, SEEK_SET) == -1)
755 return got_ferror(f2, GOT_ERR_IO);
757 return check_file_contents_equal(same, f1, f2);
760 /*
761 * Perform a 3-way merge where the file f_orig acts as the common
762 * ancestor, the file f_deriv acts as the first derived version,
763 * and the file f_deriv2 acts as the second derived version.
764 * The merge result will be written to a new file at ondisk_path; any
765 * existing file at this path will be replaced.
766 */
767 static const struct got_error *
768 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
769 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
770 const char *path, uint16_t st_mode,
771 const char *label_orig, const char *label_deriv, const char *label_deriv2,
772 enum got_diff_algorithm diff_algo, struct got_repository *repo,
773 got_worktree_checkout_cb progress_cb, void *progress_arg)
775 const struct got_error *err = NULL;
776 int merged_fd = -1;
777 FILE *f_merged = NULL;
778 char *merged_path = NULL, *base_path = NULL;
779 int overlapcnt = 0;
780 char *parent = NULL;
782 *local_changes_subsumed = 0;
784 err = got_path_dirname(&parent, ondisk_path);
785 if (err)
786 return err;
788 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
794 if (err)
795 goto done;
797 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
798 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
799 if (err)
800 goto done;
802 err = (*progress_cb)(progress_arg,
803 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
804 if (err)
805 goto done;
807 if (fsync(merged_fd) != 0) {
808 err = got_error_from_errno("fsync");
809 goto done;
812 f_merged = fdopen(merged_fd, "r");
813 if (f_merged == NULL) {
814 err = got_error_from_errno("fdopen");
815 goto done;
817 merged_fd = -1;
819 /* Check if a clean merge has subsumed all local changes. */
820 if (overlapcnt == 0) {
821 err = check_files_equal(local_changes_subsumed, f_deriv,
822 f_merged);
823 if (err)
824 goto done;
827 if (fchmod(fileno(f_merged), st_mode) != 0) {
828 err = got_error_from_errno2("fchmod", merged_path);
829 goto done;
832 if (rename(merged_path, ondisk_path) != 0) {
833 err = got_error_from_errno3("rename", merged_path,
834 ondisk_path);
835 goto done;
837 done:
838 if (err) {
839 if (merged_path)
840 unlink(merged_path);
842 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
843 err = got_error_from_errno("close");
844 if (f_merged && fclose(f_merged) == EOF && err == NULL)
845 err = got_error_from_errno("fclose");
846 free(merged_path);
847 free(base_path);
848 free(parent);
849 return err;
852 static const struct got_error *
853 update_symlink(const char *ondisk_path, const char *target_path,
854 size_t target_len)
856 /* This is not atomic but matches what 'ln -sf' does. */
857 if (unlink(ondisk_path) == -1)
858 return got_error_from_errno2("unlink", ondisk_path);
859 if (symlink(target_path, ondisk_path) == -1)
860 return got_error_from_errno3("symlink", target_path,
861 ondisk_path);
862 return NULL;
865 /*
866 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
867 * in the work tree with a file that contains conflict markers and the
868 * conflicting target paths of the original version, a "derived version"
869 * of a symlink from an incoming change, and a local version of the symlink.
871 * The original versions's target path can be NULL if it is not available,
872 * such as if both derived versions added a new symlink at the same path.
874 * The incoming derived symlink target is NULL in case the incoming change
875 * has deleted this symlink.
876 */
877 static const struct got_error *
878 install_symlink_conflict(const char *deriv_target,
879 struct got_object_id *deriv_base_commit_id, const char *orig_target,
880 const char *label_orig, const char *local_target, const char *ondisk_path)
882 const struct got_error *err;
883 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
884 FILE *f = NULL;
886 err = got_object_id_str(&id_str, deriv_base_commit_id);
887 if (err)
888 return got_error_from_errno("asprintf");
890 if (asprintf(&label_deriv, "%s: commit %s",
891 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
892 err = got_error_from_errno("asprintf");
893 goto done;
896 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
897 if (err)
898 goto done;
900 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("fchmod", path);
902 goto done;
905 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
906 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
907 deriv_target ? deriv_target : "(symlink was deleted)",
908 orig_target ? label_orig : "",
909 orig_target ? "\n" : "",
910 orig_target ? orig_target : "",
911 orig_target ? "\n" : "",
912 GOT_DIFF_CONFLICT_MARKER_SEP,
913 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
914 err = got_error_from_errno2("fprintf", path);
915 goto done;
918 if (unlink(ondisk_path) == -1) {
919 err = got_error_from_errno2("unlink", ondisk_path);
920 goto done;
922 if (rename(path, ondisk_path) == -1) {
923 err = got_error_from_errno3("rename", path, ondisk_path);
924 goto done;
926 done:
927 if (f != NULL && fclose(f) == EOF && err == NULL)
928 err = got_error_from_errno2("fclose", path);
929 free(path);
930 free(id_str);
931 free(label_deriv);
932 return err;
935 /* forward declaration */
936 static const struct got_error *
937 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
938 const char *, const char *, uint16_t, const char *,
939 struct got_blob_object *, struct got_object_id *,
940 struct got_repository *, got_worktree_checkout_cb, void *);
942 /*
943 * Merge a symlink into the work tree, where blob_orig acts as the common
944 * ancestor, deriv_target is the link target of the first derived version,
945 * and the symlink on disk acts as the second derived version.
946 * Assume that contents of both blobs represent symlinks.
947 */
948 static const struct got_error *
949 merge_symlink(struct got_worktree *worktree,
950 struct got_blob_object *blob_orig, const char *ondisk_path,
951 const char *path, const char *label_orig, const char *deriv_target,
952 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
953 got_worktree_checkout_cb progress_cb, void *progress_arg)
955 const struct got_error *err = NULL;
956 char *ancestor_target = NULL;
957 struct stat sb;
958 ssize_t ondisk_len, deriv_len;
959 char ondisk_target[PATH_MAX];
960 int have_local_change = 0;
961 int have_incoming_change = 0;
963 if (lstat(ondisk_path, &sb) == -1)
964 return got_error_from_errno2("lstat", ondisk_path);
966 ondisk_len = readlink(ondisk_path, ondisk_target,
967 sizeof(ondisk_target));
968 if (ondisk_len == -1) {
969 err = got_error_from_errno2("readlink",
970 ondisk_path);
971 goto done;
973 ondisk_target[ondisk_len] = '\0';
975 if (blob_orig) {
976 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
977 if (err)
978 goto done;
981 if (ancestor_target == NULL ||
982 (ondisk_len != strlen(ancestor_target) ||
983 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
984 have_local_change = 1;
986 deriv_len = strlen(deriv_target);
987 if (ancestor_target == NULL ||
988 (deriv_len != strlen(ancestor_target) ||
989 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
990 have_incoming_change = 1;
992 if (!have_local_change && !have_incoming_change) {
993 if (ancestor_target) {
994 /* Both sides made the same change. */
995 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
996 path);
997 } else if (deriv_len == ondisk_len &&
998 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
999 /* Both sides added the same symlink. */
1000 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1001 path);
1002 } else {
1003 /* Both sides added symlinks which don't match. */
1004 err = install_symlink_conflict(deriv_target,
1005 deriv_base_commit_id, ancestor_target,
1006 label_orig, ondisk_target, ondisk_path);
1007 if (err)
1008 goto done;
1009 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1010 path);
1012 } else if (!have_local_change && have_incoming_change) {
1013 /* Apply the incoming change. */
1014 err = update_symlink(ondisk_path, deriv_target,
1015 strlen(deriv_target));
1016 if (err)
1017 goto done;
1018 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1019 } else if (have_local_change && have_incoming_change) {
1020 if (deriv_len == ondisk_len &&
1021 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1022 /* Both sides made the same change. */
1023 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1024 path);
1025 } else {
1026 err = install_symlink_conflict(deriv_target,
1027 deriv_base_commit_id, ancestor_target, label_orig,
1028 ondisk_target, ondisk_path);
1029 if (err)
1030 goto done;
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1032 path);
1036 done:
1037 free(ancestor_target);
1038 return err;
1041 static const struct got_error *
1042 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1044 const struct got_error *err = NULL;
1045 char target_path[PATH_MAX];
1046 ssize_t target_len;
1047 size_t n;
1048 FILE *f;
1050 *outfile = NULL;
1052 f = got_opentemp();
1053 if (f == NULL)
1054 return got_error_from_errno("got_opentemp");
1055 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1056 if (target_len == -1) {
1057 err = got_error_from_errno2("readlink", ondisk_path);
1058 goto done;
1060 n = fwrite(target_path, 1, target_len, f);
1061 if (n != target_len) {
1062 err = got_ferror(f, GOT_ERR_IO);
1063 goto done;
1065 if (fflush(f) == EOF) {
1066 err = got_error_from_errno("fflush");
1067 goto done;
1069 if (fseek(f, 0L, SEEK_SET) == -1) {
1070 err = got_ferror(f, GOT_ERR_IO);
1071 goto done;
1073 done:
1074 if (err)
1075 fclose(f);
1076 else
1077 *outfile = f;
1078 return err;
1082 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1083 * blob_deriv acts as the first derived version, and the file on disk
1084 * acts as the second derived version.
1086 static const struct got_error *
1087 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1088 struct got_blob_object *blob_orig, const char *ondisk_path,
1089 const char *path, uint16_t st_mode, const char *label_orig,
1090 struct got_blob_object *blob_deriv,
1091 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1092 got_worktree_checkout_cb progress_cb, void *progress_arg)
1094 const struct got_error *err = NULL;
1095 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1096 char *blob_orig_path = NULL;
1097 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1098 char *label_deriv = NULL, *parent = NULL;
1100 *local_changes_subsumed = 0;
1102 err = got_path_dirname(&parent, ondisk_path);
1103 if (err)
1104 return err;
1106 if (blob_orig) {
1107 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1108 parent) == -1) {
1109 err = got_error_from_errno("asprintf");
1110 base_path = NULL;
1111 goto done;
1114 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1115 if (err)
1116 goto done;
1117 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1118 blob_orig);
1119 if (err)
1120 goto done;
1121 free(base_path);
1122 } else {
1124 * No common ancestor exists. This is an "add vs add" conflict
1125 * and we simply use an empty ancestor file to make both files
1126 * appear in the merged result in their entirety.
1128 f_orig = got_opentemp();
1129 if (f_orig == NULL) {
1130 err = got_error_from_errno("got_opentemp");
1131 goto done;
1135 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1136 err = got_error_from_errno("asprintf");
1137 base_path = NULL;
1138 goto done;
1141 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 if (err)
1143 goto done;
1144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1145 blob_deriv);
1146 if (err)
1147 goto done;
1149 err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 if (err)
1151 goto done;
1152 if (asprintf(&label_deriv, "%s: commit %s",
1153 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1154 err = got_error_from_errno("asprintf");
1155 goto done;
1159 * In order the run a 3-way merge with a symlink we copy the symlink's
1160 * target path into a temporary file and use that file with diff3.
1162 if (S_ISLNK(st_mode)) {
1163 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1164 if (err)
1165 goto done;
1166 } else {
1167 int fd;
1168 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1169 if (fd == -1) {
1170 err = got_error_from_errno2("open", ondisk_path);
1171 goto done;
1173 f_deriv2 = fdopen(fd, "r");
1174 if (f_deriv2 == NULL) {
1175 err = got_error_from_errno2("fdopen", ondisk_path);
1176 close(fd);
1177 goto done;
1181 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1182 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1183 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1184 done:
1185 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1186 err = got_error_from_errno("fclose");
1187 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 free(base_path);
1192 if (blob_orig_path) {
1193 unlink(blob_orig_path);
1194 free(blob_orig_path);
1196 if (blob_deriv_path) {
1197 unlink(blob_deriv_path);
1198 free(blob_deriv_path);
1200 free(id_str);
1201 free(label_deriv);
1202 free(parent);
1203 return err;
1206 static const struct got_error *
1207 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1208 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1209 int wt_fd, const char *path, struct got_object_id *blob_id)
1211 const struct got_error *err = NULL;
1212 struct got_fileindex_entry *new_ie;
1214 *new_iep = NULL;
1216 err = got_fileindex_entry_alloc(&new_ie, path);
1217 if (err)
1218 return err;
1220 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1221 blob_id->sha1, base_commit_id->sha1, 1);
1222 if (err)
1223 goto done;
1225 err = got_fileindex_entry_add(fileindex, new_ie);
1226 done:
1227 if (err)
1228 got_fileindex_entry_free(new_ie);
1229 else
1230 *new_iep = new_ie;
1231 return err;
1234 static mode_t
1235 get_ondisk_perms(int executable, mode_t st_mode)
1237 mode_t xbits = S_IXUSR;
1239 if (executable) {
1240 /* Map read bits to execute bits. */
1241 if (st_mode & S_IRGRP)
1242 xbits |= S_IXGRP;
1243 if (st_mode & S_IROTH)
1244 xbits |= S_IXOTH;
1245 return st_mode | xbits;
1248 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 /* forward declaration */
1252 static const struct got_error *
1253 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1254 const char *path, mode_t te_mode, mode_t st_mode,
1255 struct got_blob_object *blob, int restoring_missing_file,
1256 int reverting_versioned_file, int installing_bad_symlink,
1257 int path_is_unversioned, struct got_repository *repo,
1258 got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 * This function assumes that the provided symlink target points at a
1262 * safe location in the work tree!
1264 static const struct got_error *
1265 replace_existing_symlink(int *did_something, const char *ondisk_path,
1266 const char *target_path, size_t target_len)
1268 const struct got_error *err = NULL;
1269 ssize_t elen;
1270 char etarget[PATH_MAX];
1271 int fd;
1273 *did_something = 0;
1276 * "Bad" symlinks (those pointing outside the work tree or into the
1277 * .got directory) are installed in the work tree as a regular file
1278 * which contains the bad symlink target path.
1279 * The new symlink target has already been checked for safety by our
1280 * caller. If we can successfully open a regular file then we simply
1281 * replace this file with a symlink below.
1283 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1284 if (fd == -1) {
1285 if (errno != ELOOP)
1286 return got_error_from_errno2("open", ondisk_path);
1288 /* We are updating an existing on-disk symlink. */
1289 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1290 if (elen == -1)
1291 return got_error_from_errno2("readlink", ondisk_path);
1293 if (elen == target_len &&
1294 memcmp(etarget, target_path, target_len) == 0)
1295 return NULL; /* nothing to do */
1298 *did_something = 1;
1299 err = update_symlink(ondisk_path, target_path, target_len);
1300 if (fd != -1 && close(fd) == -1 && err == NULL)
1301 err = got_error_from_errno2("close", ondisk_path);
1302 return err;
1305 static const struct got_error *
1306 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1307 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1309 const struct got_error *err = NULL;
1310 char canonpath[PATH_MAX];
1311 char *path_got = NULL;
1313 *is_bad_symlink = 0;
1315 if (target_len >= sizeof(canonpath)) {
1316 *is_bad_symlink = 1;
1317 return NULL;
1321 * We do not use realpath(3) to resolve the symlink's target
1322 * path because we don't want to resolve symlinks recursively.
1323 * Instead we make the path absolute and then canonicalize it.
1324 * Relative symlink target lookup should begin at the directory
1325 * in which the blob object is being installed.
1327 if (!got_path_is_absolute(target_path)) {
1328 char *abspath, *parent;
1329 err = got_path_dirname(&parent, ondisk_path);
1330 if (err)
1331 return err;
1332 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1333 free(parent);
1334 return got_error_from_errno("asprintf");
1336 free(parent);
1337 if (strlen(abspath) >= sizeof(canonpath)) {
1338 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1339 free(abspath);
1340 return err;
1342 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1343 free(abspath);
1344 if (err)
1345 return err;
1346 } else {
1347 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1348 if (err)
1349 return err;
1352 /* Only allow symlinks pointing at paths within the work tree. */
1353 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1354 *is_bad_symlink = 1;
1355 return NULL;
1358 /* Do not allow symlinks pointing into the .got directory. */
1359 if (asprintf(&path_got, "%s/%s", wtroot_path,
1360 GOT_WORKTREE_GOT_DIR) == -1)
1361 return got_error_from_errno("asprintf");
1362 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1363 *is_bad_symlink = 1;
1365 free(path_got);
1366 return NULL;
1369 static const struct got_error *
1370 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1371 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1372 int restoring_missing_file, int reverting_versioned_file,
1373 int path_is_unversioned, int allow_bad_symlinks,
1374 struct got_repository *repo,
1375 got_worktree_checkout_cb progress_cb, void *progress_arg)
1377 const struct got_error *err = NULL;
1378 char target_path[PATH_MAX];
1379 size_t len, target_len = 0;
1380 char *path_got = NULL;
1381 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1382 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1384 *is_bad_symlink = 0;
1387 * Blob object content specifies the target path of the link.
1388 * If a symbolic link cannot be installed we instead create
1389 * a regular file which contains the link target path stored
1390 * in the blob object.
1392 do {
1393 err = got_object_blob_read_block(&len, blob);
1394 if (len + target_len >= sizeof(target_path)) {
1395 /* Path too long; install as a regular file. */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 return install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file,
1401 1, path_is_unversioned, repo, progress_cb,
1402 progress_arg);
1404 if (len > 0) {
1405 /* Skip blob object header first time around. */
1406 memcpy(target_path + target_len, buf + hdrlen,
1407 len - hdrlen);
1408 target_len += len - hdrlen;
1409 hdrlen = 0;
1411 } while (len != 0);
1412 target_path[target_len] = '\0';
1414 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1415 ondisk_path, worktree->root_path);
1416 if (err)
1417 return err;
1419 if (*is_bad_symlink && !allow_bad_symlinks) {
1420 /* install as a regular file */
1421 got_object_blob_rewind(blob);
1422 err = install_blob(worktree, ondisk_path, path,
1423 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1424 restoring_missing_file, reverting_versioned_file, 1,
1425 path_is_unversioned, repo, progress_cb, progress_arg);
1426 goto done;
1429 if (symlink(target_path, ondisk_path) == -1) {
1430 if (errno == EEXIST) {
1431 int symlink_replaced;
1432 if (path_is_unversioned) {
1433 err = (*progress_cb)(progress_arg,
1434 GOT_STATUS_UNVERSIONED, path);
1435 goto done;
1437 err = replace_existing_symlink(&symlink_replaced,
1438 ondisk_path, target_path, target_len);
1439 if (err)
1440 goto done;
1441 if (progress_cb) {
1442 if (symlink_replaced) {
1443 err = (*progress_cb)(progress_arg,
1444 reverting_versioned_file ?
1445 GOT_STATUS_REVERT :
1446 GOT_STATUS_UPDATE, path);
1447 } else {
1448 err = (*progress_cb)(progress_arg,
1449 GOT_STATUS_EXISTS, path);
1452 goto done; /* Nothing else to do. */
1455 if (errno == ENOENT) {
1456 char *parent;
1457 err = got_path_dirname(&parent, ondisk_path);
1458 if (err)
1459 goto done;
1460 err = add_dir_on_disk(worktree, parent);
1461 free(parent);
1462 if (err)
1463 goto done;
1465 * Retry, and fall through to error handling
1466 * below if this second attempt fails.
1468 if (symlink(target_path, ondisk_path) != -1) {
1469 err = NULL; /* success */
1470 goto done;
1474 /* Handle errors from first or second creation attempt. */
1475 if (errno == ENAMETOOLONG) {
1476 /* bad target path; install as a regular file */
1477 *is_bad_symlink = 1;
1478 got_object_blob_rewind(blob);
1479 err = install_blob(worktree, ondisk_path, path,
1480 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1481 restoring_missing_file, reverting_versioned_file, 1,
1482 path_is_unversioned, repo,
1483 progress_cb, progress_arg);
1484 } else if (errno == ENOTDIR) {
1485 err = got_error_path(ondisk_path,
1486 GOT_ERR_FILE_OBSTRUCTED);
1487 } else {
1488 err = got_error_from_errno3("symlink",
1489 target_path, ondisk_path);
1491 } else if (progress_cb)
1492 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1493 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1494 done:
1495 free(path_got);
1496 return err;
1499 static const struct got_error *
1500 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1501 const char *path, mode_t te_mode, mode_t st_mode,
1502 struct got_blob_object *blob, int restoring_missing_file,
1503 int reverting_versioned_file, int installing_bad_symlink,
1504 int path_is_unversioned, struct got_repository *repo,
1505 got_worktree_checkout_cb progress_cb, void *progress_arg)
1507 const struct got_error *err = NULL;
1508 int fd = -1;
1509 size_t len, hdrlen;
1510 int update = 0;
1511 char *tmppath = NULL;
1513 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1514 GOT_DEFAULT_FILE_MODE);
1515 if (fd == -1) {
1516 if (errno == ENOENT) {
1517 char *parent;
1518 err = got_path_dirname(&parent, path);
1519 if (err)
1520 return err;
1521 err = add_dir_on_disk(worktree, parent);
1522 free(parent);
1523 if (err)
1524 return err;
1525 fd = open(ondisk_path,
1526 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1527 GOT_DEFAULT_FILE_MODE);
1528 if (fd == -1)
1529 return got_error_from_errno2("open",
1530 ondisk_path);
1531 } else if (errno == EEXIST) {
1532 if (path_is_unversioned) {
1533 err = (*progress_cb)(progress_arg,
1534 GOT_STATUS_UNVERSIONED, path);
1535 goto done;
1537 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1538 !S_ISREG(st_mode) && !installing_bad_symlink) {
1539 /* TODO file is obstructed; do something */
1540 err = got_error_path(ondisk_path,
1541 GOT_ERR_FILE_OBSTRUCTED);
1542 goto done;
1543 } else {
1544 err = got_opentemp_named_fd(&tmppath, &fd,
1545 ondisk_path);
1546 if (err)
1547 goto done;
1548 update = 1;
1550 } else
1551 return got_error_from_errno2("open", ondisk_path);
1554 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1555 err = got_error_from_errno2("fchmod",
1556 update ? tmppath : ondisk_path);
1557 goto done;
1560 if (progress_cb) {
1561 if (restoring_missing_file)
1562 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1563 path);
1564 else if (reverting_versioned_file)
1565 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1566 path);
1567 else
1568 err = (*progress_cb)(progress_arg,
1569 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1570 if (err)
1571 goto done;
1574 hdrlen = got_object_blob_get_hdrlen(blob);
1575 do {
1576 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1577 err = got_object_blob_read_block(&len, blob);
1578 if (err)
1579 break;
1580 if (len > 0) {
1581 /* Skip blob object header first time around. */
1582 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1583 if (outlen == -1) {
1584 err = got_error_from_errno("write");
1585 goto done;
1586 } else if (outlen != len - hdrlen) {
1587 err = got_error(GOT_ERR_IO);
1588 goto done;
1590 hdrlen = 0;
1592 } while (len != 0);
1594 if (fsync(fd) != 0) {
1595 err = got_error_from_errno("fsync");
1596 goto done;
1599 if (update) {
1600 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1601 err = got_error_from_errno2("unlink", ondisk_path);
1602 goto done;
1604 if (rename(tmppath, ondisk_path) != 0) {
1605 err = got_error_from_errno3("rename", tmppath,
1606 ondisk_path);
1607 goto done;
1609 free(tmppath);
1610 tmppath = NULL;
1613 done:
1614 if (fd != -1 && close(fd) == -1 && err == NULL)
1615 err = got_error_from_errno("close");
1616 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1617 err = got_error_from_errno2("unlink", tmppath);
1618 free(tmppath);
1619 return err;
1622 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1623 static const struct got_error *
1624 get_modified_file_content_status(unsigned char *status, FILE *f)
1626 const struct got_error *err = NULL;
1627 const char *markers[3] = {
1628 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1629 GOT_DIFF_CONFLICT_MARKER_SEP,
1630 GOT_DIFF_CONFLICT_MARKER_END
1632 int i = 0;
1633 char *line = NULL;
1634 size_t linesize = 0;
1635 ssize_t linelen;
1637 while (*status == GOT_STATUS_MODIFY) {
1638 linelen = getline(&line, &linesize, f);
1639 if (linelen == -1) {
1640 if (feof(f))
1641 break;
1642 err = got_ferror(f, GOT_ERR_IO);
1643 break;
1646 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1647 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1648 == 0)
1649 *status = GOT_STATUS_CONFLICT;
1650 else
1651 i++;
1654 free(line);
1656 return err;
1659 static int
1660 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1662 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1663 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1666 static int
1667 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1669 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1670 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1671 ie->mtime_sec == sb->st_mtim.tv_sec &&
1672 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1673 ie->size == (sb->st_size & 0xffffffff) &&
1674 !xbit_differs(ie, sb->st_mode));
1677 static unsigned char
1678 get_staged_status(struct got_fileindex_entry *ie)
1680 switch (got_fileindex_entry_stage_get(ie)) {
1681 case GOT_FILEIDX_STAGE_ADD:
1682 return GOT_STATUS_ADD;
1683 case GOT_FILEIDX_STAGE_DELETE:
1684 return GOT_STATUS_DELETE;
1685 case GOT_FILEIDX_STAGE_MODIFY:
1686 return GOT_STATUS_MODIFY;
1687 default:
1688 return GOT_STATUS_NO_CHANGE;
1692 static const struct got_error *
1693 get_symlink_modification_status(unsigned char *status,
1694 struct got_fileindex_entry *ie, const char *abspath,
1695 int dirfd, const char *de_name, struct got_blob_object *blob)
1697 const struct got_error *err = NULL;
1698 char target_path[PATH_MAX];
1699 char etarget[PATH_MAX];
1700 ssize_t elen;
1701 size_t len, target_len = 0;
1702 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1703 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1705 *status = GOT_STATUS_NO_CHANGE;
1707 /* Blob object content specifies the target path of the link. */
1708 do {
1709 err = got_object_blob_read_block(&len, blob);
1710 if (err)
1711 return err;
1712 if (len + target_len >= sizeof(target_path)) {
1714 * Should not happen. The blob contents were OK
1715 * when this symlink was installed.
1717 return got_error(GOT_ERR_NO_SPACE);
1719 if (len > 0) {
1720 /* Skip blob object header first time around. */
1721 memcpy(target_path + target_len, buf + hdrlen,
1722 len - hdrlen);
1723 target_len += len - hdrlen;
1724 hdrlen = 0;
1726 } while (len != 0);
1727 target_path[target_len] = '\0';
1729 if (dirfd != -1) {
1730 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1731 if (elen == -1)
1732 return got_error_from_errno2("readlinkat", abspath);
1733 } else {
1734 elen = readlink(abspath, etarget, sizeof(etarget));
1735 if (elen == -1)
1736 return got_error_from_errno2("readlink", abspath);
1739 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1740 *status = GOT_STATUS_MODIFY;
1742 return NULL;
1745 static const struct got_error *
1746 get_file_status(unsigned char *status, struct stat *sb,
1747 struct got_fileindex_entry *ie, const char *abspath,
1748 int dirfd, const char *de_name, struct got_repository *repo)
1750 const struct got_error *err = NULL;
1751 struct got_object_id id;
1752 size_t hdrlen;
1753 int fd = -1;
1754 FILE *f = NULL;
1755 uint8_t fbuf[8192];
1756 struct got_blob_object *blob = NULL;
1757 size_t flen, blen;
1758 unsigned char staged_status = get_staged_status(ie);
1760 *status = GOT_STATUS_NO_CHANGE;
1761 memset(sb, 0, sizeof(*sb));
1764 * Whenever the caller provides a directory descriptor and a
1765 * directory entry name for the file, use them! This prevents
1766 * race conditions if filesystem paths change beneath our feet.
1768 if (dirfd != -1) {
1769 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1770 if (errno == ENOENT) {
1771 if (got_fileindex_entry_has_file_on_disk(ie))
1772 *status = GOT_STATUS_MISSING;
1773 else
1774 *status = GOT_STATUS_DELETE;
1775 goto done;
1777 err = got_error_from_errno2("fstatat", abspath);
1778 goto done;
1780 } else {
1781 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1782 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1783 return got_error_from_errno2("open", abspath);
1784 else if (fd == -1 && errno == ELOOP) {
1785 if (lstat(abspath, sb) == -1)
1786 return got_error_from_errno2("lstat", abspath);
1787 } else if (fd == -1 || fstat(fd, sb) == -1) {
1788 if (errno == ENOENT) {
1789 if (got_fileindex_entry_has_file_on_disk(ie))
1790 *status = GOT_STATUS_MISSING;
1791 else
1792 *status = GOT_STATUS_DELETE;
1793 goto done;
1795 err = got_error_from_errno2("fstat", abspath);
1796 goto done;
1800 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1801 *status = GOT_STATUS_OBSTRUCTED;
1802 goto done;
1805 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1806 *status = GOT_STATUS_DELETE;
1807 goto done;
1808 } else if (!got_fileindex_entry_has_blob(ie) &&
1809 staged_status != GOT_STATUS_ADD) {
1810 *status = GOT_STATUS_ADD;
1811 goto done;
1814 if (!stat_info_differs(ie, sb))
1815 goto done;
1817 if (S_ISLNK(sb->st_mode) &&
1818 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1819 *status = GOT_STATUS_MODIFY;
1820 goto done;
1823 if (staged_status == GOT_STATUS_MODIFY ||
1824 staged_status == GOT_STATUS_ADD)
1825 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1826 else
1827 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1829 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1830 if (err)
1831 goto done;
1833 if (S_ISLNK(sb->st_mode)) {
1834 err = get_symlink_modification_status(status, ie,
1835 abspath, dirfd, de_name, blob);
1836 goto done;
1839 if (dirfd != -1) {
1840 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1841 if (fd == -1) {
1842 err = got_error_from_errno2("openat", abspath);
1843 goto done;
1847 f = fdopen(fd, "r");
1848 if (f == NULL) {
1849 err = got_error_from_errno2("fdopen", abspath);
1850 goto done;
1852 fd = -1;
1853 hdrlen = got_object_blob_get_hdrlen(blob);
1854 for (;;) {
1855 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1856 err = got_object_blob_read_block(&blen, blob);
1857 if (err)
1858 goto done;
1859 /* Skip length of blob object header first time around. */
1860 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1861 if (flen == 0 && ferror(f)) {
1862 err = got_error_from_errno("fread");
1863 goto done;
1865 if (blen - hdrlen == 0) {
1866 if (flen != 0)
1867 *status = GOT_STATUS_MODIFY;
1868 break;
1869 } else if (flen == 0) {
1870 if (blen - hdrlen != 0)
1871 *status = GOT_STATUS_MODIFY;
1872 break;
1873 } else if (blen - hdrlen == flen) {
1874 /* Skip blob object header first time around. */
1875 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1876 *status = GOT_STATUS_MODIFY;
1877 break;
1879 } else {
1880 *status = GOT_STATUS_MODIFY;
1881 break;
1883 hdrlen = 0;
1886 if (*status == GOT_STATUS_MODIFY) {
1887 rewind(f);
1888 err = get_modified_file_content_status(status, f);
1889 } else if (xbit_differs(ie, sb->st_mode))
1890 *status = GOT_STATUS_MODE_CHANGE;
1891 done:
1892 if (blob)
1893 got_object_blob_close(blob);
1894 if (f != NULL && fclose(f) == EOF && err == NULL)
1895 err = got_error_from_errno2("fclose", abspath);
1896 if (fd != -1 && close(fd) == -1 && err == NULL)
1897 err = got_error_from_errno2("close", abspath);
1898 return err;
1902 * Update timestamps in the file index if a file is unmodified and
1903 * we had to run a full content comparison to find out.
1905 static const struct got_error *
1906 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1907 struct got_fileindex_entry *ie, struct stat *sb)
1909 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1910 return got_fileindex_entry_update(ie, wt_fd, path,
1911 ie->blob_sha1, ie->commit_sha1, 1);
1913 return NULL;
1916 static const struct got_error *
1917 update_blob(struct got_worktree *worktree,
1918 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1919 struct got_tree_entry *te, const char *path,
1920 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1921 void *progress_arg)
1923 const struct got_error *err = NULL;
1924 struct got_blob_object *blob = NULL;
1925 char *ondisk_path;
1926 unsigned char status = GOT_STATUS_NO_CHANGE;
1927 struct stat sb;
1929 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1930 return got_error_from_errno("asprintf");
1932 if (ie) {
1933 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1934 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1935 goto done;
1937 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1938 repo);
1939 if (err)
1940 goto done;
1941 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1942 sb.st_mode = got_fileindex_perms_to_st(ie);
1943 } else {
1944 if (stat(ondisk_path, &sb) == -1) {
1945 if (errno != ENOENT) {
1946 err = got_error_from_errno2("stat",
1947 ondisk_path);
1948 goto done;
1950 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1951 status = GOT_STATUS_UNVERSIONED;
1952 } else {
1953 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1954 status = GOT_STATUS_UNVERSIONED;
1955 else
1956 status = GOT_STATUS_OBSTRUCTED;
1960 if (status == GOT_STATUS_OBSTRUCTED) {
1961 if (ie)
1962 got_fileindex_entry_mark_skipped(ie);
1963 err = (*progress_cb)(progress_arg, status, path);
1964 goto done;
1966 if (status == GOT_STATUS_CONFLICT) {
1967 if (ie)
1968 got_fileindex_entry_mark_skipped(ie);
1969 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1970 path);
1971 goto done;
1974 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1975 (S_ISLNK(te->mode) ||
1976 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1978 * This is a regular file or an installed bad symlink.
1979 * If the file index indicates that this file is already
1980 * up-to-date with respect to the repository we can skip
1981 * updating contents of this file.
1983 if (got_fileindex_entry_has_commit(ie) &&
1984 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1985 SHA1_DIGEST_LENGTH) == 0) {
1986 /* Same commit. */
1987 err = sync_timestamps(worktree->root_fd,
1988 path, status, ie, &sb);
1989 if (err)
1990 goto done;
1991 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1992 path);
1993 goto done;
1995 if (got_fileindex_entry_has_blob(ie) &&
1996 memcmp(ie->blob_sha1, te->id.sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1998 /* Different commit but the same blob. */
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2001 if (err)
2002 goto done;
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 path);
2005 goto done;
2009 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2010 if (err)
2011 goto done;
2013 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2014 int update_timestamps;
2015 struct got_blob_object *blob2 = NULL;
2016 char *label_orig = NULL;
2017 if (got_fileindex_entry_has_blob(ie)) {
2018 struct got_object_id id2;
2019 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2020 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2021 if (err)
2022 goto done;
2024 if (got_fileindex_entry_has_commit(ie)) {
2025 char id_str[SHA1_DIGEST_STRING_LENGTH];
2026 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2027 sizeof(id_str)) == NULL) {
2028 err = got_error_path(id_str,
2029 GOT_ERR_BAD_OBJ_ID_STR);
2030 goto done;
2032 if (asprintf(&label_orig, "%s: commit %s",
2033 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2034 err = got_error_from_errno("asprintf");
2035 goto done;
2038 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2039 char *link_target;
2040 err = got_object_blob_read_to_str(&link_target, blob);
2041 if (err)
2042 goto done;
2043 err = merge_symlink(worktree, blob2, ondisk_path, path,
2044 label_orig, link_target, worktree->base_commit_id,
2045 repo, progress_cb, progress_arg);
2046 free(link_target);
2047 } else {
2048 err = merge_blob(&update_timestamps, worktree, blob2,
2049 ondisk_path, path, sb.st_mode, label_orig, blob,
2050 worktree->base_commit_id, repo,
2051 progress_cb, progress_arg);
2053 free(label_orig);
2054 if (blob2)
2055 got_object_blob_close(blob2);
2056 if (err)
2057 goto done;
2059 * Do not update timestamps of files with local changes.
2060 * Otherwise, a future status walk would treat them as
2061 * unmodified files again.
2063 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2064 blob->id.sha1, worktree->base_commit_id->sha1,
2065 update_timestamps);
2066 } else if (status == GOT_STATUS_MODE_CHANGE) {
2067 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2068 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2069 } else if (status == GOT_STATUS_DELETE) {
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2071 if (err)
2072 goto done;
2073 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2074 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2075 if (err)
2076 goto done;
2077 } else {
2078 int is_bad_symlink = 0;
2079 if (S_ISLNK(te->mode)) {
2080 err = install_symlink(&is_bad_symlink, worktree,
2081 ondisk_path, path, blob,
2082 status == GOT_STATUS_MISSING, 0,
2083 status == GOT_STATUS_UNVERSIONED, 0,
2084 repo, progress_cb, progress_arg);
2085 } else {
2086 err = install_blob(worktree, ondisk_path, path,
2087 te->mode, sb.st_mode, blob,
2088 status == GOT_STATUS_MISSING, 0, 0,
2089 status == GOT_STATUS_UNVERSIONED, repo,
2090 progress_cb, progress_arg);
2092 if (err)
2093 goto done;
2095 if (ie) {
2096 err = got_fileindex_entry_update(ie,
2097 worktree->root_fd, path, blob->id.sha1,
2098 worktree->base_commit_id->sha1, 1);
2099 } else {
2100 err = create_fileindex_entry(&ie, fileindex,
2101 worktree->base_commit_id, worktree->root_fd, path,
2102 &blob->id);
2104 if (err)
2105 goto done;
2107 if (is_bad_symlink) {
2108 got_fileindex_entry_filetype_set(ie,
2109 GOT_FILEIDX_MODE_BAD_SYMLINK);
2112 got_object_blob_close(blob);
2113 done:
2114 free(ondisk_path);
2115 return err;
2118 static const struct got_error *
2119 remove_ondisk_file(const char *root_path, const char *path)
2121 const struct got_error *err = NULL;
2122 char *ondisk_path = NULL, *parent = NULL;
2124 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2125 return got_error_from_errno("asprintf");
2127 if (unlink(ondisk_path) == -1) {
2128 if (errno != ENOENT)
2129 err = got_error_from_errno2("unlink", ondisk_path);
2130 } else {
2131 size_t root_len = strlen(root_path);
2132 err = got_path_dirname(&parent, ondisk_path);
2133 if (err)
2134 goto done;
2135 while (got_path_cmp(parent, root_path,
2136 strlen(parent), root_len) != 0) {
2137 free(ondisk_path);
2138 ondisk_path = parent;
2139 parent = NULL;
2140 if (rmdir(ondisk_path) == -1) {
2141 if (errno != ENOTEMPTY)
2142 err = got_error_from_errno2("rmdir",
2143 ondisk_path);
2144 break;
2146 err = got_path_dirname(&parent, ondisk_path);
2147 if (err)
2148 break;
2151 done:
2152 free(ondisk_path);
2153 free(parent);
2154 return err;
2157 static const struct got_error *
2158 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2159 struct got_fileindex_entry *ie, struct got_repository *repo,
2160 got_worktree_checkout_cb progress_cb, void *progress_arg)
2162 const struct got_error *err = NULL;
2163 unsigned char status;
2164 struct stat sb;
2165 char *ondisk_path;
2167 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2168 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2171 == -1)
2172 return got_error_from_errno("asprintf");
2174 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2175 if (err)
2176 goto done;
2178 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2179 char ondisk_target[PATH_MAX];
2180 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2181 sizeof(ondisk_target));
2182 if (ondisk_len == -1) {
2183 err = got_error_from_errno2("readlink", ondisk_path);
2184 goto done;
2186 ondisk_target[ondisk_len] = '\0';
2187 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2188 NULL, NULL, /* XXX pass common ancestor info? */
2189 ondisk_target, ondisk_path);
2190 if (err)
2191 goto done;
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2193 ie->path);
2194 goto done;
2197 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2198 status == GOT_STATUS_ADD) {
2199 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2200 if (err)
2201 goto done;
2203 * Preserve the working file and change the deleted blob's
2204 * entry into a schedule-add entry.
2206 err = got_fileindex_entry_update(ie, worktree->root_fd,
2207 ie->path, NULL, NULL, 0);
2208 } else {
2209 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2210 if (err)
2211 goto done;
2212 if (status == GOT_STATUS_NO_CHANGE) {
2213 err = remove_ondisk_file(worktree->root_path, ie->path);
2214 if (err)
2215 goto done;
2217 got_fileindex_entry_remove(fileindex, ie);
2219 done:
2220 free(ondisk_path);
2221 return err;
2224 struct diff_cb_arg {
2225 struct got_fileindex *fileindex;
2226 struct got_worktree *worktree;
2227 struct got_repository *repo;
2228 got_worktree_checkout_cb progress_cb;
2229 void *progress_arg;
2230 got_cancel_cb cancel_cb;
2231 void *cancel_arg;
2234 static const struct got_error *
2235 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2236 struct got_tree_entry *te, 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 update_blob(a->worktree, a->fileindex, ie, te,
2244 ie->path, a->repo, a->progress_cb, a->progress_arg);
2247 static const struct got_error *
2248 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2250 struct diff_cb_arg *a = arg;
2252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2253 return got_error(GOT_ERR_CANCELLED);
2255 return delete_blob(a->worktree, a->fileindex, ie,
2256 a->repo, a->progress_cb, a->progress_arg);
2259 static const struct got_error *
2260 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2262 struct diff_cb_arg *a = arg;
2263 const struct got_error *err;
2264 char *path;
2266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2267 return got_error(GOT_ERR_CANCELLED);
2269 if (got_object_tree_entry_is_submodule(te))
2270 return NULL;
2272 if (asprintf(&path, "%s%s%s", parent_path,
2273 parent_path[0] ? "/" : "", te->name)
2274 == -1)
2275 return got_error_from_errno("asprintf");
2277 if (S_ISDIR(te->mode))
2278 err = add_dir_on_disk(a->worktree, path);
2279 else
2280 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2281 a->repo, a->progress_cb, a->progress_arg);
2283 free(path);
2284 return err;
2287 const struct got_error *
2288 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2290 uint32_t uuid_status;
2292 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2293 if (uuid_status != uuid_s_ok) {
2294 *uuidstr = NULL;
2295 return got_error_uuid(uuid_status, "uuid_to_string");
2298 return NULL;
2301 static const struct got_error *
2302 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2304 const struct got_error *err = NULL;
2305 char *uuidstr = NULL;
2307 *refname = NULL;
2309 err = got_worktree_get_uuid(&uuidstr, worktree);
2310 if (err)
2311 return err;
2313 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2314 err = got_error_from_errno("asprintf");
2315 *refname = NULL;
2317 free(uuidstr);
2318 return err;
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2388 *path = NULL;
2389 return got_error_from_errno("asprintf");
2391 return NULL;
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2417 char *refname;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2420 if (err)
2421 return err;
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2424 if (err)
2425 goto done;
2427 err = got_ref_write(ref, repo);
2428 done:
2429 free(refname);
2430 if (ref)
2431 got_ref_close(ref);
2432 return err;
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2445 return err;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2454 FILE *index = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2462 if (err)
2463 goto done;
2465 index = fopen(*fileindex_path, "rb");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2469 } else {
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2474 done:
2475 if (err) {
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2479 *fileindex = NULL;
2481 return err;
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2486 const char *path;
2487 size_t path_len;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2490 void *progress_arg;
2493 /* Bump base commit ID of all files within an updated part of the work tree. */
2494 static const struct got_error *
2495 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2497 const struct got_error *err;
2498 struct bump_base_commit_id_arg *a = arg;
2500 if (a->entry_name) {
2501 if (strcmp(ie->path, a->path) != 0)
2502 return NULL;
2503 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2504 return NULL;
2506 if (got_fileindex_entry_was_skipped(ie))
2507 return NULL;
2509 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2510 SHA1_DIGEST_LENGTH) == 0)
2511 return NULL;
2513 if (a->progress_cb) {
2514 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2515 ie->path);
2516 if (err)
2517 return err;
2519 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2520 return NULL;
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2532 bbc_arg.path = "";
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 fileindex_path);
2551 if (err)
2552 goto done;
2554 err = got_fileindex_write(fileindex, new_index);
2555 if (err)
2556 goto done;
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2560 fileindex_path);
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2569 timeout.tv_sec = 0;
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2572 done:
2573 if (new_index)
2574 fclose(new_index);
2575 free(new_fileindex_path);
2576 return err;
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_worktree *worktree, struct got_repository *repo)
2584 const struct got_error *err = NULL;
2585 struct got_object_id *id = NULL;
2586 char *in_repo_path = NULL;
2587 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2589 *entry_type = GOT_OBJ_TYPE_ANY;
2590 *tree_relpath = NULL;
2591 *tree_id = NULL;
2593 if (wt_relpath[0] == '\0') {
2594 /* Check out all files within the work tree. */
2595 *entry_type = GOT_OBJ_TYPE_TREE;
2596 *tree_relpath = strdup("");
2597 if (*tree_relpath == NULL) {
2598 err = got_error_from_errno("strdup");
2599 goto done;
2601 err = got_object_id_by_path(tree_id, repo,
2602 worktree->base_commit_id, worktree->path_prefix);
2603 if (err)
2604 goto done;
2605 return NULL;
2608 /* Check out a subset of files in the work tree. */
2610 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2611 is_root_wt ? "" : "/", wt_relpath) == -1) {
2612 err = got_error_from_errno("asprintf");
2613 goto done;
2616 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2617 in_repo_path);
2618 if (err)
2619 goto done;
2621 free(in_repo_path);
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2625 if (err)
2626 goto done;
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2640 goto done;
2642 } else {
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2645 if (err)
2646 return err;
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2651 goto done;
2654 err = got_object_id_by_path(tree_id, repo,
2655 worktree->base_commit_id, in_repo_path);
2656 } else {
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2661 goto done;
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2669 done:
2670 free(id);
2671 free(in_repo_path);
2672 if (err) {
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2676 free(*tree_id);
2677 *tree_id = NULL;
2679 return err;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2695 if (err) {
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2698 goto done;
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2701 if (err)
2702 return err;
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2707 if (err)
2708 goto done;
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2711 if (err)
2712 goto done;
2714 if (entry_name &&
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2717 goto done;
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2725 arg.repo = repo;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2732 done:
2733 if (tree)
2734 got_object_tree_close(tree);
2735 if (commit)
2736 got_object_commit_close(commit);
2737 return err;
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2755 int entry_type;
2756 char *relpath;
2757 char *entry_name;
2758 } *tpd = NULL;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2764 if (err)
2765 return err;
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, worktree, repo);
2777 if (err) {
2778 free(tpd);
2779 goto done;
2782 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2783 err = got_path_basename(&tpd->entry_name, pe->path);
2784 if (err) {
2785 free(tpd->relpath);
2786 free(tpd->tree_id);
2787 free(tpd);
2788 goto done;
2790 } else
2791 tpd->entry_name = NULL;
2793 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2797 * Read the file index.
2798 * Checking out files is supposed to be an idempotent operation.
2799 * If the on-disk file index is incomplete we will try to complete it.
2801 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2802 if (err)
2803 goto done;
2805 tpd = STAILQ_FIRST(&tree_paths);
2806 TAILQ_FOREACH(pe, paths, entry) {
2807 struct bump_base_commit_id_arg bbc_arg;
2809 err = checkout_files(worktree, fileindex, tpd->relpath,
2810 tpd->tree_id, tpd->entry_name, repo,
2811 progress_cb, progress_arg, cancel_cb, cancel_arg);
2812 if (err)
2813 break;
2815 bbc_arg.base_commit_id = worktree->base_commit_id;
2816 bbc_arg.entry_name = tpd->entry_name;
2817 bbc_arg.path = pe->path;
2818 bbc_arg.path_len = pe->path_len;
2819 bbc_arg.progress_cb = progress_cb;
2820 bbc_arg.progress_arg = progress_arg;
2821 err = got_fileindex_for_each_entry_safe(fileindex,
2822 bump_base_commit_id, &bbc_arg);
2823 if (err)
2824 break;
2826 tpd = STAILQ_NEXT(tpd, entry);
2828 sync_err = sync_fileindex(fileindex, fileindex_path);
2829 if (sync_err && err == NULL)
2830 err = sync_err;
2831 done:
2832 free(fileindex_path);
2833 if (tree)
2834 got_object_tree_close(tree);
2835 if (commit)
2836 got_object_commit_close(commit);
2837 if (fileindex)
2838 got_fileindex_free(fileindex);
2839 while (!STAILQ_EMPTY(&tree_paths)) {
2840 tpd = STAILQ_FIRST(&tree_paths);
2841 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2842 free(tpd->relpath);
2843 free(tpd->tree_id);
2844 free(tpd);
2846 unlockerr = lock_worktree(worktree, LOCK_SH);
2847 if (unlockerr && err == NULL)
2848 err = unlockerr;
2849 return err;
2852 struct merge_file_cb_arg {
2853 struct got_worktree *worktree;
2854 struct got_fileindex *fileindex;
2855 got_worktree_checkout_cb progress_cb;
2856 void *progress_arg;
2857 got_cancel_cb cancel_cb;
2858 void *cancel_arg;
2859 const char *label_orig;
2860 struct got_object_id *commit_id2;
2861 int allow_bad_symlinks;
2864 static const struct got_error *
2865 merge_file_cb(void *arg, struct got_blob_object *blob1,
2866 struct got_blob_object *blob2, struct got_object_id *id1,
2867 struct got_object_id *id2, const char *path1, const char *path2,
2868 mode_t mode1, mode_t mode2, struct got_repository *repo)
2870 static const struct got_error *err = NULL;
2871 struct merge_file_cb_arg *a = arg;
2872 struct got_fileindex_entry *ie;
2873 char *ondisk_path = NULL;
2874 struct stat sb;
2875 unsigned char status;
2876 int local_changes_subsumed;
2877 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2878 char *id_str = NULL, *label_deriv2 = NULL;
2880 if (blob1 && blob2) {
2881 ie = got_fileindex_entry_get(a->fileindex, path2,
2882 strlen(path2));
2883 if (ie == NULL)
2884 return (*a->progress_cb)(a->progress_arg,
2885 GOT_STATUS_MISSING, path2);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 path2) == -1)
2889 return got_error_from_errno("asprintf");
2891 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2892 repo);
2893 if (err)
2894 goto done;
2896 if (status == GOT_STATUS_DELETE) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_MERGE, path2);
2899 goto done;
2901 if (status != GOT_STATUS_NO_CHANGE &&
2902 status != GOT_STATUS_MODIFY &&
2903 status != GOT_STATUS_CONFLICT &&
2904 status != GOT_STATUS_ADD) {
2905 err = (*a->progress_cb)(a->progress_arg, status, path2);
2906 goto done;
2909 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2910 char *link_target2;
2911 err = got_object_blob_read_to_str(&link_target2, blob2);
2912 if (err)
2913 goto done;
2914 err = merge_symlink(a->worktree, blob1, ondisk_path,
2915 path2, a->label_orig, link_target2, a->commit_id2,
2916 repo, a->progress_cb, a->progress_arg);
2917 free(link_target2);
2918 } else {
2919 int fd;
2921 f_orig = got_opentemp();
2922 if (f_orig == NULL) {
2923 err = got_error_from_errno("got_opentemp");
2924 goto done;
2926 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2927 f_orig, blob1);
2928 if (err)
2929 goto done;
2931 f_deriv2 = got_opentemp();
2932 if (f_deriv2 == NULL)
2933 goto done;
2934 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2935 f_deriv2, blob2);
2936 if (err)
2937 goto done;
2939 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2940 if (fd == -1) {
2941 err = got_error_from_errno2("open",
2942 ondisk_path);
2943 goto done;
2945 f_deriv = fdopen(fd, "r");
2946 if (f_deriv == NULL) {
2947 err = got_error_from_errno2("fdopen",
2948 ondisk_path);
2949 close(fd);
2950 goto done;
2952 err = got_object_id_str(&id_str, a->commit_id2);
2953 if (err)
2954 goto done;
2955 if (asprintf(&label_deriv2, "%s: commit %s",
2956 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2957 err = got_error_from_errno("asprintf");
2958 goto done;
2960 err = merge_file(&local_changes_subsumed, a->worktree,
2961 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2962 sb.st_mode, a->label_orig, NULL, label_deriv2,
2963 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2964 a->progress_cb, a->progress_arg);
2966 } else if (blob1) {
2967 ie = got_fileindex_entry_get(a->fileindex, path1,
2968 strlen(path1));
2969 if (ie == NULL)
2970 return (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_MISSING, path1);
2973 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2974 path1) == -1)
2975 return got_error_from_errno("asprintf");
2977 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2978 repo);
2979 if (err)
2980 goto done;
2982 switch (status) {
2983 case GOT_STATUS_NO_CHANGE:
2984 err = (*a->progress_cb)(a->progress_arg,
2985 GOT_STATUS_DELETE, path1);
2986 if (err)
2987 goto done;
2988 err = remove_ondisk_file(a->worktree->root_path, path1);
2989 if (err)
2990 goto done;
2991 if (ie)
2992 got_fileindex_entry_mark_deleted_from_disk(ie);
2993 break;
2994 case GOT_STATUS_DELETE:
2995 case GOT_STATUS_MISSING:
2996 err = (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_DELETE, path1);
2998 if (err)
2999 goto done;
3000 if (ie)
3001 got_fileindex_entry_mark_deleted_from_disk(ie);
3002 break;
3003 case GOT_STATUS_ADD: {
3004 struct got_object_id *id;
3005 FILE *blob1_f;
3007 * Delete the added file only if its content already
3008 * exists in the repository.
3010 err = got_object_blob_file_create(&id, &blob1_f, path1);
3011 if (err)
3012 goto done;
3013 if (got_object_id_cmp(id, id1) == 0) {
3014 err = (*a->progress_cb)(a->progress_arg,
3015 GOT_STATUS_DELETE, path1);
3016 if (err)
3017 goto done;
3018 err = remove_ondisk_file(a->worktree->root_path,
3019 path1);
3020 if (err)
3021 goto done;
3022 if (ie)
3023 got_fileindex_entry_remove(a->fileindex,
3024 ie);
3025 } else {
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_CANNOT_DELETE, path1);
3029 if (fclose(blob1_f) == EOF && err == NULL)
3030 err = got_error_from_errno("fclose");
3031 free(id);
3032 if (err)
3033 goto done;
3034 break;
3036 case GOT_STATUS_MODIFY:
3037 case GOT_STATUS_CONFLICT:
3038 err = (*a->progress_cb)(a->progress_arg,
3039 GOT_STATUS_CANNOT_DELETE, path1);
3040 if (err)
3041 goto done;
3042 break;
3043 case GOT_STATUS_OBSTRUCTED:
3044 err = (*a->progress_cb)(a->progress_arg, status, path1);
3045 if (err)
3046 goto done;
3047 break;
3048 default:
3049 break;
3051 } else if (blob2) {
3052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3053 path2) == -1)
3054 return got_error_from_errno("asprintf");
3055 ie = got_fileindex_entry_get(a->fileindex, path2,
3056 strlen(path2));
3057 if (ie) {
3058 err = get_file_status(&status, &sb, ie, ondisk_path,
3059 -1, NULL, repo);
3060 if (err)
3061 goto done;
3062 if (status != GOT_STATUS_NO_CHANGE &&
3063 status != GOT_STATUS_MODIFY &&
3064 status != GOT_STATUS_CONFLICT &&
3065 status != GOT_STATUS_ADD) {
3066 err = (*a->progress_cb)(a->progress_arg,
3067 status, path2);
3068 goto done;
3070 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3071 char *link_target2;
3072 err = got_object_blob_read_to_str(&link_target2,
3073 blob2);
3074 if (err)
3075 goto done;
3076 err = merge_symlink(a->worktree, NULL,
3077 ondisk_path, path2, a->label_orig,
3078 link_target2, a->commit_id2, repo,
3079 a->progress_cb, a->progress_arg);
3080 free(link_target2);
3081 } else if (S_ISREG(sb.st_mode)) {
3082 err = merge_blob(&local_changes_subsumed,
3083 a->worktree, NULL, ondisk_path, path2,
3084 sb.st_mode, a->label_orig, blob2,
3085 a->commit_id2, repo, a->progress_cb,
3086 a->progress_arg);
3087 } else {
3088 err = got_error_path(ondisk_path,
3089 GOT_ERR_FILE_OBSTRUCTED);
3091 if (err)
3092 goto done;
3093 if (status == GOT_STATUS_DELETE) {
3094 err = got_fileindex_entry_update(ie,
3095 a->worktree->root_fd, path2, blob2->id.sha1,
3096 a->worktree->base_commit_id->sha1, 0);
3097 if (err)
3098 goto done;
3100 } else {
3101 int is_bad_symlink = 0;
3102 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3103 if (S_ISLNK(mode2)) {
3104 err = install_symlink(&is_bad_symlink,
3105 a->worktree, ondisk_path, path2, blob2, 0,
3106 0, 1, a->allow_bad_symlinks, repo,
3107 a->progress_cb, a->progress_arg);
3108 } else {
3109 err = install_blob(a->worktree, ondisk_path, path2,
3110 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3111 a->progress_cb, a->progress_arg);
3113 if (err)
3114 goto done;
3115 err = got_fileindex_entry_alloc(&ie, path2);
3116 if (err)
3117 goto done;
3118 err = got_fileindex_entry_update(ie,
3119 a->worktree->root_fd, path2, NULL, NULL, 1);
3120 if (err) {
3121 got_fileindex_entry_free(ie);
3122 goto done;
3124 err = got_fileindex_entry_add(a->fileindex, ie);
3125 if (err) {
3126 got_fileindex_entry_free(ie);
3127 goto done;
3129 if (is_bad_symlink) {
3130 got_fileindex_entry_filetype_set(ie,
3131 GOT_FILEIDX_MODE_BAD_SYMLINK);
3135 done:
3136 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3137 err = got_error_from_errno("fclose");
3138 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3139 err = got_error_from_errno("fclose");
3140 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3141 err = got_error_from_errno("fclose");
3142 free(id_str);
3143 free(label_deriv2);
3144 free(ondisk_path);
3145 return err;
3148 static const struct got_error *
3149 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3151 struct got_worktree *worktree = arg;
3153 /* Reject merges into a work tree with mixed base commits. */
3154 if (got_fileindex_entry_has_commit(ie) &&
3155 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3156 SHA1_DIGEST_LENGTH) != 0)
3157 return got_error(GOT_ERR_MIXED_COMMITS);
3159 return NULL;
3162 struct check_merge_conflicts_arg {
3163 struct got_worktree *worktree;
3164 struct got_fileindex *fileindex;
3165 struct got_repository *repo;
3168 static const struct got_error *
3169 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3170 struct got_blob_object *blob2, struct got_object_id *id1,
3171 struct got_object_id *id2, const char *path1, const char *path2,
3172 mode_t mode1, mode_t mode2, struct got_repository *repo)
3174 const struct got_error *err = NULL;
3175 struct check_merge_conflicts_arg *a = arg;
3176 unsigned char status;
3177 struct stat sb;
3178 struct got_fileindex_entry *ie;
3179 const char *path = path2 ? path2 : path1;
3180 struct got_object_id *id = id2 ? id2 : id1;
3181 char *ondisk_path;
3183 if (id == NULL)
3184 return NULL;
3186 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3187 if (ie == NULL)
3188 return NULL;
3190 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3191 == -1)
3192 return got_error_from_errno("asprintf");
3194 /* Reject merges into a work tree with conflicted files. */
3195 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3196 free(ondisk_path);
3197 if (err)
3198 return err;
3199 if (status == GOT_STATUS_CONFLICT)
3200 return got_error(GOT_ERR_CONFLICTS);
3202 return NULL;
3205 static const struct got_error *
3206 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3207 const char *fileindex_path, struct got_object_id *commit_id1,
3208 struct got_object_id *commit_id2, struct got_repository *repo,
3209 got_worktree_checkout_cb progress_cb, void *progress_arg,
3210 got_cancel_cb cancel_cb, void *cancel_arg)
3212 const struct got_error *err = NULL, *sync_err;
3213 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3214 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3215 struct check_merge_conflicts_arg cmc_arg;
3216 struct merge_file_cb_arg arg;
3217 char *label_orig = NULL;
3219 if (commit_id1) {
3220 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3221 worktree->path_prefix);
3222 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3223 goto done;
3225 if (tree_id1) {
3226 char *id_str;
3228 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3229 if (err)
3230 goto done;
3232 err = got_object_id_str(&id_str, commit_id1);
3233 if (err)
3234 goto done;
3236 if (asprintf(&label_orig, "%s: commit %s",
3237 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3238 err = got_error_from_errno("asprintf");
3239 free(id_str);
3240 goto done;
3242 free(id_str);
3245 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3246 worktree->path_prefix);
3247 if (err)
3248 goto done;
3250 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3251 if (err)
3252 goto done;
3254 cmc_arg.worktree = worktree;
3255 cmc_arg.fileindex = fileindex;
3256 cmc_arg.repo = repo;
3257 err = got_diff_tree(tree1, tree2, "", "", repo,
3258 check_merge_conflicts, &cmc_arg, 0);
3259 if (err)
3260 goto done;
3262 arg.worktree = worktree;
3263 arg.fileindex = fileindex;
3264 arg.progress_cb = progress_cb;
3265 arg.progress_arg = progress_arg;
3266 arg.cancel_cb = cancel_cb;
3267 arg.cancel_arg = cancel_arg;
3268 arg.label_orig = label_orig;
3269 arg.commit_id2 = commit_id2;
3270 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3271 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3272 sync_err = sync_fileindex(fileindex, fileindex_path);
3273 if (sync_err && err == NULL)
3274 err = sync_err;
3275 done:
3276 if (tree1)
3277 got_object_tree_close(tree1);
3278 if (tree2)
3279 got_object_tree_close(tree2);
3280 free(label_orig);
3281 return err;
3284 const struct got_error *
3285 got_worktree_merge_files(struct got_worktree *worktree,
3286 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3287 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3288 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3290 const struct got_error *err, *unlockerr;
3291 char *fileindex_path = NULL;
3292 struct got_fileindex *fileindex = NULL;
3294 err = lock_worktree(worktree, LOCK_EX);
3295 if (err)
3296 return err;
3298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3299 if (err)
3300 goto done;
3302 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3303 worktree);
3304 if (err)
3305 goto done;
3307 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3308 commit_id2, repo, progress_cb, progress_arg,
3309 cancel_cb, cancel_arg);
3310 done:
3311 if (fileindex)
3312 got_fileindex_free(fileindex);
3313 free(fileindex_path);
3314 unlockerr = lock_worktree(worktree, LOCK_SH);
3315 if (unlockerr && err == NULL)
3316 err = unlockerr;
3317 return err;
3320 struct diff_dir_cb_arg {
3321 struct got_fileindex *fileindex;
3322 struct got_worktree *worktree;
3323 const char *status_path;
3324 size_t status_path_len;
3325 struct got_repository *repo;
3326 got_worktree_status_cb status_cb;
3327 void *status_arg;
3328 got_cancel_cb cancel_cb;
3329 void *cancel_arg;
3330 /* A pathlist containing per-directory pathlists of ignore patterns. */
3331 struct got_pathlist_head *ignores;
3332 int report_unchanged;
3333 int no_ignores;
3336 static const struct got_error *
3337 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3338 int dirfd, const char *de_name,
3339 got_worktree_status_cb status_cb, void *status_arg,
3340 struct got_repository *repo, int report_unchanged)
3342 const struct got_error *err = NULL;
3343 unsigned char status = GOT_STATUS_NO_CHANGE;
3344 unsigned char staged_status = get_staged_status(ie);
3345 struct stat sb;
3346 struct got_object_id blob_id, commit_id, staged_blob_id;
3347 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3348 struct got_object_id *staged_blob_idp = NULL;
3350 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3351 if (err)
3352 return err;
3354 if (status == GOT_STATUS_NO_CHANGE &&
3355 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3356 return NULL;
3358 if (got_fileindex_entry_has_blob(ie)) {
3359 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3360 blob_idp = &blob_id;
3362 if (got_fileindex_entry_has_commit(ie)) {
3363 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3364 commit_idp = &commit_id;
3366 if (staged_status == GOT_STATUS_ADD ||
3367 staged_status == GOT_STATUS_MODIFY) {
3368 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3369 SHA1_DIGEST_LENGTH);
3370 staged_blob_idp = &staged_blob_id;
3373 return (*status_cb)(status_arg, status, staged_status,
3374 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3377 static const struct got_error *
3378 status_old_new(void *arg, struct got_fileindex_entry *ie,
3379 struct dirent *de, const char *parent_path, int dirfd)
3381 const struct got_error *err = NULL;
3382 struct diff_dir_cb_arg *a = arg;
3383 char *abspath;
3385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3386 return got_error(GOT_ERR_CANCELLED);
3388 if (got_path_cmp(parent_path, a->status_path,
3389 strlen(parent_path), a->status_path_len) != 0 &&
3390 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3391 return NULL;
3393 if (parent_path[0]) {
3394 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3395 parent_path, de->d_name) == -1)
3396 return got_error_from_errno("asprintf");
3397 } else {
3398 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3399 de->d_name) == -1)
3400 return got_error_from_errno("asprintf");
3403 err = report_file_status(ie, abspath, dirfd, de->d_name,
3404 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3405 free(abspath);
3406 return err;
3409 static const struct got_error *
3410 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3412 struct diff_dir_cb_arg *a = arg;
3413 struct got_object_id blob_id, commit_id;
3414 unsigned char status;
3416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 return got_error(GOT_ERR_CANCELLED);
3419 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3420 return NULL;
3422 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3423 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3424 if (got_fileindex_entry_has_file_on_disk(ie))
3425 status = GOT_STATUS_MISSING;
3426 else
3427 status = GOT_STATUS_DELETE;
3428 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3429 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3432 void
3433 free_ignorelist(struct got_pathlist_head *ignorelist)
3435 struct got_pathlist_entry *pe;
3437 TAILQ_FOREACH(pe, ignorelist, entry)
3438 free((char *)pe->path);
3439 got_pathlist_free(ignorelist);
3442 void
3443 free_ignores(struct got_pathlist_head *ignores)
3445 struct got_pathlist_entry *pe;
3447 TAILQ_FOREACH(pe, ignores, entry) {
3448 struct got_pathlist_head *ignorelist = pe->data;
3449 free_ignorelist(ignorelist);
3450 free((char *)pe->path);
3452 got_pathlist_free(ignores);
3455 static const struct got_error *
3456 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3458 const struct got_error *err = NULL;
3459 struct got_pathlist_entry *pe = NULL;
3460 struct got_pathlist_head *ignorelist;
3461 char *line = NULL, *pattern, *dirpath = NULL;
3462 size_t linesize = 0;
3463 ssize_t linelen;
3465 ignorelist = calloc(1, sizeof(*ignorelist));
3466 if (ignorelist == NULL)
3467 return got_error_from_errno("calloc");
3468 TAILQ_INIT(ignorelist);
3470 while ((linelen = getline(&line, &linesize, f)) != -1) {
3471 if (linelen > 0 && line[linelen - 1] == '\n')
3472 line[linelen - 1] = '\0';
3474 /* Git's ignores may contain comments. */
3475 if (line[0] == '#')
3476 continue;
3478 /* Git's negated patterns are not (yet?) supported. */
3479 if (line[0] == '!')
3480 continue;
3482 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3483 line) == -1) {
3484 err = got_error_from_errno("asprintf");
3485 goto done;
3487 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3488 if (err)
3489 goto done;
3491 if (ferror(f)) {
3492 err = got_error_from_errno("getline");
3493 goto done;
3496 dirpath = strdup(path);
3497 if (dirpath == NULL) {
3498 err = got_error_from_errno("strdup");
3499 goto done;
3501 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3502 done:
3503 free(line);
3504 if (err || pe == NULL) {
3505 free(dirpath);
3506 free_ignorelist(ignorelist);
3508 return err;
3511 int
3512 match_ignores(struct got_pathlist_head *ignores, const char *path)
3514 struct got_pathlist_entry *pe;
3516 /* Handle patterns which match in all directories. */
3517 TAILQ_FOREACH(pe, ignores, entry) {
3518 struct got_pathlist_head *ignorelist = pe->data;
3519 struct got_pathlist_entry *pi;
3521 TAILQ_FOREACH(pi, ignorelist, entry) {
3522 const char *p, *pattern = pi->path;
3524 if (strncmp(pattern, "**/", 3) != 0)
3525 continue;
3526 pattern += 3;
3527 p = path;
3528 while (*p) {
3529 if (fnmatch(pattern, p,
3530 FNM_PATHNAME | FNM_LEADING_DIR)) {
3531 /* Retry in next directory. */
3532 while (*p && *p != '/')
3533 p++;
3534 while (*p == '/')
3535 p++;
3536 continue;
3538 return 1;
3544 * The ignores pathlist contains ignore lists from children before
3545 * parents, so we can find the most specific ignorelist by walking
3546 * ignores backwards.
3548 pe = TAILQ_LAST(ignores, got_pathlist_head);
3549 while (pe) {
3550 if (got_path_is_child(path, pe->path, pe->path_len)) {
3551 struct got_pathlist_head *ignorelist = pe->data;
3552 struct got_pathlist_entry *pi;
3553 TAILQ_FOREACH(pi, ignorelist, entry) {
3554 const char *pattern = pi->path;
3555 int flags = FNM_LEADING_DIR;
3556 if (strstr(pattern, "/**/") == NULL)
3557 flags |= FNM_PATHNAME;
3558 if (fnmatch(pattern, path, flags))
3559 continue;
3560 return 1;
3563 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3566 return 0;
3569 static const struct got_error *
3570 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3571 const char *path, int dirfd, const char *ignores_filename)
3573 const struct got_error *err = NULL;
3574 char *ignorespath;
3575 int fd = -1;
3576 FILE *ignoresfile = NULL;
3578 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3579 path[0] ? "/" : "", ignores_filename) == -1)
3580 return got_error_from_errno("asprintf");
3582 if (dirfd != -1) {
3583 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3584 if (fd == -1) {
3585 if (errno != ENOENT && errno != EACCES)
3586 err = got_error_from_errno2("openat",
3587 ignorespath);
3588 } else {
3589 ignoresfile = fdopen(fd, "r");
3590 if (ignoresfile == NULL)
3591 err = got_error_from_errno2("fdopen",
3592 ignorespath);
3593 else {
3594 fd = -1;
3595 err = read_ignores(ignores, path, ignoresfile);
3598 } else {
3599 ignoresfile = fopen(ignorespath, "r");
3600 if (ignoresfile == NULL) {
3601 if (errno != ENOENT && errno != EACCES)
3602 err = got_error_from_errno2("fopen",
3603 ignorespath);
3604 } else
3605 err = read_ignores(ignores, path, ignoresfile);
3608 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3609 err = got_error_from_errno2("fclose", path);
3610 if (fd != -1 && close(fd) == -1 && err == NULL)
3611 err = got_error_from_errno2("close", path);
3612 free(ignorespath);
3613 return err;
3616 static const struct got_error *
3617 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3619 const struct got_error *err = NULL;
3620 struct diff_dir_cb_arg *a = arg;
3621 char *path = NULL;
3623 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3624 return got_error(GOT_ERR_CANCELLED);
3626 if (parent_path[0]) {
3627 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3628 return got_error_from_errno("asprintf");
3629 } else {
3630 path = de->d_name;
3633 if (de->d_type != DT_DIR &&
3634 got_path_is_child(path, a->status_path, a->status_path_len)
3635 && !match_ignores(a->ignores, path))
3636 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3637 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3638 if (parent_path[0])
3639 free(path);
3640 return err;
3643 static const struct got_error *
3644 status_traverse(void *arg, const char *path, int dirfd)
3646 const struct got_error *err = NULL;
3647 struct diff_dir_cb_arg *a = arg;
3649 if (a->no_ignores)
3650 return NULL;
3652 err = add_ignores(a->ignores, a->worktree->root_path,
3653 path, dirfd, ".cvsignore");
3654 if (err)
3655 return err;
3657 err = add_ignores(a->ignores, a->worktree->root_path, path,
3658 dirfd, ".gitignore");
3660 return err;
3663 static const struct got_error *
3664 report_single_file_status(const char *path, const char *ondisk_path,
3665 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3666 void *status_arg, struct got_repository *repo, int report_unchanged,
3667 struct got_pathlist_head *ignores, int no_ignores)
3669 struct got_fileindex_entry *ie;
3670 struct stat sb;
3672 if (!no_ignores && match_ignores(ignores, path))
3673 return NULL;
3675 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3676 if (ie)
3677 return report_file_status(ie, ondisk_path, -1, NULL,
3678 status_cb, status_arg, repo, report_unchanged);
3680 if (lstat(ondisk_path, &sb) == -1) {
3681 if (errno != ENOENT)
3682 return got_error_from_errno2("lstat", ondisk_path);
3683 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3684 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3685 return NULL;
3688 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3689 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3690 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3692 return NULL;
3695 static const struct got_error *
3696 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3697 const char *root_path, const char *path)
3699 const struct got_error *err;
3700 char *parent_path, *next_parent_path = NULL;
3702 err = add_ignores(ignores, root_path, "", -1,
3703 ".cvsignore");
3704 if (err)
3705 return err;
3707 err = add_ignores(ignores, root_path, "", -1,
3708 ".gitignore");
3709 if (err)
3710 return err;
3712 err = got_path_dirname(&parent_path, path);
3713 if (err) {
3714 if (err->code == GOT_ERR_BAD_PATH)
3715 return NULL; /* cannot traverse parent */
3716 return err;
3718 for (;;) {
3719 err = add_ignores(ignores, root_path, parent_path, -1,
3720 ".cvsignore");
3721 if (err)
3722 break;
3723 err = add_ignores(ignores, root_path, parent_path, -1,
3724 ".gitignore");
3725 if (err)
3726 break;
3727 err = got_path_dirname(&next_parent_path, parent_path);
3728 if (err) {
3729 if (err->code == GOT_ERR_BAD_PATH)
3730 err = NULL; /* traversed everything */
3731 break;
3733 if (got_path_is_root_dir(parent_path))
3734 break;
3735 free(parent_path);
3736 parent_path = next_parent_path;
3737 next_parent_path = NULL;
3740 free(parent_path);
3741 free(next_parent_path);
3742 return err;
3745 static const struct got_error *
3746 worktree_status(struct got_worktree *worktree, const char *path,
3747 struct got_fileindex *fileindex, struct got_repository *repo,
3748 got_worktree_status_cb status_cb, void *status_arg,
3749 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3750 int report_unchanged)
3752 const struct got_error *err = NULL;
3753 int fd = -1;
3754 struct got_fileindex_diff_dir_cb fdiff_cb;
3755 struct diff_dir_cb_arg arg;
3756 char *ondisk_path = NULL;
3757 struct got_pathlist_head ignores;
3759 TAILQ_INIT(&ignores);
3761 if (asprintf(&ondisk_path, "%s%s%s",
3762 worktree->root_path, path[0] ? "/" : "", path) == -1)
3763 return got_error_from_errno("asprintf");
3765 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3766 if (fd == -1) {
3767 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3768 errno != ELOOP)
3769 err = got_error_from_errno2("open", ondisk_path);
3770 else {
3771 if (!no_ignores) {
3772 err = add_ignores_from_parent_paths(&ignores,
3773 worktree->root_path, ondisk_path);
3774 if (err)
3775 goto done;
3777 err = report_single_file_status(path, ondisk_path,
3778 fileindex, status_cb, status_arg, repo,
3779 report_unchanged, &ignores, no_ignores);
3781 } else {
3782 fdiff_cb.diff_old_new = status_old_new;
3783 fdiff_cb.diff_old = status_old;
3784 fdiff_cb.diff_new = status_new;
3785 fdiff_cb.diff_traverse = status_traverse;
3786 arg.fileindex = fileindex;
3787 arg.worktree = worktree;
3788 arg.status_path = path;
3789 arg.status_path_len = strlen(path);
3790 arg.repo = repo;
3791 arg.status_cb = status_cb;
3792 arg.status_arg = status_arg;
3793 arg.cancel_cb = cancel_cb;
3794 arg.cancel_arg = cancel_arg;
3795 arg.report_unchanged = report_unchanged;
3796 arg.no_ignores = no_ignores;
3797 if (!no_ignores) {
3798 err = add_ignores_from_parent_paths(&ignores,
3799 worktree->root_path, path);
3800 if (err)
3801 goto done;
3803 arg.ignores = &ignores;
3804 err = got_fileindex_diff_dir(fileindex, fd,
3805 worktree->root_path, path, repo, &fdiff_cb, &arg);
3807 done:
3808 free_ignores(&ignores);
3809 if (fd != -1 && close(fd) == -1 && err == NULL)
3810 err = got_error_from_errno("close");
3811 free(ondisk_path);
3812 return err;
3815 const struct got_error *
3816 got_worktree_status(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, struct got_repository *repo,
3818 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3819 got_cancel_cb cancel_cb, void *cancel_arg)
3821 const struct got_error *err = NULL;
3822 char *fileindex_path = NULL;
3823 struct got_fileindex *fileindex = NULL;
3824 struct got_pathlist_entry *pe;
3826 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3827 if (err)
3828 return err;
3830 TAILQ_FOREACH(pe, paths, entry) {
3831 err = worktree_status(worktree, pe->path, fileindex, repo,
3832 status_cb, status_arg, cancel_cb, cancel_arg,
3833 no_ignores, 0);
3834 if (err)
3835 break;
3837 free(fileindex_path);
3838 got_fileindex_free(fileindex);
3839 return err;
3842 const struct got_error *
3843 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3844 const char *arg)
3846 const struct got_error *err = NULL;
3847 char *resolved = NULL, *cwd = NULL, *path = NULL;
3848 size_t len;
3849 struct stat sb;
3850 char *abspath = NULL;
3851 char canonpath[PATH_MAX];
3853 *wt_path = NULL;
3855 cwd = getcwd(NULL, 0);
3856 if (cwd == NULL)
3857 return got_error_from_errno("getcwd");
3859 if (lstat(arg, &sb) == -1) {
3860 if (errno != ENOENT) {
3861 err = got_error_from_errno2("lstat", arg);
3862 goto done;
3864 sb.st_mode = 0;
3866 if (S_ISLNK(sb.st_mode)) {
3868 * We cannot use realpath(3) with symlinks since we want to
3869 * operate on the symlink itself.
3870 * But we can make the path absolute, assuming it is relative
3871 * to the current working directory, and then canonicalize it.
3873 if (!got_path_is_absolute(arg)) {
3874 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3875 err = got_error_from_errno("asprintf");
3876 goto done;
3880 err = got_canonpath(abspath ? abspath : arg, canonpath,
3881 sizeof(canonpath));
3882 if (err)
3883 goto done;
3884 resolved = strdup(canonpath);
3885 if (resolved == NULL) {
3886 err = got_error_from_errno("strdup");
3887 goto done;
3889 } else {
3890 resolved = realpath(arg, NULL);
3891 if (resolved == NULL) {
3892 if (errno != ENOENT) {
3893 err = got_error_from_errno2("realpath", arg);
3894 goto done;
3896 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3897 err = got_error_from_errno("asprintf");
3898 goto done;
3900 err = got_canonpath(abspath, canonpath,
3901 sizeof(canonpath));
3902 if (err)
3903 goto done;
3904 resolved = strdup(canonpath);
3905 if (resolved == NULL) {
3906 err = got_error_from_errno("strdup");
3907 goto done;
3912 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3913 strlen(got_worktree_get_root_path(worktree)))) {
3914 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3915 goto done;
3918 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3919 err = got_path_skip_common_ancestor(&path,
3920 got_worktree_get_root_path(worktree), resolved);
3921 if (err)
3922 goto done;
3923 } else {
3924 path = strdup("");
3925 if (path == NULL) {
3926 err = got_error_from_errno("strdup");
3927 goto done;
3931 /* XXX status walk can't deal with trailing slash! */
3932 len = strlen(path);
3933 while (len > 0 && path[len - 1] == '/') {
3934 path[len - 1] = '\0';
3935 len--;
3937 done:
3938 free(abspath);
3939 free(resolved);
3940 free(cwd);
3941 if (err == NULL)
3942 *wt_path = path;
3943 else
3944 free(path);
3945 return err;
3948 struct schedule_addition_args {
3949 struct got_worktree *worktree;
3950 struct got_fileindex *fileindex;
3951 got_worktree_checkout_cb progress_cb;
3952 void *progress_arg;
3953 struct got_repository *repo;
3956 static const struct got_error *
3957 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3958 const char *relpath, struct got_object_id *blob_id,
3959 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3960 int dirfd, const char *de_name)
3962 struct schedule_addition_args *a = arg;
3963 const struct got_error *err = NULL;
3964 struct got_fileindex_entry *ie;
3965 struct stat sb;
3966 char *ondisk_path;
3968 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3969 relpath) == -1)
3970 return got_error_from_errno("asprintf");
3972 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3973 if (ie) {
3974 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3975 de_name, a->repo);
3976 if (err)
3977 goto done;
3978 /* Re-adding an existing entry is a no-op. */
3979 if (status == GOT_STATUS_ADD)
3980 goto done;
3981 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3982 if (err)
3983 goto done;
3986 if (status != GOT_STATUS_UNVERSIONED) {
3987 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3988 goto done;
3991 err = got_fileindex_entry_alloc(&ie, relpath);
3992 if (err)
3993 goto done;
3994 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3995 relpath, NULL, NULL, 1);
3996 if (err) {
3997 got_fileindex_entry_free(ie);
3998 goto done;
4000 err = got_fileindex_entry_add(a->fileindex, ie);
4001 if (err) {
4002 got_fileindex_entry_free(ie);
4003 goto done;
4005 done:
4006 free(ondisk_path);
4007 if (err)
4008 return err;
4009 if (status == GOT_STATUS_ADD)
4010 return NULL;
4011 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4014 const struct got_error *
4015 got_worktree_schedule_add(struct got_worktree *worktree,
4016 struct got_pathlist_head *paths,
4017 got_worktree_checkout_cb progress_cb, void *progress_arg,
4018 struct got_repository *repo, int no_ignores)
4020 struct got_fileindex *fileindex = NULL;
4021 char *fileindex_path = NULL;
4022 const struct got_error *err = NULL, *sync_err, *unlockerr;
4023 struct got_pathlist_entry *pe;
4024 struct schedule_addition_args saa;
4026 err = lock_worktree(worktree, LOCK_EX);
4027 if (err)
4028 return err;
4030 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4031 if (err)
4032 goto done;
4034 saa.worktree = worktree;
4035 saa.fileindex = fileindex;
4036 saa.progress_cb = progress_cb;
4037 saa.progress_arg = progress_arg;
4038 saa.repo = repo;
4040 TAILQ_FOREACH(pe, paths, entry) {
4041 err = worktree_status(worktree, pe->path, fileindex, repo,
4042 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4043 if (err)
4044 break;
4046 sync_err = sync_fileindex(fileindex, fileindex_path);
4047 if (sync_err && err == NULL)
4048 err = sync_err;
4049 done:
4050 free(fileindex_path);
4051 if (fileindex)
4052 got_fileindex_free(fileindex);
4053 unlockerr = lock_worktree(worktree, LOCK_SH);
4054 if (unlockerr && err == NULL)
4055 err = unlockerr;
4056 return err;
4059 struct schedule_deletion_args {
4060 struct got_worktree *worktree;
4061 struct got_fileindex *fileindex;
4062 got_worktree_delete_cb progress_cb;
4063 void *progress_arg;
4064 struct got_repository *repo;
4065 int delete_local_mods;
4066 int keep_on_disk;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4079 struct stat sb;
4080 char *ondisk_path;
4082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4083 if (ie == NULL)
4084 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4086 staged_status = get_staged_status(ie);
4087 if (staged_status != GOT_STATUS_NO_CHANGE) {
4088 if (staged_status == GOT_STATUS_DELETE)
4089 return NULL;
4090 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4093 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4094 relpath) == -1)
4095 return got_error_from_errno("asprintf");
4097 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4098 a->repo);
4099 if (err)
4100 goto done;
4102 if (a->status_codes) {
4103 size_t ncodes = strlen(a->status_codes);
4104 int i;
4105 for (i = 0; i < ncodes ; i++) {
4106 if (status == a->status_codes[i])
4107 break;
4109 if (i == ncodes) {
4110 /* Do not delete files in non-matching status. */
4111 free(ondisk_path);
4112 return NULL;
4114 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4115 a->status_codes[i] != GOT_STATUS_MISSING) {
4116 static char msg[64];
4117 snprintf(msg, sizeof(msg),
4118 "invalid status code '%c'", a->status_codes[i]);
4119 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4120 goto done;
4124 if (status != GOT_STATUS_NO_CHANGE) {
4125 if (status == GOT_STATUS_DELETE)
4126 goto done;
4127 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4128 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4129 goto done;
4131 if (status != GOT_STATUS_MODIFY &&
4132 status != GOT_STATUS_MISSING) {
4133 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4134 goto done;
4138 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4139 size_t root_len;
4141 if (dirfd != -1) {
4142 if (unlinkat(dirfd, de_name, 0) != 0) {
4143 err = got_error_from_errno2("unlinkat",
4144 ondisk_path);
4145 goto done;
4147 } else if (unlink(ondisk_path) != 0) {
4148 err = got_error_from_errno2("unlink", ondisk_path);
4149 goto done;
4152 root_len = strlen(a->worktree->root_path);
4153 do {
4154 char *parent;
4155 err = got_path_dirname(&parent, ondisk_path);
4156 if (err)
4157 goto done;
4158 free(ondisk_path);
4159 ondisk_path = parent;
4160 if (rmdir(ondisk_path) == -1) {
4161 if (errno != ENOTEMPTY)
4162 err = got_error_from_errno2("rmdir",
4163 ondisk_path);
4164 break;
4166 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4167 strlen(ondisk_path), root_len) != 0);
4170 got_fileindex_entry_mark_deleted_from_disk(ie);
4171 done:
4172 free(ondisk_path);
4173 if (err)
4174 return err;
4175 if (status == GOT_STATUS_DELETE)
4176 return NULL;
4177 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4178 staged_status, relpath);
4181 const struct got_error *
4182 got_worktree_schedule_delete(struct got_worktree *worktree,
4183 struct got_pathlist_head *paths, int delete_local_mods,
4184 const char *status_codes,
4185 got_worktree_delete_cb progress_cb, void *progress_arg,
4186 struct got_repository *repo, int keep_on_disk)
4188 struct got_fileindex *fileindex = NULL;
4189 char *fileindex_path = NULL;
4190 const struct got_error *err = NULL, *sync_err, *unlockerr;
4191 struct got_pathlist_entry *pe;
4192 struct schedule_deletion_args sda;
4194 err = lock_worktree(worktree, LOCK_EX);
4195 if (err)
4196 return err;
4198 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4199 if (err)
4200 goto done;
4202 sda.worktree = worktree;
4203 sda.fileindex = fileindex;
4204 sda.progress_cb = progress_cb;
4205 sda.progress_arg = progress_arg;
4206 sda.repo = repo;
4207 sda.delete_local_mods = delete_local_mods;
4208 sda.keep_on_disk = keep_on_disk;
4209 sda.status_codes = status_codes;
4211 TAILQ_FOREACH(pe, paths, entry) {
4212 err = worktree_status(worktree, pe->path, fileindex, repo,
4213 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4214 if (err)
4215 break;
4217 sync_err = sync_fileindex(fileindex, fileindex_path);
4218 if (sync_err && err == NULL)
4219 err = sync_err;
4220 done:
4221 free(fileindex_path);
4222 if (fileindex)
4223 got_fileindex_free(fileindex);
4224 unlockerr = lock_worktree(worktree, LOCK_SH);
4225 if (unlockerr && err == NULL)
4226 err = unlockerr;
4227 return err;
4230 static const struct got_error *
4231 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4233 const struct got_error *err = NULL;
4234 char *line = NULL;
4235 size_t linesize = 0, n;
4236 ssize_t linelen;
4238 linelen = getline(&line, &linesize, infile);
4239 if (linelen == -1) {
4240 if (ferror(infile)) {
4241 err = got_error_from_errno("getline");
4242 goto done;
4244 return NULL;
4246 if (outfile) {
4247 n = fwrite(line, 1, linelen, outfile);
4248 if (n != linelen) {
4249 err = got_ferror(outfile, GOT_ERR_IO);
4250 goto done;
4253 if (rejectfile) {
4254 n = fwrite(line, 1, linelen, rejectfile);
4255 if (n != linelen)
4256 err = got_ferror(outfile, GOT_ERR_IO);
4258 done:
4259 free(line);
4260 return err;
4263 static const struct got_error *
4264 skip_one_line(FILE *f)
4266 char *line = NULL;
4267 size_t linesize = 0;
4268 ssize_t linelen;
4270 linelen = getline(&line, &linesize, f);
4271 if (linelen == -1) {
4272 if (ferror(f))
4273 return got_error_from_errno("getline");
4274 return NULL;
4276 free(line);
4277 return NULL;
4280 static const struct got_error *
4281 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4282 int start_old, int end_old, int start_new, int end_new,
4283 FILE *outfile, FILE *rejectfile)
4285 const struct got_error *err;
4287 /* Copy old file's lines leading up to patch. */
4288 while (!feof(f1) && *line_cur1 < start_old) {
4289 err = copy_one_line(f1, outfile, NULL);
4290 if (err)
4291 return err;
4292 (*line_cur1)++;
4294 /* Skip new file's lines leading up to patch. */
4295 while (!feof(f2) && *line_cur2 < start_new) {
4296 if (rejectfile)
4297 err = copy_one_line(f2, NULL, rejectfile);
4298 else
4299 err = skip_one_line(f2);
4300 if (err)
4301 return err;
4302 (*line_cur2)++;
4304 /* Copy patched lines. */
4305 while (!feof(f2) && *line_cur2 <= end_new) {
4306 err = copy_one_line(f2, outfile, NULL);
4307 if (err)
4308 return err;
4309 (*line_cur2)++;
4311 /* Skip over old file's replaced lines. */
4312 while (!feof(f1) && *line_cur1 <= end_old) {
4313 if (rejectfile)
4314 err = copy_one_line(f1, NULL, rejectfile);
4315 else
4316 err = skip_one_line(f1);
4317 if (err)
4318 return err;
4319 (*line_cur1)++;
4322 return NULL;
4325 static const struct got_error *
4326 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4327 FILE *outfile, FILE *rejectfile)
4329 const struct got_error *err;
4331 if (outfile) {
4332 /* Copy old file's lines until EOF. */
4333 while (!feof(f1)) {
4334 err = copy_one_line(f1, outfile, NULL);
4335 if (err)
4336 return err;
4337 (*line_cur1)++;
4340 if (rejectfile) {
4341 /* Copy new file's lines until EOF. */
4342 while (!feof(f2)) {
4343 err = copy_one_line(f2, NULL, rejectfile);
4344 if (err)
4345 return err;
4346 (*line_cur2)++;
4350 return NULL;
4353 static const struct got_error *
4354 apply_or_reject_change(int *choice, int *nchunks_used,
4355 struct diff_result *diff_result, int n,
4356 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4357 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4358 got_worktree_patch_cb patch_cb, void *patch_arg)
4360 const struct got_error *err = NULL;
4361 struct diff_chunk_context cc = {};
4362 int start_old, end_old, start_new, end_new;
4363 FILE *hunkfile;
4364 struct diff_output_unidiff_state *diff_state;
4365 struct diff_input_info diff_info;
4366 int rc;
4368 *choice = GOT_PATCH_CHOICE_NONE;
4370 /* Get changed line numbers without context lines for copy_change(). */
4371 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4372 start_old = cc.left.start;
4373 end_old = cc.left.end;
4374 start_new = cc.right.start;
4375 end_new = cc.right.end;
4377 /* Get the same change with context lines for display. */
4378 memset(&cc, 0, sizeof(cc));
4379 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4381 memset(&diff_info, 0, sizeof(diff_info));
4382 diff_info.left_path = relpath;
4383 diff_info.right_path = relpath;
4385 diff_state = diff_output_unidiff_state_alloc();
4386 if (diff_state == NULL)
4387 return got_error_set_errno(ENOMEM,
4388 "diff_output_unidiff_state_alloc");
4390 hunkfile = got_opentemp();
4391 if (hunkfile == NULL) {
4392 err = got_error_from_errno("got_opentemp");
4393 goto done;
4396 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4397 diff_result, &cc);
4398 if (rc != DIFF_RC_OK) {
4399 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4400 goto done;
4403 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4404 err = got_ferror(hunkfile, GOT_ERR_IO);
4405 goto done;
4408 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4409 hunkfile, changeno, nchanges);
4410 if (err)
4411 goto done;
4413 switch (*choice) {
4414 case GOT_PATCH_CHOICE_YES:
4415 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4416 end_old, start_new, end_new, outfile, rejectfile);
4417 break;
4418 case GOT_PATCH_CHOICE_NO:
4419 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4420 end_old, start_new, end_new, rejectfile, outfile);
4421 break;
4422 case GOT_PATCH_CHOICE_QUIT:
4423 break;
4424 default:
4425 err = got_error(GOT_ERR_PATCH_CHOICE);
4426 break;
4428 done:
4429 diff_output_unidiff_state_free(diff_state);
4430 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4431 err = got_error_from_errno("fclose");
4432 return err;
4435 struct revert_file_args {
4436 struct got_worktree *worktree;
4437 struct got_fileindex *fileindex;
4438 got_worktree_checkout_cb progress_cb;
4439 void *progress_arg;
4440 got_worktree_patch_cb patch_cb;
4441 void *patch_arg;
4442 struct got_repository *repo;
4443 int unlink_added_files;
4446 static const struct got_error *
4447 create_patched_content(char **path_outfile, int reverse_patch,
4448 struct got_object_id *blob_id, const char *path2,
4449 int dirfd2, const char *de_name2,
4450 const char *relpath, struct got_repository *repo,
4451 got_worktree_patch_cb patch_cb, void *patch_arg)
4453 const struct got_error *err, *free_err;
4454 struct got_blob_object *blob = NULL;
4455 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4456 int fd2 = -1;
4457 char link_target[PATH_MAX];
4458 ssize_t link_len = 0;
4459 char *path1 = NULL, *id_str = NULL;
4460 struct stat sb2;
4461 struct got_diffreg_result *diffreg_result = NULL;
4462 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4463 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4465 *path_outfile = NULL;
4467 err = got_object_id_str(&id_str, blob_id);
4468 if (err)
4469 return err;
4471 if (dirfd2 != -1) {
4472 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4473 if (fd2 == -1) {
4474 if (errno != ELOOP) {
4475 err = got_error_from_errno2("openat", path2);
4476 goto done;
4478 link_len = readlinkat(dirfd2, de_name2,
4479 link_target, sizeof(link_target));
4480 if (link_len == -1)
4481 return got_error_from_errno2("readlinkat", path2);
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4485 } else {
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4487 if (fd2 == -1) {
4488 if (errno != ELOOP) {
4489 err = got_error_from_errno2("open", path2);
4490 goto done;
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4494 if (link_len == -1)
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 if (fd2 != -1) {
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4503 goto done;
4506 f2 = fdopen(fd2, "r");
4507 if (f2 == NULL) {
4508 err = got_error_from_errno2("fdopen", path2);
4509 goto done;
4511 fd2 = -1;
4512 } else {
4513 size_t n;
4514 f2 = got_opentemp();
4515 if (f2 == NULL) {
4516 err = got_error_from_errno2("got_opentemp", path2);
4517 goto done;
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4522 goto done;
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4526 goto done;
4528 rewind(f2);
4531 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4532 if (err)
4533 goto done;
4535 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4536 if (err)
4537 goto done;
4539 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4540 if (err)
4541 goto done;
4543 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4544 NULL);
4545 if (err)
4546 goto done;
4548 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4549 if (err)
4550 goto done;
4552 if (fseek(f1, 0L, SEEK_SET) == -1)
4553 return got_ferror(f1, GOT_ERR_IO);
4554 if (fseek(f2, 0L, SEEK_SET) == -1)
4555 return got_ferror(f2, GOT_ERR_IO);
4557 /* Count the number of actual changes in the diff result. */
4558 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4559 struct diff_chunk_context cc = {};
4560 diff_chunk_context_load_change(&cc, &nchunks_used,
4561 diffreg_result->result, n, 0);
4562 nchanges++;
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 int choice;
4566 err = apply_or_reject_change(&choice, &nchunks_used,
4567 diffreg_result->result, n, relpath, f1, f2,
4568 &line_cur1, &line_cur2,
4569 reverse_patch ? NULL : outfile,
4570 reverse_patch ? outfile : NULL,
4571 ++i, nchanges, patch_cb, patch_arg);
4572 if (err)
4573 goto done;
4574 if (choice == GOT_PATCH_CHOICE_YES)
4575 have_content = 1;
4576 else if (choice == GOT_PATCH_CHOICE_QUIT)
4577 break;
4579 if (have_content) {
4580 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4581 reverse_patch ? NULL : outfile,
4582 reverse_patch ? outfile : NULL);
4583 if (err)
4584 goto done;
4586 if (!S_ISLNK(sb2.st_mode)) {
4587 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4588 err = got_error_from_errno2("fchmod", path2);
4589 goto done;
4593 done:
4594 free(id_str);
4595 if (blob)
4596 got_object_blob_close(blob);
4597 free_err = got_diffreg_result_free(diffreg_result);
4598 if (err == NULL)
4599 err = free_err;
4600 if (f1 && fclose(f1) == EOF && err == NULL)
4601 err = got_error_from_errno2("fclose", path1);
4602 if (f2 && fclose(f2) == EOF && err == NULL)
4603 err = got_error_from_errno2("fclose", path2);
4604 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4605 err = got_error_from_errno2("close", path2);
4606 if (outfile && fclose(outfile) == EOF && err == NULL)
4607 err = got_error_from_errno2("fclose", *path_outfile);
4608 if (path1 && unlink(path1) == -1 && err == NULL)
4609 err = got_error_from_errno2("unlink", path1);
4610 if (err || !have_content) {
4611 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4612 err = got_error_from_errno2("unlink", *path_outfile);
4613 free(*path_outfile);
4614 *path_outfile = NULL;
4616 free(path1);
4617 return err;
4620 static const struct got_error *
4621 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4622 const char *relpath, struct got_object_id *blob_id,
4623 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4624 int dirfd, const char *de_name)
4626 struct revert_file_args *a = arg;
4627 const struct got_error *err = NULL;
4628 char *parent_path = NULL;
4629 struct got_fileindex_entry *ie;
4630 struct got_tree_object *tree = NULL;
4631 struct got_object_id *tree_id = NULL;
4632 const struct got_tree_entry *te = NULL;
4633 char *tree_path = NULL, *te_name;
4634 char *ondisk_path = NULL, *path_content = NULL;
4635 struct got_blob_object *blob = NULL;
4637 /* Reverting a staged deletion is a no-op. */
4638 if (status == GOT_STATUS_DELETE &&
4639 staged_status != GOT_STATUS_NO_CHANGE)
4640 return NULL;
4642 if (status == GOT_STATUS_UNVERSIONED)
4643 return (*a->progress_cb)(a->progress_arg,
4644 GOT_STATUS_UNVERSIONED, relpath);
4646 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4647 if (ie == NULL)
4648 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4650 /* Construct in-repository path of tree which contains this blob. */
4651 err = got_path_dirname(&parent_path, ie->path);
4652 if (err) {
4653 if (err->code != GOT_ERR_BAD_PATH)
4654 goto done;
4655 parent_path = strdup("/");
4656 if (parent_path == NULL) {
4657 err = got_error_from_errno("strdup");
4658 goto done;
4661 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4662 tree_path = strdup(parent_path);
4663 if (tree_path == NULL) {
4664 err = got_error_from_errno("strdup");
4665 goto done;
4667 } else {
4668 if (got_path_is_root_dir(parent_path)) {
4669 tree_path = strdup(a->worktree->path_prefix);
4670 if (tree_path == NULL) {
4671 err = got_error_from_errno("strdup");
4672 goto done;
4674 } else {
4675 if (asprintf(&tree_path, "%s/%s",
4676 a->worktree->path_prefix, parent_path) == -1) {
4677 err = got_error_from_errno("asprintf");
4678 goto done;
4683 err = got_object_id_by_path(&tree_id, a->repo,
4684 a->worktree->base_commit_id, tree_path);
4685 if (err) {
4686 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4687 (status == GOT_STATUS_ADD ||
4688 staged_status == GOT_STATUS_ADD)))
4689 goto done;
4690 } else {
4691 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4692 if (err)
4693 goto done;
4695 err = got_path_basename(&te_name, ie->path);
4696 if (err)
4697 goto done;
4699 te = got_object_tree_find_entry(tree, te_name);
4700 free(te_name);
4701 if (te == NULL && status != GOT_STATUS_ADD &&
4702 staged_status != GOT_STATUS_ADD) {
4703 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4704 goto done;
4708 switch (status) {
4709 case GOT_STATUS_ADD:
4710 if (a->patch_cb) {
4711 int choice = GOT_PATCH_CHOICE_NONE;
4712 err = (*a->patch_cb)(&choice, a->patch_arg,
4713 status, ie->path, NULL, 1, 1);
4714 if (err)
4715 goto done;
4716 if (choice != GOT_PATCH_CHOICE_YES)
4717 break;
4719 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4720 ie->path);
4721 if (err)
4722 goto done;
4723 got_fileindex_entry_remove(a->fileindex, ie);
4724 if (a->unlink_added_files) {
4725 if (asprintf(&ondisk_path, "%s/%s",
4726 got_worktree_get_root_path(a->worktree),
4727 relpath) == -1) {
4728 err = got_error_from_errno("asprintf");
4729 goto done;
4731 if (unlink(ondisk_path) == -1) {
4732 err = got_error_from_errno2("unlink",
4733 ondisk_path);
4734 break;
4737 break;
4738 case GOT_STATUS_DELETE:
4739 if (a->patch_cb) {
4740 int choice = GOT_PATCH_CHOICE_NONE;
4741 err = (*a->patch_cb)(&choice, a->patch_arg,
4742 status, ie->path, NULL, 1, 1);
4743 if (err)
4744 goto done;
4745 if (choice != GOT_PATCH_CHOICE_YES)
4746 break;
4748 /* fall through */
4749 case GOT_STATUS_MODIFY:
4750 case GOT_STATUS_MODE_CHANGE:
4751 case GOT_STATUS_CONFLICT:
4752 case GOT_STATUS_MISSING: {
4753 struct got_object_id id;
4754 if (staged_status == GOT_STATUS_ADD ||
4755 staged_status == GOT_STATUS_MODIFY) {
4756 memcpy(id.sha1, ie->staged_blob_sha1,
4757 SHA1_DIGEST_LENGTH);
4758 } else
4759 memcpy(id.sha1, ie->blob_sha1,
4760 SHA1_DIGEST_LENGTH);
4761 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4762 if (err)
4763 goto done;
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4767 err = got_error_from_errno("asprintf");
4768 goto done;
4771 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4772 status == GOT_STATUS_CONFLICT)) {
4773 int is_bad_symlink = 0;
4774 err = create_patched_content(&path_content, 1, &id,
4775 ondisk_path, dirfd, de_name, ie->path, a->repo,
4776 a->patch_cb, a->patch_arg);
4777 if (err || path_content == NULL)
4778 break;
4779 if (te && S_ISLNK(te->mode)) {
4780 if (unlink(path_content) == -1) {
4781 err = got_error_from_errno2("unlink",
4782 path_content);
4783 break;
4785 err = install_symlink(&is_bad_symlink,
4786 a->worktree, ondisk_path, ie->path,
4787 blob, 0, 1, 0, 0, a->repo,
4788 a->progress_cb, a->progress_arg);
4789 } else {
4790 if (rename(path_content, ondisk_path) == -1) {
4791 err = got_error_from_errno3("rename",
4792 path_content, ondisk_path);
4793 goto done;
4796 } else {
4797 int is_bad_symlink = 0;
4798 if (te && S_ISLNK(te->mode)) {
4799 err = install_symlink(&is_bad_symlink,
4800 a->worktree, ondisk_path, ie->path,
4801 blob, 0, 1, 0, 0, a->repo,
4802 a->progress_cb, a->progress_arg);
4803 } else {
4804 err = install_blob(a->worktree, ondisk_path,
4805 ie->path,
4806 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4807 got_fileindex_perms_to_st(ie), blob,
4808 0, 1, 0, 0, a->repo,
4809 a->progress_cb, a->progress_arg);
4811 if (err)
4812 goto done;
4813 if (status == GOT_STATUS_DELETE ||
4814 status == GOT_STATUS_MODE_CHANGE) {
4815 err = got_fileindex_entry_update(ie,
4816 a->worktree->root_fd, relpath,
4817 blob->id.sha1,
4818 a->worktree->base_commit_id->sha1, 1);
4819 if (err)
4820 goto done;
4822 if (is_bad_symlink) {
4823 got_fileindex_entry_filetype_set(ie,
4824 GOT_FILEIDX_MODE_BAD_SYMLINK);
4827 break;
4829 default:
4830 break;
4832 done:
4833 free(ondisk_path);
4834 free(path_content);
4835 free(parent_path);
4836 free(tree_path);
4837 if (blob)
4838 got_object_blob_close(blob);
4839 if (tree)
4840 got_object_tree_close(tree);
4841 free(tree_id);
4842 return err;
4845 const struct got_error *
4846 got_worktree_revert(struct got_worktree *worktree,
4847 struct got_pathlist_head *paths,
4848 got_worktree_checkout_cb progress_cb, void *progress_arg,
4849 got_worktree_patch_cb patch_cb, void *patch_arg,
4850 struct got_repository *repo)
4852 struct got_fileindex *fileindex = NULL;
4853 char *fileindex_path = NULL;
4854 const struct got_error *err = NULL, *unlockerr = NULL;
4855 const struct got_error *sync_err = NULL;
4856 struct got_pathlist_entry *pe;
4857 struct revert_file_args rfa;
4859 err = lock_worktree(worktree, LOCK_EX);
4860 if (err)
4861 return err;
4863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4864 if (err)
4865 goto done;
4867 rfa.worktree = worktree;
4868 rfa.fileindex = fileindex;
4869 rfa.progress_cb = progress_cb;
4870 rfa.progress_arg = progress_arg;
4871 rfa.patch_cb = patch_cb;
4872 rfa.patch_arg = patch_arg;
4873 rfa.repo = repo;
4874 rfa.unlink_added_files = 0;
4875 TAILQ_FOREACH(pe, paths, entry) {
4876 err = worktree_status(worktree, pe->path, fileindex, repo,
4877 revert_file, &rfa, NULL, NULL, 0, 0);
4878 if (err)
4879 break;
4881 sync_err = sync_fileindex(fileindex, fileindex_path);
4882 if (sync_err && err == NULL)
4883 err = sync_err;
4884 done:
4885 free(fileindex_path);
4886 if (fileindex)
4887 got_fileindex_free(fileindex);
4888 unlockerr = lock_worktree(worktree, LOCK_SH);
4889 if (unlockerr && err == NULL)
4890 err = unlockerr;
4891 return err;
4894 static void
4895 free_commitable(struct got_commitable *ct)
4897 free(ct->path);
4898 free(ct->in_repo_path);
4899 free(ct->ondisk_path);
4900 free(ct->blob_id);
4901 free(ct->base_blob_id);
4902 free(ct->staged_blob_id);
4903 free(ct->base_commit_id);
4904 free(ct);
4907 struct collect_commitables_arg {
4908 struct got_pathlist_head *commitable_paths;
4909 struct got_repository *repo;
4910 struct got_worktree *worktree;
4911 struct got_fileindex *fileindex;
4912 int have_staged_files;
4913 int allow_bad_symlinks;
4916 static const struct got_error *
4917 collect_commitables(void *arg, unsigned char status,
4918 unsigned char staged_status, const char *relpath,
4919 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4920 struct got_object_id *commit_id, int dirfd, const char *de_name)
4922 struct collect_commitables_arg *a = arg;
4923 const struct got_error *err = NULL;
4924 struct got_commitable *ct = NULL;
4925 struct got_pathlist_entry *new = NULL;
4926 char *parent_path = NULL, *path = NULL;
4927 struct stat sb;
4929 if (a->have_staged_files) {
4930 if (staged_status != GOT_STATUS_MODIFY &&
4931 staged_status != GOT_STATUS_ADD &&
4932 staged_status != GOT_STATUS_DELETE)
4933 return NULL;
4934 } else {
4935 if (status == GOT_STATUS_CONFLICT)
4936 return got_error(GOT_ERR_COMMIT_CONFLICT);
4938 if (status != GOT_STATUS_MODIFY &&
4939 status != GOT_STATUS_MODE_CHANGE &&
4940 status != GOT_STATUS_ADD &&
4941 status != GOT_STATUS_DELETE)
4942 return NULL;
4945 if (asprintf(&path, "/%s", relpath) == -1) {
4946 err = got_error_from_errno("asprintf");
4947 goto done;
4949 if (strcmp(path, "/") == 0) {
4950 parent_path = strdup("");
4951 if (parent_path == NULL)
4952 return got_error_from_errno("strdup");
4953 } else {
4954 err = got_path_dirname(&parent_path, path);
4955 if (err)
4956 return err;
4959 ct = calloc(1, sizeof(*ct));
4960 if (ct == NULL) {
4961 err = got_error_from_errno("calloc");
4962 goto done;
4965 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4966 relpath) == -1) {
4967 err = got_error_from_errno("asprintf");
4968 goto done;
4971 if (staged_status == GOT_STATUS_ADD ||
4972 staged_status == GOT_STATUS_MODIFY) {
4973 struct got_fileindex_entry *ie;
4974 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4975 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4976 case GOT_FILEIDX_MODE_REGULAR_FILE:
4977 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4978 ct->mode = S_IFREG;
4979 break;
4980 case GOT_FILEIDX_MODE_SYMLINK:
4981 ct->mode = S_IFLNK;
4982 break;
4983 default:
4984 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4985 goto done;
4987 ct->mode |= got_fileindex_entry_perms_get(ie);
4988 } else if (status != GOT_STATUS_DELETE &&
4989 staged_status != GOT_STATUS_DELETE) {
4990 if (dirfd != -1) {
4991 if (fstatat(dirfd, de_name, &sb,
4992 AT_SYMLINK_NOFOLLOW) == -1) {
4993 err = got_error_from_errno2("fstatat",
4994 ct->ondisk_path);
4995 goto done;
4997 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4998 err = got_error_from_errno2("lstat", ct->ondisk_path);
4999 goto done;
5001 ct->mode = sb.st_mode;
5004 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5005 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5006 relpath) == -1) {
5007 err = got_error_from_errno("asprintf");
5008 goto done;
5011 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5012 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5013 int is_bad_symlink;
5014 char target_path[PATH_MAX];
5015 ssize_t target_len;
5016 target_len = readlink(ct->ondisk_path, target_path,
5017 sizeof(target_path));
5018 if (target_len == -1) {
5019 err = got_error_from_errno2("readlink",
5020 ct->ondisk_path);
5021 goto done;
5023 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5024 target_len, ct->ondisk_path, a->worktree->root_path);
5025 if (err)
5026 goto done;
5027 if (is_bad_symlink) {
5028 err = got_error_path(ct->ondisk_path,
5029 GOT_ERR_BAD_SYMLINK);
5030 goto done;
5035 ct->status = status;
5036 ct->staged_status = staged_status;
5037 ct->blob_id = NULL; /* will be filled in when blob gets created */
5038 if (ct->status != GOT_STATUS_ADD &&
5039 ct->staged_status != GOT_STATUS_ADD) {
5040 ct->base_blob_id = got_object_id_dup(blob_id);
5041 if (ct->base_blob_id == NULL) {
5042 err = got_error_from_errno("got_object_id_dup");
5043 goto done;
5045 ct->base_commit_id = got_object_id_dup(commit_id);
5046 if (ct->base_commit_id == NULL) {
5047 err = got_error_from_errno("got_object_id_dup");
5048 goto done;
5051 if (ct->staged_status == GOT_STATUS_ADD ||
5052 ct->staged_status == GOT_STATUS_MODIFY) {
5053 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5054 if (ct->staged_blob_id == NULL) {
5055 err = got_error_from_errno("got_object_id_dup");
5056 goto done;
5059 ct->path = strdup(path);
5060 if (ct->path == NULL) {
5061 err = got_error_from_errno("strdup");
5062 goto done;
5064 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5065 done:
5066 if (ct && (err || new == NULL))
5067 free_commitable(ct);
5068 free(parent_path);
5069 free(path);
5070 return err;
5073 static const struct got_error *write_tree(struct got_object_id **, int *,
5074 struct got_tree_object *, const char *, struct got_pathlist_head *,
5075 got_worktree_status_cb status_cb, void *status_arg,
5076 struct got_repository *);
5078 static const struct got_error *
5079 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5080 struct got_tree_entry *te, const char *parent_path,
5081 struct got_pathlist_head *commitable_paths,
5082 got_worktree_status_cb status_cb, void *status_arg,
5083 struct got_repository *repo)
5085 const struct got_error *err = NULL;
5086 struct got_tree_object *subtree;
5087 char *subpath;
5089 if (asprintf(&subpath, "%s%s%s", parent_path,
5090 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5091 return got_error_from_errno("asprintf");
5093 err = got_object_open_as_tree(&subtree, repo, &te->id);
5094 if (err)
5095 return err;
5097 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5098 commitable_paths, status_cb, status_arg, repo);
5099 got_object_tree_close(subtree);
5100 free(subpath);
5101 return err;
5104 static const struct got_error *
5105 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5107 const struct got_error *err = NULL;
5108 char *ct_parent_path = NULL;
5110 *match = 0;
5112 if (strchr(ct->in_repo_path, '/') == NULL) {
5113 *match = got_path_is_root_dir(path);
5114 return NULL;
5117 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5118 if (err)
5119 return err;
5120 *match = (strcmp(path, ct_parent_path) == 0);
5121 free(ct_parent_path);
5122 return err;
5125 static mode_t
5126 get_ct_file_mode(struct got_commitable *ct)
5128 if (S_ISLNK(ct->mode))
5129 return S_IFLNK;
5131 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5134 static const struct got_error *
5135 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5136 struct got_tree_entry *te, struct got_commitable *ct)
5138 const struct got_error *err = NULL;
5140 *new_te = NULL;
5142 err = got_object_tree_entry_dup(new_te, te);
5143 if (err)
5144 goto done;
5146 (*new_te)->mode = get_ct_file_mode(ct);
5148 if (ct->staged_status == GOT_STATUS_MODIFY)
5149 memcpy(&(*new_te)->id, ct->staged_blob_id,
5150 sizeof((*new_te)->id));
5151 else
5152 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5153 done:
5154 if (err && *new_te) {
5155 free(*new_te);
5156 *new_te = NULL;
5158 return err;
5161 static const struct got_error *
5162 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5163 struct got_commitable *ct)
5165 const struct got_error *err = NULL;
5166 char *ct_name = NULL;
5168 *new_te = NULL;
5170 *new_te = calloc(1, sizeof(**new_te));
5171 if (*new_te == NULL)
5172 return got_error_from_errno("calloc");
5174 err = got_path_basename(&ct_name, ct->path);
5175 if (err)
5176 goto done;
5177 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5178 sizeof((*new_te)->name)) {
5179 err = got_error(GOT_ERR_NO_SPACE);
5180 goto done;
5183 (*new_te)->mode = get_ct_file_mode(ct);
5185 if (ct->staged_status == GOT_STATUS_ADD)
5186 memcpy(&(*new_te)->id, ct->staged_blob_id,
5187 sizeof((*new_te)->id));
5188 else
5189 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5190 done:
5191 free(ct_name);
5192 if (err && *new_te) {
5193 free(*new_te);
5194 *new_te = NULL;
5196 return err;
5199 static const struct got_error *
5200 insert_tree_entry(struct got_tree_entry *new_te,
5201 struct got_pathlist_head *paths)
5203 const struct got_error *err = NULL;
5204 struct got_pathlist_entry *new_pe;
5206 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5207 if (err)
5208 return err;
5209 if (new_pe == NULL)
5210 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5211 return NULL;
5214 static const struct got_error *
5215 report_ct_status(struct got_commitable *ct,
5216 got_worktree_status_cb status_cb, void *status_arg)
5218 const char *ct_path = ct->path;
5219 unsigned char status;
5221 while (ct_path[0] == '/')
5222 ct_path++;
5224 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5225 status = ct->staged_status;
5226 else
5227 status = ct->status;
5229 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5230 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5233 static const struct got_error *
5234 match_modified_subtree(int *modified, struct got_tree_entry *te,
5235 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5237 const struct got_error *err = NULL;
5238 struct got_pathlist_entry *pe;
5239 char *te_path;
5241 *modified = 0;
5243 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5244 got_path_is_root_dir(base_tree_path) ? "" : "/",
5245 te->name) == -1)
5246 return got_error_from_errno("asprintf");
5248 TAILQ_FOREACH(pe, commitable_paths, entry) {
5249 struct got_commitable *ct = pe->data;
5250 *modified = got_path_is_child(ct->in_repo_path, te_path,
5251 strlen(te_path));
5252 if (*modified)
5253 break;
5256 free(te_path);
5257 return err;
5260 static const struct got_error *
5261 match_deleted_or_modified_ct(struct got_commitable **ctp,
5262 struct got_tree_entry *te, const char *base_tree_path,
5263 struct got_pathlist_head *commitable_paths)
5265 const struct got_error *err = NULL;
5266 struct got_pathlist_entry *pe;
5268 *ctp = NULL;
5270 TAILQ_FOREACH(pe, commitable_paths, entry) {
5271 struct got_commitable *ct = pe->data;
5272 char *ct_name = NULL;
5273 int path_matches;
5275 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5276 if (ct->status != GOT_STATUS_MODIFY &&
5277 ct->status != GOT_STATUS_MODE_CHANGE &&
5278 ct->status != GOT_STATUS_DELETE)
5279 continue;
5280 } else {
5281 if (ct->staged_status != GOT_STATUS_MODIFY &&
5282 ct->staged_status != GOT_STATUS_DELETE)
5283 continue;
5286 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5287 continue;
5289 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5290 if (err)
5291 return err;
5292 if (!path_matches)
5293 continue;
5295 err = got_path_basename(&ct_name, pe->path);
5296 if (err)
5297 return err;
5299 if (strcmp(te->name, ct_name) != 0) {
5300 free(ct_name);
5301 continue;
5303 free(ct_name);
5305 *ctp = ct;
5306 break;
5309 return err;
5312 static const struct got_error *
5313 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5314 const char *child_path, const char *path_base_tree,
5315 struct got_pathlist_head *commitable_paths,
5316 got_worktree_status_cb status_cb, void *status_arg,
5317 struct got_repository *repo)
5319 const struct got_error *err = NULL;
5320 struct got_tree_entry *new_te;
5321 char *subtree_path;
5322 struct got_object_id *id = NULL;
5323 int nentries;
5325 *new_tep = NULL;
5327 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5328 got_path_is_root_dir(path_base_tree) ? "" : "/",
5329 child_path) == -1)
5330 return got_error_from_errno("asprintf");
5332 new_te = calloc(1, sizeof(*new_te));
5333 if (new_te == NULL)
5334 return got_error_from_errno("calloc");
5335 new_te->mode = S_IFDIR;
5337 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5338 sizeof(new_te->name)) {
5339 err = got_error(GOT_ERR_NO_SPACE);
5340 goto done;
5342 err = write_tree(&id, &nentries, NULL, subtree_path,
5343 commitable_paths, status_cb, status_arg, repo);
5344 if (err) {
5345 free(new_te);
5346 goto done;
5348 memcpy(&new_te->id, id, sizeof(new_te->id));
5349 done:
5350 free(id);
5351 free(subtree_path);
5352 if (err == NULL)
5353 *new_tep = new_te;
5354 return err;
5357 static const struct got_error *
5358 write_tree(struct got_object_id **new_tree_id, int *nentries,
5359 struct got_tree_object *base_tree, const char *path_base_tree,
5360 struct got_pathlist_head *commitable_paths,
5361 got_worktree_status_cb status_cb, void *status_arg,
5362 struct got_repository *repo)
5364 const struct got_error *err = NULL;
5365 struct got_pathlist_head paths;
5366 struct got_tree_entry *te, *new_te = NULL;
5367 struct got_pathlist_entry *pe;
5369 TAILQ_INIT(&paths);
5370 *nentries = 0;
5372 /* Insert, and recurse into, newly added entries first. */
5373 TAILQ_FOREACH(pe, commitable_paths, entry) {
5374 struct got_commitable *ct = pe->data;
5375 char *child_path = NULL, *slash;
5377 if ((ct->status != GOT_STATUS_ADD &&
5378 ct->staged_status != GOT_STATUS_ADD) ||
5379 (ct->flags & GOT_COMMITABLE_ADDED))
5380 continue;
5382 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5383 strlen(path_base_tree)))
5384 continue;
5386 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5387 ct->in_repo_path);
5388 if (err)
5389 goto done;
5391 slash = strchr(child_path, '/');
5392 if (slash == NULL) {
5393 err = alloc_added_blob_tree_entry(&new_te, ct);
5394 if (err)
5395 goto done;
5396 err = report_ct_status(ct, status_cb, status_arg);
5397 if (err)
5398 goto done;
5399 ct->flags |= GOT_COMMITABLE_ADDED;
5400 err = insert_tree_entry(new_te, &paths);
5401 if (err)
5402 goto done;
5403 (*nentries)++;
5404 } else {
5405 *slash = '\0'; /* trim trailing path components */
5406 if (base_tree == NULL ||
5407 got_object_tree_find_entry(base_tree, child_path)
5408 == NULL) {
5409 err = make_subtree_for_added_blob(&new_te,
5410 child_path, path_base_tree,
5411 commitable_paths, status_cb, status_arg,
5412 repo);
5413 if (err)
5414 goto done;
5415 err = insert_tree_entry(new_te, &paths);
5416 if (err)
5417 goto done;
5418 (*nentries)++;
5423 if (base_tree) {
5424 int i, nbase_entries;
5425 /* Handle modified and deleted entries. */
5426 nbase_entries = got_object_tree_get_nentries(base_tree);
5427 for (i = 0; i < nbase_entries; i++) {
5428 struct got_commitable *ct = NULL;
5430 te = got_object_tree_get_entry(base_tree, i);
5431 if (got_object_tree_entry_is_submodule(te)) {
5432 /* Entry is a submodule; just copy it. */
5433 err = got_object_tree_entry_dup(&new_te, te);
5434 if (err)
5435 goto done;
5436 err = insert_tree_entry(new_te, &paths);
5437 if (err)
5438 goto done;
5439 (*nentries)++;
5440 continue;
5443 if (S_ISDIR(te->mode)) {
5444 int modified;
5445 err = got_object_tree_entry_dup(&new_te, te);
5446 if (err)
5447 goto done;
5448 err = match_modified_subtree(&modified, te,
5449 path_base_tree, commitable_paths);
5450 if (err)
5451 goto done;
5452 /* Avoid recursion into unmodified subtrees. */
5453 if (modified) {
5454 struct got_object_id *new_id;
5455 int nsubentries;
5456 err = write_subtree(&new_id,
5457 &nsubentries, te,
5458 path_base_tree, commitable_paths,
5459 status_cb, status_arg, repo);
5460 if (err)
5461 goto done;
5462 if (nsubentries == 0) {
5463 /* All entries were deleted. */
5464 free(new_id);
5465 continue;
5467 memcpy(&new_te->id, new_id,
5468 sizeof(new_te->id));
5469 free(new_id);
5471 err = insert_tree_entry(new_te, &paths);
5472 if (err)
5473 goto done;
5474 (*nentries)++;
5475 continue;
5478 err = match_deleted_or_modified_ct(&ct, te,
5479 path_base_tree, commitable_paths);
5480 if (err)
5481 goto done;
5482 if (ct) {
5483 /* NB: Deleted entries get dropped here. */
5484 if (ct->status == GOT_STATUS_MODIFY ||
5485 ct->status == GOT_STATUS_MODE_CHANGE ||
5486 ct->staged_status == GOT_STATUS_MODIFY) {
5487 err = alloc_modified_blob_tree_entry(
5488 &new_te, te, ct);
5489 if (err)
5490 goto done;
5491 err = insert_tree_entry(new_te, &paths);
5492 if (err)
5493 goto done;
5494 (*nentries)++;
5496 err = report_ct_status(ct, status_cb,
5497 status_arg);
5498 if (err)
5499 goto done;
5500 } else {
5501 /* Entry is unchanged; just copy it. */
5502 err = got_object_tree_entry_dup(&new_te, te);
5503 if (err)
5504 goto done;
5505 err = insert_tree_entry(new_te, &paths);
5506 if (err)
5507 goto done;
5508 (*nentries)++;
5513 /* Write new list of entries; deleted entries have been dropped. */
5514 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5515 done:
5516 got_pathlist_free(&paths);
5517 return err;
5520 static const struct got_error *
5521 update_fileindex_after_commit(struct got_worktree *worktree,
5522 struct got_pathlist_head *commitable_paths,
5523 struct got_object_id *new_base_commit_id,
5524 struct got_fileindex *fileindex, int have_staged_files)
5526 const struct got_error *err = NULL;
5527 struct got_pathlist_entry *pe;
5528 char *relpath = NULL;
5530 TAILQ_FOREACH(pe, commitable_paths, entry) {
5531 struct got_fileindex_entry *ie;
5532 struct got_commitable *ct = pe->data;
5534 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5536 err = got_path_skip_common_ancestor(&relpath,
5537 worktree->root_path, ct->ondisk_path);
5538 if (err)
5539 goto done;
5541 if (ie) {
5542 if (ct->status == GOT_STATUS_DELETE ||
5543 ct->staged_status == GOT_STATUS_DELETE) {
5544 got_fileindex_entry_remove(fileindex, ie);
5545 } else if (ct->staged_status == GOT_STATUS_ADD ||
5546 ct->staged_status == GOT_STATUS_MODIFY) {
5547 got_fileindex_entry_stage_set(ie,
5548 GOT_FILEIDX_STAGE_NONE);
5549 got_fileindex_entry_staged_filetype_set(ie, 0);
5551 err = got_fileindex_entry_update(ie,
5552 worktree->root_fd, relpath,
5553 ct->staged_blob_id->sha1,
5554 new_base_commit_id->sha1,
5555 !have_staged_files);
5556 } else
5557 err = got_fileindex_entry_update(ie,
5558 worktree->root_fd, relpath,
5559 ct->blob_id->sha1,
5560 new_base_commit_id->sha1,
5561 !have_staged_files);
5562 } else {
5563 err = got_fileindex_entry_alloc(&ie, pe->path);
5564 if (err)
5565 goto done;
5566 err = got_fileindex_entry_update(ie,
5567 worktree->root_fd, relpath, ct->blob_id->sha1,
5568 new_base_commit_id->sha1, 1);
5569 if (err) {
5570 got_fileindex_entry_free(ie);
5571 goto done;
5573 err = got_fileindex_entry_add(fileindex, ie);
5574 if (err) {
5575 got_fileindex_entry_free(ie);
5576 goto done;
5579 free(relpath);
5580 relpath = NULL;
5582 done:
5583 free(relpath);
5584 return err;
5588 static const struct got_error *
5589 check_out_of_date(const char *in_repo_path, unsigned char status,
5590 unsigned char staged_status, struct got_object_id *base_blob_id,
5591 struct got_object_id *base_commit_id,
5592 struct got_object_id *head_commit_id, struct got_repository *repo,
5593 int ood_errcode)
5595 const struct got_error *err = NULL;
5596 struct got_object_id *id = NULL;
5598 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5599 /* Trivial case: base commit == head commit */
5600 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5601 return NULL;
5603 * Ensure file content which local changes were based
5604 * on matches file content in the branch head.
5606 err = got_object_id_by_path(&id, repo, head_commit_id,
5607 in_repo_path);
5608 if (err) {
5609 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5610 err = got_error(ood_errcode);
5611 goto done;
5612 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5613 err = got_error(ood_errcode);
5614 } else {
5615 /* Require that added files don't exist in the branch head. */
5616 err = got_object_id_by_path(&id, repo, head_commit_id,
5617 in_repo_path);
5618 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5619 goto done;
5620 err = id ? got_error(ood_errcode) : NULL;
5622 done:
5623 free(id);
5624 return err;
5627 const struct got_error *
5628 commit_worktree(struct got_object_id **new_commit_id,
5629 struct got_pathlist_head *commitable_paths,
5630 struct got_object_id *head_commit_id,
5631 struct got_object_id *parent_id2,
5632 struct got_worktree *worktree,
5633 const char *author, const char *committer,
5634 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5635 got_worktree_status_cb status_cb, void *status_arg,
5636 struct got_repository *repo)
5638 const struct got_error *err = NULL, *unlockerr = NULL;
5639 struct got_pathlist_entry *pe;
5640 const char *head_ref_name = NULL;
5641 struct got_commit_object *head_commit = NULL;
5642 struct got_reference *head_ref2 = NULL;
5643 struct got_object_id *head_commit_id2 = NULL;
5644 struct got_tree_object *head_tree = NULL;
5645 struct got_object_id *new_tree_id = NULL;
5646 int nentries, nparents = 0;
5647 struct got_object_id_queue parent_ids;
5648 struct got_object_qid *pid = NULL;
5649 char *logmsg = NULL;
5651 *new_commit_id = NULL;
5653 STAILQ_INIT(&parent_ids);
5655 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5656 if (err)
5657 goto done;
5659 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5660 if (err)
5661 goto done;
5663 if (commit_msg_cb != NULL) {
5664 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5665 if (err)
5666 goto done;
5669 if (logmsg == NULL || strlen(logmsg) == 0) {
5670 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5671 goto done;
5674 /* Create blobs from added and modified files and record their IDs. */
5675 TAILQ_FOREACH(pe, commitable_paths, entry) {
5676 struct got_commitable *ct = pe->data;
5677 char *ondisk_path;
5679 /* Blobs for staged files already exist. */
5680 if (ct->staged_status == GOT_STATUS_ADD ||
5681 ct->staged_status == GOT_STATUS_MODIFY)
5682 continue;
5684 if (ct->status != GOT_STATUS_ADD &&
5685 ct->status != GOT_STATUS_MODIFY &&
5686 ct->status != GOT_STATUS_MODE_CHANGE)
5687 continue;
5689 if (asprintf(&ondisk_path, "%s/%s",
5690 worktree->root_path, pe->path) == -1) {
5691 err = got_error_from_errno("asprintf");
5692 goto done;
5694 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5695 free(ondisk_path);
5696 if (err)
5697 goto done;
5700 /* Recursively write new tree objects. */
5701 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5702 commitable_paths, status_cb, status_arg, repo);
5703 if (err)
5704 goto done;
5706 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5707 if (err)
5708 goto done;
5709 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5710 nparents++;
5711 if (parent_id2) {
5712 err = got_object_qid_alloc(&pid, parent_id2);
5713 if (err)
5714 goto done;
5715 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5716 nparents++;
5718 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5719 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5720 if (logmsg != NULL)
5721 free(logmsg);
5722 if (err)
5723 goto done;
5725 /* Check if a concurrent commit to our branch has occurred. */
5726 head_ref_name = got_worktree_get_head_ref_name(worktree);
5727 if (head_ref_name == NULL) {
5728 err = got_error_from_errno("got_worktree_get_head_ref_name");
5729 goto done;
5731 /* Lock the reference here to prevent concurrent modification. */
5732 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5733 if (err)
5734 goto done;
5735 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5736 if (err)
5737 goto done;
5738 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5739 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5740 goto done;
5742 /* Update branch head in repository. */
5743 err = got_ref_change_ref(head_ref2, *new_commit_id);
5744 if (err)
5745 goto done;
5746 err = got_ref_write(head_ref2, repo);
5747 if (err)
5748 goto done;
5750 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5751 if (err)
5752 goto done;
5754 err = ref_base_commit(worktree, repo);
5755 if (err)
5756 goto done;
5757 done:
5758 got_object_id_queue_free(&parent_ids);
5759 if (head_tree)
5760 got_object_tree_close(head_tree);
5761 if (head_commit)
5762 got_object_commit_close(head_commit);
5763 free(head_commit_id2);
5764 if (head_ref2) {
5765 unlockerr = got_ref_unlock(head_ref2);
5766 if (unlockerr && err == NULL)
5767 err = unlockerr;
5768 got_ref_close(head_ref2);
5770 return err;
5773 static const struct got_error *
5774 check_path_is_commitable(const char *path,
5775 struct got_pathlist_head *commitable_paths)
5777 struct got_pathlist_entry *cpe = NULL;
5778 size_t path_len = strlen(path);
5780 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5781 struct got_commitable *ct = cpe->data;
5782 const char *ct_path = ct->path;
5784 while (ct_path[0] == '/')
5785 ct_path++;
5787 if (strcmp(path, ct_path) == 0 ||
5788 got_path_is_child(ct_path, path, path_len))
5789 break;
5792 if (cpe == NULL)
5793 return got_error_path(path, GOT_ERR_BAD_PATH);
5795 return NULL;
5798 static const struct got_error *
5799 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5801 int *have_staged_files = arg;
5803 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5804 *have_staged_files = 1;
5805 return got_error(GOT_ERR_CANCELLED);
5808 return NULL;
5811 static const struct got_error *
5812 check_non_staged_files(struct got_fileindex *fileindex,
5813 struct got_pathlist_head *paths)
5815 struct got_pathlist_entry *pe;
5816 struct got_fileindex_entry *ie;
5818 TAILQ_FOREACH(pe, paths, entry) {
5819 if (pe->path[0] == '\0')
5820 continue;
5821 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5822 if (ie == NULL)
5823 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5824 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5825 return got_error_path(pe->path,
5826 GOT_ERR_FILE_NOT_STAGED);
5829 return NULL;
5832 const struct got_error *
5833 got_worktree_commit(struct got_object_id **new_commit_id,
5834 struct got_worktree *worktree, struct got_pathlist_head *paths,
5835 const char *author, const char *committer, int allow_bad_symlinks,
5836 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5837 got_worktree_status_cb status_cb, void *status_arg,
5838 struct got_repository *repo)
5840 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5841 struct got_fileindex *fileindex = NULL;
5842 char *fileindex_path = NULL;
5843 struct got_pathlist_head commitable_paths;
5844 struct collect_commitables_arg cc_arg;
5845 struct got_pathlist_entry *pe;
5846 struct got_reference *head_ref = NULL;
5847 struct got_object_id *head_commit_id = NULL;
5848 int have_staged_files = 0;
5850 *new_commit_id = NULL;
5852 TAILQ_INIT(&commitable_paths);
5854 err = lock_worktree(worktree, LOCK_EX);
5855 if (err)
5856 goto done;
5858 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5859 if (err)
5860 goto done;
5862 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5863 if (err)
5864 goto done;
5866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5867 if (err)
5868 goto done;
5870 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5871 &have_staged_files);
5872 if (err && err->code != GOT_ERR_CANCELLED)
5873 goto done;
5874 if (have_staged_files) {
5875 err = check_non_staged_files(fileindex, paths);
5876 if (err)
5877 goto done;
5880 cc_arg.commitable_paths = &commitable_paths;
5881 cc_arg.worktree = worktree;
5882 cc_arg.fileindex = fileindex;
5883 cc_arg.repo = repo;
5884 cc_arg.have_staged_files = have_staged_files;
5885 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5886 TAILQ_FOREACH(pe, paths, entry) {
5887 err = worktree_status(worktree, pe->path, fileindex, repo,
5888 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5889 if (err)
5890 goto done;
5893 if (TAILQ_EMPTY(&commitable_paths)) {
5894 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5895 goto done;
5898 TAILQ_FOREACH(pe, paths, entry) {
5899 err = check_path_is_commitable(pe->path, &commitable_paths);
5900 if (err)
5901 goto done;
5904 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5905 struct got_commitable *ct = pe->data;
5906 const char *ct_path = ct->in_repo_path;
5908 while (ct_path[0] == '/')
5909 ct_path++;
5910 err = check_out_of_date(ct_path, ct->status,
5911 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5912 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5913 if (err)
5914 goto done;
5918 err = commit_worktree(new_commit_id, &commitable_paths,
5919 head_commit_id, NULL, worktree, author, committer,
5920 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5921 if (err)
5922 goto done;
5924 err = update_fileindex_after_commit(worktree, &commitable_paths,
5925 *new_commit_id, fileindex, have_staged_files);
5926 sync_err = sync_fileindex(fileindex, fileindex_path);
5927 if (sync_err && err == NULL)
5928 err = sync_err;
5929 done:
5930 if (fileindex)
5931 got_fileindex_free(fileindex);
5932 free(fileindex_path);
5933 unlockerr = lock_worktree(worktree, LOCK_SH);
5934 if (unlockerr && err == NULL)
5935 err = unlockerr;
5936 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5937 struct got_commitable *ct = pe->data;
5938 free_commitable(ct);
5940 got_pathlist_free(&commitable_paths);
5941 return err;
5944 const char *
5945 got_commitable_get_path(struct got_commitable *ct)
5947 return ct->path;
5950 unsigned int
5951 got_commitable_get_status(struct got_commitable *ct)
5953 return ct->status;
5956 struct check_rebase_ok_arg {
5957 struct got_worktree *worktree;
5958 struct got_repository *repo;
5961 static const struct got_error *
5962 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5964 const struct got_error *err = NULL;
5965 struct check_rebase_ok_arg *a = arg;
5966 unsigned char status;
5967 struct stat sb;
5968 char *ondisk_path;
5970 /* Reject rebase of a work tree with mixed base commits. */
5971 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5972 SHA1_DIGEST_LENGTH))
5973 return got_error(GOT_ERR_MIXED_COMMITS);
5975 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5976 == -1)
5977 return got_error_from_errno("asprintf");
5979 /* Reject rebase of a work tree with modified or staged files. */
5980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5981 free(ondisk_path);
5982 if (err)
5983 return err;
5985 if (status != GOT_STATUS_NO_CHANGE)
5986 return got_error(GOT_ERR_MODIFIED);
5987 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5988 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5990 return NULL;
5993 const struct got_error *
5994 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5995 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5996 struct got_worktree *worktree, struct got_reference *branch,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL;
6000 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6001 char *branch_ref_name = NULL;
6002 char *fileindex_path = NULL;
6003 struct check_rebase_ok_arg ok_arg;
6004 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6005 struct got_object_id *wt_branch_tip = NULL;
6007 *new_base_branch_ref = NULL;
6008 *tmp_branch = NULL;
6009 *fileindex = NULL;
6011 err = lock_worktree(worktree, LOCK_EX);
6012 if (err)
6013 return err;
6015 err = open_fileindex(fileindex, &fileindex_path, worktree);
6016 if (err)
6017 goto done;
6019 ok_arg.worktree = worktree;
6020 ok_arg.repo = repo;
6021 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6022 &ok_arg);
6023 if (err)
6024 goto done;
6026 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6027 if (err)
6028 goto done;
6030 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6031 if (err)
6032 goto done;
6034 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6035 if (err)
6036 goto done;
6038 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6039 0);
6040 if (err)
6041 goto done;
6043 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6044 if (err)
6045 goto done;
6046 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6047 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6048 goto done;
6051 err = got_ref_alloc_symref(new_base_branch_ref,
6052 new_base_branch_ref_name, wt_branch);
6053 if (err)
6054 goto done;
6055 err = got_ref_write(*new_base_branch_ref, repo);
6056 if (err)
6057 goto done;
6059 /* TODO Lock original branch's ref while rebasing? */
6061 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6062 if (err)
6063 goto done;
6065 err = got_ref_write(branch_ref, repo);
6066 if (err)
6067 goto done;
6069 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6070 worktree->base_commit_id);
6071 if (err)
6072 goto done;
6073 err = got_ref_write(*tmp_branch, repo);
6074 if (err)
6075 goto done;
6077 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6078 if (err)
6079 goto done;
6080 done:
6081 free(fileindex_path);
6082 free(tmp_branch_name);
6083 free(new_base_branch_ref_name);
6084 free(branch_ref_name);
6085 if (branch_ref)
6086 got_ref_close(branch_ref);
6087 if (wt_branch)
6088 got_ref_close(wt_branch);
6089 free(wt_branch_tip);
6090 if (err) {
6091 if (*new_base_branch_ref) {
6092 got_ref_close(*new_base_branch_ref);
6093 *new_base_branch_ref = NULL;
6095 if (*tmp_branch) {
6096 got_ref_close(*tmp_branch);
6097 *tmp_branch = NULL;
6099 if (*fileindex) {
6100 got_fileindex_free(*fileindex);
6101 *fileindex = NULL;
6103 lock_worktree(worktree, LOCK_SH);
6105 return err;
6108 const struct got_error *
6109 got_worktree_rebase_continue(struct got_object_id **commit_id,
6110 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6111 struct got_reference **branch, struct got_fileindex **fileindex,
6112 struct got_worktree *worktree, struct got_repository *repo)
6114 const struct got_error *err;
6115 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6116 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6117 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6118 char *fileindex_path = NULL;
6119 int have_staged_files = 0;
6121 *commit_id = NULL;
6122 *new_base_branch = NULL;
6123 *tmp_branch = NULL;
6124 *branch = NULL;
6125 *fileindex = NULL;
6127 err = lock_worktree(worktree, LOCK_EX);
6128 if (err)
6129 return err;
6131 err = open_fileindex(fileindex, &fileindex_path, worktree);
6132 if (err)
6133 goto done;
6135 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6136 &have_staged_files);
6137 if (err && err->code != GOT_ERR_CANCELLED)
6138 goto done;
6139 if (have_staged_files) {
6140 err = got_error(GOT_ERR_STAGED_PATHS);
6141 goto done;
6144 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6145 if (err)
6146 goto done;
6148 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6149 if (err)
6150 goto done;
6152 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6153 if (err)
6154 goto done;
6156 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6157 if (err)
6158 goto done;
6160 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6161 if (err)
6162 goto done;
6164 err = got_ref_open(branch, repo,
6165 got_ref_get_symref_target(branch_ref), 0);
6166 if (err)
6167 goto done;
6169 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6170 if (err)
6171 goto done;
6173 err = got_ref_resolve(commit_id, repo, commit_ref);
6174 if (err)
6175 goto done;
6177 err = got_ref_open(new_base_branch, repo,
6178 new_base_branch_ref_name, 0);
6179 if (err)
6180 goto done;
6182 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6183 if (err)
6184 goto done;
6185 done:
6186 free(commit_ref_name);
6187 free(branch_ref_name);
6188 free(fileindex_path);
6189 if (commit_ref)
6190 got_ref_close(commit_ref);
6191 if (branch_ref)
6192 got_ref_close(branch_ref);
6193 if (err) {
6194 free(*commit_id);
6195 *commit_id = NULL;
6196 if (*tmp_branch) {
6197 got_ref_close(*tmp_branch);
6198 *tmp_branch = NULL;
6200 if (*new_base_branch) {
6201 got_ref_close(*new_base_branch);
6202 *new_base_branch = NULL;
6204 if (*branch) {
6205 got_ref_close(*branch);
6206 *branch = NULL;
6208 if (*fileindex) {
6209 got_fileindex_free(*fileindex);
6210 *fileindex = NULL;
6212 lock_worktree(worktree, LOCK_SH);
6214 return err;
6217 const struct got_error *
6218 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6220 const struct got_error *err;
6221 char *tmp_branch_name = NULL;
6223 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6224 if (err)
6225 return err;
6227 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6228 free(tmp_branch_name);
6229 return NULL;
6232 static const struct got_error *
6233 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6234 char **logmsg, void *arg)
6236 *logmsg = arg;
6237 return NULL;
6240 static const struct got_error *
6241 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6242 const char *path, struct got_object_id *blob_id,
6243 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6244 int dirfd, const char *de_name)
6246 return NULL;
6249 struct collect_merged_paths_arg {
6250 got_worktree_checkout_cb progress_cb;
6251 void *progress_arg;
6252 struct got_pathlist_head *merged_paths;
6255 static const struct got_error *
6256 collect_merged_paths(void *arg, unsigned char status, const char *path)
6258 const struct got_error *err;
6259 struct collect_merged_paths_arg *a = arg;
6260 char *p;
6261 struct got_pathlist_entry *new;
6263 err = (*a->progress_cb)(a->progress_arg, status, path);
6264 if (err)
6265 return err;
6267 if (status != GOT_STATUS_MERGE &&
6268 status != GOT_STATUS_ADD &&
6269 status != GOT_STATUS_DELETE &&
6270 status != GOT_STATUS_CONFLICT)
6271 return NULL;
6273 p = strdup(path);
6274 if (p == NULL)
6275 return got_error_from_errno("strdup");
6277 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6278 if (err || new == NULL)
6279 free(p);
6280 return err;
6283 void
6284 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6286 struct got_pathlist_entry *pe;
6288 TAILQ_FOREACH(pe, merged_paths, entry)
6289 free((char *)pe->path);
6291 got_pathlist_free(merged_paths);
6294 static const struct got_error *
6295 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6296 int is_rebase, struct got_repository *repo)
6298 const struct got_error *err;
6299 struct got_reference *commit_ref = NULL;
6301 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6302 if (err) {
6303 if (err->code != GOT_ERR_NOT_REF)
6304 goto done;
6305 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6306 if (err)
6307 goto done;
6308 err = got_ref_write(commit_ref, repo);
6309 if (err)
6310 goto done;
6311 } else if (is_rebase) {
6312 struct got_object_id *stored_id;
6313 int cmp;
6315 err = got_ref_resolve(&stored_id, repo, commit_ref);
6316 if (err)
6317 goto done;
6318 cmp = got_object_id_cmp(commit_id, stored_id);
6319 free(stored_id);
6320 if (cmp != 0) {
6321 err = got_error(GOT_ERR_REBASE_COMMITID);
6322 goto done;
6325 done:
6326 if (commit_ref)
6327 got_ref_close(commit_ref);
6328 return err;
6331 static const struct got_error *
6332 rebase_merge_files(struct got_pathlist_head *merged_paths,
6333 const char *commit_ref_name, struct got_worktree *worktree,
6334 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6335 struct got_object_id *commit_id, struct got_repository *repo,
6336 got_worktree_checkout_cb progress_cb, void *progress_arg,
6337 got_cancel_cb cancel_cb, void *cancel_arg)
6339 const struct got_error *err;
6340 struct got_reference *commit_ref = NULL;
6341 struct collect_merged_paths_arg cmp_arg;
6342 char *fileindex_path;
6344 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6346 err = get_fileindex_path(&fileindex_path, worktree);
6347 if (err)
6348 return err;
6350 cmp_arg.progress_cb = progress_cb;
6351 cmp_arg.progress_arg = progress_arg;
6352 cmp_arg.merged_paths = merged_paths;
6353 err = merge_files(worktree, fileindex, fileindex_path,
6354 parent_commit_id, commit_id, repo, collect_merged_paths,
6355 &cmp_arg, cancel_cb, cancel_arg);
6356 if (commit_ref)
6357 got_ref_close(commit_ref);
6358 return err;
6361 const struct got_error *
6362 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6363 struct got_worktree *worktree, struct got_fileindex *fileindex,
6364 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6365 struct got_repository *repo,
6366 got_worktree_checkout_cb progress_cb, void *progress_arg,
6367 got_cancel_cb cancel_cb, void *cancel_arg)
6369 const struct got_error *err;
6370 char *commit_ref_name;
6372 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6373 if (err)
6374 return err;
6376 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6377 if (err)
6378 goto done;
6380 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6381 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6382 progress_arg, cancel_cb, cancel_arg);
6383 done:
6384 free(commit_ref_name);
6385 return err;
6388 const struct got_error *
6389 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6390 struct got_worktree *worktree, struct got_fileindex *fileindex,
6391 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6392 struct got_repository *repo,
6393 got_worktree_checkout_cb progress_cb, void *progress_arg,
6394 got_cancel_cb cancel_cb, void *cancel_arg)
6396 const struct got_error *err;
6397 char *commit_ref_name;
6399 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6400 if (err)
6401 return err;
6403 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6404 if (err)
6405 goto done;
6407 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6408 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6409 progress_arg, cancel_cb, cancel_arg);
6410 done:
6411 free(commit_ref_name);
6412 return err;
6415 static const struct got_error *
6416 rebase_commit(struct got_object_id **new_commit_id,
6417 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6418 struct got_worktree *worktree, struct got_fileindex *fileindex,
6419 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6420 const char *new_logmsg, struct got_repository *repo)
6422 const struct got_error *err, *sync_err;
6423 struct got_pathlist_head commitable_paths;
6424 struct collect_commitables_arg cc_arg;
6425 char *fileindex_path = NULL;
6426 struct got_reference *head_ref = NULL;
6427 struct got_object_id *head_commit_id = NULL;
6428 char *logmsg = NULL;
6430 TAILQ_INIT(&commitable_paths);
6431 *new_commit_id = NULL;
6433 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6435 err = get_fileindex_path(&fileindex_path, worktree);
6436 if (err)
6437 return err;
6439 cc_arg.commitable_paths = &commitable_paths;
6440 cc_arg.worktree = worktree;
6441 cc_arg.repo = repo;
6442 cc_arg.have_staged_files = 0;
6444 * If possible get the status of individual files directly to
6445 * avoid crawling the entire work tree once per rebased commit.
6447 * Ideally, merged_paths would contain a list of commitables
6448 * we could use so we could skip worktree_status() entirely.
6449 * However, we would then need carefully keep track of cumulative
6450 * effects of operations such as file additions and deletions
6451 * in 'got histedit -f' (folding multiple commits into one),
6452 * and this extra complexity is not really worth it.
6454 if (merged_paths) {
6455 struct got_pathlist_entry *pe;
6456 TAILQ_FOREACH(pe, merged_paths, entry) {
6457 err = worktree_status(worktree, pe->path, fileindex,
6458 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6459 0);
6460 if (err)
6461 goto done;
6463 } else {
6464 err = worktree_status(worktree, "", fileindex, repo,
6465 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6466 if (err)
6467 goto done;
6470 if (TAILQ_EMPTY(&commitable_paths)) {
6471 /* No-op change; commit will be elided. */
6472 err = got_ref_delete(commit_ref, repo);
6473 if (err)
6474 goto done;
6475 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6476 goto done;
6479 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6480 if (err)
6481 goto done;
6483 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6484 if (err)
6485 goto done;
6487 if (new_logmsg) {
6488 logmsg = strdup(new_logmsg);
6489 if (logmsg == NULL) {
6490 err = got_error_from_errno("strdup");
6491 goto done;
6493 } else {
6494 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6495 if (err)
6496 goto done;
6499 /* NB: commit_worktree will call free(logmsg) */
6500 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6501 NULL, worktree, got_object_commit_get_author(orig_commit),
6502 got_object_commit_get_committer(orig_commit),
6503 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6504 if (err)
6505 goto done;
6507 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6508 if (err)
6509 goto done;
6511 err = got_ref_delete(commit_ref, repo);
6512 if (err)
6513 goto done;
6515 err = update_fileindex_after_commit(worktree, &commitable_paths,
6516 *new_commit_id, fileindex, 0);
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6519 err = sync_err;
6520 done:
6521 free(fileindex_path);
6522 free(head_commit_id);
6523 if (head_ref)
6524 got_ref_close(head_ref);
6525 if (err) {
6526 free(*new_commit_id);
6527 *new_commit_id = NULL;
6529 return err;
6532 const struct got_error *
6533 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6534 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6536 struct got_commit_object *orig_commit,
6537 struct got_object_id *orig_commit_id, struct got_repository *repo)
6539 const struct got_error *err;
6540 char *commit_ref_name;
6541 struct got_reference *commit_ref = NULL;
6542 struct got_object_id *commit_id = NULL;
6544 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6545 if (err)
6546 return err;
6548 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6549 if (err)
6550 goto done;
6551 err = got_ref_resolve(&commit_id, repo, commit_ref);
6552 if (err)
6553 goto done;
6554 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6555 err = got_error(GOT_ERR_REBASE_COMMITID);
6556 goto done;
6559 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6560 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6561 done:
6562 if (commit_ref)
6563 got_ref_close(commit_ref);
6564 free(commit_ref_name);
6565 free(commit_id);
6566 return err;
6569 const struct got_error *
6570 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6571 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6572 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6573 struct got_commit_object *orig_commit,
6574 struct got_object_id *orig_commit_id, const char *new_logmsg,
6575 struct got_repository *repo)
6577 const struct got_error *err;
6578 char *commit_ref_name;
6579 struct got_reference *commit_ref = NULL;
6581 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6582 if (err)
6583 return err;
6585 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6586 if (err)
6587 goto done;
6589 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6590 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6591 done:
6592 if (commit_ref)
6593 got_ref_close(commit_ref);
6594 free(commit_ref_name);
6595 return err;
6598 const struct got_error *
6599 got_worktree_rebase_postpone(struct got_worktree *worktree,
6600 struct got_fileindex *fileindex)
6602 if (fileindex)
6603 got_fileindex_free(fileindex);
6604 return lock_worktree(worktree, LOCK_SH);
6607 static const struct got_error *
6608 delete_ref(const char *name, struct got_repository *repo)
6610 const struct got_error *err;
6611 struct got_reference *ref;
6613 err = got_ref_open(&ref, repo, name, 0);
6614 if (err) {
6615 if (err->code == GOT_ERR_NOT_REF)
6616 return NULL;
6617 return err;
6620 err = got_ref_delete(ref, repo);
6621 got_ref_close(ref);
6622 return err;
6625 static const struct got_error *
6626 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6628 const struct got_error *err;
6629 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6630 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6632 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6633 if (err)
6634 goto done;
6635 err = delete_ref(tmp_branch_name, repo);
6636 if (err)
6637 goto done;
6639 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6640 if (err)
6641 goto done;
6642 err = delete_ref(new_base_branch_ref_name, repo);
6643 if (err)
6644 goto done;
6646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6647 if (err)
6648 goto done;
6649 err = delete_ref(branch_ref_name, repo);
6650 if (err)
6651 goto done;
6653 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6654 if (err)
6655 goto done;
6656 err = delete_ref(commit_ref_name, repo);
6657 if (err)
6658 goto done;
6660 done:
6661 free(tmp_branch_name);
6662 free(new_base_branch_ref_name);
6663 free(branch_ref_name);
6664 free(commit_ref_name);
6665 return err;
6668 const struct got_error *
6669 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6670 struct got_object_id *new_commit_id, struct got_repository *repo)
6672 const struct got_error *err;
6673 struct got_reference *ref = NULL;
6674 struct got_object_id *old_commit_id = NULL;
6675 const char *branch_name = NULL;
6676 char *new_id_str = NULL;
6677 char *refname = NULL;
6679 branch_name = got_ref_get_name(branch);
6680 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6681 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6682 branch_name += 11;
6684 err = got_object_id_str(&new_id_str, new_commit_id);
6685 if (err)
6686 return err;
6688 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6689 new_id_str) == -1) {
6690 err = got_error_from_errno("asprintf");
6691 goto done;
6694 err = got_ref_resolve(&old_commit_id, repo, branch);
6695 if (err)
6696 goto done;
6698 err = got_ref_alloc(&ref, refname, old_commit_id);
6699 if (err)
6700 goto done;
6702 err = got_ref_write(ref, repo);
6703 done:
6704 free(new_id_str);
6705 free(refname);
6706 free(old_commit_id);
6707 if (ref)
6708 got_ref_close(ref);
6709 return err;
6712 const struct got_error *
6713 got_worktree_rebase_complete(struct got_worktree *worktree,
6714 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6715 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6716 struct got_repository *repo, int create_backup)
6718 const struct got_error *err, *unlockerr, *sync_err;
6719 struct got_object_id *new_head_commit_id = NULL;
6720 char *fileindex_path = NULL;
6722 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6723 if (err)
6724 return err;
6726 if (create_backup) {
6727 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6728 rebased_branch, new_head_commit_id, repo);
6729 if (err)
6730 goto done;
6733 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6734 if (err)
6735 goto done;
6737 err = got_ref_write(rebased_branch, repo);
6738 if (err)
6739 goto done;
6741 err = got_worktree_set_head_ref(worktree, rebased_branch);
6742 if (err)
6743 goto done;
6745 err = delete_rebase_refs(worktree, repo);
6746 if (err)
6747 goto done;
6749 err = get_fileindex_path(&fileindex_path, worktree);
6750 if (err)
6751 goto done;
6752 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6753 sync_err = sync_fileindex(fileindex, fileindex_path);
6754 if (sync_err && err == NULL)
6755 err = sync_err;
6756 done:
6757 got_fileindex_free(fileindex);
6758 free(fileindex_path);
6759 free(new_head_commit_id);
6760 unlockerr = lock_worktree(worktree, LOCK_SH);
6761 if (unlockerr && err == NULL)
6762 err = unlockerr;
6763 return err;
6766 const struct got_error *
6767 got_worktree_rebase_abort(struct got_worktree *worktree,
6768 struct got_fileindex *fileindex, struct got_repository *repo,
6769 struct got_reference *new_base_branch,
6770 got_worktree_checkout_cb progress_cb, void *progress_arg)
6772 const struct got_error *err, *unlockerr, *sync_err;
6773 struct got_reference *resolved = NULL;
6774 struct got_object_id *commit_id = NULL;
6775 char *fileindex_path = NULL;
6776 struct revert_file_args rfa;
6777 struct got_object_id *tree_id = NULL;
6779 err = lock_worktree(worktree, LOCK_EX);
6780 if (err)
6781 return err;
6783 err = got_ref_open(&resolved, repo,
6784 got_ref_get_symref_target(new_base_branch), 0);
6785 if (err)
6786 goto done;
6788 err = got_worktree_set_head_ref(worktree, resolved);
6789 if (err)
6790 goto done;
6793 * XXX commits to the base branch could have happened while
6794 * we were busy rebasing; should we store the original commit ID
6795 * when rebase begins and read it back here?
6797 err = got_ref_resolve(&commit_id, repo, resolved);
6798 if (err)
6799 goto done;
6801 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6802 if (err)
6803 goto done;
6805 err = got_object_id_by_path(&tree_id, repo,
6806 worktree->base_commit_id, worktree->path_prefix);
6807 if (err)
6808 goto done;
6810 err = delete_rebase_refs(worktree, repo);
6811 if (err)
6812 goto done;
6814 err = get_fileindex_path(&fileindex_path, worktree);
6815 if (err)
6816 goto done;
6818 rfa.worktree = worktree;
6819 rfa.fileindex = fileindex;
6820 rfa.progress_cb = progress_cb;
6821 rfa.progress_arg = progress_arg;
6822 rfa.patch_cb = NULL;
6823 rfa.patch_arg = NULL;
6824 rfa.repo = repo;
6825 rfa.unlink_added_files = 0;
6826 err = worktree_status(worktree, "", fileindex, repo,
6827 revert_file, &rfa, NULL, NULL, 0, 0);
6828 if (err)
6829 goto sync;
6831 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6832 repo, progress_cb, progress_arg, NULL, NULL);
6833 sync:
6834 sync_err = sync_fileindex(fileindex, fileindex_path);
6835 if (sync_err && err == NULL)
6836 err = sync_err;
6837 done:
6838 got_ref_close(resolved);
6839 free(tree_id);
6840 free(commit_id);
6841 if (fileindex)
6842 got_fileindex_free(fileindex);
6843 free(fileindex_path);
6845 unlockerr = lock_worktree(worktree, LOCK_SH);
6846 if (unlockerr && err == NULL)
6847 err = unlockerr;
6848 return err;
6851 const struct got_error *
6852 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6853 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6854 struct got_fileindex **fileindex, struct got_worktree *worktree,
6855 struct got_repository *repo)
6857 const struct got_error *err = NULL;
6858 char *tmp_branch_name = NULL;
6859 char *branch_ref_name = NULL;
6860 char *base_commit_ref_name = NULL;
6861 char *fileindex_path = NULL;
6862 struct check_rebase_ok_arg ok_arg;
6863 struct got_reference *wt_branch = NULL;
6864 struct got_reference *base_commit_ref = NULL;
6866 *tmp_branch = NULL;
6867 *branch_ref = NULL;
6868 *base_commit_id = NULL;
6869 *fileindex = NULL;
6871 err = lock_worktree(worktree, LOCK_EX);
6872 if (err)
6873 return err;
6875 err = open_fileindex(fileindex, &fileindex_path, worktree);
6876 if (err)
6877 goto done;
6879 ok_arg.worktree = worktree;
6880 ok_arg.repo = repo;
6881 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6882 &ok_arg);
6883 if (err)
6884 goto done;
6886 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6887 if (err)
6888 goto done;
6890 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6891 if (err)
6892 goto done;
6894 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6895 worktree);
6896 if (err)
6897 goto done;
6899 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6900 0);
6901 if (err)
6902 goto done;
6904 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6905 if (err)
6906 goto done;
6908 err = got_ref_write(*branch_ref, repo);
6909 if (err)
6910 goto done;
6912 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6913 worktree->base_commit_id);
6914 if (err)
6915 goto done;
6916 err = got_ref_write(base_commit_ref, repo);
6917 if (err)
6918 goto done;
6919 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6920 if (*base_commit_id == NULL) {
6921 err = got_error_from_errno("got_object_id_dup");
6922 goto done;
6925 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6926 worktree->base_commit_id);
6927 if (err)
6928 goto done;
6929 err = got_ref_write(*tmp_branch, repo);
6930 if (err)
6931 goto done;
6933 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6934 if (err)
6935 goto done;
6936 done:
6937 free(fileindex_path);
6938 free(tmp_branch_name);
6939 free(branch_ref_name);
6940 free(base_commit_ref_name);
6941 if (wt_branch)
6942 got_ref_close(wt_branch);
6943 if (err) {
6944 if (*branch_ref) {
6945 got_ref_close(*branch_ref);
6946 *branch_ref = NULL;
6948 if (*tmp_branch) {
6949 got_ref_close(*tmp_branch);
6950 *tmp_branch = NULL;
6952 free(*base_commit_id);
6953 if (*fileindex) {
6954 got_fileindex_free(*fileindex);
6955 *fileindex = NULL;
6957 lock_worktree(worktree, LOCK_SH);
6959 return err;
6962 const struct got_error *
6963 got_worktree_histedit_postpone(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex)
6966 if (fileindex)
6967 got_fileindex_free(fileindex);
6968 return lock_worktree(worktree, LOCK_SH);
6971 const struct got_error *
6972 got_worktree_histedit_in_progress(int *in_progress,
6973 struct got_worktree *worktree)
6975 const struct got_error *err;
6976 char *tmp_branch_name = NULL;
6978 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6979 if (err)
6980 return err;
6982 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6983 free(tmp_branch_name);
6984 return NULL;
6987 const struct got_error *
6988 got_worktree_histedit_continue(struct got_object_id **commit_id,
6989 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6990 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6991 struct got_worktree *worktree, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6995 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6996 struct got_reference *commit_ref = NULL;
6997 struct got_reference *base_commit_ref = NULL;
6998 char *fileindex_path = NULL;
6999 int have_staged_files = 0;
7001 *commit_id = NULL;
7002 *tmp_branch = NULL;
7003 *base_commit_id = NULL;
7004 *fileindex = NULL;
7006 err = lock_worktree(worktree, LOCK_EX);
7007 if (err)
7008 return err;
7010 err = open_fileindex(fileindex, &fileindex_path, worktree);
7011 if (err)
7012 goto done;
7014 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7015 &have_staged_files);
7016 if (err && err->code != GOT_ERR_CANCELLED)
7017 goto done;
7018 if (have_staged_files) {
7019 err = got_error(GOT_ERR_STAGED_PATHS);
7020 goto done;
7023 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7024 if (err)
7025 goto done;
7027 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7028 if (err)
7029 goto done;
7031 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7032 if (err)
7033 goto done;
7035 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7036 worktree);
7037 if (err)
7038 goto done;
7040 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7041 if (err)
7042 goto done;
7044 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7045 if (err)
7046 goto done;
7047 err = got_ref_resolve(commit_id, repo, commit_ref);
7048 if (err)
7049 goto done;
7051 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7052 if (err)
7053 goto done;
7054 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7055 if (err)
7056 goto done;
7058 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7059 if (err)
7060 goto done;
7061 done:
7062 free(commit_ref_name);
7063 free(branch_ref_name);
7064 free(fileindex_path);
7065 if (commit_ref)
7066 got_ref_close(commit_ref);
7067 if (base_commit_ref)
7068 got_ref_close(base_commit_ref);
7069 if (err) {
7070 free(*commit_id);
7071 *commit_id = NULL;
7072 free(*base_commit_id);
7073 *base_commit_id = NULL;
7074 if (*tmp_branch) {
7075 got_ref_close(*tmp_branch);
7076 *tmp_branch = NULL;
7078 if (*fileindex) {
7079 got_fileindex_free(*fileindex);
7080 *fileindex = NULL;
7082 lock_worktree(worktree, LOCK_EX);
7084 return err;
7087 static const struct got_error *
7088 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7090 const struct got_error *err;
7091 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7092 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7094 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7095 if (err)
7096 goto done;
7097 err = delete_ref(tmp_branch_name, repo);
7098 if (err)
7099 goto done;
7101 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7102 worktree);
7103 if (err)
7104 goto done;
7105 err = delete_ref(base_commit_ref_name, repo);
7106 if (err)
7107 goto done;
7109 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7110 if (err)
7111 goto done;
7112 err = delete_ref(branch_ref_name, repo);
7113 if (err)
7114 goto done;
7116 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7117 if (err)
7118 goto done;
7119 err = delete_ref(commit_ref_name, repo);
7120 if (err)
7121 goto done;
7122 done:
7123 free(tmp_branch_name);
7124 free(base_commit_ref_name);
7125 free(branch_ref_name);
7126 free(commit_ref_name);
7127 return err;
7130 const struct got_error *
7131 got_worktree_histedit_abort(struct got_worktree *worktree,
7132 struct got_fileindex *fileindex, struct got_repository *repo,
7133 struct got_reference *branch, struct got_object_id *base_commit_id,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg)
7136 const struct got_error *err, *unlockerr, *sync_err;
7137 struct got_reference *resolved = NULL;
7138 char *fileindex_path = NULL;
7139 struct got_object_id *tree_id = NULL;
7140 struct revert_file_args rfa;
7142 err = lock_worktree(worktree, LOCK_EX);
7143 if (err)
7144 return err;
7146 err = got_ref_open(&resolved, repo,
7147 got_ref_get_symref_target(branch), 0);
7148 if (err)
7149 goto done;
7151 err = got_worktree_set_head_ref(worktree, resolved);
7152 if (err)
7153 goto done;
7155 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7156 if (err)
7157 goto done;
7159 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7160 worktree->path_prefix);
7161 if (err)
7162 goto done;
7164 err = delete_histedit_refs(worktree, repo);
7165 if (err)
7166 goto done;
7168 err = get_fileindex_path(&fileindex_path, worktree);
7169 if (err)
7170 goto done;
7172 rfa.worktree = worktree;
7173 rfa.fileindex = fileindex;
7174 rfa.progress_cb = progress_cb;
7175 rfa.progress_arg = progress_arg;
7176 rfa.patch_cb = NULL;
7177 rfa.patch_arg = NULL;
7178 rfa.repo = repo;
7179 rfa.unlink_added_files = 0;
7180 err = worktree_status(worktree, "", fileindex, repo,
7181 revert_file, &rfa, NULL, NULL, 0, 0);
7182 if (err)
7183 goto sync;
7185 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7186 repo, progress_cb, progress_arg, NULL, NULL);
7187 sync:
7188 sync_err = sync_fileindex(fileindex, fileindex_path);
7189 if (sync_err && err == NULL)
7190 err = sync_err;
7191 done:
7192 got_ref_close(resolved);
7193 free(tree_id);
7194 free(fileindex_path);
7196 unlockerr = lock_worktree(worktree, LOCK_SH);
7197 if (unlockerr && err == NULL)
7198 err = unlockerr;
7199 return err;
7202 const struct got_error *
7203 got_worktree_histedit_complete(struct got_worktree *worktree,
7204 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7205 struct got_reference *edited_branch, struct got_repository *repo)
7207 const struct got_error *err, *unlockerr, *sync_err;
7208 struct got_object_id *new_head_commit_id = NULL;
7209 struct got_reference *resolved = NULL;
7210 char *fileindex_path = NULL;
7212 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7213 if (err)
7214 return err;
7216 err = got_ref_open(&resolved, repo,
7217 got_ref_get_symref_target(edited_branch), 0);
7218 if (err)
7219 goto done;
7221 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7222 resolved, new_head_commit_id, repo);
7223 if (err)
7224 goto done;
7226 err = got_ref_change_ref(resolved, new_head_commit_id);
7227 if (err)
7228 goto done;
7230 err = got_ref_write(resolved, repo);
7231 if (err)
7232 goto done;
7234 err = got_worktree_set_head_ref(worktree, resolved);
7235 if (err)
7236 goto done;
7238 err = delete_histedit_refs(worktree, repo);
7239 if (err)
7240 goto done;
7242 err = get_fileindex_path(&fileindex_path, worktree);
7243 if (err)
7244 goto done;
7245 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7246 sync_err = sync_fileindex(fileindex, fileindex_path);
7247 if (sync_err && err == NULL)
7248 err = sync_err;
7249 done:
7250 got_fileindex_free(fileindex);
7251 free(fileindex_path);
7252 free(new_head_commit_id);
7253 unlockerr = lock_worktree(worktree, LOCK_SH);
7254 if (unlockerr && err == NULL)
7255 err = unlockerr;
7256 return err;
7259 const struct got_error *
7260 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7261 struct got_object_id *commit_id, struct got_repository *repo)
7263 const struct got_error *err;
7264 char *commit_ref_name;
7266 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7267 if (err)
7268 return err;
7270 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7271 if (err)
7272 goto done;
7274 err = delete_ref(commit_ref_name, repo);
7275 done:
7276 free(commit_ref_name);
7277 return err;
7280 const struct got_error *
7281 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7282 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7283 struct got_worktree *worktree, const char *refname,
7284 struct got_repository *repo)
7286 const struct got_error *err = NULL;
7287 char *fileindex_path = NULL;
7288 struct check_rebase_ok_arg ok_arg;
7290 *fileindex = NULL;
7291 *branch_ref = NULL;
7292 *base_branch_ref = NULL;
7294 err = lock_worktree(worktree, LOCK_EX);
7295 if (err)
7296 return err;
7298 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7299 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7300 "cannot integrate a branch into itself; "
7301 "update -b or different branch name required");
7302 goto done;
7305 err = open_fileindex(fileindex, &fileindex_path, worktree);
7306 if (err)
7307 goto done;
7309 /* Preconditions are the same as for rebase. */
7310 ok_arg.worktree = worktree;
7311 ok_arg.repo = repo;
7312 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7313 &ok_arg);
7314 if (err)
7315 goto done;
7317 err = got_ref_open(branch_ref, repo, refname, 1);
7318 if (err)
7319 goto done;
7321 err = got_ref_open(base_branch_ref, repo,
7322 got_worktree_get_head_ref_name(worktree), 1);
7323 done:
7324 if (err) {
7325 if (*branch_ref) {
7326 got_ref_close(*branch_ref);
7327 *branch_ref = NULL;
7329 if (*base_branch_ref) {
7330 got_ref_close(*base_branch_ref);
7331 *base_branch_ref = NULL;
7333 if (*fileindex) {
7334 got_fileindex_free(*fileindex);
7335 *fileindex = NULL;
7337 lock_worktree(worktree, LOCK_SH);
7339 return err;
7342 const struct got_error *
7343 got_worktree_integrate_continue(struct got_worktree *worktree,
7344 struct got_fileindex *fileindex, struct got_repository *repo,
7345 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7346 got_worktree_checkout_cb progress_cb, void *progress_arg,
7347 got_cancel_cb cancel_cb, void *cancel_arg)
7349 const struct got_error *err = NULL, *sync_err, *unlockerr;
7350 char *fileindex_path = NULL;
7351 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7353 err = get_fileindex_path(&fileindex_path, worktree);
7354 if (err)
7355 goto done;
7357 err = got_ref_resolve(&commit_id, repo, branch_ref);
7358 if (err)
7359 goto done;
7361 err = got_object_id_by_path(&tree_id, repo, commit_id,
7362 worktree->path_prefix);
7363 if (err)
7364 goto done;
7366 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7367 if (err)
7368 goto done;
7370 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7371 progress_cb, progress_arg, cancel_cb, cancel_arg);
7372 if (err)
7373 goto sync;
7375 err = got_ref_change_ref(base_branch_ref, commit_id);
7376 if (err)
7377 goto sync;
7379 err = got_ref_write(base_branch_ref, repo);
7380 if (err)
7381 goto sync;
7383 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7384 sync:
7385 sync_err = sync_fileindex(fileindex, fileindex_path);
7386 if (sync_err && err == NULL)
7387 err = sync_err;
7389 done:
7390 unlockerr = got_ref_unlock(branch_ref);
7391 if (unlockerr && err == NULL)
7392 err = unlockerr;
7393 got_ref_close(branch_ref);
7395 unlockerr = got_ref_unlock(base_branch_ref);
7396 if (unlockerr && err == NULL)
7397 err = unlockerr;
7398 got_ref_close(base_branch_ref);
7400 got_fileindex_free(fileindex);
7401 free(fileindex_path);
7402 free(tree_id);
7404 unlockerr = lock_worktree(worktree, LOCK_SH);
7405 if (unlockerr && err == NULL)
7406 err = unlockerr;
7407 return err;
7410 const struct got_error *
7411 got_worktree_integrate_abort(struct got_worktree *worktree,
7412 struct got_fileindex *fileindex, struct got_repository *repo,
7413 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7415 const struct got_error *err = NULL, *unlockerr = NULL;
7417 got_fileindex_free(fileindex);
7419 err = lock_worktree(worktree, LOCK_SH);
7421 unlockerr = got_ref_unlock(branch_ref);
7422 if (unlockerr && err == NULL)
7423 err = unlockerr;
7424 got_ref_close(branch_ref);
7426 unlockerr = got_ref_unlock(base_branch_ref);
7427 if (unlockerr && err == NULL)
7428 err = unlockerr;
7429 got_ref_close(base_branch_ref);
7431 return err;
7434 const struct got_error *
7435 got_worktree_merge_postpone(struct got_worktree *worktree,
7436 struct got_fileindex *fileindex)
7438 const struct got_error *err, *sync_err;
7439 char *fileindex_path = NULL;
7441 err = get_fileindex_path(&fileindex_path, worktree);
7442 if (err)
7443 goto done;
7445 sync_err = sync_fileindex(fileindex, fileindex_path);
7447 err = lock_worktree(worktree, LOCK_SH);
7448 if (sync_err && err == NULL)
7449 err = sync_err;
7450 done:
7451 got_fileindex_free(fileindex);
7452 free(fileindex_path);
7453 return err;
7456 static const struct got_error *
7457 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7459 const struct got_error *err;
7460 char *branch_refname = NULL, *commit_refname = NULL;
7462 err = get_merge_branch_ref_name(&branch_refname, worktree);
7463 if (err)
7464 goto done;
7465 err = delete_ref(branch_refname, repo);
7466 if (err)
7467 goto done;
7469 err = get_merge_commit_ref_name(&commit_refname, worktree);
7470 if (err)
7471 goto done;
7472 err = delete_ref(commit_refname, repo);
7473 if (err)
7474 goto done;
7476 done:
7477 free(branch_refname);
7478 free(commit_refname);
7479 return err;
7482 struct merge_commit_msg_arg {
7483 struct got_worktree *worktree;
7484 const char *branch_name;
7487 static const struct got_error *
7488 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7489 void *arg)
7491 struct merge_commit_msg_arg *a = arg;
7493 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7494 got_worktree_get_head_ref_name(a->worktree)) == -1)
7495 return got_error_from_errno("asprintf");
7497 return NULL;
7500 static const struct got_error *
7501 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7502 const char *path, struct got_object_id *blob_id,
7503 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7504 int dirfd, const char *de_name)
7506 return NULL;
7509 const struct got_error *
7510 got_worktree_merge_branch(struct got_worktree *worktree,
7511 struct got_fileindex *fileindex,
7512 struct got_object_id *yca_commit_id,
7513 struct got_object_id *branch_tip,
7514 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7515 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7517 const struct got_error *err;
7518 char *fileindex_path = NULL;
7520 err = get_fileindex_path(&fileindex_path, worktree);
7521 if (err)
7522 goto done;
7524 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7525 worktree);
7526 if (err)
7527 goto done;
7529 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7530 branch_tip, repo, progress_cb, progress_arg,
7531 cancel_cb, cancel_arg);
7532 done:
7533 free(fileindex_path);
7534 return err;
7537 const struct got_error *
7538 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7539 struct got_worktree *worktree, struct got_fileindex *fileindex,
7540 const char *author, const char *committer, int allow_bad_symlinks,
7541 struct got_object_id *branch_tip, const char *branch_name,
7542 struct got_repository *repo)
7544 const struct got_error *err = NULL, *sync_err;
7545 struct got_pathlist_head commitable_paths;
7546 struct collect_commitables_arg cc_arg;
7547 struct got_pathlist_entry *pe;
7548 struct got_reference *head_ref = NULL;
7549 struct got_object_id *head_commit_id = NULL;
7550 int have_staged_files = 0;
7551 struct merge_commit_msg_arg mcm_arg;
7552 char *fileindex_path = NULL;
7554 *new_commit_id = NULL;
7556 TAILQ_INIT(&commitable_paths);
7558 err = get_fileindex_path(&fileindex_path, worktree);
7559 if (err)
7560 goto done;
7562 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7563 if (err)
7564 goto done;
7566 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7567 if (err)
7568 goto done;
7570 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7571 &have_staged_files);
7572 if (err && err->code != GOT_ERR_CANCELLED)
7573 goto done;
7574 if (have_staged_files) {
7575 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7576 goto done;
7579 cc_arg.commitable_paths = &commitable_paths;
7580 cc_arg.worktree = worktree;
7581 cc_arg.fileindex = fileindex;
7582 cc_arg.repo = repo;
7583 cc_arg.have_staged_files = have_staged_files;
7584 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7585 err = worktree_status(worktree, "", fileindex, repo,
7586 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7587 if (err)
7588 goto done;
7590 if (TAILQ_EMPTY(&commitable_paths)) {
7591 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7592 "merge of %s cannot proceed", branch_name);
7593 goto done;
7596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7597 struct got_commitable *ct = pe->data;
7598 const char *ct_path = ct->in_repo_path;
7600 while (ct_path[0] == '/')
7601 ct_path++;
7602 err = check_out_of_date(ct_path, ct->status,
7603 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7604 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7605 if (err)
7606 goto done;
7610 mcm_arg.worktree = worktree;
7611 mcm_arg.branch_name = branch_name;
7612 err = commit_worktree(new_commit_id, &commitable_paths,
7613 head_commit_id, branch_tip, worktree, author, committer,
7614 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7615 if (err)
7616 goto done;
7618 err = update_fileindex_after_commit(worktree, &commitable_paths,
7619 *new_commit_id, fileindex, have_staged_files);
7620 sync_err = sync_fileindex(fileindex, fileindex_path);
7621 if (sync_err && err == NULL)
7622 err = sync_err;
7623 done:
7624 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7625 struct got_commitable *ct = pe->data;
7626 free_commitable(ct);
7628 got_pathlist_free(&commitable_paths);
7629 free(fileindex_path);
7630 return err;
7633 const struct got_error *
7634 got_worktree_merge_complete(struct got_worktree *worktree,
7635 struct got_fileindex *fileindex, struct got_repository *repo)
7637 const struct got_error *err, *unlockerr, *sync_err;
7638 char *fileindex_path = NULL;
7640 err = delete_merge_refs(worktree, repo);
7641 if (err)
7642 goto done;
7644 err = get_fileindex_path(&fileindex_path, worktree);
7645 if (err)
7646 goto done;
7647 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7648 sync_err = sync_fileindex(fileindex, fileindex_path);
7649 if (sync_err && err == NULL)
7650 err = sync_err;
7651 done:
7652 got_fileindex_free(fileindex);
7653 free(fileindex_path);
7654 unlockerr = lock_worktree(worktree, LOCK_SH);
7655 if (unlockerr && err == NULL)
7656 err = unlockerr;
7657 return err;
7660 const struct got_error *
7661 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7662 struct got_repository *repo)
7664 const struct got_error *err;
7665 char *branch_refname = NULL;
7666 struct got_reference *branch_ref = NULL;
7668 *in_progress = 0;
7670 err = get_merge_branch_ref_name(&branch_refname, worktree);
7671 if (err)
7672 return err;
7673 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7674 free(branch_refname);
7675 if (err) {
7676 if (err->code != GOT_ERR_NOT_REF)
7677 return err;
7678 } else
7679 *in_progress = 1;
7681 return NULL;
7684 const struct got_error *got_worktree_merge_prepare(
7685 struct got_fileindex **fileindex, struct got_worktree *worktree,
7686 struct got_reference *branch, struct got_repository *repo)
7688 const struct got_error *err = NULL;
7689 char *fileindex_path = NULL;
7690 char *branch_refname = NULL, *commit_refname = NULL;
7691 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7692 struct got_reference *commit_ref = NULL;
7693 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7694 struct check_rebase_ok_arg ok_arg;
7696 *fileindex = NULL;
7698 err = lock_worktree(worktree, LOCK_EX);
7699 if (err)
7700 return err;
7702 err = open_fileindex(fileindex, &fileindex_path, worktree);
7703 if (err)
7704 goto done;
7706 /* Preconditions are the same as for rebase. */
7707 ok_arg.worktree = worktree;
7708 ok_arg.repo = repo;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7710 &ok_arg);
7711 if (err)
7712 goto done;
7714 err = get_merge_branch_ref_name(&branch_refname, worktree);
7715 if (err)
7716 return err;
7718 err = get_merge_commit_ref_name(&commit_refname, worktree);
7719 if (err)
7720 return err;
7722 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7723 0);
7724 if (err)
7725 goto done;
7727 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7728 if (err)
7729 goto done;
7731 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7732 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7733 goto done;
7736 err = got_ref_resolve(&branch_tip, repo, branch);
7737 if (err)
7738 goto done;
7740 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7741 if (err)
7742 goto done;
7743 err = got_ref_write(branch_ref, repo);
7744 if (err)
7745 goto done;
7747 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7748 if (err)
7749 goto done;
7750 err = got_ref_write(commit_ref, repo);
7751 if (err)
7752 goto done;
7754 done:
7755 free(branch_refname);
7756 free(commit_refname);
7757 free(fileindex_path);
7758 if (branch_ref)
7759 got_ref_close(branch_ref);
7760 if (commit_ref)
7761 got_ref_close(commit_ref);
7762 if (wt_branch)
7763 got_ref_close(wt_branch);
7764 free(wt_branch_tip);
7765 if (err) {
7766 if (*fileindex) {
7767 got_fileindex_free(*fileindex);
7768 *fileindex = NULL;
7770 lock_worktree(worktree, LOCK_SH);
7772 return err;
7775 const struct got_error *
7776 got_worktree_merge_continue(char **branch_name,
7777 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7778 struct got_worktree *worktree, struct got_repository *repo)
7780 const struct got_error *err;
7781 char *commit_refname = NULL, *branch_refname = NULL;
7782 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7783 char *fileindex_path = NULL;
7784 int have_staged_files = 0;
7786 *branch_name = NULL;
7787 *branch_tip = NULL;
7788 *fileindex = NULL;
7790 err = lock_worktree(worktree, LOCK_EX);
7791 if (err)
7792 return err;
7794 err = open_fileindex(fileindex, &fileindex_path, worktree);
7795 if (err)
7796 goto done;
7798 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7799 &have_staged_files);
7800 if (err && err->code != GOT_ERR_CANCELLED)
7801 goto done;
7802 if (have_staged_files) {
7803 err = got_error(GOT_ERR_STAGED_PATHS);
7804 goto done;
7807 err = get_merge_branch_ref_name(&branch_refname, worktree);
7808 if (err)
7809 goto done;
7811 err = get_merge_commit_ref_name(&commit_refname, worktree);
7812 if (err)
7813 goto done;
7815 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7816 if (err)
7817 goto done;
7819 if (!got_ref_is_symbolic(branch_ref)) {
7820 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7821 "%s is not a symbolic reference",
7822 got_ref_get_name(branch_ref));
7823 goto done;
7825 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7826 if (*branch_name == NULL) {
7827 err = got_error_from_errno("strdup");
7828 goto done;
7831 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7832 if (err)
7833 goto done;
7835 err = got_ref_resolve(branch_tip, repo, commit_ref);
7836 if (err)
7837 goto done;
7838 done:
7839 free(commit_refname);
7840 free(branch_refname);
7841 free(fileindex_path);
7842 if (commit_ref)
7843 got_ref_close(commit_ref);
7844 if (branch_ref)
7845 got_ref_close(branch_ref);
7846 if (err) {
7847 if (*branch_name) {
7848 free(*branch_name);
7849 *branch_name = NULL;
7851 free(*branch_tip);
7852 *branch_tip = NULL;
7853 if (*fileindex) {
7854 got_fileindex_free(*fileindex);
7855 *fileindex = NULL;
7857 lock_worktree(worktree, LOCK_SH);
7859 return err;
7862 const struct got_error *
7863 got_worktree_merge_abort(struct got_worktree *worktree,
7864 struct got_fileindex *fileindex, struct got_repository *repo,
7865 got_worktree_checkout_cb progress_cb, void *progress_arg)
7867 const struct got_error *err, *unlockerr, *sync_err;
7868 struct got_object_id *commit_id = NULL;
7869 char *fileindex_path = NULL;
7870 struct revert_file_args rfa;
7871 struct got_object_id *tree_id = NULL;
7873 err = got_object_id_by_path(&tree_id, repo,
7874 worktree->base_commit_id, worktree->path_prefix);
7875 if (err)
7876 goto done;
7878 err = delete_merge_refs(worktree, repo);
7879 if (err)
7880 goto done;
7882 err = get_fileindex_path(&fileindex_path, worktree);
7883 if (err)
7884 goto done;
7886 rfa.worktree = worktree;
7887 rfa.fileindex = fileindex;
7888 rfa.progress_cb = progress_cb;
7889 rfa.progress_arg = progress_arg;
7890 rfa.patch_cb = NULL;
7891 rfa.patch_arg = NULL;
7892 rfa.repo = repo;
7893 rfa.unlink_added_files = 1;
7894 err = worktree_status(worktree, "", fileindex, repo,
7895 revert_file, &rfa, NULL, NULL, 0, 0);
7896 if (err)
7897 goto sync;
7899 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7900 repo, progress_cb, progress_arg, NULL, NULL);
7901 sync:
7902 sync_err = sync_fileindex(fileindex, fileindex_path);
7903 if (sync_err && err == NULL)
7904 err = sync_err;
7905 done:
7906 free(tree_id);
7907 free(commit_id);
7908 if (fileindex)
7909 got_fileindex_free(fileindex);
7910 free(fileindex_path);
7912 unlockerr = lock_worktree(worktree, LOCK_SH);
7913 if (unlockerr && err == NULL)
7914 err = unlockerr;
7915 return err;
7918 struct check_stage_ok_arg {
7919 struct got_object_id *head_commit_id;
7920 struct got_worktree *worktree;
7921 struct got_fileindex *fileindex;
7922 struct got_repository *repo;
7923 int have_changes;
7926 const struct got_error *
7927 check_stage_ok(void *arg, unsigned char status,
7928 unsigned char staged_status, const char *relpath,
7929 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7930 struct got_object_id *commit_id, int dirfd, const char *de_name)
7932 struct check_stage_ok_arg *a = arg;
7933 const struct got_error *err = NULL;
7934 struct got_fileindex_entry *ie;
7935 struct got_object_id base_commit_id;
7936 struct got_object_id *base_commit_idp = NULL;
7937 char *in_repo_path = NULL, *p;
7939 if (status == GOT_STATUS_UNVERSIONED ||
7940 status == GOT_STATUS_NO_CHANGE)
7941 return NULL;
7942 if (status == GOT_STATUS_NONEXISTENT)
7943 return got_error_set_errno(ENOENT, relpath);
7945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7946 if (ie == NULL)
7947 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7949 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7950 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7951 relpath) == -1)
7952 return got_error_from_errno("asprintf");
7954 if (got_fileindex_entry_has_commit(ie)) {
7955 memcpy(base_commit_id.sha1, ie->commit_sha1,
7956 SHA1_DIGEST_LENGTH);
7957 base_commit_idp = &base_commit_id;
7960 if (status == GOT_STATUS_CONFLICT) {
7961 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7962 goto done;
7963 } else if (status != GOT_STATUS_ADD &&
7964 status != GOT_STATUS_MODIFY &&
7965 status != GOT_STATUS_DELETE) {
7966 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7967 goto done;
7970 a->have_changes = 1;
7972 p = in_repo_path;
7973 while (p[0] == '/')
7974 p++;
7975 err = check_out_of_date(p, status, staged_status,
7976 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7977 GOT_ERR_STAGE_OUT_OF_DATE);
7978 done:
7979 free(in_repo_path);
7980 return err;
7983 struct stage_path_arg {
7984 struct got_worktree *worktree;
7985 struct got_fileindex *fileindex;
7986 struct got_repository *repo;
7987 got_worktree_status_cb status_cb;
7988 void *status_arg;
7989 got_worktree_patch_cb patch_cb;
7990 void *patch_arg;
7991 int staged_something;
7992 int allow_bad_symlinks;
7995 static const struct got_error *
7996 stage_path(void *arg, unsigned char status,
7997 unsigned char staged_status, const char *relpath,
7998 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7999 struct got_object_id *commit_id, int dirfd, const char *de_name)
8001 struct stage_path_arg *a = arg;
8002 const struct got_error *err = NULL;
8003 struct got_fileindex_entry *ie;
8004 char *ondisk_path = NULL, *path_content = NULL;
8005 uint32_t stage;
8006 struct got_object_id *new_staged_blob_id = NULL;
8007 struct stat sb;
8009 if (status == GOT_STATUS_UNVERSIONED)
8010 return NULL;
8012 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8013 if (ie == NULL)
8014 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8016 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8017 relpath)== -1)
8018 return got_error_from_errno("asprintf");
8020 switch (status) {
8021 case GOT_STATUS_ADD:
8022 case GOT_STATUS_MODIFY:
8023 /* XXX could sb.st_mode be passed in by our caller? */
8024 if (lstat(ondisk_path, &sb) == -1) {
8025 err = got_error_from_errno2("lstat", ondisk_path);
8026 break;
8028 if (a->patch_cb) {
8029 if (status == GOT_STATUS_ADD) {
8030 int choice = GOT_PATCH_CHOICE_NONE;
8031 err = (*a->patch_cb)(&choice, a->patch_arg,
8032 status, ie->path, NULL, 1, 1);
8033 if (err)
8034 break;
8035 if (choice != GOT_PATCH_CHOICE_YES)
8036 break;
8037 } else {
8038 err = create_patched_content(&path_content, 0,
8039 staged_blob_id ? staged_blob_id : blob_id,
8040 ondisk_path, dirfd, de_name, ie->path,
8041 a->repo, a->patch_cb, a->patch_arg);
8042 if (err || path_content == NULL)
8043 break;
8046 err = got_object_blob_create(&new_staged_blob_id,
8047 path_content ? path_content : ondisk_path, a->repo);
8048 if (err)
8049 break;
8050 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8051 SHA1_DIGEST_LENGTH);
8052 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8053 stage = GOT_FILEIDX_STAGE_ADD;
8054 else
8055 stage = GOT_FILEIDX_STAGE_MODIFY;
8056 got_fileindex_entry_stage_set(ie, stage);
8057 if (S_ISLNK(sb.st_mode)) {
8058 int is_bad_symlink = 0;
8059 if (!a->allow_bad_symlinks) {
8060 char target_path[PATH_MAX];
8061 ssize_t target_len;
8062 target_len = readlink(ondisk_path, target_path,
8063 sizeof(target_path));
8064 if (target_len == -1) {
8065 err = got_error_from_errno2("readlink",
8066 ondisk_path);
8067 break;
8069 err = is_bad_symlink_target(&is_bad_symlink,
8070 target_path, target_len, ondisk_path,
8071 a->worktree->root_path);
8072 if (err)
8073 break;
8074 if (is_bad_symlink) {
8075 err = got_error_path(ondisk_path,
8076 GOT_ERR_BAD_SYMLINK);
8077 break;
8080 if (is_bad_symlink)
8081 got_fileindex_entry_staged_filetype_set(ie,
8082 GOT_FILEIDX_MODE_BAD_SYMLINK);
8083 else
8084 got_fileindex_entry_staged_filetype_set(ie,
8085 GOT_FILEIDX_MODE_SYMLINK);
8086 } else {
8087 got_fileindex_entry_staged_filetype_set(ie,
8088 GOT_FILEIDX_MODE_REGULAR_FILE);
8090 a->staged_something = 1;
8091 if (a->status_cb == NULL)
8092 break;
8093 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8094 get_staged_status(ie), relpath, blob_id,
8095 new_staged_blob_id, NULL, dirfd, de_name);
8096 break;
8097 case GOT_STATUS_DELETE:
8098 if (staged_status == GOT_STATUS_DELETE)
8099 break;
8100 if (a->patch_cb) {
8101 int choice = GOT_PATCH_CHOICE_NONE;
8102 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8103 ie->path, NULL, 1, 1);
8104 if (err)
8105 break;
8106 if (choice == GOT_PATCH_CHOICE_NO)
8107 break;
8108 if (choice != GOT_PATCH_CHOICE_YES) {
8109 err = got_error(GOT_ERR_PATCH_CHOICE);
8110 break;
8113 stage = GOT_FILEIDX_STAGE_DELETE;
8114 got_fileindex_entry_stage_set(ie, stage);
8115 a->staged_something = 1;
8116 if (a->status_cb == NULL)
8117 break;
8118 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8119 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8120 de_name);
8121 break;
8122 case GOT_STATUS_NO_CHANGE:
8123 break;
8124 case GOT_STATUS_CONFLICT:
8125 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8126 break;
8127 case GOT_STATUS_NONEXISTENT:
8128 err = got_error_set_errno(ENOENT, relpath);
8129 break;
8130 default:
8131 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8132 break;
8135 if (path_content && unlink(path_content) == -1 && err == NULL)
8136 err = got_error_from_errno2("unlink", path_content);
8137 free(path_content);
8138 free(ondisk_path);
8139 free(new_staged_blob_id);
8140 return err;
8143 const struct got_error *
8144 got_worktree_stage(struct got_worktree *worktree,
8145 struct got_pathlist_head *paths,
8146 got_worktree_status_cb status_cb, void *status_arg,
8147 got_worktree_patch_cb patch_cb, void *patch_arg,
8148 int allow_bad_symlinks, struct got_repository *repo)
8150 const struct got_error *err = NULL, *sync_err, *unlockerr;
8151 struct got_pathlist_entry *pe;
8152 struct got_fileindex *fileindex = NULL;
8153 char *fileindex_path = NULL;
8154 struct got_reference *head_ref = NULL;
8155 struct got_object_id *head_commit_id = NULL;
8156 struct check_stage_ok_arg oka;
8157 struct stage_path_arg spa;
8159 err = lock_worktree(worktree, LOCK_EX);
8160 if (err)
8161 return err;
8163 err = got_ref_open(&head_ref, repo,
8164 got_worktree_get_head_ref_name(worktree), 0);
8165 if (err)
8166 goto done;
8167 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8168 if (err)
8169 goto done;
8170 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8171 if (err)
8172 goto done;
8174 /* Check pre-conditions before staging anything. */
8175 oka.head_commit_id = head_commit_id;
8176 oka.worktree = worktree;
8177 oka.fileindex = fileindex;
8178 oka.repo = repo;
8179 oka.have_changes = 0;
8180 TAILQ_FOREACH(pe, paths, entry) {
8181 err = worktree_status(worktree, pe->path, fileindex, repo,
8182 check_stage_ok, &oka, NULL, NULL, 0, 0);
8183 if (err)
8184 goto done;
8186 if (!oka.have_changes) {
8187 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8188 goto done;
8191 spa.worktree = worktree;
8192 spa.fileindex = fileindex;
8193 spa.repo = repo;
8194 spa.patch_cb = patch_cb;
8195 spa.patch_arg = patch_arg;
8196 spa.status_cb = status_cb;
8197 spa.status_arg = status_arg;
8198 spa.staged_something = 0;
8199 spa.allow_bad_symlinks = allow_bad_symlinks;
8200 TAILQ_FOREACH(pe, paths, entry) {
8201 err = worktree_status(worktree, pe->path, fileindex, repo,
8202 stage_path, &spa, NULL, NULL, 0, 0);
8203 if (err)
8204 goto done;
8206 if (!spa.staged_something) {
8207 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8208 goto done;
8211 sync_err = sync_fileindex(fileindex, fileindex_path);
8212 if (sync_err && err == NULL)
8213 err = sync_err;
8214 done:
8215 if (head_ref)
8216 got_ref_close(head_ref);
8217 free(head_commit_id);
8218 free(fileindex_path);
8219 if (fileindex)
8220 got_fileindex_free(fileindex);
8221 unlockerr = lock_worktree(worktree, LOCK_SH);
8222 if (unlockerr && err == NULL)
8223 err = unlockerr;
8224 return err;
8227 struct unstage_path_arg {
8228 struct got_worktree *worktree;
8229 struct got_fileindex *fileindex;
8230 struct got_repository *repo;
8231 got_worktree_checkout_cb progress_cb;
8232 void *progress_arg;
8233 got_worktree_patch_cb patch_cb;
8234 void *patch_arg;
8237 static const struct got_error *
8238 create_unstaged_content(char **path_unstaged_content,
8239 char **path_new_staged_content, struct got_object_id *blob_id,
8240 struct got_object_id *staged_blob_id, const char *relpath,
8241 struct got_repository *repo,
8242 got_worktree_patch_cb patch_cb, void *patch_arg)
8244 const struct got_error *err, *free_err;
8245 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8246 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8247 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8248 struct got_diffreg_result *diffreg_result = NULL;
8249 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8250 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8252 *path_unstaged_content = NULL;
8253 *path_new_staged_content = NULL;
8255 err = got_object_id_str(&label1, blob_id);
8256 if (err)
8257 return err;
8258 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8259 if (err)
8260 goto done;
8262 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8263 if (err)
8264 goto done;
8266 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8267 if (err)
8268 goto done;
8270 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8271 if (err)
8272 goto done;
8274 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8275 if (err)
8276 goto done;
8278 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8279 if (err)
8280 goto done;
8282 err = got_diff_files(&diffreg_result, f1, label1, f2,
8283 path2, 3, 0, 1, NULL);
8284 if (err)
8285 goto done;
8287 err = got_opentemp_named(path_unstaged_content, &outfile,
8288 "got-unstaged-content");
8289 if (err)
8290 goto done;
8291 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8292 "got-new-staged-content");
8293 if (err)
8294 goto done;
8296 if (fseek(f1, 0L, SEEK_SET) == -1) {
8297 err = got_ferror(f1, GOT_ERR_IO);
8298 goto done;
8300 if (fseek(f2, 0L, SEEK_SET) == -1) {
8301 err = got_ferror(f2, GOT_ERR_IO);
8302 goto done;
8304 /* Count the number of actual changes in the diff result. */
8305 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8306 struct diff_chunk_context cc = {};
8307 diff_chunk_context_load_change(&cc, &nchunks_used,
8308 diffreg_result->result, n, 0);
8309 nchanges++;
8311 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8312 int choice;
8313 err = apply_or_reject_change(&choice, &nchunks_used,
8314 diffreg_result->result, n, relpath, f1, f2,
8315 &line_cur1, &line_cur2,
8316 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8317 if (err)
8318 goto done;
8319 if (choice == GOT_PATCH_CHOICE_YES)
8320 have_content = 1;
8321 else
8322 have_rejected_content = 1;
8323 if (choice == GOT_PATCH_CHOICE_QUIT)
8324 break;
8326 if (have_content || have_rejected_content)
8327 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8328 outfile, rejectfile);
8329 done:
8330 free(label1);
8331 if (blob)
8332 got_object_blob_close(blob);
8333 if (staged_blob)
8334 got_object_blob_close(staged_blob);
8335 free_err = got_diffreg_result_free(diffreg_result);
8336 if (free_err && err == NULL)
8337 err = free_err;
8338 if (f1 && fclose(f1) == EOF && err == NULL)
8339 err = got_error_from_errno2("fclose", path1);
8340 if (f2 && fclose(f2) == EOF && err == NULL)
8341 err = got_error_from_errno2("fclose", path2);
8342 if (outfile && fclose(outfile) == EOF && err == NULL)
8343 err = got_error_from_errno2("fclose", *path_unstaged_content);
8344 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8345 err = got_error_from_errno2("fclose", *path_new_staged_content);
8346 if (path1 && unlink(path1) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink", path1);
8348 if (path2 && unlink(path2) == -1 && err == NULL)
8349 err = got_error_from_errno2("unlink", path2);
8350 if (err || !have_content) {
8351 if (*path_unstaged_content &&
8352 unlink(*path_unstaged_content) == -1 && err == NULL)
8353 err = got_error_from_errno2("unlink",
8354 *path_unstaged_content);
8355 free(*path_unstaged_content);
8356 *path_unstaged_content = NULL;
8358 if (err || !have_content || !have_rejected_content) {
8359 if (*path_new_staged_content &&
8360 unlink(*path_new_staged_content) == -1 && err == NULL)
8361 err = got_error_from_errno2("unlink",
8362 *path_new_staged_content);
8363 free(*path_new_staged_content);
8364 *path_new_staged_content = NULL;
8366 free(path1);
8367 free(path2);
8368 return err;
8371 static const struct got_error *
8372 unstage_hunks(struct got_object_id *staged_blob_id,
8373 struct got_blob_object *blob_base,
8374 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8375 const char *ondisk_path, const char *label_orig,
8376 struct got_worktree *worktree, struct got_repository *repo,
8377 got_worktree_patch_cb patch_cb, void *patch_arg,
8378 got_worktree_checkout_cb progress_cb, void *progress_arg)
8380 const struct got_error *err = NULL;
8381 char *path_unstaged_content = NULL;
8382 char *path_new_staged_content = NULL;
8383 char *parent = NULL, *base_path = NULL;
8384 char *blob_base_path = NULL;
8385 struct got_object_id *new_staged_blob_id = NULL;
8386 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8387 struct stat sb;
8389 err = create_unstaged_content(&path_unstaged_content,
8390 &path_new_staged_content, blob_id, staged_blob_id,
8391 ie->path, repo, patch_cb, patch_arg);
8392 if (err)
8393 return err;
8395 if (path_unstaged_content == NULL)
8396 return NULL;
8398 if (path_new_staged_content) {
8399 err = got_object_blob_create(&new_staged_blob_id,
8400 path_new_staged_content, repo);
8401 if (err)
8402 goto done;
8405 f = fopen(path_unstaged_content, "r");
8406 if (f == NULL) {
8407 err = got_error_from_errno2("fopen",
8408 path_unstaged_content);
8409 goto done;
8411 if (fstat(fileno(f), &sb) == -1) {
8412 err = got_error_from_errno2("fstat", path_unstaged_content);
8413 goto done;
8415 if (got_fileindex_entry_staged_filetype_get(ie) ==
8416 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8417 char link_target[PATH_MAX];
8418 size_t r;
8419 r = fread(link_target, 1, sizeof(link_target), f);
8420 if (r == 0 && ferror(f)) {
8421 err = got_error_from_errno("fread");
8422 goto done;
8424 if (r >= sizeof(link_target)) { /* should not happen */
8425 err = got_error(GOT_ERR_NO_SPACE);
8426 goto done;
8428 link_target[r] = '\0';
8429 err = merge_symlink(worktree, blob_base,
8430 ondisk_path, ie->path, label_orig, link_target,
8431 worktree->base_commit_id, repo, progress_cb,
8432 progress_arg);
8433 } else {
8434 int local_changes_subsumed;
8436 err = got_path_dirname(&parent, ondisk_path);
8437 if (err)
8438 return err;
8440 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8441 parent) == -1) {
8442 err = got_error_from_errno("asprintf");
8443 base_path = NULL;
8444 goto done;
8447 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8448 if (err)
8449 goto done;
8450 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8451 blob_base);
8452 if (err)
8453 goto done;
8456 * In order the run a 3-way merge with a symlink we copy the symlink's
8457 * target path into a temporary file and use that file with diff3.
8459 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8460 err = dump_symlink_target_path_to_file(&f_deriv2,
8461 ondisk_path);
8462 if (err)
8463 goto done;
8464 } else {
8465 int fd;
8466 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8467 if (fd == -1) {
8468 err = got_error_from_errno2("open", ondisk_path);
8469 goto done;
8471 f_deriv2 = fdopen(fd, "r");
8472 if (f_deriv2 == NULL) {
8473 err = got_error_from_errno2("fdopen", ondisk_path);
8474 close(fd);
8475 goto done;
8479 err = merge_file(&local_changes_subsumed, worktree,
8480 f_base, f, f_deriv2, ondisk_path, ie->path,
8481 got_fileindex_perms_to_st(ie),
8482 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8483 repo, progress_cb, progress_arg);
8485 if (err)
8486 goto done;
8488 if (new_staged_blob_id) {
8489 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8490 SHA1_DIGEST_LENGTH);
8491 } else {
8492 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8493 got_fileindex_entry_staged_filetype_set(ie, 0);
8495 done:
8496 free(new_staged_blob_id);
8497 if (path_unstaged_content &&
8498 unlink(path_unstaged_content) == -1 && err == NULL)
8499 err = got_error_from_errno2("unlink", path_unstaged_content);
8500 if (path_new_staged_content &&
8501 unlink(path_new_staged_content) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", path_new_staged_content);
8503 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8504 err = got_error_from_errno2("unlink", blob_base_path);
8505 if (f_base && fclose(f_base) == EOF && err == NULL)
8506 err = got_error_from_errno2("fclose", path_unstaged_content);
8507 if (f && fclose(f) == EOF && err == NULL)
8508 err = got_error_from_errno2("fclose", path_unstaged_content);
8509 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8510 err = got_error_from_errno2("fclose", ondisk_path);
8511 free(path_unstaged_content);
8512 free(path_new_staged_content);
8513 free(blob_base_path);
8514 free(parent);
8515 free(base_path);
8516 return err;
8519 static const struct got_error *
8520 unstage_path(void *arg, unsigned char status,
8521 unsigned char staged_status, const char *relpath,
8522 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8523 struct got_object_id *commit_id, int dirfd, const char *de_name)
8525 const struct got_error *err = NULL;
8526 struct unstage_path_arg *a = arg;
8527 struct got_fileindex_entry *ie;
8528 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8529 char *ondisk_path = NULL;
8530 char *id_str = NULL, *label_orig = NULL;
8531 int local_changes_subsumed;
8532 struct stat sb;
8534 if (staged_status != GOT_STATUS_ADD &&
8535 staged_status != GOT_STATUS_MODIFY &&
8536 staged_status != GOT_STATUS_DELETE)
8537 return NULL;
8539 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8540 if (ie == NULL)
8541 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8543 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8544 == -1)
8545 return got_error_from_errno("asprintf");
8547 err = got_object_id_str(&id_str,
8548 commit_id ? commit_id : a->worktree->base_commit_id);
8549 if (err)
8550 goto done;
8551 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8552 id_str) == -1) {
8553 err = got_error_from_errno("asprintf");
8554 goto done;
8557 switch (staged_status) {
8558 case GOT_STATUS_MODIFY:
8559 err = got_object_open_as_blob(&blob_base, a->repo,
8560 blob_id, 8192);
8561 if (err)
8562 break;
8563 /* fall through */
8564 case GOT_STATUS_ADD:
8565 if (a->patch_cb) {
8566 if (staged_status == GOT_STATUS_ADD) {
8567 int choice = GOT_PATCH_CHOICE_NONE;
8568 err = (*a->patch_cb)(&choice, a->patch_arg,
8569 staged_status, ie->path, NULL, 1, 1);
8570 if (err)
8571 break;
8572 if (choice != GOT_PATCH_CHOICE_YES)
8573 break;
8574 } else {
8575 err = unstage_hunks(staged_blob_id,
8576 blob_base, blob_id, ie, ondisk_path,
8577 label_orig, a->worktree, a->repo,
8578 a->patch_cb, a->patch_arg,
8579 a->progress_cb, a->progress_arg);
8580 break; /* Done with this file. */
8583 err = got_object_open_as_blob(&blob_staged, a->repo,
8584 staged_blob_id, 8192);
8585 if (err)
8586 break;
8587 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8588 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8589 case GOT_FILEIDX_MODE_REGULAR_FILE:
8590 err = merge_blob(&local_changes_subsumed, a->worktree,
8591 blob_base, ondisk_path, relpath,
8592 got_fileindex_perms_to_st(ie), label_orig,
8593 blob_staged, commit_id ? commit_id :
8594 a->worktree->base_commit_id, a->repo,
8595 a->progress_cb, a->progress_arg);
8596 break;
8597 case GOT_FILEIDX_MODE_SYMLINK:
8598 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8599 char *staged_target;
8600 err = got_object_blob_read_to_str(
8601 &staged_target, blob_staged);
8602 if (err)
8603 goto done;
8604 err = merge_symlink(a->worktree, blob_base,
8605 ondisk_path, relpath, label_orig,
8606 staged_target, commit_id ? commit_id :
8607 a->worktree->base_commit_id,
8608 a->repo, a->progress_cb, a->progress_arg);
8609 free(staged_target);
8610 } else {
8611 err = merge_blob(&local_changes_subsumed,
8612 a->worktree, blob_base, ondisk_path,
8613 relpath, got_fileindex_perms_to_st(ie),
8614 label_orig, blob_staged,
8615 commit_id ? commit_id :
8616 a->worktree->base_commit_id, a->repo,
8617 a->progress_cb, a->progress_arg);
8619 break;
8620 default:
8621 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8622 break;
8624 if (err == NULL) {
8625 got_fileindex_entry_stage_set(ie,
8626 GOT_FILEIDX_STAGE_NONE);
8627 got_fileindex_entry_staged_filetype_set(ie, 0);
8629 break;
8630 case GOT_STATUS_DELETE:
8631 if (a->patch_cb) {
8632 int choice = GOT_PATCH_CHOICE_NONE;
8633 err = (*a->patch_cb)(&choice, a->patch_arg,
8634 staged_status, ie->path, NULL, 1, 1);
8635 if (err)
8636 break;
8637 if (choice == GOT_PATCH_CHOICE_NO)
8638 break;
8639 if (choice != GOT_PATCH_CHOICE_YES) {
8640 err = got_error(GOT_ERR_PATCH_CHOICE);
8641 break;
8644 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8645 got_fileindex_entry_staged_filetype_set(ie, 0);
8646 err = get_file_status(&status, &sb, ie, ondisk_path,
8647 dirfd, de_name, a->repo);
8648 if (err)
8649 break;
8650 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8651 break;
8653 done:
8654 free(ondisk_path);
8655 if (blob_base)
8656 got_object_blob_close(blob_base);
8657 if (blob_staged)
8658 got_object_blob_close(blob_staged);
8659 free(id_str);
8660 free(label_orig);
8661 return err;
8664 const struct got_error *
8665 got_worktree_unstage(struct got_worktree *worktree,
8666 struct got_pathlist_head *paths,
8667 got_worktree_checkout_cb progress_cb, void *progress_arg,
8668 got_worktree_patch_cb patch_cb, void *patch_arg,
8669 struct got_repository *repo)
8671 const struct got_error *err = NULL, *sync_err, *unlockerr;
8672 struct got_pathlist_entry *pe;
8673 struct got_fileindex *fileindex = NULL;
8674 char *fileindex_path = NULL;
8675 struct unstage_path_arg upa;
8677 err = lock_worktree(worktree, LOCK_EX);
8678 if (err)
8679 return err;
8681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8682 if (err)
8683 goto done;
8685 upa.worktree = worktree;
8686 upa.fileindex = fileindex;
8687 upa.repo = repo;
8688 upa.progress_cb = progress_cb;
8689 upa.progress_arg = progress_arg;
8690 upa.patch_cb = patch_cb;
8691 upa.patch_arg = patch_arg;
8692 TAILQ_FOREACH(pe, paths, entry) {
8693 err = worktree_status(worktree, pe->path, fileindex, repo,
8694 unstage_path, &upa, NULL, NULL, 0, 0);
8695 if (err)
8696 goto done;
8699 sync_err = sync_fileindex(fileindex, fileindex_path);
8700 if (sync_err && err == NULL)
8701 err = sync_err;
8702 done:
8703 free(fileindex_path);
8704 if (fileindex)
8705 got_fileindex_free(fileindex);
8706 unlockerr = lock_worktree(worktree, LOCK_SH);
8707 if (unlockerr && err == NULL)
8708 err = unlockerr;
8709 return err;
8712 struct report_file_info_arg {
8713 struct got_worktree *worktree;
8714 got_worktree_path_info_cb info_cb;
8715 void *info_arg;
8716 struct got_pathlist_head *paths;
8717 got_cancel_cb cancel_cb;
8718 void *cancel_arg;
8721 static const struct got_error *
8722 report_file_info(void *arg, struct got_fileindex_entry *ie)
8724 struct report_file_info_arg *a = arg;
8725 struct got_pathlist_entry *pe;
8726 struct got_object_id blob_id, staged_blob_id, commit_id;
8727 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8728 struct got_object_id *commit_idp = NULL;
8729 int stage;
8731 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8732 return got_error(GOT_ERR_CANCELLED);
8734 TAILQ_FOREACH(pe, a->paths, entry) {
8735 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8736 got_path_is_child(ie->path, pe->path, pe->path_len))
8737 break;
8739 if (pe == NULL) /* not found */
8740 return NULL;
8742 if (got_fileindex_entry_has_blob(ie)) {
8743 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8744 blob_idp = &blob_id;
8746 stage = got_fileindex_entry_stage_get(ie);
8747 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8748 stage == GOT_FILEIDX_STAGE_ADD) {
8749 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8750 SHA1_DIGEST_LENGTH);
8751 staged_blob_idp = &staged_blob_id;
8754 if (got_fileindex_entry_has_commit(ie)) {
8755 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8756 commit_idp = &commit_id;
8759 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8760 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8763 const struct got_error *
8764 got_worktree_path_info(struct got_worktree *worktree,
8765 struct got_pathlist_head *paths,
8766 got_worktree_path_info_cb info_cb, void *info_arg,
8767 got_cancel_cb cancel_cb, void *cancel_arg)
8770 const struct got_error *err = NULL, *unlockerr;
8771 struct got_fileindex *fileindex = NULL;
8772 char *fileindex_path = NULL;
8773 struct report_file_info_arg arg;
8775 err = lock_worktree(worktree, LOCK_SH);
8776 if (err)
8777 return err;
8779 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8780 if (err)
8781 goto done;
8783 arg.worktree = worktree;
8784 arg.info_cb = info_cb;
8785 arg.info_arg = info_arg;
8786 arg.paths = paths;
8787 arg.cancel_cb = cancel_cb;
8788 arg.cancel_arg = cancel_arg;
8789 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8790 &arg);
8791 done:
8792 free(fileindex_path);
8793 if (fileindex)
8794 got_fileindex_free(fileindex);
8795 unlockerr = lock_worktree(worktree, LOCK_UN);
8796 if (unlockerr && err == NULL)
8797 err = unlockerr;
8798 return err;