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 <sha1.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
67 {
68 const struct got_error *err = NULL;
69 char *path;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
75 free(path);
76 return err;
77 }
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
81 {
82 const struct got_error *err = NULL;
83 FILE *tmpfile = NULL;
84 char *tmppath = NULL;
85 char *path = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
89 path = NULL;
90 goto done;
91 }
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 if (err)
95 goto done;
97 if (content) {
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
101 goto done;
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
107 unlink(tmppath);
108 goto done;
111 done:
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
114 free(tmppath);
115 return err;
118 static const struct got_error *
119 read_meta_file(char **content, const char *path_got, const char *name)
121 const struct got_error *err = NULL;
122 char *path;
123 int fd = -1;
124 ssize_t n;
125 struct stat sb;
127 *content = NULL;
129 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
130 err = got_error_from_errno("asprintf");
131 path = NULL;
132 goto done;
135 fd = open(path, O_RDONLY | O_NOFOLLOW);
136 if (fd == -1) {
137 if (errno == ENOENT)
138 err = got_error_path(path, GOT_ERR_WORKTREE_META);
139 else
140 err = got_error_from_errno2("open", path);
141 goto done;
143 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
144 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 : got_error_from_errno2("flock", path));
146 goto done;
149 if (fstat(fd, &sb) != 0) {
150 err = got_error_from_errno2("fstat", path);
151 goto done;
153 *content = calloc(1, sb.st_size);
154 if (*content == NULL) {
155 err = got_error_from_errno("calloc");
156 goto done;
159 n = read(fd, *content, sb.st_size);
160 if (n != sb.st_size) {
161 err = (n == -1 ? got_error_from_errno2("read", path) :
162 got_error_path(path, GOT_ERR_WORKTREE_META));
163 goto done;
165 if ((*content)[sb.st_size - 1] != '\n') {
166 err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 goto done;
169 (*content)[sb.st_size - 1] = '\0';
171 done:
172 if (fd != -1 && close(fd) == -1 && err == NULL)
173 err = got_error_from_errno2("close", path_got);
174 free(path);
175 if (err) {
176 free(*content);
177 *content = NULL;
179 return err;
182 static const struct got_error *
183 write_head_ref(const char *path_got, struct got_reference *head_ref)
185 const struct got_error *err = NULL;
186 char *refstr = NULL;
188 if (got_ref_is_symbolic(head_ref)) {
189 refstr = got_ref_to_str(head_ref);
190 if (refstr == NULL)
191 return got_error_from_errno("got_ref_to_str");
192 } else {
193 refstr = strdup(got_ref_get_name(head_ref));
194 if (refstr == NULL)
195 return got_error_from_errno("strdup");
197 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 free(refstr);
199 return err;
202 const struct got_error *
203 got_worktree_init(const char *path, struct got_reference *head_ref,
204 const char *prefix, struct got_repository *repo)
206 const struct got_error *err = NULL;
207 struct got_object_id *commit_id = NULL;
208 uuid_t uuid;
209 uint32_t uuid_status;
210 int obj_type;
211 char *path_got = NULL;
212 char *formatstr = NULL;
213 char *absprefix = NULL;
214 char *basestr = NULL;
215 char *uuidstr = NULL;
217 if (strcmp(path, got_repo_get_path(repo)) == 0) {
218 err = got_error(GOT_ERR_WORKTREE_REPO);
219 goto done;
222 err = got_ref_resolve(&commit_id, repo, head_ref);
223 if (err)
224 return err;
225 err = got_object_get_type(&obj_type, repo, commit_id);
226 if (err)
227 return err;
228 if (obj_type != GOT_OBJ_TYPE_COMMIT)
229 return got_error(GOT_ERR_OBJ_TYPE);
231 if (!got_path_is_absolute(prefix)) {
232 if (asprintf(&absprefix, "/%s", prefix) == -1)
233 return got_error_from_errno("asprintf");
236 /* Create top-level directory (may already exist). */
237 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
238 err = got_error_from_errno2("mkdir", path);
239 goto done;
242 /* Create .got directory (may already exist). */
243 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
248 err = got_error_from_errno2("mkdir", path_got);
249 goto done;
252 /* Create an empty lock file. */
253 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 if (err)
255 goto done;
257 /* Create an empty file index. */
258 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 if (err)
260 goto done;
262 /* Write the HEAD reference. */
263 err = write_head_ref(path_got, head_ref);
264 if (err)
265 goto done;
267 /* Record our base commit. */
268 err = got_object_id_str(&basestr, commit_id);
269 if (err)
270 goto done;
271 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 if (err)
273 goto done;
275 /* Store path to repository. */
276 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 got_repo_get_path(repo));
278 if (err)
279 goto done;
281 /* Store in-repository path prefix. */
282 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 absprefix ? absprefix : prefix);
284 if (err)
285 goto done;
287 /* Generate UUID. */
288 uuid_create(&uuid, &uuid_status);
289 if (uuid_status != uuid_s_ok) {
290 err = got_error_uuid(uuid_status, "uuid_create");
291 goto done;
293 uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 if (uuid_status != uuid_s_ok) {
295 err = got_error_uuid(uuid_status, "uuid_to_string");
296 goto done;
298 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 if (err)
300 goto done;
302 /* Stamp work tree with format file. */
303 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 err = got_error_from_errno("asprintf");
305 goto done;
307 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 if (err)
309 goto done;
311 done:
312 free(commit_id);
313 free(path_got);
314 free(formatstr);
315 free(absprefix);
316 free(basestr);
317 free(uuidstr);
318 return err;
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
325 char *path_got;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 int version, fd = -1;
331 const char *errstr;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
335 *worktree = NULL;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno("asprintf");
339 path_got = NULL;
340 goto done;
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno("asprintf");
345 path_lock = NULL;
346 goto done;
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 if (fd == -1) {
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno2("open", path_lock));
353 goto done;
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 if (err)
358 goto done;
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 if (errstr) {
362 err = got_error_msg(GOT_ERR_WORKTREE_META,
363 "could not parse work tree format version number");
364 goto done;
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
368 goto done;
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno("calloc");
374 goto done;
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = realpath(path, NULL);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno2("realpath", path);
381 goto done;
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
385 if (err)
386 goto done;
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
390 if (err)
391 goto done;
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
395 if (err)
396 goto done;
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 if (err)
400 goto done;
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status, "uuid_from_string");
404 goto done;
407 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
408 if (err)
409 goto done;
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 base_commit_id_str);
413 if (err)
414 goto done;
416 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
417 GOT_WORKTREE_HEAD_REF);
418 if (err)
419 goto done;
421 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
422 (*worktree)->root_path,
423 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
424 err = got_error_from_errno("asprintf");
425 goto done;
428 err = got_gotconfig_read(&(*worktree)->gotconfig,
429 (*worktree)->gotconfig_path);
431 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
432 if ((*worktree)->root_fd == -1) {
433 err = got_error_from_errno2("open", (*worktree)->root_path);
434 goto done;
436 done:
437 if (repo) {
438 const struct got_error *close_err = got_repo_close(repo);
439 if (err == NULL)
440 err = close_err;
442 free(path_got);
443 free(path_lock);
444 free(base_commit_id_str);
445 free(uuidstr);
446 free(formatstr);
447 if (err) {
448 if (fd != -1)
449 close(fd);
450 if (*worktree != NULL)
451 got_worktree_close(*worktree);
452 *worktree = NULL;
453 } else
454 (*worktree)->lockfd = fd;
456 return err;
459 const struct got_error *
460 got_worktree_open(struct got_worktree **worktree, const char *path)
462 const struct got_error *err = NULL;
463 char *worktree_path;
465 worktree_path = strdup(path);
466 if (worktree_path == NULL)
467 return got_error_from_errno("strdup");
469 for (;;) {
470 char *parent_path;
472 err = open_worktree(worktree, worktree_path);
473 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 free(worktree_path);
475 return err;
477 if (*worktree) {
478 free(worktree_path);
479 return NULL;
481 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 break;
483 err = got_path_dirname(&parent_path, worktree_path);
484 if (err) {
485 if (err->code != GOT_ERR_BAD_PATH) {
486 free(worktree_path);
487 return err;
489 break;
491 free(worktree_path);
492 worktree_path = parent_path;
495 free(worktree_path);
496 return got_error(GOT_ERR_NOT_WORKTREE);
499 const struct got_error *
500 got_worktree_close(struct got_worktree *worktree)
502 const struct got_error *err = NULL;
504 if (worktree->lockfd != -1) {
505 if (close(worktree->lockfd) == -1)
506 err = got_error_from_errno2("close",
507 got_worktree_get_root_path(worktree));
509 if (close(worktree->root_fd) == -1 && err == NULL)
510 err = got_error_from_errno2("close",
511 got_worktree_get_root_path(worktree));
512 free(worktree->repo_path);
513 free(worktree->path_prefix);
514 free(worktree->base_commit_id);
515 free(worktree->head_ref_name);
516 free(worktree->root_path);
517 free(worktree->gotconfig_path);
518 got_gotconfig_free(worktree->gotconfig);
519 free(worktree);
520 return err;
523 const char *
524 got_worktree_get_root_path(struct got_worktree *worktree)
526 return worktree->root_path;
529 const char *
530 got_worktree_get_repo_path(struct got_worktree *worktree)
532 return worktree->repo_path;
534 const char *
535 got_worktree_get_path_prefix(struct got_worktree *worktree)
537 return worktree->path_prefix;
540 const struct got_error *
541 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
542 const char *path_prefix)
544 char *absprefix = NULL;
546 if (!got_path_is_absolute(path_prefix)) {
547 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
548 return got_error_from_errno("asprintf");
550 *match = (strcmp(absprefix ? absprefix : path_prefix,
551 worktree->path_prefix) == 0);
552 free(absprefix);
553 return NULL;
556 const char *
557 got_worktree_get_head_ref_name(struct got_worktree *worktree)
559 return worktree->head_ref_name;
562 const struct got_error *
563 got_worktree_set_head_ref(struct got_worktree *worktree,
564 struct got_reference *head_ref)
566 const struct got_error *err = NULL;
567 char *path_got = NULL, *head_ref_name = NULL;
569 if (asprintf(&path_got, "%s/%s", worktree->root_path,
570 GOT_WORKTREE_GOT_DIR) == -1) {
571 err = got_error_from_errno("asprintf");
572 path_got = NULL;
573 goto done;
576 head_ref_name = strdup(got_ref_get_name(head_ref));
577 if (head_ref_name == NULL) {
578 err = got_error_from_errno("strdup");
579 goto done;
582 err = write_head_ref(path_got, head_ref);
583 if (err)
584 goto done;
586 free(worktree->head_ref_name);
587 worktree->head_ref_name = head_ref_name;
588 done:
589 free(path_got);
590 if (err)
591 free(head_ref_name);
592 return err;
595 struct got_object_id *
596 got_worktree_get_base_commit_id(struct got_worktree *worktree)
598 return worktree->base_commit_id;
601 const struct got_error *
602 got_worktree_set_base_commit_id(struct got_worktree *worktree,
603 struct got_repository *repo, struct got_object_id *commit_id)
605 const struct got_error *err;
606 struct got_object *obj = NULL;
607 char *id_str = NULL;
608 char *path_got = NULL;
610 if (asprintf(&path_got, "%s/%s", worktree->root_path,
611 GOT_WORKTREE_GOT_DIR) == -1) {
612 err = got_error_from_errno("asprintf");
613 path_got = NULL;
614 goto done;
617 err = got_object_open(&obj, repo, commit_id);
618 if (err)
619 return err;
621 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
622 err = got_error(GOT_ERR_OBJ_TYPE);
623 goto done;
626 /* Record our base commit. */
627 err = got_object_id_str(&id_str, commit_id);
628 if (err)
629 goto done;
630 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
631 if (err)
632 goto done;
634 free(worktree->base_commit_id);
635 worktree->base_commit_id = got_object_id_dup(commit_id);
636 if (worktree->base_commit_id == NULL) {
637 err = got_error_from_errno("got_object_id_dup");
638 goto done;
640 done:
641 if (obj)
642 got_object_close(obj);
643 free(id_str);
644 free(path_got);
645 return err;
648 const struct got_gotconfig *
649 got_worktree_get_gotconfig(struct got_worktree *worktree)
651 return worktree->gotconfig;
654 static const struct got_error *
655 lock_worktree(struct got_worktree *worktree, int operation)
657 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
658 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
659 : got_error_from_errno2("flock",
660 got_worktree_get_root_path(worktree)));
661 return NULL;
664 static const struct got_error *
665 add_dir_on_disk(struct got_worktree *worktree, const char *path)
667 const struct got_error *err = NULL;
668 char *abspath;
670 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
671 return got_error_from_errno("asprintf");
673 err = got_path_mkdir(abspath);
674 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
675 struct stat sb;
676 err = NULL;
677 if (lstat(abspath, &sb) == -1) {
678 err = got_error_from_errno2("lstat", abspath);
679 } else if (!S_ISDIR(sb.st_mode)) {
680 /* TODO directory is obstructed; do something */
681 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 free(abspath);
685 return err;
688 static const struct got_error *
689 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
691 const struct got_error *err = NULL;
692 uint8_t fbuf1[8192];
693 uint8_t fbuf2[8192];
694 size_t flen1 = 0, flen2 = 0;
696 *same = 1;
698 for (;;) {
699 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
700 if (flen1 == 0 && ferror(f1)) {
701 err = got_error_from_errno("fread");
702 break;
704 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
705 if (flen2 == 0 && ferror(f2)) {
706 err = got_error_from_errno("fread");
707 break;
709 if (flen1 == 0) {
710 if (flen2 != 0)
711 *same = 0;
712 break;
713 } else if (flen2 == 0) {
714 if (flen1 != 0)
715 *same = 0;
716 break;
717 } else if (flen1 == flen2) {
718 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 *same = 0;
720 break;
722 } else {
723 *same = 0;
724 break;
728 return err;
731 static const struct got_error *
732 check_files_equal(int *same, FILE *f1, FILE *f2)
734 struct stat sb;
735 size_t size1, size2;
737 *same = 1;
739 if (fstat(fileno(f1), &sb) != 0)
740 return got_error_from_errno("fstat");
741 size1 = sb.st_size;
743 if (fstat(fileno(f2), &sb) != 0)
744 return got_error_from_errno("fstat");
745 size2 = sb.st_size;
747 if (size1 != size2) {
748 *same = 0;
749 return NULL;
752 if (fseek(f1, 0L, SEEK_SET) == -1)
753 return got_ferror(f1, GOT_ERR_IO);
754 if (fseek(f2, 0L, SEEK_SET) == -1)
755 return got_ferror(f2, GOT_ERR_IO);
757 return check_file_contents_equal(same, f1, f2);
760 /*
761 * Perform a 3-way merge where the file f_orig acts as the common
762 * ancestor, the file f_deriv acts as the first derived version,
763 * and the file f_deriv2 acts as the second derived version.
764 * The merge result will be written to a new file at ondisk_path; any
765 * existing file at this path will be replaced.
766 */
767 static const struct got_error *
768 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
769 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
770 const char *path, uint16_t st_mode,
771 const char *label_orig, const char *label_deriv, const char *label_deriv2,
772 enum got_diff_algorithm diff_algo, struct got_repository *repo,
773 got_worktree_checkout_cb progress_cb, void *progress_arg)
775 const struct got_error *err = NULL;
776 int merged_fd = -1;
777 FILE *f_merged = NULL;
778 char *merged_path = NULL, *base_path = NULL;
779 int overlapcnt = 0;
780 char *parent = NULL;
782 *local_changes_subsumed = 0;
784 err = got_path_dirname(&parent, ondisk_path);
785 if (err)
786 return err;
788 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
794 if (err)
795 goto done;
797 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
798 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
799 if (err)
800 goto done;
802 err = (*progress_cb)(progress_arg,
803 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
804 if (err)
805 goto done;
807 if (fsync(merged_fd) != 0) {
808 err = got_error_from_errno("fsync");
809 goto done;
812 f_merged = fdopen(merged_fd, "r");
813 if (f_merged == NULL) {
814 err = got_error_from_errno("fdopen");
815 goto done;
817 merged_fd = -1;
819 /* Check if a clean merge has subsumed all local changes. */
820 if (overlapcnt == 0) {
821 err = check_files_equal(local_changes_subsumed, f_deriv,
822 f_merged);
823 if (err)
824 goto done;
827 if (fchmod(fileno(f_merged), st_mode) != 0) {
828 err = got_error_from_errno2("fchmod", merged_path);
829 goto done;
832 if (rename(merged_path, ondisk_path) != 0) {
833 err = got_error_from_errno3("rename", merged_path,
834 ondisk_path);
835 goto done;
837 done:
838 if (err) {
839 if (merged_path)
840 unlink(merged_path);
842 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
843 err = got_error_from_errno("close");
844 if (f_merged && fclose(f_merged) == EOF && err == NULL)
845 err = got_error_from_errno("fclose");
846 free(merged_path);
847 free(base_path);
848 free(parent);
849 return err;
852 static const struct got_error *
853 update_symlink(const char *ondisk_path, const char *target_path,
854 size_t target_len)
856 /* This is not atomic but matches what 'ln -sf' does. */
857 if (unlink(ondisk_path) == -1)
858 return got_error_from_errno2("unlink", ondisk_path);
859 if (symlink(target_path, ondisk_path) == -1)
860 return got_error_from_errno3("symlink", target_path,
861 ondisk_path);
862 return NULL;
865 /*
866 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
867 * in the work tree with a file that contains conflict markers and the
868 * conflicting target paths of the original version, a "derived version"
869 * of a symlink from an incoming change, and a local version of the symlink.
871 * The original versions's target path can be NULL if it is not available,
872 * such as if both derived versions added a new symlink at the same path.
874 * The incoming derived symlink target is NULL in case the incoming change
875 * has deleted this symlink.
876 */
877 static const struct got_error *
878 install_symlink_conflict(const char *deriv_target,
879 struct got_object_id *deriv_base_commit_id, const char *orig_target,
880 const char *label_orig, const char *local_target, const char *ondisk_path)
882 const struct got_error *err;
883 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
884 FILE *f = NULL;
886 err = got_object_id_str(&id_str, deriv_base_commit_id);
887 if (err)
888 return got_error_from_errno("asprintf");
890 if (asprintf(&label_deriv, "%s: commit %s",
891 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
892 err = got_error_from_errno("asprintf");
893 goto done;
896 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
897 if (err)
898 goto done;
900 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("fchmod", path);
902 goto done;
905 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
906 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
907 deriv_target ? deriv_target : "(symlink was deleted)",
908 orig_target ? label_orig : "",
909 orig_target ? "\n" : "",
910 orig_target ? orig_target : "",
911 orig_target ? "\n" : "",
912 GOT_DIFF_CONFLICT_MARKER_SEP,
913 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
914 err = got_error_from_errno2("fprintf", path);
915 goto done;
918 if (unlink(ondisk_path) == -1) {
919 err = got_error_from_errno2("unlink", ondisk_path);
920 goto done;
922 if (rename(path, ondisk_path) == -1) {
923 err = got_error_from_errno3("rename", path, ondisk_path);
924 goto done;
926 done:
927 if (f != NULL && fclose(f) == EOF && err == NULL)
928 err = got_error_from_errno2("fclose", path);
929 free(path);
930 free(id_str);
931 free(label_deriv);
932 return err;
935 /* forward declaration */
936 static const struct got_error *
937 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
938 const char *, const char *, uint16_t, const char *,
939 struct got_blob_object *, struct got_object_id *,
940 struct got_repository *, got_worktree_checkout_cb, void *);
942 /*
943 * Merge a symlink into the work tree, where blob_orig acts as the common
944 * ancestor, deriv_target is the link target of the first derived version,
945 * and the symlink on disk acts as the second derived version.
946 * Assume that contents of both blobs represent symlinks.
947 */
948 static const struct got_error *
949 merge_symlink(struct got_worktree *worktree,
950 struct got_blob_object *blob_orig, const char *ondisk_path,
951 const char *path, const char *label_orig, const char *deriv_target,
952 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
953 got_worktree_checkout_cb progress_cb, void *progress_arg)
955 const struct got_error *err = NULL;
956 char *ancestor_target = NULL;
957 struct stat sb;
958 ssize_t ondisk_len, deriv_len;
959 char ondisk_target[PATH_MAX];
960 int have_local_change = 0;
961 int have_incoming_change = 0;
963 if (lstat(ondisk_path, &sb) == -1)
964 return got_error_from_errno2("lstat", ondisk_path);
966 ondisk_len = readlink(ondisk_path, ondisk_target,
967 sizeof(ondisk_target));
968 if (ondisk_len == -1) {
969 err = got_error_from_errno2("readlink",
970 ondisk_path);
971 goto done;
973 ondisk_target[ondisk_len] = '\0';
975 if (blob_orig) {
976 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
977 if (err)
978 goto done;
981 if (ancestor_target == NULL ||
982 (ondisk_len != strlen(ancestor_target) ||
983 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
984 have_local_change = 1;
986 deriv_len = strlen(deriv_target);
987 if (ancestor_target == NULL ||
988 (deriv_len != strlen(ancestor_target) ||
989 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
990 have_incoming_change = 1;
992 if (!have_local_change && !have_incoming_change) {
993 if (ancestor_target) {
994 /* Both sides made the same change. */
995 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
996 path);
997 } else if (deriv_len == ondisk_len &&
998 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
999 /* Both sides added the same symlink. */
1000 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1001 path);
1002 } else {
1003 /* Both sides added symlinks which don't match. */
1004 err = install_symlink_conflict(deriv_target,
1005 deriv_base_commit_id, ancestor_target,
1006 label_orig, ondisk_target, ondisk_path);
1007 if (err)
1008 goto done;
1009 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1010 path);
1012 } else if (!have_local_change && have_incoming_change) {
1013 /* Apply the incoming change. */
1014 err = update_symlink(ondisk_path, deriv_target,
1015 strlen(deriv_target));
1016 if (err)
1017 goto done;
1018 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1019 } else if (have_local_change && have_incoming_change) {
1020 if (deriv_len == ondisk_len &&
1021 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1022 /* Both sides made the same change. */
1023 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1024 path);
1025 } else {
1026 err = install_symlink_conflict(deriv_target,
1027 deriv_base_commit_id, ancestor_target, label_orig,
1028 ondisk_target, ondisk_path);
1029 if (err)
1030 goto done;
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1032 path);
1036 done:
1037 free(ancestor_target);
1038 return err;
1041 static const struct got_error *
1042 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1044 const struct got_error *err = NULL;
1045 char target_path[PATH_MAX];
1046 ssize_t target_len;
1047 size_t n;
1048 FILE *f;
1050 *outfile = NULL;
1052 f = got_opentemp();
1053 if (f == NULL)
1054 return got_error_from_errno("got_opentemp");
1055 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1056 if (target_len == -1) {
1057 err = got_error_from_errno2("readlink", ondisk_path);
1058 goto done;
1060 n = fwrite(target_path, 1, target_len, f);
1061 if (n != target_len) {
1062 err = got_ferror(f, GOT_ERR_IO);
1063 goto done;
1065 if (fflush(f) == EOF) {
1066 err = got_error_from_errno("fflush");
1067 goto done;
1069 if (fseek(f, 0L, SEEK_SET) == -1) {
1070 err = got_ferror(f, GOT_ERR_IO);
1071 goto done;
1073 done:
1074 if (err)
1075 fclose(f);
1076 else
1077 *outfile = f;
1078 return err;
1082 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1083 * blob_deriv acts as the first derived version, and the file on disk
1084 * acts as the second derived version.
1086 static const struct got_error *
1087 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1088 struct got_blob_object *blob_orig, const char *ondisk_path,
1089 const char *path, uint16_t st_mode, const char *label_orig,
1090 struct got_blob_object *blob_deriv,
1091 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1092 got_worktree_checkout_cb progress_cb, void *progress_arg)
1094 const struct got_error *err = NULL;
1095 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1096 char *blob_orig_path = NULL;
1097 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1098 char *label_deriv = NULL, *parent = NULL;
1100 *local_changes_subsumed = 0;
1102 err = got_path_dirname(&parent, ondisk_path);
1103 if (err)
1104 return err;
1106 if (blob_orig) {
1107 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1108 parent) == -1) {
1109 err = got_error_from_errno("asprintf");
1110 base_path = NULL;
1111 goto done;
1114 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1115 if (err)
1116 goto done;
1117 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1118 blob_orig);
1119 if (err)
1120 goto done;
1121 free(base_path);
1122 } else {
1124 * No common ancestor exists. This is an "add vs add" conflict
1125 * and we simply use an empty ancestor file to make both files
1126 * appear in the merged result in their entirety.
1128 f_orig = got_opentemp();
1129 if (f_orig == NULL) {
1130 err = got_error_from_errno("got_opentemp");
1131 goto done;
1135 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1136 err = got_error_from_errno("asprintf");
1137 base_path = NULL;
1138 goto done;
1141 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 if (err)
1143 goto done;
1144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1145 blob_deriv);
1146 if (err)
1147 goto done;
1149 err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 if (err)
1151 goto done;
1152 if (asprintf(&label_deriv, "%s: commit %s",
1153 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1154 err = got_error_from_errno("asprintf");
1155 goto done;
1159 * In order the run a 3-way merge with a symlink we copy the symlink's
1160 * target path into a temporary file and use that file with diff3.
1162 if (S_ISLNK(st_mode)) {
1163 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1164 if (err)
1165 goto done;
1166 } else {
1167 int fd;
1168 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1169 if (fd == -1) {
1170 err = got_error_from_errno2("open", ondisk_path);
1171 goto done;
1173 f_deriv2 = fdopen(fd, "r");
1174 if (f_deriv2 == NULL) {
1175 err = got_error_from_errno2("fdopen", ondisk_path);
1176 close(fd);
1177 goto done;
1181 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1182 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1183 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1184 done:
1185 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1186 err = got_error_from_errno("fclose");
1187 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 free(base_path);
1192 if (blob_orig_path) {
1193 unlink(blob_orig_path);
1194 free(blob_orig_path);
1196 if (blob_deriv_path) {
1197 unlink(blob_deriv_path);
1198 free(blob_deriv_path);
1200 free(id_str);
1201 free(label_deriv);
1202 free(parent);
1203 return err;
1206 static const struct got_error *
1207 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1208 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1209 int wt_fd, const char *path, struct got_object_id *blob_id)
1211 const struct got_error *err = NULL;
1212 struct got_fileindex_entry *new_ie;
1214 *new_iep = NULL;
1216 err = got_fileindex_entry_alloc(&new_ie, path);
1217 if (err)
1218 return err;
1220 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1221 blob_id->sha1, base_commit_id->sha1, 1);
1222 if (err)
1223 goto done;
1225 err = got_fileindex_entry_add(fileindex, new_ie);
1226 done:
1227 if (err)
1228 got_fileindex_entry_free(new_ie);
1229 else
1230 *new_iep = new_ie;
1231 return err;
1234 static mode_t
1235 get_ondisk_perms(int executable, mode_t st_mode)
1237 mode_t xbits = S_IXUSR;
1239 if (executable) {
1240 /* Map read bits to execute bits. */
1241 if (st_mode & S_IRGRP)
1242 xbits |= S_IXGRP;
1243 if (st_mode & S_IROTH)
1244 xbits |= S_IXOTH;
1245 return st_mode | xbits;
1248 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 /* forward declaration */
1252 static const struct got_error *
1253 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1254 const char *path, mode_t te_mode, mode_t st_mode,
1255 struct got_blob_object *blob, int restoring_missing_file,
1256 int reverting_versioned_file, int installing_bad_symlink,
1257 int path_is_unversioned, struct got_repository *repo,
1258 got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 * This function assumes that the provided symlink target points at a
1262 * safe location in the work tree!
1264 static const struct got_error *
1265 replace_existing_symlink(int *did_something, const char *ondisk_path,
1266 const char *target_path, size_t target_len)
1268 const struct got_error *err = NULL;
1269 ssize_t elen;
1270 char etarget[PATH_MAX];
1271 int fd;
1273 *did_something = 0;
1276 * "Bad" symlinks (those pointing outside the work tree or into the
1277 * .got directory) are installed in the work tree as a regular file
1278 * which contains the bad symlink target path.
1279 * The new symlink target has already been checked for safety by our
1280 * caller. If we can successfully open a regular file then we simply
1281 * replace this file with a symlink below.
1283 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1284 if (fd == -1) {
1285 if (errno != ELOOP)
1286 return got_error_from_errno2("open", ondisk_path);
1288 /* We are updating an existing on-disk symlink. */
1289 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1290 if (elen == -1)
1291 return got_error_from_errno2("readlink", ondisk_path);
1293 if (elen == target_len &&
1294 memcmp(etarget, target_path, target_len) == 0)
1295 return NULL; /* nothing to do */
1298 *did_something = 1;
1299 err = update_symlink(ondisk_path, target_path, target_len);
1300 if (fd != -1 && close(fd) == -1 && err == NULL)
1301 err = got_error_from_errno2("close", ondisk_path);
1302 return err;
1305 static const struct got_error *
1306 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1307 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1309 const struct got_error *err = NULL;
1310 char canonpath[PATH_MAX];
1311 char *path_got = NULL;
1313 *is_bad_symlink = 0;
1315 if (target_len >= sizeof(canonpath)) {
1316 *is_bad_symlink = 1;
1317 return NULL;
1321 * We do not use realpath(3) to resolve the symlink's target
1322 * path because we don't want to resolve symlinks recursively.
1323 * Instead we make the path absolute and then canonicalize it.
1324 * Relative symlink target lookup should begin at the directory
1325 * in which the blob object is being installed.
1327 if (!got_path_is_absolute(target_path)) {
1328 char *abspath, *parent;
1329 err = got_path_dirname(&parent, ondisk_path);
1330 if (err)
1331 return err;
1332 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1333 free(parent);
1334 return got_error_from_errno("asprintf");
1336 free(parent);
1337 if (strlen(abspath) >= sizeof(canonpath)) {
1338 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1339 free(abspath);
1340 return err;
1342 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1343 free(abspath);
1344 if (err)
1345 return err;
1346 } else {
1347 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1348 if (err)
1349 return err;
1352 /* Only allow symlinks pointing at paths within the work tree. */
1353 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1354 *is_bad_symlink = 1;
1355 return NULL;
1358 /* Do not allow symlinks pointing into the .got directory. */
1359 if (asprintf(&path_got, "%s/%s", wtroot_path,
1360 GOT_WORKTREE_GOT_DIR) == -1)
1361 return got_error_from_errno("asprintf");
1362 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1363 *is_bad_symlink = 1;
1365 free(path_got);
1366 return NULL;
1369 static const struct got_error *
1370 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1371 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1372 int restoring_missing_file, int reverting_versioned_file,
1373 int path_is_unversioned, struct got_repository *repo,
1374 got_worktree_checkout_cb progress_cb, void *progress_arg)
1376 const struct got_error *err = NULL;
1377 char target_path[PATH_MAX];
1378 size_t len, target_len = 0;
1379 char *path_got = NULL;
1380 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1381 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1383 *is_bad_symlink = 0;
1386 * Blob object content specifies the target path of the link.
1387 * If a symbolic link cannot be installed we instead create
1388 * a regular file which contains the link target path stored
1389 * in the blob object.
1391 do {
1392 err = got_object_blob_read_block(&len, blob);
1393 if (len + target_len >= sizeof(target_path)) {
1394 /* Path too long; install as a regular file. */
1395 *is_bad_symlink = 1;
1396 got_object_blob_rewind(blob);
1397 return install_blob(worktree, ondisk_path, path,
1398 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 restoring_missing_file, reverting_versioned_file,
1400 1, path_is_unversioned, repo, progress_cb,
1401 progress_arg);
1403 if (len > 0) {
1404 /* Skip blob object header first time around. */
1405 memcpy(target_path + target_len, buf + hdrlen,
1406 len - hdrlen);
1407 target_len += len - hdrlen;
1408 hdrlen = 0;
1410 } while (len != 0);
1411 target_path[target_len] = '\0';
1413 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1414 ondisk_path, worktree->root_path);
1415 if (err)
1416 return err;
1418 if (*is_bad_symlink) {
1419 /* install as a regular file */
1420 got_object_blob_rewind(blob);
1421 err = install_blob(worktree, ondisk_path, path,
1422 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1423 restoring_missing_file, reverting_versioned_file, 1,
1424 path_is_unversioned, repo, progress_cb, progress_arg);
1425 goto done;
1428 if (symlink(target_path, ondisk_path) == -1) {
1429 if (errno == EEXIST) {
1430 int symlink_replaced;
1431 if (path_is_unversioned) {
1432 err = (*progress_cb)(progress_arg,
1433 GOT_STATUS_UNVERSIONED, path);
1434 goto done;
1436 err = replace_existing_symlink(&symlink_replaced,
1437 ondisk_path, target_path, target_len);
1438 if (err)
1439 goto done;
1440 if (progress_cb) {
1441 if (symlink_replaced) {
1442 err = (*progress_cb)(progress_arg,
1443 reverting_versioned_file ?
1444 GOT_STATUS_REVERT :
1445 GOT_STATUS_UPDATE, path);
1446 } else {
1447 err = (*progress_cb)(progress_arg,
1448 GOT_STATUS_EXISTS, path);
1451 goto done; /* Nothing else to do. */
1454 if (errno == ENOENT) {
1455 char *parent;
1456 err = got_path_dirname(&parent, ondisk_path);
1457 if (err)
1458 goto done;
1459 err = add_dir_on_disk(worktree, parent);
1460 free(parent);
1461 if (err)
1462 goto done;
1464 * Retry, and fall through to error handling
1465 * below if this second attempt fails.
1467 if (symlink(target_path, ondisk_path) != -1) {
1468 err = NULL; /* success */
1469 goto done;
1473 /* Handle errors from first or second creation attempt. */
1474 if (errno == ENAMETOOLONG) {
1475 /* bad target path; install as a regular file */
1476 *is_bad_symlink = 1;
1477 got_object_blob_rewind(blob);
1478 err = install_blob(worktree, ondisk_path, path,
1479 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1480 restoring_missing_file, reverting_versioned_file, 1,
1481 path_is_unversioned, repo,
1482 progress_cb, progress_arg);
1483 } else if (errno == ENOTDIR) {
1484 err = got_error_path(ondisk_path,
1485 GOT_ERR_FILE_OBSTRUCTED);
1486 } else {
1487 err = got_error_from_errno3("symlink",
1488 target_path, ondisk_path);
1490 } else if (progress_cb)
1491 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1492 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1493 done:
1494 free(path_got);
1495 return err;
1498 static const struct got_error *
1499 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1500 const char *path, mode_t te_mode, mode_t st_mode,
1501 struct got_blob_object *blob, int restoring_missing_file,
1502 int reverting_versioned_file, int installing_bad_symlink,
1503 int path_is_unversioned, struct got_repository *repo,
1504 got_worktree_checkout_cb progress_cb, void *progress_arg)
1506 const struct got_error *err = NULL;
1507 int fd = -1;
1508 size_t len, hdrlen;
1509 int update = 0;
1510 char *tmppath = NULL;
1512 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1513 GOT_DEFAULT_FILE_MODE);
1514 if (fd == -1) {
1515 if (errno == ENOENT) {
1516 char *parent;
1517 err = got_path_dirname(&parent, path);
1518 if (err)
1519 return err;
1520 err = add_dir_on_disk(worktree, parent);
1521 free(parent);
1522 if (err)
1523 return err;
1524 fd = open(ondisk_path,
1525 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1526 GOT_DEFAULT_FILE_MODE);
1527 if (fd == -1)
1528 return got_error_from_errno2("open",
1529 ondisk_path);
1530 } else if (errno == EEXIST) {
1531 if (path_is_unversioned) {
1532 err = (*progress_cb)(progress_arg,
1533 GOT_STATUS_UNVERSIONED, path);
1534 goto done;
1536 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1537 !S_ISREG(st_mode) && !installing_bad_symlink) {
1538 /* TODO file is obstructed; do something */
1539 err = got_error_path(ondisk_path,
1540 GOT_ERR_FILE_OBSTRUCTED);
1541 goto done;
1542 } else {
1543 err = got_opentemp_named_fd(&tmppath, &fd,
1544 ondisk_path);
1545 if (err)
1546 goto done;
1547 update = 1;
1549 } else
1550 return got_error_from_errno2("open", ondisk_path);
1553 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1554 err = got_error_from_errno2("fchmod",
1555 update ? tmppath : ondisk_path);
1556 goto done;
1559 if (progress_cb) {
1560 if (restoring_missing_file)
1561 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1562 path);
1563 else if (reverting_versioned_file)
1564 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1565 path);
1566 else
1567 err = (*progress_cb)(progress_arg,
1568 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1569 if (err)
1570 goto done;
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1574 do {
1575 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1576 err = got_object_blob_read_block(&len, blob);
1577 if (err)
1578 break;
1579 if (len > 0) {
1580 /* Skip blob object header first time around. */
1581 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1582 if (outlen == -1) {
1583 err = got_error_from_errno("write");
1584 goto done;
1585 } else if (outlen != len - hdrlen) {
1586 err = got_error(GOT_ERR_IO);
1587 goto done;
1589 hdrlen = 0;
1591 } while (len != 0);
1593 if (fsync(fd) != 0) {
1594 err = got_error_from_errno("fsync");
1595 goto done;
1598 if (update) {
1599 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1600 err = got_error_from_errno2("unlink", ondisk_path);
1601 goto done;
1603 if (rename(tmppath, ondisk_path) != 0) {
1604 err = got_error_from_errno3("rename", tmppath,
1605 ondisk_path);
1606 goto done;
1608 free(tmppath);
1609 tmppath = NULL;
1612 done:
1613 if (fd != -1 && close(fd) == -1 && err == NULL)
1614 err = got_error_from_errno("close");
1615 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1616 err = got_error_from_errno2("unlink", tmppath);
1617 free(tmppath);
1618 return err;
1621 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1622 static const struct got_error *
1623 get_modified_file_content_status(unsigned char *status, FILE *f)
1625 const struct got_error *err = NULL;
1626 const char *markers[3] = {
1627 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1628 GOT_DIFF_CONFLICT_MARKER_SEP,
1629 GOT_DIFF_CONFLICT_MARKER_END
1631 int i = 0;
1632 char *line = NULL;
1633 size_t linesize = 0;
1634 ssize_t linelen;
1636 while (*status == GOT_STATUS_MODIFY) {
1637 linelen = getline(&line, &linesize, f);
1638 if (linelen == -1) {
1639 if (feof(f))
1640 break;
1641 err = got_ferror(f, GOT_ERR_IO);
1642 break;
1645 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1646 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1647 == 0)
1648 *status = GOT_STATUS_CONFLICT;
1649 else
1650 i++;
1653 free(line);
1655 return err;
1658 static int
1659 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1661 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1662 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1665 static int
1666 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1668 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1669 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1670 ie->mtime_sec == sb->st_mtim.tv_sec &&
1671 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1672 ie->size == (sb->st_size & 0xffffffff) &&
1673 !xbit_differs(ie, sb->st_mode));
1676 static unsigned char
1677 get_staged_status(struct got_fileindex_entry *ie)
1679 switch (got_fileindex_entry_stage_get(ie)) {
1680 case GOT_FILEIDX_STAGE_ADD:
1681 return GOT_STATUS_ADD;
1682 case GOT_FILEIDX_STAGE_DELETE:
1683 return GOT_STATUS_DELETE;
1684 case GOT_FILEIDX_STAGE_MODIFY:
1685 return GOT_STATUS_MODIFY;
1686 default:
1687 return GOT_STATUS_NO_CHANGE;
1691 static const struct got_error *
1692 get_symlink_modification_status(unsigned char *status,
1693 struct got_fileindex_entry *ie, const char *abspath,
1694 int dirfd, const char *de_name, struct got_blob_object *blob)
1696 const struct got_error *err = NULL;
1697 char target_path[PATH_MAX];
1698 char etarget[PATH_MAX];
1699 ssize_t elen;
1700 size_t len, target_len = 0;
1701 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1702 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1704 *status = GOT_STATUS_NO_CHANGE;
1706 /* Blob object content specifies the target path of the link. */
1707 do {
1708 err = got_object_blob_read_block(&len, blob);
1709 if (err)
1710 return err;
1711 if (len + target_len >= sizeof(target_path)) {
1713 * Should not happen. The blob contents were OK
1714 * when this symlink was installed.
1716 return got_error(GOT_ERR_NO_SPACE);
1718 if (len > 0) {
1719 /* Skip blob object header first time around. */
1720 memcpy(target_path + target_len, buf + hdrlen,
1721 len - hdrlen);
1722 target_len += len - hdrlen;
1723 hdrlen = 0;
1725 } while (len != 0);
1726 target_path[target_len] = '\0';
1728 if (dirfd != -1) {
1729 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1730 if (elen == -1)
1731 return got_error_from_errno2("readlinkat", abspath);
1732 } else {
1733 elen = readlink(abspath, etarget, sizeof(etarget));
1734 if (elen == -1)
1735 return got_error_from_errno2("readlink", abspath);
1738 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1739 *status = GOT_STATUS_MODIFY;
1741 return NULL;
1744 static const struct got_error *
1745 get_file_status(unsigned char *status, struct stat *sb,
1746 struct got_fileindex_entry *ie, const char *abspath,
1747 int dirfd, const char *de_name, struct got_repository *repo)
1749 const struct got_error *err = NULL;
1750 struct got_object_id id;
1751 size_t hdrlen;
1752 int fd = -1;
1753 FILE *f = NULL;
1754 uint8_t fbuf[8192];
1755 struct got_blob_object *blob = NULL;
1756 size_t flen, blen;
1757 unsigned char staged_status = get_staged_status(ie);
1759 *status = GOT_STATUS_NO_CHANGE;
1760 memset(sb, 0, sizeof(*sb));
1763 * Whenever the caller provides a directory descriptor and a
1764 * directory entry name for the file, use them! This prevents
1765 * race conditions if filesystem paths change beneath our feet.
1767 if (dirfd != -1) {
1768 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1772 else
1773 *status = GOT_STATUS_DELETE;
1774 goto done;
1776 err = got_error_from_errno2("fstatat", abspath);
1777 goto done;
1779 } else {
1780 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1781 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1782 return got_error_from_errno2("open", abspath);
1783 else if (fd == -1 && errno == ELOOP) {
1784 if (lstat(abspath, sb) == -1)
1785 return got_error_from_errno2("lstat", abspath);
1786 } else if (fd == -1 || fstat(fd, sb) == -1) {
1787 if (errno == ENOENT) {
1788 if (got_fileindex_entry_has_file_on_disk(ie))
1789 *status = GOT_STATUS_MISSING;
1790 else
1791 *status = GOT_STATUS_DELETE;
1792 goto done;
1794 err = got_error_from_errno2("fstat", abspath);
1795 goto done;
1799 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1800 *status = GOT_STATUS_OBSTRUCTED;
1801 goto done;
1804 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1805 *status = GOT_STATUS_DELETE;
1806 goto done;
1807 } else if (!got_fileindex_entry_has_blob(ie) &&
1808 staged_status != GOT_STATUS_ADD) {
1809 *status = GOT_STATUS_ADD;
1810 goto done;
1813 if (!stat_info_differs(ie, sb))
1814 goto done;
1816 if (S_ISLNK(sb->st_mode) &&
1817 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1818 *status = GOT_STATUS_MODIFY;
1819 goto done;
1822 if (staged_status == GOT_STATUS_MODIFY ||
1823 staged_status == GOT_STATUS_ADD)
1824 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1825 else
1826 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1828 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1829 if (err)
1830 goto done;
1832 if (S_ISLNK(sb->st_mode)) {
1833 err = get_symlink_modification_status(status, ie,
1834 abspath, dirfd, de_name, blob);
1835 goto done;
1838 if (dirfd != -1) {
1839 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1840 if (fd == -1) {
1841 err = got_error_from_errno2("openat", abspath);
1842 goto done;
1846 f = fdopen(fd, "r");
1847 if (f == NULL) {
1848 err = got_error_from_errno2("fdopen", abspath);
1849 goto done;
1851 fd = -1;
1852 hdrlen = got_object_blob_get_hdrlen(blob);
1853 for (;;) {
1854 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1855 err = got_object_blob_read_block(&blen, blob);
1856 if (err)
1857 goto done;
1858 /* Skip length of blob object header first time around. */
1859 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1860 if (flen == 0 && ferror(f)) {
1861 err = got_error_from_errno("fread");
1862 goto done;
1864 if (blen - hdrlen == 0) {
1865 if (flen != 0)
1866 *status = GOT_STATUS_MODIFY;
1867 break;
1868 } else if (flen == 0) {
1869 if (blen - hdrlen != 0)
1870 *status = GOT_STATUS_MODIFY;
1871 break;
1872 } else if (blen - hdrlen == flen) {
1873 /* Skip blob object header first time around. */
1874 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1875 *status = GOT_STATUS_MODIFY;
1876 break;
1878 } else {
1879 *status = GOT_STATUS_MODIFY;
1880 break;
1882 hdrlen = 0;
1885 if (*status == GOT_STATUS_MODIFY) {
1886 rewind(f);
1887 err = get_modified_file_content_status(status, f);
1888 } else if (xbit_differs(ie, sb->st_mode))
1889 *status = GOT_STATUS_MODE_CHANGE;
1890 done:
1891 if (blob)
1892 got_object_blob_close(blob);
1893 if (f != NULL && fclose(f) == EOF && err == NULL)
1894 err = got_error_from_errno2("fclose", abspath);
1895 if (fd != -1 && close(fd) == -1 && err == NULL)
1896 err = got_error_from_errno2("close", abspath);
1897 return err;
1901 * Update timestamps in the file index if a file is unmodified and
1902 * we had to run a full content comparison to find out.
1904 static const struct got_error *
1905 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1906 struct got_fileindex_entry *ie, struct stat *sb)
1908 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1909 return got_fileindex_entry_update(ie, wt_fd, path,
1910 ie->blob_sha1, ie->commit_sha1, 1);
1912 return NULL;
1915 static const struct got_error *
1916 update_blob(struct got_worktree *worktree,
1917 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1918 struct got_tree_entry *te, const char *path,
1919 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1920 void *progress_arg)
1922 const struct got_error *err = NULL;
1923 struct got_blob_object *blob = NULL;
1924 char *ondisk_path;
1925 unsigned char status = GOT_STATUS_NO_CHANGE;
1926 struct stat sb;
1928 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1929 return got_error_from_errno("asprintf");
1931 if (ie) {
1932 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1933 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1934 goto done;
1936 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1937 repo);
1938 if (err)
1939 goto done;
1940 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1941 sb.st_mode = got_fileindex_perms_to_st(ie);
1942 } else {
1943 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 status = GOT_STATUS_UNVERSIONED;
1947 if (status == GOT_STATUS_OBSTRUCTED) {
1948 if (ie)
1949 got_fileindex_entry_mark_skipped(ie);
1950 err = (*progress_cb)(progress_arg, status, path);
1951 goto done;
1953 if (status == GOT_STATUS_CONFLICT) {
1954 if (ie)
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 path);
1958 goto done;
1961 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 (S_ISLNK(te->mode) ||
1963 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1965 * This is a regular file or an installed bad symlink.
1966 * If the file index indicates that this file is already
1967 * up-to-date with respect to the repository we can skip
1968 * updating contents of this file.
1970 if (got_fileindex_entry_has_commit(ie) &&
1971 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Same commit. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1976 if (err)
1977 goto done;
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 path);
1980 goto done;
1982 if (got_fileindex_entry_has_blob(ie) &&
1983 memcmp(ie->blob_sha1, te->id.sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Different commit but the same blob. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1988 if (err)
1989 goto done;
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 path);
1992 goto done;
1996 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1997 if (err)
1998 goto done;
2000 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 int update_timestamps;
2002 struct got_blob_object *blob2 = NULL;
2003 char *label_orig = NULL;
2004 if (got_fileindex_entry_has_blob(ie)) {
2005 struct got_object_id id2;
2006 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2008 if (err)
2009 goto done;
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 sizeof(id_str)) == NULL) {
2015 err = got_error_path(id_str,
2016 GOT_ERR_BAD_OBJ_ID_STR);
2017 goto done;
2019 if (asprintf(&label_orig, "%s: commit %s",
2020 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 err = got_error_from_errno("asprintf");
2022 goto done;
2025 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 char *link_target;
2027 err = got_object_blob_read_to_str(&link_target, blob);
2028 if (err)
2029 goto done;
2030 err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 label_orig, link_target, worktree->base_commit_id,
2032 repo, progress_cb, progress_arg);
2033 free(link_target);
2034 } else {
2035 err = merge_blob(&update_timestamps, worktree, blob2,
2036 ondisk_path, path, sb.st_mode, label_orig, blob,
2037 worktree->base_commit_id, repo,
2038 progress_cb, progress_arg);
2040 free(label_orig);
2041 if (blob2)
2042 got_object_blob_close(blob2);
2043 if (err)
2044 goto done;
2046 * Do not update timestamps of files with local changes.
2047 * Otherwise, a future status walk would treat them as
2048 * unmodified files again.
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1,
2052 update_timestamps);
2053 } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 } else if (status == GOT_STATUS_DELETE) {
2057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 if (err)
2059 goto done;
2060 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 if (err)
2063 goto done;
2064 } else {
2065 int is_bad_symlink = 0;
2066 if (S_ISLNK(te->mode)) {
2067 err = install_symlink(&is_bad_symlink, worktree,
2068 ondisk_path, path, blob,
2069 status == GOT_STATUS_MISSING, 0,
2070 status == GOT_STATUS_UNVERSIONED, repo,
2071 progress_cb, progress_arg);
2072 } else {
2073 err = install_blob(worktree, ondisk_path, path,
2074 te->mode, sb.st_mode, blob,
2075 status == GOT_STATUS_MISSING, 0, 0,
2076 status == GOT_STATUS_UNVERSIONED, repo,
2077 progress_cb, progress_arg);
2079 if (err)
2080 goto done;
2082 if (ie) {
2083 err = got_fileindex_entry_update(ie,
2084 worktree->root_fd, path, blob->id.sha1,
2085 worktree->base_commit_id->sha1, 1);
2086 } else {
2087 err = create_fileindex_entry(&ie, fileindex,
2088 worktree->base_commit_id, worktree->root_fd, path,
2089 &blob->id);
2091 if (err)
2092 goto done;
2094 if (is_bad_symlink) {
2095 got_fileindex_entry_filetype_set(ie,
2096 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 got_object_blob_close(blob);
2100 done:
2101 free(ondisk_path);
2102 return err;
2105 static const struct got_error *
2106 remove_ondisk_file(const char *root_path, const char *path)
2108 const struct got_error *err = NULL;
2109 char *ondisk_path = NULL, *parent = NULL;
2111 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 return got_error_from_errno("asprintf");
2114 if (unlink(ondisk_path) == -1) {
2115 if (errno != ENOENT)
2116 err = got_error_from_errno2("unlink", ondisk_path);
2117 } else {
2118 size_t root_len = strlen(root_path);
2119 err = got_path_dirname(&parent, ondisk_path);
2120 if (err)
2121 goto done;
2122 while (got_path_cmp(parent, root_path,
2123 strlen(parent), root_len) != 0) {
2124 free(ondisk_path);
2125 ondisk_path = parent;
2126 parent = NULL;
2127 if (rmdir(ondisk_path) == -1) {
2128 if (errno != ENOTEMPTY)
2129 err = got_error_from_errno2("rmdir",
2130 ondisk_path);
2131 break;
2133 err = got_path_dirname(&parent, ondisk_path);
2134 if (err)
2135 break;
2138 done:
2139 free(ondisk_path);
2140 free(parent);
2141 return err;
2144 static const struct got_error *
2145 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2146 struct got_fileindex_entry *ie, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg)
2149 const struct got_error *err = NULL;
2150 unsigned char status;
2151 struct stat sb;
2152 char *ondisk_path;
2154 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2155 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2157 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2158 == -1)
2159 return got_error_from_errno("asprintf");
2161 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2162 if (err)
2163 goto done;
2165 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2166 char ondisk_target[PATH_MAX];
2167 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2168 sizeof(ondisk_target));
2169 if (ondisk_len == -1) {
2170 err = got_error_from_errno2("readlink", ondisk_path);
2171 goto done;
2173 ondisk_target[ondisk_len] = '\0';
2174 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2175 NULL, NULL, /* XXX pass common ancestor info? */
2176 ondisk_target, ondisk_path);
2177 if (err)
2178 goto done;
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2180 ie->path);
2181 goto done;
2184 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2185 status == GOT_STATUS_ADD) {
2186 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2187 if (err)
2188 goto done;
2190 * Preserve the working file and change the deleted blob's
2191 * entry into a schedule-add entry.
2193 err = got_fileindex_entry_update(ie, worktree->root_fd,
2194 ie->path, NULL, NULL, 0);
2195 } else {
2196 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2197 if (err)
2198 goto done;
2199 if (status == GOT_STATUS_NO_CHANGE) {
2200 err = remove_ondisk_file(worktree->root_path, ie->path);
2201 if (err)
2202 goto done;
2204 got_fileindex_entry_remove(fileindex, ie);
2206 done:
2207 free(ondisk_path);
2208 return err;
2211 struct diff_cb_arg {
2212 struct got_fileindex *fileindex;
2213 struct got_worktree *worktree;
2214 struct got_repository *repo;
2215 got_worktree_checkout_cb progress_cb;
2216 void *progress_arg;
2217 got_cancel_cb cancel_cb;
2218 void *cancel_arg;
2221 static const struct got_error *
2222 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2223 struct got_tree_entry *te, const char *parent_path)
2225 struct diff_cb_arg *a = arg;
2227 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2228 return got_error(GOT_ERR_CANCELLED);
2230 return update_blob(a->worktree, a->fileindex, ie, te,
2231 ie->path, a->repo, a->progress_cb, a->progress_arg);
2234 static const struct got_error *
2235 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 struct diff_cb_arg *a = arg;
2239 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 return got_error(GOT_ERR_CANCELLED);
2242 return delete_blob(a->worktree, a->fileindex, ie,
2243 a->repo, a->progress_cb, a->progress_arg);
2246 static const struct got_error *
2247 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2249 struct diff_cb_arg *a = arg;
2250 const struct got_error *err;
2251 char *path;
2253 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 return got_error(GOT_ERR_CANCELLED);
2256 if (got_object_tree_entry_is_submodule(te))
2257 return NULL;
2259 if (asprintf(&path, "%s%s%s", parent_path,
2260 parent_path[0] ? "/" : "", te->name)
2261 == -1)
2262 return got_error_from_errno("asprintf");
2264 if (S_ISDIR(te->mode))
2265 err = add_dir_on_disk(a->worktree, path);
2266 else
2267 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2268 a->repo, a->progress_cb, a->progress_arg);
2270 free(path);
2271 return err;
2274 const struct got_error *
2275 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2277 uint32_t uuid_status;
2279 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2280 if (uuid_status != uuid_s_ok) {
2281 *uuidstr = NULL;
2282 return got_error_uuid(uuid_status, "uuid_to_string");
2285 return NULL;
2288 static const struct got_error *
2289 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2291 const struct got_error *err = NULL;
2292 char *uuidstr = NULL;
2294 *refname = NULL;
2296 err = got_worktree_get_uuid(&uuidstr, worktree);
2297 if (err)
2298 return err;
2300 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2301 err = got_error_from_errno("asprintf");
2302 *refname = NULL;
2304 free(uuidstr);
2305 return err;
2308 const struct got_error *
2309 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2311 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2314 static const struct got_error *
2315 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2321 static const struct got_error *
2322 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2334 static const struct got_error *
2335 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree,
2338 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2341 static const struct got_error *
2342 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2348 static const struct got_error *
2349 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2362 static const struct got_error *
2363 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree,
2366 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2369 const struct got_error *
2370 got_worktree_get_histedit_script_path(char **path,
2371 struct got_worktree *worktree)
2373 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2374 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2375 *path = NULL;
2376 return got_error_from_errno("asprintf");
2378 return NULL;
2382 * Prevent Git's garbage collector from deleting our base commit by
2383 * setting a reference to our base commit's ID.
2385 static const struct got_error *
2386 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2388 const struct got_error *err = NULL;
2389 struct got_reference *ref = NULL;
2390 char *refname;
2392 err = got_worktree_get_base_ref_name(&refname, worktree);
2393 if (err)
2394 return err;
2396 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2397 if (err)
2398 goto done;
2400 err = got_ref_write(ref, repo);
2401 done:
2402 free(refname);
2403 if (ref)
2404 got_ref_close(ref);
2405 return err;
2408 static const struct got_error *
2409 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2411 const struct got_error *err = NULL;
2413 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2414 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2415 err = got_error_from_errno("asprintf");
2416 *fileindex_path = NULL;
2418 return err;
2422 static const struct got_error *
2423 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2424 struct got_worktree *worktree)
2426 const struct got_error *err = NULL;
2427 FILE *index = NULL;
2429 *fileindex_path = NULL;
2430 *fileindex = got_fileindex_alloc();
2431 if (*fileindex == NULL)
2432 return got_error_from_errno("got_fileindex_alloc");
2434 err = get_fileindex_path(fileindex_path, worktree);
2435 if (err)
2436 goto done;
2438 index = fopen(*fileindex_path, "rb");
2439 if (index == NULL) {
2440 if (errno != ENOENT)
2441 err = got_error_from_errno2("fopen", *fileindex_path);
2442 } else {
2443 err = got_fileindex_read(*fileindex, index);
2444 if (fclose(index) == EOF && err == NULL)
2445 err = got_error_from_errno("fclose");
2447 done:
2448 if (err) {
2449 free(*fileindex_path);
2450 *fileindex_path = NULL;
2451 got_fileindex_free(*fileindex);
2452 *fileindex = NULL;
2454 return err;
2457 struct bump_base_commit_id_arg {
2458 struct got_object_id *base_commit_id;
2459 const char *path;
2460 size_t path_len;
2461 const char *entry_name;
2462 got_worktree_checkout_cb progress_cb;
2463 void *progress_arg;
2466 /* Bump base commit ID of all files within an updated part of the work tree. */
2467 static const struct got_error *
2468 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2470 const struct got_error *err;
2471 struct bump_base_commit_id_arg *a = arg;
2473 if (a->entry_name) {
2474 if (strcmp(ie->path, a->path) != 0)
2475 return NULL;
2476 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2477 return NULL;
2479 if (got_fileindex_entry_was_skipped(ie))
2480 return NULL;
2482 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2483 SHA1_DIGEST_LENGTH) == 0)
2484 return NULL;
2486 if (a->progress_cb) {
2487 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2488 ie->path);
2489 if (err)
2490 return err;
2492 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2493 return NULL;
2496 static const struct got_error *
2497 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2498 struct got_fileindex *fileindex,
2499 got_worktree_checkout_cb progress_cb, void *progress_arg)
2501 struct bump_base_commit_id_arg bbc_arg;
2503 bbc_arg.base_commit_id = worktree->base_commit_id;
2504 bbc_arg.entry_name = NULL;
2505 bbc_arg.path = "";
2506 bbc_arg.path_len = 0;
2507 bbc_arg.progress_cb = progress_cb;
2508 bbc_arg.progress_arg = progress_arg;
2510 return got_fileindex_for_each_entry_safe(fileindex,
2511 bump_base_commit_id, &bbc_arg);
2514 static const struct got_error *
2515 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2517 const struct got_error *err = NULL;
2518 char *new_fileindex_path = NULL;
2519 FILE *new_index = NULL;
2520 struct timespec timeout;
2522 err = got_opentemp_named(&new_fileindex_path, &new_index,
2523 fileindex_path);
2524 if (err)
2525 goto done;
2527 err = got_fileindex_write(fileindex, new_index);
2528 if (err)
2529 goto done;
2531 if (rename(new_fileindex_path, fileindex_path) != 0) {
2532 err = got_error_from_errno3("rename", new_fileindex_path,
2533 fileindex_path);
2534 unlink(new_fileindex_path);
2538 * Sleep for a short amount of time to ensure that files modified after
2539 * this program exits have a different time stamp from the one which
2540 * was recorded in the file index.
2542 timeout.tv_sec = 0;
2543 timeout.tv_nsec = 1;
2544 nanosleep(&timeout, NULL);
2545 done:
2546 if (new_index)
2547 fclose(new_index);
2548 free(new_fileindex_path);
2549 return err;
2552 static const struct got_error *
2553 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2554 struct got_object_id **tree_id, const char *wt_relpath,
2555 struct got_worktree *worktree, struct got_repository *repo)
2557 const struct got_error *err = NULL;
2558 struct got_object_id *id = NULL;
2559 char *in_repo_path = NULL;
2560 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2562 *entry_type = GOT_OBJ_TYPE_ANY;
2563 *tree_relpath = NULL;
2564 *tree_id = NULL;
2566 if (wt_relpath[0] == '\0') {
2567 /* Check out all files within the work tree. */
2568 *entry_type = GOT_OBJ_TYPE_TREE;
2569 *tree_relpath = strdup("");
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2572 goto done;
2574 err = got_object_id_by_path(tree_id, repo,
2575 worktree->base_commit_id, worktree->path_prefix);
2576 if (err)
2577 goto done;
2578 return NULL;
2581 /* Check out a subset of files in the work tree. */
2583 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2584 is_root_wt ? "" : "/", wt_relpath) == -1) {
2585 err = got_error_from_errno("asprintf");
2586 goto done;
2589 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2590 in_repo_path);
2591 if (err)
2592 goto done;
2594 free(in_repo_path);
2595 in_repo_path = NULL;
2597 err = got_object_get_type(entry_type, repo, id);
2598 if (err)
2599 goto done;
2601 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2602 /* Check out a single file. */
2603 if (strchr(wt_relpath, '/') == NULL) {
2604 /* Check out a single file in work tree's root dir. */
2605 in_repo_path = strdup(worktree->path_prefix);
2606 if (in_repo_path == NULL) {
2607 err = got_error_from_errno("strdup");
2608 goto done;
2610 *tree_relpath = strdup("");
2611 if (*tree_relpath == NULL) {
2612 err = got_error_from_errno("strdup");
2613 goto done;
2615 } else {
2616 /* Check out a single file in a subdirectory. */
2617 err = got_path_dirname(tree_relpath, wt_relpath);
2618 if (err)
2619 return err;
2620 if (asprintf(&in_repo_path, "%s%s%s",
2621 worktree->path_prefix, is_root_wt ? "" : "/",
2622 *tree_relpath) == -1) {
2623 err = got_error_from_errno("asprintf");
2624 goto done;
2627 err = got_object_id_by_path(tree_id, repo,
2628 worktree->base_commit_id, in_repo_path);
2629 } else {
2630 /* Check out all files within a subdirectory. */
2631 *tree_id = got_object_id_dup(id);
2632 if (*tree_id == NULL) {
2633 err = got_error_from_errno("got_object_id_dup");
2634 goto done;
2636 *tree_relpath = strdup(wt_relpath);
2637 if (*tree_relpath == NULL) {
2638 err = got_error_from_errno("strdup");
2639 goto done;
2642 done:
2643 free(id);
2644 free(in_repo_path);
2645 if (err) {
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 free(*tree_relpath);
2648 *tree_relpath = NULL;
2649 free(*tree_id);
2650 *tree_id = NULL;
2652 return err;
2655 static const struct got_error *
2656 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2657 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2658 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2659 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2661 const struct got_error *err = NULL;
2662 struct got_commit_object *commit = NULL;
2663 struct got_tree_object *tree = NULL;
2664 struct got_fileindex_diff_tree_cb diff_cb;
2665 struct diff_cb_arg arg;
2667 err = ref_base_commit(worktree, repo);
2668 if (err) {
2669 if (!(err->code == GOT_ERR_ERRNO &&
2670 (errno == EACCES || errno == EROFS)))
2671 goto done;
2672 err = (*progress_cb)(progress_arg,
2673 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2674 if (err)
2675 return err;
2678 err = got_object_open_as_commit(&commit, repo,
2679 worktree->base_commit_id);
2680 if (err)
2681 goto done;
2683 err = got_object_open_as_tree(&tree, repo, tree_id);
2684 if (err)
2685 goto done;
2687 if (entry_name &&
2688 got_object_tree_find_entry(tree, entry_name) == NULL) {
2689 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2690 goto done;
2693 diff_cb.diff_old_new = diff_old_new;
2694 diff_cb.diff_old = diff_old;
2695 diff_cb.diff_new = diff_new;
2696 arg.fileindex = fileindex;
2697 arg.worktree = worktree;
2698 arg.repo = repo;
2699 arg.progress_cb = progress_cb;
2700 arg.progress_arg = progress_arg;
2701 arg.cancel_cb = cancel_cb;
2702 arg.cancel_arg = cancel_arg;
2703 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2704 entry_name, repo, &diff_cb, &arg);
2705 done:
2706 if (tree)
2707 got_object_tree_close(tree);
2708 if (commit)
2709 got_object_commit_close(commit);
2710 return err;
2713 const struct got_error *
2714 got_worktree_checkout_files(struct got_worktree *worktree,
2715 struct got_pathlist_head *paths, struct got_repository *repo,
2716 got_worktree_checkout_cb progress_cb, void *progress_arg,
2717 got_cancel_cb cancel_cb, void *cancel_arg)
2719 const struct got_error *err = NULL, *sync_err, *unlockerr;
2720 struct got_commit_object *commit = NULL;
2721 struct got_tree_object *tree = NULL;
2722 struct got_fileindex *fileindex = NULL;
2723 char *fileindex_path = NULL;
2724 struct got_pathlist_entry *pe;
2725 struct tree_path_data {
2726 STAILQ_ENTRY(tree_path_data) entry;
2727 struct got_object_id *tree_id;
2728 int entry_type;
2729 char *relpath;
2730 char *entry_name;
2731 } *tpd = NULL;
2732 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2734 STAILQ_INIT(&tree_paths);
2736 err = lock_worktree(worktree, LOCK_EX);
2737 if (err)
2738 return err;
2740 /* Map all specified paths to in-repository trees. */
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 tpd = malloc(sizeof(*tpd));
2743 if (tpd == NULL) {
2744 err = got_error_from_errno("malloc");
2745 goto done;
2748 err = find_tree_entry_for_checkout(&tpd->entry_type,
2749 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2750 if (err) {
2751 free(tpd);
2752 goto done;
2755 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2756 err = got_path_basename(&tpd->entry_name, pe->path);
2757 if (err) {
2758 free(tpd->relpath);
2759 free(tpd->tree_id);
2760 free(tpd);
2761 goto done;
2763 } else
2764 tpd->entry_name = NULL;
2766 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2770 * Read the file index.
2771 * Checking out files is supposed to be an idempotent operation.
2772 * If the on-disk file index is incomplete we will try to complete it.
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 if (err)
2776 goto done;
2778 tpd = STAILQ_FIRST(&tree_paths);
2779 TAILQ_FOREACH(pe, paths, entry) {
2780 struct bump_base_commit_id_arg bbc_arg;
2782 err = checkout_files(worktree, fileindex, tpd->relpath,
2783 tpd->tree_id, tpd->entry_name, repo,
2784 progress_cb, progress_arg, cancel_cb, cancel_arg);
2785 if (err)
2786 break;
2788 bbc_arg.base_commit_id = worktree->base_commit_id;
2789 bbc_arg.entry_name = tpd->entry_name;
2790 bbc_arg.path = pe->path;
2791 bbc_arg.path_len = pe->path_len;
2792 bbc_arg.progress_cb = progress_cb;
2793 bbc_arg.progress_arg = progress_arg;
2794 err = got_fileindex_for_each_entry_safe(fileindex,
2795 bump_base_commit_id, &bbc_arg);
2796 if (err)
2797 break;
2799 tpd = STAILQ_NEXT(tpd, entry);
2801 sync_err = sync_fileindex(fileindex, fileindex_path);
2802 if (sync_err && err == NULL)
2803 err = sync_err;
2804 done:
2805 free(fileindex_path);
2806 if (tree)
2807 got_object_tree_close(tree);
2808 if (commit)
2809 got_object_commit_close(commit);
2810 if (fileindex)
2811 got_fileindex_free(fileindex);
2812 while (!STAILQ_EMPTY(&tree_paths)) {
2813 tpd = STAILQ_FIRST(&tree_paths);
2814 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2815 free(tpd->relpath);
2816 free(tpd->tree_id);
2817 free(tpd);
2819 unlockerr = lock_worktree(worktree, LOCK_SH);
2820 if (unlockerr && err == NULL)
2821 err = unlockerr;
2822 return err;
2825 struct merge_file_cb_arg {
2826 struct got_worktree *worktree;
2827 struct got_fileindex *fileindex;
2828 got_worktree_checkout_cb progress_cb;
2829 void *progress_arg;
2830 got_cancel_cb cancel_cb;
2831 void *cancel_arg;
2832 const char *label_orig;
2833 struct got_object_id *commit_id2;
2836 static const struct got_error *
2837 merge_file_cb(void *arg, struct got_blob_object *blob1,
2838 struct got_blob_object *blob2, struct got_object_id *id1,
2839 struct got_object_id *id2, const char *path1, const char *path2,
2840 mode_t mode1, mode_t mode2, struct got_repository *repo)
2842 static const struct got_error *err = NULL;
2843 struct merge_file_cb_arg *a = arg;
2844 struct got_fileindex_entry *ie;
2845 char *ondisk_path = NULL;
2846 struct stat sb;
2847 unsigned char status;
2848 int local_changes_subsumed;
2849 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2850 char *id_str = NULL, *label_deriv2 = NULL;
2852 if (blob1 && blob2) {
2853 ie = got_fileindex_entry_get(a->fileindex, path2,
2854 strlen(path2));
2855 if (ie == NULL)
2856 return (*a->progress_cb)(a->progress_arg,
2857 GOT_STATUS_MISSING, path2);
2859 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2860 path2) == -1)
2861 return got_error_from_errno("asprintf");
2863 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2864 repo);
2865 if (err)
2866 goto done;
2868 if (status == GOT_STATUS_DELETE) {
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_MERGE, path2);
2871 goto done;
2873 if (status != GOT_STATUS_NO_CHANGE &&
2874 status != GOT_STATUS_MODIFY &&
2875 status != GOT_STATUS_CONFLICT &&
2876 status != GOT_STATUS_ADD) {
2877 err = (*a->progress_cb)(a->progress_arg, status, path2);
2878 goto done;
2881 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2882 char *link_target2;
2883 err = got_object_blob_read_to_str(&link_target2, blob2);
2884 if (err)
2885 goto done;
2886 err = merge_symlink(a->worktree, blob1, ondisk_path,
2887 path2, a->label_orig, link_target2, a->commit_id2,
2888 repo, a->progress_cb, a->progress_arg);
2889 free(link_target2);
2890 } else {
2891 int fd;
2893 f_orig = got_opentemp();
2894 if (f_orig == NULL) {
2895 err = got_error_from_errno("got_opentemp");
2896 goto done;
2898 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2899 f_orig, blob1);
2900 if (err)
2901 goto done;
2903 f_deriv2 = got_opentemp();
2904 if (f_deriv2 == NULL)
2905 goto done;
2906 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2907 f_deriv2, blob2);
2908 if (err)
2909 goto done;
2911 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2912 if (fd == -1) {
2913 err = got_error_from_errno2("open",
2914 ondisk_path);
2915 goto done;
2917 f_deriv = fdopen(fd, "r");
2918 if (f_deriv == NULL) {
2919 err = got_error_from_errno2("fdopen",
2920 ondisk_path);
2921 close(fd);
2922 goto done;
2924 err = got_object_id_str(&id_str, a->commit_id2);
2925 if (err)
2926 goto done;
2927 if (asprintf(&label_deriv2, "%s: commit %s",
2928 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2930 goto done;
2932 err = merge_file(&local_changes_subsumed, a->worktree,
2933 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2934 sb.st_mode, a->label_orig, NULL, label_deriv2,
2935 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2936 a->progress_cb, a->progress_arg);
2938 } else if (blob1) {
2939 ie = got_fileindex_entry_get(a->fileindex, path1,
2940 strlen(path1));
2941 if (ie == NULL)
2942 return (*a->progress_cb)(a->progress_arg,
2943 GOT_STATUS_MISSING, path1);
2945 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2946 path1) == -1)
2947 return got_error_from_errno("asprintf");
2949 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2950 repo);
2951 if (err)
2952 goto done;
2954 switch (status) {
2955 case GOT_STATUS_NO_CHANGE:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_DELETE, path1);
2958 if (err)
2959 goto done;
2960 err = remove_ondisk_file(a->worktree->root_path, path1);
2961 if (err)
2962 goto done;
2963 if (ie)
2964 got_fileindex_entry_mark_deleted_from_disk(ie);
2965 break;
2966 case GOT_STATUS_DELETE:
2967 case GOT_STATUS_MISSING:
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_DELETE, path1);
2970 if (err)
2971 goto done;
2972 if (ie)
2973 got_fileindex_entry_mark_deleted_from_disk(ie);
2974 break;
2975 case GOT_STATUS_ADD: {
2976 struct got_object_id *id;
2977 FILE *blob1_f;
2979 * Delete the added file only if its content already
2980 * exists in the repository.
2982 err = got_object_blob_file_create(&id, &blob1_f, path1);
2983 if (err)
2984 goto done;
2985 if (got_object_id_cmp(id, id1) == 0) {
2986 err = (*a->progress_cb)(a->progress_arg,
2987 GOT_STATUS_DELETE, path1);
2988 if (err)
2989 goto done;
2990 err = remove_ondisk_file(a->worktree->root_path,
2991 path1);
2992 if (err)
2993 goto done;
2994 if (ie)
2995 got_fileindex_entry_remove(a->fileindex,
2996 ie);
2997 } else {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_CANNOT_DELETE, path1);
3001 if (fclose(blob1_f) == EOF && err == NULL)
3002 err = got_error_from_errno("fclose");
3003 free(id);
3004 if (err)
3005 goto done;
3006 break;
3008 case GOT_STATUS_MODIFY:
3009 case GOT_STATUS_CONFLICT:
3010 err = (*a->progress_cb)(a->progress_arg,
3011 GOT_STATUS_CANNOT_DELETE, path1);
3012 if (err)
3013 goto done;
3014 break;
3015 case GOT_STATUS_OBSTRUCTED:
3016 err = (*a->progress_cb)(a->progress_arg, status, path1);
3017 if (err)
3018 goto done;
3019 break;
3020 default:
3021 break;
3023 } else if (blob2) {
3024 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3025 path2) == -1)
3026 return got_error_from_errno("asprintf");
3027 ie = got_fileindex_entry_get(a->fileindex, path2,
3028 strlen(path2));
3029 if (ie) {
3030 err = get_file_status(&status, &sb, ie, ondisk_path,
3031 -1, NULL, repo);
3032 if (err)
3033 goto done;
3034 if (status != GOT_STATUS_NO_CHANGE &&
3035 status != GOT_STATUS_MODIFY &&
3036 status != GOT_STATUS_CONFLICT &&
3037 status != GOT_STATUS_ADD) {
3038 err = (*a->progress_cb)(a->progress_arg,
3039 status, path2);
3040 goto done;
3042 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3043 char *link_target2;
3044 err = got_object_blob_read_to_str(&link_target2,
3045 blob2);
3046 if (err)
3047 goto done;
3048 err = merge_symlink(a->worktree, NULL,
3049 ondisk_path, path2, a->label_orig,
3050 link_target2, a->commit_id2, repo,
3051 a->progress_cb, a->progress_arg);
3052 free(link_target2);
3053 } else if (S_ISREG(sb.st_mode)) {
3054 err = merge_blob(&local_changes_subsumed,
3055 a->worktree, NULL, ondisk_path, path2,
3056 sb.st_mode, a->label_orig, blob2,
3057 a->commit_id2, repo, a->progress_cb,
3058 a->progress_arg);
3059 } else {
3060 err = got_error_path(ondisk_path,
3061 GOT_ERR_FILE_OBSTRUCTED);
3063 if (err)
3064 goto done;
3065 if (status == GOT_STATUS_DELETE) {
3066 err = got_fileindex_entry_update(ie,
3067 a->worktree->root_fd, path2, blob2->id.sha1,
3068 a->worktree->base_commit_id->sha1, 0);
3069 if (err)
3070 goto done;
3072 } else {
3073 int is_bad_symlink = 0;
3074 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3075 if (S_ISLNK(mode2)) {
3076 err = install_symlink(&is_bad_symlink,
3077 a->worktree, ondisk_path, path2, blob2, 0,
3078 0, 1, repo, a->progress_cb, a->progress_arg);
3079 } else {
3080 err = install_blob(a->worktree, ondisk_path, path2,
3081 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3082 a->progress_cb, a->progress_arg);
3084 if (err)
3085 goto done;
3086 err = got_fileindex_entry_alloc(&ie, path2);
3087 if (err)
3088 goto done;
3089 err = got_fileindex_entry_update(ie,
3090 a->worktree->root_fd, path2, NULL, NULL, 1);
3091 if (err) {
3092 got_fileindex_entry_free(ie);
3093 goto done;
3095 err = got_fileindex_entry_add(a->fileindex, ie);
3096 if (err) {
3097 got_fileindex_entry_free(ie);
3098 goto done;
3100 if (is_bad_symlink) {
3101 got_fileindex_entry_filetype_set(ie,
3102 GOT_FILEIDX_MODE_BAD_SYMLINK);
3106 done:
3107 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3108 err = got_error_from_errno("fclose");
3109 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3110 err = got_error_from_errno("fclose");
3111 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3112 err = got_error_from_errno("fclose");
3113 free(id_str);
3114 free(label_deriv2);
3115 free(ondisk_path);
3116 return err;
3119 static const struct got_error *
3120 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3122 struct got_worktree *worktree = arg;
3124 /* Reject merges into a work tree with mixed base commits. */
3125 if (got_fileindex_entry_has_commit(ie) &&
3126 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3127 SHA1_DIGEST_LENGTH) != 0)
3128 return got_error(GOT_ERR_MIXED_COMMITS);
3130 return NULL;
3133 struct check_merge_conflicts_arg {
3134 struct got_worktree *worktree;
3135 struct got_fileindex *fileindex;
3136 struct got_repository *repo;
3139 static const struct got_error *
3140 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3141 struct got_blob_object *blob2, struct got_object_id *id1,
3142 struct got_object_id *id2, const char *path1, const char *path2,
3143 mode_t mode1, mode_t mode2, struct got_repository *repo)
3145 const struct got_error *err = NULL;
3146 struct check_merge_conflicts_arg *a = arg;
3147 unsigned char status;
3148 struct stat sb;
3149 struct got_fileindex_entry *ie;
3150 const char *path = path2 ? path2 : path1;
3151 struct got_object_id *id = id2 ? id2 : id1;
3152 char *ondisk_path;
3154 if (id == NULL)
3155 return NULL;
3157 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3158 if (ie == NULL)
3159 return NULL;
3161 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3162 == -1)
3163 return got_error_from_errno("asprintf");
3165 /* Reject merges into a work tree with conflicted files. */
3166 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3167 free(ondisk_path);
3168 if (err)
3169 return err;
3170 if (status == GOT_STATUS_CONFLICT)
3171 return got_error(GOT_ERR_CONFLICTS);
3173 return NULL;
3176 static const struct got_error *
3177 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3178 const char *fileindex_path, struct got_object_id *commit_id1,
3179 struct got_object_id *commit_id2, struct got_repository *repo,
3180 got_worktree_checkout_cb progress_cb, void *progress_arg,
3181 got_cancel_cb cancel_cb, void *cancel_arg)
3183 const struct got_error *err = NULL, *sync_err;
3184 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 struct check_merge_conflicts_arg cmc_arg;
3187 struct merge_file_cb_arg arg;
3188 char *label_orig = NULL;
3190 if (commit_id1) {
3191 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3192 worktree->path_prefix);
3193 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3194 goto done;
3196 if (tree_id1) {
3197 char *id_str;
3199 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3200 if (err)
3201 goto done;
3203 err = got_object_id_str(&id_str, commit_id1);
3204 if (err)
3205 goto done;
3207 if (asprintf(&label_orig, "%s: commit %s",
3208 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3209 err = got_error_from_errno("asprintf");
3210 free(id_str);
3211 goto done;
3213 free(id_str);
3216 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3217 worktree->path_prefix);
3218 if (err)
3219 goto done;
3221 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3222 if (err)
3223 goto done;
3225 cmc_arg.worktree = worktree;
3226 cmc_arg.fileindex = fileindex;
3227 cmc_arg.repo = repo;
3228 err = got_diff_tree(tree1, tree2, "", "", repo,
3229 check_merge_conflicts, &cmc_arg, 0);
3230 if (err)
3231 goto done;
3233 arg.worktree = worktree;
3234 arg.fileindex = fileindex;
3235 arg.progress_cb = progress_cb;
3236 arg.progress_arg = progress_arg;
3237 arg.cancel_cb = cancel_cb;
3238 arg.cancel_arg = cancel_arg;
3239 arg.label_orig = label_orig;
3240 arg.commit_id2 = commit_id2;
3241 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3242 sync_err = sync_fileindex(fileindex, fileindex_path);
3243 if (sync_err && err == NULL)
3244 err = sync_err;
3245 done:
3246 if (tree1)
3247 got_object_tree_close(tree1);
3248 if (tree2)
3249 got_object_tree_close(tree2);
3250 free(label_orig);
3251 return err;
3254 const struct got_error *
3255 got_worktree_merge_files(struct got_worktree *worktree,
3256 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3257 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3258 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3260 const struct got_error *err, *unlockerr;
3261 char *fileindex_path = NULL;
3262 struct got_fileindex *fileindex = NULL;
3264 err = lock_worktree(worktree, LOCK_EX);
3265 if (err)
3266 return err;
3268 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3269 if (err)
3270 goto done;
3272 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3273 worktree);
3274 if (err)
3275 goto done;
3277 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3278 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3279 done:
3280 if (fileindex)
3281 got_fileindex_free(fileindex);
3282 free(fileindex_path);
3283 unlockerr = lock_worktree(worktree, LOCK_SH);
3284 if (unlockerr && err == NULL)
3285 err = unlockerr;
3286 return err;
3289 struct diff_dir_cb_arg {
3290 struct got_fileindex *fileindex;
3291 struct got_worktree *worktree;
3292 const char *status_path;
3293 size_t status_path_len;
3294 struct got_repository *repo;
3295 got_worktree_status_cb status_cb;
3296 void *status_arg;
3297 got_cancel_cb cancel_cb;
3298 void *cancel_arg;
3299 /* A pathlist containing per-directory pathlists of ignore patterns. */
3300 struct got_pathlist_head *ignores;
3301 int report_unchanged;
3302 int no_ignores;
3305 static const struct got_error *
3306 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3307 int dirfd, const char *de_name,
3308 got_worktree_status_cb status_cb, void *status_arg,
3309 struct got_repository *repo, int report_unchanged)
3311 const struct got_error *err = NULL;
3312 unsigned char status = GOT_STATUS_NO_CHANGE;
3313 unsigned char staged_status = get_staged_status(ie);
3314 struct stat sb;
3315 struct got_object_id blob_id, commit_id, staged_blob_id;
3316 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3317 struct got_object_id *staged_blob_idp = NULL;
3319 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3320 if (err)
3321 return err;
3323 if (status == GOT_STATUS_NO_CHANGE &&
3324 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3325 return NULL;
3327 if (got_fileindex_entry_has_blob(ie)) {
3328 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3329 blob_idp = &blob_id;
3331 if (got_fileindex_entry_has_commit(ie)) {
3332 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3333 commit_idp = &commit_id;
3335 if (staged_status == GOT_STATUS_ADD ||
3336 staged_status == GOT_STATUS_MODIFY) {
3337 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3338 SHA1_DIGEST_LENGTH);
3339 staged_blob_idp = &staged_blob_id;
3342 return (*status_cb)(status_arg, status, staged_status,
3343 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3346 static const struct got_error *
3347 status_old_new(void *arg, struct got_fileindex_entry *ie,
3348 struct dirent *de, const char *parent_path, int dirfd)
3350 const struct got_error *err = NULL;
3351 struct diff_dir_cb_arg *a = arg;
3352 char *abspath;
3354 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3355 return got_error(GOT_ERR_CANCELLED);
3357 if (got_path_cmp(parent_path, a->status_path,
3358 strlen(parent_path), a->status_path_len) != 0 &&
3359 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3360 return NULL;
3362 if (parent_path[0]) {
3363 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3364 parent_path, de->d_name) == -1)
3365 return got_error_from_errno("asprintf");
3366 } else {
3367 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3368 de->d_name) == -1)
3369 return got_error_from_errno("asprintf");
3372 err = report_file_status(ie, abspath, dirfd, de->d_name,
3373 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3374 free(abspath);
3375 return err;
3378 static const struct got_error *
3379 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3381 struct diff_dir_cb_arg *a = arg;
3382 struct got_object_id blob_id, commit_id;
3383 unsigned char status;
3385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3386 return got_error(GOT_ERR_CANCELLED);
3388 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3389 return NULL;
3391 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3392 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3393 if (got_fileindex_entry_has_file_on_disk(ie))
3394 status = GOT_STATUS_MISSING;
3395 else
3396 status = GOT_STATUS_DELETE;
3397 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3398 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3401 void
3402 free_ignorelist(struct got_pathlist_head *ignorelist)
3404 struct got_pathlist_entry *pe;
3406 TAILQ_FOREACH(pe, ignorelist, entry)
3407 free((char *)pe->path);
3408 got_pathlist_free(ignorelist);
3411 void
3412 free_ignores(struct got_pathlist_head *ignores)
3414 struct got_pathlist_entry *pe;
3416 TAILQ_FOREACH(pe, ignores, entry) {
3417 struct got_pathlist_head *ignorelist = pe->data;
3418 free_ignorelist(ignorelist);
3419 free((char *)pe->path);
3421 got_pathlist_free(ignores);
3424 static const struct got_error *
3425 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3427 const struct got_error *err = NULL;
3428 struct got_pathlist_entry *pe = NULL;
3429 struct got_pathlist_head *ignorelist;
3430 char *line = NULL, *pattern, *dirpath = NULL;
3431 size_t linesize = 0;
3432 ssize_t linelen;
3434 ignorelist = calloc(1, sizeof(*ignorelist));
3435 if (ignorelist == NULL)
3436 return got_error_from_errno("calloc");
3437 TAILQ_INIT(ignorelist);
3439 while ((linelen = getline(&line, &linesize, f)) != -1) {
3440 if (linelen > 0 && line[linelen - 1] == '\n')
3441 line[linelen - 1] = '\0';
3443 /* Git's ignores may contain comments. */
3444 if (line[0] == '#')
3445 continue;
3447 /* Git's negated patterns are not (yet?) supported. */
3448 if (line[0] == '!')
3449 continue;
3451 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3452 line) == -1) {
3453 err = got_error_from_errno("asprintf");
3454 goto done;
3456 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3457 if (err)
3458 goto done;
3460 if (ferror(f)) {
3461 err = got_error_from_errno("getline");
3462 goto done;
3465 dirpath = strdup(path);
3466 if (dirpath == NULL) {
3467 err = got_error_from_errno("strdup");
3468 goto done;
3470 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3471 done:
3472 free(line);
3473 if (err || pe == NULL) {
3474 free(dirpath);
3475 free_ignorelist(ignorelist);
3477 return err;
3480 int
3481 match_ignores(struct got_pathlist_head *ignores, const char *path)
3483 struct got_pathlist_entry *pe;
3485 /* Handle patterns which match in all directories. */
3486 TAILQ_FOREACH(pe, ignores, entry) {
3487 struct got_pathlist_head *ignorelist = pe->data;
3488 struct got_pathlist_entry *pi;
3490 TAILQ_FOREACH(pi, ignorelist, entry) {
3491 const char *p, *pattern = pi->path;
3493 if (strncmp(pattern, "**/", 3) != 0)
3494 continue;
3495 pattern += 3;
3496 p = path;
3497 while (*p) {
3498 if (fnmatch(pattern, p,
3499 FNM_PATHNAME | FNM_LEADING_DIR)) {
3500 /* Retry in next directory. */
3501 while (*p && *p != '/')
3502 p++;
3503 while (*p == '/')
3504 p++;
3505 continue;
3507 return 1;
3513 * The ignores pathlist contains ignore lists from children before
3514 * parents, so we can find the most specific ignorelist by walking
3515 * ignores backwards.
3517 pe = TAILQ_LAST(ignores, got_pathlist_head);
3518 while (pe) {
3519 if (got_path_is_child(path, pe->path, pe->path_len)) {
3520 struct got_pathlist_head *ignorelist = pe->data;
3521 struct got_pathlist_entry *pi;
3522 TAILQ_FOREACH(pi, ignorelist, entry) {
3523 const char *pattern = pi->path;
3524 int flags = FNM_LEADING_DIR;
3525 if (strstr(pattern, "/**/") == NULL)
3526 flags |= FNM_PATHNAME;
3527 if (fnmatch(pattern, path, flags))
3528 continue;
3529 return 1;
3532 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3535 return 0;
3538 static const struct got_error *
3539 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3540 const char *path, int dirfd, const char *ignores_filename)
3542 const struct got_error *err = NULL;
3543 char *ignorespath;
3544 int fd = -1;
3545 FILE *ignoresfile = NULL;
3547 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3548 path[0] ? "/" : "", ignores_filename) == -1)
3549 return got_error_from_errno("asprintf");
3551 if (dirfd != -1) {
3552 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3553 if (fd == -1) {
3554 if (errno != ENOENT && errno != EACCES)
3555 err = got_error_from_errno2("openat",
3556 ignorespath);
3557 } else {
3558 ignoresfile = fdopen(fd, "r");
3559 if (ignoresfile == NULL)
3560 err = got_error_from_errno2("fdopen",
3561 ignorespath);
3562 else {
3563 fd = -1;
3564 err = read_ignores(ignores, path, ignoresfile);
3567 } else {
3568 ignoresfile = fopen(ignorespath, "r");
3569 if (ignoresfile == NULL) {
3570 if (errno != ENOENT && errno != EACCES)
3571 err = got_error_from_errno2("fopen",
3572 ignorespath);
3573 } else
3574 err = read_ignores(ignores, path, ignoresfile);
3577 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3578 err = got_error_from_errno2("fclose", path);
3579 if (fd != -1 && close(fd) == -1 && err == NULL)
3580 err = got_error_from_errno2("close", path);
3581 free(ignorespath);
3582 return err;
3585 static const struct got_error *
3586 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3588 const struct got_error *err = NULL;
3589 struct diff_dir_cb_arg *a = arg;
3590 char *path = NULL;
3592 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 return got_error(GOT_ERR_CANCELLED);
3595 if (parent_path[0]) {
3596 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 return got_error_from_errno("asprintf");
3598 } else {
3599 path = de->d_name;
3602 if (de->d_type != DT_DIR &&
3603 got_path_is_child(path, a->status_path, a->status_path_len)
3604 && !match_ignores(a->ignores, path))
3605 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3606 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3607 if (parent_path[0])
3608 free(path);
3609 return err;
3612 static const struct got_error *
3613 status_traverse(void *arg, const char *path, int dirfd)
3615 const struct got_error *err = NULL;
3616 struct diff_dir_cb_arg *a = arg;
3618 if (a->no_ignores)
3619 return NULL;
3621 err = add_ignores(a->ignores, a->worktree->root_path,
3622 path, dirfd, ".cvsignore");
3623 if (err)
3624 return err;
3626 err = add_ignores(a->ignores, a->worktree->root_path, path,
3627 dirfd, ".gitignore");
3629 return err;
3632 static const struct got_error *
3633 report_single_file_status(const char *path, const char *ondisk_path,
3634 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3635 void *status_arg, struct got_repository *repo, int report_unchanged,
3636 struct got_pathlist_head *ignores, int no_ignores)
3638 struct got_fileindex_entry *ie;
3639 struct stat sb;
3641 if (!no_ignores && match_ignores(ignores, path))
3642 return NULL;
3644 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3645 if (ie)
3646 return report_file_status(ie, ondisk_path, -1, NULL,
3647 status_cb, status_arg, repo, report_unchanged);
3649 if (lstat(ondisk_path, &sb) == -1) {
3650 if (errno != ENOENT)
3651 return got_error_from_errno2("lstat", ondisk_path);
3652 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3654 return NULL;
3657 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3658 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3659 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3661 return NULL;
3664 static const struct got_error *
3665 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3666 const char *root_path, const char *path)
3668 const struct got_error *err;
3669 char *parent_path, *next_parent_path = NULL;
3671 err = add_ignores(ignores, root_path, "", -1,
3672 ".cvsignore");
3673 if (err)
3674 return err;
3676 err = add_ignores(ignores, root_path, "", -1,
3677 ".gitignore");
3678 if (err)
3679 return err;
3681 err = got_path_dirname(&parent_path, path);
3682 if (err) {
3683 if (err->code == GOT_ERR_BAD_PATH)
3684 return NULL; /* cannot traverse parent */
3685 return err;
3687 for (;;) {
3688 err = add_ignores(ignores, root_path, parent_path, -1,
3689 ".cvsignore");
3690 if (err)
3691 break;
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3693 ".gitignore");
3694 if (err)
3695 break;
3696 err = got_path_dirname(&next_parent_path, parent_path);
3697 if (err) {
3698 if (err->code == GOT_ERR_BAD_PATH)
3699 err = NULL; /* traversed everything */
3700 break;
3702 if (got_path_is_root_dir(parent_path))
3703 break;
3704 free(parent_path);
3705 parent_path = next_parent_path;
3706 next_parent_path = NULL;
3709 free(parent_path);
3710 free(next_parent_path);
3711 return err;
3714 static const struct got_error *
3715 worktree_status(struct got_worktree *worktree, const char *path,
3716 struct got_fileindex *fileindex, struct got_repository *repo,
3717 got_worktree_status_cb status_cb, void *status_arg,
3718 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3719 int report_unchanged)
3721 const struct got_error *err = NULL;
3722 int fd = -1;
3723 struct got_fileindex_diff_dir_cb fdiff_cb;
3724 struct diff_dir_cb_arg arg;
3725 char *ondisk_path = NULL;
3726 struct got_pathlist_head ignores;
3728 TAILQ_INIT(&ignores);
3730 if (asprintf(&ondisk_path, "%s%s%s",
3731 worktree->root_path, path[0] ? "/" : "", path) == -1)
3732 return got_error_from_errno("asprintf");
3734 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3735 if (fd == -1) {
3736 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3737 errno != ELOOP)
3738 err = got_error_from_errno2("open", ondisk_path);
3739 else {
3740 if (!no_ignores) {
3741 err = add_ignores_from_parent_paths(&ignores,
3742 worktree->root_path, ondisk_path);
3743 if (err)
3744 goto done;
3746 err = report_single_file_status(path, ondisk_path,
3747 fileindex, status_cb, status_arg, repo,
3748 report_unchanged, &ignores, no_ignores);
3750 } else {
3751 fdiff_cb.diff_old_new = status_old_new;
3752 fdiff_cb.diff_old = status_old;
3753 fdiff_cb.diff_new = status_new;
3754 fdiff_cb.diff_traverse = status_traverse;
3755 arg.fileindex = fileindex;
3756 arg.worktree = worktree;
3757 arg.status_path = path;
3758 arg.status_path_len = strlen(path);
3759 arg.repo = repo;
3760 arg.status_cb = status_cb;
3761 arg.status_arg = status_arg;
3762 arg.cancel_cb = cancel_cb;
3763 arg.cancel_arg = cancel_arg;
3764 arg.report_unchanged = report_unchanged;
3765 arg.no_ignores = no_ignores;
3766 if (!no_ignores) {
3767 err = add_ignores_from_parent_paths(&ignores,
3768 worktree->root_path, path);
3769 if (err)
3770 goto done;
3772 arg.ignores = &ignores;
3773 err = got_fileindex_diff_dir(fileindex, fd,
3774 worktree->root_path, path, repo, &fdiff_cb, &arg);
3776 done:
3777 free_ignores(&ignores);
3778 if (fd != -1 && close(fd) == -1 && err == NULL)
3779 err = got_error_from_errno("close");
3780 free(ondisk_path);
3781 return err;
3784 const struct got_error *
3785 got_worktree_status(struct got_worktree *worktree,
3786 struct got_pathlist_head *paths, struct got_repository *repo,
3787 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3788 got_cancel_cb cancel_cb, void *cancel_arg)
3790 const struct got_error *err = NULL;
3791 char *fileindex_path = NULL;
3792 struct got_fileindex *fileindex = NULL;
3793 struct got_pathlist_entry *pe;
3795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3796 if (err)
3797 return err;
3799 TAILQ_FOREACH(pe, paths, entry) {
3800 err = worktree_status(worktree, pe->path, fileindex, repo,
3801 status_cb, status_arg, cancel_cb, cancel_arg,
3802 no_ignores, 0);
3803 if (err)
3804 break;
3806 free(fileindex_path);
3807 got_fileindex_free(fileindex);
3808 return err;
3811 const struct got_error *
3812 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3813 const char *arg)
3815 const struct got_error *err = NULL;
3816 char *resolved = NULL, *cwd = NULL, *path = NULL;
3817 size_t len;
3818 struct stat sb;
3819 char *abspath = NULL;
3820 char canonpath[PATH_MAX];
3822 *wt_path = NULL;
3824 cwd = getcwd(NULL, 0);
3825 if (cwd == NULL)
3826 return got_error_from_errno("getcwd");
3828 if (lstat(arg, &sb) == -1) {
3829 if (errno != ENOENT) {
3830 err = got_error_from_errno2("lstat", arg);
3831 goto done;
3833 sb.st_mode = 0;
3835 if (S_ISLNK(sb.st_mode)) {
3837 * We cannot use realpath(3) with symlinks since we want to
3838 * operate on the symlink itself.
3839 * But we can make the path absolute, assuming it is relative
3840 * to the current working directory, and then canonicalize it.
3842 if (!got_path_is_absolute(arg)) {
3843 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3844 err = got_error_from_errno("asprintf");
3845 goto done;
3849 err = got_canonpath(abspath ? abspath : arg, canonpath,
3850 sizeof(canonpath));
3851 if (err)
3852 goto done;
3853 resolved = strdup(canonpath);
3854 if (resolved == NULL) {
3855 err = got_error_from_errno("strdup");
3856 goto done;
3858 } else {
3859 resolved = realpath(arg, NULL);
3860 if (resolved == NULL) {
3861 if (errno != ENOENT) {
3862 err = got_error_from_errno2("realpath", arg);
3863 goto done;
3865 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 err = got_error_from_errno("asprintf");
3867 goto done;
3869 err = got_canonpath(abspath, canonpath,
3870 sizeof(canonpath));
3871 if (err)
3872 goto done;
3873 resolved = strdup(canonpath);
3874 if (resolved == NULL) {
3875 err = got_error_from_errno("strdup");
3876 goto done;
3881 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3882 strlen(got_worktree_get_root_path(worktree)))) {
3883 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3884 goto done;
3887 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3888 err = got_path_skip_common_ancestor(&path,
3889 got_worktree_get_root_path(worktree), resolved);
3890 if (err)
3891 goto done;
3892 } else {
3893 path = strdup("");
3894 if (path == NULL) {
3895 err = got_error_from_errno("strdup");
3896 goto done;
3900 /* XXX status walk can't deal with trailing slash! */
3901 len = strlen(path);
3902 while (len > 0 && path[len - 1] == '/') {
3903 path[len - 1] = '\0';
3904 len--;
3906 done:
3907 free(abspath);
3908 free(resolved);
3909 free(cwd);
3910 if (err == NULL)
3911 *wt_path = path;
3912 else
3913 free(path);
3914 return err;
3917 struct schedule_addition_args {
3918 struct got_worktree *worktree;
3919 struct got_fileindex *fileindex;
3920 got_worktree_checkout_cb progress_cb;
3921 void *progress_arg;
3922 struct got_repository *repo;
3925 static const struct got_error *
3926 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3927 const char *relpath, struct got_object_id *blob_id,
3928 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3929 int dirfd, const char *de_name)
3931 struct schedule_addition_args *a = arg;
3932 const struct got_error *err = NULL;
3933 struct got_fileindex_entry *ie;
3934 struct stat sb;
3935 char *ondisk_path;
3937 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3938 relpath) == -1)
3939 return got_error_from_errno("asprintf");
3941 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3942 if (ie) {
3943 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3944 de_name, a->repo);
3945 if (err)
3946 goto done;
3947 /* Re-adding an existing entry is a no-op. */
3948 if (status == GOT_STATUS_ADD)
3949 goto done;
3950 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3951 if (err)
3952 goto done;
3955 if (status != GOT_STATUS_UNVERSIONED) {
3956 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3957 goto done;
3960 err = got_fileindex_entry_alloc(&ie, relpath);
3961 if (err)
3962 goto done;
3963 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3964 relpath, NULL, NULL, 1);
3965 if (err) {
3966 got_fileindex_entry_free(ie);
3967 goto done;
3969 err = got_fileindex_entry_add(a->fileindex, ie);
3970 if (err) {
3971 got_fileindex_entry_free(ie);
3972 goto done;
3974 done:
3975 free(ondisk_path);
3976 if (err)
3977 return err;
3978 if (status == GOT_STATUS_ADD)
3979 return NULL;
3980 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3983 const struct got_error *
3984 got_worktree_schedule_add(struct got_worktree *worktree,
3985 struct got_pathlist_head *paths,
3986 got_worktree_checkout_cb progress_cb, void *progress_arg,
3987 struct got_repository *repo, int no_ignores)
3989 struct got_fileindex *fileindex = NULL;
3990 char *fileindex_path = NULL;
3991 const struct got_error *err = NULL, *sync_err, *unlockerr;
3992 struct got_pathlist_entry *pe;
3993 struct schedule_addition_args saa;
3995 err = lock_worktree(worktree, LOCK_EX);
3996 if (err)
3997 return err;
3999 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4000 if (err)
4001 goto done;
4003 saa.worktree = worktree;
4004 saa.fileindex = fileindex;
4005 saa.progress_cb = progress_cb;
4006 saa.progress_arg = progress_arg;
4007 saa.repo = repo;
4009 TAILQ_FOREACH(pe, paths, entry) {
4010 err = worktree_status(worktree, pe->path, fileindex, repo,
4011 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4012 if (err)
4013 break;
4015 sync_err = sync_fileindex(fileindex, fileindex_path);
4016 if (sync_err && err == NULL)
4017 err = sync_err;
4018 done:
4019 free(fileindex_path);
4020 if (fileindex)
4021 got_fileindex_free(fileindex);
4022 unlockerr = lock_worktree(worktree, LOCK_SH);
4023 if (unlockerr && err == NULL)
4024 err = unlockerr;
4025 return err;
4028 struct schedule_deletion_args {
4029 struct got_worktree *worktree;
4030 struct got_fileindex *fileindex;
4031 got_worktree_delete_cb progress_cb;
4032 void *progress_arg;
4033 struct got_repository *repo;
4034 int delete_local_mods;
4035 int keep_on_disk;
4036 const char *status_codes;
4039 static const struct got_error *
4040 schedule_for_deletion(void *arg, unsigned char status,
4041 unsigned char staged_status, const char *relpath,
4042 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 struct got_object_id *commit_id, int dirfd, const char *de_name)
4045 struct schedule_deletion_args *a = arg;
4046 const struct got_error *err = NULL;
4047 struct got_fileindex_entry *ie = NULL;
4048 struct stat sb;
4049 char *ondisk_path;
4051 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4052 if (ie == NULL)
4053 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4055 staged_status = get_staged_status(ie);
4056 if (staged_status != GOT_STATUS_NO_CHANGE) {
4057 if (staged_status == GOT_STATUS_DELETE)
4058 return NULL;
4059 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4063 relpath) == -1)
4064 return got_error_from_errno("asprintf");
4066 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4067 a->repo);
4068 if (err)
4069 goto done;
4071 if (a->status_codes) {
4072 size_t ncodes = strlen(a->status_codes);
4073 int i;
4074 for (i = 0; i < ncodes ; i++) {
4075 if (status == a->status_codes[i])
4076 break;
4078 if (i == ncodes) {
4079 /* Do not delete files in non-matching status. */
4080 free(ondisk_path);
4081 return NULL;
4083 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4084 a->status_codes[i] != GOT_STATUS_MISSING) {
4085 static char msg[64];
4086 snprintf(msg, sizeof(msg),
4087 "invalid status code '%c'", a->status_codes[i]);
4088 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4089 goto done;
4093 if (status != GOT_STATUS_NO_CHANGE) {
4094 if (status == GOT_STATUS_DELETE)
4095 goto done;
4096 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4097 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4098 goto done;
4100 if (status != GOT_STATUS_MODIFY &&
4101 status != GOT_STATUS_MISSING) {
4102 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4103 goto done;
4107 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4108 size_t root_len;
4110 if (dirfd != -1) {
4111 if (unlinkat(dirfd, de_name, 0) != 0) {
4112 err = got_error_from_errno2("unlinkat",
4113 ondisk_path);
4114 goto done;
4116 } else if (unlink(ondisk_path) != 0) {
4117 err = got_error_from_errno2("unlink", ondisk_path);
4118 goto done;
4121 root_len = strlen(a->worktree->root_path);
4122 do {
4123 char *parent;
4124 err = got_path_dirname(&parent, ondisk_path);
4125 if (err)
4126 goto done;
4127 free(ondisk_path);
4128 ondisk_path = parent;
4129 if (rmdir(ondisk_path) == -1) {
4130 if (errno != ENOTEMPTY)
4131 err = got_error_from_errno2("rmdir",
4132 ondisk_path);
4133 break;
4135 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4136 strlen(ondisk_path), root_len) != 0);
4139 got_fileindex_entry_mark_deleted_from_disk(ie);
4140 done:
4141 free(ondisk_path);
4142 if (err)
4143 return err;
4144 if (status == GOT_STATUS_DELETE)
4145 return NULL;
4146 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4147 staged_status, relpath);
4150 const struct got_error *
4151 got_worktree_schedule_delete(struct got_worktree *worktree,
4152 struct got_pathlist_head *paths, int delete_local_mods,
4153 const char *status_codes,
4154 got_worktree_delete_cb progress_cb, void *progress_arg,
4155 struct got_repository *repo, int keep_on_disk)
4157 struct got_fileindex *fileindex = NULL;
4158 char *fileindex_path = NULL;
4159 const struct got_error *err = NULL, *sync_err, *unlockerr;
4160 struct got_pathlist_entry *pe;
4161 struct schedule_deletion_args sda;
4163 err = lock_worktree(worktree, LOCK_EX);
4164 if (err)
4165 return err;
4167 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4168 if (err)
4169 goto done;
4171 sda.worktree = worktree;
4172 sda.fileindex = fileindex;
4173 sda.progress_cb = progress_cb;
4174 sda.progress_arg = progress_arg;
4175 sda.repo = repo;
4176 sda.delete_local_mods = delete_local_mods;
4177 sda.keep_on_disk = keep_on_disk;
4178 sda.status_codes = status_codes;
4180 TAILQ_FOREACH(pe, paths, entry) {
4181 err = worktree_status(worktree, pe->path, fileindex, repo,
4182 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4183 if (err)
4184 break;
4186 sync_err = sync_fileindex(fileindex, fileindex_path);
4187 if (sync_err && err == NULL)
4188 err = sync_err;
4189 done:
4190 free(fileindex_path);
4191 if (fileindex)
4192 got_fileindex_free(fileindex);
4193 unlockerr = lock_worktree(worktree, LOCK_SH);
4194 if (unlockerr && err == NULL)
4195 err = unlockerr;
4196 return err;
4199 static const struct got_error *
4200 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4202 const struct got_error *err = NULL;
4203 char *line = NULL;
4204 size_t linesize = 0, n;
4205 ssize_t linelen;
4207 linelen = getline(&line, &linesize, infile);
4208 if (linelen == -1) {
4209 if (ferror(infile)) {
4210 err = got_error_from_errno("getline");
4211 goto done;
4213 return NULL;
4215 if (outfile) {
4216 n = fwrite(line, 1, linelen, outfile);
4217 if (n != linelen) {
4218 err = got_ferror(outfile, GOT_ERR_IO);
4219 goto done;
4222 if (rejectfile) {
4223 n = fwrite(line, 1, linelen, rejectfile);
4224 if (n != linelen)
4225 err = got_ferror(outfile, GOT_ERR_IO);
4227 done:
4228 free(line);
4229 return err;
4232 static const struct got_error *
4233 skip_one_line(FILE *f)
4235 char *line = NULL;
4236 size_t linesize = 0;
4237 ssize_t linelen;
4239 linelen = getline(&line, &linesize, f);
4240 if (linelen == -1) {
4241 if (ferror(f))
4242 return got_error_from_errno("getline");
4243 return NULL;
4245 free(line);
4246 return NULL;
4249 static const struct got_error *
4250 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4251 int start_old, int end_old, int start_new, int end_new,
4252 FILE *outfile, FILE *rejectfile)
4254 const struct got_error *err;
4256 /* Copy old file's lines leading up to patch. */
4257 while (!feof(f1) && *line_cur1 < start_old) {
4258 err = copy_one_line(f1, outfile, NULL);
4259 if (err)
4260 return err;
4261 (*line_cur1)++;
4263 /* Skip new file's lines leading up to patch. */
4264 while (!feof(f2) && *line_cur2 < start_new) {
4265 if (rejectfile)
4266 err = copy_one_line(f2, NULL, rejectfile);
4267 else
4268 err = skip_one_line(f2);
4269 if (err)
4270 return err;
4271 (*line_cur2)++;
4273 /* Copy patched lines. */
4274 while (!feof(f2) && *line_cur2 <= end_new) {
4275 err = copy_one_line(f2, outfile, NULL);
4276 if (err)
4277 return err;
4278 (*line_cur2)++;
4280 /* Skip over old file's replaced lines. */
4281 while (!feof(f1) && *line_cur1 <= end_old) {
4282 if (rejectfile)
4283 err = copy_one_line(f1, NULL, rejectfile);
4284 else
4285 err = skip_one_line(f1);
4286 if (err)
4287 return err;
4288 (*line_cur1)++;
4291 return NULL;
4294 static const struct got_error *
4295 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4296 FILE *outfile, FILE *rejectfile)
4298 const struct got_error *err;
4300 if (outfile) {
4301 /* Copy old file's lines until EOF. */
4302 while (!feof(f1)) {
4303 err = copy_one_line(f1, outfile, NULL);
4304 if (err)
4305 return err;
4306 (*line_cur1)++;
4309 if (rejectfile) {
4310 /* Copy new file's lines until EOF. */
4311 while (!feof(f2)) {
4312 err = copy_one_line(f2, NULL, rejectfile);
4313 if (err)
4314 return err;
4315 (*line_cur2)++;
4319 return NULL;
4322 static const struct got_error *
4323 apply_or_reject_change(int *choice, int *nchunks_used,
4324 struct diff_result *diff_result, int n,
4325 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4326 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4327 got_worktree_patch_cb patch_cb, void *patch_arg)
4329 const struct got_error *err = NULL;
4330 struct diff_chunk_context cc = {};
4331 int start_old, end_old, start_new, end_new;
4332 FILE *hunkfile;
4333 struct diff_output_unidiff_state *diff_state;
4334 struct diff_input_info diff_info;
4335 int rc;
4337 *choice = GOT_PATCH_CHOICE_NONE;
4339 /* Get changed line numbers without context lines for copy_change(). */
4340 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4341 start_old = cc.left.start;
4342 end_old = cc.left.end;
4343 start_new = cc.right.start;
4344 end_new = cc.right.end;
4346 /* Get the same change with context lines for display. */
4347 memset(&cc, 0, sizeof(cc));
4348 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4350 memset(&diff_info, 0, sizeof(diff_info));
4351 diff_info.left_path = relpath;
4352 diff_info.right_path = relpath;
4354 diff_state = diff_output_unidiff_state_alloc();
4355 if (diff_state == NULL)
4356 return got_error_set_errno(ENOMEM,
4357 "diff_output_unidiff_state_alloc");
4359 hunkfile = got_opentemp();
4360 if (hunkfile == NULL) {
4361 err = got_error_from_errno("got_opentemp");
4362 goto done;
4365 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4366 diff_result, &cc);
4367 if (rc != DIFF_RC_OK) {
4368 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4369 goto done;
4372 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4373 err = got_ferror(hunkfile, GOT_ERR_IO);
4374 goto done;
4377 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4378 hunkfile, changeno, nchanges);
4379 if (err)
4380 goto done;
4382 switch (*choice) {
4383 case GOT_PATCH_CHOICE_YES:
4384 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4385 end_old, start_new, end_new, outfile, rejectfile);
4386 break;
4387 case GOT_PATCH_CHOICE_NO:
4388 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4389 end_old, start_new, end_new, rejectfile, outfile);
4390 break;
4391 case GOT_PATCH_CHOICE_QUIT:
4392 break;
4393 default:
4394 err = got_error(GOT_ERR_PATCH_CHOICE);
4395 break;
4397 done:
4398 diff_output_unidiff_state_free(diff_state);
4399 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4400 err = got_error_from_errno("fclose");
4401 return err;
4404 struct revert_file_args {
4405 struct got_worktree *worktree;
4406 struct got_fileindex *fileindex;
4407 got_worktree_checkout_cb progress_cb;
4408 void *progress_arg;
4409 got_worktree_patch_cb patch_cb;
4410 void *patch_arg;
4411 struct got_repository *repo;
4414 static const struct got_error *
4415 create_patched_content(char **path_outfile, int reverse_patch,
4416 struct got_object_id *blob_id, const char *path2,
4417 int dirfd2, const char *de_name2,
4418 const char *relpath, struct got_repository *repo,
4419 got_worktree_patch_cb patch_cb, void *patch_arg)
4421 const struct got_error *err, *free_err;
4422 struct got_blob_object *blob = NULL;
4423 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4424 int fd2 = -1;
4425 char link_target[PATH_MAX];
4426 ssize_t link_len = 0;
4427 char *path1 = NULL, *id_str = NULL;
4428 struct stat sb2;
4429 struct got_diffreg_result *diffreg_result = NULL;
4430 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4431 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4433 *path_outfile = NULL;
4435 err = got_object_id_str(&id_str, blob_id);
4436 if (err)
4437 return err;
4439 if (dirfd2 != -1) {
4440 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4441 if (fd2 == -1) {
4442 if (errno != ELOOP) {
4443 err = got_error_from_errno2("openat", path2);
4444 goto done;
4446 link_len = readlinkat(dirfd2, de_name2,
4447 link_target, sizeof(link_target));
4448 if (link_len == -1)
4449 return got_error_from_errno2("readlinkat", path2);
4450 sb2.st_mode = S_IFLNK;
4451 sb2.st_size = link_len;
4453 } else {
4454 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4455 if (fd2 == -1) {
4456 if (errno != ELOOP) {
4457 err = got_error_from_errno2("open", path2);
4458 goto done;
4460 link_len = readlink(path2, link_target,
4461 sizeof(link_target));
4462 if (link_len == -1)
4463 return got_error_from_errno2("readlink", path2);
4464 sb2.st_mode = S_IFLNK;
4465 sb2.st_size = link_len;
4468 if (fd2 != -1) {
4469 if (fstat(fd2, &sb2) == -1) {
4470 err = got_error_from_errno2("fstat", path2);
4471 goto done;
4474 f2 = fdopen(fd2, "r");
4475 if (f2 == NULL) {
4476 err = got_error_from_errno2("fdopen", path2);
4477 goto done;
4479 fd2 = -1;
4480 } else {
4481 size_t n;
4482 f2 = got_opentemp();
4483 if (f2 == NULL) {
4484 err = got_error_from_errno2("got_opentemp", path2);
4485 goto done;
4487 n = fwrite(link_target, 1, link_len, f2);
4488 if (n != link_len) {
4489 err = got_ferror(f2, GOT_ERR_IO);
4490 goto done;
4492 if (fflush(f2) == EOF) {
4493 err = got_error_from_errno("fflush");
4494 goto done;
4496 rewind(f2);
4499 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4500 if (err)
4501 goto done;
4503 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4504 if (err)
4505 goto done;
4507 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4508 if (err)
4509 goto done;
4511 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4512 NULL);
4513 if (err)
4514 goto done;
4516 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4517 if (err)
4518 goto done;
4520 if (fseek(f1, 0L, SEEK_SET) == -1)
4521 return got_ferror(f1, GOT_ERR_IO);
4522 if (fseek(f2, 0L, SEEK_SET) == -1)
4523 return got_ferror(f2, GOT_ERR_IO);
4525 /* Count the number of actual changes in the diff result. */
4526 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4527 struct diff_chunk_context cc = {};
4528 diff_chunk_context_load_change(&cc, &nchunks_used,
4529 diffreg_result->result, n, 0);
4530 nchanges++;
4532 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4533 int choice;
4534 err = apply_or_reject_change(&choice, &nchunks_used,
4535 diffreg_result->result, n, relpath, f1, f2,
4536 &line_cur1, &line_cur2,
4537 reverse_patch ? NULL : outfile,
4538 reverse_patch ? outfile : NULL,
4539 ++i, nchanges, patch_cb, patch_arg);
4540 if (err)
4541 goto done;
4542 if (choice == GOT_PATCH_CHOICE_YES)
4543 have_content = 1;
4544 else if (choice == GOT_PATCH_CHOICE_QUIT)
4545 break;
4547 if (have_content) {
4548 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4549 reverse_patch ? NULL : outfile,
4550 reverse_patch ? outfile : NULL);
4551 if (err)
4552 goto done;
4554 if (!S_ISLNK(sb2.st_mode)) {
4555 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4556 err = got_error_from_errno2("fchmod", path2);
4557 goto done;
4561 done:
4562 free(id_str);
4563 if (blob)
4564 got_object_blob_close(blob);
4565 free_err = got_diffreg_result_free(diffreg_result);
4566 if (err == NULL)
4567 err = free_err;
4568 if (f1 && fclose(f1) == EOF && err == NULL)
4569 err = got_error_from_errno2("fclose", path1);
4570 if (f2 && fclose(f2) == EOF && err == NULL)
4571 err = got_error_from_errno2("fclose", path2);
4572 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4573 err = got_error_from_errno2("close", path2);
4574 if (outfile && fclose(outfile) == EOF && err == NULL)
4575 err = got_error_from_errno2("fclose", *path_outfile);
4576 if (path1 && unlink(path1) == -1 && err == NULL)
4577 err = got_error_from_errno2("unlink", path1);
4578 if (err || !have_content) {
4579 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4580 err = got_error_from_errno2("unlink", *path_outfile);
4581 free(*path_outfile);
4582 *path_outfile = NULL;
4584 free(path1);
4585 return err;
4588 static const struct got_error *
4589 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4590 const char *relpath, struct got_object_id *blob_id,
4591 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4592 int dirfd, const char *de_name)
4594 struct revert_file_args *a = arg;
4595 const struct got_error *err = NULL;
4596 char *parent_path = NULL;
4597 struct got_fileindex_entry *ie;
4598 struct got_tree_object *tree = NULL;
4599 struct got_object_id *tree_id = NULL;
4600 const struct got_tree_entry *te = NULL;
4601 char *tree_path = NULL, *te_name;
4602 char *ondisk_path = NULL, *path_content = NULL;
4603 struct got_blob_object *blob = NULL;
4605 /* Reverting a staged deletion is a no-op. */
4606 if (status == GOT_STATUS_DELETE &&
4607 staged_status != GOT_STATUS_NO_CHANGE)
4608 return NULL;
4610 if (status == GOT_STATUS_UNVERSIONED)
4611 return (*a->progress_cb)(a->progress_arg,
4612 GOT_STATUS_UNVERSIONED, relpath);
4614 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4615 if (ie == NULL)
4616 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4618 /* Construct in-repository path of tree which contains this blob. */
4619 err = got_path_dirname(&parent_path, ie->path);
4620 if (err) {
4621 if (err->code != GOT_ERR_BAD_PATH)
4622 goto done;
4623 parent_path = strdup("/");
4624 if (parent_path == NULL) {
4625 err = got_error_from_errno("strdup");
4626 goto done;
4629 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4630 tree_path = strdup(parent_path);
4631 if (tree_path == NULL) {
4632 err = got_error_from_errno("strdup");
4633 goto done;
4635 } else {
4636 if (got_path_is_root_dir(parent_path)) {
4637 tree_path = strdup(a->worktree->path_prefix);
4638 if (tree_path == NULL) {
4639 err = got_error_from_errno("strdup");
4640 goto done;
4642 } else {
4643 if (asprintf(&tree_path, "%s/%s",
4644 a->worktree->path_prefix, parent_path) == -1) {
4645 err = got_error_from_errno("asprintf");
4646 goto done;
4651 err = got_object_id_by_path(&tree_id, a->repo,
4652 a->worktree->base_commit_id, tree_path);
4653 if (err) {
4654 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4655 (status == GOT_STATUS_ADD ||
4656 staged_status == GOT_STATUS_ADD)))
4657 goto done;
4658 } else {
4659 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4660 if (err)
4661 goto done;
4663 err = got_path_basename(&te_name, ie->path);
4664 if (err)
4665 goto done;
4667 te = got_object_tree_find_entry(tree, te_name);
4668 free(te_name);
4669 if (te == NULL && status != GOT_STATUS_ADD &&
4670 staged_status != GOT_STATUS_ADD) {
4671 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4672 goto done;
4676 switch (status) {
4677 case GOT_STATUS_ADD:
4678 if (a->patch_cb) {
4679 int choice = GOT_PATCH_CHOICE_NONE;
4680 err = (*a->patch_cb)(&choice, a->patch_arg,
4681 status, ie->path, NULL, 1, 1);
4682 if (err)
4683 goto done;
4684 if (choice != GOT_PATCH_CHOICE_YES)
4685 break;
4687 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4688 ie->path);
4689 if (err)
4690 goto done;
4691 got_fileindex_entry_remove(a->fileindex, ie);
4692 break;
4693 case GOT_STATUS_DELETE:
4694 if (a->patch_cb) {
4695 int choice = GOT_PATCH_CHOICE_NONE;
4696 err = (*a->patch_cb)(&choice, a->patch_arg,
4697 status, ie->path, NULL, 1, 1);
4698 if (err)
4699 goto done;
4700 if (choice != GOT_PATCH_CHOICE_YES)
4701 break;
4703 /* fall through */
4704 case GOT_STATUS_MODIFY:
4705 case GOT_STATUS_MODE_CHANGE:
4706 case GOT_STATUS_CONFLICT:
4707 case GOT_STATUS_MISSING: {
4708 struct got_object_id id;
4709 if (staged_status == GOT_STATUS_ADD ||
4710 staged_status == GOT_STATUS_MODIFY) {
4711 memcpy(id.sha1, ie->staged_blob_sha1,
4712 SHA1_DIGEST_LENGTH);
4713 } else
4714 memcpy(id.sha1, ie->blob_sha1,
4715 SHA1_DIGEST_LENGTH);
4716 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4717 if (err)
4718 goto done;
4720 if (asprintf(&ondisk_path, "%s/%s",
4721 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4722 err = got_error_from_errno("asprintf");
4723 goto done;
4726 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4727 status == GOT_STATUS_CONFLICT)) {
4728 int is_bad_symlink = 0;
4729 err = create_patched_content(&path_content, 1, &id,
4730 ondisk_path, dirfd, de_name, ie->path, a->repo,
4731 a->patch_cb, a->patch_arg);
4732 if (err || path_content == NULL)
4733 break;
4734 if (te && S_ISLNK(te->mode)) {
4735 if (unlink(path_content) == -1) {
4736 err = got_error_from_errno2("unlink",
4737 path_content);
4738 break;
4740 err = install_symlink(&is_bad_symlink,
4741 a->worktree, ondisk_path, ie->path,
4742 blob, 0, 1, 0, a->repo,
4743 a->progress_cb, a->progress_arg);
4744 } else {
4745 if (rename(path_content, ondisk_path) == -1) {
4746 err = got_error_from_errno3("rename",
4747 path_content, ondisk_path);
4748 goto done;
4751 } else {
4752 int is_bad_symlink = 0;
4753 if (te && S_ISLNK(te->mode)) {
4754 err = install_symlink(&is_bad_symlink,
4755 a->worktree, ondisk_path, ie->path,
4756 blob, 0, 1, 0, a->repo,
4757 a->progress_cb, a->progress_arg);
4758 } else {
4759 err = install_blob(a->worktree, ondisk_path,
4760 ie->path,
4761 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4762 got_fileindex_perms_to_st(ie), blob,
4763 0, 1, 0, 0, a->repo,
4764 a->progress_cb, a->progress_arg);
4766 if (err)
4767 goto done;
4768 if (status == GOT_STATUS_DELETE ||
4769 status == GOT_STATUS_MODE_CHANGE) {
4770 err = got_fileindex_entry_update(ie,
4771 a->worktree->root_fd, relpath,
4772 blob->id.sha1,
4773 a->worktree->base_commit_id->sha1, 1);
4774 if (err)
4775 goto done;
4777 if (is_bad_symlink) {
4778 got_fileindex_entry_filetype_set(ie,
4779 GOT_FILEIDX_MODE_BAD_SYMLINK);
4782 break;
4784 default:
4785 break;
4787 done:
4788 free(ondisk_path);
4789 free(path_content);
4790 free(parent_path);
4791 free(tree_path);
4792 if (blob)
4793 got_object_blob_close(blob);
4794 if (tree)
4795 got_object_tree_close(tree);
4796 free(tree_id);
4797 return err;
4800 const struct got_error *
4801 got_worktree_revert(struct got_worktree *worktree,
4802 struct got_pathlist_head *paths,
4803 got_worktree_checkout_cb progress_cb, void *progress_arg,
4804 got_worktree_patch_cb patch_cb, void *patch_arg,
4805 struct got_repository *repo)
4807 struct got_fileindex *fileindex = NULL;
4808 char *fileindex_path = NULL;
4809 const struct got_error *err = NULL, *unlockerr = NULL;
4810 const struct got_error *sync_err = NULL;
4811 struct got_pathlist_entry *pe;
4812 struct revert_file_args rfa;
4814 err = lock_worktree(worktree, LOCK_EX);
4815 if (err)
4816 return err;
4818 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4819 if (err)
4820 goto done;
4822 rfa.worktree = worktree;
4823 rfa.fileindex = fileindex;
4824 rfa.progress_cb = progress_cb;
4825 rfa.progress_arg = progress_arg;
4826 rfa.patch_cb = patch_cb;
4827 rfa.patch_arg = patch_arg;
4828 rfa.repo = repo;
4829 TAILQ_FOREACH(pe, paths, entry) {
4830 err = worktree_status(worktree, pe->path, fileindex, repo,
4831 revert_file, &rfa, NULL, NULL, 0, 0);
4832 if (err)
4833 break;
4835 sync_err = sync_fileindex(fileindex, fileindex_path);
4836 if (sync_err && err == NULL)
4837 err = sync_err;
4838 done:
4839 free(fileindex_path);
4840 if (fileindex)
4841 got_fileindex_free(fileindex);
4842 unlockerr = lock_worktree(worktree, LOCK_SH);
4843 if (unlockerr && err == NULL)
4844 err = unlockerr;
4845 return err;
4848 static void
4849 free_commitable(struct got_commitable *ct)
4851 free(ct->path);
4852 free(ct->in_repo_path);
4853 free(ct->ondisk_path);
4854 free(ct->blob_id);
4855 free(ct->base_blob_id);
4856 free(ct->staged_blob_id);
4857 free(ct->base_commit_id);
4858 free(ct);
4861 struct collect_commitables_arg {
4862 struct got_pathlist_head *commitable_paths;
4863 struct got_repository *repo;
4864 struct got_worktree *worktree;
4865 struct got_fileindex *fileindex;
4866 int have_staged_files;
4867 int allow_bad_symlinks;
4870 static const struct got_error *
4871 collect_commitables(void *arg, unsigned char status,
4872 unsigned char staged_status, const char *relpath,
4873 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4874 struct got_object_id *commit_id, int dirfd, const char *de_name)
4876 struct collect_commitables_arg *a = arg;
4877 const struct got_error *err = NULL;
4878 struct got_commitable *ct = NULL;
4879 struct got_pathlist_entry *new = NULL;
4880 char *parent_path = NULL, *path = NULL;
4881 struct stat sb;
4883 if (a->have_staged_files) {
4884 if (staged_status != GOT_STATUS_MODIFY &&
4885 staged_status != GOT_STATUS_ADD &&
4886 staged_status != GOT_STATUS_DELETE)
4887 return NULL;
4888 } else {
4889 if (status == GOT_STATUS_CONFLICT)
4890 return got_error(GOT_ERR_COMMIT_CONFLICT);
4892 if (status != GOT_STATUS_MODIFY &&
4893 status != GOT_STATUS_MODE_CHANGE &&
4894 status != GOT_STATUS_ADD &&
4895 status != GOT_STATUS_DELETE)
4896 return NULL;
4899 if (asprintf(&path, "/%s", relpath) == -1) {
4900 err = got_error_from_errno("asprintf");
4901 goto done;
4903 if (strcmp(path, "/") == 0) {
4904 parent_path = strdup("");
4905 if (parent_path == NULL)
4906 return got_error_from_errno("strdup");
4907 } else {
4908 err = got_path_dirname(&parent_path, path);
4909 if (err)
4910 return err;
4913 ct = calloc(1, sizeof(*ct));
4914 if (ct == NULL) {
4915 err = got_error_from_errno("calloc");
4916 goto done;
4919 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4920 relpath) == -1) {
4921 err = got_error_from_errno("asprintf");
4922 goto done;
4925 if (staged_status == GOT_STATUS_ADD ||
4926 staged_status == GOT_STATUS_MODIFY) {
4927 struct got_fileindex_entry *ie;
4928 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4929 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4930 case GOT_FILEIDX_MODE_REGULAR_FILE:
4931 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4932 ct->mode = S_IFREG;
4933 break;
4934 case GOT_FILEIDX_MODE_SYMLINK:
4935 ct->mode = S_IFLNK;
4936 break;
4937 default:
4938 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4939 goto done;
4941 ct->mode |= got_fileindex_entry_perms_get(ie);
4942 } else if (status != GOT_STATUS_DELETE &&
4943 staged_status != GOT_STATUS_DELETE) {
4944 if (dirfd != -1) {
4945 if (fstatat(dirfd, de_name, &sb,
4946 AT_SYMLINK_NOFOLLOW) == -1) {
4947 err = got_error_from_errno2("fstatat",
4948 ct->ondisk_path);
4949 goto done;
4951 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4952 err = got_error_from_errno2("lstat", ct->ondisk_path);
4953 goto done;
4955 ct->mode = sb.st_mode;
4958 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4959 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4960 relpath) == -1) {
4961 err = got_error_from_errno("asprintf");
4962 goto done;
4965 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4966 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4967 int is_bad_symlink;
4968 char target_path[PATH_MAX];
4969 ssize_t target_len;
4970 target_len = readlink(ct->ondisk_path, target_path,
4971 sizeof(target_path));
4972 if (target_len == -1) {
4973 err = got_error_from_errno2("readlink",
4974 ct->ondisk_path);
4975 goto done;
4977 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4978 target_len, ct->ondisk_path, a->worktree->root_path);
4979 if (err)
4980 goto done;
4981 if (is_bad_symlink) {
4982 err = got_error_path(ct->ondisk_path,
4983 GOT_ERR_BAD_SYMLINK);
4984 goto done;
4989 ct->status = status;
4990 ct->staged_status = staged_status;
4991 ct->blob_id = NULL; /* will be filled in when blob gets created */
4992 if (ct->status != GOT_STATUS_ADD &&
4993 ct->staged_status != GOT_STATUS_ADD) {
4994 ct->base_blob_id = got_object_id_dup(blob_id);
4995 if (ct->base_blob_id == NULL) {
4996 err = got_error_from_errno("got_object_id_dup");
4997 goto done;
4999 ct->base_commit_id = got_object_id_dup(commit_id);
5000 if (ct->base_commit_id == NULL) {
5001 err = got_error_from_errno("got_object_id_dup");
5002 goto done;
5005 if (ct->staged_status == GOT_STATUS_ADD ||
5006 ct->staged_status == GOT_STATUS_MODIFY) {
5007 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5008 if (ct->staged_blob_id == NULL) {
5009 err = got_error_from_errno("got_object_id_dup");
5010 goto done;
5013 ct->path = strdup(path);
5014 if (ct->path == NULL) {
5015 err = got_error_from_errno("strdup");
5016 goto done;
5018 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5019 done:
5020 if (ct && (err || new == NULL))
5021 free_commitable(ct);
5022 free(parent_path);
5023 free(path);
5024 return err;
5027 static const struct got_error *write_tree(struct got_object_id **, int *,
5028 struct got_tree_object *, const char *, struct got_pathlist_head *,
5029 got_worktree_status_cb status_cb, void *status_arg,
5030 struct got_repository *);
5032 static const struct got_error *
5033 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5034 struct got_tree_entry *te, const char *parent_path,
5035 struct got_pathlist_head *commitable_paths,
5036 got_worktree_status_cb status_cb, void *status_arg,
5037 struct got_repository *repo)
5039 const struct got_error *err = NULL;
5040 struct got_tree_object *subtree;
5041 char *subpath;
5043 if (asprintf(&subpath, "%s%s%s", parent_path,
5044 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5045 return got_error_from_errno("asprintf");
5047 err = got_object_open_as_tree(&subtree, repo, &te->id);
5048 if (err)
5049 return err;
5051 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5052 commitable_paths, status_cb, status_arg, repo);
5053 got_object_tree_close(subtree);
5054 free(subpath);
5055 return err;
5058 static const struct got_error *
5059 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5061 const struct got_error *err = NULL;
5062 char *ct_parent_path = NULL;
5064 *match = 0;
5066 if (strchr(ct->in_repo_path, '/') == NULL) {
5067 *match = got_path_is_root_dir(path);
5068 return NULL;
5071 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5072 if (err)
5073 return err;
5074 *match = (strcmp(path, ct_parent_path) == 0);
5075 free(ct_parent_path);
5076 return err;
5079 static mode_t
5080 get_ct_file_mode(struct got_commitable *ct)
5082 if (S_ISLNK(ct->mode))
5083 return S_IFLNK;
5085 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5088 static const struct got_error *
5089 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5090 struct got_tree_entry *te, struct got_commitable *ct)
5092 const struct got_error *err = NULL;
5094 *new_te = NULL;
5096 err = got_object_tree_entry_dup(new_te, te);
5097 if (err)
5098 goto done;
5100 (*new_te)->mode = get_ct_file_mode(ct);
5102 if (ct->staged_status == GOT_STATUS_MODIFY)
5103 memcpy(&(*new_te)->id, ct->staged_blob_id,
5104 sizeof((*new_te)->id));
5105 else
5106 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5107 done:
5108 if (err && *new_te) {
5109 free(*new_te);
5110 *new_te = NULL;
5112 return err;
5115 static const struct got_error *
5116 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5117 struct got_commitable *ct)
5119 const struct got_error *err = NULL;
5120 char *ct_name = NULL;
5122 *new_te = NULL;
5124 *new_te = calloc(1, sizeof(**new_te));
5125 if (*new_te == NULL)
5126 return got_error_from_errno("calloc");
5128 err = got_path_basename(&ct_name, ct->path);
5129 if (err)
5130 goto done;
5131 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5132 sizeof((*new_te)->name)) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5134 goto done;
5137 (*new_te)->mode = get_ct_file_mode(ct);
5139 if (ct->staged_status == GOT_STATUS_ADD)
5140 memcpy(&(*new_te)->id, ct->staged_blob_id,
5141 sizeof((*new_te)->id));
5142 else
5143 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5144 done:
5145 free(ct_name);
5146 if (err && *new_te) {
5147 free(*new_te);
5148 *new_te = NULL;
5150 return err;
5153 static const struct got_error *
5154 insert_tree_entry(struct got_tree_entry *new_te,
5155 struct got_pathlist_head *paths)
5157 const struct got_error *err = NULL;
5158 struct got_pathlist_entry *new_pe;
5160 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5161 if (err)
5162 return err;
5163 if (new_pe == NULL)
5164 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5165 return NULL;
5168 static const struct got_error *
5169 report_ct_status(struct got_commitable *ct,
5170 got_worktree_status_cb status_cb, void *status_arg)
5172 const char *ct_path = ct->path;
5173 unsigned char status;
5175 while (ct_path[0] == '/')
5176 ct_path++;
5178 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5179 status = ct->staged_status;
5180 else
5181 status = ct->status;
5183 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5184 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5187 static const struct got_error *
5188 match_modified_subtree(int *modified, struct got_tree_entry *te,
5189 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5191 const struct got_error *err = NULL;
5192 struct got_pathlist_entry *pe;
5193 char *te_path;
5195 *modified = 0;
5197 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5198 got_path_is_root_dir(base_tree_path) ? "" : "/",
5199 te->name) == -1)
5200 return got_error_from_errno("asprintf");
5202 TAILQ_FOREACH(pe, commitable_paths, entry) {
5203 struct got_commitable *ct = pe->data;
5204 *modified = got_path_is_child(ct->in_repo_path, te_path,
5205 strlen(te_path));
5206 if (*modified)
5207 break;
5210 free(te_path);
5211 return err;
5214 static const struct got_error *
5215 match_deleted_or_modified_ct(struct got_commitable **ctp,
5216 struct got_tree_entry *te, const char *base_tree_path,
5217 struct got_pathlist_head *commitable_paths)
5219 const struct got_error *err = NULL;
5220 struct got_pathlist_entry *pe;
5222 *ctp = NULL;
5224 TAILQ_FOREACH(pe, commitable_paths, entry) {
5225 struct got_commitable *ct = pe->data;
5226 char *ct_name = NULL;
5227 int path_matches;
5229 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5230 if (ct->status != GOT_STATUS_MODIFY &&
5231 ct->status != GOT_STATUS_MODE_CHANGE &&
5232 ct->status != GOT_STATUS_DELETE)
5233 continue;
5234 } else {
5235 if (ct->staged_status != GOT_STATUS_MODIFY &&
5236 ct->staged_status != GOT_STATUS_DELETE)
5237 continue;
5240 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5241 continue;
5243 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5244 if (err)
5245 return err;
5246 if (!path_matches)
5247 continue;
5249 err = got_path_basename(&ct_name, pe->path);
5250 if (err)
5251 return err;
5253 if (strcmp(te->name, ct_name) != 0) {
5254 free(ct_name);
5255 continue;
5257 free(ct_name);
5259 *ctp = ct;
5260 break;
5263 return err;
5266 static const struct got_error *
5267 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5268 const char *child_path, const char *path_base_tree,
5269 struct got_pathlist_head *commitable_paths,
5270 got_worktree_status_cb status_cb, void *status_arg,
5271 struct got_repository *repo)
5273 const struct got_error *err = NULL;
5274 struct got_tree_entry *new_te;
5275 char *subtree_path;
5276 struct got_object_id *id = NULL;
5277 int nentries;
5279 *new_tep = NULL;
5281 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5282 got_path_is_root_dir(path_base_tree) ? "" : "/",
5283 child_path) == -1)
5284 return got_error_from_errno("asprintf");
5286 new_te = calloc(1, sizeof(*new_te));
5287 if (new_te == NULL)
5288 return got_error_from_errno("calloc");
5289 new_te->mode = S_IFDIR;
5291 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5292 sizeof(new_te->name)) {
5293 err = got_error(GOT_ERR_NO_SPACE);
5294 goto done;
5296 err = write_tree(&id, &nentries, NULL, subtree_path,
5297 commitable_paths, status_cb, status_arg, repo);
5298 if (err) {
5299 free(new_te);
5300 goto done;
5302 memcpy(&new_te->id, id, sizeof(new_te->id));
5303 done:
5304 free(id);
5305 free(subtree_path);
5306 if (err == NULL)
5307 *new_tep = new_te;
5308 return err;
5311 static const struct got_error *
5312 write_tree(struct got_object_id **new_tree_id, int *nentries,
5313 struct got_tree_object *base_tree, const char *path_base_tree,
5314 struct got_pathlist_head *commitable_paths,
5315 got_worktree_status_cb status_cb, void *status_arg,
5316 struct got_repository *repo)
5318 const struct got_error *err = NULL;
5319 struct got_pathlist_head paths;
5320 struct got_tree_entry *te, *new_te = NULL;
5321 struct got_pathlist_entry *pe;
5323 TAILQ_INIT(&paths);
5324 *nentries = 0;
5326 /* Insert, and recurse into, newly added entries first. */
5327 TAILQ_FOREACH(pe, commitable_paths, entry) {
5328 struct got_commitable *ct = pe->data;
5329 char *child_path = NULL, *slash;
5331 if ((ct->status != GOT_STATUS_ADD &&
5332 ct->staged_status != GOT_STATUS_ADD) ||
5333 (ct->flags & GOT_COMMITABLE_ADDED))
5334 continue;
5336 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5337 strlen(path_base_tree)))
5338 continue;
5340 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5341 ct->in_repo_path);
5342 if (err)
5343 goto done;
5345 slash = strchr(child_path, '/');
5346 if (slash == NULL) {
5347 err = alloc_added_blob_tree_entry(&new_te, ct);
5348 if (err)
5349 goto done;
5350 err = report_ct_status(ct, status_cb, status_arg);
5351 if (err)
5352 goto done;
5353 ct->flags |= GOT_COMMITABLE_ADDED;
5354 err = insert_tree_entry(new_te, &paths);
5355 if (err)
5356 goto done;
5357 (*nentries)++;
5358 } else {
5359 *slash = '\0'; /* trim trailing path components */
5360 if (base_tree == NULL ||
5361 got_object_tree_find_entry(base_tree, child_path)
5362 == NULL) {
5363 err = make_subtree_for_added_blob(&new_te,
5364 child_path, path_base_tree,
5365 commitable_paths, status_cb, status_arg,
5366 repo);
5367 if (err)
5368 goto done;
5369 err = insert_tree_entry(new_te, &paths);
5370 if (err)
5371 goto done;
5372 (*nentries)++;
5377 if (base_tree) {
5378 int i, nbase_entries;
5379 /* Handle modified and deleted entries. */
5380 nbase_entries = got_object_tree_get_nentries(base_tree);
5381 for (i = 0; i < nbase_entries; i++) {
5382 struct got_commitable *ct = NULL;
5384 te = got_object_tree_get_entry(base_tree, i);
5385 if (got_object_tree_entry_is_submodule(te)) {
5386 /* Entry is a submodule; just copy it. */
5387 err = got_object_tree_entry_dup(&new_te, te);
5388 if (err)
5389 goto done;
5390 err = insert_tree_entry(new_te, &paths);
5391 if (err)
5392 goto done;
5393 (*nentries)++;
5394 continue;
5397 if (S_ISDIR(te->mode)) {
5398 int modified;
5399 err = got_object_tree_entry_dup(&new_te, te);
5400 if (err)
5401 goto done;
5402 err = match_modified_subtree(&modified, te,
5403 path_base_tree, commitable_paths);
5404 if (err)
5405 goto done;
5406 /* Avoid recursion into unmodified subtrees. */
5407 if (modified) {
5408 struct got_object_id *new_id;
5409 int nsubentries;
5410 err = write_subtree(&new_id,
5411 &nsubentries, te,
5412 path_base_tree, commitable_paths,
5413 status_cb, status_arg, repo);
5414 if (err)
5415 goto done;
5416 if (nsubentries == 0) {
5417 /* All entries were deleted. */
5418 free(new_id);
5419 continue;
5421 memcpy(&new_te->id, new_id,
5422 sizeof(new_te->id));
5423 free(new_id);
5425 err = insert_tree_entry(new_te, &paths);
5426 if (err)
5427 goto done;
5428 (*nentries)++;
5429 continue;
5432 err = match_deleted_or_modified_ct(&ct, te,
5433 path_base_tree, commitable_paths);
5434 if (err)
5435 goto done;
5436 if (ct) {
5437 /* NB: Deleted entries get dropped here. */
5438 if (ct->status == GOT_STATUS_MODIFY ||
5439 ct->status == GOT_STATUS_MODE_CHANGE ||
5440 ct->staged_status == GOT_STATUS_MODIFY) {
5441 err = alloc_modified_blob_tree_entry(
5442 &new_te, te, ct);
5443 if (err)
5444 goto done;
5445 err = insert_tree_entry(new_te, &paths);
5446 if (err)
5447 goto done;
5448 (*nentries)++;
5450 err = report_ct_status(ct, status_cb,
5451 status_arg);
5452 if (err)
5453 goto done;
5454 } else {
5455 /* Entry is unchanged; just copy it. */
5456 err = got_object_tree_entry_dup(&new_te, te);
5457 if (err)
5458 goto done;
5459 err = insert_tree_entry(new_te, &paths);
5460 if (err)
5461 goto done;
5462 (*nentries)++;
5467 /* Write new list of entries; deleted entries have been dropped. */
5468 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5469 done:
5470 got_pathlist_free(&paths);
5471 return err;
5474 static const struct got_error *
5475 update_fileindex_after_commit(struct got_worktree *worktree,
5476 struct got_pathlist_head *commitable_paths,
5477 struct got_object_id *new_base_commit_id,
5478 struct got_fileindex *fileindex, int have_staged_files)
5480 const struct got_error *err = NULL;
5481 struct got_pathlist_entry *pe;
5482 char *relpath = NULL;
5484 TAILQ_FOREACH(pe, commitable_paths, entry) {
5485 struct got_fileindex_entry *ie;
5486 struct got_commitable *ct = pe->data;
5488 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5490 err = got_path_skip_common_ancestor(&relpath,
5491 worktree->root_path, ct->ondisk_path);
5492 if (err)
5493 goto done;
5495 if (ie) {
5496 if (ct->status == GOT_STATUS_DELETE ||
5497 ct->staged_status == GOT_STATUS_DELETE) {
5498 got_fileindex_entry_remove(fileindex, ie);
5499 } else if (ct->staged_status == GOT_STATUS_ADD ||
5500 ct->staged_status == GOT_STATUS_MODIFY) {
5501 got_fileindex_entry_stage_set(ie,
5502 GOT_FILEIDX_STAGE_NONE);
5503 got_fileindex_entry_staged_filetype_set(ie, 0);
5505 err = got_fileindex_entry_update(ie,
5506 worktree->root_fd, relpath,
5507 ct->staged_blob_id->sha1,
5508 new_base_commit_id->sha1,
5509 !have_staged_files);
5510 } else
5511 err = got_fileindex_entry_update(ie,
5512 worktree->root_fd, relpath,
5513 ct->blob_id->sha1,
5514 new_base_commit_id->sha1,
5515 !have_staged_files);
5516 } else {
5517 err = got_fileindex_entry_alloc(&ie, pe->path);
5518 if (err)
5519 goto done;
5520 err = got_fileindex_entry_update(ie,
5521 worktree->root_fd, relpath, ct->blob_id->sha1,
5522 new_base_commit_id->sha1, 1);
5523 if (err) {
5524 got_fileindex_entry_free(ie);
5525 goto done;
5527 err = got_fileindex_entry_add(fileindex, ie);
5528 if (err) {
5529 got_fileindex_entry_free(ie);
5530 goto done;
5533 free(relpath);
5534 relpath = NULL;
5536 done:
5537 free(relpath);
5538 return err;
5542 static const struct got_error *
5543 check_out_of_date(const char *in_repo_path, unsigned char status,
5544 unsigned char staged_status, struct got_object_id *base_blob_id,
5545 struct got_object_id *base_commit_id,
5546 struct got_object_id *head_commit_id, struct got_repository *repo,
5547 int ood_errcode)
5549 const struct got_error *err = NULL;
5550 struct got_object_id *id = NULL;
5552 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5553 /* Trivial case: base commit == head commit */
5554 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5555 return NULL;
5557 * Ensure file content which local changes were based
5558 * on matches file content in the branch head.
5560 err = got_object_id_by_path(&id, repo, head_commit_id,
5561 in_repo_path);
5562 if (err) {
5563 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5564 err = got_error(ood_errcode);
5565 goto done;
5566 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5567 err = got_error(ood_errcode);
5568 } else {
5569 /* Require that added files don't exist in the branch head. */
5570 err = got_object_id_by_path(&id, repo, head_commit_id,
5571 in_repo_path);
5572 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5573 goto done;
5574 err = id ? got_error(ood_errcode) : NULL;
5576 done:
5577 free(id);
5578 return err;
5581 const struct got_error *
5582 commit_worktree(struct got_object_id **new_commit_id,
5583 struct got_pathlist_head *commitable_paths,
5584 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5585 const char *author, const char *committer,
5586 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5587 got_worktree_status_cb status_cb, void *status_arg,
5588 struct got_repository *repo)
5590 const struct got_error *err = NULL, *unlockerr = NULL;
5591 struct got_pathlist_entry *pe;
5592 const char *head_ref_name = NULL;
5593 struct got_commit_object *head_commit = NULL;
5594 struct got_reference *head_ref2 = NULL;
5595 struct got_object_id *head_commit_id2 = NULL;
5596 struct got_tree_object *head_tree = NULL;
5597 struct got_object_id *new_tree_id = NULL;
5598 int nentries;
5599 struct got_object_id_queue parent_ids;
5600 struct got_object_qid *pid = NULL;
5601 char *logmsg = NULL;
5603 *new_commit_id = NULL;
5605 STAILQ_INIT(&parent_ids);
5607 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5608 if (err)
5609 goto done;
5611 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5612 if (err)
5613 goto done;
5615 if (commit_msg_cb != NULL) {
5616 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5617 if (err)
5618 goto done;
5621 if (logmsg == NULL || strlen(logmsg) == 0) {
5622 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5623 goto done;
5626 /* Create blobs from added and modified files and record their IDs. */
5627 TAILQ_FOREACH(pe, commitable_paths, entry) {
5628 struct got_commitable *ct = pe->data;
5629 char *ondisk_path;
5631 /* Blobs for staged files already exist. */
5632 if (ct->staged_status == GOT_STATUS_ADD ||
5633 ct->staged_status == GOT_STATUS_MODIFY)
5634 continue;
5636 if (ct->status != GOT_STATUS_ADD &&
5637 ct->status != GOT_STATUS_MODIFY &&
5638 ct->status != GOT_STATUS_MODE_CHANGE)
5639 continue;
5641 if (asprintf(&ondisk_path, "%s/%s",
5642 worktree->root_path, pe->path) == -1) {
5643 err = got_error_from_errno("asprintf");
5644 goto done;
5646 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5647 free(ondisk_path);
5648 if (err)
5649 goto done;
5652 /* Recursively write new tree objects. */
5653 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5654 commitable_paths, status_cb, status_arg, repo);
5655 if (err)
5656 goto done;
5658 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5659 if (err)
5660 goto done;
5661 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5662 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5663 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5664 got_object_qid_free(pid);
5665 if (logmsg != NULL)
5666 free(logmsg);
5667 if (err)
5668 goto done;
5670 /* Check if a concurrent commit to our branch has occurred. */
5671 head_ref_name = got_worktree_get_head_ref_name(worktree);
5672 if (head_ref_name == NULL) {
5673 err = got_error_from_errno("got_worktree_get_head_ref_name");
5674 goto done;
5676 /* Lock the reference here to prevent concurrent modification. */
5677 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5678 if (err)
5679 goto done;
5680 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5681 if (err)
5682 goto done;
5683 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5684 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5685 goto done;
5687 /* Update branch head in repository. */
5688 err = got_ref_change_ref(head_ref2, *new_commit_id);
5689 if (err)
5690 goto done;
5691 err = got_ref_write(head_ref2, repo);
5692 if (err)
5693 goto done;
5695 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5696 if (err)
5697 goto done;
5699 err = ref_base_commit(worktree, repo);
5700 if (err)
5701 goto done;
5702 done:
5703 if (head_tree)
5704 got_object_tree_close(head_tree);
5705 if (head_commit)
5706 got_object_commit_close(head_commit);
5707 free(head_commit_id2);
5708 if (head_ref2) {
5709 unlockerr = got_ref_unlock(head_ref2);
5710 if (unlockerr && err == NULL)
5711 err = unlockerr;
5712 got_ref_close(head_ref2);
5714 return err;
5717 static const struct got_error *
5718 check_path_is_commitable(const char *path,
5719 struct got_pathlist_head *commitable_paths)
5721 struct got_pathlist_entry *cpe = NULL;
5722 size_t path_len = strlen(path);
5724 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5725 struct got_commitable *ct = cpe->data;
5726 const char *ct_path = ct->path;
5728 while (ct_path[0] == '/')
5729 ct_path++;
5731 if (strcmp(path, ct_path) == 0 ||
5732 got_path_is_child(ct_path, path, path_len))
5733 break;
5736 if (cpe == NULL)
5737 return got_error_path(path, GOT_ERR_BAD_PATH);
5739 return NULL;
5742 static const struct got_error *
5743 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5745 int *have_staged_files = arg;
5747 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5748 *have_staged_files = 1;
5749 return got_error(GOT_ERR_CANCELLED);
5752 return NULL;
5755 static const struct got_error *
5756 check_non_staged_files(struct got_fileindex *fileindex,
5757 struct got_pathlist_head *paths)
5759 struct got_pathlist_entry *pe;
5760 struct got_fileindex_entry *ie;
5762 TAILQ_FOREACH(pe, paths, entry) {
5763 if (pe->path[0] == '\0')
5764 continue;
5765 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5766 if (ie == NULL)
5767 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5768 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5769 return got_error_path(pe->path,
5770 GOT_ERR_FILE_NOT_STAGED);
5773 return NULL;
5776 const struct got_error *
5777 got_worktree_commit(struct got_object_id **new_commit_id,
5778 struct got_worktree *worktree, struct got_pathlist_head *paths,
5779 const char *author, const char *committer, int allow_bad_symlinks,
5780 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5781 got_worktree_status_cb status_cb, void *status_arg,
5782 struct got_repository *repo)
5784 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5785 struct got_fileindex *fileindex = NULL;
5786 char *fileindex_path = NULL;
5787 struct got_pathlist_head commitable_paths;
5788 struct collect_commitables_arg cc_arg;
5789 struct got_pathlist_entry *pe;
5790 struct got_reference *head_ref = NULL;
5791 struct got_object_id *head_commit_id = NULL;
5792 int have_staged_files = 0;
5794 *new_commit_id = NULL;
5796 TAILQ_INIT(&commitable_paths);
5798 err = lock_worktree(worktree, LOCK_EX);
5799 if (err)
5800 goto done;
5802 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5803 if (err)
5804 goto done;
5806 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5807 if (err)
5808 goto done;
5810 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5811 if (err)
5812 goto done;
5814 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5815 &have_staged_files);
5816 if (err && err->code != GOT_ERR_CANCELLED)
5817 goto done;
5818 if (have_staged_files) {
5819 err = check_non_staged_files(fileindex, paths);
5820 if (err)
5821 goto done;
5824 cc_arg.commitable_paths = &commitable_paths;
5825 cc_arg.worktree = worktree;
5826 cc_arg.fileindex = fileindex;
5827 cc_arg.repo = repo;
5828 cc_arg.have_staged_files = have_staged_files;
5829 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5830 TAILQ_FOREACH(pe, paths, entry) {
5831 err = worktree_status(worktree, pe->path, fileindex, repo,
5832 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5833 if (err)
5834 goto done;
5837 if (TAILQ_EMPTY(&commitable_paths)) {
5838 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5839 goto done;
5842 TAILQ_FOREACH(pe, paths, entry) {
5843 err = check_path_is_commitable(pe->path, &commitable_paths);
5844 if (err)
5845 goto done;
5848 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5849 struct got_commitable *ct = pe->data;
5850 const char *ct_path = ct->in_repo_path;
5852 while (ct_path[0] == '/')
5853 ct_path++;
5854 err = check_out_of_date(ct_path, ct->status,
5855 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5856 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5857 if (err)
5858 goto done;
5862 err = commit_worktree(new_commit_id, &commitable_paths,
5863 head_commit_id, worktree, author, committer,
5864 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5865 if (err)
5866 goto done;
5868 err = update_fileindex_after_commit(worktree, &commitable_paths,
5869 *new_commit_id, fileindex, have_staged_files);
5870 sync_err = sync_fileindex(fileindex, fileindex_path);
5871 if (sync_err && err == NULL)
5872 err = sync_err;
5873 done:
5874 if (fileindex)
5875 got_fileindex_free(fileindex);
5876 free(fileindex_path);
5877 unlockerr = lock_worktree(worktree, LOCK_SH);
5878 if (unlockerr && err == NULL)
5879 err = unlockerr;
5880 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5881 struct got_commitable *ct = pe->data;
5882 free_commitable(ct);
5884 got_pathlist_free(&commitable_paths);
5885 return err;
5888 const char *
5889 got_commitable_get_path(struct got_commitable *ct)
5891 return ct->path;
5894 unsigned int
5895 got_commitable_get_status(struct got_commitable *ct)
5897 return ct->status;
5900 struct check_rebase_ok_arg {
5901 struct got_worktree *worktree;
5902 struct got_repository *repo;
5905 static const struct got_error *
5906 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5908 const struct got_error *err = NULL;
5909 struct check_rebase_ok_arg *a = arg;
5910 unsigned char status;
5911 struct stat sb;
5912 char *ondisk_path;
5914 /* Reject rebase of a work tree with mixed base commits. */
5915 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5916 SHA1_DIGEST_LENGTH))
5917 return got_error(GOT_ERR_MIXED_COMMITS);
5919 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5920 == -1)
5921 return got_error_from_errno("asprintf");
5923 /* Reject rebase of a work tree with modified or staged files. */
5924 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5925 free(ondisk_path);
5926 if (err)
5927 return err;
5929 if (status != GOT_STATUS_NO_CHANGE)
5930 return got_error(GOT_ERR_MODIFIED);
5931 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5932 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5934 return NULL;
5937 const struct got_error *
5938 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5939 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5940 struct got_worktree *worktree, struct got_reference *branch,
5941 struct got_repository *repo)
5943 const struct got_error *err = NULL;
5944 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5945 char *branch_ref_name = NULL;
5946 char *fileindex_path = NULL;
5947 struct check_rebase_ok_arg ok_arg;
5948 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5949 struct got_object_id *wt_branch_tip = NULL;
5951 *new_base_branch_ref = NULL;
5952 *tmp_branch = NULL;
5953 *fileindex = NULL;
5955 err = lock_worktree(worktree, LOCK_EX);
5956 if (err)
5957 return err;
5959 err = open_fileindex(fileindex, &fileindex_path, worktree);
5960 if (err)
5961 goto done;
5963 ok_arg.worktree = worktree;
5964 ok_arg.repo = repo;
5965 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5966 &ok_arg);
5967 if (err)
5968 goto done;
5970 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5971 if (err)
5972 goto done;
5974 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5975 if (err)
5976 goto done;
5978 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5979 if (err)
5980 goto done;
5982 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5983 0);
5984 if (err)
5985 goto done;
5987 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5988 if (err)
5989 goto done;
5990 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5991 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5992 goto done;
5995 err = got_ref_alloc_symref(new_base_branch_ref,
5996 new_base_branch_ref_name, wt_branch);
5997 if (err)
5998 goto done;
5999 err = got_ref_write(*new_base_branch_ref, repo);
6000 if (err)
6001 goto done;
6003 /* TODO Lock original branch's ref while rebasing? */
6005 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6006 if (err)
6007 goto done;
6009 err = got_ref_write(branch_ref, repo);
6010 if (err)
6011 goto done;
6013 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6014 worktree->base_commit_id);
6015 if (err)
6016 goto done;
6017 err = got_ref_write(*tmp_branch, repo);
6018 if (err)
6019 goto done;
6021 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6022 if (err)
6023 goto done;
6024 done:
6025 free(fileindex_path);
6026 free(tmp_branch_name);
6027 free(new_base_branch_ref_name);
6028 free(branch_ref_name);
6029 if (branch_ref)
6030 got_ref_close(branch_ref);
6031 if (wt_branch)
6032 got_ref_close(wt_branch);
6033 free(wt_branch_tip);
6034 if (err) {
6035 if (*new_base_branch_ref) {
6036 got_ref_close(*new_base_branch_ref);
6037 *new_base_branch_ref = NULL;
6039 if (*tmp_branch) {
6040 got_ref_close(*tmp_branch);
6041 *tmp_branch = NULL;
6043 if (*fileindex) {
6044 got_fileindex_free(*fileindex);
6045 *fileindex = NULL;
6047 lock_worktree(worktree, LOCK_SH);
6049 return err;
6052 const struct got_error *
6053 got_worktree_rebase_continue(struct got_object_id **commit_id,
6054 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6055 struct got_reference **branch, struct got_fileindex **fileindex,
6056 struct got_worktree *worktree, struct got_repository *repo)
6058 const struct got_error *err;
6059 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6060 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6061 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6062 char *fileindex_path = NULL;
6063 int have_staged_files = 0;
6065 *commit_id = NULL;
6066 *new_base_branch = NULL;
6067 *tmp_branch = NULL;
6068 *branch = NULL;
6069 *fileindex = NULL;
6071 err = lock_worktree(worktree, LOCK_EX);
6072 if (err)
6073 return err;
6075 err = open_fileindex(fileindex, &fileindex_path, worktree);
6076 if (err)
6077 goto done;
6079 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6080 &have_staged_files);
6081 if (err && err->code != GOT_ERR_CANCELLED)
6082 goto done;
6083 if (have_staged_files) {
6084 err = got_error(GOT_ERR_STAGED_PATHS);
6085 goto done;
6088 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6089 if (err)
6090 goto done;
6092 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6093 if (err)
6094 goto done;
6096 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6097 if (err)
6098 goto done;
6100 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6101 if (err)
6102 goto done;
6104 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6105 if (err)
6106 goto done;
6108 err = got_ref_open(branch, repo,
6109 got_ref_get_symref_target(branch_ref), 0);
6110 if (err)
6111 goto done;
6113 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6114 if (err)
6115 goto done;
6117 err = got_ref_resolve(commit_id, repo, commit_ref);
6118 if (err)
6119 goto done;
6121 err = got_ref_open(new_base_branch, repo,
6122 new_base_branch_ref_name, 0);
6123 if (err)
6124 goto done;
6126 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6127 if (err)
6128 goto done;
6129 done:
6130 free(commit_ref_name);
6131 free(branch_ref_name);
6132 free(fileindex_path);
6133 if (commit_ref)
6134 got_ref_close(commit_ref);
6135 if (branch_ref)
6136 got_ref_close(branch_ref);
6137 if (err) {
6138 free(*commit_id);
6139 *commit_id = NULL;
6140 if (*tmp_branch) {
6141 got_ref_close(*tmp_branch);
6142 *tmp_branch = NULL;
6144 if (*new_base_branch) {
6145 got_ref_close(*new_base_branch);
6146 *new_base_branch = NULL;
6148 if (*branch) {
6149 got_ref_close(*branch);
6150 *branch = NULL;
6152 if (*fileindex) {
6153 got_fileindex_free(*fileindex);
6154 *fileindex = NULL;
6156 lock_worktree(worktree, LOCK_SH);
6158 return err;
6161 const struct got_error *
6162 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6164 const struct got_error *err;
6165 char *tmp_branch_name = NULL;
6167 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6168 if (err)
6169 return err;
6171 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6172 free(tmp_branch_name);
6173 return NULL;
6176 static const struct got_error *
6177 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6178 char **logmsg, void *arg)
6180 *logmsg = arg;
6181 return NULL;
6184 static const struct got_error *
6185 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6186 const char *path, struct got_object_id *blob_id,
6187 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6188 int dirfd, const char *de_name)
6190 return NULL;
6193 struct collect_merged_paths_arg {
6194 got_worktree_checkout_cb progress_cb;
6195 void *progress_arg;
6196 struct got_pathlist_head *merged_paths;
6199 static const struct got_error *
6200 collect_merged_paths(void *arg, unsigned char status, const char *path)
6202 const struct got_error *err;
6203 struct collect_merged_paths_arg *a = arg;
6204 char *p;
6205 struct got_pathlist_entry *new;
6207 err = (*a->progress_cb)(a->progress_arg, status, path);
6208 if (err)
6209 return err;
6211 if (status != GOT_STATUS_MERGE &&
6212 status != GOT_STATUS_ADD &&
6213 status != GOT_STATUS_DELETE &&
6214 status != GOT_STATUS_CONFLICT)
6215 return NULL;
6217 p = strdup(path);
6218 if (p == NULL)
6219 return got_error_from_errno("strdup");
6221 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6222 if (err || new == NULL)
6223 free(p);
6224 return err;
6227 void
6228 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6230 struct got_pathlist_entry *pe;
6232 TAILQ_FOREACH(pe, merged_paths, entry)
6233 free((char *)pe->path);
6235 got_pathlist_free(merged_paths);
6238 static const struct got_error *
6239 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6240 int is_rebase, struct got_repository *repo)
6242 const struct got_error *err;
6243 struct got_reference *commit_ref = NULL;
6245 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6246 if (err) {
6247 if (err->code != GOT_ERR_NOT_REF)
6248 goto done;
6249 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6250 if (err)
6251 goto done;
6252 err = got_ref_write(commit_ref, repo);
6253 if (err)
6254 goto done;
6255 } else if (is_rebase) {
6256 struct got_object_id *stored_id;
6257 int cmp;
6259 err = got_ref_resolve(&stored_id, repo, commit_ref);
6260 if (err)
6261 goto done;
6262 cmp = got_object_id_cmp(commit_id, stored_id);
6263 free(stored_id);
6264 if (cmp != 0) {
6265 err = got_error(GOT_ERR_REBASE_COMMITID);
6266 goto done;
6269 done:
6270 if (commit_ref)
6271 got_ref_close(commit_ref);
6272 return err;
6275 static const struct got_error *
6276 rebase_merge_files(struct got_pathlist_head *merged_paths,
6277 const char *commit_ref_name, struct got_worktree *worktree,
6278 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6279 struct got_object_id *commit_id, struct got_repository *repo,
6280 got_worktree_checkout_cb progress_cb, void *progress_arg,
6281 got_cancel_cb cancel_cb, void *cancel_arg)
6283 const struct got_error *err;
6284 struct got_reference *commit_ref = NULL;
6285 struct collect_merged_paths_arg cmp_arg;
6286 char *fileindex_path;
6288 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6290 err = get_fileindex_path(&fileindex_path, worktree);
6291 if (err)
6292 return err;
6294 cmp_arg.progress_cb = progress_cb;
6295 cmp_arg.progress_arg = progress_arg;
6296 cmp_arg.merged_paths = merged_paths;
6297 err = merge_files(worktree, fileindex, fileindex_path,
6298 parent_commit_id, commit_id, repo, collect_merged_paths,
6299 &cmp_arg, cancel_cb, cancel_arg);
6300 if (commit_ref)
6301 got_ref_close(commit_ref);
6302 return err;
6305 const struct got_error *
6306 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6307 struct got_worktree *worktree, struct got_fileindex *fileindex,
6308 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6309 struct got_repository *repo,
6310 got_worktree_checkout_cb progress_cb, void *progress_arg,
6311 got_cancel_cb cancel_cb, void *cancel_arg)
6313 const struct got_error *err;
6314 char *commit_ref_name;
6316 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6317 if (err)
6318 return err;
6320 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6321 if (err)
6322 goto done;
6324 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6325 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6326 progress_arg, cancel_cb, cancel_arg);
6327 done:
6328 free(commit_ref_name);
6329 return err;
6332 const struct got_error *
6333 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6334 struct got_worktree *worktree, struct got_fileindex *fileindex,
6335 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6336 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 char *commit_ref_name;
6343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6344 if (err)
6345 return err;
6347 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6348 if (err)
6349 goto done;
6351 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6352 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6353 progress_arg, cancel_cb, cancel_arg);
6354 done:
6355 free(commit_ref_name);
6356 return err;
6359 static const struct got_error *
6360 rebase_commit(struct got_object_id **new_commit_id,
6361 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6362 struct got_worktree *worktree, struct got_fileindex *fileindex,
6363 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6364 const char *new_logmsg, struct got_repository *repo)
6366 const struct got_error *err, *sync_err;
6367 struct got_pathlist_head commitable_paths;
6368 struct collect_commitables_arg cc_arg;
6369 char *fileindex_path = NULL;
6370 struct got_reference *head_ref = NULL;
6371 struct got_object_id *head_commit_id = NULL;
6372 char *logmsg = NULL;
6374 TAILQ_INIT(&commitable_paths);
6375 *new_commit_id = NULL;
6377 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6379 err = get_fileindex_path(&fileindex_path, worktree);
6380 if (err)
6381 return err;
6383 cc_arg.commitable_paths = &commitable_paths;
6384 cc_arg.worktree = worktree;
6385 cc_arg.repo = repo;
6386 cc_arg.have_staged_files = 0;
6388 * If possible get the status of individual files directly to
6389 * avoid crawling the entire work tree once per rebased commit.
6391 * Ideally, merged_paths would contain a list of commitables
6392 * we could use so we could skip worktree_status() entirely.
6393 * However, we would then need carefully keep track of cumulative
6394 * effects of operations such as file additions and deletions
6395 * in 'got histedit -f' (folding multiple commits into one),
6396 * and this extra complexity is not really worth it.
6398 if (merged_paths) {
6399 struct got_pathlist_entry *pe;
6400 TAILQ_FOREACH(pe, merged_paths, entry) {
6401 err = worktree_status(worktree, pe->path, fileindex,
6402 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6403 0);
6404 if (err)
6405 goto done;
6407 } else {
6408 err = worktree_status(worktree, "", fileindex, repo,
6409 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6410 if (err)
6411 goto done;
6414 if (TAILQ_EMPTY(&commitable_paths)) {
6415 /* No-op change; commit will be elided. */
6416 err = got_ref_delete(commit_ref, repo);
6417 if (err)
6418 goto done;
6419 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6420 goto done;
6423 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6424 if (err)
6425 goto done;
6427 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6428 if (err)
6429 goto done;
6431 if (new_logmsg) {
6432 logmsg = strdup(new_logmsg);
6433 if (logmsg == NULL) {
6434 err = got_error_from_errno("strdup");
6435 goto done;
6437 } else {
6438 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6439 if (err)
6440 goto done;
6443 /* NB: commit_worktree will call free(logmsg) */
6444 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6445 worktree, got_object_commit_get_author(orig_commit),
6446 got_object_commit_get_committer(orig_commit),
6447 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6448 if (err)
6449 goto done;
6451 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6452 if (err)
6453 goto done;
6455 err = got_ref_delete(commit_ref, repo);
6456 if (err)
6457 goto done;
6459 err = update_fileindex_after_commit(worktree, &commitable_paths,
6460 *new_commit_id, fileindex, 0);
6461 sync_err = sync_fileindex(fileindex, fileindex_path);
6462 if (sync_err && err == NULL)
6463 err = sync_err;
6464 done:
6465 free(fileindex_path);
6466 free(head_commit_id);
6467 if (head_ref)
6468 got_ref_close(head_ref);
6469 if (err) {
6470 free(*new_commit_id);
6471 *new_commit_id = NULL;
6473 return err;
6476 const struct got_error *
6477 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6478 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6479 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6480 struct got_commit_object *orig_commit,
6481 struct got_object_id *orig_commit_id, struct got_repository *repo)
6483 const struct got_error *err;
6484 char *commit_ref_name;
6485 struct got_reference *commit_ref = NULL;
6486 struct got_object_id *commit_id = NULL;
6488 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6489 if (err)
6490 return err;
6492 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6493 if (err)
6494 goto done;
6495 err = got_ref_resolve(&commit_id, repo, commit_ref);
6496 if (err)
6497 goto done;
6498 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6499 err = got_error(GOT_ERR_REBASE_COMMITID);
6500 goto done;
6503 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6504 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6505 done:
6506 if (commit_ref)
6507 got_ref_close(commit_ref);
6508 free(commit_ref_name);
6509 free(commit_id);
6510 return err;
6513 const struct got_error *
6514 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6515 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6516 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6517 struct got_commit_object *orig_commit,
6518 struct got_object_id *orig_commit_id, const char *new_logmsg,
6519 struct got_repository *repo)
6521 const struct got_error *err;
6522 char *commit_ref_name;
6523 struct got_reference *commit_ref = NULL;
6525 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6526 if (err)
6527 return err;
6529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6530 if (err)
6531 goto done;
6533 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6534 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6535 done:
6536 if (commit_ref)
6537 got_ref_close(commit_ref);
6538 free(commit_ref_name);
6539 return err;
6542 const struct got_error *
6543 got_worktree_rebase_postpone(struct got_worktree *worktree,
6544 struct got_fileindex *fileindex)
6546 if (fileindex)
6547 got_fileindex_free(fileindex);
6548 return lock_worktree(worktree, LOCK_SH);
6551 static const struct got_error *
6552 delete_ref(const char *name, struct got_repository *repo)
6554 const struct got_error *err;
6555 struct got_reference *ref;
6557 err = got_ref_open(&ref, repo, name, 0);
6558 if (err) {
6559 if (err->code == GOT_ERR_NOT_REF)
6560 return NULL;
6561 return err;
6564 err = got_ref_delete(ref, repo);
6565 got_ref_close(ref);
6566 return err;
6569 static const struct got_error *
6570 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6572 const struct got_error *err;
6573 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6574 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6576 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6577 if (err)
6578 goto done;
6579 err = delete_ref(tmp_branch_name, repo);
6580 if (err)
6581 goto done;
6583 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6584 if (err)
6585 goto done;
6586 err = delete_ref(new_base_branch_ref_name, repo);
6587 if (err)
6588 goto done;
6590 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6591 if (err)
6592 goto done;
6593 err = delete_ref(branch_ref_name, repo);
6594 if (err)
6595 goto done;
6597 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6598 if (err)
6599 goto done;
6600 err = delete_ref(commit_ref_name, repo);
6601 if (err)
6602 goto done;
6604 done:
6605 free(tmp_branch_name);
6606 free(new_base_branch_ref_name);
6607 free(branch_ref_name);
6608 free(commit_ref_name);
6609 return err;
6612 const struct got_error *
6613 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6614 struct got_object_id *new_commit_id, struct got_repository *repo)
6616 const struct got_error *err;
6617 struct got_reference *ref = NULL;
6618 struct got_object_id *old_commit_id = NULL;
6619 const char *branch_name = NULL;
6620 char *new_id_str = NULL;
6621 char *refname = NULL;
6623 branch_name = got_ref_get_name(branch);
6624 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6625 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6626 branch_name += 11;
6628 err = got_object_id_str(&new_id_str, new_commit_id);
6629 if (err)
6630 return err;
6632 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6633 new_id_str) == -1) {
6634 err = got_error_from_errno("asprintf");
6635 goto done;
6638 err = got_ref_resolve(&old_commit_id, repo, branch);
6639 if (err)
6640 goto done;
6642 err = got_ref_alloc(&ref, refname, old_commit_id);
6643 if (err)
6644 goto done;
6646 err = got_ref_write(ref, repo);
6647 done:
6648 free(new_id_str);
6649 free(refname);
6650 free(old_commit_id);
6651 if (ref)
6652 got_ref_close(ref);
6653 return err;
6656 const struct got_error *
6657 got_worktree_rebase_complete(struct got_worktree *worktree,
6658 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6659 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6660 struct got_repository *repo, int create_backup)
6662 const struct got_error *err, *unlockerr, *sync_err;
6663 struct got_object_id *new_head_commit_id = NULL;
6664 char *fileindex_path = NULL;
6666 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6667 if (err)
6668 return err;
6670 if (create_backup) {
6671 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6672 rebased_branch, new_head_commit_id, repo);
6673 if (err)
6674 goto done;
6677 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6678 if (err)
6679 goto done;
6681 err = got_ref_write(rebased_branch, repo);
6682 if (err)
6683 goto done;
6685 err = got_worktree_set_head_ref(worktree, rebased_branch);
6686 if (err)
6687 goto done;
6689 err = delete_rebase_refs(worktree, repo);
6690 if (err)
6691 goto done;
6693 err = get_fileindex_path(&fileindex_path, worktree);
6694 if (err)
6695 goto done;
6696 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6697 sync_err = sync_fileindex(fileindex, fileindex_path);
6698 if (sync_err && err == NULL)
6699 err = sync_err;
6700 done:
6701 got_fileindex_free(fileindex);
6702 free(fileindex_path);
6703 free(new_head_commit_id);
6704 unlockerr = lock_worktree(worktree, LOCK_SH);
6705 if (unlockerr && err == NULL)
6706 err = unlockerr;
6707 return err;
6710 const struct got_error *
6711 got_worktree_rebase_abort(struct got_worktree *worktree,
6712 struct got_fileindex *fileindex, struct got_repository *repo,
6713 struct got_reference *new_base_branch,
6714 got_worktree_checkout_cb progress_cb, void *progress_arg)
6716 const struct got_error *err, *unlockerr, *sync_err;
6717 struct got_reference *resolved = NULL;
6718 struct got_object_id *commit_id = NULL;
6719 char *fileindex_path = NULL;
6720 struct revert_file_args rfa;
6721 struct got_object_id *tree_id = NULL;
6723 err = lock_worktree(worktree, LOCK_EX);
6724 if (err)
6725 return err;
6727 err = got_ref_open(&resolved, repo,
6728 got_ref_get_symref_target(new_base_branch), 0);
6729 if (err)
6730 goto done;
6732 err = got_worktree_set_head_ref(worktree, resolved);
6733 if (err)
6734 goto done;
6737 * XXX commits to the base branch could have happened while
6738 * we were busy rebasing; should we store the original commit ID
6739 * when rebase begins and read it back here?
6741 err = got_ref_resolve(&commit_id, repo, resolved);
6742 if (err)
6743 goto done;
6745 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6746 if (err)
6747 goto done;
6749 err = got_object_id_by_path(&tree_id, repo,
6750 worktree->base_commit_id, worktree->path_prefix);
6751 if (err)
6752 goto done;
6754 err = delete_rebase_refs(worktree, repo);
6755 if (err)
6756 goto done;
6758 err = get_fileindex_path(&fileindex_path, worktree);
6759 if (err)
6760 goto done;
6762 rfa.worktree = worktree;
6763 rfa.fileindex = fileindex;
6764 rfa.progress_cb = progress_cb;
6765 rfa.progress_arg = progress_arg;
6766 rfa.patch_cb = NULL;
6767 rfa.patch_arg = NULL;
6768 rfa.repo = repo;
6769 err = worktree_status(worktree, "", fileindex, repo,
6770 revert_file, &rfa, NULL, NULL, 0, 0);
6771 if (err)
6772 goto sync;
6774 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6775 repo, progress_cb, progress_arg, NULL, NULL);
6776 sync:
6777 sync_err = sync_fileindex(fileindex, fileindex_path);
6778 if (sync_err && err == NULL)
6779 err = sync_err;
6780 done:
6781 got_ref_close(resolved);
6782 free(tree_id);
6783 free(commit_id);
6784 if (fileindex)
6785 got_fileindex_free(fileindex);
6786 free(fileindex_path);
6788 unlockerr = lock_worktree(worktree, LOCK_SH);
6789 if (unlockerr && err == NULL)
6790 err = unlockerr;
6791 return err;
6794 const struct got_error *
6795 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6796 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6797 struct got_fileindex **fileindex, struct got_worktree *worktree,
6798 struct got_repository *repo)
6800 const struct got_error *err = NULL;
6801 char *tmp_branch_name = NULL;
6802 char *branch_ref_name = NULL;
6803 char *base_commit_ref_name = NULL;
6804 char *fileindex_path = NULL;
6805 struct check_rebase_ok_arg ok_arg;
6806 struct got_reference *wt_branch = NULL;
6807 struct got_reference *base_commit_ref = NULL;
6809 *tmp_branch = NULL;
6810 *branch_ref = NULL;
6811 *base_commit_id = NULL;
6812 *fileindex = NULL;
6814 err = lock_worktree(worktree, LOCK_EX);
6815 if (err)
6816 return err;
6818 err = open_fileindex(fileindex, &fileindex_path, worktree);
6819 if (err)
6820 goto done;
6822 ok_arg.worktree = worktree;
6823 ok_arg.repo = repo;
6824 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6825 &ok_arg);
6826 if (err)
6827 goto done;
6829 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6830 if (err)
6831 goto done;
6833 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6834 if (err)
6835 goto done;
6837 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6838 worktree);
6839 if (err)
6840 goto done;
6842 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6843 0);
6844 if (err)
6845 goto done;
6847 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6848 if (err)
6849 goto done;
6851 err = got_ref_write(*branch_ref, repo);
6852 if (err)
6853 goto done;
6855 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6856 worktree->base_commit_id);
6857 if (err)
6858 goto done;
6859 err = got_ref_write(base_commit_ref, repo);
6860 if (err)
6861 goto done;
6862 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6863 if (*base_commit_id == NULL) {
6864 err = got_error_from_errno("got_object_id_dup");
6865 goto done;
6868 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6869 worktree->base_commit_id);
6870 if (err)
6871 goto done;
6872 err = got_ref_write(*tmp_branch, repo);
6873 if (err)
6874 goto done;
6876 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6877 if (err)
6878 goto done;
6879 done:
6880 free(fileindex_path);
6881 free(tmp_branch_name);
6882 free(branch_ref_name);
6883 free(base_commit_ref_name);
6884 if (wt_branch)
6885 got_ref_close(wt_branch);
6886 if (err) {
6887 if (*branch_ref) {
6888 got_ref_close(*branch_ref);
6889 *branch_ref = NULL;
6891 if (*tmp_branch) {
6892 got_ref_close(*tmp_branch);
6893 *tmp_branch = NULL;
6895 free(*base_commit_id);
6896 if (*fileindex) {
6897 got_fileindex_free(*fileindex);
6898 *fileindex = NULL;
6900 lock_worktree(worktree, LOCK_SH);
6902 return err;
6905 const struct got_error *
6906 got_worktree_histedit_postpone(struct got_worktree *worktree,
6907 struct got_fileindex *fileindex)
6909 if (fileindex)
6910 got_fileindex_free(fileindex);
6911 return lock_worktree(worktree, LOCK_SH);
6914 const struct got_error *
6915 got_worktree_histedit_in_progress(int *in_progress,
6916 struct got_worktree *worktree)
6918 const struct got_error *err;
6919 char *tmp_branch_name = NULL;
6921 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6922 if (err)
6923 return err;
6925 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6926 free(tmp_branch_name);
6927 return NULL;
6930 const struct got_error *
6931 got_worktree_histedit_continue(struct got_object_id **commit_id,
6932 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6933 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6934 struct got_worktree *worktree, struct got_repository *repo)
6936 const struct got_error *err;
6937 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6938 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6939 struct got_reference *commit_ref = NULL;
6940 struct got_reference *base_commit_ref = NULL;
6941 char *fileindex_path = NULL;
6942 int have_staged_files = 0;
6944 *commit_id = NULL;
6945 *tmp_branch = NULL;
6946 *base_commit_id = NULL;
6947 *fileindex = NULL;
6949 err = lock_worktree(worktree, LOCK_EX);
6950 if (err)
6951 return err;
6953 err = open_fileindex(fileindex, &fileindex_path, worktree);
6954 if (err)
6955 goto done;
6957 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6958 &have_staged_files);
6959 if (err && err->code != GOT_ERR_CANCELLED)
6960 goto done;
6961 if (have_staged_files) {
6962 err = got_error(GOT_ERR_STAGED_PATHS);
6963 goto done;
6966 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6967 if (err)
6968 goto done;
6970 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6971 if (err)
6972 goto done;
6974 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6975 if (err)
6976 goto done;
6978 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6979 worktree);
6980 if (err)
6981 goto done;
6983 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6984 if (err)
6985 goto done;
6987 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6988 if (err)
6989 goto done;
6990 err = got_ref_resolve(commit_id, repo, commit_ref);
6991 if (err)
6992 goto done;
6994 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6995 if (err)
6996 goto done;
6997 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6998 if (err)
6999 goto done;
7001 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7002 if (err)
7003 goto done;
7004 done:
7005 free(commit_ref_name);
7006 free(branch_ref_name);
7007 free(fileindex_path);
7008 if (commit_ref)
7009 got_ref_close(commit_ref);
7010 if (base_commit_ref)
7011 got_ref_close(base_commit_ref);
7012 if (err) {
7013 free(*commit_id);
7014 *commit_id = NULL;
7015 free(*base_commit_id);
7016 *base_commit_id = NULL;
7017 if (*tmp_branch) {
7018 got_ref_close(*tmp_branch);
7019 *tmp_branch = NULL;
7021 if (*fileindex) {
7022 got_fileindex_free(*fileindex);
7023 *fileindex = NULL;
7025 lock_worktree(worktree, LOCK_EX);
7027 return err;
7030 static const struct got_error *
7031 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7033 const struct got_error *err;
7034 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7035 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7037 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7038 if (err)
7039 goto done;
7040 err = delete_ref(tmp_branch_name, repo);
7041 if (err)
7042 goto done;
7044 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7045 worktree);
7046 if (err)
7047 goto done;
7048 err = delete_ref(base_commit_ref_name, repo);
7049 if (err)
7050 goto done;
7052 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7053 if (err)
7054 goto done;
7055 err = delete_ref(branch_ref_name, repo);
7056 if (err)
7057 goto done;
7059 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7060 if (err)
7061 goto done;
7062 err = delete_ref(commit_ref_name, repo);
7063 if (err)
7064 goto done;
7065 done:
7066 free(tmp_branch_name);
7067 free(base_commit_ref_name);
7068 free(branch_ref_name);
7069 free(commit_ref_name);
7070 return err;
7073 const struct got_error *
7074 got_worktree_histedit_abort(struct got_worktree *worktree,
7075 struct got_fileindex *fileindex, struct got_repository *repo,
7076 struct got_reference *branch, struct got_object_id *base_commit_id,
7077 got_worktree_checkout_cb progress_cb, void *progress_arg)
7079 const struct got_error *err, *unlockerr, *sync_err;
7080 struct got_reference *resolved = NULL;
7081 char *fileindex_path = NULL;
7082 struct got_object_id *tree_id = NULL;
7083 struct revert_file_args rfa;
7085 err = lock_worktree(worktree, LOCK_EX);
7086 if (err)
7087 return err;
7089 err = got_ref_open(&resolved, repo,
7090 got_ref_get_symref_target(branch), 0);
7091 if (err)
7092 goto done;
7094 err = got_worktree_set_head_ref(worktree, resolved);
7095 if (err)
7096 goto done;
7098 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7099 if (err)
7100 goto done;
7102 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7103 worktree->path_prefix);
7104 if (err)
7105 goto done;
7107 err = delete_histedit_refs(worktree, repo);
7108 if (err)
7109 goto done;
7111 err = get_fileindex_path(&fileindex_path, worktree);
7112 if (err)
7113 goto done;
7115 rfa.worktree = worktree;
7116 rfa.fileindex = fileindex;
7117 rfa.progress_cb = progress_cb;
7118 rfa.progress_arg = progress_arg;
7119 rfa.patch_cb = NULL;
7120 rfa.patch_arg = NULL;
7121 rfa.repo = repo;
7122 err = worktree_status(worktree, "", fileindex, repo,
7123 revert_file, &rfa, NULL, NULL, 0, 0);
7124 if (err)
7125 goto sync;
7127 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7128 repo, progress_cb, progress_arg, NULL, NULL);
7129 sync:
7130 sync_err = sync_fileindex(fileindex, fileindex_path);
7131 if (sync_err && err == NULL)
7132 err = sync_err;
7133 done:
7134 got_ref_close(resolved);
7135 free(tree_id);
7136 free(fileindex_path);
7138 unlockerr = lock_worktree(worktree, LOCK_SH);
7139 if (unlockerr && err == NULL)
7140 err = unlockerr;
7141 return err;
7144 const struct got_error *
7145 got_worktree_histedit_complete(struct got_worktree *worktree,
7146 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7147 struct got_reference *edited_branch, struct got_repository *repo)
7149 const struct got_error *err, *unlockerr, *sync_err;
7150 struct got_object_id *new_head_commit_id = NULL;
7151 struct got_reference *resolved = NULL;
7152 char *fileindex_path = NULL;
7154 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7155 if (err)
7156 return err;
7158 err = got_ref_open(&resolved, repo,
7159 got_ref_get_symref_target(edited_branch), 0);
7160 if (err)
7161 goto done;
7163 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7164 resolved, new_head_commit_id, repo);
7165 if (err)
7166 goto done;
7168 err = got_ref_change_ref(resolved, new_head_commit_id);
7169 if (err)
7170 goto done;
7172 err = got_ref_write(resolved, repo);
7173 if (err)
7174 goto done;
7176 err = got_worktree_set_head_ref(worktree, resolved);
7177 if (err)
7178 goto done;
7180 err = delete_histedit_refs(worktree, repo);
7181 if (err)
7182 goto done;
7184 err = get_fileindex_path(&fileindex_path, worktree);
7185 if (err)
7186 goto done;
7187 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7188 sync_err = sync_fileindex(fileindex, fileindex_path);
7189 if (sync_err && err == NULL)
7190 err = sync_err;
7191 done:
7192 got_fileindex_free(fileindex);
7193 free(fileindex_path);
7194 free(new_head_commit_id);
7195 unlockerr = lock_worktree(worktree, LOCK_SH);
7196 if (unlockerr && err == NULL)
7197 err = unlockerr;
7198 return err;
7201 const struct got_error *
7202 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7203 struct got_object_id *commit_id, struct got_repository *repo)
7205 const struct got_error *err;
7206 char *commit_ref_name;
7208 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7209 if (err)
7210 return err;
7212 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7213 if (err)
7214 goto done;
7216 err = delete_ref(commit_ref_name, repo);
7217 done:
7218 free(commit_ref_name);
7219 return err;
7222 const struct got_error *
7223 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7224 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7225 struct got_worktree *worktree, const char *refname,
7226 struct got_repository *repo)
7228 const struct got_error *err = NULL;
7229 char *fileindex_path = NULL;
7230 struct check_rebase_ok_arg ok_arg;
7232 *fileindex = NULL;
7233 *branch_ref = NULL;
7234 *base_branch_ref = NULL;
7236 err = lock_worktree(worktree, LOCK_EX);
7237 if (err)
7238 return err;
7240 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7241 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7242 "cannot integrate a branch into itself; "
7243 "update -b or different branch name required");
7244 goto done;
7247 err = open_fileindex(fileindex, &fileindex_path, worktree);
7248 if (err)
7249 goto done;
7251 /* Preconditions are the same as for rebase. */
7252 ok_arg.worktree = worktree;
7253 ok_arg.repo = repo;
7254 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7255 &ok_arg);
7256 if (err)
7257 goto done;
7259 err = got_ref_open(branch_ref, repo, refname, 1);
7260 if (err)
7261 goto done;
7263 err = got_ref_open(base_branch_ref, repo,
7264 got_worktree_get_head_ref_name(worktree), 1);
7265 done:
7266 if (err) {
7267 if (*branch_ref) {
7268 got_ref_close(*branch_ref);
7269 *branch_ref = NULL;
7271 if (*base_branch_ref) {
7272 got_ref_close(*base_branch_ref);
7273 *base_branch_ref = NULL;
7275 if (*fileindex) {
7276 got_fileindex_free(*fileindex);
7277 *fileindex = NULL;
7279 lock_worktree(worktree, LOCK_SH);
7281 return err;
7284 const struct got_error *
7285 got_worktree_integrate_continue(struct got_worktree *worktree,
7286 struct got_fileindex *fileindex, struct got_repository *repo,
7287 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7288 got_worktree_checkout_cb progress_cb, void *progress_arg,
7289 got_cancel_cb cancel_cb, void *cancel_arg)
7291 const struct got_error *err = NULL, *sync_err, *unlockerr;
7292 char *fileindex_path = NULL;
7293 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7295 err = get_fileindex_path(&fileindex_path, worktree);
7296 if (err)
7297 goto done;
7299 err = got_ref_resolve(&commit_id, repo, branch_ref);
7300 if (err)
7301 goto done;
7303 err = got_object_id_by_path(&tree_id, repo, commit_id,
7304 worktree->path_prefix);
7305 if (err)
7306 goto done;
7308 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7309 if (err)
7310 goto done;
7312 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7313 progress_cb, progress_arg, cancel_cb, cancel_arg);
7314 if (err)
7315 goto sync;
7317 err = got_ref_change_ref(base_branch_ref, commit_id);
7318 if (err)
7319 goto sync;
7321 err = got_ref_write(base_branch_ref, repo);
7322 if (err)
7323 goto sync;
7325 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7326 sync:
7327 sync_err = sync_fileindex(fileindex, fileindex_path);
7328 if (sync_err && err == NULL)
7329 err = sync_err;
7331 done:
7332 unlockerr = got_ref_unlock(branch_ref);
7333 if (unlockerr && err == NULL)
7334 err = unlockerr;
7335 got_ref_close(branch_ref);
7337 unlockerr = got_ref_unlock(base_branch_ref);
7338 if (unlockerr && err == NULL)
7339 err = unlockerr;
7340 got_ref_close(base_branch_ref);
7342 got_fileindex_free(fileindex);
7343 free(fileindex_path);
7344 free(tree_id);
7346 unlockerr = lock_worktree(worktree, LOCK_SH);
7347 if (unlockerr && err == NULL)
7348 err = unlockerr;
7349 return err;
7352 const struct got_error *
7353 got_worktree_integrate_abort(struct got_worktree *worktree,
7354 struct got_fileindex *fileindex, struct got_repository *repo,
7355 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7357 const struct got_error *err = NULL, *unlockerr = NULL;
7359 got_fileindex_free(fileindex);
7361 err = lock_worktree(worktree, LOCK_SH);
7363 unlockerr = got_ref_unlock(branch_ref);
7364 if (unlockerr && err == NULL)
7365 err = unlockerr;
7366 got_ref_close(branch_ref);
7368 unlockerr = got_ref_unlock(base_branch_ref);
7369 if (unlockerr && err == NULL)
7370 err = unlockerr;
7371 got_ref_close(base_branch_ref);
7373 return err;
7376 struct check_stage_ok_arg {
7377 struct got_object_id *head_commit_id;
7378 struct got_worktree *worktree;
7379 struct got_fileindex *fileindex;
7380 struct got_repository *repo;
7381 int have_changes;
7384 const struct got_error *
7385 check_stage_ok(void *arg, unsigned char status,
7386 unsigned char staged_status, const char *relpath,
7387 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7388 struct got_object_id *commit_id, int dirfd, const char *de_name)
7390 struct check_stage_ok_arg *a = arg;
7391 const struct got_error *err = NULL;
7392 struct got_fileindex_entry *ie;
7393 struct got_object_id base_commit_id;
7394 struct got_object_id *base_commit_idp = NULL;
7395 char *in_repo_path = NULL, *p;
7397 if (status == GOT_STATUS_UNVERSIONED ||
7398 status == GOT_STATUS_NO_CHANGE)
7399 return NULL;
7400 if (status == GOT_STATUS_NONEXISTENT)
7401 return got_error_set_errno(ENOENT, relpath);
7403 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7404 if (ie == NULL)
7405 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7407 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7408 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7409 relpath) == -1)
7410 return got_error_from_errno("asprintf");
7412 if (got_fileindex_entry_has_commit(ie)) {
7413 memcpy(base_commit_id.sha1, ie->commit_sha1,
7414 SHA1_DIGEST_LENGTH);
7415 base_commit_idp = &base_commit_id;
7418 if (status == GOT_STATUS_CONFLICT) {
7419 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7420 goto done;
7421 } else if (status != GOT_STATUS_ADD &&
7422 status != GOT_STATUS_MODIFY &&
7423 status != GOT_STATUS_DELETE) {
7424 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7425 goto done;
7428 a->have_changes = 1;
7430 p = in_repo_path;
7431 while (p[0] == '/')
7432 p++;
7433 err = check_out_of_date(p, status, staged_status,
7434 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7435 GOT_ERR_STAGE_OUT_OF_DATE);
7436 done:
7437 free(in_repo_path);
7438 return err;
7441 struct stage_path_arg {
7442 struct got_worktree *worktree;
7443 struct got_fileindex *fileindex;
7444 struct got_repository *repo;
7445 got_worktree_status_cb status_cb;
7446 void *status_arg;
7447 got_worktree_patch_cb patch_cb;
7448 void *patch_arg;
7449 int staged_something;
7450 int allow_bad_symlinks;
7453 static const struct got_error *
7454 stage_path(void *arg, unsigned char status,
7455 unsigned char staged_status, const char *relpath,
7456 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7457 struct got_object_id *commit_id, int dirfd, const char *de_name)
7459 struct stage_path_arg *a = arg;
7460 const struct got_error *err = NULL;
7461 struct got_fileindex_entry *ie;
7462 char *ondisk_path = NULL, *path_content = NULL;
7463 uint32_t stage;
7464 struct got_object_id *new_staged_blob_id = NULL;
7465 struct stat sb;
7467 if (status == GOT_STATUS_UNVERSIONED)
7468 return NULL;
7470 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7471 if (ie == NULL)
7472 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7474 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7475 relpath)== -1)
7476 return got_error_from_errno("asprintf");
7478 switch (status) {
7479 case GOT_STATUS_ADD:
7480 case GOT_STATUS_MODIFY:
7481 /* XXX could sb.st_mode be passed in by our caller? */
7482 if (lstat(ondisk_path, &sb) == -1) {
7483 err = got_error_from_errno2("lstat", ondisk_path);
7484 break;
7486 if (a->patch_cb) {
7487 if (status == GOT_STATUS_ADD) {
7488 int choice = GOT_PATCH_CHOICE_NONE;
7489 err = (*a->patch_cb)(&choice, a->patch_arg,
7490 status, ie->path, NULL, 1, 1);
7491 if (err)
7492 break;
7493 if (choice != GOT_PATCH_CHOICE_YES)
7494 break;
7495 } else {
7496 err = create_patched_content(&path_content, 0,
7497 staged_blob_id ? staged_blob_id : blob_id,
7498 ondisk_path, dirfd, de_name, ie->path,
7499 a->repo, a->patch_cb, a->patch_arg);
7500 if (err || path_content == NULL)
7501 break;
7504 err = got_object_blob_create(&new_staged_blob_id,
7505 path_content ? path_content : ondisk_path, a->repo);
7506 if (err)
7507 break;
7508 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7509 SHA1_DIGEST_LENGTH);
7510 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7511 stage = GOT_FILEIDX_STAGE_ADD;
7512 else
7513 stage = GOT_FILEIDX_STAGE_MODIFY;
7514 got_fileindex_entry_stage_set(ie, stage);
7515 if (S_ISLNK(sb.st_mode)) {
7516 int is_bad_symlink = 0;
7517 if (!a->allow_bad_symlinks) {
7518 char target_path[PATH_MAX];
7519 ssize_t target_len;
7520 target_len = readlink(ondisk_path, target_path,
7521 sizeof(target_path));
7522 if (target_len == -1) {
7523 err = got_error_from_errno2("readlink",
7524 ondisk_path);
7525 break;
7527 err = is_bad_symlink_target(&is_bad_symlink,
7528 target_path, target_len, ondisk_path,
7529 a->worktree->root_path);
7530 if (err)
7531 break;
7532 if (is_bad_symlink) {
7533 err = got_error_path(ondisk_path,
7534 GOT_ERR_BAD_SYMLINK);
7535 break;
7538 if (is_bad_symlink)
7539 got_fileindex_entry_staged_filetype_set(ie,
7540 GOT_FILEIDX_MODE_BAD_SYMLINK);
7541 else
7542 got_fileindex_entry_staged_filetype_set(ie,
7543 GOT_FILEIDX_MODE_SYMLINK);
7544 } else {
7545 got_fileindex_entry_staged_filetype_set(ie,
7546 GOT_FILEIDX_MODE_REGULAR_FILE);
7548 a->staged_something = 1;
7549 if (a->status_cb == NULL)
7550 break;
7551 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7552 get_staged_status(ie), relpath, blob_id,
7553 new_staged_blob_id, NULL, dirfd, de_name);
7554 break;
7555 case GOT_STATUS_DELETE:
7556 if (staged_status == GOT_STATUS_DELETE)
7557 break;
7558 if (a->patch_cb) {
7559 int choice = GOT_PATCH_CHOICE_NONE;
7560 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7561 ie->path, NULL, 1, 1);
7562 if (err)
7563 break;
7564 if (choice == GOT_PATCH_CHOICE_NO)
7565 break;
7566 if (choice != GOT_PATCH_CHOICE_YES) {
7567 err = got_error(GOT_ERR_PATCH_CHOICE);
7568 break;
7571 stage = GOT_FILEIDX_STAGE_DELETE;
7572 got_fileindex_entry_stage_set(ie, stage);
7573 a->staged_something = 1;
7574 if (a->status_cb == NULL)
7575 break;
7576 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7577 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7578 de_name);
7579 break;
7580 case GOT_STATUS_NO_CHANGE:
7581 break;
7582 case GOT_STATUS_CONFLICT:
7583 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7584 break;
7585 case GOT_STATUS_NONEXISTENT:
7586 err = got_error_set_errno(ENOENT, relpath);
7587 break;
7588 default:
7589 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7590 break;
7593 if (path_content && unlink(path_content) == -1 && err == NULL)
7594 err = got_error_from_errno2("unlink", path_content);
7595 free(path_content);
7596 free(ondisk_path);
7597 free(new_staged_blob_id);
7598 return err;
7601 const struct got_error *
7602 got_worktree_stage(struct got_worktree *worktree,
7603 struct got_pathlist_head *paths,
7604 got_worktree_status_cb status_cb, void *status_arg,
7605 got_worktree_patch_cb patch_cb, void *patch_arg,
7606 int allow_bad_symlinks, struct got_repository *repo)
7608 const struct got_error *err = NULL, *sync_err, *unlockerr;
7609 struct got_pathlist_entry *pe;
7610 struct got_fileindex *fileindex = NULL;
7611 char *fileindex_path = NULL;
7612 struct got_reference *head_ref = NULL;
7613 struct got_object_id *head_commit_id = NULL;
7614 struct check_stage_ok_arg oka;
7615 struct stage_path_arg spa;
7617 err = lock_worktree(worktree, LOCK_EX);
7618 if (err)
7619 return err;
7621 err = got_ref_open(&head_ref, repo,
7622 got_worktree_get_head_ref_name(worktree), 0);
7623 if (err)
7624 goto done;
7625 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7626 if (err)
7627 goto done;
7628 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7629 if (err)
7630 goto done;
7632 /* Check pre-conditions before staging anything. */
7633 oka.head_commit_id = head_commit_id;
7634 oka.worktree = worktree;
7635 oka.fileindex = fileindex;
7636 oka.repo = repo;
7637 oka.have_changes = 0;
7638 TAILQ_FOREACH(pe, paths, entry) {
7639 err = worktree_status(worktree, pe->path, fileindex, repo,
7640 check_stage_ok, &oka, NULL, NULL, 0, 0);
7641 if (err)
7642 goto done;
7644 if (!oka.have_changes) {
7645 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7646 goto done;
7649 spa.worktree = worktree;
7650 spa.fileindex = fileindex;
7651 spa.repo = repo;
7652 spa.patch_cb = patch_cb;
7653 spa.patch_arg = patch_arg;
7654 spa.status_cb = status_cb;
7655 spa.status_arg = status_arg;
7656 spa.staged_something = 0;
7657 spa.allow_bad_symlinks = allow_bad_symlinks;
7658 TAILQ_FOREACH(pe, paths, entry) {
7659 err = worktree_status(worktree, pe->path, fileindex, repo,
7660 stage_path, &spa, NULL, NULL, 0, 0);
7661 if (err)
7662 goto done;
7664 if (!spa.staged_something) {
7665 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7666 goto done;
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7671 err = sync_err;
7672 done:
7673 if (head_ref)
7674 got_ref_close(head_ref);
7675 free(head_commit_id);
7676 free(fileindex_path);
7677 if (fileindex)
7678 got_fileindex_free(fileindex);
7679 unlockerr = lock_worktree(worktree, LOCK_SH);
7680 if (unlockerr && err == NULL)
7681 err = unlockerr;
7682 return err;
7685 struct unstage_path_arg {
7686 struct got_worktree *worktree;
7687 struct got_fileindex *fileindex;
7688 struct got_repository *repo;
7689 got_worktree_checkout_cb progress_cb;
7690 void *progress_arg;
7691 got_worktree_patch_cb patch_cb;
7692 void *patch_arg;
7695 static const struct got_error *
7696 create_unstaged_content(char **path_unstaged_content,
7697 char **path_new_staged_content, struct got_object_id *blob_id,
7698 struct got_object_id *staged_blob_id, const char *relpath,
7699 struct got_repository *repo,
7700 got_worktree_patch_cb patch_cb, void *patch_arg)
7702 const struct got_error *err, *free_err;
7703 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7704 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7705 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7706 struct got_diffreg_result *diffreg_result = NULL;
7707 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7708 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7710 *path_unstaged_content = NULL;
7711 *path_new_staged_content = NULL;
7713 err = got_object_id_str(&label1, blob_id);
7714 if (err)
7715 return err;
7716 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7717 if (err)
7718 goto done;
7720 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7721 if (err)
7722 goto done;
7724 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7725 if (err)
7726 goto done;
7728 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7729 if (err)
7730 goto done;
7732 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7733 if (err)
7734 goto done;
7736 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7737 if (err)
7738 goto done;
7740 err = got_diff_files(&diffreg_result, f1, label1, f2,
7741 path2, 3, 0, 1, NULL);
7742 if (err)
7743 goto done;
7745 err = got_opentemp_named(path_unstaged_content, &outfile,
7746 "got-unstaged-content");
7747 if (err)
7748 goto done;
7749 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7750 "got-new-staged-content");
7751 if (err)
7752 goto done;
7754 if (fseek(f1, 0L, SEEK_SET) == -1) {
7755 err = got_ferror(f1, GOT_ERR_IO);
7756 goto done;
7758 if (fseek(f2, 0L, SEEK_SET) == -1) {
7759 err = got_ferror(f2, GOT_ERR_IO);
7760 goto done;
7762 /* Count the number of actual changes in the diff result. */
7763 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7764 struct diff_chunk_context cc = {};
7765 diff_chunk_context_load_change(&cc, &nchunks_used,
7766 diffreg_result->result, n, 0);
7767 nchanges++;
7769 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7770 int choice;
7771 err = apply_or_reject_change(&choice, &nchunks_used,
7772 diffreg_result->result, n, relpath, f1, f2,
7773 &line_cur1, &line_cur2,
7774 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7775 if (err)
7776 goto done;
7777 if (choice == GOT_PATCH_CHOICE_YES)
7778 have_content = 1;
7779 else
7780 have_rejected_content = 1;
7781 if (choice == GOT_PATCH_CHOICE_QUIT)
7782 break;
7784 if (have_content || have_rejected_content)
7785 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7786 outfile, rejectfile);
7787 done:
7788 free(label1);
7789 if (blob)
7790 got_object_blob_close(blob);
7791 if (staged_blob)
7792 got_object_blob_close(staged_blob);
7793 free_err = got_diffreg_result_free(diffreg_result);
7794 if (free_err && err == NULL)
7795 err = free_err;
7796 if (f1 && fclose(f1) == EOF && err == NULL)
7797 err = got_error_from_errno2("fclose", path1);
7798 if (f2 && fclose(f2) == EOF && err == NULL)
7799 err = got_error_from_errno2("fclose", path2);
7800 if (outfile && fclose(outfile) == EOF && err == NULL)
7801 err = got_error_from_errno2("fclose", *path_unstaged_content);
7802 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7803 err = got_error_from_errno2("fclose", *path_new_staged_content);
7804 if (path1 && unlink(path1) == -1 && err == NULL)
7805 err = got_error_from_errno2("unlink", path1);
7806 if (path2 && unlink(path2) == -1 && err == NULL)
7807 err = got_error_from_errno2("unlink", path2);
7808 if (err || !have_content) {
7809 if (*path_unstaged_content &&
7810 unlink(*path_unstaged_content) == -1 && err == NULL)
7811 err = got_error_from_errno2("unlink",
7812 *path_unstaged_content);
7813 free(*path_unstaged_content);
7814 *path_unstaged_content = NULL;
7816 if (err || !have_content || !have_rejected_content) {
7817 if (*path_new_staged_content &&
7818 unlink(*path_new_staged_content) == -1 && err == NULL)
7819 err = got_error_from_errno2("unlink",
7820 *path_new_staged_content);
7821 free(*path_new_staged_content);
7822 *path_new_staged_content = NULL;
7824 free(path1);
7825 free(path2);
7826 return err;
7829 static const struct got_error *
7830 unstage_hunks(struct got_object_id *staged_blob_id,
7831 struct got_blob_object *blob_base,
7832 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7833 const char *ondisk_path, const char *label_orig,
7834 struct got_worktree *worktree, struct got_repository *repo,
7835 got_worktree_patch_cb patch_cb, void *patch_arg,
7836 got_worktree_checkout_cb progress_cb, void *progress_arg)
7838 const struct got_error *err = NULL;
7839 char *path_unstaged_content = NULL;
7840 char *path_new_staged_content = NULL;
7841 char *parent = NULL, *base_path = NULL;
7842 char *blob_base_path = NULL;
7843 struct got_object_id *new_staged_blob_id = NULL;
7844 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7845 struct stat sb;
7847 err = create_unstaged_content(&path_unstaged_content,
7848 &path_new_staged_content, blob_id, staged_blob_id,
7849 ie->path, repo, patch_cb, patch_arg);
7850 if (err)
7851 return err;
7853 if (path_unstaged_content == NULL)
7854 return NULL;
7856 if (path_new_staged_content) {
7857 err = got_object_blob_create(&new_staged_blob_id,
7858 path_new_staged_content, repo);
7859 if (err)
7860 goto done;
7863 f = fopen(path_unstaged_content, "r");
7864 if (f == NULL) {
7865 err = got_error_from_errno2("fopen",
7866 path_unstaged_content);
7867 goto done;
7869 if (fstat(fileno(f), &sb) == -1) {
7870 err = got_error_from_errno2("fstat", path_unstaged_content);
7871 goto done;
7873 if (got_fileindex_entry_staged_filetype_get(ie) ==
7874 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7875 char link_target[PATH_MAX];
7876 size_t r;
7877 r = fread(link_target, 1, sizeof(link_target), f);
7878 if (r == 0 && ferror(f)) {
7879 err = got_error_from_errno("fread");
7880 goto done;
7882 if (r >= sizeof(link_target)) { /* should not happen */
7883 err = got_error(GOT_ERR_NO_SPACE);
7884 goto done;
7886 link_target[r] = '\0';
7887 err = merge_symlink(worktree, blob_base,
7888 ondisk_path, ie->path, label_orig, link_target,
7889 worktree->base_commit_id, repo, progress_cb,
7890 progress_arg);
7891 } else {
7892 int local_changes_subsumed;
7894 err = got_path_dirname(&parent, ondisk_path);
7895 if (err)
7896 return err;
7898 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7899 parent) == -1) {
7900 err = got_error_from_errno("asprintf");
7901 base_path = NULL;
7902 goto done;
7905 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7906 if (err)
7907 goto done;
7908 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7909 blob_base);
7910 if (err)
7911 goto done;
7914 * In order the run a 3-way merge with a symlink we copy the symlink's
7915 * target path into a temporary file and use that file with diff3.
7917 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7918 err = dump_symlink_target_path_to_file(&f_deriv2,
7919 ondisk_path);
7920 if (err)
7921 goto done;
7922 } else {
7923 int fd;
7924 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7925 if (fd == -1) {
7926 err = got_error_from_errno2("open", ondisk_path);
7927 goto done;
7929 f_deriv2 = fdopen(fd, "r");
7930 if (f_deriv2 == NULL) {
7931 err = got_error_from_errno2("fdopen", ondisk_path);
7932 close(fd);
7933 goto done;
7937 err = merge_file(&local_changes_subsumed, worktree,
7938 f_base, f, f_deriv2, ondisk_path, ie->path,
7939 got_fileindex_perms_to_st(ie),
7940 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7941 repo, progress_cb, progress_arg);
7943 if (err)
7944 goto done;
7946 if (new_staged_blob_id) {
7947 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7948 SHA1_DIGEST_LENGTH);
7949 } else {
7950 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7951 got_fileindex_entry_staged_filetype_set(ie, 0);
7953 done:
7954 free(new_staged_blob_id);
7955 if (path_unstaged_content &&
7956 unlink(path_unstaged_content) == -1 && err == NULL)
7957 err = got_error_from_errno2("unlink", path_unstaged_content);
7958 if (path_new_staged_content &&
7959 unlink(path_new_staged_content) == -1 && err == NULL)
7960 err = got_error_from_errno2("unlink", path_new_staged_content);
7961 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7962 err = got_error_from_errno2("unlink", blob_base_path);
7963 if (f_base && fclose(f_base) == EOF && err == NULL)
7964 err = got_error_from_errno2("fclose", path_unstaged_content);
7965 if (f && fclose(f) == EOF && err == NULL)
7966 err = got_error_from_errno2("fclose", path_unstaged_content);
7967 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7968 err = got_error_from_errno2("fclose", ondisk_path);
7969 free(path_unstaged_content);
7970 free(path_new_staged_content);
7971 free(blob_base_path);
7972 free(parent);
7973 free(base_path);
7974 return err;
7977 static const struct got_error *
7978 unstage_path(void *arg, unsigned char status,
7979 unsigned char staged_status, const char *relpath,
7980 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7981 struct got_object_id *commit_id, int dirfd, const char *de_name)
7983 const struct got_error *err = NULL;
7984 struct unstage_path_arg *a = arg;
7985 struct got_fileindex_entry *ie;
7986 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7987 char *ondisk_path = NULL;
7988 char *id_str = NULL, *label_orig = NULL;
7989 int local_changes_subsumed;
7990 struct stat sb;
7992 if (staged_status != GOT_STATUS_ADD &&
7993 staged_status != GOT_STATUS_MODIFY &&
7994 staged_status != GOT_STATUS_DELETE)
7995 return NULL;
7997 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7998 if (ie == NULL)
7999 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8001 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8002 == -1)
8003 return got_error_from_errno("asprintf");
8005 err = got_object_id_str(&id_str,
8006 commit_id ? commit_id : a->worktree->base_commit_id);
8007 if (err)
8008 goto done;
8009 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8010 id_str) == -1) {
8011 err = got_error_from_errno("asprintf");
8012 goto done;
8015 switch (staged_status) {
8016 case GOT_STATUS_MODIFY:
8017 err = got_object_open_as_blob(&blob_base, a->repo,
8018 blob_id, 8192);
8019 if (err)
8020 break;
8021 /* fall through */
8022 case GOT_STATUS_ADD:
8023 if (a->patch_cb) {
8024 if (staged_status == GOT_STATUS_ADD) {
8025 int choice = GOT_PATCH_CHOICE_NONE;
8026 err = (*a->patch_cb)(&choice, a->patch_arg,
8027 staged_status, ie->path, NULL, 1, 1);
8028 if (err)
8029 break;
8030 if (choice != GOT_PATCH_CHOICE_YES)
8031 break;
8032 } else {
8033 err = unstage_hunks(staged_blob_id,
8034 blob_base, blob_id, ie, ondisk_path,
8035 label_orig, a->worktree, a->repo,
8036 a->patch_cb, a->patch_arg,
8037 a->progress_cb, a->progress_arg);
8038 break; /* Done with this file. */
8041 err = got_object_open_as_blob(&blob_staged, a->repo,
8042 staged_blob_id, 8192);
8043 if (err)
8044 break;
8045 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8046 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8047 case GOT_FILEIDX_MODE_REGULAR_FILE:
8048 err = merge_blob(&local_changes_subsumed, a->worktree,
8049 blob_base, ondisk_path, relpath,
8050 got_fileindex_perms_to_st(ie), label_orig,
8051 blob_staged, commit_id ? commit_id :
8052 a->worktree->base_commit_id, a->repo,
8053 a->progress_cb, a->progress_arg);
8054 break;
8055 case GOT_FILEIDX_MODE_SYMLINK:
8056 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8057 char *staged_target;
8058 err = got_object_blob_read_to_str(
8059 &staged_target, blob_staged);
8060 if (err)
8061 goto done;
8062 err = merge_symlink(a->worktree, blob_base,
8063 ondisk_path, relpath, label_orig,
8064 staged_target, commit_id ? commit_id :
8065 a->worktree->base_commit_id,
8066 a->repo, a->progress_cb, a->progress_arg);
8067 free(staged_target);
8068 } else {
8069 err = merge_blob(&local_changes_subsumed,
8070 a->worktree, blob_base, ondisk_path,
8071 relpath, got_fileindex_perms_to_st(ie),
8072 label_orig, blob_staged,
8073 commit_id ? commit_id :
8074 a->worktree->base_commit_id, a->repo,
8075 a->progress_cb, a->progress_arg);
8077 break;
8078 default:
8079 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8080 break;
8082 if (err == NULL) {
8083 got_fileindex_entry_stage_set(ie,
8084 GOT_FILEIDX_STAGE_NONE);
8085 got_fileindex_entry_staged_filetype_set(ie, 0);
8087 break;
8088 case GOT_STATUS_DELETE:
8089 if (a->patch_cb) {
8090 int choice = GOT_PATCH_CHOICE_NONE;
8091 err = (*a->patch_cb)(&choice, a->patch_arg,
8092 staged_status, ie->path, NULL, 1, 1);
8093 if (err)
8094 break;
8095 if (choice == GOT_PATCH_CHOICE_NO)
8096 break;
8097 if (choice != GOT_PATCH_CHOICE_YES) {
8098 err = got_error(GOT_ERR_PATCH_CHOICE);
8099 break;
8102 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8103 got_fileindex_entry_staged_filetype_set(ie, 0);
8104 err = get_file_status(&status, &sb, ie, ondisk_path,
8105 dirfd, de_name, a->repo);
8106 if (err)
8107 break;
8108 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8109 break;
8111 done:
8112 free(ondisk_path);
8113 if (blob_base)
8114 got_object_blob_close(blob_base);
8115 if (blob_staged)
8116 got_object_blob_close(blob_staged);
8117 free(id_str);
8118 free(label_orig);
8119 return err;
8122 const struct got_error *
8123 got_worktree_unstage(struct got_worktree *worktree,
8124 struct got_pathlist_head *paths,
8125 got_worktree_checkout_cb progress_cb, void *progress_arg,
8126 got_worktree_patch_cb patch_cb, void *patch_arg,
8127 struct got_repository *repo)
8129 const struct got_error *err = NULL, *sync_err, *unlockerr;
8130 struct got_pathlist_entry *pe;
8131 struct got_fileindex *fileindex = NULL;
8132 char *fileindex_path = NULL;
8133 struct unstage_path_arg upa;
8135 err = lock_worktree(worktree, LOCK_EX);
8136 if (err)
8137 return err;
8139 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8140 if (err)
8141 goto done;
8143 upa.worktree = worktree;
8144 upa.fileindex = fileindex;
8145 upa.repo = repo;
8146 upa.progress_cb = progress_cb;
8147 upa.progress_arg = progress_arg;
8148 upa.patch_cb = patch_cb;
8149 upa.patch_arg = patch_arg;
8150 TAILQ_FOREACH(pe, paths, entry) {
8151 err = worktree_status(worktree, pe->path, fileindex, repo,
8152 unstage_path, &upa, NULL, NULL, 0, 0);
8153 if (err)
8154 goto done;
8157 sync_err = sync_fileindex(fileindex, fileindex_path);
8158 if (sync_err && err == NULL)
8159 err = sync_err;
8160 done:
8161 free(fileindex_path);
8162 if (fileindex)
8163 got_fileindex_free(fileindex);
8164 unlockerr = lock_worktree(worktree, LOCK_SH);
8165 if (unlockerr && err == NULL)
8166 err = unlockerr;
8167 return err;
8170 struct report_file_info_arg {
8171 struct got_worktree *worktree;
8172 got_worktree_path_info_cb info_cb;
8173 void *info_arg;
8174 struct got_pathlist_head *paths;
8175 got_cancel_cb cancel_cb;
8176 void *cancel_arg;
8179 static const struct got_error *
8180 report_file_info(void *arg, struct got_fileindex_entry *ie)
8182 struct report_file_info_arg *a = arg;
8183 struct got_pathlist_entry *pe;
8184 struct got_object_id blob_id, staged_blob_id, commit_id;
8185 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8186 struct got_object_id *commit_idp = NULL;
8187 int stage;
8189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8190 return got_error(GOT_ERR_CANCELLED);
8192 TAILQ_FOREACH(pe, a->paths, entry) {
8193 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8194 got_path_is_child(ie->path, pe->path, pe->path_len))
8195 break;
8197 if (pe == NULL) /* not found */
8198 return NULL;
8200 if (got_fileindex_entry_has_blob(ie)) {
8201 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8202 blob_idp = &blob_id;
8204 stage = got_fileindex_entry_stage_get(ie);
8205 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8206 stage == GOT_FILEIDX_STAGE_ADD) {
8207 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8208 SHA1_DIGEST_LENGTH);
8209 staged_blob_idp = &staged_blob_id;
8212 if (got_fileindex_entry_has_commit(ie)) {
8213 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8214 commit_idp = &commit_id;
8217 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8218 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8221 const struct got_error *
8222 got_worktree_path_info(struct got_worktree *worktree,
8223 struct got_pathlist_head *paths,
8224 got_worktree_path_info_cb info_cb, void *info_arg,
8225 got_cancel_cb cancel_cb, void *cancel_arg)
8228 const struct got_error *err = NULL, *unlockerr;
8229 struct got_fileindex *fileindex = NULL;
8230 char *fileindex_path = NULL;
8231 struct report_file_info_arg arg;
8233 err = lock_worktree(worktree, LOCK_SH);
8234 if (err)
8235 return err;
8237 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8238 if (err)
8239 goto done;
8241 arg.worktree = worktree;
8242 arg.info_cb = info_cb;
8243 arg.info_arg = info_arg;
8244 arg.paths = paths;
8245 arg.cancel_cb = cancel_cb;
8246 arg.cancel_arg = cancel_arg;
8247 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8248 &arg);
8249 done:
8250 free(fileindex_path);
8251 if (fileindex)
8252 got_fileindex_free(fileindex);
8253 unlockerr = lock_worktree(worktree, LOCK_UN);
8254 if (unlockerr && err == NULL)
8255 err = unlockerr;
8256 return err;