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 while (ct_path[0] == '/')
5223 ct_path++;
5225 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5226 status = ct->staged_status;
5227 else
5228 status = ct->status;
5230 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5231 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5234 static const struct got_error *
5235 match_modified_subtree(int *modified, struct got_tree_entry *te,
5236 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5238 const struct got_error *err = NULL;
5239 struct got_pathlist_entry *pe;
5240 char *te_path;
5242 *modified = 0;
5244 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5245 got_path_is_root_dir(base_tree_path) ? "" : "/",
5246 te->name) == -1)
5247 return got_error_from_errno("asprintf");
5249 TAILQ_FOREACH(pe, commitable_paths, entry) {
5250 struct got_commitable *ct = pe->data;
5251 *modified = got_path_is_child(ct->in_repo_path, te_path,
5252 strlen(te_path));
5253 if (*modified)
5254 break;
5257 free(te_path);
5258 return err;
5261 static const struct got_error *
5262 match_deleted_or_modified_ct(struct got_commitable **ctp,
5263 struct got_tree_entry *te, const char *base_tree_path,
5264 struct got_pathlist_head *commitable_paths)
5266 const struct got_error *err = NULL;
5267 struct got_pathlist_entry *pe;
5269 *ctp = NULL;
5271 TAILQ_FOREACH(pe, commitable_paths, entry) {
5272 struct got_commitable *ct = pe->data;
5273 char *ct_name = NULL;
5274 int path_matches;
5276 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5277 if (ct->status != GOT_STATUS_MODIFY &&
5278 ct->status != GOT_STATUS_MODE_CHANGE &&
5279 ct->status != GOT_STATUS_DELETE)
5280 continue;
5281 } else {
5282 if (ct->staged_status != GOT_STATUS_MODIFY &&
5283 ct->staged_status != GOT_STATUS_DELETE)
5284 continue;
5287 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5288 continue;
5290 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5291 if (err)
5292 return err;
5293 if (!path_matches)
5294 continue;
5296 err = got_path_basename(&ct_name, pe->path);
5297 if (err)
5298 return err;
5300 if (strcmp(te->name, ct_name) != 0) {
5301 free(ct_name);
5302 continue;
5304 free(ct_name);
5306 *ctp = ct;
5307 break;
5310 return err;
5313 static const struct got_error *
5314 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5315 const char *child_path, const char *path_base_tree,
5316 struct got_pathlist_head *commitable_paths,
5317 got_worktree_status_cb status_cb, void *status_arg,
5318 struct got_repository *repo)
5320 const struct got_error *err = NULL;
5321 struct got_tree_entry *new_te;
5322 char *subtree_path;
5323 struct got_object_id *id = NULL;
5324 int nentries;
5326 *new_tep = NULL;
5328 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5329 got_path_is_root_dir(path_base_tree) ? "" : "/",
5330 child_path) == -1)
5331 return got_error_from_errno("asprintf");
5333 new_te = calloc(1, sizeof(*new_te));
5334 if (new_te == NULL)
5335 return got_error_from_errno("calloc");
5336 new_te->mode = S_IFDIR;
5338 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5339 sizeof(new_te->name)) {
5340 err = got_error(GOT_ERR_NO_SPACE);
5341 goto done;
5343 err = write_tree(&id, &nentries, NULL, subtree_path,
5344 commitable_paths, status_cb, status_arg, repo);
5345 if (err) {
5346 free(new_te);
5347 goto done;
5349 memcpy(&new_te->id, id, sizeof(new_te->id));
5350 done:
5351 free(id);
5352 free(subtree_path);
5353 if (err == NULL)
5354 *new_tep = new_te;
5355 return err;
5358 static const struct got_error *
5359 write_tree(struct got_object_id **new_tree_id, int *nentries,
5360 struct got_tree_object *base_tree, const char *path_base_tree,
5361 struct got_pathlist_head *commitable_paths,
5362 got_worktree_status_cb status_cb, void *status_arg,
5363 struct got_repository *repo)
5365 const struct got_error *err = NULL;
5366 struct got_pathlist_head paths;
5367 struct got_tree_entry *te, *new_te = NULL;
5368 struct got_pathlist_entry *pe;
5370 TAILQ_INIT(&paths);
5371 *nentries = 0;
5373 /* Insert, and recurse into, newly added entries first. */
5374 TAILQ_FOREACH(pe, commitable_paths, entry) {
5375 struct got_commitable *ct = pe->data;
5376 char *child_path = NULL, *slash;
5378 if ((ct->status != GOT_STATUS_ADD &&
5379 ct->staged_status != GOT_STATUS_ADD) ||
5380 (ct->flags & GOT_COMMITABLE_ADDED))
5381 continue;
5383 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5384 strlen(path_base_tree)))
5385 continue;
5387 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5388 ct->in_repo_path);
5389 if (err)
5390 goto done;
5392 slash = strchr(child_path, '/');
5393 if (slash == NULL) {
5394 err = alloc_added_blob_tree_entry(&new_te, ct);
5395 if (err)
5396 goto done;
5397 err = report_ct_status(ct, status_cb, status_arg);
5398 if (err)
5399 goto done;
5400 ct->flags |= GOT_COMMITABLE_ADDED;
5401 err = insert_tree_entry(new_te, &paths);
5402 if (err)
5403 goto done;
5404 (*nentries)++;
5405 } else {
5406 *slash = '\0'; /* trim trailing path components */
5407 if (base_tree == NULL ||
5408 got_object_tree_find_entry(base_tree, child_path)
5409 == NULL) {
5410 err = make_subtree_for_added_blob(&new_te,
5411 child_path, path_base_tree,
5412 commitable_paths, status_cb, status_arg,
5413 repo);
5414 if (err)
5415 goto done;
5416 err = insert_tree_entry(new_te, &paths);
5417 if (err)
5418 goto done;
5419 (*nentries)++;
5424 if (base_tree) {
5425 int i, nbase_entries;
5426 /* Handle modified and deleted entries. */
5427 nbase_entries = got_object_tree_get_nentries(base_tree);
5428 for (i = 0; i < nbase_entries; i++) {
5429 struct got_commitable *ct = NULL;
5431 te = got_object_tree_get_entry(base_tree, i);
5432 if (got_object_tree_entry_is_submodule(te)) {
5433 /* Entry is a submodule; just copy it. */
5434 err = got_object_tree_entry_dup(&new_te, te);
5435 if (err)
5436 goto done;
5437 err = insert_tree_entry(new_te, &paths);
5438 if (err)
5439 goto done;
5440 (*nentries)++;
5441 continue;
5444 if (S_ISDIR(te->mode)) {
5445 int modified;
5446 err = got_object_tree_entry_dup(&new_te, te);
5447 if (err)
5448 goto done;
5449 err = match_modified_subtree(&modified, te,
5450 path_base_tree, commitable_paths);
5451 if (err)
5452 goto done;
5453 /* Avoid recursion into unmodified subtrees. */
5454 if (modified) {
5455 struct got_object_id *new_id;
5456 int nsubentries;
5457 err = write_subtree(&new_id,
5458 &nsubentries, te,
5459 path_base_tree, commitable_paths,
5460 status_cb, status_arg, repo);
5461 if (err)
5462 goto done;
5463 if (nsubentries == 0) {
5464 /* All entries were deleted. */
5465 free(new_id);
5466 continue;
5468 memcpy(&new_te->id, new_id,
5469 sizeof(new_te->id));
5470 free(new_id);
5472 err = insert_tree_entry(new_te, &paths);
5473 if (err)
5474 goto done;
5475 (*nentries)++;
5476 continue;
5479 err = match_deleted_or_modified_ct(&ct, te,
5480 path_base_tree, commitable_paths);
5481 if (err)
5482 goto done;
5483 if (ct) {
5484 /* NB: Deleted entries get dropped here. */
5485 if (ct->status == GOT_STATUS_MODIFY ||
5486 ct->status == GOT_STATUS_MODE_CHANGE ||
5487 ct->staged_status == GOT_STATUS_MODIFY) {
5488 err = alloc_modified_blob_tree_entry(
5489 &new_te, te, ct);
5490 if (err)
5491 goto done;
5492 err = insert_tree_entry(new_te, &paths);
5493 if (err)
5494 goto done;
5495 (*nentries)++;
5497 err = report_ct_status(ct, status_cb,
5498 status_arg);
5499 if (err)
5500 goto done;
5501 } else {
5502 /* Entry is unchanged; just copy it. */
5503 err = got_object_tree_entry_dup(&new_te, te);
5504 if (err)
5505 goto done;
5506 err = insert_tree_entry(new_te, &paths);
5507 if (err)
5508 goto done;
5509 (*nentries)++;
5514 /* Write new list of entries; deleted entries have been dropped. */
5515 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5516 done:
5517 got_pathlist_free(&paths);
5518 return err;
5521 static const struct got_error *
5522 update_fileindex_after_commit(struct got_worktree *worktree,
5523 struct got_pathlist_head *commitable_paths,
5524 struct got_object_id *new_base_commit_id,
5525 struct got_fileindex *fileindex, int have_staged_files)
5527 const struct got_error *err = NULL;
5528 struct got_pathlist_entry *pe;
5529 char *relpath = NULL;
5531 TAILQ_FOREACH(pe, commitable_paths, entry) {
5532 struct got_fileindex_entry *ie;
5533 struct got_commitable *ct = pe->data;
5535 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5537 err = got_path_skip_common_ancestor(&relpath,
5538 worktree->root_path, ct->ondisk_path);
5539 if (err)
5540 goto done;
5542 if (ie) {
5543 if (ct->status == GOT_STATUS_DELETE ||
5544 ct->staged_status == GOT_STATUS_DELETE) {
5545 got_fileindex_entry_remove(fileindex, ie);
5546 } else if (ct->staged_status == GOT_STATUS_ADD ||
5547 ct->staged_status == GOT_STATUS_MODIFY) {
5548 got_fileindex_entry_stage_set(ie,
5549 GOT_FILEIDX_STAGE_NONE);
5550 got_fileindex_entry_staged_filetype_set(ie, 0);
5552 err = got_fileindex_entry_update(ie,
5553 worktree->root_fd, relpath,
5554 ct->staged_blob_id->sha1,
5555 new_base_commit_id->sha1,
5556 !have_staged_files);
5557 } else
5558 err = got_fileindex_entry_update(ie,
5559 worktree->root_fd, relpath,
5560 ct->blob_id->sha1,
5561 new_base_commit_id->sha1,
5562 !have_staged_files);
5563 } else {
5564 err = got_fileindex_entry_alloc(&ie, pe->path);
5565 if (err)
5566 goto done;
5567 err = got_fileindex_entry_update(ie,
5568 worktree->root_fd, relpath, ct->blob_id->sha1,
5569 new_base_commit_id->sha1, 1);
5570 if (err) {
5571 got_fileindex_entry_free(ie);
5572 goto done;
5574 err = got_fileindex_entry_add(fileindex, ie);
5575 if (err) {
5576 got_fileindex_entry_free(ie);
5577 goto done;
5580 free(relpath);
5581 relpath = NULL;
5583 done:
5584 free(relpath);
5585 return err;
5589 static const struct got_error *
5590 check_out_of_date(const char *in_repo_path, unsigned char status,
5591 unsigned char staged_status, struct got_object_id *base_blob_id,
5592 struct got_object_id *base_commit_id,
5593 struct got_object_id *head_commit_id, struct got_repository *repo,
5594 int ood_errcode)
5596 const struct got_error *err = NULL;
5597 struct got_object_id *id = NULL;
5599 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5600 /* Trivial case: base commit == head commit */
5601 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5602 return NULL;
5604 * Ensure file content which local changes were based
5605 * on matches file content in the branch head.
5607 err = got_object_id_by_path(&id, repo, head_commit_id,
5608 in_repo_path);
5609 if (err) {
5610 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5611 err = got_error(ood_errcode);
5612 goto done;
5613 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5614 err = got_error(ood_errcode);
5615 } else {
5616 /* Require that added files don't exist in the branch head. */
5617 err = got_object_id_by_path(&id, repo, head_commit_id,
5618 in_repo_path);
5619 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5620 goto done;
5621 err = id ? got_error(ood_errcode) : NULL;
5623 done:
5624 free(id);
5625 return err;
5628 const struct got_error *
5629 commit_worktree(struct got_object_id **new_commit_id,
5630 struct got_pathlist_head *commitable_paths,
5631 struct got_object_id *head_commit_id,
5632 struct got_object_id *parent_id2,
5633 struct got_worktree *worktree,
5634 const char *author, const char *committer,
5635 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5636 got_worktree_status_cb status_cb, void *status_arg,
5637 struct got_repository *repo)
5639 const struct got_error *err = NULL, *unlockerr = NULL;
5640 struct got_pathlist_entry *pe;
5641 const char *head_ref_name = NULL;
5642 struct got_commit_object *head_commit = NULL;
5643 struct got_reference *head_ref2 = NULL;
5644 struct got_object_id *head_commit_id2 = NULL;
5645 struct got_tree_object *head_tree = NULL;
5646 struct got_object_id *new_tree_id = NULL;
5647 int nentries, nparents = 0;
5648 struct got_object_id_queue parent_ids;
5649 struct got_object_qid *pid = NULL;
5650 char *logmsg = NULL;
5652 *new_commit_id = NULL;
5654 STAILQ_INIT(&parent_ids);
5656 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5657 if (err)
5658 goto done;
5660 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5661 if (err)
5662 goto done;
5664 if (commit_msg_cb != NULL) {
5665 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5666 if (err)
5667 goto done;
5670 if (logmsg == NULL || strlen(logmsg) == 0) {
5671 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5672 goto done;
5675 /* Create blobs from added and modified files and record their IDs. */
5676 TAILQ_FOREACH(pe, commitable_paths, entry) {
5677 struct got_commitable *ct = pe->data;
5678 char *ondisk_path;
5680 /* Blobs for staged files already exist. */
5681 if (ct->staged_status == GOT_STATUS_ADD ||
5682 ct->staged_status == GOT_STATUS_MODIFY)
5683 continue;
5685 if (ct->status != GOT_STATUS_ADD &&
5686 ct->status != GOT_STATUS_MODIFY &&
5687 ct->status != GOT_STATUS_MODE_CHANGE)
5688 continue;
5690 if (asprintf(&ondisk_path, "%s/%s",
5691 worktree->root_path, pe->path) == -1) {
5692 err = got_error_from_errno("asprintf");
5693 goto done;
5695 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5696 free(ondisk_path);
5697 if (err)
5698 goto done;
5701 /* Recursively write new tree objects. */
5702 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5703 commitable_paths, status_cb, status_arg, repo);
5704 if (err)
5705 goto done;
5707 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5708 if (err)
5709 goto done;
5710 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5711 nparents++;
5712 if (parent_id2) {
5713 err = got_object_qid_alloc(&pid, parent_id2);
5714 if (err)
5715 goto done;
5716 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5717 nparents++;
5719 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5720 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5721 if (logmsg != NULL)
5722 free(logmsg);
5723 if (err)
5724 goto done;
5726 /* Check if a concurrent commit to our branch has occurred. */
5727 head_ref_name = got_worktree_get_head_ref_name(worktree);
5728 if (head_ref_name == NULL) {
5729 err = got_error_from_errno("got_worktree_get_head_ref_name");
5730 goto done;
5732 /* Lock the reference here to prevent concurrent modification. */
5733 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5734 if (err)
5735 goto done;
5736 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5737 if (err)
5738 goto done;
5739 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5740 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5741 goto done;
5743 /* Update branch head in repository. */
5744 err = got_ref_change_ref(head_ref2, *new_commit_id);
5745 if (err)
5746 goto done;
5747 err = got_ref_write(head_ref2, repo);
5748 if (err)
5749 goto done;
5751 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5752 if (err)
5753 goto done;
5755 err = ref_base_commit(worktree, repo);
5756 if (err)
5757 goto done;
5758 done:
5759 got_object_id_queue_free(&parent_ids);
5760 if (head_tree)
5761 got_object_tree_close(head_tree);
5762 if (head_commit)
5763 got_object_commit_close(head_commit);
5764 free(head_commit_id2);
5765 if (head_ref2) {
5766 unlockerr = got_ref_unlock(head_ref2);
5767 if (unlockerr && err == NULL)
5768 err = unlockerr;
5769 got_ref_close(head_ref2);
5771 return err;
5774 static const struct got_error *
5775 check_path_is_commitable(const char *path,
5776 struct got_pathlist_head *commitable_paths)
5778 struct got_pathlist_entry *cpe = NULL;
5779 size_t path_len = strlen(path);
5781 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5782 struct got_commitable *ct = cpe->data;
5783 const char *ct_path = ct->path;
5785 while (ct_path[0] == '/')
5786 ct_path++;
5788 if (strcmp(path, ct_path) == 0 ||
5789 got_path_is_child(ct_path, path, path_len))
5790 break;
5793 if (cpe == NULL)
5794 return got_error_path(path, GOT_ERR_BAD_PATH);
5796 return NULL;
5799 static const struct got_error *
5800 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5802 int *have_staged_files = arg;
5804 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5805 *have_staged_files = 1;
5806 return got_error(GOT_ERR_CANCELLED);
5809 return NULL;
5812 static const struct got_error *
5813 check_non_staged_files(struct got_fileindex *fileindex,
5814 struct got_pathlist_head *paths)
5816 struct got_pathlist_entry *pe;
5817 struct got_fileindex_entry *ie;
5819 TAILQ_FOREACH(pe, paths, entry) {
5820 if (pe->path[0] == '\0')
5821 continue;
5822 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5823 if (ie == NULL)
5824 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5825 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5826 return got_error_path(pe->path,
5827 GOT_ERR_FILE_NOT_STAGED);
5830 return NULL;
5833 const struct got_error *
5834 got_worktree_commit(struct got_object_id **new_commit_id,
5835 struct got_worktree *worktree, struct got_pathlist_head *paths,
5836 const char *author, const char *committer, int allow_bad_symlinks,
5837 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5838 got_worktree_status_cb status_cb, void *status_arg,
5839 struct got_repository *repo)
5841 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5842 struct got_fileindex *fileindex = NULL;
5843 char *fileindex_path = NULL;
5844 struct got_pathlist_head commitable_paths;
5845 struct collect_commitables_arg cc_arg;
5846 struct got_pathlist_entry *pe;
5847 struct got_reference *head_ref = NULL;
5848 struct got_object_id *head_commit_id = NULL;
5849 int have_staged_files = 0;
5851 *new_commit_id = NULL;
5853 TAILQ_INIT(&commitable_paths);
5855 err = lock_worktree(worktree, LOCK_EX);
5856 if (err)
5857 goto done;
5859 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5860 if (err)
5861 goto done;
5863 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5864 if (err)
5865 goto done;
5867 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5868 if (err)
5869 goto done;
5871 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5872 &have_staged_files);
5873 if (err && err->code != GOT_ERR_CANCELLED)
5874 goto done;
5875 if (have_staged_files) {
5876 err = check_non_staged_files(fileindex, paths);
5877 if (err)
5878 goto done;
5881 cc_arg.commitable_paths = &commitable_paths;
5882 cc_arg.worktree = worktree;
5883 cc_arg.fileindex = fileindex;
5884 cc_arg.repo = repo;
5885 cc_arg.have_staged_files = have_staged_files;
5886 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5887 TAILQ_FOREACH(pe, paths, entry) {
5888 err = worktree_status(worktree, pe->path, fileindex, repo,
5889 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5890 if (err)
5891 goto done;
5894 if (TAILQ_EMPTY(&commitable_paths)) {
5895 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5896 goto done;
5899 TAILQ_FOREACH(pe, paths, entry) {
5900 err = check_path_is_commitable(pe->path, &commitable_paths);
5901 if (err)
5902 goto done;
5905 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5906 struct got_commitable *ct = pe->data;
5907 const char *ct_path = ct->in_repo_path;
5909 while (ct_path[0] == '/')
5910 ct_path++;
5911 err = check_out_of_date(ct_path, ct->status,
5912 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5913 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5914 if (err)
5915 goto done;
5919 err = commit_worktree(new_commit_id, &commitable_paths,
5920 head_commit_id, NULL, worktree, author, committer,
5921 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5922 if (err)
5923 goto done;
5925 err = update_fileindex_after_commit(worktree, &commitable_paths,
5926 *new_commit_id, fileindex, have_staged_files);
5927 sync_err = sync_fileindex(fileindex, fileindex_path);
5928 if (sync_err && err == NULL)
5929 err = sync_err;
5930 done:
5931 if (fileindex)
5932 got_fileindex_free(fileindex);
5933 free(fileindex_path);
5934 unlockerr = lock_worktree(worktree, LOCK_SH);
5935 if (unlockerr && err == NULL)
5936 err = unlockerr;
5937 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5938 struct got_commitable *ct = pe->data;
5939 free_commitable(ct);
5941 got_pathlist_free(&commitable_paths);
5942 return err;
5945 const char *
5946 got_commitable_get_path(struct got_commitable *ct)
5948 return ct->path;
5951 unsigned int
5952 got_commitable_get_status(struct got_commitable *ct)
5954 return ct->status;
5957 struct check_rebase_ok_arg {
5958 struct got_worktree *worktree;
5959 struct got_repository *repo;
5962 static const struct got_error *
5963 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5965 const struct got_error *err = NULL;
5966 struct check_rebase_ok_arg *a = arg;
5967 unsigned char status;
5968 struct stat sb;
5969 char *ondisk_path;
5971 /* Reject rebase of a work tree with mixed base commits. */
5972 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5973 SHA1_DIGEST_LENGTH))
5974 return got_error(GOT_ERR_MIXED_COMMITS);
5976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5977 == -1)
5978 return got_error_from_errno("asprintf");
5980 /* Reject rebase of a work tree with modified or staged files. */
5981 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5982 free(ondisk_path);
5983 if (err)
5984 return err;
5986 if (status != GOT_STATUS_NO_CHANGE)
5987 return got_error(GOT_ERR_MODIFIED);
5988 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5989 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5991 return NULL;
5994 const struct got_error *
5995 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5996 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5997 struct got_worktree *worktree, struct got_reference *branch,
5998 struct got_repository *repo)
6000 const struct got_error *err = NULL;
6001 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6002 char *branch_ref_name = NULL;
6003 char *fileindex_path = NULL;
6004 struct check_rebase_ok_arg ok_arg;
6005 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6006 struct got_object_id *wt_branch_tip = NULL;
6008 *new_base_branch_ref = NULL;
6009 *tmp_branch = NULL;
6010 *fileindex = NULL;
6012 err = lock_worktree(worktree, LOCK_EX);
6013 if (err)
6014 return err;
6016 err = open_fileindex(fileindex, &fileindex_path, worktree);
6017 if (err)
6018 goto done;
6020 ok_arg.worktree = worktree;
6021 ok_arg.repo = repo;
6022 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6023 &ok_arg);
6024 if (err)
6025 goto done;
6027 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6028 if (err)
6029 goto done;
6031 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6032 if (err)
6033 goto done;
6035 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6036 if (err)
6037 goto done;
6039 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6040 0);
6041 if (err)
6042 goto done;
6044 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6045 if (err)
6046 goto done;
6047 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6048 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6049 goto done;
6052 err = got_ref_alloc_symref(new_base_branch_ref,
6053 new_base_branch_ref_name, wt_branch);
6054 if (err)
6055 goto done;
6056 err = got_ref_write(*new_base_branch_ref, repo);
6057 if (err)
6058 goto done;
6060 /* TODO Lock original branch's ref while rebasing? */
6062 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6063 if (err)
6064 goto done;
6066 err = got_ref_write(branch_ref, repo);
6067 if (err)
6068 goto done;
6070 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6071 worktree->base_commit_id);
6072 if (err)
6073 goto done;
6074 err = got_ref_write(*tmp_branch, repo);
6075 if (err)
6076 goto done;
6078 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6079 if (err)
6080 goto done;
6081 done:
6082 free(fileindex_path);
6083 free(tmp_branch_name);
6084 free(new_base_branch_ref_name);
6085 free(branch_ref_name);
6086 if (branch_ref)
6087 got_ref_close(branch_ref);
6088 if (wt_branch)
6089 got_ref_close(wt_branch);
6090 free(wt_branch_tip);
6091 if (err) {
6092 if (*new_base_branch_ref) {
6093 got_ref_close(*new_base_branch_ref);
6094 *new_base_branch_ref = NULL;
6096 if (*tmp_branch) {
6097 got_ref_close(*tmp_branch);
6098 *tmp_branch = NULL;
6100 if (*fileindex) {
6101 got_fileindex_free(*fileindex);
6102 *fileindex = NULL;
6104 lock_worktree(worktree, LOCK_SH);
6106 return err;
6109 const struct got_error *
6110 got_worktree_rebase_continue(struct got_object_id **commit_id,
6111 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6112 struct got_reference **branch, struct got_fileindex **fileindex,
6113 struct got_worktree *worktree, struct got_repository *repo)
6115 const struct got_error *err;
6116 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6117 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6118 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6119 char *fileindex_path = NULL;
6120 int have_staged_files = 0;
6122 *commit_id = NULL;
6123 *new_base_branch = NULL;
6124 *tmp_branch = NULL;
6125 *branch = NULL;
6126 *fileindex = NULL;
6128 err = lock_worktree(worktree, LOCK_EX);
6129 if (err)
6130 return err;
6132 err = open_fileindex(fileindex, &fileindex_path, worktree);
6133 if (err)
6134 goto done;
6136 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6137 &have_staged_files);
6138 if (err && err->code != GOT_ERR_CANCELLED)
6139 goto done;
6140 if (have_staged_files) {
6141 err = got_error(GOT_ERR_STAGED_PATHS);
6142 goto done;
6145 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6146 if (err)
6147 goto done;
6149 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6150 if (err)
6151 goto done;
6153 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6154 if (err)
6155 goto done;
6157 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6158 if (err)
6159 goto done;
6161 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6162 if (err)
6163 goto done;
6165 err = got_ref_open(branch, repo,
6166 got_ref_get_symref_target(branch_ref), 0);
6167 if (err)
6168 goto done;
6170 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6171 if (err)
6172 goto done;
6174 err = got_ref_resolve(commit_id, repo, commit_ref);
6175 if (err)
6176 goto done;
6178 err = got_ref_open(new_base_branch, repo,
6179 new_base_branch_ref_name, 0);
6180 if (err)
6181 goto done;
6183 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6184 if (err)
6185 goto done;
6186 done:
6187 free(commit_ref_name);
6188 free(branch_ref_name);
6189 free(fileindex_path);
6190 if (commit_ref)
6191 got_ref_close(commit_ref);
6192 if (branch_ref)
6193 got_ref_close(branch_ref);
6194 if (err) {
6195 free(*commit_id);
6196 *commit_id = NULL;
6197 if (*tmp_branch) {
6198 got_ref_close(*tmp_branch);
6199 *tmp_branch = NULL;
6201 if (*new_base_branch) {
6202 got_ref_close(*new_base_branch);
6203 *new_base_branch = NULL;
6205 if (*branch) {
6206 got_ref_close(*branch);
6207 *branch = NULL;
6209 if (*fileindex) {
6210 got_fileindex_free(*fileindex);
6211 *fileindex = NULL;
6213 lock_worktree(worktree, LOCK_SH);
6215 return err;
6218 const struct got_error *
6219 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6221 const struct got_error *err;
6222 char *tmp_branch_name = NULL;
6224 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6225 if (err)
6226 return err;
6228 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6229 free(tmp_branch_name);
6230 return NULL;
6233 static const struct got_error *
6234 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6235 char **logmsg, void *arg)
6237 *logmsg = arg;
6238 return NULL;
6241 static const struct got_error *
6242 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6243 const char *path, struct got_object_id *blob_id,
6244 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6245 int dirfd, const char *de_name)
6247 return NULL;
6250 struct collect_merged_paths_arg {
6251 got_worktree_checkout_cb progress_cb;
6252 void *progress_arg;
6253 struct got_pathlist_head *merged_paths;
6256 static const struct got_error *
6257 collect_merged_paths(void *arg, unsigned char status, const char *path)
6259 const struct got_error *err;
6260 struct collect_merged_paths_arg *a = arg;
6261 char *p;
6262 struct got_pathlist_entry *new;
6264 err = (*a->progress_cb)(a->progress_arg, status, path);
6265 if (err)
6266 return err;
6268 if (status != GOT_STATUS_MERGE &&
6269 status != GOT_STATUS_ADD &&
6270 status != GOT_STATUS_DELETE &&
6271 status != GOT_STATUS_CONFLICT)
6272 return NULL;
6274 p = strdup(path);
6275 if (p == NULL)
6276 return got_error_from_errno("strdup");
6278 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6279 if (err || new == NULL)
6280 free(p);
6281 return err;
6284 void
6285 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6287 struct got_pathlist_entry *pe;
6289 TAILQ_FOREACH(pe, merged_paths, entry)
6290 free((char *)pe->path);
6292 got_pathlist_free(merged_paths);
6295 static const struct got_error *
6296 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6297 int is_rebase, struct got_repository *repo)
6299 const struct got_error *err;
6300 struct got_reference *commit_ref = NULL;
6302 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6303 if (err) {
6304 if (err->code != GOT_ERR_NOT_REF)
6305 goto done;
6306 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6307 if (err)
6308 goto done;
6309 err = got_ref_write(commit_ref, repo);
6310 if (err)
6311 goto done;
6312 } else if (is_rebase) {
6313 struct got_object_id *stored_id;
6314 int cmp;
6316 err = got_ref_resolve(&stored_id, repo, commit_ref);
6317 if (err)
6318 goto done;
6319 cmp = got_object_id_cmp(commit_id, stored_id);
6320 free(stored_id);
6321 if (cmp != 0) {
6322 err = got_error(GOT_ERR_REBASE_COMMITID);
6323 goto done;
6326 done:
6327 if (commit_ref)
6328 got_ref_close(commit_ref);
6329 return err;
6332 static const struct got_error *
6333 rebase_merge_files(struct got_pathlist_head *merged_paths,
6334 const char *commit_ref_name, struct got_worktree *worktree,
6335 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6336 struct got_object_id *commit_id, struct got_repository *repo,
6337 got_worktree_checkout_cb progress_cb, void *progress_arg,
6338 got_cancel_cb cancel_cb, void *cancel_arg)
6340 const struct got_error *err;
6341 struct got_reference *commit_ref = NULL;
6342 struct collect_merged_paths_arg cmp_arg;
6343 char *fileindex_path;
6345 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6347 err = get_fileindex_path(&fileindex_path, worktree);
6348 if (err)
6349 return err;
6351 cmp_arg.progress_cb = progress_cb;
6352 cmp_arg.progress_arg = progress_arg;
6353 cmp_arg.merged_paths = merged_paths;
6354 err = merge_files(worktree, fileindex, fileindex_path,
6355 parent_commit_id, commit_id, repo, collect_merged_paths,
6356 &cmp_arg, cancel_cb, cancel_arg);
6357 if (commit_ref)
6358 got_ref_close(commit_ref);
6359 return err;
6362 const struct got_error *
6363 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6364 struct got_worktree *worktree, struct got_fileindex *fileindex,
6365 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6366 struct got_repository *repo,
6367 got_worktree_checkout_cb progress_cb, void *progress_arg,
6368 got_cancel_cb cancel_cb, void *cancel_arg)
6370 const struct got_error *err;
6371 char *commit_ref_name;
6373 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6374 if (err)
6375 return err;
6377 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6378 if (err)
6379 goto done;
6381 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6382 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6383 progress_arg, cancel_cb, cancel_arg);
6384 done:
6385 free(commit_ref_name);
6386 return err;
6389 const struct got_error *
6390 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6391 struct got_worktree *worktree, struct got_fileindex *fileindex,
6392 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6393 struct got_repository *repo,
6394 got_worktree_checkout_cb progress_cb, void *progress_arg,
6395 got_cancel_cb cancel_cb, void *cancel_arg)
6397 const struct got_error *err;
6398 char *commit_ref_name;
6400 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6401 if (err)
6402 return err;
6404 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6405 if (err)
6406 goto done;
6408 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6409 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6410 progress_arg, cancel_cb, cancel_arg);
6411 done:
6412 free(commit_ref_name);
6413 return err;
6416 static const struct got_error *
6417 rebase_commit(struct got_object_id **new_commit_id,
6418 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6419 struct got_worktree *worktree, struct got_fileindex *fileindex,
6420 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6421 const char *new_logmsg, struct got_repository *repo)
6423 const struct got_error *err, *sync_err;
6424 struct got_pathlist_head commitable_paths;
6425 struct collect_commitables_arg cc_arg;
6426 char *fileindex_path = NULL;
6427 struct got_reference *head_ref = NULL;
6428 struct got_object_id *head_commit_id = NULL;
6429 char *logmsg = NULL;
6431 TAILQ_INIT(&commitable_paths);
6432 *new_commit_id = NULL;
6434 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6436 err = get_fileindex_path(&fileindex_path, worktree);
6437 if (err)
6438 return err;
6440 cc_arg.commitable_paths = &commitable_paths;
6441 cc_arg.worktree = worktree;
6442 cc_arg.repo = repo;
6443 cc_arg.have_staged_files = 0;
6445 * If possible get the status of individual files directly to
6446 * avoid crawling the entire work tree once per rebased commit.
6448 * Ideally, merged_paths would contain a list of commitables
6449 * we could use so we could skip worktree_status() entirely.
6450 * However, we would then need carefully keep track of cumulative
6451 * effects of operations such as file additions and deletions
6452 * in 'got histedit -f' (folding multiple commits into one),
6453 * and this extra complexity is not really worth it.
6455 if (merged_paths) {
6456 struct got_pathlist_entry *pe;
6457 TAILQ_FOREACH(pe, merged_paths, entry) {
6458 err = worktree_status(worktree, pe->path, fileindex,
6459 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6460 0);
6461 if (err)
6462 goto done;
6464 } else {
6465 err = worktree_status(worktree, "", fileindex, repo,
6466 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6467 if (err)
6468 goto done;
6471 if (TAILQ_EMPTY(&commitable_paths)) {
6472 /* No-op change; commit will be elided. */
6473 err = got_ref_delete(commit_ref, repo);
6474 if (err)
6475 goto done;
6476 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6477 goto done;
6480 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6481 if (err)
6482 goto done;
6484 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6485 if (err)
6486 goto done;
6488 if (new_logmsg) {
6489 logmsg = strdup(new_logmsg);
6490 if (logmsg == NULL) {
6491 err = got_error_from_errno("strdup");
6492 goto done;
6494 } else {
6495 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6496 if (err)
6497 goto done;
6500 /* NB: commit_worktree will call free(logmsg) */
6501 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6502 NULL, worktree, got_object_commit_get_author(orig_commit),
6503 got_object_commit_get_committer(orig_commit),
6504 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6505 if (err)
6506 goto done;
6508 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6509 if (err)
6510 goto done;
6512 err = got_ref_delete(commit_ref, repo);
6513 if (err)
6514 goto done;
6516 err = update_fileindex_after_commit(worktree, &commitable_paths,
6517 *new_commit_id, fileindex, 0);
6518 sync_err = sync_fileindex(fileindex, fileindex_path);
6519 if (sync_err && err == NULL)
6520 err = sync_err;
6521 done:
6522 free(fileindex_path);
6523 free(head_commit_id);
6524 if (head_ref)
6525 got_ref_close(head_ref);
6526 if (err) {
6527 free(*new_commit_id);
6528 *new_commit_id = NULL;
6530 return err;
6533 const struct got_error *
6534 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6535 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6536 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6537 struct got_commit_object *orig_commit,
6538 struct got_object_id *orig_commit_id, struct got_repository *repo)
6540 const struct got_error *err;
6541 char *commit_ref_name;
6542 struct got_reference *commit_ref = NULL;
6543 struct got_object_id *commit_id = NULL;
6545 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6546 if (err)
6547 return err;
6549 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6550 if (err)
6551 goto done;
6552 err = got_ref_resolve(&commit_id, repo, commit_ref);
6553 if (err)
6554 goto done;
6555 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6556 err = got_error(GOT_ERR_REBASE_COMMITID);
6557 goto done;
6560 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6561 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6562 done:
6563 if (commit_ref)
6564 got_ref_close(commit_ref);
6565 free(commit_ref_name);
6566 free(commit_id);
6567 return err;
6570 const struct got_error *
6571 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6572 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6573 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6574 struct got_commit_object *orig_commit,
6575 struct got_object_id *orig_commit_id, const char *new_logmsg,
6576 struct got_repository *repo)
6578 const struct got_error *err;
6579 char *commit_ref_name;
6580 struct got_reference *commit_ref = NULL;
6582 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6583 if (err)
6584 return err;
6586 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6587 if (err)
6588 goto done;
6590 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6591 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6592 done:
6593 if (commit_ref)
6594 got_ref_close(commit_ref);
6595 free(commit_ref_name);
6596 return err;
6599 const struct got_error *
6600 got_worktree_rebase_postpone(struct got_worktree *worktree,
6601 struct got_fileindex *fileindex)
6603 if (fileindex)
6604 got_fileindex_free(fileindex);
6605 return lock_worktree(worktree, LOCK_SH);
6608 static const struct got_error *
6609 delete_ref(const char *name, struct got_repository *repo)
6611 const struct got_error *err;
6612 struct got_reference *ref;
6614 err = got_ref_open(&ref, repo, name, 0);
6615 if (err) {
6616 if (err->code == GOT_ERR_NOT_REF)
6617 return NULL;
6618 return err;
6621 err = got_ref_delete(ref, repo);
6622 got_ref_close(ref);
6623 return err;
6626 static const struct got_error *
6627 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6629 const struct got_error *err;
6630 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6631 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6633 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6634 if (err)
6635 goto done;
6636 err = delete_ref(tmp_branch_name, repo);
6637 if (err)
6638 goto done;
6640 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6641 if (err)
6642 goto done;
6643 err = delete_ref(new_base_branch_ref_name, repo);
6644 if (err)
6645 goto done;
6647 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6648 if (err)
6649 goto done;
6650 err = delete_ref(branch_ref_name, repo);
6651 if (err)
6652 goto done;
6654 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6655 if (err)
6656 goto done;
6657 err = delete_ref(commit_ref_name, repo);
6658 if (err)
6659 goto done;
6661 done:
6662 free(tmp_branch_name);
6663 free(new_base_branch_ref_name);
6664 free(branch_ref_name);
6665 free(commit_ref_name);
6666 return err;
6669 const struct got_error *
6670 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6671 struct got_object_id *new_commit_id, struct got_repository *repo)
6673 const struct got_error *err;
6674 struct got_reference *ref = NULL;
6675 struct got_object_id *old_commit_id = NULL;
6676 const char *branch_name = NULL;
6677 char *new_id_str = NULL;
6678 char *refname = NULL;
6680 branch_name = got_ref_get_name(branch);
6681 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6682 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6683 branch_name += 11;
6685 err = got_object_id_str(&new_id_str, new_commit_id);
6686 if (err)
6687 return err;
6689 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6690 new_id_str) == -1) {
6691 err = got_error_from_errno("asprintf");
6692 goto done;
6695 err = got_ref_resolve(&old_commit_id, repo, branch);
6696 if (err)
6697 goto done;
6699 err = got_ref_alloc(&ref, refname, old_commit_id);
6700 if (err)
6701 goto done;
6703 err = got_ref_write(ref, repo);
6704 done:
6705 free(new_id_str);
6706 free(refname);
6707 free(old_commit_id);
6708 if (ref)
6709 got_ref_close(ref);
6710 return err;
6713 const struct got_error *
6714 got_worktree_rebase_complete(struct got_worktree *worktree,
6715 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6716 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6717 struct got_repository *repo, int create_backup)
6719 const struct got_error *err, *unlockerr, *sync_err;
6720 struct got_object_id *new_head_commit_id = NULL;
6721 char *fileindex_path = NULL;
6723 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6724 if (err)
6725 return err;
6727 if (create_backup) {
6728 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6729 rebased_branch, new_head_commit_id, repo);
6730 if (err)
6731 goto done;
6734 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6735 if (err)
6736 goto done;
6738 err = got_ref_write(rebased_branch, repo);
6739 if (err)
6740 goto done;
6742 err = got_worktree_set_head_ref(worktree, rebased_branch);
6743 if (err)
6744 goto done;
6746 err = delete_rebase_refs(worktree, repo);
6747 if (err)
6748 goto done;
6750 err = get_fileindex_path(&fileindex_path, worktree);
6751 if (err)
6752 goto done;
6753 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6754 sync_err = sync_fileindex(fileindex, fileindex_path);
6755 if (sync_err && err == NULL)
6756 err = sync_err;
6757 done:
6758 got_fileindex_free(fileindex);
6759 free(fileindex_path);
6760 free(new_head_commit_id);
6761 unlockerr = lock_worktree(worktree, LOCK_SH);
6762 if (unlockerr && err == NULL)
6763 err = unlockerr;
6764 return err;
6767 const struct got_error *
6768 got_worktree_rebase_abort(struct got_worktree *worktree,
6769 struct got_fileindex *fileindex, struct got_repository *repo,
6770 struct got_reference *new_base_branch,
6771 got_worktree_checkout_cb progress_cb, void *progress_arg)
6773 const struct got_error *err, *unlockerr, *sync_err;
6774 struct got_reference *resolved = NULL;
6775 struct got_object_id *commit_id = NULL;
6776 char *fileindex_path = NULL;
6777 struct revert_file_args rfa;
6778 struct got_object_id *tree_id = NULL;
6780 err = lock_worktree(worktree, LOCK_EX);
6781 if (err)
6782 return err;
6784 err = got_ref_open(&resolved, repo,
6785 got_ref_get_symref_target(new_base_branch), 0);
6786 if (err)
6787 goto done;
6789 err = got_worktree_set_head_ref(worktree, resolved);
6790 if (err)
6791 goto done;
6794 * XXX commits to the base branch could have happened while
6795 * we were busy rebasing; should we store the original commit ID
6796 * when rebase begins and read it back here?
6798 err = got_ref_resolve(&commit_id, repo, resolved);
6799 if (err)
6800 goto done;
6802 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6803 if (err)
6804 goto done;
6806 err = got_object_id_by_path(&tree_id, repo,
6807 worktree->base_commit_id, worktree->path_prefix);
6808 if (err)
6809 goto done;
6811 err = delete_rebase_refs(worktree, repo);
6812 if (err)
6813 goto done;
6815 err = get_fileindex_path(&fileindex_path, worktree);
6816 if (err)
6817 goto done;
6819 rfa.worktree = worktree;
6820 rfa.fileindex = fileindex;
6821 rfa.progress_cb = progress_cb;
6822 rfa.progress_arg = progress_arg;
6823 rfa.patch_cb = NULL;
6824 rfa.patch_arg = NULL;
6825 rfa.repo = repo;
6826 rfa.unlink_added_files = 0;
6827 err = worktree_status(worktree, "", fileindex, repo,
6828 revert_file, &rfa, NULL, NULL, 0, 0);
6829 if (err)
6830 goto sync;
6832 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6833 repo, progress_cb, progress_arg, NULL, NULL);
6834 sync:
6835 sync_err = sync_fileindex(fileindex, fileindex_path);
6836 if (sync_err && err == NULL)
6837 err = sync_err;
6838 done:
6839 got_ref_close(resolved);
6840 free(tree_id);
6841 free(commit_id);
6842 if (fileindex)
6843 got_fileindex_free(fileindex);
6844 free(fileindex_path);
6846 unlockerr = lock_worktree(worktree, LOCK_SH);
6847 if (unlockerr && err == NULL)
6848 err = unlockerr;
6849 return err;
6852 const struct got_error *
6853 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6854 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6855 struct got_fileindex **fileindex, struct got_worktree *worktree,
6856 struct got_repository *repo)
6858 const struct got_error *err = NULL;
6859 char *tmp_branch_name = NULL;
6860 char *branch_ref_name = NULL;
6861 char *base_commit_ref_name = NULL;
6862 char *fileindex_path = NULL;
6863 struct check_rebase_ok_arg ok_arg;
6864 struct got_reference *wt_branch = NULL;
6865 struct got_reference *base_commit_ref = NULL;
6867 *tmp_branch = NULL;
6868 *branch_ref = NULL;
6869 *base_commit_id = NULL;
6870 *fileindex = NULL;
6872 err = lock_worktree(worktree, LOCK_EX);
6873 if (err)
6874 return err;
6876 err = open_fileindex(fileindex, &fileindex_path, worktree);
6877 if (err)
6878 goto done;
6880 ok_arg.worktree = worktree;
6881 ok_arg.repo = repo;
6882 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6883 &ok_arg);
6884 if (err)
6885 goto done;
6887 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6888 if (err)
6889 goto done;
6891 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6892 if (err)
6893 goto done;
6895 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6896 worktree);
6897 if (err)
6898 goto done;
6900 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6901 0);
6902 if (err)
6903 goto done;
6905 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6906 if (err)
6907 goto done;
6909 err = got_ref_write(*branch_ref, repo);
6910 if (err)
6911 goto done;
6913 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6914 worktree->base_commit_id);
6915 if (err)
6916 goto done;
6917 err = got_ref_write(base_commit_ref, repo);
6918 if (err)
6919 goto done;
6920 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6921 if (*base_commit_id == NULL) {
6922 err = got_error_from_errno("got_object_id_dup");
6923 goto done;
6926 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6927 worktree->base_commit_id);
6928 if (err)
6929 goto done;
6930 err = got_ref_write(*tmp_branch, repo);
6931 if (err)
6932 goto done;
6934 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6935 if (err)
6936 goto done;
6937 done:
6938 free(fileindex_path);
6939 free(tmp_branch_name);
6940 free(branch_ref_name);
6941 free(base_commit_ref_name);
6942 if (wt_branch)
6943 got_ref_close(wt_branch);
6944 if (err) {
6945 if (*branch_ref) {
6946 got_ref_close(*branch_ref);
6947 *branch_ref = NULL;
6949 if (*tmp_branch) {
6950 got_ref_close(*tmp_branch);
6951 *tmp_branch = NULL;
6953 free(*base_commit_id);
6954 if (*fileindex) {
6955 got_fileindex_free(*fileindex);
6956 *fileindex = NULL;
6958 lock_worktree(worktree, LOCK_SH);
6960 return err;
6963 const struct got_error *
6964 got_worktree_histedit_postpone(struct got_worktree *worktree,
6965 struct got_fileindex *fileindex)
6967 if (fileindex)
6968 got_fileindex_free(fileindex);
6969 return lock_worktree(worktree, LOCK_SH);
6972 const struct got_error *
6973 got_worktree_histedit_in_progress(int *in_progress,
6974 struct got_worktree *worktree)
6976 const struct got_error *err;
6977 char *tmp_branch_name = NULL;
6979 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6980 if (err)
6981 return err;
6983 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6984 free(tmp_branch_name);
6985 return NULL;
6988 const struct got_error *
6989 got_worktree_histedit_continue(struct got_object_id **commit_id,
6990 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6991 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6992 struct got_worktree *worktree, struct got_repository *repo)
6994 const struct got_error *err;
6995 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6996 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6997 struct got_reference *commit_ref = NULL;
6998 struct got_reference *base_commit_ref = NULL;
6999 char *fileindex_path = NULL;
7000 int have_staged_files = 0;
7002 *commit_id = NULL;
7003 *tmp_branch = NULL;
7004 *base_commit_id = NULL;
7005 *fileindex = NULL;
7007 err = lock_worktree(worktree, LOCK_EX);
7008 if (err)
7009 return err;
7011 err = open_fileindex(fileindex, &fileindex_path, worktree);
7012 if (err)
7013 goto done;
7015 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7016 &have_staged_files);
7017 if (err && err->code != GOT_ERR_CANCELLED)
7018 goto done;
7019 if (have_staged_files) {
7020 err = got_error(GOT_ERR_STAGED_PATHS);
7021 goto done;
7024 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7025 if (err)
7026 goto done;
7028 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7029 if (err)
7030 goto done;
7032 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7033 if (err)
7034 goto done;
7036 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7037 worktree);
7038 if (err)
7039 goto done;
7041 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7042 if (err)
7043 goto done;
7045 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7046 if (err)
7047 goto done;
7048 err = got_ref_resolve(commit_id, repo, commit_ref);
7049 if (err)
7050 goto done;
7052 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7053 if (err)
7054 goto done;
7055 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7056 if (err)
7057 goto done;
7059 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7060 if (err)
7061 goto done;
7062 done:
7063 free(commit_ref_name);
7064 free(branch_ref_name);
7065 free(fileindex_path);
7066 if (commit_ref)
7067 got_ref_close(commit_ref);
7068 if (base_commit_ref)
7069 got_ref_close(base_commit_ref);
7070 if (err) {
7071 free(*commit_id);
7072 *commit_id = NULL;
7073 free(*base_commit_id);
7074 *base_commit_id = NULL;
7075 if (*tmp_branch) {
7076 got_ref_close(*tmp_branch);
7077 *tmp_branch = NULL;
7079 if (*fileindex) {
7080 got_fileindex_free(*fileindex);
7081 *fileindex = NULL;
7083 lock_worktree(worktree, LOCK_EX);
7085 return err;
7088 static const struct got_error *
7089 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7091 const struct got_error *err;
7092 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7093 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7095 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7096 if (err)
7097 goto done;
7098 err = delete_ref(tmp_branch_name, repo);
7099 if (err)
7100 goto done;
7102 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7103 worktree);
7104 if (err)
7105 goto done;
7106 err = delete_ref(base_commit_ref_name, repo);
7107 if (err)
7108 goto done;
7110 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7111 if (err)
7112 goto done;
7113 err = delete_ref(branch_ref_name, repo);
7114 if (err)
7115 goto done;
7117 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7118 if (err)
7119 goto done;
7120 err = delete_ref(commit_ref_name, repo);
7121 if (err)
7122 goto done;
7123 done:
7124 free(tmp_branch_name);
7125 free(base_commit_ref_name);
7126 free(branch_ref_name);
7127 free(commit_ref_name);
7128 return err;
7131 const struct got_error *
7132 got_worktree_histedit_abort(struct got_worktree *worktree,
7133 struct got_fileindex *fileindex, struct got_repository *repo,
7134 struct got_reference *branch, struct got_object_id *base_commit_id,
7135 got_worktree_checkout_cb progress_cb, void *progress_arg)
7137 const struct got_error *err, *unlockerr, *sync_err;
7138 struct got_reference *resolved = NULL;
7139 char *fileindex_path = NULL;
7140 struct got_object_id *tree_id = NULL;
7141 struct revert_file_args rfa;
7143 err = lock_worktree(worktree, LOCK_EX);
7144 if (err)
7145 return err;
7147 err = got_ref_open(&resolved, repo,
7148 got_ref_get_symref_target(branch), 0);
7149 if (err)
7150 goto done;
7152 err = got_worktree_set_head_ref(worktree, resolved);
7153 if (err)
7154 goto done;
7156 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7157 if (err)
7158 goto done;
7160 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7161 worktree->path_prefix);
7162 if (err)
7163 goto done;
7165 err = delete_histedit_refs(worktree, repo);
7166 if (err)
7167 goto done;
7169 err = get_fileindex_path(&fileindex_path, worktree);
7170 if (err)
7171 goto done;
7173 rfa.worktree = worktree;
7174 rfa.fileindex = fileindex;
7175 rfa.progress_cb = progress_cb;
7176 rfa.progress_arg = progress_arg;
7177 rfa.patch_cb = NULL;
7178 rfa.patch_arg = NULL;
7179 rfa.repo = repo;
7180 rfa.unlink_added_files = 0;
7181 err = worktree_status(worktree, "", fileindex, repo,
7182 revert_file, &rfa, NULL, NULL, 0, 0);
7183 if (err)
7184 goto sync;
7186 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7187 repo, progress_cb, progress_arg, NULL, NULL);
7188 sync:
7189 sync_err = sync_fileindex(fileindex, fileindex_path);
7190 if (sync_err && err == NULL)
7191 err = sync_err;
7192 done:
7193 got_ref_close(resolved);
7194 free(tree_id);
7195 free(fileindex_path);
7197 unlockerr = lock_worktree(worktree, LOCK_SH);
7198 if (unlockerr && err == NULL)
7199 err = unlockerr;
7200 return err;
7203 const struct got_error *
7204 got_worktree_histedit_complete(struct got_worktree *worktree,
7205 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7206 struct got_reference *edited_branch, struct got_repository *repo)
7208 const struct got_error *err, *unlockerr, *sync_err;
7209 struct got_object_id *new_head_commit_id = NULL;
7210 struct got_reference *resolved = NULL;
7211 char *fileindex_path = NULL;
7213 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7214 if (err)
7215 return err;
7217 err = got_ref_open(&resolved, repo,
7218 got_ref_get_symref_target(edited_branch), 0);
7219 if (err)
7220 goto done;
7222 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7223 resolved, new_head_commit_id, repo);
7224 if (err)
7225 goto done;
7227 err = got_ref_change_ref(resolved, new_head_commit_id);
7228 if (err)
7229 goto done;
7231 err = got_ref_write(resolved, repo);
7232 if (err)
7233 goto done;
7235 err = got_worktree_set_head_ref(worktree, resolved);
7236 if (err)
7237 goto done;
7239 err = delete_histedit_refs(worktree, repo);
7240 if (err)
7241 goto done;
7243 err = get_fileindex_path(&fileindex_path, worktree);
7244 if (err)
7245 goto done;
7246 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7247 sync_err = sync_fileindex(fileindex, fileindex_path);
7248 if (sync_err && err == NULL)
7249 err = sync_err;
7250 done:
7251 got_fileindex_free(fileindex);
7252 free(fileindex_path);
7253 free(new_head_commit_id);
7254 unlockerr = lock_worktree(worktree, LOCK_SH);
7255 if (unlockerr && err == NULL)
7256 err = unlockerr;
7257 return err;
7260 const struct got_error *
7261 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7262 struct got_object_id *commit_id, struct got_repository *repo)
7264 const struct got_error *err;
7265 char *commit_ref_name;
7267 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7268 if (err)
7269 return err;
7271 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7272 if (err)
7273 goto done;
7275 err = delete_ref(commit_ref_name, repo);
7276 done:
7277 free(commit_ref_name);
7278 return err;
7281 const struct got_error *
7282 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7283 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7284 struct got_worktree *worktree, const char *refname,
7285 struct got_repository *repo)
7287 const struct got_error *err = NULL;
7288 char *fileindex_path = NULL;
7289 struct check_rebase_ok_arg ok_arg;
7291 *fileindex = NULL;
7292 *branch_ref = NULL;
7293 *base_branch_ref = NULL;
7295 err = lock_worktree(worktree, LOCK_EX);
7296 if (err)
7297 return err;
7299 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7300 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7301 "cannot integrate a branch into itself; "
7302 "update -b or different branch name required");
7303 goto done;
7306 err = open_fileindex(fileindex, &fileindex_path, worktree);
7307 if (err)
7308 goto done;
7310 /* Preconditions are the same as for rebase. */
7311 ok_arg.worktree = worktree;
7312 ok_arg.repo = repo;
7313 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7314 &ok_arg);
7315 if (err)
7316 goto done;
7318 err = got_ref_open(branch_ref, repo, refname, 1);
7319 if (err)
7320 goto done;
7322 err = got_ref_open(base_branch_ref, repo,
7323 got_worktree_get_head_ref_name(worktree), 1);
7324 done:
7325 if (err) {
7326 if (*branch_ref) {
7327 got_ref_close(*branch_ref);
7328 *branch_ref = NULL;
7330 if (*base_branch_ref) {
7331 got_ref_close(*base_branch_ref);
7332 *base_branch_ref = NULL;
7334 if (*fileindex) {
7335 got_fileindex_free(*fileindex);
7336 *fileindex = NULL;
7338 lock_worktree(worktree, LOCK_SH);
7340 return err;
7343 const struct got_error *
7344 got_worktree_integrate_continue(struct got_worktree *worktree,
7345 struct got_fileindex *fileindex, struct got_repository *repo,
7346 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7347 got_worktree_checkout_cb progress_cb, void *progress_arg,
7348 got_cancel_cb cancel_cb, void *cancel_arg)
7350 const struct got_error *err = NULL, *sync_err, *unlockerr;
7351 char *fileindex_path = NULL;
7352 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7354 err = get_fileindex_path(&fileindex_path, worktree);
7355 if (err)
7356 goto done;
7358 err = got_ref_resolve(&commit_id, repo, branch_ref);
7359 if (err)
7360 goto done;
7362 err = got_object_id_by_path(&tree_id, repo, commit_id,
7363 worktree->path_prefix);
7364 if (err)
7365 goto done;
7367 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7368 if (err)
7369 goto done;
7371 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7372 progress_cb, progress_arg, cancel_cb, cancel_arg);
7373 if (err)
7374 goto sync;
7376 err = got_ref_change_ref(base_branch_ref, commit_id);
7377 if (err)
7378 goto sync;
7380 err = got_ref_write(base_branch_ref, repo);
7381 if (err)
7382 goto sync;
7384 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7385 sync:
7386 sync_err = sync_fileindex(fileindex, fileindex_path);
7387 if (sync_err && err == NULL)
7388 err = sync_err;
7390 done:
7391 unlockerr = got_ref_unlock(branch_ref);
7392 if (unlockerr && err == NULL)
7393 err = unlockerr;
7394 got_ref_close(branch_ref);
7396 unlockerr = got_ref_unlock(base_branch_ref);
7397 if (unlockerr && err == NULL)
7398 err = unlockerr;
7399 got_ref_close(base_branch_ref);
7401 got_fileindex_free(fileindex);
7402 free(fileindex_path);
7403 free(tree_id);
7405 unlockerr = lock_worktree(worktree, LOCK_SH);
7406 if (unlockerr && err == NULL)
7407 err = unlockerr;
7408 return err;
7411 const struct got_error *
7412 got_worktree_integrate_abort(struct got_worktree *worktree,
7413 struct got_fileindex *fileindex, struct got_repository *repo,
7414 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7416 const struct got_error *err = NULL, *unlockerr = NULL;
7418 got_fileindex_free(fileindex);
7420 err = lock_worktree(worktree, LOCK_SH);
7422 unlockerr = got_ref_unlock(branch_ref);
7423 if (unlockerr && err == NULL)
7424 err = unlockerr;
7425 got_ref_close(branch_ref);
7427 unlockerr = got_ref_unlock(base_branch_ref);
7428 if (unlockerr && err == NULL)
7429 err = unlockerr;
7430 got_ref_close(base_branch_ref);
7432 return err;
7435 const struct got_error *
7436 got_worktree_merge_postpone(struct got_worktree *worktree,
7437 struct got_fileindex *fileindex)
7439 const struct got_error *err, *sync_err;
7440 char *fileindex_path = NULL;
7442 err = get_fileindex_path(&fileindex_path, worktree);
7443 if (err)
7444 goto done;
7446 sync_err = sync_fileindex(fileindex, fileindex_path);
7448 err = lock_worktree(worktree, LOCK_SH);
7449 if (sync_err && err == NULL)
7450 err = sync_err;
7451 done:
7452 got_fileindex_free(fileindex);
7453 free(fileindex_path);
7454 return err;
7457 static const struct got_error *
7458 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7460 const struct got_error *err;
7461 char *branch_refname = NULL, *commit_refname = NULL;
7463 err = get_merge_branch_ref_name(&branch_refname, worktree);
7464 if (err)
7465 goto done;
7466 err = delete_ref(branch_refname, repo);
7467 if (err)
7468 goto done;
7470 err = get_merge_commit_ref_name(&commit_refname, worktree);
7471 if (err)
7472 goto done;
7473 err = delete_ref(commit_refname, repo);
7474 if (err)
7475 goto done;
7477 done:
7478 free(branch_refname);
7479 free(commit_refname);
7480 return err;
7483 struct merge_commit_msg_arg {
7484 struct got_worktree *worktree;
7485 const char *branch_name;
7488 static const struct got_error *
7489 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7490 void *arg)
7492 struct merge_commit_msg_arg *a = arg;
7494 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7495 got_worktree_get_head_ref_name(a->worktree)) == -1)
7496 return got_error_from_errno("asprintf");
7498 return NULL;
7501 static const struct got_error *
7502 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7503 const char *path, struct got_object_id *blob_id,
7504 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7505 int dirfd, const char *de_name)
7507 return NULL;
7510 const struct got_error *
7511 got_worktree_merge_branch(struct got_worktree *worktree,
7512 struct got_fileindex *fileindex,
7513 struct got_object_id *yca_commit_id,
7514 struct got_object_id *branch_tip,
7515 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7516 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7518 const struct got_error *err;
7519 char *fileindex_path = NULL;
7521 err = get_fileindex_path(&fileindex_path, worktree);
7522 if (err)
7523 goto done;
7525 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7526 worktree);
7527 if (err)
7528 goto done;
7530 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7531 branch_tip, repo, progress_cb, progress_arg,
7532 cancel_cb, cancel_arg);
7533 done:
7534 free(fileindex_path);
7535 return err;
7538 const struct got_error *
7539 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7540 struct got_worktree *worktree, struct got_fileindex *fileindex,
7541 const char *author, const char *committer, int allow_bad_symlinks,
7542 struct got_object_id *branch_tip, const char *branch_name,
7543 struct got_repository *repo)
7545 const struct got_error *err = NULL, *sync_err;
7546 struct got_pathlist_head commitable_paths;
7547 struct collect_commitables_arg cc_arg;
7548 struct got_pathlist_entry *pe;
7549 struct got_reference *head_ref = NULL;
7550 struct got_object_id *head_commit_id = NULL;
7551 int have_staged_files = 0;
7552 struct merge_commit_msg_arg mcm_arg;
7553 char *fileindex_path = NULL;
7555 *new_commit_id = NULL;
7557 TAILQ_INIT(&commitable_paths);
7559 err = get_fileindex_path(&fileindex_path, worktree);
7560 if (err)
7561 goto done;
7563 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7564 if (err)
7565 goto done;
7567 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7568 if (err)
7569 goto done;
7571 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7572 &have_staged_files);
7573 if (err && err->code != GOT_ERR_CANCELLED)
7574 goto done;
7575 if (have_staged_files) {
7576 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7577 goto done;
7580 cc_arg.commitable_paths = &commitable_paths;
7581 cc_arg.worktree = worktree;
7582 cc_arg.fileindex = fileindex;
7583 cc_arg.repo = repo;
7584 cc_arg.have_staged_files = have_staged_files;
7585 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7586 err = worktree_status(worktree, "", fileindex, repo,
7587 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7588 if (err)
7589 goto done;
7591 if (TAILQ_EMPTY(&commitable_paths)) {
7592 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7593 "merge of %s cannot proceed", branch_name);
7594 goto done;
7597 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7598 struct got_commitable *ct = pe->data;
7599 const char *ct_path = ct->in_repo_path;
7601 while (ct_path[0] == '/')
7602 ct_path++;
7603 err = check_out_of_date(ct_path, ct->status,
7604 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7605 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7606 if (err)
7607 goto done;
7611 mcm_arg.worktree = worktree;
7612 mcm_arg.branch_name = branch_name;
7613 err = commit_worktree(new_commit_id, &commitable_paths,
7614 head_commit_id, branch_tip, worktree, author, committer,
7615 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7616 if (err)
7617 goto done;
7619 err = update_fileindex_after_commit(worktree, &commitable_paths,
7620 *new_commit_id, fileindex, have_staged_files);
7621 sync_err = sync_fileindex(fileindex, fileindex_path);
7622 if (sync_err && err == NULL)
7623 err = sync_err;
7624 done:
7625 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7626 struct got_commitable *ct = pe->data;
7627 free_commitable(ct);
7629 got_pathlist_free(&commitable_paths);
7630 free(fileindex_path);
7631 return err;
7634 const struct got_error *
7635 got_worktree_merge_complete(struct got_worktree *worktree,
7636 struct got_fileindex *fileindex, struct got_repository *repo)
7638 const struct got_error *err, *unlockerr, *sync_err;
7639 char *fileindex_path = NULL;
7641 err = delete_merge_refs(worktree, repo);
7642 if (err)
7643 goto done;
7645 err = get_fileindex_path(&fileindex_path, worktree);
7646 if (err)
7647 goto done;
7648 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7649 sync_err = sync_fileindex(fileindex, fileindex_path);
7650 if (sync_err && err == NULL)
7651 err = sync_err;
7652 done:
7653 got_fileindex_free(fileindex);
7654 free(fileindex_path);
7655 unlockerr = lock_worktree(worktree, LOCK_SH);
7656 if (unlockerr && err == NULL)
7657 err = unlockerr;
7658 return err;
7661 const struct got_error *
7662 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7663 struct got_repository *repo)
7665 const struct got_error *err;
7666 char *branch_refname = NULL;
7667 struct got_reference *branch_ref = NULL;
7669 *in_progress = 0;
7671 err = get_merge_branch_ref_name(&branch_refname, worktree);
7672 if (err)
7673 return err;
7674 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7675 free(branch_refname);
7676 if (err) {
7677 if (err->code != GOT_ERR_NOT_REF)
7678 return err;
7679 } else
7680 *in_progress = 1;
7682 return NULL;
7685 const struct got_error *got_worktree_merge_prepare(
7686 struct got_fileindex **fileindex, struct got_worktree *worktree,
7687 struct got_reference *branch, struct got_repository *repo)
7689 const struct got_error *err = NULL;
7690 char *fileindex_path = NULL;
7691 char *branch_refname = NULL, *commit_refname = NULL;
7692 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7693 struct got_reference *commit_ref = NULL;
7694 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7695 struct check_rebase_ok_arg ok_arg;
7697 *fileindex = NULL;
7699 err = lock_worktree(worktree, LOCK_EX);
7700 if (err)
7701 return err;
7703 err = open_fileindex(fileindex, &fileindex_path, worktree);
7704 if (err)
7705 goto done;
7707 /* Preconditions are the same as for rebase. */
7708 ok_arg.worktree = worktree;
7709 ok_arg.repo = repo;
7710 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7711 &ok_arg);
7712 if (err)
7713 goto done;
7715 err = get_merge_branch_ref_name(&branch_refname, worktree);
7716 if (err)
7717 return err;
7719 err = get_merge_commit_ref_name(&commit_refname, worktree);
7720 if (err)
7721 return err;
7723 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7724 0);
7725 if (err)
7726 goto done;
7728 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7729 if (err)
7730 goto done;
7732 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7733 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7734 goto done;
7737 err = got_ref_resolve(&branch_tip, repo, branch);
7738 if (err)
7739 goto done;
7741 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7742 if (err)
7743 goto done;
7744 err = got_ref_write(branch_ref, repo);
7745 if (err)
7746 goto done;
7748 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7749 if (err)
7750 goto done;
7751 err = got_ref_write(commit_ref, repo);
7752 if (err)
7753 goto done;
7755 done:
7756 free(branch_refname);
7757 free(commit_refname);
7758 free(fileindex_path);
7759 if (branch_ref)
7760 got_ref_close(branch_ref);
7761 if (commit_ref)
7762 got_ref_close(commit_ref);
7763 if (wt_branch)
7764 got_ref_close(wt_branch);
7765 free(wt_branch_tip);
7766 if (err) {
7767 if (*fileindex) {
7768 got_fileindex_free(*fileindex);
7769 *fileindex = NULL;
7771 lock_worktree(worktree, LOCK_SH);
7773 return err;
7776 const struct got_error *
7777 got_worktree_merge_continue(char **branch_name,
7778 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7779 struct got_worktree *worktree, struct got_repository *repo)
7781 const struct got_error *err;
7782 char *commit_refname = NULL, *branch_refname = NULL;
7783 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7784 char *fileindex_path = NULL;
7785 int have_staged_files = 0;
7787 *branch_name = NULL;
7788 *branch_tip = NULL;
7789 *fileindex = NULL;
7791 err = lock_worktree(worktree, LOCK_EX);
7792 if (err)
7793 return err;
7795 err = open_fileindex(fileindex, &fileindex_path, worktree);
7796 if (err)
7797 goto done;
7799 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7800 &have_staged_files);
7801 if (err && err->code != GOT_ERR_CANCELLED)
7802 goto done;
7803 if (have_staged_files) {
7804 err = got_error(GOT_ERR_STAGED_PATHS);
7805 goto done;
7808 err = get_merge_branch_ref_name(&branch_refname, worktree);
7809 if (err)
7810 goto done;
7812 err = get_merge_commit_ref_name(&commit_refname, worktree);
7813 if (err)
7814 goto done;
7816 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7817 if (err)
7818 goto done;
7820 if (!got_ref_is_symbolic(branch_ref)) {
7821 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7822 "%s is not a symbolic reference",
7823 got_ref_get_name(branch_ref));
7824 goto done;
7826 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7827 if (*branch_name == NULL) {
7828 err = got_error_from_errno("strdup");
7829 goto done;
7832 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7833 if (err)
7834 goto done;
7836 err = got_ref_resolve(branch_tip, repo, commit_ref);
7837 if (err)
7838 goto done;
7839 done:
7840 free(commit_refname);
7841 free(branch_refname);
7842 free(fileindex_path);
7843 if (commit_ref)
7844 got_ref_close(commit_ref);
7845 if (branch_ref)
7846 got_ref_close(branch_ref);
7847 if (err) {
7848 if (*branch_name) {
7849 free(*branch_name);
7850 *branch_name = NULL;
7852 free(*branch_tip);
7853 *branch_tip = NULL;
7854 if (*fileindex) {
7855 got_fileindex_free(*fileindex);
7856 *fileindex = NULL;
7858 lock_worktree(worktree, LOCK_SH);
7860 return err;
7863 const struct got_error *
7864 got_worktree_merge_abort(struct got_worktree *worktree,
7865 struct got_fileindex *fileindex, struct got_repository *repo,
7866 got_worktree_checkout_cb progress_cb, void *progress_arg)
7868 const struct got_error *err, *unlockerr, *sync_err;
7869 struct got_object_id *commit_id = NULL;
7870 char *fileindex_path = NULL;
7871 struct revert_file_args rfa;
7872 struct got_object_id *tree_id = NULL;
7874 err = got_object_id_by_path(&tree_id, repo,
7875 worktree->base_commit_id, worktree->path_prefix);
7876 if (err)
7877 goto done;
7879 err = delete_merge_refs(worktree, repo);
7880 if (err)
7881 goto done;
7883 err = get_fileindex_path(&fileindex_path, worktree);
7884 if (err)
7885 goto done;
7887 rfa.worktree = worktree;
7888 rfa.fileindex = fileindex;
7889 rfa.progress_cb = progress_cb;
7890 rfa.progress_arg = progress_arg;
7891 rfa.patch_cb = NULL;
7892 rfa.patch_arg = NULL;
7893 rfa.repo = repo;
7894 rfa.unlink_added_files = 1;
7895 err = worktree_status(worktree, "", fileindex, repo,
7896 revert_file, &rfa, NULL, NULL, 0, 0);
7897 if (err)
7898 goto sync;
7900 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7901 repo, progress_cb, progress_arg, NULL, NULL);
7902 sync:
7903 sync_err = sync_fileindex(fileindex, fileindex_path);
7904 if (sync_err && err == NULL)
7905 err = sync_err;
7906 done:
7907 free(tree_id);
7908 free(commit_id);
7909 if (fileindex)
7910 got_fileindex_free(fileindex);
7911 free(fileindex_path);
7913 unlockerr = lock_worktree(worktree, LOCK_SH);
7914 if (unlockerr && err == NULL)
7915 err = unlockerr;
7916 return err;
7919 struct check_stage_ok_arg {
7920 struct got_object_id *head_commit_id;
7921 struct got_worktree *worktree;
7922 struct got_fileindex *fileindex;
7923 struct got_repository *repo;
7924 int have_changes;
7927 const struct got_error *
7928 check_stage_ok(void *arg, unsigned char status,
7929 unsigned char staged_status, const char *relpath,
7930 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7931 struct got_object_id *commit_id, int dirfd, const char *de_name)
7933 struct check_stage_ok_arg *a = arg;
7934 const struct got_error *err = NULL;
7935 struct got_fileindex_entry *ie;
7936 struct got_object_id base_commit_id;
7937 struct got_object_id *base_commit_idp = NULL;
7938 char *in_repo_path = NULL, *p;
7940 if (status == GOT_STATUS_UNVERSIONED ||
7941 status == GOT_STATUS_NO_CHANGE)
7942 return NULL;
7943 if (status == GOT_STATUS_NONEXISTENT)
7944 return got_error_set_errno(ENOENT, relpath);
7946 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7947 if (ie == NULL)
7948 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7950 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7951 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7952 relpath) == -1)
7953 return got_error_from_errno("asprintf");
7955 if (got_fileindex_entry_has_commit(ie)) {
7956 memcpy(base_commit_id.sha1, ie->commit_sha1,
7957 SHA1_DIGEST_LENGTH);
7958 base_commit_idp = &base_commit_id;
7961 if (status == GOT_STATUS_CONFLICT) {
7962 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7963 goto done;
7964 } else if (status != GOT_STATUS_ADD &&
7965 status != GOT_STATUS_MODIFY &&
7966 status != GOT_STATUS_DELETE) {
7967 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7968 goto done;
7971 a->have_changes = 1;
7973 p = in_repo_path;
7974 while (p[0] == '/')
7975 p++;
7976 err = check_out_of_date(p, status, staged_status,
7977 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7978 GOT_ERR_STAGE_OUT_OF_DATE);
7979 done:
7980 free(in_repo_path);
7981 return err;
7984 struct stage_path_arg {
7985 struct got_worktree *worktree;
7986 struct got_fileindex *fileindex;
7987 struct got_repository *repo;
7988 got_worktree_status_cb status_cb;
7989 void *status_arg;
7990 got_worktree_patch_cb patch_cb;
7991 void *patch_arg;
7992 int staged_something;
7993 int allow_bad_symlinks;
7996 static const struct got_error *
7997 stage_path(void *arg, unsigned char status,
7998 unsigned char staged_status, const char *relpath,
7999 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8000 struct got_object_id *commit_id, int dirfd, const char *de_name)
8002 struct stage_path_arg *a = arg;
8003 const struct got_error *err = NULL;
8004 struct got_fileindex_entry *ie;
8005 char *ondisk_path = NULL, *path_content = NULL;
8006 uint32_t stage;
8007 struct got_object_id *new_staged_blob_id = NULL;
8008 struct stat sb;
8010 if (status == GOT_STATUS_UNVERSIONED)
8011 return NULL;
8013 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8014 if (ie == NULL)
8015 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8017 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8018 relpath)== -1)
8019 return got_error_from_errno("asprintf");
8021 switch (status) {
8022 case GOT_STATUS_ADD:
8023 case GOT_STATUS_MODIFY:
8024 /* XXX could sb.st_mode be passed in by our caller? */
8025 if (lstat(ondisk_path, &sb) == -1) {
8026 err = got_error_from_errno2("lstat", ondisk_path);
8027 break;
8029 if (a->patch_cb) {
8030 if (status == GOT_STATUS_ADD) {
8031 int choice = GOT_PATCH_CHOICE_NONE;
8032 err = (*a->patch_cb)(&choice, a->patch_arg,
8033 status, ie->path, NULL, 1, 1);
8034 if (err)
8035 break;
8036 if (choice != GOT_PATCH_CHOICE_YES)
8037 break;
8038 } else {
8039 err = create_patched_content(&path_content, 0,
8040 staged_blob_id ? staged_blob_id : blob_id,
8041 ondisk_path, dirfd, de_name, ie->path,
8042 a->repo, a->patch_cb, a->patch_arg);
8043 if (err || path_content == NULL)
8044 break;
8047 err = got_object_blob_create(&new_staged_blob_id,
8048 path_content ? path_content : ondisk_path, a->repo);
8049 if (err)
8050 break;
8051 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8052 SHA1_DIGEST_LENGTH);
8053 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8054 stage = GOT_FILEIDX_STAGE_ADD;
8055 else
8056 stage = GOT_FILEIDX_STAGE_MODIFY;
8057 got_fileindex_entry_stage_set(ie, stage);
8058 if (S_ISLNK(sb.st_mode)) {
8059 int is_bad_symlink = 0;
8060 if (!a->allow_bad_symlinks) {
8061 char target_path[PATH_MAX];
8062 ssize_t target_len;
8063 target_len = readlink(ondisk_path, target_path,
8064 sizeof(target_path));
8065 if (target_len == -1) {
8066 err = got_error_from_errno2("readlink",
8067 ondisk_path);
8068 break;
8070 err = is_bad_symlink_target(&is_bad_symlink,
8071 target_path, target_len, ondisk_path,
8072 a->worktree->root_path);
8073 if (err)
8074 break;
8075 if (is_bad_symlink) {
8076 err = got_error_path(ondisk_path,
8077 GOT_ERR_BAD_SYMLINK);
8078 break;
8081 if (is_bad_symlink)
8082 got_fileindex_entry_staged_filetype_set(ie,
8083 GOT_FILEIDX_MODE_BAD_SYMLINK);
8084 else
8085 got_fileindex_entry_staged_filetype_set(ie,
8086 GOT_FILEIDX_MODE_SYMLINK);
8087 } else {
8088 got_fileindex_entry_staged_filetype_set(ie,
8089 GOT_FILEIDX_MODE_REGULAR_FILE);
8091 a->staged_something = 1;
8092 if (a->status_cb == NULL)
8093 break;
8094 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8095 get_staged_status(ie), relpath, blob_id,
8096 new_staged_blob_id, NULL, dirfd, de_name);
8097 break;
8098 case GOT_STATUS_DELETE:
8099 if (staged_status == GOT_STATUS_DELETE)
8100 break;
8101 if (a->patch_cb) {
8102 int choice = GOT_PATCH_CHOICE_NONE;
8103 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8104 ie->path, NULL, 1, 1);
8105 if (err)
8106 break;
8107 if (choice == GOT_PATCH_CHOICE_NO)
8108 break;
8109 if (choice != GOT_PATCH_CHOICE_YES) {
8110 err = got_error(GOT_ERR_PATCH_CHOICE);
8111 break;
8114 stage = GOT_FILEIDX_STAGE_DELETE;
8115 got_fileindex_entry_stage_set(ie, stage);
8116 a->staged_something = 1;
8117 if (a->status_cb == NULL)
8118 break;
8119 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8120 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8121 de_name);
8122 break;
8123 case GOT_STATUS_NO_CHANGE:
8124 break;
8125 case GOT_STATUS_CONFLICT:
8126 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8127 break;
8128 case GOT_STATUS_NONEXISTENT:
8129 err = got_error_set_errno(ENOENT, relpath);
8130 break;
8131 default:
8132 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8133 break;
8136 if (path_content && unlink(path_content) == -1 && err == NULL)
8137 err = got_error_from_errno2("unlink", path_content);
8138 free(path_content);
8139 free(ondisk_path);
8140 free(new_staged_blob_id);
8141 return err;
8144 const struct got_error *
8145 got_worktree_stage(struct got_worktree *worktree,
8146 struct got_pathlist_head *paths,
8147 got_worktree_status_cb status_cb, void *status_arg,
8148 got_worktree_patch_cb patch_cb, void *patch_arg,
8149 int allow_bad_symlinks, struct got_repository *repo)
8151 const struct got_error *err = NULL, *sync_err, *unlockerr;
8152 struct got_pathlist_entry *pe;
8153 struct got_fileindex *fileindex = NULL;
8154 char *fileindex_path = NULL;
8155 struct got_reference *head_ref = NULL;
8156 struct got_object_id *head_commit_id = NULL;
8157 struct check_stage_ok_arg oka;
8158 struct stage_path_arg spa;
8160 err = lock_worktree(worktree, LOCK_EX);
8161 if (err)
8162 return err;
8164 err = got_ref_open(&head_ref, repo,
8165 got_worktree_get_head_ref_name(worktree), 0);
8166 if (err)
8167 goto done;
8168 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8169 if (err)
8170 goto done;
8171 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8172 if (err)
8173 goto done;
8175 /* Check pre-conditions before staging anything. */
8176 oka.head_commit_id = head_commit_id;
8177 oka.worktree = worktree;
8178 oka.fileindex = fileindex;
8179 oka.repo = repo;
8180 oka.have_changes = 0;
8181 TAILQ_FOREACH(pe, paths, entry) {
8182 err = worktree_status(worktree, pe->path, fileindex, repo,
8183 check_stage_ok, &oka, NULL, NULL, 0, 0);
8184 if (err)
8185 goto done;
8187 if (!oka.have_changes) {
8188 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8189 goto done;
8192 spa.worktree = worktree;
8193 spa.fileindex = fileindex;
8194 spa.repo = repo;
8195 spa.patch_cb = patch_cb;
8196 spa.patch_arg = patch_arg;
8197 spa.status_cb = status_cb;
8198 spa.status_arg = status_arg;
8199 spa.staged_something = 0;
8200 spa.allow_bad_symlinks = allow_bad_symlinks;
8201 TAILQ_FOREACH(pe, paths, entry) {
8202 err = worktree_status(worktree, pe->path, fileindex, repo,
8203 stage_path, &spa, NULL, NULL, 0, 0);
8204 if (err)
8205 goto done;
8207 if (!spa.staged_something) {
8208 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8209 goto done;
8212 sync_err = sync_fileindex(fileindex, fileindex_path);
8213 if (sync_err && err == NULL)
8214 err = sync_err;
8215 done:
8216 if (head_ref)
8217 got_ref_close(head_ref);
8218 free(head_commit_id);
8219 free(fileindex_path);
8220 if (fileindex)
8221 got_fileindex_free(fileindex);
8222 unlockerr = lock_worktree(worktree, LOCK_SH);
8223 if (unlockerr && err == NULL)
8224 err = unlockerr;
8225 return err;
8228 struct unstage_path_arg {
8229 struct got_worktree *worktree;
8230 struct got_fileindex *fileindex;
8231 struct got_repository *repo;
8232 got_worktree_checkout_cb progress_cb;
8233 void *progress_arg;
8234 got_worktree_patch_cb patch_cb;
8235 void *patch_arg;
8238 static const struct got_error *
8239 create_unstaged_content(char **path_unstaged_content,
8240 char **path_new_staged_content, struct got_object_id *blob_id,
8241 struct got_object_id *staged_blob_id, const char *relpath,
8242 struct got_repository *repo,
8243 got_worktree_patch_cb patch_cb, void *patch_arg)
8245 const struct got_error *err, *free_err;
8246 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8247 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8248 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8249 struct got_diffreg_result *diffreg_result = NULL;
8250 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8251 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8253 *path_unstaged_content = NULL;
8254 *path_new_staged_content = NULL;
8256 err = got_object_id_str(&label1, blob_id);
8257 if (err)
8258 return err;
8259 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8260 if (err)
8261 goto done;
8263 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8264 if (err)
8265 goto done;
8267 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8268 if (err)
8269 goto done;
8271 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8272 if (err)
8273 goto done;
8275 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8276 if (err)
8277 goto done;
8279 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8280 if (err)
8281 goto done;
8283 err = got_diff_files(&diffreg_result, f1, label1, f2,
8284 path2, 3, 0, 1, NULL);
8285 if (err)
8286 goto done;
8288 err = got_opentemp_named(path_unstaged_content, &outfile,
8289 "got-unstaged-content");
8290 if (err)
8291 goto done;
8292 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8293 "got-new-staged-content");
8294 if (err)
8295 goto done;
8297 if (fseek(f1, 0L, SEEK_SET) == -1) {
8298 err = got_ferror(f1, GOT_ERR_IO);
8299 goto done;
8301 if (fseek(f2, 0L, SEEK_SET) == -1) {
8302 err = got_ferror(f2, GOT_ERR_IO);
8303 goto done;
8305 /* Count the number of actual changes in the diff result. */
8306 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8307 struct diff_chunk_context cc = {};
8308 diff_chunk_context_load_change(&cc, &nchunks_used,
8309 diffreg_result->result, n, 0);
8310 nchanges++;
8312 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8313 int choice;
8314 err = apply_or_reject_change(&choice, &nchunks_used,
8315 diffreg_result->result, n, relpath, f1, f2,
8316 &line_cur1, &line_cur2,
8317 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8318 if (err)
8319 goto done;
8320 if (choice == GOT_PATCH_CHOICE_YES)
8321 have_content = 1;
8322 else
8323 have_rejected_content = 1;
8324 if (choice == GOT_PATCH_CHOICE_QUIT)
8325 break;
8327 if (have_content || have_rejected_content)
8328 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8329 outfile, rejectfile);
8330 done:
8331 free(label1);
8332 if (blob)
8333 got_object_blob_close(blob);
8334 if (staged_blob)
8335 got_object_blob_close(staged_blob);
8336 free_err = got_diffreg_result_free(diffreg_result);
8337 if (free_err && err == NULL)
8338 err = free_err;
8339 if (f1 && fclose(f1) == EOF && err == NULL)
8340 err = got_error_from_errno2("fclose", path1);
8341 if (f2 && fclose(f2) == EOF && err == NULL)
8342 err = got_error_from_errno2("fclose", path2);
8343 if (outfile && fclose(outfile) == EOF && err == NULL)
8344 err = got_error_from_errno2("fclose", *path_unstaged_content);
8345 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", *path_new_staged_content);
8347 if (path1 && unlink(path1) == -1 && err == NULL)
8348 err = got_error_from_errno2("unlink", path1);
8349 if (path2 && unlink(path2) == -1 && err == NULL)
8350 err = got_error_from_errno2("unlink", path2);
8351 if (err || !have_content) {
8352 if (*path_unstaged_content &&
8353 unlink(*path_unstaged_content) == -1 && err == NULL)
8354 err = got_error_from_errno2("unlink",
8355 *path_unstaged_content);
8356 free(*path_unstaged_content);
8357 *path_unstaged_content = NULL;
8359 if (err || !have_content || !have_rejected_content) {
8360 if (*path_new_staged_content &&
8361 unlink(*path_new_staged_content) == -1 && err == NULL)
8362 err = got_error_from_errno2("unlink",
8363 *path_new_staged_content);
8364 free(*path_new_staged_content);
8365 *path_new_staged_content = NULL;
8367 free(path1);
8368 free(path2);
8369 return err;
8372 static const struct got_error *
8373 unstage_hunks(struct got_object_id *staged_blob_id,
8374 struct got_blob_object *blob_base,
8375 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8376 const char *ondisk_path, const char *label_orig,
8377 struct got_worktree *worktree, struct got_repository *repo,
8378 got_worktree_patch_cb patch_cb, void *patch_arg,
8379 got_worktree_checkout_cb progress_cb, void *progress_arg)
8381 const struct got_error *err = NULL;
8382 char *path_unstaged_content = NULL;
8383 char *path_new_staged_content = NULL;
8384 char *parent = NULL, *base_path = NULL;
8385 char *blob_base_path = NULL;
8386 struct got_object_id *new_staged_blob_id = NULL;
8387 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8388 struct stat sb;
8390 err = create_unstaged_content(&path_unstaged_content,
8391 &path_new_staged_content, blob_id, staged_blob_id,
8392 ie->path, repo, patch_cb, patch_arg);
8393 if (err)
8394 return err;
8396 if (path_unstaged_content == NULL)
8397 return NULL;
8399 if (path_new_staged_content) {
8400 err = got_object_blob_create(&new_staged_blob_id,
8401 path_new_staged_content, repo);
8402 if (err)
8403 goto done;
8406 f = fopen(path_unstaged_content, "r");
8407 if (f == NULL) {
8408 err = got_error_from_errno2("fopen",
8409 path_unstaged_content);
8410 goto done;
8412 if (fstat(fileno(f), &sb) == -1) {
8413 err = got_error_from_errno2("fstat", path_unstaged_content);
8414 goto done;
8416 if (got_fileindex_entry_staged_filetype_get(ie) ==
8417 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8418 char link_target[PATH_MAX];
8419 size_t r;
8420 r = fread(link_target, 1, sizeof(link_target), f);
8421 if (r == 0 && ferror(f)) {
8422 err = got_error_from_errno("fread");
8423 goto done;
8425 if (r >= sizeof(link_target)) { /* should not happen */
8426 err = got_error(GOT_ERR_NO_SPACE);
8427 goto done;
8429 link_target[r] = '\0';
8430 err = merge_symlink(worktree, blob_base,
8431 ondisk_path, ie->path, label_orig, link_target,
8432 worktree->base_commit_id, repo, progress_cb,
8433 progress_arg);
8434 } else {
8435 int local_changes_subsumed;
8437 err = got_path_dirname(&parent, ondisk_path);
8438 if (err)
8439 return err;
8441 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8442 parent) == -1) {
8443 err = got_error_from_errno("asprintf");
8444 base_path = NULL;
8445 goto done;
8448 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8449 if (err)
8450 goto done;
8451 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8452 blob_base);
8453 if (err)
8454 goto done;
8457 * In order the run a 3-way merge with a symlink we copy the symlink's
8458 * target path into a temporary file and use that file with diff3.
8460 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8461 err = dump_symlink_target_path_to_file(&f_deriv2,
8462 ondisk_path);
8463 if (err)
8464 goto done;
8465 } else {
8466 int fd;
8467 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8468 if (fd == -1) {
8469 err = got_error_from_errno2("open", ondisk_path);
8470 goto done;
8472 f_deriv2 = fdopen(fd, "r");
8473 if (f_deriv2 == NULL) {
8474 err = got_error_from_errno2("fdopen", ondisk_path);
8475 close(fd);
8476 goto done;
8480 err = merge_file(&local_changes_subsumed, worktree,
8481 f_base, f, f_deriv2, ondisk_path, ie->path,
8482 got_fileindex_perms_to_st(ie),
8483 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8484 repo, progress_cb, progress_arg);
8486 if (err)
8487 goto done;
8489 if (new_staged_blob_id) {
8490 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8491 SHA1_DIGEST_LENGTH);
8492 } else {
8493 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8494 got_fileindex_entry_staged_filetype_set(ie, 0);
8496 done:
8497 free(new_staged_blob_id);
8498 if (path_unstaged_content &&
8499 unlink(path_unstaged_content) == -1 && err == NULL)
8500 err = got_error_from_errno2("unlink", path_unstaged_content);
8501 if (path_new_staged_content &&
8502 unlink(path_new_staged_content) == -1 && err == NULL)
8503 err = got_error_from_errno2("unlink", path_new_staged_content);
8504 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8505 err = got_error_from_errno2("unlink", blob_base_path);
8506 if (f_base && fclose(f_base) == EOF && err == NULL)
8507 err = got_error_from_errno2("fclose", path_unstaged_content);
8508 if (f && fclose(f) == EOF && err == NULL)
8509 err = got_error_from_errno2("fclose", path_unstaged_content);
8510 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8511 err = got_error_from_errno2("fclose", ondisk_path);
8512 free(path_unstaged_content);
8513 free(path_new_staged_content);
8514 free(blob_base_path);
8515 free(parent);
8516 free(base_path);
8517 return err;
8520 static const struct got_error *
8521 unstage_path(void *arg, unsigned char status,
8522 unsigned char staged_status, const char *relpath,
8523 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8524 struct got_object_id *commit_id, int dirfd, const char *de_name)
8526 const struct got_error *err = NULL;
8527 struct unstage_path_arg *a = arg;
8528 struct got_fileindex_entry *ie;
8529 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8530 char *ondisk_path = NULL;
8531 char *id_str = NULL, *label_orig = NULL;
8532 int local_changes_subsumed;
8533 struct stat sb;
8535 if (staged_status != GOT_STATUS_ADD &&
8536 staged_status != GOT_STATUS_MODIFY &&
8537 staged_status != GOT_STATUS_DELETE)
8538 return NULL;
8540 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8541 if (ie == NULL)
8542 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8544 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8545 == -1)
8546 return got_error_from_errno("asprintf");
8548 err = got_object_id_str(&id_str,
8549 commit_id ? commit_id : a->worktree->base_commit_id);
8550 if (err)
8551 goto done;
8552 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8553 id_str) == -1) {
8554 err = got_error_from_errno("asprintf");
8555 goto done;
8558 switch (staged_status) {
8559 case GOT_STATUS_MODIFY:
8560 err = got_object_open_as_blob(&blob_base, a->repo,
8561 blob_id, 8192);
8562 if (err)
8563 break;
8564 /* fall through */
8565 case GOT_STATUS_ADD:
8566 if (a->patch_cb) {
8567 if (staged_status == GOT_STATUS_ADD) {
8568 int choice = GOT_PATCH_CHOICE_NONE;
8569 err = (*a->patch_cb)(&choice, a->patch_arg,
8570 staged_status, ie->path, NULL, 1, 1);
8571 if (err)
8572 break;
8573 if (choice != GOT_PATCH_CHOICE_YES)
8574 break;
8575 } else {
8576 err = unstage_hunks(staged_blob_id,
8577 blob_base, blob_id, ie, ondisk_path,
8578 label_orig, a->worktree, a->repo,
8579 a->patch_cb, a->patch_arg,
8580 a->progress_cb, a->progress_arg);
8581 break; /* Done with this file. */
8584 err = got_object_open_as_blob(&blob_staged, a->repo,
8585 staged_blob_id, 8192);
8586 if (err)
8587 break;
8588 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8589 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8590 case GOT_FILEIDX_MODE_REGULAR_FILE:
8591 err = merge_blob(&local_changes_subsumed, a->worktree,
8592 blob_base, ondisk_path, relpath,
8593 got_fileindex_perms_to_st(ie), label_orig,
8594 blob_staged, commit_id ? commit_id :
8595 a->worktree->base_commit_id, a->repo,
8596 a->progress_cb, a->progress_arg);
8597 break;
8598 case GOT_FILEIDX_MODE_SYMLINK:
8599 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8600 char *staged_target;
8601 err = got_object_blob_read_to_str(
8602 &staged_target, blob_staged);
8603 if (err)
8604 goto done;
8605 err = merge_symlink(a->worktree, blob_base,
8606 ondisk_path, relpath, label_orig,
8607 staged_target, commit_id ? commit_id :
8608 a->worktree->base_commit_id,
8609 a->repo, a->progress_cb, a->progress_arg);
8610 free(staged_target);
8611 } else {
8612 err = merge_blob(&local_changes_subsumed,
8613 a->worktree, blob_base, ondisk_path,
8614 relpath, got_fileindex_perms_to_st(ie),
8615 label_orig, blob_staged,
8616 commit_id ? commit_id :
8617 a->worktree->base_commit_id, a->repo,
8618 a->progress_cb, a->progress_arg);
8620 break;
8621 default:
8622 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8623 break;
8625 if (err == NULL) {
8626 got_fileindex_entry_stage_set(ie,
8627 GOT_FILEIDX_STAGE_NONE);
8628 got_fileindex_entry_staged_filetype_set(ie, 0);
8630 break;
8631 case GOT_STATUS_DELETE:
8632 if (a->patch_cb) {
8633 int choice = GOT_PATCH_CHOICE_NONE;
8634 err = (*a->patch_cb)(&choice, a->patch_arg,
8635 staged_status, ie->path, NULL, 1, 1);
8636 if (err)
8637 break;
8638 if (choice == GOT_PATCH_CHOICE_NO)
8639 break;
8640 if (choice != GOT_PATCH_CHOICE_YES) {
8641 err = got_error(GOT_ERR_PATCH_CHOICE);
8642 break;
8645 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8646 got_fileindex_entry_staged_filetype_set(ie, 0);
8647 err = get_file_status(&status, &sb, ie, ondisk_path,
8648 dirfd, de_name, a->repo);
8649 if (err)
8650 break;
8651 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8652 break;
8654 done:
8655 free(ondisk_path);
8656 if (blob_base)
8657 got_object_blob_close(blob_base);
8658 if (blob_staged)
8659 got_object_blob_close(blob_staged);
8660 free(id_str);
8661 free(label_orig);
8662 return err;
8665 const struct got_error *
8666 got_worktree_unstage(struct got_worktree *worktree,
8667 struct got_pathlist_head *paths,
8668 got_worktree_checkout_cb progress_cb, void *progress_arg,
8669 got_worktree_patch_cb patch_cb, void *patch_arg,
8670 struct got_repository *repo)
8672 const struct got_error *err = NULL, *sync_err, *unlockerr;
8673 struct got_pathlist_entry *pe;
8674 struct got_fileindex *fileindex = NULL;
8675 char *fileindex_path = NULL;
8676 struct unstage_path_arg upa;
8678 err = lock_worktree(worktree, LOCK_EX);
8679 if (err)
8680 return err;
8682 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8683 if (err)
8684 goto done;
8686 upa.worktree = worktree;
8687 upa.fileindex = fileindex;
8688 upa.repo = repo;
8689 upa.progress_cb = progress_cb;
8690 upa.progress_arg = progress_arg;
8691 upa.patch_cb = patch_cb;
8692 upa.patch_arg = patch_arg;
8693 TAILQ_FOREACH(pe, paths, entry) {
8694 err = worktree_status(worktree, pe->path, fileindex, repo,
8695 unstage_path, &upa, NULL, NULL, 0, 0);
8696 if (err)
8697 goto done;
8700 sync_err = sync_fileindex(fileindex, fileindex_path);
8701 if (sync_err && err == NULL)
8702 err = sync_err;
8703 done:
8704 free(fileindex_path);
8705 if (fileindex)
8706 got_fileindex_free(fileindex);
8707 unlockerr = lock_worktree(worktree, LOCK_SH);
8708 if (unlockerr && err == NULL)
8709 err = unlockerr;
8710 return err;
8713 struct report_file_info_arg {
8714 struct got_worktree *worktree;
8715 got_worktree_path_info_cb info_cb;
8716 void *info_arg;
8717 struct got_pathlist_head *paths;
8718 got_cancel_cb cancel_cb;
8719 void *cancel_arg;
8722 static const struct got_error *
8723 report_file_info(void *arg, struct got_fileindex_entry *ie)
8725 struct report_file_info_arg *a = arg;
8726 struct got_pathlist_entry *pe;
8727 struct got_object_id blob_id, staged_blob_id, commit_id;
8728 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8729 struct got_object_id *commit_idp = NULL;
8730 int stage;
8732 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8733 return got_error(GOT_ERR_CANCELLED);
8735 TAILQ_FOREACH(pe, a->paths, entry) {
8736 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8737 got_path_is_child(ie->path, pe->path, pe->path_len))
8738 break;
8740 if (pe == NULL) /* not found */
8741 return NULL;
8743 if (got_fileindex_entry_has_blob(ie)) {
8744 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8745 blob_idp = &blob_id;
8747 stage = got_fileindex_entry_stage_get(ie);
8748 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8749 stage == GOT_FILEIDX_STAGE_ADD) {
8750 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8751 SHA1_DIGEST_LENGTH);
8752 staged_blob_idp = &staged_blob_id;
8755 if (got_fileindex_entry_has_commit(ie)) {
8756 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8757 commit_idp = &commit_id;
8760 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8761 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8764 const struct got_error *
8765 got_worktree_path_info(struct got_worktree *worktree,
8766 struct got_pathlist_head *paths,
8767 got_worktree_path_info_cb info_cb, void *info_arg,
8768 got_cancel_cb cancel_cb, void *cancel_arg)
8771 const struct got_error *err = NULL, *unlockerr;
8772 struct got_fileindex *fileindex = NULL;
8773 char *fileindex_path = NULL;
8774 struct report_file_info_arg arg;
8776 err = lock_worktree(worktree, LOCK_SH);
8777 if (err)
8778 return err;
8780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8781 if (err)
8782 goto done;
8784 arg.worktree = worktree;
8785 arg.info_cb = info_cb;
8786 arg.info_arg = info_arg;
8787 arg.paths = paths;
8788 arg.cancel_cb = cancel_cb;
8789 arg.cancel_arg = cancel_arg;
8790 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8791 &arg);
8792 done:
8793 free(fileindex_path);
8794 if (fileindex)
8795 got_fileindex_free(fileindex);
8796 unlockerr = lock_worktree(worktree, LOCK_UN);
8797 if (unlockerr && err == NULL)
8798 err = unlockerr;
8799 return err;