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: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
942 goto done;
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
947 goto done;
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
951 goto done;
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
955 goto done;
957 done:
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
960 free(path);
961 free(id_str);
962 free(label_deriv);
963 return err;
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
973 /*
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, blob_deriv acts as the first derived version, and the symlink
976 * on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
978 */
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, uint16_t st_mode, const char *label_orig,
983 struct got_blob_object *blob_deriv,
984 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 got_worktree_checkout_cb progress_cb, void *progress_arg)
987 const struct got_error *err = NULL;
988 char *ancestor_target = NULL, *deriv_target = NULL;
989 struct stat sb;
990 ssize_t ondisk_len, deriv_len;
991 char ondisk_target[PATH_MAX];
992 int have_local_change = 0;
993 int have_incoming_change = 0;
995 if (lstat(ondisk_path, &sb) == -1)
996 return got_error_from_errno2("lstat", ondisk_path);
998 if (!S_ISLNK(sb.st_mode)) {
999 /*
1000 * If there is a regular file on disk, merge the symlink
1001 * target path into this file, which will usually cause
1002 * a merge conflict.
1004 if (S_ISREG(sb.st_mode)) {
1005 int local_changes_subsumed;
1006 return merge_blob(&local_changes_subsumed, worktree,
1007 NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 blob_deriv, deriv_base_commit_id,
1009 repo, progress_cb, progress_arg);
1012 /* TODO symlink is obstructed; do something */
1013 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1016 ondisk_len = readlink(ondisk_path, ondisk_target,
1017 sizeof(ondisk_target));
1018 if (ondisk_len == -1) {
1019 err = got_error_from_errno2("readlink",
1020 ondisk_path);
1021 goto done;
1023 ondisk_target[ondisk_len] = '\0';
1025 if (blob_orig) {
1026 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 if (err)
1028 goto done;
1031 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1032 if (err)
1033 goto done;
1035 if (ancestor_target == NULL ||
1036 (ondisk_len != strlen(ancestor_target) ||
1037 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 have_local_change = 1;
1040 deriv_len = strlen(deriv_target);
1041 if (ancestor_target == NULL ||
1042 (deriv_len != strlen(ancestor_target) ||
1043 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 have_incoming_change = 1;
1046 if (!have_local_change && !have_incoming_change) {
1047 if (ancestor_target) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 path);
1051 } else if (deriv_len == ondisk_len &&
1052 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 /* Both sides added the same symlink. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 path);
1056 } else {
1057 /* Both sides added symlinks which don't match. */
1058 err = install_symlink_conflict(deriv_target,
1059 deriv_base_commit_id, ancestor_target,
1060 label_orig, ondisk_target, ondisk_path);
1061 if (err)
1062 goto done;
1063 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1064 path);
1066 } else if (!have_local_change && have_incoming_change) {
1067 /* Apply the incoming change. */
1068 err = update_symlink(ondisk_path, deriv_target,
1069 strlen(deriv_target));
1070 if (err)
1071 goto done;
1072 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 } else if (have_local_change && have_incoming_change) {
1074 if (deriv_len == ondisk_len &&
1075 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1076 /* Both sides made the same change. */
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 path);
1079 } else {
1080 err = install_symlink_conflict(deriv_target,
1081 deriv_base_commit_id, ancestor_target, label_orig,
1082 ondisk_target, ondisk_path);
1083 if (err)
1084 goto done;
1085 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1086 path);
1090 done:
1091 free(ancestor_target);
1092 free(deriv_target);
1093 return err;
1097 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1098 * blob_deriv acts as the first derived version, and the file on disk
1099 * acts as the second derived version.
1101 static const struct got_error *
1102 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1103 struct got_blob_object *blob_orig, const char *ondisk_path,
1104 const char *path, uint16_t st_mode, const char *label_orig,
1105 struct got_blob_object *blob_deriv,
1106 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1107 got_worktree_checkout_cb progress_cb, void *progress_arg)
1109 const struct got_error *err = NULL;
1110 FILE *f_deriv = NULL;
1111 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1112 char *label_deriv = NULL, *parent;
1114 *local_changes_subsumed = 0;
1116 parent = dirname(ondisk_path);
1117 if (parent == NULL)
1118 return got_error_from_errno2("dirname", ondisk_path);
1120 free(base_path);
1121 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1122 err = got_error_from_errno("asprintf");
1123 base_path = NULL;
1124 goto done;
1127 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1128 if (err)
1129 goto done;
1130 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1131 blob_deriv);
1132 if (err)
1133 goto done;
1135 err = got_object_id_str(&id_str, deriv_base_commit_id);
1136 if (err)
1137 goto done;
1138 if (asprintf(&label_deriv, "%s: commit %s",
1139 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1140 err = got_error_from_errno("asprintf");
1141 goto done;
1144 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1145 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1146 label_deriv, repo, progress_cb, progress_arg);
1147 done:
1148 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1149 err = got_error_from_errno("fclose");
1150 free(base_path);
1151 if (blob_deriv_path) {
1152 unlink(blob_deriv_path);
1153 free(blob_deriv_path);
1155 free(id_str);
1156 free(label_deriv);
1157 return err;
1160 static const struct got_error *
1161 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1162 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1163 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1165 const struct got_error *err = NULL;
1166 struct got_fileindex_entry *new_ie;
1168 *new_iep = NULL;
1170 err = got_fileindex_entry_alloc(&new_ie, path);
1171 if (err)
1172 return err;
1174 err = got_fileindex_entry_update(new_ie, ondisk_path,
1175 blob_id->sha1, base_commit_id->sha1, 1);
1176 if (err)
1177 goto done;
1179 err = got_fileindex_entry_add(fileindex, new_ie);
1180 done:
1181 if (err)
1182 got_fileindex_entry_free(new_ie);
1183 else
1184 *new_iep = new_ie;
1185 return err;
1188 static mode_t
1189 get_ondisk_perms(int executable, mode_t st_mode)
1191 mode_t xbits = S_IXUSR;
1193 if (executable) {
1194 /* Map read bits to execute bits. */
1195 if (st_mode & S_IRGRP)
1196 xbits |= S_IXGRP;
1197 if (st_mode & S_IROTH)
1198 xbits |= S_IXOTH;
1199 return st_mode | xbits;
1202 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1205 /* forward declaration */
1206 static const struct got_error *
1207 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1208 const char *path, mode_t te_mode, mode_t st_mode,
1209 struct got_blob_object *blob, int restoring_missing_file,
1210 int reverting_versioned_file, int installing_bad_symlink,
1211 int path_is_unversioned, struct got_repository *repo,
1212 got_worktree_checkout_cb progress_cb, void *progress_arg);
1215 * This function assumes that the provided symlink target points at a
1216 * safe location in the work tree!
1218 static const struct got_error *
1219 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1220 size_t target_len)
1222 const struct got_error *err = NULL;
1223 ssize_t elen;
1224 char etarget[PATH_MAX];
1225 int fd;
1228 * "Bad" symlinks (those pointing outside the work tree or into the
1229 * .got directory) are installed in the work tree as a regular file
1230 * which contains the bad symlink target path.
1231 * The new symlink target has already been checked for safety by our
1232 * caller. If we can successfully open a regular file then we simply
1233 * replace this file with a symlink below.
1235 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1236 if (fd == -1) {
1237 if (errno != ELOOP)
1238 return got_error_from_errno2("open", ondisk_path);
1240 /* We are updating an existing on-disk symlink. */
1241 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1242 if (elen == -1)
1243 return got_error_from_errno2("readlink", ondisk_path);
1245 if (elen == target_len &&
1246 memcmp(etarget, target_path, target_len) == 0)
1247 return NULL; /* nothing to do */
1250 err = update_symlink(ondisk_path, target_path, target_len);
1251 if (fd != -1 && close(fd) == -1 && err == NULL)
1252 err = got_error_from_errno2("close", ondisk_path);
1253 return err;
1256 static const struct got_error *
1257 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1258 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1259 int restoring_missing_file, int reverting_versioned_file,
1260 int path_is_unversioned, struct got_repository *repo,
1261 got_worktree_checkout_cb progress_cb, void *progress_arg)
1263 const struct got_error *err = NULL;
1264 char target_path[PATH_MAX];
1265 char canonpath[PATH_MAX];
1266 size_t len, target_len = 0;
1267 char *path_got = NULL;
1268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 *is_bad_symlink = 0;
1274 * Blob object content specifies the target path of the link.
1275 * If a symbolic link cannot be installed we instead create
1276 * a regular file which contains the link target path stored
1277 * in the blob object.
1279 do {
1280 err = got_object_blob_read_block(&len, blob);
1281 if (len + target_len >= sizeof(target_path)) {
1282 /* Path too long; install as a regular file. */
1283 *is_bad_symlink = 1;
1284 got_object_blob_rewind(blob);
1285 return install_blob(worktree, ondisk_path, path,
1286 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1287 restoring_missing_file, reverting_versioned_file,
1288 1, path_is_unversioned, repo, progress_cb,
1289 progress_arg);
1291 if (len > 0) {
1292 /* Skip blob object header first time around. */
1293 memcpy(target_path + target_len, buf + hdrlen,
1294 len - hdrlen);
1295 target_len += len - hdrlen;
1296 hdrlen = 0;
1298 } while (len != 0);
1299 target_path[target_len] = '\0';
1302 * We do not use realpath(3) to resolve the symlink's target path
1303 * because we don't want to resolve symlinks recursively. Instead
1304 * we make the path absolute and then canonicalize it.
1305 * Relative symlink target lookup should begin at the directory
1306 * in which the blob object is being installed.
1308 if (!got_path_is_absolute(target_path)) {
1309 char *abspath;
1310 char *parent = dirname(ondisk_path);
1311 if (parent == NULL) {
1312 err = got_error_from_errno2("dirname", ondisk_path);
1313 goto done;
1315 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1316 err = got_error_from_errno("asprintf");
1317 goto done;
1319 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1320 free(abspath);
1321 if (err)
1322 goto done;
1323 } else {
1324 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1325 if (err)
1326 goto done;
1329 /* Only allow symlinks pointing at paths within the work tree. */
1330 if (!got_path_is_child(canonpath, worktree->root_path,
1331 strlen(worktree->root_path))) {
1332 /* install as a regular file */
1333 *is_bad_symlink = 1;
1334 got_object_blob_rewind(blob);
1335 err = install_blob(worktree, ondisk_path, path,
1336 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1337 restoring_missing_file, reverting_versioned_file, 1,
1338 path_is_unversioned, repo, progress_cb, progress_arg);
1339 goto done;
1342 /* Do not allow symlinks pointing into the .got directory. */
1343 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1344 GOT_WORKTREE_GOT_DIR) == -1) {
1345 err = got_error_from_errno("asprintf");
1346 goto done;
1348 if (got_path_is_child(canonpath, path_got, strlen(path_got))) {
1349 /* install as a regular file */
1350 *is_bad_symlink = 1;
1351 got_object_blob_rewind(blob);
1352 err = install_blob(worktree, ondisk_path, path,
1353 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1354 restoring_missing_file, reverting_versioned_file, 1,
1355 path_is_unversioned, repo, progress_cb, progress_arg);
1356 goto done;
1359 if (symlink(target_path, ondisk_path) == -1) {
1360 if (errno == EEXIST) {
1361 if (path_is_unversioned) {
1362 err = (*progress_cb)(progress_arg,
1363 GOT_STATUS_UNVERSIONED, path);
1364 goto done;
1366 err = replace_existing_symlink(ondisk_path,
1367 target_path, target_len);
1368 if (err)
1369 goto done;
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1374 path);
1376 goto done; /* Nothing else to do. */
1379 if (errno == ENOENT) {
1380 char *parent = dirname(ondisk_path);
1381 if (parent == NULL) {
1382 err = got_error_from_errno2("dirname",
1383 ondisk_path);
1384 goto done;
1386 err = add_dir_on_disk(worktree, parent);
1387 if (err)
1388 goto done;
1390 * Retry, and fall through to error handling
1391 * below if this second attempt fails.
1393 if (symlink(target_path, ondisk_path) != -1) {
1394 err = NULL; /* success */
1395 goto done;
1399 /* Handle errors from first or second creation attempt. */
1400 if (errno == ENAMETOOLONG) {
1401 /* bad target path; install as a regular file */
1402 *is_bad_symlink = 1;
1403 got_object_blob_rewind(blob);
1404 err = install_blob(worktree, ondisk_path, path,
1405 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1406 restoring_missing_file, reverting_versioned_file, 1,
1407 path_is_unversioned, repo,
1408 progress_cb, progress_arg);
1409 } else if (errno == ENOTDIR) {
1410 err = got_error_path(ondisk_path,
1411 GOT_ERR_FILE_OBSTRUCTED);
1412 } else {
1413 err = got_error_from_errno3("symlink",
1414 target_path, ondisk_path);
1416 } else if (progress_cb)
1417 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1419 done:
1420 free(path_got);
1421 return err;
1424 static const struct got_error *
1425 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1426 const char *path, mode_t te_mode, mode_t st_mode,
1427 struct got_blob_object *blob, int restoring_missing_file,
1428 int reverting_versioned_file, int installing_bad_symlink,
1429 int path_is_unversioned, struct got_repository *repo,
1430 got_worktree_checkout_cb progress_cb, void *progress_arg)
1432 const struct got_error *err = NULL;
1433 int fd = -1;
1434 size_t len, hdrlen;
1435 int update = 0;
1436 char *tmppath = NULL;
1438 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1440 if (fd == -1) {
1441 if (errno == ENOENT) {
1442 char *parent = dirname(path);
1443 if (parent == NULL)
1444 return got_error_from_errno2("dirname", path);
1445 err = add_dir_on_disk(worktree, parent);
1446 if (err)
1447 return err;
1448 fd = open(ondisk_path,
1449 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1450 GOT_DEFAULT_FILE_MODE);
1451 if (fd == -1)
1452 return got_error_from_errno2("open",
1453 ondisk_path);
1454 } else if (errno == EEXIST) {
1455 if (path_is_unversioned) {
1456 err = (*progress_cb)(progress_arg,
1457 GOT_STATUS_UNVERSIONED, path);
1458 goto done;
1460 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1461 /* TODO file is obstructed; do something */
1462 err = got_error_path(ondisk_path,
1463 GOT_ERR_FILE_OBSTRUCTED);
1464 goto done;
1465 } else {
1466 err = got_opentemp_named_fd(&tmppath, &fd,
1467 ondisk_path);
1468 if (err)
1469 goto done;
1470 update = 1;
1472 } else
1473 return got_error_from_errno2("open", ondisk_path);
1476 if (progress_cb) {
1477 if (restoring_missing_file)
1478 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1479 path);
1480 else if (reverting_versioned_file)
1481 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 path);
1483 else
1484 err = (*progress_cb)(progress_arg,
1485 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1486 if (err)
1487 goto done;
1490 hdrlen = got_object_blob_get_hdrlen(blob);
1491 do {
1492 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1493 err = got_object_blob_read_block(&len, blob);
1494 if (err)
1495 break;
1496 if (len > 0) {
1497 /* Skip blob object header first time around. */
1498 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1499 if (outlen == -1) {
1500 err = got_error_from_errno("write");
1501 goto done;
1502 } else if (outlen != len - hdrlen) {
1503 err = got_error(GOT_ERR_IO);
1504 goto done;
1506 hdrlen = 0;
1508 } while (len != 0);
1510 if (fsync(fd) != 0) {
1511 err = got_error_from_errno("fsync");
1512 goto done;
1515 if (update) {
1516 if (rename(tmppath, ondisk_path) != 0) {
1517 err = got_error_from_errno3("rename", tmppath,
1518 ondisk_path);
1519 unlink(tmppath);
1520 goto done;
1524 if (chmod(ondisk_path,
1525 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1526 err = got_error_from_errno2("chmod", ondisk_path);
1527 goto done;
1530 done:
1531 if (fd != -1 && close(fd) != 0 && err == NULL)
1532 err = got_error_from_errno("close");
1533 free(tmppath);
1534 return err;
1537 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1538 static const struct got_error *
1539 get_modified_file_content_status(unsigned char *status, FILE *f)
1541 const struct got_error *err = NULL;
1542 const char *markers[3] = {
1543 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1544 GOT_DIFF_CONFLICT_MARKER_SEP,
1545 GOT_DIFF_CONFLICT_MARKER_END
1547 int i = 0;
1548 char *line;
1549 size_t len;
1550 const char delim[3] = {'\0', '\0', '\0'};
1552 while (*status == GOT_STATUS_MODIFY) {
1553 line = fparseln(f, &len, NULL, delim, 0);
1554 if (line == NULL) {
1555 if (feof(f))
1556 break;
1557 err = got_ferror(f, GOT_ERR_IO);
1558 break;
1561 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1562 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1563 == 0)
1564 *status = GOT_STATUS_CONFLICT;
1565 else
1566 i++;
1570 return err;
1573 static int
1574 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1576 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1577 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1580 static int
1581 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1583 return !(ie->ctime_sec == sb->st_ctime &&
1584 ie->ctime_nsec == sb->st_ctimensec &&
1585 ie->mtime_sec == sb->st_mtime &&
1586 ie->mtime_nsec == sb->st_mtimensec &&
1587 ie->size == (sb->st_size & 0xffffffff) &&
1588 !xbit_differs(ie, sb->st_mode));
1591 static unsigned char
1592 get_staged_status(struct got_fileindex_entry *ie)
1594 switch (got_fileindex_entry_stage_get(ie)) {
1595 case GOT_FILEIDX_STAGE_ADD:
1596 return GOT_STATUS_ADD;
1597 case GOT_FILEIDX_STAGE_DELETE:
1598 return GOT_STATUS_DELETE;
1599 case GOT_FILEIDX_STAGE_MODIFY:
1600 return GOT_STATUS_MODIFY;
1601 default:
1602 return GOT_STATUS_NO_CHANGE;
1606 static const struct got_error *
1607 get_symlink_status(unsigned char *status, struct stat *sb,
1608 struct got_fileindex_entry *ie, const char *abspath,
1609 int dirfd, const char *de_name, struct got_blob_object *blob)
1611 const struct got_error *err = NULL;
1612 char target_path[PATH_MAX];
1613 char etarget[PATH_MAX];
1614 ssize_t elen;
1615 size_t len, target_len = 0;
1616 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1617 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1619 *status = GOT_STATUS_NO_CHANGE;
1621 /* Blob object content specifies the target path of the link. */
1622 do {
1623 err = got_object_blob_read_block(&len, blob);
1624 if (err)
1625 return err;
1626 if (len + target_len >= sizeof(target_path)) {
1628 * Should not happen. The blob contents were OK
1629 * when this symlink was installed.
1631 return got_error(GOT_ERR_NO_SPACE);
1633 if (len > 0) {
1634 /* Skip blob object header first time around. */
1635 memcpy(target_path + target_len, buf + hdrlen,
1636 len - hdrlen);
1637 target_len += len - hdrlen;
1638 hdrlen = 0;
1640 } while (len != 0);
1641 target_path[target_len] = '\0';
1643 if (dirfd != -1) {
1644 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1645 if (elen == -1)
1646 return got_error_from_errno2("readlinkat", abspath);
1647 } else {
1648 elen = readlink(abspath, etarget, sizeof(etarget));
1649 if (elen == -1)
1650 return got_error_from_errno2("readlink", abspath);
1653 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1654 *status = GOT_STATUS_MODIFY;
1656 return NULL;
1659 static const struct got_error *
1660 get_file_status(unsigned char *status, struct stat *sb,
1661 struct got_fileindex_entry *ie, const char *abspath,
1662 int dirfd, const char *de_name, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 struct got_object_id id;
1666 size_t hdrlen;
1667 int fd = -1;
1668 FILE *f = NULL;
1669 uint8_t fbuf[8192];
1670 struct got_blob_object *blob = NULL;
1671 size_t flen, blen;
1672 unsigned char staged_status = get_staged_status(ie);
1674 *status = GOT_STATUS_NO_CHANGE;
1677 * Whenever the caller provides a directory descriptor and a
1678 * directory entry name for the file, use them! This prevents
1679 * race conditions if filesystem paths change beneath our feet.
1681 if (dirfd != -1) {
1682 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1686 else
1687 *status = GOT_STATUS_DELETE;
1688 goto done;
1690 err = got_error_from_errno2("fstatat", abspath);
1691 goto done;
1693 } else {
1694 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1695 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1696 return got_error_from_errno2("open", abspath);
1697 else if (fd == -1 && errno == ELOOP) {
1698 if (lstat(abspath, sb) == -1)
1699 return got_error_from_errno2("lstat", abspath);
1700 } else if (fd == -1 || fstat(fd, sb) == -1) {
1701 if (errno == ENOENT) {
1702 if (got_fileindex_entry_has_file_on_disk(ie))
1703 *status = GOT_STATUS_MISSING;
1704 else
1705 *status = GOT_STATUS_DELETE;
1706 goto done;
1708 err = got_error_from_errno2("fstat", abspath);
1709 goto done;
1713 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1714 *status = GOT_STATUS_OBSTRUCTED;
1715 goto done;
1718 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1719 *status = GOT_STATUS_DELETE;
1720 goto done;
1721 } else if (!got_fileindex_entry_has_blob(ie) &&
1722 staged_status != GOT_STATUS_ADD) {
1723 *status = GOT_STATUS_ADD;
1724 goto done;
1727 if (!stat_info_differs(ie, sb))
1728 goto done;
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1733 else
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1737 if (err)
1738 goto done;
1740 if (S_ISLNK(sb->st_mode)) {
1741 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1742 de_name, blob);
1743 goto done;
1747 if (dirfd != -1) {
1748 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1749 if (fd == -1) {
1750 err = got_error_from_errno2("openat", abspath);
1751 goto done;
1755 f = fdopen(fd, "r");
1756 if (f == NULL) {
1757 err = got_error_from_errno2("fdopen", abspath);
1758 goto done;
1760 fd = -1;
1761 hdrlen = got_object_blob_get_hdrlen(blob);
1762 for (;;) {
1763 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1764 err = got_object_blob_read_block(&blen, blob);
1765 if (err)
1766 goto done;
1767 /* Skip length of blob object header first time around. */
1768 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1769 if (flen == 0 && ferror(f)) {
1770 err = got_error_from_errno("fread");
1771 goto done;
1773 if (blen == 0) {
1774 if (flen != 0)
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1777 } else if (flen == 0) {
1778 if (blen != 0)
1779 *status = GOT_STATUS_MODIFY;
1780 break;
1781 } else if (blen - hdrlen == flen) {
1782 /* Skip blob object header first time around. */
1783 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1784 *status = GOT_STATUS_MODIFY;
1785 break;
1787 } else {
1788 *status = GOT_STATUS_MODIFY;
1789 break;
1791 hdrlen = 0;
1794 if (*status == GOT_STATUS_MODIFY) {
1795 rewind(f);
1796 err = get_modified_file_content_status(status, f);
1797 } else if (xbit_differs(ie, sb->st_mode))
1798 *status = GOT_STATUS_MODE_CHANGE;
1799 done:
1800 if (blob)
1801 got_object_blob_close(blob);
1802 if (f != NULL && fclose(f) == EOF && err == NULL)
1803 err = got_error_from_errno2("fclose", abspath);
1804 if (fd != -1 && close(fd) == -1 && err == NULL)
1805 err = got_error_from_errno2("close", abspath);
1806 return err;
1810 * Update timestamps in the file index if a file is unmodified and
1811 * we had to run a full content comparison to find out.
1813 static const struct got_error *
1814 sync_timestamps(char *ondisk_path, unsigned char status,
1815 struct got_fileindex_entry *ie, struct stat *sb)
1817 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1818 return got_fileindex_entry_update(ie, ondisk_path,
1819 ie->blob_sha1, ie->commit_sha1, 1);
1821 return NULL;
1824 static const struct got_error *
1825 update_blob(struct got_worktree *worktree,
1826 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1827 struct got_tree_entry *te, const char *path,
1828 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1829 void *progress_arg)
1831 const struct got_error *err = NULL;
1832 struct got_blob_object *blob = NULL;
1833 char *ondisk_path;
1834 unsigned char status = GOT_STATUS_NO_CHANGE;
1835 struct stat sb;
1837 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1838 return got_error_from_errno("asprintf");
1840 if (ie) {
1841 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1842 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1843 goto done;
1845 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1846 repo);
1847 if (err)
1848 goto done;
1849 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1850 sb.st_mode = got_fileindex_perms_to_st(ie);
1851 } else {
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1856 if (status == GOT_STATUS_OBSTRUCTED) {
1857 err = (*progress_cb)(progress_arg, status, path);
1858 goto done;
1860 if (status == GOT_STATUS_CONFLICT) {
1861 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1862 path);
1863 goto done;
1866 if (ie && status != GOT_STATUS_MISSING &&
1867 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1871 err = sync_timestamps(ondisk_path, status, ie, &sb);
1872 if (err)
1873 goto done;
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1875 path);
1876 goto done;
1878 if (got_fileindex_entry_has_blob(ie) &&
1879 memcmp(ie->blob_sha1, te->id.sha1,
1880 SHA1_DIGEST_LENGTH) == 0) {
1881 err = sync_timestamps(ondisk_path, status, ie, &sb);
1882 goto done;
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 if (err)
1888 goto done;
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 if (err)
1899 goto done;
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1907 goto done;
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 goto done;
1915 if (S_ISLNK(te->mode)) {
1916 err = merge_symlink(worktree, blob2,
1917 ondisk_path, path, sb.st_mode, label_orig,
1918 blob, worktree->base_commit_id, repo,
1919 progress_cb, progress_arg);
1920 } else {
1921 err = merge_blob(&update_timestamps, worktree, blob2,
1922 ondisk_path, path, sb.st_mode, label_orig, blob,
1923 worktree->base_commit_id, repo,
1924 progress_cb, progress_arg);
1926 free(label_orig);
1927 if (blob2)
1928 got_object_blob_close(blob2);
1929 if (err)
1930 goto done;
1932 * Do not update timestamps of files with local changes.
1933 * Otherwise, a future status walk would treat them as
1934 * unmodified files again.
1936 err = got_fileindex_entry_update(ie, ondisk_path,
1937 blob->id.sha1, worktree->base_commit_id->sha1,
1938 update_timestamps);
1939 } else if (status == GOT_STATUS_MODE_CHANGE) {
1940 err = got_fileindex_entry_update(ie, ondisk_path,
1941 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1942 } else if (status == GOT_STATUS_DELETE) {
1943 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1944 if (err)
1945 goto done;
1946 err = got_fileindex_entry_update(ie, ondisk_path,
1947 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1948 if (err)
1949 goto done;
1950 } else {
1951 int is_bad_symlink = 0;
1952 if (S_ISLNK(te->mode)) {
1953 err = install_symlink(&is_bad_symlink, worktree,
1954 ondisk_path, path, blob,
1955 status == GOT_STATUS_MISSING, 0,
1956 status == GOT_STATUS_UNVERSIONED, repo,
1957 progress_cb, progress_arg);
1958 } else {
1959 err = install_blob(worktree, ondisk_path, path,
1960 te->mode, sb.st_mode, blob,
1961 status == GOT_STATUS_MISSING, 0, 0,
1962 status == GOT_STATUS_UNVERSIONED, repo,
1963 progress_cb, progress_arg);
1965 if (err)
1966 goto done;
1968 if (ie) {
1969 err = got_fileindex_entry_update(ie, ondisk_path,
1970 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1971 } else {
1972 err = create_fileindex_entry(&ie, fileindex,
1973 worktree->base_commit_id, ondisk_path, path,
1974 &blob->id);
1976 if (err)
1977 goto done;
1979 if (is_bad_symlink) {
1980 got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1984 got_object_blob_close(blob);
1985 done:
1986 free(ondisk_path);
1987 return err;
1990 static const struct got_error *
1991 remove_ondisk_file(const char *root_path, const char *path)
1993 const struct got_error *err = NULL;
1994 char *ondisk_path = NULL;
1996 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (unlink(ondisk_path) == -1) {
2000 if (errno != ENOENT)
2001 err = got_error_from_errno2("unlink", ondisk_path);
2002 } else {
2003 char *parent = dirname(ondisk_path);
2004 while (parent && strcmp(parent, root_path) != 0) {
2005 if (rmdir(parent) == -1) {
2006 if (errno != ENOTEMPTY)
2007 err = got_error_from_errno2("rmdir",
2008 parent);
2009 break;
2011 parent = dirname(parent);
2014 free(ondisk_path);
2015 return err;
2018 static const struct got_error *
2019 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2020 struct got_fileindex_entry *ie, struct got_repository *repo,
2021 got_worktree_checkout_cb progress_cb, void *progress_arg)
2023 const struct got_error *err = NULL;
2024 unsigned char status;
2025 struct stat sb;
2026 char *ondisk_path;
2028 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2029 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2031 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2032 == -1)
2033 return got_error_from_errno("asprintf");
2035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2036 if (err)
2037 goto done;
2039 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2040 char ondisk_target[PATH_MAX];
2041 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2042 sizeof(ondisk_target));
2043 if (ondisk_len == -1) {
2044 err = got_error_from_errno2("readlink", ondisk_path);
2045 goto done;
2047 ondisk_target[ondisk_len] = '\0';
2048 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2049 NULL, NULL, /* XXX pass common ancestor info? */
2050 ondisk_target, ondisk_path);
2051 if (err)
2052 goto done;
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2054 ie->path);
2055 goto done;
2058 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2059 status == GOT_STATUS_ADD) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2061 if (err)
2062 goto done;
2064 * Preserve the working file and change the deleted blob's
2065 * entry into a schedule-add entry.
2067 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 0);
2069 } else {
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2071 if (err)
2072 goto done;
2073 if (status == GOT_STATUS_NO_CHANGE) {
2074 err = remove_ondisk_file(worktree->root_path, ie->path);
2075 if (err)
2076 goto done;
2078 got_fileindex_entry_remove(fileindex, ie);
2080 done:
2081 free(ondisk_path);
2082 return err;
2085 struct diff_cb_arg {
2086 struct got_fileindex *fileindex;
2087 struct got_worktree *worktree;
2088 struct got_repository *repo;
2089 got_worktree_checkout_cb progress_cb;
2090 void *progress_arg;
2091 got_cancel_cb cancel_cb;
2092 void *cancel_arg;
2095 static const struct got_error *
2096 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2097 struct got_tree_entry *te, const char *parent_path)
2099 struct diff_cb_arg *a = arg;
2101 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2102 return got_error(GOT_ERR_CANCELLED);
2104 return update_blob(a->worktree, a->fileindex, ie, te,
2105 ie->path, a->repo, a->progress_cb, a->progress_arg);
2108 static const struct got_error *
2109 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2111 struct diff_cb_arg *a = arg;
2113 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2114 return got_error(GOT_ERR_CANCELLED);
2116 return delete_blob(a->worktree, a->fileindex, ie,
2117 a->repo, a->progress_cb, a->progress_arg);
2120 static const struct got_error *
2121 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2123 struct diff_cb_arg *a = arg;
2124 const struct got_error *err;
2125 char *path;
2127 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2128 return got_error(GOT_ERR_CANCELLED);
2130 if (got_object_tree_entry_is_submodule(te))
2131 return NULL;
2133 if (asprintf(&path, "%s%s%s", parent_path,
2134 parent_path[0] ? "/" : "", te->name)
2135 == -1)
2136 return got_error_from_errno("asprintf");
2138 if (S_ISDIR(te->mode))
2139 err = add_dir_on_disk(a->worktree, path);
2140 else
2141 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2142 a->repo, a->progress_cb, a->progress_arg);
2144 free(path);
2145 return err;
2148 static const struct got_error *
2149 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2151 const struct got_error *err = NULL;
2152 char *uuidstr = NULL;
2153 uint32_t uuid_status;
2155 *refname = NULL;
2157 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2158 if (uuid_status != uuid_s_ok)
2159 return got_error_uuid(uuid_status, "uuid_to_string");
2161 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2162 == -1) {
2163 err = got_error_from_errno("asprintf");
2164 *refname = NULL;
2166 free(uuidstr);
2167 return err;
2170 const struct got_error *
2171 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2173 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2176 static const struct got_error *
2177 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2179 return get_ref_name(refname, worktree,
2180 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2183 static const struct got_error *
2184 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2186 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2189 static const struct got_error *
2190 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2192 return get_ref_name(refname, worktree,
2193 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2196 static const struct got_error *
2197 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2199 return get_ref_name(refname, worktree,
2200 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2203 static const struct got_error *
2204 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2206 return get_ref_name(refname, worktree,
2207 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2210 static const struct got_error *
2211 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2213 return get_ref_name(refname, worktree,
2214 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2217 static const struct got_error *
2218 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2224 static const struct got_error *
2225 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2231 const struct got_error *
2232 got_worktree_get_histedit_script_path(char **path,
2233 struct got_worktree *worktree)
2235 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2236 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2237 *path = NULL;
2238 return got_error_from_errno("asprintf");
2240 return NULL;
2244 * Prevent Git's garbage collector from deleting our base commit by
2245 * setting a reference to our base commit's ID.
2247 static const struct got_error *
2248 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_reference *ref = NULL;
2252 char *refname;
2254 err = got_worktree_get_base_ref_name(&refname, worktree);
2255 if (err)
2256 return err;
2258 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2259 if (err)
2260 goto done;
2262 err = got_ref_write(ref, repo);
2263 done:
2264 free(refname);
2265 if (ref)
2266 got_ref_close(ref);
2267 return err;
2270 static const struct got_error *
2271 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2273 const struct got_error *err = NULL;
2275 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2276 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 *fileindex_path = NULL;
2280 return err;
2284 static const struct got_error *
2285 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2286 struct got_worktree *worktree)
2288 const struct got_error *err = NULL;
2289 FILE *index = NULL;
2291 *fileindex_path = NULL;
2292 *fileindex = got_fileindex_alloc();
2293 if (*fileindex == NULL)
2294 return got_error_from_errno("got_fileindex_alloc");
2296 err = get_fileindex_path(fileindex_path, worktree);
2297 if (err)
2298 goto done;
2300 index = fopen(*fileindex_path, "rb");
2301 if (index == NULL) {
2302 if (errno != ENOENT)
2303 err = got_error_from_errno2("fopen", *fileindex_path);
2304 } else {
2305 err = got_fileindex_read(*fileindex, index);
2306 if (fclose(index) != 0 && err == NULL)
2307 err = got_error_from_errno("fclose");
2309 done:
2310 if (err) {
2311 free(*fileindex_path);
2312 *fileindex_path = NULL;
2313 got_fileindex_free(*fileindex);
2314 *fileindex = NULL;
2316 return err;
2319 struct bump_base_commit_id_arg {
2320 struct got_object_id *base_commit_id;
2321 const char *path;
2322 size_t path_len;
2323 const char *entry_name;
2324 got_worktree_checkout_cb progress_cb;
2325 void *progress_arg;
2328 /* Bump base commit ID of all files within an updated part of the work tree. */
2329 static const struct got_error *
2330 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2332 const struct got_error *err;
2333 struct bump_base_commit_id_arg *a = arg;
2335 if (a->entry_name) {
2336 if (strcmp(ie->path, a->path) != 0)
2337 return NULL;
2338 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2339 return NULL;
2341 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2342 SHA1_DIGEST_LENGTH) == 0)
2343 return NULL;
2345 if (a->progress_cb) {
2346 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2347 ie->path);
2348 if (err)
2349 return err;
2351 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2352 return NULL;
2355 static const struct got_error *
2356 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2358 const struct got_error *err = NULL;
2359 char *new_fileindex_path = NULL;
2360 FILE *new_index = NULL;
2361 struct timespec timeout;
2363 err = got_opentemp_named(&new_fileindex_path, &new_index,
2364 fileindex_path);
2365 if (err)
2366 goto done;
2368 err = got_fileindex_write(fileindex, new_index);
2369 if (err)
2370 goto done;
2372 if (rename(new_fileindex_path, fileindex_path) != 0) {
2373 err = got_error_from_errno3("rename", new_fileindex_path,
2374 fileindex_path);
2375 unlink(new_fileindex_path);
2379 * Sleep for a short amount of time to ensure that files modified after
2380 * this program exits have a different time stamp from the one which
2381 * was recorded in the file index.
2383 timeout.tv_sec = 0;
2384 timeout.tv_nsec = 1;
2385 nanosleep(&timeout, NULL);
2386 done:
2387 if (new_index)
2388 fclose(new_index);
2389 free(new_fileindex_path);
2390 return err;
2393 static const struct got_error *
2394 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2395 struct got_object_id **tree_id, const char *wt_relpath,
2396 struct got_worktree *worktree, struct got_repository *repo)
2398 const struct got_error *err = NULL;
2399 struct got_object_id *id = NULL;
2400 char *in_repo_path = NULL;
2401 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2403 *entry_type = GOT_OBJ_TYPE_ANY;
2404 *tree_relpath = NULL;
2405 *tree_id = NULL;
2407 if (wt_relpath[0] == '\0') {
2408 /* Check out all files within the work tree. */
2409 *entry_type = GOT_OBJ_TYPE_TREE;
2410 *tree_relpath = strdup("");
2411 if (*tree_relpath == NULL) {
2412 err = got_error_from_errno("strdup");
2413 goto done;
2415 err = got_object_id_by_path(tree_id, repo,
2416 worktree->base_commit_id, worktree->path_prefix);
2417 if (err)
2418 goto done;
2419 return NULL;
2422 /* Check out a subset of files in the work tree. */
2424 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2425 is_root_wt ? "" : "/", wt_relpath) == -1) {
2426 err = got_error_from_errno("asprintf");
2427 goto done;
2430 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2431 in_repo_path);
2432 if (err)
2433 goto done;
2435 free(in_repo_path);
2436 in_repo_path = NULL;
2438 err = got_object_get_type(entry_type, repo, id);
2439 if (err)
2440 goto done;
2442 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2443 /* Check out a single file. */
2444 if (strchr(wt_relpath, '/') == NULL) {
2445 /* Check out a single file in work tree's root dir. */
2446 in_repo_path = strdup(worktree->path_prefix);
2447 if (in_repo_path == NULL) {
2448 err = got_error_from_errno("strdup");
2449 goto done;
2451 *tree_relpath = strdup("");
2452 if (*tree_relpath == NULL) {
2453 err = got_error_from_errno("strdup");
2454 goto done;
2456 } else {
2457 /* Check out a single file in a subdirectory. */
2458 err = got_path_dirname(tree_relpath, wt_relpath);
2459 if (err)
2460 return err;
2461 if (asprintf(&in_repo_path, "%s%s%s",
2462 worktree->path_prefix, is_root_wt ? "" : "/",
2463 *tree_relpath) == -1) {
2464 err = got_error_from_errno("asprintf");
2465 goto done;
2468 err = got_object_id_by_path(tree_id, repo,
2469 worktree->base_commit_id, in_repo_path);
2470 } else {
2471 /* Check out all files within a subdirectory. */
2472 *tree_id = got_object_id_dup(id);
2473 if (*tree_id == NULL) {
2474 err = got_error_from_errno("got_object_id_dup");
2475 goto done;
2477 *tree_relpath = strdup(wt_relpath);
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2480 goto done;
2483 done:
2484 free(id);
2485 free(in_repo_path);
2486 if (err) {
2487 *entry_type = GOT_OBJ_TYPE_ANY;
2488 free(*tree_relpath);
2489 *tree_relpath = NULL;
2490 free(*tree_id);
2491 *tree_id = NULL;
2493 return err;
2496 static const struct got_error *
2497 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2498 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2499 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2500 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2502 const struct got_error *err = NULL;
2503 struct got_commit_object *commit = NULL;
2504 struct got_tree_object *tree = NULL;
2505 struct got_fileindex_diff_tree_cb diff_cb;
2506 struct diff_cb_arg arg;
2508 err = ref_base_commit(worktree, repo);
2509 if (err) {
2510 if (!(err->code == GOT_ERR_ERRNO &&
2511 (errno == EACCES || errno == EROFS)))
2512 goto done;
2513 err = (*progress_cb)(progress_arg,
2514 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2515 if (err)
2516 return err;
2519 err = got_object_open_as_commit(&commit, repo,
2520 worktree->base_commit_id);
2521 if (err)
2522 goto done;
2524 err = got_object_open_as_tree(&tree, repo, tree_id);
2525 if (err)
2526 goto done;
2528 if (entry_name &&
2529 got_object_tree_find_entry(tree, entry_name) == NULL) {
2530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2531 goto done;
2534 diff_cb.diff_old_new = diff_old_new;
2535 diff_cb.diff_old = diff_old;
2536 diff_cb.diff_new = diff_new;
2537 arg.fileindex = fileindex;
2538 arg.worktree = worktree;
2539 arg.repo = repo;
2540 arg.progress_cb = progress_cb;
2541 arg.progress_arg = progress_arg;
2542 arg.cancel_cb = cancel_cb;
2543 arg.cancel_arg = cancel_arg;
2544 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2545 entry_name, repo, &diff_cb, &arg);
2546 done:
2547 if (tree)
2548 got_object_tree_close(tree);
2549 if (commit)
2550 got_object_commit_close(commit);
2551 return err;
2554 const struct got_error *
2555 got_worktree_checkout_files(struct got_worktree *worktree,
2556 struct got_pathlist_head *paths, struct got_repository *repo,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg,
2558 got_cancel_cb cancel_cb, void *cancel_arg)
2560 const struct got_error *err = NULL, *sync_err, *unlockerr;
2561 struct got_commit_object *commit = NULL;
2562 struct got_tree_object *tree = NULL;
2563 struct got_fileindex *fileindex = NULL;
2564 char *fileindex_path = NULL;
2565 struct got_pathlist_entry *pe;
2566 struct tree_path_data {
2567 SIMPLEQ_ENTRY(tree_path_data) entry;
2568 struct got_object_id *tree_id;
2569 int entry_type;
2570 char *relpath;
2571 char *entry_name;
2572 } *tpd = NULL;
2573 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2575 SIMPLEQ_INIT(&tree_paths);
2577 err = lock_worktree(worktree, LOCK_EX);
2578 if (err)
2579 return err;
2581 /* Map all specified paths to in-repository trees. */
2582 TAILQ_FOREACH(pe, paths, entry) {
2583 tpd = malloc(sizeof(*tpd));
2584 if (tpd == NULL) {
2585 err = got_error_from_errno("malloc");
2586 goto done;
2589 err = find_tree_entry_for_checkout(&tpd->entry_type,
2590 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2591 if (err) {
2592 free(tpd);
2593 goto done;
2596 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2597 err = got_path_basename(&tpd->entry_name, pe->path);
2598 if (err) {
2599 free(tpd->relpath);
2600 free(tpd->tree_id);
2601 free(tpd);
2602 goto done;
2604 } else
2605 tpd->entry_name = NULL;
2607 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2611 * Read the file index.
2612 * Checking out files is supposed to be an idempotent operation.
2613 * If the on-disk file index is incomplete we will try to complete it.
2615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2616 if (err)
2617 goto done;
2619 tpd = SIMPLEQ_FIRST(&tree_paths);
2620 TAILQ_FOREACH(pe, paths, entry) {
2621 struct bump_base_commit_id_arg bbc_arg;
2623 err = checkout_files(worktree, fileindex, tpd->relpath,
2624 tpd->tree_id, tpd->entry_name, repo,
2625 progress_cb, progress_arg, cancel_cb, cancel_arg);
2626 if (err)
2627 break;
2629 bbc_arg.base_commit_id = worktree->base_commit_id;
2630 bbc_arg.entry_name = tpd->entry_name;
2631 bbc_arg.path = pe->path;
2632 bbc_arg.path_len = pe->path_len;
2633 bbc_arg.progress_cb = progress_cb;
2634 bbc_arg.progress_arg = progress_arg;
2635 err = got_fileindex_for_each_entry_safe(fileindex,
2636 bump_base_commit_id, &bbc_arg);
2637 if (err)
2638 break;
2640 tpd = SIMPLEQ_NEXT(tpd, entry);
2642 sync_err = sync_fileindex(fileindex, fileindex_path);
2643 if (sync_err && err == NULL)
2644 err = sync_err;
2645 done:
2646 free(fileindex_path);
2647 if (tree)
2648 got_object_tree_close(tree);
2649 if (commit)
2650 got_object_commit_close(commit);
2651 if (fileindex)
2652 got_fileindex_free(fileindex);
2653 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2654 tpd = SIMPLEQ_FIRST(&tree_paths);
2655 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2656 free(tpd->relpath);
2657 free(tpd->tree_id);
2658 free(tpd);
2660 unlockerr = lock_worktree(worktree, LOCK_SH);
2661 if (unlockerr && err == NULL)
2662 err = unlockerr;
2663 return err;
2666 struct merge_file_cb_arg {
2667 struct got_worktree *worktree;
2668 struct got_fileindex *fileindex;
2669 got_worktree_checkout_cb progress_cb;
2670 void *progress_arg;
2671 got_cancel_cb cancel_cb;
2672 void *cancel_arg;
2673 const char *label_orig;
2674 struct got_object_id *commit_id2;
2677 static const struct got_error *
2678 merge_file_cb(void *arg, struct got_blob_object *blob1,
2679 struct got_blob_object *blob2, struct got_object_id *id1,
2680 struct got_object_id *id2, const char *path1, const char *path2,
2681 mode_t mode1, mode_t mode2, struct got_repository *repo)
2683 static const struct got_error *err = NULL;
2684 struct merge_file_cb_arg *a = arg;
2685 struct got_fileindex_entry *ie;
2686 char *ondisk_path = NULL;
2687 struct stat sb;
2688 unsigned char status;
2689 int local_changes_subsumed;
2691 if (blob1 && blob2) {
2692 ie = got_fileindex_entry_get(a->fileindex, path2,
2693 strlen(path2));
2694 if (ie == NULL)
2695 return (*a->progress_cb)(a->progress_arg,
2696 GOT_STATUS_MISSING, path2);
2698 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2699 path2) == -1)
2700 return got_error_from_errno("asprintf");
2702 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2703 repo);
2704 if (err)
2705 goto done;
2707 if (status == GOT_STATUS_DELETE) {
2708 err = (*a->progress_cb)(a->progress_arg,
2709 GOT_STATUS_MERGE, path2);
2710 goto done;
2712 if (status != GOT_STATUS_NO_CHANGE &&
2713 status != GOT_STATUS_MODIFY &&
2714 status != GOT_STATUS_CONFLICT &&
2715 status != GOT_STATUS_ADD) {
2716 err = (*a->progress_cb)(a->progress_arg, status, path2);
2717 goto done;
2720 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2721 err = merge_symlink(a->worktree, blob1,
2722 ondisk_path, path2, sb.st_mode, a->label_orig,
2723 blob2, a->commit_id2, repo, a->progress_cb,
2724 a->progress_arg);
2725 } else {
2726 err = merge_blob(&local_changes_subsumed, a->worktree,
2727 blob1, ondisk_path, path2, sb.st_mode,
2728 a->label_orig, blob2, a->commit_id2, repo,
2729 a->progress_cb, a->progress_arg);
2731 } else if (blob1) {
2732 ie = got_fileindex_entry_get(a->fileindex, path1,
2733 strlen(path1));
2734 if (ie == NULL)
2735 return (*a->progress_cb)(a->progress_arg,
2736 GOT_STATUS_MISSING, path1);
2738 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2739 path1) == -1)
2740 return got_error_from_errno("asprintf");
2742 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2743 repo);
2744 if (err)
2745 goto done;
2747 switch (status) {
2748 case GOT_STATUS_NO_CHANGE:
2749 err = (*a->progress_cb)(a->progress_arg,
2750 GOT_STATUS_DELETE, path1);
2751 if (err)
2752 goto done;
2753 err = remove_ondisk_file(a->worktree->root_path, path1);
2754 if (err)
2755 goto done;
2756 if (ie)
2757 got_fileindex_entry_mark_deleted_from_disk(ie);
2758 break;
2759 case GOT_STATUS_DELETE:
2760 case GOT_STATUS_MISSING:
2761 err = (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_DELETE, path1);
2763 if (err)
2764 goto done;
2765 if (ie)
2766 got_fileindex_entry_mark_deleted_from_disk(ie);
2767 break;
2768 case GOT_STATUS_ADD:
2769 case GOT_STATUS_MODIFY:
2770 case GOT_STATUS_CONFLICT:
2771 err = (*a->progress_cb)(a->progress_arg,
2772 GOT_STATUS_CANNOT_DELETE, path1);
2773 if (err)
2774 goto done;
2775 break;
2776 case GOT_STATUS_OBSTRUCTED:
2777 err = (*a->progress_cb)(a->progress_arg, status, path1);
2778 if (err)
2779 goto done;
2780 break;
2781 default:
2782 break;
2784 } else if (blob2) {
2785 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2786 path2) == -1)
2787 return got_error_from_errno("asprintf");
2788 ie = got_fileindex_entry_get(a->fileindex, path2,
2789 strlen(path2));
2790 if (ie) {
2791 err = get_file_status(&status, &sb, ie, ondisk_path,
2792 -1, NULL, repo);
2793 if (err)
2794 goto done;
2795 if (status != GOT_STATUS_NO_CHANGE &&
2796 status != GOT_STATUS_MODIFY &&
2797 status != GOT_STATUS_CONFLICT &&
2798 status != GOT_STATUS_ADD) {
2799 err = (*a->progress_cb)(a->progress_arg,
2800 status, path2);
2801 goto done;
2803 if (S_ISLNK(mode2)) {
2804 err = merge_symlink(a->worktree, NULL,
2805 ondisk_path, path2, sb.st_mode,
2806 a->label_orig, blob2, a->commit_id2,
2807 repo, a->progress_cb, a->progress_arg);
2808 if (err)
2809 goto done;
2810 } else {
2811 err = merge_blob(&local_changes_subsumed,
2812 a->worktree, NULL, ondisk_path, path2,
2813 sb.st_mode, a->label_orig, blob2,
2814 a->commit_id2, repo, a->progress_cb,
2815 a->progress_arg);
2816 if (err)
2817 goto done;
2819 if (status == GOT_STATUS_DELETE) {
2820 err = got_fileindex_entry_update(ie,
2821 ondisk_path, blob2->id.sha1,
2822 a->worktree->base_commit_id->sha1, 0);
2823 if (err)
2824 goto done;
2826 } else {
2827 int is_bad_symlink = 0;
2828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2829 if (S_ISLNK(mode2)) {
2830 err = install_symlink(&is_bad_symlink,
2831 a->worktree, ondisk_path, path2, blob2, 0,
2832 0, 1, repo, a->progress_cb, a->progress_arg);
2833 } else {
2834 err = install_blob(a->worktree, ondisk_path, path2,
2835 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2836 a->progress_cb, a->progress_arg);
2838 if (err)
2839 goto done;
2840 err = got_fileindex_entry_alloc(&ie, path2);
2841 if (err)
2842 goto done;
2843 err = got_fileindex_entry_update(ie, ondisk_path,
2844 NULL, NULL, 1);
2845 if (err) {
2846 got_fileindex_entry_free(ie);
2847 goto done;
2849 err = got_fileindex_entry_add(a->fileindex, ie);
2850 if (err) {
2851 got_fileindex_entry_free(ie);
2852 goto done;
2854 if (is_bad_symlink) {
2855 got_fileindex_entry_filetype_set(ie,
2856 GOT_FILEIDX_MODE_BAD_SYMLINK);
2860 done:
2861 free(ondisk_path);
2862 return err;
2865 struct check_merge_ok_arg {
2866 struct got_worktree *worktree;
2867 struct got_repository *repo;
2870 static const struct got_error *
2871 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2873 const struct got_error *err = NULL;
2874 struct check_merge_ok_arg *a = arg;
2875 unsigned char status;
2876 struct stat sb;
2877 char *ondisk_path;
2879 /* Reject merges into a work tree with mixed base commits. */
2880 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2881 SHA1_DIGEST_LENGTH))
2882 return got_error(GOT_ERR_MIXED_COMMITS);
2884 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2885 == -1)
2886 return got_error_from_errno("asprintf");
2888 /* Reject merges into a work tree with conflicted files. */
2889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2890 if (err)
2891 return err;
2892 if (status == GOT_STATUS_CONFLICT)
2893 return got_error(GOT_ERR_CONFLICTS);
2895 return NULL;
2898 static const struct got_error *
2899 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2900 const char *fileindex_path, struct got_object_id *commit_id1,
2901 struct got_object_id *commit_id2, struct got_repository *repo,
2902 got_worktree_checkout_cb progress_cb, void *progress_arg,
2903 got_cancel_cb cancel_cb, void *cancel_arg)
2905 const struct got_error *err = NULL, *sync_err;
2906 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2907 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2908 struct merge_file_cb_arg arg;
2909 char *label_orig = NULL;
2911 if (commit_id1) {
2912 char *id_str;
2914 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2915 worktree->path_prefix);
2916 if (err)
2917 goto done;
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2920 if (err)
2921 goto done;
2923 err = got_object_id_str(&id_str, commit_id1);
2924 if (err)
2925 goto done;
2927 if (asprintf(&label_orig, "%s: commit %s",
2928 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2930 free(id_str);
2931 goto done;
2933 free(id_str);
2936 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2937 worktree->path_prefix);
2938 if (err)
2939 goto done;
2941 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2942 if (err)
2943 goto done;
2945 arg.worktree = worktree;
2946 arg.fileindex = fileindex;
2947 arg.progress_cb = progress_cb;
2948 arg.progress_arg = progress_arg;
2949 arg.cancel_cb = cancel_cb;
2950 arg.cancel_arg = cancel_arg;
2951 arg.label_orig = label_orig;
2952 arg.commit_id2 = commit_id2;
2953 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2954 sync_err = sync_fileindex(fileindex, fileindex_path);
2955 if (sync_err && err == NULL)
2956 err = sync_err;
2957 done:
2958 if (tree1)
2959 got_object_tree_close(tree1);
2960 if (tree2)
2961 got_object_tree_close(tree2);
2962 free(label_orig);
2963 return err;
2966 const struct got_error *
2967 got_worktree_merge_files(struct got_worktree *worktree,
2968 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2969 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2970 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2972 const struct got_error *err, *unlockerr;
2973 char *fileindex_path = NULL;
2974 struct got_fileindex *fileindex = NULL;
2975 struct check_merge_ok_arg mok_arg;
2977 err = lock_worktree(worktree, LOCK_EX);
2978 if (err)
2979 return err;
2981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2982 if (err)
2983 goto done;
2985 mok_arg.worktree = worktree;
2986 mok_arg.repo = repo;
2987 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2988 &mok_arg);
2989 if (err)
2990 goto done;
2992 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2993 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2994 done:
2995 if (fileindex)
2996 got_fileindex_free(fileindex);
2997 free(fileindex_path);
2998 unlockerr = lock_worktree(worktree, LOCK_SH);
2999 if (unlockerr && err == NULL)
3000 err = unlockerr;
3001 return err;
3004 struct diff_dir_cb_arg {
3005 struct got_fileindex *fileindex;
3006 struct got_worktree *worktree;
3007 const char *status_path;
3008 size_t status_path_len;
3009 struct got_repository *repo;
3010 got_worktree_status_cb status_cb;
3011 void *status_arg;
3012 got_cancel_cb cancel_cb;
3013 void *cancel_arg;
3014 /* A pathlist containing per-directory pathlists of ignore patterns. */
3015 struct got_pathlist_head ignores;
3016 int report_unchanged;
3017 int no_ignores;
3020 static const struct got_error *
3021 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3022 int dirfd, const char *de_name,
3023 got_worktree_status_cb status_cb, void *status_arg,
3024 struct got_repository *repo, int report_unchanged)
3026 const struct got_error *err = NULL;
3027 unsigned char status = GOT_STATUS_NO_CHANGE;
3028 unsigned char staged_status = get_staged_status(ie);
3029 struct stat sb;
3030 struct got_object_id blob_id, commit_id, staged_blob_id;
3031 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3032 struct got_object_id *staged_blob_idp = NULL;
3034 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3035 if (err)
3036 return err;
3038 if (status == GOT_STATUS_NO_CHANGE &&
3039 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3040 return NULL;
3042 if (got_fileindex_entry_has_blob(ie)) {
3043 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3044 blob_idp = &blob_id;
3046 if (got_fileindex_entry_has_commit(ie)) {
3047 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3048 commit_idp = &commit_id;
3050 if (staged_status == GOT_STATUS_ADD ||
3051 staged_status == GOT_STATUS_MODIFY) {
3052 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3053 SHA1_DIGEST_LENGTH);
3054 staged_blob_idp = &staged_blob_id;
3057 return (*status_cb)(status_arg, status, staged_status,
3058 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3061 static const struct got_error *
3062 status_old_new(void *arg, struct got_fileindex_entry *ie,
3063 struct dirent *de, const char *parent_path, int dirfd)
3065 const struct got_error *err = NULL;
3066 struct diff_dir_cb_arg *a = arg;
3067 char *abspath;
3069 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3070 return got_error(GOT_ERR_CANCELLED);
3072 if (got_path_cmp(parent_path, a->status_path,
3073 strlen(parent_path), a->status_path_len) != 0 &&
3074 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3075 return NULL;
3077 if (parent_path[0]) {
3078 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3079 parent_path, de->d_name) == -1)
3080 return got_error_from_errno("asprintf");
3081 } else {
3082 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3083 de->d_name) == -1)
3084 return got_error_from_errno("asprintf");
3087 err = report_file_status(ie, abspath, dirfd, de->d_name,
3088 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3089 free(abspath);
3090 return err;
3093 static const struct got_error *
3094 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3096 struct diff_dir_cb_arg *a = arg;
3097 struct got_object_id blob_id, commit_id;
3098 unsigned char status;
3100 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3101 return got_error(GOT_ERR_CANCELLED);
3103 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3104 return NULL;
3106 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3107 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3108 if (got_fileindex_entry_has_file_on_disk(ie))
3109 status = GOT_STATUS_MISSING;
3110 else
3111 status = GOT_STATUS_DELETE;
3112 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3113 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3116 void
3117 free_ignorelist(struct got_pathlist_head *ignorelist)
3119 struct got_pathlist_entry *pe;
3121 TAILQ_FOREACH(pe, ignorelist, entry)
3122 free((char *)pe->path);
3123 got_pathlist_free(ignorelist);
3126 void
3127 free_ignores(struct got_pathlist_head *ignores)
3129 struct got_pathlist_entry *pe;
3131 TAILQ_FOREACH(pe, ignores, entry) {
3132 struct got_pathlist_head *ignorelist = pe->data;
3133 free_ignorelist(ignorelist);
3134 free((char *)pe->path);
3136 got_pathlist_free(ignores);
3139 static const struct got_error *
3140 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3142 const struct got_error *err = NULL;
3143 struct got_pathlist_entry *pe = NULL;
3144 struct got_pathlist_head *ignorelist;
3145 char *line = NULL, *pattern, *dirpath = NULL;
3146 size_t linesize = 0;
3147 ssize_t linelen;
3149 ignorelist = calloc(1, sizeof(*ignorelist));
3150 if (ignorelist == NULL)
3151 return got_error_from_errno("calloc");
3152 TAILQ_INIT(ignorelist);
3154 while ((linelen = getline(&line, &linesize, f)) != -1) {
3155 if (linelen > 0 && line[linelen - 1] == '\n')
3156 line[linelen - 1] = '\0';
3158 /* Git's ignores may contain comments. */
3159 if (line[0] == '#')
3160 continue;
3162 /* Git's negated patterns are not (yet?) supported. */
3163 if (line[0] == '!')
3164 continue;
3166 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3167 line) == -1) {
3168 err = got_error_from_errno("asprintf");
3169 goto done;
3171 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3172 if (err)
3173 goto done;
3175 if (ferror(f)) {
3176 err = got_error_from_errno("getline");
3177 goto done;
3180 dirpath = strdup(path);
3181 if (dirpath == NULL) {
3182 err = got_error_from_errno("strdup");
3183 goto done;
3185 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3186 done:
3187 free(line);
3188 if (err || pe == NULL) {
3189 free(dirpath);
3190 free_ignorelist(ignorelist);
3192 return err;
3195 int
3196 match_ignores(struct got_pathlist_head *ignores, const char *path)
3198 struct got_pathlist_entry *pe;
3200 /* Handle patterns which match in all directories. */
3201 TAILQ_FOREACH(pe, ignores, entry) {
3202 struct got_pathlist_head *ignorelist = pe->data;
3203 struct got_pathlist_entry *pi;
3205 TAILQ_FOREACH(pi, ignorelist, entry) {
3206 const char *p, *pattern = pi->path;
3208 if (strncmp(pattern, "**/", 3) != 0)
3209 continue;
3210 pattern += 3;
3211 p = path;
3212 while (*p) {
3213 if (fnmatch(pattern, p,
3214 FNM_PATHNAME | FNM_LEADING_DIR)) {
3215 /* Retry in next directory. */
3216 while (*p && *p != '/')
3217 p++;
3218 while (*p == '/')
3219 p++;
3220 continue;
3222 return 1;
3228 * The ignores pathlist contains ignore lists from children before
3229 * parents, so we can find the most specific ignorelist by walking
3230 * ignores backwards.
3232 pe = TAILQ_LAST(ignores, got_pathlist_head);
3233 while (pe) {
3234 if (got_path_is_child(path, pe->path, pe->path_len)) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 struct got_pathlist_entry *pi;
3237 TAILQ_FOREACH(pi, ignorelist, entry) {
3238 const char *pattern = pi->path;
3239 int flags = FNM_LEADING_DIR;
3240 if (strstr(pattern, "/**/") == NULL)
3241 flags |= FNM_PATHNAME;
3242 if (fnmatch(pattern, path, flags))
3243 continue;
3244 return 1;
3247 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3250 return 0;
3253 static const struct got_error *
3254 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3255 const char *path, int dirfd, const char *ignores_filename)
3257 const struct got_error *err = NULL;
3258 char *ignorespath;
3259 int fd = -1;
3260 FILE *ignoresfile = NULL;
3262 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3263 path[0] ? "/" : "", ignores_filename) == -1)
3264 return got_error_from_errno("asprintf");
3266 if (dirfd != -1) {
3267 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3268 if (fd == -1) {
3269 if (errno != ENOENT && errno != EACCES)
3270 err = got_error_from_errno2("openat",
3271 ignorespath);
3272 } else {
3273 ignoresfile = fdopen(fd, "r");
3274 if (ignoresfile == NULL)
3275 err = got_error_from_errno2("fdopen",
3276 ignorespath);
3277 else {
3278 fd = -1;
3279 err = read_ignores(ignores, path, ignoresfile);
3282 } else {
3283 ignoresfile = fopen(ignorespath, "r");
3284 if (ignoresfile == NULL) {
3285 if (errno != ENOENT && errno != EACCES)
3286 err = got_error_from_errno2("fopen",
3287 ignorespath);
3288 } else
3289 err = read_ignores(ignores, path, ignoresfile);
3292 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3293 err = got_error_from_errno2("fclose", path);
3294 if (fd != -1 && close(fd) == -1 && err == NULL)
3295 err = got_error_from_errno2("close", path);
3296 free(ignorespath);
3297 return err;
3300 static const struct got_error *
3301 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3303 const struct got_error *err = NULL;
3304 struct diff_dir_cb_arg *a = arg;
3305 char *path = NULL;
3307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3308 return got_error(GOT_ERR_CANCELLED);
3310 if (parent_path[0]) {
3311 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3312 return got_error_from_errno("asprintf");
3313 } else {
3314 path = de->d_name;
3317 if (de->d_type != DT_DIR &&
3318 got_path_is_child(path, a->status_path, a->status_path_len)
3319 && !match_ignores(&a->ignores, path))
3320 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3321 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3322 if (parent_path[0])
3323 free(path);
3324 return err;
3327 static const struct got_error *
3328 status_traverse(void *arg, const char *path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3333 if (a->no_ignores)
3334 return NULL;
3336 err = add_ignores(&a->ignores, a->worktree->root_path,
3337 path, dirfd, ".cvsignore");
3338 if (err)
3339 return err;
3341 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3342 dirfd, ".gitignore");
3344 return err;
3347 static const struct got_error *
3348 report_single_file_status(const char *path, const char *ondisk_path,
3349 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3350 void *status_arg, struct got_repository *repo, int report_unchanged)
3352 struct got_fileindex_entry *ie;
3353 struct stat sb;
3355 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3356 if (ie)
3357 return report_file_status(ie, ondisk_path, -1, NULL,
3358 status_cb, status_arg, repo, report_unchanged);
3360 if (lstat(ondisk_path, &sb) == -1) {
3361 if (errno != ENOENT)
3362 return got_error_from_errno2("lstat", ondisk_path);
3363 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3365 return NULL;
3368 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3369 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3370 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3372 return NULL;
3375 static const struct got_error *
3376 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3377 const char *root_path, const char *path)
3379 const struct got_error *err;
3380 char *parent_path, *next_parent_path = NULL;
3382 err = add_ignores(ignores, root_path, "", -1,
3383 ".cvsignore");
3384 if (err)
3385 return err;
3387 err = add_ignores(ignores, root_path, "", -1,
3388 ".gitignore");
3389 if (err)
3390 return err;
3392 err = got_path_dirname(&parent_path, path);
3393 if (err) {
3394 if (err->code == GOT_ERR_BAD_PATH)
3395 return NULL; /* cannot traverse parent */
3396 return err;
3398 for (;;) {
3399 err = add_ignores(ignores, root_path, parent_path, -1,
3400 ".cvsignore");
3401 if (err)
3402 break;
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3404 ".gitignore");
3405 if (err)
3406 break;
3407 err = got_path_dirname(&next_parent_path, parent_path);
3408 if (err) {
3409 if (err->code == GOT_ERR_BAD_PATH)
3410 err = NULL; /* traversed everything */
3411 break;
3413 free(parent_path);
3414 parent_path = next_parent_path;
3415 next_parent_path = NULL;
3418 free(parent_path);
3419 free(next_parent_path);
3420 return err;
3423 static const struct got_error *
3424 worktree_status(struct got_worktree *worktree, const char *path,
3425 struct got_fileindex *fileindex, struct got_repository *repo,
3426 got_worktree_status_cb status_cb, void *status_arg,
3427 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3428 int report_unchanged)
3430 const struct got_error *err = NULL;
3431 int fd = -1;
3432 struct got_fileindex_diff_dir_cb fdiff_cb;
3433 struct diff_dir_cb_arg arg;
3434 char *ondisk_path = NULL;
3436 TAILQ_INIT(&arg.ignores);
3438 if (asprintf(&ondisk_path, "%s%s%s",
3439 worktree->root_path, path[0] ? "/" : "", path) == -1)
3440 return got_error_from_errno("asprintf");
3442 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3443 if (fd == -1) {
3444 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3445 errno != ELOOP)
3446 err = got_error_from_errno2("open", ondisk_path);
3447 else
3448 err = report_single_file_status(path, ondisk_path,
3449 fileindex, status_cb, status_arg, repo,
3450 report_unchanged);
3451 } else {
3452 fdiff_cb.diff_old_new = status_old_new;
3453 fdiff_cb.diff_old = status_old;
3454 fdiff_cb.diff_new = status_new;
3455 fdiff_cb.diff_traverse = status_traverse;
3456 arg.fileindex = fileindex;
3457 arg.worktree = worktree;
3458 arg.status_path = path;
3459 arg.status_path_len = strlen(path);
3460 arg.repo = repo;
3461 arg.status_cb = status_cb;
3462 arg.status_arg = status_arg;
3463 arg.cancel_cb = cancel_cb;
3464 arg.cancel_arg = cancel_arg;
3465 arg.report_unchanged = report_unchanged;
3466 arg.no_ignores = no_ignores;
3467 if (!no_ignores) {
3468 err = add_ignores_from_parent_paths(&arg.ignores,
3469 worktree->root_path, path);
3470 if (err)
3471 goto done;
3473 err = got_fileindex_diff_dir(fileindex, fd,
3474 worktree->root_path, path, repo, &fdiff_cb, &arg);
3476 done:
3477 free_ignores(&arg.ignores);
3478 if (fd != -1 && close(fd) != 0 && err == NULL)
3479 err = got_error_from_errno("close");
3480 free(ondisk_path);
3481 return err;
3484 const struct got_error *
3485 got_worktree_status(struct got_worktree *worktree,
3486 struct got_pathlist_head *paths, struct got_repository *repo,
3487 got_worktree_status_cb status_cb, void *status_arg,
3488 got_cancel_cb cancel_cb, void *cancel_arg)
3490 const struct got_error *err = NULL;
3491 char *fileindex_path = NULL;
3492 struct got_fileindex *fileindex = NULL;
3493 struct got_pathlist_entry *pe;
3495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3496 if (err)
3497 return err;
3499 TAILQ_FOREACH(pe, paths, entry) {
3500 err = worktree_status(worktree, pe->path, fileindex, repo,
3501 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3502 if (err)
3503 break;
3505 free(fileindex_path);
3506 got_fileindex_free(fileindex);
3507 return err;
3510 const struct got_error *
3511 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3512 const char *arg)
3514 const struct got_error *err = NULL;
3515 char *resolved = NULL, *cwd = NULL, *path = NULL;
3516 size_t len;
3517 struct stat sb;
3519 *wt_path = NULL;
3521 cwd = getcwd(NULL, 0);
3522 if (cwd == NULL)
3523 return got_error_from_errno("getcwd");
3525 if (lstat(arg, &sb) == -1) {
3526 if (errno != ENOENT) {
3527 err = got_error_from_errno2("lstat", arg);
3528 goto done;
3531 if (S_ISLNK(sb.st_mode)) {
3533 * We cannot use realpath(3) with symlinks since we want to
3534 * operate on the symlink itself.
3535 * But we can make the path absolute, assuming it is relative
3536 * to the current working directory, and then canonicalize it.
3538 char *abspath = NULL;
3539 char canonpath[PATH_MAX];
3540 if (!got_path_is_absolute(arg)) {
3541 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3542 err = got_error_from_errno("asprintf");
3543 goto done;
3547 err = got_canonpath(abspath ? abspath : arg, canonpath,
3548 sizeof(canonpath));
3549 if (err)
3550 goto done;
3551 resolved = strdup(canonpath);
3552 if (resolved == NULL) {
3553 err = got_error_from_errno("strdup");
3554 goto done;
3556 } else {
3557 resolved = realpath(arg, NULL);
3558 if (resolved == NULL) {
3559 if (errno != ENOENT) {
3560 err = got_error_from_errno2("realpath", arg);
3561 goto done;
3563 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 goto done;
3570 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3571 strlen(got_worktree_get_root_path(worktree)))) {
3572 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3573 goto done;
3576 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3577 err = got_path_skip_common_ancestor(&path,
3578 got_worktree_get_root_path(worktree), resolved);
3579 if (err)
3580 goto done;
3581 } else {
3582 path = strdup("");
3583 if (path == NULL) {
3584 err = got_error_from_errno("strdup");
3585 goto done;
3589 /* XXX status walk can't deal with trailing slash! */
3590 len = strlen(path);
3591 while (len > 0 && path[len - 1] == '/') {
3592 path[len - 1] = '\0';
3593 len--;
3595 done:
3596 free(resolved);
3597 free(cwd);
3598 if (err == NULL)
3599 *wt_path = path;
3600 else
3601 free(path);
3602 return err;
3605 struct schedule_addition_args {
3606 struct got_worktree *worktree;
3607 struct got_fileindex *fileindex;
3608 got_worktree_checkout_cb progress_cb;
3609 void *progress_arg;
3610 struct got_repository *repo;
3613 static const struct got_error *
3614 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3615 const char *relpath, struct got_object_id *blob_id,
3616 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3617 int dirfd, const char *de_name)
3619 struct schedule_addition_args *a = arg;
3620 const struct got_error *err = NULL;
3621 struct got_fileindex_entry *ie;
3622 struct stat sb;
3623 char *ondisk_path;
3625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3626 relpath) == -1)
3627 return got_error_from_errno("asprintf");
3629 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3630 if (ie) {
3631 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3632 de_name, a->repo);
3633 if (err)
3634 goto done;
3635 /* Re-adding an existing entry is a no-op. */
3636 if (status == GOT_STATUS_ADD)
3637 goto done;
3638 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3639 if (err)
3640 goto done;
3643 if (status != GOT_STATUS_UNVERSIONED) {
3644 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3645 goto done;
3648 err = got_fileindex_entry_alloc(&ie, relpath);
3649 if (err)
3650 goto done;
3651 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3652 if (err) {
3653 got_fileindex_entry_free(ie);
3654 goto done;
3656 err = got_fileindex_entry_add(a->fileindex, ie);
3657 if (err) {
3658 got_fileindex_entry_free(ie);
3659 goto done;
3661 done:
3662 free(ondisk_path);
3663 if (err)
3664 return err;
3665 if (status == GOT_STATUS_ADD)
3666 return NULL;
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_add(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths,
3673 got_worktree_checkout_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int no_ignores)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_addition_args saa;
3682 err = lock_worktree(worktree, LOCK_EX);
3683 if (err)
3684 return err;
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3687 if (err)
3688 goto done;
3690 saa.worktree = worktree;
3691 saa.fileindex = fileindex;
3692 saa.progress_cb = progress_cb;
3693 saa.progress_arg = progress_arg;
3694 saa.repo = repo;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3699 if (err)
3700 break;
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3704 err = sync_err;
3705 done:
3706 free(fileindex_path);
3707 if (fileindex)
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3711 err = unlockerr;
3712 return err;
3715 struct schedule_deletion_args {
3716 struct got_worktree *worktree;
3717 struct got_fileindex *fileindex;
3718 got_worktree_delete_cb progress_cb;
3719 void *progress_arg;
3720 struct got_repository *repo;
3721 int delete_local_mods;
3722 int keep_on_disk;
3725 static const struct got_error *
3726 schedule_for_deletion(void *arg, unsigned char status,
3727 unsigned char staged_status, const char *relpath,
3728 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3729 struct got_object_id *commit_id, int dirfd, const char *de_name)
3731 struct schedule_deletion_args *a = arg;
3732 const struct got_error *err = NULL;
3733 struct got_fileindex_entry *ie = NULL;
3734 struct stat sb;
3735 char *ondisk_path, *parent = NULL;
3737 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3738 if (ie == NULL)
3739 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3741 staged_status = get_staged_status(ie);
3742 if (staged_status != GOT_STATUS_NO_CHANGE) {
3743 if (staged_status == GOT_STATUS_DELETE)
3744 return NULL;
3745 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3748 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3749 relpath) == -1)
3750 return got_error_from_errno("asprintf");
3752 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3753 a->repo);
3754 if (err)
3755 goto done;
3757 if (status != GOT_STATUS_NO_CHANGE) {
3758 if (status == GOT_STATUS_DELETE)
3759 goto done;
3760 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3761 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3762 goto done;
3764 if (status != GOT_STATUS_MODIFY &&
3765 status != GOT_STATUS_MISSING) {
3766 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3767 goto done;
3771 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3772 if (dirfd != -1) {
3773 if (unlinkat(dirfd, de_name, 0) != 0) {
3774 err = got_error_from_errno2("unlinkat",
3775 ondisk_path);
3776 goto done;
3778 } else if (unlink(ondisk_path) != 0) {
3779 err = got_error_from_errno2("unlink", ondisk_path);
3780 goto done;
3783 parent = dirname(ondisk_path);
3785 if (parent == NULL) {
3786 err = got_error_from_errno2("dirname", ondisk_path);
3787 goto done;
3789 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3790 if (rmdir(parent) == -1) {
3791 if (errno != ENOTEMPTY)
3792 err = got_error_from_errno2("rmdir",
3793 parent);
3794 break;
3796 parent = dirname(parent);
3797 if (parent == NULL) {
3798 err = got_error_from_errno2("dirname", parent);
3799 goto done;
3804 got_fileindex_entry_mark_deleted_from_disk(ie);
3805 done:
3806 free(ondisk_path);
3807 if (err)
3808 return err;
3809 if (status == GOT_STATUS_DELETE)
3810 return NULL;
3811 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3812 staged_status, relpath);
3815 const struct got_error *
3816 got_worktree_schedule_delete(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, int delete_local_mods,
3818 got_worktree_delete_cb progress_cb, void *progress_arg,
3819 struct got_repository *repo, int keep_on_disk)
3821 struct got_fileindex *fileindex = NULL;
3822 char *fileindex_path = NULL;
3823 const struct got_error *err = NULL, *sync_err, *unlockerr;
3824 struct got_pathlist_entry *pe;
3825 struct schedule_deletion_args sda;
3827 err = lock_worktree(worktree, LOCK_EX);
3828 if (err)
3829 return err;
3831 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3832 if (err)
3833 goto done;
3835 sda.worktree = worktree;
3836 sda.fileindex = fileindex;
3837 sda.progress_cb = progress_cb;
3838 sda.progress_arg = progress_arg;
3839 sda.repo = repo;
3840 sda.delete_local_mods = delete_local_mods;
3841 sda.keep_on_disk = keep_on_disk;
3843 TAILQ_FOREACH(pe, paths, entry) {
3844 err = worktree_status(worktree, pe->path, fileindex, repo,
3845 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3846 if (err)
3847 break;
3849 sync_err = sync_fileindex(fileindex, fileindex_path);
3850 if (sync_err && err == NULL)
3851 err = sync_err;
3852 done:
3853 free(fileindex_path);
3854 if (fileindex)
3855 got_fileindex_free(fileindex);
3856 unlockerr = lock_worktree(worktree, LOCK_SH);
3857 if (unlockerr && err == NULL)
3858 err = unlockerr;
3859 return err;
3862 static const struct got_error *
3863 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3865 const struct got_error *err = NULL;
3866 char *line = NULL;
3867 size_t linesize = 0, n;
3868 ssize_t linelen;
3870 linelen = getline(&line, &linesize, infile);
3871 if (linelen == -1) {
3872 if (ferror(infile)) {
3873 err = got_error_from_errno("getline");
3874 goto done;
3876 return NULL;
3878 if (outfile) {
3879 n = fwrite(line, 1, linelen, outfile);
3880 if (n != linelen) {
3881 err = got_ferror(outfile, GOT_ERR_IO);
3882 goto done;
3885 if (rejectfile) {
3886 n = fwrite(line, 1, linelen, rejectfile);
3887 if (n != linelen)
3888 err = got_ferror(outfile, GOT_ERR_IO);
3890 done:
3891 free(line);
3892 return err;
3895 static const struct got_error *
3896 skip_one_line(FILE *f)
3898 char *line = NULL;
3899 size_t linesize = 0;
3900 ssize_t linelen;
3902 linelen = getline(&line, &linesize, f);
3903 if (linelen == -1) {
3904 if (ferror(f))
3905 return got_error_from_errno("getline");
3906 return NULL;
3908 free(line);
3909 return NULL;
3912 static const struct got_error *
3913 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3914 int start_old, int end_old, int start_new, int end_new,
3915 FILE *outfile, FILE *rejectfile)
3917 const struct got_error *err;
3919 /* Copy old file's lines leading up to patch. */
3920 while (!feof(f1) && *line_cur1 < start_old) {
3921 err = copy_one_line(f1, outfile, NULL);
3922 if (err)
3923 return err;
3924 (*line_cur1)++;
3926 /* Skip new file's lines leading up to patch. */
3927 while (!feof(f2) && *line_cur2 < start_new) {
3928 if (rejectfile)
3929 err = copy_one_line(f2, NULL, rejectfile);
3930 else
3931 err = skip_one_line(f2);
3932 if (err)
3933 return err;
3934 (*line_cur2)++;
3936 /* Copy patched lines. */
3937 while (!feof(f2) && *line_cur2 <= end_new) {
3938 err = copy_one_line(f2, outfile, NULL);
3939 if (err)
3940 return err;
3941 (*line_cur2)++;
3943 /* Skip over old file's replaced lines. */
3944 while (!feof(f1) && *line_cur1 <= end_old) {
3945 if (rejectfile)
3946 err = copy_one_line(f1, NULL, rejectfile);
3947 else
3948 err = skip_one_line(f1);
3949 if (err)
3950 return err;
3951 (*line_cur1)++;
3954 return NULL;
3957 static const struct got_error *
3958 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3959 FILE *outfile, FILE *rejectfile)
3961 const struct got_error *err;
3963 if (outfile) {
3964 /* Copy old file's lines until EOF. */
3965 while (!feof(f1)) {
3966 err = copy_one_line(f1, outfile, NULL);
3967 if (err)
3968 return err;
3969 (*line_cur1)++;
3972 if (rejectfile) {
3973 /* Copy new file's lines until EOF. */
3974 while (!feof(f2)) {
3975 err = copy_one_line(f2, NULL, rejectfile);
3976 if (err)
3977 return err;
3978 (*line_cur2)++;
3982 return NULL;
3985 static const struct got_error *
3986 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3987 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3988 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3989 int *line_cur2, FILE *outfile, FILE *rejectfile,
3990 got_worktree_patch_cb patch_cb, void *patch_arg)
3992 const struct got_error *err = NULL;
3993 int start_old = change->cv.a;
3994 int end_old = change->cv.b;
3995 int start_new = change->cv.c;
3996 int end_new = change->cv.d;
3997 long pos1, pos2;
3998 FILE *hunkfile;
4000 *choice = GOT_PATCH_CHOICE_NONE;
4002 hunkfile = got_opentemp();
4003 if (hunkfile == NULL)
4004 return got_error_from_errno("got_opentemp");
4006 pos1 = ftell(f1);
4007 pos2 = ftell(f2);
4009 /* XXX TODO needs error checking */
4010 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4012 if (fseek(f1, pos1, SEEK_SET) == -1) {
4013 err = got_ferror(f1, GOT_ERR_IO);
4014 goto done;
4016 if (fseek(f2, pos2, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4018 goto done;
4020 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4021 err = got_ferror(hunkfile, GOT_ERR_IO);
4022 goto done;
4025 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4026 hunkfile, n, nchanges);
4027 if (err)
4028 goto done;
4030 switch (*choice) {
4031 case GOT_PATCH_CHOICE_YES:
4032 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4033 end_old, start_new, end_new, outfile, rejectfile);
4034 break;
4035 case GOT_PATCH_CHOICE_NO:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, rejectfile, outfile);
4038 break;
4039 case GOT_PATCH_CHOICE_QUIT:
4040 break;
4041 default:
4042 err = got_error(GOT_ERR_PATCH_CHOICE);
4043 break;
4045 done:
4046 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4047 err = got_error_from_errno("fclose");
4048 return err;
4051 struct revert_file_args {
4052 struct got_worktree *worktree;
4053 struct got_fileindex *fileindex;
4054 got_worktree_checkout_cb progress_cb;
4055 void *progress_arg;
4056 got_worktree_patch_cb patch_cb;
4057 void *patch_arg;
4058 struct got_repository *repo;
4061 static const struct got_error *
4062 create_patched_content(char **path_outfile, int reverse_patch,
4063 struct got_object_id *blob_id, const char *path2,
4064 int dirfd2, const char *de_name2,
4065 const char *relpath, struct got_repository *repo,
4066 got_worktree_patch_cb patch_cb, void *patch_arg)
4068 const struct got_error *err;
4069 struct got_blob_object *blob = NULL;
4070 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4071 int fd2 = -1;
4072 char link_target[PATH_MAX];
4073 ssize_t link_len = 0;
4074 char *path1 = NULL, *id_str = NULL;
4075 struct stat sb1, sb2;
4076 struct got_diff_changes *changes = NULL;
4077 struct got_diff_state *ds = NULL;
4078 struct got_diff_args *args = NULL;
4079 struct got_diff_change *change;
4080 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4081 int n = 0;
4083 *path_outfile = NULL;
4085 err = got_object_id_str(&id_str, blob_id);
4086 if (err)
4087 return err;
4089 if (dirfd2 != -1) {
4090 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4091 if (fd2 == -1) {
4092 if (errno != ELOOP) {
4093 err = got_error_from_errno2("openat", path2);
4094 goto done;
4096 link_len = readlinkat(dirfd2, de_name2,
4097 link_target, sizeof(link_target));
4098 if (link_len == -1)
4099 return got_error_from_errno2("readlinkat", path2);
4100 sb2.st_mode = S_IFLNK;
4101 sb2.st_size = link_len;
4103 } else {
4104 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4105 if (fd2 == -1) {
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("open", path2);
4108 goto done;
4110 link_len = readlink(path2, link_target,
4111 sizeof(link_target));
4112 if (link_len == -1)
4113 return got_error_from_errno2("readlink", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4118 if (fd2 != -1) {
4119 if (fstat(fd2, &sb2) == -1) {
4120 err = got_error_from_errno2("fstat", path2);
4121 goto done;
4124 f2 = fdopen(fd2, "r");
4125 if (f2 == NULL) {
4126 err = got_error_from_errno2("fdopen", path2);
4127 goto done;
4129 fd2 = -1;
4130 } else {
4131 size_t n;
4132 f2 = got_opentemp();
4133 if (f2 == NULL) {
4134 err = got_error_from_errno2("got_opentemp", path2);
4135 goto done;
4137 n = fwrite(link_target, 1, link_len, f2);
4138 if (n != link_len) {
4139 err = got_ferror(f2, GOT_ERR_IO);
4140 goto done;
4142 if (fflush(f2) == EOF) {
4143 err = got_error_from_errno("fflush");
4144 goto done;
4146 rewind(f2);
4149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4150 if (err)
4151 goto done;
4153 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4154 if (err)
4155 goto done;
4157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4158 if (err)
4159 goto done;
4161 if (stat(path1, &sb1) == -1) {
4162 err = got_error_from_errno2("stat", path1);
4163 goto done;
4166 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4167 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4168 if (err)
4169 goto done;
4171 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4172 if (err)
4173 goto done;
4175 if (fseek(f1, 0L, SEEK_SET) == -1)
4176 return got_ferror(f1, GOT_ERR_IO);
4177 if (fseek(f2, 0L, SEEK_SET) == -1)
4178 return got_ferror(f2, GOT_ERR_IO);
4179 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4180 int choice;
4181 err = apply_or_reject_change(&choice, change, ++n,
4182 changes->nchanges, ds, args, diff_flags, relpath,
4183 f1, f2, &line_cur1, &line_cur2,
4184 reverse_patch ? NULL : outfile,
4185 reverse_patch ? outfile : NULL,
4186 patch_cb, patch_arg);
4187 if (err)
4188 goto done;
4189 if (choice == GOT_PATCH_CHOICE_YES)
4190 have_content = 1;
4191 else if (choice == GOT_PATCH_CHOICE_QUIT)
4192 break;
4194 if (have_content) {
4195 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4196 reverse_patch ? NULL : outfile,
4197 reverse_patch ? outfile : NULL);
4198 if (err)
4199 goto done;
4201 if (!S_ISLNK(sb2.st_mode)) {
4202 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4203 err = got_error_from_errno2("chmod", path2);
4204 goto done;
4208 done:
4209 free(id_str);
4210 if (blob)
4211 got_object_blob_close(blob);
4212 if (f1 && fclose(f1) == EOF && err == NULL)
4213 err = got_error_from_errno2("fclose", path1);
4214 if (f2 && fclose(f2) == EOF && err == NULL)
4215 err = got_error_from_errno2("fclose", path2);
4216 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4217 err = got_error_from_errno2("close", path2);
4218 if (outfile && fclose(outfile) == EOF && err == NULL)
4219 err = got_error_from_errno2("fclose", *path_outfile);
4220 if (path1 && unlink(path1) == -1 && err == NULL)
4221 err = got_error_from_errno2("unlink", path1);
4222 if (err || !have_content) {
4223 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4224 err = got_error_from_errno2("unlink", *path_outfile);
4225 free(*path_outfile);
4226 *path_outfile = NULL;
4228 free(args);
4229 if (ds) {
4230 got_diff_state_free(ds);
4231 free(ds);
4233 if (changes)
4234 got_diff_free_changes(changes);
4235 free(path1);
4236 return err;
4239 static const struct got_error *
4240 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4241 const char *relpath, struct got_object_id *blob_id,
4242 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4243 int dirfd, const char *de_name)
4245 struct revert_file_args *a = arg;
4246 const struct got_error *err = NULL;
4247 char *parent_path = NULL;
4248 struct got_fileindex_entry *ie;
4249 struct got_tree_object *tree = NULL;
4250 struct got_object_id *tree_id = NULL;
4251 const struct got_tree_entry *te = NULL;
4252 char *tree_path = NULL, *te_name;
4253 char *ondisk_path = NULL, *path_content = NULL;
4254 struct got_blob_object *blob = NULL;
4256 /* Reverting a staged deletion is a no-op. */
4257 if (status == GOT_STATUS_DELETE &&
4258 staged_status != GOT_STATUS_NO_CHANGE)
4259 return NULL;
4261 if (status == GOT_STATUS_UNVERSIONED)
4262 return (*a->progress_cb)(a->progress_arg,
4263 GOT_STATUS_UNVERSIONED, relpath);
4265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4266 if (ie == NULL)
4267 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4269 /* Construct in-repository path of tree which contains this blob. */
4270 err = got_path_dirname(&parent_path, ie->path);
4271 if (err) {
4272 if (err->code != GOT_ERR_BAD_PATH)
4273 goto done;
4274 parent_path = strdup("/");
4275 if (parent_path == NULL) {
4276 err = got_error_from_errno("strdup");
4277 goto done;
4280 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4281 tree_path = strdup(parent_path);
4282 if (tree_path == NULL) {
4283 err = got_error_from_errno("strdup");
4284 goto done;
4286 } else {
4287 if (got_path_is_root_dir(parent_path)) {
4288 tree_path = strdup(a->worktree->path_prefix);
4289 if (tree_path == NULL) {
4290 err = got_error_from_errno("strdup");
4291 goto done;
4293 } else {
4294 if (asprintf(&tree_path, "%s/%s",
4295 a->worktree->path_prefix, parent_path) == -1) {
4296 err = got_error_from_errno("asprintf");
4297 goto done;
4302 err = got_object_id_by_path(&tree_id, a->repo,
4303 a->worktree->base_commit_id, tree_path);
4304 if (err) {
4305 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4306 (status == GOT_STATUS_ADD ||
4307 staged_status == GOT_STATUS_ADD)))
4308 goto done;
4309 } else {
4310 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4311 if (err)
4312 goto done;
4314 te_name = basename(ie->path);
4315 if (te_name == NULL) {
4316 err = got_error_from_errno2("basename", ie->path);
4317 goto done;
4320 te = got_object_tree_find_entry(tree, te_name);
4321 if (te == NULL && status != GOT_STATUS_ADD &&
4322 staged_status != GOT_STATUS_ADD) {
4323 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4324 goto done;
4328 switch (status) {
4329 case GOT_STATUS_ADD:
4330 if (a->patch_cb) {
4331 int choice = GOT_PATCH_CHOICE_NONE;
4332 err = (*a->patch_cb)(&choice, a->patch_arg,
4333 status, ie->path, NULL, 1, 1);
4334 if (err)
4335 goto done;
4336 if (choice != GOT_PATCH_CHOICE_YES)
4337 break;
4339 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4340 ie->path);
4341 if (err)
4342 goto done;
4343 got_fileindex_entry_remove(a->fileindex, ie);
4344 break;
4345 case GOT_STATUS_DELETE:
4346 if (a->patch_cb) {
4347 int choice = GOT_PATCH_CHOICE_NONE;
4348 err = (*a->patch_cb)(&choice, a->patch_arg,
4349 status, ie->path, NULL, 1, 1);
4350 if (err)
4351 goto done;
4352 if (choice != GOT_PATCH_CHOICE_YES)
4353 break;
4355 /* fall through */
4356 case GOT_STATUS_MODIFY:
4357 case GOT_STATUS_MODE_CHANGE:
4358 case GOT_STATUS_CONFLICT:
4359 case GOT_STATUS_MISSING: {
4360 struct got_object_id id;
4361 if (staged_status == GOT_STATUS_ADD ||
4362 staged_status == GOT_STATUS_MODIFY) {
4363 memcpy(id.sha1, ie->staged_blob_sha1,
4364 SHA1_DIGEST_LENGTH);
4365 } else
4366 memcpy(id.sha1, ie->blob_sha1,
4367 SHA1_DIGEST_LENGTH);
4368 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4369 if (err)
4370 goto done;
4372 if (asprintf(&ondisk_path, "%s/%s",
4373 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4374 err = got_error_from_errno("asprintf");
4375 goto done;
4378 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4379 status == GOT_STATUS_CONFLICT)) {
4380 int is_bad_symlink = 0;
4381 err = create_patched_content(&path_content, 1, &id,
4382 ondisk_path, dirfd, de_name, ie->path, a->repo,
4383 a->patch_cb, a->patch_arg);
4384 if (err || path_content == NULL)
4385 break;
4386 if (te && S_ISLNK(te->mode)) {
4387 if (unlink(path_content) == -1) {
4388 err = got_error_from_errno2("unlink",
4389 path_content);
4390 break;
4392 err = install_symlink(&is_bad_symlink,
4393 a->worktree, ondisk_path, ie->path,
4394 blob, 0, 1, 0, a->repo,
4395 a->progress_cb, a->progress_arg);
4396 } else {
4397 if (rename(path_content, ondisk_path) == -1) {
4398 err = got_error_from_errno3("rename",
4399 path_content, ondisk_path);
4400 goto done;
4403 } else {
4404 int is_bad_symlink = 0;
4405 if (te && S_ISLNK(te->mode)) {
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4410 } else {
4411 err = install_blob(a->worktree, ondisk_path,
4412 ie->path,
4413 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4414 got_fileindex_perms_to_st(ie), blob,
4415 0, 1, 0, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4418 if (err)
4419 goto done;
4420 if (status == GOT_STATUS_DELETE ||
4421 status == GOT_STATUS_MODE_CHANGE) {
4422 err = got_fileindex_entry_update(ie,
4423 ondisk_path, blob->id.sha1,
4424 a->worktree->base_commit_id->sha1, 1);
4425 if (err)
4426 goto done;
4428 if (is_bad_symlink) {
4429 got_fileindex_entry_filetype_set(ie,
4430 GOT_FILEIDX_MODE_BAD_SYMLINK);
4433 break;
4435 default:
4436 break;
4438 done:
4439 free(ondisk_path);
4440 free(path_content);
4441 free(parent_path);
4442 free(tree_path);
4443 if (blob)
4444 got_object_blob_close(blob);
4445 if (tree)
4446 got_object_tree_close(tree);
4447 free(tree_id);
4448 return err;
4451 const struct got_error *
4452 got_worktree_revert(struct got_worktree *worktree,
4453 struct got_pathlist_head *paths,
4454 got_worktree_checkout_cb progress_cb, void *progress_arg,
4455 got_worktree_patch_cb patch_cb, void *patch_arg,
4456 struct got_repository *repo)
4458 struct got_fileindex *fileindex = NULL;
4459 char *fileindex_path = NULL;
4460 const struct got_error *err = NULL, *unlockerr = NULL;
4461 const struct got_error *sync_err = NULL;
4462 struct got_pathlist_entry *pe;
4463 struct revert_file_args rfa;
4465 err = lock_worktree(worktree, LOCK_EX);
4466 if (err)
4467 return err;
4469 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4470 if (err)
4471 goto done;
4473 rfa.worktree = worktree;
4474 rfa.fileindex = fileindex;
4475 rfa.progress_cb = progress_cb;
4476 rfa.progress_arg = progress_arg;
4477 rfa.patch_cb = patch_cb;
4478 rfa.patch_arg = patch_arg;
4479 rfa.repo = repo;
4480 TAILQ_FOREACH(pe, paths, entry) {
4481 err = worktree_status(worktree, pe->path, fileindex, repo,
4482 revert_file, &rfa, NULL, NULL, 0, 0);
4483 if (err)
4484 break;
4486 sync_err = sync_fileindex(fileindex, fileindex_path);
4487 if (sync_err && err == NULL)
4488 err = sync_err;
4489 done:
4490 free(fileindex_path);
4491 if (fileindex)
4492 got_fileindex_free(fileindex);
4493 unlockerr = lock_worktree(worktree, LOCK_SH);
4494 if (unlockerr && err == NULL)
4495 err = unlockerr;
4496 return err;
4499 static void
4500 free_commitable(struct got_commitable *ct)
4502 free(ct->path);
4503 free(ct->in_repo_path);
4504 free(ct->ondisk_path);
4505 free(ct->blob_id);
4506 free(ct->base_blob_id);
4507 free(ct->staged_blob_id);
4508 free(ct->base_commit_id);
4509 free(ct);
4512 struct collect_commitables_arg {
4513 struct got_pathlist_head *commitable_paths;
4514 struct got_repository *repo;
4515 struct got_worktree *worktree;
4516 struct got_fileindex *fileindex;
4517 int have_staged_files;
4520 static const struct got_error *
4521 collect_commitables(void *arg, unsigned char status,
4522 unsigned char staged_status, const char *relpath,
4523 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4524 struct got_object_id *commit_id, int dirfd, const char *de_name)
4526 struct collect_commitables_arg *a = arg;
4527 const struct got_error *err = NULL;
4528 struct got_commitable *ct = NULL;
4529 struct got_pathlist_entry *new = NULL;
4530 char *parent_path = NULL, *path = NULL;
4531 struct stat sb;
4533 if (a->have_staged_files) {
4534 if (staged_status != GOT_STATUS_MODIFY &&
4535 staged_status != GOT_STATUS_ADD &&
4536 staged_status != GOT_STATUS_DELETE)
4537 return NULL;
4538 } else {
4539 if (status == GOT_STATUS_CONFLICT)
4540 return got_error(GOT_ERR_COMMIT_CONFLICT);
4542 if (status != GOT_STATUS_MODIFY &&
4543 status != GOT_STATUS_MODE_CHANGE &&
4544 status != GOT_STATUS_ADD &&
4545 status != GOT_STATUS_DELETE)
4546 return NULL;
4549 if (asprintf(&path, "/%s", relpath) == -1) {
4550 err = got_error_from_errno("asprintf");
4551 goto done;
4553 if (strcmp(path, "/") == 0) {
4554 parent_path = strdup("");
4555 if (parent_path == NULL)
4556 return got_error_from_errno("strdup");
4557 } else {
4558 err = got_path_dirname(&parent_path, path);
4559 if (err)
4560 return err;
4563 ct = calloc(1, sizeof(*ct));
4564 if (ct == NULL) {
4565 err = got_error_from_errno("calloc");
4566 goto done;
4569 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4570 relpath) == -1) {
4571 err = got_error_from_errno("asprintf");
4572 goto done;
4575 if (staged_status == GOT_STATUS_ADD ||
4576 staged_status == GOT_STATUS_MODIFY) {
4577 struct got_fileindex_entry *ie;
4578 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4579 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4580 case GOT_FILEIDX_MODE_REGULAR_FILE:
4581 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4582 ct->mode = S_IFREG;
4583 break;
4584 case GOT_FILEIDX_MODE_SYMLINK:
4585 ct->mode = S_IFLNK;
4586 break;
4587 default:
4588 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4589 goto done;
4591 ct->mode |= got_fileindex_entry_perms_get(ie);
4592 } else if (status != GOT_STATUS_DELETE &&
4593 staged_status != GOT_STATUS_DELETE) {
4594 if (dirfd != -1) {
4595 if (fstatat(dirfd, de_name, &sb,
4596 AT_SYMLINK_NOFOLLOW) == -1) {
4597 err = got_error_from_errno2("fstatat",
4598 ct->ondisk_path);
4599 goto done;
4601 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4602 err = got_error_from_errno2("lstat", ct->ondisk_path);
4603 goto done;
4605 ct->mode = sb.st_mode;
4608 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4609 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4610 relpath) == -1) {
4611 err = got_error_from_errno("asprintf");
4612 goto done;
4615 ct->status = status;
4616 ct->staged_status = staged_status;
4617 ct->blob_id = NULL; /* will be filled in when blob gets created */
4618 if (ct->status != GOT_STATUS_ADD &&
4619 ct->staged_status != GOT_STATUS_ADD) {
4620 ct->base_blob_id = got_object_id_dup(blob_id);
4621 if (ct->base_blob_id == NULL) {
4622 err = got_error_from_errno("got_object_id_dup");
4623 goto done;
4625 ct->base_commit_id = got_object_id_dup(commit_id);
4626 if (ct->base_commit_id == NULL) {
4627 err = got_error_from_errno("got_object_id_dup");
4628 goto done;
4631 if (ct->staged_status == GOT_STATUS_ADD ||
4632 ct->staged_status == GOT_STATUS_MODIFY) {
4633 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4634 if (ct->staged_blob_id == NULL) {
4635 err = got_error_from_errno("got_object_id_dup");
4636 goto done;
4639 ct->path = strdup(path);
4640 if (ct->path == NULL) {
4641 err = got_error_from_errno("strdup");
4642 goto done;
4644 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4645 done:
4646 if (ct && (err || new == NULL))
4647 free_commitable(ct);
4648 free(parent_path);
4649 free(path);
4650 return err;
4653 static const struct got_error *write_tree(struct got_object_id **, int *,
4654 struct got_tree_object *, const char *, struct got_pathlist_head *,
4655 got_worktree_status_cb status_cb, void *status_arg,
4656 struct got_repository *);
4658 static const struct got_error *
4659 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4660 struct got_tree_entry *te, const char *parent_path,
4661 struct got_pathlist_head *commitable_paths,
4662 got_worktree_status_cb status_cb, void *status_arg,
4663 struct got_repository *repo)
4665 const struct got_error *err = NULL;
4666 struct got_tree_object *subtree;
4667 char *subpath;
4669 if (asprintf(&subpath, "%s%s%s", parent_path,
4670 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4671 return got_error_from_errno("asprintf");
4673 err = got_object_open_as_tree(&subtree, repo, &te->id);
4674 if (err)
4675 return err;
4677 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4678 commitable_paths, status_cb, status_arg, repo);
4679 got_object_tree_close(subtree);
4680 free(subpath);
4681 return err;
4684 static const struct got_error *
4685 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4687 const struct got_error *err = NULL;
4688 char *ct_parent_path = NULL;
4690 *match = 0;
4692 if (strchr(ct->in_repo_path, '/') == NULL) {
4693 *match = got_path_is_root_dir(path);
4694 return NULL;
4697 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4698 if (err)
4699 return err;
4700 *match = (strcmp(path, ct_parent_path) == 0);
4701 free(ct_parent_path);
4702 return err;
4705 static mode_t
4706 get_ct_file_mode(struct got_commitable *ct)
4708 if (S_ISLNK(ct->mode))
4709 return S_IFLNK;
4711 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4714 static const struct got_error *
4715 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4716 struct got_tree_entry *te, struct got_commitable *ct)
4718 const struct got_error *err = NULL;
4720 *new_te = NULL;
4722 err = got_object_tree_entry_dup(new_te, te);
4723 if (err)
4724 goto done;
4726 (*new_te)->mode = get_ct_file_mode(ct);
4728 if (ct->staged_status == GOT_STATUS_MODIFY)
4729 memcpy(&(*new_te)->id, ct->staged_blob_id,
4730 sizeof((*new_te)->id));
4731 else
4732 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4733 done:
4734 if (err && *new_te) {
4735 free(*new_te);
4736 *new_te = NULL;
4738 return err;
4741 static const struct got_error *
4742 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4743 struct got_commitable *ct)
4745 const struct got_error *err = NULL;
4746 char *ct_name;
4748 *new_te = NULL;
4750 *new_te = calloc(1, sizeof(**new_te));
4751 if (*new_te == NULL)
4752 return got_error_from_errno("calloc");
4754 ct_name = basename(ct->path);
4755 if (ct_name == NULL) {
4756 err = got_error_from_errno2("basename", ct->path);
4757 goto done;
4759 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4760 sizeof((*new_te)->name)) {
4761 err = got_error(GOT_ERR_NO_SPACE);
4762 goto done;
4765 (*new_te)->mode = get_ct_file_mode(ct);
4767 if (ct->staged_status == GOT_STATUS_ADD)
4768 memcpy(&(*new_te)->id, ct->staged_blob_id,
4769 sizeof((*new_te)->id));
4770 else
4771 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4772 done:
4773 if (err && *new_te) {
4774 free(*new_te);
4775 *new_te = NULL;
4777 return err;
4780 static const struct got_error *
4781 insert_tree_entry(struct got_tree_entry *new_te,
4782 struct got_pathlist_head *paths)
4784 const struct got_error *err = NULL;
4785 struct got_pathlist_entry *new_pe;
4787 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4788 if (err)
4789 return err;
4790 if (new_pe == NULL)
4791 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4792 return NULL;
4795 static const struct got_error *
4796 report_ct_status(struct got_commitable *ct,
4797 got_worktree_status_cb status_cb, void *status_arg)
4799 const char *ct_path = ct->path;
4800 unsigned char status;
4802 while (ct_path[0] == '/')
4803 ct_path++;
4805 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4806 status = ct->staged_status;
4807 else
4808 status = ct->status;
4810 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4811 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4814 static const struct got_error *
4815 match_modified_subtree(int *modified, struct got_tree_entry *te,
4816 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4818 const struct got_error *err = NULL;
4819 struct got_pathlist_entry *pe;
4820 char *te_path;
4822 *modified = 0;
4824 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4825 got_path_is_root_dir(base_tree_path) ? "" : "/",
4826 te->name) == -1)
4827 return got_error_from_errno("asprintf");
4829 TAILQ_FOREACH(pe, commitable_paths, entry) {
4830 struct got_commitable *ct = pe->data;
4831 *modified = got_path_is_child(ct->in_repo_path, te_path,
4832 strlen(te_path));
4833 if (*modified)
4834 break;
4837 free(te_path);
4838 return err;
4841 static const struct got_error *
4842 match_deleted_or_modified_ct(struct got_commitable **ctp,
4843 struct got_tree_entry *te, const char *base_tree_path,
4844 struct got_pathlist_head *commitable_paths)
4846 const struct got_error *err = NULL;
4847 struct got_pathlist_entry *pe;
4849 *ctp = NULL;
4851 TAILQ_FOREACH(pe, commitable_paths, entry) {
4852 struct got_commitable *ct = pe->data;
4853 char *ct_name = NULL;
4854 int path_matches;
4856 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4857 if (ct->status != GOT_STATUS_MODIFY &&
4858 ct->status != GOT_STATUS_MODE_CHANGE &&
4859 ct->status != GOT_STATUS_DELETE)
4860 continue;
4861 } else {
4862 if (ct->staged_status != GOT_STATUS_MODIFY &&
4863 ct->staged_status != GOT_STATUS_DELETE)
4864 continue;
4867 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4868 continue;
4870 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4871 if (err)
4872 return err;
4873 if (!path_matches)
4874 continue;
4876 ct_name = basename(pe->path);
4877 if (ct_name == NULL)
4878 return got_error_from_errno2("basename", pe->path);
4880 if (strcmp(te->name, ct_name) != 0)
4881 continue;
4883 *ctp = ct;
4884 break;
4887 return err;
4890 static const struct got_error *
4891 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4892 const char *child_path, const char *path_base_tree,
4893 struct got_pathlist_head *commitable_paths,
4894 got_worktree_status_cb status_cb, void *status_arg,
4895 struct got_repository *repo)
4897 const struct got_error *err = NULL;
4898 struct got_tree_entry *new_te;
4899 char *subtree_path;
4900 struct got_object_id *id = NULL;
4901 int nentries;
4903 *new_tep = NULL;
4905 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4906 got_path_is_root_dir(path_base_tree) ? "" : "/",
4907 child_path) == -1)
4908 return got_error_from_errno("asprintf");
4910 new_te = calloc(1, sizeof(*new_te));
4911 if (new_te == NULL)
4912 return got_error_from_errno("calloc");
4913 new_te->mode = S_IFDIR;
4915 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4916 sizeof(new_te->name)) {
4917 err = got_error(GOT_ERR_NO_SPACE);
4918 goto done;
4920 err = write_tree(&id, &nentries, NULL, subtree_path,
4921 commitable_paths, status_cb, status_arg, repo);
4922 if (err) {
4923 free(new_te);
4924 goto done;
4926 memcpy(&new_te->id, id, sizeof(new_te->id));
4927 done:
4928 free(id);
4929 free(subtree_path);
4930 if (err == NULL)
4931 *new_tep = new_te;
4932 return err;
4935 static const struct got_error *
4936 write_tree(struct got_object_id **new_tree_id, int *nentries,
4937 struct got_tree_object *base_tree, const char *path_base_tree,
4938 struct got_pathlist_head *commitable_paths,
4939 got_worktree_status_cb status_cb, void *status_arg,
4940 struct got_repository *repo)
4942 const struct got_error *err = NULL;
4943 struct got_pathlist_head paths;
4944 struct got_tree_entry *te, *new_te = NULL;
4945 struct got_pathlist_entry *pe;
4947 TAILQ_INIT(&paths);
4948 *nentries = 0;
4950 /* Insert, and recurse into, newly added entries first. */
4951 TAILQ_FOREACH(pe, commitable_paths, entry) {
4952 struct got_commitable *ct = pe->data;
4953 char *child_path = NULL, *slash;
4955 if ((ct->status != GOT_STATUS_ADD &&
4956 ct->staged_status != GOT_STATUS_ADD) ||
4957 (ct->flags & GOT_COMMITABLE_ADDED))
4958 continue;
4960 if (!got_path_is_child(pe->path, path_base_tree,
4961 strlen(path_base_tree)))
4962 continue;
4964 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4965 pe->path);
4966 if (err)
4967 goto done;
4969 slash = strchr(child_path, '/');
4970 if (slash == NULL) {
4971 err = alloc_added_blob_tree_entry(&new_te, ct);
4972 if (err)
4973 goto done;
4974 err = report_ct_status(ct, status_cb, status_arg);
4975 if (err)
4976 goto done;
4977 ct->flags |= GOT_COMMITABLE_ADDED;
4978 err = insert_tree_entry(new_te, &paths);
4979 if (err)
4980 goto done;
4981 (*nentries)++;
4982 } else {
4983 *slash = '\0'; /* trim trailing path components */
4984 if (base_tree == NULL ||
4985 got_object_tree_find_entry(base_tree, child_path)
4986 == NULL) {
4987 err = make_subtree_for_added_blob(&new_te,
4988 child_path, path_base_tree,
4989 commitable_paths, status_cb, status_arg,
4990 repo);
4991 if (err)
4992 goto done;
4993 err = insert_tree_entry(new_te, &paths);
4994 if (err)
4995 goto done;
4996 (*nentries)++;
5001 if (base_tree) {
5002 int i, nbase_entries;
5003 /* Handle modified and deleted entries. */
5004 nbase_entries = got_object_tree_get_nentries(base_tree);
5005 for (i = 0; i < nbase_entries; i++) {
5006 struct got_commitable *ct = NULL;
5008 te = got_object_tree_get_entry(base_tree, i);
5009 if (got_object_tree_entry_is_submodule(te)) {
5010 /* Entry is a submodule; just copy it. */
5011 err = got_object_tree_entry_dup(&new_te, te);
5012 if (err)
5013 goto done;
5014 err = insert_tree_entry(new_te, &paths);
5015 if (err)
5016 goto done;
5017 (*nentries)++;
5018 continue;
5021 if (S_ISDIR(te->mode)) {
5022 int modified;
5023 err = got_object_tree_entry_dup(&new_te, te);
5024 if (err)
5025 goto done;
5026 err = match_modified_subtree(&modified, te,
5027 path_base_tree, commitable_paths);
5028 if (err)
5029 goto done;
5030 /* Avoid recursion into unmodified subtrees. */
5031 if (modified) {
5032 struct got_object_id *new_id;
5033 int nsubentries;
5034 err = write_subtree(&new_id,
5035 &nsubentries, te,
5036 path_base_tree, commitable_paths,
5037 status_cb, status_arg, repo);
5038 if (err)
5039 goto done;
5040 if (nsubentries == 0) {
5041 /* All entries were deleted. */
5042 free(new_id);
5043 continue;
5045 memcpy(&new_te->id, new_id,
5046 sizeof(new_te->id));
5047 free(new_id);
5049 err = insert_tree_entry(new_te, &paths);
5050 if (err)
5051 goto done;
5052 (*nentries)++;
5053 continue;
5056 err = match_deleted_or_modified_ct(&ct, te,
5057 path_base_tree, commitable_paths);
5058 if (err)
5059 goto done;
5060 if (ct) {
5061 /* NB: Deleted entries get dropped here. */
5062 if (ct->status == GOT_STATUS_MODIFY ||
5063 ct->status == GOT_STATUS_MODE_CHANGE ||
5064 ct->staged_status == GOT_STATUS_MODIFY) {
5065 err = alloc_modified_blob_tree_entry(
5066 &new_te, te, ct);
5067 if (err)
5068 goto done;
5069 err = insert_tree_entry(new_te, &paths);
5070 if (err)
5071 goto done;
5072 (*nentries)++;
5074 err = report_ct_status(ct, status_cb,
5075 status_arg);
5076 if (err)
5077 goto done;
5078 } else {
5079 /* Entry is unchanged; just copy it. */
5080 err = got_object_tree_entry_dup(&new_te, te);
5081 if (err)
5082 goto done;
5083 err = insert_tree_entry(new_te, &paths);
5084 if (err)
5085 goto done;
5086 (*nentries)++;
5091 /* Write new list of entries; deleted entries have been dropped. */
5092 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5093 done:
5094 got_pathlist_free(&paths);
5095 return err;
5098 static const struct got_error *
5099 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5100 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5101 struct got_fileindex_entry *ie, struct got_repository *repo)
5103 const struct got_error *err = NULL;
5104 struct got_blob_object *blob = NULL;
5105 struct got_object_id *tree_id = NULL;
5106 char *tree_path = NULL;
5107 struct got_tree_object *tree = NULL;
5108 struct got_tree_entry *te;
5109 char *entry_name;
5110 unsigned char status;
5111 struct stat sb;
5113 err = get_file_status(&status, &sb, ie, ct->ondisk_path,
5114 -1, NULL, repo);
5115 if (err)
5116 return err;
5117 if (status != GOT_STATUS_NO_CHANGE)
5118 return NULL;
5120 if (ct->staged_status == GOT_STATUS_ADD ||
5121 ct->staged_status == GOT_STATUS_MODIFY) {
5122 err = got_object_open_as_blob(&blob, repo, ct->staged_blob_id,
5123 PATH_MAX);
5124 if (err)
5125 return err;
5126 } else {
5127 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5128 PATH_MAX);
5129 if (err)
5130 return err;
5133 err = got_path_dirname(&tree_path, ct->in_repo_path);
5134 if (err) {
5135 if (err->code != GOT_ERR_BAD_PATH)
5136 goto done;
5137 err = got_object_id_by_path(&tree_id, repo,
5138 new_base_commit_id, "");
5139 if (err)
5140 goto done;
5141 } else {
5142 err = got_object_id_by_path(&tree_id, repo,
5143 new_base_commit_id, tree_path);
5144 if (err)
5145 goto done;
5148 err = got_object_open_as_tree(&tree, repo, tree_id);
5149 if (err)
5150 goto done;
5152 entry_name = basename(ct->path);
5153 if (entry_name == NULL) {
5154 err = got_error_from_errno2("basename", ct->path);
5155 goto done;
5158 te = got_object_tree_find_entry(tree, entry_name);
5159 if (te == NULL) {
5160 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5161 goto done;
5164 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5165 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5166 done:
5167 if (blob)
5168 got_object_blob_close(blob);
5169 if (tree)
5170 got_object_tree_close(tree);
5171 free(tree_id);
5172 free(tree_path);
5173 return err;
5177 * After comitting a symlink we have a chance to convert "bad" symlinks
5178 * (those which point outside the work tree or into .got) to regular files.
5179 * This way, the post-commit work tree state matches a fresh checkout of
5180 * the tree which was just committed. We also mark such newly committed
5181 * symlinks as "bad" in the work tree's fileindex.
5183 static const struct got_error *
5184 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5185 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5186 struct got_worktree *worktree, struct got_repository *repo)
5188 const struct got_error *err = NULL;
5189 struct got_pathlist_entry *pe;
5191 TAILQ_FOREACH(pe, commitable_paths, entry) {
5192 struct got_commitable *ct = pe->data;
5193 struct got_fileindex_entry *ie;
5194 int is_bad_symlink = 0;
5196 if (!S_ISLNK(get_ct_file_mode(ct)) ||
5197 ct->staged_status == GOT_STATUS_DELETE ||
5198 ct->status == GOT_STATUS_DELETE)
5199 continue;
5201 ie = got_fileindex_entry_get(fileindex, ct->path,
5202 strlen(ct->path));
5203 if (ie == NULL) {
5204 err = got_error_path(ct->path, GOT_ERR_BAD_PATH);
5205 break;
5207 err = reinstall_symlink_after_commit(&is_bad_symlink,
5208 ct, new_base_commit_id, worktree, ie, repo);
5209 if (err)
5210 break;
5211 if (ie && is_bad_symlink) {
5212 got_fileindex_entry_filetype_set(ie,
5213 GOT_FILEIDX_MODE_BAD_SYMLINK);
5217 return err;
5220 static const struct got_error *
5221 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5222 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5223 int have_staged_files)
5225 const struct got_error *err = NULL;
5226 struct got_pathlist_entry *pe;
5228 TAILQ_FOREACH(pe, commitable_paths, entry) {
5229 struct got_fileindex_entry *ie;
5230 struct got_commitable *ct = pe->data;
5232 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5233 if (ie) {
5234 if (ct->status == GOT_STATUS_DELETE ||
5235 ct->staged_status == GOT_STATUS_DELETE) {
5236 got_fileindex_entry_remove(fileindex, ie);
5237 } else if (ct->staged_status == GOT_STATUS_ADD ||
5238 ct->staged_status == GOT_STATUS_MODIFY) {
5239 got_fileindex_entry_stage_set(ie,
5240 GOT_FILEIDX_STAGE_NONE);
5241 err = got_fileindex_entry_update(ie,
5242 ct->ondisk_path, ct->staged_blob_id->sha1,
5243 new_base_commit_id->sha1,
5244 !have_staged_files);
5245 } else
5246 err = got_fileindex_entry_update(ie,
5247 ct->ondisk_path, ct->blob_id->sha1,
5248 new_base_commit_id->sha1,
5249 !have_staged_files);
5250 } else {
5251 err = got_fileindex_entry_alloc(&ie, pe->path);
5252 if (err)
5253 break;
5254 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5255 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5256 if (err) {
5257 got_fileindex_entry_free(ie);
5258 break;
5260 err = got_fileindex_entry_add(fileindex, ie);
5261 if (err) {
5262 got_fileindex_entry_free(ie);
5263 break;
5267 return err;
5271 static const struct got_error *
5272 check_out_of_date(const char *in_repo_path, unsigned char status,
5273 unsigned char staged_status, struct got_object_id *base_blob_id,
5274 struct got_object_id *base_commit_id,
5275 struct got_object_id *head_commit_id, struct got_repository *repo,
5276 int ood_errcode)
5278 const struct got_error *err = NULL;
5279 struct got_object_id *id = NULL;
5281 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5282 /* Trivial case: base commit == head commit */
5283 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5284 return NULL;
5286 * Ensure file content which local changes were based
5287 * on matches file content in the branch head.
5289 err = got_object_id_by_path(&id, repo, head_commit_id,
5290 in_repo_path);
5291 if (err) {
5292 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5293 err = got_error(ood_errcode);
5294 goto done;
5295 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5296 err = got_error(ood_errcode);
5297 } else {
5298 /* Require that added files don't exist in the branch head. */
5299 err = got_object_id_by_path(&id, repo, head_commit_id,
5300 in_repo_path);
5301 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5302 goto done;
5303 err = id ? got_error(ood_errcode) : NULL;
5305 done:
5306 free(id);
5307 return err;
5310 const struct got_error *
5311 commit_worktree(struct got_object_id **new_commit_id,
5312 struct got_pathlist_head *commitable_paths,
5313 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5314 const char *author, const char *committer,
5315 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5316 got_worktree_status_cb status_cb, void *status_arg,
5317 struct got_repository *repo)
5319 const struct got_error *err = NULL, *unlockerr = NULL;
5320 struct got_pathlist_entry *pe;
5321 const char *head_ref_name = NULL;
5322 struct got_commit_object *head_commit = NULL;
5323 struct got_reference *head_ref2 = NULL;
5324 struct got_object_id *head_commit_id2 = NULL;
5325 struct got_tree_object *head_tree = NULL;
5326 struct got_object_id *new_tree_id = NULL;
5327 int nentries;
5328 struct got_object_id_queue parent_ids;
5329 struct got_object_qid *pid = NULL;
5330 char *logmsg = NULL;
5332 *new_commit_id = NULL;
5334 SIMPLEQ_INIT(&parent_ids);
5336 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5337 if (err)
5338 goto done;
5340 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5341 if (err)
5342 goto done;
5344 if (commit_msg_cb != NULL) {
5345 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5346 if (err)
5347 goto done;
5350 if (logmsg == NULL || strlen(logmsg) == 0) {
5351 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5352 goto done;
5355 /* Create blobs from added and modified files and record their IDs. */
5356 TAILQ_FOREACH(pe, commitable_paths, entry) {
5357 struct got_commitable *ct = pe->data;
5358 char *ondisk_path;
5360 /* Blobs for staged files already exist. */
5361 if (ct->staged_status == GOT_STATUS_ADD ||
5362 ct->staged_status == GOT_STATUS_MODIFY)
5363 continue;
5365 if (ct->status != GOT_STATUS_ADD &&
5366 ct->status != GOT_STATUS_MODIFY &&
5367 ct->status != GOT_STATUS_MODE_CHANGE)
5368 continue;
5370 if (asprintf(&ondisk_path, "%s/%s",
5371 worktree->root_path, pe->path) == -1) {
5372 err = got_error_from_errno("asprintf");
5373 goto done;
5375 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5376 free(ondisk_path);
5377 if (err)
5378 goto done;
5381 /* Recursively write new tree objects. */
5382 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5383 commitable_paths, status_cb, status_arg, repo);
5384 if (err)
5385 goto done;
5387 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5388 if (err)
5389 goto done;
5390 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5391 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5392 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5393 got_object_qid_free(pid);
5394 if (logmsg != NULL)
5395 free(logmsg);
5396 if (err)
5397 goto done;
5399 /* Check if a concurrent commit to our branch has occurred. */
5400 head_ref_name = got_worktree_get_head_ref_name(worktree);
5401 if (head_ref_name == NULL) {
5402 err = got_error_from_errno("got_worktree_get_head_ref_name");
5403 goto done;
5405 /* Lock the reference here to prevent concurrent modification. */
5406 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5407 if (err)
5408 goto done;
5409 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5410 if (err)
5411 goto done;
5412 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5413 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5414 goto done;
5416 /* Update branch head in repository. */
5417 err = got_ref_change_ref(head_ref2, *new_commit_id);
5418 if (err)
5419 goto done;
5420 err = got_ref_write(head_ref2, repo);
5421 if (err)
5422 goto done;
5424 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5425 if (err)
5426 goto done;
5428 err = ref_base_commit(worktree, repo);
5429 if (err)
5430 goto done;
5431 done:
5432 if (head_tree)
5433 got_object_tree_close(head_tree);
5434 if (head_commit)
5435 got_object_commit_close(head_commit);
5436 free(head_commit_id2);
5437 if (head_ref2) {
5438 unlockerr = got_ref_unlock(head_ref2);
5439 if (unlockerr && err == NULL)
5440 err = unlockerr;
5441 got_ref_close(head_ref2);
5443 return err;
5446 static const struct got_error *
5447 check_path_is_commitable(const char *path,
5448 struct got_pathlist_head *commitable_paths)
5450 struct got_pathlist_entry *cpe = NULL;
5451 size_t path_len = strlen(path);
5453 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5454 struct got_commitable *ct = cpe->data;
5455 const char *ct_path = ct->path;
5457 while (ct_path[0] == '/')
5458 ct_path++;
5460 if (strcmp(path, ct_path) == 0 ||
5461 got_path_is_child(ct_path, path, path_len))
5462 break;
5465 if (cpe == NULL)
5466 return got_error_path(path, GOT_ERR_BAD_PATH);
5468 return NULL;
5471 static const struct got_error *
5472 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5474 int *have_staged_files = arg;
5476 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5477 *have_staged_files = 1;
5478 return got_error(GOT_ERR_CANCELLED);
5481 return NULL;
5484 static const struct got_error *
5485 check_non_staged_files(struct got_fileindex *fileindex,
5486 struct got_pathlist_head *paths)
5488 struct got_pathlist_entry *pe;
5489 struct got_fileindex_entry *ie;
5491 TAILQ_FOREACH(pe, paths, entry) {
5492 if (pe->path[0] == '\0')
5493 continue;
5494 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5495 if (ie == NULL)
5496 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5497 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5498 return got_error_path(pe->path,
5499 GOT_ERR_FILE_NOT_STAGED);
5502 return NULL;
5505 const struct got_error *
5506 got_worktree_commit(struct got_object_id **new_commit_id,
5507 struct got_worktree *worktree, struct got_pathlist_head *paths,
5508 const char *author, const char *committer,
5509 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5510 got_worktree_status_cb status_cb, void *status_arg,
5511 struct got_repository *repo)
5513 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5514 struct got_fileindex *fileindex = NULL;
5515 char *fileindex_path = NULL;
5516 struct got_pathlist_head commitable_paths;
5517 struct collect_commitables_arg cc_arg;
5518 struct got_pathlist_entry *pe;
5519 struct got_reference *head_ref = NULL;
5520 struct got_object_id *head_commit_id = NULL;
5521 int have_staged_files = 0;
5523 *new_commit_id = NULL;
5525 TAILQ_INIT(&commitable_paths);
5527 err = lock_worktree(worktree, LOCK_EX);
5528 if (err)
5529 goto done;
5531 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5532 if (err)
5533 goto done;
5535 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5536 if (err)
5537 goto done;
5539 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5540 if (err)
5541 goto done;
5543 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5544 &have_staged_files);
5545 if (err && err->code != GOT_ERR_CANCELLED)
5546 goto done;
5547 if (have_staged_files) {
5548 err = check_non_staged_files(fileindex, paths);
5549 if (err)
5550 goto done;
5553 cc_arg.commitable_paths = &commitable_paths;
5554 cc_arg.worktree = worktree;
5555 cc_arg.fileindex = fileindex;
5556 cc_arg.repo = repo;
5557 cc_arg.have_staged_files = have_staged_files;
5558 TAILQ_FOREACH(pe, paths, entry) {
5559 err = worktree_status(worktree, pe->path, fileindex, repo,
5560 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5561 if (err)
5562 goto done;
5565 if (TAILQ_EMPTY(&commitable_paths)) {
5566 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5567 goto done;
5570 TAILQ_FOREACH(pe, paths, entry) {
5571 err = check_path_is_commitable(pe->path, &commitable_paths);
5572 if (err)
5573 goto done;
5576 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5577 struct got_commitable *ct = pe->data;
5578 const char *ct_path = ct->in_repo_path;
5580 while (ct_path[0] == '/')
5581 ct_path++;
5582 err = check_out_of_date(ct_path, ct->status,
5583 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5584 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5585 if (err)
5586 goto done;
5590 err = commit_worktree(new_commit_id, &commitable_paths,
5591 head_commit_id, worktree, author, committer,
5592 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5593 if (err)
5594 goto done;
5596 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5597 fileindex, have_staged_files);
5598 if (err == NULL) {
5599 err = reinstall_symlinks_after_commit(&commitable_paths,
5600 *new_commit_id, fileindex, worktree, repo);
5602 sync_err = sync_fileindex(fileindex, fileindex_path);
5603 if (sync_err && err == NULL)
5604 err = sync_err;
5605 done:
5606 if (fileindex)
5607 got_fileindex_free(fileindex);
5608 free(fileindex_path);
5609 unlockerr = lock_worktree(worktree, LOCK_SH);
5610 if (unlockerr && err == NULL)
5611 err = unlockerr;
5612 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5613 struct got_commitable *ct = pe->data;
5614 free_commitable(ct);
5616 got_pathlist_free(&commitable_paths);
5617 return err;
5620 const char *
5621 got_commitable_get_path(struct got_commitable *ct)
5623 return ct->path;
5626 unsigned int
5627 got_commitable_get_status(struct got_commitable *ct)
5629 return ct->status;
5632 struct check_rebase_ok_arg {
5633 struct got_worktree *worktree;
5634 struct got_repository *repo;
5637 static const struct got_error *
5638 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5640 const struct got_error *err = NULL;
5641 struct check_rebase_ok_arg *a = arg;
5642 unsigned char status;
5643 struct stat sb;
5644 char *ondisk_path;
5646 /* Reject rebase of a work tree with mixed base commits. */
5647 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5648 SHA1_DIGEST_LENGTH))
5649 return got_error(GOT_ERR_MIXED_COMMITS);
5651 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5652 == -1)
5653 return got_error_from_errno("asprintf");
5655 /* Reject rebase of a work tree with modified or staged files. */
5656 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5657 free(ondisk_path);
5658 if (err)
5659 return err;
5661 if (status != GOT_STATUS_NO_CHANGE)
5662 return got_error(GOT_ERR_MODIFIED);
5663 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5664 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5666 return NULL;
5669 const struct got_error *
5670 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5671 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5672 struct got_worktree *worktree, struct got_reference *branch,
5673 struct got_repository *repo)
5675 const struct got_error *err = NULL;
5676 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5677 char *branch_ref_name = NULL;
5678 char *fileindex_path = NULL;
5679 struct check_rebase_ok_arg ok_arg;
5680 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5681 struct got_object_id *wt_branch_tip = NULL;
5683 *new_base_branch_ref = NULL;
5684 *tmp_branch = NULL;
5685 *fileindex = NULL;
5687 err = lock_worktree(worktree, LOCK_EX);
5688 if (err)
5689 return err;
5691 err = open_fileindex(fileindex, &fileindex_path, worktree);
5692 if (err)
5693 goto done;
5695 ok_arg.worktree = worktree;
5696 ok_arg.repo = repo;
5697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5698 &ok_arg);
5699 if (err)
5700 goto done;
5702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5703 if (err)
5704 goto done;
5706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5707 if (err)
5708 goto done;
5710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5711 if (err)
5712 goto done;
5714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5715 0);
5716 if (err)
5717 goto done;
5719 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5720 if (err)
5721 goto done;
5722 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5723 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5724 goto done;
5727 err = got_ref_alloc_symref(new_base_branch_ref,
5728 new_base_branch_ref_name, wt_branch);
5729 if (err)
5730 goto done;
5731 err = got_ref_write(*new_base_branch_ref, repo);
5732 if (err)
5733 goto done;
5735 /* TODO Lock original branch's ref while rebasing? */
5737 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5738 if (err)
5739 goto done;
5741 err = got_ref_write(branch_ref, repo);
5742 if (err)
5743 goto done;
5745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5746 worktree->base_commit_id);
5747 if (err)
5748 goto done;
5749 err = got_ref_write(*tmp_branch, repo);
5750 if (err)
5751 goto done;
5753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5754 if (err)
5755 goto done;
5756 done:
5757 free(fileindex_path);
5758 free(tmp_branch_name);
5759 free(new_base_branch_ref_name);
5760 free(branch_ref_name);
5761 if (branch_ref)
5762 got_ref_close(branch_ref);
5763 if (wt_branch)
5764 got_ref_close(wt_branch);
5765 free(wt_branch_tip);
5766 if (err) {
5767 if (*new_base_branch_ref) {
5768 got_ref_close(*new_base_branch_ref);
5769 *new_base_branch_ref = NULL;
5771 if (*tmp_branch) {
5772 got_ref_close(*tmp_branch);
5773 *tmp_branch = NULL;
5775 if (*fileindex) {
5776 got_fileindex_free(*fileindex);
5777 *fileindex = NULL;
5779 lock_worktree(worktree, LOCK_SH);
5781 return err;
5784 const struct got_error *
5785 got_worktree_rebase_continue(struct got_object_id **commit_id,
5786 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5787 struct got_reference **branch, struct got_fileindex **fileindex,
5788 struct got_worktree *worktree, struct got_repository *repo)
5790 const struct got_error *err;
5791 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5793 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5794 char *fileindex_path = NULL;
5795 int have_staged_files = 0;
5797 *commit_id = NULL;
5798 *new_base_branch = NULL;
5799 *tmp_branch = NULL;
5800 *branch = NULL;
5801 *fileindex = NULL;
5803 err = lock_worktree(worktree, LOCK_EX);
5804 if (err)
5805 return err;
5807 err = open_fileindex(fileindex, &fileindex_path, worktree);
5808 if (err)
5809 goto done;
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5812 &have_staged_files);
5813 if (err && err->code != GOT_ERR_CANCELLED)
5814 goto done;
5815 if (have_staged_files) {
5816 err = got_error(GOT_ERR_STAGED_PATHS);
5817 goto done;
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 if (err)
5822 goto done;
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5825 if (err)
5826 goto done;
5828 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5829 if (err)
5830 goto done;
5832 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5833 if (err)
5834 goto done;
5836 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5837 if (err)
5838 goto done;
5840 err = got_ref_open(branch, repo,
5841 got_ref_get_symref_target(branch_ref), 0);
5842 if (err)
5843 goto done;
5845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5846 if (err)
5847 goto done;
5849 err = got_ref_resolve(commit_id, repo, commit_ref);
5850 if (err)
5851 goto done;
5853 err = got_ref_open(new_base_branch, repo,
5854 new_base_branch_ref_name, 0);
5855 if (err)
5856 goto done;
5858 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5859 if (err)
5860 goto done;
5861 done:
5862 free(commit_ref_name);
5863 free(branch_ref_name);
5864 free(fileindex_path);
5865 if (commit_ref)
5866 got_ref_close(commit_ref);
5867 if (branch_ref)
5868 got_ref_close(branch_ref);
5869 if (err) {
5870 free(*commit_id);
5871 *commit_id = NULL;
5872 if (*tmp_branch) {
5873 got_ref_close(*tmp_branch);
5874 *tmp_branch = NULL;
5876 if (*new_base_branch) {
5877 got_ref_close(*new_base_branch);
5878 *new_base_branch = NULL;
5880 if (*branch) {
5881 got_ref_close(*branch);
5882 *branch = NULL;
5884 if (*fileindex) {
5885 got_fileindex_free(*fileindex);
5886 *fileindex = NULL;
5888 lock_worktree(worktree, LOCK_SH);
5890 return err;
5893 const struct got_error *
5894 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5896 const struct got_error *err;
5897 char *tmp_branch_name = NULL;
5899 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5900 if (err)
5901 return err;
5903 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5904 free(tmp_branch_name);
5905 return NULL;
5908 static const struct got_error *
5909 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5910 char **logmsg, void *arg)
5912 *logmsg = arg;
5913 return NULL;
5916 static const struct got_error *
5917 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5918 const char *path, struct got_object_id *blob_id,
5919 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5920 int dirfd, const char *de_name)
5922 return NULL;
5925 struct collect_merged_paths_arg {
5926 got_worktree_checkout_cb progress_cb;
5927 void *progress_arg;
5928 struct got_pathlist_head *merged_paths;
5931 static const struct got_error *
5932 collect_merged_paths(void *arg, unsigned char status, const char *path)
5934 const struct got_error *err;
5935 struct collect_merged_paths_arg *a = arg;
5936 char *p;
5937 struct got_pathlist_entry *new;
5939 err = (*a->progress_cb)(a->progress_arg, status, path);
5940 if (err)
5941 return err;
5943 if (status != GOT_STATUS_MERGE &&
5944 status != GOT_STATUS_ADD &&
5945 status != GOT_STATUS_DELETE &&
5946 status != GOT_STATUS_CONFLICT)
5947 return NULL;
5949 p = strdup(path);
5950 if (p == NULL)
5951 return got_error_from_errno("strdup");
5953 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5954 if (err || new == NULL)
5955 free(p);
5956 return err;
5959 void
5960 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5962 struct got_pathlist_entry *pe;
5964 TAILQ_FOREACH(pe, merged_paths, entry)
5965 free((char *)pe->path);
5967 got_pathlist_free(merged_paths);
5970 static const struct got_error *
5971 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5972 int is_rebase, struct got_repository *repo)
5974 const struct got_error *err;
5975 struct got_reference *commit_ref = NULL;
5977 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5978 if (err) {
5979 if (err->code != GOT_ERR_NOT_REF)
5980 goto done;
5981 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5982 if (err)
5983 goto done;
5984 err = got_ref_write(commit_ref, repo);
5985 if (err)
5986 goto done;
5987 } else if (is_rebase) {
5988 struct got_object_id *stored_id;
5989 int cmp;
5991 err = got_ref_resolve(&stored_id, repo, commit_ref);
5992 if (err)
5993 goto done;
5994 cmp = got_object_id_cmp(commit_id, stored_id);
5995 free(stored_id);
5996 if (cmp != 0) {
5997 err = got_error(GOT_ERR_REBASE_COMMITID);
5998 goto done;
6001 done:
6002 if (commit_ref)
6003 got_ref_close(commit_ref);
6004 return err;
6007 static const struct got_error *
6008 rebase_merge_files(struct got_pathlist_head *merged_paths,
6009 const char *commit_ref_name, struct got_worktree *worktree,
6010 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6011 struct got_object_id *commit_id, struct got_repository *repo,
6012 got_worktree_checkout_cb progress_cb, void *progress_arg,
6013 got_cancel_cb cancel_cb, void *cancel_arg)
6015 const struct got_error *err;
6016 struct got_reference *commit_ref = NULL;
6017 struct collect_merged_paths_arg cmp_arg;
6018 char *fileindex_path;
6020 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6022 err = get_fileindex_path(&fileindex_path, worktree);
6023 if (err)
6024 return err;
6026 cmp_arg.progress_cb = progress_cb;
6027 cmp_arg.progress_arg = progress_arg;
6028 cmp_arg.merged_paths = merged_paths;
6029 err = merge_files(worktree, fileindex, fileindex_path,
6030 parent_commit_id, commit_id, repo, collect_merged_paths,
6031 &cmp_arg, cancel_cb, cancel_arg);
6032 if (commit_ref)
6033 got_ref_close(commit_ref);
6034 return err;
6037 const struct got_error *
6038 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6039 struct got_worktree *worktree, struct got_fileindex *fileindex,
6040 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6041 struct got_repository *repo,
6042 got_worktree_checkout_cb progress_cb, void *progress_arg,
6043 got_cancel_cb cancel_cb, void *cancel_arg)
6045 const struct got_error *err;
6046 char *commit_ref_name;
6048 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6049 if (err)
6050 return err;
6052 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6053 if (err)
6054 goto done;
6056 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6057 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6058 progress_arg, cancel_cb, cancel_arg);
6059 done:
6060 free(commit_ref_name);
6061 return err;
6064 const struct got_error *
6065 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6066 struct got_worktree *worktree, struct got_fileindex *fileindex,
6067 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6068 struct got_repository *repo,
6069 got_worktree_checkout_cb progress_cb, void *progress_arg,
6070 got_cancel_cb cancel_cb, void *cancel_arg)
6072 const struct got_error *err;
6073 char *commit_ref_name;
6075 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6076 if (err)
6077 return err;
6079 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6080 if (err)
6081 goto done;
6083 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6084 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6085 progress_arg, cancel_cb, cancel_arg);
6086 done:
6087 free(commit_ref_name);
6088 return err;
6091 static const struct got_error *
6092 rebase_commit(struct got_object_id **new_commit_id,
6093 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6096 const char *new_logmsg, struct got_repository *repo)
6098 const struct got_error *err, *sync_err;
6099 struct got_pathlist_head commitable_paths;
6100 struct collect_commitables_arg cc_arg;
6101 char *fileindex_path = NULL;
6102 struct got_reference *head_ref = NULL;
6103 struct got_object_id *head_commit_id = NULL;
6104 char *logmsg = NULL;
6106 TAILQ_INIT(&commitable_paths);
6107 *new_commit_id = NULL;
6109 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6111 err = get_fileindex_path(&fileindex_path, worktree);
6112 if (err)
6113 return err;
6115 cc_arg.commitable_paths = &commitable_paths;
6116 cc_arg.worktree = worktree;
6117 cc_arg.repo = repo;
6118 cc_arg.have_staged_files = 0;
6120 * If possible get the status of individual files directly to
6121 * avoid crawling the entire work tree once per rebased commit.
6122 * TODO: Ideally, merged_paths would contain a list of commitables
6123 * we could use so we could skip worktree_status() entirely.
6125 if (merged_paths) {
6126 struct got_pathlist_entry *pe;
6127 TAILQ_FOREACH(pe, merged_paths, entry) {
6128 err = worktree_status(worktree, pe->path, fileindex,
6129 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6130 0);
6131 if (err)
6132 goto done;
6134 } else {
6135 err = worktree_status(worktree, "", fileindex, repo,
6136 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6137 if (err)
6138 goto done;
6141 if (TAILQ_EMPTY(&commitable_paths)) {
6142 /* No-op change; commit will be elided. */
6143 err = got_ref_delete(commit_ref, repo);
6144 if (err)
6145 goto done;
6146 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6147 goto done;
6150 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6151 if (err)
6152 goto done;
6154 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6155 if (err)
6156 goto done;
6158 if (new_logmsg) {
6159 logmsg = strdup(new_logmsg);
6160 if (logmsg == NULL) {
6161 err = got_error_from_errno("strdup");
6162 goto done;
6164 } else {
6165 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6166 if (err)
6167 goto done;
6170 /* NB: commit_worktree will call free(logmsg) */
6171 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6172 worktree, got_object_commit_get_author(orig_commit),
6173 got_object_commit_get_committer(orig_commit),
6174 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6175 if (err)
6176 goto done;
6178 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6179 if (err)
6180 goto done;
6182 err = got_ref_delete(commit_ref, repo);
6183 if (err)
6184 goto done;
6186 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6187 fileindex, 0);
6188 sync_err = sync_fileindex(fileindex, fileindex_path);
6189 if (sync_err && err == NULL)
6190 err = sync_err;
6191 done:
6192 free(fileindex_path);
6193 free(head_commit_id);
6194 if (head_ref)
6195 got_ref_close(head_ref);
6196 if (err) {
6197 free(*new_commit_id);
6198 *new_commit_id = NULL;
6200 return err;
6203 const struct got_error *
6204 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6205 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6206 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6207 struct got_commit_object *orig_commit,
6208 struct got_object_id *orig_commit_id, struct got_repository *repo)
6210 const struct got_error *err;
6211 char *commit_ref_name;
6212 struct got_reference *commit_ref = NULL;
6213 struct got_object_id *commit_id = NULL;
6215 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6216 if (err)
6217 return err;
6219 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6220 if (err)
6221 goto done;
6222 err = got_ref_resolve(&commit_id, repo, commit_ref);
6223 if (err)
6224 goto done;
6225 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6226 err = got_error(GOT_ERR_REBASE_COMMITID);
6227 goto done;
6230 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6231 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6232 done:
6233 if (commit_ref)
6234 got_ref_close(commit_ref);
6235 free(commit_ref_name);
6236 free(commit_id);
6237 return err;
6240 const struct got_error *
6241 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6242 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6243 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6244 struct got_commit_object *orig_commit,
6245 struct got_object_id *orig_commit_id, const char *new_logmsg,
6246 struct got_repository *repo)
6248 const struct got_error *err;
6249 char *commit_ref_name;
6250 struct got_reference *commit_ref = NULL;
6252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6253 if (err)
6254 return err;
6256 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6257 if (err)
6258 goto done;
6260 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6261 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6262 done:
6263 if (commit_ref)
6264 got_ref_close(commit_ref);
6265 free(commit_ref_name);
6266 return err;
6269 const struct got_error *
6270 got_worktree_rebase_postpone(struct got_worktree *worktree,
6271 struct got_fileindex *fileindex)
6273 if (fileindex)
6274 got_fileindex_free(fileindex);
6275 return lock_worktree(worktree, LOCK_SH);
6278 static const struct got_error *
6279 delete_ref(const char *name, struct got_repository *repo)
6281 const struct got_error *err;
6282 struct got_reference *ref;
6284 err = got_ref_open(&ref, repo, name, 0);
6285 if (err) {
6286 if (err->code == GOT_ERR_NOT_REF)
6287 return NULL;
6288 return err;
6291 err = got_ref_delete(ref, repo);
6292 got_ref_close(ref);
6293 return err;
6296 static const struct got_error *
6297 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6299 const struct got_error *err;
6300 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6301 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6303 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6304 if (err)
6305 goto done;
6306 err = delete_ref(tmp_branch_name, repo);
6307 if (err)
6308 goto done;
6310 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6311 if (err)
6312 goto done;
6313 err = delete_ref(new_base_branch_ref_name, repo);
6314 if (err)
6315 goto done;
6317 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6318 if (err)
6319 goto done;
6320 err = delete_ref(branch_ref_name, repo);
6321 if (err)
6322 goto done;
6324 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6325 if (err)
6326 goto done;
6327 err = delete_ref(commit_ref_name, repo);
6328 if (err)
6329 goto done;
6331 done:
6332 free(tmp_branch_name);
6333 free(new_base_branch_ref_name);
6334 free(branch_ref_name);
6335 free(commit_ref_name);
6336 return err;
6339 const struct got_error *
6340 got_worktree_rebase_complete(struct got_worktree *worktree,
6341 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6342 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6343 struct got_repository *repo)
6345 const struct got_error *err, *unlockerr;
6346 struct got_object_id *new_head_commit_id = NULL;
6348 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6349 if (err)
6350 return err;
6352 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6353 if (err)
6354 goto done;
6356 err = got_ref_write(rebased_branch, repo);
6357 if (err)
6358 goto done;
6360 err = got_worktree_set_head_ref(worktree, rebased_branch);
6361 if (err)
6362 goto done;
6364 err = delete_rebase_refs(worktree, repo);
6365 done:
6366 if (fileindex)
6367 got_fileindex_free(fileindex);
6368 free(new_head_commit_id);
6369 unlockerr = lock_worktree(worktree, LOCK_SH);
6370 if (unlockerr && err == NULL)
6371 err = unlockerr;
6372 return err;
6375 const struct got_error *
6376 got_worktree_rebase_abort(struct got_worktree *worktree,
6377 struct got_fileindex *fileindex, struct got_repository *repo,
6378 struct got_reference *new_base_branch,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg)
6381 const struct got_error *err, *unlockerr, *sync_err;
6382 struct got_reference *resolved = NULL;
6383 struct got_object_id *commit_id = NULL;
6384 char *fileindex_path = NULL;
6385 struct revert_file_args rfa;
6386 struct got_object_id *tree_id = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6389 if (err)
6390 return err;
6392 err = got_ref_open(&resolved, repo,
6393 got_ref_get_symref_target(new_base_branch), 0);
6394 if (err)
6395 goto done;
6397 err = got_worktree_set_head_ref(worktree, resolved);
6398 if (err)
6399 goto done;
6402 * XXX commits to the base branch could have happened while
6403 * we were busy rebasing; should we store the original commit ID
6404 * when rebase begins and read it back here?
6406 err = got_ref_resolve(&commit_id, repo, resolved);
6407 if (err)
6408 goto done;
6410 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6411 if (err)
6412 goto done;
6414 err = got_object_id_by_path(&tree_id, repo,
6415 worktree->base_commit_id, worktree->path_prefix);
6416 if (err)
6417 goto done;
6419 err = delete_rebase_refs(worktree, repo);
6420 if (err)
6421 goto done;
6423 err = get_fileindex_path(&fileindex_path, worktree);
6424 if (err)
6425 goto done;
6427 rfa.worktree = worktree;
6428 rfa.fileindex = fileindex;
6429 rfa.progress_cb = progress_cb;
6430 rfa.progress_arg = progress_arg;
6431 rfa.patch_cb = NULL;
6432 rfa.patch_arg = NULL;
6433 rfa.repo = repo;
6434 err = worktree_status(worktree, "", fileindex, repo,
6435 revert_file, &rfa, NULL, NULL, 0, 0);
6436 if (err)
6437 goto sync;
6439 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6440 repo, progress_cb, progress_arg, NULL, NULL);
6441 sync:
6442 sync_err = sync_fileindex(fileindex, fileindex_path);
6443 if (sync_err && err == NULL)
6444 err = sync_err;
6445 done:
6446 got_ref_close(resolved);
6447 free(tree_id);
6448 free(commit_id);
6449 if (fileindex)
6450 got_fileindex_free(fileindex);
6451 free(fileindex_path);
6453 unlockerr = lock_worktree(worktree, LOCK_SH);
6454 if (unlockerr && err == NULL)
6455 err = unlockerr;
6456 return err;
6459 const struct got_error *
6460 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6461 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6462 struct got_fileindex **fileindex, struct got_worktree *worktree,
6463 struct got_repository *repo)
6465 const struct got_error *err = NULL;
6466 char *tmp_branch_name = NULL;
6467 char *branch_ref_name = NULL;
6468 char *base_commit_ref_name = NULL;
6469 char *fileindex_path = NULL;
6470 struct check_rebase_ok_arg ok_arg;
6471 struct got_reference *wt_branch = NULL;
6472 struct got_reference *base_commit_ref = NULL;
6474 *tmp_branch = NULL;
6475 *branch_ref = NULL;
6476 *base_commit_id = NULL;
6477 *fileindex = NULL;
6479 err = lock_worktree(worktree, LOCK_EX);
6480 if (err)
6481 return err;
6483 err = open_fileindex(fileindex, &fileindex_path, worktree);
6484 if (err)
6485 goto done;
6487 ok_arg.worktree = worktree;
6488 ok_arg.repo = repo;
6489 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6490 &ok_arg);
6491 if (err)
6492 goto done;
6494 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6495 if (err)
6496 goto done;
6498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6499 if (err)
6500 goto done;
6502 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6503 worktree);
6504 if (err)
6505 goto done;
6507 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6508 0);
6509 if (err)
6510 goto done;
6512 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6513 if (err)
6514 goto done;
6516 err = got_ref_write(*branch_ref, repo);
6517 if (err)
6518 goto done;
6520 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6521 worktree->base_commit_id);
6522 if (err)
6523 goto done;
6524 err = got_ref_write(base_commit_ref, repo);
6525 if (err)
6526 goto done;
6527 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6528 if (*base_commit_id == NULL) {
6529 err = got_error_from_errno("got_object_id_dup");
6530 goto done;
6533 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6534 worktree->base_commit_id);
6535 if (err)
6536 goto done;
6537 err = got_ref_write(*tmp_branch, repo);
6538 if (err)
6539 goto done;
6541 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6542 if (err)
6543 goto done;
6544 done:
6545 free(fileindex_path);
6546 free(tmp_branch_name);
6547 free(branch_ref_name);
6548 free(base_commit_ref_name);
6549 if (wt_branch)
6550 got_ref_close(wt_branch);
6551 if (err) {
6552 if (*branch_ref) {
6553 got_ref_close(*branch_ref);
6554 *branch_ref = NULL;
6556 if (*tmp_branch) {
6557 got_ref_close(*tmp_branch);
6558 *tmp_branch = NULL;
6560 free(*base_commit_id);
6561 if (*fileindex) {
6562 got_fileindex_free(*fileindex);
6563 *fileindex = NULL;
6565 lock_worktree(worktree, LOCK_SH);
6567 return err;
6570 const struct got_error *
6571 got_worktree_histedit_postpone(struct got_worktree *worktree,
6572 struct got_fileindex *fileindex)
6574 if (fileindex)
6575 got_fileindex_free(fileindex);
6576 return lock_worktree(worktree, LOCK_SH);
6579 const struct got_error *
6580 got_worktree_histedit_in_progress(int *in_progress,
6581 struct got_worktree *worktree)
6583 const struct got_error *err;
6584 char *tmp_branch_name = NULL;
6586 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6587 if (err)
6588 return err;
6590 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6591 free(tmp_branch_name);
6592 return NULL;
6595 const struct got_error *
6596 got_worktree_histedit_continue(struct got_object_id **commit_id,
6597 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6598 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6599 struct got_worktree *worktree, struct got_repository *repo)
6601 const struct got_error *err;
6602 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6603 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6604 struct got_reference *commit_ref = NULL;
6605 struct got_reference *base_commit_ref = NULL;
6606 char *fileindex_path = NULL;
6607 int have_staged_files = 0;
6609 *commit_id = NULL;
6610 *tmp_branch = NULL;
6611 *base_commit_id = NULL;
6612 *fileindex = NULL;
6614 err = lock_worktree(worktree, LOCK_EX);
6615 if (err)
6616 return err;
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 if (err)
6620 goto done;
6622 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6623 &have_staged_files);
6624 if (err && err->code != GOT_ERR_CANCELLED)
6625 goto done;
6626 if (have_staged_files) {
6627 err = got_error(GOT_ERR_STAGED_PATHS);
6628 goto done;
6631 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6632 if (err)
6633 goto done;
6635 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6636 if (err)
6637 goto done;
6639 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6640 if (err)
6641 goto done;
6643 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6644 worktree);
6645 if (err)
6646 goto done;
6648 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6649 if (err)
6650 goto done;
6652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 if (err)
6654 goto done;
6655 err = got_ref_resolve(commit_id, repo, commit_ref);
6656 if (err)
6657 goto done;
6659 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6660 if (err)
6661 goto done;
6662 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6663 if (err)
6664 goto done;
6666 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6667 if (err)
6668 goto done;
6669 done:
6670 free(commit_ref_name);
6671 free(branch_ref_name);
6672 free(fileindex_path);
6673 if (commit_ref)
6674 got_ref_close(commit_ref);
6675 if (base_commit_ref)
6676 got_ref_close(base_commit_ref);
6677 if (err) {
6678 free(*commit_id);
6679 *commit_id = NULL;
6680 free(*base_commit_id);
6681 *base_commit_id = NULL;
6682 if (*tmp_branch) {
6683 got_ref_close(*tmp_branch);
6684 *tmp_branch = NULL;
6686 if (*fileindex) {
6687 got_fileindex_free(*fileindex);
6688 *fileindex = NULL;
6690 lock_worktree(worktree, LOCK_EX);
6692 return err;
6695 static const struct got_error *
6696 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *err;
6699 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6700 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6702 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6703 if (err)
6704 goto done;
6705 err = delete_ref(tmp_branch_name, repo);
6706 if (err)
6707 goto done;
6709 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6710 worktree);
6711 if (err)
6712 goto done;
6713 err = delete_ref(base_commit_ref_name, repo);
6714 if (err)
6715 goto done;
6717 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6718 if (err)
6719 goto done;
6720 err = delete_ref(branch_ref_name, repo);
6721 if (err)
6722 goto done;
6724 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6725 if (err)
6726 goto done;
6727 err = delete_ref(commit_ref_name, repo);
6728 if (err)
6729 goto done;
6730 done:
6731 free(tmp_branch_name);
6732 free(base_commit_ref_name);
6733 free(branch_ref_name);
6734 free(commit_ref_name);
6735 return err;
6738 const struct got_error *
6739 got_worktree_histedit_abort(struct got_worktree *worktree,
6740 struct got_fileindex *fileindex, struct got_repository *repo,
6741 struct got_reference *branch, struct got_object_id *base_commit_id,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg)
6744 const struct got_error *err, *unlockerr, *sync_err;
6745 struct got_reference *resolved = NULL;
6746 char *fileindex_path = NULL;
6747 struct got_object_id *tree_id = NULL;
6748 struct revert_file_args rfa;
6750 err = lock_worktree(worktree, LOCK_EX);
6751 if (err)
6752 return err;
6754 err = got_ref_open(&resolved, repo,
6755 got_ref_get_symref_target(branch), 0);
6756 if (err)
6757 goto done;
6759 err = got_worktree_set_head_ref(worktree, resolved);
6760 if (err)
6761 goto done;
6763 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6764 if (err)
6765 goto done;
6767 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6768 worktree->path_prefix);
6769 if (err)
6770 goto done;
6772 err = delete_histedit_refs(worktree, repo);
6773 if (err)
6774 goto done;
6776 err = get_fileindex_path(&fileindex_path, worktree);
6777 if (err)
6778 goto done;
6780 rfa.worktree = worktree;
6781 rfa.fileindex = fileindex;
6782 rfa.progress_cb = progress_cb;
6783 rfa.progress_arg = progress_arg;
6784 rfa.patch_cb = NULL;
6785 rfa.patch_arg = NULL;
6786 rfa.repo = repo;
6787 err = worktree_status(worktree, "", fileindex, repo,
6788 revert_file, &rfa, NULL, NULL, 0, 0);
6789 if (err)
6790 goto sync;
6792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6793 repo, progress_cb, progress_arg, NULL, NULL);
6794 sync:
6795 sync_err = sync_fileindex(fileindex, fileindex_path);
6796 if (sync_err && err == NULL)
6797 err = sync_err;
6798 done:
6799 got_ref_close(resolved);
6800 free(tree_id);
6801 free(fileindex_path);
6803 unlockerr = lock_worktree(worktree, LOCK_SH);
6804 if (unlockerr && err == NULL)
6805 err = unlockerr;
6806 return err;
6809 const struct got_error *
6810 got_worktree_histedit_complete(struct got_worktree *worktree,
6811 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6812 struct got_reference *edited_branch, struct got_repository *repo)
6814 const struct got_error *err, *unlockerr;
6815 struct got_object_id *new_head_commit_id = NULL;
6816 struct got_reference *resolved = NULL;
6818 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6819 if (err)
6820 return err;
6822 err = got_ref_open(&resolved, repo,
6823 got_ref_get_symref_target(edited_branch), 0);
6824 if (err)
6825 goto done;
6827 err = got_ref_change_ref(resolved, new_head_commit_id);
6828 if (err)
6829 goto done;
6831 err = got_ref_write(resolved, repo);
6832 if (err)
6833 goto done;
6835 err = got_worktree_set_head_ref(worktree, resolved);
6836 if (err)
6837 goto done;
6839 err = delete_histedit_refs(worktree, repo);
6840 done:
6841 if (fileindex)
6842 got_fileindex_free(fileindex);
6843 free(new_head_commit_id);
6844 unlockerr = lock_worktree(worktree, LOCK_SH);
6845 if (unlockerr && err == NULL)
6846 err = unlockerr;
6847 return err;
6850 const struct got_error *
6851 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6852 struct got_object_id *commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6857 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6858 if (err)
6859 return err;
6861 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6862 if (err)
6863 goto done;
6865 err = delete_ref(commit_ref_name, repo);
6866 done:
6867 free(commit_ref_name);
6868 return err;
6871 const struct got_error *
6872 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6873 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6874 struct got_worktree *worktree, const char *refname,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6878 char *fileindex_path = NULL;
6879 struct check_rebase_ok_arg ok_arg;
6881 *fileindex = NULL;
6882 *branch_ref = NULL;
6883 *base_branch_ref = NULL;
6885 err = lock_worktree(worktree, LOCK_EX);
6886 if (err)
6887 return err;
6889 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6890 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6891 "cannot integrate a branch into itself; "
6892 "update -b or different branch name required");
6893 goto done;
6896 err = open_fileindex(fileindex, &fileindex_path, worktree);
6897 if (err)
6898 goto done;
6900 /* Preconditions are the same as for rebase. */
6901 ok_arg.worktree = worktree;
6902 ok_arg.repo = repo;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6904 &ok_arg);
6905 if (err)
6906 goto done;
6908 err = got_ref_open(branch_ref, repo, refname, 1);
6909 if (err)
6910 goto done;
6912 err = got_ref_open(base_branch_ref, repo,
6913 got_worktree_get_head_ref_name(worktree), 1);
6914 done:
6915 if (err) {
6916 if (*branch_ref) {
6917 got_ref_close(*branch_ref);
6918 *branch_ref = NULL;
6920 if (*base_branch_ref) {
6921 got_ref_close(*base_branch_ref);
6922 *base_branch_ref = NULL;
6924 if (*fileindex) {
6925 got_fileindex_free(*fileindex);
6926 *fileindex = NULL;
6928 lock_worktree(worktree, LOCK_SH);
6930 return err;
6933 const struct got_error *
6934 got_worktree_integrate_continue(struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_repository *repo,
6936 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err = NULL, *sync_err, *unlockerr;
6941 char *fileindex_path = NULL;
6942 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6944 err = get_fileindex_path(&fileindex_path, worktree);
6945 if (err)
6946 goto done;
6948 err = got_ref_resolve(&commit_id, repo, branch_ref);
6949 if (err)
6950 goto done;
6952 err = got_object_id_by_path(&tree_id, repo, commit_id,
6953 worktree->path_prefix);
6954 if (err)
6955 goto done;
6957 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6958 if (err)
6959 goto done;
6961 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6962 progress_cb, progress_arg, cancel_cb, cancel_arg);
6963 if (err)
6964 goto sync;
6966 err = got_ref_change_ref(base_branch_ref, commit_id);
6967 if (err)
6968 goto sync;
6970 err = got_ref_write(base_branch_ref, repo);
6971 sync:
6972 sync_err = sync_fileindex(fileindex, fileindex_path);
6973 if (sync_err && err == NULL)
6974 err = sync_err;
6976 done:
6977 unlockerr = got_ref_unlock(branch_ref);
6978 if (unlockerr && err == NULL)
6979 err = unlockerr;
6980 got_ref_close(branch_ref);
6982 unlockerr = got_ref_unlock(base_branch_ref);
6983 if (unlockerr && err == NULL)
6984 err = unlockerr;
6985 got_ref_close(base_branch_ref);
6987 got_fileindex_free(fileindex);
6988 free(fileindex_path);
6989 free(tree_id);
6991 unlockerr = lock_worktree(worktree, LOCK_SH);
6992 if (unlockerr && err == NULL)
6993 err = unlockerr;
6994 return err;
6997 const struct got_error *
6998 got_worktree_integrate_abort(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_repository *repo,
7000 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7002 const struct got_error *err = NULL, *unlockerr = NULL;
7004 got_fileindex_free(fileindex);
7006 err = lock_worktree(worktree, LOCK_SH);
7008 unlockerr = got_ref_unlock(branch_ref);
7009 if (unlockerr && err == NULL)
7010 err = unlockerr;
7011 got_ref_close(branch_ref);
7013 unlockerr = got_ref_unlock(base_branch_ref);
7014 if (unlockerr && err == NULL)
7015 err = unlockerr;
7016 got_ref_close(base_branch_ref);
7018 return err;
7021 struct check_stage_ok_arg {
7022 struct got_object_id *head_commit_id;
7023 struct got_worktree *worktree;
7024 struct got_fileindex *fileindex;
7025 struct got_repository *repo;
7026 int have_changes;
7029 const struct got_error *
7030 check_stage_ok(void *arg, unsigned char status,
7031 unsigned char staged_status, const char *relpath,
7032 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7033 struct got_object_id *commit_id, int dirfd, const char *de_name)
7035 struct check_stage_ok_arg *a = arg;
7036 const struct got_error *err = NULL;
7037 struct got_fileindex_entry *ie;
7038 struct got_object_id base_commit_id;
7039 struct got_object_id *base_commit_idp = NULL;
7040 char *in_repo_path = NULL, *p;
7042 if (status == GOT_STATUS_UNVERSIONED ||
7043 status == GOT_STATUS_NO_CHANGE)
7044 return NULL;
7045 if (status == GOT_STATUS_NONEXISTENT)
7046 return got_error_set_errno(ENOENT, relpath);
7048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7049 if (ie == NULL)
7050 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7052 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7053 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7054 relpath) == -1)
7055 return got_error_from_errno("asprintf");
7057 if (got_fileindex_entry_has_commit(ie)) {
7058 memcpy(base_commit_id.sha1, ie->commit_sha1,
7059 SHA1_DIGEST_LENGTH);
7060 base_commit_idp = &base_commit_id;
7063 if (status == GOT_STATUS_CONFLICT) {
7064 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7065 goto done;
7066 } else if (status != GOT_STATUS_ADD &&
7067 status != GOT_STATUS_MODIFY &&
7068 status != GOT_STATUS_DELETE) {
7069 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7070 goto done;
7073 a->have_changes = 1;
7075 p = in_repo_path;
7076 while (p[0] == '/')
7077 p++;
7078 err = check_out_of_date(p, status, staged_status,
7079 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7080 GOT_ERR_STAGE_OUT_OF_DATE);
7081 done:
7082 free(in_repo_path);
7083 return err;
7086 struct stage_path_arg {
7087 struct got_worktree *worktree;
7088 struct got_fileindex *fileindex;
7089 struct got_repository *repo;
7090 got_worktree_status_cb status_cb;
7091 void *status_arg;
7092 got_worktree_patch_cb patch_cb;
7093 void *patch_arg;
7094 int staged_something;
7097 static const struct got_error *
7098 stage_path(void *arg, unsigned char status,
7099 unsigned char staged_status, const char *relpath,
7100 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7101 struct got_object_id *commit_id, int dirfd, const char *de_name)
7103 struct stage_path_arg *a = arg;
7104 const struct got_error *err = NULL;
7105 struct got_fileindex_entry *ie;
7106 char *ondisk_path = NULL, *path_content = NULL;
7107 uint32_t stage;
7108 struct got_object_id *new_staged_blob_id = NULL;
7109 struct stat sb;
7111 if (status == GOT_STATUS_UNVERSIONED)
7112 return NULL;
7114 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7115 if (ie == NULL)
7116 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7118 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7119 relpath)== -1)
7120 return got_error_from_errno("asprintf");
7122 switch (status) {
7123 case GOT_STATUS_ADD:
7124 case GOT_STATUS_MODIFY:
7125 /* XXX could sb.st_mode be passed in by our caller? */
7126 if (lstat(ondisk_path, &sb) == -1) {
7127 err = got_error_from_errno2("lstat", ondisk_path);
7128 break;
7130 if (a->patch_cb) {
7131 if (status == GOT_STATUS_ADD) {
7132 int choice = GOT_PATCH_CHOICE_NONE;
7133 err = (*a->patch_cb)(&choice, a->patch_arg,
7134 status, ie->path, NULL, 1, 1);
7135 if (err)
7136 break;
7137 if (choice != GOT_PATCH_CHOICE_YES)
7138 break;
7139 } else {
7140 err = create_patched_content(&path_content, 0,
7141 staged_blob_id ? staged_blob_id : blob_id,
7142 ondisk_path, dirfd, de_name, ie->path,
7143 a->repo, a->patch_cb, a->patch_arg);
7144 if (err || path_content == NULL)
7145 break;
7148 err = got_object_blob_create(&new_staged_blob_id,
7149 path_content ? path_content : ondisk_path, a->repo);
7150 if (err)
7151 break;
7152 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7153 SHA1_DIGEST_LENGTH);
7154 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7155 stage = GOT_FILEIDX_STAGE_ADD;
7156 else
7157 stage = GOT_FILEIDX_STAGE_MODIFY;
7158 got_fileindex_entry_stage_set(ie, stage);
7159 if (S_ISLNK(sb.st_mode)) {
7160 got_fileindex_entry_staged_filetype_set(ie,
7161 GOT_FILEIDX_MODE_SYMLINK);
7162 } else {
7163 got_fileindex_entry_staged_filetype_set(ie,
7164 GOT_FILEIDX_MODE_REGULAR_FILE);
7166 a->staged_something = 1;
7167 if (a->status_cb == NULL)
7168 break;
7169 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7170 get_staged_status(ie), relpath, blob_id,
7171 new_staged_blob_id, NULL, dirfd, de_name);
7172 break;
7173 case GOT_STATUS_DELETE:
7174 if (staged_status == GOT_STATUS_DELETE)
7175 break;
7176 if (a->patch_cb) {
7177 int choice = GOT_PATCH_CHOICE_NONE;
7178 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7179 ie->path, NULL, 1, 1);
7180 if (err)
7181 break;
7182 if (choice == GOT_PATCH_CHOICE_NO)
7183 break;
7184 if (choice != GOT_PATCH_CHOICE_YES) {
7185 err = got_error(GOT_ERR_PATCH_CHOICE);
7186 break;
7189 stage = GOT_FILEIDX_STAGE_DELETE;
7190 got_fileindex_entry_stage_set(ie, stage);
7191 a->staged_something = 1;
7192 if (a->status_cb == NULL)
7193 break;
7194 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7195 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7196 de_name);
7197 break;
7198 case GOT_STATUS_NO_CHANGE:
7199 break;
7200 case GOT_STATUS_CONFLICT:
7201 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7202 break;
7203 case GOT_STATUS_NONEXISTENT:
7204 err = got_error_set_errno(ENOENT, relpath);
7205 break;
7206 default:
7207 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7208 break;
7211 if (path_content && unlink(path_content) == -1 && err == NULL)
7212 err = got_error_from_errno2("unlink", path_content);
7213 free(path_content);
7214 free(ondisk_path);
7215 free(new_staged_blob_id);
7216 return err;
7219 const struct got_error *
7220 got_worktree_stage(struct got_worktree *worktree,
7221 struct got_pathlist_head *paths,
7222 got_worktree_status_cb status_cb, void *status_arg,
7223 got_worktree_patch_cb patch_cb, void *patch_arg,
7224 struct got_repository *repo)
7226 const struct got_error *err = NULL, *sync_err, *unlockerr;
7227 struct got_pathlist_entry *pe;
7228 struct got_fileindex *fileindex = NULL;
7229 char *fileindex_path = NULL;
7230 struct got_reference *head_ref = NULL;
7231 struct got_object_id *head_commit_id = NULL;
7232 struct check_stage_ok_arg oka;
7233 struct stage_path_arg spa;
7235 err = lock_worktree(worktree, LOCK_EX);
7236 if (err)
7237 return err;
7239 err = got_ref_open(&head_ref, repo,
7240 got_worktree_get_head_ref_name(worktree), 0);
7241 if (err)
7242 goto done;
7243 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7244 if (err)
7245 goto done;
7246 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7247 if (err)
7248 goto done;
7250 /* Check pre-conditions before staging anything. */
7251 oka.head_commit_id = head_commit_id;
7252 oka.worktree = worktree;
7253 oka.fileindex = fileindex;
7254 oka.repo = repo;
7255 oka.have_changes = 0;
7256 TAILQ_FOREACH(pe, paths, entry) {
7257 err = worktree_status(worktree, pe->path, fileindex, repo,
7258 check_stage_ok, &oka, NULL, NULL, 0, 0);
7259 if (err)
7260 goto done;
7262 if (!oka.have_changes) {
7263 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7264 goto done;
7267 spa.worktree = worktree;
7268 spa.fileindex = fileindex;
7269 spa.repo = repo;
7270 spa.patch_cb = patch_cb;
7271 spa.patch_arg = patch_arg;
7272 spa.status_cb = status_cb;
7273 spa.status_arg = status_arg;
7274 spa.staged_something = 0;
7275 TAILQ_FOREACH(pe, paths, entry) {
7276 err = worktree_status(worktree, pe->path, fileindex, repo,
7277 stage_path, &spa, NULL, NULL, 0, 0);
7278 if (err)
7279 goto done;
7281 if (!spa.staged_something) {
7282 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7283 goto done;
7286 sync_err = sync_fileindex(fileindex, fileindex_path);
7287 if (sync_err && err == NULL)
7288 err = sync_err;
7289 done:
7290 if (head_ref)
7291 got_ref_close(head_ref);
7292 free(head_commit_id);
7293 free(fileindex_path);
7294 if (fileindex)
7295 got_fileindex_free(fileindex);
7296 unlockerr = lock_worktree(worktree, LOCK_SH);
7297 if (unlockerr && err == NULL)
7298 err = unlockerr;
7299 return err;
7302 struct unstage_path_arg {
7303 struct got_worktree *worktree;
7304 struct got_fileindex *fileindex;
7305 struct got_repository *repo;
7306 got_worktree_checkout_cb progress_cb;
7307 void *progress_arg;
7308 got_worktree_patch_cb patch_cb;
7309 void *patch_arg;
7312 static const struct got_error *
7313 create_unstaged_content(char **path_unstaged_content,
7314 char **path_new_staged_content, struct got_object_id *blob_id,
7315 struct got_object_id *staged_blob_id, const char *relpath,
7316 struct got_repository *repo,
7317 got_worktree_patch_cb patch_cb, void *patch_arg)
7319 const struct got_error *err;
7320 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7321 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7322 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7323 struct stat sb1, sb2;
7324 struct got_diff_changes *changes = NULL;
7325 struct got_diff_state *ds = NULL;
7326 struct got_diff_args *args = NULL;
7327 struct got_diff_change *change;
7328 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7329 int have_content = 0, have_rejected_content = 0;
7331 *path_unstaged_content = NULL;
7332 *path_new_staged_content = NULL;
7334 err = got_object_id_str(&label1, blob_id);
7335 if (err)
7336 return err;
7337 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7338 if (err)
7339 goto done;
7341 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7342 if (err)
7343 goto done;
7345 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7346 if (err)
7347 goto done;
7349 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7350 if (err)
7351 goto done;
7353 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7354 if (err)
7355 goto done;
7357 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7358 if (err)
7359 goto done;
7361 if (stat(path1, &sb1) == -1) {
7362 err = got_error_from_errno2("stat", path1);
7363 goto done;
7366 if (stat(path2, &sb2) == -1) {
7367 err = got_error_from_errno2("stat", path2);
7368 goto done;
7371 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7372 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7373 if (err)
7374 goto done;
7376 err = got_opentemp_named(path_unstaged_content, &outfile,
7377 "got-unstaged-content");
7378 if (err)
7379 goto done;
7380 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7381 "got-new-staged-content");
7382 if (err)
7383 goto done;
7385 if (fseek(f1, 0L, SEEK_SET) == -1) {
7386 err = got_ferror(f1, GOT_ERR_IO);
7387 goto done;
7389 if (fseek(f2, 0L, SEEK_SET) == -1) {
7390 err = got_ferror(f2, GOT_ERR_IO);
7391 goto done;
7393 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7394 int choice;
7395 err = apply_or_reject_change(&choice, change, ++n,
7396 changes->nchanges, ds, args, diff_flags, relpath,
7397 f1, f2, &line_cur1, &line_cur2,
7398 outfile, rejectfile, patch_cb, patch_arg);
7399 if (err)
7400 goto done;
7401 if (choice == GOT_PATCH_CHOICE_YES)
7402 have_content = 1;
7403 else
7404 have_rejected_content = 1;
7405 if (choice == GOT_PATCH_CHOICE_QUIT)
7406 break;
7408 if (have_content || have_rejected_content)
7409 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7410 outfile, rejectfile);
7411 done:
7412 free(label1);
7413 if (blob)
7414 got_object_blob_close(blob);
7415 if (staged_blob)
7416 got_object_blob_close(staged_blob);
7417 if (f1 && fclose(f1) == EOF && err == NULL)
7418 err = got_error_from_errno2("fclose", path1);
7419 if (f2 && fclose(f2) == EOF && err == NULL)
7420 err = got_error_from_errno2("fclose", path2);
7421 if (outfile && fclose(outfile) == EOF && err == NULL)
7422 err = got_error_from_errno2("fclose", *path_unstaged_content);
7423 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7424 err = got_error_from_errno2("fclose", *path_new_staged_content);
7425 if (path1 && unlink(path1) == -1 && err == NULL)
7426 err = got_error_from_errno2("unlink", path1);
7427 if (path2 && unlink(path2) == -1 && err == NULL)
7428 err = got_error_from_errno2("unlink", path2);
7429 if (err || !have_content) {
7430 if (*path_unstaged_content &&
7431 unlink(*path_unstaged_content) == -1 && err == NULL)
7432 err = got_error_from_errno2("unlink",
7433 *path_unstaged_content);
7434 free(*path_unstaged_content);
7435 *path_unstaged_content = NULL;
7437 if (err || !have_rejected_content) {
7438 if (*path_new_staged_content &&
7439 unlink(*path_new_staged_content) == -1 && err == NULL)
7440 err = got_error_from_errno2("unlink",
7441 *path_new_staged_content);
7442 free(*path_new_staged_content);
7443 *path_new_staged_content = NULL;
7445 free(args);
7446 if (ds) {
7447 got_diff_state_free(ds);
7448 free(ds);
7450 if (changes)
7451 got_diff_free_changes(changes);
7452 free(path1);
7453 free(path2);
7454 return err;
7457 static const struct got_error *
7458 unstage_path(void *arg, unsigned char status,
7459 unsigned char staged_status, const char *relpath,
7460 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7461 struct got_object_id *commit_id, int dirfd, const char *de_name)
7463 const struct got_error *err = NULL;
7464 struct unstage_path_arg *a = arg;
7465 struct got_fileindex_entry *ie;
7466 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7467 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7468 char *path_new_staged_content = NULL;
7469 char *id_str = NULL, *label_orig = NULL;
7470 int local_changes_subsumed;
7471 struct stat sb;
7473 if (staged_status != GOT_STATUS_ADD &&
7474 staged_status != GOT_STATUS_MODIFY &&
7475 staged_status != GOT_STATUS_DELETE)
7476 return NULL;
7478 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7479 if (ie == NULL)
7480 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7482 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7483 == -1)
7484 return got_error_from_errno("asprintf");
7486 err = got_object_id_str(&id_str,
7487 commit_id ? commit_id : a->worktree->base_commit_id);
7488 if (err)
7489 goto done;
7490 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7491 id_str) == -1) {
7492 err = got_error_from_errno("asprintf");
7493 goto done;
7496 switch (staged_status) {
7497 case GOT_STATUS_MODIFY:
7498 err = got_object_open_as_blob(&blob_base, a->repo,
7499 blob_id, 8192);
7500 if (err)
7501 break;
7502 /* fall through */
7503 case GOT_STATUS_ADD:
7504 if (a->patch_cb) {
7505 if (staged_status == GOT_STATUS_ADD) {
7506 int choice = GOT_PATCH_CHOICE_NONE;
7507 err = (*a->patch_cb)(&choice, a->patch_arg,
7508 staged_status, ie->path, NULL, 1, 1);
7509 if (err)
7510 break;
7511 if (choice != GOT_PATCH_CHOICE_YES)
7512 break;
7513 } else {
7514 err = create_unstaged_content(
7515 &path_unstaged_content,
7516 &path_new_staged_content, blob_id,
7517 staged_blob_id, ie->path, a->repo,
7518 a->patch_cb, a->patch_arg);
7519 if (err || path_unstaged_content == NULL)
7520 break;
7521 if (path_new_staged_content) {
7522 err = got_object_blob_create(
7523 &staged_blob_id,
7524 path_new_staged_content,
7525 a->repo);
7526 if (err)
7527 break;
7528 memcpy(ie->staged_blob_sha1,
7529 staged_blob_id->sha1,
7530 SHA1_DIGEST_LENGTH);
7532 err = merge_file(&local_changes_subsumed,
7533 a->worktree, blob_base, ondisk_path,
7534 relpath, got_fileindex_perms_to_st(ie),
7535 path_unstaged_content, label_orig,
7536 "unstaged", a->repo, a->progress_cb,
7537 a->progress_arg);
7538 if (err == NULL &&
7539 path_new_staged_content == NULL)
7540 got_fileindex_entry_stage_set(ie,
7541 GOT_FILEIDX_STAGE_NONE);
7542 break; /* Done with this file. */
7545 err = got_object_open_as_blob(&blob_staged, a->repo,
7546 staged_blob_id, 8192);
7547 if (err)
7548 break;
7549 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7550 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7551 case GOT_FILEIDX_MODE_REGULAR_FILE:
7552 err = merge_blob(&local_changes_subsumed, a->worktree,
7553 blob_base, ondisk_path, relpath,
7554 got_fileindex_perms_to_st(ie), label_orig,
7555 blob_staged, commit_id ? commit_id :
7556 a->worktree->base_commit_id, a->repo,
7557 a->progress_cb, a->progress_arg);
7558 break;
7559 case GOT_FILEIDX_MODE_SYMLINK:
7560 err = merge_symlink(a->worktree, blob_base, ondisk_path,
7561 relpath, got_fileindex_perms_to_st(ie), label_orig,
7562 blob_staged, a->worktree->base_commit_id, a->repo,
7563 a->progress_cb, a->progress_arg);
7564 break;
7565 default:
7566 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7567 break;
7569 if (err == NULL)
7570 got_fileindex_entry_stage_set(ie,
7571 GOT_FILEIDX_STAGE_NONE);
7572 break;
7573 case GOT_STATUS_DELETE:
7574 if (a->patch_cb) {
7575 int choice = GOT_PATCH_CHOICE_NONE;
7576 err = (*a->patch_cb)(&choice, a->patch_arg,
7577 staged_status, ie->path, NULL, 1, 1);
7578 if (err)
7579 break;
7580 if (choice == GOT_PATCH_CHOICE_NO)
7581 break;
7582 if (choice != GOT_PATCH_CHOICE_YES) {
7583 err = got_error(GOT_ERR_PATCH_CHOICE);
7584 break;
7587 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7588 err = get_file_status(&status, &sb, ie, ondisk_path,
7589 dirfd, de_name, a->repo);
7590 if (err)
7591 break;
7592 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7593 break;
7595 done:
7596 free(ondisk_path);
7597 if (path_unstaged_content &&
7598 unlink(path_unstaged_content) == -1 && err == NULL)
7599 err = got_error_from_errno2("unlink", path_unstaged_content);
7600 if (path_new_staged_content &&
7601 unlink(path_new_staged_content) == -1 && err == NULL)
7602 err = got_error_from_errno2("unlink", path_new_staged_content);
7603 free(path_unstaged_content);
7604 free(path_new_staged_content);
7605 if (blob_base)
7606 got_object_blob_close(blob_base);
7607 if (blob_staged)
7608 got_object_blob_close(blob_staged);
7609 free(id_str);
7610 free(label_orig);
7611 return err;
7614 const struct got_error *
7615 got_worktree_unstage(struct got_worktree *worktree,
7616 struct got_pathlist_head *paths,
7617 got_worktree_checkout_cb progress_cb, void *progress_arg,
7618 got_worktree_patch_cb patch_cb, void *patch_arg,
7619 struct got_repository *repo)
7621 const struct got_error *err = NULL, *sync_err, *unlockerr;
7622 struct got_pathlist_entry *pe;
7623 struct got_fileindex *fileindex = NULL;
7624 char *fileindex_path = NULL;
7625 struct unstage_path_arg upa;
7627 err = lock_worktree(worktree, LOCK_EX);
7628 if (err)
7629 return err;
7631 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7632 if (err)
7633 goto done;
7635 upa.worktree = worktree;
7636 upa.fileindex = fileindex;
7637 upa.repo = repo;
7638 upa.progress_cb = progress_cb;
7639 upa.progress_arg = progress_arg;
7640 upa.patch_cb = patch_cb;
7641 upa.patch_arg = patch_arg;
7642 TAILQ_FOREACH(pe, paths, entry) {
7643 err = worktree_status(worktree, pe->path, fileindex, repo,
7644 unstage_path, &upa, NULL, NULL, 0, 0);
7645 if (err)
7646 goto done;
7649 sync_err = sync_fileindex(fileindex, fileindex_path);
7650 if (sync_err && err == NULL)
7651 err = sync_err;
7652 done:
7653 free(fileindex_path);
7654 if (fileindex)
7655 got_fileindex_free(fileindex);
7656 unlockerr = lock_worktree(worktree, LOCK_SH);
7657 if (unlockerr && err == NULL)
7658 err = unlockerr;
7659 return err;