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 = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
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(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;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 /*
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
837 */
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
854 if (parent == NULL)
855 return got_error_from_errno2("dirname", ondisk_path);
857 free(base_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
860 base_path = NULL;
861 goto done;
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 if (err)
866 goto done;
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 blob_deriv);
869 if (err)
870 goto done;
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (err)
874 goto done;
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
878 goto done;
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
884 done:
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
887 free(base_path);
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
892 free(id_str);
893 free(label_deriv);
894 return err;
897 static const struct got_error *
898 update_blob_fileindex_entry(struct got_worktree *worktree,
899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 int update_timestamps)
903 const struct got_error *err = NULL;
905 if (ie == NULL)
906 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
907 if (ie)
908 err = got_fileindex_entry_update(ie, ondisk_path,
909 blob->id.sha1, worktree->base_commit_id->sha1,
910 update_timestamps);
911 else {
912 struct got_fileindex_entry *new_ie;
913 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
914 path, blob->id.sha1, worktree->base_commit_id->sha1);
915 if (!err)
916 err = got_fileindex_entry_add(fileindex, new_ie);
918 return err;
921 static mode_t
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
926 if (executable) {
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
929 xbits |= S_IXGRP;
930 if (st_mode & S_IROTH)
931 xbits |= S_IXOTH;
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 static const struct got_error *
939 install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 const char *path, mode_t te_mode, mode_t st_mode,
941 struct got_blob_object *blob, int restoring_missing_file,
942 int reverting_versioned_file, struct got_repository *repo,
943 got_worktree_checkout_cb progress_cb, void *progress_arg)
945 const struct got_error *err = NULL;
946 int fd = -1;
947 size_t len, hdrlen;
948 int update = 0;
949 char *tmppath = NULL;
951 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 GOT_DEFAULT_FILE_MODE);
953 if (fd == -1) {
954 if (errno == ENOENT) {
955 char *parent = dirname(path);
956 if (parent == NULL)
957 return got_error_from_errno2("dirname", path);
958 err = add_dir_on_disk(worktree, parent);
959 if (err)
960 return err;
961 fd = open(ondisk_path,
962 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 GOT_DEFAULT_FILE_MODE);
964 if (fd == -1)
965 return got_error_from_errno2("open",
966 ondisk_path);
967 } else if (errno == EEXIST) {
968 if (!S_ISREG(st_mode)) {
969 /* TODO file is obstructed; do something */
970 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
971 goto done;
972 } else {
973 err = got_opentemp_named_fd(&tmppath, &fd,
974 ondisk_path);
975 if (err)
976 goto done;
977 update = 1;
979 } else
980 return got_error_from_errno2("open", ondisk_path);
983 if (restoring_missing_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
985 else if (reverting_versioned_file)
986 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
987 else
988 err = (*progress_cb)(progress_arg,
989 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
990 if (err)
991 goto done;
993 hdrlen = got_object_blob_get_hdrlen(blob);
994 do {
995 const uint8_t *buf = got_object_blob_get_read_buf(blob);
996 err = got_object_blob_read_block(&len, blob);
997 if (err)
998 break;
999 if (len > 0) {
1000 /* Skip blob object header first time around. */
1001 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1002 if (outlen == -1) {
1003 err = got_error_from_errno("write");
1004 goto done;
1005 } else if (outlen != len - hdrlen) {
1006 err = got_error(GOT_ERR_IO);
1007 goto done;
1009 hdrlen = 0;
1011 } while (len != 0);
1013 if (fsync(fd) != 0) {
1014 err = got_error_from_errno("fsync");
1015 goto done;
1018 if (update) {
1019 if (rename(tmppath, ondisk_path) != 0) {
1020 err = got_error_from_errno3("rename", tmppath,
1021 ondisk_path);
1022 unlink(tmppath);
1023 goto done;
1027 if (chmod(ondisk_path,
1028 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1029 err = got_error_from_errno2("chmod", ondisk_path);
1030 goto done;
1033 done:
1034 if (fd != -1 && close(fd) != 0 && err == NULL)
1035 err = got_error_from_errno("close");
1036 free(tmppath);
1037 return err;
1040 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1041 static const struct got_error *
1042 get_modified_file_content_status(unsigned char *status, FILE *f)
1044 const struct got_error *err = NULL;
1045 const char *markers[3] = {
1046 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1047 GOT_DIFF_CONFLICT_MARKER_SEP,
1048 GOT_DIFF_CONFLICT_MARKER_END
1050 int i = 0;
1051 char *line;
1052 size_t len;
1053 const char delim[3] = {'\0', '\0', '\0'};
1055 while (*status == GOT_STATUS_MODIFY) {
1056 line = fparseln(f, &len, NULL, delim, 0);
1057 if (line == NULL) {
1058 if (feof(f))
1059 break;
1060 err = got_ferror(f, GOT_ERR_IO);
1061 break;
1064 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1065 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1066 == 0)
1067 *status = GOT_STATUS_CONFLICT;
1068 else
1069 i++;
1073 return err;
1076 static int
1077 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1079 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1080 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1083 static int
1084 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1086 return !(ie->ctime_sec == sb->st_ctime &&
1087 ie->ctime_nsec == sb->st_ctimensec &&
1088 ie->mtime_sec == sb->st_mtime &&
1089 ie->mtime_nsec == sb->st_mtimensec &&
1090 ie->size == (sb->st_size & 0xffffffff) &&
1091 !xbit_differs(ie, sb->st_mode));
1094 static unsigned char
1095 get_staged_status(struct got_fileindex_entry *ie)
1097 switch (got_fileindex_entry_stage_get(ie)) {
1098 case GOT_FILEIDX_STAGE_ADD:
1099 return GOT_STATUS_ADD;
1100 case GOT_FILEIDX_STAGE_DELETE:
1101 return GOT_STATUS_DELETE;
1102 case GOT_FILEIDX_STAGE_MODIFY:
1103 return GOT_STATUS_MODIFY;
1104 default:
1105 return GOT_STATUS_NO_CHANGE;
1109 static const struct got_error *
1110 get_file_status(unsigned char *status, struct stat *sb,
1111 struct got_fileindex_entry *ie, const char *abspath,
1112 int dirfd, const char *de_name, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_object_id id;
1116 size_t hdrlen;
1117 int fd = -1;
1118 FILE *f = NULL;
1119 uint8_t fbuf[8192];
1120 struct got_blob_object *blob = NULL;
1121 size_t flen, blen;
1122 unsigned char staged_status = get_staged_status(ie);
1124 *status = GOT_STATUS_NO_CHANGE;
1127 * Whenever the caller provides a directory descriptor and a
1128 * directory entry name for the file, use them! This prevents
1129 * race conditions if filesystem paths change beneath our feet.
1131 if (dirfd != -1) {
1132 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1133 if (errno == ENOENT) {
1134 if (got_fileindex_entry_has_file_on_disk(ie))
1135 *status = GOT_STATUS_MISSING;
1136 else
1137 *status = GOT_STATUS_DELETE;
1138 goto done;
1140 err = got_error_from_errno2("fstatat", abspath);
1141 goto done;
1143 } else {
1144 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1145 if (fd == -1 && errno != ENOENT)
1146 return got_error_from_errno2("open", abspath);
1147 if (fd == -1 || fstat(fd, sb) == -1) {
1148 if (errno == ENOENT) {
1149 if (got_fileindex_entry_has_file_on_disk(ie))
1150 *status = GOT_STATUS_MISSING;
1151 else
1152 *status = GOT_STATUS_DELETE;
1153 goto done;
1155 err = got_error_from_errno2("fstat", abspath);
1156 goto done;
1160 if (!S_ISREG(sb->st_mode)) {
1161 *status = GOT_STATUS_OBSTRUCTED;
1162 goto done;
1165 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1166 *status = GOT_STATUS_DELETE;
1167 goto done;
1168 } else if (!got_fileindex_entry_has_blob(ie) &&
1169 staged_status != GOT_STATUS_ADD) {
1170 *status = GOT_STATUS_ADD;
1171 goto done;
1174 if (!stat_info_differs(ie, sb))
1175 goto done;
1177 if (staged_status == GOT_STATUS_MODIFY ||
1178 staged_status == GOT_STATUS_ADD)
1179 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1180 else
1181 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1183 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1184 if (err)
1185 goto done;
1187 if (dirfd != -1) {
1188 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1189 if (fd == -1) {
1190 err = got_error_from_errno2("openat", abspath);
1191 goto done;
1195 f = fdopen(fd, "r");
1196 if (f == NULL) {
1197 err = got_error_from_errno2("fdopen", abspath);
1198 goto done;
1200 fd = -1;
1201 hdrlen = got_object_blob_get_hdrlen(blob);
1202 for (;;) {
1203 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1204 err = got_object_blob_read_block(&blen, blob);
1205 if (err)
1206 goto done;
1207 /* Skip length of blob object header first time around. */
1208 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1209 if (flen == 0 && ferror(f)) {
1210 err = got_error_from_errno("fread");
1211 goto done;
1213 if (blen == 0) {
1214 if (flen != 0)
1215 *status = GOT_STATUS_MODIFY;
1216 break;
1217 } else if (flen == 0) {
1218 if (blen != 0)
1219 *status = GOT_STATUS_MODIFY;
1220 break;
1221 } else if (blen - hdrlen == flen) {
1222 /* Skip blob object header first time around. */
1223 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1224 *status = GOT_STATUS_MODIFY;
1225 break;
1227 } else {
1228 *status = GOT_STATUS_MODIFY;
1229 break;
1231 hdrlen = 0;
1234 if (*status == GOT_STATUS_MODIFY) {
1235 rewind(f);
1236 err = get_modified_file_content_status(status, f);
1237 } else if (xbit_differs(ie, sb->st_mode))
1238 *status = GOT_STATUS_MODE_CHANGE;
1239 done:
1240 if (blob)
1241 got_object_blob_close(blob);
1242 if (f != NULL && fclose(f) == EOF && err == NULL)
1243 err = got_error_from_errno2("fclose", abspath);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", abspath);
1246 return err;
1250 * Update timestamps in the file index if a file is unmodified and
1251 * we had to run a full content comparison to find out.
1253 static const struct got_error *
1254 sync_timestamps(char *ondisk_path, unsigned char status,
1255 struct got_fileindex_entry *ie, struct stat *sb)
1257 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1258 return got_fileindex_entry_update(ie, ondisk_path,
1259 ie->blob_sha1, ie->commit_sha1, 1);
1261 return NULL;
1264 static const struct got_error *
1265 update_blob(struct got_worktree *worktree,
1266 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1267 struct got_tree_entry *te, const char *path,
1268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1269 void *progress_arg)
1271 const struct got_error *err = NULL;
1272 struct got_blob_object *blob = NULL;
1273 char *ondisk_path;
1274 unsigned char status = GOT_STATUS_NO_CHANGE;
1275 struct stat sb;
1277 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1278 return got_error_from_errno("asprintf");
1280 if (ie) {
1281 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1282 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1283 goto done;
1285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1286 repo);
1287 if (err)
1288 goto done;
1289 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1290 sb.st_mode = got_fileindex_perms_to_st(ie);
1291 } else
1292 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1294 if (status == GOT_STATUS_OBSTRUCTED) {
1295 err = (*progress_cb)(progress_arg, status, path);
1296 goto done;
1299 if (ie && status != GOT_STATUS_MISSING &&
1300 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1301 if (got_fileindex_entry_has_commit(ie) &&
1302 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1303 SHA1_DIGEST_LENGTH) == 0) {
1304 err = sync_timestamps(ondisk_path, status, ie, &sb);
1305 if (err)
1306 goto done;
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1308 path);
1309 goto done;
1311 if (got_fileindex_entry_has_blob(ie) &&
1312 memcmp(ie->blob_sha1, te->id.sha1,
1313 SHA1_DIGEST_LENGTH) == 0) {
1314 err = sync_timestamps(ondisk_path, status, ie, &sb);
1315 goto done;
1319 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1320 if (err)
1321 goto done;
1323 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1324 int update_timestamps;
1325 struct got_blob_object *blob2 = NULL;
1326 char *label_orig = NULL;
1327 if (got_fileindex_entry_has_blob(ie)) {
1328 struct got_object_id id2;
1329 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1330 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1331 if (err)
1332 goto done;
1334 if (got_fileindex_entry_has_commit(ie)) {
1335 char id_str[SHA1_DIGEST_STRING_LENGTH];
1336 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1337 sizeof(id_str)) == NULL) {
1338 err = got_error_path(id_str,
1339 GOT_ERR_BAD_OBJ_ID_STR);
1340 goto done;
1342 if (asprintf(&label_orig, "%s: commit %s",
1343 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1344 err = got_error_from_errno("asprintf");
1345 goto done;
1348 err = merge_blob(&update_timestamps, worktree, blob2,
1349 ondisk_path, path, sb.st_mode, label_orig, blob,
1350 worktree->base_commit_id, repo,
1351 progress_cb, progress_arg);
1352 free(label_orig);
1353 if (blob2)
1354 got_object_blob_close(blob2);
1355 if (err)
1356 goto done;
1358 * Do not update timestamps of files with local changes.
1359 * Otherwise, a future status walk would treat them as
1360 * unmodified files again.
1362 err = got_fileindex_entry_update(ie, ondisk_path,
1363 blob->id.sha1, worktree->base_commit_id->sha1,
1364 update_timestamps);
1365 } else if (status == GOT_STATUS_MODE_CHANGE) {
1366 err = got_fileindex_entry_update(ie, ondisk_path,
1367 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1368 } else if (status == GOT_STATUS_DELETE) {
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1370 if (err)
1371 goto done;
1372 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1373 ondisk_path, path, blob, 0);
1374 if (err)
1375 goto done;
1376 } else {
1377 err = install_blob(worktree, ondisk_path, path, te->mode,
1378 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1379 repo, progress_cb, progress_arg);
1380 if (err)
1381 goto done;
1382 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1383 ondisk_path, path, blob, 1);
1384 if (err)
1385 goto done;
1387 got_object_blob_close(blob);
1388 done:
1389 free(ondisk_path);
1390 return err;
1393 static const struct got_error *
1394 remove_ondisk_file(const char *root_path, const char *path)
1396 const struct got_error *err = NULL;
1397 char *ondisk_path = NULL;
1399 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1400 return got_error_from_errno("asprintf");
1402 if (unlink(ondisk_path) == -1) {
1403 if (errno != ENOENT)
1404 err = got_error_from_errno2("unlink", ondisk_path);
1405 } else {
1406 char *parent = dirname(ondisk_path);
1407 while (parent && strcmp(parent, root_path) != 0) {
1408 if (rmdir(parent) == -1) {
1409 if (errno != ENOTEMPTY)
1410 err = got_error_from_errno2("rmdir",
1411 parent);
1412 break;
1414 parent = dirname(parent);
1417 free(ondisk_path);
1418 return err;
1421 static const struct got_error *
1422 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1423 struct got_fileindex_entry *ie, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1427 unsigned char status;
1428 struct stat sb;
1429 char *ondisk_path;
1431 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1432 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1434 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1435 == -1)
1436 return got_error_from_errno("asprintf");
1438 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1439 if (err)
1440 return err;
1442 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1443 status == GOT_STATUS_ADD) {
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1445 if (err)
1446 return err;
1448 * Preserve the working file and change the deleted blob's
1449 * entry into a schedule-add entry.
1451 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1452 0);
1453 if (err)
1454 return err;
1455 } else {
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1457 if (err)
1458 return err;
1459 if (status == GOT_STATUS_NO_CHANGE) {
1460 err = remove_ondisk_file(worktree->root_path, ie->path);
1461 if (err)
1462 return err;
1464 got_fileindex_entry_remove(fileindex, ie);
1467 return err;
1470 struct diff_cb_arg {
1471 struct got_fileindex *fileindex;
1472 struct got_worktree *worktree;
1473 struct got_repository *repo;
1474 got_worktree_checkout_cb progress_cb;
1475 void *progress_arg;
1476 got_cancel_cb cancel_cb;
1477 void *cancel_arg;
1480 static const struct got_error *
1481 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1482 struct got_tree_entry *te, const char *parent_path)
1484 struct diff_cb_arg *a = arg;
1486 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1487 return got_error(GOT_ERR_CANCELLED);
1489 return update_blob(a->worktree, a->fileindex, ie, te,
1490 ie->path, a->repo, a->progress_cb, a->progress_arg);
1493 static const struct got_error *
1494 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1496 struct diff_cb_arg *a = arg;
1498 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 return got_error(GOT_ERR_CANCELLED);
1501 return delete_blob(a->worktree, a->fileindex, ie,
1502 a->repo, a->progress_cb, a->progress_arg);
1505 static const struct got_error *
1506 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1508 struct diff_cb_arg *a = arg;
1509 const struct got_error *err;
1510 char *path;
1512 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1513 return got_error(GOT_ERR_CANCELLED);
1515 if (got_object_tree_entry_is_submodule(te))
1516 return NULL;
1518 if (asprintf(&path, "%s%s%s", parent_path,
1519 parent_path[0] ? "/" : "", te->name)
1520 == -1)
1521 return got_error_from_errno("asprintf");
1523 if (S_ISDIR(te->mode))
1524 err = add_dir_on_disk(a->worktree, path);
1525 else
1526 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1527 a->repo, a->progress_cb, a->progress_arg);
1529 free(path);
1530 return err;
1533 static const struct got_error *
1534 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1536 const struct got_error *err = NULL;
1537 char *uuidstr = NULL;
1538 uint32_t uuid_status;
1540 *refname = NULL;
1542 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1543 if (uuid_status != uuid_s_ok)
1544 return got_error_uuid(uuid_status, "uuid_to_string");
1546 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1547 == -1) {
1548 err = got_error_from_errno("asprintf");
1549 *refname = NULL;
1551 free(uuidstr);
1552 return err;
1555 const struct got_error *
1556 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1558 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1561 static const struct got_error *
1562 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1564 return get_ref_name(refname, worktree,
1565 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1568 static const struct got_error *
1569 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1581 static const struct got_error *
1582 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree,
1585 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1588 static const struct got_error *
1589 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1591 return get_ref_name(refname, worktree,
1592 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1595 static const struct got_error *
1596 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1598 return get_ref_name(refname, worktree,
1599 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1602 static const struct got_error *
1603 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1605 return get_ref_name(refname, worktree,
1606 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1609 static const struct got_error *
1610 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1612 return get_ref_name(refname, worktree,
1613 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1616 const struct got_error *
1617 got_worktree_get_histedit_script_path(char **path,
1618 struct got_worktree *worktree)
1620 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1621 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1622 *path = NULL;
1623 return got_error_from_errno("asprintf");
1625 return NULL;
1629 * Prevent Git's garbage collector from deleting our base commit by
1630 * setting a reference to our base commit's ID.
1632 static const struct got_error *
1633 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1635 const struct got_error *err = NULL;
1636 struct got_reference *ref = NULL;
1637 char *refname;
1639 err = got_worktree_get_base_ref_name(&refname, worktree);
1640 if (err)
1641 return err;
1643 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1644 if (err)
1645 goto done;
1647 err = got_ref_write(ref, repo);
1648 done:
1649 free(refname);
1650 if (ref)
1651 got_ref_close(ref);
1652 return err;
1655 static const struct got_error *
1656 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1658 const struct got_error *err = NULL;
1660 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1661 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 *fileindex_path = NULL;
1665 return err;
1669 static const struct got_error *
1670 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1671 struct got_worktree *worktree)
1673 const struct got_error *err = NULL;
1674 FILE *index = NULL;
1676 *fileindex_path = NULL;
1677 *fileindex = got_fileindex_alloc();
1678 if (*fileindex == NULL)
1679 return got_error_from_errno("got_fileindex_alloc");
1681 err = get_fileindex_path(fileindex_path, worktree);
1682 if (err)
1683 goto done;
1685 index = fopen(*fileindex_path, "rb");
1686 if (index == NULL) {
1687 if (errno != ENOENT)
1688 err = got_error_from_errno2("fopen", *fileindex_path);
1689 } else {
1690 err = got_fileindex_read(*fileindex, index);
1691 if (fclose(index) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1694 done:
1695 if (err) {
1696 free(*fileindex_path);
1697 *fileindex_path = NULL;
1698 got_fileindex_free(*fileindex);
1699 *fileindex = NULL;
1701 return err;
1704 struct bump_base_commit_id_arg {
1705 struct got_object_id *base_commit_id;
1706 const char *path;
1707 size_t path_len;
1708 const char *entry_name;
1709 got_worktree_checkout_cb progress_cb;
1710 void *progress_arg;
1713 /* Bump base commit ID of all files within an updated part of the work tree. */
1714 static const struct got_error *
1715 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1717 const struct got_error *err;
1718 struct bump_base_commit_id_arg *a = arg;
1720 if (a->entry_name) {
1721 if (strcmp(ie->path, a->path) != 0)
1722 return NULL;
1723 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1724 return NULL;
1726 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1727 SHA1_DIGEST_LENGTH) == 0)
1728 return NULL;
1730 if (a->progress_cb) {
1731 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1732 ie->path);
1733 if (err)
1734 return err;
1736 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1737 return NULL;
1740 static const struct got_error *
1741 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1743 const struct got_error *err = NULL;
1744 char *new_fileindex_path = NULL;
1745 FILE *new_index = NULL;
1746 struct timespec timeout;
1748 err = got_opentemp_named(&new_fileindex_path, &new_index,
1749 fileindex_path);
1750 if (err)
1751 goto done;
1753 err = got_fileindex_write(fileindex, new_index);
1754 if (err)
1755 goto done;
1757 if (rename(new_fileindex_path, fileindex_path) != 0) {
1758 err = got_error_from_errno3("rename", new_fileindex_path,
1759 fileindex_path);
1760 unlink(new_fileindex_path);
1764 * Sleep for a short amount of time to ensure that files modified after
1765 * this program exits have a different time stamp from the one which
1766 * was recorded in the file index.
1768 timeout.tv_sec = 0;
1769 timeout.tv_nsec = 1;
1770 nanosleep(&timeout, NULL);
1771 done:
1772 if (new_index)
1773 fclose(new_index);
1774 free(new_fileindex_path);
1775 return err;
1778 static const struct got_error *
1779 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1780 struct got_object_id **tree_id, const char *wt_relpath,
1781 struct got_worktree *worktree, struct got_repository *repo)
1783 const struct got_error *err = NULL;
1784 struct got_object_id *id = NULL;
1785 char *in_repo_path = NULL;
1786 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1788 *entry_type = GOT_OBJ_TYPE_ANY;
1789 *tree_relpath = NULL;
1790 *tree_id = NULL;
1792 if (wt_relpath[0] == '\0') {
1793 /* Check out all files within the work tree. */
1794 *entry_type = GOT_OBJ_TYPE_TREE;
1795 *tree_relpath = strdup("");
1796 if (*tree_relpath == NULL) {
1797 err = got_error_from_errno("strdup");
1798 goto done;
1800 err = got_object_id_by_path(tree_id, repo,
1801 worktree->base_commit_id, worktree->path_prefix);
1802 if (err)
1803 goto done;
1804 return NULL;
1807 /* Check out a subset of files in the work tree. */
1809 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1810 is_root_wt ? "" : "/", wt_relpath) == -1) {
1811 err = got_error_from_errno("asprintf");
1812 goto done;
1815 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1816 in_repo_path);
1817 if (err)
1818 goto done;
1820 free(in_repo_path);
1821 in_repo_path = NULL;
1823 err = got_object_get_type(entry_type, repo, id);
1824 if (err)
1825 goto done;
1827 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1828 /* Check out a single file. */
1829 if (strchr(wt_relpath, '/') == NULL) {
1830 /* Check out a single file in work tree's root dir. */
1831 in_repo_path = strdup(worktree->path_prefix);
1832 if (in_repo_path == NULL) {
1833 err = got_error_from_errno("strdup");
1834 goto done;
1836 *tree_relpath = strdup("");
1837 if (*tree_relpath == NULL) {
1838 err = got_error_from_errno("strdup");
1839 goto done;
1841 } else {
1842 /* Check out a single file in a subdirectory. */
1843 err = got_path_dirname(tree_relpath, wt_relpath);
1844 if (err)
1845 return err;
1846 if (asprintf(&in_repo_path, "%s%s%s",
1847 worktree->path_prefix, is_root_wt ? "" : "/",
1848 *tree_relpath) == -1) {
1849 err = got_error_from_errno("asprintf");
1850 goto done;
1853 err = got_object_id_by_path(tree_id, repo,
1854 worktree->base_commit_id, in_repo_path);
1855 } else {
1856 /* Check out all files within a subdirectory. */
1857 *tree_id = got_object_id_dup(id);
1858 if (*tree_id == NULL) {
1859 err = got_error_from_errno("got_object_id_dup");
1860 goto done;
1862 *tree_relpath = strdup(wt_relpath);
1863 if (*tree_relpath == NULL) {
1864 err = got_error_from_errno("strdup");
1865 goto done;
1868 done:
1869 free(id);
1870 free(in_repo_path);
1871 if (err) {
1872 *entry_type = GOT_OBJ_TYPE_ANY;
1873 free(*tree_relpath);
1874 *tree_relpath = NULL;
1875 free(*tree_id);
1876 *tree_id = NULL;
1878 return err;
1881 static const struct got_error *
1882 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1883 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1884 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1885 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1887 const struct got_error *err = NULL;
1888 struct got_commit_object *commit = NULL;
1889 struct got_tree_object *tree = NULL;
1890 struct got_fileindex_diff_tree_cb diff_cb;
1891 struct diff_cb_arg arg;
1893 err = ref_base_commit(worktree, repo);
1894 if (err) {
1895 if (!(err->code == GOT_ERR_ERRNO &&
1896 (errno == EACCES || errno == EROFS)))
1897 goto done;
1898 err = (*progress_cb)(progress_arg,
1899 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1900 if (err)
1901 return err;
1904 err = got_object_open_as_commit(&commit, repo,
1905 worktree->base_commit_id);
1906 if (err)
1907 goto done;
1909 err = got_object_open_as_tree(&tree, repo, tree_id);
1910 if (err)
1911 goto done;
1913 if (entry_name &&
1914 got_object_tree_find_entry(tree, entry_name) == NULL) {
1915 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1916 goto done;
1919 diff_cb.diff_old_new = diff_old_new;
1920 diff_cb.diff_old = diff_old;
1921 diff_cb.diff_new = diff_new;
1922 arg.fileindex = fileindex;
1923 arg.worktree = worktree;
1924 arg.repo = repo;
1925 arg.progress_cb = progress_cb;
1926 arg.progress_arg = progress_arg;
1927 arg.cancel_cb = cancel_cb;
1928 arg.cancel_arg = cancel_arg;
1929 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1930 entry_name, repo, &diff_cb, &arg);
1931 done:
1932 if (tree)
1933 got_object_tree_close(tree);
1934 if (commit)
1935 got_object_commit_close(commit);
1936 return err;
1939 const struct got_error *
1940 got_worktree_checkout_files(struct got_worktree *worktree,
1941 struct got_pathlist_head *paths, struct got_repository *repo,
1942 got_worktree_checkout_cb progress_cb, void *progress_arg,
1943 got_cancel_cb cancel_cb, void *cancel_arg)
1945 const struct got_error *err = NULL, *sync_err, *unlockerr;
1946 struct got_commit_object *commit = NULL;
1947 struct got_tree_object *tree = NULL;
1948 struct got_fileindex *fileindex = NULL;
1949 char *fileindex_path = NULL;
1950 struct got_pathlist_entry *pe;
1951 struct tree_path_data {
1952 SIMPLEQ_ENTRY(tree_path_data) entry;
1953 struct got_object_id *tree_id;
1954 int entry_type;
1955 char *relpath;
1956 char *entry_name;
1957 } *tpd = NULL;
1958 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1960 SIMPLEQ_INIT(&tree_paths);
1962 err = lock_worktree(worktree, LOCK_EX);
1963 if (err)
1964 return err;
1966 /* Map all specified paths to in-repository trees. */
1967 TAILQ_FOREACH(pe, paths, entry) {
1968 tpd = malloc(sizeof(*tpd));
1969 if (tpd == NULL) {
1970 err = got_error_from_errno("malloc");
1971 goto done;
1974 err = find_tree_entry_for_checkout(&tpd->entry_type,
1975 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1976 if (err) {
1977 free(tpd);
1978 goto done;
1981 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1982 err = got_path_basename(&tpd->entry_name, pe->path);
1983 if (err) {
1984 free(tpd->relpath);
1985 free(tpd->tree_id);
1986 free(tpd);
1987 goto done;
1989 } else
1990 tpd->entry_name = NULL;
1992 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1996 * Read the file index.
1997 * Checking out files is supposed to be an idempotent operation.
1998 * If the on-disk file index is incomplete we will try to complete it.
2000 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2001 if (err)
2002 goto done;
2004 tpd = SIMPLEQ_FIRST(&tree_paths);
2005 TAILQ_FOREACH(pe, paths, entry) {
2006 struct bump_base_commit_id_arg bbc_arg;
2008 err = checkout_files(worktree, fileindex, tpd->relpath,
2009 tpd->tree_id, tpd->entry_name, repo,
2010 progress_cb, progress_arg, cancel_cb, cancel_arg);
2011 if (err)
2012 break;
2014 bbc_arg.base_commit_id = worktree->base_commit_id;
2015 bbc_arg.entry_name = tpd->entry_name;
2016 bbc_arg.path = pe->path;
2017 bbc_arg.path_len = pe->path_len;
2018 bbc_arg.progress_cb = progress_cb;
2019 bbc_arg.progress_arg = progress_arg;
2020 err = got_fileindex_for_each_entry_safe(fileindex,
2021 bump_base_commit_id, &bbc_arg);
2022 if (err)
2023 break;
2025 tpd = SIMPLEQ_NEXT(tpd, entry);
2027 sync_err = sync_fileindex(fileindex, fileindex_path);
2028 if (sync_err && err == NULL)
2029 err = sync_err;
2030 done:
2031 free(fileindex_path);
2032 if (tree)
2033 got_object_tree_close(tree);
2034 if (commit)
2035 got_object_commit_close(commit);
2036 if (fileindex)
2037 got_fileindex_free(fileindex);
2038 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2039 tpd = SIMPLEQ_FIRST(&tree_paths);
2040 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2041 free(tpd->relpath);
2042 free(tpd->tree_id);
2043 free(tpd);
2045 unlockerr = lock_worktree(worktree, LOCK_SH);
2046 if (unlockerr && err == NULL)
2047 err = unlockerr;
2048 return err;
2051 struct merge_file_cb_arg {
2052 struct got_worktree *worktree;
2053 struct got_fileindex *fileindex;
2054 got_worktree_checkout_cb progress_cb;
2055 void *progress_arg;
2056 got_cancel_cb cancel_cb;
2057 void *cancel_arg;
2058 const char *label_orig;
2059 struct got_object_id *commit_id2;
2062 static const struct got_error *
2063 merge_file_cb(void *arg, struct got_blob_object *blob1,
2064 struct got_blob_object *blob2, struct got_object_id *id1,
2065 struct got_object_id *id2, const char *path1, const char *path2,
2066 mode_t mode1, mode_t mode2, struct got_repository *repo)
2068 static const struct got_error *err = NULL;
2069 struct merge_file_cb_arg *a = arg;
2070 struct got_fileindex_entry *ie;
2071 char *ondisk_path = NULL;
2072 struct stat sb;
2073 unsigned char status;
2074 int local_changes_subsumed;
2076 if (blob1 && blob2) {
2077 ie = got_fileindex_entry_get(a->fileindex, path2,
2078 strlen(path2));
2079 if (ie == NULL)
2080 return (*a->progress_cb)(a->progress_arg,
2081 GOT_STATUS_MISSING, path2);
2083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2084 path2) == -1)
2085 return got_error_from_errno("asprintf");
2087 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2088 repo);
2089 if (err)
2090 goto done;
2092 if (status == GOT_STATUS_DELETE) {
2093 err = (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MERGE, path2);
2095 goto done;
2097 if (status != GOT_STATUS_NO_CHANGE &&
2098 status != GOT_STATUS_MODIFY &&
2099 status != GOT_STATUS_CONFLICT &&
2100 status != GOT_STATUS_ADD) {
2101 err = (*a->progress_cb)(a->progress_arg, status, path2);
2102 goto done;
2105 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2106 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2107 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2108 } else if (blob1) {
2109 ie = got_fileindex_entry_get(a->fileindex, path1,
2110 strlen(path1));
2111 if (ie == NULL)
2112 return (*a->progress_cb)(a->progress_arg,
2113 GOT_STATUS_MISSING, path1);
2115 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2116 path1) == -1)
2117 return got_error_from_errno("asprintf");
2119 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2120 repo);
2121 if (err)
2122 goto done;
2124 switch (status) {
2125 case GOT_STATUS_NO_CHANGE:
2126 err = (*a->progress_cb)(a->progress_arg,
2127 GOT_STATUS_DELETE, path1);
2128 if (err)
2129 goto done;
2130 err = remove_ondisk_file(a->worktree->root_path, path1);
2131 if (err)
2132 goto done;
2133 if (ie)
2134 got_fileindex_entry_mark_deleted_from_disk(ie);
2135 break;
2136 case GOT_STATUS_DELETE:
2137 case GOT_STATUS_MISSING:
2138 err = (*a->progress_cb)(a->progress_arg,
2139 GOT_STATUS_DELETE, path1);
2140 if (err)
2141 goto done;
2142 if (ie)
2143 got_fileindex_entry_mark_deleted_from_disk(ie);
2144 break;
2145 case GOT_STATUS_ADD:
2146 case GOT_STATUS_MODIFY:
2147 case GOT_STATUS_CONFLICT:
2148 err = (*a->progress_cb)(a->progress_arg,
2149 GOT_STATUS_CANNOT_DELETE, path1);
2150 if (err)
2151 goto done;
2152 break;
2153 case GOT_STATUS_OBSTRUCTED:
2154 err = (*a->progress_cb)(a->progress_arg, status, path1);
2155 if (err)
2156 goto done;
2157 break;
2158 default:
2159 break;
2161 } else if (blob2) {
2162 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2163 path2) == -1)
2164 return got_error_from_errno("asprintf");
2165 ie = got_fileindex_entry_get(a->fileindex, path2,
2166 strlen(path2));
2167 if (ie) {
2168 err = get_file_status(&status, &sb, ie, ondisk_path,
2169 -1, NULL, repo);
2170 if (err)
2171 goto done;
2172 if (status != GOT_STATUS_NO_CHANGE &&
2173 status != GOT_STATUS_MODIFY &&
2174 status != GOT_STATUS_CONFLICT &&
2175 status != GOT_STATUS_ADD) {
2176 err = (*a->progress_cb)(a->progress_arg,
2177 status, path2);
2178 goto done;
2180 err = merge_blob(&local_changes_subsumed, a->worktree,
2181 NULL, ondisk_path, path2, sb.st_mode,
2182 a->label_orig, blob2, a->commit_id2, repo,
2183 a->progress_cb,
2184 a->progress_arg);
2185 if (status == GOT_STATUS_DELETE) {
2186 err = update_blob_fileindex_entry(a->worktree,
2187 a->fileindex, ie, ondisk_path, ie->path,
2188 blob2, 0);
2189 if (err)
2190 goto done;
2192 } else {
2193 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2194 err = install_blob(a->worktree, ondisk_path, path2,
2195 /* XXX get this from parent tree! */
2196 GOT_DEFAULT_FILE_MODE,
2197 sb.st_mode, blob2, 0, 0, repo,
2198 a->progress_cb, a->progress_arg);
2199 if (err)
2200 goto done;
2201 err = got_fileindex_entry_alloc(&ie,
2202 ondisk_path, path2, NULL, NULL);
2203 if (err)
2204 goto done;
2205 err = got_fileindex_entry_add(a->fileindex, ie);
2206 if (err) {
2207 got_fileindex_entry_free(ie);
2208 goto done;
2212 done:
2213 free(ondisk_path);
2214 return err;
2217 struct check_merge_ok_arg {
2218 struct got_worktree *worktree;
2219 struct got_repository *repo;
2222 static const struct got_error *
2223 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2225 const struct got_error *err = NULL;
2226 struct check_merge_ok_arg *a = arg;
2227 unsigned char status;
2228 struct stat sb;
2229 char *ondisk_path;
2231 /* Reject merges into a work tree with mixed base commits. */
2232 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2233 SHA1_DIGEST_LENGTH))
2234 return got_error(GOT_ERR_MIXED_COMMITS);
2236 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2237 == -1)
2238 return got_error_from_errno("asprintf");
2240 /* Reject merges into a work tree with conflicted files. */
2241 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2242 if (err)
2243 return err;
2244 if (status == GOT_STATUS_CONFLICT)
2245 return got_error(GOT_ERR_CONFLICTS);
2247 return NULL;
2250 static const struct got_error *
2251 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2252 const char *fileindex_path, struct got_object_id *commit_id1,
2253 struct got_object_id *commit_id2, struct got_repository *repo,
2254 got_worktree_checkout_cb progress_cb, void *progress_arg,
2255 got_cancel_cb cancel_cb, void *cancel_arg)
2257 const struct got_error *err = NULL, *sync_err;
2258 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2259 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2260 struct merge_file_cb_arg arg;
2261 char *label_orig = NULL;
2263 if (commit_id1) {
2264 char *id_str;
2266 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2267 worktree->path_prefix);
2268 if (err)
2269 goto done;
2271 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2272 if (err)
2273 goto done;
2275 err = got_object_id_str(&id_str, commit_id1);
2276 if (err)
2277 goto done;
2279 if (asprintf(&label_orig, "%s: commit %s",
2280 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2281 err = got_error_from_errno("asprintf");
2282 free(id_str);
2283 goto done;
2285 free(id_str);
2288 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2289 worktree->path_prefix);
2290 if (err)
2291 goto done;
2293 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2294 if (err)
2295 goto done;
2297 arg.worktree = worktree;
2298 arg.fileindex = fileindex;
2299 arg.progress_cb = progress_cb;
2300 arg.progress_arg = progress_arg;
2301 arg.cancel_cb = cancel_cb;
2302 arg.cancel_arg = cancel_arg;
2303 arg.label_orig = label_orig;
2304 arg.commit_id2 = commit_id2;
2305 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2306 sync_err = sync_fileindex(fileindex, fileindex_path);
2307 if (sync_err && err == NULL)
2308 err = sync_err;
2309 done:
2310 if (tree1)
2311 got_object_tree_close(tree1);
2312 if (tree2)
2313 got_object_tree_close(tree2);
2314 free(label_orig);
2315 return err;
2318 const struct got_error *
2319 got_worktree_merge_files(struct got_worktree *worktree,
2320 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2321 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2322 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2324 const struct got_error *err, *unlockerr;
2325 char *fileindex_path = NULL;
2326 struct got_fileindex *fileindex = NULL;
2327 struct check_merge_ok_arg mok_arg;
2329 err = lock_worktree(worktree, LOCK_EX);
2330 if (err)
2331 return err;
2333 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2334 if (err)
2335 goto done;
2337 mok_arg.worktree = worktree;
2338 mok_arg.repo = repo;
2339 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2340 &mok_arg);
2341 if (err)
2342 goto done;
2344 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2345 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2346 done:
2347 if (fileindex)
2348 got_fileindex_free(fileindex);
2349 free(fileindex_path);
2350 unlockerr = lock_worktree(worktree, LOCK_SH);
2351 if (unlockerr && err == NULL)
2352 err = unlockerr;
2353 return err;
2356 struct diff_dir_cb_arg {
2357 struct got_fileindex *fileindex;
2358 struct got_worktree *worktree;
2359 const char *status_path;
2360 size_t status_path_len;
2361 struct got_repository *repo;
2362 got_worktree_status_cb status_cb;
2363 void *status_arg;
2364 got_cancel_cb cancel_cb;
2365 void *cancel_arg;
2366 /* A pathlist containing per-directory pathlists of ignore patterns. */
2367 struct got_pathlist_head ignores;
2368 int report_unchanged;
2371 static const struct got_error *
2372 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2373 int dirfd, const char *de_name,
2374 got_worktree_status_cb status_cb, void *status_arg,
2375 struct got_repository *repo, int report_unchanged)
2377 const struct got_error *err = NULL;
2378 unsigned char status = GOT_STATUS_NO_CHANGE;
2379 unsigned char staged_status = get_staged_status(ie);
2380 struct stat sb;
2381 struct got_object_id blob_id, commit_id, staged_blob_id;
2382 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2383 struct got_object_id *staged_blob_idp = NULL;
2385 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2386 if (err)
2387 return err;
2389 if (status == GOT_STATUS_NO_CHANGE &&
2390 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2391 return NULL;
2393 if (got_fileindex_entry_has_blob(ie)) {
2394 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2395 blob_idp = &blob_id;
2397 if (got_fileindex_entry_has_commit(ie)) {
2398 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2399 commit_idp = &commit_id;
2401 if (staged_status == GOT_STATUS_ADD ||
2402 staged_status == GOT_STATUS_MODIFY) {
2403 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2404 SHA1_DIGEST_LENGTH);
2405 staged_blob_idp = &staged_blob_id;
2408 return (*status_cb)(status_arg, status, staged_status,
2409 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2412 static const struct got_error *
2413 status_old_new(void *arg, struct got_fileindex_entry *ie,
2414 struct dirent *de, const char *parent_path, int dirfd)
2416 const struct got_error *err = NULL;
2417 struct diff_dir_cb_arg *a = arg;
2418 char *abspath;
2420 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2421 return got_error(GOT_ERR_CANCELLED);
2423 if (got_path_cmp(parent_path, a->status_path,
2424 strlen(parent_path), a->status_path_len) != 0 &&
2425 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2426 return NULL;
2428 if (parent_path[0]) {
2429 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2430 parent_path, de->d_name) == -1)
2431 return got_error_from_errno("asprintf");
2432 } else {
2433 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2434 de->d_name) == -1)
2435 return got_error_from_errno("asprintf");
2438 err = report_file_status(ie, abspath, dirfd, de->d_name,
2439 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2440 free(abspath);
2441 return err;
2444 static const struct got_error *
2445 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2447 struct diff_dir_cb_arg *a = arg;
2448 struct got_object_id blob_id, commit_id;
2449 unsigned char status;
2451 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2452 return got_error(GOT_ERR_CANCELLED);
2454 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2455 return NULL;
2457 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2459 if (got_fileindex_entry_has_file_on_disk(ie))
2460 status = GOT_STATUS_MISSING;
2461 else
2462 status = GOT_STATUS_DELETE;
2463 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2464 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2467 void
2468 free_ignorelist(struct got_pathlist_head *ignorelist)
2470 struct got_pathlist_entry *pe;
2472 TAILQ_FOREACH(pe, ignorelist, entry)
2473 free((char *)pe->path);
2474 got_pathlist_free(ignorelist);
2477 void
2478 free_ignores(struct got_pathlist_head *ignores)
2480 struct got_pathlist_entry *pe;
2482 TAILQ_FOREACH(pe, ignores, entry) {
2483 struct got_pathlist_head *ignorelist = pe->data;
2484 free_ignorelist(ignorelist);
2485 free((char *)pe->path);
2487 got_pathlist_free(ignores);
2490 static const struct got_error *
2491 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2493 const struct got_error *err = NULL;
2494 struct got_pathlist_entry *pe = NULL;
2495 struct got_pathlist_head *ignorelist;
2496 char *line = NULL, *pattern, *dirpath = NULL;
2497 size_t linesize = 0;
2498 ssize_t linelen;
2500 ignorelist = calloc(1, sizeof(*ignorelist));
2501 if (ignorelist == NULL)
2502 return got_error_from_errno("calloc");
2503 TAILQ_INIT(ignorelist);
2505 while ((linelen = getline(&line, &linesize, f)) != -1) {
2506 if (linelen > 0 && line[linelen - 1] == '\n')
2507 line[linelen - 1] = '\0';
2509 /* Git's ignores may contain comments. */
2510 if (line[0] == '#')
2511 continue;
2513 /* Git's negated patterns are not (yet?) supported. */
2514 if (line[0] == '!')
2515 continue;
2517 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2518 line) == -1) {
2519 err = got_error_from_errno("asprintf");
2520 goto done;
2522 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2523 if (err)
2524 goto done;
2526 if (ferror(f)) {
2527 err = got_error_from_errno("getline");
2528 goto done;
2531 dirpath = strdup(path);
2532 if (dirpath == NULL) {
2533 err = got_error_from_errno("strdup");
2534 goto done;
2536 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2537 done:
2538 free(line);
2539 if (err || pe == NULL) {
2540 free(dirpath);
2541 free_ignorelist(ignorelist);
2543 return err;
2546 int
2547 match_ignores(struct got_pathlist_head *ignores, const char *path)
2549 struct got_pathlist_entry *pe;
2551 /* Handle patterns which match in all directories. */
2552 TAILQ_FOREACH(pe, ignores, entry) {
2553 struct got_pathlist_head *ignorelist = pe->data;
2554 struct got_pathlist_entry *pi;
2556 TAILQ_FOREACH(pi, ignorelist, entry) {
2557 const char *p, *pattern = pi->path;
2559 if (strncmp(pattern, "**/", 3) != 0)
2560 continue;
2561 pattern += 3;
2562 p = path;
2563 while (*p) {
2564 if (fnmatch(pattern, p,
2565 FNM_PATHNAME | FNM_LEADING_DIR)) {
2566 /* Retry in next directory. */
2567 while (*p && *p != '/')
2568 p++;
2569 while (*p == '/')
2570 p++;
2571 continue;
2573 return 1;
2579 * The ignores pathlist contains ignore lists from children before
2580 * parents, so we can find the most specific ignorelist by walking
2581 * ignores backwards.
2583 pe = TAILQ_LAST(ignores, got_pathlist_head);
2584 while (pe) {
2585 if (got_path_is_child(path, pe->path, pe->path_len)) {
2586 struct got_pathlist_head *ignorelist = pe->data;
2587 struct got_pathlist_entry *pi;
2588 TAILQ_FOREACH(pi, ignorelist, entry) {
2589 const char *pattern = pi->path;
2590 int flags = FNM_LEADING_DIR;
2591 if (strstr(pattern, "/**/") == NULL)
2592 flags |= FNM_PATHNAME;
2593 if (fnmatch(pattern, path, flags))
2594 continue;
2595 return 1;
2598 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2601 return 0;
2604 static const struct got_error *
2605 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2606 const char *path, int dirfd, const char *ignores_filename)
2608 const struct got_error *err = NULL;
2609 char *ignorespath;
2610 int fd = -1;
2611 FILE *ignoresfile = NULL;
2613 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2614 path[0] ? "/" : "", ignores_filename) == -1)
2615 return got_error_from_errno("asprintf");
2617 if (dirfd != -1) {
2618 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2619 if (fd == -1) {
2620 if (errno != ENOENT && errno != EACCES)
2621 err = got_error_from_errno2("openat",
2622 ignorespath);
2623 } else {
2624 ignoresfile = fdopen(fd, "r");
2625 if (ignoresfile == NULL)
2626 err = got_error_from_errno2("fdopen",
2627 ignorespath);
2628 else {
2629 fd = -1;
2630 err = read_ignores(ignores, path, ignoresfile);
2633 } else {
2634 ignoresfile = fopen(ignorespath, "r");
2635 if (ignoresfile == NULL) {
2636 if (errno != ENOENT && errno != EACCES)
2637 err = got_error_from_errno2("fopen",
2638 ignorespath);
2639 } else
2640 err = read_ignores(ignores, path, ignoresfile);
2643 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2644 err = got_error_from_errno2("fclose", path);
2645 if (fd != -1 && close(fd) == -1 && err == NULL)
2646 err = got_error_from_errno2("close", path);
2647 free(ignorespath);
2648 return err;
2651 static const struct got_error *
2652 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2654 const struct got_error *err = NULL;
2655 struct diff_dir_cb_arg *a = arg;
2656 char *path = NULL;
2658 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2659 return got_error(GOT_ERR_CANCELLED);
2661 /* XXX ignore symlinks for now */
2662 if (de->d_type == DT_LNK)
2663 return NULL;
2665 if (parent_path[0]) {
2666 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2667 return got_error_from_errno("asprintf");
2668 } else {
2669 path = de->d_name;
2672 if (de->d_type == DT_DIR) {
2673 int subdirfd = openat(dirfd, de->d_name,
2674 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2675 if (subdirfd == -1) {
2676 if (errno != ENOENT && errno != EACCES)
2677 err = got_error_from_errno2("openat", path);
2678 } else {
2679 err = add_ignores(&a->ignores, a->worktree->root_path,
2680 path, subdirfd, ".cvsignore");
2681 if (err == NULL)
2682 err = add_ignores(&a->ignores,
2683 a->worktree->root_path, path,
2684 subdirfd, ".gitignore");
2685 if (close(subdirfd) == -1 && err == NULL)
2686 err = got_error_from_errno2("close", path);
2688 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2689 && !match_ignores(&a->ignores, path))
2690 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2691 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2692 if (parent_path[0])
2693 free(path);
2694 return err;
2697 static const struct got_error *
2698 report_single_file_status(const char *path, const char *ondisk_path,
2699 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2700 void *status_arg, struct got_repository *repo, int report_unchanged)
2702 struct got_fileindex_entry *ie;
2703 struct stat sb;
2705 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2706 if (ie)
2707 return report_file_status(ie, ondisk_path, -1, NULL,
2708 status_cb, status_arg, repo, report_unchanged);
2710 if (lstat(ondisk_path, &sb) == -1) {
2711 if (errno != ENOENT)
2712 return got_error_from_errno2("lstat", ondisk_path);
2713 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2714 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2715 return NULL;
2718 if (S_ISREG(sb.st_mode))
2719 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2720 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2722 return NULL;
2725 static const struct got_error *
2726 worktree_status(struct got_worktree *worktree, const char *path,
2727 struct got_fileindex *fileindex, struct got_repository *repo,
2728 got_worktree_status_cb status_cb, void *status_arg,
2729 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2730 int report_unchanged)
2732 const struct got_error *err = NULL;
2733 int fd = -1;
2734 struct got_fileindex_diff_dir_cb fdiff_cb;
2735 struct diff_dir_cb_arg arg;
2736 char *ondisk_path = NULL;
2738 if (asprintf(&ondisk_path, "%s%s%s",
2739 worktree->root_path, path[0] ? "/" : "", path) == -1)
2740 return got_error_from_errno("asprintf");
2742 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2743 if (fd == -1) {
2744 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2745 err = got_error_from_errno2("open", ondisk_path);
2746 else
2747 err = report_single_file_status(path, ondisk_path,
2748 fileindex, status_cb, status_arg, repo,
2749 report_unchanged);
2750 } else {
2751 fdiff_cb.diff_old_new = status_old_new;
2752 fdiff_cb.diff_old = status_old;
2753 fdiff_cb.diff_new = status_new;
2754 arg.fileindex = fileindex;
2755 arg.worktree = worktree;
2756 arg.status_path = path;
2757 arg.status_path_len = strlen(path);
2758 arg.repo = repo;
2759 arg.status_cb = status_cb;
2760 arg.status_arg = status_arg;
2761 arg.cancel_cb = cancel_cb;
2762 arg.cancel_arg = cancel_arg;
2763 arg.report_unchanged = report_unchanged;
2764 TAILQ_INIT(&arg.ignores);
2765 if (!no_ignores) {
2766 err = add_ignores(&arg.ignores, worktree->root_path,
2767 path, fd, ".cvsignore");
2768 if (err == NULL)
2769 err = add_ignores(&arg.ignores,
2770 worktree->root_path, path, fd,
2771 ".gitignore");
2773 if (err == NULL)
2774 err = got_fileindex_diff_dir(fileindex, fd,
2775 worktree->root_path, path, repo, &fdiff_cb, &arg);
2776 free_ignores(&arg.ignores);
2779 if (fd != -1 && close(fd) != 0 && err == NULL)
2780 err = got_error_from_errno("close");
2781 free(ondisk_path);
2782 return err;
2785 const struct got_error *
2786 got_worktree_status(struct got_worktree *worktree,
2787 struct got_pathlist_head *paths, struct got_repository *repo,
2788 got_worktree_status_cb status_cb, void *status_arg,
2789 got_cancel_cb cancel_cb, void *cancel_arg)
2791 const struct got_error *err = NULL;
2792 char *fileindex_path = NULL;
2793 struct got_fileindex *fileindex = NULL;
2794 struct got_pathlist_entry *pe;
2796 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2797 if (err)
2798 return err;
2800 TAILQ_FOREACH(pe, paths, entry) {
2801 err = worktree_status(worktree, pe->path, fileindex, repo,
2802 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2803 if (err)
2804 break;
2806 free(fileindex_path);
2807 got_fileindex_free(fileindex);
2808 return err;
2811 const struct got_error *
2812 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2813 const char *arg)
2815 const struct got_error *err = NULL;
2816 char *resolved, *cwd = NULL, *path = NULL;
2817 size_t len;
2819 *wt_path = NULL;
2821 resolved = realpath(arg, NULL);
2822 if (resolved == NULL) {
2823 if (errno != ENOENT)
2824 return got_error_from_errno2("realpath", arg);
2825 cwd = getcwd(NULL, 0);
2826 if (cwd == NULL)
2827 return got_error_from_errno("getcwd");
2828 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2829 err = got_error_from_errno("asprintf");
2830 goto done;
2834 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2835 strlen(got_worktree_get_root_path(worktree)))) {
2836 err = got_error(GOT_ERR_BAD_PATH);
2837 goto done;
2840 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2841 err = got_path_skip_common_ancestor(&path,
2842 got_worktree_get_root_path(worktree), resolved);
2843 if (err)
2844 goto done;
2845 } else {
2846 path = strdup("");
2847 if (path == NULL) {
2848 err = got_error_from_errno("strdup");
2849 goto done;
2853 /* XXX status walk can't deal with trailing slash! */
2854 len = strlen(path);
2855 while (len > 0 && path[len - 1] == '/') {
2856 path[len - 1] = '\0';
2857 len--;
2859 done:
2860 free(resolved);
2861 free(cwd);
2862 if (err == NULL)
2863 *wt_path = path;
2864 else
2865 free(path);
2866 return err;
2869 struct schedule_addition_args {
2870 struct got_worktree *worktree;
2871 struct got_fileindex *fileindex;
2872 got_worktree_checkout_cb progress_cb;
2873 void *progress_arg;
2874 struct got_repository *repo;
2877 static const struct got_error *
2878 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2879 const char *relpath, struct got_object_id *blob_id,
2880 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2881 int dirfd, const char *de_name)
2883 struct schedule_addition_args *a = arg;
2884 const struct got_error *err = NULL;
2885 struct got_fileindex_entry *ie;
2886 struct stat sb;
2887 char *ondisk_path;
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 relpath) == -1)
2891 return got_error_from_errno("asprintf");
2893 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2894 if (ie) {
2895 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2896 de_name, a->repo);
2897 if (err)
2898 goto done;
2899 /* Re-adding an existing entry is a no-op. */
2900 if (status == GOT_STATUS_ADD)
2901 goto done;
2902 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2903 if (err)
2904 goto done;
2907 if (status != GOT_STATUS_UNVERSIONED) {
2908 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2909 goto done;
2912 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2913 if (err)
2914 goto done;
2916 err = got_fileindex_entry_add(a->fileindex, ie);
2917 if (err) {
2918 got_fileindex_entry_free(ie);
2919 goto done;
2921 done:
2922 free(ondisk_path);
2923 if (err)
2924 return err;
2925 if (status == GOT_STATUS_ADD)
2926 return NULL;
2927 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2930 const struct got_error *
2931 got_worktree_schedule_add(struct got_worktree *worktree,
2932 struct got_pathlist_head *paths,
2933 got_worktree_checkout_cb progress_cb, void *progress_arg,
2934 struct got_repository *repo, int no_ignores)
2936 struct got_fileindex *fileindex = NULL;
2937 char *fileindex_path = NULL;
2938 const struct got_error *err = NULL, *sync_err, *unlockerr;
2939 struct got_pathlist_entry *pe;
2940 struct schedule_addition_args saa;
2942 err = lock_worktree(worktree, LOCK_EX);
2943 if (err)
2944 return err;
2946 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2947 if (err)
2948 goto done;
2950 saa.worktree = worktree;
2951 saa.fileindex = fileindex;
2952 saa.progress_cb = progress_cb;
2953 saa.progress_arg = progress_arg;
2954 saa.repo = repo;
2956 TAILQ_FOREACH(pe, paths, entry) {
2957 err = worktree_status(worktree, pe->path, fileindex, repo,
2958 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2959 if (err)
2960 break;
2962 sync_err = sync_fileindex(fileindex, fileindex_path);
2963 if (sync_err && err == NULL)
2964 err = sync_err;
2965 done:
2966 free(fileindex_path);
2967 if (fileindex)
2968 got_fileindex_free(fileindex);
2969 unlockerr = lock_worktree(worktree, LOCK_SH);
2970 if (unlockerr && err == NULL)
2971 err = unlockerr;
2972 return err;
2975 struct schedule_deletion_args {
2976 struct got_worktree *worktree;
2977 struct got_fileindex *fileindex;
2978 got_worktree_delete_cb progress_cb;
2979 void *progress_arg;
2980 struct got_repository *repo;
2981 int delete_local_mods;
2982 int keep_on_disk;
2985 static const struct got_error *
2986 schedule_for_deletion(void *arg, unsigned char status,
2987 unsigned char staged_status, const char *relpath,
2988 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2989 struct got_object_id *commit_id, int dirfd, const char *de_name)
2991 struct schedule_deletion_args *a = arg;
2992 const struct got_error *err = NULL;
2993 struct got_fileindex_entry *ie = NULL;
2994 struct stat sb;
2995 char *ondisk_path;
2997 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2998 if (ie == NULL)
2999 return got_error(GOT_ERR_BAD_PATH);
3001 staged_status = get_staged_status(ie);
3002 if (staged_status != GOT_STATUS_NO_CHANGE) {
3003 if (staged_status == GOT_STATUS_DELETE)
3004 return NULL;
3005 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3008 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3009 relpath) == -1)
3010 return got_error_from_errno("asprintf");
3012 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3013 a->repo);
3014 if (err)
3015 goto done;
3017 if (status != GOT_STATUS_NO_CHANGE) {
3018 if (status == GOT_STATUS_DELETE)
3019 goto done;
3020 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3021 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3022 goto done;
3024 if (status != GOT_STATUS_MODIFY &&
3025 status != GOT_STATUS_MISSING) {
3026 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3027 goto done;
3031 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3032 if (dirfd != -1) {
3033 if (unlinkat(dirfd, de_name, 0) != 0) {
3034 err = got_error_from_errno2("unlinkat",
3035 ondisk_path);
3036 goto done;
3038 } else if (unlink(ondisk_path) != 0) {
3039 err = got_error_from_errno2("unlink", ondisk_path);
3040 goto done;
3044 got_fileindex_entry_mark_deleted_from_disk(ie);
3045 done:
3046 free(ondisk_path);
3047 if (err)
3048 return err;
3049 if (status == GOT_STATUS_DELETE)
3050 return NULL;
3051 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3052 staged_status, relpath);
3055 const struct got_error *
3056 got_worktree_schedule_delete(struct got_worktree *worktree,
3057 struct got_pathlist_head *paths, int delete_local_mods,
3058 got_worktree_delete_cb progress_cb, void *progress_arg,
3059 struct got_repository *repo, int keep_on_disk)
3061 struct got_fileindex *fileindex = NULL;
3062 char *fileindex_path = NULL;
3063 const struct got_error *err = NULL, *sync_err, *unlockerr;
3064 struct got_pathlist_entry *pe;
3065 struct schedule_deletion_args sda;
3067 err = lock_worktree(worktree, LOCK_EX);
3068 if (err)
3069 return err;
3071 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3072 if (err)
3073 goto done;
3075 sda.worktree = worktree;
3076 sda.fileindex = fileindex;
3077 sda.progress_cb = progress_cb;
3078 sda.progress_arg = progress_arg;
3079 sda.repo = repo;
3080 sda.delete_local_mods = delete_local_mods;
3081 sda.keep_on_disk = keep_on_disk;
3083 TAILQ_FOREACH(pe, paths, entry) {
3084 err = worktree_status(worktree, pe->path, fileindex, repo,
3085 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3086 if (err)
3087 break;
3089 sync_err = sync_fileindex(fileindex, fileindex_path);
3090 if (sync_err && err == NULL)
3091 err = sync_err;
3092 done:
3093 free(fileindex_path);
3094 if (fileindex)
3095 got_fileindex_free(fileindex);
3096 unlockerr = lock_worktree(worktree, LOCK_SH);
3097 if (unlockerr && err == NULL)
3098 err = unlockerr;
3099 return err;
3102 static const struct got_error *
3103 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3105 const struct got_error *err = NULL;
3106 char *line = NULL;
3107 size_t linesize = 0, n;
3108 ssize_t linelen;
3110 linelen = getline(&line, &linesize, infile);
3111 if (linelen == -1) {
3112 if (ferror(infile)) {
3113 err = got_error_from_errno("getline");
3114 goto done;
3116 return NULL;
3118 if (outfile) {
3119 n = fwrite(line, 1, linelen, outfile);
3120 if (n != linelen) {
3121 err = got_ferror(outfile, GOT_ERR_IO);
3122 goto done;
3125 if (rejectfile) {
3126 n = fwrite(line, 1, linelen, rejectfile);
3127 if (n != linelen)
3128 err = got_ferror(outfile, GOT_ERR_IO);
3130 done:
3131 free(line);
3132 return err;
3135 static const struct got_error *
3136 skip_one_line(FILE *f)
3138 char *line = NULL;
3139 size_t linesize = 0;
3140 ssize_t linelen;
3142 linelen = getline(&line, &linesize, f);
3143 if (linelen == -1) {
3144 if (ferror(f))
3145 return got_error_from_errno("getline");
3146 return NULL;
3148 free(line);
3149 return NULL;
3152 static const struct got_error *
3153 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3154 int start_old, int end_old, int start_new, int end_new,
3155 FILE *outfile, FILE *rejectfile)
3157 const struct got_error *err;
3159 /* Copy old file's lines leading up to patch. */
3160 while (!feof(f1) && *line_cur1 < start_old) {
3161 err = copy_one_line(f1, outfile, NULL);
3162 if (err)
3163 return err;
3164 (*line_cur1)++;
3166 /* Skip new file's lines leading up to patch. */
3167 while (!feof(f2) && *line_cur2 < start_new) {
3168 if (rejectfile)
3169 err = copy_one_line(f2, NULL, rejectfile);
3170 else
3171 err = skip_one_line(f2);
3172 if (err)
3173 return err;
3174 (*line_cur2)++;
3176 /* Copy patched lines. */
3177 while (!feof(f2) && *line_cur2 <= end_new) {
3178 err = copy_one_line(f2, outfile, NULL);
3179 if (err)
3180 return err;
3181 (*line_cur2)++;
3183 /* Skip over old file's replaced lines. */
3184 while (!feof(f1) && *line_cur1 <= end_old) {
3185 if (rejectfile)
3186 err = copy_one_line(f1, NULL, rejectfile);
3187 else
3188 err = skip_one_line(f1);
3189 if (err)
3190 return err;
3191 (*line_cur1)++;
3194 return NULL;
3197 static const struct got_error *
3198 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3199 FILE *outfile, FILE *rejectfile)
3201 const struct got_error *err;
3203 if (outfile) {
3204 /* Copy old file's lines until EOF. */
3205 while (!feof(f1)) {
3206 err = copy_one_line(f1, outfile, NULL);
3207 if (err)
3208 return err;
3209 (*line_cur1)++;
3212 if (rejectfile) {
3213 /* Copy new file's lines until EOF. */
3214 while (!feof(f2)) {
3215 err = copy_one_line(f2, NULL, rejectfile);
3216 if (err)
3217 return err;
3218 (*line_cur2)++;
3222 return NULL;
3225 static const struct got_error *
3226 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3227 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3228 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3229 int *line_cur2, FILE *outfile, FILE *rejectfile,
3230 got_worktree_patch_cb patch_cb, void *patch_arg)
3232 const struct got_error *err = NULL;
3233 int start_old = change->cv.a;
3234 int end_old = change->cv.b;
3235 int start_new = change->cv.c;
3236 int end_new = change->cv.d;
3237 long pos1, pos2;
3238 FILE *hunkfile;
3240 *choice = GOT_PATCH_CHOICE_NONE;
3242 hunkfile = got_opentemp();
3243 if (hunkfile == NULL)
3244 return got_error_from_errno("got_opentemp");
3246 pos1 = ftell(f1);
3247 pos2 = ftell(f2);
3249 /* XXX TODO needs error checking */
3250 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3252 if (fseek(f1, pos1, SEEK_SET) == -1) {
3253 err = got_ferror(f1, GOT_ERR_IO);
3254 goto done;
3256 if (fseek(f2, pos2, SEEK_SET) == -1) {
3257 err = got_ferror(f1, GOT_ERR_IO);
3258 goto done;
3260 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3261 err = got_ferror(hunkfile, GOT_ERR_IO);
3262 goto done;
3265 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3266 hunkfile, n, nchanges);
3267 if (err)
3268 goto done;
3270 switch (*choice) {
3271 case GOT_PATCH_CHOICE_YES:
3272 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3273 end_old, start_new, end_new, outfile, rejectfile);
3274 break;
3275 case GOT_PATCH_CHOICE_NO:
3276 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3277 end_old, start_new, end_new, rejectfile, outfile);
3278 break;
3279 case GOT_PATCH_CHOICE_QUIT:
3280 break;
3281 default:
3282 err = got_error(GOT_ERR_PATCH_CHOICE);
3283 break;
3285 done:
3286 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3287 err = got_error_from_errno("fclose");
3288 return err;
3291 struct revert_file_args {
3292 struct got_worktree *worktree;
3293 struct got_fileindex *fileindex;
3294 got_worktree_checkout_cb progress_cb;
3295 void *progress_arg;
3296 got_worktree_patch_cb patch_cb;
3297 void *patch_arg;
3298 struct got_repository *repo;
3301 static const struct got_error *
3302 create_patched_content(char **path_outfile, int reverse_patch,
3303 struct got_object_id *blob_id, const char *path2,
3304 int dirfd2, const char *de_name2,
3305 const char *relpath, struct got_repository *repo,
3306 got_worktree_patch_cb patch_cb, void *patch_arg)
3308 const struct got_error *err;
3309 struct got_blob_object *blob = NULL;
3310 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3311 int fd2 = -1;
3312 char *path1 = NULL, *id_str = NULL;
3313 struct stat sb1, sb2;
3314 struct got_diff_changes *changes = NULL;
3315 struct got_diff_state *ds = NULL;
3316 struct got_diff_args *args = NULL;
3317 struct got_diff_change *change;
3318 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3319 int n = 0;
3321 *path_outfile = NULL;
3323 err = got_object_id_str(&id_str, blob_id);
3324 if (err)
3325 return err;
3327 if (dirfd2 != -1) {
3328 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3329 if (fd2 == -1) {
3330 err = got_error_from_errno2("openat", path2);
3331 goto done;
3333 } else {
3334 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3335 if (fd2 == -1) {
3336 err = got_error_from_errno2("open", path2);
3337 goto done;
3340 if (fstat(fd2, &sb2) == -1) {
3341 err = got_error_from_errno2("fstat", path2);
3342 goto done;
3345 f2 = fdopen(fd2, "r");
3346 if (f2 == NULL) {
3347 err = got_error_from_errno2("fdopen", path2);
3348 goto done;
3350 fd2 = -1;
3352 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3353 if (err)
3354 goto done;
3356 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3357 if (err)
3358 goto done;
3360 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3361 if (err)
3362 goto done;
3364 if (stat(path1, &sb1) == -1) {
3365 err = got_error_from_errno2("stat", path1);
3366 goto done;
3369 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3370 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3371 if (err)
3372 goto done;
3374 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3375 if (err)
3376 goto done;
3378 if (fseek(f1, 0L, SEEK_SET) == -1)
3379 return got_ferror(f1, GOT_ERR_IO);
3380 if (fseek(f2, 0L, SEEK_SET) == -1)
3381 return got_ferror(f2, GOT_ERR_IO);
3382 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3383 int choice;
3384 err = apply_or_reject_change(&choice, change, ++n,
3385 changes->nchanges, ds, args, diff_flags, relpath,
3386 f1, f2, &line_cur1, &line_cur2,
3387 reverse_patch ? NULL : outfile,
3388 reverse_patch ? outfile : NULL,
3389 patch_cb, patch_arg);
3390 if (err)
3391 goto done;
3392 if (choice == GOT_PATCH_CHOICE_YES)
3393 have_content = 1;
3394 else if (choice == GOT_PATCH_CHOICE_QUIT)
3395 break;
3397 if (have_content) {
3398 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3399 reverse_patch ? NULL : outfile,
3400 reverse_patch ? outfile : NULL);
3401 if (err)
3402 goto done;
3404 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3405 err = got_error_from_errno2("chmod", path2);
3406 goto done;
3409 done:
3410 free(id_str);
3411 if (blob)
3412 got_object_blob_close(blob);
3413 if (f1 && fclose(f1) == EOF && err == NULL)
3414 err = got_error_from_errno2("fclose", path1);
3415 if (f2 && fclose(f2) == EOF && err == NULL)
3416 err = got_error_from_errno2("fclose", path2);
3417 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3418 err = got_error_from_errno2("close", path2);
3419 if (outfile && fclose(outfile) == EOF && err == NULL)
3420 err = got_error_from_errno2("fclose", *path_outfile);
3421 if (path1 && unlink(path1) == -1 && err == NULL)
3422 err = got_error_from_errno2("unlink", path1);
3423 if (err || !have_content) {
3424 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3425 err = got_error_from_errno2("unlink", *path_outfile);
3426 free(*path_outfile);
3427 *path_outfile = NULL;
3429 free(args);
3430 if (ds) {
3431 got_diff_state_free(ds);
3432 free(ds);
3434 if (changes)
3435 got_diff_free_changes(changes);
3436 free(path1);
3437 return err;
3440 static const struct got_error *
3441 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3442 const char *relpath, struct got_object_id *blob_id,
3443 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3444 int dirfd, const char *de_name)
3446 struct revert_file_args *a = arg;
3447 const struct got_error *err = NULL;
3448 char *parent_path = NULL;
3449 struct got_fileindex_entry *ie;
3450 struct got_tree_object *tree = NULL;
3451 struct got_object_id *tree_id = NULL;
3452 const struct got_tree_entry *te = NULL;
3453 char *tree_path = NULL, *te_name;
3454 char *ondisk_path = NULL, *path_content = NULL;
3455 struct got_blob_object *blob = NULL;
3457 /* Reverting a staged deletion is a no-op. */
3458 if (status == GOT_STATUS_DELETE &&
3459 staged_status != GOT_STATUS_NO_CHANGE)
3460 return NULL;
3462 if (status == GOT_STATUS_UNVERSIONED)
3463 return (*a->progress_cb)(a->progress_arg,
3464 GOT_STATUS_UNVERSIONED, relpath);
3466 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3467 if (ie == NULL)
3468 return got_error(GOT_ERR_BAD_PATH);
3470 /* Construct in-repository path of tree which contains this blob. */
3471 err = got_path_dirname(&parent_path, ie->path);
3472 if (err) {
3473 if (err->code != GOT_ERR_BAD_PATH)
3474 goto done;
3475 parent_path = strdup("/");
3476 if (parent_path == NULL) {
3477 err = got_error_from_errno("strdup");
3478 goto done;
3481 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3482 tree_path = strdup(parent_path);
3483 if (tree_path == NULL) {
3484 err = got_error_from_errno("strdup");
3485 goto done;
3487 } else {
3488 if (got_path_is_root_dir(parent_path)) {
3489 tree_path = strdup(a->worktree->path_prefix);
3490 if (tree_path == NULL) {
3491 err = got_error_from_errno("strdup");
3492 goto done;
3494 } else {
3495 if (asprintf(&tree_path, "%s/%s",
3496 a->worktree->path_prefix, parent_path) == -1) {
3497 err = got_error_from_errno("asprintf");
3498 goto done;
3503 err = got_object_id_by_path(&tree_id, a->repo,
3504 a->worktree->base_commit_id, tree_path);
3505 if (err) {
3506 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3507 (status == GOT_STATUS_ADD ||
3508 staged_status == GOT_STATUS_ADD)))
3509 goto done;
3510 } else {
3511 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3512 if (err)
3513 goto done;
3515 te_name = basename(ie->path);
3516 if (te_name == NULL) {
3517 err = got_error_from_errno2("basename", ie->path);
3518 goto done;
3521 te = got_object_tree_find_entry(tree, te_name);
3522 if (te == NULL && status != GOT_STATUS_ADD &&
3523 staged_status != GOT_STATUS_ADD) {
3524 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3525 goto done;
3529 switch (status) {
3530 case GOT_STATUS_ADD:
3531 if (a->patch_cb) {
3532 int choice = GOT_PATCH_CHOICE_NONE;
3533 err = (*a->patch_cb)(&choice, a->patch_arg,
3534 status, ie->path, NULL, 1, 1);
3535 if (err)
3536 goto done;
3537 if (choice != GOT_PATCH_CHOICE_YES)
3538 break;
3540 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3541 ie->path);
3542 if (err)
3543 goto done;
3544 got_fileindex_entry_remove(a->fileindex, ie);
3545 break;
3546 case GOT_STATUS_DELETE:
3547 if (a->patch_cb) {
3548 int choice = GOT_PATCH_CHOICE_NONE;
3549 err = (*a->patch_cb)(&choice, a->patch_arg,
3550 status, ie->path, NULL, 1, 1);
3551 if (err)
3552 goto done;
3553 if (choice != GOT_PATCH_CHOICE_YES)
3554 break;
3556 /* fall through */
3557 case GOT_STATUS_MODIFY:
3558 case GOT_STATUS_MODE_CHANGE:
3559 case GOT_STATUS_CONFLICT:
3560 case GOT_STATUS_MISSING: {
3561 struct got_object_id id;
3562 if (staged_status == GOT_STATUS_ADD ||
3563 staged_status == GOT_STATUS_MODIFY) {
3564 memcpy(id.sha1, ie->staged_blob_sha1,
3565 SHA1_DIGEST_LENGTH);
3566 } else
3567 memcpy(id.sha1, ie->blob_sha1,
3568 SHA1_DIGEST_LENGTH);
3569 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3570 if (err)
3571 goto done;
3573 if (asprintf(&ondisk_path, "%s/%s",
3574 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3575 err = got_error_from_errno("asprintf");
3576 goto done;
3579 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3580 status == GOT_STATUS_CONFLICT)) {
3581 err = create_patched_content(&path_content, 1, &id,
3582 ondisk_path, dirfd, de_name, ie->path, a->repo,
3583 a->patch_cb, a->patch_arg);
3584 if (err || path_content == NULL)
3585 break;
3586 if (rename(path_content, ondisk_path) == -1) {
3587 err = got_error_from_errno3("rename",
3588 path_content, ondisk_path);
3589 goto done;
3591 } else {
3592 err = install_blob(a->worktree, ondisk_path, ie->path,
3593 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3594 got_fileindex_perms_to_st(ie), blob, 0, 1,
3595 a->repo, a->progress_cb, a->progress_arg);
3596 if (err)
3597 goto done;
3598 if (status == GOT_STATUS_DELETE ||
3599 status == GOT_STATUS_MODE_CHANGE) {
3600 err = update_blob_fileindex_entry(a->worktree,
3601 a->fileindex, ie, ondisk_path, ie->path,
3602 blob, 1);
3603 if (err)
3604 goto done;
3607 break;
3609 default:
3610 break;
3612 done:
3613 free(ondisk_path);
3614 free(path_content);
3615 free(parent_path);
3616 free(tree_path);
3617 if (blob)
3618 got_object_blob_close(blob);
3619 if (tree)
3620 got_object_tree_close(tree);
3621 free(tree_id);
3622 return err;
3625 const struct got_error *
3626 got_worktree_revert(struct got_worktree *worktree,
3627 struct got_pathlist_head *paths,
3628 got_worktree_checkout_cb progress_cb, void *progress_arg,
3629 got_worktree_patch_cb patch_cb, void *patch_arg,
3630 struct got_repository *repo)
3632 struct got_fileindex *fileindex = NULL;
3633 char *fileindex_path = NULL;
3634 const struct got_error *err = NULL, *unlockerr = NULL;
3635 const struct got_error *sync_err = NULL;
3636 struct got_pathlist_entry *pe;
3637 struct revert_file_args rfa;
3639 err = lock_worktree(worktree, LOCK_EX);
3640 if (err)
3641 return err;
3643 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3644 if (err)
3645 goto done;
3647 rfa.worktree = worktree;
3648 rfa.fileindex = fileindex;
3649 rfa.progress_cb = progress_cb;
3650 rfa.progress_arg = progress_arg;
3651 rfa.patch_cb = patch_cb;
3652 rfa.patch_arg = patch_arg;
3653 rfa.repo = repo;
3654 TAILQ_FOREACH(pe, paths, entry) {
3655 err = worktree_status(worktree, pe->path, fileindex, repo,
3656 revert_file, &rfa, NULL, NULL, 0, 0);
3657 if (err)
3658 break;
3660 sync_err = sync_fileindex(fileindex, fileindex_path);
3661 if (sync_err && err == NULL)
3662 err = sync_err;
3663 done:
3664 free(fileindex_path);
3665 if (fileindex)
3666 got_fileindex_free(fileindex);
3667 unlockerr = lock_worktree(worktree, LOCK_SH);
3668 if (unlockerr && err == NULL)
3669 err = unlockerr;
3670 return err;
3673 static void
3674 free_commitable(struct got_commitable *ct)
3676 free(ct->path);
3677 free(ct->in_repo_path);
3678 free(ct->ondisk_path);
3679 free(ct->blob_id);
3680 free(ct->base_blob_id);
3681 free(ct->staged_blob_id);
3682 free(ct->base_commit_id);
3683 free(ct);
3686 struct collect_commitables_arg {
3687 struct got_pathlist_head *commitable_paths;
3688 struct got_repository *repo;
3689 struct got_worktree *worktree;
3690 int have_staged_files;
3693 static const struct got_error *
3694 collect_commitables(void *arg, unsigned char status,
3695 unsigned char staged_status, const char *relpath,
3696 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3697 struct got_object_id *commit_id, int dirfd, const char *de_name)
3699 struct collect_commitables_arg *a = arg;
3700 const struct got_error *err = NULL;
3701 struct got_commitable *ct = NULL;
3702 struct got_pathlist_entry *new = NULL;
3703 char *parent_path = NULL, *path = NULL;
3704 struct stat sb;
3706 if (a->have_staged_files) {
3707 if (staged_status != GOT_STATUS_MODIFY &&
3708 staged_status != GOT_STATUS_ADD &&
3709 staged_status != GOT_STATUS_DELETE)
3710 return NULL;
3711 } else {
3712 if (status == GOT_STATUS_CONFLICT)
3713 return got_error(GOT_ERR_COMMIT_CONFLICT);
3715 if (status != GOT_STATUS_MODIFY &&
3716 status != GOT_STATUS_MODE_CHANGE &&
3717 status != GOT_STATUS_ADD &&
3718 status != GOT_STATUS_DELETE)
3719 return NULL;
3722 if (asprintf(&path, "/%s", relpath) == -1) {
3723 err = got_error_from_errno("asprintf");
3724 goto done;
3726 if (strcmp(path, "/") == 0) {
3727 parent_path = strdup("");
3728 if (parent_path == NULL)
3729 return got_error_from_errno("strdup");
3730 } else {
3731 err = got_path_dirname(&parent_path, path);
3732 if (err)
3733 return err;
3736 ct = calloc(1, sizeof(*ct));
3737 if (ct == NULL) {
3738 err = got_error_from_errno("calloc");
3739 goto done;
3742 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3743 relpath) == -1) {
3744 err = got_error_from_errno("asprintf");
3745 goto done;
3747 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3748 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3749 } else {
3750 if (dirfd != -1) {
3751 if (fstatat(dirfd, de_name, &sb,
3752 AT_SYMLINK_NOFOLLOW) == -1) {
3753 err = got_error_from_errno2("fstatat",
3754 ct->ondisk_path);
3755 goto done;
3757 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3758 err = got_error_from_errno2("lstat", ct->ondisk_path);
3759 goto done;
3761 ct->mode = sb.st_mode;
3764 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3765 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3766 relpath) == -1) {
3767 err = got_error_from_errno("asprintf");
3768 goto done;
3771 ct->status = status;
3772 ct->staged_status = staged_status;
3773 ct->blob_id = NULL; /* will be filled in when blob gets created */
3774 if (ct->status != GOT_STATUS_ADD &&
3775 ct->staged_status != GOT_STATUS_ADD) {
3776 ct->base_blob_id = got_object_id_dup(blob_id);
3777 if (ct->base_blob_id == NULL) {
3778 err = got_error_from_errno("got_object_id_dup");
3779 goto done;
3781 ct->base_commit_id = got_object_id_dup(commit_id);
3782 if (ct->base_commit_id == NULL) {
3783 err = got_error_from_errno("got_object_id_dup");
3784 goto done;
3787 if (ct->staged_status == GOT_STATUS_ADD ||
3788 ct->staged_status == GOT_STATUS_MODIFY) {
3789 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3790 if (ct->staged_blob_id == NULL) {
3791 err = got_error_from_errno("got_object_id_dup");
3792 goto done;
3795 ct->path = strdup(path);
3796 if (ct->path == NULL) {
3797 err = got_error_from_errno("strdup");
3798 goto done;
3800 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3801 done:
3802 if (ct && (err || new == NULL))
3803 free_commitable(ct);
3804 free(parent_path);
3805 free(path);
3806 return err;
3809 static const struct got_error *write_tree(struct got_object_id **,
3810 struct got_tree_object *, const char *, struct got_pathlist_head *,
3811 got_worktree_status_cb status_cb, void *status_arg,
3812 struct got_repository *);
3814 static const struct got_error *
3815 write_subtree(struct got_object_id **new_subtree_id,
3816 struct got_tree_entry *te, const char *parent_path,
3817 struct got_pathlist_head *commitable_paths,
3818 got_worktree_status_cb status_cb, void *status_arg,
3819 struct got_repository *repo)
3821 const struct got_error *err = NULL;
3822 struct got_tree_object *subtree;
3823 char *subpath;
3825 if (asprintf(&subpath, "%s%s%s", parent_path,
3826 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3827 return got_error_from_errno("asprintf");
3829 err = got_object_open_as_tree(&subtree, repo, &te->id);
3830 if (err)
3831 return err;
3833 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3834 status_cb, status_arg, repo);
3835 got_object_tree_close(subtree);
3836 free(subpath);
3837 return err;
3840 static const struct got_error *
3841 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3843 const struct got_error *err = NULL;
3844 char *ct_parent_path = NULL;
3846 *match = 0;
3848 if (strchr(ct->in_repo_path, '/') == NULL) {
3849 *match = got_path_is_root_dir(path);
3850 return NULL;
3853 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3854 if (err)
3855 return err;
3856 *match = (strcmp(path, ct_parent_path) == 0);
3857 free(ct_parent_path);
3858 return err;
3861 static mode_t
3862 get_ct_file_mode(struct got_commitable *ct)
3864 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3867 static const struct got_error *
3868 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3869 struct got_tree_entry *te, struct got_commitable *ct)
3871 const struct got_error *err = NULL;
3873 *new_te = NULL;
3875 err = got_object_tree_entry_dup(new_te, te);
3876 if (err)
3877 goto done;
3879 (*new_te)->mode = get_ct_file_mode(ct);
3881 if (ct->staged_status == GOT_STATUS_MODIFY)
3882 memcpy(&(*new_te)->id, ct->staged_blob_id,
3883 sizeof((*new_te)->id));
3884 else
3885 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3886 done:
3887 if (err && *new_te) {
3888 free(*new_te);
3889 *new_te = NULL;
3891 return err;
3894 static const struct got_error *
3895 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3896 struct got_commitable *ct)
3898 const struct got_error *err = NULL;
3899 char *ct_name;
3901 *new_te = NULL;
3903 *new_te = calloc(1, sizeof(**new_te));
3904 if (*new_te == NULL)
3905 return got_error_from_errno("calloc");
3907 ct_name = basename(ct->path);
3908 if (ct_name == NULL) {
3909 err = got_error_from_errno2("basename", ct->path);
3910 goto done;
3912 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3913 sizeof((*new_te)->name)) {
3914 err = got_error(GOT_ERR_NO_SPACE);
3915 goto done;
3918 (*new_te)->mode = get_ct_file_mode(ct);
3920 if (ct->staged_status == GOT_STATUS_ADD)
3921 memcpy(&(*new_te)->id, ct->staged_blob_id,
3922 sizeof((*new_te)->id));
3923 else
3924 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3925 done:
3926 if (err && *new_te) {
3927 free(*new_te);
3928 *new_te = NULL;
3930 return err;
3933 static const struct got_error *
3934 insert_tree_entry(struct got_tree_entry *new_te,
3935 struct got_pathlist_head *paths)
3937 const struct got_error *err = NULL;
3938 struct got_pathlist_entry *new_pe;
3940 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3941 if (err)
3942 return err;
3943 if (new_pe == NULL)
3944 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3945 return NULL;
3948 static const struct got_error *
3949 report_ct_status(struct got_commitable *ct,
3950 got_worktree_status_cb status_cb, void *status_arg)
3952 const char *ct_path = ct->path;
3953 unsigned char status;
3955 while (ct_path[0] == '/')
3956 ct_path++;
3958 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3959 status = ct->staged_status;
3960 else
3961 status = ct->status;
3963 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3964 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3967 static const struct got_error *
3968 match_modified_subtree(int *modified, struct got_tree_entry *te,
3969 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3971 const struct got_error *err = NULL;
3972 struct got_pathlist_entry *pe;
3973 char *te_path;
3975 *modified = 0;
3977 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3978 got_path_is_root_dir(base_tree_path) ? "" : "/",
3979 te->name) == -1)
3980 return got_error_from_errno("asprintf");
3982 TAILQ_FOREACH(pe, commitable_paths, entry) {
3983 struct got_commitable *ct = pe->data;
3984 *modified = got_path_is_child(ct->in_repo_path, te_path,
3985 strlen(te_path));
3986 if (*modified)
3987 break;
3990 free(te_path);
3991 return err;
3994 static const struct got_error *
3995 match_deleted_or_modified_ct(struct got_commitable **ctp,
3996 struct got_tree_entry *te, const char *base_tree_path,
3997 struct got_pathlist_head *commitable_paths)
3999 const struct got_error *err = NULL;
4000 struct got_pathlist_entry *pe;
4002 *ctp = NULL;
4004 TAILQ_FOREACH(pe, commitable_paths, entry) {
4005 struct got_commitable *ct = pe->data;
4006 char *ct_name = NULL;
4007 int path_matches;
4009 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4010 if (ct->status != GOT_STATUS_MODIFY &&
4011 ct->status != GOT_STATUS_MODE_CHANGE &&
4012 ct->status != GOT_STATUS_DELETE)
4013 continue;
4014 } else {
4015 if (ct->staged_status != GOT_STATUS_MODIFY &&
4016 ct->staged_status != GOT_STATUS_DELETE)
4017 continue;
4020 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4021 continue;
4023 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4024 if (err)
4025 return err;
4026 if (!path_matches)
4027 continue;
4029 ct_name = basename(pe->path);
4030 if (ct_name == NULL)
4031 return got_error_from_errno2("basename", pe->path);
4033 if (strcmp(te->name, ct_name) != 0)
4034 continue;
4036 *ctp = ct;
4037 break;
4040 return err;
4043 static const struct got_error *
4044 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4045 const char *child_path, const char *path_base_tree,
4046 struct got_pathlist_head *commitable_paths,
4047 got_worktree_status_cb status_cb, void *status_arg,
4048 struct got_repository *repo)
4050 const struct got_error *err = NULL;
4051 struct got_tree_entry *new_te;
4052 char *subtree_path;
4053 struct got_object_id *id = NULL;
4055 *new_tep = NULL;
4057 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4058 got_path_is_root_dir(path_base_tree) ? "" : "/",
4059 child_path) == -1)
4060 return got_error_from_errno("asprintf");
4062 new_te = calloc(1, sizeof(*new_te));
4063 if (new_te == NULL)
4064 return got_error_from_errno("calloc");
4065 new_te->mode = S_IFDIR;
4067 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4068 sizeof(new_te->name)) {
4069 err = got_error(GOT_ERR_NO_SPACE);
4070 goto done;
4072 err = write_tree(&id, NULL, subtree_path,
4073 commitable_paths, status_cb, status_arg, repo);
4074 if (err) {
4075 free(new_te);
4076 goto done;
4078 memcpy(&new_te->id, id, sizeof(new_te->id));
4079 done:
4080 free(id);
4081 free(subtree_path);
4082 if (err == NULL)
4083 *new_tep = new_te;
4084 return err;
4087 static const struct got_error *
4088 write_tree(struct got_object_id **new_tree_id,
4089 struct got_tree_object *base_tree, const char *path_base_tree,
4090 struct got_pathlist_head *commitable_paths,
4091 got_worktree_status_cb status_cb, void *status_arg,
4092 struct got_repository *repo)
4094 const struct got_error *err = NULL;
4095 struct got_pathlist_head paths;
4096 struct got_tree_entry *te, *new_te = NULL;
4097 struct got_pathlist_entry *pe;
4098 int nentries = 0;
4100 TAILQ_INIT(&paths);
4102 /* Insert, and recurse into, newly added entries first. */
4103 TAILQ_FOREACH(pe, commitable_paths, entry) {
4104 struct got_commitable *ct = pe->data;
4105 char *child_path = NULL, *slash;
4107 if ((ct->status != GOT_STATUS_ADD &&
4108 ct->staged_status != GOT_STATUS_ADD) ||
4109 (ct->flags & GOT_COMMITABLE_ADDED))
4110 continue;
4112 if (!got_path_is_child(pe->path, path_base_tree,
4113 strlen(path_base_tree)))
4114 continue;
4116 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4117 pe->path);
4118 if (err)
4119 goto done;
4121 slash = strchr(child_path, '/');
4122 if (slash == NULL) {
4123 err = alloc_added_blob_tree_entry(&new_te, ct);
4124 if (err)
4125 goto done;
4126 err = report_ct_status(ct, status_cb, status_arg);
4127 if (err)
4128 goto done;
4129 ct->flags |= GOT_COMMITABLE_ADDED;
4130 err = insert_tree_entry(new_te, &paths);
4131 if (err)
4132 goto done;
4133 nentries++;
4134 } else {
4135 *slash = '\0'; /* trim trailing path components */
4136 if (base_tree == NULL ||
4137 got_object_tree_find_entry(base_tree, child_path)
4138 == NULL) {
4139 err = make_subtree_for_added_blob(&new_te,
4140 child_path, path_base_tree,
4141 commitable_paths, status_cb, status_arg,
4142 repo);
4143 if (err)
4144 goto done;
4145 err = insert_tree_entry(new_te, &paths);
4146 if (err)
4147 goto done;
4148 nentries++;
4153 if (base_tree) {
4154 int i, nbase_entries;
4155 /* Handle modified and deleted entries. */
4156 nbase_entries = got_object_tree_get_nentries(base_tree);
4157 for (i = 0; i < nbase_entries; i++) {
4158 struct got_commitable *ct = NULL;
4160 te = got_object_tree_get_entry(base_tree, i);
4161 if (got_object_tree_entry_is_submodule(te)) {
4162 /* Entry is a submodule; just copy it. */
4163 err = got_object_tree_entry_dup(&new_te, te);
4164 if (err)
4165 goto done;
4166 err = insert_tree_entry(new_te, &paths);
4167 if (err)
4168 goto done;
4169 nentries++;
4170 continue;
4173 if (S_ISDIR(te->mode)) {
4174 int modified;
4175 err = got_object_tree_entry_dup(&new_te, te);
4176 if (err)
4177 goto done;
4178 err = match_modified_subtree(&modified, te,
4179 path_base_tree, commitable_paths);
4180 if (err)
4181 goto done;
4182 /* Avoid recursion into unmodified subtrees. */
4183 if (modified) {
4184 struct got_object_id *new_id;
4185 err = write_subtree(&new_id, te,
4186 path_base_tree, commitable_paths,
4187 status_cb, status_arg, repo);
4188 if (err)
4189 goto done;
4190 memcpy(&new_te->id, new_id,
4191 sizeof(new_te->id));
4192 free(new_id);
4194 err = insert_tree_entry(new_te, &paths);
4195 if (err)
4196 goto done;
4197 nentries++;
4198 continue;
4201 err = match_deleted_or_modified_ct(&ct, te,
4202 path_base_tree, commitable_paths);
4203 if (err)
4204 goto done;
4205 if (ct) {
4206 /* NB: Deleted entries get dropped here. */
4207 if (ct->status == GOT_STATUS_MODIFY ||
4208 ct->status == GOT_STATUS_MODE_CHANGE ||
4209 ct->staged_status == GOT_STATUS_MODIFY) {
4210 err = alloc_modified_blob_tree_entry(
4211 &new_te, te, ct);
4212 if (err)
4213 goto done;
4214 err = insert_tree_entry(new_te, &paths);
4215 if (err)
4216 goto done;
4217 nentries++;
4219 err = report_ct_status(ct, status_cb,
4220 status_arg);
4221 if (err)
4222 goto done;
4223 } else {
4224 /* Entry is unchanged; just copy it. */
4225 err = got_object_tree_entry_dup(&new_te, te);
4226 if (err)
4227 goto done;
4228 err = insert_tree_entry(new_te, &paths);
4229 if (err)
4230 goto done;
4231 nentries++;
4236 /* Write new list of entries; deleted entries have been dropped. */
4237 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4238 done:
4239 got_pathlist_free(&paths);
4240 return err;
4243 static const struct got_error *
4244 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4245 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4246 int have_staged_files)
4248 const struct got_error *err = NULL;
4249 struct got_pathlist_entry *pe;
4251 TAILQ_FOREACH(pe, commitable_paths, entry) {
4252 struct got_fileindex_entry *ie;
4253 struct got_commitable *ct = pe->data;
4255 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4256 if (ie) {
4257 if (ct->status == GOT_STATUS_DELETE ||
4258 ct->staged_status == GOT_STATUS_DELETE) {
4259 got_fileindex_entry_remove(fileindex, ie);
4260 } else if (ct->staged_status == GOT_STATUS_ADD ||
4261 ct->staged_status == GOT_STATUS_MODIFY) {
4262 got_fileindex_entry_stage_set(ie,
4263 GOT_FILEIDX_STAGE_NONE);
4264 err = got_fileindex_entry_update(ie,
4265 ct->ondisk_path, ct->staged_blob_id->sha1,
4266 new_base_commit_id->sha1,
4267 !have_staged_files);
4268 } else
4269 err = got_fileindex_entry_update(ie,
4270 ct->ondisk_path, ct->blob_id->sha1,
4271 new_base_commit_id->sha1,
4272 !have_staged_files);
4273 } else {
4274 err = got_fileindex_entry_alloc(&ie,
4275 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4276 new_base_commit_id->sha1);
4277 if (err)
4278 break;
4279 err = got_fileindex_entry_add(fileindex, ie);
4280 if (err)
4281 break;
4284 return err;
4288 static const struct got_error *
4289 check_out_of_date(const char *in_repo_path, unsigned char status,
4290 unsigned char staged_status, struct got_object_id *base_blob_id,
4291 struct got_object_id *base_commit_id,
4292 struct got_object_id *head_commit_id, struct got_repository *repo,
4293 int ood_errcode)
4295 const struct got_error *err = NULL;
4296 struct got_object_id *id = NULL;
4298 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4299 /* Trivial case: base commit == head commit */
4300 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4301 return NULL;
4303 * Ensure file content which local changes were based
4304 * on matches file content in the branch head.
4306 err = got_object_id_by_path(&id, repo, head_commit_id,
4307 in_repo_path);
4308 if (err) {
4309 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4310 err = got_error(ood_errcode);
4311 goto done;
4312 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4313 err = got_error(ood_errcode);
4314 } else {
4315 /* Require that added files don't exist in the branch head. */
4316 err = got_object_id_by_path(&id, repo, head_commit_id,
4317 in_repo_path);
4318 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4319 goto done;
4320 err = id ? got_error(ood_errcode) : NULL;
4322 done:
4323 free(id);
4324 return err;
4327 const struct got_error *
4328 commit_worktree(struct got_object_id **new_commit_id,
4329 struct got_pathlist_head *commitable_paths,
4330 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4331 const char *author, const char *committer,
4332 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4333 got_worktree_status_cb status_cb, void *status_arg,
4334 struct got_repository *repo)
4336 const struct got_error *err = NULL, *unlockerr = NULL;
4337 struct got_pathlist_entry *pe;
4338 const char *head_ref_name = NULL;
4339 struct got_commit_object *head_commit = NULL;
4340 struct got_reference *head_ref2 = NULL;
4341 struct got_object_id *head_commit_id2 = NULL;
4342 struct got_tree_object *head_tree = NULL;
4343 struct got_object_id *new_tree_id = NULL;
4344 struct got_object_id_queue parent_ids;
4345 struct got_object_qid *pid = NULL;
4346 char *logmsg = NULL;
4348 *new_commit_id = NULL;
4350 SIMPLEQ_INIT(&parent_ids);
4352 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4353 if (err)
4354 goto done;
4356 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4357 if (err)
4358 goto done;
4360 if (commit_msg_cb != NULL) {
4361 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4362 if (err)
4363 goto done;
4366 if (logmsg == NULL || strlen(logmsg) == 0) {
4367 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4368 goto done;
4371 /* Create blobs from added and modified files and record their IDs. */
4372 TAILQ_FOREACH(pe, commitable_paths, entry) {
4373 struct got_commitable *ct = pe->data;
4374 char *ondisk_path;
4376 /* Blobs for staged files already exist. */
4377 if (ct->staged_status == GOT_STATUS_ADD ||
4378 ct->staged_status == GOT_STATUS_MODIFY)
4379 continue;
4381 if (ct->status != GOT_STATUS_ADD &&
4382 ct->status != GOT_STATUS_MODIFY &&
4383 ct->status != GOT_STATUS_MODE_CHANGE)
4384 continue;
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 worktree->root_path, pe->path) == -1) {
4388 err = got_error_from_errno("asprintf");
4389 goto done;
4391 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4392 free(ondisk_path);
4393 if (err)
4394 goto done;
4397 /* Recursively write new tree objects. */
4398 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4399 status_cb, status_arg, repo);
4400 if (err)
4401 goto done;
4403 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4404 if (err)
4405 goto done;
4406 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4407 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4408 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4409 got_object_qid_free(pid);
4410 if (logmsg != NULL)
4411 free(logmsg);
4412 if (err)
4413 goto done;
4415 /* Check if a concurrent commit to our branch has occurred. */
4416 head_ref_name = got_worktree_get_head_ref_name(worktree);
4417 if (head_ref_name == NULL) {
4418 err = got_error_from_errno("got_worktree_get_head_ref_name");
4419 goto done;
4421 /* Lock the reference here to prevent concurrent modification. */
4422 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4423 if (err)
4424 goto done;
4425 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4426 if (err)
4427 goto done;
4428 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4429 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4430 goto done;
4432 /* Update branch head in repository. */
4433 err = got_ref_change_ref(head_ref2, *new_commit_id);
4434 if (err)
4435 goto done;
4436 err = got_ref_write(head_ref2, repo);
4437 if (err)
4438 goto done;
4440 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4441 if (err)
4442 goto done;
4444 err = ref_base_commit(worktree, repo);
4445 if (err)
4446 goto done;
4447 done:
4448 if (head_tree)
4449 got_object_tree_close(head_tree);
4450 if (head_commit)
4451 got_object_commit_close(head_commit);
4452 free(head_commit_id2);
4453 if (head_ref2) {
4454 unlockerr = got_ref_unlock(head_ref2);
4455 if (unlockerr && err == NULL)
4456 err = unlockerr;
4457 got_ref_close(head_ref2);
4459 return err;
4462 static const struct got_error *
4463 check_path_is_commitable(const char *path,
4464 struct got_pathlist_head *commitable_paths)
4466 struct got_pathlist_entry *cpe = NULL;
4467 size_t path_len = strlen(path);
4469 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4470 struct got_commitable *ct = cpe->data;
4471 const char *ct_path = ct->path;
4473 while (ct_path[0] == '/')
4474 ct_path++;
4476 if (strcmp(path, ct_path) == 0 ||
4477 got_path_is_child(ct_path, path, path_len))
4478 break;
4481 if (cpe == NULL)
4482 return got_error_path(path, GOT_ERR_BAD_PATH);
4484 return NULL;
4487 static const struct got_error *
4488 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4490 int *have_staged_files = arg;
4492 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4493 *have_staged_files = 1;
4494 return got_error(GOT_ERR_CANCELLED);
4497 return NULL;
4500 static const struct got_error *
4501 check_non_staged_files(struct got_fileindex *fileindex,
4502 struct got_pathlist_head *paths)
4504 struct got_pathlist_entry *pe;
4505 struct got_fileindex_entry *ie;
4507 TAILQ_FOREACH(pe, paths, entry) {
4508 if (pe->path[0] == '\0')
4509 continue;
4510 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4511 if (ie == NULL)
4512 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4513 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4514 return got_error_path(pe->path,
4515 GOT_ERR_FILE_NOT_STAGED);
4518 return NULL;
4521 const struct got_error *
4522 got_worktree_commit(struct got_object_id **new_commit_id,
4523 struct got_worktree *worktree, struct got_pathlist_head *paths,
4524 const char *author, const char *committer,
4525 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4526 got_worktree_status_cb status_cb, void *status_arg,
4527 struct got_repository *repo)
4529 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4530 struct got_fileindex *fileindex = NULL;
4531 char *fileindex_path = NULL;
4532 struct got_pathlist_head commitable_paths;
4533 struct collect_commitables_arg cc_arg;
4534 struct got_pathlist_entry *pe;
4535 struct got_reference *head_ref = NULL;
4536 struct got_object_id *head_commit_id = NULL;
4537 int have_staged_files = 0;
4539 *new_commit_id = NULL;
4541 TAILQ_INIT(&commitable_paths);
4543 err = lock_worktree(worktree, LOCK_EX);
4544 if (err)
4545 goto done;
4547 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4548 if (err)
4549 goto done;
4551 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4552 if (err)
4553 goto done;
4555 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4556 if (err)
4557 goto done;
4559 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4560 &have_staged_files);
4561 if (err && err->code != GOT_ERR_CANCELLED)
4562 goto done;
4563 if (have_staged_files) {
4564 err = check_non_staged_files(fileindex, paths);
4565 if (err)
4566 goto done;
4569 cc_arg.commitable_paths = &commitable_paths;
4570 cc_arg.worktree = worktree;
4571 cc_arg.repo = repo;
4572 cc_arg.have_staged_files = have_staged_files;
4573 TAILQ_FOREACH(pe, paths, entry) {
4574 err = worktree_status(worktree, pe->path, fileindex, repo,
4575 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4576 if (err)
4577 goto done;
4580 if (TAILQ_EMPTY(&commitable_paths)) {
4581 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4582 goto done;
4585 TAILQ_FOREACH(pe, paths, entry) {
4586 err = check_path_is_commitable(pe->path, &commitable_paths);
4587 if (err)
4588 goto done;
4591 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4592 struct got_commitable *ct = pe->data;
4593 const char *ct_path = ct->in_repo_path;
4595 while (ct_path[0] == '/')
4596 ct_path++;
4597 err = check_out_of_date(ct_path, ct->status,
4598 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4599 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4600 if (err)
4601 goto done;
4605 err = commit_worktree(new_commit_id, &commitable_paths,
4606 head_commit_id, worktree, author, committer,
4607 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4608 if (err)
4609 goto done;
4611 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4612 fileindex, have_staged_files);
4613 sync_err = sync_fileindex(fileindex, fileindex_path);
4614 if (sync_err && err == NULL)
4615 err = sync_err;
4616 done:
4617 if (fileindex)
4618 got_fileindex_free(fileindex);
4619 free(fileindex_path);
4620 unlockerr = lock_worktree(worktree, LOCK_SH);
4621 if (unlockerr && err == NULL)
4622 err = unlockerr;
4623 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4624 struct got_commitable *ct = pe->data;
4625 free_commitable(ct);
4627 got_pathlist_free(&commitable_paths);
4628 return err;
4631 const char *
4632 got_commitable_get_path(struct got_commitable *ct)
4634 return ct->path;
4637 unsigned int
4638 got_commitable_get_status(struct got_commitable *ct)
4640 return ct->status;
4643 struct check_rebase_ok_arg {
4644 struct got_worktree *worktree;
4645 struct got_repository *repo;
4648 static const struct got_error *
4649 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4651 const struct got_error *err = NULL;
4652 struct check_rebase_ok_arg *a = arg;
4653 unsigned char status;
4654 struct stat sb;
4655 char *ondisk_path;
4657 /* Reject rebase of a work tree with mixed base commits. */
4658 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4659 SHA1_DIGEST_LENGTH))
4660 return got_error(GOT_ERR_MIXED_COMMITS);
4662 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4663 == -1)
4664 return got_error_from_errno("asprintf");
4666 /* Reject rebase of a work tree with modified or staged files. */
4667 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4668 free(ondisk_path);
4669 if (err)
4670 return err;
4672 if (status != GOT_STATUS_NO_CHANGE)
4673 return got_error(GOT_ERR_MODIFIED);
4674 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4675 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4677 return NULL;
4680 const struct got_error *
4681 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4682 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4683 struct got_worktree *worktree, struct got_reference *branch,
4684 struct got_repository *repo)
4686 const struct got_error *err = NULL;
4687 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4688 char *branch_ref_name = NULL;
4689 char *fileindex_path = NULL;
4690 struct check_rebase_ok_arg ok_arg;
4691 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4692 struct got_object_id *wt_branch_tip = NULL;
4694 *new_base_branch_ref = NULL;
4695 *tmp_branch = NULL;
4696 *fileindex = NULL;
4698 err = lock_worktree(worktree, LOCK_EX);
4699 if (err)
4700 return err;
4702 err = open_fileindex(fileindex, &fileindex_path, worktree);
4703 if (err)
4704 goto done;
4706 ok_arg.worktree = worktree;
4707 ok_arg.repo = repo;
4708 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4709 &ok_arg);
4710 if (err)
4711 goto done;
4713 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4714 if (err)
4715 goto done;
4717 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4718 if (err)
4719 goto done;
4721 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4722 if (err)
4723 goto done;
4725 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4726 0);
4727 if (err)
4728 goto done;
4730 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4731 if (err)
4732 goto done;
4733 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4734 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4735 goto done;
4738 err = got_ref_alloc_symref(new_base_branch_ref,
4739 new_base_branch_ref_name, wt_branch);
4740 if (err)
4741 goto done;
4742 err = got_ref_write(*new_base_branch_ref, repo);
4743 if (err)
4744 goto done;
4746 /* TODO Lock original branch's ref while rebasing? */
4748 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4749 if (err)
4750 goto done;
4752 err = got_ref_write(branch_ref, repo);
4753 if (err)
4754 goto done;
4756 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4757 worktree->base_commit_id);
4758 if (err)
4759 goto done;
4760 err = got_ref_write(*tmp_branch, repo);
4761 if (err)
4762 goto done;
4764 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4765 if (err)
4766 goto done;
4767 done:
4768 free(fileindex_path);
4769 free(tmp_branch_name);
4770 free(new_base_branch_ref_name);
4771 free(branch_ref_name);
4772 if (branch_ref)
4773 got_ref_close(branch_ref);
4774 if (wt_branch)
4775 got_ref_close(wt_branch);
4776 free(wt_branch_tip);
4777 if (err) {
4778 if (*new_base_branch_ref) {
4779 got_ref_close(*new_base_branch_ref);
4780 *new_base_branch_ref = NULL;
4782 if (*tmp_branch) {
4783 got_ref_close(*tmp_branch);
4784 *tmp_branch = NULL;
4786 if (*fileindex) {
4787 got_fileindex_free(*fileindex);
4788 *fileindex = NULL;
4790 lock_worktree(worktree, LOCK_SH);
4792 return err;
4795 const struct got_error *
4796 got_worktree_rebase_continue(struct got_object_id **commit_id,
4797 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4798 struct got_reference **branch, struct got_fileindex **fileindex,
4799 struct got_worktree *worktree, struct got_repository *repo)
4801 const struct got_error *err;
4802 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4803 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4804 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4805 char *fileindex_path = NULL;
4806 int have_staged_files = 0;
4808 *commit_id = NULL;
4809 *new_base_branch = NULL;
4810 *tmp_branch = NULL;
4811 *branch = NULL;
4812 *fileindex = NULL;
4814 err = lock_worktree(worktree, LOCK_EX);
4815 if (err)
4816 return err;
4818 err = open_fileindex(fileindex, &fileindex_path, worktree);
4819 if (err)
4820 goto done;
4822 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4823 &have_staged_files);
4824 if (err && err->code != GOT_ERR_CANCELLED)
4825 goto done;
4826 if (have_staged_files) {
4827 err = got_error(GOT_ERR_STAGED_PATHS);
4828 goto done;
4831 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4832 if (err)
4833 goto done;
4835 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4836 if (err)
4837 goto done;
4839 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4840 if (err)
4841 goto done;
4843 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4844 if (err)
4845 goto done;
4847 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4848 if (err)
4849 goto done;
4851 err = got_ref_open(branch, repo,
4852 got_ref_get_symref_target(branch_ref), 0);
4853 if (err)
4854 goto done;
4856 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4857 if (err)
4858 goto done;
4860 err = got_ref_resolve(commit_id, repo, commit_ref);
4861 if (err)
4862 goto done;
4864 err = got_ref_open(new_base_branch, repo,
4865 new_base_branch_ref_name, 0);
4866 if (err)
4867 goto done;
4869 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4870 if (err)
4871 goto done;
4872 done:
4873 free(commit_ref_name);
4874 free(branch_ref_name);
4875 free(fileindex_path);
4876 if (commit_ref)
4877 got_ref_close(commit_ref);
4878 if (branch_ref)
4879 got_ref_close(branch_ref);
4880 if (err) {
4881 free(*commit_id);
4882 *commit_id = NULL;
4883 if (*tmp_branch) {
4884 got_ref_close(*tmp_branch);
4885 *tmp_branch = NULL;
4887 if (*new_base_branch) {
4888 got_ref_close(*new_base_branch);
4889 *new_base_branch = NULL;
4891 if (*branch) {
4892 got_ref_close(*branch);
4893 *branch = NULL;
4895 if (*fileindex) {
4896 got_fileindex_free(*fileindex);
4897 *fileindex = NULL;
4899 lock_worktree(worktree, LOCK_SH);
4901 return err;
4904 const struct got_error *
4905 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4907 const struct got_error *err;
4908 char *tmp_branch_name = NULL;
4910 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4911 if (err)
4912 return err;
4914 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4915 free(tmp_branch_name);
4916 return NULL;
4919 static const struct got_error *
4920 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4921 char **logmsg, void *arg)
4923 *logmsg = arg;
4924 return NULL;
4927 static const struct got_error *
4928 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4929 const char *path, struct got_object_id *blob_id,
4930 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4931 int dirfd, const char *de_name)
4933 return NULL;
4936 struct collect_merged_paths_arg {
4937 got_worktree_checkout_cb progress_cb;
4938 void *progress_arg;
4939 struct got_pathlist_head *merged_paths;
4942 static const struct got_error *
4943 collect_merged_paths(void *arg, unsigned char status, const char *path)
4945 const struct got_error *err;
4946 struct collect_merged_paths_arg *a = arg;
4947 char *p;
4948 struct got_pathlist_entry *new;
4950 err = (*a->progress_cb)(a->progress_arg, status, path);
4951 if (err)
4952 return err;
4954 if (status != GOT_STATUS_MERGE &&
4955 status != GOT_STATUS_ADD &&
4956 status != GOT_STATUS_DELETE &&
4957 status != GOT_STATUS_CONFLICT)
4958 return NULL;
4960 p = strdup(path);
4961 if (p == NULL)
4962 return got_error_from_errno("strdup");
4964 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4965 if (err || new == NULL)
4966 free(p);
4967 return err;
4970 void
4971 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4973 struct got_pathlist_entry *pe;
4975 TAILQ_FOREACH(pe, merged_paths, entry)
4976 free((char *)pe->path);
4978 got_pathlist_free(merged_paths);
4981 static const struct got_error *
4982 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4983 struct got_repository *repo)
4985 const struct got_error *err;
4986 struct got_reference *commit_ref = NULL;
4988 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4989 if (err) {
4990 if (err->code != GOT_ERR_NOT_REF)
4991 goto done;
4992 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4993 if (err)
4994 goto done;
4995 err = got_ref_write(commit_ref, repo);
4996 if (err)
4997 goto done;
4998 } else {
4999 struct got_object_id *stored_id;
5000 int cmp;
5002 err = got_ref_resolve(&stored_id, repo, commit_ref);
5003 if (err)
5004 goto done;
5005 cmp = got_object_id_cmp(commit_id, stored_id);
5006 free(stored_id);
5007 if (cmp != 0) {
5008 err = got_error(GOT_ERR_REBASE_COMMITID);
5009 goto done;
5012 done:
5013 if (commit_ref)
5014 got_ref_close(commit_ref);
5015 return err;
5018 static const struct got_error *
5019 rebase_merge_files(struct got_pathlist_head *merged_paths,
5020 const char *commit_ref_name, struct got_worktree *worktree,
5021 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5022 struct got_object_id *commit_id, struct got_repository *repo,
5023 got_worktree_checkout_cb progress_cb, void *progress_arg,
5024 got_cancel_cb cancel_cb, void *cancel_arg)
5026 const struct got_error *err;
5027 struct got_reference *commit_ref = NULL;
5028 struct collect_merged_paths_arg cmp_arg;
5029 char *fileindex_path;
5031 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5033 err = get_fileindex_path(&fileindex_path, worktree);
5034 if (err)
5035 return err;
5037 cmp_arg.progress_cb = progress_cb;
5038 cmp_arg.progress_arg = progress_arg;
5039 cmp_arg.merged_paths = merged_paths;
5040 err = merge_files(worktree, fileindex, fileindex_path,
5041 parent_commit_id, commit_id, repo, collect_merged_paths,
5042 &cmp_arg, cancel_cb, cancel_arg);
5043 if (commit_ref)
5044 got_ref_close(commit_ref);
5045 return err;
5048 const struct got_error *
5049 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5050 struct got_worktree *worktree, struct got_fileindex *fileindex,
5051 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5052 struct got_repository *repo,
5053 got_worktree_checkout_cb progress_cb, void *progress_arg,
5054 got_cancel_cb cancel_cb, void *cancel_arg)
5056 const struct got_error *err;
5057 char *commit_ref_name;
5059 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5060 if (err)
5061 return err;
5063 err = store_commit_id(commit_ref_name, commit_id, repo);
5064 if (err)
5065 goto done;
5067 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5068 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5069 progress_arg, cancel_cb, cancel_arg);
5070 done:
5071 free(commit_ref_name);
5072 return err;
5075 const struct got_error *
5076 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5077 struct got_worktree *worktree, struct got_fileindex *fileindex,
5078 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5079 struct got_repository *repo,
5080 got_worktree_checkout_cb progress_cb, void *progress_arg,
5081 got_cancel_cb cancel_cb, void *cancel_arg)
5083 const struct got_error *err;
5084 char *commit_ref_name;
5086 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5087 if (err)
5088 return err;
5090 err = store_commit_id(commit_ref_name, commit_id, repo);
5091 if (err)
5092 goto done;
5094 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5095 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5096 progress_arg, cancel_cb, cancel_arg);
5097 done:
5098 free(commit_ref_name);
5099 return err;
5102 static const struct got_error *
5103 rebase_commit(struct got_object_id **new_commit_id,
5104 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5105 struct got_worktree *worktree, struct got_fileindex *fileindex,
5106 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5107 const char *new_logmsg, struct got_repository *repo)
5109 const struct got_error *err, *sync_err;
5110 struct got_pathlist_head commitable_paths;
5111 struct collect_commitables_arg cc_arg;
5112 char *fileindex_path = NULL;
5113 struct got_reference *head_ref = NULL;
5114 struct got_object_id *head_commit_id = NULL;
5115 char *logmsg = NULL;
5117 TAILQ_INIT(&commitable_paths);
5118 *new_commit_id = NULL;
5120 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5122 err = get_fileindex_path(&fileindex_path, worktree);
5123 if (err)
5124 return err;
5126 cc_arg.commitable_paths = &commitable_paths;
5127 cc_arg.worktree = worktree;
5128 cc_arg.repo = repo;
5129 cc_arg.have_staged_files = 0;
5131 * If possible get the status of individual files directly to
5132 * avoid crawling the entire work tree once per rebased commit.
5133 * TODO: Ideally, merged_paths would contain a list of commitables
5134 * we could use so we could skip worktree_status() entirely.
5136 if (merged_paths) {
5137 struct got_pathlist_entry *pe;
5138 if (TAILQ_EMPTY(merged_paths)) {
5139 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5140 goto done;
5142 TAILQ_FOREACH(pe, merged_paths, entry) {
5143 err = worktree_status(worktree, pe->path, fileindex,
5144 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5145 0);
5146 if (err)
5147 goto done;
5149 } else {
5150 err = worktree_status(worktree, "", fileindex, repo,
5151 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5152 if (err)
5153 goto done;
5156 if (TAILQ_EMPTY(&commitable_paths)) {
5157 /* No-op change; commit will be elided. */
5158 err = got_ref_delete(commit_ref, repo);
5159 if (err)
5160 goto done;
5161 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5162 goto done;
5165 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5166 if (err)
5167 goto done;
5169 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5170 if (err)
5171 goto done;
5173 if (new_logmsg) {
5174 logmsg = strdup(new_logmsg);
5175 if (logmsg == NULL) {
5176 err = got_error_from_errno("strdup");
5177 goto done;
5179 } else {
5180 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5181 if (err)
5182 goto done;
5185 /* NB: commit_worktree will call free(logmsg) */
5186 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5187 worktree, got_object_commit_get_author(orig_commit),
5188 got_object_commit_get_committer(orig_commit),
5189 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5190 if (err)
5191 goto done;
5193 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5194 if (err)
5195 goto done;
5197 err = got_ref_delete(commit_ref, repo);
5198 if (err)
5199 goto done;
5201 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5202 fileindex, 0);
5203 sync_err = sync_fileindex(fileindex, fileindex_path);
5204 if (sync_err && err == NULL)
5205 err = sync_err;
5206 done:
5207 free(fileindex_path);
5208 free(head_commit_id);
5209 if (head_ref)
5210 got_ref_close(head_ref);
5211 if (err) {
5212 free(*new_commit_id);
5213 *new_commit_id = NULL;
5215 return err;
5218 const struct got_error *
5219 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5220 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5221 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5222 struct got_commit_object *orig_commit,
5223 struct got_object_id *orig_commit_id, struct got_repository *repo)
5225 const struct got_error *err;
5226 char *commit_ref_name;
5227 struct got_reference *commit_ref = NULL;
5228 struct got_object_id *commit_id = NULL;
5230 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5231 if (err)
5232 return err;
5234 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5235 if (err)
5236 goto done;
5237 err = got_ref_resolve(&commit_id, repo, commit_ref);
5238 if (err)
5239 goto done;
5240 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5241 err = got_error(GOT_ERR_REBASE_COMMITID);
5242 goto done;
5245 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5246 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5247 done:
5248 if (commit_ref)
5249 got_ref_close(commit_ref);
5250 free(commit_ref_name);
5251 free(commit_id);
5252 return err;
5255 const struct got_error *
5256 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5257 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5258 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5259 struct got_commit_object *orig_commit,
5260 struct got_object_id *orig_commit_id, const char *new_logmsg,
5261 struct got_repository *repo)
5263 const struct got_error *err;
5264 char *commit_ref_name;
5265 struct got_reference *commit_ref = NULL;
5266 struct got_object_id *commit_id = NULL;
5268 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5269 if (err)
5270 return err;
5272 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5273 if (err)
5274 goto done;
5275 err = got_ref_resolve(&commit_id, repo, commit_ref);
5276 if (err)
5277 goto done;
5278 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5279 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5280 goto done;
5283 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5284 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5285 done:
5286 if (commit_ref)
5287 got_ref_close(commit_ref);
5288 free(commit_ref_name);
5289 free(commit_id);
5290 return err;
5293 const struct got_error *
5294 got_worktree_rebase_postpone(struct got_worktree *worktree,
5295 struct got_fileindex *fileindex)
5297 if (fileindex)
5298 got_fileindex_free(fileindex);
5299 return lock_worktree(worktree, LOCK_SH);
5302 static const struct got_error *
5303 delete_ref(const char *name, struct got_repository *repo)
5305 const struct got_error *err;
5306 struct got_reference *ref;
5308 err = got_ref_open(&ref, repo, name, 0);
5309 if (err) {
5310 if (err->code == GOT_ERR_NOT_REF)
5311 return NULL;
5312 return err;
5315 err = got_ref_delete(ref, repo);
5316 got_ref_close(ref);
5317 return err;
5320 static const struct got_error *
5321 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5323 const struct got_error *err;
5324 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5325 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5327 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5328 if (err)
5329 goto done;
5330 err = delete_ref(tmp_branch_name, repo);
5331 if (err)
5332 goto done;
5334 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5335 if (err)
5336 goto done;
5337 err = delete_ref(new_base_branch_ref_name, repo);
5338 if (err)
5339 goto done;
5341 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5342 if (err)
5343 goto done;
5344 err = delete_ref(branch_ref_name, repo);
5345 if (err)
5346 goto done;
5348 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5349 if (err)
5350 goto done;
5351 err = delete_ref(commit_ref_name, repo);
5352 if (err)
5353 goto done;
5355 done:
5356 free(tmp_branch_name);
5357 free(new_base_branch_ref_name);
5358 free(branch_ref_name);
5359 free(commit_ref_name);
5360 return err;
5363 const struct got_error *
5364 got_worktree_rebase_complete(struct got_worktree *worktree,
5365 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5366 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5367 struct got_repository *repo)
5369 const struct got_error *err, *unlockerr;
5370 struct got_object_id *new_head_commit_id = NULL;
5372 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5373 if (err)
5374 return err;
5376 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5377 if (err)
5378 goto done;
5380 err = got_ref_write(rebased_branch, repo);
5381 if (err)
5382 goto done;
5384 err = got_worktree_set_head_ref(worktree, rebased_branch);
5385 if (err)
5386 goto done;
5388 err = delete_rebase_refs(worktree, repo);
5389 done:
5390 if (fileindex)
5391 got_fileindex_free(fileindex);
5392 free(new_head_commit_id);
5393 unlockerr = lock_worktree(worktree, LOCK_SH);
5394 if (unlockerr && err == NULL)
5395 err = unlockerr;
5396 return err;
5399 const struct got_error *
5400 got_worktree_rebase_abort(struct got_worktree *worktree,
5401 struct got_fileindex *fileindex, struct got_repository *repo,
5402 struct got_reference *new_base_branch,
5403 got_worktree_checkout_cb progress_cb, void *progress_arg)
5405 const struct got_error *err, *unlockerr, *sync_err;
5406 struct got_reference *resolved = NULL;
5407 struct got_object_id *commit_id = NULL;
5408 char *fileindex_path = NULL;
5409 struct revert_file_args rfa;
5410 struct got_object_id *tree_id = NULL;
5412 err = lock_worktree(worktree, LOCK_EX);
5413 if (err)
5414 return err;
5416 err = got_ref_open(&resolved, repo,
5417 got_ref_get_symref_target(new_base_branch), 0);
5418 if (err)
5419 goto done;
5421 err = got_worktree_set_head_ref(worktree, resolved);
5422 if (err)
5423 goto done;
5426 * XXX commits to the base branch could have happened while
5427 * we were busy rebasing; should we store the original commit ID
5428 * when rebase begins and read it back here?
5430 err = got_ref_resolve(&commit_id, repo, resolved);
5431 if (err)
5432 goto done;
5434 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5435 if (err)
5436 goto done;
5438 err = got_object_id_by_path(&tree_id, repo,
5439 worktree->base_commit_id, worktree->path_prefix);
5440 if (err)
5441 goto done;
5443 err = delete_rebase_refs(worktree, repo);
5444 if (err)
5445 goto done;
5447 err = get_fileindex_path(&fileindex_path, worktree);
5448 if (err)
5449 goto done;
5451 rfa.worktree = worktree;
5452 rfa.fileindex = fileindex;
5453 rfa.progress_cb = progress_cb;
5454 rfa.progress_arg = progress_arg;
5455 rfa.patch_cb = NULL;
5456 rfa.patch_arg = NULL;
5457 rfa.repo = repo;
5458 err = worktree_status(worktree, "", fileindex, repo,
5459 revert_file, &rfa, NULL, NULL, 0, 0);
5460 if (err)
5461 goto sync;
5463 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5464 repo, progress_cb, progress_arg, NULL, NULL);
5465 sync:
5466 sync_err = sync_fileindex(fileindex, fileindex_path);
5467 if (sync_err && err == NULL)
5468 err = sync_err;
5469 done:
5470 got_ref_close(resolved);
5471 free(tree_id);
5472 free(commit_id);
5473 if (fileindex)
5474 got_fileindex_free(fileindex);
5475 free(fileindex_path);
5477 unlockerr = lock_worktree(worktree, LOCK_SH);
5478 if (unlockerr && err == NULL)
5479 err = unlockerr;
5480 return err;
5483 const struct got_error *
5484 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5485 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5486 struct got_fileindex **fileindex, struct got_worktree *worktree,
5487 struct got_repository *repo)
5489 const struct got_error *err = NULL;
5490 char *tmp_branch_name = NULL;
5491 char *branch_ref_name = NULL;
5492 char *base_commit_ref_name = NULL;
5493 char *fileindex_path = NULL;
5494 struct check_rebase_ok_arg ok_arg;
5495 struct got_reference *wt_branch = NULL;
5496 struct got_reference *base_commit_ref = NULL;
5498 *tmp_branch = NULL;
5499 *branch_ref = NULL;
5500 *base_commit_id = NULL;
5501 *fileindex = NULL;
5503 err = lock_worktree(worktree, LOCK_EX);
5504 if (err)
5505 return err;
5507 err = open_fileindex(fileindex, &fileindex_path, worktree);
5508 if (err)
5509 goto done;
5511 ok_arg.worktree = worktree;
5512 ok_arg.repo = repo;
5513 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5514 &ok_arg);
5515 if (err)
5516 goto done;
5518 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5519 if (err)
5520 goto done;
5522 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5523 if (err)
5524 goto done;
5526 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5527 worktree);
5528 if (err)
5529 goto done;
5531 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5532 0);
5533 if (err)
5534 goto done;
5536 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5537 if (err)
5538 goto done;
5540 err = got_ref_write(*branch_ref, repo);
5541 if (err)
5542 goto done;
5544 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5545 worktree->base_commit_id);
5546 if (err)
5547 goto done;
5548 err = got_ref_write(base_commit_ref, repo);
5549 if (err)
5550 goto done;
5551 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5552 if (*base_commit_id == NULL) {
5553 err = got_error_from_errno("got_object_id_dup");
5554 goto done;
5557 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5558 worktree->base_commit_id);
5559 if (err)
5560 goto done;
5561 err = got_ref_write(*tmp_branch, repo);
5562 if (err)
5563 goto done;
5565 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5566 if (err)
5567 goto done;
5568 done:
5569 free(fileindex_path);
5570 free(tmp_branch_name);
5571 free(branch_ref_name);
5572 free(base_commit_ref_name);
5573 if (wt_branch)
5574 got_ref_close(wt_branch);
5575 if (err) {
5576 if (*branch_ref) {
5577 got_ref_close(*branch_ref);
5578 *branch_ref = NULL;
5580 if (*tmp_branch) {
5581 got_ref_close(*tmp_branch);
5582 *tmp_branch = NULL;
5584 free(*base_commit_id);
5585 if (*fileindex) {
5586 got_fileindex_free(*fileindex);
5587 *fileindex = NULL;
5589 lock_worktree(worktree, LOCK_SH);
5591 return err;
5594 const struct got_error *
5595 got_worktree_histedit_postpone(struct got_worktree *worktree,
5596 struct got_fileindex *fileindex)
5598 if (fileindex)
5599 got_fileindex_free(fileindex);
5600 return lock_worktree(worktree, LOCK_SH);
5603 const struct got_error *
5604 got_worktree_histedit_in_progress(int *in_progress,
5605 struct got_worktree *worktree)
5607 const struct got_error *err;
5608 char *tmp_branch_name = NULL;
5610 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5611 if (err)
5612 return err;
5614 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5615 free(tmp_branch_name);
5616 return NULL;
5619 const struct got_error *
5620 got_worktree_histedit_continue(struct got_object_id **commit_id,
5621 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5622 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5623 struct got_worktree *worktree, struct got_repository *repo)
5625 const struct got_error *err;
5626 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5627 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5628 struct got_reference *commit_ref = NULL;
5629 struct got_reference *base_commit_ref = NULL;
5630 char *fileindex_path = NULL;
5631 int have_staged_files = 0;
5633 *commit_id = NULL;
5634 *tmp_branch = NULL;
5635 *base_commit_id = NULL;
5636 *fileindex = NULL;
5638 err = lock_worktree(worktree, LOCK_EX);
5639 if (err)
5640 return err;
5642 err = open_fileindex(fileindex, &fileindex_path, worktree);
5643 if (err)
5644 goto done;
5646 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5647 &have_staged_files);
5648 if (err && err->code != GOT_ERR_CANCELLED)
5649 goto done;
5650 if (have_staged_files) {
5651 err = got_error(GOT_ERR_STAGED_PATHS);
5652 goto done;
5655 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5656 if (err)
5657 goto done;
5659 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5660 if (err)
5661 goto done;
5663 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5664 if (err)
5665 goto done;
5667 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5668 worktree);
5669 if (err)
5670 goto done;
5672 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5673 if (err)
5674 goto done;
5676 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5677 if (err)
5678 goto done;
5679 err = got_ref_resolve(commit_id, repo, commit_ref);
5680 if (err)
5681 goto done;
5683 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5684 if (err)
5685 goto done;
5686 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5687 if (err)
5688 goto done;
5690 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5691 if (err)
5692 goto done;
5693 done:
5694 free(commit_ref_name);
5695 free(branch_ref_name);
5696 free(fileindex_path);
5697 if (commit_ref)
5698 got_ref_close(commit_ref);
5699 if (base_commit_ref)
5700 got_ref_close(base_commit_ref);
5701 if (err) {
5702 free(*commit_id);
5703 *commit_id = NULL;
5704 free(*base_commit_id);
5705 *base_commit_id = NULL;
5706 if (*tmp_branch) {
5707 got_ref_close(*tmp_branch);
5708 *tmp_branch = NULL;
5710 if (*fileindex) {
5711 got_fileindex_free(*fileindex);
5712 *fileindex = NULL;
5714 lock_worktree(worktree, LOCK_EX);
5716 return err;
5719 static const struct got_error *
5720 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5722 const struct got_error *err;
5723 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5724 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5726 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5727 if (err)
5728 goto done;
5729 err = delete_ref(tmp_branch_name, repo);
5730 if (err)
5731 goto done;
5733 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5734 worktree);
5735 if (err)
5736 goto done;
5737 err = delete_ref(base_commit_ref_name, repo);
5738 if (err)
5739 goto done;
5741 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5742 if (err)
5743 goto done;
5744 err = delete_ref(branch_ref_name, repo);
5745 if (err)
5746 goto done;
5748 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5749 if (err)
5750 goto done;
5751 err = delete_ref(commit_ref_name, repo);
5752 if (err)
5753 goto done;
5754 done:
5755 free(tmp_branch_name);
5756 free(base_commit_ref_name);
5757 free(branch_ref_name);
5758 free(commit_ref_name);
5759 return err;
5762 const struct got_error *
5763 got_worktree_histedit_abort(struct got_worktree *worktree,
5764 struct got_fileindex *fileindex, struct got_repository *repo,
5765 struct got_reference *branch, struct got_object_id *base_commit_id,
5766 got_worktree_checkout_cb progress_cb, void *progress_arg)
5768 const struct got_error *err, *unlockerr, *sync_err;
5769 struct got_reference *resolved = NULL;
5770 char *fileindex_path = NULL;
5771 struct got_object_id *tree_id = NULL;
5772 struct revert_file_args rfa;
5774 err = lock_worktree(worktree, LOCK_EX);
5775 if (err)
5776 return err;
5778 err = got_ref_open(&resolved, repo,
5779 got_ref_get_symref_target(branch), 0);
5780 if (err)
5781 goto done;
5783 err = got_worktree_set_head_ref(worktree, resolved);
5784 if (err)
5785 goto done;
5787 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5788 if (err)
5789 goto done;
5791 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5792 worktree->path_prefix);
5793 if (err)
5794 goto done;
5796 err = delete_histedit_refs(worktree, repo);
5797 if (err)
5798 goto done;
5800 err = get_fileindex_path(&fileindex_path, worktree);
5801 if (err)
5802 goto done;
5804 rfa.worktree = worktree;
5805 rfa.fileindex = fileindex;
5806 rfa.progress_cb = progress_cb;
5807 rfa.progress_arg = progress_arg;
5808 rfa.patch_cb = NULL;
5809 rfa.patch_arg = NULL;
5810 rfa.repo = repo;
5811 err = worktree_status(worktree, "", fileindex, repo,
5812 revert_file, &rfa, NULL, NULL, 0, 0);
5813 if (err)
5814 goto sync;
5816 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5817 repo, progress_cb, progress_arg, NULL, NULL);
5818 sync:
5819 sync_err = sync_fileindex(fileindex, fileindex_path);
5820 if (sync_err && err == NULL)
5821 err = sync_err;
5822 done:
5823 got_ref_close(resolved);
5824 free(tree_id);
5825 free(fileindex_path);
5827 unlockerr = lock_worktree(worktree, LOCK_SH);
5828 if (unlockerr && err == NULL)
5829 err = unlockerr;
5830 return err;
5833 const struct got_error *
5834 got_worktree_histedit_complete(struct got_worktree *worktree,
5835 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5836 struct got_reference *edited_branch, struct got_repository *repo)
5838 const struct got_error *err, *unlockerr;
5839 struct got_object_id *new_head_commit_id = NULL;
5840 struct got_reference *resolved = NULL;
5842 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5843 if (err)
5844 return err;
5846 err = got_ref_open(&resolved, repo,
5847 got_ref_get_symref_target(edited_branch), 0);
5848 if (err)
5849 goto done;
5851 err = got_ref_change_ref(resolved, new_head_commit_id);
5852 if (err)
5853 goto done;
5855 err = got_ref_write(resolved, repo);
5856 if (err)
5857 goto done;
5859 err = got_worktree_set_head_ref(worktree, resolved);
5860 if (err)
5861 goto done;
5863 err = delete_histedit_refs(worktree, repo);
5864 done:
5865 if (fileindex)
5866 got_fileindex_free(fileindex);
5867 free(new_head_commit_id);
5868 unlockerr = lock_worktree(worktree, LOCK_SH);
5869 if (unlockerr && err == NULL)
5870 err = unlockerr;
5871 return err;
5874 const struct got_error *
5875 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5876 struct got_object_id *commit_id, struct got_repository *repo)
5878 const struct got_error *err;
5879 char *commit_ref_name;
5881 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5882 if (err)
5883 return err;
5885 err = store_commit_id(commit_ref_name, commit_id, repo);
5886 if (err)
5887 goto done;
5889 err = delete_ref(commit_ref_name, repo);
5890 done:
5891 free(commit_ref_name);
5892 return err;
5895 const struct got_error *
5896 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5897 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5898 struct got_worktree *worktree, const char *refname,
5899 struct got_repository *repo)
5901 const struct got_error *err = NULL;
5902 char *fileindex_path = NULL;
5903 struct check_rebase_ok_arg ok_arg;
5905 *fileindex = NULL;
5906 *branch_ref = NULL;
5907 *base_branch_ref = NULL;
5909 err = lock_worktree(worktree, LOCK_EX);
5910 if (err)
5911 return err;
5913 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5914 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5915 "cannot integrate a branch into itself; "
5916 "update -b or different branch name required");
5917 goto done;
5920 err = open_fileindex(fileindex, &fileindex_path, worktree);
5921 if (err)
5922 goto done;
5924 /* Preconditions are the same as for rebase. */
5925 ok_arg.worktree = worktree;
5926 ok_arg.repo = repo;
5927 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5928 &ok_arg);
5929 if (err)
5930 goto done;
5932 err = got_ref_open(branch_ref, repo, refname, 1);
5933 if (err)
5934 goto done;
5936 err = got_ref_open(base_branch_ref, repo,
5937 got_worktree_get_head_ref_name(worktree), 1);
5938 done:
5939 if (err) {
5940 if (*branch_ref) {
5941 got_ref_close(*branch_ref);
5942 *branch_ref = NULL;
5944 if (*base_branch_ref) {
5945 got_ref_close(*base_branch_ref);
5946 *base_branch_ref = NULL;
5948 if (*fileindex) {
5949 got_fileindex_free(*fileindex);
5950 *fileindex = NULL;
5952 lock_worktree(worktree, LOCK_SH);
5954 return err;
5957 const struct got_error *
5958 got_worktree_integrate_continue(struct got_worktree *worktree,
5959 struct got_fileindex *fileindex, struct got_repository *repo,
5960 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5961 got_worktree_checkout_cb progress_cb, void *progress_arg,
5962 got_cancel_cb cancel_cb, void *cancel_arg)
5964 const struct got_error *err = NULL, *sync_err, *unlockerr;
5965 char *fileindex_path = NULL;
5966 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5968 err = get_fileindex_path(&fileindex_path, worktree);
5969 if (err)
5970 goto done;
5972 err = got_ref_resolve(&commit_id, repo, branch_ref);
5973 if (err)
5974 goto done;
5976 err = got_object_id_by_path(&tree_id, repo, commit_id,
5977 worktree->path_prefix);
5978 if (err)
5979 goto done;
5981 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5982 if (err)
5983 goto done;
5985 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5986 progress_cb, progress_arg, cancel_cb, cancel_arg);
5987 if (err)
5988 goto sync;
5990 err = got_ref_change_ref(base_branch_ref, commit_id);
5991 if (err)
5992 goto sync;
5994 err = got_ref_write(base_branch_ref, repo);
5995 sync:
5996 sync_err = sync_fileindex(fileindex, fileindex_path);
5997 if (sync_err && err == NULL)
5998 err = sync_err;
6000 done:
6001 unlockerr = got_ref_unlock(branch_ref);
6002 if (unlockerr && err == NULL)
6003 err = unlockerr;
6004 got_ref_close(branch_ref);
6006 unlockerr = got_ref_unlock(base_branch_ref);
6007 if (unlockerr && err == NULL)
6008 err = unlockerr;
6009 got_ref_close(base_branch_ref);
6011 got_fileindex_free(fileindex);
6012 free(fileindex_path);
6013 free(tree_id);
6015 unlockerr = lock_worktree(worktree, LOCK_SH);
6016 if (unlockerr && err == NULL)
6017 err = unlockerr;
6018 return err;
6021 const struct got_error *
6022 got_worktree_integrate_abort(struct got_worktree *worktree,
6023 struct got_fileindex *fileindex, struct got_repository *repo,
6024 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6026 const struct got_error *err = NULL, *unlockerr = NULL;
6028 got_fileindex_free(fileindex);
6030 err = lock_worktree(worktree, LOCK_SH);
6032 unlockerr = got_ref_unlock(branch_ref);
6033 if (unlockerr && err == NULL)
6034 err = unlockerr;
6035 got_ref_close(branch_ref);
6037 unlockerr = got_ref_unlock(base_branch_ref);
6038 if (unlockerr && err == NULL)
6039 err = unlockerr;
6040 got_ref_close(base_branch_ref);
6042 return err;
6045 struct check_stage_ok_arg {
6046 struct got_object_id *head_commit_id;
6047 struct got_worktree *worktree;
6048 struct got_fileindex *fileindex;
6049 struct got_repository *repo;
6050 int have_changes;
6053 const struct got_error *
6054 check_stage_ok(void *arg, unsigned char status,
6055 unsigned char staged_status, const char *relpath,
6056 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6057 struct got_object_id *commit_id, int dirfd, const char *de_name)
6059 struct check_stage_ok_arg *a = arg;
6060 const struct got_error *err = NULL;
6061 struct got_fileindex_entry *ie;
6062 struct got_object_id base_commit_id;
6063 struct got_object_id *base_commit_idp = NULL;
6064 char *in_repo_path = NULL, *p;
6066 if (status == GOT_STATUS_UNVERSIONED ||
6067 status == GOT_STATUS_NO_CHANGE)
6068 return NULL;
6069 if (status == GOT_STATUS_NONEXISTENT)
6070 return got_error_set_errno(ENOENT, relpath);
6072 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6073 if (ie == NULL)
6074 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6076 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6077 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6078 relpath) == -1)
6079 return got_error_from_errno("asprintf");
6081 if (got_fileindex_entry_has_commit(ie)) {
6082 memcpy(base_commit_id.sha1, ie->commit_sha1,
6083 SHA1_DIGEST_LENGTH);
6084 base_commit_idp = &base_commit_id;
6087 if (status == GOT_STATUS_CONFLICT) {
6088 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6089 goto done;
6090 } else if (status != GOT_STATUS_ADD &&
6091 status != GOT_STATUS_MODIFY &&
6092 status != GOT_STATUS_DELETE) {
6093 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6094 goto done;
6097 a->have_changes = 1;
6099 p = in_repo_path;
6100 while (p[0] == '/')
6101 p++;
6102 err = check_out_of_date(p, status, staged_status,
6103 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6104 GOT_ERR_STAGE_OUT_OF_DATE);
6105 done:
6106 free(in_repo_path);
6107 return err;
6110 struct stage_path_arg {
6111 struct got_worktree *worktree;
6112 struct got_fileindex *fileindex;
6113 struct got_repository *repo;
6114 got_worktree_status_cb status_cb;
6115 void *status_arg;
6116 got_worktree_patch_cb patch_cb;
6117 void *patch_arg;
6118 int staged_something;
6121 static const struct got_error *
6122 stage_path(void *arg, unsigned char status,
6123 unsigned char staged_status, const char *relpath,
6124 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6125 struct got_object_id *commit_id, int dirfd, const char *de_name)
6127 struct stage_path_arg *a = arg;
6128 const struct got_error *err = NULL;
6129 struct got_fileindex_entry *ie;
6130 char *ondisk_path = NULL, *path_content = NULL;
6131 uint32_t stage;
6132 struct got_object_id *new_staged_blob_id = NULL;
6134 if (status == GOT_STATUS_UNVERSIONED)
6135 return NULL;
6137 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6138 if (ie == NULL)
6139 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6141 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6142 relpath)== -1)
6143 return got_error_from_errno("asprintf");
6145 switch (status) {
6146 case GOT_STATUS_ADD:
6147 case GOT_STATUS_MODIFY:
6148 if (a->patch_cb) {
6149 if (status == GOT_STATUS_ADD) {
6150 int choice = GOT_PATCH_CHOICE_NONE;
6151 err = (*a->patch_cb)(&choice, a->patch_arg,
6152 status, ie->path, NULL, 1, 1);
6153 if (err)
6154 break;
6155 if (choice != GOT_PATCH_CHOICE_YES)
6156 break;
6157 } else {
6158 err = create_patched_content(&path_content, 0,
6159 staged_blob_id ? staged_blob_id : blob_id,
6160 ondisk_path, dirfd, de_name, ie->path,
6161 a->repo, a->patch_cb, a->patch_arg);
6162 if (err || path_content == NULL)
6163 break;
6166 err = got_object_blob_create(&new_staged_blob_id,
6167 path_content ? path_content : ondisk_path, a->repo);
6168 if (err)
6169 break;
6170 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6171 SHA1_DIGEST_LENGTH);
6172 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6173 stage = GOT_FILEIDX_STAGE_ADD;
6174 else
6175 stage = GOT_FILEIDX_STAGE_MODIFY;
6176 got_fileindex_entry_stage_set(ie, stage);
6177 a->staged_something = 1;
6178 if (a->status_cb == NULL)
6179 break;
6180 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6181 get_staged_status(ie), relpath, blob_id,
6182 new_staged_blob_id, NULL, dirfd, de_name);
6183 break;
6184 case GOT_STATUS_DELETE:
6185 if (staged_status == GOT_STATUS_DELETE)
6186 break;
6187 if (a->patch_cb) {
6188 int choice = GOT_PATCH_CHOICE_NONE;
6189 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6190 ie->path, NULL, 1, 1);
6191 if (err)
6192 break;
6193 if (choice == GOT_PATCH_CHOICE_NO)
6194 break;
6195 if (choice != GOT_PATCH_CHOICE_YES) {
6196 err = got_error(GOT_ERR_PATCH_CHOICE);
6197 break;
6200 stage = GOT_FILEIDX_STAGE_DELETE;
6201 got_fileindex_entry_stage_set(ie, stage);
6202 a->staged_something = 1;
6203 if (a->status_cb == NULL)
6204 break;
6205 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6206 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6207 de_name);
6208 break;
6209 case GOT_STATUS_NO_CHANGE:
6210 break;
6211 case GOT_STATUS_CONFLICT:
6212 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6213 break;
6214 case GOT_STATUS_NONEXISTENT:
6215 err = got_error_set_errno(ENOENT, relpath);
6216 break;
6217 default:
6218 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6219 break;
6222 if (path_content && unlink(path_content) == -1 && err == NULL)
6223 err = got_error_from_errno2("unlink", path_content);
6224 free(path_content);
6225 free(ondisk_path);
6226 free(new_staged_blob_id);
6227 return err;
6230 const struct got_error *
6231 got_worktree_stage(struct got_worktree *worktree,
6232 struct got_pathlist_head *paths,
6233 got_worktree_status_cb status_cb, void *status_arg,
6234 got_worktree_patch_cb patch_cb, void *patch_arg,
6235 struct got_repository *repo)
6237 const struct got_error *err = NULL, *sync_err, *unlockerr;
6238 struct got_pathlist_entry *pe;
6239 struct got_fileindex *fileindex = NULL;
6240 char *fileindex_path = NULL;
6241 struct got_reference *head_ref = NULL;
6242 struct got_object_id *head_commit_id = NULL;
6243 struct check_stage_ok_arg oka;
6244 struct stage_path_arg spa;
6246 err = lock_worktree(worktree, LOCK_EX);
6247 if (err)
6248 return err;
6250 err = got_ref_open(&head_ref, repo,
6251 got_worktree_get_head_ref_name(worktree), 0);
6252 if (err)
6253 goto done;
6254 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6255 if (err)
6256 goto done;
6257 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6258 if (err)
6259 goto done;
6261 /* Check pre-conditions before staging anything. */
6262 oka.head_commit_id = head_commit_id;
6263 oka.worktree = worktree;
6264 oka.fileindex = fileindex;
6265 oka.repo = repo;
6266 oka.have_changes = 0;
6267 TAILQ_FOREACH(pe, paths, entry) {
6268 err = worktree_status(worktree, pe->path, fileindex, repo,
6269 check_stage_ok, &oka, NULL, NULL, 0, 0);
6270 if (err)
6271 goto done;
6273 if (!oka.have_changes) {
6274 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6275 goto done;
6278 spa.worktree = worktree;
6279 spa.fileindex = fileindex;
6280 spa.repo = repo;
6281 spa.patch_cb = patch_cb;
6282 spa.patch_arg = patch_arg;
6283 spa.status_cb = status_cb;
6284 spa.status_arg = status_arg;
6285 spa.staged_something = 0;
6286 TAILQ_FOREACH(pe, paths, entry) {
6287 err = worktree_status(worktree, pe->path, fileindex, repo,
6288 stage_path, &spa, NULL, NULL, 0, 0);
6289 if (err)
6290 goto done;
6292 if (!spa.staged_something) {
6293 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6294 goto done;
6297 sync_err = sync_fileindex(fileindex, fileindex_path);
6298 if (sync_err && err == NULL)
6299 err = sync_err;
6300 done:
6301 if (head_ref)
6302 got_ref_close(head_ref);
6303 free(head_commit_id);
6304 free(fileindex_path);
6305 if (fileindex)
6306 got_fileindex_free(fileindex);
6307 unlockerr = lock_worktree(worktree, LOCK_SH);
6308 if (unlockerr && err == NULL)
6309 err = unlockerr;
6310 return err;
6313 struct unstage_path_arg {
6314 struct got_worktree *worktree;
6315 struct got_fileindex *fileindex;
6316 struct got_repository *repo;
6317 got_worktree_checkout_cb progress_cb;
6318 void *progress_arg;
6319 got_worktree_patch_cb patch_cb;
6320 void *patch_arg;
6323 static const struct got_error *
6324 create_unstaged_content(char **path_unstaged_content,
6325 char **path_new_staged_content, struct got_object_id *blob_id,
6326 struct got_object_id *staged_blob_id, const char *relpath,
6327 struct got_repository *repo,
6328 got_worktree_patch_cb patch_cb, void *patch_arg)
6330 const struct got_error *err;
6331 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6332 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6333 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6334 struct stat sb1, sb2;
6335 struct got_diff_changes *changes = NULL;
6336 struct got_diff_state *ds = NULL;
6337 struct got_diff_args *args = NULL;
6338 struct got_diff_change *change;
6339 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6340 int have_content = 0, have_rejected_content = 0;
6342 *path_unstaged_content = NULL;
6343 *path_new_staged_content = NULL;
6345 err = got_object_id_str(&label1, blob_id);
6346 if (err)
6347 return err;
6348 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6349 if (err)
6350 goto done;
6352 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6353 if (err)
6354 goto done;
6356 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6357 if (err)
6358 goto done;
6360 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6361 if (err)
6362 goto done;
6364 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6365 if (err)
6366 goto done;
6368 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6369 if (err)
6370 goto done;
6372 if (stat(path1, &sb1) == -1) {
6373 err = got_error_from_errno2("stat", path1);
6374 goto done;
6377 if (stat(path2, &sb2) == -1) {
6378 err = got_error_from_errno2("stat", path2);
6379 goto done;
6382 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6383 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6384 if (err)
6385 goto done;
6387 err = got_opentemp_named(path_unstaged_content, &outfile,
6388 "got-unstaged-content");
6389 if (err)
6390 goto done;
6391 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6392 "got-new-staged-content");
6393 if (err)
6394 goto done;
6396 if (fseek(f1, 0L, SEEK_SET) == -1) {
6397 err = got_ferror(f1, GOT_ERR_IO);
6398 goto done;
6400 if (fseek(f2, 0L, SEEK_SET) == -1) {
6401 err = got_ferror(f2, GOT_ERR_IO);
6402 goto done;
6404 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6405 int choice;
6406 err = apply_or_reject_change(&choice, change, ++n,
6407 changes->nchanges, ds, args, diff_flags, relpath,
6408 f1, f2, &line_cur1, &line_cur2,
6409 outfile, rejectfile, patch_cb, patch_arg);
6410 if (err)
6411 goto done;
6412 if (choice == GOT_PATCH_CHOICE_YES)
6413 have_content = 1;
6414 else
6415 have_rejected_content = 1;
6416 if (choice == GOT_PATCH_CHOICE_QUIT)
6417 break;
6419 if (have_content || have_rejected_content)
6420 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6421 outfile, rejectfile);
6422 done:
6423 free(label1);
6424 if (blob)
6425 got_object_blob_close(blob);
6426 if (staged_blob)
6427 got_object_blob_close(staged_blob);
6428 if (f1 && fclose(f1) == EOF && err == NULL)
6429 err = got_error_from_errno2("fclose", path1);
6430 if (f2 && fclose(f2) == EOF && err == NULL)
6431 err = got_error_from_errno2("fclose", path2);
6432 if (outfile && fclose(outfile) == EOF && err == NULL)
6433 err = got_error_from_errno2("fclose", *path_unstaged_content);
6434 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6435 err = got_error_from_errno2("fclose", *path_new_staged_content);
6436 if (path1 && unlink(path1) == -1 && err == NULL)
6437 err = got_error_from_errno2("unlink", path1);
6438 if (path2 && unlink(path2) == -1 && err == NULL)
6439 err = got_error_from_errno2("unlink", path2);
6440 if (err || !have_content) {
6441 if (*path_unstaged_content &&
6442 unlink(*path_unstaged_content) == -1 && err == NULL)
6443 err = got_error_from_errno2("unlink",
6444 *path_unstaged_content);
6445 free(*path_unstaged_content);
6446 *path_unstaged_content = NULL;
6448 if (err || !have_rejected_content) {
6449 if (*path_new_staged_content &&
6450 unlink(*path_new_staged_content) == -1 && err == NULL)
6451 err = got_error_from_errno2("unlink",
6452 *path_new_staged_content);
6453 free(*path_new_staged_content);
6454 *path_new_staged_content = NULL;
6456 free(args);
6457 if (ds) {
6458 got_diff_state_free(ds);
6459 free(ds);
6461 if (changes)
6462 got_diff_free_changes(changes);
6463 free(path1);
6464 free(path2);
6465 return err;
6468 static const struct got_error *
6469 unstage_path(void *arg, unsigned char status,
6470 unsigned char staged_status, const char *relpath,
6471 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6472 struct got_object_id *commit_id, int dirfd, const char *de_name)
6474 const struct got_error *err = NULL;
6475 struct unstage_path_arg *a = arg;
6476 struct got_fileindex_entry *ie;
6477 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6478 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6479 char *path_new_staged_content = NULL;
6480 char *id_str = NULL, *label_orig = NULL;
6481 int local_changes_subsumed;
6482 struct stat sb;
6484 if (staged_status != GOT_STATUS_ADD &&
6485 staged_status != GOT_STATUS_MODIFY &&
6486 staged_status != GOT_STATUS_DELETE)
6487 return NULL;
6489 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6490 if (ie == NULL)
6491 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6493 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6494 == -1)
6495 return got_error_from_errno("asprintf");
6497 err = got_object_id_str(&id_str,
6498 commit_id ? commit_id : a->worktree->base_commit_id);
6499 if (err)
6500 goto done;
6501 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6502 id_str) == -1) {
6503 err = got_error_from_errno("asprintf");
6504 goto done;
6507 switch (staged_status) {
6508 case GOT_STATUS_MODIFY:
6509 err = got_object_open_as_blob(&blob_base, a->repo,
6510 blob_id, 8192);
6511 if (err)
6512 break;
6513 /* fall through */
6514 case GOT_STATUS_ADD:
6515 if (a->patch_cb) {
6516 if (staged_status == GOT_STATUS_ADD) {
6517 int choice = GOT_PATCH_CHOICE_NONE;
6518 err = (*a->patch_cb)(&choice, a->patch_arg,
6519 staged_status, ie->path, NULL, 1, 1);
6520 if (err)
6521 break;
6522 if (choice != GOT_PATCH_CHOICE_YES)
6523 break;
6524 } else {
6525 err = create_unstaged_content(
6526 &path_unstaged_content,
6527 &path_new_staged_content, blob_id,
6528 staged_blob_id, ie->path, a->repo,
6529 a->patch_cb, a->patch_arg);
6530 if (err || path_unstaged_content == NULL)
6531 break;
6532 if (path_new_staged_content) {
6533 err = got_object_blob_create(
6534 &staged_blob_id,
6535 path_new_staged_content,
6536 a->repo);
6537 if (err)
6538 break;
6539 memcpy(ie->staged_blob_sha1,
6540 staged_blob_id->sha1,
6541 SHA1_DIGEST_LENGTH);
6543 err = merge_file(&local_changes_subsumed,
6544 a->worktree, blob_base, ondisk_path,
6545 relpath, got_fileindex_perms_to_st(ie),
6546 path_unstaged_content, label_orig,
6547 "unstaged", a->repo, a->progress_cb,
6548 a->progress_arg);
6549 if (err == NULL &&
6550 path_new_staged_content == NULL)
6551 got_fileindex_entry_stage_set(ie,
6552 GOT_FILEIDX_STAGE_NONE);
6553 break; /* Done with this file. */
6556 err = got_object_open_as_blob(&blob_staged, a->repo,
6557 staged_blob_id, 8192);
6558 if (err)
6559 break;
6560 err = merge_blob(&local_changes_subsumed, a->worktree,
6561 blob_base, ondisk_path, relpath,
6562 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6563 commit_id ? commit_id : a->worktree->base_commit_id,
6564 a->repo, a->progress_cb, a->progress_arg);
6565 if (err == NULL)
6566 got_fileindex_entry_stage_set(ie,
6567 GOT_FILEIDX_STAGE_NONE);
6568 break;
6569 case GOT_STATUS_DELETE:
6570 if (a->patch_cb) {
6571 int choice = GOT_PATCH_CHOICE_NONE;
6572 err = (*a->patch_cb)(&choice, a->patch_arg,
6573 staged_status, ie->path, NULL, 1, 1);
6574 if (err)
6575 break;
6576 if (choice == GOT_PATCH_CHOICE_NO)
6577 break;
6578 if (choice != GOT_PATCH_CHOICE_YES) {
6579 err = got_error(GOT_ERR_PATCH_CHOICE);
6580 break;
6583 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6584 err = get_file_status(&status, &sb, ie, ondisk_path,
6585 dirfd, de_name, a->repo);
6586 if (err)
6587 break;
6588 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6589 break;
6591 done:
6592 free(ondisk_path);
6593 if (path_unstaged_content &&
6594 unlink(path_unstaged_content) == -1 && err == NULL)
6595 err = got_error_from_errno2("unlink", path_unstaged_content);
6596 if (path_new_staged_content &&
6597 unlink(path_new_staged_content) == -1 && err == NULL)
6598 err = got_error_from_errno2("unlink", path_new_staged_content);
6599 free(path_unstaged_content);
6600 free(path_new_staged_content);
6601 if (blob_base)
6602 got_object_blob_close(blob_base);
6603 if (blob_staged)
6604 got_object_blob_close(blob_staged);
6605 free(id_str);
6606 free(label_orig);
6607 return err;
6610 const struct got_error *
6611 got_worktree_unstage(struct got_worktree *worktree,
6612 struct got_pathlist_head *paths,
6613 got_worktree_checkout_cb progress_cb, void *progress_arg,
6614 got_worktree_patch_cb patch_cb, void *patch_arg,
6615 struct got_repository *repo)
6617 const struct got_error *err = NULL, *sync_err, *unlockerr;
6618 struct got_pathlist_entry *pe;
6619 struct got_fileindex *fileindex = NULL;
6620 char *fileindex_path = NULL;
6621 struct unstage_path_arg upa;
6623 err = lock_worktree(worktree, LOCK_EX);
6624 if (err)
6625 return err;
6627 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6628 if (err)
6629 goto done;
6631 upa.worktree = worktree;
6632 upa.fileindex = fileindex;
6633 upa.repo = repo;
6634 upa.progress_cb = progress_cb;
6635 upa.progress_arg = progress_arg;
6636 upa.patch_cb = patch_cb;
6637 upa.patch_arg = patch_arg;
6638 TAILQ_FOREACH(pe, paths, entry) {
6639 err = worktree_status(worktree, pe->path, fileindex, repo,
6640 unstage_path, &upa, NULL, NULL, 0, 0);
6641 if (err)
6642 goto done;
6645 sync_err = sync_fileindex(fileindex, fileindex_path);
6646 if (sync_err && err == NULL)
6647 err = sync_err;
6648 done:
6649 free(fileindex_path);
6650 if (fileindex)
6651 got_fileindex_free(fileindex);
6652 unlockerr = lock_worktree(worktree, LOCK_SH);
6653 if (unlockerr && err == NULL)
6654 err = unlockerr;
6655 return err;