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 const struct got_error *
2147 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2149 uint32_t uuid_status;
2151 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2152 if (uuid_status != uuid_s_ok) {
2153 *uuidstr = NULL;
2154 return got_error_uuid(uuid_status, "uuid_to_string");
2157 return NULL;
2160 static const struct got_error *
2161 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2163 const struct got_error *err = NULL;
2164 char *uuidstr = NULL;
2166 *refname = NULL;
2168 err = got_worktree_get_uuid(&uuidstr, worktree);
2169 if (err)
2170 return err;
2172 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2173 err = got_error_from_errno("asprintf");
2174 *refname = NULL;
2176 free(uuidstr);
2177 return err;
2180 const struct got_error *
2181 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2183 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2186 static const struct got_error *
2187 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree,
2190 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2193 static const struct got_error *
2194 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2196 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2241 const struct got_error *
2242 got_worktree_get_histedit_script_path(char **path,
2243 struct got_worktree *worktree)
2245 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2246 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2247 *path = NULL;
2248 return got_error_from_errno("asprintf");
2250 return NULL;
2254 * Prevent Git's garbage collector from deleting our base commit by
2255 * setting a reference to our base commit's ID.
2257 static const struct got_error *
2258 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 struct got_reference *ref = NULL;
2262 char *refname;
2264 err = got_worktree_get_base_ref_name(&refname, worktree);
2265 if (err)
2266 return err;
2268 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2269 if (err)
2270 goto done;
2272 err = got_ref_write(ref, repo);
2273 done:
2274 free(refname);
2275 if (ref)
2276 got_ref_close(ref);
2277 return err;
2280 static const struct got_error *
2281 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2283 const struct got_error *err = NULL;
2285 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2286 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2287 err = got_error_from_errno("asprintf");
2288 *fileindex_path = NULL;
2290 return err;
2294 static const struct got_error *
2295 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2296 struct got_worktree *worktree)
2298 const struct got_error *err = NULL;
2299 FILE *index = NULL;
2301 *fileindex_path = NULL;
2302 *fileindex = got_fileindex_alloc();
2303 if (*fileindex == NULL)
2304 return got_error_from_errno("got_fileindex_alloc");
2306 err = get_fileindex_path(fileindex_path, worktree);
2307 if (err)
2308 goto done;
2310 index = fopen(*fileindex_path, "rb");
2311 if (index == NULL) {
2312 if (errno != ENOENT)
2313 err = got_error_from_errno2("fopen", *fileindex_path);
2314 } else {
2315 err = got_fileindex_read(*fileindex, index);
2316 if (fclose(index) != 0 && err == NULL)
2317 err = got_error_from_errno("fclose");
2319 done:
2320 if (err) {
2321 free(*fileindex_path);
2322 *fileindex_path = NULL;
2323 got_fileindex_free(*fileindex);
2324 *fileindex = NULL;
2326 return err;
2329 struct bump_base_commit_id_arg {
2330 struct got_object_id *base_commit_id;
2331 const char *path;
2332 size_t path_len;
2333 const char *entry_name;
2334 got_worktree_checkout_cb progress_cb;
2335 void *progress_arg;
2338 /* Bump base commit ID of all files within an updated part of the work tree. */
2339 static const struct got_error *
2340 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2342 const struct got_error *err;
2343 struct bump_base_commit_id_arg *a = arg;
2345 if (a->entry_name) {
2346 if (strcmp(ie->path, a->path) != 0)
2347 return NULL;
2348 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2349 return NULL;
2351 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2352 SHA1_DIGEST_LENGTH) == 0)
2353 return NULL;
2355 if (a->progress_cb) {
2356 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2357 ie->path);
2358 if (err)
2359 return err;
2361 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2362 return NULL;
2365 static const struct got_error *
2366 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2368 const struct got_error *err = NULL;
2369 char *new_fileindex_path = NULL;
2370 FILE *new_index = NULL;
2371 struct timespec timeout;
2373 err = got_opentemp_named(&new_fileindex_path, &new_index,
2374 fileindex_path);
2375 if (err)
2376 goto done;
2378 err = got_fileindex_write(fileindex, new_index);
2379 if (err)
2380 goto done;
2382 if (rename(new_fileindex_path, fileindex_path) != 0) {
2383 err = got_error_from_errno3("rename", new_fileindex_path,
2384 fileindex_path);
2385 unlink(new_fileindex_path);
2389 * Sleep for a short amount of time to ensure that files modified after
2390 * this program exits have a different time stamp from the one which
2391 * was recorded in the file index.
2393 timeout.tv_sec = 0;
2394 timeout.tv_nsec = 1;
2395 nanosleep(&timeout, NULL);
2396 done:
2397 if (new_index)
2398 fclose(new_index);
2399 free(new_fileindex_path);
2400 return err;
2403 static const struct got_error *
2404 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2405 struct got_object_id **tree_id, const char *wt_relpath,
2406 struct got_worktree *worktree, struct got_repository *repo)
2408 const struct got_error *err = NULL;
2409 struct got_object_id *id = NULL;
2410 char *in_repo_path = NULL;
2411 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2413 *entry_type = GOT_OBJ_TYPE_ANY;
2414 *tree_relpath = NULL;
2415 *tree_id = NULL;
2417 if (wt_relpath[0] == '\0') {
2418 /* Check out all files within the work tree. */
2419 *entry_type = GOT_OBJ_TYPE_TREE;
2420 *tree_relpath = strdup("");
2421 if (*tree_relpath == NULL) {
2422 err = got_error_from_errno("strdup");
2423 goto done;
2425 err = got_object_id_by_path(tree_id, repo,
2426 worktree->base_commit_id, worktree->path_prefix);
2427 if (err)
2428 goto done;
2429 return NULL;
2432 /* Check out a subset of files in the work tree. */
2434 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2435 is_root_wt ? "" : "/", wt_relpath) == -1) {
2436 err = got_error_from_errno("asprintf");
2437 goto done;
2440 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2441 in_repo_path);
2442 if (err)
2443 goto done;
2445 free(in_repo_path);
2446 in_repo_path = NULL;
2448 err = got_object_get_type(entry_type, repo, id);
2449 if (err)
2450 goto done;
2452 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2453 /* Check out a single file. */
2454 if (strchr(wt_relpath, '/') == NULL) {
2455 /* Check out a single file in work tree's root dir. */
2456 in_repo_path = strdup(worktree->path_prefix);
2457 if (in_repo_path == NULL) {
2458 err = got_error_from_errno("strdup");
2459 goto done;
2461 *tree_relpath = strdup("");
2462 if (*tree_relpath == NULL) {
2463 err = got_error_from_errno("strdup");
2464 goto done;
2466 } else {
2467 /* Check out a single file in a subdirectory. */
2468 err = got_path_dirname(tree_relpath, wt_relpath);
2469 if (err)
2470 return err;
2471 if (asprintf(&in_repo_path, "%s%s%s",
2472 worktree->path_prefix, is_root_wt ? "" : "/",
2473 *tree_relpath) == -1) {
2474 err = got_error_from_errno("asprintf");
2475 goto done;
2478 err = got_object_id_by_path(tree_id, repo,
2479 worktree->base_commit_id, in_repo_path);
2480 } else {
2481 /* Check out all files within a subdirectory. */
2482 *tree_id = got_object_id_dup(id);
2483 if (*tree_id == NULL) {
2484 err = got_error_from_errno("got_object_id_dup");
2485 goto done;
2487 *tree_relpath = strdup(wt_relpath);
2488 if (*tree_relpath == NULL) {
2489 err = got_error_from_errno("strdup");
2490 goto done;
2493 done:
2494 free(id);
2495 free(in_repo_path);
2496 if (err) {
2497 *entry_type = GOT_OBJ_TYPE_ANY;
2498 free(*tree_relpath);
2499 *tree_relpath = NULL;
2500 free(*tree_id);
2501 *tree_id = NULL;
2503 return err;
2506 static const struct got_error *
2507 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2508 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2509 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2510 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2512 const struct got_error *err = NULL;
2513 struct got_commit_object *commit = NULL;
2514 struct got_tree_object *tree = NULL;
2515 struct got_fileindex_diff_tree_cb diff_cb;
2516 struct diff_cb_arg arg;
2518 err = ref_base_commit(worktree, repo);
2519 if (err) {
2520 if (!(err->code == GOT_ERR_ERRNO &&
2521 (errno == EACCES || errno == EROFS)))
2522 goto done;
2523 err = (*progress_cb)(progress_arg,
2524 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2525 if (err)
2526 return err;
2529 err = got_object_open_as_commit(&commit, repo,
2530 worktree->base_commit_id);
2531 if (err)
2532 goto done;
2534 err = got_object_open_as_tree(&tree, repo, tree_id);
2535 if (err)
2536 goto done;
2538 if (entry_name &&
2539 got_object_tree_find_entry(tree, entry_name) == NULL) {
2540 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2541 goto done;
2544 diff_cb.diff_old_new = diff_old_new;
2545 diff_cb.diff_old = diff_old;
2546 diff_cb.diff_new = diff_new;
2547 arg.fileindex = fileindex;
2548 arg.worktree = worktree;
2549 arg.repo = repo;
2550 arg.progress_cb = progress_cb;
2551 arg.progress_arg = progress_arg;
2552 arg.cancel_cb = cancel_cb;
2553 arg.cancel_arg = cancel_arg;
2554 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2555 entry_name, repo, &diff_cb, &arg);
2556 done:
2557 if (tree)
2558 got_object_tree_close(tree);
2559 if (commit)
2560 got_object_commit_close(commit);
2561 return err;
2564 const struct got_error *
2565 got_worktree_checkout_files(struct got_worktree *worktree,
2566 struct got_pathlist_head *paths, struct got_repository *repo,
2567 got_worktree_checkout_cb progress_cb, void *progress_arg,
2568 got_cancel_cb cancel_cb, void *cancel_arg)
2570 const struct got_error *err = NULL, *sync_err, *unlockerr;
2571 struct got_commit_object *commit = NULL;
2572 struct got_tree_object *tree = NULL;
2573 struct got_fileindex *fileindex = NULL;
2574 char *fileindex_path = NULL;
2575 struct got_pathlist_entry *pe;
2576 struct tree_path_data {
2577 SIMPLEQ_ENTRY(tree_path_data) entry;
2578 struct got_object_id *tree_id;
2579 int entry_type;
2580 char *relpath;
2581 char *entry_name;
2582 } *tpd = NULL;
2583 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2585 SIMPLEQ_INIT(&tree_paths);
2587 err = lock_worktree(worktree, LOCK_EX);
2588 if (err)
2589 return err;
2591 /* Map all specified paths to in-repository trees. */
2592 TAILQ_FOREACH(pe, paths, entry) {
2593 tpd = malloc(sizeof(*tpd));
2594 if (tpd == NULL) {
2595 err = got_error_from_errno("malloc");
2596 goto done;
2599 err = find_tree_entry_for_checkout(&tpd->entry_type,
2600 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2601 if (err) {
2602 free(tpd);
2603 goto done;
2606 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2607 err = got_path_basename(&tpd->entry_name, pe->path);
2608 if (err) {
2609 free(tpd->relpath);
2610 free(tpd->tree_id);
2611 free(tpd);
2612 goto done;
2614 } else
2615 tpd->entry_name = NULL;
2617 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2621 * Read the file index.
2622 * Checking out files is supposed to be an idempotent operation.
2623 * If the on-disk file index is incomplete we will try to complete it.
2625 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2626 if (err)
2627 goto done;
2629 tpd = SIMPLEQ_FIRST(&tree_paths);
2630 TAILQ_FOREACH(pe, paths, entry) {
2631 struct bump_base_commit_id_arg bbc_arg;
2633 err = checkout_files(worktree, fileindex, tpd->relpath,
2634 tpd->tree_id, tpd->entry_name, repo,
2635 progress_cb, progress_arg, cancel_cb, cancel_arg);
2636 if (err)
2637 break;
2639 bbc_arg.base_commit_id = worktree->base_commit_id;
2640 bbc_arg.entry_name = tpd->entry_name;
2641 bbc_arg.path = pe->path;
2642 bbc_arg.path_len = pe->path_len;
2643 bbc_arg.progress_cb = progress_cb;
2644 bbc_arg.progress_arg = progress_arg;
2645 err = got_fileindex_for_each_entry_safe(fileindex,
2646 bump_base_commit_id, &bbc_arg);
2647 if (err)
2648 break;
2650 tpd = SIMPLEQ_NEXT(tpd, entry);
2652 sync_err = sync_fileindex(fileindex, fileindex_path);
2653 if (sync_err && err == NULL)
2654 err = sync_err;
2655 done:
2656 free(fileindex_path);
2657 if (tree)
2658 got_object_tree_close(tree);
2659 if (commit)
2660 got_object_commit_close(commit);
2661 if (fileindex)
2662 got_fileindex_free(fileindex);
2663 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2664 tpd = SIMPLEQ_FIRST(&tree_paths);
2665 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2666 free(tpd->relpath);
2667 free(tpd->tree_id);
2668 free(tpd);
2670 unlockerr = lock_worktree(worktree, LOCK_SH);
2671 if (unlockerr && err == NULL)
2672 err = unlockerr;
2673 return err;
2676 struct merge_file_cb_arg {
2677 struct got_worktree *worktree;
2678 struct got_fileindex *fileindex;
2679 got_worktree_checkout_cb progress_cb;
2680 void *progress_arg;
2681 got_cancel_cb cancel_cb;
2682 void *cancel_arg;
2683 const char *label_orig;
2684 struct got_object_id *commit_id2;
2687 static const struct got_error *
2688 merge_file_cb(void *arg, struct got_blob_object *blob1,
2689 struct got_blob_object *blob2, struct got_object_id *id1,
2690 struct got_object_id *id2, const char *path1, const char *path2,
2691 mode_t mode1, mode_t mode2, struct got_repository *repo)
2693 static const struct got_error *err = NULL;
2694 struct merge_file_cb_arg *a = arg;
2695 struct got_fileindex_entry *ie;
2696 char *ondisk_path = NULL;
2697 struct stat sb;
2698 unsigned char status;
2699 int local_changes_subsumed;
2701 if (blob1 && blob2) {
2702 ie = got_fileindex_entry_get(a->fileindex, path2,
2703 strlen(path2));
2704 if (ie == NULL)
2705 return (*a->progress_cb)(a->progress_arg,
2706 GOT_STATUS_MISSING, path2);
2708 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2709 path2) == -1)
2710 return got_error_from_errno("asprintf");
2712 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2713 repo);
2714 if (err)
2715 goto done;
2717 if (status == GOT_STATUS_DELETE) {
2718 err = (*a->progress_cb)(a->progress_arg,
2719 GOT_STATUS_MERGE, path2);
2720 goto done;
2722 if (status != GOT_STATUS_NO_CHANGE &&
2723 status != GOT_STATUS_MODIFY &&
2724 status != GOT_STATUS_CONFLICT &&
2725 status != GOT_STATUS_ADD) {
2726 err = (*a->progress_cb)(a->progress_arg, status, path2);
2727 goto done;
2730 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2731 char *link_target2;
2732 err = got_object_blob_read_to_str(&link_target2, blob2);
2733 if (err)
2734 goto done;
2735 err = merge_symlink(a->worktree, blob1, ondisk_path,
2736 path2, a->label_orig, link_target2, a->commit_id2,
2737 repo, a->progress_cb, a->progress_arg);
2738 free(link_target2);
2739 } else {
2740 err = merge_blob(&local_changes_subsumed, a->worktree,
2741 blob1, ondisk_path, path2, sb.st_mode,
2742 a->label_orig, blob2, a->commit_id2, repo,
2743 a->progress_cb, a->progress_arg);
2745 } else if (blob1) {
2746 ie = got_fileindex_entry_get(a->fileindex, path1,
2747 strlen(path1));
2748 if (ie == NULL)
2749 return (*a->progress_cb)(a->progress_arg,
2750 GOT_STATUS_MISSING, path1);
2752 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2753 path1) == -1)
2754 return got_error_from_errno("asprintf");
2756 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2757 repo);
2758 if (err)
2759 goto done;
2761 switch (status) {
2762 case GOT_STATUS_NO_CHANGE:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2765 if (err)
2766 goto done;
2767 err = remove_ondisk_file(a->worktree->root_path, path1);
2768 if (err)
2769 goto done;
2770 if (ie)
2771 got_fileindex_entry_mark_deleted_from_disk(ie);
2772 break;
2773 case GOT_STATUS_DELETE:
2774 case GOT_STATUS_MISSING:
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_DELETE, path1);
2777 if (err)
2778 goto done;
2779 if (ie)
2780 got_fileindex_entry_mark_deleted_from_disk(ie);
2781 break;
2782 case GOT_STATUS_ADD:
2783 case GOT_STATUS_MODIFY:
2784 case GOT_STATUS_CONFLICT:
2785 err = (*a->progress_cb)(a->progress_arg,
2786 GOT_STATUS_CANNOT_DELETE, path1);
2787 if (err)
2788 goto done;
2789 break;
2790 case GOT_STATUS_OBSTRUCTED:
2791 err = (*a->progress_cb)(a->progress_arg, status, path1);
2792 if (err)
2793 goto done;
2794 break;
2795 default:
2796 break;
2798 } else if (blob2) {
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 path2) == -1)
2801 return got_error_from_errno("asprintf");
2802 ie = got_fileindex_entry_get(a->fileindex, path2,
2803 strlen(path2));
2804 if (ie) {
2805 err = get_file_status(&status, &sb, ie, ondisk_path,
2806 -1, NULL, repo);
2807 if (err)
2808 goto done;
2809 if (status != GOT_STATUS_NO_CHANGE &&
2810 status != GOT_STATUS_MODIFY &&
2811 status != GOT_STATUS_CONFLICT &&
2812 status != GOT_STATUS_ADD) {
2813 err = (*a->progress_cb)(a->progress_arg,
2814 status, path2);
2815 goto done;
2817 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2818 char *link_target2;
2819 err = got_object_blob_read_to_str(&link_target2,
2820 blob2);
2821 if (err)
2822 goto done;
2823 err = merge_symlink(a->worktree, NULL,
2824 ondisk_path, path2, a->label_orig,
2825 link_target2, a->commit_id2, repo,
2826 a->progress_cb, a->progress_arg);
2827 free(link_target2);
2828 } else if (S_ISREG(sb.st_mode)) {
2829 err = merge_blob(&local_changes_subsumed,
2830 a->worktree, NULL, ondisk_path, path2,
2831 sb.st_mode, a->label_orig, blob2,
2832 a->commit_id2, repo, a->progress_cb,
2833 a->progress_arg);
2834 } else {
2835 err = got_error_path(ondisk_path,
2836 GOT_ERR_FILE_OBSTRUCTED);
2838 if (err)
2839 goto done;
2840 if (status == GOT_STATUS_DELETE) {
2841 err = got_fileindex_entry_update(ie,
2842 ondisk_path, blob2->id.sha1,
2843 a->worktree->base_commit_id->sha1, 0);
2844 if (err)
2845 goto done;
2847 } else {
2848 int is_bad_symlink = 0;
2849 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2850 if (S_ISLNK(mode2)) {
2851 err = install_symlink(&is_bad_symlink,
2852 a->worktree, ondisk_path, path2, blob2, 0,
2853 0, 1, repo, a->progress_cb, a->progress_arg);
2854 } else {
2855 err = install_blob(a->worktree, ondisk_path, path2,
2856 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2857 a->progress_cb, a->progress_arg);
2859 if (err)
2860 goto done;
2861 err = got_fileindex_entry_alloc(&ie, path2);
2862 if (err)
2863 goto done;
2864 err = got_fileindex_entry_update(ie, ondisk_path,
2865 NULL, NULL, 1);
2866 if (err) {
2867 got_fileindex_entry_free(ie);
2868 goto done;
2870 err = got_fileindex_entry_add(a->fileindex, ie);
2871 if (err) {
2872 got_fileindex_entry_free(ie);
2873 goto done;
2875 if (is_bad_symlink) {
2876 got_fileindex_entry_filetype_set(ie,
2877 GOT_FILEIDX_MODE_BAD_SYMLINK);
2881 done:
2882 free(ondisk_path);
2883 return err;
2886 struct check_merge_ok_arg {
2887 struct got_worktree *worktree;
2888 struct got_repository *repo;
2891 static const struct got_error *
2892 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2894 const struct got_error *err = NULL;
2895 struct check_merge_ok_arg *a = arg;
2896 unsigned char status;
2897 struct stat sb;
2898 char *ondisk_path;
2900 /* Reject merges into a work tree with mixed base commits. */
2901 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2902 SHA1_DIGEST_LENGTH))
2903 return got_error(GOT_ERR_MIXED_COMMITS);
2905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2906 == -1)
2907 return got_error_from_errno("asprintf");
2909 /* Reject merges into a work tree with conflicted files. */
2910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2911 if (err)
2912 return err;
2913 if (status == GOT_STATUS_CONFLICT)
2914 return got_error(GOT_ERR_CONFLICTS);
2916 return NULL;
2919 static const struct got_error *
2920 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2921 const char *fileindex_path, struct got_object_id *commit_id1,
2922 struct got_object_id *commit_id2, struct got_repository *repo,
2923 got_worktree_checkout_cb progress_cb, void *progress_arg,
2924 got_cancel_cb cancel_cb, void *cancel_arg)
2926 const struct got_error *err = NULL, *sync_err;
2927 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2928 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2929 struct merge_file_cb_arg arg;
2930 char *label_orig = NULL;
2932 if (commit_id1) {
2933 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2934 worktree->path_prefix);
2935 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2936 goto done;
2938 if (tree_id1) {
2939 char *id_str;
2941 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2942 if (err)
2943 goto done;
2945 err = got_object_id_str(&id_str, commit_id1);
2946 if (err)
2947 goto done;
2949 if (asprintf(&label_orig, "%s: commit %s",
2950 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2951 err = got_error_from_errno("asprintf");
2952 free(id_str);
2953 goto done;
2955 free(id_str);
2958 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2959 worktree->path_prefix);
2960 if (err)
2961 goto done;
2963 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2964 if (err)
2965 goto done;
2967 arg.worktree = worktree;
2968 arg.fileindex = fileindex;
2969 arg.progress_cb = progress_cb;
2970 arg.progress_arg = progress_arg;
2971 arg.cancel_cb = cancel_cb;
2972 arg.cancel_arg = cancel_arg;
2973 arg.label_orig = label_orig;
2974 arg.commit_id2 = commit_id2;
2975 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2976 sync_err = sync_fileindex(fileindex, fileindex_path);
2977 if (sync_err && err == NULL)
2978 err = sync_err;
2979 done:
2980 if (tree1)
2981 got_object_tree_close(tree1);
2982 if (tree2)
2983 got_object_tree_close(tree2);
2984 free(label_orig);
2985 return err;
2988 const struct got_error *
2989 got_worktree_merge_files(struct got_worktree *worktree,
2990 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2991 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2992 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2994 const struct got_error *err, *unlockerr;
2995 char *fileindex_path = NULL;
2996 struct got_fileindex *fileindex = NULL;
2997 struct check_merge_ok_arg mok_arg;
2999 err = lock_worktree(worktree, LOCK_EX);
3000 if (err)
3001 return err;
3003 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3004 if (err)
3005 goto done;
3007 mok_arg.worktree = worktree;
3008 mok_arg.repo = repo;
3009 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3010 &mok_arg);
3011 if (err)
3012 goto done;
3014 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3015 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3016 done:
3017 if (fileindex)
3018 got_fileindex_free(fileindex);
3019 free(fileindex_path);
3020 unlockerr = lock_worktree(worktree, LOCK_SH);
3021 if (unlockerr && err == NULL)
3022 err = unlockerr;
3023 return err;
3026 struct diff_dir_cb_arg {
3027 struct got_fileindex *fileindex;
3028 struct got_worktree *worktree;
3029 const char *status_path;
3030 size_t status_path_len;
3031 struct got_repository *repo;
3032 got_worktree_status_cb status_cb;
3033 void *status_arg;
3034 got_cancel_cb cancel_cb;
3035 void *cancel_arg;
3036 /* A pathlist containing per-directory pathlists of ignore patterns. */
3037 struct got_pathlist_head ignores;
3038 int report_unchanged;
3039 int no_ignores;
3042 static const struct got_error *
3043 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3044 int dirfd, const char *de_name,
3045 got_worktree_status_cb status_cb, void *status_arg,
3046 struct got_repository *repo, int report_unchanged)
3048 const struct got_error *err = NULL;
3049 unsigned char status = GOT_STATUS_NO_CHANGE;
3050 unsigned char staged_status = get_staged_status(ie);
3051 struct stat sb;
3052 struct got_object_id blob_id, commit_id, staged_blob_id;
3053 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3054 struct got_object_id *staged_blob_idp = NULL;
3056 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3057 if (err)
3058 return err;
3060 if (status == GOT_STATUS_NO_CHANGE &&
3061 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3062 return NULL;
3064 if (got_fileindex_entry_has_blob(ie)) {
3065 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3066 blob_idp = &blob_id;
3068 if (got_fileindex_entry_has_commit(ie)) {
3069 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3070 commit_idp = &commit_id;
3072 if (staged_status == GOT_STATUS_ADD ||
3073 staged_status == GOT_STATUS_MODIFY) {
3074 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3075 SHA1_DIGEST_LENGTH);
3076 staged_blob_idp = &staged_blob_id;
3079 return (*status_cb)(status_arg, status, staged_status,
3080 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3083 static const struct got_error *
3084 status_old_new(void *arg, struct got_fileindex_entry *ie,
3085 struct dirent *de, const char *parent_path, int dirfd)
3087 const struct got_error *err = NULL;
3088 struct diff_dir_cb_arg *a = arg;
3089 char *abspath;
3091 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3092 return got_error(GOT_ERR_CANCELLED);
3094 if (got_path_cmp(parent_path, a->status_path,
3095 strlen(parent_path), a->status_path_len) != 0 &&
3096 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3097 return NULL;
3099 if (parent_path[0]) {
3100 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3101 parent_path, de->d_name) == -1)
3102 return got_error_from_errno("asprintf");
3103 } else {
3104 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3105 de->d_name) == -1)
3106 return got_error_from_errno("asprintf");
3109 err = report_file_status(ie, abspath, dirfd, de->d_name,
3110 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3111 free(abspath);
3112 return err;
3115 static const struct got_error *
3116 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3118 struct diff_dir_cb_arg *a = arg;
3119 struct got_object_id blob_id, commit_id;
3120 unsigned char status;
3122 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3123 return got_error(GOT_ERR_CANCELLED);
3125 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3126 return NULL;
3128 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3129 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3130 if (got_fileindex_entry_has_file_on_disk(ie))
3131 status = GOT_STATUS_MISSING;
3132 else
3133 status = GOT_STATUS_DELETE;
3134 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3135 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3138 void
3139 free_ignorelist(struct got_pathlist_head *ignorelist)
3141 struct got_pathlist_entry *pe;
3143 TAILQ_FOREACH(pe, ignorelist, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(ignorelist);
3148 void
3149 free_ignores(struct got_pathlist_head *ignores)
3151 struct got_pathlist_entry *pe;
3153 TAILQ_FOREACH(pe, ignores, entry) {
3154 struct got_pathlist_head *ignorelist = pe->data;
3155 free_ignorelist(ignorelist);
3156 free((char *)pe->path);
3158 got_pathlist_free(ignores);
3161 static const struct got_error *
3162 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3164 const struct got_error *err = NULL;
3165 struct got_pathlist_entry *pe = NULL;
3166 struct got_pathlist_head *ignorelist;
3167 char *line = NULL, *pattern, *dirpath = NULL;
3168 size_t linesize = 0;
3169 ssize_t linelen;
3171 ignorelist = calloc(1, sizeof(*ignorelist));
3172 if (ignorelist == NULL)
3173 return got_error_from_errno("calloc");
3174 TAILQ_INIT(ignorelist);
3176 while ((linelen = getline(&line, &linesize, f)) != -1) {
3177 if (linelen > 0 && line[linelen - 1] == '\n')
3178 line[linelen - 1] = '\0';
3180 /* Git's ignores may contain comments. */
3181 if (line[0] == '#')
3182 continue;
3184 /* Git's negated patterns are not (yet?) supported. */
3185 if (line[0] == '!')
3186 continue;
3188 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3189 line) == -1) {
3190 err = got_error_from_errno("asprintf");
3191 goto done;
3193 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3194 if (err)
3195 goto done;
3197 if (ferror(f)) {
3198 err = got_error_from_errno("getline");
3199 goto done;
3202 dirpath = strdup(path);
3203 if (dirpath == NULL) {
3204 err = got_error_from_errno("strdup");
3205 goto done;
3207 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3208 done:
3209 free(line);
3210 if (err || pe == NULL) {
3211 free(dirpath);
3212 free_ignorelist(ignorelist);
3214 return err;
3217 int
3218 match_ignores(struct got_pathlist_head *ignores, const char *path)
3220 struct got_pathlist_entry *pe;
3222 /* Handle patterns which match in all directories. */
3223 TAILQ_FOREACH(pe, ignores, entry) {
3224 struct got_pathlist_head *ignorelist = pe->data;
3225 struct got_pathlist_entry *pi;
3227 TAILQ_FOREACH(pi, ignorelist, entry) {
3228 const char *p, *pattern = pi->path;
3230 if (strncmp(pattern, "**/", 3) != 0)
3231 continue;
3232 pattern += 3;
3233 p = path;
3234 while (*p) {
3235 if (fnmatch(pattern, p,
3236 FNM_PATHNAME | FNM_LEADING_DIR)) {
3237 /* Retry in next directory. */
3238 while (*p && *p != '/')
3239 p++;
3240 while (*p == '/')
3241 p++;
3242 continue;
3244 return 1;
3250 * The ignores pathlist contains ignore lists from children before
3251 * parents, so we can find the most specific ignorelist by walking
3252 * ignores backwards.
3254 pe = TAILQ_LAST(ignores, got_pathlist_head);
3255 while (pe) {
3256 if (got_path_is_child(path, pe->path, pe->path_len)) {
3257 struct got_pathlist_head *ignorelist = pe->data;
3258 struct got_pathlist_entry *pi;
3259 TAILQ_FOREACH(pi, ignorelist, entry) {
3260 const char *pattern = pi->path;
3261 int flags = FNM_LEADING_DIR;
3262 if (strstr(pattern, "/**/") == NULL)
3263 flags |= FNM_PATHNAME;
3264 if (fnmatch(pattern, path, flags))
3265 continue;
3266 return 1;
3269 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3272 return 0;
3275 static const struct got_error *
3276 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3277 const char *path, int dirfd, const char *ignores_filename)
3279 const struct got_error *err = NULL;
3280 char *ignorespath;
3281 int fd = -1;
3282 FILE *ignoresfile = NULL;
3284 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3285 path[0] ? "/" : "", ignores_filename) == -1)
3286 return got_error_from_errno("asprintf");
3288 if (dirfd != -1) {
3289 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3290 if (fd == -1) {
3291 if (errno != ENOENT && errno != EACCES)
3292 err = got_error_from_errno2("openat",
3293 ignorespath);
3294 } else {
3295 ignoresfile = fdopen(fd, "r");
3296 if (ignoresfile == NULL)
3297 err = got_error_from_errno2("fdopen",
3298 ignorespath);
3299 else {
3300 fd = -1;
3301 err = read_ignores(ignores, path, ignoresfile);
3304 } else {
3305 ignoresfile = fopen(ignorespath, "r");
3306 if (ignoresfile == NULL) {
3307 if (errno != ENOENT && errno != EACCES)
3308 err = got_error_from_errno2("fopen",
3309 ignorespath);
3310 } else
3311 err = read_ignores(ignores, path, ignoresfile);
3314 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3315 err = got_error_from_errno2("fclose", path);
3316 if (fd != -1 && close(fd) == -1 && err == NULL)
3317 err = got_error_from_errno2("close", path);
3318 free(ignorespath);
3319 return err;
3322 static const struct got_error *
3323 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3325 const struct got_error *err = NULL;
3326 struct diff_dir_cb_arg *a = arg;
3327 char *path = NULL;
3329 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3330 return got_error(GOT_ERR_CANCELLED);
3332 if (parent_path[0]) {
3333 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3334 return got_error_from_errno("asprintf");
3335 } else {
3336 path = de->d_name;
3339 if (de->d_type != DT_DIR &&
3340 got_path_is_child(path, a->status_path, a->status_path_len)
3341 && !match_ignores(&a->ignores, path))
3342 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3343 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3344 if (parent_path[0])
3345 free(path);
3346 return err;
3349 static const struct got_error *
3350 status_traverse(void *arg, const char *path, int dirfd)
3352 const struct got_error *err = NULL;
3353 struct diff_dir_cb_arg *a = arg;
3355 if (a->no_ignores)
3356 return NULL;
3358 err = add_ignores(&a->ignores, a->worktree->root_path,
3359 path, dirfd, ".cvsignore");
3360 if (err)
3361 return err;
3363 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3364 dirfd, ".gitignore");
3366 return err;
3369 static const struct got_error *
3370 report_single_file_status(const char *path, const char *ondisk_path,
3371 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3372 void *status_arg, struct got_repository *repo, int report_unchanged)
3374 struct got_fileindex_entry *ie;
3375 struct stat sb;
3377 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3378 if (ie)
3379 return report_file_status(ie, ondisk_path, -1, NULL,
3380 status_cb, status_arg, repo, report_unchanged);
3382 if (lstat(ondisk_path, &sb) == -1) {
3383 if (errno != ENOENT)
3384 return got_error_from_errno2("lstat", ondisk_path);
3385 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3386 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3387 return NULL;
3390 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3391 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3392 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3394 return NULL;
3397 static const struct got_error *
3398 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3399 const char *root_path, const char *path)
3401 const struct got_error *err;
3402 char *parent_path, *next_parent_path = NULL;
3404 err = add_ignores(ignores, root_path, "", -1,
3405 ".cvsignore");
3406 if (err)
3407 return err;
3409 err = add_ignores(ignores, root_path, "", -1,
3410 ".gitignore");
3411 if (err)
3412 return err;
3414 err = got_path_dirname(&parent_path, path);
3415 if (err) {
3416 if (err->code == GOT_ERR_BAD_PATH)
3417 return NULL; /* cannot traverse parent */
3418 return err;
3420 for (;;) {
3421 err = add_ignores(ignores, root_path, parent_path, -1,
3422 ".cvsignore");
3423 if (err)
3424 break;
3425 err = add_ignores(ignores, root_path, parent_path, -1,
3426 ".gitignore");
3427 if (err)
3428 break;
3429 err = got_path_dirname(&next_parent_path, parent_path);
3430 if (err) {
3431 if (err->code == GOT_ERR_BAD_PATH)
3432 err = NULL; /* traversed everything */
3433 break;
3435 free(parent_path);
3436 parent_path = next_parent_path;
3437 next_parent_path = NULL;
3440 free(parent_path);
3441 free(next_parent_path);
3442 return err;
3445 static const struct got_error *
3446 worktree_status(struct got_worktree *worktree, const char *path,
3447 struct got_fileindex *fileindex, struct got_repository *repo,
3448 got_worktree_status_cb status_cb, void *status_arg,
3449 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3450 int report_unchanged)
3452 const struct got_error *err = NULL;
3453 int fd = -1;
3454 struct got_fileindex_diff_dir_cb fdiff_cb;
3455 struct diff_dir_cb_arg arg;
3456 char *ondisk_path = NULL;
3458 TAILQ_INIT(&arg.ignores);
3460 if (asprintf(&ondisk_path, "%s%s%s",
3461 worktree->root_path, path[0] ? "/" : "", path) == -1)
3462 return got_error_from_errno("asprintf");
3464 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3465 if (fd == -1) {
3466 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3467 errno != ELOOP)
3468 err = got_error_from_errno2("open", ondisk_path);
3469 else
3470 err = report_single_file_status(path, ondisk_path,
3471 fileindex, status_cb, status_arg, repo,
3472 report_unchanged);
3473 } else {
3474 fdiff_cb.diff_old_new = status_old_new;
3475 fdiff_cb.diff_old = status_old;
3476 fdiff_cb.diff_new = status_new;
3477 fdiff_cb.diff_traverse = status_traverse;
3478 arg.fileindex = fileindex;
3479 arg.worktree = worktree;
3480 arg.status_path = path;
3481 arg.status_path_len = strlen(path);
3482 arg.repo = repo;
3483 arg.status_cb = status_cb;
3484 arg.status_arg = status_arg;
3485 arg.cancel_cb = cancel_cb;
3486 arg.cancel_arg = cancel_arg;
3487 arg.report_unchanged = report_unchanged;
3488 arg.no_ignores = no_ignores;
3489 if (!no_ignores) {
3490 err = add_ignores_from_parent_paths(&arg.ignores,
3491 worktree->root_path, path);
3492 if (err)
3493 goto done;
3495 err = got_fileindex_diff_dir(fileindex, fd,
3496 worktree->root_path, path, repo, &fdiff_cb, &arg);
3498 done:
3499 free_ignores(&arg.ignores);
3500 if (fd != -1 && close(fd) != 0 && err == NULL)
3501 err = got_error_from_errno("close");
3502 free(ondisk_path);
3503 return err;
3506 const struct got_error *
3507 got_worktree_status(struct got_worktree *worktree,
3508 struct got_pathlist_head *paths, struct got_repository *repo,
3509 got_worktree_status_cb status_cb, void *status_arg,
3510 got_cancel_cb cancel_cb, void *cancel_arg)
3512 const struct got_error *err = NULL;
3513 char *fileindex_path = NULL;
3514 struct got_fileindex *fileindex = NULL;
3515 struct got_pathlist_entry *pe;
3517 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3518 if (err)
3519 return err;
3521 TAILQ_FOREACH(pe, paths, entry) {
3522 err = worktree_status(worktree, pe->path, fileindex, repo,
3523 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3524 if (err)
3525 break;
3527 free(fileindex_path);
3528 got_fileindex_free(fileindex);
3529 return err;
3532 const struct got_error *
3533 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3534 const char *arg)
3536 const struct got_error *err = NULL;
3537 char *resolved = NULL, *cwd = NULL, *path = NULL;
3538 size_t len;
3539 struct stat sb;
3541 *wt_path = NULL;
3543 cwd = getcwd(NULL, 0);
3544 if (cwd == NULL)
3545 return got_error_from_errno("getcwd");
3547 if (lstat(arg, &sb) == -1) {
3548 if (errno != ENOENT) {
3549 err = got_error_from_errno2("lstat", arg);
3550 goto done;
3553 if (S_ISLNK(sb.st_mode)) {
3555 * We cannot use realpath(3) with symlinks since we want to
3556 * operate on the symlink itself.
3557 * But we can make the path absolute, assuming it is relative
3558 * to the current working directory, and then canonicalize it.
3560 char *abspath = NULL;
3561 char canonpath[PATH_MAX];
3562 if (!got_path_is_absolute(arg)) {
3563 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 goto done;
3569 err = got_canonpath(abspath ? abspath : arg, canonpath,
3570 sizeof(canonpath));
3571 if (err)
3572 goto done;
3573 resolved = strdup(canonpath);
3574 if (resolved == NULL) {
3575 err = got_error_from_errno("strdup");
3576 goto done;
3578 } else {
3579 resolved = realpath(arg, NULL);
3580 if (resolved == NULL) {
3581 if (errno != ENOENT) {
3582 err = got_error_from_errno2("realpath", arg);
3583 goto done;
3585 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3586 err = got_error_from_errno("asprintf");
3587 goto done;
3592 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3593 strlen(got_worktree_get_root_path(worktree)))) {
3594 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3595 goto done;
3598 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3599 err = got_path_skip_common_ancestor(&path,
3600 got_worktree_get_root_path(worktree), resolved);
3601 if (err)
3602 goto done;
3603 } else {
3604 path = strdup("");
3605 if (path == NULL) {
3606 err = got_error_from_errno("strdup");
3607 goto done;
3611 /* XXX status walk can't deal with trailing slash! */
3612 len = strlen(path);
3613 while (len > 0 && path[len - 1] == '/') {
3614 path[len - 1] = '\0';
3615 len--;
3617 done:
3618 free(resolved);
3619 free(cwd);
3620 if (err == NULL)
3621 *wt_path = path;
3622 else
3623 free(path);
3624 return err;
3627 struct schedule_addition_args {
3628 struct got_worktree *worktree;
3629 struct got_fileindex *fileindex;
3630 got_worktree_checkout_cb progress_cb;
3631 void *progress_arg;
3632 struct got_repository *repo;
3635 static const struct got_error *
3636 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3637 const char *relpath, struct got_object_id *blob_id,
3638 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3639 int dirfd, const char *de_name)
3641 struct schedule_addition_args *a = arg;
3642 const struct got_error *err = NULL;
3643 struct got_fileindex_entry *ie;
3644 struct stat sb;
3645 char *ondisk_path;
3647 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3648 relpath) == -1)
3649 return got_error_from_errno("asprintf");
3651 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3652 if (ie) {
3653 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3654 de_name, a->repo);
3655 if (err)
3656 goto done;
3657 /* Re-adding an existing entry is a no-op. */
3658 if (status == GOT_STATUS_ADD)
3659 goto done;
3660 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3661 if (err)
3662 goto done;
3665 if (status != GOT_STATUS_UNVERSIONED) {
3666 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3667 goto done;
3670 err = got_fileindex_entry_alloc(&ie, relpath);
3671 if (err)
3672 goto done;
3673 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3674 if (err) {
3675 got_fileindex_entry_free(ie);
3676 goto done;
3678 err = got_fileindex_entry_add(a->fileindex, ie);
3679 if (err) {
3680 got_fileindex_entry_free(ie);
3681 goto done;
3683 done:
3684 free(ondisk_path);
3685 if (err)
3686 return err;
3687 if (status == GOT_STATUS_ADD)
3688 return NULL;
3689 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3692 const struct got_error *
3693 got_worktree_schedule_add(struct got_worktree *worktree,
3694 struct got_pathlist_head *paths,
3695 got_worktree_checkout_cb progress_cb, void *progress_arg,
3696 struct got_repository *repo, int no_ignores)
3698 struct got_fileindex *fileindex = NULL;
3699 char *fileindex_path = NULL;
3700 const struct got_error *err = NULL, *sync_err, *unlockerr;
3701 struct got_pathlist_entry *pe;
3702 struct schedule_addition_args saa;
3704 err = lock_worktree(worktree, LOCK_EX);
3705 if (err)
3706 return err;
3708 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3709 if (err)
3710 goto done;
3712 saa.worktree = worktree;
3713 saa.fileindex = fileindex;
3714 saa.progress_cb = progress_cb;
3715 saa.progress_arg = progress_arg;
3716 saa.repo = repo;
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3721 if (err)
3722 break;
3724 sync_err = sync_fileindex(fileindex, fileindex_path);
3725 if (sync_err && err == NULL)
3726 err = sync_err;
3727 done:
3728 free(fileindex_path);
3729 if (fileindex)
3730 got_fileindex_free(fileindex);
3731 unlockerr = lock_worktree(worktree, LOCK_SH);
3732 if (unlockerr && err == NULL)
3733 err = unlockerr;
3734 return err;
3737 struct schedule_deletion_args {
3738 struct got_worktree *worktree;
3739 struct got_fileindex *fileindex;
3740 got_worktree_delete_cb progress_cb;
3741 void *progress_arg;
3742 struct got_repository *repo;
3743 int delete_local_mods;
3744 int keep_on_disk;
3745 const char *status_codes;
3748 static const struct got_error *
3749 schedule_for_deletion(void *arg, unsigned char status,
3750 unsigned char staged_status, const char *relpath,
3751 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3752 struct got_object_id *commit_id, int dirfd, const char *de_name)
3754 struct schedule_deletion_args *a = arg;
3755 const struct got_error *err = NULL;
3756 struct got_fileindex_entry *ie = NULL;
3757 struct stat sb;
3758 char *ondisk_path, *parent = NULL;
3760 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3761 if (ie == NULL)
3762 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3764 staged_status = get_staged_status(ie);
3765 if (staged_status != GOT_STATUS_NO_CHANGE) {
3766 if (staged_status == GOT_STATUS_DELETE)
3767 return NULL;
3768 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3771 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3772 relpath) == -1)
3773 return got_error_from_errno("asprintf");
3775 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3776 a->repo);
3777 if (err)
3778 goto done;
3780 if (a->status_codes) {
3781 size_t ncodes = strlen(a->status_codes);
3782 int i;
3783 for (i = 0; i < ncodes ; i++) {
3784 if (status == a->status_codes[i])
3785 break;
3787 if (i == ncodes) {
3788 /* Do not delete files in non-matching status. */
3789 free(ondisk_path);
3790 return NULL;
3792 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3793 a->status_codes[i] != GOT_STATUS_MISSING) {
3794 static char msg[64];
3795 snprintf(msg, sizeof(msg),
3796 "invalid status code '%c'", a->status_codes[i]);
3797 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3798 goto done;
3802 if (status != GOT_STATUS_NO_CHANGE) {
3803 if (status == GOT_STATUS_DELETE)
3804 goto done;
3805 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3806 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3807 goto done;
3809 if (status != GOT_STATUS_MODIFY &&
3810 status != GOT_STATUS_MISSING) {
3811 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3812 goto done;
3816 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3817 if (dirfd != -1) {
3818 if (unlinkat(dirfd, de_name, 0) != 0) {
3819 err = got_error_from_errno2("unlinkat",
3820 ondisk_path);
3821 goto done;
3823 } else if (unlink(ondisk_path) != 0) {
3824 err = got_error_from_errno2("unlink", ondisk_path);
3825 goto done;
3828 parent = dirname(ondisk_path);
3830 if (parent == NULL) {
3831 err = got_error_from_errno2("dirname", ondisk_path);
3832 goto done;
3834 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3835 if (rmdir(parent) == -1) {
3836 if (errno != ENOTEMPTY)
3837 err = got_error_from_errno2("rmdir",
3838 parent);
3839 break;
3841 parent = dirname(parent);
3842 if (parent == NULL) {
3843 err = got_error_from_errno2("dirname", parent);
3844 goto done;
3849 got_fileindex_entry_mark_deleted_from_disk(ie);
3850 done:
3851 free(ondisk_path);
3852 if (err)
3853 return err;
3854 if (status == GOT_STATUS_DELETE)
3855 return NULL;
3856 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3857 staged_status, relpath);
3860 const struct got_error *
3861 got_worktree_schedule_delete(struct got_worktree *worktree,
3862 struct got_pathlist_head *paths, int delete_local_mods,
3863 const char *status_codes,
3864 got_worktree_delete_cb progress_cb, void *progress_arg,
3865 struct got_repository *repo, int keep_on_disk)
3867 struct got_fileindex *fileindex = NULL;
3868 char *fileindex_path = NULL;
3869 const struct got_error *err = NULL, *sync_err, *unlockerr;
3870 struct got_pathlist_entry *pe;
3871 struct schedule_deletion_args sda;
3873 err = lock_worktree(worktree, LOCK_EX);
3874 if (err)
3875 return err;
3877 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3878 if (err)
3879 goto done;
3881 sda.worktree = worktree;
3882 sda.fileindex = fileindex;
3883 sda.progress_cb = progress_cb;
3884 sda.progress_arg = progress_arg;
3885 sda.repo = repo;
3886 sda.delete_local_mods = delete_local_mods;
3887 sda.keep_on_disk = keep_on_disk;
3888 sda.status_codes = status_codes;
3890 TAILQ_FOREACH(pe, paths, entry) {
3891 err = worktree_status(worktree, pe->path, fileindex, repo,
3892 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3893 if (err)
3894 break;
3896 sync_err = sync_fileindex(fileindex, fileindex_path);
3897 if (sync_err && err == NULL)
3898 err = sync_err;
3899 done:
3900 free(fileindex_path);
3901 if (fileindex)
3902 got_fileindex_free(fileindex);
3903 unlockerr = lock_worktree(worktree, LOCK_SH);
3904 if (unlockerr && err == NULL)
3905 err = unlockerr;
3906 return err;
3909 static const struct got_error *
3910 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3912 const struct got_error *err = NULL;
3913 char *line = NULL;
3914 size_t linesize = 0, n;
3915 ssize_t linelen;
3917 linelen = getline(&line, &linesize, infile);
3918 if (linelen == -1) {
3919 if (ferror(infile)) {
3920 err = got_error_from_errno("getline");
3921 goto done;
3923 return NULL;
3925 if (outfile) {
3926 n = fwrite(line, 1, linelen, outfile);
3927 if (n != linelen) {
3928 err = got_ferror(outfile, GOT_ERR_IO);
3929 goto done;
3932 if (rejectfile) {
3933 n = fwrite(line, 1, linelen, rejectfile);
3934 if (n != linelen)
3935 err = got_ferror(outfile, GOT_ERR_IO);
3937 done:
3938 free(line);
3939 return err;
3942 static const struct got_error *
3943 skip_one_line(FILE *f)
3945 char *line = NULL;
3946 size_t linesize = 0;
3947 ssize_t linelen;
3949 linelen = getline(&line, &linesize, f);
3950 if (linelen == -1) {
3951 if (ferror(f))
3952 return got_error_from_errno("getline");
3953 return NULL;
3955 free(line);
3956 return NULL;
3959 static const struct got_error *
3960 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3961 int start_old, int end_old, int start_new, int end_new,
3962 FILE *outfile, FILE *rejectfile)
3964 const struct got_error *err;
3966 /* Copy old file's lines leading up to patch. */
3967 while (!feof(f1) && *line_cur1 < start_old) {
3968 err = copy_one_line(f1, outfile, NULL);
3969 if (err)
3970 return err;
3971 (*line_cur1)++;
3973 /* Skip new file's lines leading up to patch. */
3974 while (!feof(f2) && *line_cur2 < start_new) {
3975 if (rejectfile)
3976 err = copy_one_line(f2, NULL, rejectfile);
3977 else
3978 err = skip_one_line(f2);
3979 if (err)
3980 return err;
3981 (*line_cur2)++;
3983 /* Copy patched lines. */
3984 while (!feof(f2) && *line_cur2 <= end_new) {
3985 err = copy_one_line(f2, outfile, NULL);
3986 if (err)
3987 return err;
3988 (*line_cur2)++;
3990 /* Skip over old file's replaced lines. */
3991 while (!feof(f1) && *line_cur1 <= end_old) {
3992 if (rejectfile)
3993 err = copy_one_line(f1, NULL, rejectfile);
3994 else
3995 err = skip_one_line(f1);
3996 if (err)
3997 return err;
3998 (*line_cur1)++;
4001 return NULL;
4004 static const struct got_error *
4005 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4006 FILE *outfile, FILE *rejectfile)
4008 const struct got_error *err;
4010 if (outfile) {
4011 /* Copy old file's lines until EOF. */
4012 while (!feof(f1)) {
4013 err = copy_one_line(f1, outfile, NULL);
4014 if (err)
4015 return err;
4016 (*line_cur1)++;
4019 if (rejectfile) {
4020 /* Copy new file's lines until EOF. */
4021 while (!feof(f2)) {
4022 err = copy_one_line(f2, NULL, rejectfile);
4023 if (err)
4024 return err;
4025 (*line_cur2)++;
4029 return NULL;
4032 static const struct got_error *
4033 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4034 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4035 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4036 int *line_cur2, FILE *outfile, FILE *rejectfile,
4037 got_worktree_patch_cb patch_cb, void *patch_arg)
4039 const struct got_error *err = NULL;
4040 int start_old = change->cv.a;
4041 int end_old = change->cv.b;
4042 int start_new = change->cv.c;
4043 int end_new = change->cv.d;
4044 long pos1, pos2;
4045 FILE *hunkfile;
4047 *choice = GOT_PATCH_CHOICE_NONE;
4049 hunkfile = got_opentemp();
4050 if (hunkfile == NULL)
4051 return got_error_from_errno("got_opentemp");
4053 pos1 = ftell(f1);
4054 pos2 = ftell(f2);
4056 /* XXX TODO needs error checking */
4057 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4059 if (fseek(f1, pos1, SEEK_SET) == -1) {
4060 err = got_ferror(f1, GOT_ERR_IO);
4061 goto done;
4063 if (fseek(f2, pos2, SEEK_SET) == -1) {
4064 err = got_ferror(f1, GOT_ERR_IO);
4065 goto done;
4067 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4068 err = got_ferror(hunkfile, GOT_ERR_IO);
4069 goto done;
4072 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4073 hunkfile, n, nchanges);
4074 if (err)
4075 goto done;
4077 switch (*choice) {
4078 case GOT_PATCH_CHOICE_YES:
4079 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4080 end_old, start_new, end_new, outfile, rejectfile);
4081 break;
4082 case GOT_PATCH_CHOICE_NO:
4083 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4084 end_old, start_new, end_new, rejectfile, outfile);
4085 break;
4086 case GOT_PATCH_CHOICE_QUIT:
4087 break;
4088 default:
4089 err = got_error(GOT_ERR_PATCH_CHOICE);
4090 break;
4092 done:
4093 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4094 err = got_error_from_errno("fclose");
4095 return err;
4098 struct revert_file_args {
4099 struct got_worktree *worktree;
4100 struct got_fileindex *fileindex;
4101 got_worktree_checkout_cb progress_cb;
4102 void *progress_arg;
4103 got_worktree_patch_cb patch_cb;
4104 void *patch_arg;
4105 struct got_repository *repo;
4108 static const struct got_error *
4109 create_patched_content(char **path_outfile, int reverse_patch,
4110 struct got_object_id *blob_id, const char *path2,
4111 int dirfd2, const char *de_name2,
4112 const char *relpath, struct got_repository *repo,
4113 got_worktree_patch_cb patch_cb, void *patch_arg)
4115 const struct got_error *err;
4116 struct got_blob_object *blob = NULL;
4117 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4118 int fd2 = -1;
4119 char link_target[PATH_MAX];
4120 ssize_t link_len = 0;
4121 char *path1 = NULL, *id_str = NULL;
4122 struct stat sb1, sb2;
4123 struct got_diff_changes *changes = NULL;
4124 struct got_diff_state *ds = NULL;
4125 struct got_diff_args *args = NULL;
4126 struct got_diff_change *change;
4127 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4128 int n = 0;
4130 *path_outfile = NULL;
4132 err = got_object_id_str(&id_str, blob_id);
4133 if (err)
4134 return err;
4136 if (dirfd2 != -1) {
4137 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4138 if (fd2 == -1) {
4139 if (errno != ELOOP) {
4140 err = got_error_from_errno2("openat", path2);
4141 goto done;
4143 link_len = readlinkat(dirfd2, de_name2,
4144 link_target, sizeof(link_target));
4145 if (link_len == -1)
4146 return got_error_from_errno2("readlinkat", path2);
4147 sb2.st_mode = S_IFLNK;
4148 sb2.st_size = link_len;
4150 } else {
4151 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4152 if (fd2 == -1) {
4153 if (errno != ELOOP) {
4154 err = got_error_from_errno2("open", path2);
4155 goto done;
4157 link_len = readlink(path2, link_target,
4158 sizeof(link_target));
4159 if (link_len == -1)
4160 return got_error_from_errno2("readlink", path2);
4161 sb2.st_mode = S_IFLNK;
4162 sb2.st_size = link_len;
4165 if (fd2 != -1) {
4166 if (fstat(fd2, &sb2) == -1) {
4167 err = got_error_from_errno2("fstat", path2);
4168 goto done;
4171 f2 = fdopen(fd2, "r");
4172 if (f2 == NULL) {
4173 err = got_error_from_errno2("fdopen", path2);
4174 goto done;
4176 fd2 = -1;
4177 } else {
4178 size_t n;
4179 f2 = got_opentemp();
4180 if (f2 == NULL) {
4181 err = got_error_from_errno2("got_opentemp", path2);
4182 goto done;
4184 n = fwrite(link_target, 1, link_len, f2);
4185 if (n != link_len) {
4186 err = got_ferror(f2, GOT_ERR_IO);
4187 goto done;
4189 if (fflush(f2) == EOF) {
4190 err = got_error_from_errno("fflush");
4191 goto done;
4193 rewind(f2);
4196 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4197 if (err)
4198 goto done;
4200 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4201 if (err)
4202 goto done;
4204 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4205 if (err)
4206 goto done;
4208 if (stat(path1, &sb1) == -1) {
4209 err = got_error_from_errno2("stat", path1);
4210 goto done;
4213 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4214 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4215 if (err)
4216 goto done;
4218 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4219 if (err)
4220 goto done;
4222 if (fseek(f1, 0L, SEEK_SET) == -1)
4223 return got_ferror(f1, GOT_ERR_IO);
4224 if (fseek(f2, 0L, SEEK_SET) == -1)
4225 return got_ferror(f2, GOT_ERR_IO);
4226 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4227 int choice;
4228 err = apply_or_reject_change(&choice, change, ++n,
4229 changes->nchanges, ds, args, diff_flags, relpath,
4230 f1, f2, &line_cur1, &line_cur2,
4231 reverse_patch ? NULL : outfile,
4232 reverse_patch ? outfile : NULL,
4233 patch_cb, patch_arg);
4234 if (err)
4235 goto done;
4236 if (choice == GOT_PATCH_CHOICE_YES)
4237 have_content = 1;
4238 else if (choice == GOT_PATCH_CHOICE_QUIT)
4239 break;
4241 if (have_content) {
4242 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4243 reverse_patch ? NULL : outfile,
4244 reverse_patch ? outfile : NULL);
4245 if (err)
4246 goto done;
4248 if (!S_ISLNK(sb2.st_mode)) {
4249 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4250 err = got_error_from_errno2("chmod", path2);
4251 goto done;
4255 done:
4256 free(id_str);
4257 if (blob)
4258 got_object_blob_close(blob);
4259 if (f1 && fclose(f1) == EOF && err == NULL)
4260 err = got_error_from_errno2("fclose", path1);
4261 if (f2 && fclose(f2) == EOF && err == NULL)
4262 err = got_error_from_errno2("fclose", path2);
4263 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4264 err = got_error_from_errno2("close", path2);
4265 if (outfile && fclose(outfile) == EOF && err == NULL)
4266 err = got_error_from_errno2("fclose", *path_outfile);
4267 if (path1 && unlink(path1) == -1 && err == NULL)
4268 err = got_error_from_errno2("unlink", path1);
4269 if (err || !have_content) {
4270 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4271 err = got_error_from_errno2("unlink", *path_outfile);
4272 free(*path_outfile);
4273 *path_outfile = NULL;
4275 free(args);
4276 if (ds) {
4277 got_diff_state_free(ds);
4278 free(ds);
4280 if (changes)
4281 got_diff_free_changes(changes);
4282 free(path1);
4283 return err;
4286 static const struct got_error *
4287 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4288 const char *relpath, struct got_object_id *blob_id,
4289 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4290 int dirfd, const char *de_name)
4292 struct revert_file_args *a = arg;
4293 const struct got_error *err = NULL;
4294 char *parent_path = NULL;
4295 struct got_fileindex_entry *ie;
4296 struct got_tree_object *tree = NULL;
4297 struct got_object_id *tree_id = NULL;
4298 const struct got_tree_entry *te = NULL;
4299 char *tree_path = NULL, *te_name;
4300 char *ondisk_path = NULL, *path_content = NULL;
4301 struct got_blob_object *blob = NULL;
4303 /* Reverting a staged deletion is a no-op. */
4304 if (status == GOT_STATUS_DELETE &&
4305 staged_status != GOT_STATUS_NO_CHANGE)
4306 return NULL;
4308 if (status == GOT_STATUS_UNVERSIONED)
4309 return (*a->progress_cb)(a->progress_arg,
4310 GOT_STATUS_UNVERSIONED, relpath);
4312 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4313 if (ie == NULL)
4314 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4316 /* Construct in-repository path of tree which contains this blob. */
4317 err = got_path_dirname(&parent_path, ie->path);
4318 if (err) {
4319 if (err->code != GOT_ERR_BAD_PATH)
4320 goto done;
4321 parent_path = strdup("/");
4322 if (parent_path == NULL) {
4323 err = got_error_from_errno("strdup");
4324 goto done;
4327 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4328 tree_path = strdup(parent_path);
4329 if (tree_path == NULL) {
4330 err = got_error_from_errno("strdup");
4331 goto done;
4333 } else {
4334 if (got_path_is_root_dir(parent_path)) {
4335 tree_path = strdup(a->worktree->path_prefix);
4336 if (tree_path == NULL) {
4337 err = got_error_from_errno("strdup");
4338 goto done;
4340 } else {
4341 if (asprintf(&tree_path, "%s/%s",
4342 a->worktree->path_prefix, parent_path) == -1) {
4343 err = got_error_from_errno("asprintf");
4344 goto done;
4349 err = got_object_id_by_path(&tree_id, a->repo,
4350 a->worktree->base_commit_id, tree_path);
4351 if (err) {
4352 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4353 (status == GOT_STATUS_ADD ||
4354 staged_status == GOT_STATUS_ADD)))
4355 goto done;
4356 } else {
4357 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4358 if (err)
4359 goto done;
4361 te_name = basename(ie->path);
4362 if (te_name == NULL) {
4363 err = got_error_from_errno2("basename", ie->path);
4364 goto done;
4367 te = got_object_tree_find_entry(tree, te_name);
4368 if (te == NULL && status != GOT_STATUS_ADD &&
4369 staged_status != GOT_STATUS_ADD) {
4370 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4371 goto done;
4375 switch (status) {
4376 case GOT_STATUS_ADD:
4377 if (a->patch_cb) {
4378 int choice = GOT_PATCH_CHOICE_NONE;
4379 err = (*a->patch_cb)(&choice, a->patch_arg,
4380 status, ie->path, NULL, 1, 1);
4381 if (err)
4382 goto done;
4383 if (choice != GOT_PATCH_CHOICE_YES)
4384 break;
4386 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4387 ie->path);
4388 if (err)
4389 goto done;
4390 got_fileindex_entry_remove(a->fileindex, ie);
4391 break;
4392 case GOT_STATUS_DELETE:
4393 if (a->patch_cb) {
4394 int choice = GOT_PATCH_CHOICE_NONE;
4395 err = (*a->patch_cb)(&choice, a->patch_arg,
4396 status, ie->path, NULL, 1, 1);
4397 if (err)
4398 goto done;
4399 if (choice != GOT_PATCH_CHOICE_YES)
4400 break;
4402 /* fall through */
4403 case GOT_STATUS_MODIFY:
4404 case GOT_STATUS_MODE_CHANGE:
4405 case GOT_STATUS_CONFLICT:
4406 case GOT_STATUS_MISSING: {
4407 struct got_object_id id;
4408 if (staged_status == GOT_STATUS_ADD ||
4409 staged_status == GOT_STATUS_MODIFY) {
4410 memcpy(id.sha1, ie->staged_blob_sha1,
4411 SHA1_DIGEST_LENGTH);
4412 } else
4413 memcpy(id.sha1, ie->blob_sha1,
4414 SHA1_DIGEST_LENGTH);
4415 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4416 if (err)
4417 goto done;
4419 if (asprintf(&ondisk_path, "%s/%s",
4420 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4421 err = got_error_from_errno("asprintf");
4422 goto done;
4425 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4426 status == GOT_STATUS_CONFLICT)) {
4427 int is_bad_symlink = 0;
4428 err = create_patched_content(&path_content, 1, &id,
4429 ondisk_path, dirfd, de_name, ie->path, a->repo,
4430 a->patch_cb, a->patch_arg);
4431 if (err || path_content == NULL)
4432 break;
4433 if (te && S_ISLNK(te->mode)) {
4434 if (unlink(path_content) == -1) {
4435 err = got_error_from_errno2("unlink",
4436 path_content);
4437 break;
4439 err = install_symlink(&is_bad_symlink,
4440 a->worktree, ondisk_path, ie->path,
4441 blob, 0, 1, 0, a->repo,
4442 a->progress_cb, a->progress_arg);
4443 } else {
4444 if (rename(path_content, ondisk_path) == -1) {
4445 err = got_error_from_errno3("rename",
4446 path_content, ondisk_path);
4447 goto done;
4450 } else {
4451 int is_bad_symlink = 0;
4452 if (te && S_ISLNK(te->mode)) {
4453 err = install_symlink(&is_bad_symlink,
4454 a->worktree, ondisk_path, ie->path,
4455 blob, 0, 1, 0, a->repo,
4456 a->progress_cb, a->progress_arg);
4457 } else {
4458 err = install_blob(a->worktree, ondisk_path,
4459 ie->path,
4460 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4461 got_fileindex_perms_to_st(ie), blob,
4462 0, 1, 0, 0, a->repo,
4463 a->progress_cb, a->progress_arg);
4465 if (err)
4466 goto done;
4467 if (status == GOT_STATUS_DELETE ||
4468 status == GOT_STATUS_MODE_CHANGE) {
4469 err = got_fileindex_entry_update(ie,
4470 ondisk_path, blob->id.sha1,
4471 a->worktree->base_commit_id->sha1, 1);
4472 if (err)
4473 goto done;
4475 if (is_bad_symlink) {
4476 got_fileindex_entry_filetype_set(ie,
4477 GOT_FILEIDX_MODE_BAD_SYMLINK);
4480 break;
4482 default:
4483 break;
4485 done:
4486 free(ondisk_path);
4487 free(path_content);
4488 free(parent_path);
4489 free(tree_path);
4490 if (blob)
4491 got_object_blob_close(blob);
4492 if (tree)
4493 got_object_tree_close(tree);
4494 free(tree_id);
4495 return err;
4498 const struct got_error *
4499 got_worktree_revert(struct got_worktree *worktree,
4500 struct got_pathlist_head *paths,
4501 got_worktree_checkout_cb progress_cb, void *progress_arg,
4502 got_worktree_patch_cb patch_cb, void *patch_arg,
4503 struct got_repository *repo)
4505 struct got_fileindex *fileindex = NULL;
4506 char *fileindex_path = NULL;
4507 const struct got_error *err = NULL, *unlockerr = NULL;
4508 const struct got_error *sync_err = NULL;
4509 struct got_pathlist_entry *pe;
4510 struct revert_file_args rfa;
4512 err = lock_worktree(worktree, LOCK_EX);
4513 if (err)
4514 return err;
4516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4517 if (err)
4518 goto done;
4520 rfa.worktree = worktree;
4521 rfa.fileindex = fileindex;
4522 rfa.progress_cb = progress_cb;
4523 rfa.progress_arg = progress_arg;
4524 rfa.patch_cb = patch_cb;
4525 rfa.patch_arg = patch_arg;
4526 rfa.repo = repo;
4527 TAILQ_FOREACH(pe, paths, entry) {
4528 err = worktree_status(worktree, pe->path, fileindex, repo,
4529 revert_file, &rfa, NULL, NULL, 0, 0);
4530 if (err)
4531 break;
4533 sync_err = sync_fileindex(fileindex, fileindex_path);
4534 if (sync_err && err == NULL)
4535 err = sync_err;
4536 done:
4537 free(fileindex_path);
4538 if (fileindex)
4539 got_fileindex_free(fileindex);
4540 unlockerr = lock_worktree(worktree, LOCK_SH);
4541 if (unlockerr && err == NULL)
4542 err = unlockerr;
4543 return err;
4546 static void
4547 free_commitable(struct got_commitable *ct)
4549 free(ct->path);
4550 free(ct->in_repo_path);
4551 free(ct->ondisk_path);
4552 free(ct->blob_id);
4553 free(ct->base_blob_id);
4554 free(ct->staged_blob_id);
4555 free(ct->base_commit_id);
4556 free(ct);
4559 struct collect_commitables_arg {
4560 struct got_pathlist_head *commitable_paths;
4561 struct got_repository *repo;
4562 struct got_worktree *worktree;
4563 struct got_fileindex *fileindex;
4564 int have_staged_files;
4565 int allow_bad_symlinks;
4568 static const struct got_error *
4569 collect_commitables(void *arg, unsigned char status,
4570 unsigned char staged_status, const char *relpath,
4571 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4572 struct got_object_id *commit_id, int dirfd, const char *de_name)
4574 struct collect_commitables_arg *a = arg;
4575 const struct got_error *err = NULL;
4576 struct got_commitable *ct = NULL;
4577 struct got_pathlist_entry *new = NULL;
4578 char *parent_path = NULL, *path = NULL;
4579 struct stat sb;
4581 if (a->have_staged_files) {
4582 if (staged_status != GOT_STATUS_MODIFY &&
4583 staged_status != GOT_STATUS_ADD &&
4584 staged_status != GOT_STATUS_DELETE)
4585 return NULL;
4586 } else {
4587 if (status == GOT_STATUS_CONFLICT)
4588 return got_error(GOT_ERR_COMMIT_CONFLICT);
4590 if (status != GOT_STATUS_MODIFY &&
4591 status != GOT_STATUS_MODE_CHANGE &&
4592 status != GOT_STATUS_ADD &&
4593 status != GOT_STATUS_DELETE)
4594 return NULL;
4597 if (asprintf(&path, "/%s", relpath) == -1) {
4598 err = got_error_from_errno("asprintf");
4599 goto done;
4601 if (strcmp(path, "/") == 0) {
4602 parent_path = strdup("");
4603 if (parent_path == NULL)
4604 return got_error_from_errno("strdup");
4605 } else {
4606 err = got_path_dirname(&parent_path, path);
4607 if (err)
4608 return err;
4611 ct = calloc(1, sizeof(*ct));
4612 if (ct == NULL) {
4613 err = got_error_from_errno("calloc");
4614 goto done;
4617 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4618 relpath) == -1) {
4619 err = got_error_from_errno("asprintf");
4620 goto done;
4623 if (staged_status == GOT_STATUS_ADD ||
4624 staged_status == GOT_STATUS_MODIFY) {
4625 struct got_fileindex_entry *ie;
4626 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4627 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4628 case GOT_FILEIDX_MODE_REGULAR_FILE:
4629 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4630 ct->mode = S_IFREG;
4631 break;
4632 case GOT_FILEIDX_MODE_SYMLINK:
4633 ct->mode = S_IFLNK;
4634 break;
4635 default:
4636 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4637 goto done;
4639 ct->mode |= got_fileindex_entry_perms_get(ie);
4640 } else if (status != GOT_STATUS_DELETE &&
4641 staged_status != GOT_STATUS_DELETE) {
4642 if (dirfd != -1) {
4643 if (fstatat(dirfd, de_name, &sb,
4644 AT_SYMLINK_NOFOLLOW) == -1) {
4645 err = got_error_from_errno2("fstatat",
4646 ct->ondisk_path);
4647 goto done;
4649 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4650 err = got_error_from_errno2("lstat", ct->ondisk_path);
4651 goto done;
4653 ct->mode = sb.st_mode;
4656 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4657 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4658 relpath) == -1) {
4659 err = got_error_from_errno("asprintf");
4660 goto done;
4663 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4664 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4665 int is_bad_symlink;
4666 char target_path[PATH_MAX];
4667 ssize_t target_len;
4668 target_len = readlink(ct->ondisk_path, target_path,
4669 sizeof(target_path));
4670 if (target_len == -1) {
4671 err = got_error_from_errno2("readlink",
4672 ct->ondisk_path);
4673 goto done;
4675 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4676 target_len, ct->ondisk_path, a->worktree->root_path);
4677 if (err)
4678 goto done;
4679 if (is_bad_symlink) {
4680 err = got_error_path(ct->ondisk_path,
4681 GOT_ERR_BAD_SYMLINK);
4682 goto done;
4687 ct->status = status;
4688 ct->staged_status = staged_status;
4689 ct->blob_id = NULL; /* will be filled in when blob gets created */
4690 if (ct->status != GOT_STATUS_ADD &&
4691 ct->staged_status != GOT_STATUS_ADD) {
4692 ct->base_blob_id = got_object_id_dup(blob_id);
4693 if (ct->base_blob_id == NULL) {
4694 err = got_error_from_errno("got_object_id_dup");
4695 goto done;
4697 ct->base_commit_id = got_object_id_dup(commit_id);
4698 if (ct->base_commit_id == NULL) {
4699 err = got_error_from_errno("got_object_id_dup");
4700 goto done;
4703 if (ct->staged_status == GOT_STATUS_ADD ||
4704 ct->staged_status == GOT_STATUS_MODIFY) {
4705 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4706 if (ct->staged_blob_id == NULL) {
4707 err = got_error_from_errno("got_object_id_dup");
4708 goto done;
4711 ct->path = strdup(path);
4712 if (ct->path == NULL) {
4713 err = got_error_from_errno("strdup");
4714 goto done;
4716 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4717 done:
4718 if (ct && (err || new == NULL))
4719 free_commitable(ct);
4720 free(parent_path);
4721 free(path);
4722 return err;
4725 static const struct got_error *write_tree(struct got_object_id **, int *,
4726 struct got_tree_object *, const char *, struct got_pathlist_head *,
4727 got_worktree_status_cb status_cb, void *status_arg,
4728 struct got_repository *);
4730 static const struct got_error *
4731 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4732 struct got_tree_entry *te, const char *parent_path,
4733 struct got_pathlist_head *commitable_paths,
4734 got_worktree_status_cb status_cb, void *status_arg,
4735 struct got_repository *repo)
4737 const struct got_error *err = NULL;
4738 struct got_tree_object *subtree;
4739 char *subpath;
4741 if (asprintf(&subpath, "%s%s%s", parent_path,
4742 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4743 return got_error_from_errno("asprintf");
4745 err = got_object_open_as_tree(&subtree, repo, &te->id);
4746 if (err)
4747 return err;
4749 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4750 commitable_paths, status_cb, status_arg, repo);
4751 got_object_tree_close(subtree);
4752 free(subpath);
4753 return err;
4756 static const struct got_error *
4757 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4759 const struct got_error *err = NULL;
4760 char *ct_parent_path = NULL;
4762 *match = 0;
4764 if (strchr(ct->in_repo_path, '/') == NULL) {
4765 *match = got_path_is_root_dir(path);
4766 return NULL;
4769 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4770 if (err)
4771 return err;
4772 *match = (strcmp(path, ct_parent_path) == 0);
4773 free(ct_parent_path);
4774 return err;
4777 static mode_t
4778 get_ct_file_mode(struct got_commitable *ct)
4780 if (S_ISLNK(ct->mode))
4781 return S_IFLNK;
4783 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4786 static const struct got_error *
4787 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4788 struct got_tree_entry *te, struct got_commitable *ct)
4790 const struct got_error *err = NULL;
4792 *new_te = NULL;
4794 err = got_object_tree_entry_dup(new_te, te);
4795 if (err)
4796 goto done;
4798 (*new_te)->mode = get_ct_file_mode(ct);
4800 if (ct->staged_status == GOT_STATUS_MODIFY)
4801 memcpy(&(*new_te)->id, ct->staged_blob_id,
4802 sizeof((*new_te)->id));
4803 else
4804 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4805 done:
4806 if (err && *new_te) {
4807 free(*new_te);
4808 *new_te = NULL;
4810 return err;
4813 static const struct got_error *
4814 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4815 struct got_commitable *ct)
4817 const struct got_error *err = NULL;
4818 char *ct_name;
4820 *new_te = NULL;
4822 *new_te = calloc(1, sizeof(**new_te));
4823 if (*new_te == NULL)
4824 return got_error_from_errno("calloc");
4826 ct_name = basename(ct->path);
4827 if (ct_name == NULL) {
4828 err = got_error_from_errno2("basename", ct->path);
4829 goto done;
4831 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4832 sizeof((*new_te)->name)) {
4833 err = got_error(GOT_ERR_NO_SPACE);
4834 goto done;
4837 (*new_te)->mode = get_ct_file_mode(ct);
4839 if (ct->staged_status == GOT_STATUS_ADD)
4840 memcpy(&(*new_te)->id, ct->staged_blob_id,
4841 sizeof((*new_te)->id));
4842 else
4843 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4844 done:
4845 if (err && *new_te) {
4846 free(*new_te);
4847 *new_te = NULL;
4849 return err;
4852 static const struct got_error *
4853 insert_tree_entry(struct got_tree_entry *new_te,
4854 struct got_pathlist_head *paths)
4856 const struct got_error *err = NULL;
4857 struct got_pathlist_entry *new_pe;
4859 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4860 if (err)
4861 return err;
4862 if (new_pe == NULL)
4863 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4864 return NULL;
4867 static const struct got_error *
4868 report_ct_status(struct got_commitable *ct,
4869 got_worktree_status_cb status_cb, void *status_arg)
4871 const char *ct_path = ct->path;
4872 unsigned char status;
4874 while (ct_path[0] == '/')
4875 ct_path++;
4877 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4878 status = ct->staged_status;
4879 else
4880 status = ct->status;
4882 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4883 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4886 static const struct got_error *
4887 match_modified_subtree(int *modified, struct got_tree_entry *te,
4888 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4890 const struct got_error *err = NULL;
4891 struct got_pathlist_entry *pe;
4892 char *te_path;
4894 *modified = 0;
4896 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4897 got_path_is_root_dir(base_tree_path) ? "" : "/",
4898 te->name) == -1)
4899 return got_error_from_errno("asprintf");
4901 TAILQ_FOREACH(pe, commitable_paths, entry) {
4902 struct got_commitable *ct = pe->data;
4903 *modified = got_path_is_child(ct->in_repo_path, te_path,
4904 strlen(te_path));
4905 if (*modified)
4906 break;
4909 free(te_path);
4910 return err;
4913 static const struct got_error *
4914 match_deleted_or_modified_ct(struct got_commitable **ctp,
4915 struct got_tree_entry *te, const char *base_tree_path,
4916 struct got_pathlist_head *commitable_paths)
4918 const struct got_error *err = NULL;
4919 struct got_pathlist_entry *pe;
4921 *ctp = NULL;
4923 TAILQ_FOREACH(pe, commitable_paths, entry) {
4924 struct got_commitable *ct = pe->data;
4925 char *ct_name = NULL;
4926 int path_matches;
4928 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4929 if (ct->status != GOT_STATUS_MODIFY &&
4930 ct->status != GOT_STATUS_MODE_CHANGE &&
4931 ct->status != GOT_STATUS_DELETE)
4932 continue;
4933 } else {
4934 if (ct->staged_status != GOT_STATUS_MODIFY &&
4935 ct->staged_status != GOT_STATUS_DELETE)
4936 continue;
4939 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4940 continue;
4942 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4943 if (err)
4944 return err;
4945 if (!path_matches)
4946 continue;
4948 ct_name = basename(pe->path);
4949 if (ct_name == NULL)
4950 return got_error_from_errno2("basename", pe->path);
4952 if (strcmp(te->name, ct_name) != 0)
4953 continue;
4955 *ctp = ct;
4956 break;
4959 return err;
4962 static const struct got_error *
4963 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4964 const char *child_path, const char *path_base_tree,
4965 struct got_pathlist_head *commitable_paths,
4966 got_worktree_status_cb status_cb, void *status_arg,
4967 struct got_repository *repo)
4969 const struct got_error *err = NULL;
4970 struct got_tree_entry *new_te;
4971 char *subtree_path;
4972 struct got_object_id *id = NULL;
4973 int nentries;
4975 *new_tep = NULL;
4977 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4978 got_path_is_root_dir(path_base_tree) ? "" : "/",
4979 child_path) == -1)
4980 return got_error_from_errno("asprintf");
4982 new_te = calloc(1, sizeof(*new_te));
4983 if (new_te == NULL)
4984 return got_error_from_errno("calloc");
4985 new_te->mode = S_IFDIR;
4987 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4988 sizeof(new_te->name)) {
4989 err = got_error(GOT_ERR_NO_SPACE);
4990 goto done;
4992 err = write_tree(&id, &nentries, NULL, subtree_path,
4993 commitable_paths, status_cb, status_arg, repo);
4994 if (err) {
4995 free(new_te);
4996 goto done;
4998 memcpy(&new_te->id, id, sizeof(new_te->id));
4999 done:
5000 free(id);
5001 free(subtree_path);
5002 if (err == NULL)
5003 *new_tep = new_te;
5004 return err;
5007 static const struct got_error *
5008 write_tree(struct got_object_id **new_tree_id, int *nentries,
5009 struct got_tree_object *base_tree, const char *path_base_tree,
5010 struct got_pathlist_head *commitable_paths,
5011 got_worktree_status_cb status_cb, void *status_arg,
5012 struct got_repository *repo)
5014 const struct got_error *err = NULL;
5015 struct got_pathlist_head paths;
5016 struct got_tree_entry *te, *new_te = NULL;
5017 struct got_pathlist_entry *pe;
5019 TAILQ_INIT(&paths);
5020 *nentries = 0;
5022 /* Insert, and recurse into, newly added entries first. */
5023 TAILQ_FOREACH(pe, commitable_paths, entry) {
5024 struct got_commitable *ct = pe->data;
5025 char *child_path = NULL, *slash;
5027 if ((ct->status != GOT_STATUS_ADD &&
5028 ct->staged_status != GOT_STATUS_ADD) ||
5029 (ct->flags & GOT_COMMITABLE_ADDED))
5030 continue;
5032 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5033 strlen(path_base_tree)))
5034 continue;
5036 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5037 ct->in_repo_path);
5038 if (err)
5039 goto done;
5041 slash = strchr(child_path, '/');
5042 if (slash == NULL) {
5043 err = alloc_added_blob_tree_entry(&new_te, ct);
5044 if (err)
5045 goto done;
5046 err = report_ct_status(ct, status_cb, status_arg);
5047 if (err)
5048 goto done;
5049 ct->flags |= GOT_COMMITABLE_ADDED;
5050 err = insert_tree_entry(new_te, &paths);
5051 if (err)
5052 goto done;
5053 (*nentries)++;
5054 } else {
5055 *slash = '\0'; /* trim trailing path components */
5056 if (base_tree == NULL ||
5057 got_object_tree_find_entry(base_tree, child_path)
5058 == NULL) {
5059 err = make_subtree_for_added_blob(&new_te,
5060 child_path, path_base_tree,
5061 commitable_paths, status_cb, status_arg,
5062 repo);
5063 if (err)
5064 goto done;
5065 err = insert_tree_entry(new_te, &paths);
5066 if (err)
5067 goto done;
5068 (*nentries)++;
5073 if (base_tree) {
5074 int i, nbase_entries;
5075 /* Handle modified and deleted entries. */
5076 nbase_entries = got_object_tree_get_nentries(base_tree);
5077 for (i = 0; i < nbase_entries; i++) {
5078 struct got_commitable *ct = NULL;
5080 te = got_object_tree_get_entry(base_tree, i);
5081 if (got_object_tree_entry_is_submodule(te)) {
5082 /* Entry is a submodule; just copy it. */
5083 err = got_object_tree_entry_dup(&new_te, te);
5084 if (err)
5085 goto done;
5086 err = insert_tree_entry(new_te, &paths);
5087 if (err)
5088 goto done;
5089 (*nentries)++;
5090 continue;
5093 if (S_ISDIR(te->mode)) {
5094 int modified;
5095 err = got_object_tree_entry_dup(&new_te, te);
5096 if (err)
5097 goto done;
5098 err = match_modified_subtree(&modified, te,
5099 path_base_tree, commitable_paths);
5100 if (err)
5101 goto done;
5102 /* Avoid recursion into unmodified subtrees. */
5103 if (modified) {
5104 struct got_object_id *new_id;
5105 int nsubentries;
5106 err = write_subtree(&new_id,
5107 &nsubentries, te,
5108 path_base_tree, commitable_paths,
5109 status_cb, status_arg, repo);
5110 if (err)
5111 goto done;
5112 if (nsubentries == 0) {
5113 /* All entries were deleted. */
5114 free(new_id);
5115 continue;
5117 memcpy(&new_te->id, new_id,
5118 sizeof(new_te->id));
5119 free(new_id);
5121 err = insert_tree_entry(new_te, &paths);
5122 if (err)
5123 goto done;
5124 (*nentries)++;
5125 continue;
5128 err = match_deleted_or_modified_ct(&ct, te,
5129 path_base_tree, commitable_paths);
5130 if (err)
5131 goto done;
5132 if (ct) {
5133 /* NB: Deleted entries get dropped here. */
5134 if (ct->status == GOT_STATUS_MODIFY ||
5135 ct->status == GOT_STATUS_MODE_CHANGE ||
5136 ct->staged_status == GOT_STATUS_MODIFY) {
5137 err = alloc_modified_blob_tree_entry(
5138 &new_te, te, ct);
5139 if (err)
5140 goto done;
5141 err = insert_tree_entry(new_te, &paths);
5142 if (err)
5143 goto done;
5144 (*nentries)++;
5146 err = report_ct_status(ct, status_cb,
5147 status_arg);
5148 if (err)
5149 goto done;
5150 } else {
5151 /* Entry is unchanged; just copy it. */
5152 err = got_object_tree_entry_dup(&new_te, te);
5153 if (err)
5154 goto done;
5155 err = insert_tree_entry(new_te, &paths);
5156 if (err)
5157 goto done;
5158 (*nentries)++;
5163 /* Write new list of entries; deleted entries have been dropped. */
5164 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5165 done:
5166 got_pathlist_free(&paths);
5167 return err;
5170 static const struct got_error *
5171 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5172 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5173 int have_staged_files)
5175 const struct got_error *err = NULL;
5176 struct got_pathlist_entry *pe;
5178 TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 struct got_fileindex_entry *ie;
5180 struct got_commitable *ct = pe->data;
5182 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5183 if (ie) {
5184 if (ct->status == GOT_STATUS_DELETE ||
5185 ct->staged_status == GOT_STATUS_DELETE) {
5186 got_fileindex_entry_remove(fileindex, ie);
5187 } else if (ct->staged_status == GOT_STATUS_ADD ||
5188 ct->staged_status == GOT_STATUS_MODIFY) {
5189 got_fileindex_entry_stage_set(ie,
5190 GOT_FILEIDX_STAGE_NONE);
5191 err = got_fileindex_entry_update(ie,
5192 ct->ondisk_path, ct->staged_blob_id->sha1,
5193 new_base_commit_id->sha1,
5194 !have_staged_files);
5195 } else
5196 err = got_fileindex_entry_update(ie,
5197 ct->ondisk_path, ct->blob_id->sha1,
5198 new_base_commit_id->sha1,
5199 !have_staged_files);
5200 } else {
5201 err = got_fileindex_entry_alloc(&ie, pe->path);
5202 if (err)
5203 break;
5204 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5205 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5206 if (err) {
5207 got_fileindex_entry_free(ie);
5208 break;
5210 err = got_fileindex_entry_add(fileindex, ie);
5211 if (err) {
5212 got_fileindex_entry_free(ie);
5213 break;
5217 return err;
5221 static const struct got_error *
5222 check_out_of_date(const char *in_repo_path, unsigned char status,
5223 unsigned char staged_status, struct got_object_id *base_blob_id,
5224 struct got_object_id *base_commit_id,
5225 struct got_object_id *head_commit_id, struct got_repository *repo,
5226 int ood_errcode)
5228 const struct got_error *err = NULL;
5229 struct got_object_id *id = NULL;
5231 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5232 /* Trivial case: base commit == head commit */
5233 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5234 return NULL;
5236 * Ensure file content which local changes were based
5237 * on matches file content in the branch head.
5239 err = got_object_id_by_path(&id, repo, head_commit_id,
5240 in_repo_path);
5241 if (err) {
5242 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5243 err = got_error(ood_errcode);
5244 goto done;
5245 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5246 err = got_error(ood_errcode);
5247 } else {
5248 /* Require that added files don't exist in the branch head. */
5249 err = got_object_id_by_path(&id, repo, head_commit_id,
5250 in_repo_path);
5251 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5252 goto done;
5253 err = id ? got_error(ood_errcode) : NULL;
5255 done:
5256 free(id);
5257 return err;
5260 const struct got_error *
5261 commit_worktree(struct got_object_id **new_commit_id,
5262 struct got_pathlist_head *commitable_paths,
5263 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5264 const char *author, const char *committer,
5265 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5266 got_worktree_status_cb status_cb, void *status_arg,
5267 struct got_repository *repo)
5269 const struct got_error *err = NULL, *unlockerr = NULL;
5270 struct got_pathlist_entry *pe;
5271 const char *head_ref_name = NULL;
5272 struct got_commit_object *head_commit = NULL;
5273 struct got_reference *head_ref2 = NULL;
5274 struct got_object_id *head_commit_id2 = NULL;
5275 struct got_tree_object *head_tree = NULL;
5276 struct got_object_id *new_tree_id = NULL;
5277 int nentries;
5278 struct got_object_id_queue parent_ids;
5279 struct got_object_qid *pid = NULL;
5280 char *logmsg = NULL;
5282 *new_commit_id = NULL;
5284 SIMPLEQ_INIT(&parent_ids);
5286 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5287 if (err)
5288 goto done;
5290 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5291 if (err)
5292 goto done;
5294 if (commit_msg_cb != NULL) {
5295 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5296 if (err)
5297 goto done;
5300 if (logmsg == NULL || strlen(logmsg) == 0) {
5301 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5302 goto done;
5305 /* Create blobs from added and modified files and record their IDs. */
5306 TAILQ_FOREACH(pe, commitable_paths, entry) {
5307 struct got_commitable *ct = pe->data;
5308 char *ondisk_path;
5310 /* Blobs for staged files already exist. */
5311 if (ct->staged_status == GOT_STATUS_ADD ||
5312 ct->staged_status == GOT_STATUS_MODIFY)
5313 continue;
5315 if (ct->status != GOT_STATUS_ADD &&
5316 ct->status != GOT_STATUS_MODIFY &&
5317 ct->status != GOT_STATUS_MODE_CHANGE)
5318 continue;
5320 if (asprintf(&ondisk_path, "%s/%s",
5321 worktree->root_path, pe->path) == -1) {
5322 err = got_error_from_errno("asprintf");
5323 goto done;
5325 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5326 free(ondisk_path);
5327 if (err)
5328 goto done;
5331 /* Recursively write new tree objects. */
5332 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5333 commitable_paths, status_cb, status_arg, repo);
5334 if (err)
5335 goto done;
5337 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5338 if (err)
5339 goto done;
5340 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5341 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5342 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5343 got_object_qid_free(pid);
5344 if (logmsg != NULL)
5345 free(logmsg);
5346 if (err)
5347 goto done;
5349 /* Check if a concurrent commit to our branch has occurred. */
5350 head_ref_name = got_worktree_get_head_ref_name(worktree);
5351 if (head_ref_name == NULL) {
5352 err = got_error_from_errno("got_worktree_get_head_ref_name");
5353 goto done;
5355 /* Lock the reference here to prevent concurrent modification. */
5356 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5357 if (err)
5358 goto done;
5359 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5360 if (err)
5361 goto done;
5362 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5363 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5364 goto done;
5366 /* Update branch head in repository. */
5367 err = got_ref_change_ref(head_ref2, *new_commit_id);
5368 if (err)
5369 goto done;
5370 err = got_ref_write(head_ref2, repo);
5371 if (err)
5372 goto done;
5374 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5375 if (err)
5376 goto done;
5378 err = ref_base_commit(worktree, repo);
5379 if (err)
5380 goto done;
5381 done:
5382 if (head_tree)
5383 got_object_tree_close(head_tree);
5384 if (head_commit)
5385 got_object_commit_close(head_commit);
5386 free(head_commit_id2);
5387 if (head_ref2) {
5388 unlockerr = got_ref_unlock(head_ref2);
5389 if (unlockerr && err == NULL)
5390 err = unlockerr;
5391 got_ref_close(head_ref2);
5393 return err;
5396 static const struct got_error *
5397 check_path_is_commitable(const char *path,
5398 struct got_pathlist_head *commitable_paths)
5400 struct got_pathlist_entry *cpe = NULL;
5401 size_t path_len = strlen(path);
5403 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5404 struct got_commitable *ct = cpe->data;
5405 const char *ct_path = ct->path;
5407 while (ct_path[0] == '/')
5408 ct_path++;
5410 if (strcmp(path, ct_path) == 0 ||
5411 got_path_is_child(ct_path, path, path_len))
5412 break;
5415 if (cpe == NULL)
5416 return got_error_path(path, GOT_ERR_BAD_PATH);
5418 return NULL;
5421 static const struct got_error *
5422 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5424 int *have_staged_files = arg;
5426 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5427 *have_staged_files = 1;
5428 return got_error(GOT_ERR_CANCELLED);
5431 return NULL;
5434 static const struct got_error *
5435 check_non_staged_files(struct got_fileindex *fileindex,
5436 struct got_pathlist_head *paths)
5438 struct got_pathlist_entry *pe;
5439 struct got_fileindex_entry *ie;
5441 TAILQ_FOREACH(pe, paths, entry) {
5442 if (pe->path[0] == '\0')
5443 continue;
5444 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5445 if (ie == NULL)
5446 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5447 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5448 return got_error_path(pe->path,
5449 GOT_ERR_FILE_NOT_STAGED);
5452 return NULL;
5455 const struct got_error *
5456 got_worktree_commit(struct got_object_id **new_commit_id,
5457 struct got_worktree *worktree, struct got_pathlist_head *paths,
5458 const char *author, const char *committer, int allow_bad_symlinks,
5459 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5460 got_worktree_status_cb status_cb, void *status_arg,
5461 struct got_repository *repo)
5463 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5464 struct got_fileindex *fileindex = NULL;
5465 char *fileindex_path = NULL;
5466 struct got_pathlist_head commitable_paths;
5467 struct collect_commitables_arg cc_arg;
5468 struct got_pathlist_entry *pe;
5469 struct got_reference *head_ref = NULL;
5470 struct got_object_id *head_commit_id = NULL;
5471 int have_staged_files = 0;
5473 *new_commit_id = NULL;
5475 TAILQ_INIT(&commitable_paths);
5477 err = lock_worktree(worktree, LOCK_EX);
5478 if (err)
5479 goto done;
5481 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5482 if (err)
5483 goto done;
5485 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5486 if (err)
5487 goto done;
5489 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5490 if (err)
5491 goto done;
5493 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5494 &have_staged_files);
5495 if (err && err->code != GOT_ERR_CANCELLED)
5496 goto done;
5497 if (have_staged_files) {
5498 err = check_non_staged_files(fileindex, paths);
5499 if (err)
5500 goto done;
5503 cc_arg.commitable_paths = &commitable_paths;
5504 cc_arg.worktree = worktree;
5505 cc_arg.fileindex = fileindex;
5506 cc_arg.repo = repo;
5507 cc_arg.have_staged_files = have_staged_files;
5508 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5509 TAILQ_FOREACH(pe, paths, entry) {
5510 err = worktree_status(worktree, pe->path, fileindex, repo,
5511 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5512 if (err)
5513 goto done;
5516 if (TAILQ_EMPTY(&commitable_paths)) {
5517 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5518 goto done;
5521 TAILQ_FOREACH(pe, paths, entry) {
5522 err = check_path_is_commitable(pe->path, &commitable_paths);
5523 if (err)
5524 goto done;
5527 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5528 struct got_commitable *ct = pe->data;
5529 const char *ct_path = ct->in_repo_path;
5531 while (ct_path[0] == '/')
5532 ct_path++;
5533 err = check_out_of_date(ct_path, ct->status,
5534 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5535 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5536 if (err)
5537 goto done;
5541 err = commit_worktree(new_commit_id, &commitable_paths,
5542 head_commit_id, worktree, author, committer,
5543 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5544 if (err)
5545 goto done;
5547 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5548 fileindex, have_staged_files);
5549 sync_err = sync_fileindex(fileindex, fileindex_path);
5550 if (sync_err && err == NULL)
5551 err = sync_err;
5552 done:
5553 if (fileindex)
5554 got_fileindex_free(fileindex);
5555 free(fileindex_path);
5556 unlockerr = lock_worktree(worktree, LOCK_SH);
5557 if (unlockerr && err == NULL)
5558 err = unlockerr;
5559 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5560 struct got_commitable *ct = pe->data;
5561 free_commitable(ct);
5563 got_pathlist_free(&commitable_paths);
5564 return err;
5567 const char *
5568 got_commitable_get_path(struct got_commitable *ct)
5570 return ct->path;
5573 unsigned int
5574 got_commitable_get_status(struct got_commitable *ct)
5576 return ct->status;
5579 struct check_rebase_ok_arg {
5580 struct got_worktree *worktree;
5581 struct got_repository *repo;
5584 static const struct got_error *
5585 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5587 const struct got_error *err = NULL;
5588 struct check_rebase_ok_arg *a = arg;
5589 unsigned char status;
5590 struct stat sb;
5591 char *ondisk_path;
5593 /* Reject rebase of a work tree with mixed base commits. */
5594 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5595 SHA1_DIGEST_LENGTH))
5596 return got_error(GOT_ERR_MIXED_COMMITS);
5598 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5599 == -1)
5600 return got_error_from_errno("asprintf");
5602 /* Reject rebase of a work tree with modified or staged files. */
5603 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5604 free(ondisk_path);
5605 if (err)
5606 return err;
5608 if (status != GOT_STATUS_NO_CHANGE)
5609 return got_error(GOT_ERR_MODIFIED);
5610 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5611 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5613 return NULL;
5616 const struct got_error *
5617 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5618 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5619 struct got_worktree *worktree, struct got_reference *branch,
5620 struct got_repository *repo)
5622 const struct got_error *err = NULL;
5623 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5624 char *branch_ref_name = NULL;
5625 char *fileindex_path = NULL;
5626 struct check_rebase_ok_arg ok_arg;
5627 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5628 struct got_object_id *wt_branch_tip = NULL;
5630 *new_base_branch_ref = NULL;
5631 *tmp_branch = NULL;
5632 *fileindex = NULL;
5634 err = lock_worktree(worktree, LOCK_EX);
5635 if (err)
5636 return err;
5638 err = open_fileindex(fileindex, &fileindex_path, worktree);
5639 if (err)
5640 goto done;
5642 ok_arg.worktree = worktree;
5643 ok_arg.repo = repo;
5644 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5645 &ok_arg);
5646 if (err)
5647 goto done;
5649 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5650 if (err)
5651 goto done;
5653 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5654 if (err)
5655 goto done;
5657 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5658 if (err)
5659 goto done;
5661 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5662 0);
5663 if (err)
5664 goto done;
5666 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5667 if (err)
5668 goto done;
5669 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5670 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5671 goto done;
5674 err = got_ref_alloc_symref(new_base_branch_ref,
5675 new_base_branch_ref_name, wt_branch);
5676 if (err)
5677 goto done;
5678 err = got_ref_write(*new_base_branch_ref, repo);
5679 if (err)
5680 goto done;
5682 /* TODO Lock original branch's ref while rebasing? */
5684 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5685 if (err)
5686 goto done;
5688 err = got_ref_write(branch_ref, repo);
5689 if (err)
5690 goto done;
5692 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5693 worktree->base_commit_id);
5694 if (err)
5695 goto done;
5696 err = got_ref_write(*tmp_branch, repo);
5697 if (err)
5698 goto done;
5700 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5701 if (err)
5702 goto done;
5703 done:
5704 free(fileindex_path);
5705 free(tmp_branch_name);
5706 free(new_base_branch_ref_name);
5707 free(branch_ref_name);
5708 if (branch_ref)
5709 got_ref_close(branch_ref);
5710 if (wt_branch)
5711 got_ref_close(wt_branch);
5712 free(wt_branch_tip);
5713 if (err) {
5714 if (*new_base_branch_ref) {
5715 got_ref_close(*new_base_branch_ref);
5716 *new_base_branch_ref = NULL;
5718 if (*tmp_branch) {
5719 got_ref_close(*tmp_branch);
5720 *tmp_branch = NULL;
5722 if (*fileindex) {
5723 got_fileindex_free(*fileindex);
5724 *fileindex = NULL;
5726 lock_worktree(worktree, LOCK_SH);
5728 return err;
5731 const struct got_error *
5732 got_worktree_rebase_continue(struct got_object_id **commit_id,
5733 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5734 struct got_reference **branch, struct got_fileindex **fileindex,
5735 struct got_worktree *worktree, struct got_repository *repo)
5737 const struct got_error *err;
5738 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5739 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5740 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5741 char *fileindex_path = NULL;
5742 int have_staged_files = 0;
5744 *commit_id = NULL;
5745 *new_base_branch = NULL;
5746 *tmp_branch = NULL;
5747 *branch = NULL;
5748 *fileindex = NULL;
5750 err = lock_worktree(worktree, LOCK_EX);
5751 if (err)
5752 return err;
5754 err = open_fileindex(fileindex, &fileindex_path, worktree);
5755 if (err)
5756 goto done;
5758 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5759 &have_staged_files);
5760 if (err && err->code != GOT_ERR_CANCELLED)
5761 goto done;
5762 if (have_staged_files) {
5763 err = got_error(GOT_ERR_STAGED_PATHS);
5764 goto done;
5767 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5768 if (err)
5769 goto done;
5771 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5772 if (err)
5773 goto done;
5775 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5776 if (err)
5777 goto done;
5779 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5780 if (err)
5781 goto done;
5783 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5784 if (err)
5785 goto done;
5787 err = got_ref_open(branch, repo,
5788 got_ref_get_symref_target(branch_ref), 0);
5789 if (err)
5790 goto done;
5792 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5793 if (err)
5794 goto done;
5796 err = got_ref_resolve(commit_id, repo, commit_ref);
5797 if (err)
5798 goto done;
5800 err = got_ref_open(new_base_branch, repo,
5801 new_base_branch_ref_name, 0);
5802 if (err)
5803 goto done;
5805 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5806 if (err)
5807 goto done;
5808 done:
5809 free(commit_ref_name);
5810 free(branch_ref_name);
5811 free(fileindex_path);
5812 if (commit_ref)
5813 got_ref_close(commit_ref);
5814 if (branch_ref)
5815 got_ref_close(branch_ref);
5816 if (err) {
5817 free(*commit_id);
5818 *commit_id = NULL;
5819 if (*tmp_branch) {
5820 got_ref_close(*tmp_branch);
5821 *tmp_branch = NULL;
5823 if (*new_base_branch) {
5824 got_ref_close(*new_base_branch);
5825 *new_base_branch = NULL;
5827 if (*branch) {
5828 got_ref_close(*branch);
5829 *branch = NULL;
5831 if (*fileindex) {
5832 got_fileindex_free(*fileindex);
5833 *fileindex = NULL;
5835 lock_worktree(worktree, LOCK_SH);
5837 return err;
5840 const struct got_error *
5841 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5843 const struct got_error *err;
5844 char *tmp_branch_name = NULL;
5846 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5847 if (err)
5848 return err;
5850 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5851 free(tmp_branch_name);
5852 return NULL;
5855 static const struct got_error *
5856 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5857 char **logmsg, void *arg)
5859 *logmsg = arg;
5860 return NULL;
5863 static const struct got_error *
5864 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5865 const char *path, struct got_object_id *blob_id,
5866 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5867 int dirfd, const char *de_name)
5869 return NULL;
5872 struct collect_merged_paths_arg {
5873 got_worktree_checkout_cb progress_cb;
5874 void *progress_arg;
5875 struct got_pathlist_head *merged_paths;
5878 static const struct got_error *
5879 collect_merged_paths(void *arg, unsigned char status, const char *path)
5881 const struct got_error *err;
5882 struct collect_merged_paths_arg *a = arg;
5883 char *p;
5884 struct got_pathlist_entry *new;
5886 err = (*a->progress_cb)(a->progress_arg, status, path);
5887 if (err)
5888 return err;
5890 if (status != GOT_STATUS_MERGE &&
5891 status != GOT_STATUS_ADD &&
5892 status != GOT_STATUS_DELETE &&
5893 status != GOT_STATUS_CONFLICT)
5894 return NULL;
5896 p = strdup(path);
5897 if (p == NULL)
5898 return got_error_from_errno("strdup");
5900 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5901 if (err || new == NULL)
5902 free(p);
5903 return err;
5906 void
5907 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5909 struct got_pathlist_entry *pe;
5911 TAILQ_FOREACH(pe, merged_paths, entry)
5912 free((char *)pe->path);
5914 got_pathlist_free(merged_paths);
5917 static const struct got_error *
5918 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5919 int is_rebase, struct got_repository *repo)
5921 const struct got_error *err;
5922 struct got_reference *commit_ref = NULL;
5924 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5925 if (err) {
5926 if (err->code != GOT_ERR_NOT_REF)
5927 goto done;
5928 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5929 if (err)
5930 goto done;
5931 err = got_ref_write(commit_ref, repo);
5932 if (err)
5933 goto done;
5934 } else if (is_rebase) {
5935 struct got_object_id *stored_id;
5936 int cmp;
5938 err = got_ref_resolve(&stored_id, repo, commit_ref);
5939 if (err)
5940 goto done;
5941 cmp = got_object_id_cmp(commit_id, stored_id);
5942 free(stored_id);
5943 if (cmp != 0) {
5944 err = got_error(GOT_ERR_REBASE_COMMITID);
5945 goto done;
5948 done:
5949 if (commit_ref)
5950 got_ref_close(commit_ref);
5951 return err;
5954 static const struct got_error *
5955 rebase_merge_files(struct got_pathlist_head *merged_paths,
5956 const char *commit_ref_name, struct got_worktree *worktree,
5957 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5958 struct got_object_id *commit_id, struct got_repository *repo,
5959 got_worktree_checkout_cb progress_cb, void *progress_arg,
5960 got_cancel_cb cancel_cb, void *cancel_arg)
5962 const struct got_error *err;
5963 struct got_reference *commit_ref = NULL;
5964 struct collect_merged_paths_arg cmp_arg;
5965 char *fileindex_path;
5967 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5969 err = get_fileindex_path(&fileindex_path, worktree);
5970 if (err)
5971 return err;
5973 cmp_arg.progress_cb = progress_cb;
5974 cmp_arg.progress_arg = progress_arg;
5975 cmp_arg.merged_paths = merged_paths;
5976 err = merge_files(worktree, fileindex, fileindex_path,
5977 parent_commit_id, commit_id, repo, collect_merged_paths,
5978 &cmp_arg, cancel_cb, cancel_arg);
5979 if (commit_ref)
5980 got_ref_close(commit_ref);
5981 return err;
5984 const struct got_error *
5985 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5986 struct got_worktree *worktree, struct got_fileindex *fileindex,
5987 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5988 struct got_repository *repo,
5989 got_worktree_checkout_cb progress_cb, void *progress_arg,
5990 got_cancel_cb cancel_cb, void *cancel_arg)
5992 const struct got_error *err;
5993 char *commit_ref_name;
5995 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5996 if (err)
5997 return err;
5999 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6000 if (err)
6001 goto done;
6003 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6004 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6005 progress_arg, cancel_cb, cancel_arg);
6006 done:
6007 free(commit_ref_name);
6008 return err;
6011 const struct got_error *
6012 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6013 struct got_worktree *worktree, struct got_fileindex *fileindex,
6014 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6015 struct got_repository *repo,
6016 got_worktree_checkout_cb progress_cb, void *progress_arg,
6017 got_cancel_cb cancel_cb, void *cancel_arg)
6019 const struct got_error *err;
6020 char *commit_ref_name;
6022 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6023 if (err)
6024 return err;
6026 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6027 if (err)
6028 goto done;
6030 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6031 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6032 progress_arg, cancel_cb, cancel_arg);
6033 done:
6034 free(commit_ref_name);
6035 return err;
6038 static const struct got_error *
6039 rebase_commit(struct got_object_id **new_commit_id,
6040 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6041 struct got_worktree *worktree, struct got_fileindex *fileindex,
6042 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6043 const char *new_logmsg, struct got_repository *repo)
6045 const struct got_error *err, *sync_err;
6046 struct got_pathlist_head commitable_paths;
6047 struct collect_commitables_arg cc_arg;
6048 char *fileindex_path = NULL;
6049 struct got_reference *head_ref = NULL;
6050 struct got_object_id *head_commit_id = NULL;
6051 char *logmsg = NULL;
6053 TAILQ_INIT(&commitable_paths);
6054 *new_commit_id = NULL;
6056 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6058 err = get_fileindex_path(&fileindex_path, worktree);
6059 if (err)
6060 return err;
6062 cc_arg.commitable_paths = &commitable_paths;
6063 cc_arg.worktree = worktree;
6064 cc_arg.repo = repo;
6065 cc_arg.have_staged_files = 0;
6067 * If possible get the status of individual files directly to
6068 * avoid crawling the entire work tree once per rebased commit.
6069 * TODO: Ideally, merged_paths would contain a list of commitables
6070 * we could use so we could skip worktree_status() entirely.
6072 if (merged_paths) {
6073 struct got_pathlist_entry *pe;
6074 TAILQ_FOREACH(pe, merged_paths, entry) {
6075 err = worktree_status(worktree, pe->path, fileindex,
6076 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6077 0);
6078 if (err)
6079 goto done;
6081 } else {
6082 err = worktree_status(worktree, "", fileindex, repo,
6083 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6084 if (err)
6085 goto done;
6088 if (TAILQ_EMPTY(&commitable_paths)) {
6089 /* No-op change; commit will be elided. */
6090 err = got_ref_delete(commit_ref, repo);
6091 if (err)
6092 goto done;
6093 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6094 goto done;
6097 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6098 if (err)
6099 goto done;
6101 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6102 if (err)
6103 goto done;
6105 if (new_logmsg) {
6106 logmsg = strdup(new_logmsg);
6107 if (logmsg == NULL) {
6108 err = got_error_from_errno("strdup");
6109 goto done;
6111 } else {
6112 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6113 if (err)
6114 goto done;
6117 /* NB: commit_worktree will call free(logmsg) */
6118 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6119 worktree, got_object_commit_get_author(orig_commit),
6120 got_object_commit_get_committer(orig_commit),
6121 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6122 if (err)
6123 goto done;
6125 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6126 if (err)
6127 goto done;
6129 err = got_ref_delete(commit_ref, repo);
6130 if (err)
6131 goto done;
6133 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6134 fileindex, 0);
6135 sync_err = sync_fileindex(fileindex, fileindex_path);
6136 if (sync_err && err == NULL)
6137 err = sync_err;
6138 done:
6139 free(fileindex_path);
6140 free(head_commit_id);
6141 if (head_ref)
6142 got_ref_close(head_ref);
6143 if (err) {
6144 free(*new_commit_id);
6145 *new_commit_id = NULL;
6147 return err;
6150 const struct got_error *
6151 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6152 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6153 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6154 struct got_commit_object *orig_commit,
6155 struct got_object_id *orig_commit_id, struct got_repository *repo)
6157 const struct got_error *err;
6158 char *commit_ref_name;
6159 struct got_reference *commit_ref = NULL;
6160 struct got_object_id *commit_id = NULL;
6162 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6163 if (err)
6164 return err;
6166 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6167 if (err)
6168 goto done;
6169 err = got_ref_resolve(&commit_id, repo, commit_ref);
6170 if (err)
6171 goto done;
6172 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6173 err = got_error(GOT_ERR_REBASE_COMMITID);
6174 goto done;
6177 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6178 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6179 done:
6180 if (commit_ref)
6181 got_ref_close(commit_ref);
6182 free(commit_ref_name);
6183 free(commit_id);
6184 return err;
6187 const struct got_error *
6188 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6189 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6190 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6191 struct got_commit_object *orig_commit,
6192 struct got_object_id *orig_commit_id, const char *new_logmsg,
6193 struct got_repository *repo)
6195 const struct got_error *err;
6196 char *commit_ref_name;
6197 struct got_reference *commit_ref = NULL;
6199 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6200 if (err)
6201 return err;
6203 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6204 if (err)
6205 goto done;
6207 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6208 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6209 done:
6210 if (commit_ref)
6211 got_ref_close(commit_ref);
6212 free(commit_ref_name);
6213 return err;
6216 const struct got_error *
6217 got_worktree_rebase_postpone(struct got_worktree *worktree,
6218 struct got_fileindex *fileindex)
6220 if (fileindex)
6221 got_fileindex_free(fileindex);
6222 return lock_worktree(worktree, LOCK_SH);
6225 static const struct got_error *
6226 delete_ref(const char *name, struct got_repository *repo)
6228 const struct got_error *err;
6229 struct got_reference *ref;
6231 err = got_ref_open(&ref, repo, name, 0);
6232 if (err) {
6233 if (err->code == GOT_ERR_NOT_REF)
6234 return NULL;
6235 return err;
6238 err = got_ref_delete(ref, repo);
6239 got_ref_close(ref);
6240 return err;
6243 static const struct got_error *
6244 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6246 const struct got_error *err;
6247 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6248 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6250 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6251 if (err)
6252 goto done;
6253 err = delete_ref(tmp_branch_name, repo);
6254 if (err)
6255 goto done;
6257 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6258 if (err)
6259 goto done;
6260 err = delete_ref(new_base_branch_ref_name, repo);
6261 if (err)
6262 goto done;
6264 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6265 if (err)
6266 goto done;
6267 err = delete_ref(branch_ref_name, repo);
6268 if (err)
6269 goto done;
6271 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6272 if (err)
6273 goto done;
6274 err = delete_ref(commit_ref_name, repo);
6275 if (err)
6276 goto done;
6278 done:
6279 free(tmp_branch_name);
6280 free(new_base_branch_ref_name);
6281 free(branch_ref_name);
6282 free(commit_ref_name);
6283 return err;
6286 const struct got_error *
6287 got_worktree_rebase_complete(struct got_worktree *worktree,
6288 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6289 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6290 struct got_repository *repo)
6292 const struct got_error *err, *unlockerr;
6293 struct got_object_id *new_head_commit_id = NULL;
6295 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6296 if (err)
6297 return err;
6299 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6300 if (err)
6301 goto done;
6303 err = got_ref_write(rebased_branch, repo);
6304 if (err)
6305 goto done;
6307 err = got_worktree_set_head_ref(worktree, rebased_branch);
6308 if (err)
6309 goto done;
6311 err = delete_rebase_refs(worktree, repo);
6312 done:
6313 if (fileindex)
6314 got_fileindex_free(fileindex);
6315 free(new_head_commit_id);
6316 unlockerr = lock_worktree(worktree, LOCK_SH);
6317 if (unlockerr && err == NULL)
6318 err = unlockerr;
6319 return err;
6322 const struct got_error *
6323 got_worktree_rebase_abort(struct got_worktree *worktree,
6324 struct got_fileindex *fileindex, struct got_repository *repo,
6325 struct got_reference *new_base_branch,
6326 got_worktree_checkout_cb progress_cb, void *progress_arg)
6328 const struct got_error *err, *unlockerr, *sync_err;
6329 struct got_reference *resolved = NULL;
6330 struct got_object_id *commit_id = NULL;
6331 char *fileindex_path = NULL;
6332 struct revert_file_args rfa;
6333 struct got_object_id *tree_id = NULL;
6335 err = lock_worktree(worktree, LOCK_EX);
6336 if (err)
6337 return err;
6339 err = got_ref_open(&resolved, repo,
6340 got_ref_get_symref_target(new_base_branch), 0);
6341 if (err)
6342 goto done;
6344 err = got_worktree_set_head_ref(worktree, resolved);
6345 if (err)
6346 goto done;
6349 * XXX commits to the base branch could have happened while
6350 * we were busy rebasing; should we store the original commit ID
6351 * when rebase begins and read it back here?
6353 err = got_ref_resolve(&commit_id, repo, resolved);
6354 if (err)
6355 goto done;
6357 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6358 if (err)
6359 goto done;
6361 err = got_object_id_by_path(&tree_id, repo,
6362 worktree->base_commit_id, worktree->path_prefix);
6363 if (err)
6364 goto done;
6366 err = delete_rebase_refs(worktree, repo);
6367 if (err)
6368 goto done;
6370 err = get_fileindex_path(&fileindex_path, worktree);
6371 if (err)
6372 goto done;
6374 rfa.worktree = worktree;
6375 rfa.fileindex = fileindex;
6376 rfa.progress_cb = progress_cb;
6377 rfa.progress_arg = progress_arg;
6378 rfa.patch_cb = NULL;
6379 rfa.patch_arg = NULL;
6380 rfa.repo = repo;
6381 err = worktree_status(worktree, "", fileindex, repo,
6382 revert_file, &rfa, NULL, NULL, 0, 0);
6383 if (err)
6384 goto sync;
6386 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6387 repo, progress_cb, progress_arg, NULL, NULL);
6388 sync:
6389 sync_err = sync_fileindex(fileindex, fileindex_path);
6390 if (sync_err && err == NULL)
6391 err = sync_err;
6392 done:
6393 got_ref_close(resolved);
6394 free(tree_id);
6395 free(commit_id);
6396 if (fileindex)
6397 got_fileindex_free(fileindex);
6398 free(fileindex_path);
6400 unlockerr = lock_worktree(worktree, LOCK_SH);
6401 if (unlockerr && err == NULL)
6402 err = unlockerr;
6403 return err;
6406 const struct got_error *
6407 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6408 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6409 struct got_fileindex **fileindex, struct got_worktree *worktree,
6410 struct got_repository *repo)
6412 const struct got_error *err = NULL;
6413 char *tmp_branch_name = NULL;
6414 char *branch_ref_name = NULL;
6415 char *base_commit_ref_name = NULL;
6416 char *fileindex_path = NULL;
6417 struct check_rebase_ok_arg ok_arg;
6418 struct got_reference *wt_branch = NULL;
6419 struct got_reference *base_commit_ref = NULL;
6421 *tmp_branch = NULL;
6422 *branch_ref = NULL;
6423 *base_commit_id = NULL;
6424 *fileindex = NULL;
6426 err = lock_worktree(worktree, LOCK_EX);
6427 if (err)
6428 return err;
6430 err = open_fileindex(fileindex, &fileindex_path, worktree);
6431 if (err)
6432 goto done;
6434 ok_arg.worktree = worktree;
6435 ok_arg.repo = repo;
6436 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6437 &ok_arg);
6438 if (err)
6439 goto done;
6441 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6442 if (err)
6443 goto done;
6445 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6446 if (err)
6447 goto done;
6449 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6450 worktree);
6451 if (err)
6452 goto done;
6454 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6455 0);
6456 if (err)
6457 goto done;
6459 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6460 if (err)
6461 goto done;
6463 err = got_ref_write(*branch_ref, repo);
6464 if (err)
6465 goto done;
6467 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6468 worktree->base_commit_id);
6469 if (err)
6470 goto done;
6471 err = got_ref_write(base_commit_ref, repo);
6472 if (err)
6473 goto done;
6474 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6475 if (*base_commit_id == NULL) {
6476 err = got_error_from_errno("got_object_id_dup");
6477 goto done;
6480 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6481 worktree->base_commit_id);
6482 if (err)
6483 goto done;
6484 err = got_ref_write(*tmp_branch, repo);
6485 if (err)
6486 goto done;
6488 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6489 if (err)
6490 goto done;
6491 done:
6492 free(fileindex_path);
6493 free(tmp_branch_name);
6494 free(branch_ref_name);
6495 free(base_commit_ref_name);
6496 if (wt_branch)
6497 got_ref_close(wt_branch);
6498 if (err) {
6499 if (*branch_ref) {
6500 got_ref_close(*branch_ref);
6501 *branch_ref = NULL;
6503 if (*tmp_branch) {
6504 got_ref_close(*tmp_branch);
6505 *tmp_branch = NULL;
6507 free(*base_commit_id);
6508 if (*fileindex) {
6509 got_fileindex_free(*fileindex);
6510 *fileindex = NULL;
6512 lock_worktree(worktree, LOCK_SH);
6514 return err;
6517 const struct got_error *
6518 got_worktree_histedit_postpone(struct got_worktree *worktree,
6519 struct got_fileindex *fileindex)
6521 if (fileindex)
6522 got_fileindex_free(fileindex);
6523 return lock_worktree(worktree, LOCK_SH);
6526 const struct got_error *
6527 got_worktree_histedit_in_progress(int *in_progress,
6528 struct got_worktree *worktree)
6530 const struct got_error *err;
6531 char *tmp_branch_name = NULL;
6533 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6534 if (err)
6535 return err;
6537 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6538 free(tmp_branch_name);
6539 return NULL;
6542 const struct got_error *
6543 got_worktree_histedit_continue(struct got_object_id **commit_id,
6544 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6545 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6546 struct got_worktree *worktree, struct got_repository *repo)
6548 const struct got_error *err;
6549 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6550 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6551 struct got_reference *commit_ref = NULL;
6552 struct got_reference *base_commit_ref = NULL;
6553 char *fileindex_path = NULL;
6554 int have_staged_files = 0;
6556 *commit_id = NULL;
6557 *tmp_branch = NULL;
6558 *base_commit_id = NULL;
6559 *fileindex = NULL;
6561 err = lock_worktree(worktree, LOCK_EX);
6562 if (err)
6563 return err;
6565 err = open_fileindex(fileindex, &fileindex_path, worktree);
6566 if (err)
6567 goto done;
6569 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6570 &have_staged_files);
6571 if (err && err->code != GOT_ERR_CANCELLED)
6572 goto done;
6573 if (have_staged_files) {
6574 err = got_error(GOT_ERR_STAGED_PATHS);
6575 goto done;
6578 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6579 if (err)
6580 goto done;
6582 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6583 if (err)
6584 goto done;
6586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6587 if (err)
6588 goto done;
6590 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6591 worktree);
6592 if (err)
6593 goto done;
6595 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6596 if (err)
6597 goto done;
6599 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6600 if (err)
6601 goto done;
6602 err = got_ref_resolve(commit_id, repo, commit_ref);
6603 if (err)
6604 goto done;
6606 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6607 if (err)
6608 goto done;
6609 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6610 if (err)
6611 goto done;
6613 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6614 if (err)
6615 goto done;
6616 done:
6617 free(commit_ref_name);
6618 free(branch_ref_name);
6619 free(fileindex_path);
6620 if (commit_ref)
6621 got_ref_close(commit_ref);
6622 if (base_commit_ref)
6623 got_ref_close(base_commit_ref);
6624 if (err) {
6625 free(*commit_id);
6626 *commit_id = NULL;
6627 free(*base_commit_id);
6628 *base_commit_id = NULL;
6629 if (*tmp_branch) {
6630 got_ref_close(*tmp_branch);
6631 *tmp_branch = NULL;
6633 if (*fileindex) {
6634 got_fileindex_free(*fileindex);
6635 *fileindex = NULL;
6637 lock_worktree(worktree, LOCK_EX);
6639 return err;
6642 static const struct got_error *
6643 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6645 const struct got_error *err;
6646 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6647 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6649 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6650 if (err)
6651 goto done;
6652 err = delete_ref(tmp_branch_name, repo);
6653 if (err)
6654 goto done;
6656 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6657 worktree);
6658 if (err)
6659 goto done;
6660 err = delete_ref(base_commit_ref_name, repo);
6661 if (err)
6662 goto done;
6664 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6665 if (err)
6666 goto done;
6667 err = delete_ref(branch_ref_name, repo);
6668 if (err)
6669 goto done;
6671 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6672 if (err)
6673 goto done;
6674 err = delete_ref(commit_ref_name, repo);
6675 if (err)
6676 goto done;
6677 done:
6678 free(tmp_branch_name);
6679 free(base_commit_ref_name);
6680 free(branch_ref_name);
6681 free(commit_ref_name);
6682 return err;
6685 const struct got_error *
6686 got_worktree_histedit_abort(struct got_worktree *worktree,
6687 struct got_fileindex *fileindex, struct got_repository *repo,
6688 struct got_reference *branch, struct got_object_id *base_commit_id,
6689 got_worktree_checkout_cb progress_cb, void *progress_arg)
6691 const struct got_error *err, *unlockerr, *sync_err;
6692 struct got_reference *resolved = NULL;
6693 char *fileindex_path = NULL;
6694 struct got_object_id *tree_id = NULL;
6695 struct revert_file_args rfa;
6697 err = lock_worktree(worktree, LOCK_EX);
6698 if (err)
6699 return err;
6701 err = got_ref_open(&resolved, repo,
6702 got_ref_get_symref_target(branch), 0);
6703 if (err)
6704 goto done;
6706 err = got_worktree_set_head_ref(worktree, resolved);
6707 if (err)
6708 goto done;
6710 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6711 if (err)
6712 goto done;
6714 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6715 worktree->path_prefix);
6716 if (err)
6717 goto done;
6719 err = delete_histedit_refs(worktree, repo);
6720 if (err)
6721 goto done;
6723 err = get_fileindex_path(&fileindex_path, worktree);
6724 if (err)
6725 goto done;
6727 rfa.worktree = worktree;
6728 rfa.fileindex = fileindex;
6729 rfa.progress_cb = progress_cb;
6730 rfa.progress_arg = progress_arg;
6731 rfa.patch_cb = NULL;
6732 rfa.patch_arg = NULL;
6733 rfa.repo = repo;
6734 err = worktree_status(worktree, "", fileindex, repo,
6735 revert_file, &rfa, NULL, NULL, 0, 0);
6736 if (err)
6737 goto sync;
6739 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6740 repo, progress_cb, progress_arg, NULL, NULL);
6741 sync:
6742 sync_err = sync_fileindex(fileindex, fileindex_path);
6743 if (sync_err && err == NULL)
6744 err = sync_err;
6745 done:
6746 got_ref_close(resolved);
6747 free(tree_id);
6748 free(fileindex_path);
6750 unlockerr = lock_worktree(worktree, LOCK_SH);
6751 if (unlockerr && err == NULL)
6752 err = unlockerr;
6753 return err;
6756 const struct got_error *
6757 got_worktree_histedit_complete(struct got_worktree *worktree,
6758 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6759 struct got_reference *edited_branch, struct got_repository *repo)
6761 const struct got_error *err, *unlockerr;
6762 struct got_object_id *new_head_commit_id = NULL;
6763 struct got_reference *resolved = NULL;
6765 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6766 if (err)
6767 return err;
6769 err = got_ref_open(&resolved, repo,
6770 got_ref_get_symref_target(edited_branch), 0);
6771 if (err)
6772 goto done;
6774 err = got_ref_change_ref(resolved, new_head_commit_id);
6775 if (err)
6776 goto done;
6778 err = got_ref_write(resolved, repo);
6779 if (err)
6780 goto done;
6782 err = got_worktree_set_head_ref(worktree, resolved);
6783 if (err)
6784 goto done;
6786 err = delete_histedit_refs(worktree, repo);
6787 done:
6788 if (fileindex)
6789 got_fileindex_free(fileindex);
6790 free(new_head_commit_id);
6791 unlockerr = lock_worktree(worktree, LOCK_SH);
6792 if (unlockerr && err == NULL)
6793 err = unlockerr;
6794 return err;
6797 const struct got_error *
6798 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6799 struct got_object_id *commit_id, struct got_repository *repo)
6801 const struct got_error *err;
6802 char *commit_ref_name;
6804 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6805 if (err)
6806 return err;
6808 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6809 if (err)
6810 goto done;
6812 err = delete_ref(commit_ref_name, repo);
6813 done:
6814 free(commit_ref_name);
6815 return err;
6818 const struct got_error *
6819 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6820 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6821 struct got_worktree *worktree, const char *refname,
6822 struct got_repository *repo)
6824 const struct got_error *err = NULL;
6825 char *fileindex_path = NULL;
6826 struct check_rebase_ok_arg ok_arg;
6828 *fileindex = NULL;
6829 *branch_ref = NULL;
6830 *base_branch_ref = NULL;
6832 err = lock_worktree(worktree, LOCK_EX);
6833 if (err)
6834 return err;
6836 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6837 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6838 "cannot integrate a branch into itself; "
6839 "update -b or different branch name required");
6840 goto done;
6843 err = open_fileindex(fileindex, &fileindex_path, worktree);
6844 if (err)
6845 goto done;
6847 /* Preconditions are the same as for rebase. */
6848 ok_arg.worktree = worktree;
6849 ok_arg.repo = repo;
6850 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6851 &ok_arg);
6852 if (err)
6853 goto done;
6855 err = got_ref_open(branch_ref, repo, refname, 1);
6856 if (err)
6857 goto done;
6859 err = got_ref_open(base_branch_ref, repo,
6860 got_worktree_get_head_ref_name(worktree), 1);
6861 done:
6862 if (err) {
6863 if (*branch_ref) {
6864 got_ref_close(*branch_ref);
6865 *branch_ref = NULL;
6867 if (*base_branch_ref) {
6868 got_ref_close(*base_branch_ref);
6869 *base_branch_ref = NULL;
6871 if (*fileindex) {
6872 got_fileindex_free(*fileindex);
6873 *fileindex = NULL;
6875 lock_worktree(worktree, LOCK_SH);
6877 return err;
6880 const struct got_error *
6881 got_worktree_integrate_continue(struct got_worktree *worktree,
6882 struct got_fileindex *fileindex, struct got_repository *repo,
6883 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6884 got_worktree_checkout_cb progress_cb, void *progress_arg,
6885 got_cancel_cb cancel_cb, void *cancel_arg)
6887 const struct got_error *err = NULL, *sync_err, *unlockerr;
6888 char *fileindex_path = NULL;
6889 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6891 err = get_fileindex_path(&fileindex_path, worktree);
6892 if (err)
6893 goto done;
6895 err = got_ref_resolve(&commit_id, repo, branch_ref);
6896 if (err)
6897 goto done;
6899 err = got_object_id_by_path(&tree_id, repo, commit_id,
6900 worktree->path_prefix);
6901 if (err)
6902 goto done;
6904 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6905 if (err)
6906 goto done;
6908 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6909 progress_cb, progress_arg, cancel_cb, cancel_arg);
6910 if (err)
6911 goto sync;
6913 err = got_ref_change_ref(base_branch_ref, commit_id);
6914 if (err)
6915 goto sync;
6917 err = got_ref_write(base_branch_ref, repo);
6918 sync:
6919 sync_err = sync_fileindex(fileindex, fileindex_path);
6920 if (sync_err && err == NULL)
6921 err = sync_err;
6923 done:
6924 unlockerr = got_ref_unlock(branch_ref);
6925 if (unlockerr && err == NULL)
6926 err = unlockerr;
6927 got_ref_close(branch_ref);
6929 unlockerr = got_ref_unlock(base_branch_ref);
6930 if (unlockerr && err == NULL)
6931 err = unlockerr;
6932 got_ref_close(base_branch_ref);
6934 got_fileindex_free(fileindex);
6935 free(fileindex_path);
6936 free(tree_id);
6938 unlockerr = lock_worktree(worktree, LOCK_SH);
6939 if (unlockerr && err == NULL)
6940 err = unlockerr;
6941 return err;
6944 const struct got_error *
6945 got_worktree_integrate_abort(struct got_worktree *worktree,
6946 struct got_fileindex *fileindex, struct got_repository *repo,
6947 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6949 const struct got_error *err = NULL, *unlockerr = NULL;
6951 got_fileindex_free(fileindex);
6953 err = lock_worktree(worktree, LOCK_SH);
6955 unlockerr = got_ref_unlock(branch_ref);
6956 if (unlockerr && err == NULL)
6957 err = unlockerr;
6958 got_ref_close(branch_ref);
6960 unlockerr = got_ref_unlock(base_branch_ref);
6961 if (unlockerr && err == NULL)
6962 err = unlockerr;
6963 got_ref_close(base_branch_ref);
6965 return err;
6968 struct check_stage_ok_arg {
6969 struct got_object_id *head_commit_id;
6970 struct got_worktree *worktree;
6971 struct got_fileindex *fileindex;
6972 struct got_repository *repo;
6973 int have_changes;
6976 const struct got_error *
6977 check_stage_ok(void *arg, unsigned char status,
6978 unsigned char staged_status, const char *relpath,
6979 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6980 struct got_object_id *commit_id, int dirfd, const char *de_name)
6982 struct check_stage_ok_arg *a = arg;
6983 const struct got_error *err = NULL;
6984 struct got_fileindex_entry *ie;
6985 struct got_object_id base_commit_id;
6986 struct got_object_id *base_commit_idp = NULL;
6987 char *in_repo_path = NULL, *p;
6989 if (status == GOT_STATUS_UNVERSIONED ||
6990 status == GOT_STATUS_NO_CHANGE)
6991 return NULL;
6992 if (status == GOT_STATUS_NONEXISTENT)
6993 return got_error_set_errno(ENOENT, relpath);
6995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6996 if (ie == NULL)
6997 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6999 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7000 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7001 relpath) == -1)
7002 return got_error_from_errno("asprintf");
7004 if (got_fileindex_entry_has_commit(ie)) {
7005 memcpy(base_commit_id.sha1, ie->commit_sha1,
7006 SHA1_DIGEST_LENGTH);
7007 base_commit_idp = &base_commit_id;
7010 if (status == GOT_STATUS_CONFLICT) {
7011 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7012 goto done;
7013 } else if (status != GOT_STATUS_ADD &&
7014 status != GOT_STATUS_MODIFY &&
7015 status != GOT_STATUS_DELETE) {
7016 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7017 goto done;
7020 a->have_changes = 1;
7022 p = in_repo_path;
7023 while (p[0] == '/')
7024 p++;
7025 err = check_out_of_date(p, status, staged_status,
7026 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7027 GOT_ERR_STAGE_OUT_OF_DATE);
7028 done:
7029 free(in_repo_path);
7030 return err;
7033 struct stage_path_arg {
7034 struct got_worktree *worktree;
7035 struct got_fileindex *fileindex;
7036 struct got_repository *repo;
7037 got_worktree_status_cb status_cb;
7038 void *status_arg;
7039 got_worktree_patch_cb patch_cb;
7040 void *patch_arg;
7041 int staged_something;
7042 int allow_bad_symlinks;
7045 static const struct got_error *
7046 stage_path(void *arg, unsigned char status,
7047 unsigned char staged_status, const char *relpath,
7048 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7049 struct got_object_id *commit_id, int dirfd, const char *de_name)
7051 struct stage_path_arg *a = arg;
7052 const struct got_error *err = NULL;
7053 struct got_fileindex_entry *ie;
7054 char *ondisk_path = NULL, *path_content = NULL;
7055 uint32_t stage;
7056 struct got_object_id *new_staged_blob_id = NULL;
7057 struct stat sb;
7059 if (status == GOT_STATUS_UNVERSIONED)
7060 return NULL;
7062 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7063 if (ie == NULL)
7064 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7067 relpath)== -1)
7068 return got_error_from_errno("asprintf");
7070 switch (status) {
7071 case GOT_STATUS_ADD:
7072 case GOT_STATUS_MODIFY:
7073 /* XXX could sb.st_mode be passed in by our caller? */
7074 if (lstat(ondisk_path, &sb) == -1) {
7075 err = got_error_from_errno2("lstat", ondisk_path);
7076 break;
7078 if (a->patch_cb) {
7079 if (status == GOT_STATUS_ADD) {
7080 int choice = GOT_PATCH_CHOICE_NONE;
7081 err = (*a->patch_cb)(&choice, a->patch_arg,
7082 status, ie->path, NULL, 1, 1);
7083 if (err)
7084 break;
7085 if (choice != GOT_PATCH_CHOICE_YES)
7086 break;
7087 } else {
7088 err = create_patched_content(&path_content, 0,
7089 staged_blob_id ? staged_blob_id : blob_id,
7090 ondisk_path, dirfd, de_name, ie->path,
7091 a->repo, a->patch_cb, a->patch_arg);
7092 if (err || path_content == NULL)
7093 break;
7096 err = got_object_blob_create(&new_staged_blob_id,
7097 path_content ? path_content : ondisk_path, a->repo);
7098 if (err)
7099 break;
7100 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7101 SHA1_DIGEST_LENGTH);
7102 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7103 stage = GOT_FILEIDX_STAGE_ADD;
7104 else
7105 stage = GOT_FILEIDX_STAGE_MODIFY;
7106 got_fileindex_entry_stage_set(ie, stage);
7107 if (S_ISLNK(sb.st_mode)) {
7108 int is_bad_symlink = 0;
7109 if (!a->allow_bad_symlinks) {
7110 char target_path[PATH_MAX];
7111 ssize_t target_len;
7112 target_len = readlink(ondisk_path, target_path,
7113 sizeof(target_path));
7114 if (target_len == -1) {
7115 err = got_error_from_errno2("readlink",
7116 ondisk_path);
7117 break;
7119 err = is_bad_symlink_target(&is_bad_symlink,
7120 target_path, target_len, ondisk_path,
7121 a->worktree->root_path);
7122 if (err)
7123 break;
7124 if (is_bad_symlink) {
7125 err = got_error_path(ondisk_path,
7126 GOT_ERR_BAD_SYMLINK);
7127 break;
7130 if (is_bad_symlink)
7131 got_fileindex_entry_staged_filetype_set(ie,
7132 GOT_FILEIDX_MODE_BAD_SYMLINK);
7133 else
7134 got_fileindex_entry_staged_filetype_set(ie,
7135 GOT_FILEIDX_MODE_SYMLINK);
7136 } else {
7137 got_fileindex_entry_staged_filetype_set(ie,
7138 GOT_FILEIDX_MODE_REGULAR_FILE);
7140 a->staged_something = 1;
7141 if (a->status_cb == NULL)
7142 break;
7143 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7144 get_staged_status(ie), relpath, blob_id,
7145 new_staged_blob_id, NULL, dirfd, de_name);
7146 break;
7147 case GOT_STATUS_DELETE:
7148 if (staged_status == GOT_STATUS_DELETE)
7149 break;
7150 if (a->patch_cb) {
7151 int choice = GOT_PATCH_CHOICE_NONE;
7152 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7153 ie->path, NULL, 1, 1);
7154 if (err)
7155 break;
7156 if (choice == GOT_PATCH_CHOICE_NO)
7157 break;
7158 if (choice != GOT_PATCH_CHOICE_YES) {
7159 err = got_error(GOT_ERR_PATCH_CHOICE);
7160 break;
7163 stage = GOT_FILEIDX_STAGE_DELETE;
7164 got_fileindex_entry_stage_set(ie, stage);
7165 a->staged_something = 1;
7166 if (a->status_cb == NULL)
7167 break;
7168 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7169 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7170 de_name);
7171 break;
7172 case GOT_STATUS_NO_CHANGE:
7173 break;
7174 case GOT_STATUS_CONFLICT:
7175 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7176 break;
7177 case GOT_STATUS_NONEXISTENT:
7178 err = got_error_set_errno(ENOENT, relpath);
7179 break;
7180 default:
7181 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7182 break;
7185 if (path_content && unlink(path_content) == -1 && err == NULL)
7186 err = got_error_from_errno2("unlink", path_content);
7187 free(path_content);
7188 free(ondisk_path);
7189 free(new_staged_blob_id);
7190 return err;
7193 const struct got_error *
7194 got_worktree_stage(struct got_worktree *worktree,
7195 struct got_pathlist_head *paths,
7196 got_worktree_status_cb status_cb, void *status_arg,
7197 got_worktree_patch_cb patch_cb, void *patch_arg,
7198 int allow_bad_symlinks, struct got_repository *repo)
7200 const struct got_error *err = NULL, *sync_err, *unlockerr;
7201 struct got_pathlist_entry *pe;
7202 struct got_fileindex *fileindex = NULL;
7203 char *fileindex_path = NULL;
7204 struct got_reference *head_ref = NULL;
7205 struct got_object_id *head_commit_id = NULL;
7206 struct check_stage_ok_arg oka;
7207 struct stage_path_arg spa;
7209 err = lock_worktree(worktree, LOCK_EX);
7210 if (err)
7211 return err;
7213 err = got_ref_open(&head_ref, repo,
7214 got_worktree_get_head_ref_name(worktree), 0);
7215 if (err)
7216 goto done;
7217 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7218 if (err)
7219 goto done;
7220 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7221 if (err)
7222 goto done;
7224 /* Check pre-conditions before staging anything. */
7225 oka.head_commit_id = head_commit_id;
7226 oka.worktree = worktree;
7227 oka.fileindex = fileindex;
7228 oka.repo = repo;
7229 oka.have_changes = 0;
7230 TAILQ_FOREACH(pe, paths, entry) {
7231 err = worktree_status(worktree, pe->path, fileindex, repo,
7232 check_stage_ok, &oka, NULL, NULL, 0, 0);
7233 if (err)
7234 goto done;
7236 if (!oka.have_changes) {
7237 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7238 goto done;
7241 spa.worktree = worktree;
7242 spa.fileindex = fileindex;
7243 spa.repo = repo;
7244 spa.patch_cb = patch_cb;
7245 spa.patch_arg = patch_arg;
7246 spa.status_cb = status_cb;
7247 spa.status_arg = status_arg;
7248 spa.staged_something = 0;
7249 spa.allow_bad_symlinks = allow_bad_symlinks;
7250 TAILQ_FOREACH(pe, paths, entry) {
7251 err = worktree_status(worktree, pe->path, fileindex, repo,
7252 stage_path, &spa, NULL, NULL, 0, 0);
7253 if (err)
7254 goto done;
7256 if (!spa.staged_something) {
7257 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7258 goto done;
7261 sync_err = sync_fileindex(fileindex, fileindex_path);
7262 if (sync_err && err == NULL)
7263 err = sync_err;
7264 done:
7265 if (head_ref)
7266 got_ref_close(head_ref);
7267 free(head_commit_id);
7268 free(fileindex_path);
7269 if (fileindex)
7270 got_fileindex_free(fileindex);
7271 unlockerr = lock_worktree(worktree, LOCK_SH);
7272 if (unlockerr && err == NULL)
7273 err = unlockerr;
7274 return err;
7277 struct unstage_path_arg {
7278 struct got_worktree *worktree;
7279 struct got_fileindex *fileindex;
7280 struct got_repository *repo;
7281 got_worktree_checkout_cb progress_cb;
7282 void *progress_arg;
7283 got_worktree_patch_cb patch_cb;
7284 void *patch_arg;
7287 static const struct got_error *
7288 create_unstaged_content(char **path_unstaged_content,
7289 char **path_new_staged_content, struct got_object_id *blob_id,
7290 struct got_object_id *staged_blob_id, const char *relpath,
7291 struct got_repository *repo,
7292 got_worktree_patch_cb patch_cb, void *patch_arg)
7294 const struct got_error *err;
7295 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7296 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7297 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7298 struct stat sb1, sb2;
7299 struct got_diff_changes *changes = NULL;
7300 struct got_diff_state *ds = NULL;
7301 struct got_diff_args *args = NULL;
7302 struct got_diff_change *change;
7303 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7304 int have_content = 0, have_rejected_content = 0;
7306 *path_unstaged_content = NULL;
7307 *path_new_staged_content = NULL;
7309 err = got_object_id_str(&label1, blob_id);
7310 if (err)
7311 return err;
7312 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7313 if (err)
7314 goto done;
7316 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7317 if (err)
7318 goto done;
7320 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7321 if (err)
7322 goto done;
7324 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7325 if (err)
7326 goto done;
7328 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7329 if (err)
7330 goto done;
7332 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7333 if (err)
7334 goto done;
7336 if (stat(path1, &sb1) == -1) {
7337 err = got_error_from_errno2("stat", path1);
7338 goto done;
7341 if (stat(path2, &sb2) == -1) {
7342 err = got_error_from_errno2("stat", path2);
7343 goto done;
7346 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7347 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7348 if (err)
7349 goto done;
7351 err = got_opentemp_named(path_unstaged_content, &outfile,
7352 "got-unstaged-content");
7353 if (err)
7354 goto done;
7355 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7356 "got-new-staged-content");
7357 if (err)
7358 goto done;
7360 if (fseek(f1, 0L, SEEK_SET) == -1) {
7361 err = got_ferror(f1, GOT_ERR_IO);
7362 goto done;
7364 if (fseek(f2, 0L, SEEK_SET) == -1) {
7365 err = got_ferror(f2, GOT_ERR_IO);
7366 goto done;
7368 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7369 int choice;
7370 err = apply_or_reject_change(&choice, change, ++n,
7371 changes->nchanges, ds, args, diff_flags, relpath,
7372 f1, f2, &line_cur1, &line_cur2,
7373 outfile, rejectfile, patch_cb, patch_arg);
7374 if (err)
7375 goto done;
7376 if (choice == GOT_PATCH_CHOICE_YES)
7377 have_content = 1;
7378 else
7379 have_rejected_content = 1;
7380 if (choice == GOT_PATCH_CHOICE_QUIT)
7381 break;
7383 if (have_content || have_rejected_content)
7384 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7385 outfile, rejectfile);
7386 done:
7387 free(label1);
7388 if (blob)
7389 got_object_blob_close(blob);
7390 if (staged_blob)
7391 got_object_blob_close(staged_blob);
7392 if (f1 && fclose(f1) == EOF && err == NULL)
7393 err = got_error_from_errno2("fclose", path1);
7394 if (f2 && fclose(f2) == EOF && err == NULL)
7395 err = got_error_from_errno2("fclose", path2);
7396 if (outfile && fclose(outfile) == EOF && err == NULL)
7397 err = got_error_from_errno2("fclose", *path_unstaged_content);
7398 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7399 err = got_error_from_errno2("fclose", *path_new_staged_content);
7400 if (path1 && unlink(path1) == -1 && err == NULL)
7401 err = got_error_from_errno2("unlink", path1);
7402 if (path2 && unlink(path2) == -1 && err == NULL)
7403 err = got_error_from_errno2("unlink", path2);
7404 if (err || !have_content) {
7405 if (*path_unstaged_content &&
7406 unlink(*path_unstaged_content) == -1 && err == NULL)
7407 err = got_error_from_errno2("unlink",
7408 *path_unstaged_content);
7409 free(*path_unstaged_content);
7410 *path_unstaged_content = NULL;
7412 if (err || !have_content || !have_rejected_content) {
7413 if (*path_new_staged_content &&
7414 unlink(*path_new_staged_content) == -1 && err == NULL)
7415 err = got_error_from_errno2("unlink",
7416 *path_new_staged_content);
7417 free(*path_new_staged_content);
7418 *path_new_staged_content = NULL;
7420 free(args);
7421 if (ds) {
7422 got_diff_state_free(ds);
7423 free(ds);
7425 if (changes)
7426 got_diff_free_changes(changes);
7427 free(path1);
7428 free(path2);
7429 return err;
7432 static const struct got_error *
7433 unstage_hunks(struct got_object_id *staged_blob_id,
7434 struct got_blob_object *blob_base,
7435 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7436 const char *ondisk_path, const char *label_orig,
7437 struct got_worktree *worktree, struct got_repository *repo,
7438 got_worktree_patch_cb patch_cb, void *patch_arg,
7439 got_worktree_checkout_cb progress_cb, void *progress_arg)
7441 const struct got_error *err = NULL;
7442 char *path_unstaged_content = NULL;
7443 char *path_new_staged_content = NULL;
7444 struct got_object_id *new_staged_blob_id = NULL;
7445 FILE *f = NULL;
7446 struct stat sb;
7448 err = create_unstaged_content(&path_unstaged_content,
7449 &path_new_staged_content, blob_id, staged_blob_id,
7450 ie->path, repo, patch_cb, patch_arg);
7451 if (err)
7452 return err;
7454 if (path_unstaged_content == NULL)
7455 return NULL;
7457 if (path_new_staged_content) {
7458 err = got_object_blob_create(&new_staged_blob_id,
7459 path_new_staged_content, repo);
7460 if (err)
7461 goto done;
7464 f = fopen(path_unstaged_content, "r");
7465 if (f == NULL) {
7466 err = got_error_from_errno2("fopen",
7467 path_unstaged_content);
7468 goto done;
7470 if (fstat(fileno(f), &sb) == -1) {
7471 err = got_error_from_errno2("fstat", path_unstaged_content);
7472 goto done;
7474 if (got_fileindex_entry_staged_filetype_get(ie) ==
7475 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7476 char link_target[PATH_MAX];
7477 size_t r;
7478 r = fread(link_target, 1, sizeof(link_target), f);
7479 if (r == 0 && ferror(f)) {
7480 err = got_error_from_errno("fread");
7481 goto done;
7483 if (r >= sizeof(link_target)) { /* should not happen */
7484 err = got_error(GOT_ERR_NO_SPACE);
7485 goto done;
7487 link_target[r] = '\0';
7488 err = merge_symlink(worktree, blob_base,
7489 ondisk_path, ie->path, label_orig, link_target,
7490 worktree->base_commit_id, repo, progress_cb,
7491 progress_arg);
7492 } else {
7493 int local_changes_subsumed;
7494 err = merge_file(&local_changes_subsumed, worktree,
7495 blob_base, ondisk_path, ie->path,
7496 got_fileindex_perms_to_st(ie),
7497 path_unstaged_content, label_orig, "unstaged",
7498 repo, progress_cb, progress_arg);
7500 if (err)
7501 goto done;
7503 if (new_staged_blob_id) {
7504 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7505 SHA1_DIGEST_LENGTH);
7506 } else
7507 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7508 done:
7509 free(new_staged_blob_id);
7510 if (path_unstaged_content &&
7511 unlink(path_unstaged_content) == -1 && err == NULL)
7512 err = got_error_from_errno2("unlink", path_unstaged_content);
7513 if (path_new_staged_content &&
7514 unlink(path_new_staged_content) == -1 && err == NULL)
7515 err = got_error_from_errno2("unlink", path_new_staged_content);
7516 if (f && fclose(f) != 0 && err == NULL)
7517 err = got_error_from_errno2("fclose", path_unstaged_content);
7518 free(path_unstaged_content);
7519 free(path_new_staged_content);
7520 return err;
7523 static const struct got_error *
7524 unstage_path(void *arg, unsigned char status,
7525 unsigned char staged_status, const char *relpath,
7526 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7527 struct got_object_id *commit_id, int dirfd, const char *de_name)
7529 const struct got_error *err = NULL;
7530 struct unstage_path_arg *a = arg;
7531 struct got_fileindex_entry *ie;
7532 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7533 char *ondisk_path = NULL;
7534 char *id_str = NULL, *label_orig = NULL;
7535 int local_changes_subsumed;
7536 struct stat sb;
7538 if (staged_status != GOT_STATUS_ADD &&
7539 staged_status != GOT_STATUS_MODIFY &&
7540 staged_status != GOT_STATUS_DELETE)
7541 return NULL;
7543 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7544 if (ie == NULL)
7545 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7547 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7548 == -1)
7549 return got_error_from_errno("asprintf");
7551 err = got_object_id_str(&id_str,
7552 commit_id ? commit_id : a->worktree->base_commit_id);
7553 if (err)
7554 goto done;
7555 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7556 id_str) == -1) {
7557 err = got_error_from_errno("asprintf");
7558 goto done;
7561 switch (staged_status) {
7562 case GOT_STATUS_MODIFY:
7563 err = got_object_open_as_blob(&blob_base, a->repo,
7564 blob_id, 8192);
7565 if (err)
7566 break;
7567 /* fall through */
7568 case GOT_STATUS_ADD:
7569 if (a->patch_cb) {
7570 if (staged_status == GOT_STATUS_ADD) {
7571 int choice = GOT_PATCH_CHOICE_NONE;
7572 err = (*a->patch_cb)(&choice, a->patch_arg,
7573 staged_status, ie->path, NULL, 1, 1);
7574 if (err)
7575 break;
7576 if (choice != GOT_PATCH_CHOICE_YES)
7577 break;
7578 } else {
7579 err = unstage_hunks(staged_blob_id,
7580 blob_base, blob_id, ie, ondisk_path,
7581 label_orig, a->worktree, a->repo,
7582 a->patch_cb, a->patch_arg,
7583 a->progress_cb, a->progress_arg);
7584 break; /* Done with this file. */
7587 err = got_object_open_as_blob(&blob_staged, a->repo,
7588 staged_blob_id, 8192);
7589 if (err)
7590 break;
7591 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7592 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7593 case GOT_FILEIDX_MODE_REGULAR_FILE:
7594 err = merge_blob(&local_changes_subsumed, a->worktree,
7595 blob_base, ondisk_path, relpath,
7596 got_fileindex_perms_to_st(ie), label_orig,
7597 blob_staged, commit_id ? commit_id :
7598 a->worktree->base_commit_id, a->repo,
7599 a->progress_cb, a->progress_arg);
7600 break;
7601 case GOT_FILEIDX_MODE_SYMLINK:
7602 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7603 char *staged_target;
7604 err = got_object_blob_read_to_str(
7605 &staged_target, blob_staged);
7606 if (err)
7607 goto done;
7608 err = merge_symlink(a->worktree, blob_base,
7609 ondisk_path, relpath, label_orig,
7610 staged_target, commit_id ? commit_id :
7611 a->worktree->base_commit_id,
7612 a->repo, a->progress_cb, a->progress_arg);
7613 free(staged_target);
7614 } else {
7615 err = merge_blob(&local_changes_subsumed,
7616 a->worktree, blob_base, ondisk_path,
7617 relpath, got_fileindex_perms_to_st(ie),
7618 label_orig, blob_staged,
7619 commit_id ? commit_id :
7620 a->worktree->base_commit_id, a->repo,
7621 a->progress_cb, a->progress_arg);
7623 break;
7624 default:
7625 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7626 break;
7628 if (err == NULL)
7629 got_fileindex_entry_stage_set(ie,
7630 GOT_FILEIDX_STAGE_NONE);
7631 break;
7632 case GOT_STATUS_DELETE:
7633 if (a->patch_cb) {
7634 int choice = GOT_PATCH_CHOICE_NONE;
7635 err = (*a->patch_cb)(&choice, a->patch_arg,
7636 staged_status, ie->path, NULL, 1, 1);
7637 if (err)
7638 break;
7639 if (choice == GOT_PATCH_CHOICE_NO)
7640 break;
7641 if (choice != GOT_PATCH_CHOICE_YES) {
7642 err = got_error(GOT_ERR_PATCH_CHOICE);
7643 break;
7646 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7647 err = get_file_status(&status, &sb, ie, ondisk_path,
7648 dirfd, de_name, a->repo);
7649 if (err)
7650 break;
7651 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7652 break;
7654 done:
7655 free(ondisk_path);
7656 if (blob_base)
7657 got_object_blob_close(blob_base);
7658 if (blob_staged)
7659 got_object_blob_close(blob_staged);
7660 free(id_str);
7661 free(label_orig);
7662 return err;
7665 const struct got_error *
7666 got_worktree_unstage(struct got_worktree *worktree,
7667 struct got_pathlist_head *paths,
7668 got_worktree_checkout_cb progress_cb, void *progress_arg,
7669 got_worktree_patch_cb patch_cb, void *patch_arg,
7670 struct got_repository *repo)
7672 const struct got_error *err = NULL, *sync_err, *unlockerr;
7673 struct got_pathlist_entry *pe;
7674 struct got_fileindex *fileindex = NULL;
7675 char *fileindex_path = NULL;
7676 struct unstage_path_arg upa;
7678 err = lock_worktree(worktree, LOCK_EX);
7679 if (err)
7680 return err;
7682 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7683 if (err)
7684 goto done;
7686 upa.worktree = worktree;
7687 upa.fileindex = fileindex;
7688 upa.repo = repo;
7689 upa.progress_cb = progress_cb;
7690 upa.progress_arg = progress_arg;
7691 upa.patch_cb = patch_cb;
7692 upa.patch_arg = patch_arg;
7693 TAILQ_FOREACH(pe, paths, entry) {
7694 err = worktree_status(worktree, pe->path, fileindex, repo,
7695 unstage_path, &upa, NULL, NULL, 0, 0);
7696 if (err)
7697 goto done;
7700 sync_err = sync_fileindex(fileindex, fileindex_path);
7701 if (sync_err && err == NULL)
7702 err = sync_err;
7703 done:
7704 free(fileindex_path);
7705 if (fileindex)
7706 got_fileindex_free(fileindex);
7707 unlockerr = lock_worktree(worktree, LOCK_SH);
7708 if (unlockerr && err == NULL)
7709 err = unlockerr;
7710 return err;
7713 struct report_file_info_arg {
7714 struct got_worktree *worktree;
7715 got_worktree_path_info_cb info_cb;
7716 void *info_arg;
7717 struct got_pathlist_head *paths;
7718 got_cancel_cb cancel_cb;
7719 void *cancel_arg;
7722 static const struct got_error *
7723 report_file_info(void *arg, struct got_fileindex_entry *ie)
7725 struct report_file_info_arg *a = arg;
7726 struct got_pathlist_entry *pe;
7727 struct got_object_id blob_id, staged_blob_id, commit_id;
7728 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7729 struct got_object_id *commit_idp = NULL;
7730 int stage;
7732 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7733 return got_error(GOT_ERR_CANCELLED);
7735 TAILQ_FOREACH(pe, a->paths, entry) {
7736 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7737 got_path_is_child(ie->path, pe->path, pe->path_len))
7738 break;
7740 if (pe == NULL) /* not found */
7741 return NULL;
7743 if (got_fileindex_entry_has_blob(ie)) {
7744 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7745 blob_idp = &blob_id;
7747 stage = got_fileindex_entry_stage_get(ie);
7748 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7749 stage == GOT_FILEIDX_STAGE_ADD) {
7750 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7751 SHA1_DIGEST_LENGTH);
7752 staged_blob_idp = &staged_blob_id;
7755 if (got_fileindex_entry_has_commit(ie)) {
7756 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7757 commit_idp = &commit_id;
7760 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7761 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7764 const struct got_error *
7765 got_worktree_path_info(struct got_worktree *worktree,
7766 struct got_pathlist_head *paths,
7767 got_worktree_path_info_cb info_cb, void *info_arg,
7768 got_cancel_cb cancel_cb, void *cancel_arg)
7771 const struct got_error *err = NULL, *unlockerr;
7772 struct got_fileindex *fileindex = NULL;
7773 char *fileindex_path = NULL;
7774 struct report_file_info_arg arg;
7776 err = lock_worktree(worktree, LOCK_SH);
7777 if (err)
7778 return err;
7780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7781 if (err)
7782 goto done;
7784 arg.worktree = worktree;
7785 arg.info_cb = info_cb;
7786 arg.info_arg = info_arg;
7787 arg.paths = paths;
7788 arg.cancel_cb = cancel_cb;
7789 arg.cancel_arg = cancel_arg;
7790 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7791 &arg);
7792 done:
7793 free(fileindex_path);
7794 if (fileindex)
7795 got_fileindex_free(fileindex);
7796 unlockerr = lock_worktree(worktree, LOCK_UN);
7797 if (unlockerr && err == NULL)
7798 err = unlockerr;
7799 return err;