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>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
751 if (parent == NULL)
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 if (err)
759 goto done;
761 free(base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
764 base_path = NULL;
765 goto done;
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 if (err)
770 goto done;
771 if (blob_orig) {
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 blob_orig);
774 if (err)
775 goto done;
776 } else {
777 /*
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
781 */
784 /*
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
787 */
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
790 ssize_t target_len;
791 size_t n;
793 free(base_path);
794 if (asprintf(&base_path, "%s/got-symlink-merge",
795 parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 base_path = NULL;
798 goto done;
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 if (err)
802 goto done;
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
807 goto done;
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
812 goto done;
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
816 goto done;
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
823 if (err)
824 goto done;
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (err)
829 goto done;
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
833 goto done;
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
839 merged_path);
840 if (err)
841 goto done;
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
846 goto done;
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
851 ondisk_path);
852 goto done;
854 done:
855 if (err) {
856 if (merged_path)
857 unlink(merged_path);
859 if (symlink_path) {
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
865 free(symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
870 free(merged_path);
871 free(base_path);
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
876 return err;
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
881 size_t target_len)
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
888 ondisk_path);
889 return NULL;
892 /*
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
903 */
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 FILE *f = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
914 if (err)
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 if (err)
925 goto done;
927 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 deriv_target ? deriv_target : "(symlink was deleted)",
930 orig_target ? label_orig : "",
931 orig_target ? "\n" : "",
932 orig_target ? orig_target : "",
933 orig_target ? "\n" : "",
934 GOT_DIFF_CONFLICT_MARKER_SEP,
935 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 err = got_error_from_errno2("fprintf", path);
937 goto done;
940 if (unlink(ondisk_path) == -1) {
941 err = got_error_from_errno2("unlink", ondisk_path);
942 goto done;
944 if (rename(path, ondisk_path) == -1) {
945 err = got_error_from_errno3("rename", path, ondisk_path);
946 goto done;
948 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 err = got_error_from_errno2("chmod", ondisk_path);
950 goto done;
952 done:
953 if (f != NULL && fclose(f) == EOF && err == NULL)
954 err = got_error_from_errno2("fclose", path);
955 free(path);
956 free(id_str);
957 free(label_deriv);
958 return err;
961 /* forward declaration */
962 static const struct got_error *
963 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 const char *, const char *, uint16_t, const char *,
965 struct got_blob_object *, struct got_object_id *,
966 struct got_repository *, got_worktree_checkout_cb, void *);
968 /*
969 * Merge a symlink into the work tree, where blob_orig acts as the common
970 * ancestor, deriv_target is the link target of the first derived version,
971 * and the symlink on disk acts as the second derived version.
972 * Assume that contents of both blobs represent symlinks.
973 */
974 static const struct got_error *
975 merge_symlink(struct got_worktree *worktree,
976 struct got_blob_object *blob_orig, const char *ondisk_path,
977 const char *path, const char *label_orig, const char *deriv_target,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 char *ancestor_target = NULL;
983 struct stat sb;
984 ssize_t ondisk_len, deriv_len;
985 char ondisk_target[PATH_MAX];
986 int have_local_change = 0;
987 int have_incoming_change = 0;
989 if (lstat(ondisk_path, &sb) == -1)
990 return got_error_from_errno2("lstat", ondisk_path);
992 ondisk_len = readlink(ondisk_path, ondisk_target,
993 sizeof(ondisk_target));
994 if (ondisk_len == -1) {
995 err = got_error_from_errno2("readlink",
996 ondisk_path);
997 goto done;
999 ondisk_target[ondisk_len] = '\0';
1001 if (blob_orig) {
1002 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1003 if (err)
1004 goto done;
1007 if (ancestor_target == NULL ||
1008 (ondisk_len != strlen(ancestor_target) ||
1009 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 have_local_change = 1;
1012 deriv_len = strlen(deriv_target);
1013 if (ancestor_target == NULL ||
1014 (deriv_len != strlen(ancestor_target) ||
1015 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 have_incoming_change = 1;
1018 if (!have_local_change && !have_incoming_change) {
1019 if (ancestor_target) {
1020 /* Both sides made the same change. */
1021 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1022 path);
1023 } else if (deriv_len == ondisk_len &&
1024 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 /* Both sides added the same symlink. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1027 path);
1028 } else {
1029 /* Both sides added symlinks which don't match. */
1030 err = install_symlink_conflict(deriv_target,
1031 deriv_base_commit_id, ancestor_target,
1032 label_orig, ondisk_target, ondisk_path);
1033 if (err)
1034 goto done;
1035 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1036 path);
1038 } else if (!have_local_change && have_incoming_change) {
1039 /* Apply the incoming change. */
1040 err = update_symlink(ondisk_path, deriv_target,
1041 strlen(deriv_target));
1042 if (err)
1043 goto done;
1044 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 } else if (have_local_change && have_incoming_change) {
1046 if (deriv_len == ondisk_len &&
1047 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 path);
1051 } else {
1052 err = install_symlink_conflict(deriv_target,
1053 deriv_base_commit_id, ancestor_target, label_orig,
1054 ondisk_target, ondisk_path);
1055 if (err)
1056 goto done;
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1058 path);
1062 done:
1063 free(ancestor_target);
1064 return err;
1068 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 * blob_deriv acts as the first derived version, and the file on disk
1070 * acts as the second derived version.
1072 static const struct got_error *
1073 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 struct got_blob_object *blob_orig, const char *ondisk_path,
1075 const char *path, uint16_t st_mode, const char *label_orig,
1076 struct got_blob_object *blob_deriv,
1077 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 got_worktree_checkout_cb progress_cb, void *progress_arg)
1080 const struct got_error *err = NULL;
1081 FILE *f_deriv = NULL;
1082 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 char *label_deriv = NULL, *parent;
1085 *local_changes_subsumed = 0;
1087 parent = dirname(ondisk_path);
1088 if (parent == NULL)
1089 return got_error_from_errno2("dirname", ondisk_path);
1091 free(base_path);
1092 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 err = got_error_from_errno("asprintf");
1094 base_path = NULL;
1095 goto done;
1098 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1099 if (err)
1100 goto done;
1101 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1102 blob_deriv);
1103 if (err)
1104 goto done;
1106 err = got_object_id_str(&id_str, deriv_base_commit_id);
1107 if (err)
1108 goto done;
1109 if (asprintf(&label_deriv, "%s: commit %s",
1110 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 goto done;
1115 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 label_deriv, repo, progress_cb, progress_arg);
1118 done:
1119 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 err = got_error_from_errno("fclose");
1121 free(base_path);
1122 if (blob_deriv_path) {
1123 unlink(blob_deriv_path);
1124 free(blob_deriv_path);
1126 free(id_str);
1127 free(label_deriv);
1128 return err;
1131 static const struct got_error *
1132 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1136 const struct got_error *err = NULL;
1137 struct got_fileindex_entry *new_ie;
1139 *new_iep = NULL;
1141 err = got_fileindex_entry_alloc(&new_ie, path);
1142 if (err)
1143 return err;
1145 err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 blob_id->sha1, base_commit_id->sha1, 1);
1147 if (err)
1148 goto done;
1150 err = got_fileindex_entry_add(fileindex, new_ie);
1151 done:
1152 if (err)
1153 got_fileindex_entry_free(new_ie);
1154 else
1155 *new_iep = new_ie;
1156 return err;
1159 static mode_t
1160 get_ondisk_perms(int executable, mode_t st_mode)
1162 mode_t xbits = S_IXUSR;
1164 if (executable) {
1165 /* Map read bits to execute bits. */
1166 if (st_mode & S_IRGRP)
1167 xbits |= S_IXGRP;
1168 if (st_mode & S_IROTH)
1169 xbits |= S_IXOTH;
1170 return st_mode | xbits;
1173 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1176 /* forward declaration */
1177 static const struct got_error *
1178 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 const char *path, mode_t te_mode, mode_t st_mode,
1180 struct got_blob_object *blob, int restoring_missing_file,
1181 int reverting_versioned_file, int installing_bad_symlink,
1182 int path_is_unversioned, struct got_repository *repo,
1183 got_worktree_checkout_cb progress_cb, void *progress_arg);
1186 * This function assumes that the provided symlink target points at a
1187 * safe location in the work tree!
1189 static const struct got_error *
1190 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1191 size_t target_len)
1193 const struct got_error *err = NULL;
1194 ssize_t elen;
1195 char etarget[PATH_MAX];
1196 int fd;
1199 * "Bad" symlinks (those pointing outside the work tree or into the
1200 * .got directory) are installed in the work tree as a regular file
1201 * which contains the bad symlink target path.
1202 * The new symlink target has already been checked for safety by our
1203 * caller. If we can successfully open a regular file then we simply
1204 * replace this file with a symlink below.
1206 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1207 if (fd == -1) {
1208 if (errno != ELOOP)
1209 return got_error_from_errno2("open", ondisk_path);
1211 /* We are updating an existing on-disk symlink. */
1212 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1213 if (elen == -1)
1214 return got_error_from_errno2("readlink", ondisk_path);
1216 if (elen == target_len &&
1217 memcmp(etarget, target_path, target_len) == 0)
1218 return NULL; /* nothing to do */
1221 err = update_symlink(ondisk_path, target_path, target_len);
1222 if (fd != -1 && close(fd) == -1 && err == NULL)
1223 err = got_error_from_errno2("close", ondisk_path);
1224 return err;
1227 static const struct got_error *
1228 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1231 const struct got_error *err = NULL;
1232 char canonpath[PATH_MAX];
1233 char *path_got = NULL;
1235 *is_bad_symlink = 0;
1237 if (target_len >= sizeof(canonpath)) {
1238 *is_bad_symlink = 1;
1239 return NULL;
1243 * We do not use realpath(3) to resolve the symlink's target
1244 * path because we don't want to resolve symlinks recursively.
1245 * Instead we make the path absolute and then canonicalize it.
1246 * Relative symlink target lookup should begin at the directory
1247 * in which the blob object is being installed.
1249 if (!got_path_is_absolute(target_path)) {
1250 char *abspath;
1251 char *parent = dirname(ondisk_path);
1252 if (parent == NULL)
1253 return got_error_from_errno2("dirname", ondisk_path);
1254 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 return got_error_from_errno("asprintf");
1256 if (strlen(abspath) >= sizeof(canonpath)) {
1257 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1258 free(abspath);
1259 return err;
1261 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1262 free(abspath);
1263 if (err)
1264 return err;
1265 } else {
1266 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1267 if (err)
1268 return err;
1271 /* Only allow symlinks pointing at paths within the work tree. */
1272 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 *is_bad_symlink = 1;
1274 return NULL;
1277 /* Do not allow symlinks pointing into the .got directory. */
1278 if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 GOT_WORKTREE_GOT_DIR) == -1)
1280 return got_error_from_errno("asprintf");
1281 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 *is_bad_symlink = 1;
1284 free(path_got);
1285 return NULL;
1288 static const struct got_error *
1289 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 int restoring_missing_file, int reverting_versioned_file,
1292 int path_is_unversioned, struct got_repository *repo,
1293 got_worktree_checkout_cb progress_cb, void *progress_arg)
1295 const struct got_error *err = NULL;
1296 char target_path[PATH_MAX];
1297 size_t len, target_len = 0;
1298 char *path_got = NULL;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *is_bad_symlink = 0;
1305 * Blob object content specifies the target path of the link.
1306 * If a symbolic link cannot be installed we instead create
1307 * a regular file which contains the link target path stored
1308 * in the blob object.
1310 do {
1311 err = got_object_blob_read_block(&len, blob);
1312 if (len + target_len >= sizeof(target_path)) {
1313 /* Path too long; install as a regular file. */
1314 *is_bad_symlink = 1;
1315 got_object_blob_rewind(blob);
1316 return install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file,
1319 1, path_is_unversioned, repo, progress_cb,
1320 progress_arg);
1322 if (len > 0) {
1323 /* Skip blob object header first time around. */
1324 memcpy(target_path + target_len, buf + hdrlen,
1325 len - hdrlen);
1326 target_len += len - hdrlen;
1327 hdrlen = 0;
1329 } while (len != 0);
1330 target_path[target_len] = '\0';
1332 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 ondisk_path, worktree->root_path);
1334 if (err)
1335 return err;
1337 if (*is_bad_symlink) {
1338 /* install as a regular file */
1339 *is_bad_symlink = 1;
1340 got_object_blob_rewind(blob);
1341 err = install_blob(worktree, ondisk_path, path,
1342 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 restoring_missing_file, reverting_versioned_file, 1,
1344 path_is_unversioned, repo, progress_cb, progress_arg);
1345 goto done;
1348 if (symlink(target_path, ondisk_path) == -1) {
1349 if (errno == EEXIST) {
1350 if (path_is_unversioned) {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_UNVERSIONED, path);
1353 goto done;
1355 err = replace_existing_symlink(ondisk_path,
1356 target_path, target_len);
1357 if (err)
1358 goto done;
1359 if (progress_cb) {
1360 err = (*progress_cb)(progress_arg,
1361 reverting_versioned_file ?
1362 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1363 path);
1365 goto done; /* Nothing else to do. */
1368 if (errno == ENOENT) {
1369 char *parent = dirname(ondisk_path);
1370 if (parent == NULL) {
1371 err = got_error_from_errno2("dirname",
1372 ondisk_path);
1373 goto done;
1375 err = add_dir_on_disk(worktree, parent);
1376 if (err)
1377 goto done;
1379 * Retry, and fall through to error handling
1380 * below if this second attempt fails.
1382 if (symlink(target_path, ondisk_path) != -1) {
1383 err = NULL; /* success */
1384 goto done;
1388 /* Handle errors from first or second creation attempt. */
1389 if (errno == ENAMETOOLONG) {
1390 /* bad target path; install as a regular file */
1391 *is_bad_symlink = 1;
1392 got_object_blob_rewind(blob);
1393 err = install_blob(worktree, ondisk_path, path,
1394 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 restoring_missing_file, reverting_versioned_file, 1,
1396 path_is_unversioned, repo,
1397 progress_cb, progress_arg);
1398 } else if (errno == ENOTDIR) {
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1401 } else {
1402 err = got_error_from_errno3("symlink",
1403 target_path, ondisk_path);
1405 } else if (progress_cb)
1406 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1408 done:
1409 free(path_got);
1410 return err;
1413 static const struct got_error *
1414 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 const char *path, mode_t te_mode, mode_t st_mode,
1416 struct got_blob_object *blob, int restoring_missing_file,
1417 int reverting_versioned_file, int installing_bad_symlink,
1418 int path_is_unversioned, struct got_repository *repo,
1419 got_worktree_checkout_cb progress_cb, void *progress_arg)
1421 const struct got_error *err = NULL;
1422 int fd = -1;
1423 size_t len, hdrlen;
1424 int update = 0;
1425 char *tmppath = NULL;
1427 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 GOT_DEFAULT_FILE_MODE);
1429 if (fd == -1) {
1430 if (errno == ENOENT) {
1431 char *parent = dirname(path);
1432 if (parent == NULL)
1433 return got_error_from_errno2("dirname", path);
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err)
1436 return err;
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1440 if (fd == -1)
1441 return got_error_from_errno2("open",
1442 ondisk_path);
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 err = (*progress_cb)(progress_arg,
1446 GOT_STATUS_UNVERSIONED, path);
1447 goto done;
1449 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 /* TODO file is obstructed; do something */
1451 err = got_error_path(ondisk_path,
1452 GOT_ERR_FILE_OBSTRUCTED);
1453 goto done;
1454 } else {
1455 err = got_opentemp_named_fd(&tmppath, &fd,
1456 ondisk_path);
1457 if (err)
1458 goto done;
1459 update = 1;
1461 } else
1462 return got_error_from_errno2("open", ondisk_path);
1465 if (progress_cb) {
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 path);
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 path);
1472 else
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 if (err)
1476 goto done;
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1480 do {
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1483 if (err)
1484 break;
1485 if (len > 0) {
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 if (outlen == -1) {
1489 err = got_error_from_errno("write");
1490 goto done;
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1493 goto done;
1495 hdrlen = 0;
1497 } while (len != 0);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1501 goto done;
1504 if (update) {
1505 if (rename(tmppath, ondisk_path) != 0) {
1506 err = got_error_from_errno3("rename", tmppath,
1507 ondisk_path);
1508 unlink(tmppath);
1509 goto done;
1513 if (chmod(ondisk_path,
1514 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 err = got_error_from_errno2("chmod", ondisk_path);
1516 goto done;
1519 done:
1520 if (fd != -1 && close(fd) != 0 && err == NULL)
1521 err = got_error_from_errno("close");
1522 free(tmppath);
1523 return err;
1526 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 static const struct got_error *
1528 get_modified_file_content_status(unsigned char *status, FILE *f)
1530 const struct got_error *err = NULL;
1531 const char *markers[3] = {
1532 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 GOT_DIFF_CONFLICT_MARKER_SEP,
1534 GOT_DIFF_CONFLICT_MARKER_END
1536 int i = 0;
1537 char *line;
1538 size_t len;
1539 const char delim[3] = {'\0', '\0', '\0'};
1541 while (*status == GOT_STATUS_MODIFY) {
1542 line = fparseln(f, &len, NULL, delim, 0);
1543 if (line == NULL) {
1544 if (feof(f))
1545 break;
1546 err = got_ferror(f, GOT_ERR_IO);
1547 break;
1550 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1552 == 0)
1553 *status = GOT_STATUS_CONFLICT;
1554 else
1555 i++;
1559 return err;
1562 static int
1563 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1565 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1569 static int
1570 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1572 return !(ie->ctime_sec == sb->st_ctime &&
1573 ie->ctime_nsec == sb->st_ctimensec &&
1574 ie->mtime_sec == sb->st_mtime &&
1575 ie->mtime_nsec == sb->st_mtimensec &&
1576 ie->size == (sb->st_size & 0xffffffff) &&
1577 !xbit_differs(ie, sb->st_mode));
1580 static unsigned char
1581 get_staged_status(struct got_fileindex_entry *ie)
1583 switch (got_fileindex_entry_stage_get(ie)) {
1584 case GOT_FILEIDX_STAGE_ADD:
1585 return GOT_STATUS_ADD;
1586 case GOT_FILEIDX_STAGE_DELETE:
1587 return GOT_STATUS_DELETE;
1588 case GOT_FILEIDX_STAGE_MODIFY:
1589 return GOT_STATUS_MODIFY;
1590 default:
1591 return GOT_STATUS_NO_CHANGE;
1595 static const struct got_error *
1596 get_symlink_modification_status(unsigned char *status,
1597 struct got_fileindex_entry *ie, const char *abspath,
1598 int dirfd, const char *de_name, struct got_blob_object *blob)
1600 const struct got_error *err = NULL;
1601 char target_path[PATH_MAX];
1602 char etarget[PATH_MAX];
1603 ssize_t elen;
1604 size_t len, target_len = 0;
1605 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1608 *status = GOT_STATUS_NO_CHANGE;
1610 /* Blob object content specifies the target path of the link. */
1611 do {
1612 err = got_object_blob_read_block(&len, blob);
1613 if (err)
1614 return err;
1615 if (len + target_len >= sizeof(target_path)) {
1617 * Should not happen. The blob contents were OK
1618 * when this symlink was installed.
1620 return got_error(GOT_ERR_NO_SPACE);
1622 if (len > 0) {
1623 /* Skip blob object header first time around. */
1624 memcpy(target_path + target_len, buf + hdrlen,
1625 len - hdrlen);
1626 target_len += len - hdrlen;
1627 hdrlen = 0;
1629 } while (len != 0);
1630 target_path[target_len] = '\0';
1632 if (dirfd != -1) {
1633 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1634 if (elen == -1)
1635 return got_error_from_errno2("readlinkat", abspath);
1636 } else {
1637 elen = readlink(abspath, etarget, sizeof(etarget));
1638 if (elen == -1)
1639 return got_error_from_errno2("readlink", abspath);
1642 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 *status = GOT_STATUS_MODIFY;
1645 return NULL;
1648 static const struct got_error *
1649 get_file_status(unsigned char *status, struct stat *sb,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 struct got_object_id id;
1655 size_t hdrlen;
1656 int fd = -1;
1657 FILE *f = NULL;
1658 uint8_t fbuf[8192];
1659 struct got_blob_object *blob = NULL;
1660 size_t flen, blen;
1661 unsigned char staged_status = get_staged_status(ie);
1663 *status = GOT_STATUS_NO_CHANGE;
1666 * Whenever the caller provides a directory descriptor and a
1667 * directory entry name for the file, use them! This prevents
1668 * race conditions if filesystem paths change beneath our feet.
1670 if (dirfd != -1) {
1671 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 if (errno == ENOENT) {
1673 if (got_fileindex_entry_has_file_on_disk(ie))
1674 *status = GOT_STATUS_MISSING;
1675 else
1676 *status = GOT_STATUS_DELETE;
1677 goto done;
1679 err = got_error_from_errno2("fstatat", abspath);
1680 goto done;
1682 } else {
1683 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 return got_error_from_errno2("open", abspath);
1686 else if (fd == -1 && errno == ELOOP) {
1687 if (lstat(abspath, sb) == -1)
1688 return got_error_from_errno2("lstat", abspath);
1689 } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 if (errno == ENOENT) {
1691 if (got_fileindex_entry_has_file_on_disk(ie))
1692 *status = GOT_STATUS_MISSING;
1693 else
1694 *status = GOT_STATUS_DELETE;
1695 goto done;
1697 err = got_error_from_errno2("fstat", abspath);
1698 goto done;
1702 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 *status = GOT_STATUS_OBSTRUCTED;
1704 goto done;
1707 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 *status = GOT_STATUS_DELETE;
1709 goto done;
1710 } else if (!got_fileindex_entry_has_blob(ie) &&
1711 staged_status != GOT_STATUS_ADD) {
1712 *status = GOT_STATUS_ADD;
1713 goto done;
1716 if (!stat_info_differs(ie, sb))
1717 goto done;
1719 if (S_ISLNK(sb->st_mode) &&
1720 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 *status = GOT_STATUS_MODIFY;
1722 goto done;
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1728 else
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1732 if (err)
1733 goto done;
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1738 goto done;
1741 if (dirfd != -1) {
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1743 if (fd == -1) {
1744 err = got_error_from_errno2("openat", abspath);
1745 goto done;
1749 f = fdopen(fd, "r");
1750 if (f == NULL) {
1751 err = got_error_from_errno2("fdopen", abspath);
1752 goto done;
1754 fd = -1;
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1756 for (;;) {
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1759 if (err)
1760 goto done;
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1765 goto done;
1767 if (blen == 0) {
1768 if (flen != 0)
1769 *status = GOT_STATUS_MODIFY;
1770 break;
1771 } else if (flen == 0) {
1772 if (blen != 0)
1773 *status = GOT_STATUS_MODIFY;
1774 break;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1781 } else {
1782 *status = GOT_STATUS_MODIFY;
1783 break;
1785 hdrlen = 0;
1788 if (*status == GOT_STATUS_MODIFY) {
1789 rewind(f);
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1793 done:
1794 if (blob)
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1800 return err;
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(char *ondisk_path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, ondisk_path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1815 return NULL;
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 void *progress_arg)
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1827 char *ondisk_path;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 struct stat sb;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1834 if (ie) {
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 goto done;
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 repo);
1841 if (err)
1842 goto done;
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1845 } else {
1846 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 status = GOT_STATUS_UNVERSIONED;
1850 if (status == GOT_STATUS_OBSTRUCTED) {
1851 err = (*progress_cb)(progress_arg, status, path);
1852 goto done;
1854 if (status == GOT_STATUS_CONFLICT) {
1855 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1856 path);
1857 goto done;
1860 if (ie && status != GOT_STATUS_MISSING &&
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 if (got_fileindex_entry_has_commit(ie) &&
1863 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(ondisk_path, status, ie, &sb);
1866 if (err)
1867 goto done;
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 path);
1870 goto done;
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 err = sync_timestamps(ondisk_path, status, ie, &sb);
1876 goto done;
1880 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1881 if (err)
1882 goto done;
1884 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 int update_timestamps;
1886 struct got_blob_object *blob2 = NULL;
1887 char *label_orig = NULL;
1888 if (got_fileindex_entry_has_blob(ie)) {
1889 struct got_object_id id2;
1890 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1892 if (err)
1893 goto done;
1895 if (got_fileindex_entry_has_commit(ie)) {
1896 char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 sizeof(id_str)) == NULL) {
1899 err = got_error_path(id_str,
1900 GOT_ERR_BAD_OBJ_ID_STR);
1901 goto done;
1903 if (asprintf(&label_orig, "%s: commit %s",
1904 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 err = got_error_from_errno("asprintf");
1906 goto done;
1909 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1910 char *link_target;
1911 err = got_object_blob_read_to_str(&link_target, blob);
1912 if (err)
1913 goto done;
1914 err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 label_orig, link_target, worktree->base_commit_id,
1916 repo, progress_cb, progress_arg);
1917 free(link_target);
1918 } else {
1919 err = merge_blob(&update_timestamps, worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig, blob,
1921 worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1924 free(label_orig);
1925 if (blob2)
1926 got_object_blob_close(blob2);
1927 if (err)
1928 goto done;
1930 * Do not update timestamps of files with local changes.
1931 * Otherwise, a future status walk would treat them as
1932 * unmodified files again.
1934 err = got_fileindex_entry_update(ie, ondisk_path,
1935 blob->id.sha1, worktree->base_commit_id->sha1,
1936 update_timestamps);
1937 } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 err = got_fileindex_entry_update(ie, ondisk_path,
1939 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 } else if (status == GOT_STATUS_DELETE) {
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1942 if (err)
1943 goto done;
1944 err = got_fileindex_entry_update(ie, ondisk_path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 if (err)
1947 goto done;
1948 } else {
1949 int is_bad_symlink = 0;
1950 if (S_ISLNK(te->mode)) {
1951 err = install_symlink(&is_bad_symlink, worktree,
1952 ondisk_path, path, blob,
1953 status == GOT_STATUS_MISSING, 0,
1954 status == GOT_STATUS_UNVERSIONED, repo,
1955 progress_cb, progress_arg);
1956 } else {
1957 err = install_blob(worktree, ondisk_path, path,
1958 te->mode, sb.st_mode, blob,
1959 status == GOT_STATUS_MISSING, 0, 0,
1960 status == GOT_STATUS_UNVERSIONED, repo,
1961 progress_cb, progress_arg);
1963 if (err)
1964 goto done;
1966 if (ie) {
1967 err = got_fileindex_entry_update(ie, ondisk_path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1969 } else {
1970 err = create_fileindex_entry(&ie, fileindex,
1971 worktree->base_commit_id, ondisk_path, path,
1972 &blob->id);
1974 if (err)
1975 goto done;
1977 if (is_bad_symlink) {
1978 got_fileindex_entry_filetype_set(ie,
1979 GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 got_object_blob_close(blob);
1983 done:
1984 free(ondisk_path);
1985 return err;
1988 static const struct got_error *
1989 remove_ondisk_file(const char *root_path, const char *path)
1991 const struct got_error *err = NULL;
1992 char *ondisk_path = NULL;
1994 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 return got_error_from_errno("asprintf");
1997 if (unlink(ondisk_path) == -1) {
1998 if (errno != ENOENT)
1999 err = got_error_from_errno2("unlink", ondisk_path);
2000 } else {
2001 char *parent = dirname(ondisk_path);
2002 while (parent && strcmp(parent, root_path) != 0) {
2003 if (rmdir(parent) == -1) {
2004 if (errno != ENOTEMPTY)
2005 err = got_error_from_errno2("rmdir",
2006 parent);
2007 break;
2009 parent = dirname(parent);
2012 free(ondisk_path);
2013 return err;
2016 static const struct got_error *
2017 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 struct got_fileindex_entry *ie, struct got_repository *repo,
2019 got_worktree_checkout_cb progress_cb, void *progress_arg)
2021 const struct got_error *err = NULL;
2022 unsigned char status;
2023 struct stat sb;
2024 char *ondisk_path;
2026 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2029 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2030 == -1)
2031 return got_error_from_errno("asprintf");
2033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2034 if (err)
2035 goto done;
2037 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 char ondisk_target[PATH_MAX];
2039 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 sizeof(ondisk_target));
2041 if (ondisk_len == -1) {
2042 err = got_error_from_errno2("readlink", ondisk_path);
2043 goto done;
2045 ondisk_target[ondisk_len] = '\0';
2046 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 NULL, NULL, /* XXX pass common ancestor info? */
2048 ondisk_target, ondisk_path);
2049 if (err)
2050 goto done;
2051 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2052 ie->path);
2053 goto done;
2056 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 status == GOT_STATUS_ADD) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2059 if (err)
2060 goto done;
2062 * Preserve the working file and change the deleted blob's
2063 * entry into a schedule-add entry.
2065 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2066 0);
2067 } else {
2068 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2069 if (err)
2070 goto done;
2071 if (status == GOT_STATUS_NO_CHANGE) {
2072 err = remove_ondisk_file(worktree->root_path, ie->path);
2073 if (err)
2074 goto done;
2076 got_fileindex_entry_remove(fileindex, ie);
2078 done:
2079 free(ondisk_path);
2080 return err;
2083 struct diff_cb_arg {
2084 struct got_fileindex *fileindex;
2085 struct got_worktree *worktree;
2086 struct got_repository *repo;
2087 got_worktree_checkout_cb progress_cb;
2088 void *progress_arg;
2089 got_cancel_cb cancel_cb;
2090 void *cancel_arg;
2093 static const struct got_error *
2094 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 struct got_tree_entry *te, const char *parent_path)
2097 struct diff_cb_arg *a = arg;
2099 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 return got_error(GOT_ERR_CANCELLED);
2102 return update_blob(a->worktree, a->fileindex, ie, te,
2103 ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 static const struct got_error *
2107 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2109 struct diff_cb_arg *a = arg;
2111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 return got_error(GOT_ERR_CANCELLED);
2114 return delete_blob(a->worktree, a->fileindex, ie,
2115 a->repo, a->progress_cb, a->progress_arg);
2118 static const struct got_error *
2119 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2121 struct diff_cb_arg *a = arg;
2122 const struct got_error *err;
2123 char *path;
2125 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 return got_error(GOT_ERR_CANCELLED);
2128 if (got_object_tree_entry_is_submodule(te))
2129 return NULL;
2131 if (asprintf(&path, "%s%s%s", parent_path,
2132 parent_path[0] ? "/" : "", te->name)
2133 == -1)
2134 return got_error_from_errno("asprintf");
2136 if (S_ISDIR(te->mode))
2137 err = add_dir_on_disk(a->worktree, path);
2138 else
2139 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 a->repo, a->progress_cb, a->progress_arg);
2142 free(path);
2143 return err;
2146 static const struct got_error *
2147 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2149 const struct got_error *err = NULL;
2150 char *uuidstr = NULL;
2151 uint32_t uuid_status;
2153 *refname = NULL;
2155 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2156 if (uuid_status != uuid_s_ok)
2157 return got_error_uuid(uuid_status, "uuid_to_string");
2159 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2160 == -1) {
2161 err = got_error_from_errno("asprintf");
2162 *refname = NULL;
2164 free(uuidstr);
2165 return err;
2168 const struct got_error *
2169 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2171 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2174 static const struct got_error *
2175 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2177 return get_ref_name(refname, worktree,
2178 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2181 static const struct got_error *
2182 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2184 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2187 static const struct got_error *
2188 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2190 return get_ref_name(refname, worktree,
2191 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2194 static const struct got_error *
2195 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2197 return get_ref_name(refname, worktree,
2198 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2201 static const struct got_error *
2202 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree,
2205 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2208 static const struct got_error *
2209 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2211 return get_ref_name(refname, worktree,
2212 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2215 static const struct got_error *
2216 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2218 return get_ref_name(refname, worktree,
2219 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2222 static const struct got_error *
2223 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2225 return get_ref_name(refname, worktree,
2226 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2229 const struct got_error *
2230 got_worktree_get_histedit_script_path(char **path,
2231 struct got_worktree *worktree)
2233 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2234 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2235 *path = NULL;
2236 return got_error_from_errno("asprintf");
2238 return NULL;
2242 * Prevent Git's garbage collector from deleting our base commit by
2243 * setting a reference to our base commit's ID.
2245 static const struct got_error *
2246 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2248 const struct got_error *err = NULL;
2249 struct got_reference *ref = NULL;
2250 char *refname;
2252 err = got_worktree_get_base_ref_name(&refname, worktree);
2253 if (err)
2254 return err;
2256 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2257 if (err)
2258 goto done;
2260 err = got_ref_write(ref, repo);
2261 done:
2262 free(refname);
2263 if (ref)
2264 got_ref_close(ref);
2265 return err;
2268 static const struct got_error *
2269 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2271 const struct got_error *err = NULL;
2273 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2274 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2275 err = got_error_from_errno("asprintf");
2276 *fileindex_path = NULL;
2278 return err;
2282 static const struct got_error *
2283 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2284 struct got_worktree *worktree)
2286 const struct got_error *err = NULL;
2287 FILE *index = NULL;
2289 *fileindex_path = NULL;
2290 *fileindex = got_fileindex_alloc();
2291 if (*fileindex == NULL)
2292 return got_error_from_errno("got_fileindex_alloc");
2294 err = get_fileindex_path(fileindex_path, worktree);
2295 if (err)
2296 goto done;
2298 index = fopen(*fileindex_path, "rb");
2299 if (index == NULL) {
2300 if (errno != ENOENT)
2301 err = got_error_from_errno2("fopen", *fileindex_path);
2302 } else {
2303 err = got_fileindex_read(*fileindex, index);
2304 if (fclose(index) != 0 && err == NULL)
2305 err = got_error_from_errno("fclose");
2307 done:
2308 if (err) {
2309 free(*fileindex_path);
2310 *fileindex_path = NULL;
2311 got_fileindex_free(*fileindex);
2312 *fileindex = NULL;
2314 return err;
2317 struct bump_base_commit_id_arg {
2318 struct got_object_id *base_commit_id;
2319 const char *path;
2320 size_t path_len;
2321 const char *entry_name;
2322 got_worktree_checkout_cb progress_cb;
2323 void *progress_arg;
2326 /* Bump base commit ID of all files within an updated part of the work tree. */
2327 static const struct got_error *
2328 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2330 const struct got_error *err;
2331 struct bump_base_commit_id_arg *a = arg;
2333 if (a->entry_name) {
2334 if (strcmp(ie->path, a->path) != 0)
2335 return NULL;
2336 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2337 return NULL;
2339 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2340 SHA1_DIGEST_LENGTH) == 0)
2341 return NULL;
2343 if (a->progress_cb) {
2344 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2345 ie->path);
2346 if (err)
2347 return err;
2349 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2350 return NULL;
2353 static const struct got_error *
2354 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2356 const struct got_error *err = NULL;
2357 char *new_fileindex_path = NULL;
2358 FILE *new_index = NULL;
2359 struct timespec timeout;
2361 err = got_opentemp_named(&new_fileindex_path, &new_index,
2362 fileindex_path);
2363 if (err)
2364 goto done;
2366 err = got_fileindex_write(fileindex, new_index);
2367 if (err)
2368 goto done;
2370 if (rename(new_fileindex_path, fileindex_path) != 0) {
2371 err = got_error_from_errno3("rename", new_fileindex_path,
2372 fileindex_path);
2373 unlink(new_fileindex_path);
2377 * Sleep for a short amount of time to ensure that files modified after
2378 * this program exits have a different time stamp from the one which
2379 * was recorded in the file index.
2381 timeout.tv_sec = 0;
2382 timeout.tv_nsec = 1;
2383 nanosleep(&timeout, NULL);
2384 done:
2385 if (new_index)
2386 fclose(new_index);
2387 free(new_fileindex_path);
2388 return err;
2391 static const struct got_error *
2392 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2393 struct got_object_id **tree_id, const char *wt_relpath,
2394 struct got_worktree *worktree, struct got_repository *repo)
2396 const struct got_error *err = NULL;
2397 struct got_object_id *id = NULL;
2398 char *in_repo_path = NULL;
2399 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2401 *entry_type = GOT_OBJ_TYPE_ANY;
2402 *tree_relpath = NULL;
2403 *tree_id = NULL;
2405 if (wt_relpath[0] == '\0') {
2406 /* Check out all files within the work tree. */
2407 *entry_type = GOT_OBJ_TYPE_TREE;
2408 *tree_relpath = strdup("");
2409 if (*tree_relpath == NULL) {
2410 err = got_error_from_errno("strdup");
2411 goto done;
2413 err = got_object_id_by_path(tree_id, repo,
2414 worktree->base_commit_id, worktree->path_prefix);
2415 if (err)
2416 goto done;
2417 return NULL;
2420 /* Check out a subset of files in the work tree. */
2422 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2423 is_root_wt ? "" : "/", wt_relpath) == -1) {
2424 err = got_error_from_errno("asprintf");
2425 goto done;
2428 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2429 in_repo_path);
2430 if (err)
2431 goto done;
2433 free(in_repo_path);
2434 in_repo_path = NULL;
2436 err = got_object_get_type(entry_type, repo, id);
2437 if (err)
2438 goto done;
2440 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2441 /* Check out a single file. */
2442 if (strchr(wt_relpath, '/') == NULL) {
2443 /* Check out a single file in work tree's root dir. */
2444 in_repo_path = strdup(worktree->path_prefix);
2445 if (in_repo_path == NULL) {
2446 err = got_error_from_errno("strdup");
2447 goto done;
2449 *tree_relpath = strdup("");
2450 if (*tree_relpath == NULL) {
2451 err = got_error_from_errno("strdup");
2452 goto done;
2454 } else {
2455 /* Check out a single file in a subdirectory. */
2456 err = got_path_dirname(tree_relpath, wt_relpath);
2457 if (err)
2458 return err;
2459 if (asprintf(&in_repo_path, "%s%s%s",
2460 worktree->path_prefix, is_root_wt ? "" : "/",
2461 *tree_relpath) == -1) {
2462 err = got_error_from_errno("asprintf");
2463 goto done;
2466 err = got_object_id_by_path(tree_id, repo,
2467 worktree->base_commit_id, in_repo_path);
2468 } else {
2469 /* Check out all files within a subdirectory. */
2470 *tree_id = got_object_id_dup(id);
2471 if (*tree_id == NULL) {
2472 err = got_error_from_errno("got_object_id_dup");
2473 goto done;
2475 *tree_relpath = strdup(wt_relpath);
2476 if (*tree_relpath == NULL) {
2477 err = got_error_from_errno("strdup");
2478 goto done;
2481 done:
2482 free(id);
2483 free(in_repo_path);
2484 if (err) {
2485 *entry_type = GOT_OBJ_TYPE_ANY;
2486 free(*tree_relpath);
2487 *tree_relpath = NULL;
2488 free(*tree_id);
2489 *tree_id = NULL;
2491 return err;
2494 static const struct got_error *
2495 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2496 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2497 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2498 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2500 const struct got_error *err = NULL;
2501 struct got_commit_object *commit = NULL;
2502 struct got_tree_object *tree = NULL;
2503 struct got_fileindex_diff_tree_cb diff_cb;
2504 struct diff_cb_arg arg;
2506 err = ref_base_commit(worktree, repo);
2507 if (err) {
2508 if (!(err->code == GOT_ERR_ERRNO &&
2509 (errno == EACCES || errno == EROFS)))
2510 goto done;
2511 err = (*progress_cb)(progress_arg,
2512 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2513 if (err)
2514 return err;
2517 err = got_object_open_as_commit(&commit, repo,
2518 worktree->base_commit_id);
2519 if (err)
2520 goto done;
2522 err = got_object_open_as_tree(&tree, repo, tree_id);
2523 if (err)
2524 goto done;
2526 if (entry_name &&
2527 got_object_tree_find_entry(tree, entry_name) == NULL) {
2528 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2529 goto done;
2532 diff_cb.diff_old_new = diff_old_new;
2533 diff_cb.diff_old = diff_old;
2534 diff_cb.diff_new = diff_new;
2535 arg.fileindex = fileindex;
2536 arg.worktree = worktree;
2537 arg.repo = repo;
2538 arg.progress_cb = progress_cb;
2539 arg.progress_arg = progress_arg;
2540 arg.cancel_cb = cancel_cb;
2541 arg.cancel_arg = cancel_arg;
2542 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2543 entry_name, repo, &diff_cb, &arg);
2544 done:
2545 if (tree)
2546 got_object_tree_close(tree);
2547 if (commit)
2548 got_object_commit_close(commit);
2549 return err;
2552 const struct got_error *
2553 got_worktree_checkout_files(struct got_worktree *worktree,
2554 struct got_pathlist_head *paths, struct got_repository *repo,
2555 got_worktree_checkout_cb progress_cb, void *progress_arg,
2556 got_cancel_cb cancel_cb, void *cancel_arg)
2558 const struct got_error *err = NULL, *sync_err, *unlockerr;
2559 struct got_commit_object *commit = NULL;
2560 struct got_tree_object *tree = NULL;
2561 struct got_fileindex *fileindex = NULL;
2562 char *fileindex_path = NULL;
2563 struct got_pathlist_entry *pe;
2564 struct tree_path_data {
2565 SIMPLEQ_ENTRY(tree_path_data) entry;
2566 struct got_object_id *tree_id;
2567 int entry_type;
2568 char *relpath;
2569 char *entry_name;
2570 } *tpd = NULL;
2571 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2573 SIMPLEQ_INIT(&tree_paths);
2575 err = lock_worktree(worktree, LOCK_EX);
2576 if (err)
2577 return err;
2579 /* Map all specified paths to in-repository trees. */
2580 TAILQ_FOREACH(pe, paths, entry) {
2581 tpd = malloc(sizeof(*tpd));
2582 if (tpd == NULL) {
2583 err = got_error_from_errno("malloc");
2584 goto done;
2587 err = find_tree_entry_for_checkout(&tpd->entry_type,
2588 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2589 if (err) {
2590 free(tpd);
2591 goto done;
2594 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2595 err = got_path_basename(&tpd->entry_name, pe->path);
2596 if (err) {
2597 free(tpd->relpath);
2598 free(tpd->tree_id);
2599 free(tpd);
2600 goto done;
2602 } else
2603 tpd->entry_name = NULL;
2605 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2609 * Read the file index.
2610 * Checking out files is supposed to be an idempotent operation.
2611 * If the on-disk file index is incomplete we will try to complete it.
2613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2614 if (err)
2615 goto done;
2617 tpd = SIMPLEQ_FIRST(&tree_paths);
2618 TAILQ_FOREACH(pe, paths, entry) {
2619 struct bump_base_commit_id_arg bbc_arg;
2621 err = checkout_files(worktree, fileindex, tpd->relpath,
2622 tpd->tree_id, tpd->entry_name, repo,
2623 progress_cb, progress_arg, cancel_cb, cancel_arg);
2624 if (err)
2625 break;
2627 bbc_arg.base_commit_id = worktree->base_commit_id;
2628 bbc_arg.entry_name = tpd->entry_name;
2629 bbc_arg.path = pe->path;
2630 bbc_arg.path_len = pe->path_len;
2631 bbc_arg.progress_cb = progress_cb;
2632 bbc_arg.progress_arg = progress_arg;
2633 err = got_fileindex_for_each_entry_safe(fileindex,
2634 bump_base_commit_id, &bbc_arg);
2635 if (err)
2636 break;
2638 tpd = SIMPLEQ_NEXT(tpd, entry);
2640 sync_err = sync_fileindex(fileindex, fileindex_path);
2641 if (sync_err && err == NULL)
2642 err = sync_err;
2643 done:
2644 free(fileindex_path);
2645 if (tree)
2646 got_object_tree_close(tree);
2647 if (commit)
2648 got_object_commit_close(commit);
2649 if (fileindex)
2650 got_fileindex_free(fileindex);
2651 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2652 tpd = SIMPLEQ_FIRST(&tree_paths);
2653 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2654 free(tpd->relpath);
2655 free(tpd->tree_id);
2656 free(tpd);
2658 unlockerr = lock_worktree(worktree, LOCK_SH);
2659 if (unlockerr && err == NULL)
2660 err = unlockerr;
2661 return err;
2664 struct merge_file_cb_arg {
2665 struct got_worktree *worktree;
2666 struct got_fileindex *fileindex;
2667 got_worktree_checkout_cb progress_cb;
2668 void *progress_arg;
2669 got_cancel_cb cancel_cb;
2670 void *cancel_arg;
2671 const char *label_orig;
2672 struct got_object_id *commit_id2;
2675 static const struct got_error *
2676 merge_file_cb(void *arg, struct got_blob_object *blob1,
2677 struct got_blob_object *blob2, struct got_object_id *id1,
2678 struct got_object_id *id2, const char *path1, const char *path2,
2679 mode_t mode1, mode_t mode2, struct got_repository *repo)
2681 static const struct got_error *err = NULL;
2682 struct merge_file_cb_arg *a = arg;
2683 struct got_fileindex_entry *ie;
2684 char *ondisk_path = NULL;
2685 struct stat sb;
2686 unsigned char status;
2687 int local_changes_subsumed;
2689 if (blob1 && blob2) {
2690 ie = got_fileindex_entry_get(a->fileindex, path2,
2691 strlen(path2));
2692 if (ie == NULL)
2693 return (*a->progress_cb)(a->progress_arg,
2694 GOT_STATUS_MISSING, path2);
2696 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2697 path2) == -1)
2698 return got_error_from_errno("asprintf");
2700 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2701 repo);
2702 if (err)
2703 goto done;
2705 if (status == GOT_STATUS_DELETE) {
2706 err = (*a->progress_cb)(a->progress_arg,
2707 GOT_STATUS_MERGE, path2);
2708 goto done;
2710 if (status != GOT_STATUS_NO_CHANGE &&
2711 status != GOT_STATUS_MODIFY &&
2712 status != GOT_STATUS_CONFLICT &&
2713 status != GOT_STATUS_ADD) {
2714 err = (*a->progress_cb)(a->progress_arg, status, path2);
2715 goto done;
2718 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2719 char *link_target2;
2720 err = got_object_blob_read_to_str(&link_target2, blob2);
2721 if (err)
2722 goto done;
2723 err = merge_symlink(a->worktree, blob1, ondisk_path,
2724 path2, a->label_orig, link_target2, a->commit_id2,
2725 repo, a->progress_cb, a->progress_arg);
2726 free(link_target2);
2727 } else {
2728 err = merge_blob(&local_changes_subsumed, a->worktree,
2729 blob1, ondisk_path, path2, sb.st_mode,
2730 a->label_orig, blob2, a->commit_id2, repo,
2731 a->progress_cb, a->progress_arg);
2733 } else if (blob1) {
2734 ie = got_fileindex_entry_get(a->fileindex, path1,
2735 strlen(path1));
2736 if (ie == NULL)
2737 return (*a->progress_cb)(a->progress_arg,
2738 GOT_STATUS_MISSING, path1);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2741 path1) == -1)
2742 return got_error_from_errno("asprintf");
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2745 repo);
2746 if (err)
2747 goto done;
2749 switch (status) {
2750 case GOT_STATUS_NO_CHANGE:
2751 err = (*a->progress_cb)(a->progress_arg,
2752 GOT_STATUS_DELETE, path1);
2753 if (err)
2754 goto done;
2755 err = remove_ondisk_file(a->worktree->root_path, path1);
2756 if (err)
2757 goto done;
2758 if (ie)
2759 got_fileindex_entry_mark_deleted_from_disk(ie);
2760 break;
2761 case GOT_STATUS_DELETE:
2762 case GOT_STATUS_MISSING:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2765 if (err)
2766 goto done;
2767 if (ie)
2768 got_fileindex_entry_mark_deleted_from_disk(ie);
2769 break;
2770 case GOT_STATUS_ADD:
2771 case GOT_STATUS_MODIFY:
2772 case GOT_STATUS_CONFLICT:
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_CANNOT_DELETE, path1);
2775 if (err)
2776 goto done;
2777 break;
2778 case GOT_STATUS_OBSTRUCTED:
2779 err = (*a->progress_cb)(a->progress_arg, status, path1);
2780 if (err)
2781 goto done;
2782 break;
2783 default:
2784 break;
2786 } else if (blob2) {
2787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2788 path2) == -1)
2789 return got_error_from_errno("asprintf");
2790 ie = got_fileindex_entry_get(a->fileindex, path2,
2791 strlen(path2));
2792 if (ie) {
2793 err = get_file_status(&status, &sb, ie, ondisk_path,
2794 -1, NULL, repo);
2795 if (err)
2796 goto done;
2797 if (status != GOT_STATUS_NO_CHANGE &&
2798 status != GOT_STATUS_MODIFY &&
2799 status != GOT_STATUS_CONFLICT &&
2800 status != GOT_STATUS_ADD) {
2801 err = (*a->progress_cb)(a->progress_arg,
2802 status, path2);
2803 goto done;
2805 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2806 char *link_target2;
2807 err = got_object_blob_read_to_str(&link_target2,
2808 blob2);
2809 if (err)
2810 goto done;
2811 err = merge_symlink(a->worktree, NULL,
2812 ondisk_path, path2, a->label_orig,
2813 link_target2, a->commit_id2, repo,
2814 a->progress_cb, a->progress_arg);
2815 free(link_target2);
2816 } else if (S_ISREG(sb.st_mode)) {
2817 err = merge_blob(&local_changes_subsumed,
2818 a->worktree, NULL, ondisk_path, path2,
2819 sb.st_mode, a->label_orig, blob2,
2820 a->commit_id2, repo, a->progress_cb,
2821 a->progress_arg);
2822 } else {
2823 err = got_error_path(ondisk_path,
2824 GOT_ERR_FILE_OBSTRUCTED);
2826 if (err)
2827 goto done;
2828 if (status == GOT_STATUS_DELETE) {
2829 err = got_fileindex_entry_update(ie,
2830 ondisk_path, blob2->id.sha1,
2831 a->worktree->base_commit_id->sha1, 0);
2832 if (err)
2833 goto done;
2835 } else {
2836 int is_bad_symlink = 0;
2837 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2838 if (S_ISLNK(mode2)) {
2839 err = install_symlink(&is_bad_symlink,
2840 a->worktree, ondisk_path, path2, blob2, 0,
2841 0, 1, repo, a->progress_cb, a->progress_arg);
2842 } else {
2843 err = install_blob(a->worktree, ondisk_path, path2,
2844 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2845 a->progress_cb, a->progress_arg);
2847 if (err)
2848 goto done;
2849 err = got_fileindex_entry_alloc(&ie, path2);
2850 if (err)
2851 goto done;
2852 err = got_fileindex_entry_update(ie, ondisk_path,
2853 NULL, NULL, 1);
2854 if (err) {
2855 got_fileindex_entry_free(ie);
2856 goto done;
2858 err = got_fileindex_entry_add(a->fileindex, ie);
2859 if (err) {
2860 got_fileindex_entry_free(ie);
2861 goto done;
2863 if (is_bad_symlink) {
2864 got_fileindex_entry_filetype_set(ie,
2865 GOT_FILEIDX_MODE_BAD_SYMLINK);
2869 done:
2870 free(ondisk_path);
2871 return err;
2874 struct check_merge_ok_arg {
2875 struct got_worktree *worktree;
2876 struct got_repository *repo;
2879 static const struct got_error *
2880 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2882 const struct got_error *err = NULL;
2883 struct check_merge_ok_arg *a = arg;
2884 unsigned char status;
2885 struct stat sb;
2886 char *ondisk_path;
2888 /* Reject merges into a work tree with mixed base commits. */
2889 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2890 SHA1_DIGEST_LENGTH))
2891 return got_error(GOT_ERR_MIXED_COMMITS);
2893 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2894 == -1)
2895 return got_error_from_errno("asprintf");
2897 /* Reject merges into a work tree with conflicted files. */
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2899 if (err)
2900 return err;
2901 if (status == GOT_STATUS_CONFLICT)
2902 return got_error(GOT_ERR_CONFLICTS);
2904 return NULL;
2907 static const struct got_error *
2908 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2909 const char *fileindex_path, struct got_object_id *commit_id1,
2910 struct got_object_id *commit_id2, struct got_repository *repo,
2911 got_worktree_checkout_cb progress_cb, void *progress_arg,
2912 got_cancel_cb cancel_cb, void *cancel_arg)
2914 const struct got_error *err = NULL, *sync_err;
2915 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2916 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2917 struct merge_file_cb_arg arg;
2918 char *label_orig = NULL;
2920 if (commit_id1) {
2921 char *id_str;
2923 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2924 worktree->path_prefix);
2925 if (err)
2926 goto done;
2928 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2929 if (err)
2930 goto done;
2932 err = got_object_id_str(&id_str, commit_id1);
2933 if (err)
2934 goto done;
2936 if (asprintf(&label_orig, "%s: commit %s",
2937 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2938 err = got_error_from_errno("asprintf");
2939 free(id_str);
2940 goto done;
2942 free(id_str);
2945 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2946 worktree->path_prefix);
2947 if (err)
2948 goto done;
2950 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2951 if (err)
2952 goto done;
2954 arg.worktree = worktree;
2955 arg.fileindex = fileindex;
2956 arg.progress_cb = progress_cb;
2957 arg.progress_arg = progress_arg;
2958 arg.cancel_cb = cancel_cb;
2959 arg.cancel_arg = cancel_arg;
2960 arg.label_orig = label_orig;
2961 arg.commit_id2 = commit_id2;
2962 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2963 sync_err = sync_fileindex(fileindex, fileindex_path);
2964 if (sync_err && err == NULL)
2965 err = sync_err;
2966 done:
2967 if (tree1)
2968 got_object_tree_close(tree1);
2969 if (tree2)
2970 got_object_tree_close(tree2);
2971 free(label_orig);
2972 return err;
2975 const struct got_error *
2976 got_worktree_merge_files(struct got_worktree *worktree,
2977 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2978 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2979 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2981 const struct got_error *err, *unlockerr;
2982 char *fileindex_path = NULL;
2983 struct got_fileindex *fileindex = NULL;
2984 struct check_merge_ok_arg mok_arg;
2986 err = lock_worktree(worktree, LOCK_EX);
2987 if (err)
2988 return err;
2990 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2991 if (err)
2992 goto done;
2994 mok_arg.worktree = worktree;
2995 mok_arg.repo = repo;
2996 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2997 &mok_arg);
2998 if (err)
2999 goto done;
3001 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3002 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3003 done:
3004 if (fileindex)
3005 got_fileindex_free(fileindex);
3006 free(fileindex_path);
3007 unlockerr = lock_worktree(worktree, LOCK_SH);
3008 if (unlockerr && err == NULL)
3009 err = unlockerr;
3010 return err;
3013 struct diff_dir_cb_arg {
3014 struct got_fileindex *fileindex;
3015 struct got_worktree *worktree;
3016 const char *status_path;
3017 size_t status_path_len;
3018 struct got_repository *repo;
3019 got_worktree_status_cb status_cb;
3020 void *status_arg;
3021 got_cancel_cb cancel_cb;
3022 void *cancel_arg;
3023 /* A pathlist containing per-directory pathlists of ignore patterns. */
3024 struct got_pathlist_head ignores;
3025 int report_unchanged;
3026 int no_ignores;
3029 static const struct got_error *
3030 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3031 int dirfd, const char *de_name,
3032 got_worktree_status_cb status_cb, void *status_arg,
3033 struct got_repository *repo, int report_unchanged)
3035 const struct got_error *err = NULL;
3036 unsigned char status = GOT_STATUS_NO_CHANGE;
3037 unsigned char staged_status = get_staged_status(ie);
3038 struct stat sb;
3039 struct got_object_id blob_id, commit_id, staged_blob_id;
3040 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3041 struct got_object_id *staged_blob_idp = NULL;
3043 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3044 if (err)
3045 return err;
3047 if (status == GOT_STATUS_NO_CHANGE &&
3048 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3049 return NULL;
3051 if (got_fileindex_entry_has_blob(ie)) {
3052 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3053 blob_idp = &blob_id;
3055 if (got_fileindex_entry_has_commit(ie)) {
3056 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3057 commit_idp = &commit_id;
3059 if (staged_status == GOT_STATUS_ADD ||
3060 staged_status == GOT_STATUS_MODIFY) {
3061 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3062 SHA1_DIGEST_LENGTH);
3063 staged_blob_idp = &staged_blob_id;
3066 return (*status_cb)(status_arg, status, staged_status,
3067 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3070 static const struct got_error *
3071 status_old_new(void *arg, struct got_fileindex_entry *ie,
3072 struct dirent *de, const char *parent_path, int dirfd)
3074 const struct got_error *err = NULL;
3075 struct diff_dir_cb_arg *a = arg;
3076 char *abspath;
3078 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3079 return got_error(GOT_ERR_CANCELLED);
3081 if (got_path_cmp(parent_path, a->status_path,
3082 strlen(parent_path), a->status_path_len) != 0 &&
3083 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3084 return NULL;
3086 if (parent_path[0]) {
3087 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3088 parent_path, de->d_name) == -1)
3089 return got_error_from_errno("asprintf");
3090 } else {
3091 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3092 de->d_name) == -1)
3093 return got_error_from_errno("asprintf");
3096 err = report_file_status(ie, abspath, dirfd, de->d_name,
3097 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3098 free(abspath);
3099 return err;
3102 static const struct got_error *
3103 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3105 struct diff_dir_cb_arg *a = arg;
3106 struct got_object_id blob_id, commit_id;
3107 unsigned char status;
3109 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3110 return got_error(GOT_ERR_CANCELLED);
3112 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3113 return NULL;
3115 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3116 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3117 if (got_fileindex_entry_has_file_on_disk(ie))
3118 status = GOT_STATUS_MISSING;
3119 else
3120 status = GOT_STATUS_DELETE;
3121 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3122 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3125 void
3126 free_ignorelist(struct got_pathlist_head *ignorelist)
3128 struct got_pathlist_entry *pe;
3130 TAILQ_FOREACH(pe, ignorelist, entry)
3131 free((char *)pe->path);
3132 got_pathlist_free(ignorelist);
3135 void
3136 free_ignores(struct got_pathlist_head *ignores)
3138 struct got_pathlist_entry *pe;
3140 TAILQ_FOREACH(pe, ignores, entry) {
3141 struct got_pathlist_head *ignorelist = pe->data;
3142 free_ignorelist(ignorelist);
3143 free((char *)pe->path);
3145 got_pathlist_free(ignores);
3148 static const struct got_error *
3149 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3151 const struct got_error *err = NULL;
3152 struct got_pathlist_entry *pe = NULL;
3153 struct got_pathlist_head *ignorelist;
3154 char *line = NULL, *pattern, *dirpath = NULL;
3155 size_t linesize = 0;
3156 ssize_t linelen;
3158 ignorelist = calloc(1, sizeof(*ignorelist));
3159 if (ignorelist == NULL)
3160 return got_error_from_errno("calloc");
3161 TAILQ_INIT(ignorelist);
3163 while ((linelen = getline(&line, &linesize, f)) != -1) {
3164 if (linelen > 0 && line[linelen - 1] == '\n')
3165 line[linelen - 1] = '\0';
3167 /* Git's ignores may contain comments. */
3168 if (line[0] == '#')
3169 continue;
3171 /* Git's negated patterns are not (yet?) supported. */
3172 if (line[0] == '!')
3173 continue;
3175 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3176 line) == -1) {
3177 err = got_error_from_errno("asprintf");
3178 goto done;
3180 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3181 if (err)
3182 goto done;
3184 if (ferror(f)) {
3185 err = got_error_from_errno("getline");
3186 goto done;
3189 dirpath = strdup(path);
3190 if (dirpath == NULL) {
3191 err = got_error_from_errno("strdup");
3192 goto done;
3194 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3195 done:
3196 free(line);
3197 if (err || pe == NULL) {
3198 free(dirpath);
3199 free_ignorelist(ignorelist);
3201 return err;
3204 int
3205 match_ignores(struct got_pathlist_head *ignores, const char *path)
3207 struct got_pathlist_entry *pe;
3209 /* Handle patterns which match in all directories. */
3210 TAILQ_FOREACH(pe, ignores, entry) {
3211 struct got_pathlist_head *ignorelist = pe->data;
3212 struct got_pathlist_entry *pi;
3214 TAILQ_FOREACH(pi, ignorelist, entry) {
3215 const char *p, *pattern = pi->path;
3217 if (strncmp(pattern, "**/", 3) != 0)
3218 continue;
3219 pattern += 3;
3220 p = path;
3221 while (*p) {
3222 if (fnmatch(pattern, p,
3223 FNM_PATHNAME | FNM_LEADING_DIR)) {
3224 /* Retry in next directory. */
3225 while (*p && *p != '/')
3226 p++;
3227 while (*p == '/')
3228 p++;
3229 continue;
3231 return 1;
3237 * The ignores pathlist contains ignore lists from children before
3238 * parents, so we can find the most specific ignorelist by walking
3239 * ignores backwards.
3241 pe = TAILQ_LAST(ignores, got_pathlist_head);
3242 while (pe) {
3243 if (got_path_is_child(path, pe->path, pe->path_len)) {
3244 struct got_pathlist_head *ignorelist = pe->data;
3245 struct got_pathlist_entry *pi;
3246 TAILQ_FOREACH(pi, ignorelist, entry) {
3247 const char *pattern = pi->path;
3248 int flags = FNM_LEADING_DIR;
3249 if (strstr(pattern, "/**/") == NULL)
3250 flags |= FNM_PATHNAME;
3251 if (fnmatch(pattern, path, flags))
3252 continue;
3253 return 1;
3256 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3259 return 0;
3262 static const struct got_error *
3263 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3264 const char *path, int dirfd, const char *ignores_filename)
3266 const struct got_error *err = NULL;
3267 char *ignorespath;
3268 int fd = -1;
3269 FILE *ignoresfile = NULL;
3271 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3272 path[0] ? "/" : "", ignores_filename) == -1)
3273 return got_error_from_errno("asprintf");
3275 if (dirfd != -1) {
3276 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3277 if (fd == -1) {
3278 if (errno != ENOENT && errno != EACCES)
3279 err = got_error_from_errno2("openat",
3280 ignorespath);
3281 } else {
3282 ignoresfile = fdopen(fd, "r");
3283 if (ignoresfile == NULL)
3284 err = got_error_from_errno2("fdopen",
3285 ignorespath);
3286 else {
3287 fd = -1;
3288 err = read_ignores(ignores, path, ignoresfile);
3291 } else {
3292 ignoresfile = fopen(ignorespath, "r");
3293 if (ignoresfile == NULL) {
3294 if (errno != ENOENT && errno != EACCES)
3295 err = got_error_from_errno2("fopen",
3296 ignorespath);
3297 } else
3298 err = read_ignores(ignores, path, ignoresfile);
3301 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3302 err = got_error_from_errno2("fclose", path);
3303 if (fd != -1 && close(fd) == -1 && err == NULL)
3304 err = got_error_from_errno2("close", path);
3305 free(ignorespath);
3306 return err;
3309 static const struct got_error *
3310 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3312 const struct got_error *err = NULL;
3313 struct diff_dir_cb_arg *a = arg;
3314 char *path = NULL;
3316 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3317 return got_error(GOT_ERR_CANCELLED);
3319 if (parent_path[0]) {
3320 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3321 return got_error_from_errno("asprintf");
3322 } else {
3323 path = de->d_name;
3326 if (de->d_type != DT_DIR &&
3327 got_path_is_child(path, a->status_path, a->status_path_len)
3328 && !match_ignores(&a->ignores, path))
3329 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3330 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3331 if (parent_path[0])
3332 free(path);
3333 return err;
3336 static const struct got_error *
3337 status_traverse(void *arg, const char *path, int dirfd)
3339 const struct got_error *err = NULL;
3340 struct diff_dir_cb_arg *a = arg;
3342 if (a->no_ignores)
3343 return NULL;
3345 err = add_ignores(&a->ignores, a->worktree->root_path,
3346 path, dirfd, ".cvsignore");
3347 if (err)
3348 return err;
3350 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3351 dirfd, ".gitignore");
3353 return err;
3356 static const struct got_error *
3357 report_single_file_status(const char *path, const char *ondisk_path,
3358 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3359 void *status_arg, struct got_repository *repo, int report_unchanged)
3361 struct got_fileindex_entry *ie;
3362 struct stat sb;
3364 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3365 if (ie)
3366 return report_file_status(ie, ondisk_path, -1, NULL,
3367 status_cb, status_arg, repo, report_unchanged);
3369 if (lstat(ondisk_path, &sb) == -1) {
3370 if (errno != ENOENT)
3371 return got_error_from_errno2("lstat", ondisk_path);
3372 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3373 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3374 return NULL;
3377 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3378 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3379 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3381 return NULL;
3384 static const struct got_error *
3385 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3386 const char *root_path, const char *path)
3388 const struct got_error *err;
3389 char *parent_path, *next_parent_path = NULL;
3391 err = add_ignores(ignores, root_path, "", -1,
3392 ".cvsignore");
3393 if (err)
3394 return err;
3396 err = add_ignores(ignores, root_path, "", -1,
3397 ".gitignore");
3398 if (err)
3399 return err;
3401 err = got_path_dirname(&parent_path, path);
3402 if (err) {
3403 if (err->code == GOT_ERR_BAD_PATH)
3404 return NULL; /* cannot traverse parent */
3405 return err;
3407 for (;;) {
3408 err = add_ignores(ignores, root_path, parent_path, -1,
3409 ".cvsignore");
3410 if (err)
3411 break;
3412 err = add_ignores(ignores, root_path, parent_path, -1,
3413 ".gitignore");
3414 if (err)
3415 break;
3416 err = got_path_dirname(&next_parent_path, parent_path);
3417 if (err) {
3418 if (err->code == GOT_ERR_BAD_PATH)
3419 err = NULL; /* traversed everything */
3420 break;
3422 free(parent_path);
3423 parent_path = next_parent_path;
3424 next_parent_path = NULL;
3427 free(parent_path);
3428 free(next_parent_path);
3429 return err;
3432 static const struct got_error *
3433 worktree_status(struct got_worktree *worktree, const char *path,
3434 struct got_fileindex *fileindex, struct got_repository *repo,
3435 got_worktree_status_cb status_cb, void *status_arg,
3436 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3437 int report_unchanged)
3439 const struct got_error *err = NULL;
3440 int fd = -1;
3441 struct got_fileindex_diff_dir_cb fdiff_cb;
3442 struct diff_dir_cb_arg arg;
3443 char *ondisk_path = NULL;
3445 TAILQ_INIT(&arg.ignores);
3447 if (asprintf(&ondisk_path, "%s%s%s",
3448 worktree->root_path, path[0] ? "/" : "", path) == -1)
3449 return got_error_from_errno("asprintf");
3451 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3452 if (fd == -1) {
3453 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3454 errno != ELOOP)
3455 err = got_error_from_errno2("open", ondisk_path);
3456 else
3457 err = report_single_file_status(path, ondisk_path,
3458 fileindex, status_cb, status_arg, repo,
3459 report_unchanged);
3460 } else {
3461 fdiff_cb.diff_old_new = status_old_new;
3462 fdiff_cb.diff_old = status_old;
3463 fdiff_cb.diff_new = status_new;
3464 fdiff_cb.diff_traverse = status_traverse;
3465 arg.fileindex = fileindex;
3466 arg.worktree = worktree;
3467 arg.status_path = path;
3468 arg.status_path_len = strlen(path);
3469 arg.repo = repo;
3470 arg.status_cb = status_cb;
3471 arg.status_arg = status_arg;
3472 arg.cancel_cb = cancel_cb;
3473 arg.cancel_arg = cancel_arg;
3474 arg.report_unchanged = report_unchanged;
3475 arg.no_ignores = no_ignores;
3476 if (!no_ignores) {
3477 err = add_ignores_from_parent_paths(&arg.ignores,
3478 worktree->root_path, path);
3479 if (err)
3480 goto done;
3482 err = got_fileindex_diff_dir(fileindex, fd,
3483 worktree->root_path, path, repo, &fdiff_cb, &arg);
3485 done:
3486 free_ignores(&arg.ignores);
3487 if (fd != -1 && close(fd) != 0 && err == NULL)
3488 err = got_error_from_errno("close");
3489 free(ondisk_path);
3490 return err;
3493 const struct got_error *
3494 got_worktree_status(struct got_worktree *worktree,
3495 struct got_pathlist_head *paths, struct got_repository *repo,
3496 got_worktree_status_cb status_cb, void *status_arg,
3497 got_cancel_cb cancel_cb, void *cancel_arg)
3499 const struct got_error *err = NULL;
3500 char *fileindex_path = NULL;
3501 struct got_fileindex *fileindex = NULL;
3502 struct got_pathlist_entry *pe;
3504 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3505 if (err)
3506 return err;
3508 TAILQ_FOREACH(pe, paths, entry) {
3509 err = worktree_status(worktree, pe->path, fileindex, repo,
3510 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3511 if (err)
3512 break;
3514 free(fileindex_path);
3515 got_fileindex_free(fileindex);
3516 return err;
3519 const struct got_error *
3520 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3521 const char *arg)
3523 const struct got_error *err = NULL;
3524 char *resolved = NULL, *cwd = NULL, *path = NULL;
3525 size_t len;
3526 struct stat sb;
3528 *wt_path = NULL;
3530 cwd = getcwd(NULL, 0);
3531 if (cwd == NULL)
3532 return got_error_from_errno("getcwd");
3534 if (lstat(arg, &sb) == -1) {
3535 if (errno != ENOENT) {
3536 err = got_error_from_errno2("lstat", arg);
3537 goto done;
3540 if (S_ISLNK(sb.st_mode)) {
3542 * We cannot use realpath(3) with symlinks since we want to
3543 * operate on the symlink itself.
3544 * But we can make the path absolute, assuming it is relative
3545 * to the current working directory, and then canonicalize it.
3547 char *abspath = NULL;
3548 char canonpath[PATH_MAX];
3549 if (!got_path_is_absolute(arg)) {
3550 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3551 err = got_error_from_errno("asprintf");
3552 goto done;
3556 err = got_canonpath(abspath ? abspath : arg, canonpath,
3557 sizeof(canonpath));
3558 if (err)
3559 goto done;
3560 resolved = strdup(canonpath);
3561 if (resolved == NULL) {
3562 err = got_error_from_errno("strdup");
3563 goto done;
3565 } else {
3566 resolved = realpath(arg, NULL);
3567 if (resolved == NULL) {
3568 if (errno != ENOENT) {
3569 err = got_error_from_errno2("realpath", arg);
3570 goto done;
3572 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3573 err = got_error_from_errno("asprintf");
3574 goto done;
3579 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3580 strlen(got_worktree_get_root_path(worktree)))) {
3581 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3582 goto done;
3585 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3586 err = got_path_skip_common_ancestor(&path,
3587 got_worktree_get_root_path(worktree), resolved);
3588 if (err)
3589 goto done;
3590 } else {
3591 path = strdup("");
3592 if (path == NULL) {
3593 err = got_error_from_errno("strdup");
3594 goto done;
3598 /* XXX status walk can't deal with trailing slash! */
3599 len = strlen(path);
3600 while (len > 0 && path[len - 1] == '/') {
3601 path[len - 1] = '\0';
3602 len--;
3604 done:
3605 free(resolved);
3606 free(cwd);
3607 if (err == NULL)
3608 *wt_path = path;
3609 else
3610 free(path);
3611 return err;
3614 struct schedule_addition_args {
3615 struct got_worktree *worktree;
3616 struct got_fileindex *fileindex;
3617 got_worktree_checkout_cb progress_cb;
3618 void *progress_arg;
3619 struct got_repository *repo;
3622 static const struct got_error *
3623 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3624 const char *relpath, struct got_object_id *blob_id,
3625 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3626 int dirfd, const char *de_name)
3628 struct schedule_addition_args *a = arg;
3629 const struct got_error *err = NULL;
3630 struct got_fileindex_entry *ie;
3631 struct stat sb;
3632 char *ondisk_path;
3634 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3635 relpath) == -1)
3636 return got_error_from_errno("asprintf");
3638 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3639 if (ie) {
3640 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3641 de_name, a->repo);
3642 if (err)
3643 goto done;
3644 /* Re-adding an existing entry is a no-op. */
3645 if (status == GOT_STATUS_ADD)
3646 goto done;
3647 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3648 if (err)
3649 goto done;
3652 if (status != GOT_STATUS_UNVERSIONED) {
3653 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3654 goto done;
3657 err = got_fileindex_entry_alloc(&ie, relpath);
3658 if (err)
3659 goto done;
3660 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3661 if (err) {
3662 got_fileindex_entry_free(ie);
3663 goto done;
3665 err = got_fileindex_entry_add(a->fileindex, ie);
3666 if (err) {
3667 got_fileindex_entry_free(ie);
3668 goto done;
3670 done:
3671 free(ondisk_path);
3672 if (err)
3673 return err;
3674 if (status == GOT_STATUS_ADD)
3675 return NULL;
3676 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3679 const struct got_error *
3680 got_worktree_schedule_add(struct got_worktree *worktree,
3681 struct got_pathlist_head *paths,
3682 got_worktree_checkout_cb progress_cb, void *progress_arg,
3683 struct got_repository *repo, int no_ignores)
3685 struct got_fileindex *fileindex = NULL;
3686 char *fileindex_path = NULL;
3687 const struct got_error *err = NULL, *sync_err, *unlockerr;
3688 struct got_pathlist_entry *pe;
3689 struct schedule_addition_args saa;
3691 err = lock_worktree(worktree, LOCK_EX);
3692 if (err)
3693 return err;
3695 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3696 if (err)
3697 goto done;
3699 saa.worktree = worktree;
3700 saa.fileindex = fileindex;
3701 saa.progress_cb = progress_cb;
3702 saa.progress_arg = progress_arg;
3703 saa.repo = repo;
3705 TAILQ_FOREACH(pe, paths, entry) {
3706 err = worktree_status(worktree, pe->path, fileindex, repo,
3707 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3708 if (err)
3709 break;
3711 sync_err = sync_fileindex(fileindex, fileindex_path);
3712 if (sync_err && err == NULL)
3713 err = sync_err;
3714 done:
3715 free(fileindex_path);
3716 if (fileindex)
3717 got_fileindex_free(fileindex);
3718 unlockerr = lock_worktree(worktree, LOCK_SH);
3719 if (unlockerr && err == NULL)
3720 err = unlockerr;
3721 return err;
3724 struct schedule_deletion_args {
3725 struct got_worktree *worktree;
3726 struct got_fileindex *fileindex;
3727 got_worktree_delete_cb progress_cb;
3728 void *progress_arg;
3729 struct got_repository *repo;
3730 int delete_local_mods;
3731 int keep_on_disk;
3734 static const struct got_error *
3735 schedule_for_deletion(void *arg, unsigned char status,
3736 unsigned char staged_status, const char *relpath,
3737 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3738 struct got_object_id *commit_id, int dirfd, const char *de_name)
3740 struct schedule_deletion_args *a = arg;
3741 const struct got_error *err = NULL;
3742 struct got_fileindex_entry *ie = NULL;
3743 struct stat sb;
3744 char *ondisk_path, *parent = NULL;
3746 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3747 if (ie == NULL)
3748 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3750 staged_status = get_staged_status(ie);
3751 if (staged_status != GOT_STATUS_NO_CHANGE) {
3752 if (staged_status == GOT_STATUS_DELETE)
3753 return NULL;
3754 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3758 relpath) == -1)
3759 return got_error_from_errno("asprintf");
3761 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3762 a->repo);
3763 if (err)
3764 goto done;
3766 if (status != GOT_STATUS_NO_CHANGE) {
3767 if (status == GOT_STATUS_DELETE)
3768 goto done;
3769 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3770 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3771 goto done;
3773 if (status != GOT_STATUS_MODIFY &&
3774 status != GOT_STATUS_MISSING) {
3775 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3776 goto done;
3780 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3781 if (dirfd != -1) {
3782 if (unlinkat(dirfd, de_name, 0) != 0) {
3783 err = got_error_from_errno2("unlinkat",
3784 ondisk_path);
3785 goto done;
3787 } else if (unlink(ondisk_path) != 0) {
3788 err = got_error_from_errno2("unlink", ondisk_path);
3789 goto done;
3792 parent = dirname(ondisk_path);
3794 if (parent == NULL) {
3795 err = got_error_from_errno2("dirname", ondisk_path);
3796 goto done;
3798 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3799 if (rmdir(parent) == -1) {
3800 if (errno != ENOTEMPTY)
3801 err = got_error_from_errno2("rmdir",
3802 parent);
3803 break;
3805 parent = dirname(parent);
3806 if (parent == NULL) {
3807 err = got_error_from_errno2("dirname", parent);
3808 goto done;
3813 got_fileindex_entry_mark_deleted_from_disk(ie);
3814 done:
3815 free(ondisk_path);
3816 if (err)
3817 return err;
3818 if (status == GOT_STATUS_DELETE)
3819 return NULL;
3820 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3821 staged_status, relpath);
3824 const struct got_error *
3825 got_worktree_schedule_delete(struct got_worktree *worktree,
3826 struct got_pathlist_head *paths, int delete_local_mods,
3827 got_worktree_delete_cb progress_cb, void *progress_arg,
3828 struct got_repository *repo, int keep_on_disk)
3830 struct got_fileindex *fileindex = NULL;
3831 char *fileindex_path = NULL;
3832 const struct got_error *err = NULL, *sync_err, *unlockerr;
3833 struct got_pathlist_entry *pe;
3834 struct schedule_deletion_args sda;
3836 err = lock_worktree(worktree, LOCK_EX);
3837 if (err)
3838 return err;
3840 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3841 if (err)
3842 goto done;
3844 sda.worktree = worktree;
3845 sda.fileindex = fileindex;
3846 sda.progress_cb = progress_cb;
3847 sda.progress_arg = progress_arg;
3848 sda.repo = repo;
3849 sda.delete_local_mods = delete_local_mods;
3850 sda.keep_on_disk = keep_on_disk;
3852 TAILQ_FOREACH(pe, paths, entry) {
3853 err = worktree_status(worktree, pe->path, fileindex, repo,
3854 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3855 if (err)
3856 break;
3858 sync_err = sync_fileindex(fileindex, fileindex_path);
3859 if (sync_err && err == NULL)
3860 err = sync_err;
3861 done:
3862 free(fileindex_path);
3863 if (fileindex)
3864 got_fileindex_free(fileindex);
3865 unlockerr = lock_worktree(worktree, LOCK_SH);
3866 if (unlockerr && err == NULL)
3867 err = unlockerr;
3868 return err;
3871 static const struct got_error *
3872 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3874 const struct got_error *err = NULL;
3875 char *line = NULL;
3876 size_t linesize = 0, n;
3877 ssize_t linelen;
3879 linelen = getline(&line, &linesize, infile);
3880 if (linelen == -1) {
3881 if (ferror(infile)) {
3882 err = got_error_from_errno("getline");
3883 goto done;
3885 return NULL;
3887 if (outfile) {
3888 n = fwrite(line, 1, linelen, outfile);
3889 if (n != linelen) {
3890 err = got_ferror(outfile, GOT_ERR_IO);
3891 goto done;
3894 if (rejectfile) {
3895 n = fwrite(line, 1, linelen, rejectfile);
3896 if (n != linelen)
3897 err = got_ferror(outfile, GOT_ERR_IO);
3899 done:
3900 free(line);
3901 return err;
3904 static const struct got_error *
3905 skip_one_line(FILE *f)
3907 char *line = NULL;
3908 size_t linesize = 0;
3909 ssize_t linelen;
3911 linelen = getline(&line, &linesize, f);
3912 if (linelen == -1) {
3913 if (ferror(f))
3914 return got_error_from_errno("getline");
3915 return NULL;
3917 free(line);
3918 return NULL;
3921 static const struct got_error *
3922 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3923 int start_old, int end_old, int start_new, int end_new,
3924 FILE *outfile, FILE *rejectfile)
3926 const struct got_error *err;
3928 /* Copy old file's lines leading up to patch. */
3929 while (!feof(f1) && *line_cur1 < start_old) {
3930 err = copy_one_line(f1, outfile, NULL);
3931 if (err)
3932 return err;
3933 (*line_cur1)++;
3935 /* Skip new file's lines leading up to patch. */
3936 while (!feof(f2) && *line_cur2 < start_new) {
3937 if (rejectfile)
3938 err = copy_one_line(f2, NULL, rejectfile);
3939 else
3940 err = skip_one_line(f2);
3941 if (err)
3942 return err;
3943 (*line_cur2)++;
3945 /* Copy patched lines. */
3946 while (!feof(f2) && *line_cur2 <= end_new) {
3947 err = copy_one_line(f2, outfile, NULL);
3948 if (err)
3949 return err;
3950 (*line_cur2)++;
3952 /* Skip over old file's replaced lines. */
3953 while (!feof(f1) && *line_cur1 <= end_old) {
3954 if (rejectfile)
3955 err = copy_one_line(f1, NULL, rejectfile);
3956 else
3957 err = skip_one_line(f1);
3958 if (err)
3959 return err;
3960 (*line_cur1)++;
3963 return NULL;
3966 static const struct got_error *
3967 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3968 FILE *outfile, FILE *rejectfile)
3970 const struct got_error *err;
3972 if (outfile) {
3973 /* Copy old file's lines until EOF. */
3974 while (!feof(f1)) {
3975 err = copy_one_line(f1, outfile, NULL);
3976 if (err)
3977 return err;
3978 (*line_cur1)++;
3981 if (rejectfile) {
3982 /* Copy new file's lines until EOF. */
3983 while (!feof(f2)) {
3984 err = copy_one_line(f2, NULL, rejectfile);
3985 if (err)
3986 return err;
3987 (*line_cur2)++;
3991 return NULL;
3994 static const struct got_error *
3995 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3996 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3997 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3998 int *line_cur2, FILE *outfile, FILE *rejectfile,
3999 got_worktree_patch_cb patch_cb, void *patch_arg)
4001 const struct got_error *err = NULL;
4002 int start_old = change->cv.a;
4003 int end_old = change->cv.b;
4004 int start_new = change->cv.c;
4005 int end_new = change->cv.d;
4006 long pos1, pos2;
4007 FILE *hunkfile;
4009 *choice = GOT_PATCH_CHOICE_NONE;
4011 hunkfile = got_opentemp();
4012 if (hunkfile == NULL)
4013 return got_error_from_errno("got_opentemp");
4015 pos1 = ftell(f1);
4016 pos2 = ftell(f2);
4018 /* XXX TODO needs error checking */
4019 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4021 if (fseek(f1, pos1, SEEK_SET) == -1) {
4022 err = got_ferror(f1, GOT_ERR_IO);
4023 goto done;
4025 if (fseek(f2, pos2, SEEK_SET) == -1) {
4026 err = got_ferror(f1, GOT_ERR_IO);
4027 goto done;
4029 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4030 err = got_ferror(hunkfile, GOT_ERR_IO);
4031 goto done;
4034 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4035 hunkfile, n, nchanges);
4036 if (err)
4037 goto done;
4039 switch (*choice) {
4040 case GOT_PATCH_CHOICE_YES:
4041 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4042 end_old, start_new, end_new, outfile, rejectfile);
4043 break;
4044 case GOT_PATCH_CHOICE_NO:
4045 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4046 end_old, start_new, end_new, rejectfile, outfile);
4047 break;
4048 case GOT_PATCH_CHOICE_QUIT:
4049 break;
4050 default:
4051 err = got_error(GOT_ERR_PATCH_CHOICE);
4052 break;
4054 done:
4055 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4056 err = got_error_from_errno("fclose");
4057 return err;
4060 struct revert_file_args {
4061 struct got_worktree *worktree;
4062 struct got_fileindex *fileindex;
4063 got_worktree_checkout_cb progress_cb;
4064 void *progress_arg;
4065 got_worktree_patch_cb patch_cb;
4066 void *patch_arg;
4067 struct got_repository *repo;
4070 static const struct got_error *
4071 create_patched_content(char **path_outfile, int reverse_patch,
4072 struct got_object_id *blob_id, const char *path2,
4073 int dirfd2, const char *de_name2,
4074 const char *relpath, struct got_repository *repo,
4075 got_worktree_patch_cb patch_cb, void *patch_arg)
4077 const struct got_error *err;
4078 struct got_blob_object *blob = NULL;
4079 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4080 int fd2 = -1;
4081 char link_target[PATH_MAX];
4082 ssize_t link_len = 0;
4083 char *path1 = NULL, *id_str = NULL;
4084 struct stat sb1, sb2;
4085 struct got_diff_changes *changes = NULL;
4086 struct got_diff_state *ds = NULL;
4087 struct got_diff_args *args = NULL;
4088 struct got_diff_change *change;
4089 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4090 int n = 0;
4092 *path_outfile = NULL;
4094 err = got_object_id_str(&id_str, blob_id);
4095 if (err)
4096 return err;
4098 if (dirfd2 != -1) {
4099 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4100 if (fd2 == -1) {
4101 if (errno != ELOOP) {
4102 err = got_error_from_errno2("openat", path2);
4103 goto done;
4105 link_len = readlinkat(dirfd2, de_name2,
4106 link_target, sizeof(link_target));
4107 if (link_len == -1)
4108 return got_error_from_errno2("readlinkat", path2);
4109 sb2.st_mode = S_IFLNK;
4110 sb2.st_size = link_len;
4112 } else {
4113 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4114 if (fd2 == -1) {
4115 if (errno != ELOOP) {
4116 err = got_error_from_errno2("open", path2);
4117 goto done;
4119 link_len = readlink(path2, link_target,
4120 sizeof(link_target));
4121 if (link_len == -1)
4122 return got_error_from_errno2("readlink", path2);
4123 sb2.st_mode = S_IFLNK;
4124 sb2.st_size = link_len;
4127 if (fd2 != -1) {
4128 if (fstat(fd2, &sb2) == -1) {
4129 err = got_error_from_errno2("fstat", path2);
4130 goto done;
4133 f2 = fdopen(fd2, "r");
4134 if (f2 == NULL) {
4135 err = got_error_from_errno2("fdopen", path2);
4136 goto done;
4138 fd2 = -1;
4139 } else {
4140 size_t n;
4141 f2 = got_opentemp();
4142 if (f2 == NULL) {
4143 err = got_error_from_errno2("got_opentemp", path2);
4144 goto done;
4146 n = fwrite(link_target, 1, link_len, f2);
4147 if (n != link_len) {
4148 err = got_ferror(f2, GOT_ERR_IO);
4149 goto done;
4151 if (fflush(f2) == EOF) {
4152 err = got_error_from_errno("fflush");
4153 goto done;
4155 rewind(f2);
4158 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4159 if (err)
4160 goto done;
4162 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4163 if (err)
4164 goto done;
4166 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4167 if (err)
4168 goto done;
4170 if (stat(path1, &sb1) == -1) {
4171 err = got_error_from_errno2("stat", path1);
4172 goto done;
4175 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4176 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4177 if (err)
4178 goto done;
4180 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4181 if (err)
4182 goto done;
4184 if (fseek(f1, 0L, SEEK_SET) == -1)
4185 return got_ferror(f1, GOT_ERR_IO);
4186 if (fseek(f2, 0L, SEEK_SET) == -1)
4187 return got_ferror(f2, GOT_ERR_IO);
4188 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4189 int choice;
4190 err = apply_or_reject_change(&choice, change, ++n,
4191 changes->nchanges, ds, args, diff_flags, relpath,
4192 f1, f2, &line_cur1, &line_cur2,
4193 reverse_patch ? NULL : outfile,
4194 reverse_patch ? outfile : NULL,
4195 patch_cb, patch_arg);
4196 if (err)
4197 goto done;
4198 if (choice == GOT_PATCH_CHOICE_YES)
4199 have_content = 1;
4200 else if (choice == GOT_PATCH_CHOICE_QUIT)
4201 break;
4203 if (have_content) {
4204 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4205 reverse_patch ? NULL : outfile,
4206 reverse_patch ? outfile : NULL);
4207 if (err)
4208 goto done;
4210 if (!S_ISLNK(sb2.st_mode)) {
4211 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4212 err = got_error_from_errno2("chmod", path2);
4213 goto done;
4217 done:
4218 free(id_str);
4219 if (blob)
4220 got_object_blob_close(blob);
4221 if (f1 && fclose(f1) == EOF && err == NULL)
4222 err = got_error_from_errno2("fclose", path1);
4223 if (f2 && fclose(f2) == EOF && err == NULL)
4224 err = got_error_from_errno2("fclose", path2);
4225 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4226 err = got_error_from_errno2("close", path2);
4227 if (outfile && fclose(outfile) == EOF && err == NULL)
4228 err = got_error_from_errno2("fclose", *path_outfile);
4229 if (path1 && unlink(path1) == -1 && err == NULL)
4230 err = got_error_from_errno2("unlink", path1);
4231 if (err || !have_content) {
4232 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4233 err = got_error_from_errno2("unlink", *path_outfile);
4234 free(*path_outfile);
4235 *path_outfile = NULL;
4237 free(args);
4238 if (ds) {
4239 got_diff_state_free(ds);
4240 free(ds);
4242 if (changes)
4243 got_diff_free_changes(changes);
4244 free(path1);
4245 return err;
4248 static const struct got_error *
4249 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4250 const char *relpath, struct got_object_id *blob_id,
4251 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4252 int dirfd, const char *de_name)
4254 struct revert_file_args *a = arg;
4255 const struct got_error *err = NULL;
4256 char *parent_path = NULL;
4257 struct got_fileindex_entry *ie;
4258 struct got_tree_object *tree = NULL;
4259 struct got_object_id *tree_id = NULL;
4260 const struct got_tree_entry *te = NULL;
4261 char *tree_path = NULL, *te_name;
4262 char *ondisk_path = NULL, *path_content = NULL;
4263 struct got_blob_object *blob = NULL;
4265 /* Reverting a staged deletion is a no-op. */
4266 if (status == GOT_STATUS_DELETE &&
4267 staged_status != GOT_STATUS_NO_CHANGE)
4268 return NULL;
4270 if (status == GOT_STATUS_UNVERSIONED)
4271 return (*a->progress_cb)(a->progress_arg,
4272 GOT_STATUS_UNVERSIONED, relpath);
4274 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4275 if (ie == NULL)
4276 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4278 /* Construct in-repository path of tree which contains this blob. */
4279 err = got_path_dirname(&parent_path, ie->path);
4280 if (err) {
4281 if (err->code != GOT_ERR_BAD_PATH)
4282 goto done;
4283 parent_path = strdup("/");
4284 if (parent_path == NULL) {
4285 err = got_error_from_errno("strdup");
4286 goto done;
4289 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4290 tree_path = strdup(parent_path);
4291 if (tree_path == NULL) {
4292 err = got_error_from_errno("strdup");
4293 goto done;
4295 } else {
4296 if (got_path_is_root_dir(parent_path)) {
4297 tree_path = strdup(a->worktree->path_prefix);
4298 if (tree_path == NULL) {
4299 err = got_error_from_errno("strdup");
4300 goto done;
4302 } else {
4303 if (asprintf(&tree_path, "%s/%s",
4304 a->worktree->path_prefix, parent_path) == -1) {
4305 err = got_error_from_errno("asprintf");
4306 goto done;
4311 err = got_object_id_by_path(&tree_id, a->repo,
4312 a->worktree->base_commit_id, tree_path);
4313 if (err) {
4314 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4315 (status == GOT_STATUS_ADD ||
4316 staged_status == GOT_STATUS_ADD)))
4317 goto done;
4318 } else {
4319 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4320 if (err)
4321 goto done;
4323 te_name = basename(ie->path);
4324 if (te_name == NULL) {
4325 err = got_error_from_errno2("basename", ie->path);
4326 goto done;
4329 te = got_object_tree_find_entry(tree, te_name);
4330 if (te == NULL && status != GOT_STATUS_ADD &&
4331 staged_status != GOT_STATUS_ADD) {
4332 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4333 goto done;
4337 switch (status) {
4338 case GOT_STATUS_ADD:
4339 if (a->patch_cb) {
4340 int choice = GOT_PATCH_CHOICE_NONE;
4341 err = (*a->patch_cb)(&choice, a->patch_arg,
4342 status, ie->path, NULL, 1, 1);
4343 if (err)
4344 goto done;
4345 if (choice != GOT_PATCH_CHOICE_YES)
4346 break;
4348 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4349 ie->path);
4350 if (err)
4351 goto done;
4352 got_fileindex_entry_remove(a->fileindex, ie);
4353 break;
4354 case GOT_STATUS_DELETE:
4355 if (a->patch_cb) {
4356 int choice = GOT_PATCH_CHOICE_NONE;
4357 err = (*a->patch_cb)(&choice, a->patch_arg,
4358 status, ie->path, NULL, 1, 1);
4359 if (err)
4360 goto done;
4361 if (choice != GOT_PATCH_CHOICE_YES)
4362 break;
4364 /* fall through */
4365 case GOT_STATUS_MODIFY:
4366 case GOT_STATUS_MODE_CHANGE:
4367 case GOT_STATUS_CONFLICT:
4368 case GOT_STATUS_MISSING: {
4369 struct got_object_id id;
4370 if (staged_status == GOT_STATUS_ADD ||
4371 staged_status == GOT_STATUS_MODIFY) {
4372 memcpy(id.sha1, ie->staged_blob_sha1,
4373 SHA1_DIGEST_LENGTH);
4374 } else
4375 memcpy(id.sha1, ie->blob_sha1,
4376 SHA1_DIGEST_LENGTH);
4377 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4378 if (err)
4379 goto done;
4381 if (asprintf(&ondisk_path, "%s/%s",
4382 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4383 err = got_error_from_errno("asprintf");
4384 goto done;
4387 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4388 status == GOT_STATUS_CONFLICT)) {
4389 int is_bad_symlink = 0;
4390 err = create_patched_content(&path_content, 1, &id,
4391 ondisk_path, dirfd, de_name, ie->path, a->repo,
4392 a->patch_cb, a->patch_arg);
4393 if (err || path_content == NULL)
4394 break;
4395 if (te && S_ISLNK(te->mode)) {
4396 if (unlink(path_content) == -1) {
4397 err = got_error_from_errno2("unlink",
4398 path_content);
4399 break;
4401 err = install_symlink(&is_bad_symlink,
4402 a->worktree, ondisk_path, ie->path,
4403 blob, 0, 1, 0, a->repo,
4404 a->progress_cb, a->progress_arg);
4405 } else {
4406 if (rename(path_content, ondisk_path) == -1) {
4407 err = got_error_from_errno3("rename",
4408 path_content, ondisk_path);
4409 goto done;
4412 } else {
4413 int is_bad_symlink = 0;
4414 if (te && S_ISLNK(te->mode)) {
4415 err = install_symlink(&is_bad_symlink,
4416 a->worktree, ondisk_path, ie->path,
4417 blob, 0, 1, 0, a->repo,
4418 a->progress_cb, a->progress_arg);
4419 } else {
4420 err = install_blob(a->worktree, ondisk_path,
4421 ie->path,
4422 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4423 got_fileindex_perms_to_st(ie), blob,
4424 0, 1, 0, 0, a->repo,
4425 a->progress_cb, a->progress_arg);
4427 if (err)
4428 goto done;
4429 if (status == GOT_STATUS_DELETE ||
4430 status == GOT_STATUS_MODE_CHANGE) {
4431 err = got_fileindex_entry_update(ie,
4432 ondisk_path, blob->id.sha1,
4433 a->worktree->base_commit_id->sha1, 1);
4434 if (err)
4435 goto done;
4437 if (is_bad_symlink) {
4438 got_fileindex_entry_filetype_set(ie,
4439 GOT_FILEIDX_MODE_BAD_SYMLINK);
4442 break;
4444 default:
4445 break;
4447 done:
4448 free(ondisk_path);
4449 free(path_content);
4450 free(parent_path);
4451 free(tree_path);
4452 if (blob)
4453 got_object_blob_close(blob);
4454 if (tree)
4455 got_object_tree_close(tree);
4456 free(tree_id);
4457 return err;
4460 const struct got_error *
4461 got_worktree_revert(struct got_worktree *worktree,
4462 struct got_pathlist_head *paths,
4463 got_worktree_checkout_cb progress_cb, void *progress_arg,
4464 got_worktree_patch_cb patch_cb, void *patch_arg,
4465 struct got_repository *repo)
4467 struct got_fileindex *fileindex = NULL;
4468 char *fileindex_path = NULL;
4469 const struct got_error *err = NULL, *unlockerr = NULL;
4470 const struct got_error *sync_err = NULL;
4471 struct got_pathlist_entry *pe;
4472 struct revert_file_args rfa;
4474 err = lock_worktree(worktree, LOCK_EX);
4475 if (err)
4476 return err;
4478 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4479 if (err)
4480 goto done;
4482 rfa.worktree = worktree;
4483 rfa.fileindex = fileindex;
4484 rfa.progress_cb = progress_cb;
4485 rfa.progress_arg = progress_arg;
4486 rfa.patch_cb = patch_cb;
4487 rfa.patch_arg = patch_arg;
4488 rfa.repo = repo;
4489 TAILQ_FOREACH(pe, paths, entry) {
4490 err = worktree_status(worktree, pe->path, fileindex, repo,
4491 revert_file, &rfa, NULL, NULL, 0, 0);
4492 if (err)
4493 break;
4495 sync_err = sync_fileindex(fileindex, fileindex_path);
4496 if (sync_err && err == NULL)
4497 err = sync_err;
4498 done:
4499 free(fileindex_path);
4500 if (fileindex)
4501 got_fileindex_free(fileindex);
4502 unlockerr = lock_worktree(worktree, LOCK_SH);
4503 if (unlockerr && err == NULL)
4504 err = unlockerr;
4505 return err;
4508 static void
4509 free_commitable(struct got_commitable *ct)
4511 free(ct->path);
4512 free(ct->in_repo_path);
4513 free(ct->ondisk_path);
4514 free(ct->blob_id);
4515 free(ct->base_blob_id);
4516 free(ct->staged_blob_id);
4517 free(ct->base_commit_id);
4518 free(ct);
4521 struct collect_commitables_arg {
4522 struct got_pathlist_head *commitable_paths;
4523 struct got_repository *repo;
4524 struct got_worktree *worktree;
4525 struct got_fileindex *fileindex;
4526 int have_staged_files;
4527 int allow_bad_symlinks;
4530 static const struct got_error *
4531 collect_commitables(void *arg, unsigned char status,
4532 unsigned char staged_status, const char *relpath,
4533 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4534 struct got_object_id *commit_id, int dirfd, const char *de_name)
4536 struct collect_commitables_arg *a = arg;
4537 const struct got_error *err = NULL;
4538 struct got_commitable *ct = NULL;
4539 struct got_pathlist_entry *new = NULL;
4540 char *parent_path = NULL, *path = NULL;
4541 struct stat sb;
4543 if (a->have_staged_files) {
4544 if (staged_status != GOT_STATUS_MODIFY &&
4545 staged_status != GOT_STATUS_ADD &&
4546 staged_status != GOT_STATUS_DELETE)
4547 return NULL;
4548 } else {
4549 if (status == GOT_STATUS_CONFLICT)
4550 return got_error(GOT_ERR_COMMIT_CONFLICT);
4552 if (status != GOT_STATUS_MODIFY &&
4553 status != GOT_STATUS_MODE_CHANGE &&
4554 status != GOT_STATUS_ADD &&
4555 status != GOT_STATUS_DELETE)
4556 return NULL;
4559 if (asprintf(&path, "/%s", relpath) == -1) {
4560 err = got_error_from_errno("asprintf");
4561 goto done;
4563 if (strcmp(path, "/") == 0) {
4564 parent_path = strdup("");
4565 if (parent_path == NULL)
4566 return got_error_from_errno("strdup");
4567 } else {
4568 err = got_path_dirname(&parent_path, path);
4569 if (err)
4570 return err;
4573 ct = calloc(1, sizeof(*ct));
4574 if (ct == NULL) {
4575 err = got_error_from_errno("calloc");
4576 goto done;
4579 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4580 relpath) == -1) {
4581 err = got_error_from_errno("asprintf");
4582 goto done;
4585 if (staged_status == GOT_STATUS_ADD ||
4586 staged_status == GOT_STATUS_MODIFY) {
4587 struct got_fileindex_entry *ie;
4588 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4589 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4590 case GOT_FILEIDX_MODE_REGULAR_FILE:
4591 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4592 ct->mode = S_IFREG;
4593 break;
4594 case GOT_FILEIDX_MODE_SYMLINK:
4595 ct->mode = S_IFLNK;
4596 break;
4597 default:
4598 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4599 goto done;
4601 ct->mode |= got_fileindex_entry_perms_get(ie);
4602 } else if (status != GOT_STATUS_DELETE &&
4603 staged_status != GOT_STATUS_DELETE) {
4604 if (dirfd != -1) {
4605 if (fstatat(dirfd, de_name, &sb,
4606 AT_SYMLINK_NOFOLLOW) == -1) {
4607 err = got_error_from_errno2("fstatat",
4608 ct->ondisk_path);
4609 goto done;
4611 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4612 err = got_error_from_errno2("lstat", ct->ondisk_path);
4613 goto done;
4615 ct->mode = sb.st_mode;
4618 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4619 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4620 relpath) == -1) {
4621 err = got_error_from_errno("asprintf");
4622 goto done;
4625 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4626 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4627 int is_bad_symlink;
4628 char target_path[PATH_MAX];
4629 ssize_t target_len;
4630 target_len = readlink(ct->ondisk_path, target_path,
4631 sizeof(target_path));
4632 if (target_len == -1) {
4633 err = got_error_from_errno2("readlink",
4634 ct->ondisk_path);
4635 goto done;
4637 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4638 target_len, ct->ondisk_path, a->worktree->root_path);
4639 if (err)
4640 goto done;
4641 if (is_bad_symlink) {
4642 err = got_error_path(ct->ondisk_path,
4643 GOT_ERR_BAD_SYMLINK);
4644 goto done;
4649 ct->status = status;
4650 ct->staged_status = staged_status;
4651 ct->blob_id = NULL; /* will be filled in when blob gets created */
4652 if (ct->status != GOT_STATUS_ADD &&
4653 ct->staged_status != GOT_STATUS_ADD) {
4654 ct->base_blob_id = got_object_id_dup(blob_id);
4655 if (ct->base_blob_id == NULL) {
4656 err = got_error_from_errno("got_object_id_dup");
4657 goto done;
4659 ct->base_commit_id = got_object_id_dup(commit_id);
4660 if (ct->base_commit_id == NULL) {
4661 err = got_error_from_errno("got_object_id_dup");
4662 goto done;
4665 if (ct->staged_status == GOT_STATUS_ADD ||
4666 ct->staged_status == GOT_STATUS_MODIFY) {
4667 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4668 if (ct->staged_blob_id == NULL) {
4669 err = got_error_from_errno("got_object_id_dup");
4670 goto done;
4673 ct->path = strdup(path);
4674 if (ct->path == NULL) {
4675 err = got_error_from_errno("strdup");
4676 goto done;
4678 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4679 done:
4680 if (ct && (err || new == NULL))
4681 free_commitable(ct);
4682 free(parent_path);
4683 free(path);
4684 return err;
4687 static const struct got_error *write_tree(struct got_object_id **, int *,
4688 struct got_tree_object *, const char *, struct got_pathlist_head *,
4689 got_worktree_status_cb status_cb, void *status_arg,
4690 struct got_repository *);
4692 static const struct got_error *
4693 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4694 struct got_tree_entry *te, const char *parent_path,
4695 struct got_pathlist_head *commitable_paths,
4696 got_worktree_status_cb status_cb, void *status_arg,
4697 struct got_repository *repo)
4699 const struct got_error *err = NULL;
4700 struct got_tree_object *subtree;
4701 char *subpath;
4703 if (asprintf(&subpath, "%s%s%s", parent_path,
4704 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4705 return got_error_from_errno("asprintf");
4707 err = got_object_open_as_tree(&subtree, repo, &te->id);
4708 if (err)
4709 return err;
4711 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4712 commitable_paths, status_cb, status_arg, repo);
4713 got_object_tree_close(subtree);
4714 free(subpath);
4715 return err;
4718 static const struct got_error *
4719 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4721 const struct got_error *err = NULL;
4722 char *ct_parent_path = NULL;
4724 *match = 0;
4726 if (strchr(ct->in_repo_path, '/') == NULL) {
4727 *match = got_path_is_root_dir(path);
4728 return NULL;
4731 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4732 if (err)
4733 return err;
4734 *match = (strcmp(path, ct_parent_path) == 0);
4735 free(ct_parent_path);
4736 return err;
4739 static mode_t
4740 get_ct_file_mode(struct got_commitable *ct)
4742 if (S_ISLNK(ct->mode))
4743 return S_IFLNK;
4745 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4748 static const struct got_error *
4749 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4750 struct got_tree_entry *te, struct got_commitable *ct)
4752 const struct got_error *err = NULL;
4754 *new_te = NULL;
4756 err = got_object_tree_entry_dup(new_te, te);
4757 if (err)
4758 goto done;
4760 (*new_te)->mode = get_ct_file_mode(ct);
4762 if (ct->staged_status == GOT_STATUS_MODIFY)
4763 memcpy(&(*new_te)->id, ct->staged_blob_id,
4764 sizeof((*new_te)->id));
4765 else
4766 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4767 done:
4768 if (err && *new_te) {
4769 free(*new_te);
4770 *new_te = NULL;
4772 return err;
4775 static const struct got_error *
4776 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4777 struct got_commitable *ct)
4779 const struct got_error *err = NULL;
4780 char *ct_name;
4782 *new_te = NULL;
4784 *new_te = calloc(1, sizeof(**new_te));
4785 if (*new_te == NULL)
4786 return got_error_from_errno("calloc");
4788 ct_name = basename(ct->path);
4789 if (ct_name == NULL) {
4790 err = got_error_from_errno2("basename", ct->path);
4791 goto done;
4793 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4794 sizeof((*new_te)->name)) {
4795 err = got_error(GOT_ERR_NO_SPACE);
4796 goto done;
4799 (*new_te)->mode = get_ct_file_mode(ct);
4801 if (ct->staged_status == GOT_STATUS_ADD)
4802 memcpy(&(*new_te)->id, ct->staged_blob_id,
4803 sizeof((*new_te)->id));
4804 else
4805 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4806 done:
4807 if (err && *new_te) {
4808 free(*new_te);
4809 *new_te = NULL;
4811 return err;
4814 static const struct got_error *
4815 insert_tree_entry(struct got_tree_entry *new_te,
4816 struct got_pathlist_head *paths)
4818 const struct got_error *err = NULL;
4819 struct got_pathlist_entry *new_pe;
4821 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4822 if (err)
4823 return err;
4824 if (new_pe == NULL)
4825 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4826 return NULL;
4829 static const struct got_error *
4830 report_ct_status(struct got_commitable *ct,
4831 got_worktree_status_cb status_cb, void *status_arg)
4833 const char *ct_path = ct->path;
4834 unsigned char status;
4836 while (ct_path[0] == '/')
4837 ct_path++;
4839 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4840 status = ct->staged_status;
4841 else
4842 status = ct->status;
4844 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4845 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4848 static const struct got_error *
4849 match_modified_subtree(int *modified, struct got_tree_entry *te,
4850 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4852 const struct got_error *err = NULL;
4853 struct got_pathlist_entry *pe;
4854 char *te_path;
4856 *modified = 0;
4858 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4859 got_path_is_root_dir(base_tree_path) ? "" : "/",
4860 te->name) == -1)
4861 return got_error_from_errno("asprintf");
4863 TAILQ_FOREACH(pe, commitable_paths, entry) {
4864 struct got_commitable *ct = pe->data;
4865 *modified = got_path_is_child(ct->in_repo_path, te_path,
4866 strlen(te_path));
4867 if (*modified)
4868 break;
4871 free(te_path);
4872 return err;
4875 static const struct got_error *
4876 match_deleted_or_modified_ct(struct got_commitable **ctp,
4877 struct got_tree_entry *te, const char *base_tree_path,
4878 struct got_pathlist_head *commitable_paths)
4880 const struct got_error *err = NULL;
4881 struct got_pathlist_entry *pe;
4883 *ctp = NULL;
4885 TAILQ_FOREACH(pe, commitable_paths, entry) {
4886 struct got_commitable *ct = pe->data;
4887 char *ct_name = NULL;
4888 int path_matches;
4890 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4891 if (ct->status != GOT_STATUS_MODIFY &&
4892 ct->status != GOT_STATUS_MODE_CHANGE &&
4893 ct->status != GOT_STATUS_DELETE)
4894 continue;
4895 } else {
4896 if (ct->staged_status != GOT_STATUS_MODIFY &&
4897 ct->staged_status != GOT_STATUS_DELETE)
4898 continue;
4901 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4902 continue;
4904 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4905 if (err)
4906 return err;
4907 if (!path_matches)
4908 continue;
4910 ct_name = basename(pe->path);
4911 if (ct_name == NULL)
4912 return got_error_from_errno2("basename", pe->path);
4914 if (strcmp(te->name, ct_name) != 0)
4915 continue;
4917 *ctp = ct;
4918 break;
4921 return err;
4924 static const struct got_error *
4925 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4926 const char *child_path, const char *path_base_tree,
4927 struct got_pathlist_head *commitable_paths,
4928 got_worktree_status_cb status_cb, void *status_arg,
4929 struct got_repository *repo)
4931 const struct got_error *err = NULL;
4932 struct got_tree_entry *new_te;
4933 char *subtree_path;
4934 struct got_object_id *id = NULL;
4935 int nentries;
4937 *new_tep = NULL;
4939 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4940 got_path_is_root_dir(path_base_tree) ? "" : "/",
4941 child_path) == -1)
4942 return got_error_from_errno("asprintf");
4944 new_te = calloc(1, sizeof(*new_te));
4945 if (new_te == NULL)
4946 return got_error_from_errno("calloc");
4947 new_te->mode = S_IFDIR;
4949 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4950 sizeof(new_te->name)) {
4951 err = got_error(GOT_ERR_NO_SPACE);
4952 goto done;
4954 err = write_tree(&id, &nentries, NULL, subtree_path,
4955 commitable_paths, status_cb, status_arg, repo);
4956 if (err) {
4957 free(new_te);
4958 goto done;
4960 memcpy(&new_te->id, id, sizeof(new_te->id));
4961 done:
4962 free(id);
4963 free(subtree_path);
4964 if (err == NULL)
4965 *new_tep = new_te;
4966 return err;
4969 static const struct got_error *
4970 write_tree(struct got_object_id **new_tree_id, int *nentries,
4971 struct got_tree_object *base_tree, const char *path_base_tree,
4972 struct got_pathlist_head *commitable_paths,
4973 got_worktree_status_cb status_cb, void *status_arg,
4974 struct got_repository *repo)
4976 const struct got_error *err = NULL;
4977 struct got_pathlist_head paths;
4978 struct got_tree_entry *te, *new_te = NULL;
4979 struct got_pathlist_entry *pe;
4981 TAILQ_INIT(&paths);
4982 *nentries = 0;
4984 /* Insert, and recurse into, newly added entries first. */
4985 TAILQ_FOREACH(pe, commitable_paths, entry) {
4986 struct got_commitable *ct = pe->data;
4987 char *child_path = NULL, *slash;
4989 if ((ct->status != GOT_STATUS_ADD &&
4990 ct->staged_status != GOT_STATUS_ADD) ||
4991 (ct->flags & GOT_COMMITABLE_ADDED))
4992 continue;
4994 if (!got_path_is_child(pe->path, path_base_tree,
4995 strlen(path_base_tree)))
4996 continue;
4998 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4999 pe->path);
5000 if (err)
5001 goto done;
5003 slash = strchr(child_path, '/');
5004 if (slash == NULL) {
5005 err = alloc_added_blob_tree_entry(&new_te, ct);
5006 if (err)
5007 goto done;
5008 err = report_ct_status(ct, status_cb, status_arg);
5009 if (err)
5010 goto done;
5011 ct->flags |= GOT_COMMITABLE_ADDED;
5012 err = insert_tree_entry(new_te, &paths);
5013 if (err)
5014 goto done;
5015 (*nentries)++;
5016 } else {
5017 *slash = '\0'; /* trim trailing path components */
5018 if (base_tree == NULL ||
5019 got_object_tree_find_entry(base_tree, child_path)
5020 == NULL) {
5021 err = make_subtree_for_added_blob(&new_te,
5022 child_path, path_base_tree,
5023 commitable_paths, status_cb, status_arg,
5024 repo);
5025 if (err)
5026 goto done;
5027 err = insert_tree_entry(new_te, &paths);
5028 if (err)
5029 goto done;
5030 (*nentries)++;
5035 if (base_tree) {
5036 int i, nbase_entries;
5037 /* Handle modified and deleted entries. */
5038 nbase_entries = got_object_tree_get_nentries(base_tree);
5039 for (i = 0; i < nbase_entries; i++) {
5040 struct got_commitable *ct = NULL;
5042 te = got_object_tree_get_entry(base_tree, i);
5043 if (got_object_tree_entry_is_submodule(te)) {
5044 /* Entry is a submodule; just copy it. */
5045 err = got_object_tree_entry_dup(&new_te, te);
5046 if (err)
5047 goto done;
5048 err = insert_tree_entry(new_te, &paths);
5049 if (err)
5050 goto done;
5051 (*nentries)++;
5052 continue;
5055 if (S_ISDIR(te->mode)) {
5056 int modified;
5057 err = got_object_tree_entry_dup(&new_te, te);
5058 if (err)
5059 goto done;
5060 err = match_modified_subtree(&modified, te,
5061 path_base_tree, commitable_paths);
5062 if (err)
5063 goto done;
5064 /* Avoid recursion into unmodified subtrees. */
5065 if (modified) {
5066 struct got_object_id *new_id;
5067 int nsubentries;
5068 err = write_subtree(&new_id,
5069 &nsubentries, te,
5070 path_base_tree, commitable_paths,
5071 status_cb, status_arg, repo);
5072 if (err)
5073 goto done;
5074 if (nsubentries == 0) {
5075 /* All entries were deleted. */
5076 free(new_id);
5077 continue;
5079 memcpy(&new_te->id, new_id,
5080 sizeof(new_te->id));
5081 free(new_id);
5083 err = insert_tree_entry(new_te, &paths);
5084 if (err)
5085 goto done;
5086 (*nentries)++;
5087 continue;
5090 err = match_deleted_or_modified_ct(&ct, te,
5091 path_base_tree, commitable_paths);
5092 if (err)
5093 goto done;
5094 if (ct) {
5095 /* NB: Deleted entries get dropped here. */
5096 if (ct->status == GOT_STATUS_MODIFY ||
5097 ct->status == GOT_STATUS_MODE_CHANGE ||
5098 ct->staged_status == GOT_STATUS_MODIFY) {
5099 err = alloc_modified_blob_tree_entry(
5100 &new_te, te, ct);
5101 if (err)
5102 goto done;
5103 err = insert_tree_entry(new_te, &paths);
5104 if (err)
5105 goto done;
5106 (*nentries)++;
5108 err = report_ct_status(ct, status_cb,
5109 status_arg);
5110 if (err)
5111 goto done;
5112 } else {
5113 /* Entry is unchanged; just copy it. */
5114 err = got_object_tree_entry_dup(&new_te, te);
5115 if (err)
5116 goto done;
5117 err = insert_tree_entry(new_te, &paths);
5118 if (err)
5119 goto done;
5120 (*nentries)++;
5125 /* Write new list of entries; deleted entries have been dropped. */
5126 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5127 done:
5128 got_pathlist_free(&paths);
5129 return err;
5132 static const struct got_error *
5133 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5134 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5135 int have_staged_files)
5137 const struct got_error *err = NULL;
5138 struct got_pathlist_entry *pe;
5140 TAILQ_FOREACH(pe, commitable_paths, entry) {
5141 struct got_fileindex_entry *ie;
5142 struct got_commitable *ct = pe->data;
5144 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5145 if (ie) {
5146 if (ct->status == GOT_STATUS_DELETE ||
5147 ct->staged_status == GOT_STATUS_DELETE) {
5148 got_fileindex_entry_remove(fileindex, ie);
5149 } else if (ct->staged_status == GOT_STATUS_ADD ||
5150 ct->staged_status == GOT_STATUS_MODIFY) {
5151 got_fileindex_entry_stage_set(ie,
5152 GOT_FILEIDX_STAGE_NONE);
5153 err = got_fileindex_entry_update(ie,
5154 ct->ondisk_path, ct->staged_blob_id->sha1,
5155 new_base_commit_id->sha1,
5156 !have_staged_files);
5157 } else
5158 err = got_fileindex_entry_update(ie,
5159 ct->ondisk_path, ct->blob_id->sha1,
5160 new_base_commit_id->sha1,
5161 !have_staged_files);
5162 } else {
5163 err = got_fileindex_entry_alloc(&ie, pe->path);
5164 if (err)
5165 break;
5166 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5167 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5168 if (err) {
5169 got_fileindex_entry_free(ie);
5170 break;
5172 err = got_fileindex_entry_add(fileindex, ie);
5173 if (err) {
5174 got_fileindex_entry_free(ie);
5175 break;
5179 return err;
5183 static const struct got_error *
5184 check_out_of_date(const char *in_repo_path, unsigned char status,
5185 unsigned char staged_status, struct got_object_id *base_blob_id,
5186 struct got_object_id *base_commit_id,
5187 struct got_object_id *head_commit_id, struct got_repository *repo,
5188 int ood_errcode)
5190 const struct got_error *err = NULL;
5191 struct got_object_id *id = NULL;
5193 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5194 /* Trivial case: base commit == head commit */
5195 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5196 return NULL;
5198 * Ensure file content which local changes were based
5199 * on matches file content in the branch head.
5201 err = got_object_id_by_path(&id, repo, head_commit_id,
5202 in_repo_path);
5203 if (err) {
5204 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5205 err = got_error(ood_errcode);
5206 goto done;
5207 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5208 err = got_error(ood_errcode);
5209 } else {
5210 /* Require that added files don't exist in the branch head. */
5211 err = got_object_id_by_path(&id, repo, head_commit_id,
5212 in_repo_path);
5213 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5214 goto done;
5215 err = id ? got_error(ood_errcode) : NULL;
5217 done:
5218 free(id);
5219 return err;
5222 const struct got_error *
5223 commit_worktree(struct got_object_id **new_commit_id,
5224 struct got_pathlist_head *commitable_paths,
5225 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5226 const char *author, const char *committer,
5227 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5228 got_worktree_status_cb status_cb, void *status_arg,
5229 struct got_repository *repo)
5231 const struct got_error *err = NULL, *unlockerr = NULL;
5232 struct got_pathlist_entry *pe;
5233 const char *head_ref_name = NULL;
5234 struct got_commit_object *head_commit = NULL;
5235 struct got_reference *head_ref2 = NULL;
5236 struct got_object_id *head_commit_id2 = NULL;
5237 struct got_tree_object *head_tree = NULL;
5238 struct got_object_id *new_tree_id = NULL;
5239 int nentries;
5240 struct got_object_id_queue parent_ids;
5241 struct got_object_qid *pid = NULL;
5242 char *logmsg = NULL;
5244 *new_commit_id = NULL;
5246 SIMPLEQ_INIT(&parent_ids);
5248 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5249 if (err)
5250 goto done;
5252 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5253 if (err)
5254 goto done;
5256 if (commit_msg_cb != NULL) {
5257 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5258 if (err)
5259 goto done;
5262 if (logmsg == NULL || strlen(logmsg) == 0) {
5263 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5264 goto done;
5267 /* Create blobs from added and modified files and record their IDs. */
5268 TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 struct got_commitable *ct = pe->data;
5270 char *ondisk_path;
5272 /* Blobs for staged files already exist. */
5273 if (ct->staged_status == GOT_STATUS_ADD ||
5274 ct->staged_status == GOT_STATUS_MODIFY)
5275 continue;
5277 if (ct->status != GOT_STATUS_ADD &&
5278 ct->status != GOT_STATUS_MODIFY &&
5279 ct->status != GOT_STATUS_MODE_CHANGE)
5280 continue;
5282 if (asprintf(&ondisk_path, "%s/%s",
5283 worktree->root_path, pe->path) == -1) {
5284 err = got_error_from_errno("asprintf");
5285 goto done;
5287 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5288 free(ondisk_path);
5289 if (err)
5290 goto done;
5293 /* Recursively write new tree objects. */
5294 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5295 commitable_paths, status_cb, status_arg, repo);
5296 if (err)
5297 goto done;
5299 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5300 if (err)
5301 goto done;
5302 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5303 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5304 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5305 got_object_qid_free(pid);
5306 if (logmsg != NULL)
5307 free(logmsg);
5308 if (err)
5309 goto done;
5311 /* Check if a concurrent commit to our branch has occurred. */
5312 head_ref_name = got_worktree_get_head_ref_name(worktree);
5313 if (head_ref_name == NULL) {
5314 err = got_error_from_errno("got_worktree_get_head_ref_name");
5315 goto done;
5317 /* Lock the reference here to prevent concurrent modification. */
5318 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5319 if (err)
5320 goto done;
5321 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5322 if (err)
5323 goto done;
5324 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5325 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5326 goto done;
5328 /* Update branch head in repository. */
5329 err = got_ref_change_ref(head_ref2, *new_commit_id);
5330 if (err)
5331 goto done;
5332 err = got_ref_write(head_ref2, repo);
5333 if (err)
5334 goto done;
5336 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5337 if (err)
5338 goto done;
5340 err = ref_base_commit(worktree, repo);
5341 if (err)
5342 goto done;
5343 done:
5344 if (head_tree)
5345 got_object_tree_close(head_tree);
5346 if (head_commit)
5347 got_object_commit_close(head_commit);
5348 free(head_commit_id2);
5349 if (head_ref2) {
5350 unlockerr = got_ref_unlock(head_ref2);
5351 if (unlockerr && err == NULL)
5352 err = unlockerr;
5353 got_ref_close(head_ref2);
5355 return err;
5358 static const struct got_error *
5359 check_path_is_commitable(const char *path,
5360 struct got_pathlist_head *commitable_paths)
5362 struct got_pathlist_entry *cpe = NULL;
5363 size_t path_len = strlen(path);
5365 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5366 struct got_commitable *ct = cpe->data;
5367 const char *ct_path = ct->path;
5369 while (ct_path[0] == '/')
5370 ct_path++;
5372 if (strcmp(path, ct_path) == 0 ||
5373 got_path_is_child(ct_path, path, path_len))
5374 break;
5377 if (cpe == NULL)
5378 return got_error_path(path, GOT_ERR_BAD_PATH);
5380 return NULL;
5383 static const struct got_error *
5384 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5386 int *have_staged_files = arg;
5388 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5389 *have_staged_files = 1;
5390 return got_error(GOT_ERR_CANCELLED);
5393 return NULL;
5396 static const struct got_error *
5397 check_non_staged_files(struct got_fileindex *fileindex,
5398 struct got_pathlist_head *paths)
5400 struct got_pathlist_entry *pe;
5401 struct got_fileindex_entry *ie;
5403 TAILQ_FOREACH(pe, paths, entry) {
5404 if (pe->path[0] == '\0')
5405 continue;
5406 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5407 if (ie == NULL)
5408 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5409 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5410 return got_error_path(pe->path,
5411 GOT_ERR_FILE_NOT_STAGED);
5414 return NULL;
5417 const struct got_error *
5418 got_worktree_commit(struct got_object_id **new_commit_id,
5419 struct got_worktree *worktree, struct got_pathlist_head *paths,
5420 const char *author, const char *committer, int allow_bad_symlinks,
5421 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5422 got_worktree_status_cb status_cb, void *status_arg,
5423 struct got_repository *repo)
5425 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5426 struct got_fileindex *fileindex = NULL;
5427 char *fileindex_path = NULL;
5428 struct got_pathlist_head commitable_paths;
5429 struct collect_commitables_arg cc_arg;
5430 struct got_pathlist_entry *pe;
5431 struct got_reference *head_ref = NULL;
5432 struct got_object_id *head_commit_id = NULL;
5433 int have_staged_files = 0;
5435 *new_commit_id = NULL;
5437 TAILQ_INIT(&commitable_paths);
5439 err = lock_worktree(worktree, LOCK_EX);
5440 if (err)
5441 goto done;
5443 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5444 if (err)
5445 goto done;
5447 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5448 if (err)
5449 goto done;
5451 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5452 if (err)
5453 goto done;
5455 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5456 &have_staged_files);
5457 if (err && err->code != GOT_ERR_CANCELLED)
5458 goto done;
5459 if (have_staged_files) {
5460 err = check_non_staged_files(fileindex, paths);
5461 if (err)
5462 goto done;
5465 cc_arg.commitable_paths = &commitable_paths;
5466 cc_arg.worktree = worktree;
5467 cc_arg.fileindex = fileindex;
5468 cc_arg.repo = repo;
5469 cc_arg.have_staged_files = have_staged_files;
5470 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5471 TAILQ_FOREACH(pe, paths, entry) {
5472 err = worktree_status(worktree, pe->path, fileindex, repo,
5473 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5474 if (err)
5475 goto done;
5478 if (TAILQ_EMPTY(&commitable_paths)) {
5479 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5480 goto done;
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = check_path_is_commitable(pe->path, &commitable_paths);
5485 if (err)
5486 goto done;
5489 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5490 struct got_commitable *ct = pe->data;
5491 const char *ct_path = ct->in_repo_path;
5493 while (ct_path[0] == '/')
5494 ct_path++;
5495 err = check_out_of_date(ct_path, ct->status,
5496 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5497 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5498 if (err)
5499 goto done;
5503 err = commit_worktree(new_commit_id, &commitable_paths,
5504 head_commit_id, worktree, author, committer,
5505 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5506 if (err)
5507 goto done;
5509 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5510 fileindex, have_staged_files);
5511 sync_err = sync_fileindex(fileindex, fileindex_path);
5512 if (sync_err && err == NULL)
5513 err = sync_err;
5514 done:
5515 if (fileindex)
5516 got_fileindex_free(fileindex);
5517 free(fileindex_path);
5518 unlockerr = lock_worktree(worktree, LOCK_SH);
5519 if (unlockerr && err == NULL)
5520 err = unlockerr;
5521 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5522 struct got_commitable *ct = pe->data;
5523 free_commitable(ct);
5525 got_pathlist_free(&commitable_paths);
5526 return err;
5529 const char *
5530 got_commitable_get_path(struct got_commitable *ct)
5532 return ct->path;
5535 unsigned int
5536 got_commitable_get_status(struct got_commitable *ct)
5538 return ct->status;
5541 struct check_rebase_ok_arg {
5542 struct got_worktree *worktree;
5543 struct got_repository *repo;
5546 static const struct got_error *
5547 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5549 const struct got_error *err = NULL;
5550 struct check_rebase_ok_arg *a = arg;
5551 unsigned char status;
5552 struct stat sb;
5553 char *ondisk_path;
5555 /* Reject rebase of a work tree with mixed base commits. */
5556 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5557 SHA1_DIGEST_LENGTH))
5558 return got_error(GOT_ERR_MIXED_COMMITS);
5560 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5561 == -1)
5562 return got_error_from_errno("asprintf");
5564 /* Reject rebase of a work tree with modified or staged files. */
5565 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5566 free(ondisk_path);
5567 if (err)
5568 return err;
5570 if (status != GOT_STATUS_NO_CHANGE)
5571 return got_error(GOT_ERR_MODIFIED);
5572 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5573 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5575 return NULL;
5578 const struct got_error *
5579 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5580 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5581 struct got_worktree *worktree, struct got_reference *branch,
5582 struct got_repository *repo)
5584 const struct got_error *err = NULL;
5585 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5586 char *branch_ref_name = NULL;
5587 char *fileindex_path = NULL;
5588 struct check_rebase_ok_arg ok_arg;
5589 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5590 struct got_object_id *wt_branch_tip = NULL;
5592 *new_base_branch_ref = NULL;
5593 *tmp_branch = NULL;
5594 *fileindex = NULL;
5596 err = lock_worktree(worktree, LOCK_EX);
5597 if (err)
5598 return err;
5600 err = open_fileindex(fileindex, &fileindex_path, worktree);
5601 if (err)
5602 goto done;
5604 ok_arg.worktree = worktree;
5605 ok_arg.repo = repo;
5606 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5607 &ok_arg);
5608 if (err)
5609 goto done;
5611 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5612 if (err)
5613 goto done;
5615 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5616 if (err)
5617 goto done;
5619 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5620 if (err)
5621 goto done;
5623 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5624 0);
5625 if (err)
5626 goto done;
5628 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5629 if (err)
5630 goto done;
5631 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5632 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5633 goto done;
5636 err = got_ref_alloc_symref(new_base_branch_ref,
5637 new_base_branch_ref_name, wt_branch);
5638 if (err)
5639 goto done;
5640 err = got_ref_write(*new_base_branch_ref, repo);
5641 if (err)
5642 goto done;
5644 /* TODO Lock original branch's ref while rebasing? */
5646 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5647 if (err)
5648 goto done;
5650 err = got_ref_write(branch_ref, repo);
5651 if (err)
5652 goto done;
5654 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5655 worktree->base_commit_id);
5656 if (err)
5657 goto done;
5658 err = got_ref_write(*tmp_branch, repo);
5659 if (err)
5660 goto done;
5662 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5663 if (err)
5664 goto done;
5665 done:
5666 free(fileindex_path);
5667 free(tmp_branch_name);
5668 free(new_base_branch_ref_name);
5669 free(branch_ref_name);
5670 if (branch_ref)
5671 got_ref_close(branch_ref);
5672 if (wt_branch)
5673 got_ref_close(wt_branch);
5674 free(wt_branch_tip);
5675 if (err) {
5676 if (*new_base_branch_ref) {
5677 got_ref_close(*new_base_branch_ref);
5678 *new_base_branch_ref = NULL;
5680 if (*tmp_branch) {
5681 got_ref_close(*tmp_branch);
5682 *tmp_branch = NULL;
5684 if (*fileindex) {
5685 got_fileindex_free(*fileindex);
5686 *fileindex = NULL;
5688 lock_worktree(worktree, LOCK_SH);
5690 return err;
5693 const struct got_error *
5694 got_worktree_rebase_continue(struct got_object_id **commit_id,
5695 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5696 struct got_reference **branch, struct got_fileindex **fileindex,
5697 struct got_worktree *worktree, struct got_repository *repo)
5699 const struct got_error *err;
5700 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5701 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5702 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5703 char *fileindex_path = NULL;
5704 int have_staged_files = 0;
5706 *commit_id = NULL;
5707 *new_base_branch = NULL;
5708 *tmp_branch = NULL;
5709 *branch = NULL;
5710 *fileindex = NULL;
5712 err = lock_worktree(worktree, LOCK_EX);
5713 if (err)
5714 return err;
5716 err = open_fileindex(fileindex, &fileindex_path, worktree);
5717 if (err)
5718 goto done;
5720 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5721 &have_staged_files);
5722 if (err && err->code != GOT_ERR_CANCELLED)
5723 goto done;
5724 if (have_staged_files) {
5725 err = got_error(GOT_ERR_STAGED_PATHS);
5726 goto done;
5729 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5730 if (err)
5731 goto done;
5733 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5734 if (err)
5735 goto done;
5737 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5738 if (err)
5739 goto done;
5741 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5742 if (err)
5743 goto done;
5745 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5746 if (err)
5747 goto done;
5749 err = got_ref_open(branch, repo,
5750 got_ref_get_symref_target(branch_ref), 0);
5751 if (err)
5752 goto done;
5754 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5755 if (err)
5756 goto done;
5758 err = got_ref_resolve(commit_id, repo, commit_ref);
5759 if (err)
5760 goto done;
5762 err = got_ref_open(new_base_branch, repo,
5763 new_base_branch_ref_name, 0);
5764 if (err)
5765 goto done;
5767 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5768 if (err)
5769 goto done;
5770 done:
5771 free(commit_ref_name);
5772 free(branch_ref_name);
5773 free(fileindex_path);
5774 if (commit_ref)
5775 got_ref_close(commit_ref);
5776 if (branch_ref)
5777 got_ref_close(branch_ref);
5778 if (err) {
5779 free(*commit_id);
5780 *commit_id = NULL;
5781 if (*tmp_branch) {
5782 got_ref_close(*tmp_branch);
5783 *tmp_branch = NULL;
5785 if (*new_base_branch) {
5786 got_ref_close(*new_base_branch);
5787 *new_base_branch = NULL;
5789 if (*branch) {
5790 got_ref_close(*branch);
5791 *branch = NULL;
5793 if (*fileindex) {
5794 got_fileindex_free(*fileindex);
5795 *fileindex = NULL;
5797 lock_worktree(worktree, LOCK_SH);
5799 return err;
5802 const struct got_error *
5803 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5805 const struct got_error *err;
5806 char *tmp_branch_name = NULL;
5808 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5809 if (err)
5810 return err;
5812 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5813 free(tmp_branch_name);
5814 return NULL;
5817 static const struct got_error *
5818 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5819 char **logmsg, void *arg)
5821 *logmsg = arg;
5822 return NULL;
5825 static const struct got_error *
5826 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5827 const char *path, struct got_object_id *blob_id,
5828 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5829 int dirfd, const char *de_name)
5831 return NULL;
5834 struct collect_merged_paths_arg {
5835 got_worktree_checkout_cb progress_cb;
5836 void *progress_arg;
5837 struct got_pathlist_head *merged_paths;
5840 static const struct got_error *
5841 collect_merged_paths(void *arg, unsigned char status, const char *path)
5843 const struct got_error *err;
5844 struct collect_merged_paths_arg *a = arg;
5845 char *p;
5846 struct got_pathlist_entry *new;
5848 err = (*a->progress_cb)(a->progress_arg, status, path);
5849 if (err)
5850 return err;
5852 if (status != GOT_STATUS_MERGE &&
5853 status != GOT_STATUS_ADD &&
5854 status != GOT_STATUS_DELETE &&
5855 status != GOT_STATUS_CONFLICT)
5856 return NULL;
5858 p = strdup(path);
5859 if (p == NULL)
5860 return got_error_from_errno("strdup");
5862 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5863 if (err || new == NULL)
5864 free(p);
5865 return err;
5868 void
5869 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5871 struct got_pathlist_entry *pe;
5873 TAILQ_FOREACH(pe, merged_paths, entry)
5874 free((char *)pe->path);
5876 got_pathlist_free(merged_paths);
5879 static const struct got_error *
5880 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5881 int is_rebase, struct got_repository *repo)
5883 const struct got_error *err;
5884 struct got_reference *commit_ref = NULL;
5886 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5887 if (err) {
5888 if (err->code != GOT_ERR_NOT_REF)
5889 goto done;
5890 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5891 if (err)
5892 goto done;
5893 err = got_ref_write(commit_ref, repo);
5894 if (err)
5895 goto done;
5896 } else if (is_rebase) {
5897 struct got_object_id *stored_id;
5898 int cmp;
5900 err = got_ref_resolve(&stored_id, repo, commit_ref);
5901 if (err)
5902 goto done;
5903 cmp = got_object_id_cmp(commit_id, stored_id);
5904 free(stored_id);
5905 if (cmp != 0) {
5906 err = got_error(GOT_ERR_REBASE_COMMITID);
5907 goto done;
5910 done:
5911 if (commit_ref)
5912 got_ref_close(commit_ref);
5913 return err;
5916 static const struct got_error *
5917 rebase_merge_files(struct got_pathlist_head *merged_paths,
5918 const char *commit_ref_name, struct got_worktree *worktree,
5919 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5920 struct got_object_id *commit_id, struct got_repository *repo,
5921 got_worktree_checkout_cb progress_cb, void *progress_arg,
5922 got_cancel_cb cancel_cb, void *cancel_arg)
5924 const struct got_error *err;
5925 struct got_reference *commit_ref = NULL;
5926 struct collect_merged_paths_arg cmp_arg;
5927 char *fileindex_path;
5929 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5931 err = get_fileindex_path(&fileindex_path, worktree);
5932 if (err)
5933 return err;
5935 cmp_arg.progress_cb = progress_cb;
5936 cmp_arg.progress_arg = progress_arg;
5937 cmp_arg.merged_paths = merged_paths;
5938 err = merge_files(worktree, fileindex, fileindex_path,
5939 parent_commit_id, commit_id, repo, collect_merged_paths,
5940 &cmp_arg, cancel_cb, cancel_arg);
5941 if (commit_ref)
5942 got_ref_close(commit_ref);
5943 return err;
5946 const struct got_error *
5947 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5948 struct got_worktree *worktree, struct got_fileindex *fileindex,
5949 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5950 struct got_repository *repo,
5951 got_worktree_checkout_cb progress_cb, void *progress_arg,
5952 got_cancel_cb cancel_cb, void *cancel_arg)
5954 const struct got_error *err;
5955 char *commit_ref_name;
5957 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5958 if (err)
5959 return err;
5961 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5962 if (err)
5963 goto done;
5965 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5966 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5967 progress_arg, cancel_cb, cancel_arg);
5968 done:
5969 free(commit_ref_name);
5970 return err;
5973 const struct got_error *
5974 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5975 struct got_worktree *worktree, struct got_fileindex *fileindex,
5976 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5977 struct got_repository *repo,
5978 got_worktree_checkout_cb progress_cb, void *progress_arg,
5979 got_cancel_cb cancel_cb, void *cancel_arg)
5981 const struct got_error *err;
5982 char *commit_ref_name;
5984 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5985 if (err)
5986 return err;
5988 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5989 if (err)
5990 goto done;
5992 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5993 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5994 progress_arg, cancel_cb, cancel_arg);
5995 done:
5996 free(commit_ref_name);
5997 return err;
6000 static const struct got_error *
6001 rebase_commit(struct got_object_id **new_commit_id,
6002 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6003 struct got_worktree *worktree, struct got_fileindex *fileindex,
6004 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6005 const char *new_logmsg, struct got_repository *repo)
6007 const struct got_error *err, *sync_err;
6008 struct got_pathlist_head commitable_paths;
6009 struct collect_commitables_arg cc_arg;
6010 char *fileindex_path = NULL;
6011 struct got_reference *head_ref = NULL;
6012 struct got_object_id *head_commit_id = NULL;
6013 char *logmsg = NULL;
6015 TAILQ_INIT(&commitable_paths);
6016 *new_commit_id = NULL;
6018 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6020 err = get_fileindex_path(&fileindex_path, worktree);
6021 if (err)
6022 return err;
6024 cc_arg.commitable_paths = &commitable_paths;
6025 cc_arg.worktree = worktree;
6026 cc_arg.repo = repo;
6027 cc_arg.have_staged_files = 0;
6029 * If possible get the status of individual files directly to
6030 * avoid crawling the entire work tree once per rebased commit.
6031 * TODO: Ideally, merged_paths would contain a list of commitables
6032 * we could use so we could skip worktree_status() entirely.
6034 if (merged_paths) {
6035 struct got_pathlist_entry *pe;
6036 TAILQ_FOREACH(pe, merged_paths, entry) {
6037 err = worktree_status(worktree, pe->path, fileindex,
6038 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6039 0);
6040 if (err)
6041 goto done;
6043 } else {
6044 err = worktree_status(worktree, "", fileindex, repo,
6045 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6046 if (err)
6047 goto done;
6050 if (TAILQ_EMPTY(&commitable_paths)) {
6051 /* No-op change; commit will be elided. */
6052 err = got_ref_delete(commit_ref, repo);
6053 if (err)
6054 goto done;
6055 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6056 goto done;
6059 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6060 if (err)
6061 goto done;
6063 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6064 if (err)
6065 goto done;
6067 if (new_logmsg) {
6068 logmsg = strdup(new_logmsg);
6069 if (logmsg == NULL) {
6070 err = got_error_from_errno("strdup");
6071 goto done;
6073 } else {
6074 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6075 if (err)
6076 goto done;
6079 /* NB: commit_worktree will call free(logmsg) */
6080 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6081 worktree, got_object_commit_get_author(orig_commit),
6082 got_object_commit_get_committer(orig_commit),
6083 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6084 if (err)
6085 goto done;
6087 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6088 if (err)
6089 goto done;
6091 err = got_ref_delete(commit_ref, repo);
6092 if (err)
6093 goto done;
6095 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6096 fileindex, 0);
6097 sync_err = sync_fileindex(fileindex, fileindex_path);
6098 if (sync_err && err == NULL)
6099 err = sync_err;
6100 done:
6101 free(fileindex_path);
6102 free(head_commit_id);
6103 if (head_ref)
6104 got_ref_close(head_ref);
6105 if (err) {
6106 free(*new_commit_id);
6107 *new_commit_id = NULL;
6109 return err;
6112 const struct got_error *
6113 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6114 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6115 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6116 struct got_commit_object *orig_commit,
6117 struct got_object_id *orig_commit_id, struct got_repository *repo)
6119 const struct got_error *err;
6120 char *commit_ref_name;
6121 struct got_reference *commit_ref = NULL;
6122 struct got_object_id *commit_id = NULL;
6124 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6125 if (err)
6126 return err;
6128 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6129 if (err)
6130 goto done;
6131 err = got_ref_resolve(&commit_id, repo, commit_ref);
6132 if (err)
6133 goto done;
6134 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6135 err = got_error(GOT_ERR_REBASE_COMMITID);
6136 goto done;
6139 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6140 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6141 done:
6142 if (commit_ref)
6143 got_ref_close(commit_ref);
6144 free(commit_ref_name);
6145 free(commit_id);
6146 return err;
6149 const struct got_error *
6150 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6151 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6152 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6153 struct got_commit_object *orig_commit,
6154 struct got_object_id *orig_commit_id, const char *new_logmsg,
6155 struct got_repository *repo)
6157 const struct got_error *err;
6158 char *commit_ref_name;
6159 struct got_reference *commit_ref = NULL;
6161 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6162 if (err)
6163 return err;
6165 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6166 if (err)
6167 goto done;
6169 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6170 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6171 done:
6172 if (commit_ref)
6173 got_ref_close(commit_ref);
6174 free(commit_ref_name);
6175 return err;
6178 const struct got_error *
6179 got_worktree_rebase_postpone(struct got_worktree *worktree,
6180 struct got_fileindex *fileindex)
6182 if (fileindex)
6183 got_fileindex_free(fileindex);
6184 return lock_worktree(worktree, LOCK_SH);
6187 static const struct got_error *
6188 delete_ref(const char *name, struct got_repository *repo)
6190 const struct got_error *err;
6191 struct got_reference *ref;
6193 err = got_ref_open(&ref, repo, name, 0);
6194 if (err) {
6195 if (err->code == GOT_ERR_NOT_REF)
6196 return NULL;
6197 return err;
6200 err = got_ref_delete(ref, repo);
6201 got_ref_close(ref);
6202 return err;
6205 static const struct got_error *
6206 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6208 const struct got_error *err;
6209 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6210 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6212 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6213 if (err)
6214 goto done;
6215 err = delete_ref(tmp_branch_name, repo);
6216 if (err)
6217 goto done;
6219 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6220 if (err)
6221 goto done;
6222 err = delete_ref(new_base_branch_ref_name, repo);
6223 if (err)
6224 goto done;
6226 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6227 if (err)
6228 goto done;
6229 err = delete_ref(branch_ref_name, repo);
6230 if (err)
6231 goto done;
6233 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6234 if (err)
6235 goto done;
6236 err = delete_ref(commit_ref_name, repo);
6237 if (err)
6238 goto done;
6240 done:
6241 free(tmp_branch_name);
6242 free(new_base_branch_ref_name);
6243 free(branch_ref_name);
6244 free(commit_ref_name);
6245 return err;
6248 const struct got_error *
6249 got_worktree_rebase_complete(struct got_worktree *worktree,
6250 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6251 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6252 struct got_repository *repo)
6254 const struct got_error *err, *unlockerr;
6255 struct got_object_id *new_head_commit_id = NULL;
6257 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6258 if (err)
6259 return err;
6261 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6262 if (err)
6263 goto done;
6265 err = got_ref_write(rebased_branch, repo);
6266 if (err)
6267 goto done;
6269 err = got_worktree_set_head_ref(worktree, rebased_branch);
6270 if (err)
6271 goto done;
6273 err = delete_rebase_refs(worktree, repo);
6274 done:
6275 if (fileindex)
6276 got_fileindex_free(fileindex);
6277 free(new_head_commit_id);
6278 unlockerr = lock_worktree(worktree, LOCK_SH);
6279 if (unlockerr && err == NULL)
6280 err = unlockerr;
6281 return err;
6284 const struct got_error *
6285 got_worktree_rebase_abort(struct got_worktree *worktree,
6286 struct got_fileindex *fileindex, struct got_repository *repo,
6287 struct got_reference *new_base_branch,
6288 got_worktree_checkout_cb progress_cb, void *progress_arg)
6290 const struct got_error *err, *unlockerr, *sync_err;
6291 struct got_reference *resolved = NULL;
6292 struct got_object_id *commit_id = NULL;
6293 char *fileindex_path = NULL;
6294 struct revert_file_args rfa;
6295 struct got_object_id *tree_id = NULL;
6297 err = lock_worktree(worktree, LOCK_EX);
6298 if (err)
6299 return err;
6301 err = got_ref_open(&resolved, repo,
6302 got_ref_get_symref_target(new_base_branch), 0);
6303 if (err)
6304 goto done;
6306 err = got_worktree_set_head_ref(worktree, resolved);
6307 if (err)
6308 goto done;
6311 * XXX commits to the base branch could have happened while
6312 * we were busy rebasing; should we store the original commit ID
6313 * when rebase begins and read it back here?
6315 err = got_ref_resolve(&commit_id, repo, resolved);
6316 if (err)
6317 goto done;
6319 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6320 if (err)
6321 goto done;
6323 err = got_object_id_by_path(&tree_id, repo,
6324 worktree->base_commit_id, worktree->path_prefix);
6325 if (err)
6326 goto done;
6328 err = delete_rebase_refs(worktree, repo);
6329 if (err)
6330 goto done;
6332 err = get_fileindex_path(&fileindex_path, worktree);
6333 if (err)
6334 goto done;
6336 rfa.worktree = worktree;
6337 rfa.fileindex = fileindex;
6338 rfa.progress_cb = progress_cb;
6339 rfa.progress_arg = progress_arg;
6340 rfa.patch_cb = NULL;
6341 rfa.patch_arg = NULL;
6342 rfa.repo = repo;
6343 err = worktree_status(worktree, "", fileindex, repo,
6344 revert_file, &rfa, NULL, NULL, 0, 0);
6345 if (err)
6346 goto sync;
6348 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6349 repo, progress_cb, progress_arg, NULL, NULL);
6350 sync:
6351 sync_err = sync_fileindex(fileindex, fileindex_path);
6352 if (sync_err && err == NULL)
6353 err = sync_err;
6354 done:
6355 got_ref_close(resolved);
6356 free(tree_id);
6357 free(commit_id);
6358 if (fileindex)
6359 got_fileindex_free(fileindex);
6360 free(fileindex_path);
6362 unlockerr = lock_worktree(worktree, LOCK_SH);
6363 if (unlockerr && err == NULL)
6364 err = unlockerr;
6365 return err;
6368 const struct got_error *
6369 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6370 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6371 struct got_fileindex **fileindex, struct got_worktree *worktree,
6372 struct got_repository *repo)
6374 const struct got_error *err = NULL;
6375 char *tmp_branch_name = NULL;
6376 char *branch_ref_name = NULL;
6377 char *base_commit_ref_name = NULL;
6378 char *fileindex_path = NULL;
6379 struct check_rebase_ok_arg ok_arg;
6380 struct got_reference *wt_branch = NULL;
6381 struct got_reference *base_commit_ref = NULL;
6383 *tmp_branch = NULL;
6384 *branch_ref = NULL;
6385 *base_commit_id = NULL;
6386 *fileindex = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6389 if (err)
6390 return err;
6392 err = open_fileindex(fileindex, &fileindex_path, worktree);
6393 if (err)
6394 goto done;
6396 ok_arg.worktree = worktree;
6397 ok_arg.repo = repo;
6398 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6399 &ok_arg);
6400 if (err)
6401 goto done;
6403 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6404 if (err)
6405 goto done;
6407 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6408 if (err)
6409 goto done;
6411 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6412 worktree);
6413 if (err)
6414 goto done;
6416 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6417 0);
6418 if (err)
6419 goto done;
6421 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6422 if (err)
6423 goto done;
6425 err = got_ref_write(*branch_ref, repo);
6426 if (err)
6427 goto done;
6429 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6430 worktree->base_commit_id);
6431 if (err)
6432 goto done;
6433 err = got_ref_write(base_commit_ref, repo);
6434 if (err)
6435 goto done;
6436 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6437 if (*base_commit_id == NULL) {
6438 err = got_error_from_errno("got_object_id_dup");
6439 goto done;
6442 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6443 worktree->base_commit_id);
6444 if (err)
6445 goto done;
6446 err = got_ref_write(*tmp_branch, repo);
6447 if (err)
6448 goto done;
6450 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6451 if (err)
6452 goto done;
6453 done:
6454 free(fileindex_path);
6455 free(tmp_branch_name);
6456 free(branch_ref_name);
6457 free(base_commit_ref_name);
6458 if (wt_branch)
6459 got_ref_close(wt_branch);
6460 if (err) {
6461 if (*branch_ref) {
6462 got_ref_close(*branch_ref);
6463 *branch_ref = NULL;
6465 if (*tmp_branch) {
6466 got_ref_close(*tmp_branch);
6467 *tmp_branch = NULL;
6469 free(*base_commit_id);
6470 if (*fileindex) {
6471 got_fileindex_free(*fileindex);
6472 *fileindex = NULL;
6474 lock_worktree(worktree, LOCK_SH);
6476 return err;
6479 const struct got_error *
6480 got_worktree_histedit_postpone(struct got_worktree *worktree,
6481 struct got_fileindex *fileindex)
6483 if (fileindex)
6484 got_fileindex_free(fileindex);
6485 return lock_worktree(worktree, LOCK_SH);
6488 const struct got_error *
6489 got_worktree_histedit_in_progress(int *in_progress,
6490 struct got_worktree *worktree)
6492 const struct got_error *err;
6493 char *tmp_branch_name = NULL;
6495 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6496 if (err)
6497 return err;
6499 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6500 free(tmp_branch_name);
6501 return NULL;
6504 const struct got_error *
6505 got_worktree_histedit_continue(struct got_object_id **commit_id,
6506 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6507 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6508 struct got_worktree *worktree, struct got_repository *repo)
6510 const struct got_error *err;
6511 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6512 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6513 struct got_reference *commit_ref = NULL;
6514 struct got_reference *base_commit_ref = NULL;
6515 char *fileindex_path = NULL;
6516 int have_staged_files = 0;
6518 *commit_id = NULL;
6519 *tmp_branch = NULL;
6520 *base_commit_id = NULL;
6521 *fileindex = NULL;
6523 err = lock_worktree(worktree, LOCK_EX);
6524 if (err)
6525 return err;
6527 err = open_fileindex(fileindex, &fileindex_path, worktree);
6528 if (err)
6529 goto done;
6531 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6532 &have_staged_files);
6533 if (err && err->code != GOT_ERR_CANCELLED)
6534 goto done;
6535 if (have_staged_files) {
6536 err = got_error(GOT_ERR_STAGED_PATHS);
6537 goto done;
6540 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6541 if (err)
6542 goto done;
6544 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6545 if (err)
6546 goto done;
6548 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6549 if (err)
6550 goto done;
6552 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6553 worktree);
6554 if (err)
6555 goto done;
6557 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6558 if (err)
6559 goto done;
6561 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6562 if (err)
6563 goto done;
6564 err = got_ref_resolve(commit_id, repo, commit_ref);
6565 if (err)
6566 goto done;
6568 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6569 if (err)
6570 goto done;
6571 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6572 if (err)
6573 goto done;
6575 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6576 if (err)
6577 goto done;
6578 done:
6579 free(commit_ref_name);
6580 free(branch_ref_name);
6581 free(fileindex_path);
6582 if (commit_ref)
6583 got_ref_close(commit_ref);
6584 if (base_commit_ref)
6585 got_ref_close(base_commit_ref);
6586 if (err) {
6587 free(*commit_id);
6588 *commit_id = NULL;
6589 free(*base_commit_id);
6590 *base_commit_id = NULL;
6591 if (*tmp_branch) {
6592 got_ref_close(*tmp_branch);
6593 *tmp_branch = NULL;
6595 if (*fileindex) {
6596 got_fileindex_free(*fileindex);
6597 *fileindex = NULL;
6599 lock_worktree(worktree, LOCK_EX);
6601 return err;
6604 static const struct got_error *
6605 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6607 const struct got_error *err;
6608 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6609 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6612 if (err)
6613 goto done;
6614 err = delete_ref(tmp_branch_name, repo);
6615 if (err)
6616 goto done;
6618 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6619 worktree);
6620 if (err)
6621 goto done;
6622 err = delete_ref(base_commit_ref_name, repo);
6623 if (err)
6624 goto done;
6626 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6627 if (err)
6628 goto done;
6629 err = delete_ref(branch_ref_name, repo);
6630 if (err)
6631 goto done;
6633 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6634 if (err)
6635 goto done;
6636 err = delete_ref(commit_ref_name, repo);
6637 if (err)
6638 goto done;
6639 done:
6640 free(tmp_branch_name);
6641 free(base_commit_ref_name);
6642 free(branch_ref_name);
6643 free(commit_ref_name);
6644 return err;
6647 const struct got_error *
6648 got_worktree_histedit_abort(struct got_worktree *worktree,
6649 struct got_fileindex *fileindex, struct got_repository *repo,
6650 struct got_reference *branch, struct got_object_id *base_commit_id,
6651 got_worktree_checkout_cb progress_cb, void *progress_arg)
6653 const struct got_error *err, *unlockerr, *sync_err;
6654 struct got_reference *resolved = NULL;
6655 char *fileindex_path = NULL;
6656 struct got_object_id *tree_id = NULL;
6657 struct revert_file_args rfa;
6659 err = lock_worktree(worktree, LOCK_EX);
6660 if (err)
6661 return err;
6663 err = got_ref_open(&resolved, repo,
6664 got_ref_get_symref_target(branch), 0);
6665 if (err)
6666 goto done;
6668 err = got_worktree_set_head_ref(worktree, resolved);
6669 if (err)
6670 goto done;
6672 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6673 if (err)
6674 goto done;
6676 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6677 worktree->path_prefix);
6678 if (err)
6679 goto done;
6681 err = delete_histedit_refs(worktree, repo);
6682 if (err)
6683 goto done;
6685 err = get_fileindex_path(&fileindex_path, worktree);
6686 if (err)
6687 goto done;
6689 rfa.worktree = worktree;
6690 rfa.fileindex = fileindex;
6691 rfa.progress_cb = progress_cb;
6692 rfa.progress_arg = progress_arg;
6693 rfa.patch_cb = NULL;
6694 rfa.patch_arg = NULL;
6695 rfa.repo = repo;
6696 err = worktree_status(worktree, "", fileindex, repo,
6697 revert_file, &rfa, NULL, NULL, 0, 0);
6698 if (err)
6699 goto sync;
6701 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6702 repo, progress_cb, progress_arg, NULL, NULL);
6703 sync:
6704 sync_err = sync_fileindex(fileindex, fileindex_path);
6705 if (sync_err && err == NULL)
6706 err = sync_err;
6707 done:
6708 got_ref_close(resolved);
6709 free(tree_id);
6710 free(fileindex_path);
6712 unlockerr = lock_worktree(worktree, LOCK_SH);
6713 if (unlockerr && err == NULL)
6714 err = unlockerr;
6715 return err;
6718 const struct got_error *
6719 got_worktree_histedit_complete(struct got_worktree *worktree,
6720 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6721 struct got_reference *edited_branch, struct got_repository *repo)
6723 const struct got_error *err, *unlockerr;
6724 struct got_object_id *new_head_commit_id = NULL;
6725 struct got_reference *resolved = NULL;
6727 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6728 if (err)
6729 return err;
6731 err = got_ref_open(&resolved, repo,
6732 got_ref_get_symref_target(edited_branch), 0);
6733 if (err)
6734 goto done;
6736 err = got_ref_change_ref(resolved, new_head_commit_id);
6737 if (err)
6738 goto done;
6740 err = got_ref_write(resolved, repo);
6741 if (err)
6742 goto done;
6744 err = got_worktree_set_head_ref(worktree, resolved);
6745 if (err)
6746 goto done;
6748 err = delete_histedit_refs(worktree, repo);
6749 done:
6750 if (fileindex)
6751 got_fileindex_free(fileindex);
6752 free(new_head_commit_id);
6753 unlockerr = lock_worktree(worktree, LOCK_SH);
6754 if (unlockerr && err == NULL)
6755 err = unlockerr;
6756 return err;
6759 const struct got_error *
6760 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6761 struct got_object_id *commit_id, struct got_repository *repo)
6763 const struct got_error *err;
6764 char *commit_ref_name;
6766 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6767 if (err)
6768 return err;
6770 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6771 if (err)
6772 goto done;
6774 err = delete_ref(commit_ref_name, repo);
6775 done:
6776 free(commit_ref_name);
6777 return err;
6780 const struct got_error *
6781 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6782 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6783 struct got_worktree *worktree, const char *refname,
6784 struct got_repository *repo)
6786 const struct got_error *err = NULL;
6787 char *fileindex_path = NULL;
6788 struct check_rebase_ok_arg ok_arg;
6790 *fileindex = NULL;
6791 *branch_ref = NULL;
6792 *base_branch_ref = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6795 if (err)
6796 return err;
6798 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6799 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 "cannot integrate a branch into itself; "
6801 "update -b or different branch name required");
6802 goto done;
6805 err = open_fileindex(fileindex, &fileindex_path, worktree);
6806 if (err)
6807 goto done;
6809 /* Preconditions are the same as for rebase. */
6810 ok_arg.worktree = worktree;
6811 ok_arg.repo = repo;
6812 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6813 &ok_arg);
6814 if (err)
6815 goto done;
6817 err = got_ref_open(branch_ref, repo, refname, 1);
6818 if (err)
6819 goto done;
6821 err = got_ref_open(base_branch_ref, repo,
6822 got_worktree_get_head_ref_name(worktree), 1);
6823 done:
6824 if (err) {
6825 if (*branch_ref) {
6826 got_ref_close(*branch_ref);
6827 *branch_ref = NULL;
6829 if (*base_branch_ref) {
6830 got_ref_close(*base_branch_ref);
6831 *base_branch_ref = NULL;
6833 if (*fileindex) {
6834 got_fileindex_free(*fileindex);
6835 *fileindex = NULL;
6837 lock_worktree(worktree, LOCK_SH);
6839 return err;
6842 const struct got_error *
6843 got_worktree_integrate_continue(struct got_worktree *worktree,
6844 struct got_fileindex *fileindex, struct got_repository *repo,
6845 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6846 got_worktree_checkout_cb progress_cb, void *progress_arg,
6847 got_cancel_cb cancel_cb, void *cancel_arg)
6849 const struct got_error *err = NULL, *sync_err, *unlockerr;
6850 char *fileindex_path = NULL;
6851 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6853 err = get_fileindex_path(&fileindex_path, worktree);
6854 if (err)
6855 goto done;
6857 err = got_ref_resolve(&commit_id, repo, branch_ref);
6858 if (err)
6859 goto done;
6861 err = got_object_id_by_path(&tree_id, repo, commit_id,
6862 worktree->path_prefix);
6863 if (err)
6864 goto done;
6866 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6867 if (err)
6868 goto done;
6870 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6871 progress_cb, progress_arg, cancel_cb, cancel_arg);
6872 if (err)
6873 goto sync;
6875 err = got_ref_change_ref(base_branch_ref, commit_id);
6876 if (err)
6877 goto sync;
6879 err = got_ref_write(base_branch_ref, repo);
6880 sync:
6881 sync_err = sync_fileindex(fileindex, fileindex_path);
6882 if (sync_err && err == NULL)
6883 err = sync_err;
6885 done:
6886 unlockerr = got_ref_unlock(branch_ref);
6887 if (unlockerr && err == NULL)
6888 err = unlockerr;
6889 got_ref_close(branch_ref);
6891 unlockerr = got_ref_unlock(base_branch_ref);
6892 if (unlockerr && err == NULL)
6893 err = unlockerr;
6894 got_ref_close(base_branch_ref);
6896 got_fileindex_free(fileindex);
6897 free(fileindex_path);
6898 free(tree_id);
6900 unlockerr = lock_worktree(worktree, LOCK_SH);
6901 if (unlockerr && err == NULL)
6902 err = unlockerr;
6903 return err;
6906 const struct got_error *
6907 got_worktree_integrate_abort(struct got_worktree *worktree,
6908 struct got_fileindex *fileindex, struct got_repository *repo,
6909 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6911 const struct got_error *err = NULL, *unlockerr = NULL;
6913 got_fileindex_free(fileindex);
6915 err = lock_worktree(worktree, LOCK_SH);
6917 unlockerr = got_ref_unlock(branch_ref);
6918 if (unlockerr && err == NULL)
6919 err = unlockerr;
6920 got_ref_close(branch_ref);
6922 unlockerr = got_ref_unlock(base_branch_ref);
6923 if (unlockerr && err == NULL)
6924 err = unlockerr;
6925 got_ref_close(base_branch_ref);
6927 return err;
6930 struct check_stage_ok_arg {
6931 struct got_object_id *head_commit_id;
6932 struct got_worktree *worktree;
6933 struct got_fileindex *fileindex;
6934 struct got_repository *repo;
6935 int have_changes;
6938 const struct got_error *
6939 check_stage_ok(void *arg, unsigned char status,
6940 unsigned char staged_status, const char *relpath,
6941 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6942 struct got_object_id *commit_id, int dirfd, const char *de_name)
6944 struct check_stage_ok_arg *a = arg;
6945 const struct got_error *err = NULL;
6946 struct got_fileindex_entry *ie;
6947 struct got_object_id base_commit_id;
6948 struct got_object_id *base_commit_idp = NULL;
6949 char *in_repo_path = NULL, *p;
6951 if (status == GOT_STATUS_UNVERSIONED ||
6952 status == GOT_STATUS_NO_CHANGE)
6953 return NULL;
6954 if (status == GOT_STATUS_NONEXISTENT)
6955 return got_error_set_errno(ENOENT, relpath);
6957 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6958 if (ie == NULL)
6959 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6961 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6962 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6963 relpath) == -1)
6964 return got_error_from_errno("asprintf");
6966 if (got_fileindex_entry_has_commit(ie)) {
6967 memcpy(base_commit_id.sha1, ie->commit_sha1,
6968 SHA1_DIGEST_LENGTH);
6969 base_commit_idp = &base_commit_id;
6972 if (status == GOT_STATUS_CONFLICT) {
6973 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6974 goto done;
6975 } else if (status != GOT_STATUS_ADD &&
6976 status != GOT_STATUS_MODIFY &&
6977 status != GOT_STATUS_DELETE) {
6978 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6979 goto done;
6982 a->have_changes = 1;
6984 p = in_repo_path;
6985 while (p[0] == '/')
6986 p++;
6987 err = check_out_of_date(p, status, staged_status,
6988 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6989 GOT_ERR_STAGE_OUT_OF_DATE);
6990 done:
6991 free(in_repo_path);
6992 return err;
6995 struct stage_path_arg {
6996 struct got_worktree *worktree;
6997 struct got_fileindex *fileindex;
6998 struct got_repository *repo;
6999 got_worktree_status_cb status_cb;
7000 void *status_arg;
7001 got_worktree_patch_cb patch_cb;
7002 void *patch_arg;
7003 int staged_something;
7004 int allow_bad_symlinks;
7007 static const struct got_error *
7008 stage_path(void *arg, unsigned char status,
7009 unsigned char staged_status, const char *relpath,
7010 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7011 struct got_object_id *commit_id, int dirfd, const char *de_name)
7013 struct stage_path_arg *a = arg;
7014 const struct got_error *err = NULL;
7015 struct got_fileindex_entry *ie;
7016 char *ondisk_path = NULL, *path_content = NULL;
7017 uint32_t stage;
7018 struct got_object_id *new_staged_blob_id = NULL;
7019 struct stat sb;
7021 if (status == GOT_STATUS_UNVERSIONED)
7022 return NULL;
7024 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7025 if (ie == NULL)
7026 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7029 relpath)== -1)
7030 return got_error_from_errno("asprintf");
7032 switch (status) {
7033 case GOT_STATUS_ADD:
7034 case GOT_STATUS_MODIFY:
7035 /* XXX could sb.st_mode be passed in by our caller? */
7036 if (lstat(ondisk_path, &sb) == -1) {
7037 err = got_error_from_errno2("lstat", ondisk_path);
7038 break;
7040 if (a->patch_cb) {
7041 if (status == GOT_STATUS_ADD) {
7042 int choice = GOT_PATCH_CHOICE_NONE;
7043 err = (*a->patch_cb)(&choice, a->patch_arg,
7044 status, ie->path, NULL, 1, 1);
7045 if (err)
7046 break;
7047 if (choice != GOT_PATCH_CHOICE_YES)
7048 break;
7049 } else {
7050 err = create_patched_content(&path_content, 0,
7051 staged_blob_id ? staged_blob_id : blob_id,
7052 ondisk_path, dirfd, de_name, ie->path,
7053 a->repo, a->patch_cb, a->patch_arg);
7054 if (err || path_content == NULL)
7055 break;
7058 err = got_object_blob_create(&new_staged_blob_id,
7059 path_content ? path_content : ondisk_path, a->repo);
7060 if (err)
7061 break;
7062 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7063 SHA1_DIGEST_LENGTH);
7064 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7065 stage = GOT_FILEIDX_STAGE_ADD;
7066 else
7067 stage = GOT_FILEIDX_STAGE_MODIFY;
7068 got_fileindex_entry_stage_set(ie, stage);
7069 if (S_ISLNK(sb.st_mode)) {
7070 int is_bad_symlink = 0;
7071 if (!a->allow_bad_symlinks) {
7072 char target_path[PATH_MAX];
7073 ssize_t target_len;
7074 target_len = readlink(ondisk_path, target_path,
7075 sizeof(target_path));
7076 if (target_len == -1) {
7077 err = got_error_from_errno2("readlink",
7078 ondisk_path);
7079 break;
7081 err = is_bad_symlink_target(&is_bad_symlink,
7082 target_path, target_len, ondisk_path,
7083 a->worktree->root_path);
7084 if (err)
7085 break;
7086 if (is_bad_symlink) {
7087 err = got_error_path(ondisk_path,
7088 GOT_ERR_BAD_SYMLINK);
7089 break;
7092 if (is_bad_symlink)
7093 got_fileindex_entry_staged_filetype_set(ie,
7094 GOT_FILEIDX_MODE_BAD_SYMLINK);
7095 else
7096 got_fileindex_entry_staged_filetype_set(ie,
7097 GOT_FILEIDX_MODE_SYMLINK);
7098 } else {
7099 got_fileindex_entry_staged_filetype_set(ie,
7100 GOT_FILEIDX_MODE_REGULAR_FILE);
7102 a->staged_something = 1;
7103 if (a->status_cb == NULL)
7104 break;
7105 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7106 get_staged_status(ie), relpath, blob_id,
7107 new_staged_blob_id, NULL, dirfd, de_name);
7108 break;
7109 case GOT_STATUS_DELETE:
7110 if (staged_status == GOT_STATUS_DELETE)
7111 break;
7112 if (a->patch_cb) {
7113 int choice = GOT_PATCH_CHOICE_NONE;
7114 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7115 ie->path, NULL, 1, 1);
7116 if (err)
7117 break;
7118 if (choice == GOT_PATCH_CHOICE_NO)
7119 break;
7120 if (choice != GOT_PATCH_CHOICE_YES) {
7121 err = got_error(GOT_ERR_PATCH_CHOICE);
7122 break;
7125 stage = GOT_FILEIDX_STAGE_DELETE;
7126 got_fileindex_entry_stage_set(ie, stage);
7127 a->staged_something = 1;
7128 if (a->status_cb == NULL)
7129 break;
7130 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7131 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7132 de_name);
7133 break;
7134 case GOT_STATUS_NO_CHANGE:
7135 break;
7136 case GOT_STATUS_CONFLICT:
7137 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7138 break;
7139 case GOT_STATUS_NONEXISTENT:
7140 err = got_error_set_errno(ENOENT, relpath);
7141 break;
7142 default:
7143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7144 break;
7147 if (path_content && unlink(path_content) == -1 && err == NULL)
7148 err = got_error_from_errno2("unlink", path_content);
7149 free(path_content);
7150 free(ondisk_path);
7151 free(new_staged_blob_id);
7152 return err;
7155 const struct got_error *
7156 got_worktree_stage(struct got_worktree *worktree,
7157 struct got_pathlist_head *paths,
7158 got_worktree_status_cb status_cb, void *status_arg,
7159 got_worktree_patch_cb patch_cb, void *patch_arg,
7160 int allow_bad_symlinks, struct got_repository *repo)
7162 const struct got_error *err = NULL, *sync_err, *unlockerr;
7163 struct got_pathlist_entry *pe;
7164 struct got_fileindex *fileindex = NULL;
7165 char *fileindex_path = NULL;
7166 struct got_reference *head_ref = NULL;
7167 struct got_object_id *head_commit_id = NULL;
7168 struct check_stage_ok_arg oka;
7169 struct stage_path_arg spa;
7171 err = lock_worktree(worktree, LOCK_EX);
7172 if (err)
7173 return err;
7175 err = got_ref_open(&head_ref, repo,
7176 got_worktree_get_head_ref_name(worktree), 0);
7177 if (err)
7178 goto done;
7179 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7180 if (err)
7181 goto done;
7182 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7183 if (err)
7184 goto done;
7186 /* Check pre-conditions before staging anything. */
7187 oka.head_commit_id = head_commit_id;
7188 oka.worktree = worktree;
7189 oka.fileindex = fileindex;
7190 oka.repo = repo;
7191 oka.have_changes = 0;
7192 TAILQ_FOREACH(pe, paths, entry) {
7193 err = worktree_status(worktree, pe->path, fileindex, repo,
7194 check_stage_ok, &oka, NULL, NULL, 0, 0);
7195 if (err)
7196 goto done;
7198 if (!oka.have_changes) {
7199 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7200 goto done;
7203 spa.worktree = worktree;
7204 spa.fileindex = fileindex;
7205 spa.repo = repo;
7206 spa.patch_cb = patch_cb;
7207 spa.patch_arg = patch_arg;
7208 spa.status_cb = status_cb;
7209 spa.status_arg = status_arg;
7210 spa.staged_something = 0;
7211 spa.allow_bad_symlinks = allow_bad_symlinks;
7212 TAILQ_FOREACH(pe, paths, entry) {
7213 err = worktree_status(worktree, pe->path, fileindex, repo,
7214 stage_path, &spa, NULL, NULL, 0, 0);
7215 if (err)
7216 goto done;
7218 if (!spa.staged_something) {
7219 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7220 goto done;
7223 sync_err = sync_fileindex(fileindex, fileindex_path);
7224 if (sync_err && err == NULL)
7225 err = sync_err;
7226 done:
7227 if (head_ref)
7228 got_ref_close(head_ref);
7229 free(head_commit_id);
7230 free(fileindex_path);
7231 if (fileindex)
7232 got_fileindex_free(fileindex);
7233 unlockerr = lock_worktree(worktree, LOCK_SH);
7234 if (unlockerr && err == NULL)
7235 err = unlockerr;
7236 return err;
7239 struct unstage_path_arg {
7240 struct got_worktree *worktree;
7241 struct got_fileindex *fileindex;
7242 struct got_repository *repo;
7243 got_worktree_checkout_cb progress_cb;
7244 void *progress_arg;
7245 got_worktree_patch_cb patch_cb;
7246 void *patch_arg;
7249 static const struct got_error *
7250 create_unstaged_content(char **path_unstaged_content,
7251 char **path_new_staged_content, struct got_object_id *blob_id,
7252 struct got_object_id *staged_blob_id, const char *relpath,
7253 struct got_repository *repo,
7254 got_worktree_patch_cb patch_cb, void *patch_arg)
7256 const struct got_error *err;
7257 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7258 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7259 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7260 struct stat sb1, sb2;
7261 struct got_diff_changes *changes = NULL;
7262 struct got_diff_state *ds = NULL;
7263 struct got_diff_args *args = NULL;
7264 struct got_diff_change *change;
7265 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7266 int have_content = 0, have_rejected_content = 0;
7268 *path_unstaged_content = NULL;
7269 *path_new_staged_content = NULL;
7271 err = got_object_id_str(&label1, blob_id);
7272 if (err)
7273 return err;
7274 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7275 if (err)
7276 goto done;
7278 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7279 if (err)
7280 goto done;
7282 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7283 if (err)
7284 goto done;
7286 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7287 if (err)
7288 goto done;
7290 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7291 if (err)
7292 goto done;
7294 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7295 if (err)
7296 goto done;
7298 if (stat(path1, &sb1) == -1) {
7299 err = got_error_from_errno2("stat", path1);
7300 goto done;
7303 if (stat(path2, &sb2) == -1) {
7304 err = got_error_from_errno2("stat", path2);
7305 goto done;
7308 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7309 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7310 if (err)
7311 goto done;
7313 err = got_opentemp_named(path_unstaged_content, &outfile,
7314 "got-unstaged-content");
7315 if (err)
7316 goto done;
7317 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7318 "got-new-staged-content");
7319 if (err)
7320 goto done;
7322 if (fseek(f1, 0L, SEEK_SET) == -1) {
7323 err = got_ferror(f1, GOT_ERR_IO);
7324 goto done;
7326 if (fseek(f2, 0L, SEEK_SET) == -1) {
7327 err = got_ferror(f2, GOT_ERR_IO);
7328 goto done;
7330 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7331 int choice;
7332 err = apply_or_reject_change(&choice, change, ++n,
7333 changes->nchanges, ds, args, diff_flags, relpath,
7334 f1, f2, &line_cur1, &line_cur2,
7335 outfile, rejectfile, patch_cb, patch_arg);
7336 if (err)
7337 goto done;
7338 if (choice == GOT_PATCH_CHOICE_YES)
7339 have_content = 1;
7340 else
7341 have_rejected_content = 1;
7342 if (choice == GOT_PATCH_CHOICE_QUIT)
7343 break;
7345 if (have_content || have_rejected_content)
7346 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7347 outfile, rejectfile);
7348 done:
7349 free(label1);
7350 if (blob)
7351 got_object_blob_close(blob);
7352 if (staged_blob)
7353 got_object_blob_close(staged_blob);
7354 if (f1 && fclose(f1) == EOF && err == NULL)
7355 err = got_error_from_errno2("fclose", path1);
7356 if (f2 && fclose(f2) == EOF && err == NULL)
7357 err = got_error_from_errno2("fclose", path2);
7358 if (outfile && fclose(outfile) == EOF && err == NULL)
7359 err = got_error_from_errno2("fclose", *path_unstaged_content);
7360 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7361 err = got_error_from_errno2("fclose", *path_new_staged_content);
7362 if (path1 && unlink(path1) == -1 && err == NULL)
7363 err = got_error_from_errno2("unlink", path1);
7364 if (path2 && unlink(path2) == -1 && err == NULL)
7365 err = got_error_from_errno2("unlink", path2);
7366 if (err || !have_content) {
7367 if (*path_unstaged_content &&
7368 unlink(*path_unstaged_content) == -1 && err == NULL)
7369 err = got_error_from_errno2("unlink",
7370 *path_unstaged_content);
7371 free(*path_unstaged_content);
7372 *path_unstaged_content = NULL;
7374 if (err || !have_content || !have_rejected_content) {
7375 if (*path_new_staged_content &&
7376 unlink(*path_new_staged_content) == -1 && err == NULL)
7377 err = got_error_from_errno2("unlink",
7378 *path_new_staged_content);
7379 free(*path_new_staged_content);
7380 *path_new_staged_content = NULL;
7382 free(args);
7383 if (ds) {
7384 got_diff_state_free(ds);
7385 free(ds);
7387 if (changes)
7388 got_diff_free_changes(changes);
7389 free(path1);
7390 free(path2);
7391 return err;
7394 static const struct got_error *
7395 unstage_hunks(struct got_object_id *staged_blob_id,
7396 struct got_blob_object *blob_base,
7397 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7398 const char *ondisk_path, const char *label_orig,
7399 struct got_worktree *worktree, struct got_repository *repo,
7400 got_worktree_patch_cb patch_cb, void *patch_arg,
7401 got_worktree_checkout_cb progress_cb, void *progress_arg)
7403 const struct got_error *err = NULL;
7404 char *path_unstaged_content = NULL;
7405 char *path_new_staged_content = NULL;
7406 struct got_object_id *new_staged_blob_id = NULL;
7407 FILE *f = NULL;
7408 struct stat sb;
7410 err = create_unstaged_content(&path_unstaged_content,
7411 &path_new_staged_content, blob_id, staged_blob_id,
7412 ie->path, repo, patch_cb, patch_arg);
7413 if (err)
7414 return err;
7416 if (path_unstaged_content == NULL)
7417 return NULL;
7419 if (path_new_staged_content) {
7420 err = got_object_blob_create(&new_staged_blob_id,
7421 path_new_staged_content, repo);
7422 if (err)
7423 goto done;
7426 f = fopen(path_unstaged_content, "r");
7427 if (f == NULL) {
7428 err = got_error_from_errno2("fopen",
7429 path_unstaged_content);
7430 goto done;
7432 if (fstat(fileno(f), &sb) == -1) {
7433 err = got_error_from_errno2("fstat", path_unstaged_content);
7434 goto done;
7436 if (got_fileindex_entry_staged_filetype_get(ie) ==
7437 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7438 char link_target[PATH_MAX];
7439 size_t r;
7440 r = fread(link_target, 1, sizeof(link_target), f);
7441 if (r == 0 && ferror(f)) {
7442 err = got_error_from_errno("fread");
7443 goto done;
7445 if (r >= sizeof(link_target)) { /* should not happen */
7446 err = got_error(GOT_ERR_NO_SPACE);
7447 goto done;
7449 link_target[r] = '\0';
7450 err = merge_symlink(worktree, blob_base,
7451 ondisk_path, ie->path, label_orig, link_target,
7452 worktree->base_commit_id, repo, progress_cb,
7453 progress_arg);
7454 } else {
7455 int local_changes_subsumed;
7456 err = merge_file(&local_changes_subsumed, worktree,
7457 blob_base, ondisk_path, ie->path,
7458 got_fileindex_perms_to_st(ie),
7459 path_unstaged_content, label_orig, "unstaged",
7460 repo, progress_cb, progress_arg);
7462 if (err)
7463 goto done;
7465 if (new_staged_blob_id) {
7466 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7467 SHA1_DIGEST_LENGTH);
7468 } else
7469 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7470 done:
7471 free(new_staged_blob_id);
7472 if (path_unstaged_content &&
7473 unlink(path_unstaged_content) == -1 && err == NULL)
7474 err = got_error_from_errno2("unlink", path_unstaged_content);
7475 if (path_new_staged_content &&
7476 unlink(path_new_staged_content) == -1 && err == NULL)
7477 err = got_error_from_errno2("unlink", path_new_staged_content);
7478 if (f && fclose(f) != 0 && err == NULL)
7479 err = got_error_from_errno2("fclose", path_unstaged_content);
7480 free(path_unstaged_content);
7481 free(path_new_staged_content);
7482 return err;
7485 static const struct got_error *
7486 unstage_path(void *arg, unsigned char status,
7487 unsigned char staged_status, const char *relpath,
7488 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7489 struct got_object_id *commit_id, int dirfd, const char *de_name)
7491 const struct got_error *err = NULL;
7492 struct unstage_path_arg *a = arg;
7493 struct got_fileindex_entry *ie;
7494 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7495 char *ondisk_path = NULL;
7496 char *id_str = NULL, *label_orig = NULL;
7497 int local_changes_subsumed;
7498 struct stat sb;
7500 if (staged_status != GOT_STATUS_ADD &&
7501 staged_status != GOT_STATUS_MODIFY &&
7502 staged_status != GOT_STATUS_DELETE)
7503 return NULL;
7505 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7506 if (ie == NULL)
7507 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7509 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7510 == -1)
7511 return got_error_from_errno("asprintf");
7513 err = got_object_id_str(&id_str,
7514 commit_id ? commit_id : a->worktree->base_commit_id);
7515 if (err)
7516 goto done;
7517 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7518 id_str) == -1) {
7519 err = got_error_from_errno("asprintf");
7520 goto done;
7523 switch (staged_status) {
7524 case GOT_STATUS_MODIFY:
7525 err = got_object_open_as_blob(&blob_base, a->repo,
7526 blob_id, 8192);
7527 if (err)
7528 break;
7529 /* fall through */
7530 case GOT_STATUS_ADD:
7531 if (a->patch_cb) {
7532 if (staged_status == GOT_STATUS_ADD) {
7533 int choice = GOT_PATCH_CHOICE_NONE;
7534 err = (*a->patch_cb)(&choice, a->patch_arg,
7535 staged_status, ie->path, NULL, 1, 1);
7536 if (err)
7537 break;
7538 if (choice != GOT_PATCH_CHOICE_YES)
7539 break;
7540 } else {
7541 err = unstage_hunks(staged_blob_id,
7542 blob_base, blob_id, ie, ondisk_path,
7543 label_orig, a->worktree, a->repo,
7544 a->patch_cb, a->patch_arg,
7545 a->progress_cb, a->progress_arg);
7546 break; /* Done with this file. */
7549 err = got_object_open_as_blob(&blob_staged, a->repo,
7550 staged_blob_id, 8192);
7551 if (err)
7552 break;
7553 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7554 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7555 case GOT_FILEIDX_MODE_REGULAR_FILE:
7556 err = merge_blob(&local_changes_subsumed, a->worktree,
7557 blob_base, ondisk_path, relpath,
7558 got_fileindex_perms_to_st(ie), label_orig,
7559 blob_staged, commit_id ? commit_id :
7560 a->worktree->base_commit_id, a->repo,
7561 a->progress_cb, a->progress_arg);
7562 break;
7563 case GOT_FILEIDX_MODE_SYMLINK:
7564 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7565 char *staged_target;
7566 err = got_object_blob_read_to_str(
7567 &staged_target, blob_staged);
7568 if (err)
7569 goto done;
7570 err = merge_symlink(a->worktree, blob_base,
7571 ondisk_path, relpath, label_orig,
7572 staged_target, commit_id ? commit_id :
7573 a->worktree->base_commit_id,
7574 a->repo, a->progress_cb, a->progress_arg);
7575 free(staged_target);
7576 } else {
7577 err = merge_blob(&local_changes_subsumed,
7578 a->worktree, blob_base, ondisk_path,
7579 relpath, got_fileindex_perms_to_st(ie),
7580 label_orig, blob_staged,
7581 commit_id ? commit_id :
7582 a->worktree->base_commit_id, a->repo,
7583 a->progress_cb, a->progress_arg);
7585 break;
7586 default:
7587 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7588 break;
7590 if (err == NULL)
7591 got_fileindex_entry_stage_set(ie,
7592 GOT_FILEIDX_STAGE_NONE);
7593 break;
7594 case GOT_STATUS_DELETE:
7595 if (a->patch_cb) {
7596 int choice = GOT_PATCH_CHOICE_NONE;
7597 err = (*a->patch_cb)(&choice, a->patch_arg,
7598 staged_status, ie->path, NULL, 1, 1);
7599 if (err)
7600 break;
7601 if (choice == GOT_PATCH_CHOICE_NO)
7602 break;
7603 if (choice != GOT_PATCH_CHOICE_YES) {
7604 err = got_error(GOT_ERR_PATCH_CHOICE);
7605 break;
7608 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7609 err = get_file_status(&status, &sb, ie, ondisk_path,
7610 dirfd, de_name, a->repo);
7611 if (err)
7612 break;
7613 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7614 break;
7616 done:
7617 free(ondisk_path);
7618 if (blob_base)
7619 got_object_blob_close(blob_base);
7620 if (blob_staged)
7621 got_object_blob_close(blob_staged);
7622 free(id_str);
7623 free(label_orig);
7624 return err;
7627 const struct got_error *
7628 got_worktree_unstage(struct got_worktree *worktree,
7629 struct got_pathlist_head *paths,
7630 got_worktree_checkout_cb progress_cb, void *progress_arg,
7631 got_worktree_patch_cb patch_cb, void *patch_arg,
7632 struct got_repository *repo)
7634 const struct got_error *err = NULL, *sync_err, *unlockerr;
7635 struct got_pathlist_entry *pe;
7636 struct got_fileindex *fileindex = NULL;
7637 char *fileindex_path = NULL;
7638 struct unstage_path_arg upa;
7640 err = lock_worktree(worktree, LOCK_EX);
7641 if (err)
7642 return err;
7644 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7645 if (err)
7646 goto done;
7648 upa.worktree = worktree;
7649 upa.fileindex = fileindex;
7650 upa.repo = repo;
7651 upa.progress_cb = progress_cb;
7652 upa.progress_arg = progress_arg;
7653 upa.patch_cb = patch_cb;
7654 upa.patch_arg = patch_arg;
7655 TAILQ_FOREACH(pe, paths, entry) {
7656 err = worktree_status(worktree, pe->path, fileindex, repo,
7657 unstage_path, &upa, NULL, NULL, 0, 0);
7658 if (err)
7659 goto done;
7662 sync_err = sync_fileindex(fileindex, fileindex_path);
7663 if (sync_err && err == NULL)
7664 err = sync_err;
7665 done:
7666 free(fileindex_path);
7667 if (fileindex)
7668 got_fileindex_free(fileindex);
7669 unlockerr = lock_worktree(worktree, LOCK_SH);
7670 if (unlockerr && err == NULL)
7671 err = unlockerr;
7672 return err;