Blob


1 /*
2 * Copyright (c) 2018, 2019 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("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", 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 (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", 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 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 int dirfd, const char *de_name, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 int fd = -1;
1116 FILE *f = NULL;
1117 uint8_t fbuf[8192];
1118 struct got_blob_object *blob = NULL;
1119 size_t flen, blen;
1120 unsigned char staged_status = get_staged_status(ie);
1122 *status = GOT_STATUS_NO_CHANGE;
1125 * Whenever the caller provides a directory descriptor and a
1126 * directory entry name for the file, use them! This prevents
1127 * race conditions if filesystem paths change beneath our feet.
1129 if (dirfd != -1) {
1130 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1131 if (errno == ENOENT) {
1132 if (got_fileindex_entry_has_file_on_disk(ie))
1133 *status = GOT_STATUS_MISSING;
1134 else
1135 *status = GOT_STATUS_DELETE;
1136 goto done;
1138 err = got_error_from_errno2("fstatat", abspath);
1139 goto done;
1141 } else {
1142 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1143 if (fd == -1 && errno != ENOENT)
1144 return got_error_from_errno2("open", abspath);
1145 if (fd == -1 || fstat(fd, sb) == -1) {
1146 if (errno == ENOENT) {
1147 if (got_fileindex_entry_has_file_on_disk(ie))
1148 *status = GOT_STATUS_MISSING;
1149 else
1150 *status = GOT_STATUS_DELETE;
1151 goto done;
1153 err = got_error_from_errno2("fstat", abspath);
1154 goto done;
1158 if (!S_ISREG(sb->st_mode)) {
1159 *status = GOT_STATUS_OBSTRUCTED;
1160 goto done;
1163 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1164 *status = GOT_STATUS_DELETE;
1165 goto done;
1166 } else if (!got_fileindex_entry_has_blob(ie) &&
1167 staged_status != GOT_STATUS_ADD) {
1168 *status = GOT_STATUS_ADD;
1169 goto done;
1172 if (!stat_info_differs(ie, sb))
1173 goto done;
1175 if (staged_status == GOT_STATUS_MODIFY ||
1176 staged_status == GOT_STATUS_ADD)
1177 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1178 else
1179 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1181 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1182 if (err)
1183 goto done;
1185 if (dirfd != -1) {
1186 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1187 if (fd == -1) {
1188 err = got_error_from_errno2("openat", abspath);
1189 goto done;
1193 f = fdopen(fd, "r");
1194 if (f == NULL) {
1195 err = got_error_from_errno2("fopen", abspath);
1196 goto done;
1198 fd = -1;
1199 hdrlen = got_object_blob_get_hdrlen(blob);
1200 for (;;) {
1201 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1202 err = got_object_blob_read_block(&blen, blob);
1203 if (err)
1204 goto done;
1205 /* Skip length of blob object header first time around. */
1206 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1207 if (flen == 0 && ferror(f)) {
1208 err = got_error_from_errno("fread");
1209 goto done;
1211 if (blen == 0) {
1212 if (flen != 0)
1213 *status = GOT_STATUS_MODIFY;
1214 break;
1215 } else if (flen == 0) {
1216 if (blen != 0)
1217 *status = GOT_STATUS_MODIFY;
1218 break;
1219 } else if (blen - hdrlen == flen) {
1220 /* Skip blob object header first time around. */
1221 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1222 *status = GOT_STATUS_MODIFY;
1223 break;
1225 } else {
1226 *status = GOT_STATUS_MODIFY;
1227 break;
1229 hdrlen = 0;
1232 if (*status == GOT_STATUS_MODIFY) {
1233 rewind(f);
1234 err = get_modified_file_content_status(status, f);
1235 } else if (xbit_differs(ie, sb->st_mode))
1236 *status = GOT_STATUS_MODE_CHANGE;
1237 done:
1238 if (blob)
1239 got_object_blob_close(blob);
1240 if (f != NULL && fclose(f) == EOF && err == NULL)
1241 err = got_error_from_errno2("fclose", abspath);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", abspath);
1244 return err;
1248 * Update timestamps in the file index if a file is unmodified and
1249 * we had to run a full content comparison to find out.
1251 static const struct got_error *
1252 sync_timestamps(char *ondisk_path, unsigned char status,
1253 struct got_fileindex_entry *ie, struct stat *sb)
1255 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1256 return got_fileindex_entry_update(ie, ondisk_path,
1257 ie->blob_sha1, ie->commit_sha1, 1);
1259 return NULL;
1262 static const struct got_error *
1263 update_blob(struct got_worktree *worktree,
1264 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1265 struct got_tree_entry *te, const char *path,
1266 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1267 void *progress_arg)
1269 const struct got_error *err = NULL;
1270 struct got_blob_object *blob = NULL;
1271 char *ondisk_path;
1272 unsigned char status = GOT_STATUS_NO_CHANGE;
1273 struct stat sb;
1275 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1276 return got_error_from_errno("asprintf");
1278 if (ie) {
1279 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1280 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1281 goto done;
1283 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1284 repo);
1285 if (err)
1286 goto done;
1287 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1288 sb.st_mode = got_fileindex_perms_to_st(ie);
1289 } else
1290 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1292 if (status == GOT_STATUS_OBSTRUCTED) {
1293 err = (*progress_cb)(progress_arg, status, path);
1294 goto done;
1297 if (ie && status != GOT_STATUS_MISSING &&
1298 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1299 if (got_fileindex_entry_has_commit(ie) &&
1300 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1301 SHA1_DIGEST_LENGTH) == 0) {
1302 err = sync_timestamps(ondisk_path, status, ie, &sb);
1303 if (err)
1304 goto done;
1305 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1306 path);
1307 goto done;
1309 if (got_fileindex_entry_has_blob(ie) &&
1310 memcmp(ie->blob_sha1, te->id.sha1,
1311 SHA1_DIGEST_LENGTH) == 0) {
1312 err = sync_timestamps(ondisk_path, status, ie, &sb);
1313 goto done;
1317 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1318 if (err)
1319 goto done;
1321 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1322 int update_timestamps;
1323 struct got_blob_object *blob2 = NULL;
1324 char *label_orig = NULL;
1325 if (got_fileindex_entry_has_blob(ie)) {
1326 struct got_object_id id2;
1327 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1328 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1329 if (err)
1330 goto done;
1332 if (got_fileindex_entry_has_commit(ie)) {
1333 char id_str[SHA1_DIGEST_STRING_LENGTH];
1334 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1335 sizeof(id_str)) == NULL) {
1336 err = got_error_path(id_str,
1337 GOT_ERR_BAD_OBJ_ID_STR);
1338 goto done;
1340 if (asprintf(&label_orig, "%s: commit %s",
1341 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1342 err = got_error_from_errno("asprintf");
1343 goto done;
1346 err = merge_blob(&update_timestamps, worktree, blob2,
1347 ondisk_path, path, sb.st_mode, label_orig, blob,
1348 worktree->base_commit_id, repo,
1349 progress_cb, progress_arg);
1350 free(label_orig);
1351 if (blob2)
1352 got_object_blob_close(blob2);
1353 if (err)
1354 goto done;
1356 * Do not update timestamps of files with local changes.
1357 * Otherwise, a future status walk would treat them as
1358 * unmodified files again.
1360 err = got_fileindex_entry_update(ie, ondisk_path,
1361 blob->id.sha1, worktree->base_commit_id->sha1,
1362 update_timestamps);
1363 } else if (status == GOT_STATUS_MODE_CHANGE) {
1364 err = got_fileindex_entry_update(ie, ondisk_path,
1365 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1366 } else if (status == GOT_STATUS_DELETE) {
1367 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1368 if (err)
1369 goto done;
1370 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1371 ondisk_path, path, blob, 0);
1372 if (err)
1373 goto done;
1374 } else {
1375 err = install_blob(worktree, ondisk_path, path, te->mode,
1376 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1377 repo, progress_cb, progress_arg);
1378 if (err)
1379 goto done;
1380 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1381 ondisk_path, path, blob, 1);
1382 if (err)
1383 goto done;
1385 got_object_blob_close(blob);
1386 done:
1387 free(ondisk_path);
1388 return err;
1391 static const struct got_error *
1392 remove_ondisk_file(const char *root_path, const char *path)
1394 const struct got_error *err = NULL;
1395 char *ondisk_path = NULL;
1397 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1398 return got_error_from_errno("asprintf");
1400 if (unlink(ondisk_path) == -1) {
1401 if (errno != ENOENT)
1402 err = got_error_from_errno2("unlink", ondisk_path);
1403 } else {
1404 char *parent = dirname(ondisk_path);
1405 while (parent && strcmp(parent, root_path) != 0) {
1406 if (rmdir(parent) == -1) {
1407 if (errno != ENOTEMPTY)
1408 err = got_error_from_errno2("rmdir",
1409 parent);
1410 break;
1412 parent = dirname(parent);
1415 free(ondisk_path);
1416 return err;
1419 static const struct got_error *
1420 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1421 struct got_fileindex_entry *ie, struct got_repository *repo,
1422 got_worktree_checkout_cb progress_cb, void *progress_arg)
1424 const struct got_error *err = NULL;
1425 unsigned char status;
1426 struct stat sb;
1427 char *ondisk_path;
1429 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1430 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1432 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1433 == -1)
1434 return got_error_from_errno("asprintf");
1436 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1437 if (err)
1438 return err;
1440 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1441 status == GOT_STATUS_ADD) {
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1443 if (err)
1444 return err;
1446 * Preserve the working file and change the deleted blob's
1447 * entry into a schedule-add entry.
1449 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1450 0);
1451 if (err)
1452 return err;
1453 } else {
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1455 if (err)
1456 return err;
1457 if (status == GOT_STATUS_NO_CHANGE) {
1458 err = remove_ondisk_file(worktree->root_path, ie->path);
1459 if (err)
1460 return err;
1462 got_fileindex_entry_remove(fileindex, ie);
1465 return err;
1468 struct diff_cb_arg {
1469 struct got_fileindex *fileindex;
1470 struct got_worktree *worktree;
1471 struct got_repository *repo;
1472 got_worktree_checkout_cb progress_cb;
1473 void *progress_arg;
1474 got_cancel_cb cancel_cb;
1475 void *cancel_arg;
1478 static const struct got_error *
1479 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1480 struct got_tree_entry *te, const char *parent_path)
1482 struct diff_cb_arg *a = arg;
1484 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1485 return got_error(GOT_ERR_CANCELLED);
1487 return update_blob(a->worktree, a->fileindex, ie, te,
1488 ie->path, a->repo, a->progress_cb, a->progress_arg);
1491 static const struct got_error *
1492 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1494 struct diff_cb_arg *a = arg;
1496 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1497 return got_error(GOT_ERR_CANCELLED);
1499 return delete_blob(a->worktree, a->fileindex, ie,
1500 a->repo, a->progress_cb, a->progress_arg);
1503 static const struct got_error *
1504 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1506 struct diff_cb_arg *a = arg;
1507 const struct got_error *err;
1508 char *path;
1510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1511 return got_error(GOT_ERR_CANCELLED);
1513 if (got_object_tree_entry_is_submodule(te))
1514 return NULL;
1516 if (asprintf(&path, "%s%s%s", parent_path,
1517 parent_path[0] ? "/" : "", te->name)
1518 == -1)
1519 return got_error_from_errno("asprintf");
1521 if (S_ISDIR(te->mode))
1522 err = add_dir_on_disk(a->worktree, path);
1523 else
1524 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1525 a->repo, a->progress_cb, a->progress_arg);
1527 free(path);
1528 return err;
1531 static const struct got_error *
1532 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1534 const struct got_error *err = NULL;
1535 char *uuidstr = NULL;
1536 uint32_t uuid_status;
1538 *refname = NULL;
1540 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1541 if (uuid_status != uuid_s_ok)
1542 return got_error_uuid(uuid_status, "uuid_to_string");
1544 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1545 == -1) {
1546 err = got_error_from_errno("asprintf");
1547 *refname = NULL;
1549 free(uuidstr);
1550 return err;
1553 const struct got_error *
1554 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1556 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1559 static const struct got_error *
1560 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1562 return get_ref_name(refname, worktree,
1563 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1566 static const struct got_error *
1567 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1569 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1572 static const struct got_error *
1573 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1575 return get_ref_name(refname, worktree,
1576 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1579 static const struct got_error *
1580 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1582 return get_ref_name(refname, worktree,
1583 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1586 static const struct got_error *
1587 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1589 return get_ref_name(refname, worktree,
1590 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1593 static const struct got_error *
1594 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1596 return get_ref_name(refname, worktree,
1597 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1600 static const struct got_error *
1601 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1603 return get_ref_name(refname, worktree,
1604 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1607 static const struct got_error *
1608 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1610 return get_ref_name(refname, worktree,
1611 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1614 const struct got_error *
1615 got_worktree_get_histedit_script_path(char **path,
1616 struct got_worktree *worktree)
1618 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1619 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1620 *path = NULL;
1621 return got_error_from_errno("asprintf");
1623 return NULL;
1627 * Prevent Git's garbage collector from deleting our base commit by
1628 * setting a reference to our base commit's ID.
1630 static const struct got_error *
1631 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_reference *ref = NULL;
1635 char *refname;
1637 err = got_worktree_get_base_ref_name(&refname, worktree);
1638 if (err)
1639 return err;
1641 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1642 if (err)
1643 goto done;
1645 err = got_ref_write(ref, repo);
1646 done:
1647 free(refname);
1648 if (ref)
1649 got_ref_close(ref);
1650 return err;
1653 static const struct got_error *
1654 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1656 const struct got_error *err = NULL;
1658 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1659 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1660 err = got_error_from_errno("asprintf");
1661 *fileindex_path = NULL;
1663 return err;
1667 static const struct got_error *
1668 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1669 struct got_worktree *worktree)
1671 const struct got_error *err = NULL;
1672 FILE *index = NULL;
1674 *fileindex_path = NULL;
1675 *fileindex = got_fileindex_alloc();
1676 if (*fileindex == NULL)
1677 return got_error_from_errno("got_fileindex_alloc");
1679 err = get_fileindex_path(fileindex_path, worktree);
1680 if (err)
1681 goto done;
1683 index = fopen(*fileindex_path, "rb");
1684 if (index == NULL) {
1685 if (errno != ENOENT)
1686 err = got_error_from_errno2("fopen", *fileindex_path);
1687 } else {
1688 err = got_fileindex_read(*fileindex, index);
1689 if (fclose(index) != 0 && err == NULL)
1690 err = got_error_from_errno("fclose");
1692 done:
1693 if (err) {
1694 free(*fileindex_path);
1695 *fileindex_path = NULL;
1696 got_fileindex_free(*fileindex);
1697 *fileindex = NULL;
1699 return err;
1702 struct bump_base_commit_id_arg {
1703 struct got_object_id *base_commit_id;
1704 const char *path;
1705 size_t path_len;
1706 const char *entry_name;
1707 got_worktree_checkout_cb progress_cb;
1708 void *progress_arg;
1711 /* Bump base commit ID of all files within an updated part of the work tree. */
1712 static const struct got_error *
1713 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1715 const struct got_error *err;
1716 struct bump_base_commit_id_arg *a = arg;
1718 if (a->entry_name) {
1719 if (strcmp(ie->path, a->path) != 0)
1720 return NULL;
1721 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1722 return NULL;
1724 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1725 SHA1_DIGEST_LENGTH) == 0)
1726 return NULL;
1728 if (a->progress_cb) {
1729 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1730 ie->path);
1731 if (err)
1732 return err;
1734 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1735 return NULL;
1738 static const struct got_error *
1739 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1741 const struct got_error *err = NULL;
1742 char *new_fileindex_path = NULL;
1743 FILE *new_index = NULL;
1745 err = got_opentemp_named(&new_fileindex_path, &new_index,
1746 fileindex_path);
1747 if (err)
1748 goto done;
1750 err = got_fileindex_write(fileindex, new_index);
1751 if (err)
1752 goto done;
1754 if (rename(new_fileindex_path, fileindex_path) != 0) {
1755 err = got_error_from_errno3("rename", new_fileindex_path,
1756 fileindex_path);
1757 unlink(new_fileindex_path);
1759 done:
1760 if (new_index)
1761 fclose(new_index);
1762 free(new_fileindex_path);
1763 return err;
1766 static const struct got_error *
1767 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1768 struct got_object_id **tree_id, const char *wt_relpath,
1769 struct got_worktree *worktree, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_object_id *id = NULL;
1773 char *in_repo_path = NULL;
1774 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 *tree_relpath = NULL;
1778 *tree_id = NULL;
1780 if (wt_relpath[0] == '\0') {
1781 /* Check out all files within the work tree. */
1782 *entry_type = GOT_OBJ_TYPE_TREE;
1783 *tree_relpath = strdup("");
1784 if (*tree_relpath == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 err = got_object_id_by_path(tree_id, repo,
1789 worktree->base_commit_id, worktree->path_prefix);
1790 if (err)
1791 goto done;
1792 return NULL;
1795 /* Check out a subset of files in the work tree. */
1797 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1798 is_root_wt ? "" : "/", wt_relpath) == -1) {
1799 err = got_error_from_errno("asprintf");
1800 goto done;
1803 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1804 in_repo_path);
1805 if (err)
1806 goto done;
1808 free(in_repo_path);
1809 in_repo_path = NULL;
1811 err = got_object_get_type(entry_type, repo, id);
1812 if (err)
1813 goto done;
1815 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1816 /* Check out a single file. */
1817 if (strchr(wt_relpath, '/') == NULL) {
1818 /* Check out a single file in work tree's root dir. */
1819 in_repo_path = strdup(worktree->path_prefix);
1820 if (in_repo_path == NULL) {
1821 err = got_error_from_errno("strdup");
1822 goto done;
1824 *tree_relpath = strdup("");
1825 if (*tree_relpath == NULL) {
1826 err = got_error_from_errno("strdup");
1827 goto done;
1829 } else {
1830 /* Check out a single file in a subdirectory. */
1831 err = got_path_dirname(tree_relpath, wt_relpath);
1832 if (err)
1833 return err;
1834 if (asprintf(&in_repo_path, "%s%s%s",
1835 worktree->path_prefix, is_root_wt ? "" : "/",
1836 *tree_relpath) == -1) {
1837 err = got_error_from_errno("asprintf");
1838 goto done;
1841 err = got_object_id_by_path(tree_id, repo,
1842 worktree->base_commit_id, in_repo_path);
1843 } else {
1844 /* Check out all files within a subdirectory. */
1845 *tree_id = got_object_id_dup(id);
1846 if (*tree_id == NULL) {
1847 err = got_error_from_errno("got_object_id_dup");
1848 goto done;
1850 *tree_relpath = strdup(wt_relpath);
1851 if (*tree_relpath == NULL) {
1852 err = got_error_from_errno("strdup");
1853 goto done;
1856 done:
1857 free(id);
1858 free(in_repo_path);
1859 if (err) {
1860 *entry_type = GOT_OBJ_TYPE_ANY;
1861 free(*tree_relpath);
1862 *tree_relpath = NULL;
1863 free(*tree_id);
1864 *tree_id = NULL;
1866 return err;
1869 static const struct got_error *
1870 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1871 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1873 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1875 const struct got_error *err = NULL;
1876 struct got_commit_object *commit = NULL;
1877 struct got_tree_object *tree = NULL;
1878 struct got_fileindex_diff_tree_cb diff_cb;
1879 struct diff_cb_arg arg;
1881 err = ref_base_commit(worktree, repo);
1882 if (err)
1883 goto done;
1885 err = got_object_open_as_commit(&commit, repo,
1886 worktree->base_commit_id);
1887 if (err)
1888 goto done;
1890 err = got_object_open_as_tree(&tree, repo, tree_id);
1891 if (err)
1892 goto done;
1894 if (entry_name &&
1895 got_object_tree_find_entry(tree, entry_name) == NULL) {
1896 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1897 goto done;
1900 diff_cb.diff_old_new = diff_old_new;
1901 diff_cb.diff_old = diff_old;
1902 diff_cb.diff_new = diff_new;
1903 arg.fileindex = fileindex;
1904 arg.worktree = worktree;
1905 arg.repo = repo;
1906 arg.progress_cb = progress_cb;
1907 arg.progress_arg = progress_arg;
1908 arg.cancel_cb = cancel_cb;
1909 arg.cancel_arg = cancel_arg;
1910 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1911 entry_name, repo, &diff_cb, &arg);
1912 done:
1913 if (tree)
1914 got_object_tree_close(tree);
1915 if (commit)
1916 got_object_commit_close(commit);
1917 return err;
1920 const struct got_error *
1921 got_worktree_checkout_files(struct got_worktree *worktree,
1922 struct got_pathlist_head *paths, struct got_repository *repo,
1923 got_worktree_checkout_cb progress_cb, void *progress_arg,
1924 got_cancel_cb cancel_cb, void *cancel_arg)
1926 const struct got_error *err = NULL, *sync_err, *unlockerr;
1927 struct got_commit_object *commit = NULL;
1928 struct got_tree_object *tree = NULL;
1929 struct got_fileindex *fileindex = NULL;
1930 char *fileindex_path = NULL;
1931 struct got_pathlist_entry *pe;
1932 struct tree_path_data {
1933 SIMPLEQ_ENTRY(tree_path_data) entry;
1934 struct got_object_id *tree_id;
1935 int entry_type;
1936 char *relpath;
1937 char *entry_name;
1938 } *tpd = NULL;
1939 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1941 SIMPLEQ_INIT(&tree_paths);
1943 err = lock_worktree(worktree, LOCK_EX);
1944 if (err)
1945 return err;
1947 /* Map all specified paths to in-repository trees. */
1948 TAILQ_FOREACH(pe, paths, entry) {
1949 tpd = malloc(sizeof(*tpd));
1950 if (tpd == NULL) {
1951 err = got_error_from_errno("malloc");
1952 goto done;
1955 err = find_tree_entry_for_checkout(&tpd->entry_type,
1956 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1957 if (err) {
1958 free(tpd);
1959 goto done;
1962 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1963 err = got_path_basename(&tpd->entry_name, pe->path);
1964 if (err) {
1965 free(tpd->relpath);
1966 free(tpd->tree_id);
1967 free(tpd);
1968 goto done;
1970 } else
1971 tpd->entry_name = NULL;
1973 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1977 * Read the file index.
1978 * Checking out files is supposed to be an idempotent operation.
1979 * If the on-disk file index is incomplete we will try to complete it.
1981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1982 if (err)
1983 goto done;
1985 tpd = SIMPLEQ_FIRST(&tree_paths);
1986 TAILQ_FOREACH(pe, paths, entry) {
1987 struct bump_base_commit_id_arg bbc_arg;
1989 err = checkout_files(worktree, fileindex, tpd->relpath,
1990 tpd->tree_id, tpd->entry_name, repo,
1991 progress_cb, progress_arg, cancel_cb, cancel_arg);
1992 if (err)
1993 break;
1995 bbc_arg.base_commit_id = worktree->base_commit_id;
1996 bbc_arg.entry_name = tpd->entry_name;
1997 bbc_arg.path = pe->path;
1998 bbc_arg.path_len = pe->path_len;
1999 bbc_arg.progress_cb = progress_cb;
2000 bbc_arg.progress_arg = progress_arg;
2001 err = got_fileindex_for_each_entry_safe(fileindex,
2002 bump_base_commit_id, &bbc_arg);
2003 if (err)
2004 break;
2006 tpd = SIMPLEQ_NEXT(tpd, entry);
2008 sync_err = sync_fileindex(fileindex, fileindex_path);
2009 if (sync_err && err == NULL)
2010 err = sync_err;
2011 done:
2012 free(fileindex_path);
2013 if (tree)
2014 got_object_tree_close(tree);
2015 if (commit)
2016 got_object_commit_close(commit);
2017 if (fileindex)
2018 got_fileindex_free(fileindex);
2019 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2020 tpd = SIMPLEQ_FIRST(&tree_paths);
2021 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2022 free(tpd->relpath);
2023 free(tpd->tree_id);
2024 free(tpd);
2026 unlockerr = lock_worktree(worktree, LOCK_SH);
2027 if (unlockerr && err == NULL)
2028 err = unlockerr;
2029 return err;
2032 struct merge_file_cb_arg {
2033 struct got_worktree *worktree;
2034 struct got_fileindex *fileindex;
2035 got_worktree_checkout_cb progress_cb;
2036 void *progress_arg;
2037 got_cancel_cb cancel_cb;
2038 void *cancel_arg;
2039 const char *label_orig;
2040 struct got_object_id *commit_id2;
2043 static const struct got_error *
2044 merge_file_cb(void *arg, struct got_blob_object *blob1,
2045 struct got_blob_object *blob2, struct got_object_id *id1,
2046 struct got_object_id *id2, const char *path1, const char *path2,
2047 mode_t mode1, mode_t mode2, struct got_repository *repo)
2049 static const struct got_error *err = NULL;
2050 struct merge_file_cb_arg *a = arg;
2051 struct got_fileindex_entry *ie;
2052 char *ondisk_path = NULL;
2053 struct stat sb;
2054 unsigned char status;
2055 int local_changes_subsumed;
2057 if (blob1 && blob2) {
2058 ie = got_fileindex_entry_get(a->fileindex, path2,
2059 strlen(path2));
2060 if (ie == NULL)
2061 return (*a->progress_cb)(a->progress_arg,
2062 GOT_STATUS_MISSING, path2);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2065 path2) == -1)
2066 return got_error_from_errno("asprintf");
2068 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2069 repo);
2070 if (err)
2071 goto done;
2073 if (status == GOT_STATUS_DELETE) {
2074 err = (*a->progress_cb)(a->progress_arg,
2075 GOT_STATUS_MERGE, path2);
2076 goto done;
2078 if (status != GOT_STATUS_NO_CHANGE &&
2079 status != GOT_STATUS_MODIFY &&
2080 status != GOT_STATUS_CONFLICT &&
2081 status != GOT_STATUS_ADD) {
2082 err = (*a->progress_cb)(a->progress_arg, status, path2);
2083 goto done;
2086 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2087 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2088 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2089 } else if (blob1) {
2090 ie = got_fileindex_entry_get(a->fileindex, path1,
2091 strlen(path1));
2092 if (ie == NULL)
2093 return (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MISSING, path2);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2097 path1) == -1)
2098 return got_error_from_errno("asprintf");
2100 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2101 repo);
2102 if (err)
2103 goto done;
2105 switch (status) {
2106 case GOT_STATUS_NO_CHANGE:
2107 err = (*a->progress_cb)(a->progress_arg,
2108 GOT_STATUS_DELETE, path1);
2109 if (err)
2110 goto done;
2111 err = remove_ondisk_file(a->worktree->root_path, path1);
2112 if (err)
2113 goto done;
2114 if (ie)
2115 got_fileindex_entry_mark_deleted_from_disk(ie);
2116 break;
2117 case GOT_STATUS_DELETE:
2118 case GOT_STATUS_MISSING:
2119 err = (*a->progress_cb)(a->progress_arg,
2120 GOT_STATUS_DELETE, path1);
2121 if (err)
2122 goto done;
2123 if (ie)
2124 got_fileindex_entry_mark_deleted_from_disk(ie);
2125 break;
2126 case GOT_STATUS_ADD:
2127 case GOT_STATUS_MODIFY:
2128 case GOT_STATUS_CONFLICT:
2129 err = (*a->progress_cb)(a->progress_arg,
2130 GOT_STATUS_CANNOT_DELETE, path1);
2131 if (err)
2132 goto done;
2133 break;
2134 case GOT_STATUS_OBSTRUCTED:
2135 err = (*a->progress_cb)(a->progress_arg, status, path1);
2136 if (err)
2137 goto done;
2138 break;
2139 default:
2140 break;
2142 } else if (blob2) {
2143 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2144 path2) == -1)
2145 return got_error_from_errno("asprintf");
2146 ie = got_fileindex_entry_get(a->fileindex, path2,
2147 strlen(path2));
2148 if (ie) {
2149 err = get_file_status(&status, &sb, ie, ondisk_path,
2150 -1, NULL, repo);
2151 if (err)
2152 goto done;
2153 if (status != GOT_STATUS_NO_CHANGE &&
2154 status != GOT_STATUS_MODIFY &&
2155 status != GOT_STATUS_CONFLICT &&
2156 status != GOT_STATUS_ADD) {
2157 err = (*a->progress_cb)(a->progress_arg,
2158 status, path2);
2159 goto done;
2161 err = merge_blob(&local_changes_subsumed, a->worktree,
2162 NULL, ondisk_path, path2, sb.st_mode,
2163 a->label_orig, blob2, a->commit_id2, repo,
2164 a->progress_cb,
2165 a->progress_arg);
2166 if (status == GOT_STATUS_DELETE) {
2167 err = update_blob_fileindex_entry(a->worktree,
2168 a->fileindex, ie, ondisk_path, ie->path,
2169 blob2, 0);
2170 if (err)
2171 goto done;
2173 } else {
2174 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2175 err = install_blob(a->worktree, ondisk_path, path2,
2176 /* XXX get this from parent tree! */
2177 GOT_DEFAULT_FILE_MODE,
2178 sb.st_mode, blob2, 0, 0, repo,
2179 a->progress_cb, a->progress_arg);
2180 if (err)
2181 goto done;
2182 err = got_fileindex_entry_alloc(&ie,
2183 ondisk_path, path2, NULL, NULL);
2184 if (err)
2185 goto done;
2186 err = got_fileindex_entry_add(a->fileindex, ie);
2187 if (err) {
2188 got_fileindex_entry_free(ie);
2189 goto done;
2193 done:
2194 free(ondisk_path);
2195 return err;
2198 struct check_merge_ok_arg {
2199 struct got_worktree *worktree;
2200 struct got_repository *repo;
2203 static const struct got_error *
2204 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2206 const struct got_error *err = NULL;
2207 struct check_merge_ok_arg *a = arg;
2208 unsigned char status;
2209 struct stat sb;
2210 char *ondisk_path;
2212 /* Reject merges into a work tree with mixed base commits. */
2213 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2214 SHA1_DIGEST_LENGTH))
2215 return got_error(GOT_ERR_MIXED_COMMITS);
2217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2218 == -1)
2219 return got_error_from_errno("asprintf");
2221 /* Reject merges into a work tree with conflicted files. */
2222 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2223 if (err)
2224 return err;
2225 if (status == GOT_STATUS_CONFLICT)
2226 return got_error(GOT_ERR_CONFLICTS);
2228 return NULL;
2231 static const struct got_error *
2232 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2233 const char *fileindex_path, struct got_object_id *commit_id1,
2234 struct got_object_id *commit_id2, struct got_repository *repo,
2235 got_worktree_checkout_cb progress_cb, void *progress_arg,
2236 got_cancel_cb cancel_cb, void *cancel_arg)
2238 const struct got_error *err = NULL, *sync_err;
2239 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2240 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2241 struct merge_file_cb_arg arg;
2242 char *label_orig = NULL;
2244 if (commit_id1) {
2245 char *id_str;
2247 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2248 worktree->path_prefix);
2249 if (err)
2250 goto done;
2252 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2253 if (err)
2254 goto done;
2256 err = got_object_id_str(&id_str, commit_id1);
2257 if (err)
2258 goto done;
2260 if (asprintf(&label_orig, "%s: commit %s",
2261 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2262 err = got_error_from_errno("asprintf");
2263 free(id_str);
2264 goto done;
2266 free(id_str);
2269 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2270 worktree->path_prefix);
2271 if (err)
2272 goto done;
2274 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2275 if (err)
2276 goto done;
2278 arg.worktree = worktree;
2279 arg.fileindex = fileindex;
2280 arg.progress_cb = progress_cb;
2281 arg.progress_arg = progress_arg;
2282 arg.cancel_cb = cancel_cb;
2283 arg.cancel_arg = cancel_arg;
2284 arg.label_orig = label_orig;
2285 arg.commit_id2 = commit_id2;
2286 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2287 sync_err = sync_fileindex(fileindex, fileindex_path);
2288 if (sync_err && err == NULL)
2289 err = sync_err;
2290 done:
2291 if (tree1)
2292 got_object_tree_close(tree1);
2293 if (tree2)
2294 got_object_tree_close(tree2);
2295 free(label_orig);
2296 return err;
2299 const struct got_error *
2300 got_worktree_merge_files(struct got_worktree *worktree,
2301 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2302 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2303 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2305 const struct got_error *err, *unlockerr;
2306 char *fileindex_path = NULL;
2307 struct got_fileindex *fileindex = NULL;
2308 struct check_merge_ok_arg mok_arg;
2310 err = lock_worktree(worktree, LOCK_EX);
2311 if (err)
2312 return err;
2314 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2315 if (err)
2316 goto done;
2318 mok_arg.worktree = worktree;
2319 mok_arg.repo = repo;
2320 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2321 &mok_arg);
2322 if (err)
2323 goto done;
2325 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2326 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2327 done:
2328 if (fileindex)
2329 got_fileindex_free(fileindex);
2330 free(fileindex_path);
2331 unlockerr = lock_worktree(worktree, LOCK_SH);
2332 if (unlockerr && err == NULL)
2333 err = unlockerr;
2334 return err;
2337 struct diff_dir_cb_arg {
2338 struct got_fileindex *fileindex;
2339 struct got_worktree *worktree;
2340 const char *status_path;
2341 size_t status_path_len;
2342 struct got_repository *repo;
2343 got_worktree_status_cb status_cb;
2344 void *status_arg;
2345 got_cancel_cb cancel_cb;
2346 void *cancel_arg;
2347 /* A pathlist containing per-directory pathlists of ignore patterns. */
2348 struct got_pathlist_head ignores;
2349 int report_unchanged;
2352 static const struct got_error *
2353 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2354 int dirfd, const char *de_name,
2355 got_worktree_status_cb status_cb, void *status_arg,
2356 struct got_repository *repo, int report_unchanged)
2358 const struct got_error *err = NULL;
2359 unsigned char status = GOT_STATUS_NO_CHANGE;
2360 unsigned char staged_status = get_staged_status(ie);
2361 struct stat sb;
2362 struct got_object_id blob_id, commit_id, staged_blob_id;
2363 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2364 struct got_object_id *staged_blob_idp = NULL;
2366 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2367 if (err)
2368 return err;
2370 if (status == GOT_STATUS_NO_CHANGE &&
2371 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2372 return NULL;
2374 if (got_fileindex_entry_has_blob(ie)) {
2375 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2376 blob_idp = &blob_id;
2378 if (got_fileindex_entry_has_commit(ie)) {
2379 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2380 commit_idp = &commit_id;
2382 if (staged_status == GOT_STATUS_ADD ||
2383 staged_status == GOT_STATUS_MODIFY) {
2384 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2385 SHA1_DIGEST_LENGTH);
2386 staged_blob_idp = &staged_blob_id;
2389 return (*status_cb)(status_arg, status, staged_status,
2390 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2393 static const struct got_error *
2394 status_old_new(void *arg, struct got_fileindex_entry *ie,
2395 struct dirent *de, const char *parent_path, int dirfd)
2397 const struct got_error *err = NULL;
2398 struct diff_dir_cb_arg *a = arg;
2399 char *abspath;
2401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2402 return got_error(GOT_ERR_CANCELLED);
2404 if (got_path_cmp(parent_path, a->status_path,
2405 strlen(parent_path), a->status_path_len) != 0 &&
2406 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2407 return NULL;
2409 if (parent_path[0]) {
2410 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2411 parent_path, de->d_name) == -1)
2412 return got_error_from_errno("asprintf");
2413 } else {
2414 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2415 de->d_name) == -1)
2416 return got_error_from_errno("asprintf");
2419 err = report_file_status(ie, abspath, dirfd, de->d_name,
2420 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2421 free(abspath);
2422 return err;
2425 static const struct got_error *
2426 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2428 struct diff_dir_cb_arg *a = arg;
2429 struct got_object_id blob_id, commit_id;
2430 unsigned char status;
2432 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2433 return got_error(GOT_ERR_CANCELLED);
2435 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2436 return NULL;
2438 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2439 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2440 if (got_fileindex_entry_has_file_on_disk(ie))
2441 status = GOT_STATUS_MISSING;
2442 else
2443 status = GOT_STATUS_DELETE;
2444 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2445 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2448 void
2449 free_ignorelist(struct got_pathlist_head *ignorelist)
2451 struct got_pathlist_entry *pe;
2453 TAILQ_FOREACH(pe, ignorelist, entry)
2454 free((char *)pe->path);
2455 got_pathlist_free(ignorelist);
2458 void
2459 free_ignores(struct got_pathlist_head *ignores)
2461 struct got_pathlist_entry *pe;
2463 TAILQ_FOREACH(pe, ignores, entry) {
2464 struct got_pathlist_head *ignorelist = pe->data;
2465 free_ignorelist(ignorelist);
2466 free((char *)pe->path);
2468 got_pathlist_free(ignores);
2471 static const struct got_error *
2472 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2474 const struct got_error *err = NULL;
2475 struct got_pathlist_entry *pe = NULL;
2476 struct got_pathlist_head *ignorelist;
2477 char *line = NULL, *pattern, *dirpath = NULL;
2478 size_t linesize = 0;
2479 ssize_t linelen;
2481 ignorelist = calloc(1, sizeof(*ignorelist));
2482 if (ignorelist == NULL)
2483 return got_error_from_errno("calloc");
2484 TAILQ_INIT(ignorelist);
2486 while ((linelen = getline(&line, &linesize, f)) != -1) {
2487 if (linelen > 0 && line[linelen - 1] == '\n')
2488 line[linelen - 1] = '\0';
2490 /* Git's ignores may contain comments. */
2491 if (line[0] == '#')
2492 continue;
2494 /* Git's negated patterns are not (yet?) supported. */
2495 if (line[0] == '!')
2496 continue;
2498 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2499 line) == -1) {
2500 err = got_error_from_errno("asprintf");
2501 goto done;
2503 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2504 if (err)
2505 goto done;
2507 if (ferror(f)) {
2508 err = got_error_from_errno("getline");
2509 goto done;
2512 dirpath = strdup(path);
2513 if (dirpath == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2518 done:
2519 free(line);
2520 if (err || pe == NULL) {
2521 free(dirpath);
2522 free_ignorelist(ignorelist);
2524 return err;
2527 int
2528 match_ignores(struct got_pathlist_head *ignores, const char *path)
2530 struct got_pathlist_entry *pe;
2532 /* Handle patterns which match in all directories. */
2533 TAILQ_FOREACH(pe, ignores, entry) {
2534 struct got_pathlist_head *ignorelist = pe->data;
2535 struct got_pathlist_entry *pi;
2537 TAILQ_FOREACH(pi, ignorelist, entry) {
2538 const char *p, *pattern = pi->path;
2540 if (strncmp(pattern, "**/", 3) != 0)
2541 continue;
2542 pattern += 3;
2543 p = path;
2544 while (*p) {
2545 if (fnmatch(pattern, p,
2546 FNM_PATHNAME | FNM_LEADING_DIR)) {
2547 /* Retry in next directory. */
2548 while (*p && *p != '/')
2549 p++;
2550 while (*p == '/')
2551 p++;
2552 continue;
2554 return 1;
2560 * The ignores pathlist contains ignore lists from children before
2561 * parents, so we can find the most specific ignorelist by walking
2562 * ignores backwards.
2564 pe = TAILQ_LAST(ignores, got_pathlist_head);
2565 while (pe) {
2566 if (got_path_is_child(path, pe->path, pe->path_len)) {
2567 struct got_pathlist_head *ignorelist = pe->data;
2568 struct got_pathlist_entry *pi;
2569 TAILQ_FOREACH(pi, ignorelist, entry) {
2570 const char *pattern = pi->path;
2571 int flags = FNM_LEADING_DIR;
2572 if (strstr(pattern, "/**/") == NULL)
2573 flags |= FNM_PATHNAME;
2574 if (fnmatch(pattern, path, flags))
2575 continue;
2576 return 1;
2579 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2582 return 0;
2585 static const struct got_error *
2586 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2587 const char *path, const char *ignores_filename)
2589 const struct got_error *err = NULL;
2590 char *ignorespath;
2591 FILE *ignoresfile = NULL;
2593 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2594 path[0] ? "/" : "", ignores_filename) == -1)
2595 return got_error_from_errno("asprintf");
2597 ignoresfile = fopen(ignorespath, "r");
2598 if (ignoresfile == NULL) {
2599 if (errno != ENOENT && errno != EACCES)
2600 err = got_error_from_errno2("fopen",
2601 ignorespath);
2602 } else
2603 err = read_ignores(ignores, path, ignoresfile);
2605 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2606 err = got_error_from_errno2("fclose", path);
2607 free(ignorespath);
2608 return err;
2611 static const struct got_error *
2612 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2614 const struct got_error *err = NULL;
2615 struct diff_dir_cb_arg *a = arg;
2616 char *path = NULL;
2618 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2619 return got_error(GOT_ERR_CANCELLED);
2621 /* XXX ignore symlinks for now */
2622 if (de->d_type == DT_LNK)
2623 return NULL;
2625 if (parent_path[0]) {
2626 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2627 return got_error_from_errno("asprintf");
2628 } else {
2629 path = de->d_name;
2632 if (de->d_type == DT_DIR) {
2633 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2634 ".cvsignore");
2635 if (err == NULL)
2636 err = add_ignores(&a->ignores, a->worktree->root_path,
2637 path, ".gitignore");
2639 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2640 && !match_ignores(&a->ignores, path))
2641 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2642 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2643 if (parent_path[0])
2644 free(path);
2645 return err;
2648 static const struct got_error *
2649 report_single_file_status(const char *path, const char *ondisk_path,
2650 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2651 void *status_arg, struct got_repository *repo, int report_unchanged)
2653 struct got_fileindex_entry *ie;
2654 struct stat sb;
2656 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2657 if (ie)
2658 return report_file_status(ie, ondisk_path, -1, NULL,
2659 status_cb, status_arg, repo, report_unchanged);
2661 if (lstat(ondisk_path, &sb) == -1) {
2662 if (errno != ENOENT)
2663 return got_error_from_errno2("lstat", ondisk_path);
2664 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2665 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2666 return NULL;
2669 if (S_ISREG(sb.st_mode))
2670 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2671 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2673 return NULL;
2676 static const struct got_error *
2677 worktree_status(struct got_worktree *worktree, const char *path,
2678 struct got_fileindex *fileindex, struct got_repository *repo,
2679 got_worktree_status_cb status_cb, void *status_arg,
2680 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2681 int report_unchanged)
2683 const struct got_error *err = NULL;
2684 int fd = -1;
2685 struct got_fileindex_diff_dir_cb fdiff_cb;
2686 struct diff_dir_cb_arg arg;
2687 char *ondisk_path = NULL;
2689 if (asprintf(&ondisk_path, "%s%s%s",
2690 worktree->root_path, path[0] ? "/" : "", path) == -1)
2691 return got_error_from_errno("asprintf");
2693 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2694 if (fd == -1) {
2695 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2696 err = got_error_from_errno2("open", ondisk_path);
2697 else
2698 err = report_single_file_status(path, ondisk_path,
2699 fileindex, status_cb, status_arg, repo,
2700 report_unchanged);
2701 } else {
2702 fdiff_cb.diff_old_new = status_old_new;
2703 fdiff_cb.diff_old = status_old;
2704 fdiff_cb.diff_new = status_new;
2705 arg.fileindex = fileindex;
2706 arg.worktree = worktree;
2707 arg.status_path = path;
2708 arg.status_path_len = strlen(path);
2709 arg.repo = repo;
2710 arg.status_cb = status_cb;
2711 arg.status_arg = status_arg;
2712 arg.cancel_cb = cancel_cb;
2713 arg.cancel_arg = cancel_arg;
2714 arg.report_unchanged = report_unchanged;
2715 TAILQ_INIT(&arg.ignores);
2716 if (!no_ignores) {
2717 err = add_ignores(&arg.ignores, worktree->root_path,
2718 path, ".cvsignore");
2719 if (err == NULL)
2720 err = add_ignores(&arg.ignores,
2721 worktree->root_path, path, ".gitignore");
2723 if (err == NULL)
2724 err = got_fileindex_diff_dir(fileindex, fd,
2725 worktree->root_path, path, repo, &fdiff_cb, &arg);
2726 free_ignores(&arg.ignores);
2729 if (fd != -1 && close(fd) != 0 && err == NULL)
2730 err = got_error_from_errno("close");
2731 free(ondisk_path);
2732 return err;
2735 const struct got_error *
2736 got_worktree_status(struct got_worktree *worktree,
2737 struct got_pathlist_head *paths, struct got_repository *repo,
2738 got_worktree_status_cb status_cb, void *status_arg,
2739 got_cancel_cb cancel_cb, void *cancel_arg)
2741 const struct got_error *err = NULL;
2742 char *fileindex_path = NULL;
2743 struct got_fileindex *fileindex = NULL;
2744 struct got_pathlist_entry *pe;
2746 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2747 if (err)
2748 return err;
2750 TAILQ_FOREACH(pe, paths, entry) {
2751 err = worktree_status(worktree, pe->path, fileindex, repo,
2752 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2753 if (err)
2754 break;
2756 free(fileindex_path);
2757 got_fileindex_free(fileindex);
2758 return err;
2761 const struct got_error *
2762 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2763 const char *arg)
2765 const struct got_error *err = NULL;
2766 char *resolved, *cwd = NULL, *path = NULL;
2767 size_t len;
2769 *wt_path = NULL;
2771 resolved = realpath(arg, NULL);
2772 if (resolved == NULL) {
2773 if (errno != ENOENT)
2774 return got_error_from_errno2("realpath", arg);
2775 cwd = getcwd(NULL, 0);
2776 if (cwd == NULL)
2777 return got_error_from_errno("getcwd");
2778 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2779 err = got_error_from_errno("asprintf");
2780 goto done;
2784 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2785 strlen(got_worktree_get_root_path(worktree)))) {
2786 err = got_error(GOT_ERR_BAD_PATH);
2787 goto done;
2790 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2791 err = got_path_skip_common_ancestor(&path,
2792 got_worktree_get_root_path(worktree), resolved);
2793 if (err)
2794 goto done;
2795 } else {
2796 path = strdup("");
2797 if (path == NULL) {
2798 err = got_error_from_errno("strdup");
2799 goto done;
2803 /* XXX status walk can't deal with trailing slash! */
2804 len = strlen(path);
2805 while (len > 0 && path[len - 1] == '/') {
2806 path[len - 1] = '\0';
2807 len--;
2809 done:
2810 free(resolved);
2811 free(cwd);
2812 if (err == NULL)
2813 *wt_path = path;
2814 else
2815 free(path);
2816 return err;
2819 struct schedule_addition_args {
2820 struct got_worktree *worktree;
2821 struct got_fileindex *fileindex;
2822 got_worktree_checkout_cb progress_cb;
2823 void *progress_arg;
2824 struct got_repository *repo;
2827 static const struct got_error *
2828 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2829 const char *relpath, struct got_object_id *blob_id,
2830 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2831 int dirfd, const char *de_name)
2833 struct schedule_addition_args *a = arg;
2834 const struct got_error *err = NULL;
2835 struct got_fileindex_entry *ie;
2836 struct stat sb;
2837 char *ondisk_path;
2839 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2840 relpath) == -1)
2841 return got_error_from_errno("asprintf");
2843 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2844 if (ie) {
2845 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2846 de_name, a->repo);
2847 if (err)
2848 goto done;
2849 /* Re-adding an existing entry is a no-op. */
2850 if (status == GOT_STATUS_ADD)
2851 goto done;
2852 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2853 if (err)
2854 goto done;
2857 if (status != GOT_STATUS_UNVERSIONED) {
2858 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2859 goto done;
2862 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2863 if (err)
2864 goto done;
2866 err = got_fileindex_entry_add(a->fileindex, ie);
2867 if (err) {
2868 got_fileindex_entry_free(ie);
2869 goto done;
2871 done:
2872 free(ondisk_path);
2873 if (err)
2874 return err;
2875 if (status == GOT_STATUS_ADD)
2876 return NULL;
2877 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2880 const struct got_error *
2881 got_worktree_schedule_add(struct got_worktree *worktree,
2882 struct got_pathlist_head *paths,
2883 got_worktree_checkout_cb progress_cb, void *progress_arg,
2884 struct got_repository *repo, int no_ignores)
2886 struct got_fileindex *fileindex = NULL;
2887 char *fileindex_path = NULL;
2888 const struct got_error *err = NULL, *sync_err, *unlockerr;
2889 struct got_pathlist_entry *pe;
2890 struct schedule_addition_args saa;
2892 err = lock_worktree(worktree, LOCK_EX);
2893 if (err)
2894 return err;
2896 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2897 if (err)
2898 goto done;
2900 saa.worktree = worktree;
2901 saa.fileindex = fileindex;
2902 saa.progress_cb = progress_cb;
2903 saa.progress_arg = progress_arg;
2904 saa.repo = repo;
2906 TAILQ_FOREACH(pe, paths, entry) {
2907 err = worktree_status(worktree, pe->path, fileindex, repo,
2908 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2909 if (err)
2910 break;
2912 sync_err = sync_fileindex(fileindex, fileindex_path);
2913 if (sync_err && err == NULL)
2914 err = sync_err;
2915 done:
2916 free(fileindex_path);
2917 if (fileindex)
2918 got_fileindex_free(fileindex);
2919 unlockerr = lock_worktree(worktree, LOCK_SH);
2920 if (unlockerr && err == NULL)
2921 err = unlockerr;
2922 return err;
2925 struct schedule_deletion_args {
2926 struct got_worktree *worktree;
2927 struct got_fileindex *fileindex;
2928 got_worktree_delete_cb progress_cb;
2929 void *progress_arg;
2930 struct got_repository *repo;
2931 int delete_local_mods;
2932 int keep_on_disk;
2935 static const struct got_error *
2936 schedule_for_deletion(void *arg, unsigned char status,
2937 unsigned char staged_status, const char *relpath,
2938 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2939 struct got_object_id *commit_id, int dirfd, const char *de_name)
2941 struct schedule_deletion_args *a = arg;
2942 const struct got_error *err = NULL;
2943 struct got_fileindex_entry *ie = NULL;
2944 struct stat sb;
2945 char *ondisk_path;
2947 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2948 if (ie == NULL)
2949 return got_error(GOT_ERR_BAD_PATH);
2951 staged_status = get_staged_status(ie);
2952 if (staged_status != GOT_STATUS_NO_CHANGE) {
2953 if (staged_status == GOT_STATUS_DELETE)
2954 return NULL;
2955 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2958 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2959 relpath) == -1)
2960 return got_error_from_errno("asprintf");
2962 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2963 a->repo);
2964 if (err)
2965 goto done;
2967 if (status != GOT_STATUS_NO_CHANGE) {
2968 if (status == GOT_STATUS_DELETE)
2969 goto done;
2970 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2971 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2972 goto done;
2974 if (status != GOT_STATUS_MODIFY &&
2975 status != GOT_STATUS_MISSING) {
2976 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2977 goto done;
2981 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
2982 if (dirfd != -1) {
2983 if (unlinkat(dirfd, de_name, 0) != 0) {
2984 err = got_error_from_errno2("unlinkat",
2985 ondisk_path);
2986 goto done;
2988 } else if (unlink(ondisk_path) != 0) {
2989 err = got_error_from_errno2("unlink", ondisk_path);
2990 goto done;
2994 got_fileindex_entry_mark_deleted_from_disk(ie);
2995 done:
2996 free(ondisk_path);
2997 if (err)
2998 return err;
2999 if (status == GOT_STATUS_DELETE)
3000 return NULL;
3001 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3002 staged_status, relpath);
3005 const struct got_error *
3006 got_worktree_schedule_delete(struct got_worktree *worktree,
3007 struct got_pathlist_head *paths, int delete_local_mods,
3008 got_worktree_delete_cb progress_cb, void *progress_arg,
3009 struct got_repository *repo, int keep_on_disk)
3011 struct got_fileindex *fileindex = NULL;
3012 char *fileindex_path = NULL;
3013 const struct got_error *err = NULL, *sync_err, *unlockerr;
3014 struct got_pathlist_entry *pe;
3015 struct schedule_deletion_args sda;
3017 err = lock_worktree(worktree, LOCK_EX);
3018 if (err)
3019 return err;
3021 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3022 if (err)
3023 goto done;
3025 sda.worktree = worktree;
3026 sda.fileindex = fileindex;
3027 sda.progress_cb = progress_cb;
3028 sda.progress_arg = progress_arg;
3029 sda.repo = repo;
3030 sda.delete_local_mods = delete_local_mods;
3031 sda.keep_on_disk = keep_on_disk;
3033 TAILQ_FOREACH(pe, paths, entry) {
3034 err = worktree_status(worktree, pe->path, fileindex, repo,
3035 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3036 if (err)
3037 break;
3039 sync_err = sync_fileindex(fileindex, fileindex_path);
3040 if (sync_err && err == NULL)
3041 err = sync_err;
3042 done:
3043 free(fileindex_path);
3044 if (fileindex)
3045 got_fileindex_free(fileindex);
3046 unlockerr = lock_worktree(worktree, LOCK_SH);
3047 if (unlockerr && err == NULL)
3048 err = unlockerr;
3049 return err;
3052 static const struct got_error *
3053 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3055 const struct got_error *err = NULL;
3056 char *line = NULL;
3057 size_t linesize = 0, n;
3058 ssize_t linelen;
3060 linelen = getline(&line, &linesize, infile);
3061 if (linelen == -1) {
3062 if (ferror(infile)) {
3063 err = got_error_from_errno("getline");
3064 goto done;
3066 return NULL;
3068 if (outfile) {
3069 n = fwrite(line, 1, linelen, outfile);
3070 if (n != linelen) {
3071 err = got_ferror(outfile, GOT_ERR_IO);
3072 goto done;
3075 if (rejectfile) {
3076 n = fwrite(line, 1, linelen, rejectfile);
3077 if (n != linelen)
3078 err = got_ferror(outfile, GOT_ERR_IO);
3080 done:
3081 free(line);
3082 return err;
3085 static const struct got_error *
3086 skip_one_line(FILE *f)
3088 char *line = NULL;
3089 size_t linesize = 0;
3090 ssize_t linelen;
3092 linelen = getline(&line, &linesize, f);
3093 if (linelen == -1) {
3094 if (ferror(f))
3095 return got_error_from_errno("getline");
3096 return NULL;
3098 free(line);
3099 return NULL;
3102 static const struct got_error *
3103 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3104 int start_old, int end_old, int start_new, int end_new,
3105 FILE *outfile, FILE *rejectfile)
3107 const struct got_error *err;
3109 /* Copy old file's lines leading up to patch. */
3110 while (!feof(f1) && *line_cur1 < start_old) {
3111 err = copy_one_line(f1, outfile, NULL);
3112 if (err)
3113 return err;
3114 (*line_cur1)++;
3116 /* Skip new file's lines leading up to patch. */
3117 while (!feof(f2) && *line_cur2 < start_new) {
3118 if (rejectfile)
3119 err = copy_one_line(f2, NULL, rejectfile);
3120 else
3121 err = skip_one_line(f2);
3122 if (err)
3123 return err;
3124 (*line_cur2)++;
3126 /* Copy patched lines. */
3127 while (!feof(f2) && *line_cur2 <= end_new) {
3128 err = copy_one_line(f2, outfile, NULL);
3129 if (err)
3130 return err;
3131 (*line_cur2)++;
3133 /* Skip over old file's replaced lines. */
3134 while (!feof(f1) && *line_cur1 <= end_old) {
3135 if (rejectfile)
3136 err = copy_one_line(f1, NULL, rejectfile);
3137 else
3138 err = skip_one_line(f1);
3139 if (err)
3140 return err;
3141 (*line_cur1)++;
3144 return NULL;
3147 static const struct got_error *
3148 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3149 FILE *outfile, FILE *rejectfile)
3151 const struct got_error *err;
3153 if (outfile) {
3154 /* Copy old file's lines until EOF. */
3155 while (!feof(f1)) {
3156 err = copy_one_line(f1, outfile, NULL);
3157 if (err)
3158 return err;
3159 (*line_cur1)++;
3162 if (rejectfile) {
3163 /* Copy new file's lines until EOF. */
3164 while (!feof(f2)) {
3165 err = copy_one_line(f2, NULL, rejectfile);
3166 if (err)
3167 return err;
3168 (*line_cur2)++;
3172 return NULL;
3175 static const struct got_error *
3176 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3177 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3178 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3179 int *line_cur2, FILE *outfile, FILE *rejectfile,
3180 got_worktree_patch_cb patch_cb, void *patch_arg)
3182 const struct got_error *err = NULL;
3183 int start_old = change->cv.a;
3184 int end_old = change->cv.b;
3185 int start_new = change->cv.c;
3186 int end_new = change->cv.d;
3187 long pos1, pos2;
3188 FILE *hunkfile;
3190 *choice = GOT_PATCH_CHOICE_NONE;
3192 hunkfile = got_opentemp();
3193 if (hunkfile == NULL)
3194 return got_error_from_errno("got_opentemp");
3196 pos1 = ftell(f1);
3197 pos2 = ftell(f2);
3199 /* XXX TODO needs error checking */
3200 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3202 if (fseek(f1, pos1, SEEK_SET) == -1) {
3203 err = got_ferror(f1, GOT_ERR_IO);
3204 goto done;
3206 if (fseek(f2, pos2, SEEK_SET) == -1) {
3207 err = got_ferror(f1, GOT_ERR_IO);
3208 goto done;
3210 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3211 err = got_ferror(hunkfile, GOT_ERR_IO);
3212 goto done;
3215 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3216 hunkfile, n, nchanges);
3217 if (err)
3218 goto done;
3220 switch (*choice) {
3221 case GOT_PATCH_CHOICE_YES:
3222 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3223 end_old, start_new, end_new, outfile, rejectfile);
3224 break;
3225 case GOT_PATCH_CHOICE_NO:
3226 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3227 end_old, start_new, end_new, rejectfile, outfile);
3228 break;
3229 case GOT_PATCH_CHOICE_QUIT:
3230 break;
3231 default:
3232 err = got_error(GOT_ERR_PATCH_CHOICE);
3233 break;
3235 done:
3236 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3237 err = got_error_from_errno("fclose");
3238 return err;
3241 struct revert_file_args {
3242 struct got_worktree *worktree;
3243 struct got_fileindex *fileindex;
3244 got_worktree_checkout_cb progress_cb;
3245 void *progress_arg;
3246 got_worktree_patch_cb patch_cb;
3247 void *patch_arg;
3248 struct got_repository *repo;
3251 static const struct got_error *
3252 create_patched_content(char **path_outfile, int reverse_patch,
3253 struct got_object_id *blob_id, const char *path2,
3254 int dirfd2, const char *de_name2,
3255 const char *relpath, struct got_repository *repo,
3256 got_worktree_patch_cb patch_cb, void *patch_arg)
3258 const struct got_error *err;
3259 struct got_blob_object *blob = NULL;
3260 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3261 int fd2 = -1;
3262 char *path1 = NULL, *id_str = NULL;
3263 struct stat sb1, sb2;
3264 struct got_diff_changes *changes = NULL;
3265 struct got_diff_state *ds = NULL;
3266 struct got_diff_args *args = NULL;
3267 struct got_diff_change *change;
3268 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3269 int n = 0;
3271 *path_outfile = NULL;
3273 err = got_object_id_str(&id_str, blob_id);
3274 if (err)
3275 return err;
3277 if (dirfd2 != -1) {
3278 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3279 if (fd2 == -1) {
3280 err = got_error_from_errno2("openat", path2);
3281 goto done;
3283 } else {
3284 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3285 if (fd2 == -1) {
3286 err = got_error_from_errno2("open", path2);
3287 goto done;
3290 if (fstat(fd2, &sb2) == -1) {
3291 err = got_error_from_errno2("fstat", path2);
3292 goto done;
3295 f2 = fdopen(fd2, "r");
3296 if (f2 == NULL) {
3297 err = got_error_from_errno2("fopen", path2);
3298 goto done;
3300 fd2 = -1;
3302 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3303 if (err)
3304 goto done;
3306 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3307 if (err)
3308 goto done;
3310 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3311 if (err)
3312 goto done;
3314 if (stat(path1, &sb1) == -1) {
3315 err = got_error_from_errno2("stat", path1);
3316 goto done;
3319 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3320 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3321 if (err)
3322 goto done;
3324 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3325 if (err)
3326 goto done;
3328 if (fseek(f1, 0L, SEEK_SET) == -1)
3329 return got_ferror(f1, GOT_ERR_IO);
3330 if (fseek(f2, 0L, SEEK_SET) == -1)
3331 return got_ferror(f2, GOT_ERR_IO);
3332 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3333 int choice;
3334 err = apply_or_reject_change(&choice, change, ++n,
3335 changes->nchanges, ds, args, diff_flags, relpath,
3336 f1, f2, &line_cur1, &line_cur2,
3337 reverse_patch ? NULL : outfile,
3338 reverse_patch ? outfile : NULL,
3339 patch_cb, patch_arg);
3340 if (err)
3341 goto done;
3342 if (choice == GOT_PATCH_CHOICE_YES)
3343 have_content = 1;
3344 else if (choice == GOT_PATCH_CHOICE_QUIT)
3345 break;
3347 if (have_content) {
3348 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3349 reverse_patch ? NULL : outfile,
3350 reverse_patch ? outfile : NULL);
3351 if (err)
3352 goto done;
3354 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3355 err = got_error_from_errno2("chmod", path2);
3356 goto done;
3359 done:
3360 free(id_str);
3361 if (blob)
3362 got_object_blob_close(blob);
3363 if (f1 && fclose(f1) == EOF && err == NULL)
3364 err = got_error_from_errno2("fclose", path1);
3365 if (f2 && fclose(f2) == EOF && err == NULL)
3366 err = got_error_from_errno2("fclose", path2);
3367 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3368 err = got_error_from_errno2("close", path2);
3369 if (outfile && fclose(outfile) == EOF && err == NULL)
3370 err = got_error_from_errno2("fclose", *path_outfile);
3371 if (path1 && unlink(path1) == -1 && err == NULL)
3372 err = got_error_from_errno2("unlink", path1);
3373 if (err || !have_content) {
3374 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3375 err = got_error_from_errno2("unlink", *path_outfile);
3376 free(*path_outfile);
3377 *path_outfile = NULL;
3379 free(args);
3380 if (ds) {
3381 got_diff_state_free(ds);
3382 free(ds);
3384 if (changes)
3385 got_diff_free_changes(changes);
3386 free(path1);
3387 return err;
3390 static const struct got_error *
3391 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3392 const char *relpath, struct got_object_id *blob_id,
3393 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3394 int dirfd, const char *de_name)
3396 struct revert_file_args *a = arg;
3397 const struct got_error *err = NULL;
3398 char *parent_path = NULL;
3399 struct got_fileindex_entry *ie;
3400 struct got_tree_object *tree = NULL;
3401 struct got_object_id *tree_id = NULL;
3402 const struct got_tree_entry *te = NULL;
3403 char *tree_path = NULL, *te_name;
3404 char *ondisk_path = NULL, *path_content = NULL;
3405 struct got_blob_object *blob = NULL;
3407 /* Reverting a staged deletion is a no-op. */
3408 if (status == GOT_STATUS_DELETE &&
3409 staged_status != GOT_STATUS_NO_CHANGE)
3410 return NULL;
3412 if (status == GOT_STATUS_UNVERSIONED)
3413 return (*a->progress_cb)(a->progress_arg,
3414 GOT_STATUS_UNVERSIONED, relpath);
3416 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3417 if (ie == NULL)
3418 return got_error(GOT_ERR_BAD_PATH);
3420 /* Construct in-repository path of tree which contains this blob. */
3421 err = got_path_dirname(&parent_path, ie->path);
3422 if (err) {
3423 if (err->code != GOT_ERR_BAD_PATH)
3424 goto done;
3425 parent_path = strdup("/");
3426 if (parent_path == NULL) {
3427 err = got_error_from_errno("strdup");
3428 goto done;
3431 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3432 tree_path = strdup(parent_path);
3433 if (tree_path == NULL) {
3434 err = got_error_from_errno("strdup");
3435 goto done;
3437 } else {
3438 if (got_path_is_root_dir(parent_path)) {
3439 tree_path = strdup(a->worktree->path_prefix);
3440 if (tree_path == NULL) {
3441 err = got_error_from_errno("strdup");
3442 goto done;
3444 } else {
3445 if (asprintf(&tree_path, "%s/%s",
3446 a->worktree->path_prefix, parent_path) == -1) {
3447 err = got_error_from_errno("asprintf");
3448 goto done;
3453 err = got_object_id_by_path(&tree_id, a->repo,
3454 a->worktree->base_commit_id, tree_path);
3455 if (err) {
3456 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3457 (status == GOT_STATUS_ADD ||
3458 staged_status == GOT_STATUS_ADD)))
3459 goto done;
3460 } else {
3461 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3462 if (err)
3463 goto done;
3465 te_name = basename(ie->path);
3466 if (te_name == NULL) {
3467 err = got_error_from_errno2("basename", ie->path);
3468 goto done;
3471 te = got_object_tree_find_entry(tree, te_name);
3472 if (te == NULL && status != GOT_STATUS_ADD &&
3473 staged_status != GOT_STATUS_ADD) {
3474 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3475 goto done;
3479 switch (status) {
3480 case GOT_STATUS_ADD:
3481 if (a->patch_cb) {
3482 int choice = GOT_PATCH_CHOICE_NONE;
3483 err = (*a->patch_cb)(&choice, a->patch_arg,
3484 status, ie->path, NULL, 1, 1);
3485 if (err)
3486 goto done;
3487 if (choice != GOT_PATCH_CHOICE_YES)
3488 break;
3490 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3491 ie->path);
3492 if (err)
3493 goto done;
3494 got_fileindex_entry_remove(a->fileindex, ie);
3495 break;
3496 case GOT_STATUS_DELETE:
3497 if (a->patch_cb) {
3498 int choice = GOT_PATCH_CHOICE_NONE;
3499 err = (*a->patch_cb)(&choice, a->patch_arg,
3500 status, ie->path, NULL, 1, 1);
3501 if (err)
3502 goto done;
3503 if (choice != GOT_PATCH_CHOICE_YES)
3504 break;
3506 /* fall through */
3507 case GOT_STATUS_MODIFY:
3508 case GOT_STATUS_MODE_CHANGE:
3509 case GOT_STATUS_CONFLICT:
3510 case GOT_STATUS_MISSING: {
3511 struct got_object_id id;
3512 if (staged_status == GOT_STATUS_ADD ||
3513 staged_status == GOT_STATUS_MODIFY) {
3514 memcpy(id.sha1, ie->staged_blob_sha1,
3515 SHA1_DIGEST_LENGTH);
3516 } else
3517 memcpy(id.sha1, ie->blob_sha1,
3518 SHA1_DIGEST_LENGTH);
3519 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3520 if (err)
3521 goto done;
3523 if (asprintf(&ondisk_path, "%s/%s",
3524 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3525 err = got_error_from_errno("asprintf");
3526 goto done;
3529 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3530 status == GOT_STATUS_CONFLICT)) {
3531 err = create_patched_content(&path_content, 1, &id,
3532 ondisk_path, dirfd, de_name, ie->path, a->repo,
3533 a->patch_cb, a->patch_arg);
3534 if (err || path_content == NULL)
3535 break;
3536 if (rename(path_content, ondisk_path) == -1) {
3537 err = got_error_from_errno3("rename",
3538 path_content, ondisk_path);
3539 goto done;
3541 } else {
3542 err = install_blob(a->worktree, ondisk_path, ie->path,
3543 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3544 got_fileindex_perms_to_st(ie), blob, 0, 1,
3545 a->repo, a->progress_cb, a->progress_arg);
3546 if (err)
3547 goto done;
3548 if (status == GOT_STATUS_DELETE ||
3549 status == GOT_STATUS_MODE_CHANGE) {
3550 err = update_blob_fileindex_entry(a->worktree,
3551 a->fileindex, ie, ondisk_path, ie->path,
3552 blob, 1);
3553 if (err)
3554 goto done;
3557 break;
3559 default:
3560 break;
3562 done:
3563 free(ondisk_path);
3564 free(path_content);
3565 free(parent_path);
3566 free(tree_path);
3567 if (blob)
3568 got_object_blob_close(blob);
3569 if (tree)
3570 got_object_tree_close(tree);
3571 free(tree_id);
3572 return err;
3575 const struct got_error *
3576 got_worktree_revert(struct got_worktree *worktree,
3577 struct got_pathlist_head *paths,
3578 got_worktree_checkout_cb progress_cb, void *progress_arg,
3579 got_worktree_patch_cb patch_cb, void *patch_arg,
3580 struct got_repository *repo)
3582 struct got_fileindex *fileindex = NULL;
3583 char *fileindex_path = NULL;
3584 const struct got_error *err = NULL, *unlockerr = NULL;
3585 const struct got_error *sync_err = NULL;
3586 struct got_pathlist_entry *pe;
3587 struct revert_file_args rfa;
3589 err = lock_worktree(worktree, LOCK_EX);
3590 if (err)
3591 return err;
3593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3594 if (err)
3595 goto done;
3597 rfa.worktree = worktree;
3598 rfa.fileindex = fileindex;
3599 rfa.progress_cb = progress_cb;
3600 rfa.progress_arg = progress_arg;
3601 rfa.patch_cb = patch_cb;
3602 rfa.patch_arg = patch_arg;
3603 rfa.repo = repo;
3604 TAILQ_FOREACH(pe, paths, entry) {
3605 err = worktree_status(worktree, pe->path, fileindex, repo,
3606 revert_file, &rfa, NULL, NULL, 0, 0);
3607 if (err)
3608 break;
3610 sync_err = sync_fileindex(fileindex, fileindex_path);
3611 if (sync_err && err == NULL)
3612 err = sync_err;
3613 done:
3614 free(fileindex_path);
3615 if (fileindex)
3616 got_fileindex_free(fileindex);
3617 unlockerr = lock_worktree(worktree, LOCK_SH);
3618 if (unlockerr && err == NULL)
3619 err = unlockerr;
3620 return err;
3623 static void
3624 free_commitable(struct got_commitable *ct)
3626 free(ct->path);
3627 free(ct->in_repo_path);
3628 free(ct->ondisk_path);
3629 free(ct->blob_id);
3630 free(ct->base_blob_id);
3631 free(ct->staged_blob_id);
3632 free(ct->base_commit_id);
3633 free(ct);
3636 struct collect_commitables_arg {
3637 struct got_pathlist_head *commitable_paths;
3638 struct got_repository *repo;
3639 struct got_worktree *worktree;
3640 int have_staged_files;
3643 static const struct got_error *
3644 collect_commitables(void *arg, unsigned char status,
3645 unsigned char staged_status, const char *relpath,
3646 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3647 struct got_object_id *commit_id, int dirfd, const char *de_name)
3649 struct collect_commitables_arg *a = arg;
3650 const struct got_error *err = NULL;
3651 struct got_commitable *ct = NULL;
3652 struct got_pathlist_entry *new = NULL;
3653 char *parent_path = NULL, *path = NULL;
3654 struct stat sb;
3656 if (a->have_staged_files) {
3657 if (staged_status != GOT_STATUS_MODIFY &&
3658 staged_status != GOT_STATUS_ADD &&
3659 staged_status != GOT_STATUS_DELETE)
3660 return NULL;
3661 } else {
3662 if (status == GOT_STATUS_CONFLICT)
3663 return got_error(GOT_ERR_COMMIT_CONFLICT);
3665 if (status != GOT_STATUS_MODIFY &&
3666 status != GOT_STATUS_MODE_CHANGE &&
3667 status != GOT_STATUS_ADD &&
3668 status != GOT_STATUS_DELETE)
3669 return NULL;
3672 if (asprintf(&path, "/%s", relpath) == -1) {
3673 err = got_error_from_errno("asprintf");
3674 goto done;
3676 if (strcmp(path, "/") == 0) {
3677 parent_path = strdup("");
3678 if (parent_path == NULL)
3679 return got_error_from_errno("strdup");
3680 } else {
3681 err = got_path_dirname(&parent_path, path);
3682 if (err)
3683 return err;
3686 ct = calloc(1, sizeof(*ct));
3687 if (ct == NULL) {
3688 err = got_error_from_errno("calloc");
3689 goto done;
3692 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3693 relpath) == -1) {
3694 err = got_error_from_errno("asprintf");
3695 goto done;
3697 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3698 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3699 } else {
3700 if (dirfd != -1) {
3701 if (fstatat(dirfd, de_name, &sb,
3702 AT_SYMLINK_NOFOLLOW) == -1) {
3703 err = got_error_from_errno2("fstatat",
3704 ct->ondisk_path);
3705 goto done;
3707 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3708 err = got_error_from_errno2("lstat", ct->ondisk_path);
3709 goto done;
3711 ct->mode = sb.st_mode;
3714 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3715 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3716 relpath) == -1) {
3717 err = got_error_from_errno("asprintf");
3718 goto done;
3721 ct->status = status;
3722 ct->staged_status = staged_status;
3723 ct->blob_id = NULL; /* will be filled in when blob gets created */
3724 if (ct->status != GOT_STATUS_ADD &&
3725 ct->staged_status != GOT_STATUS_ADD) {
3726 ct->base_blob_id = got_object_id_dup(blob_id);
3727 if (ct->base_blob_id == NULL) {
3728 err = got_error_from_errno("got_object_id_dup");
3729 goto done;
3731 ct->base_commit_id = got_object_id_dup(commit_id);
3732 if (ct->base_commit_id == NULL) {
3733 err = got_error_from_errno("got_object_id_dup");
3734 goto done;
3737 if (ct->staged_status == GOT_STATUS_ADD ||
3738 ct->staged_status == GOT_STATUS_MODIFY) {
3739 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3740 if (ct->staged_blob_id == NULL) {
3741 err = got_error_from_errno("got_object_id_dup");
3742 goto done;
3745 ct->path = strdup(path);
3746 if (ct->path == NULL) {
3747 err = got_error_from_errno("strdup");
3748 goto done;
3750 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3751 done:
3752 if (ct && (err || new == NULL))
3753 free_commitable(ct);
3754 free(parent_path);
3755 free(path);
3756 return err;
3759 static const struct got_error *write_tree(struct got_object_id **,
3760 struct got_tree_object *, const char *, struct got_pathlist_head *,
3761 got_worktree_status_cb status_cb, void *status_arg,
3762 struct got_repository *);
3764 static const struct got_error *
3765 write_subtree(struct got_object_id **new_subtree_id,
3766 struct got_tree_entry *te, const char *parent_path,
3767 struct got_pathlist_head *commitable_paths,
3768 got_worktree_status_cb status_cb, void *status_arg,
3769 struct got_repository *repo)
3771 const struct got_error *err = NULL;
3772 struct got_tree_object *subtree;
3773 char *subpath;
3775 if (asprintf(&subpath, "%s%s%s", parent_path,
3776 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3777 return got_error_from_errno("asprintf");
3779 err = got_object_open_as_tree(&subtree, repo, &te->id);
3780 if (err)
3781 return err;
3783 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3784 status_cb, status_arg, repo);
3785 got_object_tree_close(subtree);
3786 free(subpath);
3787 return err;
3790 static const struct got_error *
3791 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3793 const struct got_error *err = NULL;
3794 char *ct_parent_path = NULL;
3796 *match = 0;
3798 if (strchr(ct->in_repo_path, '/') == NULL) {
3799 *match = got_path_is_root_dir(path);
3800 return NULL;
3803 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3804 if (err)
3805 return err;
3806 *match = (strcmp(path, ct_parent_path) == 0);
3807 free(ct_parent_path);
3808 return err;
3811 static mode_t
3812 get_ct_file_mode(struct got_commitable *ct)
3814 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3817 static const struct got_error *
3818 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3819 struct got_tree_entry *te, struct got_commitable *ct)
3821 const struct got_error *err = NULL;
3823 *new_te = NULL;
3825 err = got_object_tree_entry_dup(new_te, te);
3826 if (err)
3827 goto done;
3829 (*new_te)->mode = get_ct_file_mode(ct);
3831 if (ct->staged_status == GOT_STATUS_MODIFY)
3832 memcpy(&(*new_te)->id, ct->staged_blob_id,
3833 sizeof((*new_te)->id));
3834 else
3835 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3836 done:
3837 if (err && *new_te) {
3838 free(*new_te);
3839 *new_te = NULL;
3841 return err;
3844 static const struct got_error *
3845 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3846 struct got_commitable *ct)
3848 const struct got_error *err = NULL;
3849 char *ct_name;
3851 *new_te = NULL;
3853 *new_te = calloc(1, sizeof(**new_te));
3854 if (*new_te == NULL)
3855 return got_error_from_errno("calloc");
3857 ct_name = basename(ct->path);
3858 if (ct_name == NULL) {
3859 err = got_error_from_errno2("basename", ct->path);
3860 goto done;
3862 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3863 sizeof((*new_te)->name)) {
3864 err = got_error(GOT_ERR_NO_SPACE);
3865 goto done;
3868 (*new_te)->mode = get_ct_file_mode(ct);
3870 if (ct->staged_status == GOT_STATUS_ADD)
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 insert_tree_entry(struct got_tree_entry *new_te,
3885 struct got_pathlist_head *paths)
3887 const struct got_error *err = NULL;
3888 struct got_pathlist_entry *new_pe;
3890 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3891 if (err)
3892 return err;
3893 if (new_pe == NULL)
3894 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3895 return NULL;
3898 static const struct got_error *
3899 report_ct_status(struct got_commitable *ct,
3900 got_worktree_status_cb status_cb, void *status_arg)
3902 const char *ct_path = ct->path;
3903 unsigned char status;
3905 while (ct_path[0] == '/')
3906 ct_path++;
3908 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3909 status = ct->staged_status;
3910 else
3911 status = ct->status;
3913 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3914 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3917 static const struct got_error *
3918 match_modified_subtree(int *modified, struct got_tree_entry *te,
3919 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3921 const struct got_error *err = NULL;
3922 struct got_pathlist_entry *pe;
3923 char *te_path;
3925 *modified = 0;
3927 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3928 got_path_is_root_dir(base_tree_path) ? "" : "/",
3929 te->name) == -1)
3930 return got_error_from_errno("asprintf");
3932 TAILQ_FOREACH(pe, commitable_paths, entry) {
3933 struct got_commitable *ct = pe->data;
3934 *modified = got_path_is_child(ct->in_repo_path, te_path,
3935 strlen(te_path));
3936 if (*modified)
3937 break;
3940 free(te_path);
3941 return err;
3944 static const struct got_error *
3945 match_deleted_or_modified_ct(struct got_commitable **ctp,
3946 struct got_tree_entry *te, const char *base_tree_path,
3947 struct got_pathlist_head *commitable_paths)
3949 const struct got_error *err = NULL;
3950 struct got_pathlist_entry *pe;
3952 *ctp = NULL;
3954 TAILQ_FOREACH(pe, commitable_paths, entry) {
3955 struct got_commitable *ct = pe->data;
3956 char *ct_name = NULL;
3957 int path_matches;
3959 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3960 if (ct->status != GOT_STATUS_MODIFY &&
3961 ct->status != GOT_STATUS_MODE_CHANGE &&
3962 ct->status != GOT_STATUS_DELETE)
3963 continue;
3964 } else {
3965 if (ct->staged_status != GOT_STATUS_MODIFY &&
3966 ct->staged_status != GOT_STATUS_DELETE)
3967 continue;
3970 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3971 continue;
3973 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3974 if (err)
3975 return err;
3976 if (!path_matches)
3977 continue;
3979 ct_name = basename(pe->path);
3980 if (ct_name == NULL)
3981 return got_error_from_errno2("basename", pe->path);
3983 if (strcmp(te->name, ct_name) != 0)
3984 continue;
3986 *ctp = ct;
3987 break;
3990 return err;
3993 static const struct got_error *
3994 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3995 const char *child_path, const char *path_base_tree,
3996 struct got_pathlist_head *commitable_paths,
3997 got_worktree_status_cb status_cb, void *status_arg,
3998 struct got_repository *repo)
4000 const struct got_error *err = NULL;
4001 struct got_tree_entry *new_te;
4002 char *subtree_path;
4003 struct got_object_id *id = NULL;
4005 *new_tep = NULL;
4007 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4008 got_path_is_root_dir(path_base_tree) ? "" : "/",
4009 child_path) == -1)
4010 return got_error_from_errno("asprintf");
4012 new_te = calloc(1, sizeof(*new_te));
4013 if (new_te == NULL)
4014 return got_error_from_errno("calloc");
4015 new_te->mode = S_IFDIR;
4017 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4018 sizeof(new_te->name)) {
4019 err = got_error(GOT_ERR_NO_SPACE);
4020 goto done;
4022 err = write_tree(&id, NULL, subtree_path,
4023 commitable_paths, status_cb, status_arg, repo);
4024 if (err) {
4025 free(new_te);
4026 goto done;
4028 memcpy(&new_te->id, id, sizeof(new_te->id));
4029 done:
4030 free(id);
4031 free(subtree_path);
4032 if (err == NULL)
4033 *new_tep = new_te;
4034 return err;
4037 static const struct got_error *
4038 write_tree(struct got_object_id **new_tree_id,
4039 struct got_tree_object *base_tree, const char *path_base_tree,
4040 struct got_pathlist_head *commitable_paths,
4041 got_worktree_status_cb status_cb, void *status_arg,
4042 struct got_repository *repo)
4044 const struct got_error *err = NULL;
4045 struct got_pathlist_head paths;
4046 struct got_tree_entry *te, *new_te = NULL;
4047 struct got_pathlist_entry *pe;
4048 int nentries = 0;
4050 TAILQ_INIT(&paths);
4052 /* Insert, and recurse into, newly added entries first. */
4053 TAILQ_FOREACH(pe, commitable_paths, entry) {
4054 struct got_commitable *ct = pe->data;
4055 char *child_path = NULL, *slash;
4057 if ((ct->status != GOT_STATUS_ADD &&
4058 ct->staged_status != GOT_STATUS_ADD) ||
4059 (ct->flags & GOT_COMMITABLE_ADDED))
4060 continue;
4062 if (!got_path_is_child(pe->path, path_base_tree,
4063 strlen(path_base_tree)))
4064 continue;
4066 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4067 pe->path);
4068 if (err)
4069 goto done;
4071 slash = strchr(child_path, '/');
4072 if (slash == NULL) {
4073 err = alloc_added_blob_tree_entry(&new_te, ct);
4074 if (err)
4075 goto done;
4076 err = report_ct_status(ct, status_cb, status_arg);
4077 if (err)
4078 goto done;
4079 ct->flags |= GOT_COMMITABLE_ADDED;
4080 err = insert_tree_entry(new_te, &paths);
4081 if (err)
4082 goto done;
4083 nentries++;
4084 } else {
4085 *slash = '\0'; /* trim trailing path components */
4086 if (base_tree == NULL ||
4087 got_object_tree_find_entry(base_tree, child_path)
4088 == NULL) {
4089 err = make_subtree_for_added_blob(&new_te,
4090 child_path, path_base_tree,
4091 commitable_paths, status_cb, status_arg,
4092 repo);
4093 if (err)
4094 goto done;
4095 err = insert_tree_entry(new_te, &paths);
4096 if (err)
4097 goto done;
4098 nentries++;
4103 if (base_tree) {
4104 int i, nbase_entries;
4105 /* Handle modified and deleted entries. */
4106 nbase_entries = got_object_tree_get_nentries(base_tree);
4107 for (i = 0; i < nbase_entries; i++) {
4108 struct got_commitable *ct = NULL;
4110 te = got_object_tree_get_entry(base_tree, i);
4111 if (got_object_tree_entry_is_submodule(te)) {
4112 /* Entry is a submodule; just copy it. */
4113 err = got_object_tree_entry_dup(&new_te, te);
4114 if (err)
4115 goto done;
4116 err = insert_tree_entry(new_te, &paths);
4117 if (err)
4118 goto done;
4119 nentries++;
4120 continue;
4123 if (S_ISDIR(te->mode)) {
4124 int modified;
4125 err = got_object_tree_entry_dup(&new_te, te);
4126 if (err)
4127 goto done;
4128 err = match_modified_subtree(&modified, te,
4129 path_base_tree, commitable_paths);
4130 if (err)
4131 goto done;
4132 /* Avoid recursion into unmodified subtrees. */
4133 if (modified) {
4134 struct got_object_id *new_id;
4135 err = write_subtree(&new_id, te,
4136 path_base_tree, commitable_paths,
4137 status_cb, status_arg, repo);
4138 if (err)
4139 goto done;
4140 memcpy(&new_te->id, new_id,
4141 sizeof(new_te->id));
4142 free(new_id);
4144 err = insert_tree_entry(new_te, &paths);
4145 if (err)
4146 goto done;
4147 nentries++;
4148 continue;
4151 err = match_deleted_or_modified_ct(&ct, te,
4152 path_base_tree, commitable_paths);
4153 if (err)
4154 goto done;
4155 if (ct) {
4156 /* NB: Deleted entries get dropped here. */
4157 if (ct->status == GOT_STATUS_MODIFY ||
4158 ct->status == GOT_STATUS_MODE_CHANGE ||
4159 ct->staged_status == GOT_STATUS_MODIFY) {
4160 err = alloc_modified_blob_tree_entry(
4161 &new_te, te, ct);
4162 if (err)
4163 goto done;
4164 err = insert_tree_entry(new_te, &paths);
4165 if (err)
4166 goto done;
4167 nentries++;
4169 err = report_ct_status(ct, status_cb,
4170 status_arg);
4171 if (err)
4172 goto done;
4173 } else {
4174 /* Entry is unchanged; just copy it. */
4175 err = got_object_tree_entry_dup(&new_te, te);
4176 if (err)
4177 goto done;
4178 err = insert_tree_entry(new_te, &paths);
4179 if (err)
4180 goto done;
4181 nentries++;
4186 /* Write new list of entries; deleted entries have been dropped. */
4187 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4188 done:
4189 got_pathlist_free(&paths);
4190 return err;
4193 static const struct got_error *
4194 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4195 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4196 int have_staged_files)
4198 const struct got_error *err = NULL;
4199 struct got_pathlist_entry *pe;
4201 TAILQ_FOREACH(pe, commitable_paths, entry) {
4202 struct got_fileindex_entry *ie;
4203 struct got_commitable *ct = pe->data;
4205 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4206 if (ie) {
4207 if (ct->status == GOT_STATUS_DELETE ||
4208 ct->staged_status == GOT_STATUS_DELETE) {
4209 got_fileindex_entry_remove(fileindex, ie);
4210 got_fileindex_entry_free(ie);
4211 } else if (ct->staged_status == GOT_STATUS_ADD ||
4212 ct->staged_status == GOT_STATUS_MODIFY) {
4213 got_fileindex_entry_stage_set(ie,
4214 GOT_FILEIDX_STAGE_NONE);
4215 err = got_fileindex_entry_update(ie,
4216 ct->ondisk_path, ct->staged_blob_id->sha1,
4217 new_base_commit_id->sha1,
4218 !have_staged_files);
4219 } else
4220 err = got_fileindex_entry_update(ie,
4221 ct->ondisk_path, ct->blob_id->sha1,
4222 new_base_commit_id->sha1,
4223 !have_staged_files);
4224 } else {
4225 err = got_fileindex_entry_alloc(&ie,
4226 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4227 new_base_commit_id->sha1);
4228 if (err)
4229 break;
4230 err = got_fileindex_entry_add(fileindex, ie);
4231 if (err)
4232 break;
4235 return err;
4239 static const struct got_error *
4240 check_out_of_date(const char *in_repo_path, unsigned char status,
4241 unsigned char staged_status, struct got_object_id *base_blob_id,
4242 struct got_object_id *base_commit_id,
4243 struct got_object_id *head_commit_id, struct got_repository *repo,
4244 int ood_errcode)
4246 const struct got_error *err = NULL;
4247 struct got_object_id *id = NULL;
4249 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4250 /* Trivial case: base commit == head commit */
4251 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4252 return NULL;
4254 * Ensure file content which local changes were based
4255 * on matches file content in the branch head.
4257 err = got_object_id_by_path(&id, repo, head_commit_id,
4258 in_repo_path);
4259 if (err) {
4260 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4261 err = got_error(ood_errcode);
4262 goto done;
4263 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4264 err = got_error(ood_errcode);
4265 } else {
4266 /* Require that added files don't exist in the branch head. */
4267 err = got_object_id_by_path(&id, repo, head_commit_id,
4268 in_repo_path);
4269 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4270 goto done;
4271 err = id ? got_error(ood_errcode) : NULL;
4273 done:
4274 free(id);
4275 return err;
4278 const struct got_error *
4279 commit_worktree(struct got_object_id **new_commit_id,
4280 struct got_pathlist_head *commitable_paths,
4281 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4282 const char *author, const char *committer,
4283 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4284 got_worktree_status_cb status_cb, void *status_arg,
4285 struct got_repository *repo)
4287 const struct got_error *err = NULL, *unlockerr = NULL;
4288 struct got_pathlist_entry *pe;
4289 const char *head_ref_name = NULL;
4290 struct got_commit_object *head_commit = NULL;
4291 struct got_reference *head_ref2 = NULL;
4292 struct got_object_id *head_commit_id2 = NULL;
4293 struct got_tree_object *head_tree = NULL;
4294 struct got_object_id *new_tree_id = NULL;
4295 struct got_object_id_queue parent_ids;
4296 struct got_object_qid *pid = NULL;
4297 char *logmsg = NULL;
4299 *new_commit_id = NULL;
4301 SIMPLEQ_INIT(&parent_ids);
4303 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4304 if (err)
4305 goto done;
4307 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4308 if (err)
4309 goto done;
4311 if (commit_msg_cb != NULL) {
4312 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4313 if (err)
4314 goto done;
4317 if (logmsg == NULL || strlen(logmsg) == 0) {
4318 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4319 goto done;
4322 /* Create blobs from added and modified files and record their IDs. */
4323 TAILQ_FOREACH(pe, commitable_paths, entry) {
4324 struct got_commitable *ct = pe->data;
4325 char *ondisk_path;
4327 /* Blobs for staged files already exist. */
4328 if (ct->staged_status == GOT_STATUS_ADD ||
4329 ct->staged_status == GOT_STATUS_MODIFY)
4330 continue;
4332 if (ct->status != GOT_STATUS_ADD &&
4333 ct->status != GOT_STATUS_MODIFY &&
4334 ct->status != GOT_STATUS_MODE_CHANGE)
4335 continue;
4337 if (asprintf(&ondisk_path, "%s/%s",
4338 worktree->root_path, pe->path) == -1) {
4339 err = got_error_from_errno("asprintf");
4340 goto done;
4342 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4343 free(ondisk_path);
4344 if (err)
4345 goto done;
4348 /* Recursively write new tree objects. */
4349 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4350 status_cb, status_arg, repo);
4351 if (err)
4352 goto done;
4354 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4355 if (err)
4356 goto done;
4357 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4358 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4359 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4360 got_object_qid_free(pid);
4361 if (logmsg != NULL)
4362 free(logmsg);
4363 if (err)
4364 goto done;
4366 /* Check if a concurrent commit to our branch has occurred. */
4367 head_ref_name = got_worktree_get_head_ref_name(worktree);
4368 if (head_ref_name == NULL) {
4369 err = got_error_from_errno("got_worktree_get_head_ref_name");
4370 goto done;
4372 /* Lock the reference here to prevent concurrent modification. */
4373 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4374 if (err)
4375 goto done;
4376 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4377 if (err)
4378 goto done;
4379 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4380 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4381 goto done;
4383 /* Update branch head in repository. */
4384 err = got_ref_change_ref(head_ref2, *new_commit_id);
4385 if (err)
4386 goto done;
4387 err = got_ref_write(head_ref2, repo);
4388 if (err)
4389 goto done;
4391 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4392 if (err)
4393 goto done;
4395 err = ref_base_commit(worktree, repo);
4396 if (err)
4397 goto done;
4398 done:
4399 if (head_tree)
4400 got_object_tree_close(head_tree);
4401 if (head_commit)
4402 got_object_commit_close(head_commit);
4403 free(head_commit_id2);
4404 if (head_ref2) {
4405 unlockerr = got_ref_unlock(head_ref2);
4406 if (unlockerr && err == NULL)
4407 err = unlockerr;
4408 got_ref_close(head_ref2);
4410 return err;
4413 static const struct got_error *
4414 check_path_is_commitable(const char *path,
4415 struct got_pathlist_head *commitable_paths)
4417 struct got_pathlist_entry *cpe = NULL;
4418 size_t path_len = strlen(path);
4420 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4421 struct got_commitable *ct = cpe->data;
4422 const char *ct_path = ct->path;
4424 while (ct_path[0] == '/')
4425 ct_path++;
4427 if (strcmp(path, ct_path) == 0 ||
4428 got_path_is_child(ct_path, path, path_len))
4429 break;
4432 if (cpe == NULL)
4433 return got_error_path(path, GOT_ERR_BAD_PATH);
4435 return NULL;
4438 static const struct got_error *
4439 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4441 int *have_staged_files = arg;
4443 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4444 *have_staged_files = 1;
4445 return got_error(GOT_ERR_CANCELLED);
4448 return NULL;
4451 static const struct got_error *
4452 check_non_staged_files(struct got_fileindex *fileindex,
4453 struct got_pathlist_head *paths)
4455 struct got_pathlist_entry *pe;
4456 struct got_fileindex_entry *ie;
4458 TAILQ_FOREACH(pe, paths, entry) {
4459 if (pe->path[0] == '\0')
4460 continue;
4461 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4462 if (ie == NULL)
4463 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4464 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4465 return got_error_path(pe->path,
4466 GOT_ERR_FILE_NOT_STAGED);
4469 return NULL;
4472 const struct got_error *
4473 got_worktree_commit(struct got_object_id **new_commit_id,
4474 struct got_worktree *worktree, struct got_pathlist_head *paths,
4475 const char *author, const char *committer,
4476 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4477 got_worktree_status_cb status_cb, void *status_arg,
4478 struct got_repository *repo)
4480 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4481 struct got_fileindex *fileindex = NULL;
4482 char *fileindex_path = NULL;
4483 struct got_pathlist_head commitable_paths;
4484 struct collect_commitables_arg cc_arg;
4485 struct got_pathlist_entry *pe;
4486 struct got_reference *head_ref = NULL;
4487 struct got_object_id *head_commit_id = NULL;
4488 int have_staged_files = 0;
4490 *new_commit_id = NULL;
4492 TAILQ_INIT(&commitable_paths);
4494 err = lock_worktree(worktree, LOCK_EX);
4495 if (err)
4496 goto done;
4498 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4499 if (err)
4500 goto done;
4502 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4503 if (err)
4504 goto done;
4506 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4507 if (err)
4508 goto done;
4510 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4511 &have_staged_files);
4512 if (err && err->code != GOT_ERR_CANCELLED)
4513 goto done;
4514 if (have_staged_files) {
4515 err = check_non_staged_files(fileindex, paths);
4516 if (err)
4517 goto done;
4520 cc_arg.commitable_paths = &commitable_paths;
4521 cc_arg.worktree = worktree;
4522 cc_arg.repo = repo;
4523 cc_arg.have_staged_files = have_staged_files;
4524 TAILQ_FOREACH(pe, paths, entry) {
4525 err = worktree_status(worktree, pe->path, fileindex, repo,
4526 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4527 if (err)
4528 goto done;
4531 if (TAILQ_EMPTY(&commitable_paths)) {
4532 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4533 goto done;
4536 TAILQ_FOREACH(pe, paths, entry) {
4537 err = check_path_is_commitable(pe->path, &commitable_paths);
4538 if (err)
4539 goto done;
4542 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4543 struct got_commitable *ct = pe->data;
4544 const char *ct_path = ct->in_repo_path;
4546 while (ct_path[0] == '/')
4547 ct_path++;
4548 err = check_out_of_date(ct_path, ct->status,
4549 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4550 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4551 if (err)
4552 goto done;
4556 err = commit_worktree(new_commit_id, &commitable_paths,
4557 head_commit_id, worktree, author, committer,
4558 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4559 if (err)
4560 goto done;
4562 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4563 fileindex, have_staged_files);
4564 sync_err = sync_fileindex(fileindex, fileindex_path);
4565 if (sync_err && err == NULL)
4566 err = sync_err;
4567 done:
4568 if (fileindex)
4569 got_fileindex_free(fileindex);
4570 free(fileindex_path);
4571 unlockerr = lock_worktree(worktree, LOCK_SH);
4572 if (unlockerr && err == NULL)
4573 err = unlockerr;
4574 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4575 struct got_commitable *ct = pe->data;
4576 free_commitable(ct);
4578 got_pathlist_free(&commitable_paths);
4579 return err;
4582 const char *
4583 got_commitable_get_path(struct got_commitable *ct)
4585 return ct->path;
4588 unsigned int
4589 got_commitable_get_status(struct got_commitable *ct)
4591 return ct->status;
4594 struct check_rebase_ok_arg {
4595 struct got_worktree *worktree;
4596 struct got_repository *repo;
4599 static const struct got_error *
4600 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4602 const struct got_error *err = NULL;
4603 struct check_rebase_ok_arg *a = arg;
4604 unsigned char status;
4605 struct stat sb;
4606 char *ondisk_path;
4608 /* Reject rebase of a work tree with mixed base commits. */
4609 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4610 SHA1_DIGEST_LENGTH))
4611 return got_error(GOT_ERR_MIXED_COMMITS);
4613 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4614 == -1)
4615 return got_error_from_errno("asprintf");
4617 /* Reject rebase of a work tree with modified or staged files. */
4618 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4619 free(ondisk_path);
4620 if (err)
4621 return err;
4623 if (status != GOT_STATUS_NO_CHANGE)
4624 return got_error(GOT_ERR_MODIFIED);
4625 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4626 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4628 return NULL;
4631 const struct got_error *
4632 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4633 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4634 struct got_worktree *worktree, struct got_reference *branch,
4635 struct got_repository *repo)
4637 const struct got_error *err = NULL;
4638 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4639 char *branch_ref_name = NULL;
4640 char *fileindex_path = NULL;
4641 struct check_rebase_ok_arg ok_arg;
4642 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4644 *new_base_branch_ref = NULL;
4645 *tmp_branch = NULL;
4646 *fileindex = NULL;
4648 err = lock_worktree(worktree, LOCK_EX);
4649 if (err)
4650 return err;
4652 err = open_fileindex(fileindex, &fileindex_path, worktree);
4653 if (err)
4654 goto done;
4656 ok_arg.worktree = worktree;
4657 ok_arg.repo = repo;
4658 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4659 &ok_arg);
4660 if (err)
4661 goto done;
4663 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4664 if (err)
4665 goto done;
4667 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4668 if (err)
4669 goto done;
4671 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4672 if (err)
4673 goto done;
4675 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4676 0);
4677 if (err)
4678 goto done;
4680 err = got_ref_alloc_symref(new_base_branch_ref,
4681 new_base_branch_ref_name, wt_branch);
4682 if (err)
4683 goto done;
4684 err = got_ref_write(*new_base_branch_ref, repo);
4685 if (err)
4686 goto done;
4688 /* TODO Lock original branch's ref while rebasing? */
4690 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4691 if (err)
4692 goto done;
4694 err = got_ref_write(branch_ref, repo);
4695 if (err)
4696 goto done;
4698 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4699 worktree->base_commit_id);
4700 if (err)
4701 goto done;
4702 err = got_ref_write(*tmp_branch, repo);
4703 if (err)
4704 goto done;
4706 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4707 if (err)
4708 goto done;
4709 done:
4710 free(fileindex_path);
4711 free(tmp_branch_name);
4712 free(new_base_branch_ref_name);
4713 free(branch_ref_name);
4714 if (branch_ref)
4715 got_ref_close(branch_ref);
4716 if (wt_branch)
4717 got_ref_close(wt_branch);
4718 if (err) {
4719 if (*new_base_branch_ref) {
4720 got_ref_close(*new_base_branch_ref);
4721 *new_base_branch_ref = NULL;
4723 if (*tmp_branch) {
4724 got_ref_close(*tmp_branch);
4725 *tmp_branch = NULL;
4727 if (*fileindex) {
4728 got_fileindex_free(*fileindex);
4729 *fileindex = NULL;
4731 lock_worktree(worktree, LOCK_SH);
4733 return err;
4736 const struct got_error *
4737 got_worktree_rebase_continue(struct got_object_id **commit_id,
4738 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4739 struct got_reference **branch, struct got_fileindex **fileindex,
4740 struct got_worktree *worktree, struct got_repository *repo)
4742 const struct got_error *err;
4743 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4744 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4745 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4746 char *fileindex_path = NULL;
4747 int have_staged_files = 0;
4749 *commit_id = NULL;
4750 *new_base_branch = NULL;
4751 *tmp_branch = NULL;
4752 *branch = NULL;
4753 *fileindex = NULL;
4755 err = lock_worktree(worktree, LOCK_EX);
4756 if (err)
4757 return err;
4759 err = open_fileindex(fileindex, &fileindex_path, worktree);
4760 if (err)
4761 goto done;
4763 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4764 &have_staged_files);
4765 if (err && err->code != GOT_ERR_CANCELLED)
4766 goto done;
4767 if (have_staged_files) {
4768 err = got_error(GOT_ERR_STAGED_PATHS);
4769 goto done;
4772 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4773 if (err)
4774 goto done;
4776 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4777 if (err)
4778 goto done;
4780 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4781 if (err)
4782 goto done;
4784 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4785 if (err)
4786 goto done;
4788 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4789 if (err)
4790 goto done;
4792 err = got_ref_open(branch, repo,
4793 got_ref_get_symref_target(branch_ref), 0);
4794 if (err)
4795 goto done;
4797 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4798 if (err)
4799 goto done;
4801 err = got_ref_resolve(commit_id, repo, commit_ref);
4802 if (err)
4803 goto done;
4805 err = got_ref_open(new_base_branch, repo,
4806 new_base_branch_ref_name, 0);
4807 if (err)
4808 goto done;
4810 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4811 if (err)
4812 goto done;
4813 done:
4814 free(commit_ref_name);
4815 free(branch_ref_name);
4816 free(fileindex_path);
4817 if (commit_ref)
4818 got_ref_close(commit_ref);
4819 if (branch_ref)
4820 got_ref_close(branch_ref);
4821 if (err) {
4822 free(*commit_id);
4823 *commit_id = NULL;
4824 if (*tmp_branch) {
4825 got_ref_close(*tmp_branch);
4826 *tmp_branch = NULL;
4828 if (*new_base_branch) {
4829 got_ref_close(*new_base_branch);
4830 *new_base_branch = NULL;
4832 if (*branch) {
4833 got_ref_close(*branch);
4834 *branch = NULL;
4836 if (*fileindex) {
4837 got_fileindex_free(*fileindex);
4838 *fileindex = NULL;
4840 lock_worktree(worktree, LOCK_SH);
4842 return err;
4845 const struct got_error *
4846 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4848 const struct got_error *err;
4849 char *tmp_branch_name = NULL;
4851 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4852 if (err)
4853 return err;
4855 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4856 free(tmp_branch_name);
4857 return NULL;
4860 static const struct got_error *
4861 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4862 char **logmsg, void *arg)
4864 *logmsg = arg;
4865 return NULL;
4868 static const struct got_error *
4869 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4870 const char *path, struct got_object_id *blob_id,
4871 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4872 int dirfd, const char *de_name)
4874 return NULL;
4877 struct collect_merged_paths_arg {
4878 got_worktree_checkout_cb progress_cb;
4879 void *progress_arg;
4880 struct got_pathlist_head *merged_paths;
4883 static const struct got_error *
4884 collect_merged_paths(void *arg, unsigned char status, const char *path)
4886 const struct got_error *err;
4887 struct collect_merged_paths_arg *a = arg;
4888 char *p;
4889 struct got_pathlist_entry *new;
4891 err = (*a->progress_cb)(a->progress_arg, status, path);
4892 if (err)
4893 return err;
4895 if (status != GOT_STATUS_MERGE &&
4896 status != GOT_STATUS_ADD &&
4897 status != GOT_STATUS_DELETE &&
4898 status != GOT_STATUS_CONFLICT)
4899 return NULL;
4901 p = strdup(path);
4902 if (p == NULL)
4903 return got_error_from_errno("strdup");
4905 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4906 if (err || new == NULL)
4907 free(p);
4908 return err;
4911 void
4912 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4914 struct got_pathlist_entry *pe;
4916 TAILQ_FOREACH(pe, merged_paths, entry)
4917 free((char *)pe->path);
4919 got_pathlist_free(merged_paths);
4922 static const struct got_error *
4923 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4924 struct got_repository *repo)
4926 const struct got_error *err;
4927 struct got_reference *commit_ref = NULL;
4929 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4930 if (err) {
4931 if (err->code != GOT_ERR_NOT_REF)
4932 goto done;
4933 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4934 if (err)
4935 goto done;
4936 err = got_ref_write(commit_ref, repo);
4937 if (err)
4938 goto done;
4939 } else {
4940 struct got_object_id *stored_id;
4941 int cmp;
4943 err = got_ref_resolve(&stored_id, repo, commit_ref);
4944 if (err)
4945 goto done;
4946 cmp = got_object_id_cmp(commit_id, stored_id);
4947 free(stored_id);
4948 if (cmp != 0) {
4949 err = got_error(GOT_ERR_REBASE_COMMITID);
4950 goto done;
4953 done:
4954 if (commit_ref)
4955 got_ref_close(commit_ref);
4956 return err;
4959 static const struct got_error *
4960 rebase_merge_files(struct got_pathlist_head *merged_paths,
4961 const char *commit_ref_name, struct got_worktree *worktree,
4962 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4963 struct got_object_id *commit_id, struct got_repository *repo,
4964 got_worktree_checkout_cb progress_cb, void *progress_arg,
4965 got_cancel_cb cancel_cb, void *cancel_arg)
4967 const struct got_error *err;
4968 struct got_reference *commit_ref = NULL;
4969 struct collect_merged_paths_arg cmp_arg;
4970 char *fileindex_path;
4972 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4974 err = get_fileindex_path(&fileindex_path, worktree);
4975 if (err)
4976 return err;
4978 cmp_arg.progress_cb = progress_cb;
4979 cmp_arg.progress_arg = progress_arg;
4980 cmp_arg.merged_paths = merged_paths;
4981 err = merge_files(worktree, fileindex, fileindex_path,
4982 parent_commit_id, commit_id, repo, collect_merged_paths,
4983 &cmp_arg, cancel_cb, cancel_arg);
4984 if (commit_ref)
4985 got_ref_close(commit_ref);
4986 return err;
4989 const struct got_error *
4990 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4991 struct got_worktree *worktree, struct got_fileindex *fileindex,
4992 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4993 struct got_repository *repo,
4994 got_worktree_checkout_cb progress_cb, void *progress_arg,
4995 got_cancel_cb cancel_cb, void *cancel_arg)
4997 const struct got_error *err;
4998 char *commit_ref_name;
5000 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5001 if (err)
5002 return err;
5004 err = store_commit_id(commit_ref_name, commit_id, repo);
5005 if (err)
5006 goto done;
5008 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5009 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5010 progress_arg, cancel_cb, cancel_arg);
5011 done:
5012 free(commit_ref_name);
5013 return err;
5016 const struct got_error *
5017 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5018 struct got_worktree *worktree, struct got_fileindex *fileindex,
5019 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5020 struct got_repository *repo,
5021 got_worktree_checkout_cb progress_cb, void *progress_arg,
5022 got_cancel_cb cancel_cb, void *cancel_arg)
5024 const struct got_error *err;
5025 char *commit_ref_name;
5027 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5028 if (err)
5029 return err;
5031 err = store_commit_id(commit_ref_name, commit_id, repo);
5032 if (err)
5033 goto done;
5035 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5036 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5037 progress_arg, cancel_cb, cancel_arg);
5038 done:
5039 free(commit_ref_name);
5040 return err;
5043 static const struct got_error *
5044 rebase_commit(struct got_object_id **new_commit_id,
5045 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5046 struct got_worktree *worktree, struct got_fileindex *fileindex,
5047 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5048 const char *new_logmsg, struct got_repository *repo)
5050 const struct got_error *err, *sync_err;
5051 struct got_pathlist_head commitable_paths;
5052 struct collect_commitables_arg cc_arg;
5053 char *fileindex_path = NULL;
5054 struct got_reference *head_ref = NULL;
5055 struct got_object_id *head_commit_id = NULL;
5056 char *logmsg = NULL;
5058 TAILQ_INIT(&commitable_paths);
5059 *new_commit_id = NULL;
5061 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5063 err = get_fileindex_path(&fileindex_path, worktree);
5064 if (err)
5065 return err;
5067 cc_arg.commitable_paths = &commitable_paths;
5068 cc_arg.worktree = worktree;
5069 cc_arg.repo = repo;
5070 cc_arg.have_staged_files = 0;
5072 * If possible get the status of individual files directly to
5073 * avoid crawling the entire work tree once per rebased commit.
5074 * TODO: Ideally, merged_paths would contain a list of commitables
5075 * we could use so we could skip worktree_status() entirely.
5077 if (merged_paths) {
5078 struct got_pathlist_entry *pe;
5079 if (TAILQ_EMPTY(merged_paths)) {
5080 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5081 goto done;
5083 TAILQ_FOREACH(pe, merged_paths, entry) {
5084 err = worktree_status(worktree, pe->path, fileindex,
5085 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5086 0);
5087 if (err)
5088 goto done;
5090 } else {
5091 err = worktree_status(worktree, "", fileindex, repo,
5092 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5093 if (err)
5094 goto done;
5097 if (TAILQ_EMPTY(&commitable_paths)) {
5098 /* No-op change; commit will be elided. */
5099 err = got_ref_delete(commit_ref, repo);
5100 if (err)
5101 goto done;
5102 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5103 goto done;
5106 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5107 if (err)
5108 goto done;
5110 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5111 if (err)
5112 goto done;
5114 if (new_logmsg) {
5115 logmsg = strdup(new_logmsg);
5116 if (logmsg == NULL) {
5117 err = got_error_from_errno("strdup");
5118 goto done;
5120 } else {
5121 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5122 if (err)
5123 goto done;
5126 /* NB: commit_worktree will call free(logmsg) */
5127 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5128 worktree, got_object_commit_get_author(orig_commit),
5129 got_object_commit_get_committer(orig_commit),
5130 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5131 if (err)
5132 goto done;
5134 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5135 if (err)
5136 goto done;
5138 err = got_ref_delete(commit_ref, repo);
5139 if (err)
5140 goto done;
5142 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5143 fileindex, 0);
5144 sync_err = sync_fileindex(fileindex, fileindex_path);
5145 if (sync_err && err == NULL)
5146 err = sync_err;
5147 done:
5148 free(fileindex_path);
5149 free(head_commit_id);
5150 if (head_ref)
5151 got_ref_close(head_ref);
5152 if (err) {
5153 free(*new_commit_id);
5154 *new_commit_id = NULL;
5156 return err;
5159 const struct got_error *
5160 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5161 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5162 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5163 struct got_commit_object *orig_commit,
5164 struct got_object_id *orig_commit_id, struct got_repository *repo)
5166 const struct got_error *err;
5167 char *commit_ref_name;
5168 struct got_reference *commit_ref = NULL;
5169 struct got_object_id *commit_id = NULL;
5171 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5172 if (err)
5173 return err;
5175 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5176 if (err)
5177 goto done;
5178 err = got_ref_resolve(&commit_id, repo, commit_ref);
5179 if (err)
5180 goto done;
5181 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5182 err = got_error(GOT_ERR_REBASE_COMMITID);
5183 goto done;
5186 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5187 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5188 done:
5189 if (commit_ref)
5190 got_ref_close(commit_ref);
5191 free(commit_ref_name);
5192 free(commit_id);
5193 return err;
5196 const struct got_error *
5197 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5198 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5199 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5200 struct got_commit_object *orig_commit,
5201 struct got_object_id *orig_commit_id, const char *new_logmsg,
5202 struct got_repository *repo)
5204 const struct got_error *err;
5205 char *commit_ref_name;
5206 struct got_reference *commit_ref = NULL;
5207 struct got_object_id *commit_id = NULL;
5209 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5210 if (err)
5211 return err;
5213 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5214 if (err)
5215 goto done;
5216 err = got_ref_resolve(&commit_id, repo, commit_ref);
5217 if (err)
5218 goto done;
5219 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5220 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5221 goto done;
5224 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5225 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5226 done:
5227 if (commit_ref)
5228 got_ref_close(commit_ref);
5229 free(commit_ref_name);
5230 free(commit_id);
5231 return err;
5234 const struct got_error *
5235 got_worktree_rebase_postpone(struct got_worktree *worktree,
5236 struct got_fileindex *fileindex)
5238 if (fileindex)
5239 got_fileindex_free(fileindex);
5240 return lock_worktree(worktree, LOCK_SH);
5243 static const struct got_error *
5244 delete_ref(const char *name, struct got_repository *repo)
5246 const struct got_error *err;
5247 struct got_reference *ref;
5249 err = got_ref_open(&ref, repo, name, 0);
5250 if (err) {
5251 if (err->code == GOT_ERR_NOT_REF)
5252 return NULL;
5253 return err;
5256 err = got_ref_delete(ref, repo);
5257 got_ref_close(ref);
5258 return err;
5261 static const struct got_error *
5262 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5264 const struct got_error *err;
5265 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5266 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5268 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5269 if (err)
5270 goto done;
5271 err = delete_ref(tmp_branch_name, repo);
5272 if (err)
5273 goto done;
5275 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5276 if (err)
5277 goto done;
5278 err = delete_ref(new_base_branch_ref_name, repo);
5279 if (err)
5280 goto done;
5282 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5283 if (err)
5284 goto done;
5285 err = delete_ref(branch_ref_name, repo);
5286 if (err)
5287 goto done;
5289 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5290 if (err)
5291 goto done;
5292 err = delete_ref(commit_ref_name, repo);
5293 if (err)
5294 goto done;
5296 done:
5297 free(tmp_branch_name);
5298 free(new_base_branch_ref_name);
5299 free(branch_ref_name);
5300 free(commit_ref_name);
5301 return err;
5304 const struct got_error *
5305 got_worktree_rebase_complete(struct got_worktree *worktree,
5306 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5307 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5308 struct got_repository *repo)
5310 const struct got_error *err, *unlockerr;
5311 struct got_object_id *new_head_commit_id = NULL;
5313 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5314 if (err)
5315 return err;
5317 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5318 if (err)
5319 goto done;
5321 err = got_ref_write(rebased_branch, repo);
5322 if (err)
5323 goto done;
5325 err = got_worktree_set_head_ref(worktree, rebased_branch);
5326 if (err)
5327 goto done;
5329 err = delete_rebase_refs(worktree, repo);
5330 done:
5331 if (fileindex)
5332 got_fileindex_free(fileindex);
5333 free(new_head_commit_id);
5334 unlockerr = lock_worktree(worktree, LOCK_SH);
5335 if (unlockerr && err == NULL)
5336 err = unlockerr;
5337 return err;
5340 const struct got_error *
5341 got_worktree_rebase_abort(struct got_worktree *worktree,
5342 struct got_fileindex *fileindex, struct got_repository *repo,
5343 struct got_reference *new_base_branch,
5344 got_worktree_checkout_cb progress_cb, void *progress_arg)
5346 const struct got_error *err, *unlockerr, *sync_err;
5347 struct got_reference *resolved = NULL;
5348 struct got_object_id *commit_id = NULL;
5349 char *fileindex_path = NULL;
5350 struct revert_file_args rfa;
5351 struct got_object_id *tree_id = NULL;
5353 err = lock_worktree(worktree, LOCK_EX);
5354 if (err)
5355 return err;
5357 err = got_ref_open(&resolved, repo,
5358 got_ref_get_symref_target(new_base_branch), 0);
5359 if (err)
5360 goto done;
5362 err = got_worktree_set_head_ref(worktree, resolved);
5363 if (err)
5364 goto done;
5367 * XXX commits to the base branch could have happened while
5368 * we were busy rebasing; should we store the original commit ID
5369 * when rebase begins and read it back here?
5371 err = got_ref_resolve(&commit_id, repo, resolved);
5372 if (err)
5373 goto done;
5375 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5376 if (err)
5377 goto done;
5379 err = got_object_id_by_path(&tree_id, repo,
5380 worktree->base_commit_id, worktree->path_prefix);
5381 if (err)
5382 goto done;
5384 err = delete_rebase_refs(worktree, repo);
5385 if (err)
5386 goto done;
5388 err = get_fileindex_path(&fileindex_path, worktree);
5389 if (err)
5390 goto done;
5392 rfa.worktree = worktree;
5393 rfa.fileindex = fileindex;
5394 rfa.progress_cb = progress_cb;
5395 rfa.progress_arg = progress_arg;
5396 rfa.patch_cb = NULL;
5397 rfa.patch_arg = NULL;
5398 rfa.repo = repo;
5399 err = worktree_status(worktree, "", fileindex, repo,
5400 revert_file, &rfa, NULL, NULL, 0, 0);
5401 if (err)
5402 goto sync;
5404 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5405 repo, progress_cb, progress_arg, NULL, NULL);
5406 sync:
5407 sync_err = sync_fileindex(fileindex, fileindex_path);
5408 if (sync_err && err == NULL)
5409 err = sync_err;
5410 done:
5411 got_ref_close(resolved);
5412 free(tree_id);
5413 free(commit_id);
5414 if (fileindex)
5415 got_fileindex_free(fileindex);
5416 free(fileindex_path);
5418 unlockerr = lock_worktree(worktree, LOCK_SH);
5419 if (unlockerr && err == NULL)
5420 err = unlockerr;
5421 return err;
5424 const struct got_error *
5425 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5426 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5427 struct got_fileindex **fileindex, struct got_worktree *worktree,
5428 struct got_repository *repo)
5430 const struct got_error *err = NULL;
5431 char *tmp_branch_name = NULL;
5432 char *branch_ref_name = NULL;
5433 char *base_commit_ref_name = NULL;
5434 char *fileindex_path = NULL;
5435 struct check_rebase_ok_arg ok_arg;
5436 struct got_reference *wt_branch = NULL;
5437 struct got_reference *base_commit_ref = NULL;
5439 *tmp_branch = NULL;
5440 *branch_ref = NULL;
5441 *base_commit_id = NULL;
5442 *fileindex = NULL;
5444 err = lock_worktree(worktree, LOCK_EX);
5445 if (err)
5446 return err;
5448 err = open_fileindex(fileindex, &fileindex_path, worktree);
5449 if (err)
5450 goto done;
5452 ok_arg.worktree = worktree;
5453 ok_arg.repo = repo;
5454 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5455 &ok_arg);
5456 if (err)
5457 goto done;
5459 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5460 if (err)
5461 goto done;
5463 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5464 if (err)
5465 goto done;
5467 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5468 worktree);
5469 if (err)
5470 goto done;
5472 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5473 0);
5474 if (err)
5475 goto done;
5477 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5478 if (err)
5479 goto done;
5481 err = got_ref_write(*branch_ref, repo);
5482 if (err)
5483 goto done;
5485 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5486 worktree->base_commit_id);
5487 if (err)
5488 goto done;
5489 err = got_ref_write(base_commit_ref, repo);
5490 if (err)
5491 goto done;
5492 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5493 if (*base_commit_id == NULL) {
5494 err = got_error_from_errno("got_object_id_dup");
5495 goto done;
5498 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5499 worktree->base_commit_id);
5500 if (err)
5501 goto done;
5502 err = got_ref_write(*tmp_branch, repo);
5503 if (err)
5504 goto done;
5506 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5507 if (err)
5508 goto done;
5509 done:
5510 free(fileindex_path);
5511 free(tmp_branch_name);
5512 free(branch_ref_name);
5513 free(base_commit_ref_name);
5514 if (wt_branch)
5515 got_ref_close(wt_branch);
5516 if (err) {
5517 if (*branch_ref) {
5518 got_ref_close(*branch_ref);
5519 *branch_ref = NULL;
5521 if (*tmp_branch) {
5522 got_ref_close(*tmp_branch);
5523 *tmp_branch = NULL;
5525 free(*base_commit_id);
5526 if (*fileindex) {
5527 got_fileindex_free(*fileindex);
5528 *fileindex = NULL;
5530 lock_worktree(worktree, LOCK_SH);
5532 return err;
5535 const struct got_error *
5536 got_worktree_histedit_postpone(struct got_worktree *worktree,
5537 struct got_fileindex *fileindex)
5539 if (fileindex)
5540 got_fileindex_free(fileindex);
5541 return lock_worktree(worktree, LOCK_SH);
5544 const struct got_error *
5545 got_worktree_histedit_in_progress(int *in_progress,
5546 struct got_worktree *worktree)
5548 const struct got_error *err;
5549 char *tmp_branch_name = NULL;
5551 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5552 if (err)
5553 return err;
5555 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5556 free(tmp_branch_name);
5557 return NULL;
5560 const struct got_error *
5561 got_worktree_histedit_continue(struct got_object_id **commit_id,
5562 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5563 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5564 struct got_worktree *worktree, struct got_repository *repo)
5566 const struct got_error *err;
5567 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5568 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5569 struct got_reference *commit_ref = NULL;
5570 struct got_reference *base_commit_ref = NULL;
5571 char *fileindex_path = NULL;
5572 int have_staged_files = 0;
5574 *commit_id = NULL;
5575 *tmp_branch = NULL;
5576 *base_commit_id = NULL;
5577 *fileindex = NULL;
5579 err = lock_worktree(worktree, LOCK_EX);
5580 if (err)
5581 return err;
5583 err = open_fileindex(fileindex, &fileindex_path, worktree);
5584 if (err)
5585 goto done;
5587 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5588 &have_staged_files);
5589 if (err && err->code != GOT_ERR_CANCELLED)
5590 goto done;
5591 if (have_staged_files) {
5592 err = got_error(GOT_ERR_STAGED_PATHS);
5593 goto done;
5596 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5597 if (err)
5598 goto done;
5600 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5601 if (err)
5602 goto done;
5604 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5605 if (err)
5606 goto done;
5608 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5609 worktree);
5610 if (err)
5611 goto done;
5613 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5614 if (err)
5615 goto done;
5617 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5618 if (err)
5619 goto done;
5620 err = got_ref_resolve(commit_id, repo, commit_ref);
5621 if (err)
5622 goto done;
5624 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5625 if (err)
5626 goto done;
5627 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5628 if (err)
5629 goto done;
5631 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5632 if (err)
5633 goto done;
5634 done:
5635 free(commit_ref_name);
5636 free(branch_ref_name);
5637 free(fileindex_path);
5638 if (commit_ref)
5639 got_ref_close(commit_ref);
5640 if (base_commit_ref)
5641 got_ref_close(base_commit_ref);
5642 if (err) {
5643 free(*commit_id);
5644 *commit_id = NULL;
5645 free(*base_commit_id);
5646 *base_commit_id = NULL;
5647 if (*tmp_branch) {
5648 got_ref_close(*tmp_branch);
5649 *tmp_branch = NULL;
5651 if (*fileindex) {
5652 got_fileindex_free(*fileindex);
5653 *fileindex = NULL;
5655 lock_worktree(worktree, LOCK_EX);
5657 return err;
5660 static const struct got_error *
5661 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5663 const struct got_error *err;
5664 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5665 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5667 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5668 if (err)
5669 goto done;
5670 err = delete_ref(tmp_branch_name, repo);
5671 if (err)
5672 goto done;
5674 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5675 worktree);
5676 if (err)
5677 goto done;
5678 err = delete_ref(base_commit_ref_name, repo);
5679 if (err)
5680 goto done;
5682 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5683 if (err)
5684 goto done;
5685 err = delete_ref(branch_ref_name, repo);
5686 if (err)
5687 goto done;
5689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5690 if (err)
5691 goto done;
5692 err = delete_ref(commit_ref_name, repo);
5693 if (err)
5694 goto done;
5695 done:
5696 free(tmp_branch_name);
5697 free(base_commit_ref_name);
5698 free(branch_ref_name);
5699 free(commit_ref_name);
5700 return err;
5703 const struct got_error *
5704 got_worktree_histedit_abort(struct got_worktree *worktree,
5705 struct got_fileindex *fileindex, struct got_repository *repo,
5706 struct got_reference *branch, struct got_object_id *base_commit_id,
5707 got_worktree_checkout_cb progress_cb, void *progress_arg)
5709 const struct got_error *err, *unlockerr, *sync_err;
5710 struct got_reference *resolved = NULL;
5711 char *fileindex_path = NULL;
5712 struct got_object_id *tree_id = NULL;
5713 struct revert_file_args rfa;
5715 err = lock_worktree(worktree, LOCK_EX);
5716 if (err)
5717 return err;
5719 err = got_ref_open(&resolved, repo,
5720 got_ref_get_symref_target(branch), 0);
5721 if (err)
5722 goto done;
5724 err = got_worktree_set_head_ref(worktree, resolved);
5725 if (err)
5726 goto done;
5728 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5729 if (err)
5730 goto done;
5732 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5733 worktree->path_prefix);
5734 if (err)
5735 goto done;
5737 err = delete_histedit_refs(worktree, repo);
5738 if (err)
5739 goto done;
5741 err = get_fileindex_path(&fileindex_path, worktree);
5742 if (err)
5743 goto done;
5745 rfa.worktree = worktree;
5746 rfa.fileindex = fileindex;
5747 rfa.progress_cb = progress_cb;
5748 rfa.progress_arg = progress_arg;
5749 rfa.patch_cb = NULL;
5750 rfa.patch_arg = NULL;
5751 rfa.repo = repo;
5752 err = worktree_status(worktree, "", fileindex, repo,
5753 revert_file, &rfa, NULL, NULL, 0, 0);
5754 if (err)
5755 goto sync;
5757 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5758 repo, progress_cb, progress_arg, NULL, NULL);
5759 sync:
5760 sync_err = sync_fileindex(fileindex, fileindex_path);
5761 if (sync_err && err == NULL)
5762 err = sync_err;
5763 done:
5764 got_ref_close(resolved);
5765 free(tree_id);
5766 free(fileindex_path);
5768 unlockerr = lock_worktree(worktree, LOCK_SH);
5769 if (unlockerr && err == NULL)
5770 err = unlockerr;
5771 return err;
5774 const struct got_error *
5775 got_worktree_histedit_complete(struct got_worktree *worktree,
5776 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5777 struct got_reference *edited_branch, struct got_repository *repo)
5779 const struct got_error *err, *unlockerr;
5780 struct got_object_id *new_head_commit_id = NULL;
5781 struct got_reference *resolved = NULL;
5783 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5784 if (err)
5785 return err;
5787 err = got_ref_open(&resolved, repo,
5788 got_ref_get_symref_target(edited_branch), 0);
5789 if (err)
5790 goto done;
5792 err = got_ref_change_ref(resolved, new_head_commit_id);
5793 if (err)
5794 goto done;
5796 err = got_ref_write(resolved, repo);
5797 if (err)
5798 goto done;
5800 err = got_worktree_set_head_ref(worktree, resolved);
5801 if (err)
5802 goto done;
5804 err = delete_histedit_refs(worktree, repo);
5805 done:
5806 if (fileindex)
5807 got_fileindex_free(fileindex);
5808 free(new_head_commit_id);
5809 unlockerr = lock_worktree(worktree, LOCK_SH);
5810 if (unlockerr && err == NULL)
5811 err = unlockerr;
5812 return err;
5815 const struct got_error *
5816 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5817 struct got_object_id *commit_id, struct got_repository *repo)
5819 const struct got_error *err;
5820 char *commit_ref_name;
5822 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5823 if (err)
5824 return err;
5826 err = store_commit_id(commit_ref_name, commit_id, repo);
5827 if (err)
5828 goto done;
5830 err = delete_ref(commit_ref_name, repo);
5831 done:
5832 free(commit_ref_name);
5833 return err;
5836 const struct got_error *
5837 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5838 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5839 struct got_worktree *worktree, const char *refname,
5840 struct got_repository *repo)
5842 const struct got_error *err = NULL;
5843 char *fileindex_path = NULL;
5844 struct check_rebase_ok_arg ok_arg;
5846 *fileindex = NULL;
5847 *branch_ref = NULL;
5848 *base_branch_ref = NULL;
5850 err = lock_worktree(worktree, LOCK_EX);
5851 if (err)
5852 return err;
5854 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5855 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5856 "cannot integrate a branch into itself; "
5857 "update -b or different branch name required");
5858 goto done;
5861 err = open_fileindex(fileindex, &fileindex_path, worktree);
5862 if (err)
5863 goto done;
5865 /* Preconditions are the same as for rebase. */
5866 ok_arg.worktree = worktree;
5867 ok_arg.repo = repo;
5868 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5869 &ok_arg);
5870 if (err)
5871 goto done;
5873 err = got_ref_open(branch_ref, repo, refname, 1);
5874 if (err)
5875 goto done;
5877 err = got_ref_open(base_branch_ref, repo,
5878 got_worktree_get_head_ref_name(worktree), 1);
5879 done:
5880 if (err) {
5881 if (*branch_ref) {
5882 got_ref_close(*branch_ref);
5883 *branch_ref = NULL;
5885 if (*base_branch_ref) {
5886 got_ref_close(*base_branch_ref);
5887 *base_branch_ref = NULL;
5889 if (*fileindex) {
5890 got_fileindex_free(*fileindex);
5891 *fileindex = NULL;
5893 lock_worktree(worktree, LOCK_SH);
5895 return err;
5898 const struct got_error *
5899 got_worktree_integrate_continue(struct got_worktree *worktree,
5900 struct got_fileindex *fileindex, struct got_repository *repo,
5901 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5902 got_worktree_checkout_cb progress_cb, void *progress_arg,
5903 got_cancel_cb cancel_cb, void *cancel_arg)
5905 const struct got_error *err = NULL, *sync_err, *unlockerr;
5906 char *fileindex_path = NULL;
5907 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5909 err = get_fileindex_path(&fileindex_path, worktree);
5910 if (err)
5911 goto done;
5913 err = got_ref_resolve(&commit_id, repo, branch_ref);
5914 if (err)
5915 goto done;
5917 err = got_object_id_by_path(&tree_id, repo, commit_id,
5918 worktree->path_prefix);
5919 if (err)
5920 goto done;
5922 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5923 if (err)
5924 goto done;
5926 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5927 progress_cb, progress_arg, cancel_cb, cancel_arg);
5928 if (err)
5929 goto sync;
5931 err = got_ref_change_ref(base_branch_ref, commit_id);
5932 if (err)
5933 goto sync;
5935 err = got_ref_write(base_branch_ref, repo);
5936 sync:
5937 sync_err = sync_fileindex(fileindex, fileindex_path);
5938 if (sync_err && err == NULL)
5939 err = sync_err;
5941 done:
5942 unlockerr = got_ref_unlock(branch_ref);
5943 if (unlockerr && err == NULL)
5944 err = unlockerr;
5945 got_ref_close(branch_ref);
5947 unlockerr = got_ref_unlock(base_branch_ref);
5948 if (unlockerr && err == NULL)
5949 err = unlockerr;
5950 got_ref_close(base_branch_ref);
5952 got_fileindex_free(fileindex);
5953 free(fileindex_path);
5954 free(tree_id);
5956 unlockerr = lock_worktree(worktree, LOCK_SH);
5957 if (unlockerr && err == NULL)
5958 err = unlockerr;
5959 return err;
5962 const struct got_error *
5963 got_worktree_integrate_abort(struct got_worktree *worktree,
5964 struct got_fileindex *fileindex, struct got_repository *repo,
5965 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5967 const struct got_error *err = NULL, *unlockerr = NULL;
5969 got_fileindex_free(fileindex);
5971 err = lock_worktree(worktree, LOCK_SH);
5973 unlockerr = got_ref_unlock(branch_ref);
5974 if (unlockerr && err == NULL)
5975 err = unlockerr;
5976 got_ref_close(branch_ref);
5978 unlockerr = got_ref_unlock(base_branch_ref);
5979 if (unlockerr && err == NULL)
5980 err = unlockerr;
5981 got_ref_close(base_branch_ref);
5983 return err;
5986 struct check_stage_ok_arg {
5987 struct got_object_id *head_commit_id;
5988 struct got_worktree *worktree;
5989 struct got_fileindex *fileindex;
5990 struct got_repository *repo;
5991 int have_changes;
5994 const struct got_error *
5995 check_stage_ok(void *arg, unsigned char status,
5996 unsigned char staged_status, const char *relpath,
5997 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5998 struct got_object_id *commit_id, int dirfd, const char *de_name)
6000 struct check_stage_ok_arg *a = arg;
6001 const struct got_error *err = NULL;
6002 struct got_fileindex_entry *ie;
6003 struct got_object_id base_commit_id;
6004 struct got_object_id *base_commit_idp = NULL;
6005 char *in_repo_path = NULL, *p;
6007 if (status == GOT_STATUS_UNVERSIONED ||
6008 status == GOT_STATUS_NO_CHANGE)
6009 return NULL;
6010 if (status == GOT_STATUS_NONEXISTENT)
6011 return got_error_set_errno(ENOENT, relpath);
6013 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6014 if (ie == NULL)
6015 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6017 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6018 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6019 relpath) == -1)
6020 return got_error_from_errno("asprintf");
6022 if (got_fileindex_entry_has_commit(ie)) {
6023 memcpy(base_commit_id.sha1, ie->commit_sha1,
6024 SHA1_DIGEST_LENGTH);
6025 base_commit_idp = &base_commit_id;
6028 if (status == GOT_STATUS_CONFLICT) {
6029 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6030 goto done;
6031 } else if (status != GOT_STATUS_ADD &&
6032 status != GOT_STATUS_MODIFY &&
6033 status != GOT_STATUS_DELETE) {
6034 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6035 goto done;
6038 a->have_changes = 1;
6040 p = in_repo_path;
6041 while (p[0] == '/')
6042 p++;
6043 err = check_out_of_date(p, status, staged_status,
6044 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6045 GOT_ERR_STAGE_OUT_OF_DATE);
6046 done:
6047 free(in_repo_path);
6048 return err;
6051 struct stage_path_arg {
6052 struct got_worktree *worktree;
6053 struct got_fileindex *fileindex;
6054 struct got_repository *repo;
6055 got_worktree_status_cb status_cb;
6056 void *status_arg;
6057 got_worktree_patch_cb patch_cb;
6058 void *patch_arg;
6059 int staged_something;
6062 static const struct got_error *
6063 stage_path(void *arg, unsigned char status,
6064 unsigned char staged_status, const char *relpath,
6065 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6066 struct got_object_id *commit_id, int dirfd, const char *de_name)
6068 struct stage_path_arg *a = arg;
6069 const struct got_error *err = NULL;
6070 struct got_fileindex_entry *ie;
6071 char *ondisk_path = NULL, *path_content = NULL;
6072 uint32_t stage;
6073 struct got_object_id *new_staged_blob_id = NULL;
6075 if (status == GOT_STATUS_UNVERSIONED)
6076 return NULL;
6078 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6079 if (ie == NULL)
6080 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6082 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6083 relpath)== -1)
6084 return got_error_from_errno("asprintf");
6086 switch (status) {
6087 case GOT_STATUS_ADD:
6088 case GOT_STATUS_MODIFY:
6089 if (a->patch_cb) {
6090 if (status == GOT_STATUS_ADD) {
6091 int choice = GOT_PATCH_CHOICE_NONE;
6092 err = (*a->patch_cb)(&choice, a->patch_arg,
6093 status, ie->path, NULL, 1, 1);
6094 if (err)
6095 break;
6096 if (choice != GOT_PATCH_CHOICE_YES)
6097 break;
6098 } else {
6099 err = create_patched_content(&path_content, 0,
6100 staged_blob_id ? staged_blob_id : blob_id,
6101 ondisk_path, dirfd, de_name, ie->path,
6102 a->repo, a->patch_cb, a->patch_arg);
6103 if (err || path_content == NULL)
6104 break;
6107 err = got_object_blob_create(&new_staged_blob_id,
6108 path_content ? path_content : ondisk_path, a->repo);
6109 if (err)
6110 break;
6111 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6112 SHA1_DIGEST_LENGTH);
6113 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6114 stage = GOT_FILEIDX_STAGE_ADD;
6115 else
6116 stage = GOT_FILEIDX_STAGE_MODIFY;
6117 got_fileindex_entry_stage_set(ie, stage);
6118 a->staged_something = 1;
6119 if (a->status_cb == NULL)
6120 break;
6121 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6122 get_staged_status(ie), relpath, blob_id,
6123 new_staged_blob_id, NULL, dirfd, de_name);
6124 break;
6125 case GOT_STATUS_DELETE:
6126 if (staged_status == GOT_STATUS_DELETE)
6127 break;
6128 if (a->patch_cb) {
6129 int choice = GOT_PATCH_CHOICE_NONE;
6130 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6131 ie->path, NULL, 1, 1);
6132 if (err)
6133 break;
6134 if (choice == GOT_PATCH_CHOICE_NO)
6135 break;
6136 if (choice != GOT_PATCH_CHOICE_YES) {
6137 err = got_error(GOT_ERR_PATCH_CHOICE);
6138 break;
6141 stage = GOT_FILEIDX_STAGE_DELETE;
6142 got_fileindex_entry_stage_set(ie, stage);
6143 a->staged_something = 1;
6144 if (a->status_cb == NULL)
6145 break;
6146 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6147 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6148 de_name);
6149 break;
6150 case GOT_STATUS_NO_CHANGE:
6151 break;
6152 case GOT_STATUS_CONFLICT:
6153 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6154 break;
6155 case GOT_STATUS_NONEXISTENT:
6156 err = got_error_set_errno(ENOENT, relpath);
6157 break;
6158 default:
6159 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6160 break;
6163 if (path_content && unlink(path_content) == -1 && err == NULL)
6164 err = got_error_from_errno2("unlink", path_content);
6165 free(path_content);
6166 free(ondisk_path);
6167 free(new_staged_blob_id);
6168 return err;
6171 const struct got_error *
6172 got_worktree_stage(struct got_worktree *worktree,
6173 struct got_pathlist_head *paths,
6174 got_worktree_status_cb status_cb, void *status_arg,
6175 got_worktree_patch_cb patch_cb, void *patch_arg,
6176 struct got_repository *repo)
6178 const struct got_error *err = NULL, *sync_err, *unlockerr;
6179 struct got_pathlist_entry *pe;
6180 struct got_fileindex *fileindex = NULL;
6181 char *fileindex_path = NULL;
6182 struct got_reference *head_ref = NULL;
6183 struct got_object_id *head_commit_id = NULL;
6184 struct check_stage_ok_arg oka;
6185 struct stage_path_arg spa;
6187 err = lock_worktree(worktree, LOCK_EX);
6188 if (err)
6189 return err;
6191 err = got_ref_open(&head_ref, repo,
6192 got_worktree_get_head_ref_name(worktree), 0);
6193 if (err)
6194 goto done;
6195 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6196 if (err)
6197 goto done;
6198 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6199 if (err)
6200 goto done;
6202 /* Check pre-conditions before staging anything. */
6203 oka.head_commit_id = head_commit_id;
6204 oka.worktree = worktree;
6205 oka.fileindex = fileindex;
6206 oka.repo = repo;
6207 oka.have_changes = 0;
6208 TAILQ_FOREACH(pe, paths, entry) {
6209 err = worktree_status(worktree, pe->path, fileindex, repo,
6210 check_stage_ok, &oka, NULL, NULL, 0, 0);
6211 if (err)
6212 goto done;
6214 if (!oka.have_changes) {
6215 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6216 goto done;
6219 spa.worktree = worktree;
6220 spa.fileindex = fileindex;
6221 spa.repo = repo;
6222 spa.patch_cb = patch_cb;
6223 spa.patch_arg = patch_arg;
6224 spa.status_cb = status_cb;
6225 spa.status_arg = status_arg;
6226 spa.staged_something = 0;
6227 TAILQ_FOREACH(pe, paths, entry) {
6228 err = worktree_status(worktree, pe->path, fileindex, repo,
6229 stage_path, &spa, NULL, NULL, 0, 0);
6230 if (err)
6231 goto done;
6233 if (!spa.staged_something) {
6234 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6235 goto done;
6238 sync_err = sync_fileindex(fileindex, fileindex_path);
6239 if (sync_err && err == NULL)
6240 err = sync_err;
6241 done:
6242 if (head_ref)
6243 got_ref_close(head_ref);
6244 free(head_commit_id);
6245 free(fileindex_path);
6246 if (fileindex)
6247 got_fileindex_free(fileindex);
6248 unlockerr = lock_worktree(worktree, LOCK_SH);
6249 if (unlockerr && err == NULL)
6250 err = unlockerr;
6251 return err;
6254 struct unstage_path_arg {
6255 struct got_worktree *worktree;
6256 struct got_fileindex *fileindex;
6257 struct got_repository *repo;
6258 got_worktree_checkout_cb progress_cb;
6259 void *progress_arg;
6260 got_worktree_patch_cb patch_cb;
6261 void *patch_arg;
6264 static const struct got_error *
6265 create_unstaged_content(char **path_unstaged_content,
6266 char **path_new_staged_content, struct got_object_id *blob_id,
6267 struct got_object_id *staged_blob_id, const char *relpath,
6268 struct got_repository *repo,
6269 got_worktree_patch_cb patch_cb, void *patch_arg)
6271 const struct got_error *err;
6272 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6273 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6274 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6275 struct stat sb1, sb2;
6276 struct got_diff_changes *changes = NULL;
6277 struct got_diff_state *ds = NULL;
6278 struct got_diff_args *args = NULL;
6279 struct got_diff_change *change;
6280 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6281 int have_content = 0, have_rejected_content = 0;
6283 *path_unstaged_content = NULL;
6284 *path_new_staged_content = NULL;
6286 err = got_object_id_str(&label1, blob_id);
6287 if (err)
6288 return err;
6289 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6290 if (err)
6291 goto done;
6293 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6294 if (err)
6295 goto done;
6297 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6298 if (err)
6299 goto done;
6301 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6302 if (err)
6303 goto done;
6305 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6306 if (err)
6307 goto done;
6309 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6310 if (err)
6311 goto done;
6313 if (stat(path1, &sb1) == -1) {
6314 err = got_error_from_errno2("stat", path1);
6315 goto done;
6318 if (stat(path2, &sb2) == -1) {
6319 err = got_error_from_errno2("stat", path2);
6320 goto done;
6323 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6324 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6325 if (err)
6326 goto done;
6328 err = got_opentemp_named(path_unstaged_content, &outfile,
6329 "got-unstaged-content");
6330 if (err)
6331 goto done;
6332 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6333 "got-new-staged-content");
6334 if (err)
6335 goto done;
6337 if (fseek(f1, 0L, SEEK_SET) == -1) {
6338 err = got_ferror(f1, GOT_ERR_IO);
6339 goto done;
6341 if (fseek(f2, 0L, SEEK_SET) == -1) {
6342 err = got_ferror(f2, GOT_ERR_IO);
6343 goto done;
6345 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6346 int choice;
6347 err = apply_or_reject_change(&choice, change, ++n,
6348 changes->nchanges, ds, args, diff_flags, relpath,
6349 f1, f2, &line_cur1, &line_cur2,
6350 outfile, rejectfile, patch_cb, patch_arg);
6351 if (err)
6352 goto done;
6353 if (choice == GOT_PATCH_CHOICE_YES)
6354 have_content = 1;
6355 else
6356 have_rejected_content = 1;
6357 if (choice == GOT_PATCH_CHOICE_QUIT)
6358 break;
6360 if (have_content || have_rejected_content)
6361 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6362 outfile, rejectfile);
6363 done:
6364 free(label1);
6365 if (blob)
6366 got_object_blob_close(blob);
6367 if (staged_blob)
6368 got_object_blob_close(staged_blob);
6369 if (f1 && fclose(f1) == EOF && err == NULL)
6370 err = got_error_from_errno2("fclose", path1);
6371 if (f2 && fclose(f2) == EOF && err == NULL)
6372 err = got_error_from_errno2("fclose", path2);
6373 if (outfile && fclose(outfile) == EOF && err == NULL)
6374 err = got_error_from_errno2("fclose", *path_unstaged_content);
6375 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6376 err = got_error_from_errno2("fclose", *path_new_staged_content);
6377 if (path1 && unlink(path1) == -1 && err == NULL)
6378 err = got_error_from_errno2("unlink", path1);
6379 if (path2 && unlink(path2) == -1 && err == NULL)
6380 err = got_error_from_errno2("unlink", path2);
6381 if (err || !have_content) {
6382 if (*path_unstaged_content &&
6383 unlink(*path_unstaged_content) == -1 && err == NULL)
6384 err = got_error_from_errno2("unlink",
6385 *path_unstaged_content);
6386 free(*path_unstaged_content);
6387 *path_unstaged_content = NULL;
6389 if (err || !have_rejected_content) {
6390 if (*path_new_staged_content &&
6391 unlink(*path_new_staged_content) == -1 && err == NULL)
6392 err = got_error_from_errno2("unlink",
6393 *path_new_staged_content);
6394 free(*path_new_staged_content);
6395 *path_new_staged_content = NULL;
6397 free(args);
6398 if (ds) {
6399 got_diff_state_free(ds);
6400 free(ds);
6402 if (changes)
6403 got_diff_free_changes(changes);
6404 free(path1);
6405 free(path2);
6406 return err;
6409 static const struct got_error *
6410 unstage_path(void *arg, unsigned char status,
6411 unsigned char staged_status, const char *relpath,
6412 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6413 struct got_object_id *commit_id, int dirfd, const char *de_name)
6415 const struct got_error *err = NULL;
6416 struct unstage_path_arg *a = arg;
6417 struct got_fileindex_entry *ie;
6418 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6419 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6420 char *path_new_staged_content = NULL;
6421 char *id_str = NULL, *label_orig = NULL;
6422 int local_changes_subsumed;
6423 struct stat sb;
6425 if (staged_status != GOT_STATUS_ADD &&
6426 staged_status != GOT_STATUS_MODIFY &&
6427 staged_status != GOT_STATUS_DELETE)
6428 return NULL;
6430 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6431 if (ie == NULL)
6432 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6434 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6435 == -1)
6436 return got_error_from_errno("asprintf");
6438 err = got_object_id_str(&id_str,
6439 commit_id ? commit_id : a->worktree->base_commit_id);
6440 if (err)
6441 goto done;
6442 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6443 id_str) == -1) {
6444 err = got_error_from_errno("asprintf");
6445 goto done;
6448 switch (staged_status) {
6449 case GOT_STATUS_MODIFY:
6450 err = got_object_open_as_blob(&blob_base, a->repo,
6451 blob_id, 8192);
6452 if (err)
6453 break;
6454 /* fall through */
6455 case GOT_STATUS_ADD:
6456 if (a->patch_cb) {
6457 if (staged_status == GOT_STATUS_ADD) {
6458 int choice = GOT_PATCH_CHOICE_NONE;
6459 err = (*a->patch_cb)(&choice, a->patch_arg,
6460 staged_status, ie->path, NULL, 1, 1);
6461 if (err)
6462 break;
6463 if (choice != GOT_PATCH_CHOICE_YES)
6464 break;
6465 } else {
6466 err = create_unstaged_content(
6467 &path_unstaged_content,
6468 &path_new_staged_content, blob_id,
6469 staged_blob_id, ie->path, a->repo,
6470 a->patch_cb, a->patch_arg);
6471 if (err || path_unstaged_content == NULL)
6472 break;
6473 if (path_new_staged_content) {
6474 err = got_object_blob_create(
6475 &staged_blob_id,
6476 path_new_staged_content,
6477 a->repo);
6478 if (err)
6479 break;
6480 memcpy(ie->staged_blob_sha1,
6481 staged_blob_id->sha1,
6482 SHA1_DIGEST_LENGTH);
6484 err = merge_file(&local_changes_subsumed,
6485 a->worktree, blob_base, ondisk_path,
6486 relpath, got_fileindex_perms_to_st(ie),
6487 path_unstaged_content, label_orig,
6488 "unstaged", a->repo, a->progress_cb,
6489 a->progress_arg);
6490 if (err == NULL &&
6491 path_new_staged_content == NULL)
6492 got_fileindex_entry_stage_set(ie,
6493 GOT_FILEIDX_STAGE_NONE);
6494 break; /* Done with this file. */
6497 err = got_object_open_as_blob(&blob_staged, a->repo,
6498 staged_blob_id, 8192);
6499 if (err)
6500 break;
6501 err = merge_blob(&local_changes_subsumed, a->worktree,
6502 blob_base, ondisk_path, relpath,
6503 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6504 commit_id ? commit_id : a->worktree->base_commit_id,
6505 a->repo, a->progress_cb, a->progress_arg);
6506 if (err == NULL)
6507 got_fileindex_entry_stage_set(ie,
6508 GOT_FILEIDX_STAGE_NONE);
6509 break;
6510 case GOT_STATUS_DELETE:
6511 if (a->patch_cb) {
6512 int choice = GOT_PATCH_CHOICE_NONE;
6513 err = (*a->patch_cb)(&choice, a->patch_arg,
6514 staged_status, ie->path, NULL, 1, 1);
6515 if (err)
6516 break;
6517 if (choice == GOT_PATCH_CHOICE_NO)
6518 break;
6519 if (choice != GOT_PATCH_CHOICE_YES) {
6520 err = got_error(GOT_ERR_PATCH_CHOICE);
6521 break;
6524 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6525 err = get_file_status(&status, &sb, ie, ondisk_path,
6526 dirfd, de_name, a->repo);
6527 if (err)
6528 break;
6529 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6530 break;
6532 done:
6533 free(ondisk_path);
6534 if (path_unstaged_content &&
6535 unlink(path_unstaged_content) == -1 && err == NULL)
6536 err = got_error_from_errno2("unlink", path_unstaged_content);
6537 if (path_new_staged_content &&
6538 unlink(path_new_staged_content) == -1 && err == NULL)
6539 err = got_error_from_errno2("unlink", path_new_staged_content);
6540 free(path_unstaged_content);
6541 free(path_new_staged_content);
6542 if (blob_base)
6543 got_object_blob_close(blob_base);
6544 if (blob_staged)
6545 got_object_blob_close(blob_staged);
6546 free(id_str);
6547 free(label_orig);
6548 return err;
6551 const struct got_error *
6552 got_worktree_unstage(struct got_worktree *worktree,
6553 struct got_pathlist_head *paths,
6554 got_worktree_checkout_cb progress_cb, void *progress_arg,
6555 got_worktree_patch_cb patch_cb, void *patch_arg,
6556 struct got_repository *repo)
6558 const struct got_error *err = NULL, *sync_err, *unlockerr;
6559 struct got_pathlist_entry *pe;
6560 struct got_fileindex *fileindex = NULL;
6561 char *fileindex_path = NULL;
6562 struct unstage_path_arg upa;
6564 err = lock_worktree(worktree, LOCK_EX);
6565 if (err)
6566 return err;
6568 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6569 if (err)
6570 goto done;
6572 upa.worktree = worktree;
6573 upa.fileindex = fileindex;
6574 upa.repo = repo;
6575 upa.progress_cb = progress_cb;
6576 upa.progress_arg = progress_arg;
6577 upa.patch_cb = patch_cb;
6578 upa.patch_arg = patch_arg;
6579 TAILQ_FOREACH(pe, paths, entry) {
6580 err = worktree_status(worktree, pe->path, fileindex, repo,
6581 unstage_path, &upa, NULL, NULL, 0, 0);
6582 if (err)
6583 goto done;
6586 sync_err = sync_fileindex(fileindex, fileindex_path);
6587 if (sync_err && err == NULL)
6588 err = sync_err;
6589 done:
6590 free(fileindex_path);
6591 if (fileindex)
6592 got_fileindex_free(fileindex);
6593 unlockerr = lock_worktree(worktree, LOCK_SH);
6594 if (unlockerr && err == NULL)
6595 err = unlockerr;
6596 return err;