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 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target, label_orig,
1076 ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1083 done:
1084 free(ancestor_target);
1085 free(deriv_target);
1086 return err;
1090 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 * blob_deriv acts as the first derived version, and the file on disk
1092 * acts as the second derived version.
1094 static const struct got_error *
1095 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 struct got_blob_object *blob_orig, const char *ondisk_path,
1097 const char *path, uint16_t st_mode, const char *label_orig,
1098 struct got_blob_object *blob_deriv,
1099 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 got_worktree_checkout_cb progress_cb, void *progress_arg)
1102 const struct got_error *err = NULL;
1103 FILE *f_deriv = NULL;
1104 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 char *label_deriv = NULL, *parent;
1107 *local_changes_subsumed = 0;
1109 parent = dirname(ondisk_path);
1110 if (parent == NULL)
1111 return got_error_from_errno2("dirname", ondisk_path);
1113 free(base_path);
1114 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 err = got_error_from_errno("asprintf");
1116 base_path = NULL;
1117 goto done;
1120 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1121 if (err)
1122 goto done;
1123 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1124 blob_deriv);
1125 if (err)
1126 goto done;
1128 err = got_object_id_str(&id_str, deriv_base_commit_id);
1129 if (err)
1130 goto done;
1131 if (asprintf(&label_deriv, "%s: commit %s",
1132 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 err = got_error_from_errno("asprintf");
1134 goto done;
1137 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 label_deriv, repo, progress_cb, progress_arg);
1140 done:
1141 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 err = got_error_from_errno("fclose");
1143 free(base_path);
1144 if (blob_deriv_path) {
1145 unlink(blob_deriv_path);
1146 free(blob_deriv_path);
1148 free(id_str);
1149 free(label_deriv);
1150 return err;
1153 static const struct got_error *
1154 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1158 const struct got_error *err = NULL;
1159 struct got_fileindex_entry *new_ie;
1161 *new_iep = NULL;
1163 err = got_fileindex_entry_alloc(&new_ie, path);
1164 if (err)
1165 return err;
1167 err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 blob_id->sha1, base_commit_id->sha1, 1);
1169 if (err)
1170 goto done;
1172 err = got_fileindex_entry_add(fileindex, new_ie);
1173 done:
1174 if (err)
1175 got_fileindex_entry_free(new_ie);
1176 else
1177 *new_iep = new_ie;
1178 return err;
1181 static mode_t
1182 get_ondisk_perms(int executable, mode_t st_mode)
1184 mode_t xbits = S_IXUSR;
1186 if (executable) {
1187 /* Map read bits to execute bits. */
1188 if (st_mode & S_IRGRP)
1189 xbits |= S_IXGRP;
1190 if (st_mode & S_IROTH)
1191 xbits |= S_IXOTH;
1192 return st_mode | xbits;
1195 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1198 /* forward declaration */
1199 static const struct got_error *
1200 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, int installing_bad_symlink,
1204 int path_is_unversioned, struct got_repository *repo,
1205 got_worktree_checkout_cb progress_cb, void *progress_arg);
1208 * This function assumes that the provided symlink target points at a
1209 * safe location in the work tree!
1211 static const struct got_error *
1212 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 size_t target_len)
1215 const struct got_error *err = NULL;
1216 ssize_t elen;
1217 char etarget[PATH_MAX];
1218 int fd;
1221 * "Bad" symlinks (those pointing outside the work tree or into the
1222 * .got directory) are installed in the work tree as a regular file
1223 * which contains the bad symlink target path.
1224 * The new symlink target has already been checked for safety by our
1225 * caller. If we can successfully open a regular file then we simply
1226 * replace this file with a symlink below.
1228 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 if (fd == -1) {
1230 if (errno != ELOOP)
1231 return got_error_from_errno2("open", ondisk_path);
1233 /* We are updating an existing on-disk symlink. */
1234 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1235 if (elen == -1)
1236 return got_error_from_errno2("readlink", ondisk_path);
1238 if (elen == target_len &&
1239 memcmp(etarget, target_path, target_len) == 0)
1240 return NULL; /* nothing to do */
1243 err = update_symlink(ondisk_path, target_path, target_len);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", ondisk_path);
1246 return err;
1249 static const struct got_error *
1250 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 int restoring_missing_file, int reverting_versioned_file,
1253 int path_is_unversioned, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *resolved_path = NULL, *abspath = NULL;
1260 char *path_got = NULL;
1261 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1264 *is_bad_symlink = 0;
1267 * Blob object content specifies the target path of the link.
1268 * If a symbolic link cannot be installed we instead create
1269 * a regular file which contains the link target path stored
1270 * in the blob object.
1272 do {
1273 err = got_object_blob_read_block(&len, blob);
1274 if (len + target_len >= sizeof(target_path)) {
1275 /* Path too long; install as a regular file. */
1276 *is_bad_symlink = 1;
1277 got_object_blob_rewind(blob);
1278 return install_blob(worktree, ondisk_path, path,
1279 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 restoring_missing_file, reverting_versioned_file,
1281 1, path_is_unversioned, repo, progress_cb,
1282 progress_arg);
1284 if (len > 0) {
1285 /* Skip blob object header first time around. */
1286 memcpy(target_path + target_len, buf + hdrlen,
1287 len - hdrlen);
1288 target_len += len - hdrlen;
1289 hdrlen = 0;
1291 } while (len != 0);
1292 target_path[target_len] = '\0';
1295 * Relative symlink target lookup should begin at the directory
1296 * in which the blob object is being installed.
1298 if (!got_path_is_absolute(target_path)) {
1299 char *parent = dirname(ondisk_path);
1300 if (parent == NULL) {
1301 err = got_error_from_errno2("dirname", ondisk_path);
1302 goto done;
1304 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1311 * unveil(2) restricts our view of paths in the filesystem.
1312 * ENOENT will occur if a link target path does not exist or
1313 * if it points outside our unveiled path space.
1315 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 if (resolved_path == NULL) {
1317 if (errno != ENOENT) {
1318 err = got_error_from_errno2("realpath", target_path);
1319 goto done;
1323 /* Only allow symlinks pointing at paths within the work tree. */
1324 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 abspath : target_path), worktree->root_path,
1326 strlen(worktree->root_path))) {
1327 /* install as a regular file */
1328 *is_bad_symlink = 1;
1329 got_object_blob_rewind(blob);
1330 err = install_blob(worktree, ondisk_path, path,
1331 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 restoring_missing_file, reverting_versioned_file, 1,
1333 path_is_unversioned, repo, progress_cb, progress_arg);
1334 goto done;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 GOT_WORKTREE_GOT_DIR) == -1) {
1340 err = got_error_from_errno("asprintf");
1341 goto done;
1343 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 abspath : target_path), path_got, strlen(path_got))) {
1345 /* install as a regular file */
1346 *is_bad_symlink = 1;
1347 got_object_blob_rewind(blob);
1348 err = install_blob(worktree, ondisk_path, path,
1349 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 restoring_missing_file, reverting_versioned_file, 1,
1351 path_is_unversioned, repo, progress_cb, progress_arg);
1352 goto done;
1355 if (symlink(target_path, ondisk_path) == -1) {
1356 if (errno == EEXIST) {
1357 if (path_is_unversioned) {
1358 err = (*progress_cb)(progress_arg,
1359 GOT_STATUS_UNVERSIONED, path);
1360 goto done;
1362 err = replace_existing_symlink(ondisk_path,
1363 target_path, target_len);
1364 if (err)
1365 goto done;
1366 if (progress_cb) {
1367 err = (*progress_cb)(progress_arg,
1368 reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1370 path);
1372 goto done; /* Nothing else to do. */
1375 if (errno == ENOENT) {
1376 char *parent = dirname(ondisk_path);
1377 if (parent == NULL) {
1378 err = got_error_from_errno2("dirname",
1379 ondisk_path);
1380 goto done;
1382 err = add_dir_on_disk(worktree, parent);
1383 if (err)
1384 goto done;
1386 * Retry, and fall through to error handling
1387 * below if this second attempt fails.
1389 if (symlink(target_path, ondisk_path) != -1) {
1390 err = NULL; /* success */
1391 goto done;
1395 /* Handle errors from first or second creation attempt. */
1396 if (errno == ENAMETOOLONG) {
1397 /* bad target path; install as a regular file */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 err = install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file, 1,
1403 path_is_unversioned, repo,
1404 progress_cb, progress_arg);
1405 } else if (errno == ENOTDIR) {
1406 err = got_error_path(ondisk_path,
1407 GOT_ERR_FILE_OBSTRUCTED);
1408 } else {
1409 err = got_error_from_errno3("symlink",
1410 target_path, ondisk_path);
1412 } else if (progress_cb)
1413 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1414 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1415 done:
1416 free(resolved_path);
1417 free(abspath);
1418 free(path_got);
1419 return err;
1422 static const struct got_error *
1423 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1424 const char *path, mode_t te_mode, mode_t st_mode,
1425 struct got_blob_object *blob, int restoring_missing_file,
1426 int reverting_versioned_file, int installing_bad_symlink,
1427 int path_is_unversioned, struct got_repository *repo,
1428 got_worktree_checkout_cb progress_cb, void *progress_arg)
1430 const struct got_error *err = NULL;
1431 int fd = -1;
1432 size_t len, hdrlen;
1433 int update = 0;
1434 char *tmppath = NULL;
1436 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1437 GOT_DEFAULT_FILE_MODE);
1438 if (fd == -1) {
1439 if (errno == ENOENT) {
1440 char *parent = dirname(path);
1441 if (parent == NULL)
1442 return got_error_from_errno2("dirname", path);
1443 err = add_dir_on_disk(worktree, parent);
1444 if (err)
1445 return err;
1446 fd = open(ondisk_path,
1447 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 GOT_DEFAULT_FILE_MODE);
1449 if (fd == -1)
1450 return got_error_from_errno2("open",
1451 ondisk_path);
1452 } else if (errno == EEXIST) {
1453 if (path_is_unversioned) {
1454 err = (*progress_cb)(progress_arg,
1455 GOT_STATUS_UNVERSIONED, path);
1456 goto done;
1458 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1459 /* TODO file is obstructed; do something */
1460 err = got_error_path(ondisk_path,
1461 GOT_ERR_FILE_OBSTRUCTED);
1462 goto done;
1463 } else {
1464 err = got_opentemp_named_fd(&tmppath, &fd,
1465 ondisk_path);
1466 if (err)
1467 goto done;
1468 update = 1;
1470 } else
1471 return got_error_from_errno2("open", ondisk_path);
1474 if (progress_cb) {
1475 if (restoring_missing_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 path);
1478 else if (reverting_versioned_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 path);
1481 else
1482 err = (*progress_cb)(progress_arg,
1483 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 if (err)
1485 goto done;
1488 hdrlen = got_object_blob_get_hdrlen(blob);
1489 do {
1490 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 err = got_object_blob_read_block(&len, blob);
1492 if (err)
1493 break;
1494 if (len > 0) {
1495 /* Skip blob object header first time around. */
1496 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 if (outlen == -1) {
1498 err = got_error_from_errno("write");
1499 goto done;
1500 } else if (outlen != len - hdrlen) {
1501 err = got_error(GOT_ERR_IO);
1502 goto done;
1504 hdrlen = 0;
1506 } while (len != 0);
1508 if (fsync(fd) != 0) {
1509 err = got_error_from_errno("fsync");
1510 goto done;
1513 if (update) {
1514 if (rename(tmppath, ondisk_path) != 0) {
1515 err = got_error_from_errno3("rename", tmppath,
1516 ondisk_path);
1517 unlink(tmppath);
1518 goto done;
1522 if (chmod(ondisk_path,
1523 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 err = got_error_from_errno2("chmod", ondisk_path);
1525 goto done;
1528 done:
1529 if (fd != -1 && close(fd) != 0 && err == NULL)
1530 err = got_error_from_errno("close");
1531 free(tmppath);
1532 return err;
1535 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1536 static const struct got_error *
1537 get_modified_file_content_status(unsigned char *status, FILE *f)
1539 const struct got_error *err = NULL;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1545 int i = 0;
1546 char *line;
1547 size_t len;
1548 const char delim[3] = {'\0', '\0', '\0'};
1550 while (*status == GOT_STATUS_MODIFY) {
1551 line = fparseln(f, &len, NULL, delim, 0);
1552 if (line == NULL) {
1553 if (feof(f))
1554 break;
1555 err = got_ferror(f, GOT_ERR_IO);
1556 break;
1559 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1560 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1561 == 0)
1562 *status = GOT_STATUS_CONFLICT;
1563 else
1564 i++;
1568 return err;
1571 static int
1572 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1574 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1575 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1578 static int
1579 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1581 return !(ie->ctime_sec == sb->st_ctime &&
1582 ie->ctime_nsec == sb->st_ctimensec &&
1583 ie->mtime_sec == sb->st_mtime &&
1584 ie->mtime_nsec == sb->st_mtimensec &&
1585 ie->size == (sb->st_size & 0xffffffff) &&
1586 !xbit_differs(ie, sb->st_mode));
1589 static unsigned char
1590 get_staged_status(struct got_fileindex_entry *ie)
1592 switch (got_fileindex_entry_stage_get(ie)) {
1593 case GOT_FILEIDX_STAGE_ADD:
1594 return GOT_STATUS_ADD;
1595 case GOT_FILEIDX_STAGE_DELETE:
1596 return GOT_STATUS_DELETE;
1597 case GOT_FILEIDX_STAGE_MODIFY:
1598 return GOT_STATUS_MODIFY;
1599 default:
1600 return GOT_STATUS_NO_CHANGE;
1604 static const struct got_error *
1605 get_symlink_status(unsigned char *status, struct stat *sb,
1606 struct got_fileindex_entry *ie, const char *abspath,
1607 int dirfd, const char *de_name, struct got_blob_object *blob)
1609 const struct got_error *err = NULL;
1610 char target_path[PATH_MAX];
1611 char etarget[PATH_MAX];
1612 ssize_t elen;
1613 size_t len, target_len = 0;
1614 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1615 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1617 *status = GOT_STATUS_NO_CHANGE;
1619 /* Blob object content specifies the target path of the link. */
1620 do {
1621 err = got_object_blob_read_block(&len, blob);
1622 if (err)
1623 return err;
1624 if (len + target_len >= sizeof(target_path)) {
1626 * Should not happen. The blob contents were OK
1627 * when this symlink was installed.
1629 return got_error(GOT_ERR_NO_SPACE);
1631 if (len > 0) {
1632 /* Skip blob object header first time around. */
1633 memcpy(target_path + target_len, buf + hdrlen,
1634 len - hdrlen);
1635 target_len += len - hdrlen;
1636 hdrlen = 0;
1638 } while (len != 0);
1639 target_path[target_len] = '\0';
1641 if (dirfd != -1) {
1642 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1643 if (elen == -1)
1644 return got_error_from_errno2("readlinkat", abspath);
1645 } else {
1646 elen = readlink(abspath, etarget, sizeof(etarget));
1647 if (elen == -1)
1648 return got_error_from_errno2("readlinkat", abspath);
1651 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1652 *status = GOT_STATUS_MODIFY;
1654 return NULL;
1657 static const struct got_error *
1658 get_file_status(unsigned char *status, struct stat *sb,
1659 struct got_fileindex_entry *ie, const char *abspath,
1660 int dirfd, const char *de_name, struct got_repository *repo)
1662 const struct got_error *err = NULL;
1663 struct got_object_id id;
1664 size_t hdrlen;
1665 int fd = -1;
1666 FILE *f = NULL;
1667 uint8_t fbuf[8192];
1668 struct got_blob_object *blob = NULL;
1669 size_t flen, blen;
1670 unsigned char staged_status = get_staged_status(ie);
1672 *status = GOT_STATUS_NO_CHANGE;
1675 * Whenever the caller provides a directory descriptor and a
1676 * directory entry name for the file, use them! This prevents
1677 * race conditions if filesystem paths change beneath our feet.
1679 if (dirfd != -1) {
1680 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1681 if (errno == ENOENT) {
1682 if (got_fileindex_entry_has_file_on_disk(ie))
1683 *status = GOT_STATUS_MISSING;
1684 else
1685 *status = GOT_STATUS_DELETE;
1686 goto done;
1688 err = got_error_from_errno2("fstatat", abspath);
1689 goto done;
1691 } else {
1692 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1693 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1694 return got_error_from_errno2("open", abspath);
1695 else if (fd == -1 && errno == ELOOP) {
1696 if (lstat(abspath, sb) == -1)
1697 return got_error_from_errno2("lstat", abspath);
1698 } else if (fd == -1 || fstat(fd, sb) == -1) {
1699 if (errno == ENOENT) {
1700 if (got_fileindex_entry_has_file_on_disk(ie))
1701 *status = GOT_STATUS_MISSING;
1702 else
1703 *status = GOT_STATUS_DELETE;
1704 goto done;
1706 err = got_error_from_errno2("fstat", abspath);
1707 goto done;
1711 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1712 *status = GOT_STATUS_OBSTRUCTED;
1713 goto done;
1716 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1717 *status = GOT_STATUS_DELETE;
1718 goto done;
1719 } else if (!got_fileindex_entry_has_blob(ie) &&
1720 staged_status != GOT_STATUS_ADD) {
1721 *status = GOT_STATUS_ADD;
1722 goto done;
1725 if (!stat_info_differs(ie, sb))
1726 goto done;
1728 if (staged_status == GOT_STATUS_MODIFY ||
1729 staged_status == GOT_STATUS_ADD)
1730 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1731 else
1732 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1734 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 if (err)
1736 goto done;
1738 if (S_ISLNK(sb->st_mode)) {
1739 /* Staging changes to symlinks is not yet(?) supported. */
1740 if (staged_status != GOT_STATUS_NO_CHANGE) {
1741 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1742 goto done;
1744 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1745 de_name, blob);
1746 goto done;
1750 if (dirfd != -1) {
1751 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1752 if (fd == -1) {
1753 err = got_error_from_errno2("openat", abspath);
1754 goto done;
1758 f = fdopen(fd, "r");
1759 if (f == NULL) {
1760 err = got_error_from_errno2("fdopen", abspath);
1761 goto done;
1763 fd = -1;
1764 hdrlen = got_object_blob_get_hdrlen(blob);
1765 for (;;) {
1766 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1767 err = got_object_blob_read_block(&blen, blob);
1768 if (err)
1769 goto done;
1770 /* Skip length of blob object header first time around. */
1771 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1772 if (flen == 0 && ferror(f)) {
1773 err = got_error_from_errno("fread");
1774 goto done;
1776 if (blen == 0) {
1777 if (flen != 0)
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1780 } else if (flen == 0) {
1781 if (blen != 0)
1782 *status = GOT_STATUS_MODIFY;
1783 break;
1784 } else if (blen - hdrlen == flen) {
1785 /* Skip blob object header first time around. */
1786 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1787 *status = GOT_STATUS_MODIFY;
1788 break;
1790 } else {
1791 *status = GOT_STATUS_MODIFY;
1792 break;
1794 hdrlen = 0;
1797 if (*status == GOT_STATUS_MODIFY) {
1798 rewind(f);
1799 err = get_modified_file_content_status(status, f);
1800 } else if (xbit_differs(ie, sb->st_mode))
1801 *status = GOT_STATUS_MODE_CHANGE;
1802 done:
1803 if (blob)
1804 got_object_blob_close(blob);
1805 if (f != NULL && fclose(f) == EOF && err == NULL)
1806 err = got_error_from_errno2("fclose", abspath);
1807 if (fd != -1 && close(fd) == -1 && err == NULL)
1808 err = got_error_from_errno2("close", abspath);
1809 return err;
1813 * Update timestamps in the file index if a file is unmodified and
1814 * we had to run a full content comparison to find out.
1816 static const struct got_error *
1817 sync_timestamps(char *ondisk_path, unsigned char status,
1818 struct got_fileindex_entry *ie, struct stat *sb)
1820 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1821 return got_fileindex_entry_update(ie, ondisk_path,
1822 ie->blob_sha1, ie->commit_sha1, 1);
1824 return NULL;
1827 static const struct got_error *
1828 update_blob(struct got_worktree *worktree,
1829 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1830 struct got_tree_entry *te, const char *path,
1831 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1832 void *progress_arg)
1834 const struct got_error *err = NULL;
1835 struct got_blob_object *blob = NULL;
1836 char *ondisk_path;
1837 unsigned char status = GOT_STATUS_NO_CHANGE;
1838 struct stat sb;
1840 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1841 return got_error_from_errno("asprintf");
1843 if (ie) {
1844 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1845 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1846 goto done;
1848 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1849 repo);
1850 if (err)
1851 goto done;
1852 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1853 sb.st_mode = got_fileindex_perms_to_st(ie);
1854 } else {
1855 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 status = GOT_STATUS_UNVERSIONED;
1859 if (status == GOT_STATUS_OBSTRUCTED) {
1860 err = (*progress_cb)(progress_arg, status, path);
1861 goto done;
1863 if (status == GOT_STATUS_CONFLICT) {
1864 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1865 path);
1866 goto done;
1869 if (ie && status != GOT_STATUS_MISSING &&
1870 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1874 err = sync_timestamps(ondisk_path, status, ie, &sb);
1875 if (err)
1876 goto done;
1877 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 path);
1879 goto done;
1881 if (got_fileindex_entry_has_blob(ie) &&
1882 memcmp(ie->blob_sha1, te->id.sha1,
1883 SHA1_DIGEST_LENGTH) == 0) {
1884 err = sync_timestamps(ondisk_path, status, ie, &sb);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode)) {
1919 err = merge_symlink(worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig,
1921 blob, worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1923 } else {
1924 err = merge_blob(&update_timestamps, worktree, blob2,
1925 ondisk_path, path, sb.st_mode, label_orig, blob,
1926 worktree->base_commit_id, repo,
1927 progress_cb, progress_arg);
1929 free(label_orig);
1930 if (blob2)
1931 got_object_blob_close(blob2);
1932 if (err)
1933 goto done;
1935 * Do not update timestamps of files with local changes.
1936 * Otherwise, a future status walk would treat them as
1937 * unmodified files again.
1939 err = got_fileindex_entry_update(ie, ondisk_path,
1940 blob->id.sha1, worktree->base_commit_id->sha1,
1941 update_timestamps);
1942 } else if (status == GOT_STATUS_MODE_CHANGE) {
1943 err = got_fileindex_entry_update(ie, ondisk_path,
1944 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1945 } else if (status == GOT_STATUS_DELETE) {
1946 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1947 if (err)
1948 goto done;
1949 err = got_fileindex_entry_update(ie, ondisk_path,
1950 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 if (err)
1952 goto done;
1953 } else {
1954 int is_bad_symlink = 0;
1955 if (S_ISLNK(te->mode)) {
1956 err = install_symlink(&is_bad_symlink, worktree,
1957 ondisk_path, path, blob,
1958 status == GOT_STATUS_MISSING, 0,
1959 status == GOT_STATUS_UNVERSIONED, repo,
1960 progress_cb, progress_arg);
1961 } else {
1962 err = install_blob(worktree, ondisk_path, path,
1963 te->mode, sb.st_mode, blob,
1964 status == GOT_STATUS_MISSING, 0, 0,
1965 status == GOT_STATUS_UNVERSIONED, repo,
1966 progress_cb, progress_arg);
1968 if (err)
1969 goto done;
1971 if (ie) {
1972 err = got_fileindex_entry_update(ie, ondisk_path,
1973 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1974 } else {
1975 err = create_fileindex_entry(&ie, fileindex,
1976 worktree->base_commit_id, ondisk_path, path,
1977 &blob->id);
1979 if (err)
1980 goto done;
1982 if (is_bad_symlink) {
1983 got_fileindex_entry_filetype_set(ie,
1984 GOT_FILEIDX_MODE_BAD_SYMLINK);
1987 got_object_blob_close(blob);
1988 done:
1989 free(ondisk_path);
1990 return err;
1993 static const struct got_error *
1994 remove_ondisk_file(const char *root_path, const char *path)
1996 const struct got_error *err = NULL;
1997 char *ondisk_path = NULL;
1999 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2000 return got_error_from_errno("asprintf");
2002 if (unlink(ondisk_path) == -1) {
2003 if (errno != ENOENT)
2004 err = got_error_from_errno2("unlink", ondisk_path);
2005 } else {
2006 char *parent = dirname(ondisk_path);
2007 while (parent && strcmp(parent, root_path) != 0) {
2008 if (rmdir(parent) == -1) {
2009 if (errno != ENOTEMPTY)
2010 err = got_error_from_errno2("rmdir",
2011 parent);
2012 break;
2014 parent = dirname(parent);
2017 free(ondisk_path);
2018 return err;
2021 static const struct got_error *
2022 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2023 struct got_fileindex_entry *ie, struct got_repository *repo,
2024 got_worktree_checkout_cb progress_cb, void *progress_arg)
2026 const struct got_error *err = NULL;
2027 unsigned char status;
2028 struct stat sb;
2029 char *ondisk_path;
2031 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2032 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2034 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2035 == -1)
2036 return got_error_from_errno("asprintf");
2038 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2039 if (err)
2040 goto done;
2042 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2043 char ondisk_target[PATH_MAX];
2044 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2045 sizeof(ondisk_target));
2046 if (ondisk_len == -1) {
2047 err = got_error_from_errno2("readlink", ondisk_path);
2048 goto done;
2050 ondisk_target[ondisk_len] = '\0';
2051 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2052 NULL, NULL, /* XXX pass common ancestor info? */
2053 ondisk_target, ondisk_path);
2054 if (err)
2055 goto done;
2056 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2057 ie->path);
2058 goto done;
2061 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2062 status == GOT_STATUS_ADD) {
2063 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2064 if (err)
2065 goto done;
2067 * Preserve the working file and change the deleted blob's
2068 * entry into a schedule-add entry.
2070 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2071 0);
2072 } else {
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2074 if (err)
2075 goto done;
2076 if (status == GOT_STATUS_NO_CHANGE) {
2077 err = remove_ondisk_file(worktree->root_path, ie->path);
2078 if (err)
2079 goto done;
2081 got_fileindex_entry_remove(fileindex, ie);
2083 done:
2084 free(ondisk_path);
2085 return err;
2088 struct diff_cb_arg {
2089 struct got_fileindex *fileindex;
2090 struct got_worktree *worktree;
2091 struct got_repository *repo;
2092 got_worktree_checkout_cb progress_cb;
2093 void *progress_arg;
2094 got_cancel_cb cancel_cb;
2095 void *cancel_arg;
2098 static const struct got_error *
2099 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2100 struct got_tree_entry *te, const char *parent_path)
2102 struct diff_cb_arg *a = arg;
2104 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2105 return got_error(GOT_ERR_CANCELLED);
2107 return update_blob(a->worktree, a->fileindex, ie, te,
2108 ie->path, a->repo, a->progress_cb, a->progress_arg);
2111 static const struct got_error *
2112 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2114 struct diff_cb_arg *a = arg;
2116 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2117 return got_error(GOT_ERR_CANCELLED);
2119 return delete_blob(a->worktree, a->fileindex, ie,
2120 a->repo, a->progress_cb, a->progress_arg);
2123 static const struct got_error *
2124 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2126 struct diff_cb_arg *a = arg;
2127 const struct got_error *err;
2128 char *path;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 if (got_object_tree_entry_is_submodule(te))
2134 return NULL;
2136 if (asprintf(&path, "%s%s%s", parent_path,
2137 parent_path[0] ? "/" : "", te->name)
2138 == -1)
2139 return got_error_from_errno("asprintf");
2141 if (S_ISDIR(te->mode))
2142 err = add_dir_on_disk(a->worktree, path);
2143 else
2144 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2145 a->repo, a->progress_cb, a->progress_arg);
2147 free(path);
2148 return err;
2151 static const struct got_error *
2152 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2154 const struct got_error *err = NULL;
2155 char *uuidstr = NULL;
2156 uint32_t uuid_status;
2158 *refname = NULL;
2160 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2161 if (uuid_status != uuid_s_ok)
2162 return got_error_uuid(uuid_status, "uuid_to_string");
2164 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2165 == -1) {
2166 err = got_error_from_errno("asprintf");
2167 *refname = NULL;
2169 free(uuidstr);
2170 return err;
2173 const struct got_error *
2174 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2176 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2179 static const struct got_error *
2180 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2182 return get_ref_name(refname, worktree,
2183 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2186 static const struct got_error *
2187 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2192 static const struct got_error *
2193 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2195 return get_ref_name(refname, worktree,
2196 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2206 static const struct got_error *
2207 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2234 const struct got_error *
2235 got_worktree_get_histedit_script_path(char **path,
2236 struct got_worktree *worktree)
2238 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2239 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2240 *path = NULL;
2241 return got_error_from_errno("asprintf");
2243 return NULL;
2247 * Prevent Git's garbage collector from deleting our base commit by
2248 * setting a reference to our base commit's ID.
2250 static const struct got_error *
2251 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 struct got_reference *ref = NULL;
2255 char *refname;
2257 err = got_worktree_get_base_ref_name(&refname, worktree);
2258 if (err)
2259 return err;
2261 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2262 if (err)
2263 goto done;
2265 err = got_ref_write(ref, repo);
2266 done:
2267 free(refname);
2268 if (ref)
2269 got_ref_close(ref);
2270 return err;
2273 static const struct got_error *
2274 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2276 const struct got_error *err = NULL;
2278 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2279 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2280 err = got_error_from_errno("asprintf");
2281 *fileindex_path = NULL;
2283 return err;
2287 static const struct got_error *
2288 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2289 struct got_worktree *worktree)
2291 const struct got_error *err = NULL;
2292 FILE *index = NULL;
2294 *fileindex_path = NULL;
2295 *fileindex = got_fileindex_alloc();
2296 if (*fileindex == NULL)
2297 return got_error_from_errno("got_fileindex_alloc");
2299 err = get_fileindex_path(fileindex_path, worktree);
2300 if (err)
2301 goto done;
2303 index = fopen(*fileindex_path, "rb");
2304 if (index == NULL) {
2305 if (errno != ENOENT)
2306 err = got_error_from_errno2("fopen", *fileindex_path);
2307 } else {
2308 err = got_fileindex_read(*fileindex, index);
2309 if (fclose(index) != 0 && err == NULL)
2310 err = got_error_from_errno("fclose");
2312 done:
2313 if (err) {
2314 free(*fileindex_path);
2315 *fileindex_path = NULL;
2316 got_fileindex_free(*fileindex);
2317 *fileindex = NULL;
2319 return err;
2322 struct bump_base_commit_id_arg {
2323 struct got_object_id *base_commit_id;
2324 const char *path;
2325 size_t path_len;
2326 const char *entry_name;
2327 got_worktree_checkout_cb progress_cb;
2328 void *progress_arg;
2331 /* Bump base commit ID of all files within an updated part of the work tree. */
2332 static const struct got_error *
2333 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2335 const struct got_error *err;
2336 struct bump_base_commit_id_arg *a = arg;
2338 if (a->entry_name) {
2339 if (strcmp(ie->path, a->path) != 0)
2340 return NULL;
2341 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2342 return NULL;
2344 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2345 SHA1_DIGEST_LENGTH) == 0)
2346 return NULL;
2348 if (a->progress_cb) {
2349 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2350 ie->path);
2351 if (err)
2352 return err;
2354 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2355 return NULL;
2358 static const struct got_error *
2359 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2361 const struct got_error *err = NULL;
2362 char *new_fileindex_path = NULL;
2363 FILE *new_index = NULL;
2364 struct timespec timeout;
2366 err = got_opentemp_named(&new_fileindex_path, &new_index,
2367 fileindex_path);
2368 if (err)
2369 goto done;
2371 err = got_fileindex_write(fileindex, new_index);
2372 if (err)
2373 goto done;
2375 if (rename(new_fileindex_path, fileindex_path) != 0) {
2376 err = got_error_from_errno3("rename", new_fileindex_path,
2377 fileindex_path);
2378 unlink(new_fileindex_path);
2382 * Sleep for a short amount of time to ensure that files modified after
2383 * this program exits have a different time stamp from the one which
2384 * was recorded in the file index.
2386 timeout.tv_sec = 0;
2387 timeout.tv_nsec = 1;
2388 nanosleep(&timeout, NULL);
2389 done:
2390 if (new_index)
2391 fclose(new_index);
2392 free(new_fileindex_path);
2393 return err;
2396 static const struct got_error *
2397 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2398 struct got_object_id **tree_id, const char *wt_relpath,
2399 struct got_worktree *worktree, struct got_repository *repo)
2401 const struct got_error *err = NULL;
2402 struct got_object_id *id = NULL;
2403 char *in_repo_path = NULL;
2404 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2406 *entry_type = GOT_OBJ_TYPE_ANY;
2407 *tree_relpath = NULL;
2408 *tree_id = NULL;
2410 if (wt_relpath[0] == '\0') {
2411 /* Check out all files within the work tree. */
2412 *entry_type = GOT_OBJ_TYPE_TREE;
2413 *tree_relpath = strdup("");
2414 if (*tree_relpath == NULL) {
2415 err = got_error_from_errno("strdup");
2416 goto done;
2418 err = got_object_id_by_path(tree_id, repo,
2419 worktree->base_commit_id, worktree->path_prefix);
2420 if (err)
2421 goto done;
2422 return NULL;
2425 /* Check out a subset of files in the work tree. */
2427 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2428 is_root_wt ? "" : "/", wt_relpath) == -1) {
2429 err = got_error_from_errno("asprintf");
2430 goto done;
2433 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2434 in_repo_path);
2435 if (err)
2436 goto done;
2438 free(in_repo_path);
2439 in_repo_path = NULL;
2441 err = got_object_get_type(entry_type, repo, id);
2442 if (err)
2443 goto done;
2445 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2446 /* Check out a single file. */
2447 if (strchr(wt_relpath, '/') == NULL) {
2448 /* Check out a single file in work tree's root dir. */
2449 in_repo_path = strdup(worktree->path_prefix);
2450 if (in_repo_path == NULL) {
2451 err = got_error_from_errno("strdup");
2452 goto done;
2454 *tree_relpath = strdup("");
2455 if (*tree_relpath == NULL) {
2456 err = got_error_from_errno("strdup");
2457 goto done;
2459 } else {
2460 /* Check out a single file in a subdirectory. */
2461 err = got_path_dirname(tree_relpath, wt_relpath);
2462 if (err)
2463 return err;
2464 if (asprintf(&in_repo_path, "%s%s%s",
2465 worktree->path_prefix, is_root_wt ? "" : "/",
2466 *tree_relpath) == -1) {
2467 err = got_error_from_errno("asprintf");
2468 goto done;
2471 err = got_object_id_by_path(tree_id, repo,
2472 worktree->base_commit_id, in_repo_path);
2473 } else {
2474 /* Check out all files within a subdirectory. */
2475 *tree_id = got_object_id_dup(id);
2476 if (*tree_id == NULL) {
2477 err = got_error_from_errno("got_object_id_dup");
2478 goto done;
2480 *tree_relpath = strdup(wt_relpath);
2481 if (*tree_relpath == NULL) {
2482 err = got_error_from_errno("strdup");
2483 goto done;
2486 done:
2487 free(id);
2488 free(in_repo_path);
2489 if (err) {
2490 *entry_type = GOT_OBJ_TYPE_ANY;
2491 free(*tree_relpath);
2492 *tree_relpath = NULL;
2493 free(*tree_id);
2494 *tree_id = NULL;
2496 return err;
2499 static const struct got_error *
2500 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2501 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2502 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2503 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2505 const struct got_error *err = NULL;
2506 struct got_commit_object *commit = NULL;
2507 struct got_tree_object *tree = NULL;
2508 struct got_fileindex_diff_tree_cb diff_cb;
2509 struct diff_cb_arg arg;
2511 err = ref_base_commit(worktree, repo);
2512 if (err) {
2513 if (!(err->code == GOT_ERR_ERRNO &&
2514 (errno == EACCES || errno == EROFS)))
2515 goto done;
2516 err = (*progress_cb)(progress_arg,
2517 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2518 if (err)
2519 return err;
2522 err = got_object_open_as_commit(&commit, repo,
2523 worktree->base_commit_id);
2524 if (err)
2525 goto done;
2527 err = got_object_open_as_tree(&tree, repo, tree_id);
2528 if (err)
2529 goto done;
2531 if (entry_name &&
2532 got_object_tree_find_entry(tree, entry_name) == NULL) {
2533 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2534 goto done;
2537 diff_cb.diff_old_new = diff_old_new;
2538 diff_cb.diff_old = diff_old;
2539 diff_cb.diff_new = diff_new;
2540 arg.fileindex = fileindex;
2541 arg.worktree = worktree;
2542 arg.repo = repo;
2543 arg.progress_cb = progress_cb;
2544 arg.progress_arg = progress_arg;
2545 arg.cancel_cb = cancel_cb;
2546 arg.cancel_arg = cancel_arg;
2547 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2548 entry_name, repo, &diff_cb, &arg);
2549 done:
2550 if (tree)
2551 got_object_tree_close(tree);
2552 if (commit)
2553 got_object_commit_close(commit);
2554 return err;
2557 const struct got_error *
2558 got_worktree_checkout_files(struct got_worktree *worktree,
2559 struct got_pathlist_head *paths, struct got_repository *repo,
2560 got_worktree_checkout_cb progress_cb, void *progress_arg,
2561 got_cancel_cb cancel_cb, void *cancel_arg)
2563 const struct got_error *err = NULL, *sync_err, *unlockerr;
2564 struct got_commit_object *commit = NULL;
2565 struct got_tree_object *tree = NULL;
2566 struct got_fileindex *fileindex = NULL;
2567 char *fileindex_path = NULL;
2568 struct got_pathlist_entry *pe;
2569 struct tree_path_data {
2570 SIMPLEQ_ENTRY(tree_path_data) entry;
2571 struct got_object_id *tree_id;
2572 int entry_type;
2573 char *relpath;
2574 char *entry_name;
2575 } *tpd = NULL;
2576 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2578 SIMPLEQ_INIT(&tree_paths);
2580 err = lock_worktree(worktree, LOCK_EX);
2581 if (err)
2582 return err;
2584 /* Map all specified paths to in-repository trees. */
2585 TAILQ_FOREACH(pe, paths, entry) {
2586 tpd = malloc(sizeof(*tpd));
2587 if (tpd == NULL) {
2588 err = got_error_from_errno("malloc");
2589 goto done;
2592 err = find_tree_entry_for_checkout(&tpd->entry_type,
2593 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2594 if (err) {
2595 free(tpd);
2596 goto done;
2599 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2600 err = got_path_basename(&tpd->entry_name, pe->path);
2601 if (err) {
2602 free(tpd->relpath);
2603 free(tpd->tree_id);
2604 free(tpd);
2605 goto done;
2607 } else
2608 tpd->entry_name = NULL;
2610 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2614 * Read the file index.
2615 * Checking out files is supposed to be an idempotent operation.
2616 * If the on-disk file index is incomplete we will try to complete it.
2618 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2619 if (err)
2620 goto done;
2622 tpd = SIMPLEQ_FIRST(&tree_paths);
2623 TAILQ_FOREACH(pe, paths, entry) {
2624 struct bump_base_commit_id_arg bbc_arg;
2626 err = checkout_files(worktree, fileindex, tpd->relpath,
2627 tpd->tree_id, tpd->entry_name, repo,
2628 progress_cb, progress_arg, cancel_cb, cancel_arg);
2629 if (err)
2630 break;
2632 bbc_arg.base_commit_id = worktree->base_commit_id;
2633 bbc_arg.entry_name = tpd->entry_name;
2634 bbc_arg.path = pe->path;
2635 bbc_arg.path_len = pe->path_len;
2636 bbc_arg.progress_cb = progress_cb;
2637 bbc_arg.progress_arg = progress_arg;
2638 err = got_fileindex_for_each_entry_safe(fileindex,
2639 bump_base_commit_id, &bbc_arg);
2640 if (err)
2641 break;
2643 tpd = SIMPLEQ_NEXT(tpd, entry);
2645 sync_err = sync_fileindex(fileindex, fileindex_path);
2646 if (sync_err && err == NULL)
2647 err = sync_err;
2648 done:
2649 free(fileindex_path);
2650 if (tree)
2651 got_object_tree_close(tree);
2652 if (commit)
2653 got_object_commit_close(commit);
2654 if (fileindex)
2655 got_fileindex_free(fileindex);
2656 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2657 tpd = SIMPLEQ_FIRST(&tree_paths);
2658 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2659 free(tpd->relpath);
2660 free(tpd->tree_id);
2661 free(tpd);
2663 unlockerr = lock_worktree(worktree, LOCK_SH);
2664 if (unlockerr && err == NULL)
2665 err = unlockerr;
2666 return err;
2669 struct merge_file_cb_arg {
2670 struct got_worktree *worktree;
2671 struct got_fileindex *fileindex;
2672 got_worktree_checkout_cb progress_cb;
2673 void *progress_arg;
2674 got_cancel_cb cancel_cb;
2675 void *cancel_arg;
2676 const char *label_orig;
2677 struct got_object_id *commit_id2;
2680 static const struct got_error *
2681 merge_file_cb(void *arg, struct got_blob_object *blob1,
2682 struct got_blob_object *blob2, struct got_object_id *id1,
2683 struct got_object_id *id2, const char *path1, const char *path2,
2684 mode_t mode1, mode_t mode2, struct got_repository *repo)
2686 static const struct got_error *err = NULL;
2687 struct merge_file_cb_arg *a = arg;
2688 struct got_fileindex_entry *ie;
2689 char *ondisk_path = NULL;
2690 struct stat sb;
2691 unsigned char status;
2692 int local_changes_subsumed;
2694 if (blob1 && blob2) {
2695 ie = got_fileindex_entry_get(a->fileindex, path2,
2696 strlen(path2));
2697 if (ie == NULL)
2698 return (*a->progress_cb)(a->progress_arg,
2699 GOT_STATUS_MISSING, path2);
2701 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2702 path2) == -1)
2703 return got_error_from_errno("asprintf");
2705 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2706 repo);
2707 if (err)
2708 goto done;
2710 if (status == GOT_STATUS_DELETE) {
2711 err = (*a->progress_cb)(a->progress_arg,
2712 GOT_STATUS_MERGE, path2);
2713 goto done;
2715 if (status != GOT_STATUS_NO_CHANGE &&
2716 status != GOT_STATUS_MODIFY &&
2717 status != GOT_STATUS_CONFLICT &&
2718 status != GOT_STATUS_ADD) {
2719 err = (*a->progress_cb)(a->progress_arg, status, path2);
2720 goto done;
2723 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2724 err = merge_symlink(a->worktree, blob1,
2725 ondisk_path, path2, sb.st_mode, a->label_orig,
2726 blob2, a->commit_id2, repo, a->progress_cb,
2727 a->progress_arg);
2728 } else {
2729 err = merge_blob(&local_changes_subsumed, a->worktree,
2730 blob1, ondisk_path, path2, sb.st_mode,
2731 a->label_orig, blob2, a->commit_id2, repo,
2732 a->progress_cb, a->progress_arg);
2734 } else if (blob1) {
2735 ie = got_fileindex_entry_get(a->fileindex, path1,
2736 strlen(path1));
2737 if (ie == NULL)
2738 return (*a->progress_cb)(a->progress_arg,
2739 GOT_STATUS_MISSING, path1);
2741 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2742 path1) == -1)
2743 return got_error_from_errno("asprintf");
2745 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2746 repo);
2747 if (err)
2748 goto done;
2750 switch (status) {
2751 case GOT_STATUS_NO_CHANGE:
2752 err = (*a->progress_cb)(a->progress_arg,
2753 GOT_STATUS_DELETE, path1);
2754 if (err)
2755 goto done;
2756 err = remove_ondisk_file(a->worktree->root_path, path1);
2757 if (err)
2758 goto done;
2759 if (ie)
2760 got_fileindex_entry_mark_deleted_from_disk(ie);
2761 break;
2762 case GOT_STATUS_DELETE:
2763 case GOT_STATUS_MISSING:
2764 err = (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_DELETE, path1);
2766 if (err)
2767 goto done;
2768 if (ie)
2769 got_fileindex_entry_mark_deleted_from_disk(ie);
2770 break;
2771 case GOT_STATUS_ADD:
2772 case GOT_STATUS_MODIFY:
2773 case GOT_STATUS_CONFLICT:
2774 err = (*a->progress_cb)(a->progress_arg,
2775 GOT_STATUS_CANNOT_DELETE, path1);
2776 if (err)
2777 goto done;
2778 break;
2779 case GOT_STATUS_OBSTRUCTED:
2780 err = (*a->progress_cb)(a->progress_arg, status, path1);
2781 if (err)
2782 goto done;
2783 break;
2784 default:
2785 break;
2787 } else if (blob2) {
2788 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2789 path2) == -1)
2790 return got_error_from_errno("asprintf");
2791 ie = got_fileindex_entry_get(a->fileindex, path2,
2792 strlen(path2));
2793 if (ie) {
2794 err = get_file_status(&status, &sb, ie, ondisk_path,
2795 -1, NULL, repo);
2796 if (err)
2797 goto done;
2798 if (status != GOT_STATUS_NO_CHANGE &&
2799 status != GOT_STATUS_MODIFY &&
2800 status != GOT_STATUS_CONFLICT &&
2801 status != GOT_STATUS_ADD) {
2802 err = (*a->progress_cb)(a->progress_arg,
2803 status, path2);
2804 goto done;
2806 if (S_ISLNK(mode2)) {
2807 err = merge_symlink(a->worktree, NULL,
2808 ondisk_path, path2, sb.st_mode,
2809 a->label_orig, blob2, a->commit_id2,
2810 repo, a->progress_cb, a->progress_arg);
2811 if (err)
2812 goto done;
2813 } else {
2814 err = merge_blob(&local_changes_subsumed,
2815 a->worktree, NULL, ondisk_path, path2,
2816 sb.st_mode, a->label_orig, blob2,
2817 a->commit_id2, repo, a->progress_cb,
2818 a->progress_arg);
2819 if (err)
2820 goto done;
2822 if (status == GOT_STATUS_DELETE) {
2823 err = got_fileindex_entry_update(ie,
2824 ondisk_path, blob2->id.sha1,
2825 a->worktree->base_commit_id->sha1, 0);
2826 if (err)
2827 goto done;
2829 } else {
2830 int is_bad_symlink = 0;
2831 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2832 if (S_ISLNK(mode2)) {
2833 err = install_symlink(&is_bad_symlink,
2834 a->worktree, ondisk_path, path2, blob2, 0,
2835 0, 1, repo, a->progress_cb, a->progress_arg);
2836 } else {
2837 err = install_blob(a->worktree, ondisk_path, path2,
2838 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2839 a->progress_cb, a->progress_arg);
2841 if (err)
2842 goto done;
2843 err = got_fileindex_entry_alloc(&ie, path2);
2844 if (err)
2845 goto done;
2846 err = got_fileindex_entry_update(ie, ondisk_path,
2847 NULL, NULL, 1);
2848 if (err) {
2849 got_fileindex_entry_free(ie);
2850 goto done;
2852 err = got_fileindex_entry_add(a->fileindex, ie);
2853 if (err) {
2854 got_fileindex_entry_free(ie);
2855 goto done;
2857 if (is_bad_symlink) {
2858 got_fileindex_entry_filetype_set(ie,
2859 GOT_FILEIDX_MODE_BAD_SYMLINK);
2863 done:
2864 free(ondisk_path);
2865 return err;
2868 struct check_merge_ok_arg {
2869 struct got_worktree *worktree;
2870 struct got_repository *repo;
2873 static const struct got_error *
2874 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2876 const struct got_error *err = NULL;
2877 struct check_merge_ok_arg *a = arg;
2878 unsigned char status;
2879 struct stat sb;
2880 char *ondisk_path;
2882 /* Reject merges into a work tree with mixed base commits. */
2883 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2884 SHA1_DIGEST_LENGTH))
2885 return got_error(GOT_ERR_MIXED_COMMITS);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2888 == -1)
2889 return got_error_from_errno("asprintf");
2891 /* Reject merges into a work tree with conflicted files. */
2892 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2893 if (err)
2894 return err;
2895 if (status == GOT_STATUS_CONFLICT)
2896 return got_error(GOT_ERR_CONFLICTS);
2898 return NULL;
2901 static const struct got_error *
2902 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2903 const char *fileindex_path, struct got_object_id *commit_id1,
2904 struct got_object_id *commit_id2, struct got_repository *repo,
2905 got_worktree_checkout_cb progress_cb, void *progress_arg,
2906 got_cancel_cb cancel_cb, void *cancel_arg)
2908 const struct got_error *err = NULL, *sync_err;
2909 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2910 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2911 struct merge_file_cb_arg arg;
2912 char *label_orig = NULL;
2914 if (commit_id1) {
2915 char *id_str;
2917 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2918 worktree->path_prefix);
2919 if (err)
2920 goto done;
2922 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2923 if (err)
2924 goto done;
2926 err = got_object_id_str(&id_str, commit_id1);
2927 if (err)
2928 goto done;
2930 if (asprintf(&label_orig, "%s: commit %s",
2931 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2932 err = got_error_from_errno("asprintf");
2933 free(id_str);
2934 goto done;
2936 free(id_str);
2939 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2940 worktree->path_prefix);
2941 if (err)
2942 goto done;
2944 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2945 if (err)
2946 goto done;
2948 arg.worktree = worktree;
2949 arg.fileindex = fileindex;
2950 arg.progress_cb = progress_cb;
2951 arg.progress_arg = progress_arg;
2952 arg.cancel_cb = cancel_cb;
2953 arg.cancel_arg = cancel_arg;
2954 arg.label_orig = label_orig;
2955 arg.commit_id2 = commit_id2;
2956 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2957 sync_err = sync_fileindex(fileindex, fileindex_path);
2958 if (sync_err && err == NULL)
2959 err = sync_err;
2960 done:
2961 if (tree1)
2962 got_object_tree_close(tree1);
2963 if (tree2)
2964 got_object_tree_close(tree2);
2965 free(label_orig);
2966 return err;
2969 const struct got_error *
2970 got_worktree_merge_files(struct got_worktree *worktree,
2971 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2972 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2973 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2975 const struct got_error *err, *unlockerr;
2976 char *fileindex_path = NULL;
2977 struct got_fileindex *fileindex = NULL;
2978 struct check_merge_ok_arg mok_arg;
2980 err = lock_worktree(worktree, LOCK_EX);
2981 if (err)
2982 return err;
2984 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2985 if (err)
2986 goto done;
2988 mok_arg.worktree = worktree;
2989 mok_arg.repo = repo;
2990 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2991 &mok_arg);
2992 if (err)
2993 goto done;
2995 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2996 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2997 done:
2998 if (fileindex)
2999 got_fileindex_free(fileindex);
3000 free(fileindex_path);
3001 unlockerr = lock_worktree(worktree, LOCK_SH);
3002 if (unlockerr && err == NULL)
3003 err = unlockerr;
3004 return err;
3007 struct diff_dir_cb_arg {
3008 struct got_fileindex *fileindex;
3009 struct got_worktree *worktree;
3010 const char *status_path;
3011 size_t status_path_len;
3012 struct got_repository *repo;
3013 got_worktree_status_cb status_cb;
3014 void *status_arg;
3015 got_cancel_cb cancel_cb;
3016 void *cancel_arg;
3017 /* A pathlist containing per-directory pathlists of ignore patterns. */
3018 struct got_pathlist_head ignores;
3019 int report_unchanged;
3020 int no_ignores;
3023 static const struct got_error *
3024 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3025 int dirfd, const char *de_name,
3026 got_worktree_status_cb status_cb, void *status_arg,
3027 struct got_repository *repo, int report_unchanged)
3029 const struct got_error *err = NULL;
3030 unsigned char status = GOT_STATUS_NO_CHANGE;
3031 unsigned char staged_status = get_staged_status(ie);
3032 struct stat sb;
3033 struct got_object_id blob_id, commit_id, staged_blob_id;
3034 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3035 struct got_object_id *staged_blob_idp = NULL;
3037 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3038 if (err)
3039 return err;
3041 if (status == GOT_STATUS_NO_CHANGE &&
3042 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3043 return NULL;
3045 if (got_fileindex_entry_has_blob(ie)) {
3046 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3047 blob_idp = &blob_id;
3049 if (got_fileindex_entry_has_commit(ie)) {
3050 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3051 commit_idp = &commit_id;
3053 if (staged_status == GOT_STATUS_ADD ||
3054 staged_status == GOT_STATUS_MODIFY) {
3055 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3056 SHA1_DIGEST_LENGTH);
3057 staged_blob_idp = &staged_blob_id;
3060 return (*status_cb)(status_arg, status, staged_status,
3061 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3064 static const struct got_error *
3065 status_old_new(void *arg, struct got_fileindex_entry *ie,
3066 struct dirent *de, const char *parent_path, int dirfd)
3068 const struct got_error *err = NULL;
3069 struct diff_dir_cb_arg *a = arg;
3070 char *abspath;
3072 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3073 return got_error(GOT_ERR_CANCELLED);
3075 if (got_path_cmp(parent_path, a->status_path,
3076 strlen(parent_path), a->status_path_len) != 0 &&
3077 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3078 return NULL;
3080 if (parent_path[0]) {
3081 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3082 parent_path, de->d_name) == -1)
3083 return got_error_from_errno("asprintf");
3084 } else {
3085 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3086 de->d_name) == -1)
3087 return got_error_from_errno("asprintf");
3090 err = report_file_status(ie, abspath, dirfd, de->d_name,
3091 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3092 free(abspath);
3093 return err;
3096 static const struct got_error *
3097 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3099 struct diff_dir_cb_arg *a = arg;
3100 struct got_object_id blob_id, commit_id;
3101 unsigned char status;
3103 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3104 return got_error(GOT_ERR_CANCELLED);
3106 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3107 return NULL;
3109 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3110 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3111 if (got_fileindex_entry_has_file_on_disk(ie))
3112 status = GOT_STATUS_MISSING;
3113 else
3114 status = GOT_STATUS_DELETE;
3115 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3116 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3119 void
3120 free_ignorelist(struct got_pathlist_head *ignorelist)
3122 struct got_pathlist_entry *pe;
3124 TAILQ_FOREACH(pe, ignorelist, entry)
3125 free((char *)pe->path);
3126 got_pathlist_free(ignorelist);
3129 void
3130 free_ignores(struct got_pathlist_head *ignores)
3132 struct got_pathlist_entry *pe;
3134 TAILQ_FOREACH(pe, ignores, entry) {
3135 struct got_pathlist_head *ignorelist = pe->data;
3136 free_ignorelist(ignorelist);
3137 free((char *)pe->path);
3139 got_pathlist_free(ignores);
3142 static const struct got_error *
3143 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3145 const struct got_error *err = NULL;
3146 struct got_pathlist_entry *pe = NULL;
3147 struct got_pathlist_head *ignorelist;
3148 char *line = NULL, *pattern, *dirpath = NULL;
3149 size_t linesize = 0;
3150 ssize_t linelen;
3152 ignorelist = calloc(1, sizeof(*ignorelist));
3153 if (ignorelist == NULL)
3154 return got_error_from_errno("calloc");
3155 TAILQ_INIT(ignorelist);
3157 while ((linelen = getline(&line, &linesize, f)) != -1) {
3158 if (linelen > 0 && line[linelen - 1] == '\n')
3159 line[linelen - 1] = '\0';
3161 /* Git's ignores may contain comments. */
3162 if (line[0] == '#')
3163 continue;
3165 /* Git's negated patterns are not (yet?) supported. */
3166 if (line[0] == '!')
3167 continue;
3169 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3170 line) == -1) {
3171 err = got_error_from_errno("asprintf");
3172 goto done;
3174 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3175 if (err)
3176 goto done;
3178 if (ferror(f)) {
3179 err = got_error_from_errno("getline");
3180 goto done;
3183 dirpath = strdup(path);
3184 if (dirpath == NULL) {
3185 err = got_error_from_errno("strdup");
3186 goto done;
3188 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3189 done:
3190 free(line);
3191 if (err || pe == NULL) {
3192 free(dirpath);
3193 free_ignorelist(ignorelist);
3195 return err;
3198 int
3199 match_ignores(struct got_pathlist_head *ignores, const char *path)
3201 struct got_pathlist_entry *pe;
3203 /* Handle patterns which match in all directories. */
3204 TAILQ_FOREACH(pe, ignores, entry) {
3205 struct got_pathlist_head *ignorelist = pe->data;
3206 struct got_pathlist_entry *pi;
3208 TAILQ_FOREACH(pi, ignorelist, entry) {
3209 const char *p, *pattern = pi->path;
3211 if (strncmp(pattern, "**/", 3) != 0)
3212 continue;
3213 pattern += 3;
3214 p = path;
3215 while (*p) {
3216 if (fnmatch(pattern, p,
3217 FNM_PATHNAME | FNM_LEADING_DIR)) {
3218 /* Retry in next directory. */
3219 while (*p && *p != '/')
3220 p++;
3221 while (*p == '/')
3222 p++;
3223 continue;
3225 return 1;
3231 * The ignores pathlist contains ignore lists from children before
3232 * parents, so we can find the most specific ignorelist by walking
3233 * ignores backwards.
3235 pe = TAILQ_LAST(ignores, got_pathlist_head);
3236 while (pe) {
3237 if (got_path_is_child(path, pe->path, pe->path_len)) {
3238 struct got_pathlist_head *ignorelist = pe->data;
3239 struct got_pathlist_entry *pi;
3240 TAILQ_FOREACH(pi, ignorelist, entry) {
3241 const char *pattern = pi->path;
3242 int flags = FNM_LEADING_DIR;
3243 if (strstr(pattern, "/**/") == NULL)
3244 flags |= FNM_PATHNAME;
3245 if (fnmatch(pattern, path, flags))
3246 continue;
3247 return 1;
3250 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3253 return 0;
3256 static const struct got_error *
3257 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3258 const char *path, int dirfd, const char *ignores_filename)
3260 const struct got_error *err = NULL;
3261 char *ignorespath;
3262 int fd = -1;
3263 FILE *ignoresfile = NULL;
3265 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3266 path[0] ? "/" : "", ignores_filename) == -1)
3267 return got_error_from_errno("asprintf");
3269 if (dirfd != -1) {
3270 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3271 if (fd == -1) {
3272 if (errno != ENOENT && errno != EACCES)
3273 err = got_error_from_errno2("openat",
3274 ignorespath);
3275 } else {
3276 ignoresfile = fdopen(fd, "r");
3277 if (ignoresfile == NULL)
3278 err = got_error_from_errno2("fdopen",
3279 ignorespath);
3280 else {
3281 fd = -1;
3282 err = read_ignores(ignores, path, ignoresfile);
3285 } else {
3286 ignoresfile = fopen(ignorespath, "r");
3287 if (ignoresfile == NULL) {
3288 if (errno != ENOENT && errno != EACCES)
3289 err = got_error_from_errno2("fopen",
3290 ignorespath);
3291 } else
3292 err = read_ignores(ignores, path, ignoresfile);
3295 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3296 err = got_error_from_errno2("fclose", path);
3297 if (fd != -1 && close(fd) == -1 && err == NULL)
3298 err = got_error_from_errno2("close", path);
3299 free(ignorespath);
3300 return err;
3303 static const struct got_error *
3304 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3306 const struct got_error *err = NULL;
3307 struct diff_dir_cb_arg *a = arg;
3308 char *path = NULL;
3310 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3311 return got_error(GOT_ERR_CANCELLED);
3313 if (parent_path[0]) {
3314 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3315 return got_error_from_errno("asprintf");
3316 } else {
3317 path = de->d_name;
3320 if (de->d_type != DT_DIR &&
3321 got_path_is_child(path, a->status_path, a->status_path_len)
3322 && !match_ignores(&a->ignores, path))
3323 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3324 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3325 if (parent_path[0])
3326 free(path);
3327 return err;
3330 static const struct got_error *
3331 status_traverse(void *arg, const char *path, int dirfd)
3333 const struct got_error *err = NULL;
3334 struct diff_dir_cb_arg *a = arg;
3336 if (a->no_ignores)
3337 return NULL;
3339 err = add_ignores(&a->ignores, a->worktree->root_path,
3340 path, dirfd, ".cvsignore");
3341 if (err)
3342 return err;
3344 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3345 dirfd, ".gitignore");
3347 return err;
3350 static const struct got_error *
3351 report_single_file_status(const char *path, const char *ondisk_path,
3352 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3353 void *status_arg, struct got_repository *repo, int report_unchanged)
3355 struct got_fileindex_entry *ie;
3356 struct stat sb;
3358 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3359 if (ie)
3360 return report_file_status(ie, ondisk_path, -1, NULL,
3361 status_cb, status_arg, repo, report_unchanged);
3363 if (lstat(ondisk_path, &sb) == -1) {
3364 if (errno != ENOENT)
3365 return got_error_from_errno2("lstat", ondisk_path);
3366 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3367 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3368 return NULL;
3371 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3372 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3373 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3375 return NULL;
3378 static const struct got_error *
3379 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3380 const char *root_path, const char *path)
3382 const struct got_error *err;
3383 char *parent_path, *next_parent_path = NULL;
3385 err = add_ignores(ignores, root_path, "", -1,
3386 ".cvsignore");
3387 if (err)
3388 return err;
3390 err = add_ignores(ignores, root_path, "", -1,
3391 ".gitignore");
3392 if (err)
3393 return err;
3395 err = got_path_dirname(&parent_path, path);
3396 if (err) {
3397 if (err->code == GOT_ERR_BAD_PATH)
3398 return NULL; /* cannot traverse parent */
3399 return err;
3401 for (;;) {
3402 err = add_ignores(ignores, root_path, parent_path, -1,
3403 ".cvsignore");
3404 if (err)
3405 break;
3406 err = add_ignores(ignores, root_path, parent_path, -1,
3407 ".gitignore");
3408 if (err)
3409 break;
3410 err = got_path_dirname(&next_parent_path, parent_path);
3411 if (err) {
3412 if (err->code == GOT_ERR_BAD_PATH)
3413 err = NULL; /* traversed everything */
3414 break;
3416 free(parent_path);
3417 parent_path = next_parent_path;
3418 next_parent_path = NULL;
3421 free(parent_path);
3422 free(next_parent_path);
3423 return err;
3426 static const struct got_error *
3427 worktree_status(struct got_worktree *worktree, const char *path,
3428 struct got_fileindex *fileindex, struct got_repository *repo,
3429 got_worktree_status_cb status_cb, void *status_arg,
3430 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3431 int report_unchanged)
3433 const struct got_error *err = NULL;
3434 int fd = -1;
3435 struct got_fileindex_diff_dir_cb fdiff_cb;
3436 struct diff_dir_cb_arg arg;
3437 char *ondisk_path = NULL;
3439 TAILQ_INIT(&arg.ignores);
3441 if (asprintf(&ondisk_path, "%s%s%s",
3442 worktree->root_path, path[0] ? "/" : "", path) == -1)
3443 return got_error_from_errno("asprintf");
3445 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3446 if (fd == -1) {
3447 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3448 errno != ELOOP)
3449 err = got_error_from_errno2("open", ondisk_path);
3450 else
3451 err = report_single_file_status(path, ondisk_path,
3452 fileindex, status_cb, status_arg, repo,
3453 report_unchanged);
3454 } else {
3455 fdiff_cb.diff_old_new = status_old_new;
3456 fdiff_cb.diff_old = status_old;
3457 fdiff_cb.diff_new = status_new;
3458 fdiff_cb.diff_traverse = status_traverse;
3459 arg.fileindex = fileindex;
3460 arg.worktree = worktree;
3461 arg.status_path = path;
3462 arg.status_path_len = strlen(path);
3463 arg.repo = repo;
3464 arg.status_cb = status_cb;
3465 arg.status_arg = status_arg;
3466 arg.cancel_cb = cancel_cb;
3467 arg.cancel_arg = cancel_arg;
3468 arg.report_unchanged = report_unchanged;
3469 arg.no_ignores = no_ignores;
3470 if (!no_ignores) {
3471 err = add_ignores_from_parent_paths(&arg.ignores,
3472 worktree->root_path, path);
3473 if (err)
3474 goto done;
3476 err = got_fileindex_diff_dir(fileindex, fd,
3477 worktree->root_path, path, repo, &fdiff_cb, &arg);
3479 done:
3480 free_ignores(&arg.ignores);
3481 if (fd != -1 && close(fd) != 0 && err == NULL)
3482 err = got_error_from_errno("close");
3483 free(ondisk_path);
3484 return err;
3487 const struct got_error *
3488 got_worktree_status(struct got_worktree *worktree,
3489 struct got_pathlist_head *paths, struct got_repository *repo,
3490 got_worktree_status_cb status_cb, void *status_arg,
3491 got_cancel_cb cancel_cb, void *cancel_arg)
3493 const struct got_error *err = NULL;
3494 char *fileindex_path = NULL;
3495 struct got_fileindex *fileindex = NULL;
3496 struct got_pathlist_entry *pe;
3498 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3499 if (err)
3500 return err;
3502 TAILQ_FOREACH(pe, paths, entry) {
3503 err = worktree_status(worktree, pe->path, fileindex, repo,
3504 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3505 if (err)
3506 break;
3508 free(fileindex_path);
3509 got_fileindex_free(fileindex);
3510 return err;
3513 const struct got_error *
3514 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3515 const char *arg)
3517 const struct got_error *err = NULL;
3518 char *resolved = NULL, *cwd = NULL, *path = NULL;
3519 size_t len;
3520 struct stat sb;
3522 *wt_path = NULL;
3524 cwd = getcwd(NULL, 0);
3525 if (cwd == NULL)
3526 return got_error_from_errno("getcwd");
3528 if (lstat(arg, &sb) == -1) {
3529 if (errno != ENOENT) {
3530 err = got_error_from_errno2("lstat", arg);
3531 goto done;
3534 if (S_ISLNK(sb.st_mode)) {
3536 * We cannot use realpath(3) with symlinks since we want to
3537 * operate on the symlink itself.
3538 * But we can make the path absolute, assuming it is relative
3539 * to the current working directory, and then canonicalize it.
3541 char *abspath = NULL;
3542 char canonpath[PATH_MAX];
3543 if (!got_path_is_absolute(arg)) {
3544 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3545 err = got_error_from_errno("asprintf");
3546 goto done;
3550 err = got_canonpath(abspath ? abspath : arg, canonpath,
3551 sizeof(canonpath));
3552 if (err)
3553 goto done;
3554 resolved = strdup(canonpath);
3555 if (resolved == NULL) {
3556 err = got_error_from_errno("strdup");
3557 goto done;
3559 } else {
3560 resolved = realpath(arg, NULL);
3561 if (resolved == NULL) {
3562 if (errno != ENOENT) {
3563 err = got_error_from_errno2("realpath", arg);
3564 goto done;
3566 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3567 err = got_error_from_errno("asprintf");
3568 goto done;
3573 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3574 strlen(got_worktree_get_root_path(worktree)))) {
3575 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3576 goto done;
3579 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3580 err = got_path_skip_common_ancestor(&path,
3581 got_worktree_get_root_path(worktree), resolved);
3582 if (err)
3583 goto done;
3584 } else {
3585 path = strdup("");
3586 if (path == NULL) {
3587 err = got_error_from_errno("strdup");
3588 goto done;
3592 /* XXX status walk can't deal with trailing slash! */
3593 len = strlen(path);
3594 while (len > 0 && path[len - 1] == '/') {
3595 path[len - 1] = '\0';
3596 len--;
3598 done:
3599 free(resolved);
3600 free(cwd);
3601 if (err == NULL)
3602 *wt_path = path;
3603 else
3604 free(path);
3605 return err;
3608 struct schedule_addition_args {
3609 struct got_worktree *worktree;
3610 struct got_fileindex *fileindex;
3611 got_worktree_checkout_cb progress_cb;
3612 void *progress_arg;
3613 struct got_repository *repo;
3616 static const struct got_error *
3617 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3618 const char *relpath, struct got_object_id *blob_id,
3619 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3620 int dirfd, const char *de_name)
3622 struct schedule_addition_args *a = arg;
3623 const struct got_error *err = NULL;
3624 struct got_fileindex_entry *ie;
3625 struct stat sb;
3626 char *ondisk_path;
3628 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3629 relpath) == -1)
3630 return got_error_from_errno("asprintf");
3632 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3633 if (ie) {
3634 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3635 de_name, a->repo);
3636 if (err)
3637 goto done;
3638 /* Re-adding an existing entry is a no-op. */
3639 if (status == GOT_STATUS_ADD)
3640 goto done;
3641 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3642 if (err)
3643 goto done;
3646 if (status != GOT_STATUS_UNVERSIONED) {
3647 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3648 goto done;
3651 err = got_fileindex_entry_alloc(&ie, relpath);
3652 if (err)
3653 goto done;
3654 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3655 if (err) {
3656 got_fileindex_entry_free(ie);
3657 goto done;
3659 err = got_fileindex_entry_add(a->fileindex, ie);
3660 if (err) {
3661 got_fileindex_entry_free(ie);
3662 goto done;
3664 done:
3665 free(ondisk_path);
3666 if (err)
3667 return err;
3668 if (status == GOT_STATUS_ADD)
3669 return NULL;
3670 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3673 const struct got_error *
3674 got_worktree_schedule_add(struct got_worktree *worktree,
3675 struct got_pathlist_head *paths,
3676 got_worktree_checkout_cb progress_cb, void *progress_arg,
3677 struct got_repository *repo, int no_ignores)
3679 struct got_fileindex *fileindex = NULL;
3680 char *fileindex_path = NULL;
3681 const struct got_error *err = NULL, *sync_err, *unlockerr;
3682 struct got_pathlist_entry *pe;
3683 struct schedule_addition_args saa;
3685 err = lock_worktree(worktree, LOCK_EX);
3686 if (err)
3687 return err;
3689 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3690 if (err)
3691 goto done;
3693 saa.worktree = worktree;
3694 saa.fileindex = fileindex;
3695 saa.progress_cb = progress_cb;
3696 saa.progress_arg = progress_arg;
3697 saa.repo = repo;
3699 TAILQ_FOREACH(pe, paths, entry) {
3700 err = worktree_status(worktree, pe->path, fileindex, repo,
3701 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3702 if (err)
3703 break;
3705 sync_err = sync_fileindex(fileindex, fileindex_path);
3706 if (sync_err && err == NULL)
3707 err = sync_err;
3708 done:
3709 free(fileindex_path);
3710 if (fileindex)
3711 got_fileindex_free(fileindex);
3712 unlockerr = lock_worktree(worktree, LOCK_SH);
3713 if (unlockerr && err == NULL)
3714 err = unlockerr;
3715 return err;
3718 struct schedule_deletion_args {
3719 struct got_worktree *worktree;
3720 struct got_fileindex *fileindex;
3721 got_worktree_delete_cb progress_cb;
3722 void *progress_arg;
3723 struct got_repository *repo;
3724 int delete_local_mods;
3725 int keep_on_disk;
3728 static const struct got_error *
3729 schedule_for_deletion(void *arg, unsigned char status,
3730 unsigned char staged_status, const char *relpath,
3731 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3732 struct got_object_id *commit_id, int dirfd, const char *de_name)
3734 struct schedule_deletion_args *a = arg;
3735 const struct got_error *err = NULL;
3736 struct got_fileindex_entry *ie = NULL;
3737 struct stat sb;
3738 char *ondisk_path, *parent = NULL;
3740 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3741 if (ie == NULL)
3742 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3744 staged_status = get_staged_status(ie);
3745 if (staged_status != GOT_STATUS_NO_CHANGE) {
3746 if (staged_status == GOT_STATUS_DELETE)
3747 return NULL;
3748 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3751 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3752 relpath) == -1)
3753 return got_error_from_errno("asprintf");
3755 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3756 a->repo);
3757 if (err)
3758 goto done;
3760 if (status != GOT_STATUS_NO_CHANGE) {
3761 if (status == GOT_STATUS_DELETE)
3762 goto done;
3763 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3764 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3765 goto done;
3767 if (status != GOT_STATUS_MODIFY &&
3768 status != GOT_STATUS_MISSING) {
3769 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3770 goto done;
3774 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3775 if (dirfd != -1) {
3776 if (unlinkat(dirfd, de_name, 0) != 0) {
3777 err = got_error_from_errno2("unlinkat",
3778 ondisk_path);
3779 goto done;
3781 } else if (unlink(ondisk_path) != 0) {
3782 err = got_error_from_errno2("unlink", ondisk_path);
3783 goto done;
3786 parent = dirname(ondisk_path);
3788 if (parent == NULL) {
3789 err = got_error_from_errno2("dirname", ondisk_path);
3790 goto done;
3792 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3793 if (rmdir(parent) == -1) {
3794 if (errno != ENOTEMPTY)
3795 err = got_error_from_errno2("rmdir",
3796 parent);
3797 break;
3799 parent = dirname(parent);
3800 if (parent == NULL) {
3801 err = got_error_from_errno2("dirname", parent);
3802 goto done;
3807 got_fileindex_entry_mark_deleted_from_disk(ie);
3808 done:
3809 free(ondisk_path);
3810 if (err)
3811 return err;
3812 if (status == GOT_STATUS_DELETE)
3813 return NULL;
3814 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3815 staged_status, relpath);
3818 const struct got_error *
3819 got_worktree_schedule_delete(struct got_worktree *worktree,
3820 struct got_pathlist_head *paths, int delete_local_mods,
3821 got_worktree_delete_cb progress_cb, void *progress_arg,
3822 struct got_repository *repo, int keep_on_disk)
3824 struct got_fileindex *fileindex = NULL;
3825 char *fileindex_path = NULL;
3826 const struct got_error *err = NULL, *sync_err, *unlockerr;
3827 struct got_pathlist_entry *pe;
3828 struct schedule_deletion_args sda;
3830 err = lock_worktree(worktree, LOCK_EX);
3831 if (err)
3832 return err;
3834 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3835 if (err)
3836 goto done;
3838 sda.worktree = worktree;
3839 sda.fileindex = fileindex;
3840 sda.progress_cb = progress_cb;
3841 sda.progress_arg = progress_arg;
3842 sda.repo = repo;
3843 sda.delete_local_mods = delete_local_mods;
3844 sda.keep_on_disk = keep_on_disk;
3846 TAILQ_FOREACH(pe, paths, entry) {
3847 err = worktree_status(worktree, pe->path, fileindex, repo,
3848 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3849 if (err)
3850 break;
3852 sync_err = sync_fileindex(fileindex, fileindex_path);
3853 if (sync_err && err == NULL)
3854 err = sync_err;
3855 done:
3856 free(fileindex_path);
3857 if (fileindex)
3858 got_fileindex_free(fileindex);
3859 unlockerr = lock_worktree(worktree, LOCK_SH);
3860 if (unlockerr && err == NULL)
3861 err = unlockerr;
3862 return err;
3865 static const struct got_error *
3866 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3868 const struct got_error *err = NULL;
3869 char *line = NULL;
3870 size_t linesize = 0, n;
3871 ssize_t linelen;
3873 linelen = getline(&line, &linesize, infile);
3874 if (linelen == -1) {
3875 if (ferror(infile)) {
3876 err = got_error_from_errno("getline");
3877 goto done;
3879 return NULL;
3881 if (outfile) {
3882 n = fwrite(line, 1, linelen, outfile);
3883 if (n != linelen) {
3884 err = got_ferror(outfile, GOT_ERR_IO);
3885 goto done;
3888 if (rejectfile) {
3889 n = fwrite(line, 1, linelen, rejectfile);
3890 if (n != linelen)
3891 err = got_ferror(outfile, GOT_ERR_IO);
3893 done:
3894 free(line);
3895 return err;
3898 static const struct got_error *
3899 skip_one_line(FILE *f)
3901 char *line = NULL;
3902 size_t linesize = 0;
3903 ssize_t linelen;
3905 linelen = getline(&line, &linesize, f);
3906 if (linelen == -1) {
3907 if (ferror(f))
3908 return got_error_from_errno("getline");
3909 return NULL;
3911 free(line);
3912 return NULL;
3915 static const struct got_error *
3916 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3917 int start_old, int end_old, int start_new, int end_new,
3918 FILE *outfile, FILE *rejectfile)
3920 const struct got_error *err;
3922 /* Copy old file's lines leading up to patch. */
3923 while (!feof(f1) && *line_cur1 < start_old) {
3924 err = copy_one_line(f1, outfile, NULL);
3925 if (err)
3926 return err;
3927 (*line_cur1)++;
3929 /* Skip new file's lines leading up to patch. */
3930 while (!feof(f2) && *line_cur2 < start_new) {
3931 if (rejectfile)
3932 err = copy_one_line(f2, NULL, rejectfile);
3933 else
3934 err = skip_one_line(f2);
3935 if (err)
3936 return err;
3937 (*line_cur2)++;
3939 /* Copy patched lines. */
3940 while (!feof(f2) && *line_cur2 <= end_new) {
3941 err = copy_one_line(f2, outfile, NULL);
3942 if (err)
3943 return err;
3944 (*line_cur2)++;
3946 /* Skip over old file's replaced lines. */
3947 while (!feof(f1) && *line_cur1 <= end_old) {
3948 if (rejectfile)
3949 err = copy_one_line(f1, NULL, rejectfile);
3950 else
3951 err = skip_one_line(f1);
3952 if (err)
3953 return err;
3954 (*line_cur1)++;
3957 return NULL;
3960 static const struct got_error *
3961 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3962 FILE *outfile, FILE *rejectfile)
3964 const struct got_error *err;
3966 if (outfile) {
3967 /* Copy old file's lines until EOF. */
3968 while (!feof(f1)) {
3969 err = copy_one_line(f1, outfile, NULL);
3970 if (err)
3971 return err;
3972 (*line_cur1)++;
3975 if (rejectfile) {
3976 /* Copy new file's lines until EOF. */
3977 while (!feof(f2)) {
3978 err = copy_one_line(f2, NULL, rejectfile);
3979 if (err)
3980 return err;
3981 (*line_cur2)++;
3985 return NULL;
3988 static const struct got_error *
3989 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3990 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3991 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3992 int *line_cur2, FILE *outfile, FILE *rejectfile,
3993 got_worktree_patch_cb patch_cb, void *patch_arg)
3995 const struct got_error *err = NULL;
3996 int start_old = change->cv.a;
3997 int end_old = change->cv.b;
3998 int start_new = change->cv.c;
3999 int end_new = change->cv.d;
4000 long pos1, pos2;
4001 FILE *hunkfile;
4003 *choice = GOT_PATCH_CHOICE_NONE;
4005 hunkfile = got_opentemp();
4006 if (hunkfile == NULL)
4007 return got_error_from_errno("got_opentemp");
4009 pos1 = ftell(f1);
4010 pos2 = ftell(f2);
4012 /* XXX TODO needs error checking */
4013 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4015 if (fseek(f1, pos1, SEEK_SET) == -1) {
4016 err = got_ferror(f1, GOT_ERR_IO);
4017 goto done;
4019 if (fseek(f2, pos2, SEEK_SET) == -1) {
4020 err = got_ferror(f1, GOT_ERR_IO);
4021 goto done;
4023 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4024 err = got_ferror(hunkfile, GOT_ERR_IO);
4025 goto done;
4028 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4029 hunkfile, n, nchanges);
4030 if (err)
4031 goto done;
4033 switch (*choice) {
4034 case GOT_PATCH_CHOICE_YES:
4035 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4036 end_old, start_new, end_new, outfile, rejectfile);
4037 break;
4038 case GOT_PATCH_CHOICE_NO:
4039 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4040 end_old, start_new, end_new, rejectfile, outfile);
4041 break;
4042 case GOT_PATCH_CHOICE_QUIT:
4043 break;
4044 default:
4045 err = got_error(GOT_ERR_PATCH_CHOICE);
4046 break;
4048 done:
4049 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4050 err = got_error_from_errno("fclose");
4051 return err;
4054 struct revert_file_args {
4055 struct got_worktree *worktree;
4056 struct got_fileindex *fileindex;
4057 got_worktree_checkout_cb progress_cb;
4058 void *progress_arg;
4059 got_worktree_patch_cb patch_cb;
4060 void *patch_arg;
4061 struct got_repository *repo;
4064 static const struct got_error *
4065 create_patched_content(char **path_outfile, int reverse_patch,
4066 struct got_object_id *blob_id, const char *path2,
4067 int dirfd2, const char *de_name2,
4068 const char *relpath, struct got_repository *repo,
4069 got_worktree_patch_cb patch_cb, void *patch_arg)
4071 const struct got_error *err;
4072 struct got_blob_object *blob = NULL;
4073 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4074 int fd2 = -1;
4075 char *path1 = NULL, *id_str = NULL;
4076 struct stat sb1, sb2;
4077 struct got_diff_changes *changes = NULL;
4078 struct got_diff_state *ds = NULL;
4079 struct got_diff_args *args = NULL;
4080 struct got_diff_change *change;
4081 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4082 int n = 0;
4084 *path_outfile = NULL;
4086 err = got_object_id_str(&id_str, blob_id);
4087 if (err)
4088 return err;
4090 if (dirfd2 != -1) {
4091 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4092 if (fd2 == -1) {
4093 err = got_error_from_errno2("openat", path2);
4094 goto done;
4096 } else {
4097 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4098 if (fd2 == -1) {
4099 err = got_error_from_errno2("open", path2);
4100 goto done;
4103 if (fstat(fd2, &sb2) == -1) {
4104 err = got_error_from_errno2("fstat", path2);
4105 goto done;
4108 f2 = fdopen(fd2, "r");
4109 if (f2 == NULL) {
4110 err = got_error_from_errno2("fdopen", path2);
4111 goto done;
4113 fd2 = -1;
4115 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4116 if (err)
4117 goto done;
4119 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4120 if (err)
4121 goto done;
4123 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4124 if (err)
4125 goto done;
4127 if (stat(path1, &sb1) == -1) {
4128 err = got_error_from_errno2("stat", path1);
4129 goto done;
4132 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4133 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4134 if (err)
4135 goto done;
4137 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4138 if (err)
4139 goto done;
4141 if (fseek(f1, 0L, SEEK_SET) == -1)
4142 return got_ferror(f1, GOT_ERR_IO);
4143 if (fseek(f2, 0L, SEEK_SET) == -1)
4144 return got_ferror(f2, GOT_ERR_IO);
4145 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4146 int choice;
4147 err = apply_or_reject_change(&choice, change, ++n,
4148 changes->nchanges, ds, args, diff_flags, relpath,
4149 f1, f2, &line_cur1, &line_cur2,
4150 reverse_patch ? NULL : outfile,
4151 reverse_patch ? outfile : NULL,
4152 patch_cb, patch_arg);
4153 if (err)
4154 goto done;
4155 if (choice == GOT_PATCH_CHOICE_YES)
4156 have_content = 1;
4157 else if (choice == GOT_PATCH_CHOICE_QUIT)
4158 break;
4160 if (have_content) {
4161 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4162 reverse_patch ? NULL : outfile,
4163 reverse_patch ? outfile : NULL);
4164 if (err)
4165 goto done;
4167 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4168 err = got_error_from_errno2("chmod", path2);
4169 goto done;
4172 done:
4173 free(id_str);
4174 if (blob)
4175 got_object_blob_close(blob);
4176 if (f1 && fclose(f1) == EOF && err == NULL)
4177 err = got_error_from_errno2("fclose", path1);
4178 if (f2 && fclose(f2) == EOF && err == NULL)
4179 err = got_error_from_errno2("fclose", path2);
4180 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4181 err = got_error_from_errno2("close", path2);
4182 if (outfile && fclose(outfile) == EOF && err == NULL)
4183 err = got_error_from_errno2("fclose", *path_outfile);
4184 if (path1 && unlink(path1) == -1 && err == NULL)
4185 err = got_error_from_errno2("unlink", path1);
4186 if (err || !have_content) {
4187 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4188 err = got_error_from_errno2("unlink", *path_outfile);
4189 free(*path_outfile);
4190 *path_outfile = NULL;
4192 free(args);
4193 if (ds) {
4194 got_diff_state_free(ds);
4195 free(ds);
4197 if (changes)
4198 got_diff_free_changes(changes);
4199 free(path1);
4200 return err;
4203 static const struct got_error *
4204 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4205 const char *relpath, struct got_object_id *blob_id,
4206 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4207 int dirfd, const char *de_name)
4209 struct revert_file_args *a = arg;
4210 const struct got_error *err = NULL;
4211 char *parent_path = NULL;
4212 struct got_fileindex_entry *ie;
4213 struct got_tree_object *tree = NULL;
4214 struct got_object_id *tree_id = NULL;
4215 const struct got_tree_entry *te = NULL;
4216 char *tree_path = NULL, *te_name;
4217 char *ondisk_path = NULL, *path_content = NULL;
4218 struct got_blob_object *blob = NULL;
4220 /* Reverting a staged deletion is a no-op. */
4221 if (status == GOT_STATUS_DELETE &&
4222 staged_status != GOT_STATUS_NO_CHANGE)
4223 return NULL;
4225 if (status == GOT_STATUS_UNVERSIONED)
4226 return (*a->progress_cb)(a->progress_arg,
4227 GOT_STATUS_UNVERSIONED, relpath);
4229 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4230 if (ie == NULL)
4231 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4233 /* Construct in-repository path of tree which contains this blob. */
4234 err = got_path_dirname(&parent_path, ie->path);
4235 if (err) {
4236 if (err->code != GOT_ERR_BAD_PATH)
4237 goto done;
4238 parent_path = strdup("/");
4239 if (parent_path == NULL) {
4240 err = got_error_from_errno("strdup");
4241 goto done;
4244 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4245 tree_path = strdup(parent_path);
4246 if (tree_path == NULL) {
4247 err = got_error_from_errno("strdup");
4248 goto done;
4250 } else {
4251 if (got_path_is_root_dir(parent_path)) {
4252 tree_path = strdup(a->worktree->path_prefix);
4253 if (tree_path == NULL) {
4254 err = got_error_from_errno("strdup");
4255 goto done;
4257 } else {
4258 if (asprintf(&tree_path, "%s/%s",
4259 a->worktree->path_prefix, parent_path) == -1) {
4260 err = got_error_from_errno("asprintf");
4261 goto done;
4266 err = got_object_id_by_path(&tree_id, a->repo,
4267 a->worktree->base_commit_id, tree_path);
4268 if (err) {
4269 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4270 (status == GOT_STATUS_ADD ||
4271 staged_status == GOT_STATUS_ADD)))
4272 goto done;
4273 } else {
4274 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4275 if (err)
4276 goto done;
4278 te_name = basename(ie->path);
4279 if (te_name == NULL) {
4280 err = got_error_from_errno2("basename", ie->path);
4281 goto done;
4284 te = got_object_tree_find_entry(tree, te_name);
4285 if (te == NULL && status != GOT_STATUS_ADD &&
4286 staged_status != GOT_STATUS_ADD) {
4287 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4288 goto done;
4292 switch (status) {
4293 case GOT_STATUS_ADD:
4294 if (a->patch_cb) {
4295 int choice = GOT_PATCH_CHOICE_NONE;
4296 err = (*a->patch_cb)(&choice, a->patch_arg,
4297 status, ie->path, NULL, 1, 1);
4298 if (err)
4299 goto done;
4300 if (choice != GOT_PATCH_CHOICE_YES)
4301 break;
4303 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4304 ie->path);
4305 if (err)
4306 goto done;
4307 got_fileindex_entry_remove(a->fileindex, ie);
4308 break;
4309 case GOT_STATUS_DELETE:
4310 if (a->patch_cb) {
4311 int choice = GOT_PATCH_CHOICE_NONE;
4312 err = (*a->patch_cb)(&choice, a->patch_arg,
4313 status, ie->path, NULL, 1, 1);
4314 if (err)
4315 goto done;
4316 if (choice != GOT_PATCH_CHOICE_YES)
4317 break;
4319 /* fall through */
4320 case GOT_STATUS_MODIFY:
4321 case GOT_STATUS_MODE_CHANGE:
4322 case GOT_STATUS_CONFLICT:
4323 case GOT_STATUS_MISSING: {
4324 struct got_object_id id;
4325 if (staged_status == GOT_STATUS_ADD ||
4326 staged_status == GOT_STATUS_MODIFY) {
4327 memcpy(id.sha1, ie->staged_blob_sha1,
4328 SHA1_DIGEST_LENGTH);
4329 } else
4330 memcpy(id.sha1, ie->blob_sha1,
4331 SHA1_DIGEST_LENGTH);
4332 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4333 if (err)
4334 goto done;
4336 if (asprintf(&ondisk_path, "%s/%s",
4337 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4338 err = got_error_from_errno("asprintf");
4339 goto done;
4342 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4343 status == GOT_STATUS_CONFLICT)) {
4344 err = create_patched_content(&path_content, 1, &id,
4345 ondisk_path, dirfd, de_name, ie->path, a->repo,
4346 a->patch_cb, a->patch_arg);
4347 if (err || path_content == NULL)
4348 break;
4349 if (rename(path_content, ondisk_path) == -1) {
4350 err = got_error_from_errno3("rename",
4351 path_content, ondisk_path);
4352 goto done;
4354 } else {
4355 int is_bad_symlink = 0;
4356 if (te && S_ISLNK(te->mode)) {
4357 err = install_symlink(&is_bad_symlink,
4358 a->worktree, ondisk_path, ie->path,
4359 blob, 0, 1, 0, a->repo,
4360 a->progress_cb, a->progress_arg);
4361 } else {
4362 err = install_blob(a->worktree, ondisk_path,
4363 ie->path,
4364 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4365 got_fileindex_perms_to_st(ie), blob,
4366 0, 1, 0, 0, a->repo,
4367 a->progress_cb, a->progress_arg);
4369 if (err)
4370 goto done;
4371 if (status == GOT_STATUS_DELETE ||
4372 status == GOT_STATUS_MODE_CHANGE) {
4373 err = got_fileindex_entry_update(ie,
4374 ondisk_path, blob->id.sha1,
4375 a->worktree->base_commit_id->sha1, 1);
4376 if (err)
4377 goto done;
4379 if (is_bad_symlink) {
4380 got_fileindex_entry_filetype_set(ie,
4381 GOT_FILEIDX_MODE_BAD_SYMLINK);
4384 break;
4386 default:
4387 break;
4389 done:
4390 free(ondisk_path);
4391 free(path_content);
4392 free(parent_path);
4393 free(tree_path);
4394 if (blob)
4395 got_object_blob_close(blob);
4396 if (tree)
4397 got_object_tree_close(tree);
4398 free(tree_id);
4399 return err;
4402 const struct got_error *
4403 got_worktree_revert(struct got_worktree *worktree,
4404 struct got_pathlist_head *paths,
4405 got_worktree_checkout_cb progress_cb, void *progress_arg,
4406 got_worktree_patch_cb patch_cb, void *patch_arg,
4407 struct got_repository *repo)
4409 struct got_fileindex *fileindex = NULL;
4410 char *fileindex_path = NULL;
4411 const struct got_error *err = NULL, *unlockerr = NULL;
4412 const struct got_error *sync_err = NULL;
4413 struct got_pathlist_entry *pe;
4414 struct revert_file_args rfa;
4416 err = lock_worktree(worktree, LOCK_EX);
4417 if (err)
4418 return err;
4420 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4421 if (err)
4422 goto done;
4424 rfa.worktree = worktree;
4425 rfa.fileindex = fileindex;
4426 rfa.progress_cb = progress_cb;
4427 rfa.progress_arg = progress_arg;
4428 rfa.patch_cb = patch_cb;
4429 rfa.patch_arg = patch_arg;
4430 rfa.repo = repo;
4431 TAILQ_FOREACH(pe, paths, entry) {
4432 err = worktree_status(worktree, pe->path, fileindex, repo,
4433 revert_file, &rfa, NULL, NULL, 0, 0);
4434 if (err)
4435 break;
4437 sync_err = sync_fileindex(fileindex, fileindex_path);
4438 if (sync_err && err == NULL)
4439 err = sync_err;
4440 done:
4441 free(fileindex_path);
4442 if (fileindex)
4443 got_fileindex_free(fileindex);
4444 unlockerr = lock_worktree(worktree, LOCK_SH);
4445 if (unlockerr && err == NULL)
4446 err = unlockerr;
4447 return err;
4450 static void
4451 free_commitable(struct got_commitable *ct)
4453 free(ct->path);
4454 free(ct->in_repo_path);
4455 free(ct->ondisk_path);
4456 free(ct->blob_id);
4457 free(ct->base_blob_id);
4458 free(ct->staged_blob_id);
4459 free(ct->base_commit_id);
4460 free(ct);
4463 struct collect_commitables_arg {
4464 struct got_pathlist_head *commitable_paths;
4465 struct got_repository *repo;
4466 struct got_worktree *worktree;
4467 int have_staged_files;
4470 static const struct got_error *
4471 collect_commitables(void *arg, unsigned char status,
4472 unsigned char staged_status, const char *relpath,
4473 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4474 struct got_object_id *commit_id, int dirfd, const char *de_name)
4476 struct collect_commitables_arg *a = arg;
4477 const struct got_error *err = NULL;
4478 struct got_commitable *ct = NULL;
4479 struct got_pathlist_entry *new = NULL;
4480 char *parent_path = NULL, *path = NULL;
4481 struct stat sb;
4483 if (a->have_staged_files) {
4484 if (staged_status != GOT_STATUS_MODIFY &&
4485 staged_status != GOT_STATUS_ADD &&
4486 staged_status != GOT_STATUS_DELETE)
4487 return NULL;
4488 } else {
4489 if (status == GOT_STATUS_CONFLICT)
4490 return got_error(GOT_ERR_COMMIT_CONFLICT);
4492 if (status != GOT_STATUS_MODIFY &&
4493 status != GOT_STATUS_MODE_CHANGE &&
4494 status != GOT_STATUS_ADD &&
4495 status != GOT_STATUS_DELETE)
4496 return NULL;
4499 if (asprintf(&path, "/%s", relpath) == -1) {
4500 err = got_error_from_errno("asprintf");
4501 goto done;
4503 if (strcmp(path, "/") == 0) {
4504 parent_path = strdup("");
4505 if (parent_path == NULL)
4506 return got_error_from_errno("strdup");
4507 } else {
4508 err = got_path_dirname(&parent_path, path);
4509 if (err)
4510 return err;
4513 ct = calloc(1, sizeof(*ct));
4514 if (ct == NULL) {
4515 err = got_error_from_errno("calloc");
4516 goto done;
4519 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4520 relpath) == -1) {
4521 err = got_error_from_errno("asprintf");
4522 goto done;
4524 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4525 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4526 } else {
4527 if (dirfd != -1) {
4528 if (fstatat(dirfd, de_name, &sb,
4529 AT_SYMLINK_NOFOLLOW) == -1) {
4530 err = got_error_from_errno2("fstatat",
4531 ct->ondisk_path);
4532 goto done;
4534 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4535 err = got_error_from_errno2("lstat", ct->ondisk_path);
4536 goto done;
4538 ct->mode = sb.st_mode;
4541 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4542 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4543 relpath) == -1) {
4544 err = got_error_from_errno("asprintf");
4545 goto done;
4548 ct->status = status;
4549 ct->staged_status = staged_status;
4550 ct->blob_id = NULL; /* will be filled in when blob gets created */
4551 if (ct->status != GOT_STATUS_ADD &&
4552 ct->staged_status != GOT_STATUS_ADD) {
4553 ct->base_blob_id = got_object_id_dup(blob_id);
4554 if (ct->base_blob_id == NULL) {
4555 err = got_error_from_errno("got_object_id_dup");
4556 goto done;
4558 ct->base_commit_id = got_object_id_dup(commit_id);
4559 if (ct->base_commit_id == NULL) {
4560 err = got_error_from_errno("got_object_id_dup");
4561 goto done;
4564 if (ct->staged_status == GOT_STATUS_ADD ||
4565 ct->staged_status == GOT_STATUS_MODIFY) {
4566 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4567 if (ct->staged_blob_id == NULL) {
4568 err = got_error_from_errno("got_object_id_dup");
4569 goto done;
4572 ct->path = strdup(path);
4573 if (ct->path == NULL) {
4574 err = got_error_from_errno("strdup");
4575 goto done;
4577 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4578 done:
4579 if (ct && (err || new == NULL))
4580 free_commitable(ct);
4581 free(parent_path);
4582 free(path);
4583 return err;
4586 static const struct got_error *write_tree(struct got_object_id **, int *,
4587 struct got_tree_object *, const char *, struct got_pathlist_head *,
4588 got_worktree_status_cb status_cb, void *status_arg,
4589 struct got_repository *);
4591 static const struct got_error *
4592 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4593 struct got_tree_entry *te, const char *parent_path,
4594 struct got_pathlist_head *commitable_paths,
4595 got_worktree_status_cb status_cb, void *status_arg,
4596 struct got_repository *repo)
4598 const struct got_error *err = NULL;
4599 struct got_tree_object *subtree;
4600 char *subpath;
4602 if (asprintf(&subpath, "%s%s%s", parent_path,
4603 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4604 return got_error_from_errno("asprintf");
4606 err = got_object_open_as_tree(&subtree, repo, &te->id);
4607 if (err)
4608 return err;
4610 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4611 commitable_paths, status_cb, status_arg, repo);
4612 got_object_tree_close(subtree);
4613 free(subpath);
4614 return err;
4617 static const struct got_error *
4618 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4620 const struct got_error *err = NULL;
4621 char *ct_parent_path = NULL;
4623 *match = 0;
4625 if (strchr(ct->in_repo_path, '/') == NULL) {
4626 *match = got_path_is_root_dir(path);
4627 return NULL;
4630 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4631 if (err)
4632 return err;
4633 *match = (strcmp(path, ct_parent_path) == 0);
4634 free(ct_parent_path);
4635 return err;
4638 static mode_t
4639 get_ct_file_mode(struct got_commitable *ct)
4641 if (S_ISLNK(ct->mode))
4642 return S_IFLNK;
4644 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4647 static const struct got_error *
4648 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4649 struct got_tree_entry *te, struct got_commitable *ct)
4651 const struct got_error *err = NULL;
4653 *new_te = NULL;
4655 err = got_object_tree_entry_dup(new_te, te);
4656 if (err)
4657 goto done;
4659 (*new_te)->mode = get_ct_file_mode(ct);
4661 if (ct->staged_status == GOT_STATUS_MODIFY)
4662 memcpy(&(*new_te)->id, ct->staged_blob_id,
4663 sizeof((*new_te)->id));
4664 else
4665 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4666 done:
4667 if (err && *new_te) {
4668 free(*new_te);
4669 *new_te = NULL;
4671 return err;
4674 static const struct got_error *
4675 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4676 struct got_commitable *ct)
4678 const struct got_error *err = NULL;
4679 char *ct_name;
4681 *new_te = NULL;
4683 *new_te = calloc(1, sizeof(**new_te));
4684 if (*new_te == NULL)
4685 return got_error_from_errno("calloc");
4687 ct_name = basename(ct->path);
4688 if (ct_name == NULL) {
4689 err = got_error_from_errno2("basename", ct->path);
4690 goto done;
4692 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4693 sizeof((*new_te)->name)) {
4694 err = got_error(GOT_ERR_NO_SPACE);
4695 goto done;
4698 (*new_te)->mode = get_ct_file_mode(ct);
4700 if (ct->staged_status == GOT_STATUS_ADD)
4701 memcpy(&(*new_te)->id, ct->staged_blob_id,
4702 sizeof((*new_te)->id));
4703 else
4704 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4705 done:
4706 if (err && *new_te) {
4707 free(*new_te);
4708 *new_te = NULL;
4710 return err;
4713 static const struct got_error *
4714 insert_tree_entry(struct got_tree_entry *new_te,
4715 struct got_pathlist_head *paths)
4717 const struct got_error *err = NULL;
4718 struct got_pathlist_entry *new_pe;
4720 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4721 if (err)
4722 return err;
4723 if (new_pe == NULL)
4724 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4725 return NULL;
4728 static const struct got_error *
4729 report_ct_status(struct got_commitable *ct,
4730 got_worktree_status_cb status_cb, void *status_arg)
4732 const char *ct_path = ct->path;
4733 unsigned char status;
4735 while (ct_path[0] == '/')
4736 ct_path++;
4738 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4739 status = ct->staged_status;
4740 else
4741 status = ct->status;
4743 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4744 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4747 static const struct got_error *
4748 match_modified_subtree(int *modified, struct got_tree_entry *te,
4749 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4751 const struct got_error *err = NULL;
4752 struct got_pathlist_entry *pe;
4753 char *te_path;
4755 *modified = 0;
4757 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4758 got_path_is_root_dir(base_tree_path) ? "" : "/",
4759 te->name) == -1)
4760 return got_error_from_errno("asprintf");
4762 TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 struct got_commitable *ct = pe->data;
4764 *modified = got_path_is_child(ct->in_repo_path, te_path,
4765 strlen(te_path));
4766 if (*modified)
4767 break;
4770 free(te_path);
4771 return err;
4774 static const struct got_error *
4775 match_deleted_or_modified_ct(struct got_commitable **ctp,
4776 struct got_tree_entry *te, const char *base_tree_path,
4777 struct got_pathlist_head *commitable_paths)
4779 const struct got_error *err = NULL;
4780 struct got_pathlist_entry *pe;
4782 *ctp = NULL;
4784 TAILQ_FOREACH(pe, commitable_paths, entry) {
4785 struct got_commitable *ct = pe->data;
4786 char *ct_name = NULL;
4787 int path_matches;
4789 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4790 if (ct->status != GOT_STATUS_MODIFY &&
4791 ct->status != GOT_STATUS_MODE_CHANGE &&
4792 ct->status != GOT_STATUS_DELETE)
4793 continue;
4794 } else {
4795 if (ct->staged_status != GOT_STATUS_MODIFY &&
4796 ct->staged_status != GOT_STATUS_DELETE)
4797 continue;
4800 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4801 continue;
4803 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4804 if (err)
4805 return err;
4806 if (!path_matches)
4807 continue;
4809 ct_name = basename(pe->path);
4810 if (ct_name == NULL)
4811 return got_error_from_errno2("basename", pe->path);
4813 if (strcmp(te->name, ct_name) != 0)
4814 continue;
4816 *ctp = ct;
4817 break;
4820 return err;
4823 static const struct got_error *
4824 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4825 const char *child_path, const char *path_base_tree,
4826 struct got_pathlist_head *commitable_paths,
4827 got_worktree_status_cb status_cb, void *status_arg,
4828 struct got_repository *repo)
4830 const struct got_error *err = NULL;
4831 struct got_tree_entry *new_te;
4832 char *subtree_path;
4833 struct got_object_id *id = NULL;
4834 int nentries;
4836 *new_tep = NULL;
4838 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4839 got_path_is_root_dir(path_base_tree) ? "" : "/",
4840 child_path) == -1)
4841 return got_error_from_errno("asprintf");
4843 new_te = calloc(1, sizeof(*new_te));
4844 if (new_te == NULL)
4845 return got_error_from_errno("calloc");
4846 new_te->mode = S_IFDIR;
4848 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4849 sizeof(new_te->name)) {
4850 err = got_error(GOT_ERR_NO_SPACE);
4851 goto done;
4853 err = write_tree(&id, &nentries, NULL, subtree_path,
4854 commitable_paths, status_cb, status_arg, repo);
4855 if (err) {
4856 free(new_te);
4857 goto done;
4859 memcpy(&new_te->id, id, sizeof(new_te->id));
4860 done:
4861 free(id);
4862 free(subtree_path);
4863 if (err == NULL)
4864 *new_tep = new_te;
4865 return err;
4868 static const struct got_error *
4869 write_tree(struct got_object_id **new_tree_id, int *nentries,
4870 struct got_tree_object *base_tree, const char *path_base_tree,
4871 struct got_pathlist_head *commitable_paths,
4872 got_worktree_status_cb status_cb, void *status_arg,
4873 struct got_repository *repo)
4875 const struct got_error *err = NULL;
4876 struct got_pathlist_head paths;
4877 struct got_tree_entry *te, *new_te = NULL;
4878 struct got_pathlist_entry *pe;
4880 TAILQ_INIT(&paths);
4881 *nentries = 0;
4883 /* Insert, and recurse into, newly added entries first. */
4884 TAILQ_FOREACH(pe, commitable_paths, entry) {
4885 struct got_commitable *ct = pe->data;
4886 char *child_path = NULL, *slash;
4888 if ((ct->status != GOT_STATUS_ADD &&
4889 ct->staged_status != GOT_STATUS_ADD) ||
4890 (ct->flags & GOT_COMMITABLE_ADDED))
4891 continue;
4893 if (!got_path_is_child(pe->path, path_base_tree,
4894 strlen(path_base_tree)))
4895 continue;
4897 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4898 pe->path);
4899 if (err)
4900 goto done;
4902 slash = strchr(child_path, '/');
4903 if (slash == NULL) {
4904 err = alloc_added_blob_tree_entry(&new_te, ct);
4905 if (err)
4906 goto done;
4907 err = report_ct_status(ct, status_cb, status_arg);
4908 if (err)
4909 goto done;
4910 ct->flags |= GOT_COMMITABLE_ADDED;
4911 err = insert_tree_entry(new_te, &paths);
4912 if (err)
4913 goto done;
4914 (*nentries)++;
4915 } else {
4916 *slash = '\0'; /* trim trailing path components */
4917 if (base_tree == NULL ||
4918 got_object_tree_find_entry(base_tree, child_path)
4919 == NULL) {
4920 err = make_subtree_for_added_blob(&new_te,
4921 child_path, path_base_tree,
4922 commitable_paths, status_cb, status_arg,
4923 repo);
4924 if (err)
4925 goto done;
4926 err = insert_tree_entry(new_te, &paths);
4927 if (err)
4928 goto done;
4929 (*nentries)++;
4934 if (base_tree) {
4935 int i, nbase_entries;
4936 /* Handle modified and deleted entries. */
4937 nbase_entries = got_object_tree_get_nentries(base_tree);
4938 for (i = 0; i < nbase_entries; i++) {
4939 struct got_commitable *ct = NULL;
4941 te = got_object_tree_get_entry(base_tree, i);
4942 if (got_object_tree_entry_is_submodule(te)) {
4943 /* Entry is a submodule; just copy it. */
4944 err = got_object_tree_entry_dup(&new_te, te);
4945 if (err)
4946 goto done;
4947 err = insert_tree_entry(new_te, &paths);
4948 if (err)
4949 goto done;
4950 (*nentries)++;
4951 continue;
4954 if (S_ISDIR(te->mode)) {
4955 int modified;
4956 err = got_object_tree_entry_dup(&new_te, te);
4957 if (err)
4958 goto done;
4959 err = match_modified_subtree(&modified, te,
4960 path_base_tree, commitable_paths);
4961 if (err)
4962 goto done;
4963 /* Avoid recursion into unmodified subtrees. */
4964 if (modified) {
4965 struct got_object_id *new_id;
4966 int nsubentries;
4967 err = write_subtree(&new_id,
4968 &nsubentries, te,
4969 path_base_tree, commitable_paths,
4970 status_cb, status_arg, repo);
4971 if (err)
4972 goto done;
4973 if (nsubentries == 0) {
4974 /* All entries were deleted. */
4975 free(new_id);
4976 continue;
4978 memcpy(&new_te->id, new_id,
4979 sizeof(new_te->id));
4980 free(new_id);
4982 err = insert_tree_entry(new_te, &paths);
4983 if (err)
4984 goto done;
4985 (*nentries)++;
4986 continue;
4989 err = match_deleted_or_modified_ct(&ct, te,
4990 path_base_tree, commitable_paths);
4991 if (err)
4992 goto done;
4993 if (ct) {
4994 /* NB: Deleted entries get dropped here. */
4995 if (ct->status == GOT_STATUS_MODIFY ||
4996 ct->status == GOT_STATUS_MODE_CHANGE ||
4997 ct->staged_status == GOT_STATUS_MODIFY) {
4998 err = alloc_modified_blob_tree_entry(
4999 &new_te, te, ct);
5000 if (err)
5001 goto done;
5002 err = insert_tree_entry(new_te, &paths);
5003 if (err)
5004 goto done;
5005 (*nentries)++;
5007 err = report_ct_status(ct, status_cb,
5008 status_arg);
5009 if (err)
5010 goto done;
5011 } else {
5012 /* Entry is unchanged; just copy it. */
5013 err = got_object_tree_entry_dup(&new_te, te);
5014 if (err)
5015 goto done;
5016 err = insert_tree_entry(new_te, &paths);
5017 if (err)
5018 goto done;
5019 (*nentries)++;
5024 /* Write new list of entries; deleted entries have been dropped. */
5025 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5026 done:
5027 got_pathlist_free(&paths);
5028 return err;
5031 static const struct got_error *
5032 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5033 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5034 struct got_repository *repo)
5036 const struct got_error *err = NULL;
5037 struct got_blob_object *blob = NULL;
5038 struct got_object_id *tree_id = NULL;
5039 char *tree_path = NULL;
5040 struct got_tree_object *tree = NULL;
5041 struct got_tree_entry *te;
5042 char *entry_name;
5044 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
5045 if (err)
5046 return err;
5048 err = got_path_dirname(&tree_path, ct->in_repo_path);
5049 if (err) {
5050 if (err->code != GOT_ERR_BAD_PATH)
5051 goto done;
5052 err = got_object_id_by_path(&tree_id, repo,
5053 new_base_commit_id, "");
5054 if (err)
5055 goto done;
5056 } else {
5057 err = got_object_id_by_path(&tree_id, repo,
5058 new_base_commit_id, tree_path);
5059 if (err)
5060 goto done;
5063 err = got_object_open_as_tree(&tree, repo, tree_id);
5064 if (err)
5065 goto done;
5067 entry_name = basename(ct->path);
5068 if (entry_name == NULL) {
5069 err = got_error_from_errno2("basename", ct->path);
5070 goto done;
5073 te = got_object_tree_find_entry(tree, entry_name);
5074 if (te == NULL) {
5075 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5076 goto done;
5079 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5080 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5081 done:
5082 if (blob)
5083 got_object_blob_close(blob);
5084 if (tree)
5085 got_object_tree_close(tree);
5086 free(tree_id);
5087 free(tree_path);
5088 return err;
5092 * After comitting a symlink we have a chance to convert "bad" symlinks
5093 * (those which point outside the work tree or into .got) to regular files.
5094 * This way, the post-commit work tree state matches a fresh checkout of
5095 * the tree which was just committed. We also mark such newly committed
5096 * symlinks as "bad" in the work tree's fileindex.
5098 static const struct got_error *
5099 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5100 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5101 struct got_worktree *worktree, struct got_repository *repo)
5103 const struct got_error *err = NULL;
5104 struct got_pathlist_entry *pe;
5106 TAILQ_FOREACH(pe, commitable_paths, entry) {
5107 struct got_commitable *ct = pe->data;
5108 struct got_fileindex_entry *ie;
5109 int is_bad_symlink = 0;
5111 if (!S_ISLNK(get_ct_file_mode(ct)))
5112 continue;
5114 err = reinstall_symlink_after_commit(&is_bad_symlink,
5115 ct, new_base_commit_id, worktree, repo);
5116 if (err)
5117 break;
5118 ie = got_fileindex_entry_get(fileindex, ct->path,
5119 strlen(ct->path));
5120 if (ie && is_bad_symlink) {
5121 got_fileindex_entry_filetype_set(ie,
5122 GOT_FILEIDX_MODE_BAD_SYMLINK);
5126 return err;
5129 static const struct got_error *
5130 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5131 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5132 int have_staged_files)
5134 const struct got_error *err = NULL;
5135 struct got_pathlist_entry *pe;
5137 TAILQ_FOREACH(pe, commitable_paths, entry) {
5138 struct got_fileindex_entry *ie;
5139 struct got_commitable *ct = pe->data;
5141 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5142 if (ie) {
5143 if (ct->status == GOT_STATUS_DELETE ||
5144 ct->staged_status == GOT_STATUS_DELETE) {
5145 got_fileindex_entry_remove(fileindex, ie);
5146 } else if (ct->staged_status == GOT_STATUS_ADD ||
5147 ct->staged_status == GOT_STATUS_MODIFY) {
5148 got_fileindex_entry_stage_set(ie,
5149 GOT_FILEIDX_STAGE_NONE);
5150 err = got_fileindex_entry_update(ie,
5151 ct->ondisk_path, ct->staged_blob_id->sha1,
5152 new_base_commit_id->sha1,
5153 !have_staged_files);
5154 } else
5155 err = got_fileindex_entry_update(ie,
5156 ct->ondisk_path, ct->blob_id->sha1,
5157 new_base_commit_id->sha1,
5158 !have_staged_files);
5159 } else {
5160 err = got_fileindex_entry_alloc(&ie, pe->path);
5161 if (err)
5162 break;
5163 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5164 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5165 if (err) {
5166 got_fileindex_entry_free(ie);
5167 break;
5169 err = got_fileindex_entry_add(fileindex, ie);
5170 if (err) {
5171 got_fileindex_entry_free(ie);
5172 break;
5176 return err;
5180 static const struct got_error *
5181 check_out_of_date(const char *in_repo_path, unsigned char status,
5182 unsigned char staged_status, struct got_object_id *base_blob_id,
5183 struct got_object_id *base_commit_id,
5184 struct got_object_id *head_commit_id, struct got_repository *repo,
5185 int ood_errcode)
5187 const struct got_error *err = NULL;
5188 struct got_object_id *id = NULL;
5190 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5191 /* Trivial case: base commit == head commit */
5192 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5193 return NULL;
5195 * Ensure file content which local changes were based
5196 * on matches file content in the branch head.
5198 err = got_object_id_by_path(&id, repo, head_commit_id,
5199 in_repo_path);
5200 if (err) {
5201 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5202 err = got_error(ood_errcode);
5203 goto done;
5204 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5205 err = got_error(ood_errcode);
5206 } else {
5207 /* Require that added files don't exist in the branch head. */
5208 err = got_object_id_by_path(&id, repo, head_commit_id,
5209 in_repo_path);
5210 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5211 goto done;
5212 err = id ? got_error(ood_errcode) : NULL;
5214 done:
5215 free(id);
5216 return err;
5219 const struct got_error *
5220 commit_worktree(struct got_object_id **new_commit_id,
5221 struct got_pathlist_head *commitable_paths,
5222 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5223 const char *author, const char *committer,
5224 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5225 got_worktree_status_cb status_cb, void *status_arg,
5226 struct got_repository *repo)
5228 const struct got_error *err = NULL, *unlockerr = NULL;
5229 struct got_pathlist_entry *pe;
5230 const char *head_ref_name = NULL;
5231 struct got_commit_object *head_commit = NULL;
5232 struct got_reference *head_ref2 = NULL;
5233 struct got_object_id *head_commit_id2 = NULL;
5234 struct got_tree_object *head_tree = NULL;
5235 struct got_object_id *new_tree_id = NULL;
5236 int nentries;
5237 struct got_object_id_queue parent_ids;
5238 struct got_object_qid *pid = NULL;
5239 char *logmsg = NULL;
5241 *new_commit_id = NULL;
5243 SIMPLEQ_INIT(&parent_ids);
5245 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5246 if (err)
5247 goto done;
5249 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5250 if (err)
5251 goto done;
5253 if (commit_msg_cb != NULL) {
5254 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5255 if (err)
5256 goto done;
5259 if (logmsg == NULL || strlen(logmsg) == 0) {
5260 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5261 goto done;
5264 /* Create blobs from added and modified files and record their IDs. */
5265 TAILQ_FOREACH(pe, commitable_paths, entry) {
5266 struct got_commitable *ct = pe->data;
5267 char *ondisk_path;
5269 /* Blobs for staged files already exist. */
5270 if (ct->staged_status == GOT_STATUS_ADD ||
5271 ct->staged_status == GOT_STATUS_MODIFY)
5272 continue;
5274 if (ct->status != GOT_STATUS_ADD &&
5275 ct->status != GOT_STATUS_MODIFY &&
5276 ct->status != GOT_STATUS_MODE_CHANGE)
5277 continue;
5279 if (asprintf(&ondisk_path, "%s/%s",
5280 worktree->root_path, pe->path) == -1) {
5281 err = got_error_from_errno("asprintf");
5282 goto done;
5284 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5285 free(ondisk_path);
5286 if (err)
5287 goto done;
5290 /* Recursively write new tree objects. */
5291 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5292 commitable_paths, status_cb, status_arg, repo);
5293 if (err)
5294 goto done;
5296 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5297 if (err)
5298 goto done;
5299 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5300 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5301 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5302 got_object_qid_free(pid);
5303 if (logmsg != NULL)
5304 free(logmsg);
5305 if (err)
5306 goto done;
5308 /* Check if a concurrent commit to our branch has occurred. */
5309 head_ref_name = got_worktree_get_head_ref_name(worktree);
5310 if (head_ref_name == NULL) {
5311 err = got_error_from_errno("got_worktree_get_head_ref_name");
5312 goto done;
5314 /* Lock the reference here to prevent concurrent modification. */
5315 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5316 if (err)
5317 goto done;
5318 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5319 if (err)
5320 goto done;
5321 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5322 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5323 goto done;
5325 /* Update branch head in repository. */
5326 err = got_ref_change_ref(head_ref2, *new_commit_id);
5327 if (err)
5328 goto done;
5329 err = got_ref_write(head_ref2, repo);
5330 if (err)
5331 goto done;
5333 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5334 if (err)
5335 goto done;
5337 err = ref_base_commit(worktree, repo);
5338 if (err)
5339 goto done;
5340 done:
5341 if (head_tree)
5342 got_object_tree_close(head_tree);
5343 if (head_commit)
5344 got_object_commit_close(head_commit);
5345 free(head_commit_id2);
5346 if (head_ref2) {
5347 unlockerr = got_ref_unlock(head_ref2);
5348 if (unlockerr && err == NULL)
5349 err = unlockerr;
5350 got_ref_close(head_ref2);
5352 return err;
5355 static const struct got_error *
5356 check_path_is_commitable(const char *path,
5357 struct got_pathlist_head *commitable_paths)
5359 struct got_pathlist_entry *cpe = NULL;
5360 size_t path_len = strlen(path);
5362 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5363 struct got_commitable *ct = cpe->data;
5364 const char *ct_path = ct->path;
5366 while (ct_path[0] == '/')
5367 ct_path++;
5369 if (strcmp(path, ct_path) == 0 ||
5370 got_path_is_child(ct_path, path, path_len))
5371 break;
5374 if (cpe == NULL)
5375 return got_error_path(path, GOT_ERR_BAD_PATH);
5377 return NULL;
5380 static const struct got_error *
5381 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5383 int *have_staged_files = arg;
5385 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5386 *have_staged_files = 1;
5387 return got_error(GOT_ERR_CANCELLED);
5390 return NULL;
5393 static const struct got_error *
5394 check_non_staged_files(struct got_fileindex *fileindex,
5395 struct got_pathlist_head *paths)
5397 struct got_pathlist_entry *pe;
5398 struct got_fileindex_entry *ie;
5400 TAILQ_FOREACH(pe, paths, entry) {
5401 if (pe->path[0] == '\0')
5402 continue;
5403 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5404 if (ie == NULL)
5405 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5406 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5407 return got_error_path(pe->path,
5408 GOT_ERR_FILE_NOT_STAGED);
5411 return NULL;
5414 const struct got_error *
5415 got_worktree_commit(struct got_object_id **new_commit_id,
5416 struct got_worktree *worktree, struct got_pathlist_head *paths,
5417 const char *author, const char *committer,
5418 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5419 got_worktree_status_cb status_cb, void *status_arg,
5420 struct got_repository *repo)
5422 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5423 struct got_fileindex *fileindex = NULL;
5424 char *fileindex_path = NULL;
5425 struct got_pathlist_head commitable_paths;
5426 struct collect_commitables_arg cc_arg;
5427 struct got_pathlist_entry *pe;
5428 struct got_reference *head_ref = NULL;
5429 struct got_object_id *head_commit_id = NULL;
5430 int have_staged_files = 0;
5432 *new_commit_id = NULL;
5434 TAILQ_INIT(&commitable_paths);
5436 err = lock_worktree(worktree, LOCK_EX);
5437 if (err)
5438 goto done;
5440 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5441 if (err)
5442 goto done;
5444 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5445 if (err)
5446 goto done;
5448 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5449 if (err)
5450 goto done;
5452 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5453 &have_staged_files);
5454 if (err && err->code != GOT_ERR_CANCELLED)
5455 goto done;
5456 if (have_staged_files) {
5457 err = check_non_staged_files(fileindex, paths);
5458 if (err)
5459 goto done;
5462 cc_arg.commitable_paths = &commitable_paths;
5463 cc_arg.worktree = worktree;
5464 cc_arg.repo = repo;
5465 cc_arg.have_staged_files = have_staged_files;
5466 TAILQ_FOREACH(pe, paths, entry) {
5467 err = worktree_status(worktree, pe->path, fileindex, repo,
5468 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5469 if (err)
5470 goto done;
5473 if (TAILQ_EMPTY(&commitable_paths)) {
5474 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5475 goto done;
5478 TAILQ_FOREACH(pe, paths, entry) {
5479 err = check_path_is_commitable(pe->path, &commitable_paths);
5480 if (err)
5481 goto done;
5484 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5485 struct got_commitable *ct = pe->data;
5486 const char *ct_path = ct->in_repo_path;
5488 while (ct_path[0] == '/')
5489 ct_path++;
5490 err = check_out_of_date(ct_path, ct->status,
5491 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5492 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5493 if (err)
5494 goto done;
5498 err = commit_worktree(new_commit_id, &commitable_paths,
5499 head_commit_id, worktree, author, committer,
5500 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5501 if (err)
5502 goto done;
5504 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5505 fileindex, have_staged_files);
5506 if (err == NULL) {
5507 err = reinstall_symlinks_after_commit(&commitable_paths,
5508 *new_commit_id, fileindex, worktree, repo);
5510 sync_err = sync_fileindex(fileindex, fileindex_path);
5511 if (sync_err && err == NULL)
5512 err = sync_err;
5513 done:
5514 if (fileindex)
5515 got_fileindex_free(fileindex);
5516 free(fileindex_path);
5517 unlockerr = lock_worktree(worktree, LOCK_SH);
5518 if (unlockerr && err == NULL)
5519 err = unlockerr;
5520 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5521 struct got_commitable *ct = pe->data;
5522 free_commitable(ct);
5524 got_pathlist_free(&commitable_paths);
5525 return err;
5528 const char *
5529 got_commitable_get_path(struct got_commitable *ct)
5531 return ct->path;
5534 unsigned int
5535 got_commitable_get_status(struct got_commitable *ct)
5537 return ct->status;
5540 struct check_rebase_ok_arg {
5541 struct got_worktree *worktree;
5542 struct got_repository *repo;
5545 static const struct got_error *
5546 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5548 const struct got_error *err = NULL;
5549 struct check_rebase_ok_arg *a = arg;
5550 unsigned char status;
5551 struct stat sb;
5552 char *ondisk_path;
5554 /* Reject rebase of a work tree with mixed base commits. */
5555 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5556 SHA1_DIGEST_LENGTH))
5557 return got_error(GOT_ERR_MIXED_COMMITS);
5559 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5560 == -1)
5561 return got_error_from_errno("asprintf");
5563 /* Reject rebase of a work tree with modified or staged files. */
5564 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5565 free(ondisk_path);
5566 if (err)
5567 return err;
5569 if (status != GOT_STATUS_NO_CHANGE)
5570 return got_error(GOT_ERR_MODIFIED);
5571 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5572 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5574 return NULL;
5577 const struct got_error *
5578 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5579 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5580 struct got_worktree *worktree, struct got_reference *branch,
5581 struct got_repository *repo)
5583 const struct got_error *err = NULL;
5584 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5585 char *branch_ref_name = NULL;
5586 char *fileindex_path = NULL;
5587 struct check_rebase_ok_arg ok_arg;
5588 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5589 struct got_object_id *wt_branch_tip = NULL;
5591 *new_base_branch_ref = NULL;
5592 *tmp_branch = NULL;
5593 *fileindex = NULL;
5595 err = lock_worktree(worktree, LOCK_EX);
5596 if (err)
5597 return err;
5599 err = open_fileindex(fileindex, &fileindex_path, worktree);
5600 if (err)
5601 goto done;
5603 ok_arg.worktree = worktree;
5604 ok_arg.repo = repo;
5605 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5606 &ok_arg);
5607 if (err)
5608 goto done;
5610 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5611 if (err)
5612 goto done;
5614 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5615 if (err)
5616 goto done;
5618 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5619 if (err)
5620 goto done;
5622 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5623 0);
5624 if (err)
5625 goto done;
5627 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5628 if (err)
5629 goto done;
5630 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5631 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5632 goto done;
5635 err = got_ref_alloc_symref(new_base_branch_ref,
5636 new_base_branch_ref_name, wt_branch);
5637 if (err)
5638 goto done;
5639 err = got_ref_write(*new_base_branch_ref, repo);
5640 if (err)
5641 goto done;
5643 /* TODO Lock original branch's ref while rebasing? */
5645 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5646 if (err)
5647 goto done;
5649 err = got_ref_write(branch_ref, repo);
5650 if (err)
5651 goto done;
5653 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5654 worktree->base_commit_id);
5655 if (err)
5656 goto done;
5657 err = got_ref_write(*tmp_branch, repo);
5658 if (err)
5659 goto done;
5661 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5662 if (err)
5663 goto done;
5664 done:
5665 free(fileindex_path);
5666 free(tmp_branch_name);
5667 free(new_base_branch_ref_name);
5668 free(branch_ref_name);
5669 if (branch_ref)
5670 got_ref_close(branch_ref);
5671 if (wt_branch)
5672 got_ref_close(wt_branch);
5673 free(wt_branch_tip);
5674 if (err) {
5675 if (*new_base_branch_ref) {
5676 got_ref_close(*new_base_branch_ref);
5677 *new_base_branch_ref = NULL;
5679 if (*tmp_branch) {
5680 got_ref_close(*tmp_branch);
5681 *tmp_branch = NULL;
5683 if (*fileindex) {
5684 got_fileindex_free(*fileindex);
5685 *fileindex = NULL;
5687 lock_worktree(worktree, LOCK_SH);
5689 return err;
5692 const struct got_error *
5693 got_worktree_rebase_continue(struct got_object_id **commit_id,
5694 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5695 struct got_reference **branch, struct got_fileindex **fileindex,
5696 struct got_worktree *worktree, struct got_repository *repo)
5698 const struct got_error *err;
5699 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5700 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5701 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5702 char *fileindex_path = NULL;
5703 int have_staged_files = 0;
5705 *commit_id = NULL;
5706 *new_base_branch = NULL;
5707 *tmp_branch = NULL;
5708 *branch = NULL;
5709 *fileindex = NULL;
5711 err = lock_worktree(worktree, LOCK_EX);
5712 if (err)
5713 return err;
5715 err = open_fileindex(fileindex, &fileindex_path, worktree);
5716 if (err)
5717 goto done;
5719 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5720 &have_staged_files);
5721 if (err && err->code != GOT_ERR_CANCELLED)
5722 goto done;
5723 if (have_staged_files) {
5724 err = got_error(GOT_ERR_STAGED_PATHS);
5725 goto done;
5728 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5729 if (err)
5730 goto done;
5732 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5733 if (err)
5734 goto done;
5736 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5737 if (err)
5738 goto done;
5740 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5741 if (err)
5742 goto done;
5744 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5745 if (err)
5746 goto done;
5748 err = got_ref_open(branch, repo,
5749 got_ref_get_symref_target(branch_ref), 0);
5750 if (err)
5751 goto done;
5753 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5754 if (err)
5755 goto done;
5757 err = got_ref_resolve(commit_id, repo, commit_ref);
5758 if (err)
5759 goto done;
5761 err = got_ref_open(new_base_branch, repo,
5762 new_base_branch_ref_name, 0);
5763 if (err)
5764 goto done;
5766 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5767 if (err)
5768 goto done;
5769 done:
5770 free(commit_ref_name);
5771 free(branch_ref_name);
5772 free(fileindex_path);
5773 if (commit_ref)
5774 got_ref_close(commit_ref);
5775 if (branch_ref)
5776 got_ref_close(branch_ref);
5777 if (err) {
5778 free(*commit_id);
5779 *commit_id = NULL;
5780 if (*tmp_branch) {
5781 got_ref_close(*tmp_branch);
5782 *tmp_branch = NULL;
5784 if (*new_base_branch) {
5785 got_ref_close(*new_base_branch);
5786 *new_base_branch = NULL;
5788 if (*branch) {
5789 got_ref_close(*branch);
5790 *branch = NULL;
5792 if (*fileindex) {
5793 got_fileindex_free(*fileindex);
5794 *fileindex = NULL;
5796 lock_worktree(worktree, LOCK_SH);
5798 return err;
5801 const struct got_error *
5802 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5804 const struct got_error *err;
5805 char *tmp_branch_name = NULL;
5807 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5808 if (err)
5809 return err;
5811 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5812 free(tmp_branch_name);
5813 return NULL;
5816 static const struct got_error *
5817 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5818 char **logmsg, void *arg)
5820 *logmsg = arg;
5821 return NULL;
5824 static const struct got_error *
5825 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5826 const char *path, struct got_object_id *blob_id,
5827 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5828 int dirfd, const char *de_name)
5830 return NULL;
5833 struct collect_merged_paths_arg {
5834 got_worktree_checkout_cb progress_cb;
5835 void *progress_arg;
5836 struct got_pathlist_head *merged_paths;
5839 static const struct got_error *
5840 collect_merged_paths(void *arg, unsigned char status, const char *path)
5842 const struct got_error *err;
5843 struct collect_merged_paths_arg *a = arg;
5844 char *p;
5845 struct got_pathlist_entry *new;
5847 err = (*a->progress_cb)(a->progress_arg, status, path);
5848 if (err)
5849 return err;
5851 if (status != GOT_STATUS_MERGE &&
5852 status != GOT_STATUS_ADD &&
5853 status != GOT_STATUS_DELETE &&
5854 status != GOT_STATUS_CONFLICT)
5855 return NULL;
5857 p = strdup(path);
5858 if (p == NULL)
5859 return got_error_from_errno("strdup");
5861 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5862 if (err || new == NULL)
5863 free(p);
5864 return err;
5867 void
5868 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5870 struct got_pathlist_entry *pe;
5872 TAILQ_FOREACH(pe, merged_paths, entry)
5873 free((char *)pe->path);
5875 got_pathlist_free(merged_paths);
5878 static const struct got_error *
5879 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5880 int is_rebase, struct got_repository *repo)
5882 const struct got_error *err;
5883 struct got_reference *commit_ref = NULL;
5885 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5886 if (err) {
5887 if (err->code != GOT_ERR_NOT_REF)
5888 goto done;
5889 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5890 if (err)
5891 goto done;
5892 err = got_ref_write(commit_ref, repo);
5893 if (err)
5894 goto done;
5895 } else if (is_rebase) {
5896 struct got_object_id *stored_id;
5897 int cmp;
5899 err = got_ref_resolve(&stored_id, repo, commit_ref);
5900 if (err)
5901 goto done;
5902 cmp = got_object_id_cmp(commit_id, stored_id);
5903 free(stored_id);
5904 if (cmp != 0) {
5905 err = got_error(GOT_ERR_REBASE_COMMITID);
5906 goto done;
5909 done:
5910 if (commit_ref)
5911 got_ref_close(commit_ref);
5912 return err;
5915 static const struct got_error *
5916 rebase_merge_files(struct got_pathlist_head *merged_paths,
5917 const char *commit_ref_name, struct got_worktree *worktree,
5918 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5919 struct got_object_id *commit_id, struct got_repository *repo,
5920 got_worktree_checkout_cb progress_cb, void *progress_arg,
5921 got_cancel_cb cancel_cb, void *cancel_arg)
5923 const struct got_error *err;
5924 struct got_reference *commit_ref = NULL;
5925 struct collect_merged_paths_arg cmp_arg;
5926 char *fileindex_path;
5928 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5930 err = get_fileindex_path(&fileindex_path, worktree);
5931 if (err)
5932 return err;
5934 cmp_arg.progress_cb = progress_cb;
5935 cmp_arg.progress_arg = progress_arg;
5936 cmp_arg.merged_paths = merged_paths;
5937 err = merge_files(worktree, fileindex, fileindex_path,
5938 parent_commit_id, commit_id, repo, collect_merged_paths,
5939 &cmp_arg, cancel_cb, cancel_arg);
5940 if (commit_ref)
5941 got_ref_close(commit_ref);
5942 return err;
5945 const struct got_error *
5946 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5947 struct got_worktree *worktree, struct got_fileindex *fileindex,
5948 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5949 struct got_repository *repo,
5950 got_worktree_checkout_cb progress_cb, void *progress_arg,
5951 got_cancel_cb cancel_cb, void *cancel_arg)
5953 const struct got_error *err;
5954 char *commit_ref_name;
5956 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5957 if (err)
5958 return err;
5960 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5961 if (err)
5962 goto done;
5964 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5965 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5966 progress_arg, cancel_cb, cancel_arg);
5967 done:
5968 free(commit_ref_name);
5969 return err;
5972 const struct got_error *
5973 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5974 struct got_worktree *worktree, struct got_fileindex *fileindex,
5975 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5976 struct got_repository *repo,
5977 got_worktree_checkout_cb progress_cb, void *progress_arg,
5978 got_cancel_cb cancel_cb, void *cancel_arg)
5980 const struct got_error *err;
5981 char *commit_ref_name;
5983 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5984 if (err)
5985 return err;
5987 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5988 if (err)
5989 goto done;
5991 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5992 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5993 progress_arg, cancel_cb, cancel_arg);
5994 done:
5995 free(commit_ref_name);
5996 return err;
5999 static const struct got_error *
6000 rebase_commit(struct got_object_id **new_commit_id,
6001 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6002 struct got_worktree *worktree, struct got_fileindex *fileindex,
6003 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6004 const char *new_logmsg, struct got_repository *repo)
6006 const struct got_error *err, *sync_err;
6007 struct got_pathlist_head commitable_paths;
6008 struct collect_commitables_arg cc_arg;
6009 char *fileindex_path = NULL;
6010 struct got_reference *head_ref = NULL;
6011 struct got_object_id *head_commit_id = NULL;
6012 char *logmsg = NULL;
6014 TAILQ_INIT(&commitable_paths);
6015 *new_commit_id = NULL;
6017 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6019 err = get_fileindex_path(&fileindex_path, worktree);
6020 if (err)
6021 return err;
6023 cc_arg.commitable_paths = &commitable_paths;
6024 cc_arg.worktree = worktree;
6025 cc_arg.repo = repo;
6026 cc_arg.have_staged_files = 0;
6028 * If possible get the status of individual files directly to
6029 * avoid crawling the entire work tree once per rebased commit.
6030 * TODO: Ideally, merged_paths would contain a list of commitables
6031 * we could use so we could skip worktree_status() entirely.
6033 if (merged_paths) {
6034 struct got_pathlist_entry *pe;
6035 TAILQ_FOREACH(pe, merged_paths, entry) {
6036 err = worktree_status(worktree, pe->path, fileindex,
6037 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6038 0);
6039 if (err)
6040 goto done;
6042 } else {
6043 err = worktree_status(worktree, "", fileindex, repo,
6044 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6045 if (err)
6046 goto done;
6049 if (TAILQ_EMPTY(&commitable_paths)) {
6050 /* No-op change; commit will be elided. */
6051 err = got_ref_delete(commit_ref, repo);
6052 if (err)
6053 goto done;
6054 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6055 goto done;
6058 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6059 if (err)
6060 goto done;
6062 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6063 if (err)
6064 goto done;
6066 if (new_logmsg) {
6067 logmsg = strdup(new_logmsg);
6068 if (logmsg == NULL) {
6069 err = got_error_from_errno("strdup");
6070 goto done;
6072 } else {
6073 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6074 if (err)
6075 goto done;
6078 /* NB: commit_worktree will call free(logmsg) */
6079 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6080 worktree, got_object_commit_get_author(orig_commit),
6081 got_object_commit_get_committer(orig_commit),
6082 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6083 if (err)
6084 goto done;
6086 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6087 if (err)
6088 goto done;
6090 err = got_ref_delete(commit_ref, repo);
6091 if (err)
6092 goto done;
6094 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6095 fileindex, 0);
6096 sync_err = sync_fileindex(fileindex, fileindex_path);
6097 if (sync_err && err == NULL)
6098 err = sync_err;
6099 done:
6100 free(fileindex_path);
6101 free(head_commit_id);
6102 if (head_ref)
6103 got_ref_close(head_ref);
6104 if (err) {
6105 free(*new_commit_id);
6106 *new_commit_id = NULL;
6108 return err;
6111 const struct got_error *
6112 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6113 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6114 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6115 struct got_commit_object *orig_commit,
6116 struct got_object_id *orig_commit_id, struct got_repository *repo)
6118 const struct got_error *err;
6119 char *commit_ref_name;
6120 struct got_reference *commit_ref = NULL;
6121 struct got_object_id *commit_id = NULL;
6123 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6124 if (err)
6125 return err;
6127 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6128 if (err)
6129 goto done;
6130 err = got_ref_resolve(&commit_id, repo, commit_ref);
6131 if (err)
6132 goto done;
6133 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6134 err = got_error(GOT_ERR_REBASE_COMMITID);
6135 goto done;
6138 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6139 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6140 done:
6141 if (commit_ref)
6142 got_ref_close(commit_ref);
6143 free(commit_ref_name);
6144 free(commit_id);
6145 return err;
6148 const struct got_error *
6149 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6150 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6151 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6152 struct got_commit_object *orig_commit,
6153 struct got_object_id *orig_commit_id, const char *new_logmsg,
6154 struct got_repository *repo)
6156 const struct got_error *err;
6157 char *commit_ref_name;
6158 struct got_reference *commit_ref = NULL;
6160 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6161 if (err)
6162 return err;
6164 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6165 if (err)
6166 goto done;
6168 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6169 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6170 done:
6171 if (commit_ref)
6172 got_ref_close(commit_ref);
6173 free(commit_ref_name);
6174 return err;
6177 const struct got_error *
6178 got_worktree_rebase_postpone(struct got_worktree *worktree,
6179 struct got_fileindex *fileindex)
6181 if (fileindex)
6182 got_fileindex_free(fileindex);
6183 return lock_worktree(worktree, LOCK_SH);
6186 static const struct got_error *
6187 delete_ref(const char *name, struct got_repository *repo)
6189 const struct got_error *err;
6190 struct got_reference *ref;
6192 err = got_ref_open(&ref, repo, name, 0);
6193 if (err) {
6194 if (err->code == GOT_ERR_NOT_REF)
6195 return NULL;
6196 return err;
6199 err = got_ref_delete(ref, repo);
6200 got_ref_close(ref);
6201 return err;
6204 static const struct got_error *
6205 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6207 const struct got_error *err;
6208 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6209 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6211 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6212 if (err)
6213 goto done;
6214 err = delete_ref(tmp_branch_name, repo);
6215 if (err)
6216 goto done;
6218 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6219 if (err)
6220 goto done;
6221 err = delete_ref(new_base_branch_ref_name, repo);
6222 if (err)
6223 goto done;
6225 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6226 if (err)
6227 goto done;
6228 err = delete_ref(branch_ref_name, repo);
6229 if (err)
6230 goto done;
6232 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6233 if (err)
6234 goto done;
6235 err = delete_ref(commit_ref_name, repo);
6236 if (err)
6237 goto done;
6239 done:
6240 free(tmp_branch_name);
6241 free(new_base_branch_ref_name);
6242 free(branch_ref_name);
6243 free(commit_ref_name);
6244 return err;
6247 const struct got_error *
6248 got_worktree_rebase_complete(struct got_worktree *worktree,
6249 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6250 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6251 struct got_repository *repo)
6253 const struct got_error *err, *unlockerr;
6254 struct got_object_id *new_head_commit_id = NULL;
6256 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6257 if (err)
6258 return err;
6260 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6261 if (err)
6262 goto done;
6264 err = got_ref_write(rebased_branch, repo);
6265 if (err)
6266 goto done;
6268 err = got_worktree_set_head_ref(worktree, rebased_branch);
6269 if (err)
6270 goto done;
6272 err = delete_rebase_refs(worktree, repo);
6273 done:
6274 if (fileindex)
6275 got_fileindex_free(fileindex);
6276 free(new_head_commit_id);
6277 unlockerr = lock_worktree(worktree, LOCK_SH);
6278 if (unlockerr && err == NULL)
6279 err = unlockerr;
6280 return err;
6283 const struct got_error *
6284 got_worktree_rebase_abort(struct got_worktree *worktree,
6285 struct got_fileindex *fileindex, struct got_repository *repo,
6286 struct got_reference *new_base_branch,
6287 got_worktree_checkout_cb progress_cb, void *progress_arg)
6289 const struct got_error *err, *unlockerr, *sync_err;
6290 struct got_reference *resolved = NULL;
6291 struct got_object_id *commit_id = NULL;
6292 char *fileindex_path = NULL;
6293 struct revert_file_args rfa;
6294 struct got_object_id *tree_id = NULL;
6296 err = lock_worktree(worktree, LOCK_EX);
6297 if (err)
6298 return err;
6300 err = got_ref_open(&resolved, repo,
6301 got_ref_get_symref_target(new_base_branch), 0);
6302 if (err)
6303 goto done;
6305 err = got_worktree_set_head_ref(worktree, resolved);
6306 if (err)
6307 goto done;
6310 * XXX commits to the base branch could have happened while
6311 * we were busy rebasing; should we store the original commit ID
6312 * when rebase begins and read it back here?
6314 err = got_ref_resolve(&commit_id, repo, resolved);
6315 if (err)
6316 goto done;
6318 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6319 if (err)
6320 goto done;
6322 err = got_object_id_by_path(&tree_id, repo,
6323 worktree->base_commit_id, worktree->path_prefix);
6324 if (err)
6325 goto done;
6327 err = delete_rebase_refs(worktree, repo);
6328 if (err)
6329 goto done;
6331 err = get_fileindex_path(&fileindex_path, worktree);
6332 if (err)
6333 goto done;
6335 rfa.worktree = worktree;
6336 rfa.fileindex = fileindex;
6337 rfa.progress_cb = progress_cb;
6338 rfa.progress_arg = progress_arg;
6339 rfa.patch_cb = NULL;
6340 rfa.patch_arg = NULL;
6341 rfa.repo = repo;
6342 err = worktree_status(worktree, "", fileindex, repo,
6343 revert_file, &rfa, NULL, NULL, 0, 0);
6344 if (err)
6345 goto sync;
6347 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6348 repo, progress_cb, progress_arg, NULL, NULL);
6349 sync:
6350 sync_err = sync_fileindex(fileindex, fileindex_path);
6351 if (sync_err && err == NULL)
6352 err = sync_err;
6353 done:
6354 got_ref_close(resolved);
6355 free(tree_id);
6356 free(commit_id);
6357 if (fileindex)
6358 got_fileindex_free(fileindex);
6359 free(fileindex_path);
6361 unlockerr = lock_worktree(worktree, LOCK_SH);
6362 if (unlockerr && err == NULL)
6363 err = unlockerr;
6364 return err;
6367 const struct got_error *
6368 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6369 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6370 struct got_fileindex **fileindex, struct got_worktree *worktree,
6371 struct got_repository *repo)
6373 const struct got_error *err = NULL;
6374 char *tmp_branch_name = NULL;
6375 char *branch_ref_name = NULL;
6376 char *base_commit_ref_name = NULL;
6377 char *fileindex_path = NULL;
6378 struct check_rebase_ok_arg ok_arg;
6379 struct got_reference *wt_branch = NULL;
6380 struct got_reference *base_commit_ref = NULL;
6382 *tmp_branch = NULL;
6383 *branch_ref = NULL;
6384 *base_commit_id = NULL;
6385 *fileindex = NULL;
6387 err = lock_worktree(worktree, LOCK_EX);
6388 if (err)
6389 return err;
6391 err = open_fileindex(fileindex, &fileindex_path, worktree);
6392 if (err)
6393 goto done;
6395 ok_arg.worktree = worktree;
6396 ok_arg.repo = repo;
6397 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6398 &ok_arg);
6399 if (err)
6400 goto done;
6402 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6403 if (err)
6404 goto done;
6406 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6407 if (err)
6408 goto done;
6410 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6411 worktree);
6412 if (err)
6413 goto done;
6415 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6416 0);
6417 if (err)
6418 goto done;
6420 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6421 if (err)
6422 goto done;
6424 err = got_ref_write(*branch_ref, repo);
6425 if (err)
6426 goto done;
6428 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6429 worktree->base_commit_id);
6430 if (err)
6431 goto done;
6432 err = got_ref_write(base_commit_ref, repo);
6433 if (err)
6434 goto done;
6435 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6436 if (*base_commit_id == NULL) {
6437 err = got_error_from_errno("got_object_id_dup");
6438 goto done;
6441 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6442 worktree->base_commit_id);
6443 if (err)
6444 goto done;
6445 err = got_ref_write(*tmp_branch, repo);
6446 if (err)
6447 goto done;
6449 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6450 if (err)
6451 goto done;
6452 done:
6453 free(fileindex_path);
6454 free(tmp_branch_name);
6455 free(branch_ref_name);
6456 free(base_commit_ref_name);
6457 if (wt_branch)
6458 got_ref_close(wt_branch);
6459 if (err) {
6460 if (*branch_ref) {
6461 got_ref_close(*branch_ref);
6462 *branch_ref = NULL;
6464 if (*tmp_branch) {
6465 got_ref_close(*tmp_branch);
6466 *tmp_branch = NULL;
6468 free(*base_commit_id);
6469 if (*fileindex) {
6470 got_fileindex_free(*fileindex);
6471 *fileindex = NULL;
6473 lock_worktree(worktree, LOCK_SH);
6475 return err;
6478 const struct got_error *
6479 got_worktree_histedit_postpone(struct got_worktree *worktree,
6480 struct got_fileindex *fileindex)
6482 if (fileindex)
6483 got_fileindex_free(fileindex);
6484 return lock_worktree(worktree, LOCK_SH);
6487 const struct got_error *
6488 got_worktree_histedit_in_progress(int *in_progress,
6489 struct got_worktree *worktree)
6491 const struct got_error *err;
6492 char *tmp_branch_name = NULL;
6494 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6495 if (err)
6496 return err;
6498 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6499 free(tmp_branch_name);
6500 return NULL;
6503 const struct got_error *
6504 got_worktree_histedit_continue(struct got_object_id **commit_id,
6505 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6506 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6507 struct got_worktree *worktree, struct got_repository *repo)
6509 const struct got_error *err;
6510 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6511 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6512 struct got_reference *commit_ref = NULL;
6513 struct got_reference *base_commit_ref = NULL;
6514 char *fileindex_path = NULL;
6515 int have_staged_files = 0;
6517 *commit_id = NULL;
6518 *tmp_branch = NULL;
6519 *base_commit_id = NULL;
6520 *fileindex = NULL;
6522 err = lock_worktree(worktree, LOCK_EX);
6523 if (err)
6524 return err;
6526 err = open_fileindex(fileindex, &fileindex_path, worktree);
6527 if (err)
6528 goto done;
6530 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6531 &have_staged_files);
6532 if (err && err->code != GOT_ERR_CANCELLED)
6533 goto done;
6534 if (have_staged_files) {
6535 err = got_error(GOT_ERR_STAGED_PATHS);
6536 goto done;
6539 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6540 if (err)
6541 goto done;
6543 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6544 if (err)
6545 goto done;
6547 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6548 if (err)
6549 goto done;
6551 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6552 worktree);
6553 if (err)
6554 goto done;
6556 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6557 if (err)
6558 goto done;
6560 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6561 if (err)
6562 goto done;
6563 err = got_ref_resolve(commit_id, repo, commit_ref);
6564 if (err)
6565 goto done;
6567 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6568 if (err)
6569 goto done;
6570 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6571 if (err)
6572 goto done;
6574 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6575 if (err)
6576 goto done;
6577 done:
6578 free(commit_ref_name);
6579 free(branch_ref_name);
6580 free(fileindex_path);
6581 if (commit_ref)
6582 got_ref_close(commit_ref);
6583 if (base_commit_ref)
6584 got_ref_close(base_commit_ref);
6585 if (err) {
6586 free(*commit_id);
6587 *commit_id = NULL;
6588 free(*base_commit_id);
6589 *base_commit_id = NULL;
6590 if (*tmp_branch) {
6591 got_ref_close(*tmp_branch);
6592 *tmp_branch = NULL;
6594 if (*fileindex) {
6595 got_fileindex_free(*fileindex);
6596 *fileindex = NULL;
6598 lock_worktree(worktree, LOCK_EX);
6600 return err;
6603 static const struct got_error *
6604 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6606 const struct got_error *err;
6607 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6608 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6610 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6611 if (err)
6612 goto done;
6613 err = delete_ref(tmp_branch_name, repo);
6614 if (err)
6615 goto done;
6617 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6618 worktree);
6619 if (err)
6620 goto done;
6621 err = delete_ref(base_commit_ref_name, repo);
6622 if (err)
6623 goto done;
6625 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6626 if (err)
6627 goto done;
6628 err = delete_ref(branch_ref_name, repo);
6629 if (err)
6630 goto done;
6632 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6633 if (err)
6634 goto done;
6635 err = delete_ref(commit_ref_name, repo);
6636 if (err)
6637 goto done;
6638 done:
6639 free(tmp_branch_name);
6640 free(base_commit_ref_name);
6641 free(branch_ref_name);
6642 free(commit_ref_name);
6643 return err;
6646 const struct got_error *
6647 got_worktree_histedit_abort(struct got_worktree *worktree,
6648 struct got_fileindex *fileindex, struct got_repository *repo,
6649 struct got_reference *branch, struct got_object_id *base_commit_id,
6650 got_worktree_checkout_cb progress_cb, void *progress_arg)
6652 const struct got_error *err, *unlockerr, *sync_err;
6653 struct got_reference *resolved = NULL;
6654 char *fileindex_path = NULL;
6655 struct got_object_id *tree_id = NULL;
6656 struct revert_file_args rfa;
6658 err = lock_worktree(worktree, LOCK_EX);
6659 if (err)
6660 return err;
6662 err = got_ref_open(&resolved, repo,
6663 got_ref_get_symref_target(branch), 0);
6664 if (err)
6665 goto done;
6667 err = got_worktree_set_head_ref(worktree, resolved);
6668 if (err)
6669 goto done;
6671 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6672 if (err)
6673 goto done;
6675 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6676 worktree->path_prefix);
6677 if (err)
6678 goto done;
6680 err = delete_histedit_refs(worktree, repo);
6681 if (err)
6682 goto done;
6684 err = get_fileindex_path(&fileindex_path, worktree);
6685 if (err)
6686 goto done;
6688 rfa.worktree = worktree;
6689 rfa.fileindex = fileindex;
6690 rfa.progress_cb = progress_cb;
6691 rfa.progress_arg = progress_arg;
6692 rfa.patch_cb = NULL;
6693 rfa.patch_arg = NULL;
6694 rfa.repo = repo;
6695 err = worktree_status(worktree, "", fileindex, repo,
6696 revert_file, &rfa, NULL, NULL, 0, 0);
6697 if (err)
6698 goto sync;
6700 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6701 repo, progress_cb, progress_arg, NULL, NULL);
6702 sync:
6703 sync_err = sync_fileindex(fileindex, fileindex_path);
6704 if (sync_err && err == NULL)
6705 err = sync_err;
6706 done:
6707 got_ref_close(resolved);
6708 free(tree_id);
6709 free(fileindex_path);
6711 unlockerr = lock_worktree(worktree, LOCK_SH);
6712 if (unlockerr && err == NULL)
6713 err = unlockerr;
6714 return err;
6717 const struct got_error *
6718 got_worktree_histedit_complete(struct got_worktree *worktree,
6719 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6720 struct got_reference *edited_branch, struct got_repository *repo)
6722 const struct got_error *err, *unlockerr;
6723 struct got_object_id *new_head_commit_id = NULL;
6724 struct got_reference *resolved = NULL;
6726 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6727 if (err)
6728 return err;
6730 err = got_ref_open(&resolved, repo,
6731 got_ref_get_symref_target(edited_branch), 0);
6732 if (err)
6733 goto done;
6735 err = got_ref_change_ref(resolved, new_head_commit_id);
6736 if (err)
6737 goto done;
6739 err = got_ref_write(resolved, repo);
6740 if (err)
6741 goto done;
6743 err = got_worktree_set_head_ref(worktree, resolved);
6744 if (err)
6745 goto done;
6747 err = delete_histedit_refs(worktree, repo);
6748 done:
6749 if (fileindex)
6750 got_fileindex_free(fileindex);
6751 free(new_head_commit_id);
6752 unlockerr = lock_worktree(worktree, LOCK_SH);
6753 if (unlockerr && err == NULL)
6754 err = unlockerr;
6755 return err;
6758 const struct got_error *
6759 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6760 struct got_object_id *commit_id, struct got_repository *repo)
6762 const struct got_error *err;
6763 char *commit_ref_name;
6765 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6766 if (err)
6767 return err;
6769 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6770 if (err)
6771 goto done;
6773 err = delete_ref(commit_ref_name, repo);
6774 done:
6775 free(commit_ref_name);
6776 return err;
6779 const struct got_error *
6780 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6781 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6782 struct got_worktree *worktree, const char *refname,
6783 struct got_repository *repo)
6785 const struct got_error *err = NULL;
6786 char *fileindex_path = NULL;
6787 struct check_rebase_ok_arg ok_arg;
6789 *fileindex = NULL;
6790 *branch_ref = NULL;
6791 *base_branch_ref = NULL;
6793 err = lock_worktree(worktree, LOCK_EX);
6794 if (err)
6795 return err;
6797 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6798 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6799 "cannot integrate a branch into itself; "
6800 "update -b or different branch name required");
6801 goto done;
6804 err = open_fileindex(fileindex, &fileindex_path, worktree);
6805 if (err)
6806 goto done;
6808 /* Preconditions are the same as for rebase. */
6809 ok_arg.worktree = worktree;
6810 ok_arg.repo = repo;
6811 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6812 &ok_arg);
6813 if (err)
6814 goto done;
6816 err = got_ref_open(branch_ref, repo, refname, 1);
6817 if (err)
6818 goto done;
6820 err = got_ref_open(base_branch_ref, repo,
6821 got_worktree_get_head_ref_name(worktree), 1);
6822 done:
6823 if (err) {
6824 if (*branch_ref) {
6825 got_ref_close(*branch_ref);
6826 *branch_ref = NULL;
6828 if (*base_branch_ref) {
6829 got_ref_close(*base_branch_ref);
6830 *base_branch_ref = NULL;
6832 if (*fileindex) {
6833 got_fileindex_free(*fileindex);
6834 *fileindex = NULL;
6836 lock_worktree(worktree, LOCK_SH);
6838 return err;
6841 const struct got_error *
6842 got_worktree_integrate_continue(struct got_worktree *worktree,
6843 struct got_fileindex *fileindex, struct got_repository *repo,
6844 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6845 got_worktree_checkout_cb progress_cb, void *progress_arg,
6846 got_cancel_cb cancel_cb, void *cancel_arg)
6848 const struct got_error *err = NULL, *sync_err, *unlockerr;
6849 char *fileindex_path = NULL;
6850 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6852 err = get_fileindex_path(&fileindex_path, worktree);
6853 if (err)
6854 goto done;
6856 err = got_ref_resolve(&commit_id, repo, branch_ref);
6857 if (err)
6858 goto done;
6860 err = got_object_id_by_path(&tree_id, repo, commit_id,
6861 worktree->path_prefix);
6862 if (err)
6863 goto done;
6865 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6866 if (err)
6867 goto done;
6869 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6870 progress_cb, progress_arg, cancel_cb, cancel_arg);
6871 if (err)
6872 goto sync;
6874 err = got_ref_change_ref(base_branch_ref, commit_id);
6875 if (err)
6876 goto sync;
6878 err = got_ref_write(base_branch_ref, repo);
6879 sync:
6880 sync_err = sync_fileindex(fileindex, fileindex_path);
6881 if (sync_err && err == NULL)
6882 err = sync_err;
6884 done:
6885 unlockerr = got_ref_unlock(branch_ref);
6886 if (unlockerr && err == NULL)
6887 err = unlockerr;
6888 got_ref_close(branch_ref);
6890 unlockerr = got_ref_unlock(base_branch_ref);
6891 if (unlockerr && err == NULL)
6892 err = unlockerr;
6893 got_ref_close(base_branch_ref);
6895 got_fileindex_free(fileindex);
6896 free(fileindex_path);
6897 free(tree_id);
6899 unlockerr = lock_worktree(worktree, LOCK_SH);
6900 if (unlockerr && err == NULL)
6901 err = unlockerr;
6902 return err;
6905 const struct got_error *
6906 got_worktree_integrate_abort(struct got_worktree *worktree,
6907 struct got_fileindex *fileindex, struct got_repository *repo,
6908 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6910 const struct got_error *err = NULL, *unlockerr = NULL;
6912 got_fileindex_free(fileindex);
6914 err = lock_worktree(worktree, LOCK_SH);
6916 unlockerr = got_ref_unlock(branch_ref);
6917 if (unlockerr && err == NULL)
6918 err = unlockerr;
6919 got_ref_close(branch_ref);
6921 unlockerr = got_ref_unlock(base_branch_ref);
6922 if (unlockerr && err == NULL)
6923 err = unlockerr;
6924 got_ref_close(base_branch_ref);
6926 return err;
6929 struct check_stage_ok_arg {
6930 struct got_object_id *head_commit_id;
6931 struct got_worktree *worktree;
6932 struct got_fileindex *fileindex;
6933 struct got_repository *repo;
6934 int have_changes;
6937 const struct got_error *
6938 check_stage_ok(void *arg, unsigned char status,
6939 unsigned char staged_status, const char *relpath,
6940 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6941 struct got_object_id *commit_id, int dirfd, const char *de_name)
6943 struct check_stage_ok_arg *a = arg;
6944 const struct got_error *err = NULL;
6945 struct got_fileindex_entry *ie;
6946 struct got_object_id base_commit_id;
6947 struct got_object_id *base_commit_idp = NULL;
6948 char *in_repo_path = NULL, *p;
6950 if (status == GOT_STATUS_UNVERSIONED ||
6951 status == GOT_STATUS_NO_CHANGE)
6952 return NULL;
6953 if (status == GOT_STATUS_NONEXISTENT)
6954 return got_error_set_errno(ENOENT, relpath);
6956 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6957 if (ie == NULL)
6958 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6960 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6961 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6962 relpath) == -1)
6963 return got_error_from_errno("asprintf");
6965 if (got_fileindex_entry_has_commit(ie)) {
6966 memcpy(base_commit_id.sha1, ie->commit_sha1,
6967 SHA1_DIGEST_LENGTH);
6968 base_commit_idp = &base_commit_id;
6971 if (status == GOT_STATUS_CONFLICT) {
6972 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6973 goto done;
6974 } else if (status != GOT_STATUS_ADD &&
6975 status != GOT_STATUS_MODIFY &&
6976 status != GOT_STATUS_DELETE) {
6977 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6978 goto done;
6981 a->have_changes = 1;
6983 p = in_repo_path;
6984 while (p[0] == '/')
6985 p++;
6986 err = check_out_of_date(p, status, staged_status,
6987 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6988 GOT_ERR_STAGE_OUT_OF_DATE);
6989 done:
6990 free(in_repo_path);
6991 return err;
6994 struct stage_path_arg {
6995 struct got_worktree *worktree;
6996 struct got_fileindex *fileindex;
6997 struct got_repository *repo;
6998 got_worktree_status_cb status_cb;
6999 void *status_arg;
7000 got_worktree_patch_cb patch_cb;
7001 void *patch_arg;
7002 int staged_something;
7005 static const struct got_error *
7006 stage_path(void *arg, unsigned char status,
7007 unsigned char staged_status, const char *relpath,
7008 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7009 struct got_object_id *commit_id, int dirfd, const char *de_name)
7011 struct stage_path_arg *a = arg;
7012 const struct got_error *err = NULL;
7013 struct got_fileindex_entry *ie;
7014 char *ondisk_path = NULL, *path_content = NULL;
7015 uint32_t stage;
7016 struct got_object_id *new_staged_blob_id = NULL;
7018 if (status == GOT_STATUS_UNVERSIONED)
7019 return NULL;
7021 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7022 if (ie == NULL)
7023 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7025 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7026 relpath)== -1)
7027 return got_error_from_errno("asprintf");
7029 switch (status) {
7030 case GOT_STATUS_ADD:
7031 case GOT_STATUS_MODIFY:
7032 if (a->patch_cb) {
7033 if (status == GOT_STATUS_ADD) {
7034 int choice = GOT_PATCH_CHOICE_NONE;
7035 err = (*a->patch_cb)(&choice, a->patch_arg,
7036 status, ie->path, NULL, 1, 1);
7037 if (err)
7038 break;
7039 if (choice != GOT_PATCH_CHOICE_YES)
7040 break;
7041 } else {
7042 err = create_patched_content(&path_content, 0,
7043 staged_blob_id ? staged_blob_id : blob_id,
7044 ondisk_path, dirfd, de_name, ie->path,
7045 a->repo, a->patch_cb, a->patch_arg);
7046 if (err || path_content == NULL)
7047 break;
7050 err = got_object_blob_create(&new_staged_blob_id,
7051 path_content ? path_content : ondisk_path, a->repo);
7052 if (err)
7053 break;
7054 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7055 SHA1_DIGEST_LENGTH);
7056 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7057 stage = GOT_FILEIDX_STAGE_ADD;
7058 else
7059 stage = GOT_FILEIDX_STAGE_MODIFY;
7060 got_fileindex_entry_stage_set(ie, stage);
7061 a->staged_something = 1;
7062 if (a->status_cb == NULL)
7063 break;
7064 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7065 get_staged_status(ie), relpath, blob_id,
7066 new_staged_blob_id, NULL, dirfd, de_name);
7067 break;
7068 case GOT_STATUS_DELETE:
7069 if (staged_status == GOT_STATUS_DELETE)
7070 break;
7071 if (a->patch_cb) {
7072 int choice = GOT_PATCH_CHOICE_NONE;
7073 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7074 ie->path, NULL, 1, 1);
7075 if (err)
7076 break;
7077 if (choice == GOT_PATCH_CHOICE_NO)
7078 break;
7079 if (choice != GOT_PATCH_CHOICE_YES) {
7080 err = got_error(GOT_ERR_PATCH_CHOICE);
7081 break;
7084 stage = GOT_FILEIDX_STAGE_DELETE;
7085 got_fileindex_entry_stage_set(ie, stage);
7086 a->staged_something = 1;
7087 if (a->status_cb == NULL)
7088 break;
7089 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7090 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7091 de_name);
7092 break;
7093 case GOT_STATUS_NO_CHANGE:
7094 break;
7095 case GOT_STATUS_CONFLICT:
7096 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7097 break;
7098 case GOT_STATUS_NONEXISTENT:
7099 err = got_error_set_errno(ENOENT, relpath);
7100 break;
7101 default:
7102 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7103 break;
7106 if (path_content && unlink(path_content) == -1 && err == NULL)
7107 err = got_error_from_errno2("unlink", path_content);
7108 free(path_content);
7109 free(ondisk_path);
7110 free(new_staged_blob_id);
7111 return err;
7114 const struct got_error *
7115 got_worktree_stage(struct got_worktree *worktree,
7116 struct got_pathlist_head *paths,
7117 got_worktree_status_cb status_cb, void *status_arg,
7118 got_worktree_patch_cb patch_cb, void *patch_arg,
7119 struct got_repository *repo)
7121 const struct got_error *err = NULL, *sync_err, *unlockerr;
7122 struct got_pathlist_entry *pe;
7123 struct got_fileindex *fileindex = NULL;
7124 char *fileindex_path = NULL;
7125 struct got_reference *head_ref = NULL;
7126 struct got_object_id *head_commit_id = NULL;
7127 struct check_stage_ok_arg oka;
7128 struct stage_path_arg spa;
7130 err = lock_worktree(worktree, LOCK_EX);
7131 if (err)
7132 return err;
7134 err = got_ref_open(&head_ref, repo,
7135 got_worktree_get_head_ref_name(worktree), 0);
7136 if (err)
7137 goto done;
7138 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7139 if (err)
7140 goto done;
7141 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7142 if (err)
7143 goto done;
7145 /* Check pre-conditions before staging anything. */
7146 oka.head_commit_id = head_commit_id;
7147 oka.worktree = worktree;
7148 oka.fileindex = fileindex;
7149 oka.repo = repo;
7150 oka.have_changes = 0;
7151 TAILQ_FOREACH(pe, paths, entry) {
7152 err = worktree_status(worktree, pe->path, fileindex, repo,
7153 check_stage_ok, &oka, NULL, NULL, 0, 0);
7154 if (err)
7155 goto done;
7157 if (!oka.have_changes) {
7158 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7159 goto done;
7162 spa.worktree = worktree;
7163 spa.fileindex = fileindex;
7164 spa.repo = repo;
7165 spa.patch_cb = patch_cb;
7166 spa.patch_arg = patch_arg;
7167 spa.status_cb = status_cb;
7168 spa.status_arg = status_arg;
7169 spa.staged_something = 0;
7170 TAILQ_FOREACH(pe, paths, entry) {
7171 err = worktree_status(worktree, pe->path, fileindex, repo,
7172 stage_path, &spa, NULL, NULL, 0, 0);
7173 if (err)
7174 goto done;
7176 if (!spa.staged_something) {
7177 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7178 goto done;
7181 sync_err = sync_fileindex(fileindex, fileindex_path);
7182 if (sync_err && err == NULL)
7183 err = sync_err;
7184 done:
7185 if (head_ref)
7186 got_ref_close(head_ref);
7187 free(head_commit_id);
7188 free(fileindex_path);
7189 if (fileindex)
7190 got_fileindex_free(fileindex);
7191 unlockerr = lock_worktree(worktree, LOCK_SH);
7192 if (unlockerr && err == NULL)
7193 err = unlockerr;
7194 return err;
7197 struct unstage_path_arg {
7198 struct got_worktree *worktree;
7199 struct got_fileindex *fileindex;
7200 struct got_repository *repo;
7201 got_worktree_checkout_cb progress_cb;
7202 void *progress_arg;
7203 got_worktree_patch_cb patch_cb;
7204 void *patch_arg;
7207 static const struct got_error *
7208 create_unstaged_content(char **path_unstaged_content,
7209 char **path_new_staged_content, struct got_object_id *blob_id,
7210 struct got_object_id *staged_blob_id, const char *relpath,
7211 struct got_repository *repo,
7212 got_worktree_patch_cb patch_cb, void *patch_arg)
7214 const struct got_error *err;
7215 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7216 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7217 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7218 struct stat sb1, sb2;
7219 struct got_diff_changes *changes = NULL;
7220 struct got_diff_state *ds = NULL;
7221 struct got_diff_args *args = NULL;
7222 struct got_diff_change *change;
7223 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7224 int have_content = 0, have_rejected_content = 0;
7226 *path_unstaged_content = NULL;
7227 *path_new_staged_content = NULL;
7229 err = got_object_id_str(&label1, blob_id);
7230 if (err)
7231 return err;
7232 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7233 if (err)
7234 goto done;
7236 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7237 if (err)
7238 goto done;
7240 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7241 if (err)
7242 goto done;
7244 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7245 if (err)
7246 goto done;
7248 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7249 if (err)
7250 goto done;
7252 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7253 if (err)
7254 goto done;
7256 if (stat(path1, &sb1) == -1) {
7257 err = got_error_from_errno2("stat", path1);
7258 goto done;
7261 if (stat(path2, &sb2) == -1) {
7262 err = got_error_from_errno2("stat", path2);
7263 goto done;
7266 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7267 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7268 if (err)
7269 goto done;
7271 err = got_opentemp_named(path_unstaged_content, &outfile,
7272 "got-unstaged-content");
7273 if (err)
7274 goto done;
7275 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7276 "got-new-staged-content");
7277 if (err)
7278 goto done;
7280 if (fseek(f1, 0L, SEEK_SET) == -1) {
7281 err = got_ferror(f1, GOT_ERR_IO);
7282 goto done;
7284 if (fseek(f2, 0L, SEEK_SET) == -1) {
7285 err = got_ferror(f2, GOT_ERR_IO);
7286 goto done;
7288 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7289 int choice;
7290 err = apply_or_reject_change(&choice, change, ++n,
7291 changes->nchanges, ds, args, diff_flags, relpath,
7292 f1, f2, &line_cur1, &line_cur2,
7293 outfile, rejectfile, patch_cb, patch_arg);
7294 if (err)
7295 goto done;
7296 if (choice == GOT_PATCH_CHOICE_YES)
7297 have_content = 1;
7298 else
7299 have_rejected_content = 1;
7300 if (choice == GOT_PATCH_CHOICE_QUIT)
7301 break;
7303 if (have_content || have_rejected_content)
7304 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7305 outfile, rejectfile);
7306 done:
7307 free(label1);
7308 if (blob)
7309 got_object_blob_close(blob);
7310 if (staged_blob)
7311 got_object_blob_close(staged_blob);
7312 if (f1 && fclose(f1) == EOF && err == NULL)
7313 err = got_error_from_errno2("fclose", path1);
7314 if (f2 && fclose(f2) == EOF && err == NULL)
7315 err = got_error_from_errno2("fclose", path2);
7316 if (outfile && fclose(outfile) == EOF && err == NULL)
7317 err = got_error_from_errno2("fclose", *path_unstaged_content);
7318 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7319 err = got_error_from_errno2("fclose", *path_new_staged_content);
7320 if (path1 && unlink(path1) == -1 && err == NULL)
7321 err = got_error_from_errno2("unlink", path1);
7322 if (path2 && unlink(path2) == -1 && err == NULL)
7323 err = got_error_from_errno2("unlink", path2);
7324 if (err || !have_content) {
7325 if (*path_unstaged_content &&
7326 unlink(*path_unstaged_content) == -1 && err == NULL)
7327 err = got_error_from_errno2("unlink",
7328 *path_unstaged_content);
7329 free(*path_unstaged_content);
7330 *path_unstaged_content = NULL;
7332 if (err || !have_rejected_content) {
7333 if (*path_new_staged_content &&
7334 unlink(*path_new_staged_content) == -1 && err == NULL)
7335 err = got_error_from_errno2("unlink",
7336 *path_new_staged_content);
7337 free(*path_new_staged_content);
7338 *path_new_staged_content = NULL;
7340 free(args);
7341 if (ds) {
7342 got_diff_state_free(ds);
7343 free(ds);
7345 if (changes)
7346 got_diff_free_changes(changes);
7347 free(path1);
7348 free(path2);
7349 return err;
7352 static const struct got_error *
7353 unstage_path(void *arg, unsigned char status,
7354 unsigned char staged_status, const char *relpath,
7355 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7356 struct got_object_id *commit_id, int dirfd, const char *de_name)
7358 const struct got_error *err = NULL;
7359 struct unstage_path_arg *a = arg;
7360 struct got_fileindex_entry *ie;
7361 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7362 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7363 char *path_new_staged_content = NULL;
7364 char *id_str = NULL, *label_orig = NULL;
7365 int local_changes_subsumed;
7366 struct stat sb;
7368 if (staged_status != GOT_STATUS_ADD &&
7369 staged_status != GOT_STATUS_MODIFY &&
7370 staged_status != GOT_STATUS_DELETE)
7371 return NULL;
7373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7374 if (ie == NULL)
7375 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7377 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7378 == -1)
7379 return got_error_from_errno("asprintf");
7381 err = got_object_id_str(&id_str,
7382 commit_id ? commit_id : a->worktree->base_commit_id);
7383 if (err)
7384 goto done;
7385 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7386 id_str) == -1) {
7387 err = got_error_from_errno("asprintf");
7388 goto done;
7391 switch (staged_status) {
7392 case GOT_STATUS_MODIFY:
7393 err = got_object_open_as_blob(&blob_base, a->repo,
7394 blob_id, 8192);
7395 if (err)
7396 break;
7397 /* fall through */
7398 case GOT_STATUS_ADD:
7399 if (a->patch_cb) {
7400 if (staged_status == GOT_STATUS_ADD) {
7401 int choice = GOT_PATCH_CHOICE_NONE;
7402 err = (*a->patch_cb)(&choice, a->patch_arg,
7403 staged_status, ie->path, NULL, 1, 1);
7404 if (err)
7405 break;
7406 if (choice != GOT_PATCH_CHOICE_YES)
7407 break;
7408 } else {
7409 err = create_unstaged_content(
7410 &path_unstaged_content,
7411 &path_new_staged_content, blob_id,
7412 staged_blob_id, ie->path, a->repo,
7413 a->patch_cb, a->patch_arg);
7414 if (err || path_unstaged_content == NULL)
7415 break;
7416 if (path_new_staged_content) {
7417 err = got_object_blob_create(
7418 &staged_blob_id,
7419 path_new_staged_content,
7420 a->repo);
7421 if (err)
7422 break;
7423 memcpy(ie->staged_blob_sha1,
7424 staged_blob_id->sha1,
7425 SHA1_DIGEST_LENGTH);
7427 err = merge_file(&local_changes_subsumed,
7428 a->worktree, blob_base, ondisk_path,
7429 relpath, got_fileindex_perms_to_st(ie),
7430 path_unstaged_content, label_orig,
7431 "unstaged", a->repo, a->progress_cb,
7432 a->progress_arg);
7433 if (err == NULL &&
7434 path_new_staged_content == NULL)
7435 got_fileindex_entry_stage_set(ie,
7436 GOT_FILEIDX_STAGE_NONE);
7437 break; /* Done with this file. */
7440 err = got_object_open_as_blob(&blob_staged, a->repo,
7441 staged_blob_id, 8192);
7442 if (err)
7443 break;
7444 err = merge_blob(&local_changes_subsumed, a->worktree,
7445 blob_base, ondisk_path, relpath,
7446 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7447 commit_id ? commit_id : a->worktree->base_commit_id,
7448 a->repo, a->progress_cb, a->progress_arg);
7449 if (err == NULL)
7450 got_fileindex_entry_stage_set(ie,
7451 GOT_FILEIDX_STAGE_NONE);
7452 break;
7453 case GOT_STATUS_DELETE:
7454 if (a->patch_cb) {
7455 int choice = GOT_PATCH_CHOICE_NONE;
7456 err = (*a->patch_cb)(&choice, a->patch_arg,
7457 staged_status, ie->path, NULL, 1, 1);
7458 if (err)
7459 break;
7460 if (choice == GOT_PATCH_CHOICE_NO)
7461 break;
7462 if (choice != GOT_PATCH_CHOICE_YES) {
7463 err = got_error(GOT_ERR_PATCH_CHOICE);
7464 break;
7467 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7468 err = get_file_status(&status, &sb, ie, ondisk_path,
7469 dirfd, de_name, a->repo);
7470 if (err)
7471 break;
7472 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7473 break;
7475 done:
7476 free(ondisk_path);
7477 if (path_unstaged_content &&
7478 unlink(path_unstaged_content) == -1 && err == NULL)
7479 err = got_error_from_errno2("unlink", path_unstaged_content);
7480 if (path_new_staged_content &&
7481 unlink(path_new_staged_content) == -1 && err == NULL)
7482 err = got_error_from_errno2("unlink", path_new_staged_content);
7483 free(path_unstaged_content);
7484 free(path_new_staged_content);
7485 if (blob_base)
7486 got_object_blob_close(blob_base);
7487 if (blob_staged)
7488 got_object_blob_close(blob_staged);
7489 free(id_str);
7490 free(label_orig);
7491 return err;
7494 const struct got_error *
7495 got_worktree_unstage(struct got_worktree *worktree,
7496 struct got_pathlist_head *paths,
7497 got_worktree_checkout_cb progress_cb, void *progress_arg,
7498 got_worktree_patch_cb patch_cb, void *patch_arg,
7499 struct got_repository *repo)
7501 const struct got_error *err = NULL, *sync_err, *unlockerr;
7502 struct got_pathlist_entry *pe;
7503 struct got_fileindex *fileindex = NULL;
7504 char *fileindex_path = NULL;
7505 struct unstage_path_arg upa;
7507 err = lock_worktree(worktree, LOCK_EX);
7508 if (err)
7509 return err;
7511 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7512 if (err)
7513 goto done;
7515 upa.worktree = worktree;
7516 upa.fileindex = fileindex;
7517 upa.repo = repo;
7518 upa.progress_cb = progress_cb;
7519 upa.progress_arg = progress_arg;
7520 upa.patch_cb = patch_cb;
7521 upa.patch_arg = patch_arg;
7522 TAILQ_FOREACH(pe, paths, entry) {
7523 err = worktree_status(worktree, pe->path, fileindex, repo,
7524 unstage_path, &upa, NULL, NULL, 0, 0);
7525 if (err)
7526 goto done;
7529 sync_err = sync_fileindex(fileindex, fileindex_path);
7530 if (sync_err && err == NULL)
7531 err = sync_err;
7532 done:
7533 free(fileindex_path);
7534 if (fileindex)
7535 got_fileindex_free(fileindex);
7536 unlockerr = lock_worktree(worktree, LOCK_SH);
7537 if (unlockerr && err == NULL)
7538 err = unlockerr;
7539 return err;