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(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 char *id_str;
2935 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2936 worktree->path_prefix);
2937 if (err)
2938 goto done;
2940 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2941 if (err)
2942 goto done;
2944 err = got_object_id_str(&id_str, commit_id1);
2945 if (err)
2946 goto done;
2948 if (asprintf(&label_orig, "%s: commit %s",
2949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2950 err = got_error_from_errno("asprintf");
2951 free(id_str);
2952 goto done;
2954 free(id_str);
2957 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2958 worktree->path_prefix);
2959 if (err)
2960 goto done;
2962 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2963 if (err)
2964 goto done;
2966 arg.worktree = worktree;
2967 arg.fileindex = fileindex;
2968 arg.progress_cb = progress_cb;
2969 arg.progress_arg = progress_arg;
2970 arg.cancel_cb = cancel_cb;
2971 arg.cancel_arg = cancel_arg;
2972 arg.label_orig = label_orig;
2973 arg.commit_id2 = commit_id2;
2974 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2975 sync_err = sync_fileindex(fileindex, fileindex_path);
2976 if (sync_err && err == NULL)
2977 err = sync_err;
2978 done:
2979 if (tree1)
2980 got_object_tree_close(tree1);
2981 if (tree2)
2982 got_object_tree_close(tree2);
2983 free(label_orig);
2984 return err;
2987 const struct got_error *
2988 got_worktree_merge_files(struct got_worktree *worktree,
2989 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2990 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2991 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2993 const struct got_error *err, *unlockerr;
2994 char *fileindex_path = NULL;
2995 struct got_fileindex *fileindex = NULL;
2996 struct check_merge_ok_arg mok_arg;
2998 err = lock_worktree(worktree, LOCK_EX);
2999 if (err)
3000 return err;
3002 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3003 if (err)
3004 goto done;
3006 mok_arg.worktree = worktree;
3007 mok_arg.repo = repo;
3008 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3009 &mok_arg);
3010 if (err)
3011 goto done;
3013 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3014 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3015 done:
3016 if (fileindex)
3017 got_fileindex_free(fileindex);
3018 free(fileindex_path);
3019 unlockerr = lock_worktree(worktree, LOCK_SH);
3020 if (unlockerr && err == NULL)
3021 err = unlockerr;
3022 return err;
3025 struct diff_dir_cb_arg {
3026 struct got_fileindex *fileindex;
3027 struct got_worktree *worktree;
3028 const char *status_path;
3029 size_t status_path_len;
3030 struct got_repository *repo;
3031 got_worktree_status_cb status_cb;
3032 void *status_arg;
3033 got_cancel_cb cancel_cb;
3034 void *cancel_arg;
3035 /* A pathlist containing per-directory pathlists of ignore patterns. */
3036 struct got_pathlist_head ignores;
3037 int report_unchanged;
3038 int no_ignores;
3041 static const struct got_error *
3042 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3043 int dirfd, const char *de_name,
3044 got_worktree_status_cb status_cb, void *status_arg,
3045 struct got_repository *repo, int report_unchanged)
3047 const struct got_error *err = NULL;
3048 unsigned char status = GOT_STATUS_NO_CHANGE;
3049 unsigned char staged_status = get_staged_status(ie);
3050 struct stat sb;
3051 struct got_object_id blob_id, commit_id, staged_blob_id;
3052 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3053 struct got_object_id *staged_blob_idp = NULL;
3055 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3056 if (err)
3057 return err;
3059 if (status == GOT_STATUS_NO_CHANGE &&
3060 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3061 return NULL;
3063 if (got_fileindex_entry_has_blob(ie)) {
3064 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3065 blob_idp = &blob_id;
3067 if (got_fileindex_entry_has_commit(ie)) {
3068 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3069 commit_idp = &commit_id;
3071 if (staged_status == GOT_STATUS_ADD ||
3072 staged_status == GOT_STATUS_MODIFY) {
3073 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3074 SHA1_DIGEST_LENGTH);
3075 staged_blob_idp = &staged_blob_id;
3078 return (*status_cb)(status_arg, status, staged_status,
3079 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3082 static const struct got_error *
3083 status_old_new(void *arg, struct got_fileindex_entry *ie,
3084 struct dirent *de, const char *parent_path, int dirfd)
3086 const struct got_error *err = NULL;
3087 struct diff_dir_cb_arg *a = arg;
3088 char *abspath;
3090 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3091 return got_error(GOT_ERR_CANCELLED);
3093 if (got_path_cmp(parent_path, a->status_path,
3094 strlen(parent_path), a->status_path_len) != 0 &&
3095 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3096 return NULL;
3098 if (parent_path[0]) {
3099 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3100 parent_path, de->d_name) == -1)
3101 return got_error_from_errno("asprintf");
3102 } else {
3103 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3104 de->d_name) == -1)
3105 return got_error_from_errno("asprintf");
3108 err = report_file_status(ie, abspath, dirfd, de->d_name,
3109 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3110 free(abspath);
3111 return err;
3114 static const struct got_error *
3115 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3117 struct diff_dir_cb_arg *a = arg;
3118 struct got_object_id blob_id, commit_id;
3119 unsigned char status;
3121 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3122 return got_error(GOT_ERR_CANCELLED);
3124 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3125 return NULL;
3127 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3128 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3129 if (got_fileindex_entry_has_file_on_disk(ie))
3130 status = GOT_STATUS_MISSING;
3131 else
3132 status = GOT_STATUS_DELETE;
3133 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3134 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3137 void
3138 free_ignorelist(struct got_pathlist_head *ignorelist)
3140 struct got_pathlist_entry *pe;
3142 TAILQ_FOREACH(pe, ignorelist, entry)
3143 free((char *)pe->path);
3144 got_pathlist_free(ignorelist);
3147 void
3148 free_ignores(struct got_pathlist_head *ignores)
3150 struct got_pathlist_entry *pe;
3152 TAILQ_FOREACH(pe, ignores, entry) {
3153 struct got_pathlist_head *ignorelist = pe->data;
3154 free_ignorelist(ignorelist);
3155 free((char *)pe->path);
3157 got_pathlist_free(ignores);
3160 static const struct got_error *
3161 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3163 const struct got_error *err = NULL;
3164 struct got_pathlist_entry *pe = NULL;
3165 struct got_pathlist_head *ignorelist;
3166 char *line = NULL, *pattern, *dirpath = NULL;
3167 size_t linesize = 0;
3168 ssize_t linelen;
3170 ignorelist = calloc(1, sizeof(*ignorelist));
3171 if (ignorelist == NULL)
3172 return got_error_from_errno("calloc");
3173 TAILQ_INIT(ignorelist);
3175 while ((linelen = getline(&line, &linesize, f)) != -1) {
3176 if (linelen > 0 && line[linelen - 1] == '\n')
3177 line[linelen - 1] = '\0';
3179 /* Git's ignores may contain comments. */
3180 if (line[0] == '#')
3181 continue;
3183 /* Git's negated patterns are not (yet?) supported. */
3184 if (line[0] == '!')
3185 continue;
3187 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3188 line) == -1) {
3189 err = got_error_from_errno("asprintf");
3190 goto done;
3192 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3193 if (err)
3194 goto done;
3196 if (ferror(f)) {
3197 err = got_error_from_errno("getline");
3198 goto done;
3201 dirpath = strdup(path);
3202 if (dirpath == NULL) {
3203 err = got_error_from_errno("strdup");
3204 goto done;
3206 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3207 done:
3208 free(line);
3209 if (err || pe == NULL) {
3210 free(dirpath);
3211 free_ignorelist(ignorelist);
3213 return err;
3216 int
3217 match_ignores(struct got_pathlist_head *ignores, const char *path)
3219 struct got_pathlist_entry *pe;
3221 /* Handle patterns which match in all directories. */
3222 TAILQ_FOREACH(pe, ignores, entry) {
3223 struct got_pathlist_head *ignorelist = pe->data;
3224 struct got_pathlist_entry *pi;
3226 TAILQ_FOREACH(pi, ignorelist, entry) {
3227 const char *p, *pattern = pi->path;
3229 if (strncmp(pattern, "**/", 3) != 0)
3230 continue;
3231 pattern += 3;
3232 p = path;
3233 while (*p) {
3234 if (fnmatch(pattern, p,
3235 FNM_PATHNAME | FNM_LEADING_DIR)) {
3236 /* Retry in next directory. */
3237 while (*p && *p != '/')
3238 p++;
3239 while (*p == '/')
3240 p++;
3241 continue;
3243 return 1;
3249 * The ignores pathlist contains ignore lists from children before
3250 * parents, so we can find the most specific ignorelist by walking
3251 * ignores backwards.
3253 pe = TAILQ_LAST(ignores, got_pathlist_head);
3254 while (pe) {
3255 if (got_path_is_child(path, pe->path, pe->path_len)) {
3256 struct got_pathlist_head *ignorelist = pe->data;
3257 struct got_pathlist_entry *pi;
3258 TAILQ_FOREACH(pi, ignorelist, entry) {
3259 const char *pattern = pi->path;
3260 int flags = FNM_LEADING_DIR;
3261 if (strstr(pattern, "/**/") == NULL)
3262 flags |= FNM_PATHNAME;
3263 if (fnmatch(pattern, path, flags))
3264 continue;
3265 return 1;
3268 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3271 return 0;
3274 static const struct got_error *
3275 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3276 const char *path, int dirfd, const char *ignores_filename)
3278 const struct got_error *err = NULL;
3279 char *ignorespath;
3280 int fd = -1;
3281 FILE *ignoresfile = NULL;
3283 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3284 path[0] ? "/" : "", ignores_filename) == -1)
3285 return got_error_from_errno("asprintf");
3287 if (dirfd != -1) {
3288 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3289 if (fd == -1) {
3290 if (errno != ENOENT && errno != EACCES)
3291 err = got_error_from_errno2("openat",
3292 ignorespath);
3293 } else {
3294 ignoresfile = fdopen(fd, "r");
3295 if (ignoresfile == NULL)
3296 err = got_error_from_errno2("fdopen",
3297 ignorespath);
3298 else {
3299 fd = -1;
3300 err = read_ignores(ignores, path, ignoresfile);
3303 } else {
3304 ignoresfile = fopen(ignorespath, "r");
3305 if (ignoresfile == NULL) {
3306 if (errno != ENOENT && errno != EACCES)
3307 err = got_error_from_errno2("fopen",
3308 ignorespath);
3309 } else
3310 err = read_ignores(ignores, path, ignoresfile);
3313 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3314 err = got_error_from_errno2("fclose", path);
3315 if (fd != -1 && close(fd) == -1 && err == NULL)
3316 err = got_error_from_errno2("close", path);
3317 free(ignorespath);
3318 return err;
3321 static const struct got_error *
3322 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3324 const struct got_error *err = NULL;
3325 struct diff_dir_cb_arg *a = arg;
3326 char *path = NULL;
3328 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3329 return got_error(GOT_ERR_CANCELLED);
3331 if (parent_path[0]) {
3332 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3333 return got_error_from_errno("asprintf");
3334 } else {
3335 path = de->d_name;
3338 if (de->d_type != DT_DIR &&
3339 got_path_is_child(path, a->status_path, a->status_path_len)
3340 && !match_ignores(&a->ignores, path))
3341 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3342 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3343 if (parent_path[0])
3344 free(path);
3345 return err;
3348 static const struct got_error *
3349 status_traverse(void *arg, const char *path, int dirfd)
3351 const struct got_error *err = NULL;
3352 struct diff_dir_cb_arg *a = arg;
3354 if (a->no_ignores)
3355 return NULL;
3357 err = add_ignores(&a->ignores, a->worktree->root_path,
3358 path, dirfd, ".cvsignore");
3359 if (err)
3360 return err;
3362 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3363 dirfd, ".gitignore");
3365 return err;
3368 static const struct got_error *
3369 report_single_file_status(const char *path, const char *ondisk_path,
3370 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3371 void *status_arg, struct got_repository *repo, int report_unchanged)
3373 struct got_fileindex_entry *ie;
3374 struct stat sb;
3376 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3377 if (ie)
3378 return report_file_status(ie, ondisk_path, -1, NULL,
3379 status_cb, status_arg, repo, report_unchanged);
3381 if (lstat(ondisk_path, &sb) == -1) {
3382 if (errno != ENOENT)
3383 return got_error_from_errno2("lstat", ondisk_path);
3384 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3385 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3386 return NULL;
3389 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3390 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3391 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3393 return NULL;
3396 static const struct got_error *
3397 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3398 const char *root_path, const char *path)
3400 const struct got_error *err;
3401 char *parent_path, *next_parent_path = NULL;
3403 err = add_ignores(ignores, root_path, "", -1,
3404 ".cvsignore");
3405 if (err)
3406 return err;
3408 err = add_ignores(ignores, root_path, "", -1,
3409 ".gitignore");
3410 if (err)
3411 return err;
3413 err = got_path_dirname(&parent_path, path);
3414 if (err) {
3415 if (err->code == GOT_ERR_BAD_PATH)
3416 return NULL; /* cannot traverse parent */
3417 return err;
3419 for (;;) {
3420 err = add_ignores(ignores, root_path, parent_path, -1,
3421 ".cvsignore");
3422 if (err)
3423 break;
3424 err = add_ignores(ignores, root_path, parent_path, -1,
3425 ".gitignore");
3426 if (err)
3427 break;
3428 err = got_path_dirname(&next_parent_path, parent_path);
3429 if (err) {
3430 if (err->code == GOT_ERR_BAD_PATH)
3431 err = NULL; /* traversed everything */
3432 break;
3434 free(parent_path);
3435 parent_path = next_parent_path;
3436 next_parent_path = NULL;
3439 free(parent_path);
3440 free(next_parent_path);
3441 return err;
3444 static const struct got_error *
3445 worktree_status(struct got_worktree *worktree, const char *path,
3446 struct got_fileindex *fileindex, struct got_repository *repo,
3447 got_worktree_status_cb status_cb, void *status_arg,
3448 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3449 int report_unchanged)
3451 const struct got_error *err = NULL;
3452 int fd = -1;
3453 struct got_fileindex_diff_dir_cb fdiff_cb;
3454 struct diff_dir_cb_arg arg;
3455 char *ondisk_path = NULL;
3457 TAILQ_INIT(&arg.ignores);
3459 if (asprintf(&ondisk_path, "%s%s%s",
3460 worktree->root_path, path[0] ? "/" : "", path) == -1)
3461 return got_error_from_errno("asprintf");
3463 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3464 if (fd == -1) {
3465 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3466 errno != ELOOP)
3467 err = got_error_from_errno2("open", ondisk_path);
3468 else
3469 err = report_single_file_status(path, ondisk_path,
3470 fileindex, status_cb, status_arg, repo,
3471 report_unchanged);
3472 } else {
3473 fdiff_cb.diff_old_new = status_old_new;
3474 fdiff_cb.diff_old = status_old;
3475 fdiff_cb.diff_new = status_new;
3476 fdiff_cb.diff_traverse = status_traverse;
3477 arg.fileindex = fileindex;
3478 arg.worktree = worktree;
3479 arg.status_path = path;
3480 arg.status_path_len = strlen(path);
3481 arg.repo = repo;
3482 arg.status_cb = status_cb;
3483 arg.status_arg = status_arg;
3484 arg.cancel_cb = cancel_cb;
3485 arg.cancel_arg = cancel_arg;
3486 arg.report_unchanged = report_unchanged;
3487 arg.no_ignores = no_ignores;
3488 if (!no_ignores) {
3489 err = add_ignores_from_parent_paths(&arg.ignores,
3490 worktree->root_path, path);
3491 if (err)
3492 goto done;
3494 err = got_fileindex_diff_dir(fileindex, fd,
3495 worktree->root_path, path, repo, &fdiff_cb, &arg);
3497 done:
3498 free_ignores(&arg.ignores);
3499 if (fd != -1 && close(fd) != 0 && err == NULL)
3500 err = got_error_from_errno("close");
3501 free(ondisk_path);
3502 return err;
3505 const struct got_error *
3506 got_worktree_status(struct got_worktree *worktree,
3507 struct got_pathlist_head *paths, struct got_repository *repo,
3508 got_worktree_status_cb status_cb, void *status_arg,
3509 got_cancel_cb cancel_cb, void *cancel_arg)
3511 const struct got_error *err = NULL;
3512 char *fileindex_path = NULL;
3513 struct got_fileindex *fileindex = NULL;
3514 struct got_pathlist_entry *pe;
3516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3517 if (err)
3518 return err;
3520 TAILQ_FOREACH(pe, paths, entry) {
3521 err = worktree_status(worktree, pe->path, fileindex, repo,
3522 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3523 if (err)
3524 break;
3526 free(fileindex_path);
3527 got_fileindex_free(fileindex);
3528 return err;
3531 const struct got_error *
3532 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3533 const char *arg)
3535 const struct got_error *err = NULL;
3536 char *resolved = NULL, *cwd = NULL, *path = NULL;
3537 size_t len;
3538 struct stat sb;
3540 *wt_path = NULL;
3542 cwd = getcwd(NULL, 0);
3543 if (cwd == NULL)
3544 return got_error_from_errno("getcwd");
3546 if (lstat(arg, &sb) == -1) {
3547 if (errno != ENOENT) {
3548 err = got_error_from_errno2("lstat", arg);
3549 goto done;
3552 if (S_ISLNK(sb.st_mode)) {
3554 * We cannot use realpath(3) with symlinks since we want to
3555 * operate on the symlink itself.
3556 * But we can make the path absolute, assuming it is relative
3557 * to the current working directory, and then canonicalize it.
3559 char *abspath = NULL;
3560 char canonpath[PATH_MAX];
3561 if (!got_path_is_absolute(arg)) {
3562 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3563 err = got_error_from_errno("asprintf");
3564 goto done;
3568 err = got_canonpath(abspath ? abspath : arg, canonpath,
3569 sizeof(canonpath));
3570 if (err)
3571 goto done;
3572 resolved = strdup(canonpath);
3573 if (resolved == NULL) {
3574 err = got_error_from_errno("strdup");
3575 goto done;
3577 } else {
3578 resolved = realpath(arg, NULL);
3579 if (resolved == NULL) {
3580 if (errno != ENOENT) {
3581 err = got_error_from_errno2("realpath", arg);
3582 goto done;
3584 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3585 err = got_error_from_errno("asprintf");
3586 goto done;
3591 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3592 strlen(got_worktree_get_root_path(worktree)))) {
3593 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3594 goto done;
3597 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3598 err = got_path_skip_common_ancestor(&path,
3599 got_worktree_get_root_path(worktree), resolved);
3600 if (err)
3601 goto done;
3602 } else {
3603 path = strdup("");
3604 if (path == NULL) {
3605 err = got_error_from_errno("strdup");
3606 goto done;
3610 /* XXX status walk can't deal with trailing slash! */
3611 len = strlen(path);
3612 while (len > 0 && path[len - 1] == '/') {
3613 path[len - 1] = '\0';
3614 len--;
3616 done:
3617 free(resolved);
3618 free(cwd);
3619 if (err == NULL)
3620 *wt_path = path;
3621 else
3622 free(path);
3623 return err;
3626 struct schedule_addition_args {
3627 struct got_worktree *worktree;
3628 struct got_fileindex *fileindex;
3629 got_worktree_checkout_cb progress_cb;
3630 void *progress_arg;
3631 struct got_repository *repo;
3634 static const struct got_error *
3635 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3636 const char *relpath, struct got_object_id *blob_id,
3637 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3638 int dirfd, const char *de_name)
3640 struct schedule_addition_args *a = arg;
3641 const struct got_error *err = NULL;
3642 struct got_fileindex_entry *ie;
3643 struct stat sb;
3644 char *ondisk_path;
3646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3647 relpath) == -1)
3648 return got_error_from_errno("asprintf");
3650 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3651 if (ie) {
3652 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3653 de_name, a->repo);
3654 if (err)
3655 goto done;
3656 /* Re-adding an existing entry is a no-op. */
3657 if (status == GOT_STATUS_ADD)
3658 goto done;
3659 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3660 if (err)
3661 goto done;
3664 if (status != GOT_STATUS_UNVERSIONED) {
3665 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3666 goto done;
3669 err = got_fileindex_entry_alloc(&ie, relpath);
3670 if (err)
3671 goto done;
3672 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3673 if (err) {
3674 got_fileindex_entry_free(ie);
3675 goto done;
3677 err = got_fileindex_entry_add(a->fileindex, ie);
3678 if (err) {
3679 got_fileindex_entry_free(ie);
3680 goto done;
3682 done:
3683 free(ondisk_path);
3684 if (err)
3685 return err;
3686 if (status == GOT_STATUS_ADD)
3687 return NULL;
3688 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3691 const struct got_error *
3692 got_worktree_schedule_add(struct got_worktree *worktree,
3693 struct got_pathlist_head *paths,
3694 got_worktree_checkout_cb progress_cb, void *progress_arg,
3695 struct got_repository *repo, int no_ignores)
3697 struct got_fileindex *fileindex = NULL;
3698 char *fileindex_path = NULL;
3699 const struct got_error *err = NULL, *sync_err, *unlockerr;
3700 struct got_pathlist_entry *pe;
3701 struct schedule_addition_args saa;
3703 err = lock_worktree(worktree, LOCK_EX);
3704 if (err)
3705 return err;
3707 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3708 if (err)
3709 goto done;
3711 saa.worktree = worktree;
3712 saa.fileindex = fileindex;
3713 saa.progress_cb = progress_cb;
3714 saa.progress_arg = progress_arg;
3715 saa.repo = repo;
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3720 if (err)
3721 break;
3723 sync_err = sync_fileindex(fileindex, fileindex_path);
3724 if (sync_err && err == NULL)
3725 err = sync_err;
3726 done:
3727 free(fileindex_path);
3728 if (fileindex)
3729 got_fileindex_free(fileindex);
3730 unlockerr = lock_worktree(worktree, LOCK_SH);
3731 if (unlockerr && err == NULL)
3732 err = unlockerr;
3733 return err;
3736 struct schedule_deletion_args {
3737 struct got_worktree *worktree;
3738 struct got_fileindex *fileindex;
3739 got_worktree_delete_cb progress_cb;
3740 void *progress_arg;
3741 struct got_repository *repo;
3742 int delete_local_mods;
3743 int keep_on_disk;
3746 static const struct got_error *
3747 schedule_for_deletion(void *arg, unsigned char status,
3748 unsigned char staged_status, const char *relpath,
3749 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3750 struct got_object_id *commit_id, int dirfd, const char *de_name)
3752 struct schedule_deletion_args *a = arg;
3753 const struct got_error *err = NULL;
3754 struct got_fileindex_entry *ie = NULL;
3755 struct stat sb;
3756 char *ondisk_path, *parent = NULL;
3758 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3759 if (ie == NULL)
3760 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3762 staged_status = get_staged_status(ie);
3763 if (staged_status != GOT_STATUS_NO_CHANGE) {
3764 if (staged_status == GOT_STATUS_DELETE)
3765 return NULL;
3766 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3769 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3770 relpath) == -1)
3771 return got_error_from_errno("asprintf");
3773 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3774 a->repo);
3775 if (err)
3776 goto done;
3778 if (status != GOT_STATUS_NO_CHANGE) {
3779 if (status == GOT_STATUS_DELETE)
3780 goto done;
3781 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3782 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3783 goto done;
3785 if (status != GOT_STATUS_MODIFY &&
3786 status != GOT_STATUS_MISSING) {
3787 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3788 goto done;
3792 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3793 if (dirfd != -1) {
3794 if (unlinkat(dirfd, de_name, 0) != 0) {
3795 err = got_error_from_errno2("unlinkat",
3796 ondisk_path);
3797 goto done;
3799 } else if (unlink(ondisk_path) != 0) {
3800 err = got_error_from_errno2("unlink", ondisk_path);
3801 goto done;
3804 parent = dirname(ondisk_path);
3806 if (parent == NULL) {
3807 err = got_error_from_errno2("dirname", ondisk_path);
3808 goto done;
3810 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3811 if (rmdir(parent) == -1) {
3812 if (errno != ENOTEMPTY)
3813 err = got_error_from_errno2("rmdir",
3814 parent);
3815 break;
3817 parent = dirname(parent);
3818 if (parent == NULL) {
3819 err = got_error_from_errno2("dirname", parent);
3820 goto done;
3825 got_fileindex_entry_mark_deleted_from_disk(ie);
3826 done:
3827 free(ondisk_path);
3828 if (err)
3829 return err;
3830 if (status == GOT_STATUS_DELETE)
3831 return NULL;
3832 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3833 staged_status, relpath);
3836 const struct got_error *
3837 got_worktree_schedule_delete(struct got_worktree *worktree,
3838 struct got_pathlist_head *paths, int delete_local_mods,
3839 got_worktree_delete_cb progress_cb, void *progress_arg,
3840 struct got_repository *repo, int keep_on_disk)
3842 struct got_fileindex *fileindex = NULL;
3843 char *fileindex_path = NULL;
3844 const struct got_error *err = NULL, *sync_err, *unlockerr;
3845 struct got_pathlist_entry *pe;
3846 struct schedule_deletion_args sda;
3848 err = lock_worktree(worktree, LOCK_EX);
3849 if (err)
3850 return err;
3852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3853 if (err)
3854 goto done;
3856 sda.worktree = worktree;
3857 sda.fileindex = fileindex;
3858 sda.progress_cb = progress_cb;
3859 sda.progress_arg = progress_arg;
3860 sda.repo = repo;
3861 sda.delete_local_mods = delete_local_mods;
3862 sda.keep_on_disk = keep_on_disk;
3864 TAILQ_FOREACH(pe, paths, entry) {
3865 err = worktree_status(worktree, pe->path, fileindex, repo,
3866 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3867 if (err)
3868 break;
3870 sync_err = sync_fileindex(fileindex, fileindex_path);
3871 if (sync_err && err == NULL)
3872 err = sync_err;
3873 done:
3874 free(fileindex_path);
3875 if (fileindex)
3876 got_fileindex_free(fileindex);
3877 unlockerr = lock_worktree(worktree, LOCK_SH);
3878 if (unlockerr && err == NULL)
3879 err = unlockerr;
3880 return err;
3883 static const struct got_error *
3884 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3886 const struct got_error *err = NULL;
3887 char *line = NULL;
3888 size_t linesize = 0, n;
3889 ssize_t linelen;
3891 linelen = getline(&line, &linesize, infile);
3892 if (linelen == -1) {
3893 if (ferror(infile)) {
3894 err = got_error_from_errno("getline");
3895 goto done;
3897 return NULL;
3899 if (outfile) {
3900 n = fwrite(line, 1, linelen, outfile);
3901 if (n != linelen) {
3902 err = got_ferror(outfile, GOT_ERR_IO);
3903 goto done;
3906 if (rejectfile) {
3907 n = fwrite(line, 1, linelen, rejectfile);
3908 if (n != linelen)
3909 err = got_ferror(outfile, GOT_ERR_IO);
3911 done:
3912 free(line);
3913 return err;
3916 static const struct got_error *
3917 skip_one_line(FILE *f)
3919 char *line = NULL;
3920 size_t linesize = 0;
3921 ssize_t linelen;
3923 linelen = getline(&line, &linesize, f);
3924 if (linelen == -1) {
3925 if (ferror(f))
3926 return got_error_from_errno("getline");
3927 return NULL;
3929 free(line);
3930 return NULL;
3933 static const struct got_error *
3934 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3935 int start_old, int end_old, int start_new, int end_new,
3936 FILE *outfile, FILE *rejectfile)
3938 const struct got_error *err;
3940 /* Copy old file's lines leading up to patch. */
3941 while (!feof(f1) && *line_cur1 < start_old) {
3942 err = copy_one_line(f1, outfile, NULL);
3943 if (err)
3944 return err;
3945 (*line_cur1)++;
3947 /* Skip new file's lines leading up to patch. */
3948 while (!feof(f2) && *line_cur2 < start_new) {
3949 if (rejectfile)
3950 err = copy_one_line(f2, NULL, rejectfile);
3951 else
3952 err = skip_one_line(f2);
3953 if (err)
3954 return err;
3955 (*line_cur2)++;
3957 /* Copy patched lines. */
3958 while (!feof(f2) && *line_cur2 <= end_new) {
3959 err = copy_one_line(f2, outfile, NULL);
3960 if (err)
3961 return err;
3962 (*line_cur2)++;
3964 /* Skip over old file's replaced lines. */
3965 while (!feof(f1) && *line_cur1 <= end_old) {
3966 if (rejectfile)
3967 err = copy_one_line(f1, NULL, rejectfile);
3968 else
3969 err = skip_one_line(f1);
3970 if (err)
3971 return err;
3972 (*line_cur1)++;
3975 return NULL;
3978 static const struct got_error *
3979 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3980 FILE *outfile, FILE *rejectfile)
3982 const struct got_error *err;
3984 if (outfile) {
3985 /* Copy old file's lines until EOF. */
3986 while (!feof(f1)) {
3987 err = copy_one_line(f1, outfile, NULL);
3988 if (err)
3989 return err;
3990 (*line_cur1)++;
3993 if (rejectfile) {
3994 /* Copy new file's lines until EOF. */
3995 while (!feof(f2)) {
3996 err = copy_one_line(f2, NULL, rejectfile);
3997 if (err)
3998 return err;
3999 (*line_cur2)++;
4003 return NULL;
4006 static const struct got_error *
4007 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4008 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4009 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4010 int *line_cur2, FILE *outfile, FILE *rejectfile,
4011 got_worktree_patch_cb patch_cb, void *patch_arg)
4013 const struct got_error *err = NULL;
4014 int start_old = change->cv.a;
4015 int end_old = change->cv.b;
4016 int start_new = change->cv.c;
4017 int end_new = change->cv.d;
4018 long pos1, pos2;
4019 FILE *hunkfile;
4021 *choice = GOT_PATCH_CHOICE_NONE;
4023 hunkfile = got_opentemp();
4024 if (hunkfile == NULL)
4025 return got_error_from_errno("got_opentemp");
4027 pos1 = ftell(f1);
4028 pos2 = ftell(f2);
4030 /* XXX TODO needs error checking */
4031 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4033 if (fseek(f1, pos1, SEEK_SET) == -1) {
4034 err = got_ferror(f1, GOT_ERR_IO);
4035 goto done;
4037 if (fseek(f2, pos2, SEEK_SET) == -1) {
4038 err = got_ferror(f1, GOT_ERR_IO);
4039 goto done;
4041 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4042 err = got_ferror(hunkfile, GOT_ERR_IO);
4043 goto done;
4046 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4047 hunkfile, n, nchanges);
4048 if (err)
4049 goto done;
4051 switch (*choice) {
4052 case GOT_PATCH_CHOICE_YES:
4053 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4054 end_old, start_new, end_new, outfile, rejectfile);
4055 break;
4056 case GOT_PATCH_CHOICE_NO:
4057 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4058 end_old, start_new, end_new, rejectfile, outfile);
4059 break;
4060 case GOT_PATCH_CHOICE_QUIT:
4061 break;
4062 default:
4063 err = got_error(GOT_ERR_PATCH_CHOICE);
4064 break;
4066 done:
4067 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4068 err = got_error_from_errno("fclose");
4069 return err;
4072 struct revert_file_args {
4073 struct got_worktree *worktree;
4074 struct got_fileindex *fileindex;
4075 got_worktree_checkout_cb progress_cb;
4076 void *progress_arg;
4077 got_worktree_patch_cb patch_cb;
4078 void *patch_arg;
4079 struct got_repository *repo;
4082 static const struct got_error *
4083 create_patched_content(char **path_outfile, int reverse_patch,
4084 struct got_object_id *blob_id, const char *path2,
4085 int dirfd2, const char *de_name2,
4086 const char *relpath, struct got_repository *repo,
4087 got_worktree_patch_cb patch_cb, void *patch_arg)
4089 const struct got_error *err;
4090 struct got_blob_object *blob = NULL;
4091 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4092 int fd2 = -1;
4093 char link_target[PATH_MAX];
4094 ssize_t link_len = 0;
4095 char *path1 = NULL, *id_str = NULL;
4096 struct stat sb1, sb2;
4097 struct got_diff_changes *changes = NULL;
4098 struct got_diff_state *ds = NULL;
4099 struct got_diff_args *args = NULL;
4100 struct got_diff_change *change;
4101 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4102 int n = 0;
4104 *path_outfile = NULL;
4106 err = got_object_id_str(&id_str, blob_id);
4107 if (err)
4108 return err;
4110 if (dirfd2 != -1) {
4111 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4112 if (fd2 == -1) {
4113 if (errno != ELOOP) {
4114 err = got_error_from_errno2("openat", path2);
4115 goto done;
4117 link_len = readlinkat(dirfd2, de_name2,
4118 link_target, sizeof(link_target));
4119 if (link_len == -1)
4120 return got_error_from_errno2("readlinkat", path2);
4121 sb2.st_mode = S_IFLNK;
4122 sb2.st_size = link_len;
4124 } else {
4125 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4126 if (fd2 == -1) {
4127 if (errno != ELOOP) {
4128 err = got_error_from_errno2("open", path2);
4129 goto done;
4131 link_len = readlink(path2, link_target,
4132 sizeof(link_target));
4133 if (link_len == -1)
4134 return got_error_from_errno2("readlink", path2);
4135 sb2.st_mode = S_IFLNK;
4136 sb2.st_size = link_len;
4139 if (fd2 != -1) {
4140 if (fstat(fd2, &sb2) == -1) {
4141 err = got_error_from_errno2("fstat", path2);
4142 goto done;
4145 f2 = fdopen(fd2, "r");
4146 if (f2 == NULL) {
4147 err = got_error_from_errno2("fdopen", path2);
4148 goto done;
4150 fd2 = -1;
4151 } else {
4152 size_t n;
4153 f2 = got_opentemp();
4154 if (f2 == NULL) {
4155 err = got_error_from_errno2("got_opentemp", path2);
4156 goto done;
4158 n = fwrite(link_target, 1, link_len, f2);
4159 if (n != link_len) {
4160 err = got_ferror(f2, GOT_ERR_IO);
4161 goto done;
4163 if (fflush(f2) == EOF) {
4164 err = got_error_from_errno("fflush");
4165 goto done;
4167 rewind(f2);
4170 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4171 if (err)
4172 goto done;
4174 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4175 if (err)
4176 goto done;
4178 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4179 if (err)
4180 goto done;
4182 if (stat(path1, &sb1) == -1) {
4183 err = got_error_from_errno2("stat", path1);
4184 goto done;
4187 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4188 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4189 if (err)
4190 goto done;
4192 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4193 if (err)
4194 goto done;
4196 if (fseek(f1, 0L, SEEK_SET) == -1)
4197 return got_ferror(f1, GOT_ERR_IO);
4198 if (fseek(f2, 0L, SEEK_SET) == -1)
4199 return got_ferror(f2, GOT_ERR_IO);
4200 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4201 int choice;
4202 err = apply_or_reject_change(&choice, change, ++n,
4203 changes->nchanges, ds, args, diff_flags, relpath,
4204 f1, f2, &line_cur1, &line_cur2,
4205 reverse_patch ? NULL : outfile,
4206 reverse_patch ? outfile : NULL,
4207 patch_cb, patch_arg);
4208 if (err)
4209 goto done;
4210 if (choice == GOT_PATCH_CHOICE_YES)
4211 have_content = 1;
4212 else if (choice == GOT_PATCH_CHOICE_QUIT)
4213 break;
4215 if (have_content) {
4216 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4217 reverse_patch ? NULL : outfile,
4218 reverse_patch ? outfile : NULL);
4219 if (err)
4220 goto done;
4222 if (!S_ISLNK(sb2.st_mode)) {
4223 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4224 err = got_error_from_errno2("chmod", path2);
4225 goto done;
4229 done:
4230 free(id_str);
4231 if (blob)
4232 got_object_blob_close(blob);
4233 if (f1 && fclose(f1) == EOF && err == NULL)
4234 err = got_error_from_errno2("fclose", path1);
4235 if (f2 && fclose(f2) == EOF && err == NULL)
4236 err = got_error_from_errno2("fclose", path2);
4237 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4238 err = got_error_from_errno2("close", path2);
4239 if (outfile && fclose(outfile) == EOF && err == NULL)
4240 err = got_error_from_errno2("fclose", *path_outfile);
4241 if (path1 && unlink(path1) == -1 && err == NULL)
4242 err = got_error_from_errno2("unlink", path1);
4243 if (err || !have_content) {
4244 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4245 err = got_error_from_errno2("unlink", *path_outfile);
4246 free(*path_outfile);
4247 *path_outfile = NULL;
4249 free(args);
4250 if (ds) {
4251 got_diff_state_free(ds);
4252 free(ds);
4254 if (changes)
4255 got_diff_free_changes(changes);
4256 free(path1);
4257 return err;
4260 static const struct got_error *
4261 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4262 const char *relpath, struct got_object_id *blob_id,
4263 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4264 int dirfd, const char *de_name)
4266 struct revert_file_args *a = arg;
4267 const struct got_error *err = NULL;
4268 char *parent_path = NULL;
4269 struct got_fileindex_entry *ie;
4270 struct got_tree_object *tree = NULL;
4271 struct got_object_id *tree_id = NULL;
4272 const struct got_tree_entry *te = NULL;
4273 char *tree_path = NULL, *te_name;
4274 char *ondisk_path = NULL, *path_content = NULL;
4275 struct got_blob_object *blob = NULL;
4277 /* Reverting a staged deletion is a no-op. */
4278 if (status == GOT_STATUS_DELETE &&
4279 staged_status != GOT_STATUS_NO_CHANGE)
4280 return NULL;
4282 if (status == GOT_STATUS_UNVERSIONED)
4283 return (*a->progress_cb)(a->progress_arg,
4284 GOT_STATUS_UNVERSIONED, relpath);
4286 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4287 if (ie == NULL)
4288 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4290 /* Construct in-repository path of tree which contains this blob. */
4291 err = got_path_dirname(&parent_path, ie->path);
4292 if (err) {
4293 if (err->code != GOT_ERR_BAD_PATH)
4294 goto done;
4295 parent_path = strdup("/");
4296 if (parent_path == NULL) {
4297 err = got_error_from_errno("strdup");
4298 goto done;
4301 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4302 tree_path = strdup(parent_path);
4303 if (tree_path == NULL) {
4304 err = got_error_from_errno("strdup");
4305 goto done;
4307 } else {
4308 if (got_path_is_root_dir(parent_path)) {
4309 tree_path = strdup(a->worktree->path_prefix);
4310 if (tree_path == NULL) {
4311 err = got_error_from_errno("strdup");
4312 goto done;
4314 } else {
4315 if (asprintf(&tree_path, "%s/%s",
4316 a->worktree->path_prefix, parent_path) == -1) {
4317 err = got_error_from_errno("asprintf");
4318 goto done;
4323 err = got_object_id_by_path(&tree_id, a->repo,
4324 a->worktree->base_commit_id, tree_path);
4325 if (err) {
4326 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4327 (status == GOT_STATUS_ADD ||
4328 staged_status == GOT_STATUS_ADD)))
4329 goto done;
4330 } else {
4331 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4332 if (err)
4333 goto done;
4335 te_name = basename(ie->path);
4336 if (te_name == NULL) {
4337 err = got_error_from_errno2("basename", ie->path);
4338 goto done;
4341 te = got_object_tree_find_entry(tree, te_name);
4342 if (te == NULL && status != GOT_STATUS_ADD &&
4343 staged_status != GOT_STATUS_ADD) {
4344 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4345 goto done;
4349 switch (status) {
4350 case GOT_STATUS_ADD:
4351 if (a->patch_cb) {
4352 int choice = GOT_PATCH_CHOICE_NONE;
4353 err = (*a->patch_cb)(&choice, a->patch_arg,
4354 status, ie->path, NULL, 1, 1);
4355 if (err)
4356 goto done;
4357 if (choice != GOT_PATCH_CHOICE_YES)
4358 break;
4360 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4361 ie->path);
4362 if (err)
4363 goto done;
4364 got_fileindex_entry_remove(a->fileindex, ie);
4365 break;
4366 case GOT_STATUS_DELETE:
4367 if (a->patch_cb) {
4368 int choice = GOT_PATCH_CHOICE_NONE;
4369 err = (*a->patch_cb)(&choice, a->patch_arg,
4370 status, ie->path, NULL, 1, 1);
4371 if (err)
4372 goto done;
4373 if (choice != GOT_PATCH_CHOICE_YES)
4374 break;
4376 /* fall through */
4377 case GOT_STATUS_MODIFY:
4378 case GOT_STATUS_MODE_CHANGE:
4379 case GOT_STATUS_CONFLICT:
4380 case GOT_STATUS_MISSING: {
4381 struct got_object_id id;
4382 if (staged_status == GOT_STATUS_ADD ||
4383 staged_status == GOT_STATUS_MODIFY) {
4384 memcpy(id.sha1, ie->staged_blob_sha1,
4385 SHA1_DIGEST_LENGTH);
4386 } else
4387 memcpy(id.sha1, ie->blob_sha1,
4388 SHA1_DIGEST_LENGTH);
4389 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4390 if (err)
4391 goto done;
4393 if (asprintf(&ondisk_path, "%s/%s",
4394 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4395 err = got_error_from_errno("asprintf");
4396 goto done;
4399 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4400 status == GOT_STATUS_CONFLICT)) {
4401 int is_bad_symlink = 0;
4402 err = create_patched_content(&path_content, 1, &id,
4403 ondisk_path, dirfd, de_name, ie->path, a->repo,
4404 a->patch_cb, a->patch_arg);
4405 if (err || path_content == NULL)
4406 break;
4407 if (te && S_ISLNK(te->mode)) {
4408 if (unlink(path_content) == -1) {
4409 err = got_error_from_errno2("unlink",
4410 path_content);
4411 break;
4413 err = install_symlink(&is_bad_symlink,
4414 a->worktree, ondisk_path, ie->path,
4415 blob, 0, 1, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4417 } else {
4418 if (rename(path_content, ondisk_path) == -1) {
4419 err = got_error_from_errno3("rename",
4420 path_content, ondisk_path);
4421 goto done;
4424 } else {
4425 int is_bad_symlink = 0;
4426 if (te && S_ISLNK(te->mode)) {
4427 err = install_symlink(&is_bad_symlink,
4428 a->worktree, ondisk_path, ie->path,
4429 blob, 0, 1, 0, a->repo,
4430 a->progress_cb, a->progress_arg);
4431 } else {
4432 err = install_blob(a->worktree, ondisk_path,
4433 ie->path,
4434 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4435 got_fileindex_perms_to_st(ie), blob,
4436 0, 1, 0, 0, a->repo,
4437 a->progress_cb, a->progress_arg);
4439 if (err)
4440 goto done;
4441 if (status == GOT_STATUS_DELETE ||
4442 status == GOT_STATUS_MODE_CHANGE) {
4443 err = got_fileindex_entry_update(ie,
4444 ondisk_path, blob->id.sha1,
4445 a->worktree->base_commit_id->sha1, 1);
4446 if (err)
4447 goto done;
4449 if (is_bad_symlink) {
4450 got_fileindex_entry_filetype_set(ie,
4451 GOT_FILEIDX_MODE_BAD_SYMLINK);
4454 break;
4456 default:
4457 break;
4459 done:
4460 free(ondisk_path);
4461 free(path_content);
4462 free(parent_path);
4463 free(tree_path);
4464 if (blob)
4465 got_object_blob_close(blob);
4466 if (tree)
4467 got_object_tree_close(tree);
4468 free(tree_id);
4469 return err;
4472 const struct got_error *
4473 got_worktree_revert(struct got_worktree *worktree,
4474 struct got_pathlist_head *paths,
4475 got_worktree_checkout_cb progress_cb, void *progress_arg,
4476 got_worktree_patch_cb patch_cb, void *patch_arg,
4477 struct got_repository *repo)
4479 struct got_fileindex *fileindex = NULL;
4480 char *fileindex_path = NULL;
4481 const struct got_error *err = NULL, *unlockerr = NULL;
4482 const struct got_error *sync_err = NULL;
4483 struct got_pathlist_entry *pe;
4484 struct revert_file_args rfa;
4486 err = lock_worktree(worktree, LOCK_EX);
4487 if (err)
4488 return err;
4490 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4491 if (err)
4492 goto done;
4494 rfa.worktree = worktree;
4495 rfa.fileindex = fileindex;
4496 rfa.progress_cb = progress_cb;
4497 rfa.progress_arg = progress_arg;
4498 rfa.patch_cb = patch_cb;
4499 rfa.patch_arg = patch_arg;
4500 rfa.repo = repo;
4501 TAILQ_FOREACH(pe, paths, entry) {
4502 err = worktree_status(worktree, pe->path, fileindex, repo,
4503 revert_file, &rfa, NULL, NULL, 0, 0);
4504 if (err)
4505 break;
4507 sync_err = sync_fileindex(fileindex, fileindex_path);
4508 if (sync_err && err == NULL)
4509 err = sync_err;
4510 done:
4511 free(fileindex_path);
4512 if (fileindex)
4513 got_fileindex_free(fileindex);
4514 unlockerr = lock_worktree(worktree, LOCK_SH);
4515 if (unlockerr && err == NULL)
4516 err = unlockerr;
4517 return err;
4520 static void
4521 free_commitable(struct got_commitable *ct)
4523 free(ct->path);
4524 free(ct->in_repo_path);
4525 free(ct->ondisk_path);
4526 free(ct->blob_id);
4527 free(ct->base_blob_id);
4528 free(ct->staged_blob_id);
4529 free(ct->base_commit_id);
4530 free(ct);
4533 struct collect_commitables_arg {
4534 struct got_pathlist_head *commitable_paths;
4535 struct got_repository *repo;
4536 struct got_worktree *worktree;
4537 struct got_fileindex *fileindex;
4538 int have_staged_files;
4539 int allow_bad_symlinks;
4542 static const struct got_error *
4543 collect_commitables(void *arg, unsigned char status,
4544 unsigned char staged_status, const char *relpath,
4545 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4546 struct got_object_id *commit_id, int dirfd, const char *de_name)
4548 struct collect_commitables_arg *a = arg;
4549 const struct got_error *err = NULL;
4550 struct got_commitable *ct = NULL;
4551 struct got_pathlist_entry *new = NULL;
4552 char *parent_path = NULL, *path = NULL;
4553 struct stat sb;
4555 if (a->have_staged_files) {
4556 if (staged_status != GOT_STATUS_MODIFY &&
4557 staged_status != GOT_STATUS_ADD &&
4558 staged_status != GOT_STATUS_DELETE)
4559 return NULL;
4560 } else {
4561 if (status == GOT_STATUS_CONFLICT)
4562 return got_error(GOT_ERR_COMMIT_CONFLICT);
4564 if (status != GOT_STATUS_MODIFY &&
4565 status != GOT_STATUS_MODE_CHANGE &&
4566 status != GOT_STATUS_ADD &&
4567 status != GOT_STATUS_DELETE)
4568 return NULL;
4571 if (asprintf(&path, "/%s", relpath) == -1) {
4572 err = got_error_from_errno("asprintf");
4573 goto done;
4575 if (strcmp(path, "/") == 0) {
4576 parent_path = strdup("");
4577 if (parent_path == NULL)
4578 return got_error_from_errno("strdup");
4579 } else {
4580 err = got_path_dirname(&parent_path, path);
4581 if (err)
4582 return err;
4585 ct = calloc(1, sizeof(*ct));
4586 if (ct == NULL) {
4587 err = got_error_from_errno("calloc");
4588 goto done;
4591 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4592 relpath) == -1) {
4593 err = got_error_from_errno("asprintf");
4594 goto done;
4597 if (staged_status == GOT_STATUS_ADD ||
4598 staged_status == GOT_STATUS_MODIFY) {
4599 struct got_fileindex_entry *ie;
4600 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4601 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4602 case GOT_FILEIDX_MODE_REGULAR_FILE:
4603 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4604 ct->mode = S_IFREG;
4605 break;
4606 case GOT_FILEIDX_MODE_SYMLINK:
4607 ct->mode = S_IFLNK;
4608 break;
4609 default:
4610 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4611 goto done;
4613 ct->mode |= got_fileindex_entry_perms_get(ie);
4614 } else if (status != GOT_STATUS_DELETE &&
4615 staged_status != GOT_STATUS_DELETE) {
4616 if (dirfd != -1) {
4617 if (fstatat(dirfd, de_name, &sb,
4618 AT_SYMLINK_NOFOLLOW) == -1) {
4619 err = got_error_from_errno2("fstatat",
4620 ct->ondisk_path);
4621 goto done;
4623 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4624 err = got_error_from_errno2("lstat", ct->ondisk_path);
4625 goto done;
4627 ct->mode = sb.st_mode;
4630 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4631 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4632 relpath) == -1) {
4633 err = got_error_from_errno("asprintf");
4634 goto done;
4637 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4638 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4639 int is_bad_symlink;
4640 char target_path[PATH_MAX];
4641 ssize_t target_len;
4642 target_len = readlink(ct->ondisk_path, target_path,
4643 sizeof(target_path));
4644 if (target_len == -1) {
4645 err = got_error_from_errno2("readlink",
4646 ct->ondisk_path);
4647 goto done;
4649 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4650 target_len, ct->ondisk_path, a->worktree->root_path);
4651 if (err)
4652 goto done;
4653 if (is_bad_symlink) {
4654 err = got_error_path(ct->ondisk_path,
4655 GOT_ERR_BAD_SYMLINK);
4656 goto done;
4661 ct->status = status;
4662 ct->staged_status = staged_status;
4663 ct->blob_id = NULL; /* will be filled in when blob gets created */
4664 if (ct->status != GOT_STATUS_ADD &&
4665 ct->staged_status != GOT_STATUS_ADD) {
4666 ct->base_blob_id = got_object_id_dup(blob_id);
4667 if (ct->base_blob_id == NULL) {
4668 err = got_error_from_errno("got_object_id_dup");
4669 goto done;
4671 ct->base_commit_id = got_object_id_dup(commit_id);
4672 if (ct->base_commit_id == NULL) {
4673 err = got_error_from_errno("got_object_id_dup");
4674 goto done;
4677 if (ct->staged_status == GOT_STATUS_ADD ||
4678 ct->staged_status == GOT_STATUS_MODIFY) {
4679 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4680 if (ct->staged_blob_id == NULL) {
4681 err = got_error_from_errno("got_object_id_dup");
4682 goto done;
4685 ct->path = strdup(path);
4686 if (ct->path == NULL) {
4687 err = got_error_from_errno("strdup");
4688 goto done;
4690 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4691 done:
4692 if (ct && (err || new == NULL))
4693 free_commitable(ct);
4694 free(parent_path);
4695 free(path);
4696 return err;
4699 static const struct got_error *write_tree(struct got_object_id **, int *,
4700 struct got_tree_object *, const char *, struct got_pathlist_head *,
4701 got_worktree_status_cb status_cb, void *status_arg,
4702 struct got_repository *);
4704 static const struct got_error *
4705 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4706 struct got_tree_entry *te, const char *parent_path,
4707 struct got_pathlist_head *commitable_paths,
4708 got_worktree_status_cb status_cb, void *status_arg,
4709 struct got_repository *repo)
4711 const struct got_error *err = NULL;
4712 struct got_tree_object *subtree;
4713 char *subpath;
4715 if (asprintf(&subpath, "%s%s%s", parent_path,
4716 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4717 return got_error_from_errno("asprintf");
4719 err = got_object_open_as_tree(&subtree, repo, &te->id);
4720 if (err)
4721 return err;
4723 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4724 commitable_paths, status_cb, status_arg, repo);
4725 got_object_tree_close(subtree);
4726 free(subpath);
4727 return err;
4730 static const struct got_error *
4731 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4733 const struct got_error *err = NULL;
4734 char *ct_parent_path = NULL;
4736 *match = 0;
4738 if (strchr(ct->in_repo_path, '/') == NULL) {
4739 *match = got_path_is_root_dir(path);
4740 return NULL;
4743 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4744 if (err)
4745 return err;
4746 *match = (strcmp(path, ct_parent_path) == 0);
4747 free(ct_parent_path);
4748 return err;
4751 static mode_t
4752 get_ct_file_mode(struct got_commitable *ct)
4754 if (S_ISLNK(ct->mode))
4755 return S_IFLNK;
4757 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4760 static const struct got_error *
4761 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4762 struct got_tree_entry *te, struct got_commitable *ct)
4764 const struct got_error *err = NULL;
4766 *new_te = NULL;
4768 err = got_object_tree_entry_dup(new_te, te);
4769 if (err)
4770 goto done;
4772 (*new_te)->mode = get_ct_file_mode(ct);
4774 if (ct->staged_status == GOT_STATUS_MODIFY)
4775 memcpy(&(*new_te)->id, ct->staged_blob_id,
4776 sizeof((*new_te)->id));
4777 else
4778 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4779 done:
4780 if (err && *new_te) {
4781 free(*new_te);
4782 *new_te = NULL;
4784 return err;
4787 static const struct got_error *
4788 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4789 struct got_commitable *ct)
4791 const struct got_error *err = NULL;
4792 char *ct_name;
4794 *new_te = NULL;
4796 *new_te = calloc(1, sizeof(**new_te));
4797 if (*new_te == NULL)
4798 return got_error_from_errno("calloc");
4800 ct_name = basename(ct->path);
4801 if (ct_name == NULL) {
4802 err = got_error_from_errno2("basename", ct->path);
4803 goto done;
4805 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4806 sizeof((*new_te)->name)) {
4807 err = got_error(GOT_ERR_NO_SPACE);
4808 goto done;
4811 (*new_te)->mode = get_ct_file_mode(ct);
4813 if (ct->staged_status == GOT_STATUS_ADD)
4814 memcpy(&(*new_te)->id, ct->staged_blob_id,
4815 sizeof((*new_te)->id));
4816 else
4817 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4818 done:
4819 if (err && *new_te) {
4820 free(*new_te);
4821 *new_te = NULL;
4823 return err;
4826 static const struct got_error *
4827 insert_tree_entry(struct got_tree_entry *new_te,
4828 struct got_pathlist_head *paths)
4830 const struct got_error *err = NULL;
4831 struct got_pathlist_entry *new_pe;
4833 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4834 if (err)
4835 return err;
4836 if (new_pe == NULL)
4837 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4838 return NULL;
4841 static const struct got_error *
4842 report_ct_status(struct got_commitable *ct,
4843 got_worktree_status_cb status_cb, void *status_arg)
4845 const char *ct_path = ct->path;
4846 unsigned char status;
4848 while (ct_path[0] == '/')
4849 ct_path++;
4851 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4852 status = ct->staged_status;
4853 else
4854 status = ct->status;
4856 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4857 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4860 static const struct got_error *
4861 match_modified_subtree(int *modified, struct got_tree_entry *te,
4862 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4864 const struct got_error *err = NULL;
4865 struct got_pathlist_entry *pe;
4866 char *te_path;
4868 *modified = 0;
4870 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4871 got_path_is_root_dir(base_tree_path) ? "" : "/",
4872 te->name) == -1)
4873 return got_error_from_errno("asprintf");
4875 TAILQ_FOREACH(pe, commitable_paths, entry) {
4876 struct got_commitable *ct = pe->data;
4877 *modified = got_path_is_child(ct->in_repo_path, te_path,
4878 strlen(te_path));
4879 if (*modified)
4880 break;
4883 free(te_path);
4884 return err;
4887 static const struct got_error *
4888 match_deleted_or_modified_ct(struct got_commitable **ctp,
4889 struct got_tree_entry *te, const char *base_tree_path,
4890 struct got_pathlist_head *commitable_paths)
4892 const struct got_error *err = NULL;
4893 struct got_pathlist_entry *pe;
4895 *ctp = NULL;
4897 TAILQ_FOREACH(pe, commitable_paths, entry) {
4898 struct got_commitable *ct = pe->data;
4899 char *ct_name = NULL;
4900 int path_matches;
4902 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4903 if (ct->status != GOT_STATUS_MODIFY &&
4904 ct->status != GOT_STATUS_MODE_CHANGE &&
4905 ct->status != GOT_STATUS_DELETE)
4906 continue;
4907 } else {
4908 if (ct->staged_status != GOT_STATUS_MODIFY &&
4909 ct->staged_status != GOT_STATUS_DELETE)
4910 continue;
4913 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4914 continue;
4916 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4917 if (err)
4918 return err;
4919 if (!path_matches)
4920 continue;
4922 ct_name = basename(pe->path);
4923 if (ct_name == NULL)
4924 return got_error_from_errno2("basename", pe->path);
4926 if (strcmp(te->name, ct_name) != 0)
4927 continue;
4929 *ctp = ct;
4930 break;
4933 return err;
4936 static const struct got_error *
4937 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4938 const char *child_path, const char *path_base_tree,
4939 struct got_pathlist_head *commitable_paths,
4940 got_worktree_status_cb status_cb, void *status_arg,
4941 struct got_repository *repo)
4943 const struct got_error *err = NULL;
4944 struct got_tree_entry *new_te;
4945 char *subtree_path;
4946 struct got_object_id *id = NULL;
4947 int nentries;
4949 *new_tep = NULL;
4951 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4952 got_path_is_root_dir(path_base_tree) ? "" : "/",
4953 child_path) == -1)
4954 return got_error_from_errno("asprintf");
4956 new_te = calloc(1, sizeof(*new_te));
4957 if (new_te == NULL)
4958 return got_error_from_errno("calloc");
4959 new_te->mode = S_IFDIR;
4961 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4962 sizeof(new_te->name)) {
4963 err = got_error(GOT_ERR_NO_SPACE);
4964 goto done;
4966 err = write_tree(&id, &nentries, NULL, subtree_path,
4967 commitable_paths, status_cb, status_arg, repo);
4968 if (err) {
4969 free(new_te);
4970 goto done;
4972 memcpy(&new_te->id, id, sizeof(new_te->id));
4973 done:
4974 free(id);
4975 free(subtree_path);
4976 if (err == NULL)
4977 *new_tep = new_te;
4978 return err;
4981 static const struct got_error *
4982 write_tree(struct got_object_id **new_tree_id, int *nentries,
4983 struct got_tree_object *base_tree, const char *path_base_tree,
4984 struct got_pathlist_head *commitable_paths,
4985 got_worktree_status_cb status_cb, void *status_arg,
4986 struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 struct got_pathlist_head paths;
4990 struct got_tree_entry *te, *new_te = NULL;
4991 struct got_pathlist_entry *pe;
4993 TAILQ_INIT(&paths);
4994 *nentries = 0;
4996 /* Insert, and recurse into, newly added entries first. */
4997 TAILQ_FOREACH(pe, commitable_paths, entry) {
4998 struct got_commitable *ct = pe->data;
4999 char *child_path = NULL, *slash;
5001 if ((ct->status != GOT_STATUS_ADD &&
5002 ct->staged_status != GOT_STATUS_ADD) ||
5003 (ct->flags & GOT_COMMITABLE_ADDED))
5004 continue;
5006 if (!got_path_is_child(pe->path, path_base_tree,
5007 strlen(path_base_tree)))
5008 continue;
5010 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5011 pe->path);
5012 if (err)
5013 goto done;
5015 slash = strchr(child_path, '/');
5016 if (slash == NULL) {
5017 err = alloc_added_blob_tree_entry(&new_te, ct);
5018 if (err)
5019 goto done;
5020 err = report_ct_status(ct, status_cb, status_arg);
5021 if (err)
5022 goto done;
5023 ct->flags |= GOT_COMMITABLE_ADDED;
5024 err = insert_tree_entry(new_te, &paths);
5025 if (err)
5026 goto done;
5027 (*nentries)++;
5028 } else {
5029 *slash = '\0'; /* trim trailing path components */
5030 if (base_tree == NULL ||
5031 got_object_tree_find_entry(base_tree, child_path)
5032 == NULL) {
5033 err = make_subtree_for_added_blob(&new_te,
5034 child_path, path_base_tree,
5035 commitable_paths, status_cb, status_arg,
5036 repo);
5037 if (err)
5038 goto done;
5039 err = insert_tree_entry(new_te, &paths);
5040 if (err)
5041 goto done;
5042 (*nentries)++;
5047 if (base_tree) {
5048 int i, nbase_entries;
5049 /* Handle modified and deleted entries. */
5050 nbase_entries = got_object_tree_get_nentries(base_tree);
5051 for (i = 0; i < nbase_entries; i++) {
5052 struct got_commitable *ct = NULL;
5054 te = got_object_tree_get_entry(base_tree, i);
5055 if (got_object_tree_entry_is_submodule(te)) {
5056 /* Entry is a submodule; just copy it. */
5057 err = got_object_tree_entry_dup(&new_te, te);
5058 if (err)
5059 goto done;
5060 err = insert_tree_entry(new_te, &paths);
5061 if (err)
5062 goto done;
5063 (*nentries)++;
5064 continue;
5067 if (S_ISDIR(te->mode)) {
5068 int modified;
5069 err = got_object_tree_entry_dup(&new_te, te);
5070 if (err)
5071 goto done;
5072 err = match_modified_subtree(&modified, te,
5073 path_base_tree, commitable_paths);
5074 if (err)
5075 goto done;
5076 /* Avoid recursion into unmodified subtrees. */
5077 if (modified) {
5078 struct got_object_id *new_id;
5079 int nsubentries;
5080 err = write_subtree(&new_id,
5081 &nsubentries, te,
5082 path_base_tree, commitable_paths,
5083 status_cb, status_arg, repo);
5084 if (err)
5085 goto done;
5086 if (nsubentries == 0) {
5087 /* All entries were deleted. */
5088 free(new_id);
5089 continue;
5091 memcpy(&new_te->id, new_id,
5092 sizeof(new_te->id));
5093 free(new_id);
5095 err = insert_tree_entry(new_te, &paths);
5096 if (err)
5097 goto done;
5098 (*nentries)++;
5099 continue;
5102 err = match_deleted_or_modified_ct(&ct, te,
5103 path_base_tree, commitable_paths);
5104 if (err)
5105 goto done;
5106 if (ct) {
5107 /* NB: Deleted entries get dropped here. */
5108 if (ct->status == GOT_STATUS_MODIFY ||
5109 ct->status == GOT_STATUS_MODE_CHANGE ||
5110 ct->staged_status == GOT_STATUS_MODIFY) {
5111 err = alloc_modified_blob_tree_entry(
5112 &new_te, te, ct);
5113 if (err)
5114 goto done;
5115 err = insert_tree_entry(new_te, &paths);
5116 if (err)
5117 goto done;
5118 (*nentries)++;
5120 err = report_ct_status(ct, status_cb,
5121 status_arg);
5122 if (err)
5123 goto done;
5124 } else {
5125 /* Entry is unchanged; just copy it. */
5126 err = got_object_tree_entry_dup(&new_te, te);
5127 if (err)
5128 goto done;
5129 err = insert_tree_entry(new_te, &paths);
5130 if (err)
5131 goto done;
5132 (*nentries)++;
5137 /* Write new list of entries; deleted entries have been dropped. */
5138 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5139 done:
5140 got_pathlist_free(&paths);
5141 return err;
5144 static const struct got_error *
5145 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5146 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5147 int have_staged_files)
5149 const struct got_error *err = NULL;
5150 struct got_pathlist_entry *pe;
5152 TAILQ_FOREACH(pe, commitable_paths, entry) {
5153 struct got_fileindex_entry *ie;
5154 struct got_commitable *ct = pe->data;
5156 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5157 if (ie) {
5158 if (ct->status == GOT_STATUS_DELETE ||
5159 ct->staged_status == GOT_STATUS_DELETE) {
5160 got_fileindex_entry_remove(fileindex, ie);
5161 } else if (ct->staged_status == GOT_STATUS_ADD ||
5162 ct->staged_status == GOT_STATUS_MODIFY) {
5163 got_fileindex_entry_stage_set(ie,
5164 GOT_FILEIDX_STAGE_NONE);
5165 err = got_fileindex_entry_update(ie,
5166 ct->ondisk_path, ct->staged_blob_id->sha1,
5167 new_base_commit_id->sha1,
5168 !have_staged_files);
5169 } else
5170 err = got_fileindex_entry_update(ie,
5171 ct->ondisk_path, ct->blob_id->sha1,
5172 new_base_commit_id->sha1,
5173 !have_staged_files);
5174 } else {
5175 err = got_fileindex_entry_alloc(&ie, pe->path);
5176 if (err)
5177 break;
5178 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5179 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5180 if (err) {
5181 got_fileindex_entry_free(ie);
5182 break;
5184 err = got_fileindex_entry_add(fileindex, ie);
5185 if (err) {
5186 got_fileindex_entry_free(ie);
5187 break;
5191 return err;
5195 static const struct got_error *
5196 check_out_of_date(const char *in_repo_path, unsigned char status,
5197 unsigned char staged_status, struct got_object_id *base_blob_id,
5198 struct got_object_id *base_commit_id,
5199 struct got_object_id *head_commit_id, struct got_repository *repo,
5200 int ood_errcode)
5202 const struct got_error *err = NULL;
5203 struct got_object_id *id = NULL;
5205 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5206 /* Trivial case: base commit == head commit */
5207 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5208 return NULL;
5210 * Ensure file content which local changes were based
5211 * on matches file content in the branch head.
5213 err = got_object_id_by_path(&id, repo, head_commit_id,
5214 in_repo_path);
5215 if (err) {
5216 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5217 err = got_error(ood_errcode);
5218 goto done;
5219 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5220 err = got_error(ood_errcode);
5221 } else {
5222 /* Require that added files don't exist in the branch head. */
5223 err = got_object_id_by_path(&id, repo, head_commit_id,
5224 in_repo_path);
5225 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5226 goto done;
5227 err = id ? got_error(ood_errcode) : NULL;
5229 done:
5230 free(id);
5231 return err;
5234 const struct got_error *
5235 commit_worktree(struct got_object_id **new_commit_id,
5236 struct got_pathlist_head *commitable_paths,
5237 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5238 const char *author, const char *committer,
5239 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5240 got_worktree_status_cb status_cb, void *status_arg,
5241 struct got_repository *repo)
5243 const struct got_error *err = NULL, *unlockerr = NULL;
5244 struct got_pathlist_entry *pe;
5245 const char *head_ref_name = NULL;
5246 struct got_commit_object *head_commit = NULL;
5247 struct got_reference *head_ref2 = NULL;
5248 struct got_object_id *head_commit_id2 = NULL;
5249 struct got_tree_object *head_tree = NULL;
5250 struct got_object_id *new_tree_id = NULL;
5251 int nentries;
5252 struct got_object_id_queue parent_ids;
5253 struct got_object_qid *pid = NULL;
5254 char *logmsg = NULL;
5256 *new_commit_id = NULL;
5258 SIMPLEQ_INIT(&parent_ids);
5260 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5261 if (err)
5262 goto done;
5264 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5265 if (err)
5266 goto done;
5268 if (commit_msg_cb != NULL) {
5269 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5270 if (err)
5271 goto done;
5274 if (logmsg == NULL || strlen(logmsg) == 0) {
5275 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5276 goto done;
5279 /* Create blobs from added and modified files and record their IDs. */
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_commitable *ct = pe->data;
5282 char *ondisk_path;
5284 /* Blobs for staged files already exist. */
5285 if (ct->staged_status == GOT_STATUS_ADD ||
5286 ct->staged_status == GOT_STATUS_MODIFY)
5287 continue;
5289 if (ct->status != GOT_STATUS_ADD &&
5290 ct->status != GOT_STATUS_MODIFY &&
5291 ct->status != GOT_STATUS_MODE_CHANGE)
5292 continue;
5294 if (asprintf(&ondisk_path, "%s/%s",
5295 worktree->root_path, pe->path) == -1) {
5296 err = got_error_from_errno("asprintf");
5297 goto done;
5299 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5300 free(ondisk_path);
5301 if (err)
5302 goto done;
5305 /* Recursively write new tree objects. */
5306 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5307 commitable_paths, status_cb, status_arg, repo);
5308 if (err)
5309 goto done;
5311 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5312 if (err)
5313 goto done;
5314 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5315 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5316 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5317 got_object_qid_free(pid);
5318 if (logmsg != NULL)
5319 free(logmsg);
5320 if (err)
5321 goto done;
5323 /* Check if a concurrent commit to our branch has occurred. */
5324 head_ref_name = got_worktree_get_head_ref_name(worktree);
5325 if (head_ref_name == NULL) {
5326 err = got_error_from_errno("got_worktree_get_head_ref_name");
5327 goto done;
5329 /* Lock the reference here to prevent concurrent modification. */
5330 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5331 if (err)
5332 goto done;
5333 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5334 if (err)
5335 goto done;
5336 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5337 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5338 goto done;
5340 /* Update branch head in repository. */
5341 err = got_ref_change_ref(head_ref2, *new_commit_id);
5342 if (err)
5343 goto done;
5344 err = got_ref_write(head_ref2, repo);
5345 if (err)
5346 goto done;
5348 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5349 if (err)
5350 goto done;
5352 err = ref_base_commit(worktree, repo);
5353 if (err)
5354 goto done;
5355 done:
5356 if (head_tree)
5357 got_object_tree_close(head_tree);
5358 if (head_commit)
5359 got_object_commit_close(head_commit);
5360 free(head_commit_id2);
5361 if (head_ref2) {
5362 unlockerr = got_ref_unlock(head_ref2);
5363 if (unlockerr && err == NULL)
5364 err = unlockerr;
5365 got_ref_close(head_ref2);
5367 return err;
5370 static const struct got_error *
5371 check_path_is_commitable(const char *path,
5372 struct got_pathlist_head *commitable_paths)
5374 struct got_pathlist_entry *cpe = NULL;
5375 size_t path_len = strlen(path);
5377 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5378 struct got_commitable *ct = cpe->data;
5379 const char *ct_path = ct->path;
5381 while (ct_path[0] == '/')
5382 ct_path++;
5384 if (strcmp(path, ct_path) == 0 ||
5385 got_path_is_child(ct_path, path, path_len))
5386 break;
5389 if (cpe == NULL)
5390 return got_error_path(path, GOT_ERR_BAD_PATH);
5392 return NULL;
5395 static const struct got_error *
5396 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5398 int *have_staged_files = arg;
5400 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5401 *have_staged_files = 1;
5402 return got_error(GOT_ERR_CANCELLED);
5405 return NULL;
5408 static const struct got_error *
5409 check_non_staged_files(struct got_fileindex *fileindex,
5410 struct got_pathlist_head *paths)
5412 struct got_pathlist_entry *pe;
5413 struct got_fileindex_entry *ie;
5415 TAILQ_FOREACH(pe, paths, entry) {
5416 if (pe->path[0] == '\0')
5417 continue;
5418 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5419 if (ie == NULL)
5420 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5421 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5422 return got_error_path(pe->path,
5423 GOT_ERR_FILE_NOT_STAGED);
5426 return NULL;
5429 const struct got_error *
5430 got_worktree_commit(struct got_object_id **new_commit_id,
5431 struct got_worktree *worktree, struct got_pathlist_head *paths,
5432 const char *author, const char *committer, int allow_bad_symlinks,
5433 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 got_worktree_status_cb status_cb, void *status_arg,
5435 struct got_repository *repo)
5437 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5438 struct got_fileindex *fileindex = NULL;
5439 char *fileindex_path = NULL;
5440 struct got_pathlist_head commitable_paths;
5441 struct collect_commitables_arg cc_arg;
5442 struct got_pathlist_entry *pe;
5443 struct got_reference *head_ref = NULL;
5444 struct got_object_id *head_commit_id = NULL;
5445 int have_staged_files = 0;
5447 *new_commit_id = NULL;
5449 TAILQ_INIT(&commitable_paths);
5451 err = lock_worktree(worktree, LOCK_EX);
5452 if (err)
5453 goto done;
5455 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5456 if (err)
5457 goto done;
5459 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5460 if (err)
5461 goto done;
5463 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5464 if (err)
5465 goto done;
5467 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5468 &have_staged_files);
5469 if (err && err->code != GOT_ERR_CANCELLED)
5470 goto done;
5471 if (have_staged_files) {
5472 err = check_non_staged_files(fileindex, paths);
5473 if (err)
5474 goto done;
5477 cc_arg.commitable_paths = &commitable_paths;
5478 cc_arg.worktree = worktree;
5479 cc_arg.fileindex = fileindex;
5480 cc_arg.repo = repo;
5481 cc_arg.have_staged_files = have_staged_files;
5482 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = worktree_status(worktree, pe->path, fileindex, repo,
5485 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5486 if (err)
5487 goto done;
5490 if (TAILQ_EMPTY(&commitable_paths)) {
5491 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5492 goto done;
5495 TAILQ_FOREACH(pe, paths, entry) {
5496 err = check_path_is_commitable(pe->path, &commitable_paths);
5497 if (err)
5498 goto done;
5501 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5502 struct got_commitable *ct = pe->data;
5503 const char *ct_path = ct->in_repo_path;
5505 while (ct_path[0] == '/')
5506 ct_path++;
5507 err = check_out_of_date(ct_path, ct->status,
5508 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5509 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5510 if (err)
5511 goto done;
5515 err = commit_worktree(new_commit_id, &commitable_paths,
5516 head_commit_id, worktree, author, committer,
5517 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5518 if (err)
5519 goto done;
5521 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5522 fileindex, have_staged_files);
5523 sync_err = sync_fileindex(fileindex, fileindex_path);
5524 if (sync_err && err == NULL)
5525 err = sync_err;
5526 done:
5527 if (fileindex)
5528 got_fileindex_free(fileindex);
5529 free(fileindex_path);
5530 unlockerr = lock_worktree(worktree, LOCK_SH);
5531 if (unlockerr && err == NULL)
5532 err = unlockerr;
5533 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5534 struct got_commitable *ct = pe->data;
5535 free_commitable(ct);
5537 got_pathlist_free(&commitable_paths);
5538 return err;
5541 const char *
5542 got_commitable_get_path(struct got_commitable *ct)
5544 return ct->path;
5547 unsigned int
5548 got_commitable_get_status(struct got_commitable *ct)
5550 return ct->status;
5553 struct check_rebase_ok_arg {
5554 struct got_worktree *worktree;
5555 struct got_repository *repo;
5558 static const struct got_error *
5559 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5561 const struct got_error *err = NULL;
5562 struct check_rebase_ok_arg *a = arg;
5563 unsigned char status;
5564 struct stat sb;
5565 char *ondisk_path;
5567 /* Reject rebase of a work tree with mixed base commits. */
5568 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5569 SHA1_DIGEST_LENGTH))
5570 return got_error(GOT_ERR_MIXED_COMMITS);
5572 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5573 == -1)
5574 return got_error_from_errno("asprintf");
5576 /* Reject rebase of a work tree with modified or staged files. */
5577 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5578 free(ondisk_path);
5579 if (err)
5580 return err;
5582 if (status != GOT_STATUS_NO_CHANGE)
5583 return got_error(GOT_ERR_MODIFIED);
5584 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5585 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5587 return NULL;
5590 const struct got_error *
5591 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5592 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5593 struct got_worktree *worktree, struct got_reference *branch,
5594 struct got_repository *repo)
5596 const struct got_error *err = NULL;
5597 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5598 char *branch_ref_name = NULL;
5599 char *fileindex_path = NULL;
5600 struct check_rebase_ok_arg ok_arg;
5601 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5602 struct got_object_id *wt_branch_tip = NULL;
5604 *new_base_branch_ref = NULL;
5605 *tmp_branch = NULL;
5606 *fileindex = NULL;
5608 err = lock_worktree(worktree, LOCK_EX);
5609 if (err)
5610 return err;
5612 err = open_fileindex(fileindex, &fileindex_path, worktree);
5613 if (err)
5614 goto done;
5616 ok_arg.worktree = worktree;
5617 ok_arg.repo = repo;
5618 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5619 &ok_arg);
5620 if (err)
5621 goto done;
5623 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5624 if (err)
5625 goto done;
5627 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5628 if (err)
5629 goto done;
5631 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5632 if (err)
5633 goto done;
5635 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5636 0);
5637 if (err)
5638 goto done;
5640 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5641 if (err)
5642 goto done;
5643 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5644 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5645 goto done;
5648 err = got_ref_alloc_symref(new_base_branch_ref,
5649 new_base_branch_ref_name, wt_branch);
5650 if (err)
5651 goto done;
5652 err = got_ref_write(*new_base_branch_ref, repo);
5653 if (err)
5654 goto done;
5656 /* TODO Lock original branch's ref while rebasing? */
5658 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5659 if (err)
5660 goto done;
5662 err = got_ref_write(branch_ref, repo);
5663 if (err)
5664 goto done;
5666 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5667 worktree->base_commit_id);
5668 if (err)
5669 goto done;
5670 err = got_ref_write(*tmp_branch, repo);
5671 if (err)
5672 goto done;
5674 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5675 if (err)
5676 goto done;
5677 done:
5678 free(fileindex_path);
5679 free(tmp_branch_name);
5680 free(new_base_branch_ref_name);
5681 free(branch_ref_name);
5682 if (branch_ref)
5683 got_ref_close(branch_ref);
5684 if (wt_branch)
5685 got_ref_close(wt_branch);
5686 free(wt_branch_tip);
5687 if (err) {
5688 if (*new_base_branch_ref) {
5689 got_ref_close(*new_base_branch_ref);
5690 *new_base_branch_ref = NULL;
5692 if (*tmp_branch) {
5693 got_ref_close(*tmp_branch);
5694 *tmp_branch = NULL;
5696 if (*fileindex) {
5697 got_fileindex_free(*fileindex);
5698 *fileindex = NULL;
5700 lock_worktree(worktree, LOCK_SH);
5702 return err;
5705 const struct got_error *
5706 got_worktree_rebase_continue(struct got_object_id **commit_id,
5707 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5708 struct got_reference **branch, struct got_fileindex **fileindex,
5709 struct got_worktree *worktree, struct got_repository *repo)
5711 const struct got_error *err;
5712 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5713 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5714 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5715 char *fileindex_path = NULL;
5716 int have_staged_files = 0;
5718 *commit_id = NULL;
5719 *new_base_branch = NULL;
5720 *tmp_branch = NULL;
5721 *branch = NULL;
5722 *fileindex = NULL;
5724 err = lock_worktree(worktree, LOCK_EX);
5725 if (err)
5726 return err;
5728 err = open_fileindex(fileindex, &fileindex_path, worktree);
5729 if (err)
5730 goto done;
5732 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5733 &have_staged_files);
5734 if (err && err->code != GOT_ERR_CANCELLED)
5735 goto done;
5736 if (have_staged_files) {
5737 err = got_error(GOT_ERR_STAGED_PATHS);
5738 goto done;
5741 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5742 if (err)
5743 goto done;
5745 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5746 if (err)
5747 goto done;
5749 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5750 if (err)
5751 goto done;
5753 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5754 if (err)
5755 goto done;
5757 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5758 if (err)
5759 goto done;
5761 err = got_ref_open(branch, repo,
5762 got_ref_get_symref_target(branch_ref), 0);
5763 if (err)
5764 goto done;
5766 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5767 if (err)
5768 goto done;
5770 err = got_ref_resolve(commit_id, repo, commit_ref);
5771 if (err)
5772 goto done;
5774 err = got_ref_open(new_base_branch, repo,
5775 new_base_branch_ref_name, 0);
5776 if (err)
5777 goto done;
5779 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5780 if (err)
5781 goto done;
5782 done:
5783 free(commit_ref_name);
5784 free(branch_ref_name);
5785 free(fileindex_path);
5786 if (commit_ref)
5787 got_ref_close(commit_ref);
5788 if (branch_ref)
5789 got_ref_close(branch_ref);
5790 if (err) {
5791 free(*commit_id);
5792 *commit_id = NULL;
5793 if (*tmp_branch) {
5794 got_ref_close(*tmp_branch);
5795 *tmp_branch = NULL;
5797 if (*new_base_branch) {
5798 got_ref_close(*new_base_branch);
5799 *new_base_branch = NULL;
5801 if (*branch) {
5802 got_ref_close(*branch);
5803 *branch = NULL;
5805 if (*fileindex) {
5806 got_fileindex_free(*fileindex);
5807 *fileindex = NULL;
5809 lock_worktree(worktree, LOCK_SH);
5811 return err;
5814 const struct got_error *
5815 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5817 const struct got_error *err;
5818 char *tmp_branch_name = NULL;
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 if (err)
5822 return err;
5824 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5825 free(tmp_branch_name);
5826 return NULL;
5829 static const struct got_error *
5830 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5831 char **logmsg, void *arg)
5833 *logmsg = arg;
5834 return NULL;
5837 static const struct got_error *
5838 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5839 const char *path, struct got_object_id *blob_id,
5840 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5841 int dirfd, const char *de_name)
5843 return NULL;
5846 struct collect_merged_paths_arg {
5847 got_worktree_checkout_cb progress_cb;
5848 void *progress_arg;
5849 struct got_pathlist_head *merged_paths;
5852 static const struct got_error *
5853 collect_merged_paths(void *arg, unsigned char status, const char *path)
5855 const struct got_error *err;
5856 struct collect_merged_paths_arg *a = arg;
5857 char *p;
5858 struct got_pathlist_entry *new;
5860 err = (*a->progress_cb)(a->progress_arg, status, path);
5861 if (err)
5862 return err;
5864 if (status != GOT_STATUS_MERGE &&
5865 status != GOT_STATUS_ADD &&
5866 status != GOT_STATUS_DELETE &&
5867 status != GOT_STATUS_CONFLICT)
5868 return NULL;
5870 p = strdup(path);
5871 if (p == NULL)
5872 return got_error_from_errno("strdup");
5874 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5875 if (err || new == NULL)
5876 free(p);
5877 return err;
5880 void
5881 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5883 struct got_pathlist_entry *pe;
5885 TAILQ_FOREACH(pe, merged_paths, entry)
5886 free((char *)pe->path);
5888 got_pathlist_free(merged_paths);
5891 static const struct got_error *
5892 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5893 int is_rebase, struct got_repository *repo)
5895 const struct got_error *err;
5896 struct got_reference *commit_ref = NULL;
5898 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5899 if (err) {
5900 if (err->code != GOT_ERR_NOT_REF)
5901 goto done;
5902 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5903 if (err)
5904 goto done;
5905 err = got_ref_write(commit_ref, repo);
5906 if (err)
5907 goto done;
5908 } else if (is_rebase) {
5909 struct got_object_id *stored_id;
5910 int cmp;
5912 err = got_ref_resolve(&stored_id, repo, commit_ref);
5913 if (err)
5914 goto done;
5915 cmp = got_object_id_cmp(commit_id, stored_id);
5916 free(stored_id);
5917 if (cmp != 0) {
5918 err = got_error(GOT_ERR_REBASE_COMMITID);
5919 goto done;
5922 done:
5923 if (commit_ref)
5924 got_ref_close(commit_ref);
5925 return err;
5928 static const struct got_error *
5929 rebase_merge_files(struct got_pathlist_head *merged_paths,
5930 const char *commit_ref_name, struct got_worktree *worktree,
5931 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5932 struct got_object_id *commit_id, struct got_repository *repo,
5933 got_worktree_checkout_cb progress_cb, void *progress_arg,
5934 got_cancel_cb cancel_cb, void *cancel_arg)
5936 const struct got_error *err;
5937 struct got_reference *commit_ref = NULL;
5938 struct collect_merged_paths_arg cmp_arg;
5939 char *fileindex_path;
5941 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5943 err = get_fileindex_path(&fileindex_path, worktree);
5944 if (err)
5945 return err;
5947 cmp_arg.progress_cb = progress_cb;
5948 cmp_arg.progress_arg = progress_arg;
5949 cmp_arg.merged_paths = merged_paths;
5950 err = merge_files(worktree, fileindex, fileindex_path,
5951 parent_commit_id, commit_id, repo, collect_merged_paths,
5952 &cmp_arg, cancel_cb, cancel_arg);
5953 if (commit_ref)
5954 got_ref_close(commit_ref);
5955 return err;
5958 const struct got_error *
5959 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5960 struct got_worktree *worktree, struct got_fileindex *fileindex,
5961 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5962 struct got_repository *repo,
5963 got_worktree_checkout_cb progress_cb, void *progress_arg,
5964 got_cancel_cb cancel_cb, void *cancel_arg)
5966 const struct got_error *err;
5967 char *commit_ref_name;
5969 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5970 if (err)
5971 return err;
5973 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5974 if (err)
5975 goto done;
5977 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5978 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5979 progress_arg, cancel_cb, cancel_arg);
5980 done:
5981 free(commit_ref_name);
5982 return err;
5985 const struct got_error *
5986 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5987 struct got_worktree *worktree, struct got_fileindex *fileindex,
5988 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5989 struct got_repository *repo,
5990 got_worktree_checkout_cb progress_cb, void *progress_arg,
5991 got_cancel_cb cancel_cb, void *cancel_arg)
5993 const struct got_error *err;
5994 char *commit_ref_name;
5996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5997 if (err)
5998 return err;
6000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6001 if (err)
6002 goto done;
6004 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6005 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6006 progress_arg, cancel_cb, cancel_arg);
6007 done:
6008 free(commit_ref_name);
6009 return err;
6012 static const struct got_error *
6013 rebase_commit(struct got_object_id **new_commit_id,
6014 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6015 struct got_worktree *worktree, struct got_fileindex *fileindex,
6016 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6017 const char *new_logmsg, struct got_repository *repo)
6019 const struct got_error *err, *sync_err;
6020 struct got_pathlist_head commitable_paths;
6021 struct collect_commitables_arg cc_arg;
6022 char *fileindex_path = NULL;
6023 struct got_reference *head_ref = NULL;
6024 struct got_object_id *head_commit_id = NULL;
6025 char *logmsg = NULL;
6027 TAILQ_INIT(&commitable_paths);
6028 *new_commit_id = NULL;
6030 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6032 err = get_fileindex_path(&fileindex_path, worktree);
6033 if (err)
6034 return err;
6036 cc_arg.commitable_paths = &commitable_paths;
6037 cc_arg.worktree = worktree;
6038 cc_arg.repo = repo;
6039 cc_arg.have_staged_files = 0;
6041 * If possible get the status of individual files directly to
6042 * avoid crawling the entire work tree once per rebased commit.
6043 * TODO: Ideally, merged_paths would contain a list of commitables
6044 * we could use so we could skip worktree_status() entirely.
6046 if (merged_paths) {
6047 struct got_pathlist_entry *pe;
6048 TAILQ_FOREACH(pe, merged_paths, entry) {
6049 err = worktree_status(worktree, pe->path, fileindex,
6050 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6051 0);
6052 if (err)
6053 goto done;
6055 } else {
6056 err = worktree_status(worktree, "", fileindex, repo,
6057 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6058 if (err)
6059 goto done;
6062 if (TAILQ_EMPTY(&commitable_paths)) {
6063 /* No-op change; commit will be elided. */
6064 err = got_ref_delete(commit_ref, repo);
6065 if (err)
6066 goto done;
6067 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6068 goto done;
6071 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6072 if (err)
6073 goto done;
6075 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6076 if (err)
6077 goto done;
6079 if (new_logmsg) {
6080 logmsg = strdup(new_logmsg);
6081 if (logmsg == NULL) {
6082 err = got_error_from_errno("strdup");
6083 goto done;
6085 } else {
6086 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6087 if (err)
6088 goto done;
6091 /* NB: commit_worktree will call free(logmsg) */
6092 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6093 worktree, got_object_commit_get_author(orig_commit),
6094 got_object_commit_get_committer(orig_commit),
6095 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6096 if (err)
6097 goto done;
6099 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6100 if (err)
6101 goto done;
6103 err = got_ref_delete(commit_ref, repo);
6104 if (err)
6105 goto done;
6107 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6108 fileindex, 0);
6109 sync_err = sync_fileindex(fileindex, fileindex_path);
6110 if (sync_err && err == NULL)
6111 err = sync_err;
6112 done:
6113 free(fileindex_path);
6114 free(head_commit_id);
6115 if (head_ref)
6116 got_ref_close(head_ref);
6117 if (err) {
6118 free(*new_commit_id);
6119 *new_commit_id = NULL;
6121 return err;
6124 const struct got_error *
6125 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6126 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6127 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6128 struct got_commit_object *orig_commit,
6129 struct got_object_id *orig_commit_id, struct got_repository *repo)
6131 const struct got_error *err;
6132 char *commit_ref_name;
6133 struct got_reference *commit_ref = NULL;
6134 struct got_object_id *commit_id = NULL;
6136 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6137 if (err)
6138 return err;
6140 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6141 if (err)
6142 goto done;
6143 err = got_ref_resolve(&commit_id, repo, commit_ref);
6144 if (err)
6145 goto done;
6146 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6147 err = got_error(GOT_ERR_REBASE_COMMITID);
6148 goto done;
6151 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6152 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6153 done:
6154 if (commit_ref)
6155 got_ref_close(commit_ref);
6156 free(commit_ref_name);
6157 free(commit_id);
6158 return err;
6161 const struct got_error *
6162 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6163 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6164 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6165 struct got_commit_object *orig_commit,
6166 struct got_object_id *orig_commit_id, const char *new_logmsg,
6167 struct got_repository *repo)
6169 const struct got_error *err;
6170 char *commit_ref_name;
6171 struct got_reference *commit_ref = NULL;
6173 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6174 if (err)
6175 return err;
6177 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6178 if (err)
6179 goto done;
6181 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6182 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6183 done:
6184 if (commit_ref)
6185 got_ref_close(commit_ref);
6186 free(commit_ref_name);
6187 return err;
6190 const struct got_error *
6191 got_worktree_rebase_postpone(struct got_worktree *worktree,
6192 struct got_fileindex *fileindex)
6194 if (fileindex)
6195 got_fileindex_free(fileindex);
6196 return lock_worktree(worktree, LOCK_SH);
6199 static const struct got_error *
6200 delete_ref(const char *name, struct got_repository *repo)
6202 const struct got_error *err;
6203 struct got_reference *ref;
6205 err = got_ref_open(&ref, repo, name, 0);
6206 if (err) {
6207 if (err->code == GOT_ERR_NOT_REF)
6208 return NULL;
6209 return err;
6212 err = got_ref_delete(ref, repo);
6213 got_ref_close(ref);
6214 return err;
6217 static const struct got_error *
6218 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6220 const struct got_error *err;
6221 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6222 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6224 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6225 if (err)
6226 goto done;
6227 err = delete_ref(tmp_branch_name, repo);
6228 if (err)
6229 goto done;
6231 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6232 if (err)
6233 goto done;
6234 err = delete_ref(new_base_branch_ref_name, repo);
6235 if (err)
6236 goto done;
6238 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6239 if (err)
6240 goto done;
6241 err = delete_ref(branch_ref_name, repo);
6242 if (err)
6243 goto done;
6245 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6246 if (err)
6247 goto done;
6248 err = delete_ref(commit_ref_name, repo);
6249 if (err)
6250 goto done;
6252 done:
6253 free(tmp_branch_name);
6254 free(new_base_branch_ref_name);
6255 free(branch_ref_name);
6256 free(commit_ref_name);
6257 return err;
6260 const struct got_error *
6261 got_worktree_rebase_complete(struct got_worktree *worktree,
6262 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6263 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6264 struct got_repository *repo)
6266 const struct got_error *err, *unlockerr;
6267 struct got_object_id *new_head_commit_id = NULL;
6269 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6270 if (err)
6271 return err;
6273 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6274 if (err)
6275 goto done;
6277 err = got_ref_write(rebased_branch, repo);
6278 if (err)
6279 goto done;
6281 err = got_worktree_set_head_ref(worktree, rebased_branch);
6282 if (err)
6283 goto done;
6285 err = delete_rebase_refs(worktree, repo);
6286 done:
6287 if (fileindex)
6288 got_fileindex_free(fileindex);
6289 free(new_head_commit_id);
6290 unlockerr = lock_worktree(worktree, LOCK_SH);
6291 if (unlockerr && err == NULL)
6292 err = unlockerr;
6293 return err;
6296 const struct got_error *
6297 got_worktree_rebase_abort(struct got_worktree *worktree,
6298 struct got_fileindex *fileindex, struct got_repository *repo,
6299 struct got_reference *new_base_branch,
6300 got_worktree_checkout_cb progress_cb, void *progress_arg)
6302 const struct got_error *err, *unlockerr, *sync_err;
6303 struct got_reference *resolved = NULL;
6304 struct got_object_id *commit_id = NULL;
6305 char *fileindex_path = NULL;
6306 struct revert_file_args rfa;
6307 struct got_object_id *tree_id = NULL;
6309 err = lock_worktree(worktree, LOCK_EX);
6310 if (err)
6311 return err;
6313 err = got_ref_open(&resolved, repo,
6314 got_ref_get_symref_target(new_base_branch), 0);
6315 if (err)
6316 goto done;
6318 err = got_worktree_set_head_ref(worktree, resolved);
6319 if (err)
6320 goto done;
6323 * XXX commits to the base branch could have happened while
6324 * we were busy rebasing; should we store the original commit ID
6325 * when rebase begins and read it back here?
6327 err = got_ref_resolve(&commit_id, repo, resolved);
6328 if (err)
6329 goto done;
6331 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6332 if (err)
6333 goto done;
6335 err = got_object_id_by_path(&tree_id, repo,
6336 worktree->base_commit_id, worktree->path_prefix);
6337 if (err)
6338 goto done;
6340 err = delete_rebase_refs(worktree, repo);
6341 if (err)
6342 goto done;
6344 err = get_fileindex_path(&fileindex_path, worktree);
6345 if (err)
6346 goto done;
6348 rfa.worktree = worktree;
6349 rfa.fileindex = fileindex;
6350 rfa.progress_cb = progress_cb;
6351 rfa.progress_arg = progress_arg;
6352 rfa.patch_cb = NULL;
6353 rfa.patch_arg = NULL;
6354 rfa.repo = repo;
6355 err = worktree_status(worktree, "", fileindex, repo,
6356 revert_file, &rfa, NULL, NULL, 0, 0);
6357 if (err)
6358 goto sync;
6360 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6361 repo, progress_cb, progress_arg, NULL, NULL);
6362 sync:
6363 sync_err = sync_fileindex(fileindex, fileindex_path);
6364 if (sync_err && err == NULL)
6365 err = sync_err;
6366 done:
6367 got_ref_close(resolved);
6368 free(tree_id);
6369 free(commit_id);
6370 if (fileindex)
6371 got_fileindex_free(fileindex);
6372 free(fileindex_path);
6374 unlockerr = lock_worktree(worktree, LOCK_SH);
6375 if (unlockerr && err == NULL)
6376 err = unlockerr;
6377 return err;
6380 const struct got_error *
6381 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6382 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6383 struct got_fileindex **fileindex, struct got_worktree *worktree,
6384 struct got_repository *repo)
6386 const struct got_error *err = NULL;
6387 char *tmp_branch_name = NULL;
6388 char *branch_ref_name = NULL;
6389 char *base_commit_ref_name = NULL;
6390 char *fileindex_path = NULL;
6391 struct check_rebase_ok_arg ok_arg;
6392 struct got_reference *wt_branch = NULL;
6393 struct got_reference *base_commit_ref = NULL;
6395 *tmp_branch = NULL;
6396 *branch_ref = NULL;
6397 *base_commit_id = NULL;
6398 *fileindex = NULL;
6400 err = lock_worktree(worktree, LOCK_EX);
6401 if (err)
6402 return err;
6404 err = open_fileindex(fileindex, &fileindex_path, worktree);
6405 if (err)
6406 goto done;
6408 ok_arg.worktree = worktree;
6409 ok_arg.repo = repo;
6410 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6411 &ok_arg);
6412 if (err)
6413 goto done;
6415 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6416 if (err)
6417 goto done;
6419 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6420 if (err)
6421 goto done;
6423 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6424 worktree);
6425 if (err)
6426 goto done;
6428 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6429 0);
6430 if (err)
6431 goto done;
6433 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6434 if (err)
6435 goto done;
6437 err = got_ref_write(*branch_ref, repo);
6438 if (err)
6439 goto done;
6441 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6442 worktree->base_commit_id);
6443 if (err)
6444 goto done;
6445 err = got_ref_write(base_commit_ref, repo);
6446 if (err)
6447 goto done;
6448 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6449 if (*base_commit_id == NULL) {
6450 err = got_error_from_errno("got_object_id_dup");
6451 goto done;
6454 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6455 worktree->base_commit_id);
6456 if (err)
6457 goto done;
6458 err = got_ref_write(*tmp_branch, repo);
6459 if (err)
6460 goto done;
6462 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6463 if (err)
6464 goto done;
6465 done:
6466 free(fileindex_path);
6467 free(tmp_branch_name);
6468 free(branch_ref_name);
6469 free(base_commit_ref_name);
6470 if (wt_branch)
6471 got_ref_close(wt_branch);
6472 if (err) {
6473 if (*branch_ref) {
6474 got_ref_close(*branch_ref);
6475 *branch_ref = NULL;
6477 if (*tmp_branch) {
6478 got_ref_close(*tmp_branch);
6479 *tmp_branch = NULL;
6481 free(*base_commit_id);
6482 if (*fileindex) {
6483 got_fileindex_free(*fileindex);
6484 *fileindex = NULL;
6486 lock_worktree(worktree, LOCK_SH);
6488 return err;
6491 const struct got_error *
6492 got_worktree_histedit_postpone(struct got_worktree *worktree,
6493 struct got_fileindex *fileindex)
6495 if (fileindex)
6496 got_fileindex_free(fileindex);
6497 return lock_worktree(worktree, LOCK_SH);
6500 const struct got_error *
6501 got_worktree_histedit_in_progress(int *in_progress,
6502 struct got_worktree *worktree)
6504 const struct got_error *err;
6505 char *tmp_branch_name = NULL;
6507 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6508 if (err)
6509 return err;
6511 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6512 free(tmp_branch_name);
6513 return NULL;
6516 const struct got_error *
6517 got_worktree_histedit_continue(struct got_object_id **commit_id,
6518 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6519 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6520 struct got_worktree *worktree, struct got_repository *repo)
6522 const struct got_error *err;
6523 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6524 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6525 struct got_reference *commit_ref = NULL;
6526 struct got_reference *base_commit_ref = NULL;
6527 char *fileindex_path = NULL;
6528 int have_staged_files = 0;
6530 *commit_id = NULL;
6531 *tmp_branch = NULL;
6532 *base_commit_id = NULL;
6533 *fileindex = NULL;
6535 err = lock_worktree(worktree, LOCK_EX);
6536 if (err)
6537 return err;
6539 err = open_fileindex(fileindex, &fileindex_path, worktree);
6540 if (err)
6541 goto done;
6543 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6544 &have_staged_files);
6545 if (err && err->code != GOT_ERR_CANCELLED)
6546 goto done;
6547 if (have_staged_files) {
6548 err = got_error(GOT_ERR_STAGED_PATHS);
6549 goto done;
6552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6553 if (err)
6554 goto done;
6556 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6557 if (err)
6558 goto done;
6560 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6561 if (err)
6562 goto done;
6564 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6565 worktree);
6566 if (err)
6567 goto done;
6569 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6570 if (err)
6571 goto done;
6573 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6574 if (err)
6575 goto done;
6576 err = got_ref_resolve(commit_id, repo, commit_ref);
6577 if (err)
6578 goto done;
6580 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6581 if (err)
6582 goto done;
6583 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6584 if (err)
6585 goto done;
6587 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6588 if (err)
6589 goto done;
6590 done:
6591 free(commit_ref_name);
6592 free(branch_ref_name);
6593 free(fileindex_path);
6594 if (commit_ref)
6595 got_ref_close(commit_ref);
6596 if (base_commit_ref)
6597 got_ref_close(base_commit_ref);
6598 if (err) {
6599 free(*commit_id);
6600 *commit_id = NULL;
6601 free(*base_commit_id);
6602 *base_commit_id = NULL;
6603 if (*tmp_branch) {
6604 got_ref_close(*tmp_branch);
6605 *tmp_branch = NULL;
6607 if (*fileindex) {
6608 got_fileindex_free(*fileindex);
6609 *fileindex = NULL;
6611 lock_worktree(worktree, LOCK_EX);
6613 return err;
6616 static const struct got_error *
6617 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6619 const struct got_error *err;
6620 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6621 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6623 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6624 if (err)
6625 goto done;
6626 err = delete_ref(tmp_branch_name, repo);
6627 if (err)
6628 goto done;
6630 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6631 worktree);
6632 if (err)
6633 goto done;
6634 err = delete_ref(base_commit_ref_name, repo);
6635 if (err)
6636 goto done;
6638 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6639 if (err)
6640 goto done;
6641 err = delete_ref(branch_ref_name, repo);
6642 if (err)
6643 goto done;
6645 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6646 if (err)
6647 goto done;
6648 err = delete_ref(commit_ref_name, repo);
6649 if (err)
6650 goto done;
6651 done:
6652 free(tmp_branch_name);
6653 free(base_commit_ref_name);
6654 free(branch_ref_name);
6655 free(commit_ref_name);
6656 return err;
6659 const struct got_error *
6660 got_worktree_histedit_abort(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_repository *repo,
6662 struct got_reference *branch, struct got_object_id *base_commit_id,
6663 got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 const struct got_error *err, *unlockerr, *sync_err;
6666 struct got_reference *resolved = NULL;
6667 char *fileindex_path = NULL;
6668 struct got_object_id *tree_id = NULL;
6669 struct revert_file_args rfa;
6671 err = lock_worktree(worktree, LOCK_EX);
6672 if (err)
6673 return err;
6675 err = got_ref_open(&resolved, repo,
6676 got_ref_get_symref_target(branch), 0);
6677 if (err)
6678 goto done;
6680 err = got_worktree_set_head_ref(worktree, resolved);
6681 if (err)
6682 goto done;
6684 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6685 if (err)
6686 goto done;
6688 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6689 worktree->path_prefix);
6690 if (err)
6691 goto done;
6693 err = delete_histedit_refs(worktree, repo);
6694 if (err)
6695 goto done;
6697 err = get_fileindex_path(&fileindex_path, worktree);
6698 if (err)
6699 goto done;
6701 rfa.worktree = worktree;
6702 rfa.fileindex = fileindex;
6703 rfa.progress_cb = progress_cb;
6704 rfa.progress_arg = progress_arg;
6705 rfa.patch_cb = NULL;
6706 rfa.patch_arg = NULL;
6707 rfa.repo = repo;
6708 err = worktree_status(worktree, "", fileindex, repo,
6709 revert_file, &rfa, NULL, NULL, 0, 0);
6710 if (err)
6711 goto sync;
6713 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6714 repo, progress_cb, progress_arg, NULL, NULL);
6715 sync:
6716 sync_err = sync_fileindex(fileindex, fileindex_path);
6717 if (sync_err && err == NULL)
6718 err = sync_err;
6719 done:
6720 got_ref_close(resolved);
6721 free(tree_id);
6722 free(fileindex_path);
6724 unlockerr = lock_worktree(worktree, LOCK_SH);
6725 if (unlockerr && err == NULL)
6726 err = unlockerr;
6727 return err;
6730 const struct got_error *
6731 got_worktree_histedit_complete(struct got_worktree *worktree,
6732 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6733 struct got_reference *edited_branch, struct got_repository *repo)
6735 const struct got_error *err, *unlockerr;
6736 struct got_object_id *new_head_commit_id = NULL;
6737 struct got_reference *resolved = NULL;
6739 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6740 if (err)
6741 return err;
6743 err = got_ref_open(&resolved, repo,
6744 got_ref_get_symref_target(edited_branch), 0);
6745 if (err)
6746 goto done;
6748 err = got_ref_change_ref(resolved, new_head_commit_id);
6749 if (err)
6750 goto done;
6752 err = got_ref_write(resolved, repo);
6753 if (err)
6754 goto done;
6756 err = got_worktree_set_head_ref(worktree, resolved);
6757 if (err)
6758 goto done;
6760 err = delete_histedit_refs(worktree, repo);
6761 done:
6762 if (fileindex)
6763 got_fileindex_free(fileindex);
6764 free(new_head_commit_id);
6765 unlockerr = lock_worktree(worktree, LOCK_SH);
6766 if (unlockerr && err == NULL)
6767 err = unlockerr;
6768 return err;
6771 const struct got_error *
6772 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6773 struct got_object_id *commit_id, struct got_repository *repo)
6775 const struct got_error *err;
6776 char *commit_ref_name;
6778 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6779 if (err)
6780 return err;
6782 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6783 if (err)
6784 goto done;
6786 err = delete_ref(commit_ref_name, repo);
6787 done:
6788 free(commit_ref_name);
6789 return err;
6792 const struct got_error *
6793 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6794 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6795 struct got_worktree *worktree, const char *refname,
6796 struct got_repository *repo)
6798 const struct got_error *err = NULL;
6799 char *fileindex_path = NULL;
6800 struct check_rebase_ok_arg ok_arg;
6802 *fileindex = NULL;
6803 *branch_ref = NULL;
6804 *base_branch_ref = NULL;
6806 err = lock_worktree(worktree, LOCK_EX);
6807 if (err)
6808 return err;
6810 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6811 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6812 "cannot integrate a branch into itself; "
6813 "update -b or different branch name required");
6814 goto done;
6817 err = open_fileindex(fileindex, &fileindex_path, worktree);
6818 if (err)
6819 goto done;
6821 /* Preconditions are the same as for rebase. */
6822 ok_arg.worktree = worktree;
6823 ok_arg.repo = repo;
6824 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6825 &ok_arg);
6826 if (err)
6827 goto done;
6829 err = got_ref_open(branch_ref, repo, refname, 1);
6830 if (err)
6831 goto done;
6833 err = got_ref_open(base_branch_ref, repo,
6834 got_worktree_get_head_ref_name(worktree), 1);
6835 done:
6836 if (err) {
6837 if (*branch_ref) {
6838 got_ref_close(*branch_ref);
6839 *branch_ref = NULL;
6841 if (*base_branch_ref) {
6842 got_ref_close(*base_branch_ref);
6843 *base_branch_ref = NULL;
6845 if (*fileindex) {
6846 got_fileindex_free(*fileindex);
6847 *fileindex = NULL;
6849 lock_worktree(worktree, LOCK_SH);
6851 return err;
6854 const struct got_error *
6855 got_worktree_integrate_continue(struct got_worktree *worktree,
6856 struct got_fileindex *fileindex, struct got_repository *repo,
6857 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6858 got_worktree_checkout_cb progress_cb, void *progress_arg,
6859 got_cancel_cb cancel_cb, void *cancel_arg)
6861 const struct got_error *err = NULL, *sync_err, *unlockerr;
6862 char *fileindex_path = NULL;
6863 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6865 err = get_fileindex_path(&fileindex_path, worktree);
6866 if (err)
6867 goto done;
6869 err = got_ref_resolve(&commit_id, repo, branch_ref);
6870 if (err)
6871 goto done;
6873 err = got_object_id_by_path(&tree_id, repo, commit_id,
6874 worktree->path_prefix);
6875 if (err)
6876 goto done;
6878 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6879 if (err)
6880 goto done;
6882 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6883 progress_cb, progress_arg, cancel_cb, cancel_arg);
6884 if (err)
6885 goto sync;
6887 err = got_ref_change_ref(base_branch_ref, commit_id);
6888 if (err)
6889 goto sync;
6891 err = got_ref_write(base_branch_ref, repo);
6892 sync:
6893 sync_err = sync_fileindex(fileindex, fileindex_path);
6894 if (sync_err && err == NULL)
6895 err = sync_err;
6897 done:
6898 unlockerr = got_ref_unlock(branch_ref);
6899 if (unlockerr && err == NULL)
6900 err = unlockerr;
6901 got_ref_close(branch_ref);
6903 unlockerr = got_ref_unlock(base_branch_ref);
6904 if (unlockerr && err == NULL)
6905 err = unlockerr;
6906 got_ref_close(base_branch_ref);
6908 got_fileindex_free(fileindex);
6909 free(fileindex_path);
6910 free(tree_id);
6912 unlockerr = lock_worktree(worktree, LOCK_SH);
6913 if (unlockerr && err == NULL)
6914 err = unlockerr;
6915 return err;
6918 const struct got_error *
6919 got_worktree_integrate_abort(struct got_worktree *worktree,
6920 struct got_fileindex *fileindex, struct got_repository *repo,
6921 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6923 const struct got_error *err = NULL, *unlockerr = NULL;
6925 got_fileindex_free(fileindex);
6927 err = lock_worktree(worktree, LOCK_SH);
6929 unlockerr = got_ref_unlock(branch_ref);
6930 if (unlockerr && err == NULL)
6931 err = unlockerr;
6932 got_ref_close(branch_ref);
6934 unlockerr = got_ref_unlock(base_branch_ref);
6935 if (unlockerr && err == NULL)
6936 err = unlockerr;
6937 got_ref_close(base_branch_ref);
6939 return err;
6942 struct check_stage_ok_arg {
6943 struct got_object_id *head_commit_id;
6944 struct got_worktree *worktree;
6945 struct got_fileindex *fileindex;
6946 struct got_repository *repo;
6947 int have_changes;
6950 const struct got_error *
6951 check_stage_ok(void *arg, unsigned char status,
6952 unsigned char staged_status, const char *relpath,
6953 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6954 struct got_object_id *commit_id, int dirfd, const char *de_name)
6956 struct check_stage_ok_arg *a = arg;
6957 const struct got_error *err = NULL;
6958 struct got_fileindex_entry *ie;
6959 struct got_object_id base_commit_id;
6960 struct got_object_id *base_commit_idp = NULL;
6961 char *in_repo_path = NULL, *p;
6963 if (status == GOT_STATUS_UNVERSIONED ||
6964 status == GOT_STATUS_NO_CHANGE)
6965 return NULL;
6966 if (status == GOT_STATUS_NONEXISTENT)
6967 return got_error_set_errno(ENOENT, relpath);
6969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6970 if (ie == NULL)
6971 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6973 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6974 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6975 relpath) == -1)
6976 return got_error_from_errno("asprintf");
6978 if (got_fileindex_entry_has_commit(ie)) {
6979 memcpy(base_commit_id.sha1, ie->commit_sha1,
6980 SHA1_DIGEST_LENGTH);
6981 base_commit_idp = &base_commit_id;
6984 if (status == GOT_STATUS_CONFLICT) {
6985 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6986 goto done;
6987 } else if (status != GOT_STATUS_ADD &&
6988 status != GOT_STATUS_MODIFY &&
6989 status != GOT_STATUS_DELETE) {
6990 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6991 goto done;
6994 a->have_changes = 1;
6996 p = in_repo_path;
6997 while (p[0] == '/')
6998 p++;
6999 err = check_out_of_date(p, status, staged_status,
7000 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7001 GOT_ERR_STAGE_OUT_OF_DATE);
7002 done:
7003 free(in_repo_path);
7004 return err;
7007 struct stage_path_arg {
7008 struct got_worktree *worktree;
7009 struct got_fileindex *fileindex;
7010 struct got_repository *repo;
7011 got_worktree_status_cb status_cb;
7012 void *status_arg;
7013 got_worktree_patch_cb patch_cb;
7014 void *patch_arg;
7015 int staged_something;
7016 int allow_bad_symlinks;
7019 static const struct got_error *
7020 stage_path(void *arg, unsigned char status,
7021 unsigned char staged_status, const char *relpath,
7022 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7023 struct got_object_id *commit_id, int dirfd, const char *de_name)
7025 struct stage_path_arg *a = arg;
7026 const struct got_error *err = NULL;
7027 struct got_fileindex_entry *ie;
7028 char *ondisk_path = NULL, *path_content = NULL;
7029 uint32_t stage;
7030 struct got_object_id *new_staged_blob_id = NULL;
7031 struct stat sb;
7033 if (status == GOT_STATUS_UNVERSIONED)
7034 return NULL;
7036 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7037 if (ie == NULL)
7038 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7040 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7041 relpath)== -1)
7042 return got_error_from_errno("asprintf");
7044 switch (status) {
7045 case GOT_STATUS_ADD:
7046 case GOT_STATUS_MODIFY:
7047 /* XXX could sb.st_mode be passed in by our caller? */
7048 if (lstat(ondisk_path, &sb) == -1) {
7049 err = got_error_from_errno2("lstat", ondisk_path);
7050 break;
7052 if (a->patch_cb) {
7053 if (status == GOT_STATUS_ADD) {
7054 int choice = GOT_PATCH_CHOICE_NONE;
7055 err = (*a->patch_cb)(&choice, a->patch_arg,
7056 status, ie->path, NULL, 1, 1);
7057 if (err)
7058 break;
7059 if (choice != GOT_PATCH_CHOICE_YES)
7060 break;
7061 } else {
7062 err = create_patched_content(&path_content, 0,
7063 staged_blob_id ? staged_blob_id : blob_id,
7064 ondisk_path, dirfd, de_name, ie->path,
7065 a->repo, a->patch_cb, a->patch_arg);
7066 if (err || path_content == NULL)
7067 break;
7070 err = got_object_blob_create(&new_staged_blob_id,
7071 path_content ? path_content : ondisk_path, a->repo);
7072 if (err)
7073 break;
7074 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7075 SHA1_DIGEST_LENGTH);
7076 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7077 stage = GOT_FILEIDX_STAGE_ADD;
7078 else
7079 stage = GOT_FILEIDX_STAGE_MODIFY;
7080 got_fileindex_entry_stage_set(ie, stage);
7081 if (S_ISLNK(sb.st_mode)) {
7082 int is_bad_symlink = 0;
7083 if (!a->allow_bad_symlinks) {
7084 char target_path[PATH_MAX];
7085 ssize_t target_len;
7086 target_len = readlink(ondisk_path, target_path,
7087 sizeof(target_path));
7088 if (target_len == -1) {
7089 err = got_error_from_errno2("readlink",
7090 ondisk_path);
7091 break;
7093 err = is_bad_symlink_target(&is_bad_symlink,
7094 target_path, target_len, ondisk_path,
7095 a->worktree->root_path);
7096 if (err)
7097 break;
7098 if (is_bad_symlink) {
7099 err = got_error_path(ondisk_path,
7100 GOT_ERR_BAD_SYMLINK);
7101 break;
7104 if (is_bad_symlink)
7105 got_fileindex_entry_staged_filetype_set(ie,
7106 GOT_FILEIDX_MODE_BAD_SYMLINK);
7107 else
7108 got_fileindex_entry_staged_filetype_set(ie,
7109 GOT_FILEIDX_MODE_SYMLINK);
7110 } else {
7111 got_fileindex_entry_staged_filetype_set(ie,
7112 GOT_FILEIDX_MODE_REGULAR_FILE);
7114 a->staged_something = 1;
7115 if (a->status_cb == NULL)
7116 break;
7117 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7118 get_staged_status(ie), relpath, blob_id,
7119 new_staged_blob_id, NULL, dirfd, de_name);
7120 break;
7121 case GOT_STATUS_DELETE:
7122 if (staged_status == GOT_STATUS_DELETE)
7123 break;
7124 if (a->patch_cb) {
7125 int choice = GOT_PATCH_CHOICE_NONE;
7126 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7127 ie->path, NULL, 1, 1);
7128 if (err)
7129 break;
7130 if (choice == GOT_PATCH_CHOICE_NO)
7131 break;
7132 if (choice != GOT_PATCH_CHOICE_YES) {
7133 err = got_error(GOT_ERR_PATCH_CHOICE);
7134 break;
7137 stage = GOT_FILEIDX_STAGE_DELETE;
7138 got_fileindex_entry_stage_set(ie, stage);
7139 a->staged_something = 1;
7140 if (a->status_cb == NULL)
7141 break;
7142 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7143 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7144 de_name);
7145 break;
7146 case GOT_STATUS_NO_CHANGE:
7147 break;
7148 case GOT_STATUS_CONFLICT:
7149 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7150 break;
7151 case GOT_STATUS_NONEXISTENT:
7152 err = got_error_set_errno(ENOENT, relpath);
7153 break;
7154 default:
7155 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7156 break;
7159 if (path_content && unlink(path_content) == -1 && err == NULL)
7160 err = got_error_from_errno2("unlink", path_content);
7161 free(path_content);
7162 free(ondisk_path);
7163 free(new_staged_blob_id);
7164 return err;
7167 const struct got_error *
7168 got_worktree_stage(struct got_worktree *worktree,
7169 struct got_pathlist_head *paths,
7170 got_worktree_status_cb status_cb, void *status_arg,
7171 got_worktree_patch_cb patch_cb, void *patch_arg,
7172 int allow_bad_symlinks, struct got_repository *repo)
7174 const struct got_error *err = NULL, *sync_err, *unlockerr;
7175 struct got_pathlist_entry *pe;
7176 struct got_fileindex *fileindex = NULL;
7177 char *fileindex_path = NULL;
7178 struct got_reference *head_ref = NULL;
7179 struct got_object_id *head_commit_id = NULL;
7180 struct check_stage_ok_arg oka;
7181 struct stage_path_arg spa;
7183 err = lock_worktree(worktree, LOCK_EX);
7184 if (err)
7185 return err;
7187 err = got_ref_open(&head_ref, repo,
7188 got_worktree_get_head_ref_name(worktree), 0);
7189 if (err)
7190 goto done;
7191 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7192 if (err)
7193 goto done;
7194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7195 if (err)
7196 goto done;
7198 /* Check pre-conditions before staging anything. */
7199 oka.head_commit_id = head_commit_id;
7200 oka.worktree = worktree;
7201 oka.fileindex = fileindex;
7202 oka.repo = repo;
7203 oka.have_changes = 0;
7204 TAILQ_FOREACH(pe, paths, entry) {
7205 err = worktree_status(worktree, pe->path, fileindex, repo,
7206 check_stage_ok, &oka, NULL, NULL, 0, 0);
7207 if (err)
7208 goto done;
7210 if (!oka.have_changes) {
7211 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7212 goto done;
7215 spa.worktree = worktree;
7216 spa.fileindex = fileindex;
7217 spa.repo = repo;
7218 spa.patch_cb = patch_cb;
7219 spa.patch_arg = patch_arg;
7220 spa.status_cb = status_cb;
7221 spa.status_arg = status_arg;
7222 spa.staged_something = 0;
7223 spa.allow_bad_symlinks = allow_bad_symlinks;
7224 TAILQ_FOREACH(pe, paths, entry) {
7225 err = worktree_status(worktree, pe->path, fileindex, repo,
7226 stage_path, &spa, NULL, NULL, 0, 0);
7227 if (err)
7228 goto done;
7230 if (!spa.staged_something) {
7231 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7232 goto done;
7235 sync_err = sync_fileindex(fileindex, fileindex_path);
7236 if (sync_err && err == NULL)
7237 err = sync_err;
7238 done:
7239 if (head_ref)
7240 got_ref_close(head_ref);
7241 free(head_commit_id);
7242 free(fileindex_path);
7243 if (fileindex)
7244 got_fileindex_free(fileindex);
7245 unlockerr = lock_worktree(worktree, LOCK_SH);
7246 if (unlockerr && err == NULL)
7247 err = unlockerr;
7248 return err;
7251 struct unstage_path_arg {
7252 struct got_worktree *worktree;
7253 struct got_fileindex *fileindex;
7254 struct got_repository *repo;
7255 got_worktree_checkout_cb progress_cb;
7256 void *progress_arg;
7257 got_worktree_patch_cb patch_cb;
7258 void *patch_arg;
7261 static const struct got_error *
7262 create_unstaged_content(char **path_unstaged_content,
7263 char **path_new_staged_content, struct got_object_id *blob_id,
7264 struct got_object_id *staged_blob_id, const char *relpath,
7265 struct got_repository *repo,
7266 got_worktree_patch_cb patch_cb, void *patch_arg)
7268 const struct got_error *err;
7269 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7270 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7271 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7272 struct stat sb1, sb2;
7273 struct got_diff_changes *changes = NULL;
7274 struct got_diff_state *ds = NULL;
7275 struct got_diff_args *args = NULL;
7276 struct got_diff_change *change;
7277 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7278 int have_content = 0, have_rejected_content = 0;
7280 *path_unstaged_content = NULL;
7281 *path_new_staged_content = NULL;
7283 err = got_object_id_str(&label1, blob_id);
7284 if (err)
7285 return err;
7286 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7287 if (err)
7288 goto done;
7290 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7291 if (err)
7292 goto done;
7294 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7295 if (err)
7296 goto done;
7298 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7299 if (err)
7300 goto done;
7302 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7303 if (err)
7304 goto done;
7306 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7307 if (err)
7308 goto done;
7310 if (stat(path1, &sb1) == -1) {
7311 err = got_error_from_errno2("stat", path1);
7312 goto done;
7315 if (stat(path2, &sb2) == -1) {
7316 err = got_error_from_errno2("stat", path2);
7317 goto done;
7320 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7321 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7322 if (err)
7323 goto done;
7325 err = got_opentemp_named(path_unstaged_content, &outfile,
7326 "got-unstaged-content");
7327 if (err)
7328 goto done;
7329 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7330 "got-new-staged-content");
7331 if (err)
7332 goto done;
7334 if (fseek(f1, 0L, SEEK_SET) == -1) {
7335 err = got_ferror(f1, GOT_ERR_IO);
7336 goto done;
7338 if (fseek(f2, 0L, SEEK_SET) == -1) {
7339 err = got_ferror(f2, GOT_ERR_IO);
7340 goto done;
7342 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7343 int choice;
7344 err = apply_or_reject_change(&choice, change, ++n,
7345 changes->nchanges, ds, args, diff_flags, relpath,
7346 f1, f2, &line_cur1, &line_cur2,
7347 outfile, rejectfile, patch_cb, patch_arg);
7348 if (err)
7349 goto done;
7350 if (choice == GOT_PATCH_CHOICE_YES)
7351 have_content = 1;
7352 else
7353 have_rejected_content = 1;
7354 if (choice == GOT_PATCH_CHOICE_QUIT)
7355 break;
7357 if (have_content || have_rejected_content)
7358 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7359 outfile, rejectfile);
7360 done:
7361 free(label1);
7362 if (blob)
7363 got_object_blob_close(blob);
7364 if (staged_blob)
7365 got_object_blob_close(staged_blob);
7366 if (f1 && fclose(f1) == EOF && err == NULL)
7367 err = got_error_from_errno2("fclose", path1);
7368 if (f2 && fclose(f2) == EOF && err == NULL)
7369 err = got_error_from_errno2("fclose", path2);
7370 if (outfile && fclose(outfile) == EOF && err == NULL)
7371 err = got_error_from_errno2("fclose", *path_unstaged_content);
7372 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7373 err = got_error_from_errno2("fclose", *path_new_staged_content);
7374 if (path1 && unlink(path1) == -1 && err == NULL)
7375 err = got_error_from_errno2("unlink", path1);
7376 if (path2 && unlink(path2) == -1 && err == NULL)
7377 err = got_error_from_errno2("unlink", path2);
7378 if (err || !have_content) {
7379 if (*path_unstaged_content &&
7380 unlink(*path_unstaged_content) == -1 && err == NULL)
7381 err = got_error_from_errno2("unlink",
7382 *path_unstaged_content);
7383 free(*path_unstaged_content);
7384 *path_unstaged_content = NULL;
7386 if (err || !have_content || !have_rejected_content) {
7387 if (*path_new_staged_content &&
7388 unlink(*path_new_staged_content) == -1 && err == NULL)
7389 err = got_error_from_errno2("unlink",
7390 *path_new_staged_content);
7391 free(*path_new_staged_content);
7392 *path_new_staged_content = NULL;
7394 free(args);
7395 if (ds) {
7396 got_diff_state_free(ds);
7397 free(ds);
7399 if (changes)
7400 got_diff_free_changes(changes);
7401 free(path1);
7402 free(path2);
7403 return err;
7406 static const struct got_error *
7407 unstage_hunks(struct got_object_id *staged_blob_id,
7408 struct got_blob_object *blob_base,
7409 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7410 const char *ondisk_path, const char *label_orig,
7411 struct got_worktree *worktree, struct got_repository *repo,
7412 got_worktree_patch_cb patch_cb, void *patch_arg,
7413 got_worktree_checkout_cb progress_cb, void *progress_arg)
7415 const struct got_error *err = NULL;
7416 char *path_unstaged_content = NULL;
7417 char *path_new_staged_content = NULL;
7418 struct got_object_id *new_staged_blob_id = NULL;
7419 FILE *f = NULL;
7420 struct stat sb;
7422 err = create_unstaged_content(&path_unstaged_content,
7423 &path_new_staged_content, blob_id, staged_blob_id,
7424 ie->path, repo, patch_cb, patch_arg);
7425 if (err)
7426 return err;
7428 if (path_unstaged_content == NULL)
7429 return NULL;
7431 if (path_new_staged_content) {
7432 err = got_object_blob_create(&new_staged_blob_id,
7433 path_new_staged_content, repo);
7434 if (err)
7435 goto done;
7438 f = fopen(path_unstaged_content, "r");
7439 if (f == NULL) {
7440 err = got_error_from_errno2("fopen",
7441 path_unstaged_content);
7442 goto done;
7444 if (fstat(fileno(f), &sb) == -1) {
7445 err = got_error_from_errno2("fstat", path_unstaged_content);
7446 goto done;
7448 if (got_fileindex_entry_staged_filetype_get(ie) ==
7449 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7450 char link_target[PATH_MAX];
7451 size_t r;
7452 r = fread(link_target, 1, sizeof(link_target), f);
7453 if (r == 0 && ferror(f)) {
7454 err = got_error_from_errno("fread");
7455 goto done;
7457 if (r >= sizeof(link_target)) { /* should not happen */
7458 err = got_error(GOT_ERR_NO_SPACE);
7459 goto done;
7461 link_target[r] = '\0';
7462 err = merge_symlink(worktree, blob_base,
7463 ondisk_path, ie->path, label_orig, link_target,
7464 worktree->base_commit_id, repo, progress_cb,
7465 progress_arg);
7466 } else {
7467 int local_changes_subsumed;
7468 err = merge_file(&local_changes_subsumed, worktree,
7469 blob_base, ondisk_path, ie->path,
7470 got_fileindex_perms_to_st(ie),
7471 path_unstaged_content, label_orig, "unstaged",
7472 repo, progress_cb, progress_arg);
7474 if (err)
7475 goto done;
7477 if (new_staged_blob_id) {
7478 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7479 SHA1_DIGEST_LENGTH);
7480 } else
7481 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7482 done:
7483 free(new_staged_blob_id);
7484 if (path_unstaged_content &&
7485 unlink(path_unstaged_content) == -1 && err == NULL)
7486 err = got_error_from_errno2("unlink", path_unstaged_content);
7487 if (path_new_staged_content &&
7488 unlink(path_new_staged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink", path_new_staged_content);
7490 if (f && fclose(f) != 0 && err == NULL)
7491 err = got_error_from_errno2("fclose", path_unstaged_content);
7492 free(path_unstaged_content);
7493 free(path_new_staged_content);
7494 return err;
7497 static const struct got_error *
7498 unstage_path(void *arg, unsigned char status,
7499 unsigned char staged_status, const char *relpath,
7500 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7501 struct got_object_id *commit_id, int dirfd, const char *de_name)
7503 const struct got_error *err = NULL;
7504 struct unstage_path_arg *a = arg;
7505 struct got_fileindex_entry *ie;
7506 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7507 char *ondisk_path = NULL;
7508 char *id_str = NULL, *label_orig = NULL;
7509 int local_changes_subsumed;
7510 struct stat sb;
7512 if (staged_status != GOT_STATUS_ADD &&
7513 staged_status != GOT_STATUS_MODIFY &&
7514 staged_status != GOT_STATUS_DELETE)
7515 return NULL;
7517 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7518 if (ie == NULL)
7519 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7521 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7522 == -1)
7523 return got_error_from_errno("asprintf");
7525 err = got_object_id_str(&id_str,
7526 commit_id ? commit_id : a->worktree->base_commit_id);
7527 if (err)
7528 goto done;
7529 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7530 id_str) == -1) {
7531 err = got_error_from_errno("asprintf");
7532 goto done;
7535 switch (staged_status) {
7536 case GOT_STATUS_MODIFY:
7537 err = got_object_open_as_blob(&blob_base, a->repo,
7538 blob_id, 8192);
7539 if (err)
7540 break;
7541 /* fall through */
7542 case GOT_STATUS_ADD:
7543 if (a->patch_cb) {
7544 if (staged_status == GOT_STATUS_ADD) {
7545 int choice = GOT_PATCH_CHOICE_NONE;
7546 err = (*a->patch_cb)(&choice, a->patch_arg,
7547 staged_status, ie->path, NULL, 1, 1);
7548 if (err)
7549 break;
7550 if (choice != GOT_PATCH_CHOICE_YES)
7551 break;
7552 } else {
7553 err = unstage_hunks(staged_blob_id,
7554 blob_base, blob_id, ie, ondisk_path,
7555 label_orig, a->worktree, a->repo,
7556 a->patch_cb, a->patch_arg,
7557 a->progress_cb, a->progress_arg);
7558 break; /* Done with this file. */
7561 err = got_object_open_as_blob(&blob_staged, a->repo,
7562 staged_blob_id, 8192);
7563 if (err)
7564 break;
7565 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7566 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7567 case GOT_FILEIDX_MODE_REGULAR_FILE:
7568 err = merge_blob(&local_changes_subsumed, a->worktree,
7569 blob_base, ondisk_path, relpath,
7570 got_fileindex_perms_to_st(ie), label_orig,
7571 blob_staged, commit_id ? commit_id :
7572 a->worktree->base_commit_id, a->repo,
7573 a->progress_cb, a->progress_arg);
7574 break;
7575 case GOT_FILEIDX_MODE_SYMLINK:
7576 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7577 char *staged_target;
7578 err = got_object_blob_read_to_str(
7579 &staged_target, blob_staged);
7580 if (err)
7581 goto done;
7582 err = merge_symlink(a->worktree, blob_base,
7583 ondisk_path, relpath, label_orig,
7584 staged_target, commit_id ? commit_id :
7585 a->worktree->base_commit_id,
7586 a->repo, a->progress_cb, a->progress_arg);
7587 free(staged_target);
7588 } else {
7589 err = merge_blob(&local_changes_subsumed,
7590 a->worktree, blob_base, ondisk_path,
7591 relpath, got_fileindex_perms_to_st(ie),
7592 label_orig, blob_staged,
7593 commit_id ? commit_id :
7594 a->worktree->base_commit_id, a->repo,
7595 a->progress_cb, a->progress_arg);
7597 break;
7598 default:
7599 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7600 break;
7602 if (err == NULL)
7603 got_fileindex_entry_stage_set(ie,
7604 GOT_FILEIDX_STAGE_NONE);
7605 break;
7606 case GOT_STATUS_DELETE:
7607 if (a->patch_cb) {
7608 int choice = GOT_PATCH_CHOICE_NONE;
7609 err = (*a->patch_cb)(&choice, a->patch_arg,
7610 staged_status, ie->path, NULL, 1, 1);
7611 if (err)
7612 break;
7613 if (choice == GOT_PATCH_CHOICE_NO)
7614 break;
7615 if (choice != GOT_PATCH_CHOICE_YES) {
7616 err = got_error(GOT_ERR_PATCH_CHOICE);
7617 break;
7620 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7621 err = get_file_status(&status, &sb, ie, ondisk_path,
7622 dirfd, de_name, a->repo);
7623 if (err)
7624 break;
7625 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7626 break;
7628 done:
7629 free(ondisk_path);
7630 if (blob_base)
7631 got_object_blob_close(blob_base);
7632 if (blob_staged)
7633 got_object_blob_close(blob_staged);
7634 free(id_str);
7635 free(label_orig);
7636 return err;
7639 const struct got_error *
7640 got_worktree_unstage(struct got_worktree *worktree,
7641 struct got_pathlist_head *paths,
7642 got_worktree_checkout_cb progress_cb, void *progress_arg,
7643 got_worktree_patch_cb patch_cb, void *patch_arg,
7644 struct got_repository *repo)
7646 const struct got_error *err = NULL, *sync_err, *unlockerr;
7647 struct got_pathlist_entry *pe;
7648 struct got_fileindex *fileindex = NULL;
7649 char *fileindex_path = NULL;
7650 struct unstage_path_arg upa;
7652 err = lock_worktree(worktree, LOCK_EX);
7653 if (err)
7654 return err;
7656 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7657 if (err)
7658 goto done;
7660 upa.worktree = worktree;
7661 upa.fileindex = fileindex;
7662 upa.repo = repo;
7663 upa.progress_cb = progress_cb;
7664 upa.progress_arg = progress_arg;
7665 upa.patch_cb = patch_cb;
7666 upa.patch_arg = patch_arg;
7667 TAILQ_FOREACH(pe, paths, entry) {
7668 err = worktree_status(worktree, pe->path, fileindex, repo,
7669 unstage_path, &upa, NULL, NULL, 0, 0);
7670 if (err)
7671 goto done;
7674 sync_err = sync_fileindex(fileindex, fileindex_path);
7675 if (sync_err && err == NULL)
7676 err = sync_err;
7677 done:
7678 free(fileindex_path);
7679 if (fileindex)
7680 got_fileindex_free(fileindex);
7681 unlockerr = lock_worktree(worktree, LOCK_SH);
7682 if (unlockerr && err == NULL)
7683 err = unlockerr;
7684 return err;
7687 struct report_file_info_arg {
7688 struct got_worktree *worktree;
7689 got_worktree_path_info_cb info_cb;
7690 void *info_arg;
7691 struct got_pathlist_head *paths;
7692 got_cancel_cb cancel_cb;
7693 void *cancel_arg;
7696 static const struct got_error *
7697 report_file_info(void *arg, struct got_fileindex_entry *ie)
7699 struct report_file_info_arg *a = arg;
7700 struct got_pathlist_entry *pe;
7701 struct got_object_id blob_id, staged_blob_id, commit_id;
7702 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7703 struct got_object_id *commit_idp = NULL;
7704 int stage;
7706 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7707 return got_error(GOT_ERR_CANCELLED);
7709 TAILQ_FOREACH(pe, a->paths, entry) {
7710 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7711 got_path_is_child(ie->path, pe->path, pe->path_len))
7712 break;
7714 if (pe == NULL) /* not found */
7715 return NULL;
7717 if (got_fileindex_entry_has_blob(ie)) {
7718 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7719 blob_idp = &blob_id;
7721 stage = got_fileindex_entry_stage_get(ie);
7722 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7723 stage == GOT_FILEIDX_STAGE_ADD) {
7724 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7725 SHA1_DIGEST_LENGTH);
7726 staged_blob_idp = &staged_blob_id;
7729 if (got_fileindex_entry_has_commit(ie)) {
7730 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7731 commit_idp = &commit_id;
7734 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7735 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7738 const struct got_error *
7739 got_worktree_path_info(struct got_worktree *worktree,
7740 struct got_pathlist_head *paths,
7741 got_worktree_path_info_cb info_cb, void *info_arg,
7742 got_cancel_cb cancel_cb, void *cancel_arg)
7745 const struct got_error *err = NULL, *unlockerr;
7746 struct got_fileindex *fileindex = NULL;
7747 char *fileindex_path = NULL;
7748 struct report_file_info_arg arg;
7750 err = lock_worktree(worktree, LOCK_SH);
7751 if (err)
7752 return err;
7754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7755 if (err)
7756 goto done;
7758 arg.worktree = worktree;
7759 arg.info_cb = info_cb;
7760 arg.info_arg = info_arg;
7761 arg.paths = paths;
7762 arg.cancel_cb = cancel_cb;
7763 arg.cancel_arg = cancel_arg;
7764 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7765 &arg);
7766 done:
7767 free(fileindex_path);
7768 if (fileindex)
7769 got_fileindex_free(fileindex);
7770 unlockerr = lock_worktree(worktree, LOCK_UN);
7771 if (unlockerr && err == NULL)
7772 err = unlockerr;
7773 return err;