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(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3620 const struct got_error *err = NULL;
3621 struct diff_dir_cb_arg *a = arg;
3622 char *path = NULL;
3624 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3625 return got_error(GOT_ERR_CANCELLED);
3627 if (parent_path[0]) {
3628 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3629 return got_error_from_errno("asprintf");
3630 } else {
3631 path = de->d_name;
3634 if (de->d_type != DT_DIR &&
3635 got_path_is_child(path, a->status_path, a->status_path_len)
3636 && !match_ignores(a->ignores, path))
3637 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3638 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3639 if (parent_path[0])
3640 free(path);
3641 return err;
3644 static const struct got_error *
3645 status_traverse(void *arg, const char *path, int dirfd)
3647 const struct got_error *err = NULL;
3648 struct diff_dir_cb_arg *a = arg;
3650 if (a->no_ignores)
3651 return NULL;
3653 err = add_ignores(a->ignores, a->worktree->root_path,
3654 path, dirfd, ".cvsignore");
3655 if (err)
3656 return err;
3658 err = add_ignores(a->ignores, a->worktree->root_path, path,
3659 dirfd, ".gitignore");
3661 return err;
3664 static const struct got_error *
3665 report_single_file_status(const char *path, const char *ondisk_path,
3666 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3667 void *status_arg, struct got_repository *repo, int report_unchanged,
3668 struct got_pathlist_head *ignores, int no_ignores)
3670 struct got_fileindex_entry *ie;
3671 struct stat sb;
3673 if (!no_ignores && match_ignores(ignores, path))
3674 return NULL;
3676 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3677 if (ie)
3678 return report_file_status(ie, ondisk_path, -1, NULL,
3679 status_cb, status_arg, repo, report_unchanged);
3681 if (lstat(ondisk_path, &sb) == -1) {
3682 if (errno != ENOENT)
3683 return got_error_from_errno2("lstat", ondisk_path);
3684 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3685 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3686 return NULL;
3689 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3690 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3691 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3693 return NULL;
3696 static const struct got_error *
3697 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3698 const char *root_path, const char *path)
3700 const struct got_error *err;
3701 char *parent_path, *next_parent_path = NULL;
3703 err = add_ignores(ignores, root_path, "", -1,
3704 ".cvsignore");
3705 if (err)
3706 return err;
3708 err = add_ignores(ignores, root_path, "", -1,
3709 ".gitignore");
3710 if (err)
3711 return err;
3713 err = got_path_dirname(&parent_path, path);
3714 if (err) {
3715 if (err->code == GOT_ERR_BAD_PATH)
3716 return NULL; /* cannot traverse parent */
3717 return err;
3719 for (;;) {
3720 err = add_ignores(ignores, root_path, parent_path, -1,
3721 ".cvsignore");
3722 if (err)
3723 break;
3724 err = add_ignores(ignores, root_path, parent_path, -1,
3725 ".gitignore");
3726 if (err)
3727 break;
3728 err = got_path_dirname(&next_parent_path, parent_path);
3729 if (err) {
3730 if (err->code == GOT_ERR_BAD_PATH)
3731 err = NULL; /* traversed everything */
3732 break;
3734 if (got_path_is_root_dir(parent_path))
3735 break;
3736 free(parent_path);
3737 parent_path = next_parent_path;
3738 next_parent_path = NULL;
3741 free(parent_path);
3742 free(next_parent_path);
3743 return err;
3746 static const struct got_error *
3747 worktree_status(struct got_worktree *worktree, const char *path,
3748 struct got_fileindex *fileindex, struct got_repository *repo,
3749 got_worktree_status_cb status_cb, void *status_arg,
3750 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3751 int report_unchanged)
3753 const struct got_error *err = NULL;
3754 int fd = -1;
3755 struct got_fileindex_diff_dir_cb fdiff_cb;
3756 struct diff_dir_cb_arg arg;
3757 char *ondisk_path = NULL;
3758 struct got_pathlist_head ignores;
3760 TAILQ_INIT(&ignores);
3762 if (asprintf(&ondisk_path, "%s%s%s",
3763 worktree->root_path, path[0] ? "/" : "", path) == -1)
3764 return got_error_from_errno("asprintf");
3766 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3767 if (fd == -1) {
3768 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3769 !got_err_open_nofollow_on_symlink())
3770 err = got_error_from_errno2("open", ondisk_path);
3771 else {
3772 if (!no_ignores) {
3773 err = add_ignores_from_parent_paths(&ignores,
3774 worktree->root_path, ondisk_path);
3775 if (err)
3776 goto done;
3778 err = report_single_file_status(path, ondisk_path,
3779 fileindex, status_cb, status_arg, repo,
3780 report_unchanged, &ignores, no_ignores);
3782 } else {
3783 fdiff_cb.diff_old_new = status_old_new;
3784 fdiff_cb.diff_old = status_old;
3785 fdiff_cb.diff_new = status_new;
3786 fdiff_cb.diff_traverse = status_traverse;
3787 arg.fileindex = fileindex;
3788 arg.worktree = worktree;
3789 arg.status_path = path;
3790 arg.status_path_len = strlen(path);
3791 arg.repo = repo;
3792 arg.status_cb = status_cb;
3793 arg.status_arg = status_arg;
3794 arg.cancel_cb = cancel_cb;
3795 arg.cancel_arg = cancel_arg;
3796 arg.report_unchanged = report_unchanged;
3797 arg.no_ignores = no_ignores;
3798 if (!no_ignores) {
3799 err = add_ignores_from_parent_paths(&ignores,
3800 worktree->root_path, path);
3801 if (err)
3802 goto done;
3804 arg.ignores = &ignores;
3805 err = got_fileindex_diff_dir(fileindex, fd,
3806 worktree->root_path, path, repo, &fdiff_cb, &arg);
3808 done:
3809 free_ignores(&ignores);
3810 if (fd != -1 && close(fd) == -1 && err == NULL)
3811 err = got_error_from_errno("close");
3812 free(ondisk_path);
3813 return err;
3816 const struct got_error *
3817 got_worktree_status(struct got_worktree *worktree,
3818 struct got_pathlist_head *paths, struct got_repository *repo,
3819 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3820 got_cancel_cb cancel_cb, void *cancel_arg)
3822 const struct got_error *err = NULL;
3823 char *fileindex_path = NULL;
3824 struct got_fileindex *fileindex = NULL;
3825 struct got_pathlist_entry *pe;
3827 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3828 if (err)
3829 return err;
3831 TAILQ_FOREACH(pe, paths, entry) {
3832 err = worktree_status(worktree, pe->path, fileindex, repo,
3833 status_cb, status_arg, cancel_cb, cancel_arg,
3834 no_ignores, 0);
3835 if (err)
3836 break;
3838 free(fileindex_path);
3839 got_fileindex_free(fileindex);
3840 return err;
3843 const struct got_error *
3844 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3845 const char *arg)
3847 const struct got_error *err = NULL;
3848 char *resolved = NULL, *cwd = NULL, *path = NULL;
3849 size_t len;
3850 struct stat sb;
3851 char *abspath = NULL;
3852 char canonpath[PATH_MAX];
3854 *wt_path = NULL;
3856 cwd = getcwd(NULL, 0);
3857 if (cwd == NULL)
3858 return got_error_from_errno("getcwd");
3860 if (lstat(arg, &sb) == -1) {
3861 if (errno != ENOENT) {
3862 err = got_error_from_errno2("lstat", arg);
3863 goto done;
3865 sb.st_mode = 0;
3867 if (S_ISLNK(sb.st_mode)) {
3869 * We cannot use realpath(3) with symlinks since we want to
3870 * operate on the symlink itself.
3871 * But we can make the path absolute, assuming it is relative
3872 * to the current working directory, and then canonicalize it.
3874 if (!got_path_is_absolute(arg)) {
3875 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3876 err = got_error_from_errno("asprintf");
3877 goto done;
3881 err = got_canonpath(abspath ? abspath : arg, canonpath,
3882 sizeof(canonpath));
3883 if (err)
3884 goto done;
3885 resolved = strdup(canonpath);
3886 if (resolved == NULL) {
3887 err = got_error_from_errno("strdup");
3888 goto done;
3890 } else {
3891 resolved = realpath(arg, NULL);
3892 if (resolved == NULL) {
3893 if (errno != ENOENT) {
3894 err = got_error_from_errno2("realpath", arg);
3895 goto done;
3897 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3898 err = got_error_from_errno("asprintf");
3899 goto done;
3901 err = got_canonpath(abspath, canonpath,
3902 sizeof(canonpath));
3903 if (err)
3904 goto done;
3905 resolved = strdup(canonpath);
3906 if (resolved == NULL) {
3907 err = got_error_from_errno("strdup");
3908 goto done;
3913 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3914 strlen(got_worktree_get_root_path(worktree)))) {
3915 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3916 goto done;
3919 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3920 err = got_path_skip_common_ancestor(&path,
3921 got_worktree_get_root_path(worktree), resolved);
3922 if (err)
3923 goto done;
3924 } else {
3925 path = strdup("");
3926 if (path == NULL) {
3927 err = got_error_from_errno("strdup");
3928 goto done;
3932 /* XXX status walk can't deal with trailing slash! */
3933 len = strlen(path);
3934 while (len > 0 && path[len - 1] == '/') {
3935 path[len - 1] = '\0';
3936 len--;
3938 done:
3939 free(abspath);
3940 free(resolved);
3941 free(cwd);
3942 if (err == NULL)
3943 *wt_path = path;
3944 else
3945 free(path);
3946 return err;
3949 struct schedule_addition_args {
3950 struct got_worktree *worktree;
3951 struct got_fileindex *fileindex;
3952 got_worktree_checkout_cb progress_cb;
3953 void *progress_arg;
3954 struct got_repository *repo;
3957 static const struct got_error *
3958 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3959 const char *relpath, struct got_object_id *blob_id,
3960 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3961 int dirfd, const char *de_name)
3963 struct schedule_addition_args *a = arg;
3964 const struct got_error *err = NULL;
3965 struct got_fileindex_entry *ie;
3966 struct stat sb;
3967 char *ondisk_path;
3969 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3970 relpath) == -1)
3971 return got_error_from_errno("asprintf");
3973 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3974 if (ie) {
3975 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3976 de_name, a->repo);
3977 if (err)
3978 goto done;
3979 /* Re-adding an existing entry is a no-op. */
3980 if (status == GOT_STATUS_ADD)
3981 goto done;
3982 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3983 if (err)
3984 goto done;
3987 if (status != GOT_STATUS_UNVERSIONED) {
3988 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3989 goto done;
3992 err = got_fileindex_entry_alloc(&ie, relpath);
3993 if (err)
3994 goto done;
3995 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3996 relpath, NULL, NULL, 1);
3997 if (err) {
3998 got_fileindex_entry_free(ie);
3999 goto done;
4001 err = got_fileindex_entry_add(a->fileindex, ie);
4002 if (err) {
4003 got_fileindex_entry_free(ie);
4004 goto done;
4006 done:
4007 free(ondisk_path);
4008 if (err)
4009 return err;
4010 if (status == GOT_STATUS_ADD)
4011 return NULL;
4012 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4015 const struct got_error *
4016 got_worktree_schedule_add(struct got_worktree *worktree,
4017 struct got_pathlist_head *paths,
4018 got_worktree_checkout_cb progress_cb, void *progress_arg,
4019 struct got_repository *repo, int no_ignores)
4021 struct got_fileindex *fileindex = NULL;
4022 char *fileindex_path = NULL;
4023 const struct got_error *err = NULL, *sync_err, *unlockerr;
4024 struct got_pathlist_entry *pe;
4025 struct schedule_addition_args saa;
4027 err = lock_worktree(worktree, LOCK_EX);
4028 if (err)
4029 return err;
4031 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4032 if (err)
4033 goto done;
4035 saa.worktree = worktree;
4036 saa.fileindex = fileindex;
4037 saa.progress_cb = progress_cb;
4038 saa.progress_arg = progress_arg;
4039 saa.repo = repo;
4041 TAILQ_FOREACH(pe, paths, entry) {
4042 err = worktree_status(worktree, pe->path, fileindex, repo,
4043 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4044 if (err)
4045 break;
4047 sync_err = sync_fileindex(fileindex, fileindex_path);
4048 if (sync_err && err == NULL)
4049 err = sync_err;
4050 done:
4051 free(fileindex_path);
4052 if (fileindex)
4053 got_fileindex_free(fileindex);
4054 unlockerr = lock_worktree(worktree, LOCK_SH);
4055 if (unlockerr && err == NULL)
4056 err = unlockerr;
4057 return err;
4060 struct schedule_deletion_args {
4061 struct got_worktree *worktree;
4062 struct got_fileindex *fileindex;
4063 got_worktree_delete_cb progress_cb;
4064 void *progress_arg;
4065 struct got_repository *repo;
4066 int delete_local_mods;
4067 int keep_on_disk;
4068 const char *status_codes;
4071 static const struct got_error *
4072 schedule_for_deletion(void *arg, unsigned char status,
4073 unsigned char staged_status, const char *relpath,
4074 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4075 struct got_object_id *commit_id, int dirfd, const char *de_name)
4077 struct schedule_deletion_args *a = arg;
4078 const struct got_error *err = NULL;
4079 struct got_fileindex_entry *ie = NULL;
4080 struct stat sb;
4081 char *ondisk_path;
4083 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4084 if (ie == NULL)
4085 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4087 staged_status = get_staged_status(ie);
4088 if (staged_status != GOT_STATUS_NO_CHANGE) {
4089 if (staged_status == GOT_STATUS_DELETE)
4090 return NULL;
4091 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4095 relpath) == -1)
4096 return got_error_from_errno("asprintf");
4098 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4099 a->repo);
4100 if (err)
4101 goto done;
4103 if (a->status_codes) {
4104 size_t ncodes = strlen(a->status_codes);
4105 int i;
4106 for (i = 0; i < ncodes ; i++) {
4107 if (status == a->status_codes[i])
4108 break;
4110 if (i == ncodes) {
4111 /* Do not delete files in non-matching status. */
4112 free(ondisk_path);
4113 return NULL;
4115 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4116 a->status_codes[i] != GOT_STATUS_MISSING) {
4117 static char msg[64];
4118 snprintf(msg, sizeof(msg),
4119 "invalid status code '%c'", a->status_codes[i]);
4120 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4121 goto done;
4125 if (status != GOT_STATUS_NO_CHANGE) {
4126 if (status == GOT_STATUS_DELETE)
4127 goto done;
4128 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4130 goto done;
4132 if (status != GOT_STATUS_MODIFY &&
4133 status != GOT_STATUS_MISSING) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4135 goto done;
4139 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4140 size_t root_len;
4142 if (dirfd != -1) {
4143 if (unlinkat(dirfd, de_name, 0) != 0) {
4144 err = got_error_from_errno2("unlinkat",
4145 ondisk_path);
4146 goto done;
4148 } else if (unlink(ondisk_path) != 0) {
4149 err = got_error_from_errno2("unlink", ondisk_path);
4150 goto done;
4153 root_len = strlen(a->worktree->root_path);
4154 do {
4155 char *parent;
4156 err = got_path_dirname(&parent, ondisk_path);
4157 if (err)
4158 goto done;
4159 free(ondisk_path);
4160 ondisk_path = parent;
4161 if (rmdir(ondisk_path) == -1) {
4162 if (errno != ENOTEMPTY)
4163 err = got_error_from_errno2("rmdir",
4164 ondisk_path);
4165 break;
4167 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4168 strlen(ondisk_path), root_len) != 0);
4171 got_fileindex_entry_mark_deleted_from_disk(ie);
4172 done:
4173 free(ondisk_path);
4174 if (err)
4175 return err;
4176 if (status == GOT_STATUS_DELETE)
4177 return NULL;
4178 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4179 staged_status, relpath);
4182 const struct got_error *
4183 got_worktree_schedule_delete(struct got_worktree *worktree,
4184 struct got_pathlist_head *paths, int delete_local_mods,
4185 const char *status_codes,
4186 got_worktree_delete_cb progress_cb, void *progress_arg,
4187 struct got_repository *repo, int keep_on_disk)
4189 struct got_fileindex *fileindex = NULL;
4190 char *fileindex_path = NULL;
4191 const struct got_error *err = NULL, *sync_err, *unlockerr;
4192 struct got_pathlist_entry *pe;
4193 struct schedule_deletion_args sda;
4195 err = lock_worktree(worktree, LOCK_EX);
4196 if (err)
4197 return err;
4199 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4200 if (err)
4201 goto done;
4203 sda.worktree = worktree;
4204 sda.fileindex = fileindex;
4205 sda.progress_cb = progress_cb;
4206 sda.progress_arg = progress_arg;
4207 sda.repo = repo;
4208 sda.delete_local_mods = delete_local_mods;
4209 sda.keep_on_disk = keep_on_disk;
4210 sda.status_codes = status_codes;
4212 TAILQ_FOREACH(pe, paths, entry) {
4213 err = worktree_status(worktree, pe->path, fileindex, repo,
4214 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4215 if (err)
4216 break;
4218 sync_err = sync_fileindex(fileindex, fileindex_path);
4219 if (sync_err && err == NULL)
4220 err = sync_err;
4221 done:
4222 free(fileindex_path);
4223 if (fileindex)
4224 got_fileindex_free(fileindex);
4225 unlockerr = lock_worktree(worktree, LOCK_SH);
4226 if (unlockerr && err == NULL)
4227 err = unlockerr;
4228 return err;
4231 static const struct got_error *
4232 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4234 const struct got_error *err = NULL;
4235 char *line = NULL;
4236 size_t linesize = 0, n;
4237 ssize_t linelen;
4239 linelen = getline(&line, &linesize, infile);
4240 if (linelen == -1) {
4241 if (ferror(infile)) {
4242 err = got_error_from_errno("getline");
4243 goto done;
4245 return NULL;
4247 if (outfile) {
4248 n = fwrite(line, 1, linelen, outfile);
4249 if (n != linelen) {
4250 err = got_ferror(outfile, GOT_ERR_IO);
4251 goto done;
4254 if (rejectfile) {
4255 n = fwrite(line, 1, linelen, rejectfile);
4256 if (n != linelen)
4257 err = got_ferror(outfile, GOT_ERR_IO);
4259 done:
4260 free(line);
4261 return err;
4264 static const struct got_error *
4265 skip_one_line(FILE *f)
4267 char *line = NULL;
4268 size_t linesize = 0;
4269 ssize_t linelen;
4271 linelen = getline(&line, &linesize, f);
4272 if (linelen == -1) {
4273 if (ferror(f))
4274 return got_error_from_errno("getline");
4275 return NULL;
4277 free(line);
4278 return NULL;
4281 static const struct got_error *
4282 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4283 int start_old, int end_old, int start_new, int end_new,
4284 FILE *outfile, FILE *rejectfile)
4286 const struct got_error *err;
4288 /* Copy old file's lines leading up to patch. */
4289 while (!feof(f1) && *line_cur1 < start_old) {
4290 err = copy_one_line(f1, outfile, NULL);
4291 if (err)
4292 return err;
4293 (*line_cur1)++;
4295 /* Skip new file's lines leading up to patch. */
4296 while (!feof(f2) && *line_cur2 < start_new) {
4297 if (rejectfile)
4298 err = copy_one_line(f2, NULL, rejectfile);
4299 else
4300 err = skip_one_line(f2);
4301 if (err)
4302 return err;
4303 (*line_cur2)++;
4305 /* Copy patched lines. */
4306 while (!feof(f2) && *line_cur2 <= end_new) {
4307 err = copy_one_line(f2, outfile, NULL);
4308 if (err)
4309 return err;
4310 (*line_cur2)++;
4312 /* Skip over old file's replaced lines. */
4313 while (!feof(f1) && *line_cur1 <= end_old) {
4314 if (rejectfile)
4315 err = copy_one_line(f1, NULL, rejectfile);
4316 else
4317 err = skip_one_line(f1);
4318 if (err)
4319 return err;
4320 (*line_cur1)++;
4323 return NULL;
4326 static const struct got_error *
4327 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4328 FILE *outfile, FILE *rejectfile)
4330 const struct got_error *err;
4332 if (outfile) {
4333 /* Copy old file's lines until EOF. */
4334 while (!feof(f1)) {
4335 err = copy_one_line(f1, outfile, NULL);
4336 if (err)
4337 return err;
4338 (*line_cur1)++;
4341 if (rejectfile) {
4342 /* Copy new file's lines until EOF. */
4343 while (!feof(f2)) {
4344 err = copy_one_line(f2, NULL, rejectfile);
4345 if (err)
4346 return err;
4347 (*line_cur2)++;
4351 return NULL;
4354 static const struct got_error *
4355 apply_or_reject_change(int *choice, int *nchunks_used,
4356 struct diff_result *diff_result, int n,
4357 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4358 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4359 got_worktree_patch_cb patch_cb, void *patch_arg)
4361 const struct got_error *err = NULL;
4362 struct diff_chunk_context cc = {};
4363 int start_old, end_old, start_new, end_new;
4364 FILE *hunkfile;
4365 struct diff_output_unidiff_state *diff_state;
4366 struct diff_input_info diff_info;
4367 int rc;
4369 *choice = GOT_PATCH_CHOICE_NONE;
4371 /* Get changed line numbers without context lines for copy_change(). */
4372 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4373 start_old = cc.left.start;
4374 end_old = cc.left.end;
4375 start_new = cc.right.start;
4376 end_new = cc.right.end;
4378 /* Get the same change with context lines for display. */
4379 memset(&cc, 0, sizeof(cc));
4380 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4382 memset(&diff_info, 0, sizeof(diff_info));
4383 diff_info.left_path = relpath;
4384 diff_info.right_path = relpath;
4386 diff_state = diff_output_unidiff_state_alloc();
4387 if (diff_state == NULL)
4388 return got_error_set_errno(ENOMEM,
4389 "diff_output_unidiff_state_alloc");
4391 hunkfile = got_opentemp();
4392 if (hunkfile == NULL) {
4393 err = got_error_from_errno("got_opentemp");
4394 goto done;
4397 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4398 diff_result, &cc);
4399 if (rc != DIFF_RC_OK) {
4400 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4401 goto done;
4404 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4405 err = got_ferror(hunkfile, GOT_ERR_IO);
4406 goto done;
4409 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4410 hunkfile, changeno, nchanges);
4411 if (err)
4412 goto done;
4414 switch (*choice) {
4415 case GOT_PATCH_CHOICE_YES:
4416 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 end_old, start_new, end_new, outfile, rejectfile);
4418 break;
4419 case GOT_PATCH_CHOICE_NO:
4420 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4421 end_old, start_new, end_new, rejectfile, outfile);
4422 break;
4423 case GOT_PATCH_CHOICE_QUIT:
4424 break;
4425 default:
4426 err = got_error(GOT_ERR_PATCH_CHOICE);
4427 break;
4429 done:
4430 diff_output_unidiff_state_free(diff_state);
4431 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4432 err = got_error_from_errno("fclose");
4433 return err;
4436 struct revert_file_args {
4437 struct got_worktree *worktree;
4438 struct got_fileindex *fileindex;
4439 got_worktree_checkout_cb progress_cb;
4440 void *progress_arg;
4441 got_worktree_patch_cb patch_cb;
4442 void *patch_arg;
4443 struct got_repository *repo;
4444 int unlink_added_files;
4447 static const struct got_error *
4448 create_patched_content(char **path_outfile, int reverse_patch,
4449 struct got_object_id *blob_id, const char *path2,
4450 int dirfd2, const char *de_name2,
4451 const char *relpath, struct got_repository *repo,
4452 got_worktree_patch_cb patch_cb, void *patch_arg)
4454 const struct got_error *err, *free_err;
4455 struct got_blob_object *blob = NULL;
4456 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4457 int fd2 = -1;
4458 char link_target[PATH_MAX];
4459 ssize_t link_len = 0;
4460 char *path1 = NULL, *id_str = NULL;
4461 struct stat sb2;
4462 struct got_diffreg_result *diffreg_result = NULL;
4463 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4464 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4466 *path_outfile = NULL;
4468 err = got_object_id_str(&id_str, blob_id);
4469 if (err)
4470 return err;
4472 if (dirfd2 != -1) {
4473 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4474 if (fd2 == -1) {
4475 if (!got_err_open_nofollow_on_symlink()) {
4476 err = got_error_from_errno2("openat", path2);
4477 goto done;
4479 link_len = readlinkat(dirfd2, de_name2,
4480 link_target, sizeof(link_target));
4481 if (link_len == -1)
4482 return got_error_from_errno2("readlinkat", path2);
4483 sb2.st_mode = S_IFLNK;
4484 sb2.st_size = link_len;
4486 } else {
4487 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4488 if (fd2 == -1) {
4489 if (!got_err_open_nofollow_on_symlink()) {
4490 err = got_error_from_errno2("open", path2);
4491 goto done;
4493 link_len = readlink(path2, link_target,
4494 sizeof(link_target));
4495 if (link_len == -1)
4496 return got_error_from_errno2("readlink", path2);
4497 sb2.st_mode = S_IFLNK;
4498 sb2.st_size = link_len;
4501 if (fd2 != -1) {
4502 if (fstat(fd2, &sb2) == -1) {
4503 err = got_error_from_errno2("fstat", path2);
4504 goto done;
4507 f2 = fdopen(fd2, "r");
4508 if (f2 == NULL) {
4509 err = got_error_from_errno2("fdopen", path2);
4510 goto done;
4512 fd2 = -1;
4513 } else {
4514 size_t n;
4515 f2 = got_opentemp();
4516 if (f2 == NULL) {
4517 err = got_error_from_errno2("got_opentemp", path2);
4518 goto done;
4520 n = fwrite(link_target, 1, link_len, f2);
4521 if (n != link_len) {
4522 err = got_ferror(f2, GOT_ERR_IO);
4523 goto done;
4525 if (fflush(f2) == EOF) {
4526 err = got_error_from_errno("fflush");
4527 goto done;
4529 rewind(f2);
4532 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4533 if (err)
4534 goto done;
4536 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4537 if (err)
4538 goto done;
4540 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4541 if (err)
4542 goto done;
4544 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4545 NULL);
4546 if (err)
4547 goto done;
4549 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4550 if (err)
4551 goto done;
4553 if (fseek(f1, 0L, SEEK_SET) == -1)
4554 return got_ferror(f1, GOT_ERR_IO);
4555 if (fseek(f2, 0L, SEEK_SET) == -1)
4556 return got_ferror(f2, GOT_ERR_IO);
4558 /* Count the number of actual changes in the diff result. */
4559 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4560 struct diff_chunk_context cc = {};
4561 diff_chunk_context_load_change(&cc, &nchunks_used,
4562 diffreg_result->result, n, 0);
4563 nchanges++;
4565 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4566 int choice;
4567 err = apply_or_reject_change(&choice, &nchunks_used,
4568 diffreg_result->result, n, relpath, f1, f2,
4569 &line_cur1, &line_cur2,
4570 reverse_patch ? NULL : outfile,
4571 reverse_patch ? outfile : NULL,
4572 ++i, nchanges, patch_cb, patch_arg);
4573 if (err)
4574 goto done;
4575 if (choice == GOT_PATCH_CHOICE_YES)
4576 have_content = 1;
4577 else if (choice == GOT_PATCH_CHOICE_QUIT)
4578 break;
4580 if (have_content) {
4581 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4582 reverse_patch ? NULL : outfile,
4583 reverse_patch ? outfile : NULL);
4584 if (err)
4585 goto done;
4587 if (!S_ISLNK(sb2.st_mode)) {
4588 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4589 err = got_error_from_errno2("fchmod", path2);
4590 goto done;
4594 done:
4595 free(id_str);
4596 if (blob)
4597 got_object_blob_close(blob);
4598 free_err = got_diffreg_result_free(diffreg_result);
4599 if (err == NULL)
4600 err = free_err;
4601 if (f1 && fclose(f1) == EOF && err == NULL)
4602 err = got_error_from_errno2("fclose", path1);
4603 if (f2 && fclose(f2) == EOF && err == NULL)
4604 err = got_error_from_errno2("fclose", path2);
4605 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4606 err = got_error_from_errno2("close", path2);
4607 if (outfile && fclose(outfile) == EOF && err == NULL)
4608 err = got_error_from_errno2("fclose", *path_outfile);
4609 if (path1 && unlink(path1) == -1 && err == NULL)
4610 err = got_error_from_errno2("unlink", path1);
4611 if (err || !have_content) {
4612 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4613 err = got_error_from_errno2("unlink", *path_outfile);
4614 free(*path_outfile);
4615 *path_outfile = NULL;
4617 free(path1);
4618 return err;
4621 static const struct got_error *
4622 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4623 const char *relpath, struct got_object_id *blob_id,
4624 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4625 int dirfd, const char *de_name)
4627 struct revert_file_args *a = arg;
4628 const struct got_error *err = NULL;
4629 char *parent_path = NULL;
4630 struct got_fileindex_entry *ie;
4631 struct got_tree_object *tree = NULL;
4632 struct got_object_id *tree_id = NULL;
4633 const struct got_tree_entry *te = NULL;
4634 char *tree_path = NULL, *te_name;
4635 char *ondisk_path = NULL, *path_content = NULL;
4636 struct got_blob_object *blob = NULL;
4638 /* Reverting a staged deletion is a no-op. */
4639 if (status == GOT_STATUS_DELETE &&
4640 staged_status != GOT_STATUS_NO_CHANGE)
4641 return NULL;
4643 if (status == GOT_STATUS_UNVERSIONED)
4644 return (*a->progress_cb)(a->progress_arg,
4645 GOT_STATUS_UNVERSIONED, relpath);
4647 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4648 if (ie == NULL)
4649 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4651 /* Construct in-repository path of tree which contains this blob. */
4652 err = got_path_dirname(&parent_path, ie->path);
4653 if (err) {
4654 if (err->code != GOT_ERR_BAD_PATH)
4655 goto done;
4656 parent_path = strdup("/");
4657 if (parent_path == NULL) {
4658 err = got_error_from_errno("strdup");
4659 goto done;
4662 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4663 tree_path = strdup(parent_path);
4664 if (tree_path == NULL) {
4665 err = got_error_from_errno("strdup");
4666 goto done;
4668 } else {
4669 if (got_path_is_root_dir(parent_path)) {
4670 tree_path = strdup(a->worktree->path_prefix);
4671 if (tree_path == NULL) {
4672 err = got_error_from_errno("strdup");
4673 goto done;
4675 } else {
4676 if (asprintf(&tree_path, "%s/%s",
4677 a->worktree->path_prefix, parent_path) == -1) {
4678 err = got_error_from_errno("asprintf");
4679 goto done;
4684 err = got_object_id_by_path(&tree_id, a->repo,
4685 a->worktree->base_commit_id, tree_path);
4686 if (err) {
4687 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4688 (status == GOT_STATUS_ADD ||
4689 staged_status == GOT_STATUS_ADD)))
4690 goto done;
4691 } else {
4692 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4693 if (err)
4694 goto done;
4696 err = got_path_basename(&te_name, ie->path);
4697 if (err)
4698 goto done;
4700 te = got_object_tree_find_entry(tree, te_name);
4701 free(te_name);
4702 if (te == NULL && status != GOT_STATUS_ADD &&
4703 staged_status != GOT_STATUS_ADD) {
4704 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4705 goto done;
4709 switch (status) {
4710 case GOT_STATUS_ADD:
4711 if (a->patch_cb) {
4712 int choice = GOT_PATCH_CHOICE_NONE;
4713 err = (*a->patch_cb)(&choice, a->patch_arg,
4714 status, ie->path, NULL, 1, 1);
4715 if (err)
4716 goto done;
4717 if (choice != GOT_PATCH_CHOICE_YES)
4718 break;
4720 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4721 ie->path);
4722 if (err)
4723 goto done;
4724 got_fileindex_entry_remove(a->fileindex, ie);
4725 if (a->unlink_added_files) {
4726 if (asprintf(&ondisk_path, "%s/%s",
4727 got_worktree_get_root_path(a->worktree),
4728 relpath) == -1) {
4729 err = got_error_from_errno("asprintf");
4730 goto done;
4732 if (unlink(ondisk_path) == -1) {
4733 err = got_error_from_errno2("unlink",
4734 ondisk_path);
4735 break;
4738 break;
4739 case GOT_STATUS_DELETE:
4740 if (a->patch_cb) {
4741 int choice = GOT_PATCH_CHOICE_NONE;
4742 err = (*a->patch_cb)(&choice, a->patch_arg,
4743 status, ie->path, NULL, 1, 1);
4744 if (err)
4745 goto done;
4746 if (choice != GOT_PATCH_CHOICE_YES)
4747 break;
4749 /* fall through */
4750 case GOT_STATUS_MODIFY:
4751 case GOT_STATUS_MODE_CHANGE:
4752 case GOT_STATUS_CONFLICT:
4753 case GOT_STATUS_MISSING: {
4754 struct got_object_id id;
4755 if (staged_status == GOT_STATUS_ADD ||
4756 staged_status == GOT_STATUS_MODIFY) {
4757 memcpy(id.sha1, ie->staged_blob_sha1,
4758 SHA1_DIGEST_LENGTH);
4759 } else
4760 memcpy(id.sha1, ie->blob_sha1,
4761 SHA1_DIGEST_LENGTH);
4762 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4763 if (err)
4764 goto done;
4766 if (asprintf(&ondisk_path, "%s/%s",
4767 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4768 err = got_error_from_errno("asprintf");
4769 goto done;
4772 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4773 status == GOT_STATUS_CONFLICT)) {
4774 int is_bad_symlink = 0;
4775 err = create_patched_content(&path_content, 1, &id,
4776 ondisk_path, dirfd, de_name, ie->path, a->repo,
4777 a->patch_cb, a->patch_arg);
4778 if (err || path_content == NULL)
4779 break;
4780 if (te && S_ISLNK(te->mode)) {
4781 if (unlink(path_content) == -1) {
4782 err = got_error_from_errno2("unlink",
4783 path_content);
4784 break;
4786 err = install_symlink(&is_bad_symlink,
4787 a->worktree, ondisk_path, ie->path,
4788 blob, 0, 1, 0, 0, a->repo,
4789 a->progress_cb, a->progress_arg);
4790 } else {
4791 if (rename(path_content, ondisk_path) == -1) {
4792 err = got_error_from_errno3("rename",
4793 path_content, ondisk_path);
4794 goto done;
4797 } else {
4798 int is_bad_symlink = 0;
4799 if (te && S_ISLNK(te->mode)) {
4800 err = install_symlink(&is_bad_symlink,
4801 a->worktree, ondisk_path, ie->path,
4802 blob, 0, 1, 0, 0, a->repo,
4803 a->progress_cb, a->progress_arg);
4804 } else {
4805 err = install_blob(a->worktree, ondisk_path,
4806 ie->path,
4807 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4808 got_fileindex_perms_to_st(ie), blob,
4809 0, 1, 0, 0, a->repo,
4810 a->progress_cb, a->progress_arg);
4812 if (err)
4813 goto done;
4814 if (status == GOT_STATUS_DELETE ||
4815 status == GOT_STATUS_MODE_CHANGE) {
4816 err = got_fileindex_entry_update(ie,
4817 a->worktree->root_fd, relpath,
4818 blob->id.sha1,
4819 a->worktree->base_commit_id->sha1, 1);
4820 if (err)
4821 goto done;
4823 if (is_bad_symlink) {
4824 got_fileindex_entry_filetype_set(ie,
4825 GOT_FILEIDX_MODE_BAD_SYMLINK);
4828 break;
4830 default:
4831 break;
4833 done:
4834 free(ondisk_path);
4835 free(path_content);
4836 free(parent_path);
4837 free(tree_path);
4838 if (blob)
4839 got_object_blob_close(blob);
4840 if (tree)
4841 got_object_tree_close(tree);
4842 free(tree_id);
4843 return err;
4846 const struct got_error *
4847 got_worktree_revert(struct got_worktree *worktree,
4848 struct got_pathlist_head *paths,
4849 got_worktree_checkout_cb progress_cb, void *progress_arg,
4850 got_worktree_patch_cb patch_cb, void *patch_arg,
4851 struct got_repository *repo)
4853 struct got_fileindex *fileindex = NULL;
4854 char *fileindex_path = NULL;
4855 const struct got_error *err = NULL, *unlockerr = NULL;
4856 const struct got_error *sync_err = NULL;
4857 struct got_pathlist_entry *pe;
4858 struct revert_file_args rfa;
4860 err = lock_worktree(worktree, LOCK_EX);
4861 if (err)
4862 return err;
4864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4865 if (err)
4866 goto done;
4868 rfa.worktree = worktree;
4869 rfa.fileindex = fileindex;
4870 rfa.progress_cb = progress_cb;
4871 rfa.progress_arg = progress_arg;
4872 rfa.patch_cb = patch_cb;
4873 rfa.patch_arg = patch_arg;
4874 rfa.repo = repo;
4875 rfa.unlink_added_files = 0;
4876 TAILQ_FOREACH(pe, paths, entry) {
4877 err = worktree_status(worktree, pe->path, fileindex, repo,
4878 revert_file, &rfa, NULL, NULL, 0, 0);
4879 if (err)
4880 break;
4882 sync_err = sync_fileindex(fileindex, fileindex_path);
4883 if (sync_err && err == NULL)
4884 err = sync_err;
4885 done:
4886 free(fileindex_path);
4887 if (fileindex)
4888 got_fileindex_free(fileindex);
4889 unlockerr = lock_worktree(worktree, LOCK_SH);
4890 if (unlockerr && err == NULL)
4891 err = unlockerr;
4892 return err;
4895 static void
4896 free_commitable(struct got_commitable *ct)
4898 free(ct->path);
4899 free(ct->in_repo_path);
4900 free(ct->ondisk_path);
4901 free(ct->blob_id);
4902 free(ct->base_blob_id);
4903 free(ct->staged_blob_id);
4904 free(ct->base_commit_id);
4905 free(ct);
4908 struct collect_commitables_arg {
4909 struct got_pathlist_head *commitable_paths;
4910 struct got_repository *repo;
4911 struct got_worktree *worktree;
4912 struct got_fileindex *fileindex;
4913 int have_staged_files;
4914 int allow_bad_symlinks;
4917 static const struct got_error *
4918 collect_commitables(void *arg, unsigned char status,
4919 unsigned char staged_status, const char *relpath,
4920 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4921 struct got_object_id *commit_id, int dirfd, const char *de_name)
4923 struct collect_commitables_arg *a = arg;
4924 const struct got_error *err = NULL;
4925 struct got_commitable *ct = NULL;
4926 struct got_pathlist_entry *new = NULL;
4927 char *parent_path = NULL, *path = NULL;
4928 struct stat sb;
4930 if (a->have_staged_files) {
4931 if (staged_status != GOT_STATUS_MODIFY &&
4932 staged_status != GOT_STATUS_ADD &&
4933 staged_status != GOT_STATUS_DELETE)
4934 return NULL;
4935 } else {
4936 if (status == GOT_STATUS_CONFLICT)
4937 return got_error(GOT_ERR_COMMIT_CONFLICT);
4939 if (status != GOT_STATUS_MODIFY &&
4940 status != GOT_STATUS_MODE_CHANGE &&
4941 status != GOT_STATUS_ADD &&
4942 status != GOT_STATUS_DELETE)
4943 return NULL;
4946 if (asprintf(&path, "/%s", relpath) == -1) {
4947 err = got_error_from_errno("asprintf");
4948 goto done;
4950 if (strcmp(path, "/") == 0) {
4951 parent_path = strdup("");
4952 if (parent_path == NULL)
4953 return got_error_from_errno("strdup");
4954 } else {
4955 err = got_path_dirname(&parent_path, path);
4956 if (err)
4957 return err;
4960 ct = calloc(1, sizeof(*ct));
4961 if (ct == NULL) {
4962 err = got_error_from_errno("calloc");
4963 goto done;
4966 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4967 relpath) == -1) {
4968 err = got_error_from_errno("asprintf");
4969 goto done;
4972 if (staged_status == GOT_STATUS_ADD ||
4973 staged_status == GOT_STATUS_MODIFY) {
4974 struct got_fileindex_entry *ie;
4975 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4976 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4977 case GOT_FILEIDX_MODE_REGULAR_FILE:
4978 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4979 ct->mode = S_IFREG;
4980 break;
4981 case GOT_FILEIDX_MODE_SYMLINK:
4982 ct->mode = S_IFLNK;
4983 break;
4984 default:
4985 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4986 goto done;
4988 ct->mode |= got_fileindex_entry_perms_get(ie);
4989 } else if (status != GOT_STATUS_DELETE &&
4990 staged_status != GOT_STATUS_DELETE) {
4991 if (dirfd != -1) {
4992 if (fstatat(dirfd, de_name, &sb,
4993 AT_SYMLINK_NOFOLLOW) == -1) {
4994 err = got_error_from_errno2("fstatat",
4995 ct->ondisk_path);
4996 goto done;
4998 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4999 err = got_error_from_errno2("lstat", ct->ondisk_path);
5000 goto done;
5002 ct->mode = sb.st_mode;
5005 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5006 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5007 relpath) == -1) {
5008 err = got_error_from_errno("asprintf");
5009 goto done;
5012 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5013 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5014 int is_bad_symlink;
5015 char target_path[PATH_MAX];
5016 ssize_t target_len;
5017 target_len = readlink(ct->ondisk_path, target_path,
5018 sizeof(target_path));
5019 if (target_len == -1) {
5020 err = got_error_from_errno2("readlink",
5021 ct->ondisk_path);
5022 goto done;
5024 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5025 target_len, ct->ondisk_path, a->worktree->root_path);
5026 if (err)
5027 goto done;
5028 if (is_bad_symlink) {
5029 err = got_error_path(ct->ondisk_path,
5030 GOT_ERR_BAD_SYMLINK);
5031 goto done;
5036 ct->status = status;
5037 ct->staged_status = staged_status;
5038 ct->blob_id = NULL; /* will be filled in when blob gets created */
5039 if (ct->status != GOT_STATUS_ADD &&
5040 ct->staged_status != GOT_STATUS_ADD) {
5041 ct->base_blob_id = got_object_id_dup(blob_id);
5042 if (ct->base_blob_id == NULL) {
5043 err = got_error_from_errno("got_object_id_dup");
5044 goto done;
5046 ct->base_commit_id = got_object_id_dup(commit_id);
5047 if (ct->base_commit_id == NULL) {
5048 err = got_error_from_errno("got_object_id_dup");
5049 goto done;
5052 if (ct->staged_status == GOT_STATUS_ADD ||
5053 ct->staged_status == GOT_STATUS_MODIFY) {
5054 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5055 if (ct->staged_blob_id == NULL) {
5056 err = got_error_from_errno("got_object_id_dup");
5057 goto done;
5060 ct->path = strdup(path);
5061 if (ct->path == NULL) {
5062 err = got_error_from_errno("strdup");
5063 goto done;
5065 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5066 done:
5067 if (ct && (err || new == NULL))
5068 free_commitable(ct);
5069 free(parent_path);
5070 free(path);
5071 return err;
5074 static const struct got_error *write_tree(struct got_object_id **, int *,
5075 struct got_tree_object *, const char *, struct got_pathlist_head *,
5076 got_worktree_status_cb status_cb, void *status_arg,
5077 struct got_repository *);
5079 static const struct got_error *
5080 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5081 struct got_tree_entry *te, const char *parent_path,
5082 struct got_pathlist_head *commitable_paths,
5083 got_worktree_status_cb status_cb, void *status_arg,
5084 struct got_repository *repo)
5086 const struct got_error *err = NULL;
5087 struct got_tree_object *subtree;
5088 char *subpath;
5090 if (asprintf(&subpath, "%s%s%s", parent_path,
5091 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5092 return got_error_from_errno("asprintf");
5094 err = got_object_open_as_tree(&subtree, repo, &te->id);
5095 if (err)
5096 return err;
5098 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5099 commitable_paths, status_cb, status_arg, repo);
5100 got_object_tree_close(subtree);
5101 free(subpath);
5102 return err;
5105 static const struct got_error *
5106 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5108 const struct got_error *err = NULL;
5109 char *ct_parent_path = NULL;
5111 *match = 0;
5113 if (strchr(ct->in_repo_path, '/') == NULL) {
5114 *match = got_path_is_root_dir(path);
5115 return NULL;
5118 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5119 if (err)
5120 return err;
5121 *match = (strcmp(path, ct_parent_path) == 0);
5122 free(ct_parent_path);
5123 return err;
5126 static mode_t
5127 get_ct_file_mode(struct got_commitable *ct)
5129 if (S_ISLNK(ct->mode))
5130 return S_IFLNK;
5132 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5135 static const struct got_error *
5136 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5137 struct got_tree_entry *te, struct got_commitable *ct)
5139 const struct got_error *err = NULL;
5141 *new_te = NULL;
5143 err = got_object_tree_entry_dup(new_te, te);
5144 if (err)
5145 goto done;
5147 (*new_te)->mode = get_ct_file_mode(ct);
5149 if (ct->staged_status == GOT_STATUS_MODIFY)
5150 memcpy(&(*new_te)->id, ct->staged_blob_id,
5151 sizeof((*new_te)->id));
5152 else
5153 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5154 done:
5155 if (err && *new_te) {
5156 free(*new_te);
5157 *new_te = NULL;
5159 return err;
5162 static const struct got_error *
5163 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5164 struct got_commitable *ct)
5166 const struct got_error *err = NULL;
5167 char *ct_name = NULL;
5169 *new_te = NULL;
5171 *new_te = calloc(1, sizeof(**new_te));
5172 if (*new_te == NULL)
5173 return got_error_from_errno("calloc");
5175 err = got_path_basename(&ct_name, ct->path);
5176 if (err)
5177 goto done;
5178 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5179 sizeof((*new_te)->name)) {
5180 err = got_error(GOT_ERR_NO_SPACE);
5181 goto done;
5184 (*new_te)->mode = get_ct_file_mode(ct);
5186 if (ct->staged_status == GOT_STATUS_ADD)
5187 memcpy(&(*new_te)->id, ct->staged_blob_id,
5188 sizeof((*new_te)->id));
5189 else
5190 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5191 done:
5192 free(ct_name);
5193 if (err && *new_te) {
5194 free(*new_te);
5195 *new_te = NULL;
5197 return err;
5200 static const struct got_error *
5201 insert_tree_entry(struct got_tree_entry *new_te,
5202 struct got_pathlist_head *paths)
5204 const struct got_error *err = NULL;
5205 struct got_pathlist_entry *new_pe;
5207 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5208 if (err)
5209 return err;
5210 if (new_pe == NULL)
5211 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5212 return NULL;
5215 static const struct got_error *
5216 report_ct_status(struct got_commitable *ct,
5217 got_worktree_status_cb status_cb, void *status_arg)
5219 const char *ct_path = ct->path;
5220 unsigned char status;
5222 if (status_cb == NULL) /* no commit progress output desired */
5223 return NULL;
5225 while (ct_path[0] == '/')
5226 ct_path++;
5228 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5229 status = ct->staged_status;
5230 else
5231 status = ct->status;
5233 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5234 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5237 static const struct got_error *
5238 match_modified_subtree(int *modified, struct got_tree_entry *te,
5239 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5241 const struct got_error *err = NULL;
5242 struct got_pathlist_entry *pe;
5243 char *te_path;
5245 *modified = 0;
5247 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5248 got_path_is_root_dir(base_tree_path) ? "" : "/",
5249 te->name) == -1)
5250 return got_error_from_errno("asprintf");
5252 TAILQ_FOREACH(pe, commitable_paths, entry) {
5253 struct got_commitable *ct = pe->data;
5254 *modified = got_path_is_child(ct->in_repo_path, te_path,
5255 strlen(te_path));
5256 if (*modified)
5257 break;
5260 free(te_path);
5261 return err;
5264 static const struct got_error *
5265 match_deleted_or_modified_ct(struct got_commitable **ctp,
5266 struct got_tree_entry *te, const char *base_tree_path,
5267 struct got_pathlist_head *commitable_paths)
5269 const struct got_error *err = NULL;
5270 struct got_pathlist_entry *pe;
5272 *ctp = NULL;
5274 TAILQ_FOREACH(pe, commitable_paths, entry) {
5275 struct got_commitable *ct = pe->data;
5276 char *ct_name = NULL;
5277 int path_matches;
5279 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5280 if (ct->status != GOT_STATUS_MODIFY &&
5281 ct->status != GOT_STATUS_MODE_CHANGE &&
5282 ct->status != GOT_STATUS_DELETE)
5283 continue;
5284 } else {
5285 if (ct->staged_status != GOT_STATUS_MODIFY &&
5286 ct->staged_status != GOT_STATUS_DELETE)
5287 continue;
5290 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5291 continue;
5293 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5294 if (err)
5295 return err;
5296 if (!path_matches)
5297 continue;
5299 err = got_path_basename(&ct_name, pe->path);
5300 if (err)
5301 return err;
5303 if (strcmp(te->name, ct_name) != 0) {
5304 free(ct_name);
5305 continue;
5307 free(ct_name);
5309 *ctp = ct;
5310 break;
5313 return err;
5316 static const struct got_error *
5317 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5318 const char *child_path, const char *path_base_tree,
5319 struct got_pathlist_head *commitable_paths,
5320 got_worktree_status_cb status_cb, void *status_arg,
5321 struct got_repository *repo)
5323 const struct got_error *err = NULL;
5324 struct got_tree_entry *new_te;
5325 char *subtree_path;
5326 struct got_object_id *id = NULL;
5327 int nentries;
5329 *new_tep = NULL;
5331 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5332 got_path_is_root_dir(path_base_tree) ? "" : "/",
5333 child_path) == -1)
5334 return got_error_from_errno("asprintf");
5336 new_te = calloc(1, sizeof(*new_te));
5337 if (new_te == NULL)
5338 return got_error_from_errno("calloc");
5339 new_te->mode = S_IFDIR;
5341 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5342 sizeof(new_te->name)) {
5343 err = got_error(GOT_ERR_NO_SPACE);
5344 goto done;
5346 err = write_tree(&id, &nentries, NULL, subtree_path,
5347 commitable_paths, status_cb, status_arg, repo);
5348 if (err) {
5349 free(new_te);
5350 goto done;
5352 memcpy(&new_te->id, id, sizeof(new_te->id));
5353 done:
5354 free(id);
5355 free(subtree_path);
5356 if (err == NULL)
5357 *new_tep = new_te;
5358 return err;
5361 static const struct got_error *
5362 write_tree(struct got_object_id **new_tree_id, int *nentries,
5363 struct got_tree_object *base_tree, const char *path_base_tree,
5364 struct got_pathlist_head *commitable_paths,
5365 got_worktree_status_cb status_cb, void *status_arg,
5366 struct got_repository *repo)
5368 const struct got_error *err = NULL;
5369 struct got_pathlist_head paths;
5370 struct got_tree_entry *te, *new_te = NULL;
5371 struct got_pathlist_entry *pe;
5373 TAILQ_INIT(&paths);
5374 *nentries = 0;
5376 /* Insert, and recurse into, newly added entries first. */
5377 TAILQ_FOREACH(pe, commitable_paths, entry) {
5378 struct got_commitable *ct = pe->data;
5379 char *child_path = NULL, *slash;
5381 if ((ct->status != GOT_STATUS_ADD &&
5382 ct->staged_status != GOT_STATUS_ADD) ||
5383 (ct->flags & GOT_COMMITABLE_ADDED))
5384 continue;
5386 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5387 strlen(path_base_tree)))
5388 continue;
5390 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5391 ct->in_repo_path);
5392 if (err)
5393 goto done;
5395 slash = strchr(child_path, '/');
5396 if (slash == NULL) {
5397 err = alloc_added_blob_tree_entry(&new_te, ct);
5398 if (err)
5399 goto done;
5400 err = report_ct_status(ct, status_cb, status_arg);
5401 if (err)
5402 goto done;
5403 ct->flags |= GOT_COMMITABLE_ADDED;
5404 err = insert_tree_entry(new_te, &paths);
5405 if (err)
5406 goto done;
5407 (*nentries)++;
5408 } else {
5409 *slash = '\0'; /* trim trailing path components */
5410 if (base_tree == NULL ||
5411 got_object_tree_find_entry(base_tree, child_path)
5412 == NULL) {
5413 err = make_subtree_for_added_blob(&new_te,
5414 child_path, path_base_tree,
5415 commitable_paths, status_cb, status_arg,
5416 repo);
5417 if (err)
5418 goto done;
5419 err = insert_tree_entry(new_te, &paths);
5420 if (err)
5421 goto done;
5422 (*nentries)++;
5427 if (base_tree) {
5428 int i, nbase_entries;
5429 /* Handle modified and deleted entries. */
5430 nbase_entries = got_object_tree_get_nentries(base_tree);
5431 for (i = 0; i < nbase_entries; i++) {
5432 struct got_commitable *ct = NULL;
5434 te = got_object_tree_get_entry(base_tree, i);
5435 if (got_object_tree_entry_is_submodule(te)) {
5436 /* Entry is a submodule; just copy it. */
5437 err = got_object_tree_entry_dup(&new_te, te);
5438 if (err)
5439 goto done;
5440 err = insert_tree_entry(new_te, &paths);
5441 if (err)
5442 goto done;
5443 (*nentries)++;
5444 continue;
5447 if (S_ISDIR(te->mode)) {
5448 int modified;
5449 err = got_object_tree_entry_dup(&new_te, te);
5450 if (err)
5451 goto done;
5452 err = match_modified_subtree(&modified, te,
5453 path_base_tree, commitable_paths);
5454 if (err)
5455 goto done;
5456 /* Avoid recursion into unmodified subtrees. */
5457 if (modified) {
5458 struct got_object_id *new_id;
5459 int nsubentries;
5460 err = write_subtree(&new_id,
5461 &nsubentries, te,
5462 path_base_tree, commitable_paths,
5463 status_cb, status_arg, repo);
5464 if (err)
5465 goto done;
5466 if (nsubentries == 0) {
5467 /* All entries were deleted. */
5468 free(new_id);
5469 continue;
5471 memcpy(&new_te->id, new_id,
5472 sizeof(new_te->id));
5473 free(new_id);
5475 err = insert_tree_entry(new_te, &paths);
5476 if (err)
5477 goto done;
5478 (*nentries)++;
5479 continue;
5482 err = match_deleted_or_modified_ct(&ct, te,
5483 path_base_tree, commitable_paths);
5484 if (err)
5485 goto done;
5486 if (ct) {
5487 /* NB: Deleted entries get dropped here. */
5488 if (ct->status == GOT_STATUS_MODIFY ||
5489 ct->status == GOT_STATUS_MODE_CHANGE ||
5490 ct->staged_status == GOT_STATUS_MODIFY) {
5491 err = alloc_modified_blob_tree_entry(
5492 &new_te, te, ct);
5493 if (err)
5494 goto done;
5495 err = insert_tree_entry(new_te, &paths);
5496 if (err)
5497 goto done;
5498 (*nentries)++;
5500 err = report_ct_status(ct, status_cb,
5501 status_arg);
5502 if (err)
5503 goto done;
5504 } else {
5505 /* Entry is unchanged; just copy it. */
5506 err = got_object_tree_entry_dup(&new_te, te);
5507 if (err)
5508 goto done;
5509 err = insert_tree_entry(new_te, &paths);
5510 if (err)
5511 goto done;
5512 (*nentries)++;
5517 /* Write new list of entries; deleted entries have been dropped. */
5518 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5519 done:
5520 got_pathlist_free(&paths);
5521 return err;
5524 static const struct got_error *
5525 update_fileindex_after_commit(struct got_worktree *worktree,
5526 struct got_pathlist_head *commitable_paths,
5527 struct got_object_id *new_base_commit_id,
5528 struct got_fileindex *fileindex, int have_staged_files)
5530 const struct got_error *err = NULL;
5531 struct got_pathlist_entry *pe;
5532 char *relpath = NULL;
5534 TAILQ_FOREACH(pe, commitable_paths, entry) {
5535 struct got_fileindex_entry *ie;
5536 struct got_commitable *ct = pe->data;
5538 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5540 err = got_path_skip_common_ancestor(&relpath,
5541 worktree->root_path, ct->ondisk_path);
5542 if (err)
5543 goto done;
5545 if (ie) {
5546 if (ct->status == GOT_STATUS_DELETE ||
5547 ct->staged_status == GOT_STATUS_DELETE) {
5548 got_fileindex_entry_remove(fileindex, ie);
5549 } else if (ct->staged_status == GOT_STATUS_ADD ||
5550 ct->staged_status == GOT_STATUS_MODIFY) {
5551 got_fileindex_entry_stage_set(ie,
5552 GOT_FILEIDX_STAGE_NONE);
5553 got_fileindex_entry_staged_filetype_set(ie, 0);
5555 err = got_fileindex_entry_update(ie,
5556 worktree->root_fd, relpath,
5557 ct->staged_blob_id->sha1,
5558 new_base_commit_id->sha1,
5559 !have_staged_files);
5560 } else
5561 err = got_fileindex_entry_update(ie,
5562 worktree->root_fd, relpath,
5563 ct->blob_id->sha1,
5564 new_base_commit_id->sha1,
5565 !have_staged_files);
5566 } else {
5567 err = got_fileindex_entry_alloc(&ie, pe->path);
5568 if (err)
5569 goto done;
5570 err = got_fileindex_entry_update(ie,
5571 worktree->root_fd, relpath, ct->blob_id->sha1,
5572 new_base_commit_id->sha1, 1);
5573 if (err) {
5574 got_fileindex_entry_free(ie);
5575 goto done;
5577 err = got_fileindex_entry_add(fileindex, ie);
5578 if (err) {
5579 got_fileindex_entry_free(ie);
5580 goto done;
5583 free(relpath);
5584 relpath = NULL;
5586 done:
5587 free(relpath);
5588 return err;
5592 static const struct got_error *
5593 check_out_of_date(const char *in_repo_path, unsigned char status,
5594 unsigned char staged_status, struct got_object_id *base_blob_id,
5595 struct got_object_id *base_commit_id,
5596 struct got_object_id *head_commit_id, struct got_repository *repo,
5597 int ood_errcode)
5599 const struct got_error *err = NULL;
5600 struct got_object_id *id = NULL;
5602 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5603 /* Trivial case: base commit == head commit */
5604 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5605 return NULL;
5607 * Ensure file content which local changes were based
5608 * on matches file content in the branch head.
5610 err = got_object_id_by_path(&id, repo, head_commit_id,
5611 in_repo_path);
5612 if (err) {
5613 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5614 err = got_error(ood_errcode);
5615 goto done;
5616 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5617 err = got_error(ood_errcode);
5618 } else {
5619 /* Require that added files don't exist in the branch head. */
5620 err = got_object_id_by_path(&id, repo, head_commit_id,
5621 in_repo_path);
5622 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5623 goto done;
5624 err = id ? got_error(ood_errcode) : NULL;
5626 done:
5627 free(id);
5628 return err;
5631 const struct got_error *
5632 commit_worktree(struct got_object_id **new_commit_id,
5633 struct got_pathlist_head *commitable_paths,
5634 struct got_object_id *head_commit_id,
5635 struct got_object_id *parent_id2,
5636 struct got_worktree *worktree,
5637 const char *author, const char *committer,
5638 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5639 got_worktree_status_cb status_cb, void *status_arg,
5640 struct got_repository *repo)
5642 const struct got_error *err = NULL, *unlockerr = NULL;
5643 struct got_pathlist_entry *pe;
5644 const char *head_ref_name = NULL;
5645 struct got_commit_object *head_commit = NULL;
5646 struct got_reference *head_ref2 = NULL;
5647 struct got_object_id *head_commit_id2 = NULL;
5648 struct got_tree_object *head_tree = NULL;
5649 struct got_object_id *new_tree_id = NULL;
5650 int nentries, nparents = 0;
5651 struct got_object_id_queue parent_ids;
5652 struct got_object_qid *pid = NULL;
5653 char *logmsg = NULL;
5655 *new_commit_id = NULL;
5657 STAILQ_INIT(&parent_ids);
5659 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5660 if (err)
5661 goto done;
5663 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5664 if (err)
5665 goto done;
5667 if (commit_msg_cb != NULL) {
5668 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5669 if (err)
5670 goto done;
5673 if (logmsg == NULL || strlen(logmsg) == 0) {
5674 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5675 goto done;
5678 /* Create blobs from added and modified files and record their IDs. */
5679 TAILQ_FOREACH(pe, commitable_paths, entry) {
5680 struct got_commitable *ct = pe->data;
5681 char *ondisk_path;
5683 /* Blobs for staged files already exist. */
5684 if (ct->staged_status == GOT_STATUS_ADD ||
5685 ct->staged_status == GOT_STATUS_MODIFY)
5686 continue;
5688 if (ct->status != GOT_STATUS_ADD &&
5689 ct->status != GOT_STATUS_MODIFY &&
5690 ct->status != GOT_STATUS_MODE_CHANGE)
5691 continue;
5693 if (asprintf(&ondisk_path, "%s/%s",
5694 worktree->root_path, pe->path) == -1) {
5695 err = got_error_from_errno("asprintf");
5696 goto done;
5698 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5699 free(ondisk_path);
5700 if (err)
5701 goto done;
5704 /* Recursively write new tree objects. */
5705 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5706 commitable_paths, status_cb, status_arg, repo);
5707 if (err)
5708 goto done;
5710 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5711 if (err)
5712 goto done;
5713 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5714 nparents++;
5715 if (parent_id2) {
5716 err = got_object_qid_alloc(&pid, parent_id2);
5717 if (err)
5718 goto done;
5719 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5720 nparents++;
5722 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5723 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5724 if (logmsg != NULL)
5725 free(logmsg);
5726 if (err)
5727 goto done;
5729 /* Check if a concurrent commit to our branch has occurred. */
5730 head_ref_name = got_worktree_get_head_ref_name(worktree);
5731 if (head_ref_name == NULL) {
5732 err = got_error_from_errno("got_worktree_get_head_ref_name");
5733 goto done;
5735 /* Lock the reference here to prevent concurrent modification. */
5736 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5737 if (err)
5738 goto done;
5739 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5740 if (err)
5741 goto done;
5742 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5743 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5744 goto done;
5746 /* Update branch head in repository. */
5747 err = got_ref_change_ref(head_ref2, *new_commit_id);
5748 if (err)
5749 goto done;
5750 err = got_ref_write(head_ref2, repo);
5751 if (err)
5752 goto done;
5754 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5755 if (err)
5756 goto done;
5758 err = ref_base_commit(worktree, repo);
5759 if (err)
5760 goto done;
5761 done:
5762 got_object_id_queue_free(&parent_ids);
5763 if (head_tree)
5764 got_object_tree_close(head_tree);
5765 if (head_commit)
5766 got_object_commit_close(head_commit);
5767 free(head_commit_id2);
5768 if (head_ref2) {
5769 unlockerr = got_ref_unlock(head_ref2);
5770 if (unlockerr && err == NULL)
5771 err = unlockerr;
5772 got_ref_close(head_ref2);
5774 return err;
5777 static const struct got_error *
5778 check_path_is_commitable(const char *path,
5779 struct got_pathlist_head *commitable_paths)
5781 struct got_pathlist_entry *cpe = NULL;
5782 size_t path_len = strlen(path);
5784 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5785 struct got_commitable *ct = cpe->data;
5786 const char *ct_path = ct->path;
5788 while (ct_path[0] == '/')
5789 ct_path++;
5791 if (strcmp(path, ct_path) == 0 ||
5792 got_path_is_child(ct_path, path, path_len))
5793 break;
5796 if (cpe == NULL)
5797 return got_error_path(path, GOT_ERR_BAD_PATH);
5799 return NULL;
5802 static const struct got_error *
5803 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5805 int *have_staged_files = arg;
5807 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5808 *have_staged_files = 1;
5809 return got_error(GOT_ERR_CANCELLED);
5812 return NULL;
5815 static const struct got_error *
5816 check_non_staged_files(struct got_fileindex *fileindex,
5817 struct got_pathlist_head *paths)
5819 struct got_pathlist_entry *pe;
5820 struct got_fileindex_entry *ie;
5822 TAILQ_FOREACH(pe, paths, entry) {
5823 if (pe->path[0] == '\0')
5824 continue;
5825 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5826 if (ie == NULL)
5827 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5828 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5829 return got_error_path(pe->path,
5830 GOT_ERR_FILE_NOT_STAGED);
5833 return NULL;
5836 const struct got_error *
5837 got_worktree_commit(struct got_object_id **new_commit_id,
5838 struct got_worktree *worktree, struct got_pathlist_head *paths,
5839 const char *author, const char *committer, int allow_bad_symlinks,
5840 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5841 got_worktree_status_cb status_cb, void *status_arg,
5842 struct got_repository *repo)
5844 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5845 struct got_fileindex *fileindex = NULL;
5846 char *fileindex_path = NULL;
5847 struct got_pathlist_head commitable_paths;
5848 struct collect_commitables_arg cc_arg;
5849 struct got_pathlist_entry *pe;
5850 struct got_reference *head_ref = NULL;
5851 struct got_object_id *head_commit_id = NULL;
5852 int have_staged_files = 0;
5854 *new_commit_id = NULL;
5856 TAILQ_INIT(&commitable_paths);
5858 err = lock_worktree(worktree, LOCK_EX);
5859 if (err)
5860 goto done;
5862 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5863 if (err)
5864 goto done;
5866 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5867 if (err)
5868 goto done;
5870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5871 if (err)
5872 goto done;
5874 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5875 &have_staged_files);
5876 if (err && err->code != GOT_ERR_CANCELLED)
5877 goto done;
5878 if (have_staged_files) {
5879 err = check_non_staged_files(fileindex, paths);
5880 if (err)
5881 goto done;
5884 cc_arg.commitable_paths = &commitable_paths;
5885 cc_arg.worktree = worktree;
5886 cc_arg.fileindex = fileindex;
5887 cc_arg.repo = repo;
5888 cc_arg.have_staged_files = have_staged_files;
5889 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5890 TAILQ_FOREACH(pe, paths, entry) {
5891 err = worktree_status(worktree, pe->path, fileindex, repo,
5892 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5893 if (err)
5894 goto done;
5897 if (TAILQ_EMPTY(&commitable_paths)) {
5898 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5899 goto done;
5902 TAILQ_FOREACH(pe, paths, entry) {
5903 err = check_path_is_commitable(pe->path, &commitable_paths);
5904 if (err)
5905 goto done;
5908 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5909 struct got_commitable *ct = pe->data;
5910 const char *ct_path = ct->in_repo_path;
5912 while (ct_path[0] == '/')
5913 ct_path++;
5914 err = check_out_of_date(ct_path, ct->status,
5915 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5916 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5917 if (err)
5918 goto done;
5922 err = commit_worktree(new_commit_id, &commitable_paths,
5923 head_commit_id, NULL, worktree, author, committer,
5924 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5925 if (err)
5926 goto done;
5928 err = update_fileindex_after_commit(worktree, &commitable_paths,
5929 *new_commit_id, fileindex, have_staged_files);
5930 sync_err = sync_fileindex(fileindex, fileindex_path);
5931 if (sync_err && err == NULL)
5932 err = sync_err;
5933 done:
5934 if (fileindex)
5935 got_fileindex_free(fileindex);
5936 free(fileindex_path);
5937 unlockerr = lock_worktree(worktree, LOCK_SH);
5938 if (unlockerr && err == NULL)
5939 err = unlockerr;
5940 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5941 struct got_commitable *ct = pe->data;
5942 free_commitable(ct);
5944 got_pathlist_free(&commitable_paths);
5945 return err;
5948 const char *
5949 got_commitable_get_path(struct got_commitable *ct)
5951 return ct->path;
5954 unsigned int
5955 got_commitable_get_status(struct got_commitable *ct)
5957 return ct->status;
5960 struct check_rebase_ok_arg {
5961 struct got_worktree *worktree;
5962 struct got_repository *repo;
5965 static const struct got_error *
5966 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5968 const struct got_error *err = NULL;
5969 struct check_rebase_ok_arg *a = arg;
5970 unsigned char status;
5971 struct stat sb;
5972 char *ondisk_path;
5974 /* Reject rebase of a work tree with mixed base commits. */
5975 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5976 SHA1_DIGEST_LENGTH))
5977 return got_error(GOT_ERR_MIXED_COMMITS);
5979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5980 == -1)
5981 return got_error_from_errno("asprintf");
5983 /* Reject rebase of a work tree with modified or staged files. */
5984 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5985 free(ondisk_path);
5986 if (err)
5987 return err;
5989 if (status != GOT_STATUS_NO_CHANGE)
5990 return got_error(GOT_ERR_MODIFIED);
5991 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5992 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5994 return NULL;
5997 const struct got_error *
5998 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5999 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_reference *branch,
6001 struct got_repository *repo)
6003 const struct got_error *err = NULL;
6004 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6005 char *branch_ref_name = NULL;
6006 char *fileindex_path = NULL;
6007 struct check_rebase_ok_arg ok_arg;
6008 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6009 struct got_object_id *wt_branch_tip = NULL;
6011 *new_base_branch_ref = NULL;
6012 *tmp_branch = NULL;
6013 *fileindex = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6016 if (err)
6017 return err;
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6020 if (err)
6021 goto done;
6023 ok_arg.worktree = worktree;
6024 ok_arg.repo = repo;
6025 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6026 &ok_arg);
6027 if (err)
6028 goto done;
6030 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6031 if (err)
6032 goto done;
6034 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6035 if (err)
6036 goto done;
6038 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6039 if (err)
6040 goto done;
6042 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6043 0);
6044 if (err)
6045 goto done;
6047 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6048 if (err)
6049 goto done;
6050 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6051 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6052 goto done;
6055 err = got_ref_alloc_symref(new_base_branch_ref,
6056 new_base_branch_ref_name, wt_branch);
6057 if (err)
6058 goto done;
6059 err = got_ref_write(*new_base_branch_ref, repo);
6060 if (err)
6061 goto done;
6063 /* TODO Lock original branch's ref while rebasing? */
6065 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6066 if (err)
6067 goto done;
6069 err = got_ref_write(branch_ref, repo);
6070 if (err)
6071 goto done;
6073 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6074 worktree->base_commit_id);
6075 if (err)
6076 goto done;
6077 err = got_ref_write(*tmp_branch, repo);
6078 if (err)
6079 goto done;
6081 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6082 if (err)
6083 goto done;
6084 done:
6085 free(fileindex_path);
6086 free(tmp_branch_name);
6087 free(new_base_branch_ref_name);
6088 free(branch_ref_name);
6089 if (branch_ref)
6090 got_ref_close(branch_ref);
6091 if (wt_branch)
6092 got_ref_close(wt_branch);
6093 free(wt_branch_tip);
6094 if (err) {
6095 if (*new_base_branch_ref) {
6096 got_ref_close(*new_base_branch_ref);
6097 *new_base_branch_ref = NULL;
6099 if (*tmp_branch) {
6100 got_ref_close(*tmp_branch);
6101 *tmp_branch = NULL;
6103 if (*fileindex) {
6104 got_fileindex_free(*fileindex);
6105 *fileindex = NULL;
6107 lock_worktree(worktree, LOCK_SH);
6109 return err;
6112 const struct got_error *
6113 got_worktree_rebase_continue(struct got_object_id **commit_id,
6114 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6115 struct got_reference **branch, struct got_fileindex **fileindex,
6116 struct got_worktree *worktree, struct got_repository *repo)
6118 const struct got_error *err;
6119 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6120 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6121 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6122 char *fileindex_path = NULL;
6123 int have_staged_files = 0;
6125 *commit_id = NULL;
6126 *new_base_branch = NULL;
6127 *tmp_branch = NULL;
6128 *branch = NULL;
6129 *fileindex = NULL;
6131 err = lock_worktree(worktree, LOCK_EX);
6132 if (err)
6133 return err;
6135 err = open_fileindex(fileindex, &fileindex_path, worktree);
6136 if (err)
6137 goto done;
6139 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6140 &have_staged_files);
6141 if (err && err->code != GOT_ERR_CANCELLED)
6142 goto done;
6143 if (have_staged_files) {
6144 err = got_error(GOT_ERR_STAGED_PATHS);
6145 goto done;
6148 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6149 if (err)
6150 goto done;
6152 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6153 if (err)
6154 goto done;
6156 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6157 if (err)
6158 goto done;
6160 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6161 if (err)
6162 goto done;
6164 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6165 if (err)
6166 goto done;
6168 err = got_ref_open(branch, repo,
6169 got_ref_get_symref_target(branch_ref), 0);
6170 if (err)
6171 goto done;
6173 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6174 if (err)
6175 goto done;
6177 err = got_ref_resolve(commit_id, repo, commit_ref);
6178 if (err)
6179 goto done;
6181 err = got_ref_open(new_base_branch, repo,
6182 new_base_branch_ref_name, 0);
6183 if (err)
6184 goto done;
6186 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6187 if (err)
6188 goto done;
6189 done:
6190 free(commit_ref_name);
6191 free(branch_ref_name);
6192 free(fileindex_path);
6193 if (commit_ref)
6194 got_ref_close(commit_ref);
6195 if (branch_ref)
6196 got_ref_close(branch_ref);
6197 if (err) {
6198 free(*commit_id);
6199 *commit_id = NULL;
6200 if (*tmp_branch) {
6201 got_ref_close(*tmp_branch);
6202 *tmp_branch = NULL;
6204 if (*new_base_branch) {
6205 got_ref_close(*new_base_branch);
6206 *new_base_branch = NULL;
6208 if (*branch) {
6209 got_ref_close(*branch);
6210 *branch = NULL;
6212 if (*fileindex) {
6213 got_fileindex_free(*fileindex);
6214 *fileindex = NULL;
6216 lock_worktree(worktree, LOCK_SH);
6218 return err;
6221 const struct got_error *
6222 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6224 const struct got_error *err;
6225 char *tmp_branch_name = NULL;
6227 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6228 if (err)
6229 return err;
6231 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6232 free(tmp_branch_name);
6233 return NULL;
6236 static const struct got_error *
6237 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6238 char **logmsg, void *arg)
6240 *logmsg = arg;
6241 return NULL;
6244 static const struct got_error *
6245 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6246 const char *path, struct got_object_id *blob_id,
6247 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6248 int dirfd, const char *de_name)
6250 return NULL;
6253 struct collect_merged_paths_arg {
6254 got_worktree_checkout_cb progress_cb;
6255 void *progress_arg;
6256 struct got_pathlist_head *merged_paths;
6259 static const struct got_error *
6260 collect_merged_paths(void *arg, unsigned char status, const char *path)
6262 const struct got_error *err;
6263 struct collect_merged_paths_arg *a = arg;
6264 char *p;
6265 struct got_pathlist_entry *new;
6267 err = (*a->progress_cb)(a->progress_arg, status, path);
6268 if (err)
6269 return err;
6271 if (status != GOT_STATUS_MERGE &&
6272 status != GOT_STATUS_ADD &&
6273 status != GOT_STATUS_DELETE &&
6274 status != GOT_STATUS_CONFLICT)
6275 return NULL;
6277 p = strdup(path);
6278 if (p == NULL)
6279 return got_error_from_errno("strdup");
6281 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6282 if (err || new == NULL)
6283 free(p);
6284 return err;
6287 void
6288 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6290 struct got_pathlist_entry *pe;
6292 TAILQ_FOREACH(pe, merged_paths, entry)
6293 free((char *)pe->path);
6295 got_pathlist_free(merged_paths);
6298 static const struct got_error *
6299 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6300 int is_rebase, struct got_repository *repo)
6302 const struct got_error *err;
6303 struct got_reference *commit_ref = NULL;
6305 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6306 if (err) {
6307 if (err->code != GOT_ERR_NOT_REF)
6308 goto done;
6309 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6310 if (err)
6311 goto done;
6312 err = got_ref_write(commit_ref, repo);
6313 if (err)
6314 goto done;
6315 } else if (is_rebase) {
6316 struct got_object_id *stored_id;
6317 int cmp;
6319 err = got_ref_resolve(&stored_id, repo, commit_ref);
6320 if (err)
6321 goto done;
6322 cmp = got_object_id_cmp(commit_id, stored_id);
6323 free(stored_id);
6324 if (cmp != 0) {
6325 err = got_error(GOT_ERR_REBASE_COMMITID);
6326 goto done;
6329 done:
6330 if (commit_ref)
6331 got_ref_close(commit_ref);
6332 return err;
6335 static const struct got_error *
6336 rebase_merge_files(struct got_pathlist_head *merged_paths,
6337 const char *commit_ref_name, struct got_worktree *worktree,
6338 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6339 struct got_object_id *commit_id, struct got_repository *repo,
6340 got_worktree_checkout_cb progress_cb, void *progress_arg,
6341 got_cancel_cb cancel_cb, void *cancel_arg)
6343 const struct got_error *err;
6344 struct got_reference *commit_ref = NULL;
6345 struct collect_merged_paths_arg cmp_arg;
6346 char *fileindex_path;
6348 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6350 err = get_fileindex_path(&fileindex_path, worktree);
6351 if (err)
6352 return err;
6354 cmp_arg.progress_cb = progress_cb;
6355 cmp_arg.progress_arg = progress_arg;
6356 cmp_arg.merged_paths = merged_paths;
6357 err = merge_files(worktree, fileindex, fileindex_path,
6358 parent_commit_id, commit_id, repo, collect_merged_paths,
6359 &cmp_arg, cancel_cb, cancel_arg);
6360 if (commit_ref)
6361 got_ref_close(commit_ref);
6362 return err;
6365 const struct got_error *
6366 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6367 struct got_worktree *worktree, struct got_fileindex *fileindex,
6368 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6369 struct got_repository *repo,
6370 got_worktree_checkout_cb progress_cb, void *progress_arg,
6371 got_cancel_cb cancel_cb, void *cancel_arg)
6373 const struct got_error *err;
6374 char *commit_ref_name;
6376 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6377 if (err)
6378 return err;
6380 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6381 if (err)
6382 goto done;
6384 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6385 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6386 progress_arg, cancel_cb, cancel_arg);
6387 done:
6388 free(commit_ref_name);
6389 return err;
6392 const struct got_error *
6393 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6394 struct got_worktree *worktree, struct got_fileindex *fileindex,
6395 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6396 struct got_repository *repo,
6397 got_worktree_checkout_cb progress_cb, void *progress_arg,
6398 got_cancel_cb cancel_cb, void *cancel_arg)
6400 const struct got_error *err;
6401 char *commit_ref_name;
6403 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6404 if (err)
6405 return err;
6407 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6408 if (err)
6409 goto done;
6411 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6412 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6413 progress_arg, cancel_cb, cancel_arg);
6414 done:
6415 free(commit_ref_name);
6416 return err;
6419 static const struct got_error *
6420 rebase_commit(struct got_object_id **new_commit_id,
6421 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6422 struct got_worktree *worktree, struct got_fileindex *fileindex,
6423 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6424 const char *new_logmsg, struct got_repository *repo)
6426 const struct got_error *err, *sync_err;
6427 struct got_pathlist_head commitable_paths;
6428 struct collect_commitables_arg cc_arg;
6429 char *fileindex_path = NULL;
6430 struct got_reference *head_ref = NULL;
6431 struct got_object_id *head_commit_id = NULL;
6432 char *logmsg = NULL;
6434 TAILQ_INIT(&commitable_paths);
6435 *new_commit_id = NULL;
6437 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6439 err = get_fileindex_path(&fileindex_path, worktree);
6440 if (err)
6441 return err;
6443 cc_arg.commitable_paths = &commitable_paths;
6444 cc_arg.worktree = worktree;
6445 cc_arg.repo = repo;
6446 cc_arg.have_staged_files = 0;
6448 * If possible get the status of individual files directly to
6449 * avoid crawling the entire work tree once per rebased commit.
6451 * Ideally, merged_paths would contain a list of commitables
6452 * we could use so we could skip worktree_status() entirely.
6453 * However, we would then need carefully keep track of cumulative
6454 * effects of operations such as file additions and deletions
6455 * in 'got histedit -f' (folding multiple commits into one),
6456 * and this extra complexity is not really worth it.
6458 if (merged_paths) {
6459 struct got_pathlist_entry *pe;
6460 TAILQ_FOREACH(pe, merged_paths, entry) {
6461 err = worktree_status(worktree, pe->path, fileindex,
6462 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6463 0);
6464 if (err)
6465 goto done;
6467 } else {
6468 err = worktree_status(worktree, "", fileindex, repo,
6469 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6470 if (err)
6471 goto done;
6474 if (TAILQ_EMPTY(&commitable_paths)) {
6475 /* No-op change; commit will be elided. */
6476 err = got_ref_delete(commit_ref, repo);
6477 if (err)
6478 goto done;
6479 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6480 goto done;
6483 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6484 if (err)
6485 goto done;
6487 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6488 if (err)
6489 goto done;
6491 if (new_logmsg) {
6492 logmsg = strdup(new_logmsg);
6493 if (logmsg == NULL) {
6494 err = got_error_from_errno("strdup");
6495 goto done;
6497 } else {
6498 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6499 if (err)
6500 goto done;
6503 /* NB: commit_worktree will call free(logmsg) */
6504 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6505 NULL, worktree, got_object_commit_get_author(orig_commit),
6506 got_object_commit_get_committer(orig_commit),
6507 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6508 if (err)
6509 goto done;
6511 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6512 if (err)
6513 goto done;
6515 err = got_ref_delete(commit_ref, repo);
6516 if (err)
6517 goto done;
6519 err = update_fileindex_after_commit(worktree, &commitable_paths,
6520 *new_commit_id, fileindex, 0);
6521 sync_err = sync_fileindex(fileindex, fileindex_path);
6522 if (sync_err && err == NULL)
6523 err = sync_err;
6524 done:
6525 free(fileindex_path);
6526 free(head_commit_id);
6527 if (head_ref)
6528 got_ref_close(head_ref);
6529 if (err) {
6530 free(*new_commit_id);
6531 *new_commit_id = NULL;
6533 return err;
6536 const struct got_error *
6537 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6538 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6539 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6540 struct got_commit_object *orig_commit,
6541 struct got_object_id *orig_commit_id, struct got_repository *repo)
6543 const struct got_error *err;
6544 char *commit_ref_name;
6545 struct got_reference *commit_ref = NULL;
6546 struct got_object_id *commit_id = NULL;
6548 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6549 if (err)
6550 return err;
6552 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6553 if (err)
6554 goto done;
6555 err = got_ref_resolve(&commit_id, repo, commit_ref);
6556 if (err)
6557 goto done;
6558 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6559 err = got_error(GOT_ERR_REBASE_COMMITID);
6560 goto done;
6563 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6564 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6565 done:
6566 if (commit_ref)
6567 got_ref_close(commit_ref);
6568 free(commit_ref_name);
6569 free(commit_id);
6570 return err;
6573 const struct got_error *
6574 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6575 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6576 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6577 struct got_commit_object *orig_commit,
6578 struct got_object_id *orig_commit_id, const char *new_logmsg,
6579 struct got_repository *repo)
6581 const struct got_error *err;
6582 char *commit_ref_name;
6583 struct got_reference *commit_ref = NULL;
6585 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6586 if (err)
6587 return err;
6589 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6590 if (err)
6591 goto done;
6593 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6594 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6595 done:
6596 if (commit_ref)
6597 got_ref_close(commit_ref);
6598 free(commit_ref_name);
6599 return err;
6602 const struct got_error *
6603 got_worktree_rebase_postpone(struct got_worktree *worktree,
6604 struct got_fileindex *fileindex)
6606 if (fileindex)
6607 got_fileindex_free(fileindex);
6608 return lock_worktree(worktree, LOCK_SH);
6611 static const struct got_error *
6612 delete_ref(const char *name, struct got_repository *repo)
6614 const struct got_error *err;
6615 struct got_reference *ref;
6617 err = got_ref_open(&ref, repo, name, 0);
6618 if (err) {
6619 if (err->code == GOT_ERR_NOT_REF)
6620 return NULL;
6621 return err;
6624 err = got_ref_delete(ref, repo);
6625 got_ref_close(ref);
6626 return err;
6629 static const struct got_error *
6630 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6632 const struct got_error *err;
6633 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6634 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6636 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6637 if (err)
6638 goto done;
6639 err = delete_ref(tmp_branch_name, repo);
6640 if (err)
6641 goto done;
6643 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6644 if (err)
6645 goto done;
6646 err = delete_ref(new_base_branch_ref_name, repo);
6647 if (err)
6648 goto done;
6650 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6651 if (err)
6652 goto done;
6653 err = delete_ref(branch_ref_name, repo);
6654 if (err)
6655 goto done;
6657 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6658 if (err)
6659 goto done;
6660 err = delete_ref(commit_ref_name, repo);
6661 if (err)
6662 goto done;
6664 done:
6665 free(tmp_branch_name);
6666 free(new_base_branch_ref_name);
6667 free(branch_ref_name);
6668 free(commit_ref_name);
6669 return err;
6672 const struct got_error *
6673 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6674 struct got_object_id *new_commit_id, struct got_repository *repo)
6676 const struct got_error *err;
6677 struct got_reference *ref = NULL;
6678 struct got_object_id *old_commit_id = NULL;
6679 const char *branch_name = NULL;
6680 char *new_id_str = NULL;
6681 char *refname = NULL;
6683 branch_name = got_ref_get_name(branch);
6684 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6685 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6686 branch_name += 11;
6688 err = got_object_id_str(&new_id_str, new_commit_id);
6689 if (err)
6690 return err;
6692 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6693 new_id_str) == -1) {
6694 err = got_error_from_errno("asprintf");
6695 goto done;
6698 err = got_ref_resolve(&old_commit_id, repo, branch);
6699 if (err)
6700 goto done;
6702 err = got_ref_alloc(&ref, refname, old_commit_id);
6703 if (err)
6704 goto done;
6706 err = got_ref_write(ref, repo);
6707 done:
6708 free(new_id_str);
6709 free(refname);
6710 free(old_commit_id);
6711 if (ref)
6712 got_ref_close(ref);
6713 return err;
6716 const struct got_error *
6717 got_worktree_rebase_complete(struct got_worktree *worktree,
6718 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6719 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6720 struct got_repository *repo, int create_backup)
6722 const struct got_error *err, *unlockerr, *sync_err;
6723 struct got_object_id *new_head_commit_id = NULL;
6724 char *fileindex_path = NULL;
6726 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6727 if (err)
6728 return err;
6730 if (create_backup) {
6731 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6732 rebased_branch, new_head_commit_id, repo);
6733 if (err)
6734 goto done;
6737 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6738 if (err)
6739 goto done;
6741 err = got_ref_write(rebased_branch, repo);
6742 if (err)
6743 goto done;
6745 err = got_worktree_set_head_ref(worktree, rebased_branch);
6746 if (err)
6747 goto done;
6749 err = delete_rebase_refs(worktree, repo);
6750 if (err)
6751 goto done;
6753 err = get_fileindex_path(&fileindex_path, worktree);
6754 if (err)
6755 goto done;
6756 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6757 sync_err = sync_fileindex(fileindex, fileindex_path);
6758 if (sync_err && err == NULL)
6759 err = sync_err;
6760 done:
6761 got_fileindex_free(fileindex);
6762 free(fileindex_path);
6763 free(new_head_commit_id);
6764 unlockerr = lock_worktree(worktree, LOCK_SH);
6765 if (unlockerr && err == NULL)
6766 err = unlockerr;
6767 return err;
6770 const struct got_error *
6771 got_worktree_rebase_abort(struct got_worktree *worktree,
6772 struct got_fileindex *fileindex, struct got_repository *repo,
6773 struct got_reference *new_base_branch,
6774 got_worktree_checkout_cb progress_cb, void *progress_arg)
6776 const struct got_error *err, *unlockerr, *sync_err;
6777 struct got_reference *resolved = NULL;
6778 struct got_object_id *commit_id = NULL;
6779 char *fileindex_path = NULL;
6780 struct revert_file_args rfa;
6781 struct got_object_id *tree_id = NULL;
6783 err = lock_worktree(worktree, LOCK_EX);
6784 if (err)
6785 return err;
6787 err = got_ref_open(&resolved, repo,
6788 got_ref_get_symref_target(new_base_branch), 0);
6789 if (err)
6790 goto done;
6792 err = got_worktree_set_head_ref(worktree, resolved);
6793 if (err)
6794 goto done;
6797 * XXX commits to the base branch could have happened while
6798 * we were busy rebasing; should we store the original commit ID
6799 * when rebase begins and read it back here?
6801 err = got_ref_resolve(&commit_id, repo, resolved);
6802 if (err)
6803 goto done;
6805 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6806 if (err)
6807 goto done;
6809 err = got_object_id_by_path(&tree_id, repo,
6810 worktree->base_commit_id, worktree->path_prefix);
6811 if (err)
6812 goto done;
6814 err = delete_rebase_refs(worktree, repo);
6815 if (err)
6816 goto done;
6818 err = get_fileindex_path(&fileindex_path, worktree);
6819 if (err)
6820 goto done;
6822 rfa.worktree = worktree;
6823 rfa.fileindex = fileindex;
6824 rfa.progress_cb = progress_cb;
6825 rfa.progress_arg = progress_arg;
6826 rfa.patch_cb = NULL;
6827 rfa.patch_arg = NULL;
6828 rfa.repo = repo;
6829 rfa.unlink_added_files = 0;
6830 err = worktree_status(worktree, "", fileindex, repo,
6831 revert_file, &rfa, NULL, NULL, 0, 0);
6832 if (err)
6833 goto sync;
6835 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6836 repo, progress_cb, progress_arg, NULL, NULL);
6837 sync:
6838 sync_err = sync_fileindex(fileindex, fileindex_path);
6839 if (sync_err && err == NULL)
6840 err = sync_err;
6841 done:
6842 got_ref_close(resolved);
6843 free(tree_id);
6844 free(commit_id);
6845 if (fileindex)
6846 got_fileindex_free(fileindex);
6847 free(fileindex_path);
6849 unlockerr = lock_worktree(worktree, LOCK_SH);
6850 if (unlockerr && err == NULL)
6851 err = unlockerr;
6852 return err;
6855 const struct got_error *
6856 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6857 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6858 struct got_fileindex **fileindex, struct got_worktree *worktree,
6859 struct got_repository *repo)
6861 const struct got_error *err = NULL;
6862 char *tmp_branch_name = NULL;
6863 char *branch_ref_name = NULL;
6864 char *base_commit_ref_name = NULL;
6865 char *fileindex_path = NULL;
6866 struct check_rebase_ok_arg ok_arg;
6867 struct got_reference *wt_branch = NULL;
6868 struct got_reference *base_commit_ref = NULL;
6870 *tmp_branch = NULL;
6871 *branch_ref = NULL;
6872 *base_commit_id = NULL;
6873 *fileindex = NULL;
6875 err = lock_worktree(worktree, LOCK_EX);
6876 if (err)
6877 return err;
6879 err = open_fileindex(fileindex, &fileindex_path, worktree);
6880 if (err)
6881 goto done;
6883 ok_arg.worktree = worktree;
6884 ok_arg.repo = repo;
6885 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6886 &ok_arg);
6887 if (err)
6888 goto done;
6890 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6891 if (err)
6892 goto done;
6894 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6895 if (err)
6896 goto done;
6898 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6899 worktree);
6900 if (err)
6901 goto done;
6903 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6904 0);
6905 if (err)
6906 goto done;
6908 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6909 if (err)
6910 goto done;
6912 err = got_ref_write(*branch_ref, repo);
6913 if (err)
6914 goto done;
6916 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6917 worktree->base_commit_id);
6918 if (err)
6919 goto done;
6920 err = got_ref_write(base_commit_ref, repo);
6921 if (err)
6922 goto done;
6923 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6924 if (*base_commit_id == NULL) {
6925 err = got_error_from_errno("got_object_id_dup");
6926 goto done;
6929 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6930 worktree->base_commit_id);
6931 if (err)
6932 goto done;
6933 err = got_ref_write(*tmp_branch, repo);
6934 if (err)
6935 goto done;
6937 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6938 if (err)
6939 goto done;
6940 done:
6941 free(fileindex_path);
6942 free(tmp_branch_name);
6943 free(branch_ref_name);
6944 free(base_commit_ref_name);
6945 if (wt_branch)
6946 got_ref_close(wt_branch);
6947 if (err) {
6948 if (*branch_ref) {
6949 got_ref_close(*branch_ref);
6950 *branch_ref = NULL;
6952 if (*tmp_branch) {
6953 got_ref_close(*tmp_branch);
6954 *tmp_branch = NULL;
6956 free(*base_commit_id);
6957 if (*fileindex) {
6958 got_fileindex_free(*fileindex);
6959 *fileindex = NULL;
6961 lock_worktree(worktree, LOCK_SH);
6963 return err;
6966 const struct got_error *
6967 got_worktree_histedit_postpone(struct got_worktree *worktree,
6968 struct got_fileindex *fileindex)
6970 if (fileindex)
6971 got_fileindex_free(fileindex);
6972 return lock_worktree(worktree, LOCK_SH);
6975 const struct got_error *
6976 got_worktree_histedit_in_progress(int *in_progress,
6977 struct got_worktree *worktree)
6979 const struct got_error *err;
6980 char *tmp_branch_name = NULL;
6982 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6983 if (err)
6984 return err;
6986 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6987 free(tmp_branch_name);
6988 return NULL;
6991 const struct got_error *
6992 got_worktree_histedit_continue(struct got_object_id **commit_id,
6993 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6994 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6995 struct got_worktree *worktree, struct got_repository *repo)
6997 const struct got_error *err;
6998 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6999 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7000 struct got_reference *commit_ref = NULL;
7001 struct got_reference *base_commit_ref = NULL;
7002 char *fileindex_path = NULL;
7003 int have_staged_files = 0;
7005 *commit_id = NULL;
7006 *tmp_branch = NULL;
7007 *base_commit_id = NULL;
7008 *fileindex = NULL;
7010 err = lock_worktree(worktree, LOCK_EX);
7011 if (err)
7012 return err;
7014 err = open_fileindex(fileindex, &fileindex_path, worktree);
7015 if (err)
7016 goto done;
7018 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7019 &have_staged_files);
7020 if (err && err->code != GOT_ERR_CANCELLED)
7021 goto done;
7022 if (have_staged_files) {
7023 err = got_error(GOT_ERR_STAGED_PATHS);
7024 goto done;
7027 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7028 if (err)
7029 goto done;
7031 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7032 if (err)
7033 goto done;
7035 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7036 if (err)
7037 goto done;
7039 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7040 worktree);
7041 if (err)
7042 goto done;
7044 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7045 if (err)
7046 goto done;
7048 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7049 if (err)
7050 goto done;
7051 err = got_ref_resolve(commit_id, repo, commit_ref);
7052 if (err)
7053 goto done;
7055 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7056 if (err)
7057 goto done;
7058 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7059 if (err)
7060 goto done;
7062 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7063 if (err)
7064 goto done;
7065 done:
7066 free(commit_ref_name);
7067 free(branch_ref_name);
7068 free(fileindex_path);
7069 if (commit_ref)
7070 got_ref_close(commit_ref);
7071 if (base_commit_ref)
7072 got_ref_close(base_commit_ref);
7073 if (err) {
7074 free(*commit_id);
7075 *commit_id = NULL;
7076 free(*base_commit_id);
7077 *base_commit_id = NULL;
7078 if (*tmp_branch) {
7079 got_ref_close(*tmp_branch);
7080 *tmp_branch = NULL;
7082 if (*fileindex) {
7083 got_fileindex_free(*fileindex);
7084 *fileindex = NULL;
7086 lock_worktree(worktree, LOCK_EX);
7088 return err;
7091 static const struct got_error *
7092 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7094 const struct got_error *err;
7095 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7096 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7098 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7099 if (err)
7100 goto done;
7101 err = delete_ref(tmp_branch_name, repo);
7102 if (err)
7103 goto done;
7105 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7106 worktree);
7107 if (err)
7108 goto done;
7109 err = delete_ref(base_commit_ref_name, repo);
7110 if (err)
7111 goto done;
7113 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7114 if (err)
7115 goto done;
7116 err = delete_ref(branch_ref_name, repo);
7117 if (err)
7118 goto done;
7120 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7121 if (err)
7122 goto done;
7123 err = delete_ref(commit_ref_name, repo);
7124 if (err)
7125 goto done;
7126 done:
7127 free(tmp_branch_name);
7128 free(base_commit_ref_name);
7129 free(branch_ref_name);
7130 free(commit_ref_name);
7131 return err;
7134 const struct got_error *
7135 got_worktree_histedit_abort(struct got_worktree *worktree,
7136 struct got_fileindex *fileindex, struct got_repository *repo,
7137 struct got_reference *branch, struct got_object_id *base_commit_id,
7138 got_worktree_checkout_cb progress_cb, void *progress_arg)
7140 const struct got_error *err, *unlockerr, *sync_err;
7141 struct got_reference *resolved = NULL;
7142 char *fileindex_path = NULL;
7143 struct got_object_id *tree_id = NULL;
7144 struct revert_file_args rfa;
7146 err = lock_worktree(worktree, LOCK_EX);
7147 if (err)
7148 return err;
7150 err = got_ref_open(&resolved, repo,
7151 got_ref_get_symref_target(branch), 0);
7152 if (err)
7153 goto done;
7155 err = got_worktree_set_head_ref(worktree, resolved);
7156 if (err)
7157 goto done;
7159 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7160 if (err)
7161 goto done;
7163 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7164 worktree->path_prefix);
7165 if (err)
7166 goto done;
7168 err = delete_histedit_refs(worktree, repo);
7169 if (err)
7170 goto done;
7172 err = get_fileindex_path(&fileindex_path, worktree);
7173 if (err)
7174 goto done;
7176 rfa.worktree = worktree;
7177 rfa.fileindex = fileindex;
7178 rfa.progress_cb = progress_cb;
7179 rfa.progress_arg = progress_arg;
7180 rfa.patch_cb = NULL;
7181 rfa.patch_arg = NULL;
7182 rfa.repo = repo;
7183 rfa.unlink_added_files = 0;
7184 err = worktree_status(worktree, "", fileindex, repo,
7185 revert_file, &rfa, NULL, NULL, 0, 0);
7186 if (err)
7187 goto sync;
7189 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7190 repo, progress_cb, progress_arg, NULL, NULL);
7191 sync:
7192 sync_err = sync_fileindex(fileindex, fileindex_path);
7193 if (sync_err && err == NULL)
7194 err = sync_err;
7195 done:
7196 got_ref_close(resolved);
7197 free(tree_id);
7198 free(fileindex_path);
7200 unlockerr = lock_worktree(worktree, LOCK_SH);
7201 if (unlockerr && err == NULL)
7202 err = unlockerr;
7203 return err;
7206 const struct got_error *
7207 got_worktree_histedit_complete(struct got_worktree *worktree,
7208 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7209 struct got_reference *edited_branch, struct got_repository *repo)
7211 const struct got_error *err, *unlockerr, *sync_err;
7212 struct got_object_id *new_head_commit_id = NULL;
7213 struct got_reference *resolved = NULL;
7214 char *fileindex_path = NULL;
7216 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7217 if (err)
7218 return err;
7220 err = got_ref_open(&resolved, repo,
7221 got_ref_get_symref_target(edited_branch), 0);
7222 if (err)
7223 goto done;
7225 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7226 resolved, new_head_commit_id, repo);
7227 if (err)
7228 goto done;
7230 err = got_ref_change_ref(resolved, new_head_commit_id);
7231 if (err)
7232 goto done;
7234 err = got_ref_write(resolved, repo);
7235 if (err)
7236 goto done;
7238 err = got_worktree_set_head_ref(worktree, resolved);
7239 if (err)
7240 goto done;
7242 err = delete_histedit_refs(worktree, repo);
7243 if (err)
7244 goto done;
7246 err = get_fileindex_path(&fileindex_path, worktree);
7247 if (err)
7248 goto done;
7249 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7250 sync_err = sync_fileindex(fileindex, fileindex_path);
7251 if (sync_err && err == NULL)
7252 err = sync_err;
7253 done:
7254 got_fileindex_free(fileindex);
7255 free(fileindex_path);
7256 free(new_head_commit_id);
7257 unlockerr = lock_worktree(worktree, LOCK_SH);
7258 if (unlockerr && err == NULL)
7259 err = unlockerr;
7260 return err;
7263 const struct got_error *
7264 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7265 struct got_object_id *commit_id, struct got_repository *repo)
7267 const struct got_error *err;
7268 char *commit_ref_name;
7270 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7271 if (err)
7272 return err;
7274 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7275 if (err)
7276 goto done;
7278 err = delete_ref(commit_ref_name, repo);
7279 done:
7280 free(commit_ref_name);
7281 return err;
7284 const struct got_error *
7285 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7286 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7287 struct got_worktree *worktree, const char *refname,
7288 struct got_repository *repo)
7290 const struct got_error *err = NULL;
7291 char *fileindex_path = NULL;
7292 struct check_rebase_ok_arg ok_arg;
7294 *fileindex = NULL;
7295 *branch_ref = NULL;
7296 *base_branch_ref = NULL;
7298 err = lock_worktree(worktree, LOCK_EX);
7299 if (err)
7300 return err;
7302 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7303 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7304 "cannot integrate a branch into itself; "
7305 "update -b or different branch name required");
7306 goto done;
7309 err = open_fileindex(fileindex, &fileindex_path, worktree);
7310 if (err)
7311 goto done;
7313 /* Preconditions are the same as for rebase. */
7314 ok_arg.worktree = worktree;
7315 ok_arg.repo = repo;
7316 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7317 &ok_arg);
7318 if (err)
7319 goto done;
7321 err = got_ref_open(branch_ref, repo, refname, 1);
7322 if (err)
7323 goto done;
7325 err = got_ref_open(base_branch_ref, repo,
7326 got_worktree_get_head_ref_name(worktree), 1);
7327 done:
7328 if (err) {
7329 if (*branch_ref) {
7330 got_ref_close(*branch_ref);
7331 *branch_ref = NULL;
7333 if (*base_branch_ref) {
7334 got_ref_close(*base_branch_ref);
7335 *base_branch_ref = NULL;
7337 if (*fileindex) {
7338 got_fileindex_free(*fileindex);
7339 *fileindex = NULL;
7341 lock_worktree(worktree, LOCK_SH);
7343 return err;
7346 const struct got_error *
7347 got_worktree_integrate_continue(struct got_worktree *worktree,
7348 struct got_fileindex *fileindex, struct got_repository *repo,
7349 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7350 got_worktree_checkout_cb progress_cb, void *progress_arg,
7351 got_cancel_cb cancel_cb, void *cancel_arg)
7353 const struct got_error *err = NULL, *sync_err, *unlockerr;
7354 char *fileindex_path = NULL;
7355 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7357 err = get_fileindex_path(&fileindex_path, worktree);
7358 if (err)
7359 goto done;
7361 err = got_ref_resolve(&commit_id, repo, branch_ref);
7362 if (err)
7363 goto done;
7365 err = got_object_id_by_path(&tree_id, repo, commit_id,
7366 worktree->path_prefix);
7367 if (err)
7368 goto done;
7370 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7371 if (err)
7372 goto done;
7374 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7375 progress_cb, progress_arg, cancel_cb, cancel_arg);
7376 if (err)
7377 goto sync;
7379 err = got_ref_change_ref(base_branch_ref, commit_id);
7380 if (err)
7381 goto sync;
7383 err = got_ref_write(base_branch_ref, repo);
7384 if (err)
7385 goto sync;
7387 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7388 sync:
7389 sync_err = sync_fileindex(fileindex, fileindex_path);
7390 if (sync_err && err == NULL)
7391 err = sync_err;
7393 done:
7394 unlockerr = got_ref_unlock(branch_ref);
7395 if (unlockerr && err == NULL)
7396 err = unlockerr;
7397 got_ref_close(branch_ref);
7399 unlockerr = got_ref_unlock(base_branch_ref);
7400 if (unlockerr && err == NULL)
7401 err = unlockerr;
7402 got_ref_close(base_branch_ref);
7404 got_fileindex_free(fileindex);
7405 free(fileindex_path);
7406 free(tree_id);
7408 unlockerr = lock_worktree(worktree, LOCK_SH);
7409 if (unlockerr && err == NULL)
7410 err = unlockerr;
7411 return err;
7414 const struct got_error *
7415 got_worktree_integrate_abort(struct got_worktree *worktree,
7416 struct got_fileindex *fileindex, struct got_repository *repo,
7417 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7419 const struct got_error *err = NULL, *unlockerr = NULL;
7421 got_fileindex_free(fileindex);
7423 err = lock_worktree(worktree, LOCK_SH);
7425 unlockerr = got_ref_unlock(branch_ref);
7426 if (unlockerr && err == NULL)
7427 err = unlockerr;
7428 got_ref_close(branch_ref);
7430 unlockerr = got_ref_unlock(base_branch_ref);
7431 if (unlockerr && err == NULL)
7432 err = unlockerr;
7433 got_ref_close(base_branch_ref);
7435 return err;
7438 const struct got_error *
7439 got_worktree_merge_postpone(struct got_worktree *worktree,
7440 struct got_fileindex *fileindex)
7442 const struct got_error *err, *sync_err;
7443 char *fileindex_path = NULL;
7445 err = get_fileindex_path(&fileindex_path, worktree);
7446 if (err)
7447 goto done;
7449 sync_err = sync_fileindex(fileindex, fileindex_path);
7451 err = lock_worktree(worktree, LOCK_SH);
7452 if (sync_err && err == NULL)
7453 err = sync_err;
7454 done:
7455 got_fileindex_free(fileindex);
7456 free(fileindex_path);
7457 return err;
7460 static const struct got_error *
7461 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7463 const struct got_error *err;
7464 char *branch_refname = NULL, *commit_refname = NULL;
7466 err = get_merge_branch_ref_name(&branch_refname, worktree);
7467 if (err)
7468 goto done;
7469 err = delete_ref(branch_refname, repo);
7470 if (err)
7471 goto done;
7473 err = get_merge_commit_ref_name(&commit_refname, worktree);
7474 if (err)
7475 goto done;
7476 err = delete_ref(commit_refname, repo);
7477 if (err)
7478 goto done;
7480 done:
7481 free(branch_refname);
7482 free(commit_refname);
7483 return err;
7486 struct merge_commit_msg_arg {
7487 struct got_worktree *worktree;
7488 const char *branch_name;
7491 static const struct got_error *
7492 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7493 void *arg)
7495 struct merge_commit_msg_arg *a = arg;
7497 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7498 got_worktree_get_head_ref_name(a->worktree)) == -1)
7499 return got_error_from_errno("asprintf");
7501 return NULL;
7505 const struct got_error *
7506 got_worktree_merge_branch(struct got_worktree *worktree,
7507 struct got_fileindex *fileindex,
7508 struct got_object_id *yca_commit_id,
7509 struct got_object_id *branch_tip,
7510 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7511 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7513 const struct got_error *err;
7514 char *fileindex_path = NULL;
7516 err = get_fileindex_path(&fileindex_path, worktree);
7517 if (err)
7518 goto done;
7520 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7521 worktree);
7522 if (err)
7523 goto done;
7525 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7526 branch_tip, repo, progress_cb, progress_arg,
7527 cancel_cb, cancel_arg);
7528 done:
7529 free(fileindex_path);
7530 return err;
7533 const struct got_error *
7534 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7535 struct got_worktree *worktree, struct got_fileindex *fileindex,
7536 const char *author, const char *committer, int allow_bad_symlinks,
7537 struct got_object_id *branch_tip, const char *branch_name,
7538 struct got_repository *repo,
7539 got_worktree_status_cb status_cb, void *status_arg)
7542 const struct got_error *err = NULL, *sync_err;
7543 struct got_pathlist_head commitable_paths;
7544 struct collect_commitables_arg cc_arg;
7545 struct got_pathlist_entry *pe;
7546 struct got_reference *head_ref = NULL;
7547 struct got_object_id *head_commit_id = NULL;
7548 int have_staged_files = 0;
7549 struct merge_commit_msg_arg mcm_arg;
7550 char *fileindex_path = NULL;
7552 *new_commit_id = NULL;
7554 TAILQ_INIT(&commitable_paths);
7556 err = get_fileindex_path(&fileindex_path, worktree);
7557 if (err)
7558 goto done;
7560 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7561 if (err)
7562 goto done;
7564 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7565 if (err)
7566 goto done;
7568 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7569 &have_staged_files);
7570 if (err && err->code != GOT_ERR_CANCELLED)
7571 goto done;
7572 if (have_staged_files) {
7573 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7574 goto done;
7577 cc_arg.commitable_paths = &commitable_paths;
7578 cc_arg.worktree = worktree;
7579 cc_arg.fileindex = fileindex;
7580 cc_arg.repo = repo;
7581 cc_arg.have_staged_files = have_staged_files;
7582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7583 err = worktree_status(worktree, "", fileindex, repo,
7584 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7585 if (err)
7586 goto done;
7588 if (TAILQ_EMPTY(&commitable_paths)) {
7589 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7590 "merge of %s cannot proceed", branch_name);
7591 goto done;
7594 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7595 struct got_commitable *ct = pe->data;
7596 const char *ct_path = ct->in_repo_path;
7598 while (ct_path[0] == '/')
7599 ct_path++;
7600 err = check_out_of_date(ct_path, ct->status,
7601 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7602 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7603 if (err)
7604 goto done;
7608 mcm_arg.worktree = worktree;
7609 mcm_arg.branch_name = branch_name;
7610 err = commit_worktree(new_commit_id, &commitable_paths,
7611 head_commit_id, branch_tip, worktree, author, committer,
7612 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7613 if (err)
7614 goto done;
7616 err = update_fileindex_after_commit(worktree, &commitable_paths,
7617 *new_commit_id, fileindex, have_staged_files);
7618 sync_err = sync_fileindex(fileindex, fileindex_path);
7619 if (sync_err && err == NULL)
7620 err = sync_err;
7621 done:
7622 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7623 struct got_commitable *ct = pe->data;
7624 free_commitable(ct);
7626 got_pathlist_free(&commitable_paths);
7627 free(fileindex_path);
7628 return err;
7631 const struct got_error *
7632 got_worktree_merge_complete(struct got_worktree *worktree,
7633 struct got_fileindex *fileindex, struct got_repository *repo)
7635 const struct got_error *err, *unlockerr, *sync_err;
7636 char *fileindex_path = NULL;
7638 err = delete_merge_refs(worktree, repo);
7639 if (err)
7640 goto done;
7642 err = get_fileindex_path(&fileindex_path, worktree);
7643 if (err)
7644 goto done;
7645 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7646 sync_err = sync_fileindex(fileindex, fileindex_path);
7647 if (sync_err && err == NULL)
7648 err = sync_err;
7649 done:
7650 got_fileindex_free(fileindex);
7651 free(fileindex_path);
7652 unlockerr = lock_worktree(worktree, LOCK_SH);
7653 if (unlockerr && err == NULL)
7654 err = unlockerr;
7655 return err;
7658 const struct got_error *
7659 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7660 struct got_repository *repo)
7662 const struct got_error *err;
7663 char *branch_refname = NULL;
7664 struct got_reference *branch_ref = NULL;
7666 *in_progress = 0;
7668 err = get_merge_branch_ref_name(&branch_refname, worktree);
7669 if (err)
7670 return err;
7671 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7672 free(branch_refname);
7673 if (err) {
7674 if (err->code != GOT_ERR_NOT_REF)
7675 return err;
7676 } else
7677 *in_progress = 1;
7679 return NULL;
7682 const struct got_error *got_worktree_merge_prepare(
7683 struct got_fileindex **fileindex, struct got_worktree *worktree,
7684 struct got_reference *branch, struct got_repository *repo)
7686 const struct got_error *err = NULL;
7687 char *fileindex_path = NULL;
7688 char *branch_refname = NULL, *commit_refname = NULL;
7689 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7690 struct got_reference *commit_ref = NULL;
7691 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7692 struct check_rebase_ok_arg ok_arg;
7694 *fileindex = NULL;
7696 err = lock_worktree(worktree, LOCK_EX);
7697 if (err)
7698 return err;
7700 err = open_fileindex(fileindex, &fileindex_path, worktree);
7701 if (err)
7702 goto done;
7704 /* Preconditions are the same as for rebase. */
7705 ok_arg.worktree = worktree;
7706 ok_arg.repo = repo;
7707 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7708 &ok_arg);
7709 if (err)
7710 goto done;
7712 err = get_merge_branch_ref_name(&branch_refname, worktree);
7713 if (err)
7714 return err;
7716 err = get_merge_commit_ref_name(&commit_refname, worktree);
7717 if (err)
7718 return err;
7720 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7721 0);
7722 if (err)
7723 goto done;
7725 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7726 if (err)
7727 goto done;
7729 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7730 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7731 goto done;
7734 err = got_ref_resolve(&branch_tip, repo, branch);
7735 if (err)
7736 goto done;
7738 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7739 if (err)
7740 goto done;
7741 err = got_ref_write(branch_ref, repo);
7742 if (err)
7743 goto done;
7745 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7746 if (err)
7747 goto done;
7748 err = got_ref_write(commit_ref, repo);
7749 if (err)
7750 goto done;
7752 done:
7753 free(branch_refname);
7754 free(commit_refname);
7755 free(fileindex_path);
7756 if (branch_ref)
7757 got_ref_close(branch_ref);
7758 if (commit_ref)
7759 got_ref_close(commit_ref);
7760 if (wt_branch)
7761 got_ref_close(wt_branch);
7762 free(wt_branch_tip);
7763 if (err) {
7764 if (*fileindex) {
7765 got_fileindex_free(*fileindex);
7766 *fileindex = NULL;
7768 lock_worktree(worktree, LOCK_SH);
7770 return err;
7773 const struct got_error *
7774 got_worktree_merge_continue(char **branch_name,
7775 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7776 struct got_worktree *worktree, struct got_repository *repo)
7778 const struct got_error *err;
7779 char *commit_refname = NULL, *branch_refname = NULL;
7780 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7781 char *fileindex_path = NULL;
7782 int have_staged_files = 0;
7784 *branch_name = NULL;
7785 *branch_tip = NULL;
7786 *fileindex = NULL;
7788 err = lock_worktree(worktree, LOCK_EX);
7789 if (err)
7790 return err;
7792 err = open_fileindex(fileindex, &fileindex_path, worktree);
7793 if (err)
7794 goto done;
7796 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7797 &have_staged_files);
7798 if (err && err->code != GOT_ERR_CANCELLED)
7799 goto done;
7800 if (have_staged_files) {
7801 err = got_error(GOT_ERR_STAGED_PATHS);
7802 goto done;
7805 err = get_merge_branch_ref_name(&branch_refname, worktree);
7806 if (err)
7807 goto done;
7809 err = get_merge_commit_ref_name(&commit_refname, worktree);
7810 if (err)
7811 goto done;
7813 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7814 if (err)
7815 goto done;
7817 if (!got_ref_is_symbolic(branch_ref)) {
7818 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7819 "%s is not a symbolic reference",
7820 got_ref_get_name(branch_ref));
7821 goto done;
7823 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7824 if (*branch_name == NULL) {
7825 err = got_error_from_errno("strdup");
7826 goto done;
7829 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7830 if (err)
7831 goto done;
7833 err = got_ref_resolve(branch_tip, repo, commit_ref);
7834 if (err)
7835 goto done;
7836 done:
7837 free(commit_refname);
7838 free(branch_refname);
7839 free(fileindex_path);
7840 if (commit_ref)
7841 got_ref_close(commit_ref);
7842 if (branch_ref)
7843 got_ref_close(branch_ref);
7844 if (err) {
7845 if (*branch_name) {
7846 free(*branch_name);
7847 *branch_name = NULL;
7849 free(*branch_tip);
7850 *branch_tip = NULL;
7851 if (*fileindex) {
7852 got_fileindex_free(*fileindex);
7853 *fileindex = NULL;
7855 lock_worktree(worktree, LOCK_SH);
7857 return err;
7860 const struct got_error *
7861 got_worktree_merge_abort(struct got_worktree *worktree,
7862 struct got_fileindex *fileindex, struct got_repository *repo,
7863 got_worktree_checkout_cb progress_cb, void *progress_arg)
7865 const struct got_error *err, *unlockerr, *sync_err;
7866 struct got_object_id *commit_id = NULL;
7867 char *fileindex_path = NULL;
7868 struct revert_file_args rfa;
7869 struct got_object_id *tree_id = NULL;
7871 err = got_object_id_by_path(&tree_id, repo,
7872 worktree->base_commit_id, worktree->path_prefix);
7873 if (err)
7874 goto done;
7876 err = delete_merge_refs(worktree, repo);
7877 if (err)
7878 goto done;
7880 err = get_fileindex_path(&fileindex_path, worktree);
7881 if (err)
7882 goto done;
7884 rfa.worktree = worktree;
7885 rfa.fileindex = fileindex;
7886 rfa.progress_cb = progress_cb;
7887 rfa.progress_arg = progress_arg;
7888 rfa.patch_cb = NULL;
7889 rfa.patch_arg = NULL;
7890 rfa.repo = repo;
7891 rfa.unlink_added_files = 1;
7892 err = worktree_status(worktree, "", fileindex, repo,
7893 revert_file, &rfa, NULL, NULL, 0, 0);
7894 if (err)
7895 goto sync;
7897 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7898 repo, progress_cb, progress_arg, NULL, NULL);
7899 sync:
7900 sync_err = sync_fileindex(fileindex, fileindex_path);
7901 if (sync_err && err == NULL)
7902 err = sync_err;
7903 done:
7904 free(tree_id);
7905 free(commit_id);
7906 if (fileindex)
7907 got_fileindex_free(fileindex);
7908 free(fileindex_path);
7910 unlockerr = lock_worktree(worktree, LOCK_SH);
7911 if (unlockerr && err == NULL)
7912 err = unlockerr;
7913 return err;
7916 struct check_stage_ok_arg {
7917 struct got_object_id *head_commit_id;
7918 struct got_worktree *worktree;
7919 struct got_fileindex *fileindex;
7920 struct got_repository *repo;
7921 int have_changes;
7924 const struct got_error *
7925 check_stage_ok(void *arg, unsigned char status,
7926 unsigned char staged_status, const char *relpath,
7927 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7928 struct got_object_id *commit_id, int dirfd, const char *de_name)
7930 struct check_stage_ok_arg *a = arg;
7931 const struct got_error *err = NULL;
7932 struct got_fileindex_entry *ie;
7933 struct got_object_id base_commit_id;
7934 struct got_object_id *base_commit_idp = NULL;
7935 char *in_repo_path = NULL, *p;
7937 if (status == GOT_STATUS_UNVERSIONED ||
7938 status == GOT_STATUS_NO_CHANGE)
7939 return NULL;
7940 if (status == GOT_STATUS_NONEXISTENT)
7941 return got_error_set_errno(ENOENT, relpath);
7943 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7944 if (ie == NULL)
7945 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7947 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7948 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7949 relpath) == -1)
7950 return got_error_from_errno("asprintf");
7952 if (got_fileindex_entry_has_commit(ie)) {
7953 memcpy(base_commit_id.sha1, ie->commit_sha1,
7954 SHA1_DIGEST_LENGTH);
7955 base_commit_idp = &base_commit_id;
7958 if (status == GOT_STATUS_CONFLICT) {
7959 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7960 goto done;
7961 } else if (status != GOT_STATUS_ADD &&
7962 status != GOT_STATUS_MODIFY &&
7963 status != GOT_STATUS_DELETE) {
7964 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7965 goto done;
7968 a->have_changes = 1;
7970 p = in_repo_path;
7971 while (p[0] == '/')
7972 p++;
7973 err = check_out_of_date(p, status, staged_status,
7974 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7975 GOT_ERR_STAGE_OUT_OF_DATE);
7976 done:
7977 free(in_repo_path);
7978 return err;
7981 struct stage_path_arg {
7982 struct got_worktree *worktree;
7983 struct got_fileindex *fileindex;
7984 struct got_repository *repo;
7985 got_worktree_status_cb status_cb;
7986 void *status_arg;
7987 got_worktree_patch_cb patch_cb;
7988 void *patch_arg;
7989 int staged_something;
7990 int allow_bad_symlinks;
7993 static const struct got_error *
7994 stage_path(void *arg, unsigned char status,
7995 unsigned char staged_status, const char *relpath,
7996 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7997 struct got_object_id *commit_id, int dirfd, const char *de_name)
7999 struct stage_path_arg *a = arg;
8000 const struct got_error *err = NULL;
8001 struct got_fileindex_entry *ie;
8002 char *ondisk_path = NULL, *path_content = NULL;
8003 uint32_t stage;
8004 struct got_object_id *new_staged_blob_id = NULL;
8005 struct stat sb;
8007 if (status == GOT_STATUS_UNVERSIONED)
8008 return NULL;
8010 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8011 if (ie == NULL)
8012 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8015 relpath)== -1)
8016 return got_error_from_errno("asprintf");
8018 switch (status) {
8019 case GOT_STATUS_ADD:
8020 case GOT_STATUS_MODIFY:
8021 /* XXX could sb.st_mode be passed in by our caller? */
8022 if (lstat(ondisk_path, &sb) == -1) {
8023 err = got_error_from_errno2("lstat", ondisk_path);
8024 break;
8026 if (a->patch_cb) {
8027 if (status == GOT_STATUS_ADD) {
8028 int choice = GOT_PATCH_CHOICE_NONE;
8029 err = (*a->patch_cb)(&choice, a->patch_arg,
8030 status, ie->path, NULL, 1, 1);
8031 if (err)
8032 break;
8033 if (choice != GOT_PATCH_CHOICE_YES)
8034 break;
8035 } else {
8036 err = create_patched_content(&path_content, 0,
8037 staged_blob_id ? staged_blob_id : blob_id,
8038 ondisk_path, dirfd, de_name, ie->path,
8039 a->repo, a->patch_cb, a->patch_arg);
8040 if (err || path_content == NULL)
8041 break;
8044 err = got_object_blob_create(&new_staged_blob_id,
8045 path_content ? path_content : ondisk_path, a->repo);
8046 if (err)
8047 break;
8048 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8049 SHA1_DIGEST_LENGTH);
8050 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8051 stage = GOT_FILEIDX_STAGE_ADD;
8052 else
8053 stage = GOT_FILEIDX_STAGE_MODIFY;
8054 got_fileindex_entry_stage_set(ie, stage);
8055 if (S_ISLNK(sb.st_mode)) {
8056 int is_bad_symlink = 0;
8057 if (!a->allow_bad_symlinks) {
8058 char target_path[PATH_MAX];
8059 ssize_t target_len;
8060 target_len = readlink(ondisk_path, target_path,
8061 sizeof(target_path));
8062 if (target_len == -1) {
8063 err = got_error_from_errno2("readlink",
8064 ondisk_path);
8065 break;
8067 err = is_bad_symlink_target(&is_bad_symlink,
8068 target_path, target_len, ondisk_path,
8069 a->worktree->root_path);
8070 if (err)
8071 break;
8072 if (is_bad_symlink) {
8073 err = got_error_path(ondisk_path,
8074 GOT_ERR_BAD_SYMLINK);
8075 break;
8078 if (is_bad_symlink)
8079 got_fileindex_entry_staged_filetype_set(ie,
8080 GOT_FILEIDX_MODE_BAD_SYMLINK);
8081 else
8082 got_fileindex_entry_staged_filetype_set(ie,
8083 GOT_FILEIDX_MODE_SYMLINK);
8084 } else {
8085 got_fileindex_entry_staged_filetype_set(ie,
8086 GOT_FILEIDX_MODE_REGULAR_FILE);
8088 a->staged_something = 1;
8089 if (a->status_cb == NULL)
8090 break;
8091 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8092 get_staged_status(ie), relpath, blob_id,
8093 new_staged_blob_id, NULL, dirfd, de_name);
8094 break;
8095 case GOT_STATUS_DELETE:
8096 if (staged_status == GOT_STATUS_DELETE)
8097 break;
8098 if (a->patch_cb) {
8099 int choice = GOT_PATCH_CHOICE_NONE;
8100 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8101 ie->path, NULL, 1, 1);
8102 if (err)
8103 break;
8104 if (choice == GOT_PATCH_CHOICE_NO)
8105 break;
8106 if (choice != GOT_PATCH_CHOICE_YES) {
8107 err = got_error(GOT_ERR_PATCH_CHOICE);
8108 break;
8111 stage = GOT_FILEIDX_STAGE_DELETE;
8112 got_fileindex_entry_stage_set(ie, stage);
8113 a->staged_something = 1;
8114 if (a->status_cb == NULL)
8115 break;
8116 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8117 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8118 de_name);
8119 break;
8120 case GOT_STATUS_NO_CHANGE:
8121 break;
8122 case GOT_STATUS_CONFLICT:
8123 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8124 break;
8125 case GOT_STATUS_NONEXISTENT:
8126 err = got_error_set_errno(ENOENT, relpath);
8127 break;
8128 default:
8129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8130 break;
8133 if (path_content && unlink(path_content) == -1 && err == NULL)
8134 err = got_error_from_errno2("unlink", path_content);
8135 free(path_content);
8136 free(ondisk_path);
8137 free(new_staged_blob_id);
8138 return err;
8141 const struct got_error *
8142 got_worktree_stage(struct got_worktree *worktree,
8143 struct got_pathlist_head *paths,
8144 got_worktree_status_cb status_cb, void *status_arg,
8145 got_worktree_patch_cb patch_cb, void *patch_arg,
8146 int allow_bad_symlinks, struct got_repository *repo)
8148 const struct got_error *err = NULL, *sync_err, *unlockerr;
8149 struct got_pathlist_entry *pe;
8150 struct got_fileindex *fileindex = NULL;
8151 char *fileindex_path = NULL;
8152 struct got_reference *head_ref = NULL;
8153 struct got_object_id *head_commit_id = NULL;
8154 struct check_stage_ok_arg oka;
8155 struct stage_path_arg spa;
8157 err = lock_worktree(worktree, LOCK_EX);
8158 if (err)
8159 return err;
8161 err = got_ref_open(&head_ref, repo,
8162 got_worktree_get_head_ref_name(worktree), 0);
8163 if (err)
8164 goto done;
8165 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8166 if (err)
8167 goto done;
8168 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8169 if (err)
8170 goto done;
8172 /* Check pre-conditions before staging anything. */
8173 oka.head_commit_id = head_commit_id;
8174 oka.worktree = worktree;
8175 oka.fileindex = fileindex;
8176 oka.repo = repo;
8177 oka.have_changes = 0;
8178 TAILQ_FOREACH(pe, paths, entry) {
8179 err = worktree_status(worktree, pe->path, fileindex, repo,
8180 check_stage_ok, &oka, NULL, NULL, 0, 0);
8181 if (err)
8182 goto done;
8184 if (!oka.have_changes) {
8185 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8186 goto done;
8189 spa.worktree = worktree;
8190 spa.fileindex = fileindex;
8191 spa.repo = repo;
8192 spa.patch_cb = patch_cb;
8193 spa.patch_arg = patch_arg;
8194 spa.status_cb = status_cb;
8195 spa.status_arg = status_arg;
8196 spa.staged_something = 0;
8197 spa.allow_bad_symlinks = allow_bad_symlinks;
8198 TAILQ_FOREACH(pe, paths, entry) {
8199 err = worktree_status(worktree, pe->path, fileindex, repo,
8200 stage_path, &spa, NULL, NULL, 0, 0);
8201 if (err)
8202 goto done;
8204 if (!spa.staged_something) {
8205 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8206 goto done;
8209 sync_err = sync_fileindex(fileindex, fileindex_path);
8210 if (sync_err && err == NULL)
8211 err = sync_err;
8212 done:
8213 if (head_ref)
8214 got_ref_close(head_ref);
8215 free(head_commit_id);
8216 free(fileindex_path);
8217 if (fileindex)
8218 got_fileindex_free(fileindex);
8219 unlockerr = lock_worktree(worktree, LOCK_SH);
8220 if (unlockerr && err == NULL)
8221 err = unlockerr;
8222 return err;
8225 struct unstage_path_arg {
8226 struct got_worktree *worktree;
8227 struct got_fileindex *fileindex;
8228 struct got_repository *repo;
8229 got_worktree_checkout_cb progress_cb;
8230 void *progress_arg;
8231 got_worktree_patch_cb patch_cb;
8232 void *patch_arg;
8235 static const struct got_error *
8236 create_unstaged_content(char **path_unstaged_content,
8237 char **path_new_staged_content, struct got_object_id *blob_id,
8238 struct got_object_id *staged_blob_id, const char *relpath,
8239 struct got_repository *repo,
8240 got_worktree_patch_cb patch_cb, void *patch_arg)
8242 const struct got_error *err, *free_err;
8243 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8244 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8245 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8246 struct got_diffreg_result *diffreg_result = NULL;
8247 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8248 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8250 *path_unstaged_content = NULL;
8251 *path_new_staged_content = NULL;
8253 err = got_object_id_str(&label1, blob_id);
8254 if (err)
8255 return err;
8256 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8257 if (err)
8258 goto done;
8260 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8261 if (err)
8262 goto done;
8264 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8265 if (err)
8266 goto done;
8268 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8269 if (err)
8270 goto done;
8272 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8273 if (err)
8274 goto done;
8276 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8277 if (err)
8278 goto done;
8280 err = got_diff_files(&diffreg_result, f1, label1, f2,
8281 path2, 3, 0, 1, NULL);
8282 if (err)
8283 goto done;
8285 err = got_opentemp_named(path_unstaged_content, &outfile,
8286 "got-unstaged-content");
8287 if (err)
8288 goto done;
8289 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8290 "got-new-staged-content");
8291 if (err)
8292 goto done;
8294 if (fseek(f1, 0L, SEEK_SET) == -1) {
8295 err = got_ferror(f1, GOT_ERR_IO);
8296 goto done;
8298 if (fseek(f2, 0L, SEEK_SET) == -1) {
8299 err = got_ferror(f2, GOT_ERR_IO);
8300 goto done;
8302 /* Count the number of actual changes in the diff result. */
8303 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8304 struct diff_chunk_context cc = {};
8305 diff_chunk_context_load_change(&cc, &nchunks_used,
8306 diffreg_result->result, n, 0);
8307 nchanges++;
8309 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8310 int choice;
8311 err = apply_or_reject_change(&choice, &nchunks_used,
8312 diffreg_result->result, n, relpath, f1, f2,
8313 &line_cur1, &line_cur2,
8314 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8315 if (err)
8316 goto done;
8317 if (choice == GOT_PATCH_CHOICE_YES)
8318 have_content = 1;
8319 else
8320 have_rejected_content = 1;
8321 if (choice == GOT_PATCH_CHOICE_QUIT)
8322 break;
8324 if (have_content || have_rejected_content)
8325 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8326 outfile, rejectfile);
8327 done:
8328 free(label1);
8329 if (blob)
8330 got_object_blob_close(blob);
8331 if (staged_blob)
8332 got_object_blob_close(staged_blob);
8333 free_err = got_diffreg_result_free(diffreg_result);
8334 if (free_err && err == NULL)
8335 err = free_err;
8336 if (f1 && fclose(f1) == EOF && err == NULL)
8337 err = got_error_from_errno2("fclose", path1);
8338 if (f2 && fclose(f2) == EOF && err == NULL)
8339 err = got_error_from_errno2("fclose", path2);
8340 if (outfile && fclose(outfile) == EOF && err == NULL)
8341 err = got_error_from_errno2("fclose", *path_unstaged_content);
8342 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8343 err = got_error_from_errno2("fclose", *path_new_staged_content);
8344 if (path1 && unlink(path1) == -1 && err == NULL)
8345 err = got_error_from_errno2("unlink", path1);
8346 if (path2 && unlink(path2) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink", path2);
8348 if (err || !have_content) {
8349 if (*path_unstaged_content &&
8350 unlink(*path_unstaged_content) == -1 && err == NULL)
8351 err = got_error_from_errno2("unlink",
8352 *path_unstaged_content);
8353 free(*path_unstaged_content);
8354 *path_unstaged_content = NULL;
8356 if (err || !have_content || !have_rejected_content) {
8357 if (*path_new_staged_content &&
8358 unlink(*path_new_staged_content) == -1 && err == NULL)
8359 err = got_error_from_errno2("unlink",
8360 *path_new_staged_content);
8361 free(*path_new_staged_content);
8362 *path_new_staged_content = NULL;
8364 free(path1);
8365 free(path2);
8366 return err;
8369 static const struct got_error *
8370 unstage_hunks(struct got_object_id *staged_blob_id,
8371 struct got_blob_object *blob_base,
8372 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8373 const char *ondisk_path, const char *label_orig,
8374 struct got_worktree *worktree, struct got_repository *repo,
8375 got_worktree_patch_cb patch_cb, void *patch_arg,
8376 got_worktree_checkout_cb progress_cb, void *progress_arg)
8378 const struct got_error *err = NULL;
8379 char *path_unstaged_content = NULL;
8380 char *path_new_staged_content = NULL;
8381 char *parent = NULL, *base_path = NULL;
8382 char *blob_base_path = NULL;
8383 struct got_object_id *new_staged_blob_id = NULL;
8384 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8385 struct stat sb;
8387 err = create_unstaged_content(&path_unstaged_content,
8388 &path_new_staged_content, blob_id, staged_blob_id,
8389 ie->path, repo, patch_cb, patch_arg);
8390 if (err)
8391 return err;
8393 if (path_unstaged_content == NULL)
8394 return NULL;
8396 if (path_new_staged_content) {
8397 err = got_object_blob_create(&new_staged_blob_id,
8398 path_new_staged_content, repo);
8399 if (err)
8400 goto done;
8403 f = fopen(path_unstaged_content, "r");
8404 if (f == NULL) {
8405 err = got_error_from_errno2("fopen",
8406 path_unstaged_content);
8407 goto done;
8409 if (fstat(fileno(f), &sb) == -1) {
8410 err = got_error_from_errno2("fstat", path_unstaged_content);
8411 goto done;
8413 if (got_fileindex_entry_staged_filetype_get(ie) ==
8414 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8415 char link_target[PATH_MAX];
8416 size_t r;
8417 r = fread(link_target, 1, sizeof(link_target), f);
8418 if (r == 0 && ferror(f)) {
8419 err = got_error_from_errno("fread");
8420 goto done;
8422 if (r >= sizeof(link_target)) { /* should not happen */
8423 err = got_error(GOT_ERR_NO_SPACE);
8424 goto done;
8426 link_target[r] = '\0';
8427 err = merge_symlink(worktree, blob_base,
8428 ondisk_path, ie->path, label_orig, link_target,
8429 worktree->base_commit_id, repo, progress_cb,
8430 progress_arg);
8431 } else {
8432 int local_changes_subsumed;
8434 err = got_path_dirname(&parent, ondisk_path);
8435 if (err)
8436 return err;
8438 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8439 parent) == -1) {
8440 err = got_error_from_errno("asprintf");
8441 base_path = NULL;
8442 goto done;
8445 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8446 if (err)
8447 goto done;
8448 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8449 blob_base);
8450 if (err)
8451 goto done;
8454 * In order the run a 3-way merge with a symlink we copy the symlink's
8455 * target path into a temporary file and use that file with diff3.
8457 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8458 err = dump_symlink_target_path_to_file(&f_deriv2,
8459 ondisk_path);
8460 if (err)
8461 goto done;
8462 } else {
8463 int fd;
8464 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8465 if (fd == -1) {
8466 err = got_error_from_errno2("open", ondisk_path);
8467 goto done;
8469 f_deriv2 = fdopen(fd, "r");
8470 if (f_deriv2 == NULL) {
8471 err = got_error_from_errno2("fdopen", ondisk_path);
8472 close(fd);
8473 goto done;
8477 err = merge_file(&local_changes_subsumed, worktree,
8478 f_base, f, f_deriv2, ondisk_path, ie->path,
8479 got_fileindex_perms_to_st(ie),
8480 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8481 repo, progress_cb, progress_arg);
8483 if (err)
8484 goto done;
8486 if (new_staged_blob_id) {
8487 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8488 SHA1_DIGEST_LENGTH);
8489 } else {
8490 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8491 got_fileindex_entry_staged_filetype_set(ie, 0);
8493 done:
8494 free(new_staged_blob_id);
8495 if (path_unstaged_content &&
8496 unlink(path_unstaged_content) == -1 && err == NULL)
8497 err = got_error_from_errno2("unlink", path_unstaged_content);
8498 if (path_new_staged_content &&
8499 unlink(path_new_staged_content) == -1 && err == NULL)
8500 err = got_error_from_errno2("unlink", path_new_staged_content);
8501 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", blob_base_path);
8503 if (f_base && fclose(f_base) == EOF && err == NULL)
8504 err = got_error_from_errno2("fclose", path_unstaged_content);
8505 if (f && fclose(f) == EOF && err == NULL)
8506 err = got_error_from_errno2("fclose", path_unstaged_content);
8507 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8508 err = got_error_from_errno2("fclose", ondisk_path);
8509 free(path_unstaged_content);
8510 free(path_new_staged_content);
8511 free(blob_base_path);
8512 free(parent);
8513 free(base_path);
8514 return err;
8517 static const struct got_error *
8518 unstage_path(void *arg, unsigned char status,
8519 unsigned char staged_status, const char *relpath,
8520 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8521 struct got_object_id *commit_id, int dirfd, const char *de_name)
8523 const struct got_error *err = NULL;
8524 struct unstage_path_arg *a = arg;
8525 struct got_fileindex_entry *ie;
8526 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8527 char *ondisk_path = NULL;
8528 char *id_str = NULL, *label_orig = NULL;
8529 int local_changes_subsumed;
8530 struct stat sb;
8532 if (staged_status != GOT_STATUS_ADD &&
8533 staged_status != GOT_STATUS_MODIFY &&
8534 staged_status != GOT_STATUS_DELETE)
8535 return NULL;
8537 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8538 if (ie == NULL)
8539 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8541 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8542 == -1)
8543 return got_error_from_errno("asprintf");
8545 err = got_object_id_str(&id_str,
8546 commit_id ? commit_id : a->worktree->base_commit_id);
8547 if (err)
8548 goto done;
8549 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8550 id_str) == -1) {
8551 err = got_error_from_errno("asprintf");
8552 goto done;
8555 switch (staged_status) {
8556 case GOT_STATUS_MODIFY:
8557 err = got_object_open_as_blob(&blob_base, a->repo,
8558 blob_id, 8192);
8559 if (err)
8560 break;
8561 /* fall through */
8562 case GOT_STATUS_ADD:
8563 if (a->patch_cb) {
8564 if (staged_status == GOT_STATUS_ADD) {
8565 int choice = GOT_PATCH_CHOICE_NONE;
8566 err = (*a->patch_cb)(&choice, a->patch_arg,
8567 staged_status, ie->path, NULL, 1, 1);
8568 if (err)
8569 break;
8570 if (choice != GOT_PATCH_CHOICE_YES)
8571 break;
8572 } else {
8573 err = unstage_hunks(staged_blob_id,
8574 blob_base, blob_id, ie, ondisk_path,
8575 label_orig, a->worktree, a->repo,
8576 a->patch_cb, a->patch_arg,
8577 a->progress_cb, a->progress_arg);
8578 break; /* Done with this file. */
8581 err = got_object_open_as_blob(&blob_staged, a->repo,
8582 staged_blob_id, 8192);
8583 if (err)
8584 break;
8585 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8586 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8587 case GOT_FILEIDX_MODE_REGULAR_FILE:
8588 err = merge_blob(&local_changes_subsumed, a->worktree,
8589 blob_base, ondisk_path, relpath,
8590 got_fileindex_perms_to_st(ie), label_orig,
8591 blob_staged, commit_id ? commit_id :
8592 a->worktree->base_commit_id, a->repo,
8593 a->progress_cb, a->progress_arg);
8594 break;
8595 case GOT_FILEIDX_MODE_SYMLINK:
8596 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8597 char *staged_target;
8598 err = got_object_blob_read_to_str(
8599 &staged_target, blob_staged);
8600 if (err)
8601 goto done;
8602 err = merge_symlink(a->worktree, blob_base,
8603 ondisk_path, relpath, label_orig,
8604 staged_target, commit_id ? commit_id :
8605 a->worktree->base_commit_id,
8606 a->repo, a->progress_cb, a->progress_arg);
8607 free(staged_target);
8608 } else {
8609 err = merge_blob(&local_changes_subsumed,
8610 a->worktree, blob_base, ondisk_path,
8611 relpath, got_fileindex_perms_to_st(ie),
8612 label_orig, blob_staged,
8613 commit_id ? commit_id :
8614 a->worktree->base_commit_id, a->repo,
8615 a->progress_cb, a->progress_arg);
8617 break;
8618 default:
8619 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8620 break;
8622 if (err == NULL) {
8623 got_fileindex_entry_stage_set(ie,
8624 GOT_FILEIDX_STAGE_NONE);
8625 got_fileindex_entry_staged_filetype_set(ie, 0);
8627 break;
8628 case GOT_STATUS_DELETE:
8629 if (a->patch_cb) {
8630 int choice = GOT_PATCH_CHOICE_NONE;
8631 err = (*a->patch_cb)(&choice, a->patch_arg,
8632 staged_status, ie->path, NULL, 1, 1);
8633 if (err)
8634 break;
8635 if (choice == GOT_PATCH_CHOICE_NO)
8636 break;
8637 if (choice != GOT_PATCH_CHOICE_YES) {
8638 err = got_error(GOT_ERR_PATCH_CHOICE);
8639 break;
8642 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8643 got_fileindex_entry_staged_filetype_set(ie, 0);
8644 err = get_file_status(&status, &sb, ie, ondisk_path,
8645 dirfd, de_name, a->repo);
8646 if (err)
8647 break;
8648 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8649 break;
8651 done:
8652 free(ondisk_path);
8653 if (blob_base)
8654 got_object_blob_close(blob_base);
8655 if (blob_staged)
8656 got_object_blob_close(blob_staged);
8657 free(id_str);
8658 free(label_orig);
8659 return err;
8662 const struct got_error *
8663 got_worktree_unstage(struct got_worktree *worktree,
8664 struct got_pathlist_head *paths,
8665 got_worktree_checkout_cb progress_cb, void *progress_arg,
8666 got_worktree_patch_cb patch_cb, void *patch_arg,
8667 struct got_repository *repo)
8669 const struct got_error *err = NULL, *sync_err, *unlockerr;
8670 struct got_pathlist_entry *pe;
8671 struct got_fileindex *fileindex = NULL;
8672 char *fileindex_path = NULL;
8673 struct unstage_path_arg upa;
8675 err = lock_worktree(worktree, LOCK_EX);
8676 if (err)
8677 return err;
8679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8680 if (err)
8681 goto done;
8683 upa.worktree = worktree;
8684 upa.fileindex = fileindex;
8685 upa.repo = repo;
8686 upa.progress_cb = progress_cb;
8687 upa.progress_arg = progress_arg;
8688 upa.patch_cb = patch_cb;
8689 upa.patch_arg = patch_arg;
8690 TAILQ_FOREACH(pe, paths, entry) {
8691 err = worktree_status(worktree, pe->path, fileindex, repo,
8692 unstage_path, &upa, NULL, NULL, 0, 0);
8693 if (err)
8694 goto done;
8697 sync_err = sync_fileindex(fileindex, fileindex_path);
8698 if (sync_err && err == NULL)
8699 err = sync_err;
8700 done:
8701 free(fileindex_path);
8702 if (fileindex)
8703 got_fileindex_free(fileindex);
8704 unlockerr = lock_worktree(worktree, LOCK_SH);
8705 if (unlockerr && err == NULL)
8706 err = unlockerr;
8707 return err;
8710 struct report_file_info_arg {
8711 struct got_worktree *worktree;
8712 got_worktree_path_info_cb info_cb;
8713 void *info_arg;
8714 struct got_pathlist_head *paths;
8715 got_cancel_cb cancel_cb;
8716 void *cancel_arg;
8719 static const struct got_error *
8720 report_file_info(void *arg, struct got_fileindex_entry *ie)
8722 struct report_file_info_arg *a = arg;
8723 struct got_pathlist_entry *pe;
8724 struct got_object_id blob_id, staged_blob_id, commit_id;
8725 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8726 struct got_object_id *commit_idp = NULL;
8727 int stage;
8729 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8730 return got_error(GOT_ERR_CANCELLED);
8732 TAILQ_FOREACH(pe, a->paths, entry) {
8733 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8734 got_path_is_child(ie->path, pe->path, pe->path_len))
8735 break;
8737 if (pe == NULL) /* not found */
8738 return NULL;
8740 if (got_fileindex_entry_has_blob(ie)) {
8741 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8742 blob_idp = &blob_id;
8744 stage = got_fileindex_entry_stage_get(ie);
8745 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8746 stage == GOT_FILEIDX_STAGE_ADD) {
8747 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8748 SHA1_DIGEST_LENGTH);
8749 staged_blob_idp = &staged_blob_id;
8752 if (got_fileindex_entry_has_commit(ie)) {
8753 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8754 commit_idp = &commit_id;
8757 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8758 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8761 const struct got_error *
8762 got_worktree_path_info(struct got_worktree *worktree,
8763 struct got_pathlist_head *paths,
8764 got_worktree_path_info_cb info_cb, void *info_arg,
8765 got_cancel_cb cancel_cb, void *cancel_arg)
8768 const struct got_error *err = NULL, *unlockerr;
8769 struct got_fileindex *fileindex = NULL;
8770 char *fileindex_path = NULL;
8771 struct report_file_info_arg arg;
8773 err = lock_worktree(worktree, LOCK_SH);
8774 if (err)
8775 return err;
8777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8778 if (err)
8779 goto done;
8781 arg.worktree = worktree;
8782 arg.info_cb = info_cb;
8783 arg.info_arg = info_arg;
8784 arg.paths = paths;
8785 arg.cancel_cb = cancel_cb;
8786 arg.cancel_arg = cancel_arg;
8787 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8788 &arg);
8789 done:
8790 free(fileindex_path);
8791 if (fileindex)
8792 got_fileindex_free(fileindex);
8793 unlockerr = lock_worktree(worktree, LOCK_UN);
8794 if (unlockerr && err == NULL)
8795 err = unlockerr;
8796 return err;