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 (!got_err_open_nofollow_on_symlink())
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 &&
1783 !got_err_open_nofollow_on_symlink())
1784 return got_error_from_errno2("open", abspath);
1785 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1786 if (lstat(abspath, sb) == -1)
1787 return got_error_from_errno2("lstat", abspath);
1788 } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 if (errno == ENOENT) {
1790 if (got_fileindex_entry_has_file_on_disk(ie))
1791 *status = GOT_STATUS_MISSING;
1792 else
1793 *status = GOT_STATUS_DELETE;
1794 goto done;
1796 err = got_error_from_errno2("fstat", abspath);
1797 goto done;
1801 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 *status = GOT_STATUS_OBSTRUCTED;
1803 goto done;
1806 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 *status = GOT_STATUS_DELETE;
1808 goto done;
1809 } else if (!got_fileindex_entry_has_blob(ie) &&
1810 staged_status != GOT_STATUS_ADD) {
1811 *status = GOT_STATUS_ADD;
1812 goto done;
1815 if (!stat_info_differs(ie, sb))
1816 goto done;
1818 if (S_ISLNK(sb->st_mode) &&
1819 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 *status = GOT_STATUS_MODIFY;
1821 goto done;
1824 if (staged_status == GOT_STATUS_MODIFY ||
1825 staged_status == GOT_STATUS_ADD)
1826 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 else
1828 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 if (err)
1832 goto done;
1834 if (S_ISLNK(sb->st_mode)) {
1835 err = get_symlink_modification_status(status, ie,
1836 abspath, dirfd, de_name, blob);
1837 goto done;
1840 if (dirfd != -1) {
1841 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 if (fd == -1) {
1843 err = got_error_from_errno2("openat", abspath);
1844 goto done;
1848 f = fdopen(fd, "r");
1849 if (f == NULL) {
1850 err = got_error_from_errno2("fdopen", abspath);
1851 goto done;
1853 fd = -1;
1854 hdrlen = got_object_blob_get_hdrlen(blob);
1855 for (;;) {
1856 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 err = got_object_blob_read_block(&blen, blob);
1858 if (err)
1859 goto done;
1860 /* Skip length of blob object header first time around. */
1861 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 if (flen == 0 && ferror(f)) {
1863 err = got_error_from_errno("fread");
1864 goto done;
1866 if (blen - hdrlen == 0) {
1867 if (flen != 0)
1868 *status = GOT_STATUS_MODIFY;
1869 break;
1870 } else if (flen == 0) {
1871 if (blen - hdrlen != 0)
1872 *status = GOT_STATUS_MODIFY;
1873 break;
1874 } else if (blen - hdrlen == flen) {
1875 /* Skip blob object header first time around. */
1876 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 *status = GOT_STATUS_MODIFY;
1878 break;
1880 } else {
1881 *status = GOT_STATUS_MODIFY;
1882 break;
1884 hdrlen = 0;
1887 if (*status == GOT_STATUS_MODIFY) {
1888 rewind(f);
1889 err = get_modified_file_content_status(status, f);
1890 } else if (xbit_differs(ie, sb->st_mode))
1891 *status = GOT_STATUS_MODE_CHANGE;
1892 done:
1893 if (blob)
1894 got_object_blob_close(blob);
1895 if (f != NULL && fclose(f) == EOF && err == NULL)
1896 err = got_error_from_errno2("fclose", abspath);
1897 if (fd != -1 && close(fd) == -1 && err == NULL)
1898 err = got_error_from_errno2("close", abspath);
1899 return err;
1903 * Update timestamps in the file index if a file is unmodified and
1904 * we had to run a full content comparison to find out.
1906 static const struct got_error *
1907 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 struct got_fileindex_entry *ie, struct stat *sb)
1910 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 return got_fileindex_entry_update(ie, wt_fd, path,
1912 ie->blob_sha1, ie->commit_sha1, 1);
1914 return NULL;
1917 static const struct got_error *
1918 update_blob(struct got_worktree *worktree,
1919 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 struct got_tree_entry *te, const char *path,
1921 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 void *progress_arg)
1924 const struct got_error *err = NULL;
1925 struct got_blob_object *blob = NULL;
1926 char *ondisk_path;
1927 unsigned char status = GOT_STATUS_NO_CHANGE;
1928 struct stat sb;
1930 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 return got_error_from_errno("asprintf");
1933 if (ie) {
1934 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 goto done;
1938 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 repo);
1940 if (err)
1941 goto done;
1942 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 sb.st_mode = got_fileindex_perms_to_st(ie);
1944 } else {
1945 if (stat(ondisk_path, &sb) == -1) {
1946 if (errno != ENOENT) {
1947 err = got_error_from_errno2("stat",
1948 ondisk_path);
1949 goto done;
1951 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1952 status = GOT_STATUS_UNVERSIONED;
1953 } else {
1954 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1955 status = GOT_STATUS_UNVERSIONED;
1956 else
1957 status = GOT_STATUS_OBSTRUCTED;
1961 if (status == GOT_STATUS_OBSTRUCTED) {
1962 if (ie)
1963 got_fileindex_entry_mark_skipped(ie);
1964 err = (*progress_cb)(progress_arg, status, path);
1965 goto done;
1967 if (status == GOT_STATUS_CONFLICT) {
1968 if (ie)
1969 got_fileindex_entry_mark_skipped(ie);
1970 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1971 path);
1972 goto done;
1975 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 (S_ISLNK(te->mode) ||
1977 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 * This is a regular file or an installed bad symlink.
1980 * If the file index indicates that this file is already
1981 * up-to-date with respect to the repository we can skip
1982 * updating contents of this file.
1984 if (got_fileindex_entry_has_commit(ie) &&
1985 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Same commit. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1990 if (err)
1991 goto done;
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 path);
1994 goto done;
1996 if (got_fileindex_entry_has_blob(ie) &&
1997 memcmp(ie->blob_sha1, te->id.sha1,
1998 SHA1_DIGEST_LENGTH) == 0) {
1999 /* Different commit but the same blob. */
2000 err = sync_timestamps(worktree->root_fd,
2001 path, status, ie, &sb);
2002 if (err)
2003 goto done;
2004 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2005 path);
2006 goto done;
2010 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2011 if (err)
2012 goto done;
2014 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2015 int update_timestamps;
2016 struct got_blob_object *blob2 = NULL;
2017 char *label_orig = NULL;
2018 if (got_fileindex_entry_has_blob(ie)) {
2019 struct got_object_id id2;
2020 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2021 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2022 if (err)
2023 goto done;
2025 if (got_fileindex_entry_has_commit(ie)) {
2026 char id_str[SHA1_DIGEST_STRING_LENGTH];
2027 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2028 sizeof(id_str)) == NULL) {
2029 err = got_error_path(id_str,
2030 GOT_ERR_BAD_OBJ_ID_STR);
2031 goto done;
2033 if (asprintf(&label_orig, "%s: commit %s",
2034 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2035 err = got_error_from_errno("asprintf");
2036 goto done;
2039 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2040 char *link_target;
2041 err = got_object_blob_read_to_str(&link_target, blob);
2042 if (err)
2043 goto done;
2044 err = merge_symlink(worktree, blob2, ondisk_path, path,
2045 label_orig, link_target, worktree->base_commit_id,
2046 repo, progress_cb, progress_arg);
2047 free(link_target);
2048 } else {
2049 err = merge_blob(&update_timestamps, worktree, blob2,
2050 ondisk_path, path, sb.st_mode, label_orig, blob,
2051 worktree->base_commit_id, repo,
2052 progress_cb, progress_arg);
2054 free(label_orig);
2055 if (blob2)
2056 got_object_blob_close(blob2);
2057 if (err)
2058 goto done;
2060 * Do not update timestamps of files with local changes.
2061 * Otherwise, a future status walk would treat them as
2062 * unmodified files again.
2064 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2065 blob->id.sha1, worktree->base_commit_id->sha1,
2066 update_timestamps);
2067 } else if (status == GOT_STATUS_MODE_CHANGE) {
2068 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2069 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2070 } else if (status == GOT_STATUS_DELETE) {
2071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2072 if (err)
2073 goto done;
2074 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2075 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2076 if (err)
2077 goto done;
2078 } else {
2079 int is_bad_symlink = 0;
2080 if (S_ISLNK(te->mode)) {
2081 err = install_symlink(&is_bad_symlink, worktree,
2082 ondisk_path, path, blob,
2083 status == GOT_STATUS_MISSING, 0,
2084 status == GOT_STATUS_UNVERSIONED, 0,
2085 repo, progress_cb, progress_arg);
2086 } else {
2087 err = install_blob(worktree, ondisk_path, path,
2088 te->mode, sb.st_mode, blob,
2089 status == GOT_STATUS_MISSING, 0, 0,
2090 status == GOT_STATUS_UNVERSIONED, repo,
2091 progress_cb, progress_arg);
2093 if (err)
2094 goto done;
2096 if (ie) {
2097 err = got_fileindex_entry_update(ie,
2098 worktree->root_fd, path, blob->id.sha1,
2099 worktree->base_commit_id->sha1, 1);
2100 } else {
2101 err = create_fileindex_entry(&ie, fileindex,
2102 worktree->base_commit_id, worktree->root_fd, path,
2103 &blob->id);
2105 if (err)
2106 goto done;
2108 if (is_bad_symlink) {
2109 got_fileindex_entry_filetype_set(ie,
2110 GOT_FILEIDX_MODE_BAD_SYMLINK);
2113 got_object_blob_close(blob);
2114 done:
2115 free(ondisk_path);
2116 return err;
2119 static const struct got_error *
2120 remove_ondisk_file(const char *root_path, const char *path)
2122 const struct got_error *err = NULL;
2123 char *ondisk_path = NULL, *parent = NULL;
2125 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2126 return got_error_from_errno("asprintf");
2128 if (unlink(ondisk_path) == -1) {
2129 if (errno != ENOENT)
2130 err = got_error_from_errno2("unlink", ondisk_path);
2131 } else {
2132 size_t root_len = strlen(root_path);
2133 err = got_path_dirname(&parent, ondisk_path);
2134 if (err)
2135 goto done;
2136 while (got_path_cmp(parent, root_path,
2137 strlen(parent), root_len) != 0) {
2138 free(ondisk_path);
2139 ondisk_path = parent;
2140 parent = NULL;
2141 if (rmdir(ondisk_path) == -1) {
2142 if (errno != ENOTEMPTY)
2143 err = got_error_from_errno2("rmdir",
2144 ondisk_path);
2145 break;
2147 err = got_path_dirname(&parent, ondisk_path);
2148 if (err)
2149 break;
2152 done:
2153 free(ondisk_path);
2154 free(parent);
2155 return err;
2158 static const struct got_error *
2159 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2160 struct got_fileindex_entry *ie, struct got_repository *repo,
2161 got_worktree_checkout_cb progress_cb, void *progress_arg)
2163 const struct got_error *err = NULL;
2164 unsigned char status;
2165 struct stat sb;
2166 char *ondisk_path;
2168 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2169 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2171 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2172 == -1)
2173 return got_error_from_errno("asprintf");
2175 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2176 if (err)
2177 goto done;
2179 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2180 char ondisk_target[PATH_MAX];
2181 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2182 sizeof(ondisk_target));
2183 if (ondisk_len == -1) {
2184 err = got_error_from_errno2("readlink", ondisk_path);
2185 goto done;
2187 ondisk_target[ondisk_len] = '\0';
2188 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2189 NULL, NULL, /* XXX pass common ancestor info? */
2190 ondisk_target, ondisk_path);
2191 if (err)
2192 goto done;
2193 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2194 ie->path);
2195 goto done;
2198 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2199 status == GOT_STATUS_ADD) {
2200 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2201 if (err)
2202 goto done;
2204 * Preserve the working file and change the deleted blob's
2205 * entry into a schedule-add entry.
2207 err = got_fileindex_entry_update(ie, worktree->root_fd,
2208 ie->path, NULL, NULL, 0);
2209 } else {
2210 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2211 if (err)
2212 goto done;
2213 if (status == GOT_STATUS_NO_CHANGE) {
2214 err = remove_ondisk_file(worktree->root_path, ie->path);
2215 if (err)
2216 goto done;
2218 got_fileindex_entry_remove(fileindex, ie);
2220 done:
2221 free(ondisk_path);
2222 return err;
2225 struct diff_cb_arg {
2226 struct got_fileindex *fileindex;
2227 struct got_worktree *worktree;
2228 struct got_repository *repo;
2229 got_worktree_checkout_cb progress_cb;
2230 void *progress_arg;
2231 got_cancel_cb cancel_cb;
2232 void *cancel_arg;
2235 static const struct got_error *
2236 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2237 struct got_tree_entry *te, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return update_blob(a->worktree, a->fileindex, ie, te,
2245 ie->path, a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2253 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 return got_error(GOT_ERR_CANCELLED);
2256 return delete_blob(a->worktree, a->fileindex, ie,
2257 a->repo, a->progress_cb, a->progress_arg);
2260 static const struct got_error *
2261 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2263 struct diff_cb_arg *a = arg;
2264 const struct got_error *err;
2265 char *path;
2267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 return got_error(GOT_ERR_CANCELLED);
2270 if (got_object_tree_entry_is_submodule(te))
2271 return NULL;
2273 if (asprintf(&path, "%s%s%s", parent_path,
2274 parent_path[0] ? "/" : "", te->name)
2275 == -1)
2276 return got_error_from_errno("asprintf");
2278 if (S_ISDIR(te->mode))
2279 err = add_dir_on_disk(a->worktree, path);
2280 else
2281 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2282 a->repo, a->progress_cb, a->progress_arg);
2284 free(path);
2285 return err;
2288 const struct got_error *
2289 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2291 uint32_t uuid_status;
2293 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2294 if (uuid_status != uuid_s_ok) {
2295 *uuidstr = NULL;
2296 return got_error_uuid(uuid_status, "uuid_to_string");
2299 return NULL;
2302 static const struct got_error *
2303 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2305 const struct got_error *err = NULL;
2306 char *uuidstr = NULL;
2308 *refname = NULL;
2310 err = got_worktree_get_uuid(&uuidstr, worktree);
2311 if (err)
2312 return err;
2314 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2315 err = got_error_from_errno("asprintf");
2316 *refname = NULL;
2318 free(uuidstr);
2319 return err;
2322 const struct got_error *
2323 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2328 static const struct got_error *
2329 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2331 return get_ref_name(refname, worktree,
2332 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2335 static const struct got_error *
2336 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2341 static const struct got_error *
2342 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2348 static const struct got_error *
2349 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2362 static const struct got_error *
2363 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree,
2366 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2369 static const struct got_error *
2370 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2376 static const struct got_error *
2377 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree,
2380 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2383 const struct got_error *
2384 got_worktree_get_histedit_script_path(char **path,
2385 struct got_worktree *worktree)
2387 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2388 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 *path = NULL;
2390 return got_error_from_errno("asprintf");
2392 return NULL;
2395 static const struct got_error *
2396 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2398 return get_ref_name(refname, worktree,
2399 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2402 static const struct got_error *
2403 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2405 return get_ref_name(refname, worktree,
2406 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2410 * Prevent Git's garbage collector from deleting our base commit by
2411 * setting a reference to our base commit's ID.
2413 static const struct got_error *
2414 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2416 const struct got_error *err = NULL;
2417 struct got_reference *ref = NULL;
2418 char *refname;
2420 err = got_worktree_get_base_ref_name(&refname, worktree);
2421 if (err)
2422 return err;
2424 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2425 if (err)
2426 goto done;
2428 err = got_ref_write(ref, repo);
2429 done:
2430 free(refname);
2431 if (ref)
2432 got_ref_close(ref);
2433 return err;
2436 static const struct got_error *
2437 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2439 const struct got_error *err = NULL;
2441 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2442 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2443 err = got_error_from_errno("asprintf");
2444 *fileindex_path = NULL;
2446 return err;
2450 static const struct got_error *
2451 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2452 struct got_worktree *worktree)
2454 const struct got_error *err = NULL;
2455 FILE *index = NULL;
2457 *fileindex_path = NULL;
2458 *fileindex = got_fileindex_alloc();
2459 if (*fileindex == NULL)
2460 return got_error_from_errno("got_fileindex_alloc");
2462 err = get_fileindex_path(fileindex_path, worktree);
2463 if (err)
2464 goto done;
2466 index = fopen(*fileindex_path, "rb");
2467 if (index == NULL) {
2468 if (errno != ENOENT)
2469 err = got_error_from_errno2("fopen", *fileindex_path);
2470 } else {
2471 err = got_fileindex_read(*fileindex, index);
2472 if (fclose(index) == EOF && err == NULL)
2473 err = got_error_from_errno("fclose");
2475 done:
2476 if (err) {
2477 free(*fileindex_path);
2478 *fileindex_path = NULL;
2479 got_fileindex_free(*fileindex);
2480 *fileindex = NULL;
2482 return err;
2485 struct bump_base_commit_id_arg {
2486 struct got_object_id *base_commit_id;
2487 const char *path;
2488 size_t path_len;
2489 const char *entry_name;
2490 got_worktree_checkout_cb progress_cb;
2491 void *progress_arg;
2494 /* Bump base commit ID of all files within an updated part of the work tree. */
2495 static const struct got_error *
2496 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2498 const struct got_error *err;
2499 struct bump_base_commit_id_arg *a = arg;
2501 if (a->entry_name) {
2502 if (strcmp(ie->path, a->path) != 0)
2503 return NULL;
2504 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 return NULL;
2507 if (got_fileindex_entry_was_skipped(ie))
2508 return NULL;
2510 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 SHA1_DIGEST_LENGTH) == 0)
2512 return NULL;
2514 if (a->progress_cb) {
2515 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2516 ie->path);
2517 if (err)
2518 return err;
2520 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2521 return NULL;
2524 static const struct got_error *
2525 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2526 struct got_fileindex *fileindex,
2527 got_worktree_checkout_cb progress_cb, void *progress_arg)
2529 struct bump_base_commit_id_arg bbc_arg;
2531 bbc_arg.base_commit_id = worktree->base_commit_id;
2532 bbc_arg.entry_name = NULL;
2533 bbc_arg.path = "";
2534 bbc_arg.path_len = 0;
2535 bbc_arg.progress_cb = progress_cb;
2536 bbc_arg.progress_arg = progress_arg;
2538 return got_fileindex_for_each_entry_safe(fileindex,
2539 bump_base_commit_id, &bbc_arg);
2542 static const struct got_error *
2543 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2545 const struct got_error *err = NULL;
2546 char *new_fileindex_path = NULL;
2547 FILE *new_index = NULL;
2548 struct timespec timeout;
2550 err = got_opentemp_named(&new_fileindex_path, &new_index,
2551 fileindex_path);
2552 if (err)
2553 goto done;
2555 err = got_fileindex_write(fileindex, new_index);
2556 if (err)
2557 goto done;
2559 if (rename(new_fileindex_path, fileindex_path) != 0) {
2560 err = got_error_from_errno3("rename", new_fileindex_path,
2561 fileindex_path);
2562 unlink(new_fileindex_path);
2566 * Sleep for a short amount of time to ensure that files modified after
2567 * this program exits have a different time stamp from the one which
2568 * was recorded in the file index.
2570 timeout.tv_sec = 0;
2571 timeout.tv_nsec = 1;
2572 nanosleep(&timeout, NULL);
2573 done:
2574 if (new_index)
2575 fclose(new_index);
2576 free(new_fileindex_path);
2577 return err;
2580 static const struct got_error *
2581 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2582 struct got_object_id **tree_id, const char *wt_relpath,
2583 struct got_worktree *worktree, struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2592 *tree_id = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2600 goto done;
2602 err = got_object_id_by_path(tree_id, repo,
2603 worktree->base_commit_id, worktree->path_prefix);
2604 if (err)
2605 goto done;
2606 return NULL;
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2618 in_repo_path);
2619 if (err)
2620 goto done;
2622 free(in_repo_path);
2623 in_repo_path = NULL;
2625 err = got_object_get_type(entry_type, repo, id);
2626 if (err)
2627 goto done;
2629 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2630 /* Check out a single file. */
2631 if (strchr(wt_relpath, '/') == NULL) {
2632 /* Check out a single file in work tree's root dir. */
2633 in_repo_path = strdup(worktree->path_prefix);
2634 if (in_repo_path == NULL) {
2635 err = got_error_from_errno("strdup");
2636 goto done;
2638 *tree_relpath = strdup("");
2639 if (*tree_relpath == NULL) {
2640 err = got_error_from_errno("strdup");
2641 goto done;
2643 } else {
2644 /* Check out a single file in a subdirectory. */
2645 err = got_path_dirname(tree_relpath, wt_relpath);
2646 if (err)
2647 return err;
2648 if (asprintf(&in_repo_path, "%s%s%s",
2649 worktree->path_prefix, is_root_wt ? "" : "/",
2650 *tree_relpath) == -1) {
2651 err = got_error_from_errno("asprintf");
2652 goto done;
2655 err = got_object_id_by_path(tree_id, repo,
2656 worktree->base_commit_id, in_repo_path);
2657 } else {
2658 /* Check out all files within a subdirectory. */
2659 *tree_id = got_object_id_dup(id);
2660 if (*tree_id == NULL) {
2661 err = got_error_from_errno("got_object_id_dup");
2662 goto done;
2664 *tree_relpath = strdup(wt_relpath);
2665 if (*tree_relpath == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2670 done:
2671 free(id);
2672 free(in_repo_path);
2673 if (err) {
2674 *entry_type = GOT_OBJ_TYPE_ANY;
2675 free(*tree_relpath);
2676 *tree_relpath = NULL;
2677 free(*tree_id);
2678 *tree_id = NULL;
2680 return err;
2683 static const struct got_error *
2684 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2685 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2686 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2687 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2689 const struct got_error *err = NULL;
2690 struct got_commit_object *commit = NULL;
2691 struct got_tree_object *tree = NULL;
2692 struct got_fileindex_diff_tree_cb diff_cb;
2693 struct diff_cb_arg arg;
2695 err = ref_base_commit(worktree, repo);
2696 if (err) {
2697 if (!(err->code == GOT_ERR_ERRNO &&
2698 (errno == EACCES || errno == EROFS)))
2699 goto done;
2700 err = (*progress_cb)(progress_arg,
2701 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 if (err)
2703 return err;
2706 err = got_object_open_as_commit(&commit, repo,
2707 worktree->base_commit_id);
2708 if (err)
2709 goto done;
2711 err = got_object_open_as_tree(&tree, repo, tree_id);
2712 if (err)
2713 goto done;
2715 if (entry_name &&
2716 got_object_tree_find_entry(tree, entry_name) == NULL) {
2717 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2718 goto done;
2721 diff_cb.diff_old_new = diff_old_new;
2722 diff_cb.diff_old = diff_old;
2723 diff_cb.diff_new = diff_new;
2724 arg.fileindex = fileindex;
2725 arg.worktree = worktree;
2726 arg.repo = repo;
2727 arg.progress_cb = progress_cb;
2728 arg.progress_arg = progress_arg;
2729 arg.cancel_cb = cancel_cb;
2730 arg.cancel_arg = cancel_arg;
2731 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2732 entry_name, repo, &diff_cb, &arg);
2733 done:
2734 if (tree)
2735 got_object_tree_close(tree);
2736 if (commit)
2737 got_object_commit_close(commit);
2738 return err;
2741 const struct got_error *
2742 got_worktree_checkout_files(struct got_worktree *worktree,
2743 struct got_pathlist_head *paths, struct got_repository *repo,
2744 got_worktree_checkout_cb progress_cb, void *progress_arg,
2745 got_cancel_cb cancel_cb, void *cancel_arg)
2747 const struct got_error *err = NULL, *sync_err, *unlockerr;
2748 struct got_commit_object *commit = NULL;
2749 struct got_tree_object *tree = NULL;
2750 struct got_fileindex *fileindex = NULL;
2751 char *fileindex_path = NULL;
2752 struct got_pathlist_entry *pe;
2753 struct tree_path_data {
2754 STAILQ_ENTRY(tree_path_data) entry;
2755 struct got_object_id *tree_id;
2756 int entry_type;
2757 char *relpath;
2758 char *entry_name;
2759 } *tpd = NULL;
2760 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2762 STAILQ_INIT(&tree_paths);
2764 err = lock_worktree(worktree, LOCK_EX);
2765 if (err)
2766 return err;
2768 /* Map all specified paths to in-repository trees. */
2769 TAILQ_FOREACH(pe, paths, entry) {
2770 tpd = malloc(sizeof(*tpd));
2771 if (tpd == NULL) {
2772 err = got_error_from_errno("malloc");
2773 goto done;
2776 err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2778 if (err) {
2779 free(tpd);
2780 goto done;
2783 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 err = got_path_basename(&tpd->entry_name, pe->path);
2785 if (err) {
2786 free(tpd->relpath);
2787 free(tpd->tree_id);
2788 free(tpd);
2789 goto done;
2791 } else
2792 tpd->entry_name = NULL;
2794 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 * Read the file index.
2799 * Checking out files is supposed to be an idempotent operation.
2800 * If the on-disk file index is incomplete we will try to complete it.
2802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 if (err)
2804 goto done;
2806 tpd = STAILQ_FIRST(&tree_paths);
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 struct bump_base_commit_id_arg bbc_arg;
2810 err = checkout_files(worktree, fileindex, tpd->relpath,
2811 tpd->tree_id, tpd->entry_name, repo,
2812 progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 if (err)
2814 break;
2816 bbc_arg.base_commit_id = worktree->base_commit_id;
2817 bbc_arg.entry_name = tpd->entry_name;
2818 bbc_arg.path = pe->path;
2819 bbc_arg.path_len = pe->path_len;
2820 bbc_arg.progress_cb = progress_cb;
2821 bbc_arg.progress_arg = progress_arg;
2822 err = got_fileindex_for_each_entry_safe(fileindex,
2823 bump_base_commit_id, &bbc_arg);
2824 if (err)
2825 break;
2827 tpd = STAILQ_NEXT(tpd, entry);
2829 sync_err = sync_fileindex(fileindex, fileindex_path);
2830 if (sync_err && err == NULL)
2831 err = sync_err;
2832 done:
2833 free(fileindex_path);
2834 if (tree)
2835 got_object_tree_close(tree);
2836 if (commit)
2837 got_object_commit_close(commit);
2838 if (fileindex)
2839 got_fileindex_free(fileindex);
2840 while (!STAILQ_EMPTY(&tree_paths)) {
2841 tpd = STAILQ_FIRST(&tree_paths);
2842 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 free(tpd->relpath);
2844 free(tpd->tree_id);
2845 free(tpd);
2847 unlockerr = lock_worktree(worktree, LOCK_SH);
2848 if (unlockerr && err == NULL)
2849 err = unlockerr;
2850 return err;
2853 struct merge_file_cb_arg {
2854 struct got_worktree *worktree;
2855 struct got_fileindex *fileindex;
2856 got_worktree_checkout_cb progress_cb;
2857 void *progress_arg;
2858 got_cancel_cb cancel_cb;
2859 void *cancel_arg;
2860 const char *label_orig;
2861 struct got_object_id *commit_id2;
2862 int allow_bad_symlinks;
2865 static const struct got_error *
2866 merge_file_cb(void *arg, struct got_blob_object *blob1,
2867 struct got_blob_object *blob2, struct got_object_id *id1,
2868 struct got_object_id *id2, const char *path1, const char *path2,
2869 mode_t mode1, mode_t mode2, struct got_repository *repo)
2871 static const struct got_error *err = NULL;
2872 struct merge_file_cb_arg *a = arg;
2873 struct got_fileindex_entry *ie;
2874 char *ondisk_path = NULL;
2875 struct stat sb;
2876 unsigned char status;
2877 int local_changes_subsumed;
2878 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2879 char *id_str = NULL, *label_deriv2 = NULL;
2881 if (blob1 && blob2) {
2882 ie = got_fileindex_entry_get(a->fileindex, path2,
2883 strlen(path2));
2884 if (ie == NULL)
2885 return (*a->progress_cb)(a->progress_arg,
2886 GOT_STATUS_MISSING, path2);
2888 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2889 path2) == -1)
2890 return got_error_from_errno("asprintf");
2892 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2893 repo);
2894 if (err)
2895 goto done;
2897 if (status == GOT_STATUS_DELETE) {
2898 err = (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_MERGE, path2);
2900 goto done;
2902 if (status != GOT_STATUS_NO_CHANGE &&
2903 status != GOT_STATUS_MODIFY &&
2904 status != GOT_STATUS_CONFLICT &&
2905 status != GOT_STATUS_ADD) {
2906 err = (*a->progress_cb)(a->progress_arg, status, path2);
2907 goto done;
2910 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2911 char *link_target2;
2912 err = got_object_blob_read_to_str(&link_target2, blob2);
2913 if (err)
2914 goto done;
2915 err = merge_symlink(a->worktree, blob1, ondisk_path,
2916 path2, a->label_orig, link_target2, a->commit_id2,
2917 repo, a->progress_cb, a->progress_arg);
2918 free(link_target2);
2919 } else {
2920 int fd;
2922 f_orig = got_opentemp();
2923 if (f_orig == NULL) {
2924 err = got_error_from_errno("got_opentemp");
2925 goto done;
2927 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2928 f_orig, blob1);
2929 if (err)
2930 goto done;
2932 f_deriv2 = got_opentemp();
2933 if (f_deriv2 == NULL)
2934 goto done;
2935 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2936 f_deriv2, blob2);
2937 if (err)
2938 goto done;
2940 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2941 if (fd == -1) {
2942 err = got_error_from_errno2("open",
2943 ondisk_path);
2944 goto done;
2946 f_deriv = fdopen(fd, "r");
2947 if (f_deriv == NULL) {
2948 err = got_error_from_errno2("fdopen",
2949 ondisk_path);
2950 close(fd);
2951 goto done;
2953 err = got_object_id_str(&id_str, a->commit_id2);
2954 if (err)
2955 goto done;
2956 if (asprintf(&label_deriv2, "%s: commit %s",
2957 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2958 err = got_error_from_errno("asprintf");
2959 goto done;
2961 err = merge_file(&local_changes_subsumed, a->worktree,
2962 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2963 sb.st_mode, a->label_orig, NULL, label_deriv2,
2964 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2965 a->progress_cb, a->progress_arg);
2967 } else if (blob1) {
2968 ie = got_fileindex_entry_get(a->fileindex, path1,
2969 strlen(path1));
2970 if (ie == NULL)
2971 return (*a->progress_cb)(a->progress_arg,
2972 GOT_STATUS_MISSING, path1);
2974 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2975 path1) == -1)
2976 return got_error_from_errno("asprintf");
2978 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2979 repo);
2980 if (err)
2981 goto done;
2983 switch (status) {
2984 case GOT_STATUS_NO_CHANGE:
2985 err = (*a->progress_cb)(a->progress_arg,
2986 GOT_STATUS_DELETE, path1);
2987 if (err)
2988 goto done;
2989 err = remove_ondisk_file(a->worktree->root_path, path1);
2990 if (err)
2991 goto done;
2992 if (ie)
2993 got_fileindex_entry_mark_deleted_from_disk(ie);
2994 break;
2995 case GOT_STATUS_DELETE:
2996 case GOT_STATUS_MISSING:
2997 err = (*a->progress_cb)(a->progress_arg,
2998 GOT_STATUS_DELETE, path1);
2999 if (err)
3000 goto done;
3001 if (ie)
3002 got_fileindex_entry_mark_deleted_from_disk(ie);
3003 break;
3004 case GOT_STATUS_ADD: {
3005 struct got_object_id *id;
3006 FILE *blob1_f;
3008 * Delete the added file only if its content already
3009 * exists in the repository.
3011 err = got_object_blob_file_create(&id, &blob1_f, path1);
3012 if (err)
3013 goto done;
3014 if (got_object_id_cmp(id, id1) == 0) {
3015 err = (*a->progress_cb)(a->progress_arg,
3016 GOT_STATUS_DELETE, path1);
3017 if (err)
3018 goto done;
3019 err = remove_ondisk_file(a->worktree->root_path,
3020 path1);
3021 if (err)
3022 goto done;
3023 if (ie)
3024 got_fileindex_entry_remove(a->fileindex,
3025 ie);
3026 } else {
3027 err = (*a->progress_cb)(a->progress_arg,
3028 GOT_STATUS_CANNOT_DELETE, path1);
3030 if (fclose(blob1_f) == EOF && err == NULL)
3031 err = got_error_from_errno("fclose");
3032 free(id);
3033 if (err)
3034 goto done;
3035 break;
3037 case GOT_STATUS_MODIFY:
3038 case GOT_STATUS_CONFLICT:
3039 err = (*a->progress_cb)(a->progress_arg,
3040 GOT_STATUS_CANNOT_DELETE, path1);
3041 if (err)
3042 goto done;
3043 break;
3044 case GOT_STATUS_OBSTRUCTED:
3045 err = (*a->progress_cb)(a->progress_arg, status, path1);
3046 if (err)
3047 goto done;
3048 break;
3049 default:
3050 break;
3052 } else if (blob2) {
3053 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3054 path2) == -1)
3055 return got_error_from_errno("asprintf");
3056 ie = got_fileindex_entry_get(a->fileindex, path2,
3057 strlen(path2));
3058 if (ie) {
3059 err = get_file_status(&status, &sb, ie, ondisk_path,
3060 -1, NULL, repo);
3061 if (err)
3062 goto done;
3063 if (status != GOT_STATUS_NO_CHANGE &&
3064 status != GOT_STATUS_MODIFY &&
3065 status != GOT_STATUS_CONFLICT &&
3066 status != GOT_STATUS_ADD) {
3067 err = (*a->progress_cb)(a->progress_arg,
3068 status, path2);
3069 goto done;
3071 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3072 char *link_target2;
3073 err = got_object_blob_read_to_str(&link_target2,
3074 blob2);
3075 if (err)
3076 goto done;
3077 err = merge_symlink(a->worktree, NULL,
3078 ondisk_path, path2, a->label_orig,
3079 link_target2, a->commit_id2, repo,
3080 a->progress_cb, a->progress_arg);
3081 free(link_target2);
3082 } else if (S_ISREG(sb.st_mode)) {
3083 err = merge_blob(&local_changes_subsumed,
3084 a->worktree, NULL, ondisk_path, path2,
3085 sb.st_mode, a->label_orig, blob2,
3086 a->commit_id2, repo, a->progress_cb,
3087 a->progress_arg);
3088 } else {
3089 err = got_error_path(ondisk_path,
3090 GOT_ERR_FILE_OBSTRUCTED);
3092 if (err)
3093 goto done;
3094 if (status == GOT_STATUS_DELETE) {
3095 err = got_fileindex_entry_update(ie,
3096 a->worktree->root_fd, path2, blob2->id.sha1,
3097 a->worktree->base_commit_id->sha1, 0);
3098 if (err)
3099 goto done;
3101 } else {
3102 int is_bad_symlink = 0;
3103 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3104 if (S_ISLNK(mode2)) {
3105 err = install_symlink(&is_bad_symlink,
3106 a->worktree, ondisk_path, path2, blob2, 0,
3107 0, 1, a->allow_bad_symlinks, repo,
3108 a->progress_cb, a->progress_arg);
3109 } else {
3110 err = install_blob(a->worktree, ondisk_path, path2,
3111 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3112 a->progress_cb, a->progress_arg);
3114 if (err)
3115 goto done;
3116 err = got_fileindex_entry_alloc(&ie, path2);
3117 if (err)
3118 goto done;
3119 err = got_fileindex_entry_update(ie,
3120 a->worktree->root_fd, path2, NULL, NULL, 1);
3121 if (err) {
3122 got_fileindex_entry_free(ie);
3123 goto done;
3125 err = got_fileindex_entry_add(a->fileindex, ie);
3126 if (err) {
3127 got_fileindex_entry_free(ie);
3128 goto done;
3130 if (is_bad_symlink) {
3131 got_fileindex_entry_filetype_set(ie,
3132 GOT_FILEIDX_MODE_BAD_SYMLINK);
3136 done:
3137 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3138 err = got_error_from_errno("fclose");
3139 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 free(id_str);
3144 free(label_deriv2);
3145 free(ondisk_path);
3146 return err;
3149 static const struct got_error *
3150 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3152 struct got_worktree *worktree = arg;
3154 /* Reject merges into a work tree with mixed base commits. */
3155 if (got_fileindex_entry_has_commit(ie) &&
3156 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3157 SHA1_DIGEST_LENGTH) != 0)
3158 return got_error(GOT_ERR_MIXED_COMMITS);
3160 return NULL;
3163 struct check_merge_conflicts_arg {
3164 struct got_worktree *worktree;
3165 struct got_fileindex *fileindex;
3166 struct got_repository *repo;
3169 static const struct got_error *
3170 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3171 struct got_blob_object *blob2, struct got_object_id *id1,
3172 struct got_object_id *id2, const char *path1, const char *path2,
3173 mode_t mode1, mode_t mode2, struct got_repository *repo)
3175 const struct got_error *err = NULL;
3176 struct check_merge_conflicts_arg *a = arg;
3177 unsigned char status;
3178 struct stat sb;
3179 struct got_fileindex_entry *ie;
3180 const char *path = path2 ? path2 : path1;
3181 struct got_object_id *id = id2 ? id2 : id1;
3182 char *ondisk_path;
3184 if (id == NULL)
3185 return NULL;
3187 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3188 if (ie == NULL)
3189 return NULL;
3191 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3192 == -1)
3193 return got_error_from_errno("asprintf");
3195 /* Reject merges into a work tree with conflicted files. */
3196 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3197 free(ondisk_path);
3198 if (err)
3199 return err;
3200 if (status == GOT_STATUS_CONFLICT)
3201 return got_error(GOT_ERR_CONFLICTS);
3203 return NULL;
3206 static const struct got_error *
3207 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3208 const char *fileindex_path, struct got_object_id *commit_id1,
3209 struct got_object_id *commit_id2, struct got_repository *repo,
3210 got_worktree_checkout_cb progress_cb, void *progress_arg,
3211 got_cancel_cb cancel_cb, void *cancel_arg)
3213 const struct got_error *err = NULL, *sync_err;
3214 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3215 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3216 struct check_merge_conflicts_arg cmc_arg;
3217 struct merge_file_cb_arg arg;
3218 char *label_orig = NULL;
3220 if (commit_id1) {
3221 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3222 worktree->path_prefix);
3223 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3224 goto done;
3226 if (tree_id1) {
3227 char *id_str;
3229 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3230 if (err)
3231 goto done;
3233 err = got_object_id_str(&id_str, commit_id1);
3234 if (err)
3235 goto done;
3237 if (asprintf(&label_orig, "%s: commit %s",
3238 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3239 err = got_error_from_errno("asprintf");
3240 free(id_str);
3241 goto done;
3243 free(id_str);
3246 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3247 worktree->path_prefix);
3248 if (err)
3249 goto done;
3251 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3252 if (err)
3253 goto done;
3255 cmc_arg.worktree = worktree;
3256 cmc_arg.fileindex = fileindex;
3257 cmc_arg.repo = repo;
3258 err = got_diff_tree(tree1, tree2, "", "", repo,
3259 check_merge_conflicts, &cmc_arg, 0);
3260 if (err)
3261 goto done;
3263 arg.worktree = worktree;
3264 arg.fileindex = fileindex;
3265 arg.progress_cb = progress_cb;
3266 arg.progress_arg = progress_arg;
3267 arg.cancel_cb = cancel_cb;
3268 arg.cancel_arg = cancel_arg;
3269 arg.label_orig = label_orig;
3270 arg.commit_id2 = commit_id2;
3271 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3272 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3273 sync_err = sync_fileindex(fileindex, fileindex_path);
3274 if (sync_err && err == NULL)
3275 err = sync_err;
3276 done:
3277 if (tree1)
3278 got_object_tree_close(tree1);
3279 if (tree2)
3280 got_object_tree_close(tree2);
3281 free(label_orig);
3282 return err;
3285 const struct got_error *
3286 got_worktree_merge_files(struct got_worktree *worktree,
3287 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3288 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3289 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3291 const struct got_error *err, *unlockerr;
3292 char *fileindex_path = NULL;
3293 struct got_fileindex *fileindex = NULL;
3295 err = lock_worktree(worktree, LOCK_EX);
3296 if (err)
3297 return err;
3299 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3300 if (err)
3301 goto done;
3303 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3304 worktree);
3305 if (err)
3306 goto done;
3308 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3309 commit_id2, repo, progress_cb, progress_arg,
3310 cancel_cb, cancel_arg);
3311 done:
3312 if (fileindex)
3313 got_fileindex_free(fileindex);
3314 free(fileindex_path);
3315 unlockerr = lock_worktree(worktree, LOCK_SH);
3316 if (unlockerr && err == NULL)
3317 err = unlockerr;
3318 return err;
3321 struct diff_dir_cb_arg {
3322 struct got_fileindex *fileindex;
3323 struct got_worktree *worktree;
3324 const char *status_path;
3325 size_t status_path_len;
3326 struct got_repository *repo;
3327 got_worktree_status_cb status_cb;
3328 void *status_arg;
3329 got_cancel_cb cancel_cb;
3330 void *cancel_arg;
3331 /* A pathlist containing per-directory pathlists of ignore patterns. */
3332 struct got_pathlist_head *ignores;
3333 int report_unchanged;
3334 int no_ignores;
3337 static const struct got_error *
3338 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3339 int dirfd, const char *de_name,
3340 got_worktree_status_cb status_cb, void *status_arg,
3341 struct got_repository *repo, int report_unchanged)
3343 const struct got_error *err = NULL;
3344 unsigned char status = GOT_STATUS_NO_CHANGE;
3345 unsigned char staged_status = get_staged_status(ie);
3346 struct stat sb;
3347 struct got_object_id blob_id, commit_id, staged_blob_id;
3348 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3349 struct got_object_id *staged_blob_idp = NULL;
3351 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3352 if (err)
3353 return err;
3355 if (status == GOT_STATUS_NO_CHANGE &&
3356 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3357 return NULL;
3359 if (got_fileindex_entry_has_blob(ie)) {
3360 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3361 blob_idp = &blob_id;
3363 if (got_fileindex_entry_has_commit(ie)) {
3364 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3365 commit_idp = &commit_id;
3367 if (staged_status == GOT_STATUS_ADD ||
3368 staged_status == GOT_STATUS_MODIFY) {
3369 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3370 SHA1_DIGEST_LENGTH);
3371 staged_blob_idp = &staged_blob_id;
3374 return (*status_cb)(status_arg, status, staged_status,
3375 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3378 static const struct got_error *
3379 status_old_new(void *arg, struct got_fileindex_entry *ie,
3380 struct dirent *de, const char *parent_path, int dirfd)
3382 const struct got_error *err = NULL;
3383 struct diff_dir_cb_arg *a = arg;
3384 char *abspath;
3386 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3387 return got_error(GOT_ERR_CANCELLED);
3389 if (got_path_cmp(parent_path, a->status_path,
3390 strlen(parent_path), a->status_path_len) != 0 &&
3391 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3392 return NULL;
3394 if (parent_path[0]) {
3395 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3396 parent_path, de->d_name) == -1)
3397 return got_error_from_errno("asprintf");
3398 } else {
3399 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3400 de->d_name) == -1)
3401 return got_error_from_errno("asprintf");
3404 err = report_file_status(ie, abspath, dirfd, de->d_name,
3405 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3406 free(abspath);
3407 return err;
3410 static const struct got_error *
3411 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3413 struct diff_dir_cb_arg *a = arg;
3414 struct got_object_id blob_id, commit_id;
3415 unsigned char status;
3417 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3418 return got_error(GOT_ERR_CANCELLED);
3420 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3421 return NULL;
3423 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3424 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3425 if (got_fileindex_entry_has_file_on_disk(ie))
3426 status = GOT_STATUS_MISSING;
3427 else
3428 status = GOT_STATUS_DELETE;
3429 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3430 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3433 void
3434 free_ignorelist(struct got_pathlist_head *ignorelist)
3436 struct got_pathlist_entry *pe;
3438 TAILQ_FOREACH(pe, ignorelist, entry)
3439 free((char *)pe->path);
3440 got_pathlist_free(ignorelist);
3443 void
3444 free_ignores(struct got_pathlist_head *ignores)
3446 struct got_pathlist_entry *pe;
3448 TAILQ_FOREACH(pe, ignores, entry) {
3449 struct got_pathlist_head *ignorelist = pe->data;
3450 free_ignorelist(ignorelist);
3451 free((char *)pe->path);
3453 got_pathlist_free(ignores);
3456 static const struct got_error *
3457 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3459 const struct got_error *err = NULL;
3460 struct got_pathlist_entry *pe = NULL;
3461 struct got_pathlist_head *ignorelist;
3462 char *line = NULL, *pattern, *dirpath = NULL;
3463 size_t linesize = 0;
3464 ssize_t linelen;
3466 ignorelist = calloc(1, sizeof(*ignorelist));
3467 if (ignorelist == NULL)
3468 return got_error_from_errno("calloc");
3469 TAILQ_INIT(ignorelist);
3471 while ((linelen = getline(&line, &linesize, f)) != -1) {
3472 if (linelen > 0 && line[linelen - 1] == '\n')
3473 line[linelen - 1] = '\0';
3475 /* Git's ignores may contain comments. */
3476 if (line[0] == '#')
3477 continue;
3479 /* Git's negated patterns are not (yet?) supported. */
3480 if (line[0] == '!')
3481 continue;
3483 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3484 line) == -1) {
3485 err = got_error_from_errno("asprintf");
3486 goto done;
3488 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3489 if (err)
3490 goto done;
3492 if (ferror(f)) {
3493 err = got_error_from_errno("getline");
3494 goto done;
3497 dirpath = strdup(path);
3498 if (dirpath == NULL) {
3499 err = got_error_from_errno("strdup");
3500 goto done;
3502 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3503 done:
3504 free(line);
3505 if (err || pe == NULL) {
3506 free(dirpath);
3507 free_ignorelist(ignorelist);
3509 return err;
3512 int
3513 match_ignores(struct got_pathlist_head *ignores, const char *path)
3515 struct got_pathlist_entry *pe;
3517 /* Handle patterns which match in all directories. */
3518 TAILQ_FOREACH(pe, ignores, entry) {
3519 struct got_pathlist_head *ignorelist = pe->data;
3520 struct got_pathlist_entry *pi;
3522 TAILQ_FOREACH(pi, ignorelist, entry) {
3523 const char *p, *pattern = pi->path;
3525 if (strncmp(pattern, "**/", 3) != 0)
3526 continue;
3527 pattern += 3;
3528 p = path;
3529 while (*p) {
3530 if (fnmatch(pattern, p,
3531 FNM_PATHNAME | FNM_LEADING_DIR)) {
3532 /* Retry in next directory. */
3533 while (*p && *p != '/')
3534 p++;
3535 while (*p == '/')
3536 p++;
3537 continue;
3539 return 1;
3545 * The ignores pathlist contains ignore lists from children before
3546 * parents, so we can find the most specific ignorelist by walking
3547 * ignores backwards.
3549 pe = TAILQ_LAST(ignores, got_pathlist_head);
3550 while (pe) {
3551 if (got_path_is_child(path, pe->path, pe->path_len)) {
3552 struct got_pathlist_head *ignorelist = pe->data;
3553 struct got_pathlist_entry *pi;
3554 TAILQ_FOREACH(pi, ignorelist, entry) {
3555 const char *pattern = pi->path;
3556 int flags = FNM_LEADING_DIR;
3557 if (strstr(pattern, "/**/") == NULL)
3558 flags |= FNM_PATHNAME;
3559 if (fnmatch(pattern, path, flags))
3560 continue;
3561 return 1;
3564 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3567 return 0;
3570 static const struct got_error *
3571 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3572 const char *path, int dirfd, const char *ignores_filename)
3574 const struct got_error *err = NULL;
3575 char *ignorespath;
3576 int fd = -1;
3577 FILE *ignoresfile = NULL;
3579 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3580 path[0] ? "/" : "", ignores_filename) == -1)
3581 return got_error_from_errno("asprintf");
3583 if (dirfd != -1) {
3584 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3585 if (fd == -1) {
3586 if (errno != ENOENT && errno != EACCES)
3587 err = got_error_from_errno2("openat",
3588 ignorespath);
3589 } else {
3590 ignoresfile = fdopen(fd, "r");
3591 if (ignoresfile == NULL)
3592 err = got_error_from_errno2("fdopen",
3593 ignorespath);
3594 else {
3595 fd = -1;
3596 err = read_ignores(ignores, path, ignoresfile);
3599 } else {
3600 ignoresfile = fopen(ignorespath, "r");
3601 if (ignoresfile == NULL) {
3602 if (errno != ENOENT && errno != EACCES)
3603 err = got_error_from_errno2("fopen",
3604 ignorespath);
3605 } else
3606 err = read_ignores(ignores, path, ignoresfile);
3609 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3610 err = got_error_from_errno2("fclose", path);
3611 if (fd != -1 && close(fd) == -1 && err == NULL)
3612 err = got_error_from_errno2("close", path);
3613 free(ignorespath);
3614 return err;
3617 static const struct got_error *
3618 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3619 int dirfd)
3621 const struct got_error *err = NULL;
3622 struct diff_dir_cb_arg *a = arg;
3623 char *path = NULL;
3625 if (ignore != NULL)
3626 *ignore = 0;
3628 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3629 return got_error(GOT_ERR_CANCELLED);
3631 if (parent_path[0]) {
3632 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3633 return got_error_from_errno("asprintf");
3634 } else {
3635 path = de->d_name;
3638 if (de->d_type == DT_DIR) {
3639 if (!a->no_ignores && ignore != NULL &&
3640 match_ignores(a->ignores, path))
3641 *ignore = 1;
3642 } else if (!match_ignores(a->ignores, path) &&
3643 got_path_is_child(path, a->status_path, a->status_path_len))
3644 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3645 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3646 if (parent_path[0])
3647 free(path);
3648 return err;
3651 static const struct got_error *
3652 status_traverse(void *arg, const char *path, int dirfd)
3654 const struct got_error *err = NULL;
3655 struct diff_dir_cb_arg *a = arg;
3657 if (a->no_ignores)
3658 return NULL;
3660 err = add_ignores(a->ignores, a->worktree->root_path,
3661 path, dirfd, ".cvsignore");
3662 if (err)
3663 return err;
3665 err = add_ignores(a->ignores, a->worktree->root_path, path,
3666 dirfd, ".gitignore");
3668 return err;
3671 static const struct got_error *
3672 report_single_file_status(const char *path, const char *ondisk_path,
3673 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3674 void *status_arg, struct got_repository *repo, int report_unchanged,
3675 struct got_pathlist_head *ignores, int no_ignores)
3677 struct got_fileindex_entry *ie;
3678 struct stat sb;
3680 if (!no_ignores && match_ignores(ignores, path))
3681 return NULL;
3683 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3684 if (ie)
3685 return report_file_status(ie, ondisk_path, -1, NULL,
3686 status_cb, status_arg, repo, report_unchanged);
3688 if (lstat(ondisk_path, &sb) == -1) {
3689 if (errno != ENOENT)
3690 return got_error_from_errno2("lstat", ondisk_path);
3691 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3692 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3693 return NULL;
3696 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3697 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3698 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3700 return NULL;
3703 static const struct got_error *
3704 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3705 const char *root_path, const char *path)
3707 const struct got_error *err;
3708 char *parent_path, *next_parent_path = NULL;
3710 err = add_ignores(ignores, root_path, "", -1,
3711 ".cvsignore");
3712 if (err)
3713 return err;
3715 err = add_ignores(ignores, root_path, "", -1,
3716 ".gitignore");
3717 if (err)
3718 return err;
3720 err = got_path_dirname(&parent_path, path);
3721 if (err) {
3722 if (err->code == GOT_ERR_BAD_PATH)
3723 return NULL; /* cannot traverse parent */
3724 return err;
3726 for (;;) {
3727 err = add_ignores(ignores, root_path, parent_path, -1,
3728 ".cvsignore");
3729 if (err)
3730 break;
3731 err = add_ignores(ignores, root_path, parent_path, -1,
3732 ".gitignore");
3733 if (err)
3734 break;
3735 err = got_path_dirname(&next_parent_path, parent_path);
3736 if (err) {
3737 if (err->code == GOT_ERR_BAD_PATH)
3738 err = NULL; /* traversed everything */
3739 break;
3741 if (got_path_is_root_dir(parent_path))
3742 break;
3743 free(parent_path);
3744 parent_path = next_parent_path;
3745 next_parent_path = NULL;
3748 free(parent_path);
3749 free(next_parent_path);
3750 return err;
3753 static const struct got_error *
3754 worktree_status(struct got_worktree *worktree, const char *path,
3755 struct got_fileindex *fileindex, struct got_repository *repo,
3756 got_worktree_status_cb status_cb, void *status_arg,
3757 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3758 int report_unchanged)
3760 const struct got_error *err = NULL;
3761 int fd = -1;
3762 struct got_fileindex_diff_dir_cb fdiff_cb;
3763 struct diff_dir_cb_arg arg;
3764 char *ondisk_path = NULL;
3765 struct got_pathlist_head ignores;
3767 TAILQ_INIT(&ignores);
3769 if (asprintf(&ondisk_path, "%s%s%s",
3770 worktree->root_path, path[0] ? "/" : "", path) == -1)
3771 return got_error_from_errno("asprintf");
3773 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3774 if (fd == -1) {
3775 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3776 !got_err_open_nofollow_on_symlink())
3777 err = got_error_from_errno2("open", ondisk_path);
3778 else {
3779 if (!no_ignores) {
3780 err = add_ignores_from_parent_paths(&ignores,
3781 worktree->root_path, ondisk_path);
3782 if (err)
3783 goto done;
3785 err = report_single_file_status(path, ondisk_path,
3786 fileindex, status_cb, status_arg, repo,
3787 report_unchanged, &ignores, no_ignores);
3789 } else {
3790 fdiff_cb.diff_old_new = status_old_new;
3791 fdiff_cb.diff_old = status_old;
3792 fdiff_cb.diff_new = status_new;
3793 fdiff_cb.diff_traverse = status_traverse;
3794 arg.fileindex = fileindex;
3795 arg.worktree = worktree;
3796 arg.status_path = path;
3797 arg.status_path_len = strlen(path);
3798 arg.repo = repo;
3799 arg.status_cb = status_cb;
3800 arg.status_arg = status_arg;
3801 arg.cancel_cb = cancel_cb;
3802 arg.cancel_arg = cancel_arg;
3803 arg.report_unchanged = report_unchanged;
3804 arg.no_ignores = no_ignores;
3805 if (!no_ignores) {
3806 err = add_ignores_from_parent_paths(&ignores,
3807 worktree->root_path, path);
3808 if (err)
3809 goto done;
3811 arg.ignores = &ignores;
3812 err = got_fileindex_diff_dir(fileindex, fd,
3813 worktree->root_path, path, repo, &fdiff_cb, &arg);
3815 done:
3816 free_ignores(&ignores);
3817 if (fd != -1 && close(fd) == -1 && err == NULL)
3818 err = got_error_from_errno("close");
3819 free(ondisk_path);
3820 return err;
3823 const struct got_error *
3824 got_worktree_status(struct got_worktree *worktree,
3825 struct got_pathlist_head *paths, struct got_repository *repo,
3826 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3827 got_cancel_cb cancel_cb, void *cancel_arg)
3829 const struct got_error *err = NULL;
3830 char *fileindex_path = NULL;
3831 struct got_fileindex *fileindex = NULL;
3832 struct got_pathlist_entry *pe;
3834 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3835 if (err)
3836 return err;
3838 TAILQ_FOREACH(pe, paths, entry) {
3839 err = worktree_status(worktree, pe->path, fileindex, repo,
3840 status_cb, status_arg, cancel_cb, cancel_arg,
3841 no_ignores, 0);
3842 if (err)
3843 break;
3845 free(fileindex_path);
3846 got_fileindex_free(fileindex);
3847 return err;
3850 const struct got_error *
3851 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3852 const char *arg)
3854 const struct got_error *err = NULL;
3855 char *resolved = NULL, *cwd = NULL, *path = NULL;
3856 size_t len;
3857 struct stat sb;
3858 char *abspath = NULL;
3859 char canonpath[PATH_MAX];
3861 *wt_path = NULL;
3863 cwd = getcwd(NULL, 0);
3864 if (cwd == NULL)
3865 return got_error_from_errno("getcwd");
3867 if (lstat(arg, &sb) == -1) {
3868 if (errno != ENOENT) {
3869 err = got_error_from_errno2("lstat", arg);
3870 goto done;
3872 sb.st_mode = 0;
3874 if (S_ISLNK(sb.st_mode)) {
3876 * We cannot use realpath(3) with symlinks since we want to
3877 * operate on the symlink itself.
3878 * But we can make the path absolute, assuming it is relative
3879 * to the current working directory, and then canonicalize it.
3881 if (!got_path_is_absolute(arg)) {
3882 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3883 err = got_error_from_errno("asprintf");
3884 goto done;
3888 err = got_canonpath(abspath ? abspath : arg, canonpath,
3889 sizeof(canonpath));
3890 if (err)
3891 goto done;
3892 resolved = strdup(canonpath);
3893 if (resolved == NULL) {
3894 err = got_error_from_errno("strdup");
3895 goto done;
3897 } else {
3898 resolved = realpath(arg, NULL);
3899 if (resolved == NULL) {
3900 if (errno != ENOENT) {
3901 err = got_error_from_errno2("realpath", arg);
3902 goto done;
3904 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3905 err = got_error_from_errno("asprintf");
3906 goto done;
3908 err = got_canonpath(abspath, canonpath,
3909 sizeof(canonpath));
3910 if (err)
3911 goto done;
3912 resolved = strdup(canonpath);
3913 if (resolved == NULL) {
3914 err = got_error_from_errno("strdup");
3915 goto done;
3920 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3921 strlen(got_worktree_get_root_path(worktree)))) {
3922 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3923 goto done;
3926 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3927 err = got_path_skip_common_ancestor(&path,
3928 got_worktree_get_root_path(worktree), resolved);
3929 if (err)
3930 goto done;
3931 } else {
3932 path = strdup("");
3933 if (path == NULL) {
3934 err = got_error_from_errno("strdup");
3935 goto done;
3939 /* XXX status walk can't deal with trailing slash! */
3940 len = strlen(path);
3941 while (len > 0 && path[len - 1] == '/') {
3942 path[len - 1] = '\0';
3943 len--;
3945 done:
3946 free(abspath);
3947 free(resolved);
3948 free(cwd);
3949 if (err == NULL)
3950 *wt_path = path;
3951 else
3952 free(path);
3953 return err;
3956 struct schedule_addition_args {
3957 struct got_worktree *worktree;
3958 struct got_fileindex *fileindex;
3959 got_worktree_checkout_cb progress_cb;
3960 void *progress_arg;
3961 struct got_repository *repo;
3964 static const struct got_error *
3965 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3966 const char *relpath, struct got_object_id *blob_id,
3967 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3968 int dirfd, const char *de_name)
3970 struct schedule_addition_args *a = arg;
3971 const struct got_error *err = NULL;
3972 struct got_fileindex_entry *ie;
3973 struct stat sb;
3974 char *ondisk_path;
3976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3977 relpath) == -1)
3978 return got_error_from_errno("asprintf");
3980 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3981 if (ie) {
3982 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3983 de_name, a->repo);
3984 if (err)
3985 goto done;
3986 /* Re-adding an existing entry is a no-op. */
3987 if (status == GOT_STATUS_ADD)
3988 goto done;
3989 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3990 if (err)
3991 goto done;
3994 if (status != GOT_STATUS_UNVERSIONED) {
3995 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3996 goto done;
3999 err = got_fileindex_entry_alloc(&ie, relpath);
4000 if (err)
4001 goto done;
4002 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4003 relpath, NULL, NULL, 1);
4004 if (err) {
4005 got_fileindex_entry_free(ie);
4006 goto done;
4008 err = got_fileindex_entry_add(a->fileindex, ie);
4009 if (err) {
4010 got_fileindex_entry_free(ie);
4011 goto done;
4013 done:
4014 free(ondisk_path);
4015 if (err)
4016 return err;
4017 if (status == GOT_STATUS_ADD)
4018 return NULL;
4019 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4022 const struct got_error *
4023 got_worktree_schedule_add(struct got_worktree *worktree,
4024 struct got_pathlist_head *paths,
4025 got_worktree_checkout_cb progress_cb, void *progress_arg,
4026 struct got_repository *repo, int no_ignores)
4028 struct got_fileindex *fileindex = NULL;
4029 char *fileindex_path = NULL;
4030 const struct got_error *err = NULL, *sync_err, *unlockerr;
4031 struct got_pathlist_entry *pe;
4032 struct schedule_addition_args saa;
4034 err = lock_worktree(worktree, LOCK_EX);
4035 if (err)
4036 return err;
4038 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4039 if (err)
4040 goto done;
4042 saa.worktree = worktree;
4043 saa.fileindex = fileindex;
4044 saa.progress_cb = progress_cb;
4045 saa.progress_arg = progress_arg;
4046 saa.repo = repo;
4048 TAILQ_FOREACH(pe, paths, entry) {
4049 err = worktree_status(worktree, pe->path, fileindex, repo,
4050 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4051 if (err)
4052 break;
4054 sync_err = sync_fileindex(fileindex, fileindex_path);
4055 if (sync_err && err == NULL)
4056 err = sync_err;
4057 done:
4058 free(fileindex_path);
4059 if (fileindex)
4060 got_fileindex_free(fileindex);
4061 unlockerr = lock_worktree(worktree, LOCK_SH);
4062 if (unlockerr && err == NULL)
4063 err = unlockerr;
4064 return err;
4067 struct schedule_deletion_args {
4068 struct got_worktree *worktree;
4069 struct got_fileindex *fileindex;
4070 got_worktree_delete_cb progress_cb;
4071 void *progress_arg;
4072 struct got_repository *repo;
4073 int delete_local_mods;
4074 int keep_on_disk;
4075 const char *status_codes;
4078 static const struct got_error *
4079 schedule_for_deletion(void *arg, unsigned char status,
4080 unsigned char staged_status, const char *relpath,
4081 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4082 struct got_object_id *commit_id, int dirfd, const char *de_name)
4084 struct schedule_deletion_args *a = arg;
4085 const struct got_error *err = NULL;
4086 struct got_fileindex_entry *ie = NULL;
4087 struct stat sb;
4088 char *ondisk_path;
4090 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4091 if (ie == NULL)
4092 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4094 staged_status = get_staged_status(ie);
4095 if (staged_status != GOT_STATUS_NO_CHANGE) {
4096 if (staged_status == GOT_STATUS_DELETE)
4097 return NULL;
4098 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4101 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4102 relpath) == -1)
4103 return got_error_from_errno("asprintf");
4105 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4106 a->repo);
4107 if (err)
4108 goto done;
4110 if (a->status_codes) {
4111 size_t ncodes = strlen(a->status_codes);
4112 int i;
4113 for (i = 0; i < ncodes ; i++) {
4114 if (status == a->status_codes[i])
4115 break;
4117 if (i == ncodes) {
4118 /* Do not delete files in non-matching status. */
4119 free(ondisk_path);
4120 return NULL;
4122 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4123 a->status_codes[i] != GOT_STATUS_MISSING) {
4124 static char msg[64];
4125 snprintf(msg, sizeof(msg),
4126 "invalid status code '%c'", a->status_codes[i]);
4127 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4128 goto done;
4132 if (status != GOT_STATUS_NO_CHANGE) {
4133 if (status == GOT_STATUS_DELETE)
4134 goto done;
4135 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4136 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4137 goto done;
4139 if (status != GOT_STATUS_MODIFY &&
4140 status != GOT_STATUS_MISSING) {
4141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4142 goto done;
4146 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 size_t root_len;
4149 if (dirfd != -1) {
4150 if (unlinkat(dirfd, de_name, 0) != 0) {
4151 err = got_error_from_errno2("unlinkat",
4152 ondisk_path);
4153 goto done;
4155 } else if (unlink(ondisk_path) != 0) {
4156 err = got_error_from_errno2("unlink", ondisk_path);
4157 goto done;
4160 root_len = strlen(a->worktree->root_path);
4161 do {
4162 char *parent;
4163 err = got_path_dirname(&parent, ondisk_path);
4164 if (err)
4165 goto done;
4166 free(ondisk_path);
4167 ondisk_path = parent;
4168 if (rmdir(ondisk_path) == -1) {
4169 if (errno != ENOTEMPTY)
4170 err = got_error_from_errno2("rmdir",
4171 ondisk_path);
4172 break;
4174 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 strlen(ondisk_path), root_len) != 0);
4178 got_fileindex_entry_mark_deleted_from_disk(ie);
4179 done:
4180 free(ondisk_path);
4181 if (err)
4182 return err;
4183 if (status == GOT_STATUS_DELETE)
4184 return NULL;
4185 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 staged_status, relpath);
4189 const struct got_error *
4190 got_worktree_schedule_delete(struct got_worktree *worktree,
4191 struct got_pathlist_head *paths, int delete_local_mods,
4192 const char *status_codes,
4193 got_worktree_delete_cb progress_cb, void *progress_arg,
4194 struct got_repository *repo, int keep_on_disk)
4196 struct got_fileindex *fileindex = NULL;
4197 char *fileindex_path = NULL;
4198 const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 struct got_pathlist_entry *pe;
4200 struct schedule_deletion_args sda;
4202 err = lock_worktree(worktree, LOCK_EX);
4203 if (err)
4204 return err;
4206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 if (err)
4208 goto done;
4210 sda.worktree = worktree;
4211 sda.fileindex = fileindex;
4212 sda.progress_cb = progress_cb;
4213 sda.progress_arg = progress_arg;
4214 sda.repo = repo;
4215 sda.delete_local_mods = delete_local_mods;
4216 sda.keep_on_disk = keep_on_disk;
4217 sda.status_codes = status_codes;
4219 TAILQ_FOREACH(pe, paths, entry) {
4220 err = worktree_status(worktree, pe->path, fileindex, repo,
4221 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4222 if (err)
4223 break;
4225 sync_err = sync_fileindex(fileindex, fileindex_path);
4226 if (sync_err && err == NULL)
4227 err = sync_err;
4228 done:
4229 free(fileindex_path);
4230 if (fileindex)
4231 got_fileindex_free(fileindex);
4232 unlockerr = lock_worktree(worktree, LOCK_SH);
4233 if (unlockerr && err == NULL)
4234 err = unlockerr;
4235 return err;
4238 static const struct got_error *
4239 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4241 const struct got_error *err = NULL;
4242 char *line = NULL;
4243 size_t linesize = 0, n;
4244 ssize_t linelen;
4246 linelen = getline(&line, &linesize, infile);
4247 if (linelen == -1) {
4248 if (ferror(infile)) {
4249 err = got_error_from_errno("getline");
4250 goto done;
4252 return NULL;
4254 if (outfile) {
4255 n = fwrite(line, 1, linelen, outfile);
4256 if (n != linelen) {
4257 err = got_ferror(outfile, GOT_ERR_IO);
4258 goto done;
4261 if (rejectfile) {
4262 n = fwrite(line, 1, linelen, rejectfile);
4263 if (n != linelen)
4264 err = got_ferror(outfile, GOT_ERR_IO);
4266 done:
4267 free(line);
4268 return err;
4271 static const struct got_error *
4272 skip_one_line(FILE *f)
4274 char *line = NULL;
4275 size_t linesize = 0;
4276 ssize_t linelen;
4278 linelen = getline(&line, &linesize, f);
4279 if (linelen == -1) {
4280 if (ferror(f))
4281 return got_error_from_errno("getline");
4282 return NULL;
4284 free(line);
4285 return NULL;
4288 static const struct got_error *
4289 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4290 int start_old, int end_old, int start_new, int end_new,
4291 FILE *outfile, FILE *rejectfile)
4293 const struct got_error *err;
4295 /* Copy old file's lines leading up to patch. */
4296 while (!feof(f1) && *line_cur1 < start_old) {
4297 err = copy_one_line(f1, outfile, NULL);
4298 if (err)
4299 return err;
4300 (*line_cur1)++;
4302 /* Skip new file's lines leading up to patch. */
4303 while (!feof(f2) && *line_cur2 < start_new) {
4304 if (rejectfile)
4305 err = copy_one_line(f2, NULL, rejectfile);
4306 else
4307 err = skip_one_line(f2);
4308 if (err)
4309 return err;
4310 (*line_cur2)++;
4312 /* Copy patched lines. */
4313 while (!feof(f2) && *line_cur2 <= end_new) {
4314 err = copy_one_line(f2, outfile, NULL);
4315 if (err)
4316 return err;
4317 (*line_cur2)++;
4319 /* Skip over old file's replaced lines. */
4320 while (!feof(f1) && *line_cur1 <= end_old) {
4321 if (rejectfile)
4322 err = copy_one_line(f1, NULL, rejectfile);
4323 else
4324 err = skip_one_line(f1);
4325 if (err)
4326 return err;
4327 (*line_cur1)++;
4330 return NULL;
4333 static const struct got_error *
4334 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4335 FILE *outfile, FILE *rejectfile)
4337 const struct got_error *err;
4339 if (outfile) {
4340 /* Copy old file's lines until EOF. */
4341 while (!feof(f1)) {
4342 err = copy_one_line(f1, outfile, NULL);
4343 if (err)
4344 return err;
4345 (*line_cur1)++;
4348 if (rejectfile) {
4349 /* Copy new file's lines until EOF. */
4350 while (!feof(f2)) {
4351 err = copy_one_line(f2, NULL, rejectfile);
4352 if (err)
4353 return err;
4354 (*line_cur2)++;
4358 return NULL;
4361 static const struct got_error *
4362 apply_or_reject_change(int *choice, int *nchunks_used,
4363 struct diff_result *diff_result, int n,
4364 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4365 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4366 got_worktree_patch_cb patch_cb, void *patch_arg)
4368 const struct got_error *err = NULL;
4369 struct diff_chunk_context cc = {};
4370 int start_old, end_old, start_new, end_new;
4371 FILE *hunkfile;
4372 struct diff_output_unidiff_state *diff_state;
4373 struct diff_input_info diff_info;
4374 int rc;
4376 *choice = GOT_PATCH_CHOICE_NONE;
4378 /* Get changed line numbers without context lines for copy_change(). */
4379 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4380 start_old = cc.left.start;
4381 end_old = cc.left.end;
4382 start_new = cc.right.start;
4383 end_new = cc.right.end;
4385 /* Get the same change with context lines for display. */
4386 memset(&cc, 0, sizeof(cc));
4387 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4389 memset(&diff_info, 0, sizeof(diff_info));
4390 diff_info.left_path = relpath;
4391 diff_info.right_path = relpath;
4393 diff_state = diff_output_unidiff_state_alloc();
4394 if (diff_state == NULL)
4395 return got_error_set_errno(ENOMEM,
4396 "diff_output_unidiff_state_alloc");
4398 hunkfile = got_opentemp();
4399 if (hunkfile == NULL) {
4400 err = got_error_from_errno("got_opentemp");
4401 goto done;
4404 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4405 diff_result, &cc);
4406 if (rc != DIFF_RC_OK) {
4407 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4408 goto done;
4411 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4412 err = got_ferror(hunkfile, GOT_ERR_IO);
4413 goto done;
4416 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4417 hunkfile, changeno, nchanges);
4418 if (err)
4419 goto done;
4421 switch (*choice) {
4422 case GOT_PATCH_CHOICE_YES:
4423 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4424 end_old, start_new, end_new, outfile, rejectfile);
4425 break;
4426 case GOT_PATCH_CHOICE_NO:
4427 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4428 end_old, start_new, end_new, rejectfile, outfile);
4429 break;
4430 case GOT_PATCH_CHOICE_QUIT:
4431 break;
4432 default:
4433 err = got_error(GOT_ERR_PATCH_CHOICE);
4434 break;
4436 done:
4437 diff_output_unidiff_state_free(diff_state);
4438 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4439 err = got_error_from_errno("fclose");
4440 return err;
4443 struct revert_file_args {
4444 struct got_worktree *worktree;
4445 struct got_fileindex *fileindex;
4446 got_worktree_checkout_cb progress_cb;
4447 void *progress_arg;
4448 got_worktree_patch_cb patch_cb;
4449 void *patch_arg;
4450 struct got_repository *repo;
4451 int unlink_added_files;
4454 static const struct got_error *
4455 create_patched_content(char **path_outfile, int reverse_patch,
4456 struct got_object_id *blob_id, const char *path2,
4457 int dirfd2, const char *de_name2,
4458 const char *relpath, struct got_repository *repo,
4459 got_worktree_patch_cb patch_cb, void *patch_arg)
4461 const struct got_error *err, *free_err;
4462 struct got_blob_object *blob = NULL;
4463 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4464 int fd2 = -1;
4465 char link_target[PATH_MAX];
4466 ssize_t link_len = 0;
4467 char *path1 = NULL, *id_str = NULL;
4468 struct stat sb2;
4469 struct got_diffreg_result *diffreg_result = NULL;
4470 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4471 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4473 *path_outfile = NULL;
4475 err = got_object_id_str(&id_str, blob_id);
4476 if (err)
4477 return err;
4479 if (dirfd2 != -1) {
4480 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4481 if (fd2 == -1) {
4482 if (!got_err_open_nofollow_on_symlink()) {
4483 err = got_error_from_errno2("openat", path2);
4484 goto done;
4486 link_len = readlinkat(dirfd2, de_name2,
4487 link_target, sizeof(link_target));
4488 if (link_len == -1)
4489 return got_error_from_errno2("readlinkat", path2);
4490 sb2.st_mode = S_IFLNK;
4491 sb2.st_size = link_len;
4493 } else {
4494 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4495 if (fd2 == -1) {
4496 if (!got_err_open_nofollow_on_symlink()) {
4497 err = got_error_from_errno2("open", path2);
4498 goto done;
4500 link_len = readlink(path2, link_target,
4501 sizeof(link_target));
4502 if (link_len == -1)
4503 return got_error_from_errno2("readlink", path2);
4504 sb2.st_mode = S_IFLNK;
4505 sb2.st_size = link_len;
4508 if (fd2 != -1) {
4509 if (fstat(fd2, &sb2) == -1) {
4510 err = got_error_from_errno2("fstat", path2);
4511 goto done;
4514 f2 = fdopen(fd2, "r");
4515 if (f2 == NULL) {
4516 err = got_error_from_errno2("fdopen", path2);
4517 goto done;
4519 fd2 = -1;
4520 } else {
4521 size_t n;
4522 f2 = got_opentemp();
4523 if (f2 == NULL) {
4524 err = got_error_from_errno2("got_opentemp", path2);
4525 goto done;
4527 n = fwrite(link_target, 1, link_len, f2);
4528 if (n != link_len) {
4529 err = got_ferror(f2, GOT_ERR_IO);
4530 goto done;
4532 if (fflush(f2) == EOF) {
4533 err = got_error_from_errno("fflush");
4534 goto done;
4536 rewind(f2);
4539 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4540 if (err)
4541 goto done;
4543 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4544 if (err)
4545 goto done;
4547 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4548 if (err)
4549 goto done;
4551 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4552 NULL);
4553 if (err)
4554 goto done;
4556 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4557 if (err)
4558 goto done;
4560 if (fseek(f1, 0L, SEEK_SET) == -1)
4561 return got_ferror(f1, GOT_ERR_IO);
4562 if (fseek(f2, 0L, SEEK_SET) == -1)
4563 return got_ferror(f2, GOT_ERR_IO);
4565 /* Count the number of actual changes in the diff result. */
4566 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4567 struct diff_chunk_context cc = {};
4568 diff_chunk_context_load_change(&cc, &nchunks_used,
4569 diffreg_result->result, n, 0);
4570 nchanges++;
4572 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4573 int choice;
4574 err = apply_or_reject_change(&choice, &nchunks_used,
4575 diffreg_result->result, n, relpath, f1, f2,
4576 &line_cur1, &line_cur2,
4577 reverse_patch ? NULL : outfile,
4578 reverse_patch ? outfile : NULL,
4579 ++i, nchanges, patch_cb, patch_arg);
4580 if (err)
4581 goto done;
4582 if (choice == GOT_PATCH_CHOICE_YES)
4583 have_content = 1;
4584 else if (choice == GOT_PATCH_CHOICE_QUIT)
4585 break;
4587 if (have_content) {
4588 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL);
4591 if (err)
4592 goto done;
4594 if (!S_ISLNK(sb2.st_mode)) {
4595 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4596 err = got_error_from_errno2("fchmod", path2);
4597 goto done;
4601 done:
4602 free(id_str);
4603 if (blob)
4604 got_object_blob_close(blob);
4605 free_err = got_diffreg_result_free(diffreg_result);
4606 if (err == NULL)
4607 err = free_err;
4608 if (f1 && fclose(f1) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path1);
4610 if (f2 && fclose(f2) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path2);
4612 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 err = got_error_from_errno2("close", path2);
4614 if (outfile && fclose(outfile) == EOF && err == NULL)
4615 err = got_error_from_errno2("fclose", *path_outfile);
4616 if (path1 && unlink(path1) == -1 && err == NULL)
4617 err = got_error_from_errno2("unlink", path1);
4618 if (err || !have_content) {
4619 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 err = got_error_from_errno2("unlink", *path_outfile);
4621 free(*path_outfile);
4622 *path_outfile = NULL;
4624 free(path1);
4625 return err;
4628 static const struct got_error *
4629 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *relpath, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct revert_file_args *a = arg;
4635 const struct got_error *err = NULL;
4636 char *parent_path = NULL;
4637 struct got_fileindex_entry *ie;
4638 struct got_tree_object *tree = NULL;
4639 struct got_object_id *tree_id = NULL;
4640 const struct got_tree_entry *te = NULL;
4641 char *tree_path = NULL, *te_name;
4642 char *ondisk_path = NULL, *path_content = NULL;
4643 struct got_blob_object *blob = NULL;
4645 /* Reverting a staged deletion is a no-op. */
4646 if (status == GOT_STATUS_DELETE &&
4647 staged_status != GOT_STATUS_NO_CHANGE)
4648 return NULL;
4650 if (status == GOT_STATUS_UNVERSIONED)
4651 return (*a->progress_cb)(a->progress_arg,
4652 GOT_STATUS_UNVERSIONED, relpath);
4654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4655 if (ie == NULL)
4656 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4658 /* Construct in-repository path of tree which contains this blob. */
4659 err = got_path_dirname(&parent_path, ie->path);
4660 if (err) {
4661 if (err->code != GOT_ERR_BAD_PATH)
4662 goto done;
4663 parent_path = strdup("/");
4664 if (parent_path == NULL) {
4665 err = got_error_from_errno("strdup");
4666 goto done;
4669 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4670 tree_path = strdup(parent_path);
4671 if (tree_path == NULL) {
4672 err = got_error_from_errno("strdup");
4673 goto done;
4675 } else {
4676 if (got_path_is_root_dir(parent_path)) {
4677 tree_path = strdup(a->worktree->path_prefix);
4678 if (tree_path == NULL) {
4679 err = got_error_from_errno("strdup");
4680 goto done;
4682 } else {
4683 if (asprintf(&tree_path, "%s/%s",
4684 a->worktree->path_prefix, parent_path) == -1) {
4685 err = got_error_from_errno("asprintf");
4686 goto done;
4691 err = got_object_id_by_path(&tree_id, a->repo,
4692 a->worktree->base_commit_id, tree_path);
4693 if (err) {
4694 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4695 (status == GOT_STATUS_ADD ||
4696 staged_status == GOT_STATUS_ADD)))
4697 goto done;
4698 } else {
4699 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4700 if (err)
4701 goto done;
4703 err = got_path_basename(&te_name, ie->path);
4704 if (err)
4705 goto done;
4707 te = got_object_tree_find_entry(tree, te_name);
4708 free(te_name);
4709 if (te == NULL && status != GOT_STATUS_ADD &&
4710 staged_status != GOT_STATUS_ADD) {
4711 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4712 goto done;
4716 switch (status) {
4717 case GOT_STATUS_ADD:
4718 if (a->patch_cb) {
4719 int choice = GOT_PATCH_CHOICE_NONE;
4720 err = (*a->patch_cb)(&choice, a->patch_arg,
4721 status, ie->path, NULL, 1, 1);
4722 if (err)
4723 goto done;
4724 if (choice != GOT_PATCH_CHOICE_YES)
4725 break;
4727 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4728 ie->path);
4729 if (err)
4730 goto done;
4731 got_fileindex_entry_remove(a->fileindex, ie);
4732 if (a->unlink_added_files) {
4733 if (asprintf(&ondisk_path, "%s/%s",
4734 got_worktree_get_root_path(a->worktree),
4735 relpath) == -1) {
4736 err = got_error_from_errno("asprintf");
4737 goto done;
4739 if (unlink(ondisk_path) == -1) {
4740 err = got_error_from_errno2("unlink",
4741 ondisk_path);
4742 break;
4745 break;
4746 case GOT_STATUS_DELETE:
4747 if (a->patch_cb) {
4748 int choice = GOT_PATCH_CHOICE_NONE;
4749 err = (*a->patch_cb)(&choice, a->patch_arg,
4750 status, ie->path, NULL, 1, 1);
4751 if (err)
4752 goto done;
4753 if (choice != GOT_PATCH_CHOICE_YES)
4754 break;
4756 /* fall through */
4757 case GOT_STATUS_MODIFY:
4758 case GOT_STATUS_MODE_CHANGE:
4759 case GOT_STATUS_CONFLICT:
4760 case GOT_STATUS_MISSING: {
4761 struct got_object_id id;
4762 if (staged_status == GOT_STATUS_ADD ||
4763 staged_status == GOT_STATUS_MODIFY) {
4764 memcpy(id.sha1, ie->staged_blob_sha1,
4765 SHA1_DIGEST_LENGTH);
4766 } else
4767 memcpy(id.sha1, ie->blob_sha1,
4768 SHA1_DIGEST_LENGTH);
4769 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4770 if (err)
4771 goto done;
4773 if (asprintf(&ondisk_path, "%s/%s",
4774 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4775 err = got_error_from_errno("asprintf");
4776 goto done;
4779 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4780 status == GOT_STATUS_CONFLICT)) {
4781 int is_bad_symlink = 0;
4782 err = create_patched_content(&path_content, 1, &id,
4783 ondisk_path, dirfd, de_name, ie->path, a->repo,
4784 a->patch_cb, a->patch_arg);
4785 if (err || path_content == NULL)
4786 break;
4787 if (te && S_ISLNK(te->mode)) {
4788 if (unlink(path_content) == -1) {
4789 err = got_error_from_errno2("unlink",
4790 path_content);
4791 break;
4793 err = install_symlink(&is_bad_symlink,
4794 a->worktree, ondisk_path, ie->path,
4795 blob, 0, 1, 0, 0, a->repo,
4796 a->progress_cb, a->progress_arg);
4797 } else {
4798 if (rename(path_content, ondisk_path) == -1) {
4799 err = got_error_from_errno3("rename",
4800 path_content, ondisk_path);
4801 goto done;
4804 } else {
4805 int is_bad_symlink = 0;
4806 if (te && S_ISLNK(te->mode)) {
4807 err = install_symlink(&is_bad_symlink,
4808 a->worktree, ondisk_path, ie->path,
4809 blob, 0, 1, 0, 0, a->repo,
4810 a->progress_cb, a->progress_arg);
4811 } else {
4812 err = install_blob(a->worktree, ondisk_path,
4813 ie->path,
4814 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4815 got_fileindex_perms_to_st(ie), blob,
4816 0, 1, 0, 0, a->repo,
4817 a->progress_cb, a->progress_arg);
4819 if (err)
4820 goto done;
4821 if (status == GOT_STATUS_DELETE ||
4822 status == GOT_STATUS_MODE_CHANGE) {
4823 err = got_fileindex_entry_update(ie,
4824 a->worktree->root_fd, relpath,
4825 blob->id.sha1,
4826 a->worktree->base_commit_id->sha1, 1);
4827 if (err)
4828 goto done;
4830 if (is_bad_symlink) {
4831 got_fileindex_entry_filetype_set(ie,
4832 GOT_FILEIDX_MODE_BAD_SYMLINK);
4835 break;
4837 default:
4838 break;
4840 done:
4841 free(ondisk_path);
4842 free(path_content);
4843 free(parent_path);
4844 free(tree_path);
4845 if (blob)
4846 got_object_blob_close(blob);
4847 if (tree)
4848 got_object_tree_close(tree);
4849 free(tree_id);
4850 return err;
4853 const struct got_error *
4854 got_worktree_revert(struct got_worktree *worktree,
4855 struct got_pathlist_head *paths,
4856 got_worktree_checkout_cb progress_cb, void *progress_arg,
4857 got_worktree_patch_cb patch_cb, void *patch_arg,
4858 struct got_repository *repo)
4860 struct got_fileindex *fileindex = NULL;
4861 char *fileindex_path = NULL;
4862 const struct got_error *err = NULL, *unlockerr = NULL;
4863 const struct got_error *sync_err = NULL;
4864 struct got_pathlist_entry *pe;
4865 struct revert_file_args rfa;
4867 err = lock_worktree(worktree, LOCK_EX);
4868 if (err)
4869 return err;
4871 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4872 if (err)
4873 goto done;
4875 rfa.worktree = worktree;
4876 rfa.fileindex = fileindex;
4877 rfa.progress_cb = progress_cb;
4878 rfa.progress_arg = progress_arg;
4879 rfa.patch_cb = patch_cb;
4880 rfa.patch_arg = patch_arg;
4881 rfa.repo = repo;
4882 rfa.unlink_added_files = 0;
4883 TAILQ_FOREACH(pe, paths, entry) {
4884 err = worktree_status(worktree, pe->path, fileindex, repo,
4885 revert_file, &rfa, NULL, NULL, 1, 0);
4886 if (err)
4887 break;
4889 sync_err = sync_fileindex(fileindex, fileindex_path);
4890 if (sync_err && err == NULL)
4891 err = sync_err;
4892 done:
4893 free(fileindex_path);
4894 if (fileindex)
4895 got_fileindex_free(fileindex);
4896 unlockerr = lock_worktree(worktree, LOCK_SH);
4897 if (unlockerr && err == NULL)
4898 err = unlockerr;
4899 return err;
4902 static void
4903 free_commitable(struct got_commitable *ct)
4905 free(ct->path);
4906 free(ct->in_repo_path);
4907 free(ct->ondisk_path);
4908 free(ct->blob_id);
4909 free(ct->base_blob_id);
4910 free(ct->staged_blob_id);
4911 free(ct->base_commit_id);
4912 free(ct);
4915 struct collect_commitables_arg {
4916 struct got_pathlist_head *commitable_paths;
4917 struct got_repository *repo;
4918 struct got_worktree *worktree;
4919 struct got_fileindex *fileindex;
4920 int have_staged_files;
4921 int allow_bad_symlinks;
4924 static const struct got_error *
4925 collect_commitables(void *arg, unsigned char status,
4926 unsigned char staged_status, const char *relpath,
4927 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4928 struct got_object_id *commit_id, int dirfd, const char *de_name)
4930 struct collect_commitables_arg *a = arg;
4931 const struct got_error *err = NULL;
4932 struct got_commitable *ct = NULL;
4933 struct got_pathlist_entry *new = NULL;
4934 char *parent_path = NULL, *path = NULL;
4935 struct stat sb;
4937 if (a->have_staged_files) {
4938 if (staged_status != GOT_STATUS_MODIFY &&
4939 staged_status != GOT_STATUS_ADD &&
4940 staged_status != GOT_STATUS_DELETE)
4941 return NULL;
4942 } else {
4943 if (status == GOT_STATUS_CONFLICT)
4944 return got_error(GOT_ERR_COMMIT_CONFLICT);
4946 if (status != GOT_STATUS_MODIFY &&
4947 status != GOT_STATUS_MODE_CHANGE &&
4948 status != GOT_STATUS_ADD &&
4949 status != GOT_STATUS_DELETE)
4950 return NULL;
4953 if (asprintf(&path, "/%s", relpath) == -1) {
4954 err = got_error_from_errno("asprintf");
4955 goto done;
4957 if (strcmp(path, "/") == 0) {
4958 parent_path = strdup("");
4959 if (parent_path == NULL)
4960 return got_error_from_errno("strdup");
4961 } else {
4962 err = got_path_dirname(&parent_path, path);
4963 if (err)
4964 return err;
4967 ct = calloc(1, sizeof(*ct));
4968 if (ct == NULL) {
4969 err = got_error_from_errno("calloc");
4970 goto done;
4973 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4974 relpath) == -1) {
4975 err = got_error_from_errno("asprintf");
4976 goto done;
4979 if (staged_status == GOT_STATUS_ADD ||
4980 staged_status == GOT_STATUS_MODIFY) {
4981 struct got_fileindex_entry *ie;
4982 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4983 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4984 case GOT_FILEIDX_MODE_REGULAR_FILE:
4985 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4986 ct->mode = S_IFREG;
4987 break;
4988 case GOT_FILEIDX_MODE_SYMLINK:
4989 ct->mode = S_IFLNK;
4990 break;
4991 default:
4992 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4993 goto done;
4995 ct->mode |= got_fileindex_entry_perms_get(ie);
4996 } else if (status != GOT_STATUS_DELETE &&
4997 staged_status != GOT_STATUS_DELETE) {
4998 if (dirfd != -1) {
4999 if (fstatat(dirfd, de_name, &sb,
5000 AT_SYMLINK_NOFOLLOW) == -1) {
5001 err = got_error_from_errno2("fstatat",
5002 ct->ondisk_path);
5003 goto done;
5005 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5006 err = got_error_from_errno2("lstat", ct->ondisk_path);
5007 goto done;
5009 ct->mode = sb.st_mode;
5012 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5013 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5014 relpath) == -1) {
5015 err = got_error_from_errno("asprintf");
5016 goto done;
5019 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5020 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5021 int is_bad_symlink;
5022 char target_path[PATH_MAX];
5023 ssize_t target_len;
5024 target_len = readlink(ct->ondisk_path, target_path,
5025 sizeof(target_path));
5026 if (target_len == -1) {
5027 err = got_error_from_errno2("readlink",
5028 ct->ondisk_path);
5029 goto done;
5031 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5032 target_len, ct->ondisk_path, a->worktree->root_path);
5033 if (err)
5034 goto done;
5035 if (is_bad_symlink) {
5036 err = got_error_path(ct->ondisk_path,
5037 GOT_ERR_BAD_SYMLINK);
5038 goto done;
5043 ct->status = status;
5044 ct->staged_status = staged_status;
5045 ct->blob_id = NULL; /* will be filled in when blob gets created */
5046 if (ct->status != GOT_STATUS_ADD &&
5047 ct->staged_status != GOT_STATUS_ADD) {
5048 ct->base_blob_id = got_object_id_dup(blob_id);
5049 if (ct->base_blob_id == NULL) {
5050 err = got_error_from_errno("got_object_id_dup");
5051 goto done;
5053 ct->base_commit_id = got_object_id_dup(commit_id);
5054 if (ct->base_commit_id == NULL) {
5055 err = got_error_from_errno("got_object_id_dup");
5056 goto done;
5059 if (ct->staged_status == GOT_STATUS_ADD ||
5060 ct->staged_status == GOT_STATUS_MODIFY) {
5061 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5062 if (ct->staged_blob_id == NULL) {
5063 err = got_error_from_errno("got_object_id_dup");
5064 goto done;
5067 ct->path = strdup(path);
5068 if (ct->path == NULL) {
5069 err = got_error_from_errno("strdup");
5070 goto done;
5072 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5073 done:
5074 if (ct && (err || new == NULL))
5075 free_commitable(ct);
5076 free(parent_path);
5077 free(path);
5078 return err;
5081 static const struct got_error *write_tree(struct got_object_id **, int *,
5082 struct got_tree_object *, const char *, struct got_pathlist_head *,
5083 got_worktree_status_cb status_cb, void *status_arg,
5084 struct got_repository *);
5086 static const struct got_error *
5087 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5088 struct got_tree_entry *te, const char *parent_path,
5089 struct got_pathlist_head *commitable_paths,
5090 got_worktree_status_cb status_cb, void *status_arg,
5091 struct got_repository *repo)
5093 const struct got_error *err = NULL;
5094 struct got_tree_object *subtree;
5095 char *subpath;
5097 if (asprintf(&subpath, "%s%s%s", parent_path,
5098 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5099 return got_error_from_errno("asprintf");
5101 err = got_object_open_as_tree(&subtree, repo, &te->id);
5102 if (err)
5103 return err;
5105 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5106 commitable_paths, status_cb, status_arg, repo);
5107 got_object_tree_close(subtree);
5108 free(subpath);
5109 return err;
5112 static const struct got_error *
5113 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5115 const struct got_error *err = NULL;
5116 char *ct_parent_path = NULL;
5118 *match = 0;
5120 if (strchr(ct->in_repo_path, '/') == NULL) {
5121 *match = got_path_is_root_dir(path);
5122 return NULL;
5125 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5126 if (err)
5127 return err;
5128 *match = (strcmp(path, ct_parent_path) == 0);
5129 free(ct_parent_path);
5130 return err;
5133 static mode_t
5134 get_ct_file_mode(struct got_commitable *ct)
5136 if (S_ISLNK(ct->mode))
5137 return S_IFLNK;
5139 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5142 static const struct got_error *
5143 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5144 struct got_tree_entry *te, struct got_commitable *ct)
5146 const struct got_error *err = NULL;
5148 *new_te = NULL;
5150 err = got_object_tree_entry_dup(new_te, te);
5151 if (err)
5152 goto done;
5154 (*new_te)->mode = get_ct_file_mode(ct);
5156 if (ct->staged_status == GOT_STATUS_MODIFY)
5157 memcpy(&(*new_te)->id, ct->staged_blob_id,
5158 sizeof((*new_te)->id));
5159 else
5160 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5161 done:
5162 if (err && *new_te) {
5163 free(*new_te);
5164 *new_te = NULL;
5166 return err;
5169 static const struct got_error *
5170 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5171 struct got_commitable *ct)
5173 const struct got_error *err = NULL;
5174 char *ct_name = NULL;
5176 *new_te = NULL;
5178 *new_te = calloc(1, sizeof(**new_te));
5179 if (*new_te == NULL)
5180 return got_error_from_errno("calloc");
5182 err = got_path_basename(&ct_name, ct->path);
5183 if (err)
5184 goto done;
5185 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5186 sizeof((*new_te)->name)) {
5187 err = got_error(GOT_ERR_NO_SPACE);
5188 goto done;
5191 (*new_te)->mode = get_ct_file_mode(ct);
5193 if (ct->staged_status == GOT_STATUS_ADD)
5194 memcpy(&(*new_te)->id, ct->staged_blob_id,
5195 sizeof((*new_te)->id));
5196 else
5197 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5198 done:
5199 free(ct_name);
5200 if (err && *new_te) {
5201 free(*new_te);
5202 *new_te = NULL;
5204 return err;
5207 static const struct got_error *
5208 insert_tree_entry(struct got_tree_entry *new_te,
5209 struct got_pathlist_head *paths)
5211 const struct got_error *err = NULL;
5212 struct got_pathlist_entry *new_pe;
5214 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5215 if (err)
5216 return err;
5217 if (new_pe == NULL)
5218 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5219 return NULL;
5222 static const struct got_error *
5223 report_ct_status(struct got_commitable *ct,
5224 got_worktree_status_cb status_cb, void *status_arg)
5226 const char *ct_path = ct->path;
5227 unsigned char status;
5229 if (status_cb == NULL) /* no commit progress output desired */
5230 return NULL;
5232 while (ct_path[0] == '/')
5233 ct_path++;
5235 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5236 status = ct->staged_status;
5237 else
5238 status = ct->status;
5240 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5241 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5244 static const struct got_error *
5245 match_modified_subtree(int *modified, struct got_tree_entry *te,
5246 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5248 const struct got_error *err = NULL;
5249 struct got_pathlist_entry *pe;
5250 char *te_path;
5252 *modified = 0;
5254 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5255 got_path_is_root_dir(base_tree_path) ? "" : "/",
5256 te->name) == -1)
5257 return got_error_from_errno("asprintf");
5259 TAILQ_FOREACH(pe, commitable_paths, entry) {
5260 struct got_commitable *ct = pe->data;
5261 *modified = got_path_is_child(ct->in_repo_path, te_path,
5262 strlen(te_path));
5263 if (*modified)
5264 break;
5267 free(te_path);
5268 return err;
5271 static const struct got_error *
5272 match_deleted_or_modified_ct(struct got_commitable **ctp,
5273 struct got_tree_entry *te, const char *base_tree_path,
5274 struct got_pathlist_head *commitable_paths)
5276 const struct got_error *err = NULL;
5277 struct got_pathlist_entry *pe;
5279 *ctp = NULL;
5281 TAILQ_FOREACH(pe, commitable_paths, entry) {
5282 struct got_commitable *ct = pe->data;
5283 char *ct_name = NULL;
5284 int path_matches;
5286 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5287 if (ct->status != GOT_STATUS_MODIFY &&
5288 ct->status != GOT_STATUS_MODE_CHANGE &&
5289 ct->status != GOT_STATUS_DELETE)
5290 continue;
5291 } else {
5292 if (ct->staged_status != GOT_STATUS_MODIFY &&
5293 ct->staged_status != GOT_STATUS_DELETE)
5294 continue;
5297 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5298 continue;
5300 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5301 if (err)
5302 return err;
5303 if (!path_matches)
5304 continue;
5306 err = got_path_basename(&ct_name, pe->path);
5307 if (err)
5308 return err;
5310 if (strcmp(te->name, ct_name) != 0) {
5311 free(ct_name);
5312 continue;
5314 free(ct_name);
5316 *ctp = ct;
5317 break;
5320 return err;
5323 static const struct got_error *
5324 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5325 const char *child_path, const char *path_base_tree,
5326 struct got_pathlist_head *commitable_paths,
5327 got_worktree_status_cb status_cb, void *status_arg,
5328 struct got_repository *repo)
5330 const struct got_error *err = NULL;
5331 struct got_tree_entry *new_te;
5332 char *subtree_path;
5333 struct got_object_id *id = NULL;
5334 int nentries;
5336 *new_tep = NULL;
5338 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5339 got_path_is_root_dir(path_base_tree) ? "" : "/",
5340 child_path) == -1)
5341 return got_error_from_errno("asprintf");
5343 new_te = calloc(1, sizeof(*new_te));
5344 if (new_te == NULL)
5345 return got_error_from_errno("calloc");
5346 new_te->mode = S_IFDIR;
5348 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5349 sizeof(new_te->name)) {
5350 err = got_error(GOT_ERR_NO_SPACE);
5351 goto done;
5353 err = write_tree(&id, &nentries, NULL, subtree_path,
5354 commitable_paths, status_cb, status_arg, repo);
5355 if (err) {
5356 free(new_te);
5357 goto done;
5359 memcpy(&new_te->id, id, sizeof(new_te->id));
5360 done:
5361 free(id);
5362 free(subtree_path);
5363 if (err == NULL)
5364 *new_tep = new_te;
5365 return err;
5368 static const struct got_error *
5369 write_tree(struct got_object_id **new_tree_id, int *nentries,
5370 struct got_tree_object *base_tree, const char *path_base_tree,
5371 struct got_pathlist_head *commitable_paths,
5372 got_worktree_status_cb status_cb, void *status_arg,
5373 struct got_repository *repo)
5375 const struct got_error *err = NULL;
5376 struct got_pathlist_head paths;
5377 struct got_tree_entry *te, *new_te = NULL;
5378 struct got_pathlist_entry *pe;
5380 TAILQ_INIT(&paths);
5381 *nentries = 0;
5383 /* Insert, and recurse into, newly added entries first. */
5384 TAILQ_FOREACH(pe, commitable_paths, entry) {
5385 struct got_commitable *ct = pe->data;
5386 char *child_path = NULL, *slash;
5388 if ((ct->status != GOT_STATUS_ADD &&
5389 ct->staged_status != GOT_STATUS_ADD) ||
5390 (ct->flags & GOT_COMMITABLE_ADDED))
5391 continue;
5393 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5394 strlen(path_base_tree)))
5395 continue;
5397 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5398 ct->in_repo_path);
5399 if (err)
5400 goto done;
5402 slash = strchr(child_path, '/');
5403 if (slash == NULL) {
5404 err = alloc_added_blob_tree_entry(&new_te, ct);
5405 if (err)
5406 goto done;
5407 err = report_ct_status(ct, status_cb, status_arg);
5408 if (err)
5409 goto done;
5410 ct->flags |= GOT_COMMITABLE_ADDED;
5411 err = insert_tree_entry(new_te, &paths);
5412 if (err)
5413 goto done;
5414 (*nentries)++;
5415 } else {
5416 *slash = '\0'; /* trim trailing path components */
5417 if (base_tree == NULL ||
5418 got_object_tree_find_entry(base_tree, child_path)
5419 == NULL) {
5420 err = make_subtree_for_added_blob(&new_te,
5421 child_path, path_base_tree,
5422 commitable_paths, status_cb, status_arg,
5423 repo);
5424 if (err)
5425 goto done;
5426 err = insert_tree_entry(new_te, &paths);
5427 if (err)
5428 goto done;
5429 (*nentries)++;
5434 if (base_tree) {
5435 int i, nbase_entries;
5436 /* Handle modified and deleted entries. */
5437 nbase_entries = got_object_tree_get_nentries(base_tree);
5438 for (i = 0; i < nbase_entries; i++) {
5439 struct got_commitable *ct = NULL;
5441 te = got_object_tree_get_entry(base_tree, i);
5442 if (got_object_tree_entry_is_submodule(te)) {
5443 /* Entry is a submodule; just copy it. */
5444 err = got_object_tree_entry_dup(&new_te, te);
5445 if (err)
5446 goto done;
5447 err = insert_tree_entry(new_te, &paths);
5448 if (err)
5449 goto done;
5450 (*nentries)++;
5451 continue;
5454 if (S_ISDIR(te->mode)) {
5455 int modified;
5456 err = got_object_tree_entry_dup(&new_te, te);
5457 if (err)
5458 goto done;
5459 err = match_modified_subtree(&modified, te,
5460 path_base_tree, commitable_paths);
5461 if (err)
5462 goto done;
5463 /* Avoid recursion into unmodified subtrees. */
5464 if (modified) {
5465 struct got_object_id *new_id;
5466 int nsubentries;
5467 err = write_subtree(&new_id,
5468 &nsubentries, te,
5469 path_base_tree, commitable_paths,
5470 status_cb, status_arg, repo);
5471 if (err)
5472 goto done;
5473 if (nsubentries == 0) {
5474 /* All entries were deleted. */
5475 free(new_id);
5476 continue;
5478 memcpy(&new_te->id, new_id,
5479 sizeof(new_te->id));
5480 free(new_id);
5482 err = insert_tree_entry(new_te, &paths);
5483 if (err)
5484 goto done;
5485 (*nentries)++;
5486 continue;
5489 err = match_deleted_or_modified_ct(&ct, te,
5490 path_base_tree, commitable_paths);
5491 if (err)
5492 goto done;
5493 if (ct) {
5494 /* NB: Deleted entries get dropped here. */
5495 if (ct->status == GOT_STATUS_MODIFY ||
5496 ct->status == GOT_STATUS_MODE_CHANGE ||
5497 ct->staged_status == GOT_STATUS_MODIFY) {
5498 err = alloc_modified_blob_tree_entry(
5499 &new_te, te, ct);
5500 if (err)
5501 goto done;
5502 err = insert_tree_entry(new_te, &paths);
5503 if (err)
5504 goto done;
5505 (*nentries)++;
5507 err = report_ct_status(ct, status_cb,
5508 status_arg);
5509 if (err)
5510 goto done;
5511 } else {
5512 /* Entry is unchanged; just copy it. */
5513 err = got_object_tree_entry_dup(&new_te, te);
5514 if (err)
5515 goto done;
5516 err = insert_tree_entry(new_te, &paths);
5517 if (err)
5518 goto done;
5519 (*nentries)++;
5524 /* Write new list of entries; deleted entries have been dropped. */
5525 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5526 done:
5527 got_pathlist_free(&paths);
5528 return err;
5531 static const struct got_error *
5532 update_fileindex_after_commit(struct got_worktree *worktree,
5533 struct got_pathlist_head *commitable_paths,
5534 struct got_object_id *new_base_commit_id,
5535 struct got_fileindex *fileindex, int have_staged_files)
5537 const struct got_error *err = NULL;
5538 struct got_pathlist_entry *pe;
5539 char *relpath = NULL;
5541 TAILQ_FOREACH(pe, commitable_paths, entry) {
5542 struct got_fileindex_entry *ie;
5543 struct got_commitable *ct = pe->data;
5545 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5547 err = got_path_skip_common_ancestor(&relpath,
5548 worktree->root_path, ct->ondisk_path);
5549 if (err)
5550 goto done;
5552 if (ie) {
5553 if (ct->status == GOT_STATUS_DELETE ||
5554 ct->staged_status == GOT_STATUS_DELETE) {
5555 got_fileindex_entry_remove(fileindex, ie);
5556 } else if (ct->staged_status == GOT_STATUS_ADD ||
5557 ct->staged_status == GOT_STATUS_MODIFY) {
5558 got_fileindex_entry_stage_set(ie,
5559 GOT_FILEIDX_STAGE_NONE);
5560 got_fileindex_entry_staged_filetype_set(ie, 0);
5562 err = got_fileindex_entry_update(ie,
5563 worktree->root_fd, relpath,
5564 ct->staged_blob_id->sha1,
5565 new_base_commit_id->sha1,
5566 !have_staged_files);
5567 } else
5568 err = got_fileindex_entry_update(ie,
5569 worktree->root_fd, relpath,
5570 ct->blob_id->sha1,
5571 new_base_commit_id->sha1,
5572 !have_staged_files);
5573 } else {
5574 err = got_fileindex_entry_alloc(&ie, pe->path);
5575 if (err)
5576 goto done;
5577 err = got_fileindex_entry_update(ie,
5578 worktree->root_fd, relpath, ct->blob_id->sha1,
5579 new_base_commit_id->sha1, 1);
5580 if (err) {
5581 got_fileindex_entry_free(ie);
5582 goto done;
5584 err = got_fileindex_entry_add(fileindex, ie);
5585 if (err) {
5586 got_fileindex_entry_free(ie);
5587 goto done;
5590 free(relpath);
5591 relpath = NULL;
5593 done:
5594 free(relpath);
5595 return err;
5599 static const struct got_error *
5600 check_out_of_date(const char *in_repo_path, unsigned char status,
5601 unsigned char staged_status, struct got_object_id *base_blob_id,
5602 struct got_object_id *base_commit_id,
5603 struct got_object_id *head_commit_id, struct got_repository *repo,
5604 int ood_errcode)
5606 const struct got_error *err = NULL;
5607 struct got_object_id *id = NULL;
5609 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5610 /* Trivial case: base commit == head commit */
5611 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5612 return NULL;
5614 * Ensure file content which local changes were based
5615 * on matches file content in the branch head.
5617 err = got_object_id_by_path(&id, repo, head_commit_id,
5618 in_repo_path);
5619 if (err) {
5620 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5621 err = got_error(ood_errcode);
5622 goto done;
5623 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5624 err = got_error(ood_errcode);
5625 } else {
5626 /* Require that added files don't exist in the branch head. */
5627 err = got_object_id_by_path(&id, repo, head_commit_id,
5628 in_repo_path);
5629 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5630 goto done;
5631 err = id ? got_error(ood_errcode) : NULL;
5633 done:
5634 free(id);
5635 return err;
5638 const struct got_error *
5639 commit_worktree(struct got_object_id **new_commit_id,
5640 struct got_pathlist_head *commitable_paths,
5641 struct got_object_id *head_commit_id,
5642 struct got_object_id *parent_id2,
5643 struct got_worktree *worktree,
5644 const char *author, const char *committer,
5645 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5646 got_worktree_status_cb status_cb, void *status_arg,
5647 struct got_repository *repo)
5649 const struct got_error *err = NULL, *unlockerr = NULL;
5650 struct got_pathlist_entry *pe;
5651 const char *head_ref_name = NULL;
5652 struct got_commit_object *head_commit = NULL;
5653 struct got_reference *head_ref2 = NULL;
5654 struct got_object_id *head_commit_id2 = NULL;
5655 struct got_tree_object *head_tree = NULL;
5656 struct got_object_id *new_tree_id = NULL;
5657 int nentries, nparents = 0;
5658 struct got_object_id_queue parent_ids;
5659 struct got_object_qid *pid = NULL;
5660 char *logmsg = NULL;
5662 *new_commit_id = NULL;
5664 STAILQ_INIT(&parent_ids);
5666 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5667 if (err)
5668 goto done;
5670 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5671 if (err)
5672 goto done;
5674 if (commit_msg_cb != NULL) {
5675 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5676 if (err)
5677 goto done;
5680 if (logmsg == NULL || strlen(logmsg) == 0) {
5681 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5682 goto done;
5685 /* Create blobs from added and modified files and record their IDs. */
5686 TAILQ_FOREACH(pe, commitable_paths, entry) {
5687 struct got_commitable *ct = pe->data;
5688 char *ondisk_path;
5690 /* Blobs for staged files already exist. */
5691 if (ct->staged_status == GOT_STATUS_ADD ||
5692 ct->staged_status == GOT_STATUS_MODIFY)
5693 continue;
5695 if (ct->status != GOT_STATUS_ADD &&
5696 ct->status != GOT_STATUS_MODIFY &&
5697 ct->status != GOT_STATUS_MODE_CHANGE)
5698 continue;
5700 if (asprintf(&ondisk_path, "%s/%s",
5701 worktree->root_path, pe->path) == -1) {
5702 err = got_error_from_errno("asprintf");
5703 goto done;
5705 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5706 free(ondisk_path);
5707 if (err)
5708 goto done;
5711 /* Recursively write new tree objects. */
5712 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5713 commitable_paths, status_cb, status_arg, repo);
5714 if (err)
5715 goto done;
5717 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5718 if (err)
5719 goto done;
5720 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5721 nparents++;
5722 if (parent_id2) {
5723 err = got_object_qid_alloc(&pid, parent_id2);
5724 if (err)
5725 goto done;
5726 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5727 nparents++;
5729 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5730 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5731 if (logmsg != NULL)
5732 free(logmsg);
5733 if (err)
5734 goto done;
5736 /* Check if a concurrent commit to our branch has occurred. */
5737 head_ref_name = got_worktree_get_head_ref_name(worktree);
5738 if (head_ref_name == NULL) {
5739 err = got_error_from_errno("got_worktree_get_head_ref_name");
5740 goto done;
5742 /* Lock the reference here to prevent concurrent modification. */
5743 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5744 if (err)
5745 goto done;
5746 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5747 if (err)
5748 goto done;
5749 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5750 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5751 goto done;
5753 /* Update branch head in repository. */
5754 err = got_ref_change_ref(head_ref2, *new_commit_id);
5755 if (err)
5756 goto done;
5757 err = got_ref_write(head_ref2, repo);
5758 if (err)
5759 goto done;
5761 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5762 if (err)
5763 goto done;
5765 err = ref_base_commit(worktree, repo);
5766 if (err)
5767 goto done;
5768 done:
5769 got_object_id_queue_free(&parent_ids);
5770 if (head_tree)
5771 got_object_tree_close(head_tree);
5772 if (head_commit)
5773 got_object_commit_close(head_commit);
5774 free(head_commit_id2);
5775 if (head_ref2) {
5776 unlockerr = got_ref_unlock(head_ref2);
5777 if (unlockerr && err == NULL)
5778 err = unlockerr;
5779 got_ref_close(head_ref2);
5781 return err;
5784 static const struct got_error *
5785 check_path_is_commitable(const char *path,
5786 struct got_pathlist_head *commitable_paths)
5788 struct got_pathlist_entry *cpe = NULL;
5789 size_t path_len = strlen(path);
5791 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5792 struct got_commitable *ct = cpe->data;
5793 const char *ct_path = ct->path;
5795 while (ct_path[0] == '/')
5796 ct_path++;
5798 if (strcmp(path, ct_path) == 0 ||
5799 got_path_is_child(ct_path, path, path_len))
5800 break;
5803 if (cpe == NULL)
5804 return got_error_path(path, GOT_ERR_BAD_PATH);
5806 return NULL;
5809 static const struct got_error *
5810 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5812 int *have_staged_files = arg;
5814 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5815 *have_staged_files = 1;
5816 return got_error(GOT_ERR_CANCELLED);
5819 return NULL;
5822 static const struct got_error *
5823 check_non_staged_files(struct got_fileindex *fileindex,
5824 struct got_pathlist_head *paths)
5826 struct got_pathlist_entry *pe;
5827 struct got_fileindex_entry *ie;
5829 TAILQ_FOREACH(pe, paths, entry) {
5830 if (pe->path[0] == '\0')
5831 continue;
5832 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5833 if (ie == NULL)
5834 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5835 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5836 return got_error_path(pe->path,
5837 GOT_ERR_FILE_NOT_STAGED);
5840 return NULL;
5843 const struct got_error *
5844 got_worktree_commit(struct got_object_id **new_commit_id,
5845 struct got_worktree *worktree, struct got_pathlist_head *paths,
5846 const char *author, const char *committer, int allow_bad_symlinks,
5847 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5848 got_worktree_status_cb status_cb, void *status_arg,
5849 struct got_repository *repo)
5851 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5852 struct got_fileindex *fileindex = NULL;
5853 char *fileindex_path = NULL;
5854 struct got_pathlist_head commitable_paths;
5855 struct collect_commitables_arg cc_arg;
5856 struct got_pathlist_entry *pe;
5857 struct got_reference *head_ref = NULL;
5858 struct got_object_id *head_commit_id = NULL;
5859 int have_staged_files = 0;
5861 *new_commit_id = NULL;
5863 TAILQ_INIT(&commitable_paths);
5865 err = lock_worktree(worktree, LOCK_EX);
5866 if (err)
5867 goto done;
5869 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5870 if (err)
5871 goto done;
5873 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5874 if (err)
5875 goto done;
5877 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5878 if (err)
5879 goto done;
5881 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5882 &have_staged_files);
5883 if (err && err->code != GOT_ERR_CANCELLED)
5884 goto done;
5885 if (have_staged_files) {
5886 err = check_non_staged_files(fileindex, paths);
5887 if (err)
5888 goto done;
5891 cc_arg.commitable_paths = &commitable_paths;
5892 cc_arg.worktree = worktree;
5893 cc_arg.fileindex = fileindex;
5894 cc_arg.repo = repo;
5895 cc_arg.have_staged_files = have_staged_files;
5896 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5897 TAILQ_FOREACH(pe, paths, entry) {
5898 err = worktree_status(worktree, pe->path, fileindex, repo,
5899 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5900 if (err)
5901 goto done;
5904 if (TAILQ_EMPTY(&commitable_paths)) {
5905 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5906 goto done;
5909 TAILQ_FOREACH(pe, paths, entry) {
5910 err = check_path_is_commitable(pe->path, &commitable_paths);
5911 if (err)
5912 goto done;
5915 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5916 struct got_commitable *ct = pe->data;
5917 const char *ct_path = ct->in_repo_path;
5919 while (ct_path[0] == '/')
5920 ct_path++;
5921 err = check_out_of_date(ct_path, ct->status,
5922 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5923 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5924 if (err)
5925 goto done;
5929 err = commit_worktree(new_commit_id, &commitable_paths,
5930 head_commit_id, NULL, worktree, author, committer,
5931 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5932 if (err)
5933 goto done;
5935 err = update_fileindex_after_commit(worktree, &commitable_paths,
5936 *new_commit_id, fileindex, have_staged_files);
5937 sync_err = sync_fileindex(fileindex, fileindex_path);
5938 if (sync_err && err == NULL)
5939 err = sync_err;
5940 done:
5941 if (fileindex)
5942 got_fileindex_free(fileindex);
5943 free(fileindex_path);
5944 unlockerr = lock_worktree(worktree, LOCK_SH);
5945 if (unlockerr && err == NULL)
5946 err = unlockerr;
5947 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5948 struct got_commitable *ct = pe->data;
5949 free_commitable(ct);
5951 got_pathlist_free(&commitable_paths);
5952 return err;
5955 const char *
5956 got_commitable_get_path(struct got_commitable *ct)
5958 return ct->path;
5961 unsigned int
5962 got_commitable_get_status(struct got_commitable *ct)
5964 return ct->status;
5967 struct check_rebase_ok_arg {
5968 struct got_worktree *worktree;
5969 struct got_repository *repo;
5972 static const struct got_error *
5973 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5975 const struct got_error *err = NULL;
5976 struct check_rebase_ok_arg *a = arg;
5977 unsigned char status;
5978 struct stat sb;
5979 char *ondisk_path;
5981 /* Reject rebase of a work tree with mixed base commits. */
5982 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5983 SHA1_DIGEST_LENGTH))
5984 return got_error(GOT_ERR_MIXED_COMMITS);
5986 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5987 == -1)
5988 return got_error_from_errno("asprintf");
5990 /* Reject rebase of a work tree with modified or staged files. */
5991 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5992 free(ondisk_path);
5993 if (err)
5994 return err;
5996 if (status != GOT_STATUS_NO_CHANGE)
5997 return got_error(GOT_ERR_MODIFIED);
5998 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5999 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6001 return NULL;
6004 const struct got_error *
6005 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6006 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6007 struct got_worktree *worktree, struct got_reference *branch,
6008 struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6012 char *branch_ref_name = NULL;
6013 char *fileindex_path = NULL;
6014 struct check_rebase_ok_arg ok_arg;
6015 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6016 struct got_object_id *wt_branch_tip = NULL;
6018 *new_base_branch_ref = NULL;
6019 *tmp_branch = NULL;
6020 *fileindex = NULL;
6022 err = lock_worktree(worktree, LOCK_EX);
6023 if (err)
6024 return err;
6026 err = open_fileindex(fileindex, &fileindex_path, worktree);
6027 if (err)
6028 goto done;
6030 ok_arg.worktree = worktree;
6031 ok_arg.repo = repo;
6032 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6033 &ok_arg);
6034 if (err)
6035 goto done;
6037 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6038 if (err)
6039 goto done;
6041 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6042 if (err)
6043 goto done;
6045 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6046 if (err)
6047 goto done;
6049 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6050 0);
6051 if (err)
6052 goto done;
6054 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6055 if (err)
6056 goto done;
6057 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6058 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6059 goto done;
6062 err = got_ref_alloc_symref(new_base_branch_ref,
6063 new_base_branch_ref_name, wt_branch);
6064 if (err)
6065 goto done;
6066 err = got_ref_write(*new_base_branch_ref, repo);
6067 if (err)
6068 goto done;
6070 /* TODO Lock original branch's ref while rebasing? */
6072 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6073 if (err)
6074 goto done;
6076 err = got_ref_write(branch_ref, repo);
6077 if (err)
6078 goto done;
6080 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6081 worktree->base_commit_id);
6082 if (err)
6083 goto done;
6084 err = got_ref_write(*tmp_branch, repo);
6085 if (err)
6086 goto done;
6088 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6089 if (err)
6090 goto done;
6091 done:
6092 free(fileindex_path);
6093 free(tmp_branch_name);
6094 free(new_base_branch_ref_name);
6095 free(branch_ref_name);
6096 if (branch_ref)
6097 got_ref_close(branch_ref);
6098 if (wt_branch)
6099 got_ref_close(wt_branch);
6100 free(wt_branch_tip);
6101 if (err) {
6102 if (*new_base_branch_ref) {
6103 got_ref_close(*new_base_branch_ref);
6104 *new_base_branch_ref = NULL;
6106 if (*tmp_branch) {
6107 got_ref_close(*tmp_branch);
6108 *tmp_branch = NULL;
6110 if (*fileindex) {
6111 got_fileindex_free(*fileindex);
6112 *fileindex = NULL;
6114 lock_worktree(worktree, LOCK_SH);
6116 return err;
6119 const struct got_error *
6120 got_worktree_rebase_continue(struct got_object_id **commit_id,
6121 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6122 struct got_reference **branch, struct got_fileindex **fileindex,
6123 struct got_worktree *worktree, struct got_repository *repo)
6125 const struct got_error *err;
6126 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6127 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6128 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6129 char *fileindex_path = NULL;
6130 int have_staged_files = 0;
6132 *commit_id = NULL;
6133 *new_base_branch = NULL;
6134 *tmp_branch = NULL;
6135 *branch = NULL;
6136 *fileindex = NULL;
6138 err = lock_worktree(worktree, LOCK_EX);
6139 if (err)
6140 return err;
6142 err = open_fileindex(fileindex, &fileindex_path, worktree);
6143 if (err)
6144 goto done;
6146 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6147 &have_staged_files);
6148 if (err && err->code != GOT_ERR_CANCELLED)
6149 goto done;
6150 if (have_staged_files) {
6151 err = got_error(GOT_ERR_STAGED_PATHS);
6152 goto done;
6155 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6156 if (err)
6157 goto done;
6159 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6160 if (err)
6161 goto done;
6163 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6164 if (err)
6165 goto done;
6167 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6168 if (err)
6169 goto done;
6171 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6172 if (err)
6173 goto done;
6175 err = got_ref_open(branch, repo,
6176 got_ref_get_symref_target(branch_ref), 0);
6177 if (err)
6178 goto done;
6180 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6181 if (err)
6182 goto done;
6184 err = got_ref_resolve(commit_id, repo, commit_ref);
6185 if (err)
6186 goto done;
6188 err = got_ref_open(new_base_branch, repo,
6189 new_base_branch_ref_name, 0);
6190 if (err)
6191 goto done;
6193 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6194 if (err)
6195 goto done;
6196 done:
6197 free(commit_ref_name);
6198 free(branch_ref_name);
6199 free(fileindex_path);
6200 if (commit_ref)
6201 got_ref_close(commit_ref);
6202 if (branch_ref)
6203 got_ref_close(branch_ref);
6204 if (err) {
6205 free(*commit_id);
6206 *commit_id = NULL;
6207 if (*tmp_branch) {
6208 got_ref_close(*tmp_branch);
6209 *tmp_branch = NULL;
6211 if (*new_base_branch) {
6212 got_ref_close(*new_base_branch);
6213 *new_base_branch = NULL;
6215 if (*branch) {
6216 got_ref_close(*branch);
6217 *branch = NULL;
6219 if (*fileindex) {
6220 got_fileindex_free(*fileindex);
6221 *fileindex = NULL;
6223 lock_worktree(worktree, LOCK_SH);
6225 return err;
6228 const struct got_error *
6229 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6231 const struct got_error *err;
6232 char *tmp_branch_name = NULL;
6234 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6235 if (err)
6236 return err;
6238 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6239 free(tmp_branch_name);
6240 return NULL;
6243 static const struct got_error *
6244 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6245 char **logmsg, void *arg)
6247 *logmsg = arg;
6248 return NULL;
6251 static const struct got_error *
6252 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6253 const char *path, struct got_object_id *blob_id,
6254 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6255 int dirfd, const char *de_name)
6257 return NULL;
6260 struct collect_merged_paths_arg {
6261 got_worktree_checkout_cb progress_cb;
6262 void *progress_arg;
6263 struct got_pathlist_head *merged_paths;
6266 static const struct got_error *
6267 collect_merged_paths(void *arg, unsigned char status, const char *path)
6269 const struct got_error *err;
6270 struct collect_merged_paths_arg *a = arg;
6271 char *p;
6272 struct got_pathlist_entry *new;
6274 err = (*a->progress_cb)(a->progress_arg, status, path);
6275 if (err)
6276 return err;
6278 if (status != GOT_STATUS_MERGE &&
6279 status != GOT_STATUS_ADD &&
6280 status != GOT_STATUS_DELETE &&
6281 status != GOT_STATUS_CONFLICT)
6282 return NULL;
6284 p = strdup(path);
6285 if (p == NULL)
6286 return got_error_from_errno("strdup");
6288 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6289 if (err || new == NULL)
6290 free(p);
6291 return err;
6294 void
6295 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6297 struct got_pathlist_entry *pe;
6299 TAILQ_FOREACH(pe, merged_paths, entry)
6300 free((char *)pe->path);
6302 got_pathlist_free(merged_paths);
6305 static const struct got_error *
6306 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6307 int is_rebase, struct got_repository *repo)
6309 const struct got_error *err;
6310 struct got_reference *commit_ref = NULL;
6312 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6313 if (err) {
6314 if (err->code != GOT_ERR_NOT_REF)
6315 goto done;
6316 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6317 if (err)
6318 goto done;
6319 err = got_ref_write(commit_ref, repo);
6320 if (err)
6321 goto done;
6322 } else if (is_rebase) {
6323 struct got_object_id *stored_id;
6324 int cmp;
6326 err = got_ref_resolve(&stored_id, repo, commit_ref);
6327 if (err)
6328 goto done;
6329 cmp = got_object_id_cmp(commit_id, stored_id);
6330 free(stored_id);
6331 if (cmp != 0) {
6332 err = got_error(GOT_ERR_REBASE_COMMITID);
6333 goto done;
6336 done:
6337 if (commit_ref)
6338 got_ref_close(commit_ref);
6339 return err;
6342 static const struct got_error *
6343 rebase_merge_files(struct got_pathlist_head *merged_paths,
6344 const char *commit_ref_name, struct got_worktree *worktree,
6345 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6346 struct got_object_id *commit_id, struct got_repository *repo,
6347 got_worktree_checkout_cb progress_cb, void *progress_arg,
6348 got_cancel_cb cancel_cb, void *cancel_arg)
6350 const struct got_error *err;
6351 struct got_reference *commit_ref = NULL;
6352 struct collect_merged_paths_arg cmp_arg;
6353 char *fileindex_path;
6355 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6357 err = get_fileindex_path(&fileindex_path, worktree);
6358 if (err)
6359 return err;
6361 cmp_arg.progress_cb = progress_cb;
6362 cmp_arg.progress_arg = progress_arg;
6363 cmp_arg.merged_paths = merged_paths;
6364 err = merge_files(worktree, fileindex, fileindex_path,
6365 parent_commit_id, commit_id, repo, collect_merged_paths,
6366 &cmp_arg, cancel_cb, cancel_arg);
6367 if (commit_ref)
6368 got_ref_close(commit_ref);
6369 return err;
6372 const struct got_error *
6373 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6374 struct got_worktree *worktree, struct got_fileindex *fileindex,
6375 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6376 struct got_repository *repo,
6377 got_worktree_checkout_cb progress_cb, void *progress_arg,
6378 got_cancel_cb cancel_cb, void *cancel_arg)
6380 const struct got_error *err;
6381 char *commit_ref_name;
6383 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6384 if (err)
6385 return err;
6387 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6388 if (err)
6389 goto done;
6391 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6392 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6393 progress_arg, cancel_cb, cancel_arg);
6394 done:
6395 free(commit_ref_name);
6396 return err;
6399 const struct got_error *
6400 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6401 struct got_worktree *worktree, struct got_fileindex *fileindex,
6402 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6403 struct got_repository *repo,
6404 got_worktree_checkout_cb progress_cb, void *progress_arg,
6405 got_cancel_cb cancel_cb, void *cancel_arg)
6407 const struct got_error *err;
6408 char *commit_ref_name;
6410 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6411 if (err)
6412 return err;
6414 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6415 if (err)
6416 goto done;
6418 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6419 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6420 progress_arg, cancel_cb, cancel_arg);
6421 done:
6422 free(commit_ref_name);
6423 return err;
6426 static const struct got_error *
6427 rebase_commit(struct got_object_id **new_commit_id,
6428 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6429 struct got_worktree *worktree, struct got_fileindex *fileindex,
6430 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6431 const char *new_logmsg, struct got_repository *repo)
6433 const struct got_error *err, *sync_err;
6434 struct got_pathlist_head commitable_paths;
6435 struct collect_commitables_arg cc_arg;
6436 char *fileindex_path = NULL;
6437 struct got_reference *head_ref = NULL;
6438 struct got_object_id *head_commit_id = NULL;
6439 char *logmsg = NULL;
6441 TAILQ_INIT(&commitable_paths);
6442 *new_commit_id = NULL;
6444 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6446 err = get_fileindex_path(&fileindex_path, worktree);
6447 if (err)
6448 return err;
6450 cc_arg.commitable_paths = &commitable_paths;
6451 cc_arg.worktree = worktree;
6452 cc_arg.repo = repo;
6453 cc_arg.have_staged_files = 0;
6455 * If possible get the status of individual files directly to
6456 * avoid crawling the entire work tree once per rebased commit.
6458 * Ideally, merged_paths would contain a list of commitables
6459 * we could use so we could skip worktree_status() entirely.
6460 * However, we would then need carefully keep track of cumulative
6461 * effects of operations such as file additions and deletions
6462 * in 'got histedit -f' (folding multiple commits into one),
6463 * and this extra complexity is not really worth it.
6465 if (merged_paths) {
6466 struct got_pathlist_entry *pe;
6467 TAILQ_FOREACH(pe, merged_paths, entry) {
6468 err = worktree_status(worktree, pe->path, fileindex,
6469 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6470 0);
6471 if (err)
6472 goto done;
6474 } else {
6475 err = worktree_status(worktree, "", fileindex, repo,
6476 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6477 if (err)
6478 goto done;
6481 if (TAILQ_EMPTY(&commitable_paths)) {
6482 /* No-op change; commit will be elided. */
6483 err = got_ref_delete(commit_ref, repo);
6484 if (err)
6485 goto done;
6486 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6487 goto done;
6490 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6491 if (err)
6492 goto done;
6494 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6495 if (err)
6496 goto done;
6498 if (new_logmsg) {
6499 logmsg = strdup(new_logmsg);
6500 if (logmsg == NULL) {
6501 err = got_error_from_errno("strdup");
6502 goto done;
6504 } else {
6505 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6506 if (err)
6507 goto done;
6510 /* NB: commit_worktree will call free(logmsg) */
6511 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6512 NULL, worktree, got_object_commit_get_author(orig_commit),
6513 got_object_commit_get_committer(orig_commit),
6514 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6515 if (err)
6516 goto done;
6518 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6519 if (err)
6520 goto done;
6522 err = got_ref_delete(commit_ref, repo);
6523 if (err)
6524 goto done;
6526 err = update_fileindex_after_commit(worktree, &commitable_paths,
6527 *new_commit_id, fileindex, 0);
6528 sync_err = sync_fileindex(fileindex, fileindex_path);
6529 if (sync_err && err == NULL)
6530 err = sync_err;
6531 done:
6532 free(fileindex_path);
6533 free(head_commit_id);
6534 if (head_ref)
6535 got_ref_close(head_ref);
6536 if (err) {
6537 free(*new_commit_id);
6538 *new_commit_id = NULL;
6540 return err;
6543 const struct got_error *
6544 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6545 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6546 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6547 struct got_commit_object *orig_commit,
6548 struct got_object_id *orig_commit_id, struct got_repository *repo)
6550 const struct got_error *err;
6551 char *commit_ref_name;
6552 struct got_reference *commit_ref = NULL;
6553 struct got_object_id *commit_id = NULL;
6555 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6556 if (err)
6557 return err;
6559 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6560 if (err)
6561 goto done;
6562 err = got_ref_resolve(&commit_id, repo, commit_ref);
6563 if (err)
6564 goto done;
6565 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6566 err = got_error(GOT_ERR_REBASE_COMMITID);
6567 goto done;
6570 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6571 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6572 done:
6573 if (commit_ref)
6574 got_ref_close(commit_ref);
6575 free(commit_ref_name);
6576 free(commit_id);
6577 return err;
6580 const struct got_error *
6581 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6582 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6583 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6584 struct got_commit_object *orig_commit,
6585 struct got_object_id *orig_commit_id, const char *new_logmsg,
6586 struct got_repository *repo)
6588 const struct got_error *err;
6589 char *commit_ref_name;
6590 struct got_reference *commit_ref = NULL;
6592 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6593 if (err)
6594 return err;
6596 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6597 if (err)
6598 goto done;
6600 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6601 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6602 done:
6603 if (commit_ref)
6604 got_ref_close(commit_ref);
6605 free(commit_ref_name);
6606 return err;
6609 const struct got_error *
6610 got_worktree_rebase_postpone(struct got_worktree *worktree,
6611 struct got_fileindex *fileindex)
6613 if (fileindex)
6614 got_fileindex_free(fileindex);
6615 return lock_worktree(worktree, LOCK_SH);
6618 static const struct got_error *
6619 delete_ref(const char *name, struct got_repository *repo)
6621 const struct got_error *err;
6622 struct got_reference *ref;
6624 err = got_ref_open(&ref, repo, name, 0);
6625 if (err) {
6626 if (err->code == GOT_ERR_NOT_REF)
6627 return NULL;
6628 return err;
6631 err = got_ref_delete(ref, repo);
6632 got_ref_close(ref);
6633 return err;
6636 static const struct got_error *
6637 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6639 const struct got_error *err;
6640 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6641 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6643 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6644 if (err)
6645 goto done;
6646 err = delete_ref(tmp_branch_name, repo);
6647 if (err)
6648 goto done;
6650 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6651 if (err)
6652 goto done;
6653 err = delete_ref(new_base_branch_ref_name, repo);
6654 if (err)
6655 goto done;
6657 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6658 if (err)
6659 goto done;
6660 err = delete_ref(branch_ref_name, repo);
6661 if (err)
6662 goto done;
6664 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6665 if (err)
6666 goto done;
6667 err = delete_ref(commit_ref_name, repo);
6668 if (err)
6669 goto done;
6671 done:
6672 free(tmp_branch_name);
6673 free(new_base_branch_ref_name);
6674 free(branch_ref_name);
6675 free(commit_ref_name);
6676 return err;
6679 const struct got_error *
6680 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6681 struct got_object_id *new_commit_id, struct got_repository *repo)
6683 const struct got_error *err;
6684 struct got_reference *ref = NULL;
6685 struct got_object_id *old_commit_id = NULL;
6686 const char *branch_name = NULL;
6687 char *new_id_str = NULL;
6688 char *refname = NULL;
6690 branch_name = got_ref_get_name(branch);
6691 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6692 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6693 branch_name += 11;
6695 err = got_object_id_str(&new_id_str, new_commit_id);
6696 if (err)
6697 return err;
6699 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6700 new_id_str) == -1) {
6701 err = got_error_from_errno("asprintf");
6702 goto done;
6705 err = got_ref_resolve(&old_commit_id, repo, branch);
6706 if (err)
6707 goto done;
6709 err = got_ref_alloc(&ref, refname, old_commit_id);
6710 if (err)
6711 goto done;
6713 err = got_ref_write(ref, repo);
6714 done:
6715 free(new_id_str);
6716 free(refname);
6717 free(old_commit_id);
6718 if (ref)
6719 got_ref_close(ref);
6720 return err;
6723 const struct got_error *
6724 got_worktree_rebase_complete(struct got_worktree *worktree,
6725 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6726 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6727 struct got_repository *repo, int create_backup)
6729 const struct got_error *err, *unlockerr, *sync_err;
6730 struct got_object_id *new_head_commit_id = NULL;
6731 char *fileindex_path = NULL;
6733 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6734 if (err)
6735 return err;
6737 if (create_backup) {
6738 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6739 rebased_branch, new_head_commit_id, repo);
6740 if (err)
6741 goto done;
6744 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6745 if (err)
6746 goto done;
6748 err = got_ref_write(rebased_branch, repo);
6749 if (err)
6750 goto done;
6752 err = got_worktree_set_head_ref(worktree, rebased_branch);
6753 if (err)
6754 goto done;
6756 err = delete_rebase_refs(worktree, repo);
6757 if (err)
6758 goto done;
6760 err = get_fileindex_path(&fileindex_path, worktree);
6761 if (err)
6762 goto done;
6763 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6764 sync_err = sync_fileindex(fileindex, fileindex_path);
6765 if (sync_err && err == NULL)
6766 err = sync_err;
6767 done:
6768 got_fileindex_free(fileindex);
6769 free(fileindex_path);
6770 free(new_head_commit_id);
6771 unlockerr = lock_worktree(worktree, LOCK_SH);
6772 if (unlockerr && err == NULL)
6773 err = unlockerr;
6774 return err;
6777 const struct got_error *
6778 got_worktree_rebase_abort(struct got_worktree *worktree,
6779 struct got_fileindex *fileindex, struct got_repository *repo,
6780 struct got_reference *new_base_branch,
6781 got_worktree_checkout_cb progress_cb, void *progress_arg)
6783 const struct got_error *err, *unlockerr, *sync_err;
6784 struct got_reference *resolved = NULL;
6785 struct got_object_id *commit_id = NULL;
6786 char *fileindex_path = NULL;
6787 struct revert_file_args rfa;
6788 struct got_object_id *tree_id = NULL;
6790 err = lock_worktree(worktree, LOCK_EX);
6791 if (err)
6792 return err;
6794 err = got_ref_open(&resolved, repo,
6795 got_ref_get_symref_target(new_base_branch), 0);
6796 if (err)
6797 goto done;
6799 err = got_worktree_set_head_ref(worktree, resolved);
6800 if (err)
6801 goto done;
6804 * XXX commits to the base branch could have happened while
6805 * we were busy rebasing; should we store the original commit ID
6806 * when rebase begins and read it back here?
6808 err = got_ref_resolve(&commit_id, repo, resolved);
6809 if (err)
6810 goto done;
6812 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6813 if (err)
6814 goto done;
6816 err = got_object_id_by_path(&tree_id, repo,
6817 worktree->base_commit_id, worktree->path_prefix);
6818 if (err)
6819 goto done;
6821 err = delete_rebase_refs(worktree, repo);
6822 if (err)
6823 goto done;
6825 err = get_fileindex_path(&fileindex_path, worktree);
6826 if (err)
6827 goto done;
6829 rfa.worktree = worktree;
6830 rfa.fileindex = fileindex;
6831 rfa.progress_cb = progress_cb;
6832 rfa.progress_arg = progress_arg;
6833 rfa.patch_cb = NULL;
6834 rfa.patch_arg = NULL;
6835 rfa.repo = repo;
6836 rfa.unlink_added_files = 0;
6837 err = worktree_status(worktree, "", fileindex, repo,
6838 revert_file, &rfa, NULL, NULL, 1, 0);
6839 if (err)
6840 goto sync;
6842 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6843 repo, progress_cb, progress_arg, NULL, NULL);
6844 sync:
6845 sync_err = sync_fileindex(fileindex, fileindex_path);
6846 if (sync_err && err == NULL)
6847 err = sync_err;
6848 done:
6849 got_ref_close(resolved);
6850 free(tree_id);
6851 free(commit_id);
6852 if (fileindex)
6853 got_fileindex_free(fileindex);
6854 free(fileindex_path);
6856 unlockerr = lock_worktree(worktree, LOCK_SH);
6857 if (unlockerr && err == NULL)
6858 err = unlockerr;
6859 return err;
6862 const struct got_error *
6863 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6864 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6865 struct got_fileindex **fileindex, struct got_worktree *worktree,
6866 struct got_repository *repo)
6868 const struct got_error *err = NULL;
6869 char *tmp_branch_name = NULL;
6870 char *branch_ref_name = NULL;
6871 char *base_commit_ref_name = NULL;
6872 char *fileindex_path = NULL;
6873 struct check_rebase_ok_arg ok_arg;
6874 struct got_reference *wt_branch = NULL;
6875 struct got_reference *base_commit_ref = NULL;
6877 *tmp_branch = NULL;
6878 *branch_ref = NULL;
6879 *base_commit_id = NULL;
6880 *fileindex = NULL;
6882 err = lock_worktree(worktree, LOCK_EX);
6883 if (err)
6884 return err;
6886 err = open_fileindex(fileindex, &fileindex_path, worktree);
6887 if (err)
6888 goto done;
6890 ok_arg.worktree = worktree;
6891 ok_arg.repo = repo;
6892 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6893 &ok_arg);
6894 if (err)
6895 goto done;
6897 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6898 if (err)
6899 goto done;
6901 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6902 if (err)
6903 goto done;
6905 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6906 worktree);
6907 if (err)
6908 goto done;
6910 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6911 0);
6912 if (err)
6913 goto done;
6915 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6916 if (err)
6917 goto done;
6919 err = got_ref_write(*branch_ref, repo);
6920 if (err)
6921 goto done;
6923 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6924 worktree->base_commit_id);
6925 if (err)
6926 goto done;
6927 err = got_ref_write(base_commit_ref, repo);
6928 if (err)
6929 goto done;
6930 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6931 if (*base_commit_id == NULL) {
6932 err = got_error_from_errno("got_object_id_dup");
6933 goto done;
6936 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6937 worktree->base_commit_id);
6938 if (err)
6939 goto done;
6940 err = got_ref_write(*tmp_branch, repo);
6941 if (err)
6942 goto done;
6944 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6945 if (err)
6946 goto done;
6947 done:
6948 free(fileindex_path);
6949 free(tmp_branch_name);
6950 free(branch_ref_name);
6951 free(base_commit_ref_name);
6952 if (wt_branch)
6953 got_ref_close(wt_branch);
6954 if (err) {
6955 if (*branch_ref) {
6956 got_ref_close(*branch_ref);
6957 *branch_ref = NULL;
6959 if (*tmp_branch) {
6960 got_ref_close(*tmp_branch);
6961 *tmp_branch = NULL;
6963 free(*base_commit_id);
6964 if (*fileindex) {
6965 got_fileindex_free(*fileindex);
6966 *fileindex = NULL;
6968 lock_worktree(worktree, LOCK_SH);
6970 return err;
6973 const struct got_error *
6974 got_worktree_histedit_postpone(struct got_worktree *worktree,
6975 struct got_fileindex *fileindex)
6977 if (fileindex)
6978 got_fileindex_free(fileindex);
6979 return lock_worktree(worktree, LOCK_SH);
6982 const struct got_error *
6983 got_worktree_histedit_in_progress(int *in_progress,
6984 struct got_worktree *worktree)
6986 const struct got_error *err;
6987 char *tmp_branch_name = NULL;
6989 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6990 if (err)
6991 return err;
6993 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6994 free(tmp_branch_name);
6995 return NULL;
6998 const struct got_error *
6999 got_worktree_histedit_continue(struct got_object_id **commit_id,
7000 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7001 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7002 struct got_worktree *worktree, struct got_repository *repo)
7004 const struct got_error *err;
7005 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7006 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7007 struct got_reference *commit_ref = NULL;
7008 struct got_reference *base_commit_ref = NULL;
7009 char *fileindex_path = NULL;
7010 int have_staged_files = 0;
7012 *commit_id = NULL;
7013 *tmp_branch = NULL;
7014 *base_commit_id = NULL;
7015 *fileindex = NULL;
7017 err = lock_worktree(worktree, LOCK_EX);
7018 if (err)
7019 return err;
7021 err = open_fileindex(fileindex, &fileindex_path, worktree);
7022 if (err)
7023 goto done;
7025 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7026 &have_staged_files);
7027 if (err && err->code != GOT_ERR_CANCELLED)
7028 goto done;
7029 if (have_staged_files) {
7030 err = got_error(GOT_ERR_STAGED_PATHS);
7031 goto done;
7034 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7035 if (err)
7036 goto done;
7038 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7039 if (err)
7040 goto done;
7042 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7043 if (err)
7044 goto done;
7046 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7047 worktree);
7048 if (err)
7049 goto done;
7051 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7052 if (err)
7053 goto done;
7055 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7056 if (err)
7057 goto done;
7058 err = got_ref_resolve(commit_id, repo, commit_ref);
7059 if (err)
7060 goto done;
7062 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7063 if (err)
7064 goto done;
7065 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7066 if (err)
7067 goto done;
7069 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7070 if (err)
7071 goto done;
7072 done:
7073 free(commit_ref_name);
7074 free(branch_ref_name);
7075 free(fileindex_path);
7076 if (commit_ref)
7077 got_ref_close(commit_ref);
7078 if (base_commit_ref)
7079 got_ref_close(base_commit_ref);
7080 if (err) {
7081 free(*commit_id);
7082 *commit_id = NULL;
7083 free(*base_commit_id);
7084 *base_commit_id = NULL;
7085 if (*tmp_branch) {
7086 got_ref_close(*tmp_branch);
7087 *tmp_branch = NULL;
7089 if (*fileindex) {
7090 got_fileindex_free(*fileindex);
7091 *fileindex = NULL;
7093 lock_worktree(worktree, LOCK_EX);
7095 return err;
7098 static const struct got_error *
7099 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7101 const struct got_error *err;
7102 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7103 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7105 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7106 if (err)
7107 goto done;
7108 err = delete_ref(tmp_branch_name, repo);
7109 if (err)
7110 goto done;
7112 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7113 worktree);
7114 if (err)
7115 goto done;
7116 err = delete_ref(base_commit_ref_name, repo);
7117 if (err)
7118 goto done;
7120 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7121 if (err)
7122 goto done;
7123 err = delete_ref(branch_ref_name, repo);
7124 if (err)
7125 goto done;
7127 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7128 if (err)
7129 goto done;
7130 err = delete_ref(commit_ref_name, repo);
7131 if (err)
7132 goto done;
7133 done:
7134 free(tmp_branch_name);
7135 free(base_commit_ref_name);
7136 free(branch_ref_name);
7137 free(commit_ref_name);
7138 return err;
7141 const struct got_error *
7142 got_worktree_histedit_abort(struct got_worktree *worktree,
7143 struct got_fileindex *fileindex, struct got_repository *repo,
7144 struct got_reference *branch, struct got_object_id *base_commit_id,
7145 got_worktree_checkout_cb progress_cb, void *progress_arg)
7147 const struct got_error *err, *unlockerr, *sync_err;
7148 struct got_reference *resolved = NULL;
7149 char *fileindex_path = NULL;
7150 struct got_object_id *tree_id = NULL;
7151 struct revert_file_args rfa;
7153 err = lock_worktree(worktree, LOCK_EX);
7154 if (err)
7155 return err;
7157 err = got_ref_open(&resolved, repo,
7158 got_ref_get_symref_target(branch), 0);
7159 if (err)
7160 goto done;
7162 err = got_worktree_set_head_ref(worktree, resolved);
7163 if (err)
7164 goto done;
7166 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7167 if (err)
7168 goto done;
7170 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7171 worktree->path_prefix);
7172 if (err)
7173 goto done;
7175 err = delete_histedit_refs(worktree, repo);
7176 if (err)
7177 goto done;
7179 err = get_fileindex_path(&fileindex_path, worktree);
7180 if (err)
7181 goto done;
7183 rfa.worktree = worktree;
7184 rfa.fileindex = fileindex;
7185 rfa.progress_cb = progress_cb;
7186 rfa.progress_arg = progress_arg;
7187 rfa.patch_cb = NULL;
7188 rfa.patch_arg = NULL;
7189 rfa.repo = repo;
7190 rfa.unlink_added_files = 0;
7191 err = worktree_status(worktree, "", fileindex, repo,
7192 revert_file, &rfa, NULL, NULL, 1, 0);
7193 if (err)
7194 goto sync;
7196 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7197 repo, progress_cb, progress_arg, NULL, NULL);
7198 sync:
7199 sync_err = sync_fileindex(fileindex, fileindex_path);
7200 if (sync_err && err == NULL)
7201 err = sync_err;
7202 done:
7203 got_ref_close(resolved);
7204 free(tree_id);
7205 free(fileindex_path);
7207 unlockerr = lock_worktree(worktree, LOCK_SH);
7208 if (unlockerr && err == NULL)
7209 err = unlockerr;
7210 return err;
7213 const struct got_error *
7214 got_worktree_histedit_complete(struct got_worktree *worktree,
7215 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7216 struct got_reference *edited_branch, struct got_repository *repo)
7218 const struct got_error *err, *unlockerr, *sync_err;
7219 struct got_object_id *new_head_commit_id = NULL;
7220 struct got_reference *resolved = NULL;
7221 char *fileindex_path = NULL;
7223 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7224 if (err)
7225 return err;
7227 err = got_ref_open(&resolved, repo,
7228 got_ref_get_symref_target(edited_branch), 0);
7229 if (err)
7230 goto done;
7232 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7233 resolved, new_head_commit_id, repo);
7234 if (err)
7235 goto done;
7237 err = got_ref_change_ref(resolved, new_head_commit_id);
7238 if (err)
7239 goto done;
7241 err = got_ref_write(resolved, repo);
7242 if (err)
7243 goto done;
7245 err = got_worktree_set_head_ref(worktree, resolved);
7246 if (err)
7247 goto done;
7249 err = delete_histedit_refs(worktree, repo);
7250 if (err)
7251 goto done;
7253 err = get_fileindex_path(&fileindex_path, worktree);
7254 if (err)
7255 goto done;
7256 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7257 sync_err = sync_fileindex(fileindex, fileindex_path);
7258 if (sync_err && err == NULL)
7259 err = sync_err;
7260 done:
7261 got_fileindex_free(fileindex);
7262 free(fileindex_path);
7263 free(new_head_commit_id);
7264 unlockerr = lock_worktree(worktree, LOCK_SH);
7265 if (unlockerr && err == NULL)
7266 err = unlockerr;
7267 return err;
7270 const struct got_error *
7271 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7272 struct got_object_id *commit_id, struct got_repository *repo)
7274 const struct got_error *err;
7275 char *commit_ref_name;
7277 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7278 if (err)
7279 return err;
7281 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7282 if (err)
7283 goto done;
7285 err = delete_ref(commit_ref_name, repo);
7286 done:
7287 free(commit_ref_name);
7288 return err;
7291 const struct got_error *
7292 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7293 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7294 struct got_worktree *worktree, const char *refname,
7295 struct got_repository *repo)
7297 const struct got_error *err = NULL;
7298 char *fileindex_path = NULL;
7299 struct check_rebase_ok_arg ok_arg;
7301 *fileindex = NULL;
7302 *branch_ref = NULL;
7303 *base_branch_ref = NULL;
7305 err = lock_worktree(worktree, LOCK_EX);
7306 if (err)
7307 return err;
7309 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7310 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7311 "cannot integrate a branch into itself; "
7312 "update -b or different branch name required");
7313 goto done;
7316 err = open_fileindex(fileindex, &fileindex_path, worktree);
7317 if (err)
7318 goto done;
7320 /* Preconditions are the same as for rebase. */
7321 ok_arg.worktree = worktree;
7322 ok_arg.repo = repo;
7323 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7324 &ok_arg);
7325 if (err)
7326 goto done;
7328 err = got_ref_open(branch_ref, repo, refname, 1);
7329 if (err)
7330 goto done;
7332 err = got_ref_open(base_branch_ref, repo,
7333 got_worktree_get_head_ref_name(worktree), 1);
7334 done:
7335 if (err) {
7336 if (*branch_ref) {
7337 got_ref_close(*branch_ref);
7338 *branch_ref = NULL;
7340 if (*base_branch_ref) {
7341 got_ref_close(*base_branch_ref);
7342 *base_branch_ref = NULL;
7344 if (*fileindex) {
7345 got_fileindex_free(*fileindex);
7346 *fileindex = NULL;
7348 lock_worktree(worktree, LOCK_SH);
7350 return err;
7353 const struct got_error *
7354 got_worktree_integrate_continue(struct got_worktree *worktree,
7355 struct got_fileindex *fileindex, struct got_repository *repo,
7356 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7357 got_worktree_checkout_cb progress_cb, void *progress_arg,
7358 got_cancel_cb cancel_cb, void *cancel_arg)
7360 const struct got_error *err = NULL, *sync_err, *unlockerr;
7361 char *fileindex_path = NULL;
7362 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7364 err = get_fileindex_path(&fileindex_path, worktree);
7365 if (err)
7366 goto done;
7368 err = got_ref_resolve(&commit_id, repo, branch_ref);
7369 if (err)
7370 goto done;
7372 err = got_object_id_by_path(&tree_id, repo, commit_id,
7373 worktree->path_prefix);
7374 if (err)
7375 goto done;
7377 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7378 if (err)
7379 goto done;
7381 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7382 progress_cb, progress_arg, cancel_cb, cancel_arg);
7383 if (err)
7384 goto sync;
7386 err = got_ref_change_ref(base_branch_ref, commit_id);
7387 if (err)
7388 goto sync;
7390 err = got_ref_write(base_branch_ref, repo);
7391 if (err)
7392 goto sync;
7394 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7395 sync:
7396 sync_err = sync_fileindex(fileindex, fileindex_path);
7397 if (sync_err && err == NULL)
7398 err = sync_err;
7400 done:
7401 unlockerr = got_ref_unlock(branch_ref);
7402 if (unlockerr && err == NULL)
7403 err = unlockerr;
7404 got_ref_close(branch_ref);
7406 unlockerr = got_ref_unlock(base_branch_ref);
7407 if (unlockerr && err == NULL)
7408 err = unlockerr;
7409 got_ref_close(base_branch_ref);
7411 got_fileindex_free(fileindex);
7412 free(fileindex_path);
7413 free(tree_id);
7415 unlockerr = lock_worktree(worktree, LOCK_SH);
7416 if (unlockerr && err == NULL)
7417 err = unlockerr;
7418 return err;
7421 const struct got_error *
7422 got_worktree_integrate_abort(struct got_worktree *worktree,
7423 struct got_fileindex *fileindex, struct got_repository *repo,
7424 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7426 const struct got_error *err = NULL, *unlockerr = NULL;
7428 got_fileindex_free(fileindex);
7430 err = lock_worktree(worktree, LOCK_SH);
7432 unlockerr = got_ref_unlock(branch_ref);
7433 if (unlockerr && err == NULL)
7434 err = unlockerr;
7435 got_ref_close(branch_ref);
7437 unlockerr = got_ref_unlock(base_branch_ref);
7438 if (unlockerr && err == NULL)
7439 err = unlockerr;
7440 got_ref_close(base_branch_ref);
7442 return err;
7445 const struct got_error *
7446 got_worktree_merge_postpone(struct got_worktree *worktree,
7447 struct got_fileindex *fileindex)
7449 const struct got_error *err, *sync_err;
7450 char *fileindex_path = NULL;
7452 err = get_fileindex_path(&fileindex_path, worktree);
7453 if (err)
7454 goto done;
7456 sync_err = sync_fileindex(fileindex, fileindex_path);
7458 err = lock_worktree(worktree, LOCK_SH);
7459 if (sync_err && err == NULL)
7460 err = sync_err;
7461 done:
7462 got_fileindex_free(fileindex);
7463 free(fileindex_path);
7464 return err;
7467 static const struct got_error *
7468 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7470 const struct got_error *err;
7471 char *branch_refname = NULL, *commit_refname = NULL;
7473 err = get_merge_branch_ref_name(&branch_refname, worktree);
7474 if (err)
7475 goto done;
7476 err = delete_ref(branch_refname, repo);
7477 if (err)
7478 goto done;
7480 err = get_merge_commit_ref_name(&commit_refname, worktree);
7481 if (err)
7482 goto done;
7483 err = delete_ref(commit_refname, repo);
7484 if (err)
7485 goto done;
7487 done:
7488 free(branch_refname);
7489 free(commit_refname);
7490 return err;
7493 struct merge_commit_msg_arg {
7494 struct got_worktree *worktree;
7495 const char *branch_name;
7498 static const struct got_error *
7499 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7500 void *arg)
7502 struct merge_commit_msg_arg *a = arg;
7504 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7505 got_worktree_get_head_ref_name(a->worktree)) == -1)
7506 return got_error_from_errno("asprintf");
7508 return NULL;
7512 const struct got_error *
7513 got_worktree_merge_branch(struct got_worktree *worktree,
7514 struct got_fileindex *fileindex,
7515 struct got_object_id *yca_commit_id,
7516 struct got_object_id *branch_tip,
7517 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7518 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7520 const struct got_error *err;
7521 char *fileindex_path = NULL;
7523 err = get_fileindex_path(&fileindex_path, worktree);
7524 if (err)
7525 goto done;
7527 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7528 worktree);
7529 if (err)
7530 goto done;
7532 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7533 branch_tip, repo, progress_cb, progress_arg,
7534 cancel_cb, cancel_arg);
7535 done:
7536 free(fileindex_path);
7537 return err;
7540 const struct got_error *
7541 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7542 struct got_worktree *worktree, struct got_fileindex *fileindex,
7543 const char *author, const char *committer, int allow_bad_symlinks,
7544 struct got_object_id *branch_tip, const char *branch_name,
7545 struct got_repository *repo,
7546 got_worktree_status_cb status_cb, void *status_arg)
7549 const struct got_error *err = NULL, *sync_err;
7550 struct got_pathlist_head commitable_paths;
7551 struct collect_commitables_arg cc_arg;
7552 struct got_pathlist_entry *pe;
7553 struct got_reference *head_ref = NULL;
7554 struct got_object_id *head_commit_id = NULL;
7555 int have_staged_files = 0;
7556 struct merge_commit_msg_arg mcm_arg;
7557 char *fileindex_path = NULL;
7559 *new_commit_id = NULL;
7561 TAILQ_INIT(&commitable_paths);
7563 err = get_fileindex_path(&fileindex_path, worktree);
7564 if (err)
7565 goto done;
7567 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7568 if (err)
7569 goto done;
7571 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7572 if (err)
7573 goto done;
7575 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7576 &have_staged_files);
7577 if (err && err->code != GOT_ERR_CANCELLED)
7578 goto done;
7579 if (have_staged_files) {
7580 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7581 goto done;
7584 cc_arg.commitable_paths = &commitable_paths;
7585 cc_arg.worktree = worktree;
7586 cc_arg.fileindex = fileindex;
7587 cc_arg.repo = repo;
7588 cc_arg.have_staged_files = have_staged_files;
7589 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7590 err = worktree_status(worktree, "", fileindex, repo,
7591 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7592 if (err)
7593 goto done;
7595 if (TAILQ_EMPTY(&commitable_paths)) {
7596 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7597 "merge of %s cannot proceed", branch_name);
7598 goto done;
7601 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7602 struct got_commitable *ct = pe->data;
7603 const char *ct_path = ct->in_repo_path;
7605 while (ct_path[0] == '/')
7606 ct_path++;
7607 err = check_out_of_date(ct_path, ct->status,
7608 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7609 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7610 if (err)
7611 goto done;
7615 mcm_arg.worktree = worktree;
7616 mcm_arg.branch_name = branch_name;
7617 err = commit_worktree(new_commit_id, &commitable_paths,
7618 head_commit_id, branch_tip, worktree, author, committer,
7619 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7620 if (err)
7621 goto done;
7623 err = update_fileindex_after_commit(worktree, &commitable_paths,
7624 *new_commit_id, fileindex, have_staged_files);
7625 sync_err = sync_fileindex(fileindex, fileindex_path);
7626 if (sync_err && err == NULL)
7627 err = sync_err;
7628 done:
7629 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7630 struct got_commitable *ct = pe->data;
7631 free_commitable(ct);
7633 got_pathlist_free(&commitable_paths);
7634 free(fileindex_path);
7635 return err;
7638 const struct got_error *
7639 got_worktree_merge_complete(struct got_worktree *worktree,
7640 struct got_fileindex *fileindex, struct got_repository *repo)
7642 const struct got_error *err, *unlockerr, *sync_err;
7643 char *fileindex_path = NULL;
7645 err = delete_merge_refs(worktree, repo);
7646 if (err)
7647 goto done;
7649 err = get_fileindex_path(&fileindex_path, worktree);
7650 if (err)
7651 goto done;
7652 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7653 sync_err = sync_fileindex(fileindex, fileindex_path);
7654 if (sync_err && err == NULL)
7655 err = sync_err;
7656 done:
7657 got_fileindex_free(fileindex);
7658 free(fileindex_path);
7659 unlockerr = lock_worktree(worktree, LOCK_SH);
7660 if (unlockerr && err == NULL)
7661 err = unlockerr;
7662 return err;
7665 const struct got_error *
7666 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7667 struct got_repository *repo)
7669 const struct got_error *err;
7670 char *branch_refname = NULL;
7671 struct got_reference *branch_ref = NULL;
7673 *in_progress = 0;
7675 err = get_merge_branch_ref_name(&branch_refname, worktree);
7676 if (err)
7677 return err;
7678 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7679 free(branch_refname);
7680 if (err) {
7681 if (err->code != GOT_ERR_NOT_REF)
7682 return err;
7683 } else
7684 *in_progress = 1;
7686 return NULL;
7689 const struct got_error *got_worktree_merge_prepare(
7690 struct got_fileindex **fileindex, struct got_worktree *worktree,
7691 struct got_reference *branch, struct got_repository *repo)
7693 const struct got_error *err = NULL;
7694 char *fileindex_path = NULL;
7695 char *branch_refname = NULL, *commit_refname = NULL;
7696 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7697 struct got_reference *commit_ref = NULL;
7698 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7699 struct check_rebase_ok_arg ok_arg;
7701 *fileindex = NULL;
7703 err = lock_worktree(worktree, LOCK_EX);
7704 if (err)
7705 return err;
7707 err = open_fileindex(fileindex, &fileindex_path, worktree);
7708 if (err)
7709 goto done;
7711 /* Preconditions are the same as for rebase. */
7712 ok_arg.worktree = worktree;
7713 ok_arg.repo = repo;
7714 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7715 &ok_arg);
7716 if (err)
7717 goto done;
7719 err = get_merge_branch_ref_name(&branch_refname, worktree);
7720 if (err)
7721 return err;
7723 err = get_merge_commit_ref_name(&commit_refname, worktree);
7724 if (err)
7725 return err;
7727 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7728 0);
7729 if (err)
7730 goto done;
7732 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7733 if (err)
7734 goto done;
7736 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7737 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7738 goto done;
7741 err = got_ref_resolve(&branch_tip, repo, branch);
7742 if (err)
7743 goto done;
7745 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7746 if (err)
7747 goto done;
7748 err = got_ref_write(branch_ref, repo);
7749 if (err)
7750 goto done;
7752 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7753 if (err)
7754 goto done;
7755 err = got_ref_write(commit_ref, repo);
7756 if (err)
7757 goto done;
7759 done:
7760 free(branch_refname);
7761 free(commit_refname);
7762 free(fileindex_path);
7763 if (branch_ref)
7764 got_ref_close(branch_ref);
7765 if (commit_ref)
7766 got_ref_close(commit_ref);
7767 if (wt_branch)
7768 got_ref_close(wt_branch);
7769 free(wt_branch_tip);
7770 if (err) {
7771 if (*fileindex) {
7772 got_fileindex_free(*fileindex);
7773 *fileindex = NULL;
7775 lock_worktree(worktree, LOCK_SH);
7777 return err;
7780 const struct got_error *
7781 got_worktree_merge_continue(char **branch_name,
7782 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7783 struct got_worktree *worktree, struct got_repository *repo)
7785 const struct got_error *err;
7786 char *commit_refname = NULL, *branch_refname = NULL;
7787 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7788 char *fileindex_path = NULL;
7789 int have_staged_files = 0;
7791 *branch_name = NULL;
7792 *branch_tip = NULL;
7793 *fileindex = NULL;
7795 err = lock_worktree(worktree, LOCK_EX);
7796 if (err)
7797 return err;
7799 err = open_fileindex(fileindex, &fileindex_path, worktree);
7800 if (err)
7801 goto done;
7803 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7804 &have_staged_files);
7805 if (err && err->code != GOT_ERR_CANCELLED)
7806 goto done;
7807 if (have_staged_files) {
7808 err = got_error(GOT_ERR_STAGED_PATHS);
7809 goto done;
7812 err = get_merge_branch_ref_name(&branch_refname, worktree);
7813 if (err)
7814 goto done;
7816 err = get_merge_commit_ref_name(&commit_refname, worktree);
7817 if (err)
7818 goto done;
7820 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7821 if (err)
7822 goto done;
7824 if (!got_ref_is_symbolic(branch_ref)) {
7825 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7826 "%s is not a symbolic reference",
7827 got_ref_get_name(branch_ref));
7828 goto done;
7830 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7831 if (*branch_name == NULL) {
7832 err = got_error_from_errno("strdup");
7833 goto done;
7836 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7837 if (err)
7838 goto done;
7840 err = got_ref_resolve(branch_tip, repo, commit_ref);
7841 if (err)
7842 goto done;
7843 done:
7844 free(commit_refname);
7845 free(branch_refname);
7846 free(fileindex_path);
7847 if (commit_ref)
7848 got_ref_close(commit_ref);
7849 if (branch_ref)
7850 got_ref_close(branch_ref);
7851 if (err) {
7852 if (*branch_name) {
7853 free(*branch_name);
7854 *branch_name = NULL;
7856 free(*branch_tip);
7857 *branch_tip = NULL;
7858 if (*fileindex) {
7859 got_fileindex_free(*fileindex);
7860 *fileindex = NULL;
7862 lock_worktree(worktree, LOCK_SH);
7864 return err;
7867 const struct got_error *
7868 got_worktree_merge_abort(struct got_worktree *worktree,
7869 struct got_fileindex *fileindex, struct got_repository *repo,
7870 got_worktree_checkout_cb progress_cb, void *progress_arg)
7872 const struct got_error *err, *unlockerr, *sync_err;
7873 struct got_object_id *commit_id = NULL;
7874 char *fileindex_path = NULL;
7875 struct revert_file_args rfa;
7876 struct got_object_id *tree_id = NULL;
7878 err = got_object_id_by_path(&tree_id, repo,
7879 worktree->base_commit_id, worktree->path_prefix);
7880 if (err)
7881 goto done;
7883 err = delete_merge_refs(worktree, repo);
7884 if (err)
7885 goto done;
7887 err = get_fileindex_path(&fileindex_path, worktree);
7888 if (err)
7889 goto done;
7891 rfa.worktree = worktree;
7892 rfa.fileindex = fileindex;
7893 rfa.progress_cb = progress_cb;
7894 rfa.progress_arg = progress_arg;
7895 rfa.patch_cb = NULL;
7896 rfa.patch_arg = NULL;
7897 rfa.repo = repo;
7898 rfa.unlink_added_files = 1;
7899 err = worktree_status(worktree, "", fileindex, repo,
7900 revert_file, &rfa, NULL, NULL, 1, 0);
7901 if (err)
7902 goto sync;
7904 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7905 repo, progress_cb, progress_arg, NULL, NULL);
7906 sync:
7907 sync_err = sync_fileindex(fileindex, fileindex_path);
7908 if (sync_err && err == NULL)
7909 err = sync_err;
7910 done:
7911 free(tree_id);
7912 free(commit_id);
7913 if (fileindex)
7914 got_fileindex_free(fileindex);
7915 free(fileindex_path);
7917 unlockerr = lock_worktree(worktree, LOCK_SH);
7918 if (unlockerr && err == NULL)
7919 err = unlockerr;
7920 return err;
7923 struct check_stage_ok_arg {
7924 struct got_object_id *head_commit_id;
7925 struct got_worktree *worktree;
7926 struct got_fileindex *fileindex;
7927 struct got_repository *repo;
7928 int have_changes;
7931 const struct got_error *
7932 check_stage_ok(void *arg, unsigned char status,
7933 unsigned char staged_status, const char *relpath,
7934 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7935 struct got_object_id *commit_id, int dirfd, const char *de_name)
7937 struct check_stage_ok_arg *a = arg;
7938 const struct got_error *err = NULL;
7939 struct got_fileindex_entry *ie;
7940 struct got_object_id base_commit_id;
7941 struct got_object_id *base_commit_idp = NULL;
7942 char *in_repo_path = NULL, *p;
7944 if (status == GOT_STATUS_UNVERSIONED ||
7945 status == GOT_STATUS_NO_CHANGE)
7946 return NULL;
7947 if (status == GOT_STATUS_NONEXISTENT)
7948 return got_error_set_errno(ENOENT, relpath);
7950 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7951 if (ie == NULL)
7952 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7954 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7955 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7956 relpath) == -1)
7957 return got_error_from_errno("asprintf");
7959 if (got_fileindex_entry_has_commit(ie)) {
7960 memcpy(base_commit_id.sha1, ie->commit_sha1,
7961 SHA1_DIGEST_LENGTH);
7962 base_commit_idp = &base_commit_id;
7965 if (status == GOT_STATUS_CONFLICT) {
7966 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7967 goto done;
7968 } else if (status != GOT_STATUS_ADD &&
7969 status != GOT_STATUS_MODIFY &&
7970 status != GOT_STATUS_DELETE) {
7971 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7972 goto done;
7975 a->have_changes = 1;
7977 p = in_repo_path;
7978 while (p[0] == '/')
7979 p++;
7980 err = check_out_of_date(p, status, staged_status,
7981 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7982 GOT_ERR_STAGE_OUT_OF_DATE);
7983 done:
7984 free(in_repo_path);
7985 return err;
7988 struct stage_path_arg {
7989 struct got_worktree *worktree;
7990 struct got_fileindex *fileindex;
7991 struct got_repository *repo;
7992 got_worktree_status_cb status_cb;
7993 void *status_arg;
7994 got_worktree_patch_cb patch_cb;
7995 void *patch_arg;
7996 int staged_something;
7997 int allow_bad_symlinks;
8000 static const struct got_error *
8001 stage_path(void *arg, unsigned char status,
8002 unsigned char staged_status, const char *relpath,
8003 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8004 struct got_object_id *commit_id, int dirfd, const char *de_name)
8006 struct stage_path_arg *a = arg;
8007 const struct got_error *err = NULL;
8008 struct got_fileindex_entry *ie;
8009 char *ondisk_path = NULL, *path_content = NULL;
8010 uint32_t stage;
8011 struct got_object_id *new_staged_blob_id = NULL;
8012 struct stat sb;
8014 if (status == GOT_STATUS_UNVERSIONED)
8015 return NULL;
8017 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8018 if (ie == NULL)
8019 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8021 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8022 relpath)== -1)
8023 return got_error_from_errno("asprintf");
8025 switch (status) {
8026 case GOT_STATUS_ADD:
8027 case GOT_STATUS_MODIFY:
8028 /* XXX could sb.st_mode be passed in by our caller? */
8029 if (lstat(ondisk_path, &sb) == -1) {
8030 err = got_error_from_errno2("lstat", ondisk_path);
8031 break;
8033 if (a->patch_cb) {
8034 if (status == GOT_STATUS_ADD) {
8035 int choice = GOT_PATCH_CHOICE_NONE;
8036 err = (*a->patch_cb)(&choice, a->patch_arg,
8037 status, ie->path, NULL, 1, 1);
8038 if (err)
8039 break;
8040 if (choice != GOT_PATCH_CHOICE_YES)
8041 break;
8042 } else {
8043 err = create_patched_content(&path_content, 0,
8044 staged_blob_id ? staged_blob_id : blob_id,
8045 ondisk_path, dirfd, de_name, ie->path,
8046 a->repo, a->patch_cb, a->patch_arg);
8047 if (err || path_content == NULL)
8048 break;
8051 err = got_object_blob_create(&new_staged_blob_id,
8052 path_content ? path_content : ondisk_path, a->repo);
8053 if (err)
8054 break;
8055 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8056 SHA1_DIGEST_LENGTH);
8057 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8058 stage = GOT_FILEIDX_STAGE_ADD;
8059 else
8060 stage = GOT_FILEIDX_STAGE_MODIFY;
8061 got_fileindex_entry_stage_set(ie, stage);
8062 if (S_ISLNK(sb.st_mode)) {
8063 int is_bad_symlink = 0;
8064 if (!a->allow_bad_symlinks) {
8065 char target_path[PATH_MAX];
8066 ssize_t target_len;
8067 target_len = readlink(ondisk_path, target_path,
8068 sizeof(target_path));
8069 if (target_len == -1) {
8070 err = got_error_from_errno2("readlink",
8071 ondisk_path);
8072 break;
8074 err = is_bad_symlink_target(&is_bad_symlink,
8075 target_path, target_len, ondisk_path,
8076 a->worktree->root_path);
8077 if (err)
8078 break;
8079 if (is_bad_symlink) {
8080 err = got_error_path(ondisk_path,
8081 GOT_ERR_BAD_SYMLINK);
8082 break;
8085 if (is_bad_symlink)
8086 got_fileindex_entry_staged_filetype_set(ie,
8087 GOT_FILEIDX_MODE_BAD_SYMLINK);
8088 else
8089 got_fileindex_entry_staged_filetype_set(ie,
8090 GOT_FILEIDX_MODE_SYMLINK);
8091 } else {
8092 got_fileindex_entry_staged_filetype_set(ie,
8093 GOT_FILEIDX_MODE_REGULAR_FILE);
8095 a->staged_something = 1;
8096 if (a->status_cb == NULL)
8097 break;
8098 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8099 get_staged_status(ie), relpath, blob_id,
8100 new_staged_blob_id, NULL, dirfd, de_name);
8101 break;
8102 case GOT_STATUS_DELETE:
8103 if (staged_status == GOT_STATUS_DELETE)
8104 break;
8105 if (a->patch_cb) {
8106 int choice = GOT_PATCH_CHOICE_NONE;
8107 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8108 ie->path, NULL, 1, 1);
8109 if (err)
8110 break;
8111 if (choice == GOT_PATCH_CHOICE_NO)
8112 break;
8113 if (choice != GOT_PATCH_CHOICE_YES) {
8114 err = got_error(GOT_ERR_PATCH_CHOICE);
8115 break;
8118 stage = GOT_FILEIDX_STAGE_DELETE;
8119 got_fileindex_entry_stage_set(ie, stage);
8120 a->staged_something = 1;
8121 if (a->status_cb == NULL)
8122 break;
8123 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8124 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8125 de_name);
8126 break;
8127 case GOT_STATUS_NO_CHANGE:
8128 break;
8129 case GOT_STATUS_CONFLICT:
8130 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8131 break;
8132 case GOT_STATUS_NONEXISTENT:
8133 err = got_error_set_errno(ENOENT, relpath);
8134 break;
8135 default:
8136 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8137 break;
8140 if (path_content && unlink(path_content) == -1 && err == NULL)
8141 err = got_error_from_errno2("unlink", path_content);
8142 free(path_content);
8143 free(ondisk_path);
8144 free(new_staged_blob_id);
8145 return err;
8148 const struct got_error *
8149 got_worktree_stage(struct got_worktree *worktree,
8150 struct got_pathlist_head *paths,
8151 got_worktree_status_cb status_cb, void *status_arg,
8152 got_worktree_patch_cb patch_cb, void *patch_arg,
8153 int allow_bad_symlinks, struct got_repository *repo)
8155 const struct got_error *err = NULL, *sync_err, *unlockerr;
8156 struct got_pathlist_entry *pe;
8157 struct got_fileindex *fileindex = NULL;
8158 char *fileindex_path = NULL;
8159 struct got_reference *head_ref = NULL;
8160 struct got_object_id *head_commit_id = NULL;
8161 struct check_stage_ok_arg oka;
8162 struct stage_path_arg spa;
8164 err = lock_worktree(worktree, LOCK_EX);
8165 if (err)
8166 return err;
8168 err = got_ref_open(&head_ref, repo,
8169 got_worktree_get_head_ref_name(worktree), 0);
8170 if (err)
8171 goto done;
8172 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8173 if (err)
8174 goto done;
8175 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8176 if (err)
8177 goto done;
8179 /* Check pre-conditions before staging anything. */
8180 oka.head_commit_id = head_commit_id;
8181 oka.worktree = worktree;
8182 oka.fileindex = fileindex;
8183 oka.repo = repo;
8184 oka.have_changes = 0;
8185 TAILQ_FOREACH(pe, paths, entry) {
8186 err = worktree_status(worktree, pe->path, fileindex, repo,
8187 check_stage_ok, &oka, NULL, NULL, 1, 0);
8188 if (err)
8189 goto done;
8191 if (!oka.have_changes) {
8192 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8193 goto done;
8196 spa.worktree = worktree;
8197 spa.fileindex = fileindex;
8198 spa.repo = repo;
8199 spa.patch_cb = patch_cb;
8200 spa.patch_arg = patch_arg;
8201 spa.status_cb = status_cb;
8202 spa.status_arg = status_arg;
8203 spa.staged_something = 0;
8204 spa.allow_bad_symlinks = allow_bad_symlinks;
8205 TAILQ_FOREACH(pe, paths, entry) {
8206 err = worktree_status(worktree, pe->path, fileindex, repo,
8207 stage_path, &spa, NULL, NULL, 1, 0);
8208 if (err)
8209 goto done;
8211 if (!spa.staged_something) {
8212 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8213 goto done;
8216 sync_err = sync_fileindex(fileindex, fileindex_path);
8217 if (sync_err && err == NULL)
8218 err = sync_err;
8219 done:
8220 if (head_ref)
8221 got_ref_close(head_ref);
8222 free(head_commit_id);
8223 free(fileindex_path);
8224 if (fileindex)
8225 got_fileindex_free(fileindex);
8226 unlockerr = lock_worktree(worktree, LOCK_SH);
8227 if (unlockerr && err == NULL)
8228 err = unlockerr;
8229 return err;
8232 struct unstage_path_arg {
8233 struct got_worktree *worktree;
8234 struct got_fileindex *fileindex;
8235 struct got_repository *repo;
8236 got_worktree_checkout_cb progress_cb;
8237 void *progress_arg;
8238 got_worktree_patch_cb patch_cb;
8239 void *patch_arg;
8242 static const struct got_error *
8243 create_unstaged_content(char **path_unstaged_content,
8244 char **path_new_staged_content, struct got_object_id *blob_id,
8245 struct got_object_id *staged_blob_id, const char *relpath,
8246 struct got_repository *repo,
8247 got_worktree_patch_cb patch_cb, void *patch_arg)
8249 const struct got_error *err, *free_err;
8250 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8251 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8252 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8253 struct got_diffreg_result *diffreg_result = NULL;
8254 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8255 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8257 *path_unstaged_content = NULL;
8258 *path_new_staged_content = NULL;
8260 err = got_object_id_str(&label1, blob_id);
8261 if (err)
8262 return err;
8263 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8264 if (err)
8265 goto done;
8267 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8268 if (err)
8269 goto done;
8271 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8272 if (err)
8273 goto done;
8275 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8276 if (err)
8277 goto done;
8279 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8280 if (err)
8281 goto done;
8283 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8284 if (err)
8285 goto done;
8287 err = got_diff_files(&diffreg_result, f1, label1, f2,
8288 path2, 3, 0, 1, NULL);
8289 if (err)
8290 goto done;
8292 err = got_opentemp_named(path_unstaged_content, &outfile,
8293 "got-unstaged-content");
8294 if (err)
8295 goto done;
8296 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8297 "got-new-staged-content");
8298 if (err)
8299 goto done;
8301 if (fseek(f1, 0L, SEEK_SET) == -1) {
8302 err = got_ferror(f1, GOT_ERR_IO);
8303 goto done;
8305 if (fseek(f2, 0L, SEEK_SET) == -1) {
8306 err = got_ferror(f2, GOT_ERR_IO);
8307 goto done;
8309 /* Count the number of actual changes in the diff result. */
8310 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8311 struct diff_chunk_context cc = {};
8312 diff_chunk_context_load_change(&cc, &nchunks_used,
8313 diffreg_result->result, n, 0);
8314 nchanges++;
8316 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8317 int choice;
8318 err = apply_or_reject_change(&choice, &nchunks_used,
8319 diffreg_result->result, n, relpath, f1, f2,
8320 &line_cur1, &line_cur2,
8321 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8322 if (err)
8323 goto done;
8324 if (choice == GOT_PATCH_CHOICE_YES)
8325 have_content = 1;
8326 else
8327 have_rejected_content = 1;
8328 if (choice == GOT_PATCH_CHOICE_QUIT)
8329 break;
8331 if (have_content || have_rejected_content)
8332 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8333 outfile, rejectfile);
8334 done:
8335 free(label1);
8336 if (blob)
8337 got_object_blob_close(blob);
8338 if (staged_blob)
8339 got_object_blob_close(staged_blob);
8340 free_err = got_diffreg_result_free(diffreg_result);
8341 if (free_err && err == NULL)
8342 err = free_err;
8343 if (f1 && fclose(f1) == EOF && err == NULL)
8344 err = got_error_from_errno2("fclose", path1);
8345 if (f2 && fclose(f2) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", path2);
8347 if (outfile && fclose(outfile) == EOF && err == NULL)
8348 err = got_error_from_errno2("fclose", *path_unstaged_content);
8349 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8350 err = got_error_from_errno2("fclose", *path_new_staged_content);
8351 if (path1 && unlink(path1) == -1 && err == NULL)
8352 err = got_error_from_errno2("unlink", path1);
8353 if (path2 && unlink(path2) == -1 && err == NULL)
8354 err = got_error_from_errno2("unlink", path2);
8355 if (err || !have_content) {
8356 if (*path_unstaged_content &&
8357 unlink(*path_unstaged_content) == -1 && err == NULL)
8358 err = got_error_from_errno2("unlink",
8359 *path_unstaged_content);
8360 free(*path_unstaged_content);
8361 *path_unstaged_content = NULL;
8363 if (err || !have_content || !have_rejected_content) {
8364 if (*path_new_staged_content &&
8365 unlink(*path_new_staged_content) == -1 && err == NULL)
8366 err = got_error_from_errno2("unlink",
8367 *path_new_staged_content);
8368 free(*path_new_staged_content);
8369 *path_new_staged_content = NULL;
8371 free(path1);
8372 free(path2);
8373 return err;
8376 static const struct got_error *
8377 unstage_hunks(struct got_object_id *staged_blob_id,
8378 struct got_blob_object *blob_base,
8379 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8380 const char *ondisk_path, const char *label_orig,
8381 struct got_worktree *worktree, struct got_repository *repo,
8382 got_worktree_patch_cb patch_cb, void *patch_arg,
8383 got_worktree_checkout_cb progress_cb, void *progress_arg)
8385 const struct got_error *err = NULL;
8386 char *path_unstaged_content = NULL;
8387 char *path_new_staged_content = NULL;
8388 char *parent = NULL, *base_path = NULL;
8389 char *blob_base_path = NULL;
8390 struct got_object_id *new_staged_blob_id = NULL;
8391 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8392 struct stat sb;
8394 err = create_unstaged_content(&path_unstaged_content,
8395 &path_new_staged_content, blob_id, staged_blob_id,
8396 ie->path, repo, patch_cb, patch_arg);
8397 if (err)
8398 return err;
8400 if (path_unstaged_content == NULL)
8401 return NULL;
8403 if (path_new_staged_content) {
8404 err = got_object_blob_create(&new_staged_blob_id,
8405 path_new_staged_content, repo);
8406 if (err)
8407 goto done;
8410 f = fopen(path_unstaged_content, "r");
8411 if (f == NULL) {
8412 err = got_error_from_errno2("fopen",
8413 path_unstaged_content);
8414 goto done;
8416 if (fstat(fileno(f), &sb) == -1) {
8417 err = got_error_from_errno2("fstat", path_unstaged_content);
8418 goto done;
8420 if (got_fileindex_entry_staged_filetype_get(ie) ==
8421 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8422 char link_target[PATH_MAX];
8423 size_t r;
8424 r = fread(link_target, 1, sizeof(link_target), f);
8425 if (r == 0 && ferror(f)) {
8426 err = got_error_from_errno("fread");
8427 goto done;
8429 if (r >= sizeof(link_target)) { /* should not happen */
8430 err = got_error(GOT_ERR_NO_SPACE);
8431 goto done;
8433 link_target[r] = '\0';
8434 err = merge_symlink(worktree, blob_base,
8435 ondisk_path, ie->path, label_orig, link_target,
8436 worktree->base_commit_id, repo, progress_cb,
8437 progress_arg);
8438 } else {
8439 int local_changes_subsumed;
8441 err = got_path_dirname(&parent, ondisk_path);
8442 if (err)
8443 return err;
8445 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8446 parent) == -1) {
8447 err = got_error_from_errno("asprintf");
8448 base_path = NULL;
8449 goto done;
8452 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8453 if (err)
8454 goto done;
8455 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8456 blob_base);
8457 if (err)
8458 goto done;
8461 * In order the run a 3-way merge with a symlink we copy the symlink's
8462 * target path into a temporary file and use that file with diff3.
8464 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8465 err = dump_symlink_target_path_to_file(&f_deriv2,
8466 ondisk_path);
8467 if (err)
8468 goto done;
8469 } else {
8470 int fd;
8471 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8472 if (fd == -1) {
8473 err = got_error_from_errno2("open", ondisk_path);
8474 goto done;
8476 f_deriv2 = fdopen(fd, "r");
8477 if (f_deriv2 == NULL) {
8478 err = got_error_from_errno2("fdopen", ondisk_path);
8479 close(fd);
8480 goto done;
8484 err = merge_file(&local_changes_subsumed, worktree,
8485 f_base, f, f_deriv2, ondisk_path, ie->path,
8486 got_fileindex_perms_to_st(ie),
8487 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8488 repo, progress_cb, progress_arg);
8490 if (err)
8491 goto done;
8493 if (new_staged_blob_id) {
8494 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8495 SHA1_DIGEST_LENGTH);
8496 } else {
8497 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8498 got_fileindex_entry_staged_filetype_set(ie, 0);
8500 done:
8501 free(new_staged_blob_id);
8502 if (path_unstaged_content &&
8503 unlink(path_unstaged_content) == -1 && err == NULL)
8504 err = got_error_from_errno2("unlink", path_unstaged_content);
8505 if (path_new_staged_content &&
8506 unlink(path_new_staged_content) == -1 && err == NULL)
8507 err = got_error_from_errno2("unlink", path_new_staged_content);
8508 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8509 err = got_error_from_errno2("unlink", blob_base_path);
8510 if (f_base && fclose(f_base) == EOF && err == NULL)
8511 err = got_error_from_errno2("fclose", path_unstaged_content);
8512 if (f && fclose(f) == EOF && err == NULL)
8513 err = got_error_from_errno2("fclose", path_unstaged_content);
8514 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8515 err = got_error_from_errno2("fclose", ondisk_path);
8516 free(path_unstaged_content);
8517 free(path_new_staged_content);
8518 free(blob_base_path);
8519 free(parent);
8520 free(base_path);
8521 return err;
8524 static const struct got_error *
8525 unstage_path(void *arg, unsigned char status,
8526 unsigned char staged_status, const char *relpath,
8527 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8528 struct got_object_id *commit_id, int dirfd, const char *de_name)
8530 const struct got_error *err = NULL;
8531 struct unstage_path_arg *a = arg;
8532 struct got_fileindex_entry *ie;
8533 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8534 char *ondisk_path = NULL;
8535 char *id_str = NULL, *label_orig = NULL;
8536 int local_changes_subsumed;
8537 struct stat sb;
8539 if (staged_status != GOT_STATUS_ADD &&
8540 staged_status != GOT_STATUS_MODIFY &&
8541 staged_status != GOT_STATUS_DELETE)
8542 return NULL;
8544 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8545 if (ie == NULL)
8546 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8548 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8549 == -1)
8550 return got_error_from_errno("asprintf");
8552 err = got_object_id_str(&id_str,
8553 commit_id ? commit_id : a->worktree->base_commit_id);
8554 if (err)
8555 goto done;
8556 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8557 id_str) == -1) {
8558 err = got_error_from_errno("asprintf");
8559 goto done;
8562 switch (staged_status) {
8563 case GOT_STATUS_MODIFY:
8564 err = got_object_open_as_blob(&blob_base, a->repo,
8565 blob_id, 8192);
8566 if (err)
8567 break;
8568 /* fall through */
8569 case GOT_STATUS_ADD:
8570 if (a->patch_cb) {
8571 if (staged_status == GOT_STATUS_ADD) {
8572 int choice = GOT_PATCH_CHOICE_NONE;
8573 err = (*a->patch_cb)(&choice, a->patch_arg,
8574 staged_status, ie->path, NULL, 1, 1);
8575 if (err)
8576 break;
8577 if (choice != GOT_PATCH_CHOICE_YES)
8578 break;
8579 } else {
8580 err = unstage_hunks(staged_blob_id,
8581 blob_base, blob_id, ie, ondisk_path,
8582 label_orig, a->worktree, a->repo,
8583 a->patch_cb, a->patch_arg,
8584 a->progress_cb, a->progress_arg);
8585 break; /* Done with this file. */
8588 err = got_object_open_as_blob(&blob_staged, a->repo,
8589 staged_blob_id, 8192);
8590 if (err)
8591 break;
8592 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8593 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8594 case GOT_FILEIDX_MODE_REGULAR_FILE:
8595 err = merge_blob(&local_changes_subsumed, a->worktree,
8596 blob_base, ondisk_path, relpath,
8597 got_fileindex_perms_to_st(ie), label_orig,
8598 blob_staged, commit_id ? commit_id :
8599 a->worktree->base_commit_id, a->repo,
8600 a->progress_cb, a->progress_arg);
8601 break;
8602 case GOT_FILEIDX_MODE_SYMLINK:
8603 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8604 char *staged_target;
8605 err = got_object_blob_read_to_str(
8606 &staged_target, blob_staged);
8607 if (err)
8608 goto done;
8609 err = merge_symlink(a->worktree, blob_base,
8610 ondisk_path, relpath, label_orig,
8611 staged_target, commit_id ? commit_id :
8612 a->worktree->base_commit_id,
8613 a->repo, a->progress_cb, a->progress_arg);
8614 free(staged_target);
8615 } else {
8616 err = merge_blob(&local_changes_subsumed,
8617 a->worktree, blob_base, ondisk_path,
8618 relpath, got_fileindex_perms_to_st(ie),
8619 label_orig, blob_staged,
8620 commit_id ? commit_id :
8621 a->worktree->base_commit_id, a->repo,
8622 a->progress_cb, a->progress_arg);
8624 break;
8625 default:
8626 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8627 break;
8629 if (err == NULL) {
8630 got_fileindex_entry_stage_set(ie,
8631 GOT_FILEIDX_STAGE_NONE);
8632 got_fileindex_entry_staged_filetype_set(ie, 0);
8634 break;
8635 case GOT_STATUS_DELETE:
8636 if (a->patch_cb) {
8637 int choice = GOT_PATCH_CHOICE_NONE;
8638 err = (*a->patch_cb)(&choice, a->patch_arg,
8639 staged_status, ie->path, NULL, 1, 1);
8640 if (err)
8641 break;
8642 if (choice == GOT_PATCH_CHOICE_NO)
8643 break;
8644 if (choice != GOT_PATCH_CHOICE_YES) {
8645 err = got_error(GOT_ERR_PATCH_CHOICE);
8646 break;
8649 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8650 got_fileindex_entry_staged_filetype_set(ie, 0);
8651 err = get_file_status(&status, &sb, ie, ondisk_path,
8652 dirfd, de_name, a->repo);
8653 if (err)
8654 break;
8655 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8656 break;
8658 done:
8659 free(ondisk_path);
8660 if (blob_base)
8661 got_object_blob_close(blob_base);
8662 if (blob_staged)
8663 got_object_blob_close(blob_staged);
8664 free(id_str);
8665 free(label_orig);
8666 return err;
8669 const struct got_error *
8670 got_worktree_unstage(struct got_worktree *worktree,
8671 struct got_pathlist_head *paths,
8672 got_worktree_checkout_cb progress_cb, void *progress_arg,
8673 got_worktree_patch_cb patch_cb, void *patch_arg,
8674 struct got_repository *repo)
8676 const struct got_error *err = NULL, *sync_err, *unlockerr;
8677 struct got_pathlist_entry *pe;
8678 struct got_fileindex *fileindex = NULL;
8679 char *fileindex_path = NULL;
8680 struct unstage_path_arg upa;
8682 err = lock_worktree(worktree, LOCK_EX);
8683 if (err)
8684 return err;
8686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8687 if (err)
8688 goto done;
8690 upa.worktree = worktree;
8691 upa.fileindex = fileindex;
8692 upa.repo = repo;
8693 upa.progress_cb = progress_cb;
8694 upa.progress_arg = progress_arg;
8695 upa.patch_cb = patch_cb;
8696 upa.patch_arg = patch_arg;
8697 TAILQ_FOREACH(pe, paths, entry) {
8698 err = worktree_status(worktree, pe->path, fileindex, repo,
8699 unstage_path, &upa, NULL, NULL, 1, 0);
8700 if (err)
8701 goto done;
8704 sync_err = sync_fileindex(fileindex, fileindex_path);
8705 if (sync_err && err == NULL)
8706 err = sync_err;
8707 done:
8708 free(fileindex_path);
8709 if (fileindex)
8710 got_fileindex_free(fileindex);
8711 unlockerr = lock_worktree(worktree, LOCK_SH);
8712 if (unlockerr && err == NULL)
8713 err = unlockerr;
8714 return err;
8717 struct report_file_info_arg {
8718 struct got_worktree *worktree;
8719 got_worktree_path_info_cb info_cb;
8720 void *info_arg;
8721 struct got_pathlist_head *paths;
8722 got_cancel_cb cancel_cb;
8723 void *cancel_arg;
8726 static const struct got_error *
8727 report_file_info(void *arg, struct got_fileindex_entry *ie)
8729 struct report_file_info_arg *a = arg;
8730 struct got_pathlist_entry *pe;
8731 struct got_object_id blob_id, staged_blob_id, commit_id;
8732 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8733 struct got_object_id *commit_idp = NULL;
8734 int stage;
8736 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8737 return got_error(GOT_ERR_CANCELLED);
8739 TAILQ_FOREACH(pe, a->paths, entry) {
8740 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8741 got_path_is_child(ie->path, pe->path, pe->path_len))
8742 break;
8744 if (pe == NULL) /* not found */
8745 return NULL;
8747 if (got_fileindex_entry_has_blob(ie)) {
8748 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8749 blob_idp = &blob_id;
8751 stage = got_fileindex_entry_stage_get(ie);
8752 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8753 stage == GOT_FILEIDX_STAGE_ADD) {
8754 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8755 SHA1_DIGEST_LENGTH);
8756 staged_blob_idp = &staged_blob_id;
8759 if (got_fileindex_entry_has_commit(ie)) {
8760 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8761 commit_idp = &commit_id;
8764 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8765 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8768 const struct got_error *
8769 got_worktree_path_info(struct got_worktree *worktree,
8770 struct got_pathlist_head *paths,
8771 got_worktree_path_info_cb info_cb, void *info_arg,
8772 got_cancel_cb cancel_cb, void *cancel_arg)
8775 const struct got_error *err = NULL, *unlockerr;
8776 struct got_fileindex *fileindex = NULL;
8777 char *fileindex_path = NULL;
8778 struct report_file_info_arg arg;
8780 err = lock_worktree(worktree, LOCK_SH);
8781 if (err)
8782 return err;
8784 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8785 if (err)
8786 goto done;
8788 arg.worktree = worktree;
8789 arg.info_cb = info_cb;
8790 arg.info_arg = info_arg;
8791 arg.paths = paths;
8792 arg.cancel_cb = cancel_cb;
8793 arg.cancel_arg = cancel_arg;
8794 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8795 &arg);
8796 done:
8797 free(fileindex_path);
8798 if (fileindex)
8799 got_fileindex_free(fileindex);
8800 unlockerr = lock_worktree(worktree, LOCK_UN);
8801 if (unlockerr && err == NULL)
8802 err = unlockerr;
8803 return err;