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, uint16_t te_mode, uint16_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;
1747 err = got_opentemp_named(&new_fileindex_path, &new_index,
1748 fileindex_path);
1749 if (err)
1750 goto done;
1752 err = got_fileindex_write(fileindex, new_index);
1753 if (err)
1754 goto done;
1756 if (rename(new_fileindex_path, fileindex_path) != 0) {
1757 err = got_error_from_errno3("rename", new_fileindex_path,
1758 fileindex_path);
1759 unlink(new_fileindex_path);
1761 done:
1762 if (new_index)
1763 fclose(new_index);
1764 free(new_fileindex_path);
1765 return err;
1768 static const struct got_error *
1769 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1770 struct got_object_id **tree_id, const char *wt_relpath,
1771 struct got_worktree *worktree, struct got_repository *repo)
1773 const struct got_error *err = NULL;
1774 struct got_object_id *id = NULL;
1775 char *in_repo_path = NULL;
1776 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1778 *entry_type = GOT_OBJ_TYPE_ANY;
1779 *tree_relpath = NULL;
1780 *tree_id = NULL;
1782 if (wt_relpath[0] == '\0') {
1783 /* Check out all files within the work tree. */
1784 *entry_type = GOT_OBJ_TYPE_TREE;
1785 *tree_relpath = strdup("");
1786 if (*tree_relpath == NULL) {
1787 err = got_error_from_errno("strdup");
1788 goto done;
1790 err = got_object_id_by_path(tree_id, repo,
1791 worktree->base_commit_id, worktree->path_prefix);
1792 if (err)
1793 goto done;
1794 return NULL;
1797 /* Check out a subset of files in the work tree. */
1799 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1800 is_root_wt ? "" : "/", wt_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1805 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1806 in_repo_path);
1807 if (err)
1808 goto done;
1810 free(in_repo_path);
1811 in_repo_path = NULL;
1813 err = got_object_get_type(entry_type, repo, id);
1814 if (err)
1815 goto done;
1817 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1818 /* Check out a single file. */
1819 if (strchr(wt_relpath, '/') == NULL) {
1820 /* Check out a single file in work tree's root dir. */
1821 in_repo_path = strdup(worktree->path_prefix);
1822 if (in_repo_path == NULL) {
1823 err = got_error_from_errno("strdup");
1824 goto done;
1826 *tree_relpath = strdup("");
1827 if (*tree_relpath == NULL) {
1828 err = got_error_from_errno("strdup");
1829 goto done;
1831 } else {
1832 /* Check out a single file in a subdirectory. */
1833 err = got_path_dirname(tree_relpath, wt_relpath);
1834 if (err)
1835 return err;
1836 if (asprintf(&in_repo_path, "%s%s%s",
1837 worktree->path_prefix, is_root_wt ? "" : "/",
1838 *tree_relpath) == -1) {
1839 err = got_error_from_errno("asprintf");
1840 goto done;
1843 err = got_object_id_by_path(tree_id, repo,
1844 worktree->base_commit_id, in_repo_path);
1845 } else {
1846 /* Check out all files within a subdirectory. */
1847 *tree_id = got_object_id_dup(id);
1848 if (*tree_id == NULL) {
1849 err = got_error_from_errno("got_object_id_dup");
1850 goto done;
1852 *tree_relpath = strdup(wt_relpath);
1853 if (*tree_relpath == NULL) {
1854 err = got_error_from_errno("strdup");
1855 goto done;
1858 done:
1859 free(id);
1860 free(in_repo_path);
1861 if (err) {
1862 *entry_type = GOT_OBJ_TYPE_ANY;
1863 free(*tree_relpath);
1864 *tree_relpath = NULL;
1865 free(*tree_id);
1866 *tree_id = NULL;
1868 return err;
1871 static const struct got_error *
1872 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1873 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1874 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1875 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1877 const struct got_error *err = NULL;
1878 struct got_commit_object *commit = NULL;
1879 struct got_tree_object *tree = NULL;
1880 struct got_fileindex_diff_tree_cb diff_cb;
1881 struct diff_cb_arg arg;
1883 err = ref_base_commit(worktree, repo);
1884 if (err) {
1885 if (!(err->code == GOT_ERR_ERRNO && errno == EACCES))
1886 goto done;
1887 err = (*progress_cb)(progress_arg,
1888 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1889 if (err)
1890 return err;
1893 err = got_object_open_as_commit(&commit, repo,
1894 worktree->base_commit_id);
1895 if (err)
1896 goto done;
1898 err = got_object_open_as_tree(&tree, repo, tree_id);
1899 if (err)
1900 goto done;
1902 if (entry_name &&
1903 got_object_tree_find_entry(tree, entry_name) == NULL) {
1904 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1905 goto done;
1908 diff_cb.diff_old_new = diff_old_new;
1909 diff_cb.diff_old = diff_old;
1910 diff_cb.diff_new = diff_new;
1911 arg.fileindex = fileindex;
1912 arg.worktree = worktree;
1913 arg.repo = repo;
1914 arg.progress_cb = progress_cb;
1915 arg.progress_arg = progress_arg;
1916 arg.cancel_cb = cancel_cb;
1917 arg.cancel_arg = cancel_arg;
1918 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1919 entry_name, repo, &diff_cb, &arg);
1920 done:
1921 if (tree)
1922 got_object_tree_close(tree);
1923 if (commit)
1924 got_object_commit_close(commit);
1925 return err;
1928 const struct got_error *
1929 got_worktree_checkout_files(struct got_worktree *worktree,
1930 struct got_pathlist_head *paths, struct got_repository *repo,
1931 got_worktree_checkout_cb progress_cb, void *progress_arg,
1932 got_cancel_cb cancel_cb, void *cancel_arg)
1934 const struct got_error *err = NULL, *sync_err, *unlockerr;
1935 struct got_commit_object *commit = NULL;
1936 struct got_tree_object *tree = NULL;
1937 struct got_fileindex *fileindex = NULL;
1938 char *fileindex_path = NULL;
1939 struct got_pathlist_entry *pe;
1940 struct tree_path_data {
1941 SIMPLEQ_ENTRY(tree_path_data) entry;
1942 struct got_object_id *tree_id;
1943 int entry_type;
1944 char *relpath;
1945 char *entry_name;
1946 } *tpd = NULL;
1947 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1949 SIMPLEQ_INIT(&tree_paths);
1951 err = lock_worktree(worktree, LOCK_EX);
1952 if (err)
1953 return err;
1955 /* Map all specified paths to in-repository trees. */
1956 TAILQ_FOREACH(pe, paths, entry) {
1957 tpd = malloc(sizeof(*tpd));
1958 if (tpd == NULL) {
1959 err = got_error_from_errno("malloc");
1960 goto done;
1963 err = find_tree_entry_for_checkout(&tpd->entry_type,
1964 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1965 if (err) {
1966 free(tpd);
1967 goto done;
1970 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1971 err = got_path_basename(&tpd->entry_name, pe->path);
1972 if (err) {
1973 free(tpd->relpath);
1974 free(tpd->tree_id);
1975 free(tpd);
1976 goto done;
1978 } else
1979 tpd->entry_name = NULL;
1981 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1985 * Read the file index.
1986 * Checking out files is supposed to be an idempotent operation.
1987 * If the on-disk file index is incomplete we will try to complete it.
1989 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1990 if (err)
1991 goto done;
1993 tpd = SIMPLEQ_FIRST(&tree_paths);
1994 TAILQ_FOREACH(pe, paths, entry) {
1995 struct bump_base_commit_id_arg bbc_arg;
1997 err = checkout_files(worktree, fileindex, tpd->relpath,
1998 tpd->tree_id, tpd->entry_name, repo,
1999 progress_cb, progress_arg, cancel_cb, cancel_arg);
2000 if (err)
2001 break;
2003 bbc_arg.base_commit_id = worktree->base_commit_id;
2004 bbc_arg.entry_name = tpd->entry_name;
2005 bbc_arg.path = pe->path;
2006 bbc_arg.path_len = pe->path_len;
2007 bbc_arg.progress_cb = progress_cb;
2008 bbc_arg.progress_arg = progress_arg;
2009 err = got_fileindex_for_each_entry_safe(fileindex,
2010 bump_base_commit_id, &bbc_arg);
2011 if (err)
2012 break;
2014 tpd = SIMPLEQ_NEXT(tpd, entry);
2016 sync_err = sync_fileindex(fileindex, fileindex_path);
2017 if (sync_err && err == NULL)
2018 err = sync_err;
2019 done:
2020 free(fileindex_path);
2021 if (tree)
2022 got_object_tree_close(tree);
2023 if (commit)
2024 got_object_commit_close(commit);
2025 if (fileindex)
2026 got_fileindex_free(fileindex);
2027 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2028 tpd = SIMPLEQ_FIRST(&tree_paths);
2029 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2030 free(tpd->relpath);
2031 free(tpd->tree_id);
2032 free(tpd);
2034 unlockerr = lock_worktree(worktree, LOCK_SH);
2035 if (unlockerr && err == NULL)
2036 err = unlockerr;
2037 return err;
2040 struct merge_file_cb_arg {
2041 struct got_worktree *worktree;
2042 struct got_fileindex *fileindex;
2043 got_worktree_checkout_cb progress_cb;
2044 void *progress_arg;
2045 got_cancel_cb cancel_cb;
2046 void *cancel_arg;
2047 const char *label_orig;
2048 struct got_object_id *commit_id2;
2051 static const struct got_error *
2052 merge_file_cb(void *arg, struct got_blob_object *blob1,
2053 struct got_blob_object *blob2, struct got_object_id *id1,
2054 struct got_object_id *id2, const char *path1, const char *path2,
2055 mode_t mode1, mode_t mode2, struct got_repository *repo)
2057 static const struct got_error *err = NULL;
2058 struct merge_file_cb_arg *a = arg;
2059 struct got_fileindex_entry *ie;
2060 char *ondisk_path = NULL;
2061 struct stat sb;
2062 unsigned char status;
2063 int local_changes_subsumed;
2065 if (blob1 && blob2) {
2066 ie = got_fileindex_entry_get(a->fileindex, path2,
2067 strlen(path2));
2068 if (ie == NULL)
2069 return (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_MISSING, path2);
2072 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2073 path2) == -1)
2074 return got_error_from_errno("asprintf");
2076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2077 repo);
2078 if (err)
2079 goto done;
2081 if (status == GOT_STATUS_DELETE) {
2082 err = (*a->progress_cb)(a->progress_arg,
2083 GOT_STATUS_MERGE, path2);
2084 goto done;
2086 if (status != GOT_STATUS_NO_CHANGE &&
2087 status != GOT_STATUS_MODIFY &&
2088 status != GOT_STATUS_CONFLICT &&
2089 status != GOT_STATUS_ADD) {
2090 err = (*a->progress_cb)(a->progress_arg, status, path2);
2091 goto done;
2094 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2095 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2096 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2097 } else if (blob1) {
2098 ie = got_fileindex_entry_get(a->fileindex, path1,
2099 strlen(path1));
2100 if (ie == NULL)
2101 return (*a->progress_cb)(a->progress_arg,
2102 GOT_STATUS_MISSING, path2);
2104 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2105 path1) == -1)
2106 return got_error_from_errno("asprintf");
2108 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2109 repo);
2110 if (err)
2111 goto done;
2113 switch (status) {
2114 case GOT_STATUS_NO_CHANGE:
2115 err = (*a->progress_cb)(a->progress_arg,
2116 GOT_STATUS_DELETE, path1);
2117 if (err)
2118 goto done;
2119 err = remove_ondisk_file(a->worktree->root_path, path1);
2120 if (err)
2121 goto done;
2122 if (ie)
2123 got_fileindex_entry_mark_deleted_from_disk(ie);
2124 break;
2125 case GOT_STATUS_DELETE:
2126 case GOT_STATUS_MISSING:
2127 err = (*a->progress_cb)(a->progress_arg,
2128 GOT_STATUS_DELETE, path1);
2129 if (err)
2130 goto done;
2131 if (ie)
2132 got_fileindex_entry_mark_deleted_from_disk(ie);
2133 break;
2134 case GOT_STATUS_ADD:
2135 case GOT_STATUS_MODIFY:
2136 case GOT_STATUS_CONFLICT:
2137 err = (*a->progress_cb)(a->progress_arg,
2138 GOT_STATUS_CANNOT_DELETE, path1);
2139 if (err)
2140 goto done;
2141 break;
2142 case GOT_STATUS_OBSTRUCTED:
2143 err = (*a->progress_cb)(a->progress_arg, status, path1);
2144 if (err)
2145 goto done;
2146 break;
2147 default:
2148 break;
2150 } else if (blob2) {
2151 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2152 path2) == -1)
2153 return got_error_from_errno("asprintf");
2154 ie = got_fileindex_entry_get(a->fileindex, path2,
2155 strlen(path2));
2156 if (ie) {
2157 err = get_file_status(&status, &sb, ie, ondisk_path,
2158 -1, NULL, repo);
2159 if (err)
2160 goto done;
2161 if (status != GOT_STATUS_NO_CHANGE &&
2162 status != GOT_STATUS_MODIFY &&
2163 status != GOT_STATUS_CONFLICT &&
2164 status != GOT_STATUS_ADD) {
2165 err = (*a->progress_cb)(a->progress_arg,
2166 status, path2);
2167 goto done;
2169 err = merge_blob(&local_changes_subsumed, a->worktree,
2170 NULL, ondisk_path, path2, sb.st_mode,
2171 a->label_orig, blob2, a->commit_id2, repo,
2172 a->progress_cb,
2173 a->progress_arg);
2174 if (status == GOT_STATUS_DELETE) {
2175 err = update_blob_fileindex_entry(a->worktree,
2176 a->fileindex, ie, ondisk_path, ie->path,
2177 blob2, 0);
2178 if (err)
2179 goto done;
2181 } else {
2182 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2183 err = install_blob(a->worktree, ondisk_path, path2,
2184 /* XXX get this from parent tree! */
2185 GOT_DEFAULT_FILE_MODE,
2186 sb.st_mode, blob2, 0, 0, repo,
2187 a->progress_cb, a->progress_arg);
2188 if (err)
2189 goto done;
2190 err = got_fileindex_entry_alloc(&ie,
2191 ondisk_path, path2, NULL, NULL);
2192 if (err)
2193 goto done;
2194 err = got_fileindex_entry_add(a->fileindex, ie);
2195 if (err) {
2196 got_fileindex_entry_free(ie);
2197 goto done;
2201 done:
2202 free(ondisk_path);
2203 return err;
2206 struct check_merge_ok_arg {
2207 struct got_worktree *worktree;
2208 struct got_repository *repo;
2211 static const struct got_error *
2212 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2214 const struct got_error *err = NULL;
2215 struct check_merge_ok_arg *a = arg;
2216 unsigned char status;
2217 struct stat sb;
2218 char *ondisk_path;
2220 /* Reject merges into a work tree with mixed base commits. */
2221 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2222 SHA1_DIGEST_LENGTH))
2223 return got_error(GOT_ERR_MIXED_COMMITS);
2225 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2226 == -1)
2227 return got_error_from_errno("asprintf");
2229 /* Reject merges into a work tree with conflicted files. */
2230 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2231 if (err)
2232 return err;
2233 if (status == GOT_STATUS_CONFLICT)
2234 return got_error(GOT_ERR_CONFLICTS);
2236 return NULL;
2239 static const struct got_error *
2240 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2241 const char *fileindex_path, struct got_object_id *commit_id1,
2242 struct got_object_id *commit_id2, struct got_repository *repo,
2243 got_worktree_checkout_cb progress_cb, void *progress_arg,
2244 got_cancel_cb cancel_cb, void *cancel_arg)
2246 const struct got_error *err = NULL, *sync_err;
2247 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2248 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2249 struct merge_file_cb_arg arg;
2250 char *label_orig = NULL;
2252 if (commit_id1) {
2253 char *id_str;
2255 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2256 worktree->path_prefix);
2257 if (err)
2258 goto done;
2260 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2261 if (err)
2262 goto done;
2264 err = got_object_id_str(&id_str, commit_id1);
2265 if (err)
2266 goto done;
2268 if (asprintf(&label_orig, "%s: commit %s",
2269 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2270 err = got_error_from_errno("asprintf");
2271 free(id_str);
2272 goto done;
2274 free(id_str);
2277 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2278 worktree->path_prefix);
2279 if (err)
2280 goto done;
2282 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2283 if (err)
2284 goto done;
2286 arg.worktree = worktree;
2287 arg.fileindex = fileindex;
2288 arg.progress_cb = progress_cb;
2289 arg.progress_arg = progress_arg;
2290 arg.cancel_cb = cancel_cb;
2291 arg.cancel_arg = cancel_arg;
2292 arg.label_orig = label_orig;
2293 arg.commit_id2 = commit_id2;
2294 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2295 sync_err = sync_fileindex(fileindex, fileindex_path);
2296 if (sync_err && err == NULL)
2297 err = sync_err;
2298 done:
2299 if (tree1)
2300 got_object_tree_close(tree1);
2301 if (tree2)
2302 got_object_tree_close(tree2);
2303 free(label_orig);
2304 return err;
2307 const struct got_error *
2308 got_worktree_merge_files(struct got_worktree *worktree,
2309 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2310 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2311 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2313 const struct got_error *err, *unlockerr;
2314 char *fileindex_path = NULL;
2315 struct got_fileindex *fileindex = NULL;
2316 struct check_merge_ok_arg mok_arg;
2318 err = lock_worktree(worktree, LOCK_EX);
2319 if (err)
2320 return err;
2322 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2323 if (err)
2324 goto done;
2326 mok_arg.worktree = worktree;
2327 mok_arg.repo = repo;
2328 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2329 &mok_arg);
2330 if (err)
2331 goto done;
2333 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2334 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2335 done:
2336 if (fileindex)
2337 got_fileindex_free(fileindex);
2338 free(fileindex_path);
2339 unlockerr = lock_worktree(worktree, LOCK_SH);
2340 if (unlockerr && err == NULL)
2341 err = unlockerr;
2342 return err;
2345 struct diff_dir_cb_arg {
2346 struct got_fileindex *fileindex;
2347 struct got_worktree *worktree;
2348 const char *status_path;
2349 size_t status_path_len;
2350 struct got_repository *repo;
2351 got_worktree_status_cb status_cb;
2352 void *status_arg;
2353 got_cancel_cb cancel_cb;
2354 void *cancel_arg;
2355 /* A pathlist containing per-directory pathlists of ignore patterns. */
2356 struct got_pathlist_head ignores;
2357 int report_unchanged;
2360 static const struct got_error *
2361 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2362 int dirfd, const char *de_name,
2363 got_worktree_status_cb status_cb, void *status_arg,
2364 struct got_repository *repo, int report_unchanged)
2366 const struct got_error *err = NULL;
2367 unsigned char status = GOT_STATUS_NO_CHANGE;
2368 unsigned char staged_status = get_staged_status(ie);
2369 struct stat sb;
2370 struct got_object_id blob_id, commit_id, staged_blob_id;
2371 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2372 struct got_object_id *staged_blob_idp = NULL;
2374 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2375 if (err)
2376 return err;
2378 if (status == GOT_STATUS_NO_CHANGE &&
2379 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2380 return NULL;
2382 if (got_fileindex_entry_has_blob(ie)) {
2383 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2384 blob_idp = &blob_id;
2386 if (got_fileindex_entry_has_commit(ie)) {
2387 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2388 commit_idp = &commit_id;
2390 if (staged_status == GOT_STATUS_ADD ||
2391 staged_status == GOT_STATUS_MODIFY) {
2392 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2393 SHA1_DIGEST_LENGTH);
2394 staged_blob_idp = &staged_blob_id;
2397 return (*status_cb)(status_arg, status, staged_status,
2398 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2401 static const struct got_error *
2402 status_old_new(void *arg, struct got_fileindex_entry *ie,
2403 struct dirent *de, const char *parent_path, int dirfd)
2405 const struct got_error *err = NULL;
2406 struct diff_dir_cb_arg *a = arg;
2407 char *abspath;
2409 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2410 return got_error(GOT_ERR_CANCELLED);
2412 if (got_path_cmp(parent_path, a->status_path,
2413 strlen(parent_path), a->status_path_len) != 0 &&
2414 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2415 return NULL;
2417 if (parent_path[0]) {
2418 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2419 parent_path, de->d_name) == -1)
2420 return got_error_from_errno("asprintf");
2421 } else {
2422 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2423 de->d_name) == -1)
2424 return got_error_from_errno("asprintf");
2427 err = report_file_status(ie, abspath, dirfd, de->d_name,
2428 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2429 free(abspath);
2430 return err;
2433 static const struct got_error *
2434 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2436 struct diff_dir_cb_arg *a = arg;
2437 struct got_object_id blob_id, commit_id;
2438 unsigned char status;
2440 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2441 return got_error(GOT_ERR_CANCELLED);
2443 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2444 return NULL;
2446 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2447 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2448 if (got_fileindex_entry_has_file_on_disk(ie))
2449 status = GOT_STATUS_MISSING;
2450 else
2451 status = GOT_STATUS_DELETE;
2452 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2453 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2456 void
2457 free_ignorelist(struct got_pathlist_head *ignorelist)
2459 struct got_pathlist_entry *pe;
2461 TAILQ_FOREACH(pe, ignorelist, entry)
2462 free((char *)pe->path);
2463 got_pathlist_free(ignorelist);
2466 void
2467 free_ignores(struct got_pathlist_head *ignores)
2469 struct got_pathlist_entry *pe;
2471 TAILQ_FOREACH(pe, ignores, entry) {
2472 struct got_pathlist_head *ignorelist = pe->data;
2473 free_ignorelist(ignorelist);
2474 free((char *)pe->path);
2476 got_pathlist_free(ignores);
2479 static const struct got_error *
2480 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2482 const struct got_error *err = NULL;
2483 struct got_pathlist_entry *pe = NULL;
2484 struct got_pathlist_head *ignorelist;
2485 char *line = NULL, *pattern, *dirpath = NULL;
2486 size_t linesize = 0;
2487 ssize_t linelen;
2489 ignorelist = calloc(1, sizeof(*ignorelist));
2490 if (ignorelist == NULL)
2491 return got_error_from_errno("calloc");
2492 TAILQ_INIT(ignorelist);
2494 while ((linelen = getline(&line, &linesize, f)) != -1) {
2495 if (linelen > 0 && line[linelen - 1] == '\n')
2496 line[linelen - 1] = '\0';
2498 /* Git's ignores may contain comments. */
2499 if (line[0] == '#')
2500 continue;
2502 /* Git's negated patterns are not (yet?) supported. */
2503 if (line[0] == '!')
2504 continue;
2506 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2507 line) == -1) {
2508 err = got_error_from_errno("asprintf");
2509 goto done;
2511 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2512 if (err)
2513 goto done;
2515 if (ferror(f)) {
2516 err = got_error_from_errno("getline");
2517 goto done;
2520 dirpath = strdup(path);
2521 if (dirpath == NULL) {
2522 err = got_error_from_errno("strdup");
2523 goto done;
2525 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2526 done:
2527 free(line);
2528 if (err || pe == NULL) {
2529 free(dirpath);
2530 free_ignorelist(ignorelist);
2532 return err;
2535 int
2536 match_ignores(struct got_pathlist_head *ignores, const char *path)
2538 struct got_pathlist_entry *pe;
2540 /* Handle patterns which match in all directories. */
2541 TAILQ_FOREACH(pe, ignores, entry) {
2542 struct got_pathlist_head *ignorelist = pe->data;
2543 struct got_pathlist_entry *pi;
2545 TAILQ_FOREACH(pi, ignorelist, entry) {
2546 const char *p, *pattern = pi->path;
2548 if (strncmp(pattern, "**/", 3) != 0)
2549 continue;
2550 pattern += 3;
2551 p = path;
2552 while (*p) {
2553 if (fnmatch(pattern, p,
2554 FNM_PATHNAME | FNM_LEADING_DIR)) {
2555 /* Retry in next directory. */
2556 while (*p && *p != '/')
2557 p++;
2558 while (*p == '/')
2559 p++;
2560 continue;
2562 return 1;
2568 * The ignores pathlist contains ignore lists from children before
2569 * parents, so we can find the most specific ignorelist by walking
2570 * ignores backwards.
2572 pe = TAILQ_LAST(ignores, got_pathlist_head);
2573 while (pe) {
2574 if (got_path_is_child(path, pe->path, pe->path_len)) {
2575 struct got_pathlist_head *ignorelist = pe->data;
2576 struct got_pathlist_entry *pi;
2577 TAILQ_FOREACH(pi, ignorelist, entry) {
2578 const char *pattern = pi->path;
2579 int flags = FNM_LEADING_DIR;
2580 if (strstr(pattern, "/**/") == NULL)
2581 flags |= FNM_PATHNAME;
2582 if (fnmatch(pattern, path, flags))
2583 continue;
2584 return 1;
2587 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2590 return 0;
2593 static const struct got_error *
2594 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2595 const char *path, int dirfd, const char *ignores_filename)
2597 const struct got_error *err = NULL;
2598 char *ignorespath;
2599 int fd = -1;
2600 FILE *ignoresfile = NULL;
2602 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2603 path[0] ? "/" : "", ignores_filename) == -1)
2604 return got_error_from_errno("asprintf");
2606 if (dirfd != -1) {
2607 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2608 if (fd == -1) {
2609 if (errno != ENOENT && errno != EACCES)
2610 err = got_error_from_errno2("openat",
2611 ignorespath);
2612 } else {
2613 ignoresfile = fdopen(fd, "r");
2614 if (ignoresfile == NULL)
2615 err = got_error_from_errno2("fdopen",
2616 ignorespath);
2617 else {
2618 fd = -1;
2619 err = read_ignores(ignores, path, ignoresfile);
2622 } else {
2623 ignoresfile = fopen(ignorespath, "r");
2624 if (ignoresfile == NULL) {
2625 if (errno != ENOENT && errno != EACCES)
2626 err = got_error_from_errno2("fopen",
2627 ignorespath);
2628 } else
2629 err = read_ignores(ignores, path, ignoresfile);
2632 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2633 err = got_error_from_errno2("fclose", path);
2634 if (fd != -1 && close(fd) == -1 && err == NULL)
2635 err = got_error_from_errno2("close", path);
2636 free(ignorespath);
2637 return err;
2640 static const struct got_error *
2641 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2643 const struct got_error *err = NULL;
2644 struct diff_dir_cb_arg *a = arg;
2645 char *path = NULL;
2647 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2648 return got_error(GOT_ERR_CANCELLED);
2650 /* XXX ignore symlinks for now */
2651 if (de->d_type == DT_LNK)
2652 return NULL;
2654 if (parent_path[0]) {
2655 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2656 return got_error_from_errno("asprintf");
2657 } else {
2658 path = de->d_name;
2661 if (de->d_type == DT_DIR) {
2662 int subdirfd = openat(dirfd, de->d_name,
2663 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2664 if (subdirfd == -1) {
2665 if (errno != ENOENT && errno != EACCES)
2666 err = got_error_from_errno2("openat", path);
2667 } else {
2668 err = add_ignores(&a->ignores, a->worktree->root_path,
2669 path, subdirfd, ".cvsignore");
2670 if (err == NULL)
2671 err = add_ignores(&a->ignores,
2672 a->worktree->root_path, path,
2673 subdirfd, ".gitignore");
2674 if (close(subdirfd) == -1 && err == NULL)
2675 err = got_error_from_errno2("close", path);
2677 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2678 && !match_ignores(&a->ignores, path))
2679 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2680 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2681 if (parent_path[0])
2682 free(path);
2683 return err;
2686 static const struct got_error *
2687 report_single_file_status(const char *path, const char *ondisk_path,
2688 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2689 void *status_arg, struct got_repository *repo, int report_unchanged)
2691 struct got_fileindex_entry *ie;
2692 struct stat sb;
2694 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2695 if (ie)
2696 return report_file_status(ie, ondisk_path, -1, NULL,
2697 status_cb, status_arg, repo, report_unchanged);
2699 if (lstat(ondisk_path, &sb) == -1) {
2700 if (errno != ENOENT)
2701 return got_error_from_errno2("lstat", ondisk_path);
2702 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2703 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2704 return NULL;
2707 if (S_ISREG(sb.st_mode))
2708 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2709 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2711 return NULL;
2714 static const struct got_error *
2715 worktree_status(struct got_worktree *worktree, const char *path,
2716 struct got_fileindex *fileindex, struct got_repository *repo,
2717 got_worktree_status_cb status_cb, void *status_arg,
2718 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2719 int report_unchanged)
2721 const struct got_error *err = NULL;
2722 int fd = -1;
2723 struct got_fileindex_diff_dir_cb fdiff_cb;
2724 struct diff_dir_cb_arg arg;
2725 char *ondisk_path = NULL;
2727 if (asprintf(&ondisk_path, "%s%s%s",
2728 worktree->root_path, path[0] ? "/" : "", path) == -1)
2729 return got_error_from_errno("asprintf");
2731 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2732 if (fd == -1) {
2733 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2734 err = got_error_from_errno2("open", ondisk_path);
2735 else
2736 err = report_single_file_status(path, ondisk_path,
2737 fileindex, status_cb, status_arg, repo,
2738 report_unchanged);
2739 } else {
2740 fdiff_cb.diff_old_new = status_old_new;
2741 fdiff_cb.diff_old = status_old;
2742 fdiff_cb.diff_new = status_new;
2743 arg.fileindex = fileindex;
2744 arg.worktree = worktree;
2745 arg.status_path = path;
2746 arg.status_path_len = strlen(path);
2747 arg.repo = repo;
2748 arg.status_cb = status_cb;
2749 arg.status_arg = status_arg;
2750 arg.cancel_cb = cancel_cb;
2751 arg.cancel_arg = cancel_arg;
2752 arg.report_unchanged = report_unchanged;
2753 TAILQ_INIT(&arg.ignores);
2754 if (!no_ignores) {
2755 err = add_ignores(&arg.ignores, worktree->root_path,
2756 path, fd, ".cvsignore");
2757 if (err == NULL)
2758 err = add_ignores(&arg.ignores,
2759 worktree->root_path, path, fd,
2760 ".gitignore");
2762 if (err == NULL)
2763 err = got_fileindex_diff_dir(fileindex, fd,
2764 worktree->root_path, path, repo, &fdiff_cb, &arg);
2765 free_ignores(&arg.ignores);
2768 if (fd != -1 && close(fd) != 0 && err == NULL)
2769 err = got_error_from_errno("close");
2770 free(ondisk_path);
2771 return err;
2774 const struct got_error *
2775 got_worktree_status(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, struct got_repository *repo,
2777 got_worktree_status_cb status_cb, void *status_arg,
2778 got_cancel_cb cancel_cb, void *cancel_arg)
2780 const struct got_error *err = NULL;
2781 char *fileindex_path = NULL;
2782 struct got_fileindex *fileindex = NULL;
2783 struct got_pathlist_entry *pe;
2785 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2786 if (err)
2787 return err;
2789 TAILQ_FOREACH(pe, paths, entry) {
2790 err = worktree_status(worktree, pe->path, fileindex, repo,
2791 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2792 if (err)
2793 break;
2795 free(fileindex_path);
2796 got_fileindex_free(fileindex);
2797 return err;
2800 const struct got_error *
2801 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2802 const char *arg)
2804 const struct got_error *err = NULL;
2805 char *resolved, *cwd = NULL, *path = NULL;
2806 size_t len;
2808 *wt_path = NULL;
2810 resolved = realpath(arg, NULL);
2811 if (resolved == NULL) {
2812 if (errno != ENOENT)
2813 return got_error_from_errno2("realpath", arg);
2814 cwd = getcwd(NULL, 0);
2815 if (cwd == NULL)
2816 return got_error_from_errno("getcwd");
2817 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2818 err = got_error_from_errno("asprintf");
2819 goto done;
2823 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2824 strlen(got_worktree_get_root_path(worktree)))) {
2825 err = got_error(GOT_ERR_BAD_PATH);
2826 goto done;
2829 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2830 err = got_path_skip_common_ancestor(&path,
2831 got_worktree_get_root_path(worktree), resolved);
2832 if (err)
2833 goto done;
2834 } else {
2835 path = strdup("");
2836 if (path == NULL) {
2837 err = got_error_from_errno("strdup");
2838 goto done;
2842 /* XXX status walk can't deal with trailing slash! */
2843 len = strlen(path);
2844 while (len > 0 && path[len - 1] == '/') {
2845 path[len - 1] = '\0';
2846 len--;
2848 done:
2849 free(resolved);
2850 free(cwd);
2851 if (err == NULL)
2852 *wt_path = path;
2853 else
2854 free(path);
2855 return err;
2858 struct schedule_addition_args {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2862 void *progress_arg;
2863 struct got_repository *repo;
2866 static const struct got_error *
2867 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2868 const char *relpath, struct got_object_id *blob_id,
2869 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2870 int dirfd, const char *de_name)
2872 struct schedule_addition_args *a = arg;
2873 const struct got_error *err = NULL;
2874 struct got_fileindex_entry *ie;
2875 struct stat sb;
2876 char *ondisk_path;
2878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2879 relpath) == -1)
2880 return got_error_from_errno("asprintf");
2882 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2883 if (ie) {
2884 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2885 de_name, a->repo);
2886 if (err)
2887 goto done;
2888 /* Re-adding an existing entry is a no-op. */
2889 if (status == GOT_STATUS_ADD)
2890 goto done;
2891 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2892 if (err)
2893 goto done;
2896 if (status != GOT_STATUS_UNVERSIONED) {
2897 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2898 goto done;
2901 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2902 if (err)
2903 goto done;
2905 err = got_fileindex_entry_add(a->fileindex, ie);
2906 if (err) {
2907 got_fileindex_entry_free(ie);
2908 goto done;
2910 done:
2911 free(ondisk_path);
2912 if (err)
2913 return err;
2914 if (status == GOT_STATUS_ADD)
2915 return NULL;
2916 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2919 const struct got_error *
2920 got_worktree_schedule_add(struct got_worktree *worktree,
2921 struct got_pathlist_head *paths,
2922 got_worktree_checkout_cb progress_cb, void *progress_arg,
2923 struct got_repository *repo, int no_ignores)
2925 struct got_fileindex *fileindex = NULL;
2926 char *fileindex_path = NULL;
2927 const struct got_error *err = NULL, *sync_err, *unlockerr;
2928 struct got_pathlist_entry *pe;
2929 struct schedule_addition_args saa;
2931 err = lock_worktree(worktree, LOCK_EX);
2932 if (err)
2933 return err;
2935 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2936 if (err)
2937 goto done;
2939 saa.worktree = worktree;
2940 saa.fileindex = fileindex;
2941 saa.progress_cb = progress_cb;
2942 saa.progress_arg = progress_arg;
2943 saa.repo = repo;
2945 TAILQ_FOREACH(pe, paths, entry) {
2946 err = worktree_status(worktree, pe->path, fileindex, repo,
2947 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2948 if (err)
2949 break;
2951 sync_err = sync_fileindex(fileindex, fileindex_path);
2952 if (sync_err && err == NULL)
2953 err = sync_err;
2954 done:
2955 free(fileindex_path);
2956 if (fileindex)
2957 got_fileindex_free(fileindex);
2958 unlockerr = lock_worktree(worktree, LOCK_SH);
2959 if (unlockerr && err == NULL)
2960 err = unlockerr;
2961 return err;
2964 struct schedule_deletion_args {
2965 struct got_worktree *worktree;
2966 struct got_fileindex *fileindex;
2967 got_worktree_delete_cb progress_cb;
2968 void *progress_arg;
2969 struct got_repository *repo;
2970 int delete_local_mods;
2971 int keep_on_disk;
2974 static const struct got_error *
2975 schedule_for_deletion(void *arg, unsigned char status,
2976 unsigned char staged_status, const char *relpath,
2977 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2978 struct got_object_id *commit_id, int dirfd, const char *de_name)
2980 struct schedule_deletion_args *a = arg;
2981 const struct got_error *err = NULL;
2982 struct got_fileindex_entry *ie = NULL;
2983 struct stat sb;
2984 char *ondisk_path;
2986 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2987 if (ie == NULL)
2988 return got_error(GOT_ERR_BAD_PATH);
2990 staged_status = get_staged_status(ie);
2991 if (staged_status != GOT_STATUS_NO_CHANGE) {
2992 if (staged_status == GOT_STATUS_DELETE)
2993 return NULL;
2994 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2998 relpath) == -1)
2999 return got_error_from_errno("asprintf");
3001 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3002 a->repo);
3003 if (err)
3004 goto done;
3006 if (status != GOT_STATUS_NO_CHANGE) {
3007 if (status == GOT_STATUS_DELETE)
3008 goto done;
3009 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3010 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3011 goto done;
3013 if (status != GOT_STATUS_MODIFY &&
3014 status != GOT_STATUS_MISSING) {
3015 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3016 goto done;
3020 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3021 if (dirfd != -1) {
3022 if (unlinkat(dirfd, de_name, 0) != 0) {
3023 err = got_error_from_errno2("unlinkat",
3024 ondisk_path);
3025 goto done;
3027 } else if (unlink(ondisk_path) != 0) {
3028 err = got_error_from_errno2("unlink", ondisk_path);
3029 goto done;
3033 got_fileindex_entry_mark_deleted_from_disk(ie);
3034 done:
3035 free(ondisk_path);
3036 if (err)
3037 return err;
3038 if (status == GOT_STATUS_DELETE)
3039 return NULL;
3040 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3041 staged_status, relpath);
3044 const struct got_error *
3045 got_worktree_schedule_delete(struct got_worktree *worktree,
3046 struct got_pathlist_head *paths, int delete_local_mods,
3047 got_worktree_delete_cb progress_cb, void *progress_arg,
3048 struct got_repository *repo, int keep_on_disk)
3050 struct got_fileindex *fileindex = NULL;
3051 char *fileindex_path = NULL;
3052 const struct got_error *err = NULL, *sync_err, *unlockerr;
3053 struct got_pathlist_entry *pe;
3054 struct schedule_deletion_args sda;
3056 err = lock_worktree(worktree, LOCK_EX);
3057 if (err)
3058 return err;
3060 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3061 if (err)
3062 goto done;
3064 sda.worktree = worktree;
3065 sda.fileindex = fileindex;
3066 sda.progress_cb = progress_cb;
3067 sda.progress_arg = progress_arg;
3068 sda.repo = repo;
3069 sda.delete_local_mods = delete_local_mods;
3070 sda.keep_on_disk = keep_on_disk;
3072 TAILQ_FOREACH(pe, paths, entry) {
3073 err = worktree_status(worktree, pe->path, fileindex, repo,
3074 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3075 if (err)
3076 break;
3078 sync_err = sync_fileindex(fileindex, fileindex_path);
3079 if (sync_err && err == NULL)
3080 err = sync_err;
3081 done:
3082 free(fileindex_path);
3083 if (fileindex)
3084 got_fileindex_free(fileindex);
3085 unlockerr = lock_worktree(worktree, LOCK_SH);
3086 if (unlockerr && err == NULL)
3087 err = unlockerr;
3088 return err;
3091 static const struct got_error *
3092 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3094 const struct got_error *err = NULL;
3095 char *line = NULL;
3096 size_t linesize = 0, n;
3097 ssize_t linelen;
3099 linelen = getline(&line, &linesize, infile);
3100 if (linelen == -1) {
3101 if (ferror(infile)) {
3102 err = got_error_from_errno("getline");
3103 goto done;
3105 return NULL;
3107 if (outfile) {
3108 n = fwrite(line, 1, linelen, outfile);
3109 if (n != linelen) {
3110 err = got_ferror(outfile, GOT_ERR_IO);
3111 goto done;
3114 if (rejectfile) {
3115 n = fwrite(line, 1, linelen, rejectfile);
3116 if (n != linelen)
3117 err = got_ferror(outfile, GOT_ERR_IO);
3119 done:
3120 free(line);
3121 return err;
3124 static const struct got_error *
3125 skip_one_line(FILE *f)
3127 char *line = NULL;
3128 size_t linesize = 0;
3129 ssize_t linelen;
3131 linelen = getline(&line, &linesize, f);
3132 if (linelen == -1) {
3133 if (ferror(f))
3134 return got_error_from_errno("getline");
3135 return NULL;
3137 free(line);
3138 return NULL;
3141 static const struct got_error *
3142 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3143 int start_old, int end_old, int start_new, int end_new,
3144 FILE *outfile, FILE *rejectfile)
3146 const struct got_error *err;
3148 /* Copy old file's lines leading up to patch. */
3149 while (!feof(f1) && *line_cur1 < start_old) {
3150 err = copy_one_line(f1, outfile, NULL);
3151 if (err)
3152 return err;
3153 (*line_cur1)++;
3155 /* Skip new file's lines leading up to patch. */
3156 while (!feof(f2) && *line_cur2 < start_new) {
3157 if (rejectfile)
3158 err = copy_one_line(f2, NULL, rejectfile);
3159 else
3160 err = skip_one_line(f2);
3161 if (err)
3162 return err;
3163 (*line_cur2)++;
3165 /* Copy patched lines. */
3166 while (!feof(f2) && *line_cur2 <= end_new) {
3167 err = copy_one_line(f2, outfile, NULL);
3168 if (err)
3169 return err;
3170 (*line_cur2)++;
3172 /* Skip over old file's replaced lines. */
3173 while (!feof(f1) && *line_cur1 <= end_old) {
3174 if (rejectfile)
3175 err = copy_one_line(f1, NULL, rejectfile);
3176 else
3177 err = skip_one_line(f1);
3178 if (err)
3179 return err;
3180 (*line_cur1)++;
3183 return NULL;
3186 static const struct got_error *
3187 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3188 FILE *outfile, FILE *rejectfile)
3190 const struct got_error *err;
3192 if (outfile) {
3193 /* Copy old file's lines until EOF. */
3194 while (!feof(f1)) {
3195 err = copy_one_line(f1, outfile, NULL);
3196 if (err)
3197 return err;
3198 (*line_cur1)++;
3201 if (rejectfile) {
3202 /* Copy new file's lines until EOF. */
3203 while (!feof(f2)) {
3204 err = copy_one_line(f2, NULL, rejectfile);
3205 if (err)
3206 return err;
3207 (*line_cur2)++;
3211 return NULL;
3214 static const struct got_error *
3215 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3216 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3217 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3218 int *line_cur2, FILE *outfile, FILE *rejectfile,
3219 got_worktree_patch_cb patch_cb, void *patch_arg)
3221 const struct got_error *err = NULL;
3222 int start_old = change->cv.a;
3223 int end_old = change->cv.b;
3224 int start_new = change->cv.c;
3225 int end_new = change->cv.d;
3226 long pos1, pos2;
3227 FILE *hunkfile;
3229 *choice = GOT_PATCH_CHOICE_NONE;
3231 hunkfile = got_opentemp();
3232 if (hunkfile == NULL)
3233 return got_error_from_errno("got_opentemp");
3235 pos1 = ftell(f1);
3236 pos2 = ftell(f2);
3238 /* XXX TODO needs error checking */
3239 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3241 if (fseek(f1, pos1, SEEK_SET) == -1) {
3242 err = got_ferror(f1, GOT_ERR_IO);
3243 goto done;
3245 if (fseek(f2, pos2, SEEK_SET) == -1) {
3246 err = got_ferror(f1, GOT_ERR_IO);
3247 goto done;
3249 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3250 err = got_ferror(hunkfile, GOT_ERR_IO);
3251 goto done;
3254 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3255 hunkfile, n, nchanges);
3256 if (err)
3257 goto done;
3259 switch (*choice) {
3260 case GOT_PATCH_CHOICE_YES:
3261 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3262 end_old, start_new, end_new, outfile, rejectfile);
3263 break;
3264 case GOT_PATCH_CHOICE_NO:
3265 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3266 end_old, start_new, end_new, rejectfile, outfile);
3267 break;
3268 case GOT_PATCH_CHOICE_QUIT:
3269 break;
3270 default:
3271 err = got_error(GOT_ERR_PATCH_CHOICE);
3272 break;
3274 done:
3275 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3276 err = got_error_from_errno("fclose");
3277 return err;
3280 struct revert_file_args {
3281 struct got_worktree *worktree;
3282 struct got_fileindex *fileindex;
3283 got_worktree_checkout_cb progress_cb;
3284 void *progress_arg;
3285 got_worktree_patch_cb patch_cb;
3286 void *patch_arg;
3287 struct got_repository *repo;
3290 static const struct got_error *
3291 create_patched_content(char **path_outfile, int reverse_patch,
3292 struct got_object_id *blob_id, const char *path2,
3293 int dirfd2, const char *de_name2,
3294 const char *relpath, struct got_repository *repo,
3295 got_worktree_patch_cb patch_cb, void *patch_arg)
3297 const struct got_error *err;
3298 struct got_blob_object *blob = NULL;
3299 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3300 int fd2 = -1;
3301 char *path1 = NULL, *id_str = NULL;
3302 struct stat sb1, sb2;
3303 struct got_diff_changes *changes = NULL;
3304 struct got_diff_state *ds = NULL;
3305 struct got_diff_args *args = NULL;
3306 struct got_diff_change *change;
3307 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3308 int n = 0;
3310 *path_outfile = NULL;
3312 err = got_object_id_str(&id_str, blob_id);
3313 if (err)
3314 return err;
3316 if (dirfd2 != -1) {
3317 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3318 if (fd2 == -1) {
3319 err = got_error_from_errno2("openat", path2);
3320 goto done;
3322 } else {
3323 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3324 if (fd2 == -1) {
3325 err = got_error_from_errno2("open", path2);
3326 goto done;
3329 if (fstat(fd2, &sb2) == -1) {
3330 err = got_error_from_errno2("fstat", path2);
3331 goto done;
3334 f2 = fdopen(fd2, "r");
3335 if (f2 == NULL) {
3336 err = got_error_from_errno2("fdopen", path2);
3337 goto done;
3339 fd2 = -1;
3341 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3342 if (err)
3343 goto done;
3345 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3346 if (err)
3347 goto done;
3349 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3350 if (err)
3351 goto done;
3353 if (stat(path1, &sb1) == -1) {
3354 err = got_error_from_errno2("stat", path1);
3355 goto done;
3358 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3359 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3360 if (err)
3361 goto done;
3363 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3364 if (err)
3365 goto done;
3367 if (fseek(f1, 0L, SEEK_SET) == -1)
3368 return got_ferror(f1, GOT_ERR_IO);
3369 if (fseek(f2, 0L, SEEK_SET) == -1)
3370 return got_ferror(f2, GOT_ERR_IO);
3371 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3372 int choice;
3373 err = apply_or_reject_change(&choice, change, ++n,
3374 changes->nchanges, ds, args, diff_flags, relpath,
3375 f1, f2, &line_cur1, &line_cur2,
3376 reverse_patch ? NULL : outfile,
3377 reverse_patch ? outfile : NULL,
3378 patch_cb, patch_arg);
3379 if (err)
3380 goto done;
3381 if (choice == GOT_PATCH_CHOICE_YES)
3382 have_content = 1;
3383 else if (choice == GOT_PATCH_CHOICE_QUIT)
3384 break;
3386 if (have_content) {
3387 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3388 reverse_patch ? NULL : outfile,
3389 reverse_patch ? outfile : NULL);
3390 if (err)
3391 goto done;
3393 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3394 err = got_error_from_errno2("chmod", path2);
3395 goto done;
3398 done:
3399 free(id_str);
3400 if (blob)
3401 got_object_blob_close(blob);
3402 if (f1 && fclose(f1) == EOF && err == NULL)
3403 err = got_error_from_errno2("fclose", path1);
3404 if (f2 && fclose(f2) == EOF && err == NULL)
3405 err = got_error_from_errno2("fclose", path2);
3406 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3407 err = got_error_from_errno2("close", path2);
3408 if (outfile && fclose(outfile) == EOF && err == NULL)
3409 err = got_error_from_errno2("fclose", *path_outfile);
3410 if (path1 && unlink(path1) == -1 && err == NULL)
3411 err = got_error_from_errno2("unlink", path1);
3412 if (err || !have_content) {
3413 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3414 err = got_error_from_errno2("unlink", *path_outfile);
3415 free(*path_outfile);
3416 *path_outfile = NULL;
3418 free(args);
3419 if (ds) {
3420 got_diff_state_free(ds);
3421 free(ds);
3423 if (changes)
3424 got_diff_free_changes(changes);
3425 free(path1);
3426 return err;
3429 static const struct got_error *
3430 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3431 const char *relpath, struct got_object_id *blob_id,
3432 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3433 int dirfd, const char *de_name)
3435 struct revert_file_args *a = arg;
3436 const struct got_error *err = NULL;
3437 char *parent_path = NULL;
3438 struct got_fileindex_entry *ie;
3439 struct got_tree_object *tree = NULL;
3440 struct got_object_id *tree_id = NULL;
3441 const struct got_tree_entry *te = NULL;
3442 char *tree_path = NULL, *te_name;
3443 char *ondisk_path = NULL, *path_content = NULL;
3444 struct got_blob_object *blob = NULL;
3446 /* Reverting a staged deletion is a no-op. */
3447 if (status == GOT_STATUS_DELETE &&
3448 staged_status != GOT_STATUS_NO_CHANGE)
3449 return NULL;
3451 if (status == GOT_STATUS_UNVERSIONED)
3452 return (*a->progress_cb)(a->progress_arg,
3453 GOT_STATUS_UNVERSIONED, relpath);
3455 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3456 if (ie == NULL)
3457 return got_error(GOT_ERR_BAD_PATH);
3459 /* Construct in-repository path of tree which contains this blob. */
3460 err = got_path_dirname(&parent_path, ie->path);
3461 if (err) {
3462 if (err->code != GOT_ERR_BAD_PATH)
3463 goto done;
3464 parent_path = strdup("/");
3465 if (parent_path == NULL) {
3466 err = got_error_from_errno("strdup");
3467 goto done;
3470 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3471 tree_path = strdup(parent_path);
3472 if (tree_path == NULL) {
3473 err = got_error_from_errno("strdup");
3474 goto done;
3476 } else {
3477 if (got_path_is_root_dir(parent_path)) {
3478 tree_path = strdup(a->worktree->path_prefix);
3479 if (tree_path == NULL) {
3480 err = got_error_from_errno("strdup");
3481 goto done;
3483 } else {
3484 if (asprintf(&tree_path, "%s/%s",
3485 a->worktree->path_prefix, parent_path) == -1) {
3486 err = got_error_from_errno("asprintf");
3487 goto done;
3492 err = got_object_id_by_path(&tree_id, a->repo,
3493 a->worktree->base_commit_id, tree_path);
3494 if (err) {
3495 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3496 (status == GOT_STATUS_ADD ||
3497 staged_status == GOT_STATUS_ADD)))
3498 goto done;
3499 } else {
3500 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3501 if (err)
3502 goto done;
3504 te_name = basename(ie->path);
3505 if (te_name == NULL) {
3506 err = got_error_from_errno2("basename", ie->path);
3507 goto done;
3510 te = got_object_tree_find_entry(tree, te_name);
3511 if (te == NULL && status != GOT_STATUS_ADD &&
3512 staged_status != GOT_STATUS_ADD) {
3513 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3514 goto done;
3518 switch (status) {
3519 case GOT_STATUS_ADD:
3520 if (a->patch_cb) {
3521 int choice = GOT_PATCH_CHOICE_NONE;
3522 err = (*a->patch_cb)(&choice, a->patch_arg,
3523 status, ie->path, NULL, 1, 1);
3524 if (err)
3525 goto done;
3526 if (choice != GOT_PATCH_CHOICE_YES)
3527 break;
3529 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3530 ie->path);
3531 if (err)
3532 goto done;
3533 got_fileindex_entry_remove(a->fileindex, ie);
3534 break;
3535 case GOT_STATUS_DELETE:
3536 if (a->patch_cb) {
3537 int choice = GOT_PATCH_CHOICE_NONE;
3538 err = (*a->patch_cb)(&choice, a->patch_arg,
3539 status, ie->path, NULL, 1, 1);
3540 if (err)
3541 goto done;
3542 if (choice != GOT_PATCH_CHOICE_YES)
3543 break;
3545 /* fall through */
3546 case GOT_STATUS_MODIFY:
3547 case GOT_STATUS_MODE_CHANGE:
3548 case GOT_STATUS_CONFLICT:
3549 case GOT_STATUS_MISSING: {
3550 struct got_object_id id;
3551 if (staged_status == GOT_STATUS_ADD ||
3552 staged_status == GOT_STATUS_MODIFY) {
3553 memcpy(id.sha1, ie->staged_blob_sha1,
3554 SHA1_DIGEST_LENGTH);
3555 } else
3556 memcpy(id.sha1, ie->blob_sha1,
3557 SHA1_DIGEST_LENGTH);
3558 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3559 if (err)
3560 goto done;
3562 if (asprintf(&ondisk_path, "%s/%s",
3563 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 goto done;
3568 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3569 status == GOT_STATUS_CONFLICT)) {
3570 err = create_patched_content(&path_content, 1, &id,
3571 ondisk_path, dirfd, de_name, ie->path, a->repo,
3572 a->patch_cb, a->patch_arg);
3573 if (err || path_content == NULL)
3574 break;
3575 if (rename(path_content, ondisk_path) == -1) {
3576 err = got_error_from_errno3("rename",
3577 path_content, ondisk_path);
3578 goto done;
3580 } else {
3581 err = install_blob(a->worktree, ondisk_path, ie->path,
3582 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3583 got_fileindex_perms_to_st(ie), blob, 0, 1,
3584 a->repo, a->progress_cb, a->progress_arg);
3585 if (err)
3586 goto done;
3587 if (status == GOT_STATUS_DELETE ||
3588 status == GOT_STATUS_MODE_CHANGE) {
3589 err = update_blob_fileindex_entry(a->worktree,
3590 a->fileindex, ie, ondisk_path, ie->path,
3591 blob, 1);
3592 if (err)
3593 goto done;
3596 break;
3598 default:
3599 break;
3601 done:
3602 free(ondisk_path);
3603 free(path_content);
3604 free(parent_path);
3605 free(tree_path);
3606 if (blob)
3607 got_object_blob_close(blob);
3608 if (tree)
3609 got_object_tree_close(tree);
3610 free(tree_id);
3611 return err;
3614 const struct got_error *
3615 got_worktree_revert(struct got_worktree *worktree,
3616 struct got_pathlist_head *paths,
3617 got_worktree_checkout_cb progress_cb, void *progress_arg,
3618 got_worktree_patch_cb patch_cb, void *patch_arg,
3619 struct got_repository *repo)
3621 struct got_fileindex *fileindex = NULL;
3622 char *fileindex_path = NULL;
3623 const struct got_error *err = NULL, *unlockerr = NULL;
3624 const struct got_error *sync_err = NULL;
3625 struct got_pathlist_entry *pe;
3626 struct revert_file_args rfa;
3628 err = lock_worktree(worktree, LOCK_EX);
3629 if (err)
3630 return err;
3632 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3633 if (err)
3634 goto done;
3636 rfa.worktree = worktree;
3637 rfa.fileindex = fileindex;
3638 rfa.progress_cb = progress_cb;
3639 rfa.progress_arg = progress_arg;
3640 rfa.patch_cb = patch_cb;
3641 rfa.patch_arg = patch_arg;
3642 rfa.repo = repo;
3643 TAILQ_FOREACH(pe, paths, entry) {
3644 err = worktree_status(worktree, pe->path, fileindex, repo,
3645 revert_file, &rfa, NULL, NULL, 0, 0);
3646 if (err)
3647 break;
3649 sync_err = sync_fileindex(fileindex, fileindex_path);
3650 if (sync_err && err == NULL)
3651 err = sync_err;
3652 done:
3653 free(fileindex_path);
3654 if (fileindex)
3655 got_fileindex_free(fileindex);
3656 unlockerr = lock_worktree(worktree, LOCK_SH);
3657 if (unlockerr && err == NULL)
3658 err = unlockerr;
3659 return err;
3662 static void
3663 free_commitable(struct got_commitable *ct)
3665 free(ct->path);
3666 free(ct->in_repo_path);
3667 free(ct->ondisk_path);
3668 free(ct->blob_id);
3669 free(ct->base_blob_id);
3670 free(ct->staged_blob_id);
3671 free(ct->base_commit_id);
3672 free(ct);
3675 struct collect_commitables_arg {
3676 struct got_pathlist_head *commitable_paths;
3677 struct got_repository *repo;
3678 struct got_worktree *worktree;
3679 int have_staged_files;
3682 static const struct got_error *
3683 collect_commitables(void *arg, unsigned char status,
3684 unsigned char staged_status, const char *relpath,
3685 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3686 struct got_object_id *commit_id, int dirfd, const char *de_name)
3688 struct collect_commitables_arg *a = arg;
3689 const struct got_error *err = NULL;
3690 struct got_commitable *ct = NULL;
3691 struct got_pathlist_entry *new = NULL;
3692 char *parent_path = NULL, *path = NULL;
3693 struct stat sb;
3695 if (a->have_staged_files) {
3696 if (staged_status != GOT_STATUS_MODIFY &&
3697 staged_status != GOT_STATUS_ADD &&
3698 staged_status != GOT_STATUS_DELETE)
3699 return NULL;
3700 } else {
3701 if (status == GOT_STATUS_CONFLICT)
3702 return got_error(GOT_ERR_COMMIT_CONFLICT);
3704 if (status != GOT_STATUS_MODIFY &&
3705 status != GOT_STATUS_MODE_CHANGE &&
3706 status != GOT_STATUS_ADD &&
3707 status != GOT_STATUS_DELETE)
3708 return NULL;
3711 if (asprintf(&path, "/%s", relpath) == -1) {
3712 err = got_error_from_errno("asprintf");
3713 goto done;
3715 if (strcmp(path, "/") == 0) {
3716 parent_path = strdup("");
3717 if (parent_path == NULL)
3718 return got_error_from_errno("strdup");
3719 } else {
3720 err = got_path_dirname(&parent_path, path);
3721 if (err)
3722 return err;
3725 ct = calloc(1, sizeof(*ct));
3726 if (ct == NULL) {
3727 err = got_error_from_errno("calloc");
3728 goto done;
3731 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3732 relpath) == -1) {
3733 err = got_error_from_errno("asprintf");
3734 goto done;
3736 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3737 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3738 } else {
3739 if (dirfd != -1) {
3740 if (fstatat(dirfd, de_name, &sb,
3741 AT_SYMLINK_NOFOLLOW) == -1) {
3742 err = got_error_from_errno2("fstatat",
3743 ct->ondisk_path);
3744 goto done;
3746 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3747 err = got_error_from_errno2("lstat", ct->ondisk_path);
3748 goto done;
3750 ct->mode = sb.st_mode;
3753 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3754 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3755 relpath) == -1) {
3756 err = got_error_from_errno("asprintf");
3757 goto done;
3760 ct->status = status;
3761 ct->staged_status = staged_status;
3762 ct->blob_id = NULL; /* will be filled in when blob gets created */
3763 if (ct->status != GOT_STATUS_ADD &&
3764 ct->staged_status != GOT_STATUS_ADD) {
3765 ct->base_blob_id = got_object_id_dup(blob_id);
3766 if (ct->base_blob_id == NULL) {
3767 err = got_error_from_errno("got_object_id_dup");
3768 goto done;
3770 ct->base_commit_id = got_object_id_dup(commit_id);
3771 if (ct->base_commit_id == NULL) {
3772 err = got_error_from_errno("got_object_id_dup");
3773 goto done;
3776 if (ct->staged_status == GOT_STATUS_ADD ||
3777 ct->staged_status == GOT_STATUS_MODIFY) {
3778 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3779 if (ct->staged_blob_id == NULL) {
3780 err = got_error_from_errno("got_object_id_dup");
3781 goto done;
3784 ct->path = strdup(path);
3785 if (ct->path == NULL) {
3786 err = got_error_from_errno("strdup");
3787 goto done;
3789 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3790 done:
3791 if (ct && (err || new == NULL))
3792 free_commitable(ct);
3793 free(parent_path);
3794 free(path);
3795 return err;
3798 static const struct got_error *write_tree(struct got_object_id **,
3799 struct got_tree_object *, const char *, struct got_pathlist_head *,
3800 got_worktree_status_cb status_cb, void *status_arg,
3801 struct got_repository *);
3803 static const struct got_error *
3804 write_subtree(struct got_object_id **new_subtree_id,
3805 struct got_tree_entry *te, const char *parent_path,
3806 struct got_pathlist_head *commitable_paths,
3807 got_worktree_status_cb status_cb, void *status_arg,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3811 struct got_tree_object *subtree;
3812 char *subpath;
3814 if (asprintf(&subpath, "%s%s%s", parent_path,
3815 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3816 return got_error_from_errno("asprintf");
3818 err = got_object_open_as_tree(&subtree, repo, &te->id);
3819 if (err)
3820 return err;
3822 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3823 status_cb, status_arg, repo);
3824 got_object_tree_close(subtree);
3825 free(subpath);
3826 return err;
3829 static const struct got_error *
3830 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3832 const struct got_error *err = NULL;
3833 char *ct_parent_path = NULL;
3835 *match = 0;
3837 if (strchr(ct->in_repo_path, '/') == NULL) {
3838 *match = got_path_is_root_dir(path);
3839 return NULL;
3842 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3843 if (err)
3844 return err;
3845 *match = (strcmp(path, ct_parent_path) == 0);
3846 free(ct_parent_path);
3847 return err;
3850 static mode_t
3851 get_ct_file_mode(struct got_commitable *ct)
3853 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3856 static const struct got_error *
3857 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3858 struct got_tree_entry *te, struct got_commitable *ct)
3860 const struct got_error *err = NULL;
3862 *new_te = NULL;
3864 err = got_object_tree_entry_dup(new_te, te);
3865 if (err)
3866 goto done;
3868 (*new_te)->mode = get_ct_file_mode(ct);
3870 if (ct->staged_status == GOT_STATUS_MODIFY)
3871 memcpy(&(*new_te)->id, ct->staged_blob_id,
3872 sizeof((*new_te)->id));
3873 else
3874 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3875 done:
3876 if (err && *new_te) {
3877 free(*new_te);
3878 *new_te = NULL;
3880 return err;
3883 static const struct got_error *
3884 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3885 struct got_commitable *ct)
3887 const struct got_error *err = NULL;
3888 char *ct_name;
3890 *new_te = NULL;
3892 *new_te = calloc(1, sizeof(**new_te));
3893 if (*new_te == NULL)
3894 return got_error_from_errno("calloc");
3896 ct_name = basename(ct->path);
3897 if (ct_name == NULL) {
3898 err = got_error_from_errno2("basename", ct->path);
3899 goto done;
3901 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3902 sizeof((*new_te)->name)) {
3903 err = got_error(GOT_ERR_NO_SPACE);
3904 goto done;
3907 (*new_te)->mode = get_ct_file_mode(ct);
3909 if (ct->staged_status == GOT_STATUS_ADD)
3910 memcpy(&(*new_te)->id, ct->staged_blob_id,
3911 sizeof((*new_te)->id));
3912 else
3913 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3914 done:
3915 if (err && *new_te) {
3916 free(*new_te);
3917 *new_te = NULL;
3919 return err;
3922 static const struct got_error *
3923 insert_tree_entry(struct got_tree_entry *new_te,
3924 struct got_pathlist_head *paths)
3926 const struct got_error *err = NULL;
3927 struct got_pathlist_entry *new_pe;
3929 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3930 if (err)
3931 return err;
3932 if (new_pe == NULL)
3933 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3934 return NULL;
3937 static const struct got_error *
3938 report_ct_status(struct got_commitable *ct,
3939 got_worktree_status_cb status_cb, void *status_arg)
3941 const char *ct_path = ct->path;
3942 unsigned char status;
3944 while (ct_path[0] == '/')
3945 ct_path++;
3947 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3948 status = ct->staged_status;
3949 else
3950 status = ct->status;
3952 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3953 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3956 static const struct got_error *
3957 match_modified_subtree(int *modified, struct got_tree_entry *te,
3958 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3960 const struct got_error *err = NULL;
3961 struct got_pathlist_entry *pe;
3962 char *te_path;
3964 *modified = 0;
3966 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3967 got_path_is_root_dir(base_tree_path) ? "" : "/",
3968 te->name) == -1)
3969 return got_error_from_errno("asprintf");
3971 TAILQ_FOREACH(pe, commitable_paths, entry) {
3972 struct got_commitable *ct = pe->data;
3973 *modified = got_path_is_child(ct->in_repo_path, te_path,
3974 strlen(te_path));
3975 if (*modified)
3976 break;
3979 free(te_path);
3980 return err;
3983 static const struct got_error *
3984 match_deleted_or_modified_ct(struct got_commitable **ctp,
3985 struct got_tree_entry *te, const char *base_tree_path,
3986 struct got_pathlist_head *commitable_paths)
3988 const struct got_error *err = NULL;
3989 struct got_pathlist_entry *pe;
3991 *ctp = NULL;
3993 TAILQ_FOREACH(pe, commitable_paths, entry) {
3994 struct got_commitable *ct = pe->data;
3995 char *ct_name = NULL;
3996 int path_matches;
3998 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3999 if (ct->status != GOT_STATUS_MODIFY &&
4000 ct->status != GOT_STATUS_MODE_CHANGE &&
4001 ct->status != GOT_STATUS_DELETE)
4002 continue;
4003 } else {
4004 if (ct->staged_status != GOT_STATUS_MODIFY &&
4005 ct->staged_status != GOT_STATUS_DELETE)
4006 continue;
4009 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4010 continue;
4012 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4013 if (err)
4014 return err;
4015 if (!path_matches)
4016 continue;
4018 ct_name = basename(pe->path);
4019 if (ct_name == NULL)
4020 return got_error_from_errno2("basename", pe->path);
4022 if (strcmp(te->name, ct_name) != 0)
4023 continue;
4025 *ctp = ct;
4026 break;
4029 return err;
4032 static const struct got_error *
4033 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4034 const char *child_path, const char *path_base_tree,
4035 struct got_pathlist_head *commitable_paths,
4036 got_worktree_status_cb status_cb, void *status_arg,
4037 struct got_repository *repo)
4039 const struct got_error *err = NULL;
4040 struct got_tree_entry *new_te;
4041 char *subtree_path;
4042 struct got_object_id *id = NULL;
4044 *new_tep = NULL;
4046 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4047 got_path_is_root_dir(path_base_tree) ? "" : "/",
4048 child_path) == -1)
4049 return got_error_from_errno("asprintf");
4051 new_te = calloc(1, sizeof(*new_te));
4052 if (new_te == NULL)
4053 return got_error_from_errno("calloc");
4054 new_te->mode = S_IFDIR;
4056 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4057 sizeof(new_te->name)) {
4058 err = got_error(GOT_ERR_NO_SPACE);
4059 goto done;
4061 err = write_tree(&id, NULL, subtree_path,
4062 commitable_paths, status_cb, status_arg, repo);
4063 if (err) {
4064 free(new_te);
4065 goto done;
4067 memcpy(&new_te->id, id, sizeof(new_te->id));
4068 done:
4069 free(id);
4070 free(subtree_path);
4071 if (err == NULL)
4072 *new_tep = new_te;
4073 return err;
4076 static const struct got_error *
4077 write_tree(struct got_object_id **new_tree_id,
4078 struct got_tree_object *base_tree, const char *path_base_tree,
4079 struct got_pathlist_head *commitable_paths,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 struct got_repository *repo)
4083 const struct got_error *err = NULL;
4084 struct got_pathlist_head paths;
4085 struct got_tree_entry *te, *new_te = NULL;
4086 struct got_pathlist_entry *pe;
4087 int nentries = 0;
4089 TAILQ_INIT(&paths);
4091 /* Insert, and recurse into, newly added entries first. */
4092 TAILQ_FOREACH(pe, commitable_paths, entry) {
4093 struct got_commitable *ct = pe->data;
4094 char *child_path = NULL, *slash;
4096 if ((ct->status != GOT_STATUS_ADD &&
4097 ct->staged_status != GOT_STATUS_ADD) ||
4098 (ct->flags & GOT_COMMITABLE_ADDED))
4099 continue;
4101 if (!got_path_is_child(pe->path, path_base_tree,
4102 strlen(path_base_tree)))
4103 continue;
4105 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4106 pe->path);
4107 if (err)
4108 goto done;
4110 slash = strchr(child_path, '/');
4111 if (slash == NULL) {
4112 err = alloc_added_blob_tree_entry(&new_te, ct);
4113 if (err)
4114 goto done;
4115 err = report_ct_status(ct, status_cb, status_arg);
4116 if (err)
4117 goto done;
4118 ct->flags |= GOT_COMMITABLE_ADDED;
4119 err = insert_tree_entry(new_te, &paths);
4120 if (err)
4121 goto done;
4122 nentries++;
4123 } else {
4124 *slash = '\0'; /* trim trailing path components */
4125 if (base_tree == NULL ||
4126 got_object_tree_find_entry(base_tree, child_path)
4127 == NULL) {
4128 err = make_subtree_for_added_blob(&new_te,
4129 child_path, path_base_tree,
4130 commitable_paths, status_cb, status_arg,
4131 repo);
4132 if (err)
4133 goto done;
4134 err = insert_tree_entry(new_te, &paths);
4135 if (err)
4136 goto done;
4137 nentries++;
4142 if (base_tree) {
4143 int i, nbase_entries;
4144 /* Handle modified and deleted entries. */
4145 nbase_entries = got_object_tree_get_nentries(base_tree);
4146 for (i = 0; i < nbase_entries; i++) {
4147 struct got_commitable *ct = NULL;
4149 te = got_object_tree_get_entry(base_tree, i);
4150 if (got_object_tree_entry_is_submodule(te)) {
4151 /* Entry is a submodule; just copy it. */
4152 err = got_object_tree_entry_dup(&new_te, te);
4153 if (err)
4154 goto done;
4155 err = insert_tree_entry(new_te, &paths);
4156 if (err)
4157 goto done;
4158 nentries++;
4159 continue;
4162 if (S_ISDIR(te->mode)) {
4163 int modified;
4164 err = got_object_tree_entry_dup(&new_te, te);
4165 if (err)
4166 goto done;
4167 err = match_modified_subtree(&modified, te,
4168 path_base_tree, commitable_paths);
4169 if (err)
4170 goto done;
4171 /* Avoid recursion into unmodified subtrees. */
4172 if (modified) {
4173 struct got_object_id *new_id;
4174 err = write_subtree(&new_id, te,
4175 path_base_tree, commitable_paths,
4176 status_cb, status_arg, repo);
4177 if (err)
4178 goto done;
4179 memcpy(&new_te->id, new_id,
4180 sizeof(new_te->id));
4181 free(new_id);
4183 err = insert_tree_entry(new_te, &paths);
4184 if (err)
4185 goto done;
4186 nentries++;
4187 continue;
4190 err = match_deleted_or_modified_ct(&ct, te,
4191 path_base_tree, commitable_paths);
4192 if (err)
4193 goto done;
4194 if (ct) {
4195 /* NB: Deleted entries get dropped here. */
4196 if (ct->status == GOT_STATUS_MODIFY ||
4197 ct->status == GOT_STATUS_MODE_CHANGE ||
4198 ct->staged_status == GOT_STATUS_MODIFY) {
4199 err = alloc_modified_blob_tree_entry(
4200 &new_te, te, ct);
4201 if (err)
4202 goto done;
4203 err = insert_tree_entry(new_te, &paths);
4204 if (err)
4205 goto done;
4206 nentries++;
4208 err = report_ct_status(ct, status_cb,
4209 status_arg);
4210 if (err)
4211 goto done;
4212 } else {
4213 /* Entry is unchanged; just copy it. */
4214 err = got_object_tree_entry_dup(&new_te, te);
4215 if (err)
4216 goto done;
4217 err = insert_tree_entry(new_te, &paths);
4218 if (err)
4219 goto done;
4220 nentries++;
4225 /* Write new list of entries; deleted entries have been dropped. */
4226 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4227 done:
4228 got_pathlist_free(&paths);
4229 return err;
4232 static const struct got_error *
4233 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4234 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4235 int have_staged_files)
4237 const struct got_error *err = NULL;
4238 struct got_pathlist_entry *pe;
4240 TAILQ_FOREACH(pe, commitable_paths, entry) {
4241 struct got_fileindex_entry *ie;
4242 struct got_commitable *ct = pe->data;
4244 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4245 if (ie) {
4246 if (ct->status == GOT_STATUS_DELETE ||
4247 ct->staged_status == GOT_STATUS_DELETE) {
4248 got_fileindex_entry_remove(fileindex, ie);
4249 got_fileindex_entry_free(ie);
4250 } else if (ct->staged_status == GOT_STATUS_ADD ||
4251 ct->staged_status == GOT_STATUS_MODIFY) {
4252 got_fileindex_entry_stage_set(ie,
4253 GOT_FILEIDX_STAGE_NONE);
4254 err = got_fileindex_entry_update(ie,
4255 ct->ondisk_path, ct->staged_blob_id->sha1,
4256 new_base_commit_id->sha1,
4257 !have_staged_files);
4258 } else
4259 err = got_fileindex_entry_update(ie,
4260 ct->ondisk_path, ct->blob_id->sha1,
4261 new_base_commit_id->sha1,
4262 !have_staged_files);
4263 } else {
4264 err = got_fileindex_entry_alloc(&ie,
4265 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4266 new_base_commit_id->sha1);
4267 if (err)
4268 break;
4269 err = got_fileindex_entry_add(fileindex, ie);
4270 if (err)
4271 break;
4274 return err;
4278 static const struct got_error *
4279 check_out_of_date(const char *in_repo_path, unsigned char status,
4280 unsigned char staged_status, struct got_object_id *base_blob_id,
4281 struct got_object_id *base_commit_id,
4282 struct got_object_id *head_commit_id, struct got_repository *repo,
4283 int ood_errcode)
4285 const struct got_error *err = NULL;
4286 struct got_object_id *id = NULL;
4288 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4289 /* Trivial case: base commit == head commit */
4290 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4291 return NULL;
4293 * Ensure file content which local changes were based
4294 * on matches file content in the branch head.
4296 err = got_object_id_by_path(&id, repo, head_commit_id,
4297 in_repo_path);
4298 if (err) {
4299 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4300 err = got_error(ood_errcode);
4301 goto done;
4302 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4303 err = got_error(ood_errcode);
4304 } else {
4305 /* Require that added files don't exist in the branch head. */
4306 err = got_object_id_by_path(&id, repo, head_commit_id,
4307 in_repo_path);
4308 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4309 goto done;
4310 err = id ? got_error(ood_errcode) : NULL;
4312 done:
4313 free(id);
4314 return err;
4317 const struct got_error *
4318 commit_worktree(struct got_object_id **new_commit_id,
4319 struct got_pathlist_head *commitable_paths,
4320 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4321 const char *author, const char *committer,
4322 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4323 got_worktree_status_cb status_cb, void *status_arg,
4324 struct got_repository *repo)
4326 const struct got_error *err = NULL, *unlockerr = NULL;
4327 struct got_pathlist_entry *pe;
4328 const char *head_ref_name = NULL;
4329 struct got_commit_object *head_commit = NULL;
4330 struct got_reference *head_ref2 = NULL;
4331 struct got_object_id *head_commit_id2 = NULL;
4332 struct got_tree_object *head_tree = NULL;
4333 struct got_object_id *new_tree_id = NULL;
4334 struct got_object_id_queue parent_ids;
4335 struct got_object_qid *pid = NULL;
4336 char *logmsg = NULL;
4338 *new_commit_id = NULL;
4340 SIMPLEQ_INIT(&parent_ids);
4342 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4343 if (err)
4344 goto done;
4346 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4347 if (err)
4348 goto done;
4350 if (commit_msg_cb != NULL) {
4351 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4352 if (err)
4353 goto done;
4356 if (logmsg == NULL || strlen(logmsg) == 0) {
4357 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4358 goto done;
4361 /* Create blobs from added and modified files and record their IDs. */
4362 TAILQ_FOREACH(pe, commitable_paths, entry) {
4363 struct got_commitable *ct = pe->data;
4364 char *ondisk_path;
4366 /* Blobs for staged files already exist. */
4367 if (ct->staged_status == GOT_STATUS_ADD ||
4368 ct->staged_status == GOT_STATUS_MODIFY)
4369 continue;
4371 if (ct->status != GOT_STATUS_ADD &&
4372 ct->status != GOT_STATUS_MODIFY &&
4373 ct->status != GOT_STATUS_MODE_CHANGE)
4374 continue;
4376 if (asprintf(&ondisk_path, "%s/%s",
4377 worktree->root_path, pe->path) == -1) {
4378 err = got_error_from_errno("asprintf");
4379 goto done;
4381 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4382 free(ondisk_path);
4383 if (err)
4384 goto done;
4387 /* Recursively write new tree objects. */
4388 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4389 status_cb, status_arg, repo);
4390 if (err)
4391 goto done;
4393 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4394 if (err)
4395 goto done;
4396 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4397 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4398 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4399 got_object_qid_free(pid);
4400 if (logmsg != NULL)
4401 free(logmsg);
4402 if (err)
4403 goto done;
4405 /* Check if a concurrent commit to our branch has occurred. */
4406 head_ref_name = got_worktree_get_head_ref_name(worktree);
4407 if (head_ref_name == NULL) {
4408 err = got_error_from_errno("got_worktree_get_head_ref_name");
4409 goto done;
4411 /* Lock the reference here to prevent concurrent modification. */
4412 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4413 if (err)
4414 goto done;
4415 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4416 if (err)
4417 goto done;
4418 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4419 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4420 goto done;
4422 /* Update branch head in repository. */
4423 err = got_ref_change_ref(head_ref2, *new_commit_id);
4424 if (err)
4425 goto done;
4426 err = got_ref_write(head_ref2, repo);
4427 if (err)
4428 goto done;
4430 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4431 if (err)
4432 goto done;
4434 err = ref_base_commit(worktree, repo);
4435 if (err)
4436 goto done;
4437 done:
4438 if (head_tree)
4439 got_object_tree_close(head_tree);
4440 if (head_commit)
4441 got_object_commit_close(head_commit);
4442 free(head_commit_id2);
4443 if (head_ref2) {
4444 unlockerr = got_ref_unlock(head_ref2);
4445 if (unlockerr && err == NULL)
4446 err = unlockerr;
4447 got_ref_close(head_ref2);
4449 return err;
4452 static const struct got_error *
4453 check_path_is_commitable(const char *path,
4454 struct got_pathlist_head *commitable_paths)
4456 struct got_pathlist_entry *cpe = NULL;
4457 size_t path_len = strlen(path);
4459 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4460 struct got_commitable *ct = cpe->data;
4461 const char *ct_path = ct->path;
4463 while (ct_path[0] == '/')
4464 ct_path++;
4466 if (strcmp(path, ct_path) == 0 ||
4467 got_path_is_child(ct_path, path, path_len))
4468 break;
4471 if (cpe == NULL)
4472 return got_error_path(path, GOT_ERR_BAD_PATH);
4474 return NULL;
4477 static const struct got_error *
4478 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4480 int *have_staged_files = arg;
4482 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4483 *have_staged_files = 1;
4484 return got_error(GOT_ERR_CANCELLED);
4487 return NULL;
4490 static const struct got_error *
4491 check_non_staged_files(struct got_fileindex *fileindex,
4492 struct got_pathlist_head *paths)
4494 struct got_pathlist_entry *pe;
4495 struct got_fileindex_entry *ie;
4497 TAILQ_FOREACH(pe, paths, entry) {
4498 if (pe->path[0] == '\0')
4499 continue;
4500 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4501 if (ie == NULL)
4502 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4503 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4504 return got_error_path(pe->path,
4505 GOT_ERR_FILE_NOT_STAGED);
4508 return NULL;
4511 const struct got_error *
4512 got_worktree_commit(struct got_object_id **new_commit_id,
4513 struct got_worktree *worktree, struct got_pathlist_head *paths,
4514 const char *author, const char *committer,
4515 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4516 got_worktree_status_cb status_cb, void *status_arg,
4517 struct got_repository *repo)
4519 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4520 struct got_fileindex *fileindex = NULL;
4521 char *fileindex_path = NULL;
4522 struct got_pathlist_head commitable_paths;
4523 struct collect_commitables_arg cc_arg;
4524 struct got_pathlist_entry *pe;
4525 struct got_reference *head_ref = NULL;
4526 struct got_object_id *head_commit_id = NULL;
4527 int have_staged_files = 0;
4529 *new_commit_id = NULL;
4531 TAILQ_INIT(&commitable_paths);
4533 err = lock_worktree(worktree, LOCK_EX);
4534 if (err)
4535 goto done;
4537 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4538 if (err)
4539 goto done;
4541 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4542 if (err)
4543 goto done;
4545 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4546 if (err)
4547 goto done;
4549 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4550 &have_staged_files);
4551 if (err && err->code != GOT_ERR_CANCELLED)
4552 goto done;
4553 if (have_staged_files) {
4554 err = check_non_staged_files(fileindex, paths);
4555 if (err)
4556 goto done;
4559 cc_arg.commitable_paths = &commitable_paths;
4560 cc_arg.worktree = worktree;
4561 cc_arg.repo = repo;
4562 cc_arg.have_staged_files = have_staged_files;
4563 TAILQ_FOREACH(pe, paths, entry) {
4564 err = worktree_status(worktree, pe->path, fileindex, repo,
4565 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4566 if (err)
4567 goto done;
4570 if (TAILQ_EMPTY(&commitable_paths)) {
4571 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4572 goto done;
4575 TAILQ_FOREACH(pe, paths, entry) {
4576 err = check_path_is_commitable(pe->path, &commitable_paths);
4577 if (err)
4578 goto done;
4581 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4582 struct got_commitable *ct = pe->data;
4583 const char *ct_path = ct->in_repo_path;
4585 while (ct_path[0] == '/')
4586 ct_path++;
4587 err = check_out_of_date(ct_path, ct->status,
4588 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4589 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4590 if (err)
4591 goto done;
4595 err = commit_worktree(new_commit_id, &commitable_paths,
4596 head_commit_id, worktree, author, committer,
4597 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4598 if (err)
4599 goto done;
4601 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4602 fileindex, have_staged_files);
4603 sync_err = sync_fileindex(fileindex, fileindex_path);
4604 if (sync_err && err == NULL)
4605 err = sync_err;
4606 done:
4607 if (fileindex)
4608 got_fileindex_free(fileindex);
4609 free(fileindex_path);
4610 unlockerr = lock_worktree(worktree, LOCK_SH);
4611 if (unlockerr && err == NULL)
4612 err = unlockerr;
4613 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4614 struct got_commitable *ct = pe->data;
4615 free_commitable(ct);
4617 got_pathlist_free(&commitable_paths);
4618 return err;
4621 const char *
4622 got_commitable_get_path(struct got_commitable *ct)
4624 return ct->path;
4627 unsigned int
4628 got_commitable_get_status(struct got_commitable *ct)
4630 return ct->status;
4633 struct check_rebase_ok_arg {
4634 struct got_worktree *worktree;
4635 struct got_repository *repo;
4638 static const struct got_error *
4639 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4641 const struct got_error *err = NULL;
4642 struct check_rebase_ok_arg *a = arg;
4643 unsigned char status;
4644 struct stat sb;
4645 char *ondisk_path;
4647 /* Reject rebase of a work tree with mixed base commits. */
4648 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4649 SHA1_DIGEST_LENGTH))
4650 return got_error(GOT_ERR_MIXED_COMMITS);
4652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4653 == -1)
4654 return got_error_from_errno("asprintf");
4656 /* Reject rebase of a work tree with modified or staged files. */
4657 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4658 free(ondisk_path);
4659 if (err)
4660 return err;
4662 if (status != GOT_STATUS_NO_CHANGE)
4663 return got_error(GOT_ERR_MODIFIED);
4664 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4665 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4667 return NULL;
4670 const struct got_error *
4671 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4672 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4673 struct got_worktree *worktree, struct got_reference *branch,
4674 struct got_repository *repo)
4676 const struct got_error *err = NULL;
4677 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4678 char *branch_ref_name = NULL;
4679 char *fileindex_path = NULL;
4680 struct check_rebase_ok_arg ok_arg;
4681 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4682 struct got_object_id *wt_branch_tip = NULL;
4684 *new_base_branch_ref = NULL;
4685 *tmp_branch = NULL;
4686 *fileindex = NULL;
4688 err = lock_worktree(worktree, LOCK_EX);
4689 if (err)
4690 return err;
4692 err = open_fileindex(fileindex, &fileindex_path, worktree);
4693 if (err)
4694 goto done;
4696 ok_arg.worktree = worktree;
4697 ok_arg.repo = repo;
4698 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4699 &ok_arg);
4700 if (err)
4701 goto done;
4703 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4704 if (err)
4705 goto done;
4707 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4708 if (err)
4709 goto done;
4711 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4712 if (err)
4713 goto done;
4715 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4716 0);
4717 if (err)
4718 goto done;
4720 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4721 if (err)
4722 goto done;
4723 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4724 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4725 goto done;
4728 err = got_ref_alloc_symref(new_base_branch_ref,
4729 new_base_branch_ref_name, wt_branch);
4730 if (err)
4731 goto done;
4732 err = got_ref_write(*new_base_branch_ref, repo);
4733 if (err)
4734 goto done;
4736 /* TODO Lock original branch's ref while rebasing? */
4738 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4739 if (err)
4740 goto done;
4742 err = got_ref_write(branch_ref, repo);
4743 if (err)
4744 goto done;
4746 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4747 worktree->base_commit_id);
4748 if (err)
4749 goto done;
4750 err = got_ref_write(*tmp_branch, repo);
4751 if (err)
4752 goto done;
4754 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4755 if (err)
4756 goto done;
4757 done:
4758 free(fileindex_path);
4759 free(tmp_branch_name);
4760 free(new_base_branch_ref_name);
4761 free(branch_ref_name);
4762 if (branch_ref)
4763 got_ref_close(branch_ref);
4764 if (wt_branch)
4765 got_ref_close(wt_branch);
4766 free(wt_branch_tip);
4767 if (err) {
4768 if (*new_base_branch_ref) {
4769 got_ref_close(*new_base_branch_ref);
4770 *new_base_branch_ref = NULL;
4772 if (*tmp_branch) {
4773 got_ref_close(*tmp_branch);
4774 *tmp_branch = NULL;
4776 if (*fileindex) {
4777 got_fileindex_free(*fileindex);
4778 *fileindex = NULL;
4780 lock_worktree(worktree, LOCK_SH);
4782 return err;
4785 const struct got_error *
4786 got_worktree_rebase_continue(struct got_object_id **commit_id,
4787 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4788 struct got_reference **branch, struct got_fileindex **fileindex,
4789 struct got_worktree *worktree, struct got_repository *repo)
4791 const struct got_error *err;
4792 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4793 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4794 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4795 char *fileindex_path = NULL;
4796 int have_staged_files = 0;
4798 *commit_id = NULL;
4799 *new_base_branch = NULL;
4800 *tmp_branch = NULL;
4801 *branch = NULL;
4802 *fileindex = NULL;
4804 err = lock_worktree(worktree, LOCK_EX);
4805 if (err)
4806 return err;
4808 err = open_fileindex(fileindex, &fileindex_path, worktree);
4809 if (err)
4810 goto done;
4812 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4813 &have_staged_files);
4814 if (err && err->code != GOT_ERR_CANCELLED)
4815 goto done;
4816 if (have_staged_files) {
4817 err = got_error(GOT_ERR_STAGED_PATHS);
4818 goto done;
4821 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4822 if (err)
4823 goto done;
4825 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4826 if (err)
4827 goto done;
4829 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4830 if (err)
4831 goto done;
4833 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4834 if (err)
4835 goto done;
4837 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4838 if (err)
4839 goto done;
4841 err = got_ref_open(branch, repo,
4842 got_ref_get_symref_target(branch_ref), 0);
4843 if (err)
4844 goto done;
4846 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4847 if (err)
4848 goto done;
4850 err = got_ref_resolve(commit_id, repo, commit_ref);
4851 if (err)
4852 goto done;
4854 err = got_ref_open(new_base_branch, repo,
4855 new_base_branch_ref_name, 0);
4856 if (err)
4857 goto done;
4859 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4860 if (err)
4861 goto done;
4862 done:
4863 free(commit_ref_name);
4864 free(branch_ref_name);
4865 free(fileindex_path);
4866 if (commit_ref)
4867 got_ref_close(commit_ref);
4868 if (branch_ref)
4869 got_ref_close(branch_ref);
4870 if (err) {
4871 free(*commit_id);
4872 *commit_id = NULL;
4873 if (*tmp_branch) {
4874 got_ref_close(*tmp_branch);
4875 *tmp_branch = NULL;
4877 if (*new_base_branch) {
4878 got_ref_close(*new_base_branch);
4879 *new_base_branch = NULL;
4881 if (*branch) {
4882 got_ref_close(*branch);
4883 *branch = NULL;
4885 if (*fileindex) {
4886 got_fileindex_free(*fileindex);
4887 *fileindex = NULL;
4889 lock_worktree(worktree, LOCK_SH);
4891 return err;
4894 const struct got_error *
4895 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4897 const struct got_error *err;
4898 char *tmp_branch_name = NULL;
4900 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4901 if (err)
4902 return err;
4904 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4905 free(tmp_branch_name);
4906 return NULL;
4909 static const struct got_error *
4910 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4911 char **logmsg, void *arg)
4913 *logmsg = arg;
4914 return NULL;
4917 static const struct got_error *
4918 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4919 const char *path, struct got_object_id *blob_id,
4920 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4921 int dirfd, const char *de_name)
4923 return NULL;
4926 struct collect_merged_paths_arg {
4927 got_worktree_checkout_cb progress_cb;
4928 void *progress_arg;
4929 struct got_pathlist_head *merged_paths;
4932 static const struct got_error *
4933 collect_merged_paths(void *arg, unsigned char status, const char *path)
4935 const struct got_error *err;
4936 struct collect_merged_paths_arg *a = arg;
4937 char *p;
4938 struct got_pathlist_entry *new;
4940 err = (*a->progress_cb)(a->progress_arg, status, path);
4941 if (err)
4942 return err;
4944 if (status != GOT_STATUS_MERGE &&
4945 status != GOT_STATUS_ADD &&
4946 status != GOT_STATUS_DELETE &&
4947 status != GOT_STATUS_CONFLICT)
4948 return NULL;
4950 p = strdup(path);
4951 if (p == NULL)
4952 return got_error_from_errno("strdup");
4954 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4955 if (err || new == NULL)
4956 free(p);
4957 return err;
4960 void
4961 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4963 struct got_pathlist_entry *pe;
4965 TAILQ_FOREACH(pe, merged_paths, entry)
4966 free((char *)pe->path);
4968 got_pathlist_free(merged_paths);
4971 static const struct got_error *
4972 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4973 struct got_repository *repo)
4975 const struct got_error *err;
4976 struct got_reference *commit_ref = NULL;
4978 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4979 if (err) {
4980 if (err->code != GOT_ERR_NOT_REF)
4981 goto done;
4982 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4983 if (err)
4984 goto done;
4985 err = got_ref_write(commit_ref, repo);
4986 if (err)
4987 goto done;
4988 } else {
4989 struct got_object_id *stored_id;
4990 int cmp;
4992 err = got_ref_resolve(&stored_id, repo, commit_ref);
4993 if (err)
4994 goto done;
4995 cmp = got_object_id_cmp(commit_id, stored_id);
4996 free(stored_id);
4997 if (cmp != 0) {
4998 err = got_error(GOT_ERR_REBASE_COMMITID);
4999 goto done;
5002 done:
5003 if (commit_ref)
5004 got_ref_close(commit_ref);
5005 return err;
5008 static const struct got_error *
5009 rebase_merge_files(struct got_pathlist_head *merged_paths,
5010 const char *commit_ref_name, struct got_worktree *worktree,
5011 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5012 struct got_object_id *commit_id, struct got_repository *repo,
5013 got_worktree_checkout_cb progress_cb, void *progress_arg,
5014 got_cancel_cb cancel_cb, void *cancel_arg)
5016 const struct got_error *err;
5017 struct got_reference *commit_ref = NULL;
5018 struct collect_merged_paths_arg cmp_arg;
5019 char *fileindex_path;
5021 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5023 err = get_fileindex_path(&fileindex_path, worktree);
5024 if (err)
5025 return err;
5027 cmp_arg.progress_cb = progress_cb;
5028 cmp_arg.progress_arg = progress_arg;
5029 cmp_arg.merged_paths = merged_paths;
5030 err = merge_files(worktree, fileindex, fileindex_path,
5031 parent_commit_id, commit_id, repo, collect_merged_paths,
5032 &cmp_arg, cancel_cb, cancel_arg);
5033 if (commit_ref)
5034 got_ref_close(commit_ref);
5035 return err;
5038 const struct got_error *
5039 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5040 struct got_worktree *worktree, struct got_fileindex *fileindex,
5041 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5042 struct got_repository *repo,
5043 got_worktree_checkout_cb progress_cb, void *progress_arg,
5044 got_cancel_cb cancel_cb, void *cancel_arg)
5046 const struct got_error *err;
5047 char *commit_ref_name;
5049 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5050 if (err)
5051 return err;
5053 err = store_commit_id(commit_ref_name, commit_id, repo);
5054 if (err)
5055 goto done;
5057 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5058 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5059 progress_arg, cancel_cb, cancel_arg);
5060 done:
5061 free(commit_ref_name);
5062 return err;
5065 const struct got_error *
5066 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5067 struct got_worktree *worktree, struct got_fileindex *fileindex,
5068 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5069 struct got_repository *repo,
5070 got_worktree_checkout_cb progress_cb, void *progress_arg,
5071 got_cancel_cb cancel_cb, void *cancel_arg)
5073 const struct got_error *err;
5074 char *commit_ref_name;
5076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5077 if (err)
5078 return err;
5080 err = store_commit_id(commit_ref_name, commit_id, repo);
5081 if (err)
5082 goto done;
5084 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5085 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5086 progress_arg, cancel_cb, cancel_arg);
5087 done:
5088 free(commit_ref_name);
5089 return err;
5092 static const struct got_error *
5093 rebase_commit(struct got_object_id **new_commit_id,
5094 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5095 struct got_worktree *worktree, struct got_fileindex *fileindex,
5096 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5097 const char *new_logmsg, struct got_repository *repo)
5099 const struct got_error *err, *sync_err;
5100 struct got_pathlist_head commitable_paths;
5101 struct collect_commitables_arg cc_arg;
5102 char *fileindex_path = NULL;
5103 struct got_reference *head_ref = NULL;
5104 struct got_object_id *head_commit_id = NULL;
5105 char *logmsg = NULL;
5107 TAILQ_INIT(&commitable_paths);
5108 *new_commit_id = NULL;
5110 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5112 err = get_fileindex_path(&fileindex_path, worktree);
5113 if (err)
5114 return err;
5116 cc_arg.commitable_paths = &commitable_paths;
5117 cc_arg.worktree = worktree;
5118 cc_arg.repo = repo;
5119 cc_arg.have_staged_files = 0;
5121 * If possible get the status of individual files directly to
5122 * avoid crawling the entire work tree once per rebased commit.
5123 * TODO: Ideally, merged_paths would contain a list of commitables
5124 * we could use so we could skip worktree_status() entirely.
5126 if (merged_paths) {
5127 struct got_pathlist_entry *pe;
5128 if (TAILQ_EMPTY(merged_paths)) {
5129 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5130 goto done;
5132 TAILQ_FOREACH(pe, merged_paths, entry) {
5133 err = worktree_status(worktree, pe->path, fileindex,
5134 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5135 0);
5136 if (err)
5137 goto done;
5139 } else {
5140 err = worktree_status(worktree, "", fileindex, repo,
5141 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5142 if (err)
5143 goto done;
5146 if (TAILQ_EMPTY(&commitable_paths)) {
5147 /* No-op change; commit will be elided. */
5148 err = got_ref_delete(commit_ref, repo);
5149 if (err)
5150 goto done;
5151 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5152 goto done;
5155 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5156 if (err)
5157 goto done;
5159 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5160 if (err)
5161 goto done;
5163 if (new_logmsg) {
5164 logmsg = strdup(new_logmsg);
5165 if (logmsg == NULL) {
5166 err = got_error_from_errno("strdup");
5167 goto done;
5169 } else {
5170 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5171 if (err)
5172 goto done;
5175 /* NB: commit_worktree will call free(logmsg) */
5176 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5177 worktree, got_object_commit_get_author(orig_commit),
5178 got_object_commit_get_committer(orig_commit),
5179 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5180 if (err)
5181 goto done;
5183 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5184 if (err)
5185 goto done;
5187 err = got_ref_delete(commit_ref, repo);
5188 if (err)
5189 goto done;
5191 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5192 fileindex, 0);
5193 sync_err = sync_fileindex(fileindex, fileindex_path);
5194 if (sync_err && err == NULL)
5195 err = sync_err;
5196 done:
5197 free(fileindex_path);
5198 free(head_commit_id);
5199 if (head_ref)
5200 got_ref_close(head_ref);
5201 if (err) {
5202 free(*new_commit_id);
5203 *new_commit_id = NULL;
5205 return err;
5208 const struct got_error *
5209 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5210 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5211 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5212 struct got_commit_object *orig_commit,
5213 struct got_object_id *orig_commit_id, struct got_repository *repo)
5215 const struct got_error *err;
5216 char *commit_ref_name;
5217 struct got_reference *commit_ref = NULL;
5218 struct got_object_id *commit_id = NULL;
5220 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5221 if (err)
5222 return err;
5224 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5225 if (err)
5226 goto done;
5227 err = got_ref_resolve(&commit_id, repo, commit_ref);
5228 if (err)
5229 goto done;
5230 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5231 err = got_error(GOT_ERR_REBASE_COMMITID);
5232 goto done;
5235 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5236 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5237 done:
5238 if (commit_ref)
5239 got_ref_close(commit_ref);
5240 free(commit_ref_name);
5241 free(commit_id);
5242 return err;
5245 const struct got_error *
5246 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5247 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5248 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5249 struct got_commit_object *orig_commit,
5250 struct got_object_id *orig_commit_id, const char *new_logmsg,
5251 struct got_repository *repo)
5253 const struct got_error *err;
5254 char *commit_ref_name;
5255 struct got_reference *commit_ref = NULL;
5256 struct got_object_id *commit_id = NULL;
5258 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5259 if (err)
5260 return err;
5262 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5263 if (err)
5264 goto done;
5265 err = got_ref_resolve(&commit_id, repo, commit_ref);
5266 if (err)
5267 goto done;
5268 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5269 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5270 goto done;
5273 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5274 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5275 done:
5276 if (commit_ref)
5277 got_ref_close(commit_ref);
5278 free(commit_ref_name);
5279 free(commit_id);
5280 return err;
5283 const struct got_error *
5284 got_worktree_rebase_postpone(struct got_worktree *worktree,
5285 struct got_fileindex *fileindex)
5287 if (fileindex)
5288 got_fileindex_free(fileindex);
5289 return lock_worktree(worktree, LOCK_SH);
5292 static const struct got_error *
5293 delete_ref(const char *name, struct got_repository *repo)
5295 const struct got_error *err;
5296 struct got_reference *ref;
5298 err = got_ref_open(&ref, repo, name, 0);
5299 if (err) {
5300 if (err->code == GOT_ERR_NOT_REF)
5301 return NULL;
5302 return err;
5305 err = got_ref_delete(ref, repo);
5306 got_ref_close(ref);
5307 return err;
5310 static const struct got_error *
5311 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5313 const struct got_error *err;
5314 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5315 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5317 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5318 if (err)
5319 goto done;
5320 err = delete_ref(tmp_branch_name, repo);
5321 if (err)
5322 goto done;
5324 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5325 if (err)
5326 goto done;
5327 err = delete_ref(new_base_branch_ref_name, repo);
5328 if (err)
5329 goto done;
5331 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5332 if (err)
5333 goto done;
5334 err = delete_ref(branch_ref_name, repo);
5335 if (err)
5336 goto done;
5338 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5339 if (err)
5340 goto done;
5341 err = delete_ref(commit_ref_name, repo);
5342 if (err)
5343 goto done;
5345 done:
5346 free(tmp_branch_name);
5347 free(new_base_branch_ref_name);
5348 free(branch_ref_name);
5349 free(commit_ref_name);
5350 return err;
5353 const struct got_error *
5354 got_worktree_rebase_complete(struct got_worktree *worktree,
5355 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5356 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5357 struct got_repository *repo)
5359 const struct got_error *err, *unlockerr;
5360 struct got_object_id *new_head_commit_id = NULL;
5362 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5363 if (err)
5364 return err;
5366 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5367 if (err)
5368 goto done;
5370 err = got_ref_write(rebased_branch, repo);
5371 if (err)
5372 goto done;
5374 err = got_worktree_set_head_ref(worktree, rebased_branch);
5375 if (err)
5376 goto done;
5378 err = delete_rebase_refs(worktree, repo);
5379 done:
5380 if (fileindex)
5381 got_fileindex_free(fileindex);
5382 free(new_head_commit_id);
5383 unlockerr = lock_worktree(worktree, LOCK_SH);
5384 if (unlockerr && err == NULL)
5385 err = unlockerr;
5386 return err;
5389 const struct got_error *
5390 got_worktree_rebase_abort(struct got_worktree *worktree,
5391 struct got_fileindex *fileindex, struct got_repository *repo,
5392 struct got_reference *new_base_branch,
5393 got_worktree_checkout_cb progress_cb, void *progress_arg)
5395 const struct got_error *err, *unlockerr, *sync_err;
5396 struct got_reference *resolved = NULL;
5397 struct got_object_id *commit_id = NULL;
5398 char *fileindex_path = NULL;
5399 struct revert_file_args rfa;
5400 struct got_object_id *tree_id = NULL;
5402 err = lock_worktree(worktree, LOCK_EX);
5403 if (err)
5404 return err;
5406 err = got_ref_open(&resolved, repo,
5407 got_ref_get_symref_target(new_base_branch), 0);
5408 if (err)
5409 goto done;
5411 err = got_worktree_set_head_ref(worktree, resolved);
5412 if (err)
5413 goto done;
5416 * XXX commits to the base branch could have happened while
5417 * we were busy rebasing; should we store the original commit ID
5418 * when rebase begins and read it back here?
5420 err = got_ref_resolve(&commit_id, repo, resolved);
5421 if (err)
5422 goto done;
5424 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5425 if (err)
5426 goto done;
5428 err = got_object_id_by_path(&tree_id, repo,
5429 worktree->base_commit_id, worktree->path_prefix);
5430 if (err)
5431 goto done;
5433 err = delete_rebase_refs(worktree, repo);
5434 if (err)
5435 goto done;
5437 err = get_fileindex_path(&fileindex_path, worktree);
5438 if (err)
5439 goto done;
5441 rfa.worktree = worktree;
5442 rfa.fileindex = fileindex;
5443 rfa.progress_cb = progress_cb;
5444 rfa.progress_arg = progress_arg;
5445 rfa.patch_cb = NULL;
5446 rfa.patch_arg = NULL;
5447 rfa.repo = repo;
5448 err = worktree_status(worktree, "", fileindex, repo,
5449 revert_file, &rfa, NULL, NULL, 0, 0);
5450 if (err)
5451 goto sync;
5453 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5454 repo, progress_cb, progress_arg, NULL, NULL);
5455 sync:
5456 sync_err = sync_fileindex(fileindex, fileindex_path);
5457 if (sync_err && err == NULL)
5458 err = sync_err;
5459 done:
5460 got_ref_close(resolved);
5461 free(tree_id);
5462 free(commit_id);
5463 if (fileindex)
5464 got_fileindex_free(fileindex);
5465 free(fileindex_path);
5467 unlockerr = lock_worktree(worktree, LOCK_SH);
5468 if (unlockerr && err == NULL)
5469 err = unlockerr;
5470 return err;
5473 const struct got_error *
5474 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5475 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5476 struct got_fileindex **fileindex, struct got_worktree *worktree,
5477 struct got_repository *repo)
5479 const struct got_error *err = NULL;
5480 char *tmp_branch_name = NULL;
5481 char *branch_ref_name = NULL;
5482 char *base_commit_ref_name = NULL;
5483 char *fileindex_path = NULL;
5484 struct check_rebase_ok_arg ok_arg;
5485 struct got_reference *wt_branch = NULL;
5486 struct got_reference *base_commit_ref = NULL;
5488 *tmp_branch = NULL;
5489 *branch_ref = NULL;
5490 *base_commit_id = NULL;
5491 *fileindex = NULL;
5493 err = lock_worktree(worktree, LOCK_EX);
5494 if (err)
5495 return err;
5497 err = open_fileindex(fileindex, &fileindex_path, worktree);
5498 if (err)
5499 goto done;
5501 ok_arg.worktree = worktree;
5502 ok_arg.repo = repo;
5503 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5504 &ok_arg);
5505 if (err)
5506 goto done;
5508 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5509 if (err)
5510 goto done;
5512 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5513 if (err)
5514 goto done;
5516 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5517 worktree);
5518 if (err)
5519 goto done;
5521 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5522 0);
5523 if (err)
5524 goto done;
5526 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5527 if (err)
5528 goto done;
5530 err = got_ref_write(*branch_ref, repo);
5531 if (err)
5532 goto done;
5534 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5535 worktree->base_commit_id);
5536 if (err)
5537 goto done;
5538 err = got_ref_write(base_commit_ref, repo);
5539 if (err)
5540 goto done;
5541 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5542 if (*base_commit_id == NULL) {
5543 err = got_error_from_errno("got_object_id_dup");
5544 goto done;
5547 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5548 worktree->base_commit_id);
5549 if (err)
5550 goto done;
5551 err = got_ref_write(*tmp_branch, repo);
5552 if (err)
5553 goto done;
5555 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5556 if (err)
5557 goto done;
5558 done:
5559 free(fileindex_path);
5560 free(tmp_branch_name);
5561 free(branch_ref_name);
5562 free(base_commit_ref_name);
5563 if (wt_branch)
5564 got_ref_close(wt_branch);
5565 if (err) {
5566 if (*branch_ref) {
5567 got_ref_close(*branch_ref);
5568 *branch_ref = NULL;
5570 if (*tmp_branch) {
5571 got_ref_close(*tmp_branch);
5572 *tmp_branch = NULL;
5574 free(*base_commit_id);
5575 if (*fileindex) {
5576 got_fileindex_free(*fileindex);
5577 *fileindex = NULL;
5579 lock_worktree(worktree, LOCK_SH);
5581 return err;
5584 const struct got_error *
5585 got_worktree_histedit_postpone(struct got_worktree *worktree,
5586 struct got_fileindex *fileindex)
5588 if (fileindex)
5589 got_fileindex_free(fileindex);
5590 return lock_worktree(worktree, LOCK_SH);
5593 const struct got_error *
5594 got_worktree_histedit_in_progress(int *in_progress,
5595 struct got_worktree *worktree)
5597 const struct got_error *err;
5598 char *tmp_branch_name = NULL;
5600 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5601 if (err)
5602 return err;
5604 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5605 free(tmp_branch_name);
5606 return NULL;
5609 const struct got_error *
5610 got_worktree_histedit_continue(struct got_object_id **commit_id,
5611 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5612 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5613 struct got_worktree *worktree, struct got_repository *repo)
5615 const struct got_error *err;
5616 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5617 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5618 struct got_reference *commit_ref = NULL;
5619 struct got_reference *base_commit_ref = NULL;
5620 char *fileindex_path = NULL;
5621 int have_staged_files = 0;
5623 *commit_id = NULL;
5624 *tmp_branch = NULL;
5625 *base_commit_id = NULL;
5626 *fileindex = NULL;
5628 err = lock_worktree(worktree, LOCK_EX);
5629 if (err)
5630 return err;
5632 err = open_fileindex(fileindex, &fileindex_path, worktree);
5633 if (err)
5634 goto done;
5636 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5637 &have_staged_files);
5638 if (err && err->code != GOT_ERR_CANCELLED)
5639 goto done;
5640 if (have_staged_files) {
5641 err = got_error(GOT_ERR_STAGED_PATHS);
5642 goto done;
5645 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5646 if (err)
5647 goto done;
5649 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5650 if (err)
5651 goto done;
5653 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5654 if (err)
5655 goto done;
5657 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5658 worktree);
5659 if (err)
5660 goto done;
5662 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5663 if (err)
5664 goto done;
5666 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5667 if (err)
5668 goto done;
5669 err = got_ref_resolve(commit_id, repo, commit_ref);
5670 if (err)
5671 goto done;
5673 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5674 if (err)
5675 goto done;
5676 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5677 if (err)
5678 goto done;
5680 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5681 if (err)
5682 goto done;
5683 done:
5684 free(commit_ref_name);
5685 free(branch_ref_name);
5686 free(fileindex_path);
5687 if (commit_ref)
5688 got_ref_close(commit_ref);
5689 if (base_commit_ref)
5690 got_ref_close(base_commit_ref);
5691 if (err) {
5692 free(*commit_id);
5693 *commit_id = NULL;
5694 free(*base_commit_id);
5695 *base_commit_id = NULL;
5696 if (*tmp_branch) {
5697 got_ref_close(*tmp_branch);
5698 *tmp_branch = NULL;
5700 if (*fileindex) {
5701 got_fileindex_free(*fileindex);
5702 *fileindex = NULL;
5704 lock_worktree(worktree, LOCK_EX);
5706 return err;
5709 static const struct got_error *
5710 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5712 const struct got_error *err;
5713 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5714 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5716 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5717 if (err)
5718 goto done;
5719 err = delete_ref(tmp_branch_name, repo);
5720 if (err)
5721 goto done;
5723 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5724 worktree);
5725 if (err)
5726 goto done;
5727 err = delete_ref(base_commit_ref_name, repo);
5728 if (err)
5729 goto done;
5731 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5732 if (err)
5733 goto done;
5734 err = delete_ref(branch_ref_name, repo);
5735 if (err)
5736 goto done;
5738 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5739 if (err)
5740 goto done;
5741 err = delete_ref(commit_ref_name, repo);
5742 if (err)
5743 goto done;
5744 done:
5745 free(tmp_branch_name);
5746 free(base_commit_ref_name);
5747 free(branch_ref_name);
5748 free(commit_ref_name);
5749 return err;
5752 const struct got_error *
5753 got_worktree_histedit_abort(struct got_worktree *worktree,
5754 struct got_fileindex *fileindex, struct got_repository *repo,
5755 struct got_reference *branch, struct got_object_id *base_commit_id,
5756 got_worktree_checkout_cb progress_cb, void *progress_arg)
5758 const struct got_error *err, *unlockerr, *sync_err;
5759 struct got_reference *resolved = NULL;
5760 char *fileindex_path = NULL;
5761 struct got_object_id *tree_id = NULL;
5762 struct revert_file_args rfa;
5764 err = lock_worktree(worktree, LOCK_EX);
5765 if (err)
5766 return err;
5768 err = got_ref_open(&resolved, repo,
5769 got_ref_get_symref_target(branch), 0);
5770 if (err)
5771 goto done;
5773 err = got_worktree_set_head_ref(worktree, resolved);
5774 if (err)
5775 goto done;
5777 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5778 if (err)
5779 goto done;
5781 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5782 worktree->path_prefix);
5783 if (err)
5784 goto done;
5786 err = delete_histedit_refs(worktree, repo);
5787 if (err)
5788 goto done;
5790 err = get_fileindex_path(&fileindex_path, worktree);
5791 if (err)
5792 goto done;
5794 rfa.worktree = worktree;
5795 rfa.fileindex = fileindex;
5796 rfa.progress_cb = progress_cb;
5797 rfa.progress_arg = progress_arg;
5798 rfa.patch_cb = NULL;
5799 rfa.patch_arg = NULL;
5800 rfa.repo = repo;
5801 err = worktree_status(worktree, "", fileindex, repo,
5802 revert_file, &rfa, NULL, NULL, 0, 0);
5803 if (err)
5804 goto sync;
5806 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5807 repo, progress_cb, progress_arg, NULL, NULL);
5808 sync:
5809 sync_err = sync_fileindex(fileindex, fileindex_path);
5810 if (sync_err && err == NULL)
5811 err = sync_err;
5812 done:
5813 got_ref_close(resolved);
5814 free(tree_id);
5815 free(fileindex_path);
5817 unlockerr = lock_worktree(worktree, LOCK_SH);
5818 if (unlockerr && err == NULL)
5819 err = unlockerr;
5820 return err;
5823 const struct got_error *
5824 got_worktree_histedit_complete(struct got_worktree *worktree,
5825 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5826 struct got_reference *edited_branch, struct got_repository *repo)
5828 const struct got_error *err, *unlockerr;
5829 struct got_object_id *new_head_commit_id = NULL;
5830 struct got_reference *resolved = NULL;
5832 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5833 if (err)
5834 return err;
5836 err = got_ref_open(&resolved, repo,
5837 got_ref_get_symref_target(edited_branch), 0);
5838 if (err)
5839 goto done;
5841 err = got_ref_change_ref(resolved, new_head_commit_id);
5842 if (err)
5843 goto done;
5845 err = got_ref_write(resolved, repo);
5846 if (err)
5847 goto done;
5849 err = got_worktree_set_head_ref(worktree, resolved);
5850 if (err)
5851 goto done;
5853 err = delete_histedit_refs(worktree, repo);
5854 done:
5855 if (fileindex)
5856 got_fileindex_free(fileindex);
5857 free(new_head_commit_id);
5858 unlockerr = lock_worktree(worktree, LOCK_SH);
5859 if (unlockerr && err == NULL)
5860 err = unlockerr;
5861 return err;
5864 const struct got_error *
5865 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5866 struct got_object_id *commit_id, struct got_repository *repo)
5868 const struct got_error *err;
5869 char *commit_ref_name;
5871 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5872 if (err)
5873 return err;
5875 err = store_commit_id(commit_ref_name, commit_id, repo);
5876 if (err)
5877 goto done;
5879 err = delete_ref(commit_ref_name, repo);
5880 done:
5881 free(commit_ref_name);
5882 return err;
5885 const struct got_error *
5886 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5887 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5888 struct got_worktree *worktree, const char *refname,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL;
5892 char *fileindex_path = NULL;
5893 struct check_rebase_ok_arg ok_arg;
5895 *fileindex = NULL;
5896 *branch_ref = NULL;
5897 *base_branch_ref = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5900 if (err)
5901 return err;
5903 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5904 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5905 "cannot integrate a branch into itself; "
5906 "update -b or different branch name required");
5907 goto done;
5910 err = open_fileindex(fileindex, &fileindex_path, worktree);
5911 if (err)
5912 goto done;
5914 /* Preconditions are the same as for rebase. */
5915 ok_arg.worktree = worktree;
5916 ok_arg.repo = repo;
5917 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5918 &ok_arg);
5919 if (err)
5920 goto done;
5922 err = got_ref_open(branch_ref, repo, refname, 1);
5923 if (err)
5924 goto done;
5926 err = got_ref_open(base_branch_ref, repo,
5927 got_worktree_get_head_ref_name(worktree), 1);
5928 done:
5929 if (err) {
5930 if (*branch_ref) {
5931 got_ref_close(*branch_ref);
5932 *branch_ref = NULL;
5934 if (*base_branch_ref) {
5935 got_ref_close(*base_branch_ref);
5936 *base_branch_ref = NULL;
5938 if (*fileindex) {
5939 got_fileindex_free(*fileindex);
5940 *fileindex = NULL;
5942 lock_worktree(worktree, LOCK_SH);
5944 return err;
5947 const struct got_error *
5948 got_worktree_integrate_continue(struct got_worktree *worktree,
5949 struct got_fileindex *fileindex, struct got_repository *repo,
5950 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5951 got_worktree_checkout_cb progress_cb, void *progress_arg,
5952 got_cancel_cb cancel_cb, void *cancel_arg)
5954 const struct got_error *err = NULL, *sync_err, *unlockerr;
5955 char *fileindex_path = NULL;
5956 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5958 err = get_fileindex_path(&fileindex_path, worktree);
5959 if (err)
5960 goto done;
5962 err = got_ref_resolve(&commit_id, repo, branch_ref);
5963 if (err)
5964 goto done;
5966 err = got_object_id_by_path(&tree_id, repo, commit_id,
5967 worktree->path_prefix);
5968 if (err)
5969 goto done;
5971 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5972 if (err)
5973 goto done;
5975 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5976 progress_cb, progress_arg, cancel_cb, cancel_arg);
5977 if (err)
5978 goto sync;
5980 err = got_ref_change_ref(base_branch_ref, commit_id);
5981 if (err)
5982 goto sync;
5984 err = got_ref_write(base_branch_ref, repo);
5985 sync:
5986 sync_err = sync_fileindex(fileindex, fileindex_path);
5987 if (sync_err && err == NULL)
5988 err = sync_err;
5990 done:
5991 unlockerr = got_ref_unlock(branch_ref);
5992 if (unlockerr && err == NULL)
5993 err = unlockerr;
5994 got_ref_close(branch_ref);
5996 unlockerr = got_ref_unlock(base_branch_ref);
5997 if (unlockerr && err == NULL)
5998 err = unlockerr;
5999 got_ref_close(base_branch_ref);
6001 got_fileindex_free(fileindex);
6002 free(fileindex_path);
6003 free(tree_id);
6005 unlockerr = lock_worktree(worktree, LOCK_SH);
6006 if (unlockerr && err == NULL)
6007 err = unlockerr;
6008 return err;
6011 const struct got_error *
6012 got_worktree_integrate_abort(struct got_worktree *worktree,
6013 struct got_fileindex *fileindex, struct got_repository *repo,
6014 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6016 const struct got_error *err = NULL, *unlockerr = NULL;
6018 got_fileindex_free(fileindex);
6020 err = lock_worktree(worktree, LOCK_SH);
6022 unlockerr = got_ref_unlock(branch_ref);
6023 if (unlockerr && err == NULL)
6024 err = unlockerr;
6025 got_ref_close(branch_ref);
6027 unlockerr = got_ref_unlock(base_branch_ref);
6028 if (unlockerr && err == NULL)
6029 err = unlockerr;
6030 got_ref_close(base_branch_ref);
6032 return err;
6035 struct check_stage_ok_arg {
6036 struct got_object_id *head_commit_id;
6037 struct got_worktree *worktree;
6038 struct got_fileindex *fileindex;
6039 struct got_repository *repo;
6040 int have_changes;
6043 const struct got_error *
6044 check_stage_ok(void *arg, unsigned char status,
6045 unsigned char staged_status, const char *relpath,
6046 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6047 struct got_object_id *commit_id, int dirfd, const char *de_name)
6049 struct check_stage_ok_arg *a = arg;
6050 const struct got_error *err = NULL;
6051 struct got_fileindex_entry *ie;
6052 struct got_object_id base_commit_id;
6053 struct got_object_id *base_commit_idp = NULL;
6054 char *in_repo_path = NULL, *p;
6056 if (status == GOT_STATUS_UNVERSIONED ||
6057 status == GOT_STATUS_NO_CHANGE)
6058 return NULL;
6059 if (status == GOT_STATUS_NONEXISTENT)
6060 return got_error_set_errno(ENOENT, relpath);
6062 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6063 if (ie == NULL)
6064 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6066 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6067 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6068 relpath) == -1)
6069 return got_error_from_errno("asprintf");
6071 if (got_fileindex_entry_has_commit(ie)) {
6072 memcpy(base_commit_id.sha1, ie->commit_sha1,
6073 SHA1_DIGEST_LENGTH);
6074 base_commit_idp = &base_commit_id;
6077 if (status == GOT_STATUS_CONFLICT) {
6078 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6079 goto done;
6080 } else if (status != GOT_STATUS_ADD &&
6081 status != GOT_STATUS_MODIFY &&
6082 status != GOT_STATUS_DELETE) {
6083 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6084 goto done;
6087 a->have_changes = 1;
6089 p = in_repo_path;
6090 while (p[0] == '/')
6091 p++;
6092 err = check_out_of_date(p, status, staged_status,
6093 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6094 GOT_ERR_STAGE_OUT_OF_DATE);
6095 done:
6096 free(in_repo_path);
6097 return err;
6100 struct stage_path_arg {
6101 struct got_worktree *worktree;
6102 struct got_fileindex *fileindex;
6103 struct got_repository *repo;
6104 got_worktree_status_cb status_cb;
6105 void *status_arg;
6106 got_worktree_patch_cb patch_cb;
6107 void *patch_arg;
6108 int staged_something;
6111 static const struct got_error *
6112 stage_path(void *arg, unsigned char status,
6113 unsigned char staged_status, const char *relpath,
6114 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6115 struct got_object_id *commit_id, int dirfd, const char *de_name)
6117 struct stage_path_arg *a = arg;
6118 const struct got_error *err = NULL;
6119 struct got_fileindex_entry *ie;
6120 char *ondisk_path = NULL, *path_content = NULL;
6121 uint32_t stage;
6122 struct got_object_id *new_staged_blob_id = NULL;
6124 if (status == GOT_STATUS_UNVERSIONED)
6125 return NULL;
6127 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6128 if (ie == NULL)
6129 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6131 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6132 relpath)== -1)
6133 return got_error_from_errno("asprintf");
6135 switch (status) {
6136 case GOT_STATUS_ADD:
6137 case GOT_STATUS_MODIFY:
6138 if (a->patch_cb) {
6139 if (status == GOT_STATUS_ADD) {
6140 int choice = GOT_PATCH_CHOICE_NONE;
6141 err = (*a->patch_cb)(&choice, a->patch_arg,
6142 status, ie->path, NULL, 1, 1);
6143 if (err)
6144 break;
6145 if (choice != GOT_PATCH_CHOICE_YES)
6146 break;
6147 } else {
6148 err = create_patched_content(&path_content, 0,
6149 staged_blob_id ? staged_blob_id : blob_id,
6150 ondisk_path, dirfd, de_name, ie->path,
6151 a->repo, a->patch_cb, a->patch_arg);
6152 if (err || path_content == NULL)
6153 break;
6156 err = got_object_blob_create(&new_staged_blob_id,
6157 path_content ? path_content : ondisk_path, a->repo);
6158 if (err)
6159 break;
6160 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6161 SHA1_DIGEST_LENGTH);
6162 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6163 stage = GOT_FILEIDX_STAGE_ADD;
6164 else
6165 stage = GOT_FILEIDX_STAGE_MODIFY;
6166 got_fileindex_entry_stage_set(ie, stage);
6167 a->staged_something = 1;
6168 if (a->status_cb == NULL)
6169 break;
6170 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6171 get_staged_status(ie), relpath, blob_id,
6172 new_staged_blob_id, NULL, dirfd, de_name);
6173 break;
6174 case GOT_STATUS_DELETE:
6175 if (staged_status == GOT_STATUS_DELETE)
6176 break;
6177 if (a->patch_cb) {
6178 int choice = GOT_PATCH_CHOICE_NONE;
6179 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6180 ie->path, NULL, 1, 1);
6181 if (err)
6182 break;
6183 if (choice == GOT_PATCH_CHOICE_NO)
6184 break;
6185 if (choice != GOT_PATCH_CHOICE_YES) {
6186 err = got_error(GOT_ERR_PATCH_CHOICE);
6187 break;
6190 stage = GOT_FILEIDX_STAGE_DELETE;
6191 got_fileindex_entry_stage_set(ie, stage);
6192 a->staged_something = 1;
6193 if (a->status_cb == NULL)
6194 break;
6195 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6196 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6197 de_name);
6198 break;
6199 case GOT_STATUS_NO_CHANGE:
6200 break;
6201 case GOT_STATUS_CONFLICT:
6202 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6203 break;
6204 case GOT_STATUS_NONEXISTENT:
6205 err = got_error_set_errno(ENOENT, relpath);
6206 break;
6207 default:
6208 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6209 break;
6212 if (path_content && unlink(path_content) == -1 && err == NULL)
6213 err = got_error_from_errno2("unlink", path_content);
6214 free(path_content);
6215 free(ondisk_path);
6216 free(new_staged_blob_id);
6217 return err;
6220 const struct got_error *
6221 got_worktree_stage(struct got_worktree *worktree,
6222 struct got_pathlist_head *paths,
6223 got_worktree_status_cb status_cb, void *status_arg,
6224 got_worktree_patch_cb patch_cb, void *patch_arg,
6225 struct got_repository *repo)
6227 const struct got_error *err = NULL, *sync_err, *unlockerr;
6228 struct got_pathlist_entry *pe;
6229 struct got_fileindex *fileindex = NULL;
6230 char *fileindex_path = NULL;
6231 struct got_reference *head_ref = NULL;
6232 struct got_object_id *head_commit_id = NULL;
6233 struct check_stage_ok_arg oka;
6234 struct stage_path_arg spa;
6236 err = lock_worktree(worktree, LOCK_EX);
6237 if (err)
6238 return err;
6240 err = got_ref_open(&head_ref, repo,
6241 got_worktree_get_head_ref_name(worktree), 0);
6242 if (err)
6243 goto done;
6244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6245 if (err)
6246 goto done;
6247 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6248 if (err)
6249 goto done;
6251 /* Check pre-conditions before staging anything. */
6252 oka.head_commit_id = head_commit_id;
6253 oka.worktree = worktree;
6254 oka.fileindex = fileindex;
6255 oka.repo = repo;
6256 oka.have_changes = 0;
6257 TAILQ_FOREACH(pe, paths, entry) {
6258 err = worktree_status(worktree, pe->path, fileindex, repo,
6259 check_stage_ok, &oka, NULL, NULL, 0, 0);
6260 if (err)
6261 goto done;
6263 if (!oka.have_changes) {
6264 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6265 goto done;
6268 spa.worktree = worktree;
6269 spa.fileindex = fileindex;
6270 spa.repo = repo;
6271 spa.patch_cb = patch_cb;
6272 spa.patch_arg = patch_arg;
6273 spa.status_cb = status_cb;
6274 spa.status_arg = status_arg;
6275 spa.staged_something = 0;
6276 TAILQ_FOREACH(pe, paths, entry) {
6277 err = worktree_status(worktree, pe->path, fileindex, repo,
6278 stage_path, &spa, NULL, NULL, 0, 0);
6279 if (err)
6280 goto done;
6282 if (!spa.staged_something) {
6283 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6284 goto done;
6287 sync_err = sync_fileindex(fileindex, fileindex_path);
6288 if (sync_err && err == NULL)
6289 err = sync_err;
6290 done:
6291 if (head_ref)
6292 got_ref_close(head_ref);
6293 free(head_commit_id);
6294 free(fileindex_path);
6295 if (fileindex)
6296 got_fileindex_free(fileindex);
6297 unlockerr = lock_worktree(worktree, LOCK_SH);
6298 if (unlockerr && err == NULL)
6299 err = unlockerr;
6300 return err;
6303 struct unstage_path_arg {
6304 struct got_worktree *worktree;
6305 struct got_fileindex *fileindex;
6306 struct got_repository *repo;
6307 got_worktree_checkout_cb progress_cb;
6308 void *progress_arg;
6309 got_worktree_patch_cb patch_cb;
6310 void *patch_arg;
6313 static const struct got_error *
6314 create_unstaged_content(char **path_unstaged_content,
6315 char **path_new_staged_content, struct got_object_id *blob_id,
6316 struct got_object_id *staged_blob_id, const char *relpath,
6317 struct got_repository *repo,
6318 got_worktree_patch_cb patch_cb, void *patch_arg)
6320 const struct got_error *err;
6321 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6322 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6323 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6324 struct stat sb1, sb2;
6325 struct got_diff_changes *changes = NULL;
6326 struct got_diff_state *ds = NULL;
6327 struct got_diff_args *args = NULL;
6328 struct got_diff_change *change;
6329 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6330 int have_content = 0, have_rejected_content = 0;
6332 *path_unstaged_content = NULL;
6333 *path_new_staged_content = NULL;
6335 err = got_object_id_str(&label1, blob_id);
6336 if (err)
6337 return err;
6338 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6339 if (err)
6340 goto done;
6342 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6343 if (err)
6344 goto done;
6346 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6347 if (err)
6348 goto done;
6350 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6351 if (err)
6352 goto done;
6354 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6355 if (err)
6356 goto done;
6358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6359 if (err)
6360 goto done;
6362 if (stat(path1, &sb1) == -1) {
6363 err = got_error_from_errno2("stat", path1);
6364 goto done;
6367 if (stat(path2, &sb2) == -1) {
6368 err = got_error_from_errno2("stat", path2);
6369 goto done;
6372 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6373 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6374 if (err)
6375 goto done;
6377 err = got_opentemp_named(path_unstaged_content, &outfile,
6378 "got-unstaged-content");
6379 if (err)
6380 goto done;
6381 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6382 "got-new-staged-content");
6383 if (err)
6384 goto done;
6386 if (fseek(f1, 0L, SEEK_SET) == -1) {
6387 err = got_ferror(f1, GOT_ERR_IO);
6388 goto done;
6390 if (fseek(f2, 0L, SEEK_SET) == -1) {
6391 err = got_ferror(f2, GOT_ERR_IO);
6392 goto done;
6394 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6395 int choice;
6396 err = apply_or_reject_change(&choice, change, ++n,
6397 changes->nchanges, ds, args, diff_flags, relpath,
6398 f1, f2, &line_cur1, &line_cur2,
6399 outfile, rejectfile, patch_cb, patch_arg);
6400 if (err)
6401 goto done;
6402 if (choice == GOT_PATCH_CHOICE_YES)
6403 have_content = 1;
6404 else
6405 have_rejected_content = 1;
6406 if (choice == GOT_PATCH_CHOICE_QUIT)
6407 break;
6409 if (have_content || have_rejected_content)
6410 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6411 outfile, rejectfile);
6412 done:
6413 free(label1);
6414 if (blob)
6415 got_object_blob_close(blob);
6416 if (staged_blob)
6417 got_object_blob_close(staged_blob);
6418 if (f1 && fclose(f1) == EOF && err == NULL)
6419 err = got_error_from_errno2("fclose", path1);
6420 if (f2 && fclose(f2) == EOF && err == NULL)
6421 err = got_error_from_errno2("fclose", path2);
6422 if (outfile && fclose(outfile) == EOF && err == NULL)
6423 err = got_error_from_errno2("fclose", *path_unstaged_content);
6424 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6425 err = got_error_from_errno2("fclose", *path_new_staged_content);
6426 if (path1 && unlink(path1) == -1 && err == NULL)
6427 err = got_error_from_errno2("unlink", path1);
6428 if (path2 && unlink(path2) == -1 && err == NULL)
6429 err = got_error_from_errno2("unlink", path2);
6430 if (err || !have_content) {
6431 if (*path_unstaged_content &&
6432 unlink(*path_unstaged_content) == -1 && err == NULL)
6433 err = got_error_from_errno2("unlink",
6434 *path_unstaged_content);
6435 free(*path_unstaged_content);
6436 *path_unstaged_content = NULL;
6438 if (err || !have_rejected_content) {
6439 if (*path_new_staged_content &&
6440 unlink(*path_new_staged_content) == -1 && err == NULL)
6441 err = got_error_from_errno2("unlink",
6442 *path_new_staged_content);
6443 free(*path_new_staged_content);
6444 *path_new_staged_content = NULL;
6446 free(args);
6447 if (ds) {
6448 got_diff_state_free(ds);
6449 free(ds);
6451 if (changes)
6452 got_diff_free_changes(changes);
6453 free(path1);
6454 free(path2);
6455 return err;
6458 static const struct got_error *
6459 unstage_path(void *arg, unsigned char status,
6460 unsigned char staged_status, const char *relpath,
6461 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6462 struct got_object_id *commit_id, int dirfd, const char *de_name)
6464 const struct got_error *err = NULL;
6465 struct unstage_path_arg *a = arg;
6466 struct got_fileindex_entry *ie;
6467 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6468 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6469 char *path_new_staged_content = NULL;
6470 char *id_str = NULL, *label_orig = NULL;
6471 int local_changes_subsumed;
6472 struct stat sb;
6474 if (staged_status != GOT_STATUS_ADD &&
6475 staged_status != GOT_STATUS_MODIFY &&
6476 staged_status != GOT_STATUS_DELETE)
6477 return NULL;
6479 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6480 if (ie == NULL)
6481 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6483 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6484 == -1)
6485 return got_error_from_errno("asprintf");
6487 err = got_object_id_str(&id_str,
6488 commit_id ? commit_id : a->worktree->base_commit_id);
6489 if (err)
6490 goto done;
6491 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6492 id_str) == -1) {
6493 err = got_error_from_errno("asprintf");
6494 goto done;
6497 switch (staged_status) {
6498 case GOT_STATUS_MODIFY:
6499 err = got_object_open_as_blob(&blob_base, a->repo,
6500 blob_id, 8192);
6501 if (err)
6502 break;
6503 /* fall through */
6504 case GOT_STATUS_ADD:
6505 if (a->patch_cb) {
6506 if (staged_status == GOT_STATUS_ADD) {
6507 int choice = GOT_PATCH_CHOICE_NONE;
6508 err = (*a->patch_cb)(&choice, a->patch_arg,
6509 staged_status, ie->path, NULL, 1, 1);
6510 if (err)
6511 break;
6512 if (choice != GOT_PATCH_CHOICE_YES)
6513 break;
6514 } else {
6515 err = create_unstaged_content(
6516 &path_unstaged_content,
6517 &path_new_staged_content, blob_id,
6518 staged_blob_id, ie->path, a->repo,
6519 a->patch_cb, a->patch_arg);
6520 if (err || path_unstaged_content == NULL)
6521 break;
6522 if (path_new_staged_content) {
6523 err = got_object_blob_create(
6524 &staged_blob_id,
6525 path_new_staged_content,
6526 a->repo);
6527 if (err)
6528 break;
6529 memcpy(ie->staged_blob_sha1,
6530 staged_blob_id->sha1,
6531 SHA1_DIGEST_LENGTH);
6533 err = merge_file(&local_changes_subsumed,
6534 a->worktree, blob_base, ondisk_path,
6535 relpath, got_fileindex_perms_to_st(ie),
6536 path_unstaged_content, label_orig,
6537 "unstaged", a->repo, a->progress_cb,
6538 a->progress_arg);
6539 if (err == NULL &&
6540 path_new_staged_content == NULL)
6541 got_fileindex_entry_stage_set(ie,
6542 GOT_FILEIDX_STAGE_NONE);
6543 break; /* Done with this file. */
6546 err = got_object_open_as_blob(&blob_staged, a->repo,
6547 staged_blob_id, 8192);
6548 if (err)
6549 break;
6550 err = merge_blob(&local_changes_subsumed, a->worktree,
6551 blob_base, ondisk_path, relpath,
6552 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6553 commit_id ? commit_id : a->worktree->base_commit_id,
6554 a->repo, a->progress_cb, a->progress_arg);
6555 if (err == NULL)
6556 got_fileindex_entry_stage_set(ie,
6557 GOT_FILEIDX_STAGE_NONE);
6558 break;
6559 case GOT_STATUS_DELETE:
6560 if (a->patch_cb) {
6561 int choice = GOT_PATCH_CHOICE_NONE;
6562 err = (*a->patch_cb)(&choice, a->patch_arg,
6563 staged_status, ie->path, NULL, 1, 1);
6564 if (err)
6565 break;
6566 if (choice == GOT_PATCH_CHOICE_NO)
6567 break;
6568 if (choice != GOT_PATCH_CHOICE_YES) {
6569 err = got_error(GOT_ERR_PATCH_CHOICE);
6570 break;
6573 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6574 err = get_file_status(&status, &sb, ie, ondisk_path,
6575 dirfd, de_name, a->repo);
6576 if (err)
6577 break;
6578 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6579 break;
6581 done:
6582 free(ondisk_path);
6583 if (path_unstaged_content &&
6584 unlink(path_unstaged_content) == -1 && err == NULL)
6585 err = got_error_from_errno2("unlink", path_unstaged_content);
6586 if (path_new_staged_content &&
6587 unlink(path_new_staged_content) == -1 && err == NULL)
6588 err = got_error_from_errno2("unlink", path_new_staged_content);
6589 free(path_unstaged_content);
6590 free(path_new_staged_content);
6591 if (blob_base)
6592 got_object_blob_close(blob_base);
6593 if (blob_staged)
6594 got_object_blob_close(blob_staged);
6595 free(id_str);
6596 free(label_orig);
6597 return err;
6600 const struct got_error *
6601 got_worktree_unstage(struct got_worktree *worktree,
6602 struct got_pathlist_head *paths,
6603 got_worktree_checkout_cb progress_cb, void *progress_arg,
6604 got_worktree_patch_cb patch_cb, void *patch_arg,
6605 struct got_repository *repo)
6607 const struct got_error *err = NULL, *sync_err, *unlockerr;
6608 struct got_pathlist_entry *pe;
6609 struct got_fileindex *fileindex = NULL;
6610 char *fileindex_path = NULL;
6611 struct unstage_path_arg upa;
6613 err = lock_worktree(worktree, LOCK_EX);
6614 if (err)
6615 return err;
6617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6618 if (err)
6619 goto done;
6621 upa.worktree = worktree;
6622 upa.fileindex = fileindex;
6623 upa.repo = repo;
6624 upa.progress_cb = progress_cb;
6625 upa.progress_arg = progress_arg;
6626 upa.patch_cb = patch_cb;
6627 upa.patch_arg = patch_arg;
6628 TAILQ_FOREACH(pe, paths, entry) {
6629 err = worktree_status(worktree, pe->path, fileindex, repo,
6630 unstage_path, &upa, NULL, NULL, 0, 0);
6631 if (err)
6632 goto done;
6635 sync_err = sync_fileindex(fileindex, fileindex_path);
6636 if (sync_err && err == NULL)
6637 err = sync_err;
6638 done:
6639 free(fileindex_path);
6640 if (fileindex)
6641 got_fileindex_free(fileindex);
6642 unlockerr = lock_worktree(worktree, LOCK_SH);
6643 if (unlockerr && err == NULL)
6644 err = unlockerr;
6645 return err;