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 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1131 if (fd == -1 && errno != ENOENT)
1132 return got_error_from_errno2("openat", abspath);
1133 } else {
1134 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1135 if (fd == -1 && errno != ENOENT)
1136 return got_error_from_errno2("open", abspath);
1138 if (fd == -1 || fstat(fd, sb) == -1) {
1139 if (errno == ENOENT) {
1140 if (got_fileindex_entry_has_file_on_disk(ie))
1141 *status = GOT_STATUS_MISSING;
1142 else
1143 *status = GOT_STATUS_DELETE;
1144 goto done;
1146 err = got_error_from_errno2("fstat", abspath);
1147 goto done;
1150 if (!S_ISREG(sb->st_mode)) {
1151 *status = GOT_STATUS_OBSTRUCTED;
1152 goto done;
1155 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1156 *status = GOT_STATUS_DELETE;
1157 goto done;
1158 } else if (!got_fileindex_entry_has_blob(ie) &&
1159 staged_status != GOT_STATUS_ADD) {
1160 *status = GOT_STATUS_ADD;
1161 goto done;
1164 if (!stat_info_differs(ie, sb))
1165 goto done;
1167 if (staged_status == GOT_STATUS_MODIFY ||
1168 staged_status == GOT_STATUS_ADD)
1169 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1170 else
1171 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1173 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1174 if (err)
1175 goto done;
1177 f = fdopen(fd, "r");
1178 if (f == NULL) {
1179 err = got_error_from_errno2("fopen", abspath);
1180 goto done;
1182 fd = -1;
1183 hdrlen = got_object_blob_get_hdrlen(blob);
1184 for (;;) {
1185 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1186 err = got_object_blob_read_block(&blen, blob);
1187 if (err)
1188 goto done;
1189 /* Skip length of blob object header first time around. */
1190 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1191 if (flen == 0 && ferror(f)) {
1192 err = got_error_from_errno("fread");
1193 goto done;
1195 if (blen == 0) {
1196 if (flen != 0)
1197 *status = GOT_STATUS_MODIFY;
1198 break;
1199 } else if (flen == 0) {
1200 if (blen != 0)
1201 *status = GOT_STATUS_MODIFY;
1202 break;
1203 } else if (blen - hdrlen == flen) {
1204 /* Skip blob object header first time around. */
1205 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1206 *status = GOT_STATUS_MODIFY;
1207 break;
1209 } else {
1210 *status = GOT_STATUS_MODIFY;
1211 break;
1213 hdrlen = 0;
1216 if (*status == GOT_STATUS_MODIFY) {
1217 rewind(f);
1218 err = get_modified_file_content_status(status, f);
1219 } else if (xbit_differs(ie, sb->st_mode))
1220 *status = GOT_STATUS_MODE_CHANGE;
1221 done:
1222 if (blob)
1223 got_object_blob_close(blob);
1224 if (f != NULL && fclose(f) == EOF && err == NULL)
1225 err = got_error_from_errno2("fclose", abspath);
1226 if (fd != -1 && close(fd) == -1 && err == NULL)
1227 err = got_error_from_errno2("close", abspath);
1228 return err;
1232 * Update timestamps in the file index if a file is unmodified and
1233 * we had to run a full content comparison to find out.
1235 static const struct got_error *
1236 sync_timestamps(char *ondisk_path, unsigned char status,
1237 struct got_fileindex_entry *ie, struct stat *sb)
1239 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1240 return got_fileindex_entry_update(ie, ondisk_path,
1241 ie->blob_sha1, ie->commit_sha1, 1);
1243 return NULL;
1246 static const struct got_error *
1247 update_blob(struct got_worktree *worktree,
1248 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1249 struct got_tree_entry *te, const char *path,
1250 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1251 void *progress_arg)
1253 const struct got_error *err = NULL;
1254 struct got_blob_object *blob = NULL;
1255 char *ondisk_path;
1256 unsigned char status = GOT_STATUS_NO_CHANGE;
1257 struct stat sb;
1259 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1260 return got_error_from_errno("asprintf");
1262 if (ie) {
1263 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1264 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1265 goto done;
1267 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1268 repo);
1269 if (err)
1270 goto done;
1271 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1272 sb.st_mode = got_fileindex_perms_to_st(ie);
1273 } else
1274 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1276 if (status == GOT_STATUS_OBSTRUCTED) {
1277 err = (*progress_cb)(progress_arg, status, path);
1278 goto done;
1281 if (ie && status != GOT_STATUS_MISSING &&
1282 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1283 if (got_fileindex_entry_has_commit(ie) &&
1284 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1285 SHA1_DIGEST_LENGTH) == 0) {
1286 err = sync_timestamps(ondisk_path, status, ie, &sb);
1287 if (err)
1288 goto done;
1289 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1290 path);
1291 goto done;
1293 if (got_fileindex_entry_has_blob(ie) &&
1294 memcmp(ie->blob_sha1, te->id.sha1,
1295 SHA1_DIGEST_LENGTH) == 0) {
1296 err = sync_timestamps(ondisk_path, status, ie, &sb);
1297 goto done;
1301 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1302 if (err)
1303 goto done;
1305 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1306 int update_timestamps;
1307 struct got_blob_object *blob2 = NULL;
1308 char *label_orig = NULL;
1309 if (got_fileindex_entry_has_blob(ie)) {
1310 struct got_object_id id2;
1311 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1312 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1313 if (err)
1314 goto done;
1316 if (got_fileindex_entry_has_commit(ie)) {
1317 char id_str[SHA1_DIGEST_STRING_LENGTH];
1318 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1319 sizeof(id_str)) == NULL) {
1320 err = got_error_path(id_str,
1321 GOT_ERR_BAD_OBJ_ID_STR);
1322 goto done;
1324 if (asprintf(&label_orig, "%s: commit %s",
1325 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1326 err = got_error_from_errno("asprintf");
1327 goto done;
1330 err = merge_blob(&update_timestamps, worktree, blob2,
1331 ondisk_path, path, sb.st_mode, label_orig, blob,
1332 worktree->base_commit_id, repo,
1333 progress_cb, progress_arg);
1334 free(label_orig);
1335 if (blob2)
1336 got_object_blob_close(blob2);
1337 if (err)
1338 goto done;
1340 * Do not update timestamps of files with local changes.
1341 * Otherwise, a future status walk would treat them as
1342 * unmodified files again.
1344 err = got_fileindex_entry_update(ie, ondisk_path,
1345 blob->id.sha1, worktree->base_commit_id->sha1,
1346 update_timestamps);
1347 } else if (status == GOT_STATUS_MODE_CHANGE) {
1348 err = got_fileindex_entry_update(ie, ondisk_path,
1349 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1350 } else if (status == GOT_STATUS_DELETE) {
1351 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1352 if (err)
1353 goto done;
1354 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1355 ondisk_path, path, blob, 0);
1356 if (err)
1357 goto done;
1358 } else {
1359 err = install_blob(worktree, ondisk_path, path, te->mode,
1360 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1361 repo, progress_cb, progress_arg);
1362 if (err)
1363 goto done;
1364 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1365 ondisk_path, path, blob, 1);
1366 if (err)
1367 goto done;
1369 got_object_blob_close(blob);
1370 done:
1371 free(ondisk_path);
1372 return err;
1375 static const struct got_error *
1376 remove_ondisk_file(const char *root_path, const char *path)
1378 const struct got_error *err = NULL;
1379 char *ondisk_path = NULL;
1381 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1382 return got_error_from_errno("asprintf");
1384 if (unlink(ondisk_path) == -1) {
1385 if (errno != ENOENT)
1386 err = got_error_from_errno2("unlink", ondisk_path);
1387 } else {
1388 char *parent = dirname(ondisk_path);
1389 while (parent && strcmp(parent, root_path) != 0) {
1390 if (rmdir(parent) == -1) {
1391 if (errno != ENOTEMPTY)
1392 err = got_error_from_errno2("rmdir",
1393 parent);
1394 break;
1396 parent = dirname(parent);
1399 free(ondisk_path);
1400 return err;
1403 static const struct got_error *
1404 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1405 struct got_fileindex_entry *ie, struct got_repository *repo,
1406 got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 const struct got_error *err = NULL;
1409 unsigned char status;
1410 struct stat sb;
1411 char *ondisk_path;
1413 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1414 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1416 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1417 == -1)
1418 return got_error_from_errno("asprintf");
1420 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1421 if (err)
1422 return err;
1424 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1425 status == GOT_STATUS_ADD) {
1426 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1427 if (err)
1428 return err;
1430 * Preserve the working file and change the deleted blob's
1431 * entry into a schedule-add entry.
1433 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1434 0);
1435 if (err)
1436 return err;
1437 } else {
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1439 if (err)
1440 return err;
1441 if (status == GOT_STATUS_NO_CHANGE) {
1442 err = remove_ondisk_file(worktree->root_path, ie->path);
1443 if (err)
1444 return err;
1446 got_fileindex_entry_remove(fileindex, ie);
1449 return err;
1452 struct diff_cb_arg {
1453 struct got_fileindex *fileindex;
1454 struct got_worktree *worktree;
1455 struct got_repository *repo;
1456 got_worktree_checkout_cb progress_cb;
1457 void *progress_arg;
1458 got_cancel_cb cancel_cb;
1459 void *cancel_arg;
1462 static const struct got_error *
1463 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1464 struct got_tree_entry *te, const char *parent_path)
1466 struct diff_cb_arg *a = arg;
1468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1469 return got_error(GOT_ERR_CANCELLED);
1471 return update_blob(a->worktree, a->fileindex, ie, te,
1472 ie->path, a->repo, a->progress_cb, a->progress_arg);
1475 static const struct got_error *
1476 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1478 struct diff_cb_arg *a = arg;
1480 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1481 return got_error(GOT_ERR_CANCELLED);
1483 return delete_blob(a->worktree, a->fileindex, ie,
1484 a->repo, a->progress_cb, a->progress_arg);
1487 static const struct got_error *
1488 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1490 struct diff_cb_arg *a = arg;
1491 const struct got_error *err;
1492 char *path;
1494 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1495 return got_error(GOT_ERR_CANCELLED);
1497 if (got_object_tree_entry_is_submodule(te))
1498 return NULL;
1500 if (asprintf(&path, "%s%s%s", parent_path,
1501 parent_path[0] ? "/" : "", te->name)
1502 == -1)
1503 return got_error_from_errno("asprintf");
1505 if (S_ISDIR(te->mode))
1506 err = add_dir_on_disk(a->worktree, path);
1507 else
1508 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1509 a->repo, a->progress_cb, a->progress_arg);
1511 free(path);
1512 return err;
1515 static const struct got_error *
1516 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1518 const struct got_error *err = NULL;
1519 char *uuidstr = NULL;
1520 uint32_t uuid_status;
1522 *refname = NULL;
1524 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1525 if (uuid_status != uuid_s_ok)
1526 return got_error_uuid(uuid_status, "uuid_to_string");
1528 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1529 == -1) {
1530 err = got_error_from_errno("asprintf");
1531 *refname = NULL;
1533 free(uuidstr);
1534 return err;
1537 const struct got_error *
1538 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1540 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1550 static const struct got_error *
1551 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1556 static const struct got_error *
1557 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1559 return get_ref_name(refname, worktree,
1560 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1563 static const struct got_error *
1564 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1570 static const struct got_error *
1571 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1577 static const struct got_error *
1578 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1580 return get_ref_name(refname, worktree,
1581 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1584 static const struct got_error *
1585 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1587 return get_ref_name(refname, worktree,
1588 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1591 static const struct got_error *
1592 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1594 return get_ref_name(refname, worktree,
1595 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1598 const struct got_error *
1599 got_worktree_get_histedit_script_path(char **path,
1600 struct got_worktree *worktree)
1602 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1603 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1604 *path = NULL;
1605 return got_error_from_errno("asprintf");
1607 return NULL;
1611 * Prevent Git's garbage collector from deleting our base commit by
1612 * setting a reference to our base commit's ID.
1614 static const struct got_error *
1615 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_reference *ref = NULL;
1619 char *refname;
1621 err = got_worktree_get_base_ref_name(&refname, worktree);
1622 if (err)
1623 return err;
1625 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1626 if (err)
1627 goto done;
1629 err = got_ref_write(ref, repo);
1630 done:
1631 free(refname);
1632 if (ref)
1633 got_ref_close(ref);
1634 return err;
1637 static const struct got_error *
1638 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1640 const struct got_error *err = NULL;
1642 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1643 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 *fileindex_path = NULL;
1647 return err;
1651 static const struct got_error *
1652 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1653 struct got_worktree *worktree)
1655 const struct got_error *err = NULL;
1656 FILE *index = NULL;
1658 *fileindex_path = NULL;
1659 *fileindex = got_fileindex_alloc();
1660 if (*fileindex == NULL)
1661 return got_error_from_errno("got_fileindex_alloc");
1663 err = get_fileindex_path(fileindex_path, worktree);
1664 if (err)
1665 goto done;
1667 index = fopen(*fileindex_path, "rb");
1668 if (index == NULL) {
1669 if (errno != ENOENT)
1670 err = got_error_from_errno2("fopen", *fileindex_path);
1671 } else {
1672 err = got_fileindex_read(*fileindex, index);
1673 if (fclose(index) != 0 && err == NULL)
1674 err = got_error_from_errno("fclose");
1676 done:
1677 if (err) {
1678 free(*fileindex_path);
1679 *fileindex_path = NULL;
1680 got_fileindex_free(*fileindex);
1681 *fileindex = NULL;
1683 return err;
1686 struct bump_base_commit_id_arg {
1687 struct got_object_id *base_commit_id;
1688 const char *path;
1689 size_t path_len;
1690 const char *entry_name;
1691 got_worktree_checkout_cb progress_cb;
1692 void *progress_arg;
1695 /* Bump base commit ID of all files within an updated part of the work tree. */
1696 static const struct got_error *
1697 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1699 const struct got_error *err;
1700 struct bump_base_commit_id_arg *a = arg;
1702 if (a->entry_name) {
1703 if (strcmp(ie->path, a->path) != 0)
1704 return NULL;
1705 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1706 return NULL;
1708 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1709 SHA1_DIGEST_LENGTH) == 0)
1710 return NULL;
1712 if (a->progress_cb) {
1713 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1714 ie->path);
1715 if (err)
1716 return err;
1718 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1719 return NULL;
1722 static const struct got_error *
1723 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1725 const struct got_error *err = NULL;
1726 char *new_fileindex_path = NULL;
1727 FILE *new_index = NULL;
1729 err = got_opentemp_named(&new_fileindex_path, &new_index,
1730 fileindex_path);
1731 if (err)
1732 goto done;
1734 err = got_fileindex_write(fileindex, new_index);
1735 if (err)
1736 goto done;
1738 if (rename(new_fileindex_path, fileindex_path) != 0) {
1739 err = got_error_from_errno3("rename", new_fileindex_path,
1740 fileindex_path);
1741 unlink(new_fileindex_path);
1743 done:
1744 if (new_index)
1745 fclose(new_index);
1746 free(new_fileindex_path);
1747 return err;
1750 static const struct got_error *
1751 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1752 struct got_object_id **tree_id, const char *wt_relpath,
1753 struct got_worktree *worktree, struct got_repository *repo)
1755 const struct got_error *err = NULL;
1756 struct got_object_id *id = NULL;
1757 char *in_repo_path = NULL;
1758 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1760 *entry_type = GOT_OBJ_TYPE_ANY;
1761 *tree_relpath = NULL;
1762 *tree_id = NULL;
1764 if (wt_relpath[0] == '\0') {
1765 /* Check out all files within the work tree. */
1766 *entry_type = GOT_OBJ_TYPE_TREE;
1767 *tree_relpath = strdup("");
1768 if (*tree_relpath == NULL) {
1769 err = got_error_from_errno("strdup");
1770 goto done;
1772 err = got_object_id_by_path(tree_id, repo,
1773 worktree->base_commit_id, worktree->path_prefix);
1774 if (err)
1775 goto done;
1776 return NULL;
1779 /* Check out a subset of files in the work tree. */
1781 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1782 is_root_wt ? "" : "/", wt_relpath) == -1) {
1783 err = got_error_from_errno("asprintf");
1784 goto done;
1787 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1788 in_repo_path);
1789 if (err)
1790 goto done;
1792 free(in_repo_path);
1793 in_repo_path = NULL;
1795 err = got_object_get_type(entry_type, repo, id);
1796 if (err)
1797 goto done;
1799 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1800 /* Check out a single file. */
1801 if (strchr(wt_relpath, '/') == NULL) {
1802 /* Check out a single file in work tree's root dir. */
1803 in_repo_path = strdup(worktree->path_prefix);
1804 if (in_repo_path == NULL) {
1805 err = got_error_from_errno("strdup");
1806 goto done;
1808 *tree_relpath = strdup("");
1809 if (*tree_relpath == NULL) {
1810 err = got_error_from_errno("strdup");
1811 goto done;
1813 } else {
1814 /* Check out a single file in a subdirectory. */
1815 err = got_path_dirname(tree_relpath, wt_relpath);
1816 if (err)
1817 return err;
1818 if (asprintf(&in_repo_path, "%s%s%s",
1819 worktree->path_prefix, is_root_wt ? "" : "/",
1820 *tree_relpath) == -1) {
1821 err = got_error_from_errno("asprintf");
1822 goto done;
1825 err = got_object_id_by_path(tree_id, repo,
1826 worktree->base_commit_id, in_repo_path);
1827 } else {
1828 /* Check out all files within a subdirectory. */
1829 *tree_id = got_object_id_dup(id);
1830 if (*tree_id == NULL) {
1831 err = got_error_from_errno("got_object_id_dup");
1832 goto done;
1834 *tree_relpath = strdup(wt_relpath);
1835 if (*tree_relpath == NULL) {
1836 err = got_error_from_errno("strdup");
1837 goto done;
1840 done:
1841 free(id);
1842 free(in_repo_path);
1843 if (err) {
1844 *entry_type = GOT_OBJ_TYPE_ANY;
1845 free(*tree_relpath);
1846 *tree_relpath = NULL;
1847 free(*tree_id);
1848 *tree_id = NULL;
1850 return err;
1853 static const struct got_error *
1854 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1855 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1856 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1857 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1859 const struct got_error *err = NULL;
1860 struct got_commit_object *commit = NULL;
1861 struct got_tree_object *tree = NULL;
1862 struct got_fileindex_diff_tree_cb diff_cb;
1863 struct diff_cb_arg arg;
1865 err = ref_base_commit(worktree, repo);
1866 if (err)
1867 goto done;
1869 err = got_object_open_as_commit(&commit, repo,
1870 worktree->base_commit_id);
1871 if (err)
1872 goto done;
1874 err = got_object_open_as_tree(&tree, repo, tree_id);
1875 if (err)
1876 goto done;
1878 if (entry_name &&
1879 got_object_tree_find_entry(tree, entry_name) == NULL) {
1880 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1881 goto done;
1884 diff_cb.diff_old_new = diff_old_new;
1885 diff_cb.diff_old = diff_old;
1886 diff_cb.diff_new = diff_new;
1887 arg.fileindex = fileindex;
1888 arg.worktree = worktree;
1889 arg.repo = repo;
1890 arg.progress_cb = progress_cb;
1891 arg.progress_arg = progress_arg;
1892 arg.cancel_cb = cancel_cb;
1893 arg.cancel_arg = cancel_arg;
1894 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1895 entry_name, repo, &diff_cb, &arg);
1896 done:
1897 if (tree)
1898 got_object_tree_close(tree);
1899 if (commit)
1900 got_object_commit_close(commit);
1901 return err;
1904 const struct got_error *
1905 got_worktree_checkout_files(struct got_worktree *worktree,
1906 struct got_pathlist_head *paths, struct got_repository *repo,
1907 got_worktree_checkout_cb progress_cb, void *progress_arg,
1908 got_cancel_cb cancel_cb, void *cancel_arg)
1910 const struct got_error *err = NULL, *sync_err, *unlockerr;
1911 struct got_commit_object *commit = NULL;
1912 struct got_tree_object *tree = NULL;
1913 struct got_fileindex *fileindex = NULL;
1914 char *fileindex_path = NULL;
1915 struct got_pathlist_entry *pe;
1916 struct tree_path_data {
1917 SIMPLEQ_ENTRY(tree_path_data) entry;
1918 struct got_object_id *tree_id;
1919 int entry_type;
1920 char *relpath;
1921 char *entry_name;
1922 } *tpd = NULL;
1923 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1925 SIMPLEQ_INIT(&tree_paths);
1927 err = lock_worktree(worktree, LOCK_EX);
1928 if (err)
1929 return err;
1931 /* Map all specified paths to in-repository trees. */
1932 TAILQ_FOREACH(pe, paths, entry) {
1933 tpd = malloc(sizeof(*tpd));
1934 if (tpd == NULL) {
1935 err = got_error_from_errno("malloc");
1936 goto done;
1939 err = find_tree_entry_for_checkout(&tpd->entry_type,
1940 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1941 if (err) {
1942 free(tpd);
1943 goto done;
1946 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1947 err = got_path_basename(&tpd->entry_name, pe->path);
1948 if (err) {
1949 free(tpd->relpath);
1950 free(tpd->tree_id);
1951 free(tpd);
1952 goto done;
1954 } else
1955 tpd->entry_name = NULL;
1957 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1961 * Read the file index.
1962 * Checking out files is supposed to be an idempotent operation.
1963 * If the on-disk file index is incomplete we will try to complete it.
1965 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1966 if (err)
1967 goto done;
1969 tpd = SIMPLEQ_FIRST(&tree_paths);
1970 TAILQ_FOREACH(pe, paths, entry) {
1971 struct bump_base_commit_id_arg bbc_arg;
1973 err = checkout_files(worktree, fileindex, tpd->relpath,
1974 tpd->tree_id, tpd->entry_name, repo,
1975 progress_cb, progress_arg, cancel_cb, cancel_arg);
1976 if (err)
1977 break;
1979 bbc_arg.base_commit_id = worktree->base_commit_id;
1980 bbc_arg.entry_name = tpd->entry_name;
1981 bbc_arg.path = pe->path;
1982 bbc_arg.path_len = pe->path_len;
1983 bbc_arg.progress_cb = progress_cb;
1984 bbc_arg.progress_arg = progress_arg;
1985 err = got_fileindex_for_each_entry_safe(fileindex,
1986 bump_base_commit_id, &bbc_arg);
1987 if (err)
1988 break;
1990 tpd = SIMPLEQ_NEXT(tpd, entry);
1992 sync_err = sync_fileindex(fileindex, fileindex_path);
1993 if (sync_err && err == NULL)
1994 err = sync_err;
1995 done:
1996 free(fileindex_path);
1997 if (tree)
1998 got_object_tree_close(tree);
1999 if (commit)
2000 got_object_commit_close(commit);
2001 if (fileindex)
2002 got_fileindex_free(fileindex);
2003 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2004 tpd = SIMPLEQ_FIRST(&tree_paths);
2005 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2006 free(tpd->relpath);
2007 free(tpd->tree_id);
2008 free(tpd);
2010 unlockerr = lock_worktree(worktree, LOCK_SH);
2011 if (unlockerr && err == NULL)
2012 err = unlockerr;
2013 return err;
2016 struct merge_file_cb_arg {
2017 struct got_worktree *worktree;
2018 struct got_fileindex *fileindex;
2019 got_worktree_checkout_cb progress_cb;
2020 void *progress_arg;
2021 got_cancel_cb cancel_cb;
2022 void *cancel_arg;
2023 const char *label_orig;
2024 struct got_object_id *commit_id2;
2027 static const struct got_error *
2028 merge_file_cb(void *arg, struct got_blob_object *blob1,
2029 struct got_blob_object *blob2, struct got_object_id *id1,
2030 struct got_object_id *id2, const char *path1, const char *path2,
2031 mode_t mode1, mode_t mode2, struct got_repository *repo)
2033 static const struct got_error *err = NULL;
2034 struct merge_file_cb_arg *a = arg;
2035 struct got_fileindex_entry *ie;
2036 char *ondisk_path = NULL;
2037 struct stat sb;
2038 unsigned char status;
2039 int local_changes_subsumed;
2041 if (blob1 && blob2) {
2042 ie = got_fileindex_entry_get(a->fileindex, path2,
2043 strlen(path2));
2044 if (ie == NULL)
2045 return (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_MISSING, path2);
2048 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2049 path2) == -1)
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2053 repo);
2054 if (err)
2055 goto done;
2057 if (status == GOT_STATUS_DELETE) {
2058 err = (*a->progress_cb)(a->progress_arg,
2059 GOT_STATUS_MERGE, path2);
2060 goto done;
2062 if (status != GOT_STATUS_NO_CHANGE &&
2063 status != GOT_STATUS_MODIFY &&
2064 status != GOT_STATUS_CONFLICT &&
2065 status != GOT_STATUS_ADD) {
2066 err = (*a->progress_cb)(a->progress_arg, status, path2);
2067 goto done;
2070 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2071 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2072 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2073 } else if (blob1) {
2074 ie = got_fileindex_entry_get(a->fileindex, path1,
2075 strlen(path1));
2076 if (ie == NULL)
2077 return (*a->progress_cb)(a->progress_arg,
2078 GOT_STATUS_MISSING, path2);
2080 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2081 path1) == -1)
2082 return got_error_from_errno("asprintf");
2084 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2085 repo);
2086 if (err)
2087 goto done;
2089 switch (status) {
2090 case GOT_STATUS_NO_CHANGE:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_DELETE, path1);
2093 if (err)
2094 goto done;
2095 err = remove_ondisk_file(a->worktree->root_path, path1);
2096 if (err)
2097 goto done;
2098 if (ie)
2099 got_fileindex_entry_mark_deleted_from_disk(ie);
2100 break;
2101 case GOT_STATUS_DELETE:
2102 case GOT_STATUS_MISSING:
2103 err = (*a->progress_cb)(a->progress_arg,
2104 GOT_STATUS_DELETE, path1);
2105 if (err)
2106 goto done;
2107 if (ie)
2108 got_fileindex_entry_mark_deleted_from_disk(ie);
2109 break;
2110 case GOT_STATUS_ADD:
2111 case GOT_STATUS_MODIFY:
2112 case GOT_STATUS_CONFLICT:
2113 err = (*a->progress_cb)(a->progress_arg,
2114 GOT_STATUS_CANNOT_DELETE, path1);
2115 if (err)
2116 goto done;
2117 break;
2118 case GOT_STATUS_OBSTRUCTED:
2119 err = (*a->progress_cb)(a->progress_arg, status, path1);
2120 if (err)
2121 goto done;
2122 break;
2123 default:
2124 break;
2126 } else if (blob2) {
2127 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2128 path2) == -1)
2129 return got_error_from_errno("asprintf");
2130 ie = got_fileindex_entry_get(a->fileindex, path2,
2131 strlen(path2));
2132 if (ie) {
2133 err = get_file_status(&status, &sb, ie, ondisk_path,
2134 -1, NULL, repo);
2135 if (err)
2136 goto done;
2137 if (status != GOT_STATUS_NO_CHANGE &&
2138 status != GOT_STATUS_MODIFY &&
2139 status != GOT_STATUS_CONFLICT &&
2140 status != GOT_STATUS_ADD) {
2141 err = (*a->progress_cb)(a->progress_arg,
2142 status, path2);
2143 goto done;
2145 err = merge_blob(&local_changes_subsumed, a->worktree,
2146 NULL, ondisk_path, path2, sb.st_mode,
2147 a->label_orig, blob2, a->commit_id2, repo,
2148 a->progress_cb,
2149 a->progress_arg);
2150 if (status == GOT_STATUS_DELETE) {
2151 err = update_blob_fileindex_entry(a->worktree,
2152 a->fileindex, ie, ondisk_path, ie->path,
2153 blob2, 0);
2154 if (err)
2155 goto done;
2157 } else {
2158 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2159 err = install_blob(a->worktree, ondisk_path, path2,
2160 /* XXX get this from parent tree! */
2161 GOT_DEFAULT_FILE_MODE,
2162 sb.st_mode, blob2, 0, 0, repo,
2163 a->progress_cb, a->progress_arg);
2164 if (err)
2165 goto done;
2166 err = got_fileindex_entry_alloc(&ie,
2167 ondisk_path, path2, NULL, NULL);
2168 if (err)
2169 goto done;
2170 err = got_fileindex_entry_add(a->fileindex, ie);
2171 if (err) {
2172 got_fileindex_entry_free(ie);
2173 goto done;
2177 done:
2178 free(ondisk_path);
2179 return err;
2182 struct check_merge_ok_arg {
2183 struct got_worktree *worktree;
2184 struct got_repository *repo;
2187 static const struct got_error *
2188 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2190 const struct got_error *err = NULL;
2191 struct check_merge_ok_arg *a = arg;
2192 unsigned char status;
2193 struct stat sb;
2194 char *ondisk_path;
2196 /* Reject merges into a work tree with mixed base commits. */
2197 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2198 SHA1_DIGEST_LENGTH))
2199 return got_error(GOT_ERR_MIXED_COMMITS);
2201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2202 == -1)
2203 return got_error_from_errno("asprintf");
2205 /* Reject merges into a work tree with conflicted files. */
2206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2207 if (err)
2208 return err;
2209 if (status == GOT_STATUS_CONFLICT)
2210 return got_error(GOT_ERR_CONFLICTS);
2212 return NULL;
2215 static const struct got_error *
2216 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2217 const char *fileindex_path, struct got_object_id *commit_id1,
2218 struct got_object_id *commit_id2, struct got_repository *repo,
2219 got_worktree_checkout_cb progress_cb, void *progress_arg,
2220 got_cancel_cb cancel_cb, void *cancel_arg)
2222 const struct got_error *err = NULL, *sync_err;
2223 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2224 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2225 struct merge_file_cb_arg arg;
2226 char *label_orig = NULL;
2228 if (commit_id1) {
2229 char *id_str;
2231 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2232 worktree->path_prefix);
2233 if (err)
2234 goto done;
2236 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2237 if (err)
2238 goto done;
2240 err = got_object_id_str(&id_str, commit_id1);
2241 if (err)
2242 goto done;
2244 if (asprintf(&label_orig, "%s: commit %s",
2245 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2246 err = got_error_from_errno("asprintf");
2247 free(id_str);
2248 goto done;
2250 free(id_str);
2253 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2254 worktree->path_prefix);
2255 if (err)
2256 goto done;
2258 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2259 if (err)
2260 goto done;
2262 arg.worktree = worktree;
2263 arg.fileindex = fileindex;
2264 arg.progress_cb = progress_cb;
2265 arg.progress_arg = progress_arg;
2266 arg.cancel_cb = cancel_cb;
2267 arg.cancel_arg = cancel_arg;
2268 arg.label_orig = label_orig;
2269 arg.commit_id2 = commit_id2;
2270 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2271 sync_err = sync_fileindex(fileindex, fileindex_path);
2272 if (sync_err && err == NULL)
2273 err = sync_err;
2274 done:
2275 if (tree1)
2276 got_object_tree_close(tree1);
2277 if (tree2)
2278 got_object_tree_close(tree2);
2279 free(label_orig);
2280 return err;
2283 const struct got_error *
2284 got_worktree_merge_files(struct got_worktree *worktree,
2285 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2286 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2287 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2289 const struct got_error *err, *unlockerr;
2290 char *fileindex_path = NULL;
2291 struct got_fileindex *fileindex = NULL;
2292 struct check_merge_ok_arg mok_arg;
2294 err = lock_worktree(worktree, LOCK_EX);
2295 if (err)
2296 return err;
2298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2299 if (err)
2300 goto done;
2302 mok_arg.worktree = worktree;
2303 mok_arg.repo = repo;
2304 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2305 &mok_arg);
2306 if (err)
2307 goto done;
2309 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2310 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2311 done:
2312 if (fileindex)
2313 got_fileindex_free(fileindex);
2314 free(fileindex_path);
2315 unlockerr = lock_worktree(worktree, LOCK_SH);
2316 if (unlockerr && err == NULL)
2317 err = unlockerr;
2318 return err;
2321 struct diff_dir_cb_arg {
2322 struct got_fileindex *fileindex;
2323 struct got_worktree *worktree;
2324 const char *status_path;
2325 size_t status_path_len;
2326 struct got_repository *repo;
2327 got_worktree_status_cb status_cb;
2328 void *status_arg;
2329 got_cancel_cb cancel_cb;
2330 void *cancel_arg;
2331 /* A pathlist containing per-directory pathlists of ignore patterns. */
2332 struct got_pathlist_head ignores;
2333 int report_unchanged;
2336 static const struct got_error *
2337 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2338 int dirfd, const char *de_name,
2339 got_worktree_status_cb status_cb, void *status_arg,
2340 struct got_repository *repo, int report_unchanged)
2342 const struct got_error *err = NULL;
2343 unsigned char status = GOT_STATUS_NO_CHANGE;
2344 unsigned char staged_status = get_staged_status(ie);
2345 struct stat sb;
2346 struct got_object_id blob_id, commit_id, staged_blob_id;
2347 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2348 struct got_object_id *staged_blob_idp = NULL;
2350 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2351 if (err)
2352 return err;
2354 if (status == GOT_STATUS_NO_CHANGE &&
2355 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2356 return NULL;
2358 if (got_fileindex_entry_has_blob(ie)) {
2359 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2360 blob_idp = &blob_id;
2362 if (got_fileindex_entry_has_commit(ie)) {
2363 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2364 commit_idp = &commit_id;
2366 if (staged_status == GOT_STATUS_ADD ||
2367 staged_status == GOT_STATUS_MODIFY) {
2368 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2369 SHA1_DIGEST_LENGTH);
2370 staged_blob_idp = &staged_blob_id;
2373 return (*status_cb)(status_arg, status, staged_status,
2374 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2377 static const struct got_error *
2378 status_old_new(void *arg, struct got_fileindex_entry *ie,
2379 struct dirent *de, const char *parent_path, int dirfd)
2381 const struct got_error *err = NULL;
2382 struct diff_dir_cb_arg *a = arg;
2383 char *abspath;
2385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2386 return got_error(GOT_ERR_CANCELLED);
2388 if (got_path_cmp(parent_path, a->status_path,
2389 strlen(parent_path), a->status_path_len) != 0 &&
2390 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2391 return NULL;
2393 if (parent_path[0]) {
2394 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2395 parent_path, de->d_name) == -1)
2396 return got_error_from_errno("asprintf");
2397 } else {
2398 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2399 de->d_name) == -1)
2400 return got_error_from_errno("asprintf");
2403 err = report_file_status(ie, abspath, dirfd, de->d_name,
2404 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2405 free(abspath);
2406 return err;
2409 static const struct got_error *
2410 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2412 struct diff_dir_cb_arg *a = arg;
2413 struct got_object_id blob_id, commit_id;
2414 unsigned char status;
2416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2417 return got_error(GOT_ERR_CANCELLED);
2419 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2420 return NULL;
2422 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2423 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2424 if (got_fileindex_entry_has_file_on_disk(ie))
2425 status = GOT_STATUS_MISSING;
2426 else
2427 status = GOT_STATUS_DELETE;
2428 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2429 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2432 void
2433 free_ignorelist(struct got_pathlist_head *ignorelist)
2435 struct got_pathlist_entry *pe;
2437 TAILQ_FOREACH(pe, ignorelist, entry)
2438 free((char *)pe->path);
2439 got_pathlist_free(ignorelist);
2442 void
2443 free_ignores(struct got_pathlist_head *ignores)
2445 struct got_pathlist_entry *pe;
2447 TAILQ_FOREACH(pe, ignores, entry) {
2448 struct got_pathlist_head *ignorelist = pe->data;
2449 free_ignorelist(ignorelist);
2450 free((char *)pe->path);
2452 got_pathlist_free(ignores);
2455 static const struct got_error *
2456 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2458 const struct got_error *err = NULL;
2459 struct got_pathlist_entry *pe = NULL;
2460 struct got_pathlist_head *ignorelist;
2461 char *line = NULL, *pattern, *dirpath = NULL;
2462 size_t linesize = 0;
2463 ssize_t linelen;
2465 ignorelist = calloc(1, sizeof(*ignorelist));
2466 if (ignorelist == NULL)
2467 return got_error_from_errno("calloc");
2468 TAILQ_INIT(ignorelist);
2470 while ((linelen = getline(&line, &linesize, f)) != -1) {
2471 if (linelen > 0 && line[linelen - 1] == '\n')
2472 line[linelen - 1] = '\0';
2474 /* Git's ignores may contain comments. */
2475 if (line[0] == '#')
2476 continue;
2478 /* Git's negated patterns are not (yet?) supported. */
2479 if (line[0] == '!')
2480 continue;
2482 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2483 line) == -1) {
2484 err = got_error_from_errno("asprintf");
2485 goto done;
2487 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2488 if (err)
2489 goto done;
2491 if (ferror(f)) {
2492 err = got_error_from_errno("getline");
2493 goto done;
2496 dirpath = strdup(path);
2497 if (dirpath == NULL) {
2498 err = got_error_from_errno("strdup");
2499 goto done;
2501 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2502 done:
2503 free(line);
2504 if (err || pe == NULL) {
2505 free(dirpath);
2506 free_ignorelist(ignorelist);
2508 return err;
2511 int
2512 match_ignores(struct got_pathlist_head *ignores, const char *path)
2514 struct got_pathlist_entry *pe;
2516 /* Handle patterns which match in all directories. */
2517 TAILQ_FOREACH(pe, ignores, entry) {
2518 struct got_pathlist_head *ignorelist = pe->data;
2519 struct got_pathlist_entry *pi;
2521 TAILQ_FOREACH(pi, ignorelist, entry) {
2522 const char *p, *pattern = pi->path;
2524 if (strncmp(pattern, "**/", 3) != 0)
2525 continue;
2526 pattern += 3;
2527 p = path;
2528 while (*p) {
2529 if (fnmatch(pattern, p,
2530 FNM_PATHNAME | FNM_LEADING_DIR)) {
2531 /* Retry in next directory. */
2532 while (*p && *p != '/')
2533 p++;
2534 while (*p == '/')
2535 p++;
2536 continue;
2538 return 1;
2544 * The ignores pathlist contains ignore lists from children before
2545 * parents, so we can find the most specific ignorelist by walking
2546 * ignores backwards.
2548 pe = TAILQ_LAST(ignores, got_pathlist_head);
2549 while (pe) {
2550 if (got_path_is_child(path, pe->path, pe->path_len)) {
2551 struct got_pathlist_head *ignorelist = pe->data;
2552 struct got_pathlist_entry *pi;
2553 TAILQ_FOREACH(pi, ignorelist, entry) {
2554 const char *pattern = pi->path;
2555 int flags = FNM_LEADING_DIR;
2556 if (strstr(pattern, "/**/") == NULL)
2557 flags |= FNM_PATHNAME;
2558 if (fnmatch(pattern, path, flags))
2559 continue;
2560 return 1;
2563 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2566 return 0;
2569 static const struct got_error *
2570 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2571 const char *path, const char *ignores_filename)
2573 const struct got_error *err = NULL;
2574 char *ignorespath;
2575 FILE *ignoresfile = NULL;
2577 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2578 path[0] ? "/" : "", ignores_filename) == -1)
2579 return got_error_from_errno("asprintf");
2581 ignoresfile = fopen(ignorespath, "r");
2582 if (ignoresfile == NULL) {
2583 if (errno != ENOENT && errno != EACCES)
2584 err = got_error_from_errno2("fopen",
2585 ignorespath);
2586 } else
2587 err = read_ignores(ignores, path, ignoresfile);
2589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2590 err = got_error_from_errno2("fclose", path);
2591 free(ignorespath);
2592 return err;
2595 static const struct got_error *
2596 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2598 const struct got_error *err = NULL;
2599 struct diff_dir_cb_arg *a = arg;
2600 char *path = NULL;
2602 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2603 return got_error(GOT_ERR_CANCELLED);
2605 /* XXX ignore symlinks for now */
2606 if (de->d_type == DT_LNK)
2607 return NULL;
2609 if (parent_path[0]) {
2610 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2611 return got_error_from_errno("asprintf");
2612 } else {
2613 path = de->d_name;
2616 if (de->d_type == DT_DIR) {
2617 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2618 ".cvsignore");
2619 if (err == NULL)
2620 err = add_ignores(&a->ignores, a->worktree->root_path,
2621 path, ".gitignore");
2623 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2624 && !match_ignores(&a->ignores, path))
2625 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2626 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2627 if (parent_path[0])
2628 free(path);
2629 return err;
2632 static const struct got_error *
2633 report_single_file_status(const char *path, const char *ondisk_path,
2634 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2635 void *status_arg, struct got_repository *repo, int report_unchanged)
2637 struct got_fileindex_entry *ie;
2638 struct stat sb;
2640 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2641 if (ie)
2642 return report_file_status(ie, ondisk_path, -1, NULL,
2643 status_cb, status_arg, repo, report_unchanged);
2645 if (lstat(ondisk_path, &sb) == -1) {
2646 if (errno != ENOENT)
2647 return got_error_from_errno2("lstat", ondisk_path);
2648 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2649 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2650 return NULL;
2653 if (S_ISREG(sb.st_mode))
2654 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2657 return NULL;
2660 static const struct got_error *
2661 worktree_status(struct got_worktree *worktree, const char *path,
2662 struct got_fileindex *fileindex, struct got_repository *repo,
2663 got_worktree_status_cb status_cb, void *status_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2665 int report_unchanged)
2667 const struct got_error *err = NULL;
2668 int fd = -1;
2669 struct got_fileindex_diff_dir_cb fdiff_cb;
2670 struct diff_dir_cb_arg arg;
2671 char *ondisk_path = NULL;
2673 if (asprintf(&ondisk_path, "%s%s%s",
2674 worktree->root_path, path[0] ? "/" : "", path) == -1)
2675 return got_error_from_errno("asprintf");
2677 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2678 if (fd == -1) {
2679 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2680 err = got_error_from_errno2("open", ondisk_path);
2681 else
2682 err = report_single_file_status(path, ondisk_path,
2683 fileindex, status_cb, status_arg, repo,
2684 report_unchanged);
2685 } else {
2686 fdiff_cb.diff_old_new = status_old_new;
2687 fdiff_cb.diff_old = status_old;
2688 fdiff_cb.diff_new = status_new;
2689 arg.fileindex = fileindex;
2690 arg.worktree = worktree;
2691 arg.status_path = path;
2692 arg.status_path_len = strlen(path);
2693 arg.repo = repo;
2694 arg.status_cb = status_cb;
2695 arg.status_arg = status_arg;
2696 arg.cancel_cb = cancel_cb;
2697 arg.cancel_arg = cancel_arg;
2698 arg.report_unchanged = report_unchanged;
2699 TAILQ_INIT(&arg.ignores);
2700 if (!no_ignores) {
2701 err = add_ignores(&arg.ignores, worktree->root_path,
2702 path, ".cvsignore");
2703 if (err == NULL)
2704 err = add_ignores(&arg.ignores,
2705 worktree->root_path, path, ".gitignore");
2707 if (err == NULL)
2708 err = got_fileindex_diff_dir(fileindex, fd,
2709 worktree->root_path, path, repo, &fdiff_cb, &arg);
2710 free_ignores(&arg.ignores);
2713 if (fd != -1 && close(fd) != 0 && err == NULL)
2714 err = got_error_from_errno("close");
2715 free(ondisk_path);
2716 return err;
2719 const struct got_error *
2720 got_worktree_status(struct got_worktree *worktree,
2721 struct got_pathlist_head *paths, struct got_repository *repo,
2722 got_worktree_status_cb status_cb, void *status_arg,
2723 got_cancel_cb cancel_cb, void *cancel_arg)
2725 const struct got_error *err = NULL;
2726 char *fileindex_path = NULL;
2727 struct got_fileindex *fileindex = NULL;
2728 struct got_pathlist_entry *pe;
2730 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2731 if (err)
2732 return err;
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 err = worktree_status(worktree, pe->path, fileindex, repo,
2736 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2737 if (err)
2738 break;
2740 free(fileindex_path);
2741 got_fileindex_free(fileindex);
2742 return err;
2745 const struct got_error *
2746 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2747 const char *arg)
2749 const struct got_error *err = NULL;
2750 char *resolved, *cwd = NULL, *path = NULL;
2751 size_t len;
2753 *wt_path = NULL;
2755 resolved = realpath(arg, NULL);
2756 if (resolved == NULL) {
2757 if (errno != ENOENT)
2758 return got_error_from_errno2("realpath", arg);
2759 cwd = getcwd(NULL, 0);
2760 if (cwd == NULL)
2761 return got_error_from_errno("getcwd");
2762 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2763 err = got_error_from_errno("asprintf");
2764 goto done;
2768 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2769 strlen(got_worktree_get_root_path(worktree)))) {
2770 err = got_error(GOT_ERR_BAD_PATH);
2771 goto done;
2774 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2775 err = got_path_skip_common_ancestor(&path,
2776 got_worktree_get_root_path(worktree), resolved);
2777 if (err)
2778 goto done;
2779 } else {
2780 path = strdup("");
2781 if (path == NULL) {
2782 err = got_error_from_errno("strdup");
2783 goto done;
2787 /* XXX status walk can't deal with trailing slash! */
2788 len = strlen(path);
2789 while (len > 0 && path[len - 1] == '/') {
2790 path[len - 1] = '\0';
2791 len--;
2793 done:
2794 free(resolved);
2795 free(cwd);
2796 if (err == NULL)
2797 *wt_path = path;
2798 else
2799 free(path);
2800 return err;
2803 struct schedule_addition_args {
2804 struct got_worktree *worktree;
2805 struct got_fileindex *fileindex;
2806 got_worktree_checkout_cb progress_cb;
2807 void *progress_arg;
2808 struct got_repository *repo;
2811 static const struct got_error *
2812 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2813 const char *relpath, struct got_object_id *blob_id,
2814 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2815 int dirfd, const char *de_name)
2817 struct schedule_addition_args *a = arg;
2818 const struct got_error *err = NULL;
2819 struct got_fileindex_entry *ie;
2820 struct stat sb;
2821 char *ondisk_path;
2823 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2824 relpath) == -1)
2825 return got_error_from_errno("asprintf");
2827 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2828 if (ie) {
2829 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2830 de_name, a->repo);
2831 if (err)
2832 goto done;
2833 /* Re-adding an existing entry is a no-op. */
2834 if (status == GOT_STATUS_ADD)
2835 goto done;
2836 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2837 if (err)
2838 goto done;
2841 if (status != GOT_STATUS_UNVERSIONED) {
2842 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2843 goto done;
2846 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2847 if (err)
2848 goto done;
2850 err = got_fileindex_entry_add(a->fileindex, ie);
2851 if (err) {
2852 got_fileindex_entry_free(ie);
2853 goto done;
2855 done:
2856 free(ondisk_path);
2857 if (err)
2858 return err;
2859 if (status == GOT_STATUS_ADD)
2860 return NULL;
2861 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2864 const struct got_error *
2865 got_worktree_schedule_add(struct got_worktree *worktree,
2866 struct got_pathlist_head *paths,
2867 got_worktree_checkout_cb progress_cb, void *progress_arg,
2868 struct got_repository *repo, int no_ignores)
2870 struct got_fileindex *fileindex = NULL;
2871 char *fileindex_path = NULL;
2872 const struct got_error *err = NULL, *sync_err, *unlockerr;
2873 struct got_pathlist_entry *pe;
2874 struct schedule_addition_args saa;
2876 err = lock_worktree(worktree, LOCK_EX);
2877 if (err)
2878 return err;
2880 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2881 if (err)
2882 goto done;
2884 saa.worktree = worktree;
2885 saa.fileindex = fileindex;
2886 saa.progress_cb = progress_cb;
2887 saa.progress_arg = progress_arg;
2888 saa.repo = repo;
2890 TAILQ_FOREACH(pe, paths, entry) {
2891 err = worktree_status(worktree, pe->path, fileindex, repo,
2892 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2893 if (err)
2894 break;
2896 sync_err = sync_fileindex(fileindex, fileindex_path);
2897 if (sync_err && err == NULL)
2898 err = sync_err;
2899 done:
2900 free(fileindex_path);
2901 if (fileindex)
2902 got_fileindex_free(fileindex);
2903 unlockerr = lock_worktree(worktree, LOCK_SH);
2904 if (unlockerr && err == NULL)
2905 err = unlockerr;
2906 return err;
2909 struct schedule_deletion_args {
2910 struct got_worktree *worktree;
2911 struct got_fileindex *fileindex;
2912 got_worktree_delete_cb progress_cb;
2913 void *progress_arg;
2914 struct got_repository *repo;
2915 int delete_local_mods;
2918 static const struct got_error *
2919 schedule_for_deletion(void *arg, unsigned char status,
2920 unsigned char staged_status, const char *relpath,
2921 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2922 struct got_object_id *commit_id, int dirfd, const char *de_name)
2924 struct schedule_deletion_args *a = arg;
2925 const struct got_error *err = NULL;
2926 struct got_fileindex_entry *ie = NULL;
2927 struct stat sb;
2928 char *ondisk_path;
2930 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2931 if (ie == NULL)
2932 return got_error(GOT_ERR_BAD_PATH);
2934 staged_status = get_staged_status(ie);
2935 if (staged_status != GOT_STATUS_NO_CHANGE) {
2936 if (staged_status == GOT_STATUS_DELETE)
2937 return NULL;
2938 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2941 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2942 relpath) == -1)
2943 return got_error_from_errno("asprintf");
2945 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2946 a->repo);
2947 if (err)
2948 goto done;
2950 if (status != GOT_STATUS_NO_CHANGE) {
2951 if (status == GOT_STATUS_DELETE)
2952 goto done;
2953 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2954 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2955 goto done;
2957 if (status != GOT_STATUS_MODIFY &&
2958 status != GOT_STATUS_MISSING) {
2959 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2960 goto done;
2964 if (status != GOT_STATUS_MISSING) {
2965 if (dirfd != -1) {
2966 if (unlinkat(dirfd, de_name, 0) != 0) {
2967 err = got_error_from_errno2("unlinkat",
2968 ondisk_path);
2969 goto done;
2971 } else if (unlink(ondisk_path) != 0) {
2972 err = got_error_from_errno2("unlink", ondisk_path);
2973 goto done;
2977 got_fileindex_entry_mark_deleted_from_disk(ie);
2978 done:
2979 free(ondisk_path);
2980 if (err)
2981 return err;
2982 if (status == GOT_STATUS_DELETE)
2983 return NULL;
2984 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2985 staged_status, relpath);
2988 const struct got_error *
2989 got_worktree_schedule_delete(struct got_worktree *worktree,
2990 struct got_pathlist_head *paths, int delete_local_mods,
2991 got_worktree_delete_cb progress_cb, void *progress_arg,
2992 struct got_repository *repo)
2994 struct got_fileindex *fileindex = NULL;
2995 char *fileindex_path = NULL;
2996 const struct got_error *err = NULL, *sync_err, *unlockerr;
2997 struct got_pathlist_entry *pe;
2998 struct schedule_deletion_args sda;
3000 err = lock_worktree(worktree, LOCK_EX);
3001 if (err)
3002 return err;
3004 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3005 if (err)
3006 goto done;
3008 sda.worktree = worktree;
3009 sda.fileindex = fileindex;
3010 sda.progress_cb = progress_cb;
3011 sda.progress_arg = progress_arg;
3012 sda.repo = repo;
3013 sda.delete_local_mods = delete_local_mods;
3015 TAILQ_FOREACH(pe, paths, entry) {
3016 err = worktree_status(worktree, pe->path, fileindex, repo,
3017 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3018 if (err)
3019 break;
3021 sync_err = sync_fileindex(fileindex, fileindex_path);
3022 if (sync_err && err == NULL)
3023 err = sync_err;
3024 done:
3025 free(fileindex_path);
3026 if (fileindex)
3027 got_fileindex_free(fileindex);
3028 unlockerr = lock_worktree(worktree, LOCK_SH);
3029 if (unlockerr && err == NULL)
3030 err = unlockerr;
3031 return err;
3034 static const struct got_error *
3035 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3037 const struct got_error *err = NULL;
3038 char *line = NULL;
3039 size_t linesize = 0, n;
3040 ssize_t linelen;
3042 linelen = getline(&line, &linesize, infile);
3043 if (linelen == -1) {
3044 if (ferror(infile)) {
3045 err = got_error_from_errno("getline");
3046 goto done;
3048 return NULL;
3050 if (outfile) {
3051 n = fwrite(line, 1, linelen, outfile);
3052 if (n != linelen) {
3053 err = got_ferror(outfile, GOT_ERR_IO);
3054 goto done;
3057 if (rejectfile) {
3058 n = fwrite(line, 1, linelen, rejectfile);
3059 if (n != linelen)
3060 err = got_ferror(outfile, GOT_ERR_IO);
3062 done:
3063 free(line);
3064 return err;
3067 static const struct got_error *
3068 skip_one_line(FILE *f)
3070 char *line = NULL;
3071 size_t linesize = 0;
3072 ssize_t linelen;
3074 linelen = getline(&line, &linesize, f);
3075 if (linelen == -1) {
3076 if (ferror(f))
3077 return got_error_from_errno("getline");
3078 return NULL;
3080 free(line);
3081 return NULL;
3084 static const struct got_error *
3085 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3086 int start_old, int end_old, int start_new, int end_new,
3087 FILE *outfile, FILE *rejectfile)
3089 const struct got_error *err;
3091 /* Copy old file's lines leading up to patch. */
3092 while (!feof(f1) && *line_cur1 < start_old) {
3093 err = copy_one_line(f1, outfile, NULL);
3094 if (err)
3095 return err;
3096 (*line_cur1)++;
3098 /* Skip new file's lines leading up to patch. */
3099 while (!feof(f2) && *line_cur2 < start_new) {
3100 if (rejectfile)
3101 err = copy_one_line(f2, NULL, rejectfile);
3102 else
3103 err = skip_one_line(f2);
3104 if (err)
3105 return err;
3106 (*line_cur2)++;
3108 /* Copy patched lines. */
3109 while (!feof(f2) && *line_cur2 <= end_new) {
3110 err = copy_one_line(f2, outfile, NULL);
3111 if (err)
3112 return err;
3113 (*line_cur2)++;
3115 /* Skip over old file's replaced lines. */
3116 while (!feof(f1) && *line_cur1 <= end_old) {
3117 if (rejectfile)
3118 err = copy_one_line(f1, NULL, rejectfile);
3119 else
3120 err = skip_one_line(f1);
3121 if (err)
3122 return err;
3123 (*line_cur1)++;
3126 return NULL;
3129 static const struct got_error *
3130 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3131 FILE *outfile, FILE *rejectfile)
3133 const struct got_error *err;
3135 if (outfile) {
3136 /* Copy old file's lines until EOF. */
3137 while (!feof(f1)) {
3138 err = copy_one_line(f1, outfile, NULL);
3139 if (err)
3140 return err;
3141 (*line_cur1)++;
3144 if (rejectfile) {
3145 /* Copy new file's lines until EOF. */
3146 while (!feof(f2)) {
3147 err = copy_one_line(f2, NULL, rejectfile);
3148 if (err)
3149 return err;
3150 (*line_cur2)++;
3154 return NULL;
3157 static const struct got_error *
3158 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3159 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3160 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3161 int *line_cur2, FILE *outfile, FILE *rejectfile,
3162 got_worktree_patch_cb patch_cb, void *patch_arg)
3164 const struct got_error *err = NULL;
3165 int start_old = change->cv.a;
3166 int end_old = change->cv.b;
3167 int start_new = change->cv.c;
3168 int end_new = change->cv.d;
3169 long pos1, pos2;
3170 FILE *hunkfile;
3172 *choice = GOT_PATCH_CHOICE_NONE;
3174 hunkfile = got_opentemp();
3175 if (hunkfile == NULL)
3176 return got_error_from_errno("got_opentemp");
3178 pos1 = ftell(f1);
3179 pos2 = ftell(f2);
3181 /* XXX TODO needs error checking */
3182 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3184 if (fseek(f1, pos1, SEEK_SET) == -1) {
3185 err = got_ferror(f1, GOT_ERR_IO);
3186 goto done;
3188 if (fseek(f2, pos2, SEEK_SET) == -1) {
3189 err = got_ferror(f1, GOT_ERR_IO);
3190 goto done;
3192 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3193 err = got_ferror(hunkfile, GOT_ERR_IO);
3194 goto done;
3197 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3198 hunkfile, n, nchanges);
3199 if (err)
3200 goto done;
3202 switch (*choice) {
3203 case GOT_PATCH_CHOICE_YES:
3204 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3205 end_old, start_new, end_new, outfile, rejectfile);
3206 break;
3207 case GOT_PATCH_CHOICE_NO:
3208 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3209 end_old, start_new, end_new, rejectfile, outfile);
3210 break;
3211 case GOT_PATCH_CHOICE_QUIT:
3212 break;
3213 default:
3214 err = got_error(GOT_ERR_PATCH_CHOICE);
3215 break;
3217 done:
3218 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3219 err = got_error_from_errno("fclose");
3220 return err;
3223 struct revert_file_args {
3224 struct got_worktree *worktree;
3225 struct got_fileindex *fileindex;
3226 got_worktree_checkout_cb progress_cb;
3227 void *progress_arg;
3228 got_worktree_patch_cb patch_cb;
3229 void *patch_arg;
3230 struct got_repository *repo;
3233 static const struct got_error *
3234 create_patched_content(char **path_outfile, int reverse_patch,
3235 struct got_object_id *blob_id, const char *path2,
3236 int dirfd2, const char *de_name2,
3237 const char *relpath, struct got_repository *repo,
3238 got_worktree_patch_cb patch_cb, void *patch_arg)
3240 const struct got_error *err;
3241 struct got_blob_object *blob = NULL;
3242 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3243 int fd2 = -1;
3244 char *path1 = NULL, *id_str = NULL;
3245 struct stat sb1, sb2;
3246 struct got_diff_changes *changes = NULL;
3247 struct got_diff_state *ds = NULL;
3248 struct got_diff_args *args = NULL;
3249 struct got_diff_change *change;
3250 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3251 int n = 0;
3253 *path_outfile = NULL;
3255 err = got_object_id_str(&id_str, blob_id);
3256 if (err)
3257 return err;
3259 if (dirfd2 != -1) {
3260 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3261 if (fd2 == -1) {
3262 err = got_error_from_errno2("openat", path2);
3263 goto done;
3265 } else {
3266 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3267 if (fd2 == -1) {
3268 err = got_error_from_errno2("open", path2);
3269 goto done;
3272 if (fstat(fd2, &sb2) == -1) {
3273 err = got_error_from_errno2("fstat", path2);
3274 goto done;
3277 f2 = fdopen(fd2, "r");
3278 if (f2 == NULL) {
3279 err = got_error_from_errno2("fopen", path2);
3280 goto done;
3282 fd2 = -1;
3284 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3285 if (err)
3286 goto done;
3288 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3289 if (err)
3290 goto done;
3292 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3293 if (err)
3294 goto done;
3296 if (stat(path1, &sb1) == -1) {
3297 err = got_error_from_errno2("stat", path1);
3298 goto done;
3301 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3302 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3303 if (err)
3304 goto done;
3306 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3307 if (err)
3308 goto done;
3310 if (fseek(f1, 0L, SEEK_SET) == -1)
3311 return got_ferror(f1, GOT_ERR_IO);
3312 if (fseek(f2, 0L, SEEK_SET) == -1)
3313 return got_ferror(f2, GOT_ERR_IO);
3314 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3315 int choice;
3316 err = apply_or_reject_change(&choice, change, ++n,
3317 changes->nchanges, ds, args, diff_flags, relpath,
3318 f1, f2, &line_cur1, &line_cur2,
3319 reverse_patch ? NULL : outfile,
3320 reverse_patch ? outfile : NULL,
3321 patch_cb, patch_arg);
3322 if (err)
3323 goto done;
3324 if (choice == GOT_PATCH_CHOICE_YES)
3325 have_content = 1;
3326 else if (choice == GOT_PATCH_CHOICE_QUIT)
3327 break;
3329 if (have_content) {
3330 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3331 reverse_patch ? NULL : outfile,
3332 reverse_patch ? outfile : NULL);
3333 if (err)
3334 goto done;
3336 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3337 err = got_error_from_errno2("chmod", path2);
3338 goto done;
3341 done:
3342 free(id_str);
3343 if (blob)
3344 got_object_blob_close(blob);
3345 if (f1 && fclose(f1) == EOF && err == NULL)
3346 err = got_error_from_errno2("fclose", path1);
3347 if (f2 && fclose(f2) == EOF && err == NULL)
3348 err = got_error_from_errno2("fclose", path2);
3349 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3350 err = got_error_from_errno2("close", path2);
3351 if (outfile && fclose(outfile) == EOF && err == NULL)
3352 err = got_error_from_errno2("fclose", *path_outfile);
3353 if (path1 && unlink(path1) == -1 && err == NULL)
3354 err = got_error_from_errno2("unlink", path1);
3355 if (err || !have_content) {
3356 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3357 err = got_error_from_errno2("unlink", *path_outfile);
3358 free(*path_outfile);
3359 *path_outfile = NULL;
3361 free(args);
3362 if (ds) {
3363 got_diff_state_free(ds);
3364 free(ds);
3366 if (changes)
3367 got_diff_free_changes(changes);
3368 free(path1);
3369 return err;
3372 static const struct got_error *
3373 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3374 const char *relpath, struct got_object_id *blob_id,
3375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3376 int dirfd, const char *de_name)
3378 struct revert_file_args *a = arg;
3379 const struct got_error *err = NULL;
3380 char *parent_path = NULL;
3381 struct got_fileindex_entry *ie;
3382 struct got_tree_object *tree = NULL;
3383 struct got_object_id *tree_id = NULL;
3384 const struct got_tree_entry *te = NULL;
3385 char *tree_path = NULL, *te_name;
3386 char *ondisk_path = NULL, *path_content = NULL;
3387 struct got_blob_object *blob = NULL;
3389 /* Reverting a staged deletion is a no-op. */
3390 if (status == GOT_STATUS_DELETE &&
3391 staged_status != GOT_STATUS_NO_CHANGE)
3392 return NULL;
3394 if (status == GOT_STATUS_UNVERSIONED)
3395 return (*a->progress_cb)(a->progress_arg,
3396 GOT_STATUS_UNVERSIONED, relpath);
3398 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3399 if (ie == NULL)
3400 return got_error(GOT_ERR_BAD_PATH);
3402 /* Construct in-repository path of tree which contains this blob. */
3403 err = got_path_dirname(&parent_path, ie->path);
3404 if (err) {
3405 if (err->code != GOT_ERR_BAD_PATH)
3406 goto done;
3407 parent_path = strdup("/");
3408 if (parent_path == NULL) {
3409 err = got_error_from_errno("strdup");
3410 goto done;
3413 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3414 tree_path = strdup(parent_path);
3415 if (tree_path == NULL) {
3416 err = got_error_from_errno("strdup");
3417 goto done;
3419 } else {
3420 if (got_path_is_root_dir(parent_path)) {
3421 tree_path = strdup(a->worktree->path_prefix);
3422 if (tree_path == NULL) {
3423 err = got_error_from_errno("strdup");
3424 goto done;
3426 } else {
3427 if (asprintf(&tree_path, "%s/%s",
3428 a->worktree->path_prefix, parent_path) == -1) {
3429 err = got_error_from_errno("asprintf");
3430 goto done;
3435 err = got_object_id_by_path(&tree_id, a->repo,
3436 a->worktree->base_commit_id, tree_path);
3437 if (err) {
3438 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3439 (status == GOT_STATUS_ADD ||
3440 staged_status == GOT_STATUS_ADD)))
3441 goto done;
3442 } else {
3443 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3444 if (err)
3445 goto done;
3447 te_name = basename(ie->path);
3448 if (te_name == NULL) {
3449 err = got_error_from_errno2("basename", ie->path);
3450 goto done;
3453 te = got_object_tree_find_entry(tree, te_name);
3454 if (te == NULL && status != GOT_STATUS_ADD &&
3455 staged_status != GOT_STATUS_ADD) {
3456 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3457 goto done;
3461 switch (status) {
3462 case GOT_STATUS_ADD:
3463 if (a->patch_cb) {
3464 int choice = GOT_PATCH_CHOICE_NONE;
3465 err = (*a->patch_cb)(&choice, a->patch_arg,
3466 status, ie->path, NULL, 1, 1);
3467 if (err)
3468 goto done;
3469 if (choice != GOT_PATCH_CHOICE_YES)
3470 break;
3472 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3473 ie->path);
3474 if (err)
3475 goto done;
3476 got_fileindex_entry_remove(a->fileindex, ie);
3477 break;
3478 case GOT_STATUS_DELETE:
3479 if (a->patch_cb) {
3480 int choice = GOT_PATCH_CHOICE_NONE;
3481 err = (*a->patch_cb)(&choice, a->patch_arg,
3482 status, ie->path, NULL, 1, 1);
3483 if (err)
3484 goto done;
3485 if (choice != GOT_PATCH_CHOICE_YES)
3486 break;
3488 /* fall through */
3489 case GOT_STATUS_MODIFY:
3490 case GOT_STATUS_MODE_CHANGE:
3491 case GOT_STATUS_CONFLICT:
3492 case GOT_STATUS_MISSING: {
3493 struct got_object_id id;
3494 if (staged_status == GOT_STATUS_ADD ||
3495 staged_status == GOT_STATUS_MODIFY) {
3496 memcpy(id.sha1, ie->staged_blob_sha1,
3497 SHA1_DIGEST_LENGTH);
3498 } else
3499 memcpy(id.sha1, ie->blob_sha1,
3500 SHA1_DIGEST_LENGTH);
3501 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3502 if (err)
3503 goto done;
3505 if (asprintf(&ondisk_path, "%s/%s",
3506 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3507 err = got_error_from_errno("asprintf");
3508 goto done;
3511 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3512 status == GOT_STATUS_CONFLICT)) {
3513 err = create_patched_content(&path_content, 1, &id,
3514 ondisk_path, dirfd, de_name, ie->path, a->repo,
3515 a->patch_cb, a->patch_arg);
3516 if (err || path_content == NULL)
3517 break;
3518 if (rename(path_content, ondisk_path) == -1) {
3519 err = got_error_from_errno3("rename",
3520 path_content, ondisk_path);
3521 goto done;
3523 } else {
3524 err = install_blob(a->worktree, ondisk_path, ie->path,
3525 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3526 got_fileindex_perms_to_st(ie), blob, 0, 1,
3527 a->repo, a->progress_cb, a->progress_arg);
3528 if (err)
3529 goto done;
3530 if (status == GOT_STATUS_DELETE ||
3531 status == GOT_STATUS_MODE_CHANGE) {
3532 err = update_blob_fileindex_entry(a->worktree,
3533 a->fileindex, ie, ondisk_path, ie->path,
3534 blob, 1);
3535 if (err)
3536 goto done;
3539 break;
3541 default:
3542 break;
3544 done:
3545 free(ondisk_path);
3546 free(path_content);
3547 free(parent_path);
3548 free(tree_path);
3549 if (blob)
3550 got_object_blob_close(blob);
3551 if (tree)
3552 got_object_tree_close(tree);
3553 free(tree_id);
3554 return err;
3557 const struct got_error *
3558 got_worktree_revert(struct got_worktree *worktree,
3559 struct got_pathlist_head *paths,
3560 got_worktree_checkout_cb progress_cb, void *progress_arg,
3561 got_worktree_patch_cb patch_cb, void *patch_arg,
3562 struct got_repository *repo)
3564 struct got_fileindex *fileindex = NULL;
3565 char *fileindex_path = NULL;
3566 const struct got_error *err = NULL, *unlockerr = NULL;
3567 const struct got_error *sync_err = NULL;
3568 struct got_pathlist_entry *pe;
3569 struct revert_file_args rfa;
3571 err = lock_worktree(worktree, LOCK_EX);
3572 if (err)
3573 return err;
3575 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3576 if (err)
3577 goto done;
3579 rfa.worktree = worktree;
3580 rfa.fileindex = fileindex;
3581 rfa.progress_cb = progress_cb;
3582 rfa.progress_arg = progress_arg;
3583 rfa.patch_cb = patch_cb;
3584 rfa.patch_arg = patch_arg;
3585 rfa.repo = repo;
3586 TAILQ_FOREACH(pe, paths, entry) {
3587 err = worktree_status(worktree, pe->path, fileindex, repo,
3588 revert_file, &rfa, NULL, NULL, 0, 0);
3589 if (err)
3590 break;
3592 sync_err = sync_fileindex(fileindex, fileindex_path);
3593 if (sync_err && err == NULL)
3594 err = sync_err;
3595 done:
3596 free(fileindex_path);
3597 if (fileindex)
3598 got_fileindex_free(fileindex);
3599 unlockerr = lock_worktree(worktree, LOCK_SH);
3600 if (unlockerr && err == NULL)
3601 err = unlockerr;
3602 return err;
3605 static void
3606 free_commitable(struct got_commitable *ct)
3608 free(ct->path);
3609 free(ct->in_repo_path);
3610 free(ct->ondisk_path);
3611 free(ct->blob_id);
3612 free(ct->base_blob_id);
3613 free(ct->staged_blob_id);
3614 free(ct->base_commit_id);
3615 free(ct);
3618 struct collect_commitables_arg {
3619 struct got_pathlist_head *commitable_paths;
3620 struct got_repository *repo;
3621 struct got_worktree *worktree;
3622 int have_staged_files;
3625 static const struct got_error *
3626 collect_commitables(void *arg, unsigned char status,
3627 unsigned char staged_status, const char *relpath,
3628 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3629 struct got_object_id *commit_id, int dirfd, const char *de_name)
3631 struct collect_commitables_arg *a = arg;
3632 const struct got_error *err = NULL;
3633 struct got_commitable *ct = NULL;
3634 struct got_pathlist_entry *new = NULL;
3635 char *parent_path = NULL, *path = NULL;
3636 struct stat sb;
3638 if (a->have_staged_files) {
3639 if (staged_status != GOT_STATUS_MODIFY &&
3640 staged_status != GOT_STATUS_ADD &&
3641 staged_status != GOT_STATUS_DELETE)
3642 return NULL;
3643 } else {
3644 if (status == GOT_STATUS_CONFLICT)
3645 return got_error(GOT_ERR_COMMIT_CONFLICT);
3647 if (status != GOT_STATUS_MODIFY &&
3648 status != GOT_STATUS_MODE_CHANGE &&
3649 status != GOT_STATUS_ADD &&
3650 status != GOT_STATUS_DELETE)
3651 return NULL;
3654 if (asprintf(&path, "/%s", relpath) == -1) {
3655 err = got_error_from_errno("asprintf");
3656 goto done;
3658 if (strcmp(path, "/") == 0) {
3659 parent_path = strdup("");
3660 if (parent_path == NULL)
3661 return got_error_from_errno("strdup");
3662 } else {
3663 err = got_path_dirname(&parent_path, path);
3664 if (err)
3665 return err;
3668 ct = calloc(1, sizeof(*ct));
3669 if (ct == NULL) {
3670 err = got_error_from_errno("calloc");
3671 goto done;
3674 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3675 relpath) == -1) {
3676 err = got_error_from_errno("asprintf");
3677 goto done;
3679 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3680 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3681 } else {
3682 if (dirfd != -1) {
3683 if (fstatat(dirfd, de_name, &sb,
3684 AT_SYMLINK_NOFOLLOW) == -1) {
3685 err = got_error_from_errno2("lstat",
3686 ct->ondisk_path);
3687 goto done;
3689 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3690 err = got_error_from_errno2("lstat", ct->ondisk_path);
3691 goto done;
3693 ct->mode = sb.st_mode;
3696 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3697 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3698 relpath) == -1) {
3699 err = got_error_from_errno("asprintf");
3700 goto done;
3703 ct->status = status;
3704 ct->staged_status = staged_status;
3705 ct->blob_id = NULL; /* will be filled in when blob gets created */
3706 if (ct->status != GOT_STATUS_ADD &&
3707 ct->staged_status != GOT_STATUS_ADD) {
3708 ct->base_blob_id = got_object_id_dup(blob_id);
3709 if (ct->base_blob_id == NULL) {
3710 err = got_error_from_errno("got_object_id_dup");
3711 goto done;
3713 ct->base_commit_id = got_object_id_dup(commit_id);
3714 if (ct->base_commit_id == NULL) {
3715 err = got_error_from_errno("got_object_id_dup");
3716 goto done;
3719 if (ct->staged_status == GOT_STATUS_ADD ||
3720 ct->staged_status == GOT_STATUS_MODIFY) {
3721 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3722 if (ct->staged_blob_id == NULL) {
3723 err = got_error_from_errno("got_object_id_dup");
3724 goto done;
3727 ct->path = strdup(path);
3728 if (ct->path == NULL) {
3729 err = got_error_from_errno("strdup");
3730 goto done;
3732 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3733 done:
3734 if (ct && (err || new == NULL))
3735 free_commitable(ct);
3736 free(parent_path);
3737 free(path);
3738 return err;
3741 static const struct got_error *write_tree(struct got_object_id **,
3742 struct got_tree_object *, const char *, struct got_pathlist_head *,
3743 got_worktree_status_cb status_cb, void *status_arg,
3744 struct got_repository *);
3746 static const struct got_error *
3747 write_subtree(struct got_object_id **new_subtree_id,
3748 struct got_tree_entry *te, const char *parent_path,
3749 struct got_pathlist_head *commitable_paths,
3750 got_worktree_status_cb status_cb, void *status_arg,
3751 struct got_repository *repo)
3753 const struct got_error *err = NULL;
3754 struct got_tree_object *subtree;
3755 char *subpath;
3757 if (asprintf(&subpath, "%s%s%s", parent_path,
3758 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3759 return got_error_from_errno("asprintf");
3761 err = got_object_open_as_tree(&subtree, repo, &te->id);
3762 if (err)
3763 return err;
3765 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3766 status_cb, status_arg, repo);
3767 got_object_tree_close(subtree);
3768 free(subpath);
3769 return err;
3772 static const struct got_error *
3773 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3775 const struct got_error *err = NULL;
3776 char *ct_parent_path = NULL;
3778 *match = 0;
3780 if (strchr(ct->in_repo_path, '/') == NULL) {
3781 *match = got_path_is_root_dir(path);
3782 return NULL;
3785 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3786 if (err)
3787 return err;
3788 *match = (strcmp(path, ct_parent_path) == 0);
3789 free(ct_parent_path);
3790 return err;
3793 static mode_t
3794 get_ct_file_mode(struct got_commitable *ct)
3796 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3799 static const struct got_error *
3800 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3801 struct got_tree_entry *te, struct got_commitable *ct)
3803 const struct got_error *err = NULL;
3805 *new_te = NULL;
3807 err = got_object_tree_entry_dup(new_te, te);
3808 if (err)
3809 goto done;
3811 (*new_te)->mode = get_ct_file_mode(ct);
3813 if (ct->staged_status == GOT_STATUS_MODIFY)
3814 memcpy(&(*new_te)->id, ct->staged_blob_id,
3815 sizeof((*new_te)->id));
3816 else
3817 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3818 done:
3819 if (err && *new_te) {
3820 free(*new_te);
3821 *new_te = NULL;
3823 return err;
3826 static const struct got_error *
3827 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3828 struct got_commitable *ct)
3830 const struct got_error *err = NULL;
3831 char *ct_name;
3833 *new_te = NULL;
3835 *new_te = calloc(1, sizeof(**new_te));
3836 if (*new_te == NULL)
3837 return got_error_from_errno("calloc");
3839 ct_name = basename(ct->path);
3840 if (ct_name == NULL) {
3841 err = got_error_from_errno2("basename", ct->path);
3842 goto done;
3844 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3845 sizeof((*new_te)->name)) {
3846 err = got_error(GOT_ERR_NO_SPACE);
3847 goto done;
3850 (*new_te)->mode = get_ct_file_mode(ct);
3852 if (ct->staged_status == GOT_STATUS_ADD)
3853 memcpy(&(*new_te)->id, ct->staged_blob_id,
3854 sizeof((*new_te)->id));
3855 else
3856 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3857 done:
3858 if (err && *new_te) {
3859 free(*new_te);
3860 *new_te = NULL;
3862 return err;
3865 static const struct got_error *
3866 insert_tree_entry(struct got_tree_entry *new_te,
3867 struct got_pathlist_head *paths)
3869 const struct got_error *err = NULL;
3870 struct got_pathlist_entry *new_pe;
3872 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3873 if (err)
3874 return err;
3875 if (new_pe == NULL)
3876 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3877 return NULL;
3880 static const struct got_error *
3881 report_ct_status(struct got_commitable *ct,
3882 got_worktree_status_cb status_cb, void *status_arg)
3884 const char *ct_path = ct->path;
3885 unsigned char status;
3887 while (ct_path[0] == '/')
3888 ct_path++;
3890 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3891 status = ct->staged_status;
3892 else
3893 status = ct->status;
3895 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3896 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3899 static const struct got_error *
3900 match_modified_subtree(int *modified, struct got_tree_entry *te,
3901 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3903 const struct got_error *err = NULL;
3904 struct got_pathlist_entry *pe;
3905 char *te_path;
3907 *modified = 0;
3909 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3910 got_path_is_root_dir(base_tree_path) ? "" : "/",
3911 te->name) == -1)
3912 return got_error_from_errno("asprintf");
3914 TAILQ_FOREACH(pe, commitable_paths, entry) {
3915 struct got_commitable *ct = pe->data;
3916 *modified = got_path_is_child(ct->in_repo_path, te_path,
3917 strlen(te_path));
3918 if (*modified)
3919 break;
3922 free(te_path);
3923 return err;
3926 static const struct got_error *
3927 match_deleted_or_modified_ct(struct got_commitable **ctp,
3928 struct got_tree_entry *te, const char *base_tree_path,
3929 struct got_pathlist_head *commitable_paths)
3931 const struct got_error *err = NULL;
3932 struct got_pathlist_entry *pe;
3934 *ctp = NULL;
3936 TAILQ_FOREACH(pe, commitable_paths, entry) {
3937 struct got_commitable *ct = pe->data;
3938 char *ct_name = NULL;
3939 int path_matches;
3941 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3942 if (ct->status != GOT_STATUS_MODIFY &&
3943 ct->status != GOT_STATUS_MODE_CHANGE &&
3944 ct->status != GOT_STATUS_DELETE)
3945 continue;
3946 } else {
3947 if (ct->staged_status != GOT_STATUS_MODIFY &&
3948 ct->staged_status != GOT_STATUS_DELETE)
3949 continue;
3952 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3953 continue;
3955 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3956 if (err)
3957 return err;
3958 if (!path_matches)
3959 continue;
3961 ct_name = basename(pe->path);
3962 if (ct_name == NULL)
3963 return got_error_from_errno2("basename", pe->path);
3965 if (strcmp(te->name, ct_name) != 0)
3966 continue;
3968 *ctp = ct;
3969 break;
3972 return err;
3975 static const struct got_error *
3976 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3977 const char *child_path, const char *path_base_tree,
3978 struct got_pathlist_head *commitable_paths,
3979 got_worktree_status_cb status_cb, void *status_arg,
3980 struct got_repository *repo)
3982 const struct got_error *err = NULL;
3983 struct got_tree_entry *new_te;
3984 char *subtree_path;
3985 struct got_object_id *id = NULL;
3987 *new_tep = NULL;
3989 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3990 got_path_is_root_dir(path_base_tree) ? "" : "/",
3991 child_path) == -1)
3992 return got_error_from_errno("asprintf");
3994 new_te = calloc(1, sizeof(*new_te));
3995 if (new_te == NULL)
3996 return got_error_from_errno("calloc");
3997 new_te->mode = S_IFDIR;
3999 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4000 sizeof(new_te->name)) {
4001 err = got_error(GOT_ERR_NO_SPACE);
4002 goto done;
4004 err = write_tree(&id, NULL, subtree_path,
4005 commitable_paths, status_cb, status_arg, repo);
4006 if (err) {
4007 free(new_te);
4008 goto done;
4010 memcpy(&new_te->id, id, sizeof(new_te->id));
4011 done:
4012 free(id);
4013 free(subtree_path);
4014 if (err == NULL)
4015 *new_tep = new_te;
4016 return err;
4019 static const struct got_error *
4020 write_tree(struct got_object_id **new_tree_id,
4021 struct got_tree_object *base_tree, const char *path_base_tree,
4022 struct got_pathlist_head *commitable_paths,
4023 got_worktree_status_cb status_cb, void *status_arg,
4024 struct got_repository *repo)
4026 const struct got_error *err = NULL;
4027 struct got_pathlist_head paths;
4028 struct got_tree_entry *te, *new_te = NULL;
4029 struct got_pathlist_entry *pe;
4030 int nentries = 0;
4032 TAILQ_INIT(&paths);
4034 /* Insert, and recurse into, newly added entries first. */
4035 TAILQ_FOREACH(pe, commitable_paths, entry) {
4036 struct got_commitable *ct = pe->data;
4037 char *child_path = NULL, *slash;
4039 if ((ct->status != GOT_STATUS_ADD &&
4040 ct->staged_status != GOT_STATUS_ADD) ||
4041 (ct->flags & GOT_COMMITABLE_ADDED))
4042 continue;
4044 if (!got_path_is_child(pe->path, path_base_tree,
4045 strlen(path_base_tree)))
4046 continue;
4048 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4049 pe->path);
4050 if (err)
4051 goto done;
4053 slash = strchr(child_path, '/');
4054 if (slash == NULL) {
4055 err = alloc_added_blob_tree_entry(&new_te, ct);
4056 if (err)
4057 goto done;
4058 err = report_ct_status(ct, status_cb, status_arg);
4059 if (err)
4060 goto done;
4061 ct->flags |= GOT_COMMITABLE_ADDED;
4062 err = insert_tree_entry(new_te, &paths);
4063 if (err)
4064 goto done;
4065 nentries++;
4066 } else {
4067 *slash = '\0'; /* trim trailing path components */
4068 if (base_tree == NULL ||
4069 got_object_tree_find_entry(base_tree, child_path)
4070 == NULL) {
4071 err = make_subtree_for_added_blob(&new_te,
4072 child_path, path_base_tree,
4073 commitable_paths, status_cb, status_arg,
4074 repo);
4075 if (err)
4076 goto done;
4077 err = insert_tree_entry(new_te, &paths);
4078 if (err)
4079 goto done;
4080 nentries++;
4085 if (base_tree) {
4086 int i, nbase_entries;
4087 /* Handle modified and deleted entries. */
4088 nbase_entries = got_object_tree_get_nentries(base_tree);
4089 for (i = 0; i < nbase_entries; i++) {
4090 struct got_commitable *ct = NULL;
4092 te = got_object_tree_get_entry(base_tree, i);
4093 if (got_object_tree_entry_is_submodule(te)) {
4094 /* Entry is a submodule; just copy it. */
4095 err = got_object_tree_entry_dup(&new_te, te);
4096 if (err)
4097 goto done;
4098 err = insert_tree_entry(new_te, &paths);
4099 if (err)
4100 goto done;
4101 nentries++;
4102 continue;
4105 if (S_ISDIR(te->mode)) {
4106 int modified;
4107 err = got_object_tree_entry_dup(&new_te, te);
4108 if (err)
4109 goto done;
4110 err = match_modified_subtree(&modified, te,
4111 path_base_tree, commitable_paths);
4112 if (err)
4113 goto done;
4114 /* Avoid recursion into unmodified subtrees. */
4115 if (modified) {
4116 struct got_object_id *new_id;
4117 err = write_subtree(&new_id, te,
4118 path_base_tree, commitable_paths,
4119 status_cb, status_arg, repo);
4120 if (err)
4121 goto done;
4122 memcpy(&new_te->id, new_id,
4123 sizeof(new_te->id));
4124 free(new_id);
4126 err = insert_tree_entry(new_te, &paths);
4127 if (err)
4128 goto done;
4129 nentries++;
4130 continue;
4133 err = match_deleted_or_modified_ct(&ct, te,
4134 path_base_tree, commitable_paths);
4135 if (err)
4136 goto done;
4137 if (ct) {
4138 /* NB: Deleted entries get dropped here. */
4139 if (ct->status == GOT_STATUS_MODIFY ||
4140 ct->status == GOT_STATUS_MODE_CHANGE ||
4141 ct->staged_status == GOT_STATUS_MODIFY) {
4142 err = alloc_modified_blob_tree_entry(
4143 &new_te, te, ct);
4144 if (err)
4145 goto done;
4146 err = insert_tree_entry(new_te, &paths);
4147 if (err)
4148 goto done;
4149 nentries++;
4151 err = report_ct_status(ct, status_cb,
4152 status_arg);
4153 if (err)
4154 goto done;
4155 } else {
4156 /* Entry is unchanged; just copy it. */
4157 err = got_object_tree_entry_dup(&new_te, te);
4158 if (err)
4159 goto done;
4160 err = insert_tree_entry(new_te, &paths);
4161 if (err)
4162 goto done;
4163 nentries++;
4168 /* Write new list of entries; deleted entries have been dropped. */
4169 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4170 done:
4171 got_pathlist_free(&paths);
4172 return err;
4175 static const struct got_error *
4176 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4177 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4178 int have_staged_files)
4180 const struct got_error *err = NULL;
4181 struct got_pathlist_entry *pe;
4183 TAILQ_FOREACH(pe, commitable_paths, entry) {
4184 struct got_fileindex_entry *ie;
4185 struct got_commitable *ct = pe->data;
4187 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4188 if (ie) {
4189 if (ct->status == GOT_STATUS_DELETE ||
4190 ct->staged_status == GOT_STATUS_DELETE) {
4191 got_fileindex_entry_remove(fileindex, ie);
4192 got_fileindex_entry_free(ie);
4193 } else if (ct->staged_status == GOT_STATUS_ADD ||
4194 ct->staged_status == GOT_STATUS_MODIFY) {
4195 got_fileindex_entry_stage_set(ie,
4196 GOT_FILEIDX_STAGE_NONE);
4197 err = got_fileindex_entry_update(ie,
4198 ct->ondisk_path, ct->staged_blob_id->sha1,
4199 new_base_commit_id->sha1,
4200 !have_staged_files);
4201 } else
4202 err = got_fileindex_entry_update(ie,
4203 ct->ondisk_path, ct->blob_id->sha1,
4204 new_base_commit_id->sha1,
4205 !have_staged_files);
4206 } else {
4207 err = got_fileindex_entry_alloc(&ie,
4208 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4209 new_base_commit_id->sha1);
4210 if (err)
4211 break;
4212 err = got_fileindex_entry_add(fileindex, ie);
4213 if (err)
4214 break;
4217 return err;
4221 static const struct got_error *
4222 check_out_of_date(const char *in_repo_path, unsigned char status,
4223 unsigned char staged_status, struct got_object_id *base_blob_id,
4224 struct got_object_id *base_commit_id,
4225 struct got_object_id *head_commit_id, struct got_repository *repo,
4226 int ood_errcode)
4228 const struct got_error *err = NULL;
4229 struct got_object_id *id = NULL;
4231 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4232 /* Trivial case: base commit == head commit */
4233 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4234 return NULL;
4236 * Ensure file content which local changes were based
4237 * on matches file content in the branch head.
4239 err = got_object_id_by_path(&id, repo, head_commit_id,
4240 in_repo_path);
4241 if (err) {
4242 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4243 err = got_error(ood_errcode);
4244 goto done;
4245 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4246 err = got_error(ood_errcode);
4247 } else {
4248 /* Require that added files don't exist in the branch head. */
4249 err = got_object_id_by_path(&id, repo, head_commit_id,
4250 in_repo_path);
4251 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4252 goto done;
4253 err = id ? got_error(ood_errcode) : NULL;
4255 done:
4256 free(id);
4257 return err;
4260 const struct got_error *
4261 commit_worktree(struct got_object_id **new_commit_id,
4262 struct got_pathlist_head *commitable_paths,
4263 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4264 const char *author, const char *committer,
4265 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4266 got_worktree_status_cb status_cb, void *status_arg,
4267 struct got_repository *repo)
4269 const struct got_error *err = NULL, *unlockerr = NULL;
4270 struct got_pathlist_entry *pe;
4271 const char *head_ref_name = NULL;
4272 struct got_commit_object *head_commit = NULL;
4273 struct got_reference *head_ref2 = NULL;
4274 struct got_object_id *head_commit_id2 = NULL;
4275 struct got_tree_object *head_tree = NULL;
4276 struct got_object_id *new_tree_id = NULL;
4277 struct got_object_id_queue parent_ids;
4278 struct got_object_qid *pid = NULL;
4279 char *logmsg = NULL;
4281 *new_commit_id = NULL;
4283 SIMPLEQ_INIT(&parent_ids);
4285 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4286 if (err)
4287 goto done;
4289 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4290 if (err)
4291 goto done;
4293 if (commit_msg_cb != NULL) {
4294 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4295 if (err)
4296 goto done;
4299 if (logmsg == NULL || strlen(logmsg) == 0) {
4300 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4301 goto done;
4304 /* Create blobs from added and modified files and record their IDs. */
4305 TAILQ_FOREACH(pe, commitable_paths, entry) {
4306 struct got_commitable *ct = pe->data;
4307 char *ondisk_path;
4309 /* Blobs for staged files already exist. */
4310 if (ct->staged_status == GOT_STATUS_ADD ||
4311 ct->staged_status == GOT_STATUS_MODIFY)
4312 continue;
4314 if (ct->status != GOT_STATUS_ADD &&
4315 ct->status != GOT_STATUS_MODIFY &&
4316 ct->status != GOT_STATUS_MODE_CHANGE)
4317 continue;
4319 if (asprintf(&ondisk_path, "%s/%s",
4320 worktree->root_path, pe->path) == -1) {
4321 err = got_error_from_errno("asprintf");
4322 goto done;
4324 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4325 free(ondisk_path);
4326 if (err)
4327 goto done;
4330 /* Recursively write new tree objects. */
4331 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4332 status_cb, status_arg, repo);
4333 if (err)
4334 goto done;
4336 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4337 if (err)
4338 goto done;
4339 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4340 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4341 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4342 got_object_qid_free(pid);
4343 if (logmsg != NULL)
4344 free(logmsg);
4345 if (err)
4346 goto done;
4348 /* Check if a concurrent commit to our branch has occurred. */
4349 head_ref_name = got_worktree_get_head_ref_name(worktree);
4350 if (head_ref_name == NULL) {
4351 err = got_error_from_errno("got_worktree_get_head_ref_name");
4352 goto done;
4354 /* Lock the reference here to prevent concurrent modification. */
4355 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4356 if (err)
4357 goto done;
4358 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4359 if (err)
4360 goto done;
4361 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4362 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4363 goto done;
4365 /* Update branch head in repository. */
4366 err = got_ref_change_ref(head_ref2, *new_commit_id);
4367 if (err)
4368 goto done;
4369 err = got_ref_write(head_ref2, repo);
4370 if (err)
4371 goto done;
4373 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4374 if (err)
4375 goto done;
4377 err = ref_base_commit(worktree, repo);
4378 if (err)
4379 goto done;
4380 done:
4381 if (head_tree)
4382 got_object_tree_close(head_tree);
4383 if (head_commit)
4384 got_object_commit_close(head_commit);
4385 free(head_commit_id2);
4386 if (head_ref2) {
4387 unlockerr = got_ref_unlock(head_ref2);
4388 if (unlockerr && err == NULL)
4389 err = unlockerr;
4390 got_ref_close(head_ref2);
4392 return err;
4395 static const struct got_error *
4396 check_path_is_commitable(const char *path,
4397 struct got_pathlist_head *commitable_paths)
4399 struct got_pathlist_entry *cpe = NULL;
4400 size_t path_len = strlen(path);
4402 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4403 struct got_commitable *ct = cpe->data;
4404 const char *ct_path = ct->path;
4406 while (ct_path[0] == '/')
4407 ct_path++;
4409 if (strcmp(path, ct_path) == 0 ||
4410 got_path_is_child(ct_path, path, path_len))
4411 break;
4414 if (cpe == NULL)
4415 return got_error_path(path, GOT_ERR_BAD_PATH);
4417 return NULL;
4420 static const struct got_error *
4421 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4423 int *have_staged_files = arg;
4425 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4426 *have_staged_files = 1;
4427 return got_error(GOT_ERR_CANCELLED);
4430 return NULL;
4433 static const struct got_error *
4434 check_non_staged_files(struct got_fileindex *fileindex,
4435 struct got_pathlist_head *paths)
4437 struct got_pathlist_entry *pe;
4438 struct got_fileindex_entry *ie;
4440 TAILQ_FOREACH(pe, paths, entry) {
4441 if (pe->path[0] == '\0')
4442 continue;
4443 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4444 if (ie == NULL)
4445 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4446 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4447 return got_error_path(pe->path,
4448 GOT_ERR_FILE_NOT_STAGED);
4451 return NULL;
4454 const struct got_error *
4455 got_worktree_commit(struct got_object_id **new_commit_id,
4456 struct got_worktree *worktree, struct got_pathlist_head *paths,
4457 const char *author, const char *committer,
4458 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4459 got_worktree_status_cb status_cb, void *status_arg,
4460 struct got_repository *repo)
4462 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4463 struct got_fileindex *fileindex = NULL;
4464 char *fileindex_path = NULL;
4465 struct got_pathlist_head commitable_paths;
4466 struct collect_commitables_arg cc_arg;
4467 struct got_pathlist_entry *pe;
4468 struct got_reference *head_ref = NULL;
4469 struct got_object_id *head_commit_id = NULL;
4470 int have_staged_files = 0;
4472 *new_commit_id = NULL;
4474 TAILQ_INIT(&commitable_paths);
4476 err = lock_worktree(worktree, LOCK_EX);
4477 if (err)
4478 goto done;
4480 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4481 if (err)
4482 goto done;
4484 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4485 if (err)
4486 goto done;
4488 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4489 if (err)
4490 goto done;
4492 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4493 &have_staged_files);
4494 if (err && err->code != GOT_ERR_CANCELLED)
4495 goto done;
4496 if (have_staged_files) {
4497 err = check_non_staged_files(fileindex, paths);
4498 if (err)
4499 goto done;
4502 cc_arg.commitable_paths = &commitable_paths;
4503 cc_arg.worktree = worktree;
4504 cc_arg.repo = repo;
4505 cc_arg.have_staged_files = have_staged_files;
4506 TAILQ_FOREACH(pe, paths, entry) {
4507 err = worktree_status(worktree, pe->path, fileindex, repo,
4508 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4509 if (err)
4510 goto done;
4513 if (TAILQ_EMPTY(&commitable_paths)) {
4514 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4515 goto done;
4518 TAILQ_FOREACH(pe, paths, entry) {
4519 err = check_path_is_commitable(pe->path, &commitable_paths);
4520 if (err)
4521 goto done;
4524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4525 struct got_commitable *ct = pe->data;
4526 const char *ct_path = ct->in_repo_path;
4528 while (ct_path[0] == '/')
4529 ct_path++;
4530 err = check_out_of_date(ct_path, ct->status,
4531 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4532 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4533 if (err)
4534 goto done;
4538 err = commit_worktree(new_commit_id, &commitable_paths,
4539 head_commit_id, worktree, author, committer,
4540 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4541 if (err)
4542 goto done;
4544 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4545 fileindex, have_staged_files);
4546 sync_err = sync_fileindex(fileindex, fileindex_path);
4547 if (sync_err && err == NULL)
4548 err = sync_err;
4549 done:
4550 if (fileindex)
4551 got_fileindex_free(fileindex);
4552 free(fileindex_path);
4553 unlockerr = lock_worktree(worktree, LOCK_SH);
4554 if (unlockerr && err == NULL)
4555 err = unlockerr;
4556 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4557 struct got_commitable *ct = pe->data;
4558 free_commitable(ct);
4560 got_pathlist_free(&commitable_paths);
4561 return err;
4564 const char *
4565 got_commitable_get_path(struct got_commitable *ct)
4567 return ct->path;
4570 unsigned int
4571 got_commitable_get_status(struct got_commitable *ct)
4573 return ct->status;
4576 struct check_rebase_ok_arg {
4577 struct got_worktree *worktree;
4578 struct got_repository *repo;
4581 static const struct got_error *
4582 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4584 const struct got_error *err = NULL;
4585 struct check_rebase_ok_arg *a = arg;
4586 unsigned char status;
4587 struct stat sb;
4588 char *ondisk_path;
4590 /* Reject rebase of a work tree with mixed base commits. */
4591 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4592 SHA1_DIGEST_LENGTH))
4593 return got_error(GOT_ERR_MIXED_COMMITS);
4595 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4596 == -1)
4597 return got_error_from_errno("asprintf");
4599 /* Reject rebase of a work tree with modified or staged files. */
4600 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4601 free(ondisk_path);
4602 if (err)
4603 return err;
4605 if (status != GOT_STATUS_NO_CHANGE)
4606 return got_error(GOT_ERR_MODIFIED);
4607 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4608 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4610 return NULL;
4613 const struct got_error *
4614 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4615 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4616 struct got_worktree *worktree, struct got_reference *branch,
4617 struct got_repository *repo)
4619 const struct got_error *err = NULL;
4620 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4621 char *branch_ref_name = NULL;
4622 char *fileindex_path = NULL;
4623 struct check_rebase_ok_arg ok_arg;
4624 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4626 *new_base_branch_ref = NULL;
4627 *tmp_branch = NULL;
4628 *fileindex = NULL;
4630 err = lock_worktree(worktree, LOCK_EX);
4631 if (err)
4632 return err;
4634 err = open_fileindex(fileindex, &fileindex_path, worktree);
4635 if (err)
4636 goto done;
4638 ok_arg.worktree = worktree;
4639 ok_arg.repo = repo;
4640 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4641 &ok_arg);
4642 if (err)
4643 goto done;
4645 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4646 if (err)
4647 goto done;
4649 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4650 if (err)
4651 goto done;
4653 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4654 if (err)
4655 goto done;
4657 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4658 0);
4659 if (err)
4660 goto done;
4662 err = got_ref_alloc_symref(new_base_branch_ref,
4663 new_base_branch_ref_name, wt_branch);
4664 if (err)
4665 goto done;
4666 err = got_ref_write(*new_base_branch_ref, repo);
4667 if (err)
4668 goto done;
4670 /* TODO Lock original branch's ref while rebasing? */
4672 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4673 if (err)
4674 goto done;
4676 err = got_ref_write(branch_ref, repo);
4677 if (err)
4678 goto done;
4680 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4681 worktree->base_commit_id);
4682 if (err)
4683 goto done;
4684 err = got_ref_write(*tmp_branch, repo);
4685 if (err)
4686 goto done;
4688 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4689 if (err)
4690 goto done;
4691 done:
4692 free(fileindex_path);
4693 free(tmp_branch_name);
4694 free(new_base_branch_ref_name);
4695 free(branch_ref_name);
4696 if (branch_ref)
4697 got_ref_close(branch_ref);
4698 if (wt_branch)
4699 got_ref_close(wt_branch);
4700 if (err) {
4701 if (*new_base_branch_ref) {
4702 got_ref_close(*new_base_branch_ref);
4703 *new_base_branch_ref = NULL;
4705 if (*tmp_branch) {
4706 got_ref_close(*tmp_branch);
4707 *tmp_branch = NULL;
4709 if (*fileindex) {
4710 got_fileindex_free(*fileindex);
4711 *fileindex = NULL;
4713 lock_worktree(worktree, LOCK_SH);
4715 return err;
4718 const struct got_error *
4719 got_worktree_rebase_continue(struct got_object_id **commit_id,
4720 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4721 struct got_reference **branch, struct got_fileindex **fileindex,
4722 struct got_worktree *worktree, struct got_repository *repo)
4724 const struct got_error *err;
4725 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4726 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4727 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4728 char *fileindex_path = NULL;
4729 int have_staged_files = 0;
4731 *commit_id = NULL;
4732 *new_base_branch = NULL;
4733 *tmp_branch = NULL;
4734 *branch = NULL;
4735 *fileindex = NULL;
4737 err = lock_worktree(worktree, LOCK_EX);
4738 if (err)
4739 return err;
4741 err = open_fileindex(fileindex, &fileindex_path, worktree);
4742 if (err)
4743 goto done;
4745 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4746 &have_staged_files);
4747 if (err && err->code != GOT_ERR_CANCELLED)
4748 goto done;
4749 if (have_staged_files) {
4750 err = got_error(GOT_ERR_STAGED_PATHS);
4751 goto done;
4754 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4755 if (err)
4756 goto done;
4758 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4759 if (err)
4760 goto done;
4762 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4763 if (err)
4764 goto done;
4766 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4767 if (err)
4768 goto done;
4770 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4771 if (err)
4772 goto done;
4774 err = got_ref_open(branch, repo,
4775 got_ref_get_symref_target(branch_ref), 0);
4776 if (err)
4777 goto done;
4779 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4780 if (err)
4781 goto done;
4783 err = got_ref_resolve(commit_id, repo, commit_ref);
4784 if (err)
4785 goto done;
4787 err = got_ref_open(new_base_branch, repo,
4788 new_base_branch_ref_name, 0);
4789 if (err)
4790 goto done;
4792 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4793 if (err)
4794 goto done;
4795 done:
4796 free(commit_ref_name);
4797 free(branch_ref_name);
4798 free(fileindex_path);
4799 if (commit_ref)
4800 got_ref_close(commit_ref);
4801 if (branch_ref)
4802 got_ref_close(branch_ref);
4803 if (err) {
4804 free(*commit_id);
4805 *commit_id = NULL;
4806 if (*tmp_branch) {
4807 got_ref_close(*tmp_branch);
4808 *tmp_branch = NULL;
4810 if (*new_base_branch) {
4811 got_ref_close(*new_base_branch);
4812 *new_base_branch = NULL;
4814 if (*branch) {
4815 got_ref_close(*branch);
4816 *branch = NULL;
4818 if (*fileindex) {
4819 got_fileindex_free(*fileindex);
4820 *fileindex = NULL;
4822 lock_worktree(worktree, LOCK_SH);
4824 return err;
4827 const struct got_error *
4828 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4830 const struct got_error *err;
4831 char *tmp_branch_name = NULL;
4833 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4834 if (err)
4835 return err;
4837 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4838 free(tmp_branch_name);
4839 return NULL;
4842 static const struct got_error *
4843 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4844 char **logmsg, void *arg)
4846 *logmsg = arg;
4847 return NULL;
4850 static const struct got_error *
4851 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4852 const char *path, struct got_object_id *blob_id,
4853 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4854 int dirfd, const char *de_name)
4856 return NULL;
4859 struct collect_merged_paths_arg {
4860 got_worktree_checkout_cb progress_cb;
4861 void *progress_arg;
4862 struct got_pathlist_head *merged_paths;
4865 static const struct got_error *
4866 collect_merged_paths(void *arg, unsigned char status, const char *path)
4868 const struct got_error *err;
4869 struct collect_merged_paths_arg *a = arg;
4870 char *p;
4871 struct got_pathlist_entry *new;
4873 err = (*a->progress_cb)(a->progress_arg, status, path);
4874 if (err)
4875 return err;
4877 if (status != GOT_STATUS_MERGE &&
4878 status != GOT_STATUS_ADD &&
4879 status != GOT_STATUS_DELETE &&
4880 status != GOT_STATUS_CONFLICT)
4881 return NULL;
4883 p = strdup(path);
4884 if (p == NULL)
4885 return got_error_from_errno("strdup");
4887 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4888 if (err || new == NULL)
4889 free(p);
4890 return err;
4893 void
4894 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4896 struct got_pathlist_entry *pe;
4898 TAILQ_FOREACH(pe, merged_paths, entry)
4899 free((char *)pe->path);
4901 got_pathlist_free(merged_paths);
4904 static const struct got_error *
4905 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4906 struct got_repository *repo)
4908 const struct got_error *err;
4909 struct got_reference *commit_ref = NULL;
4911 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4912 if (err) {
4913 if (err->code != GOT_ERR_NOT_REF)
4914 goto done;
4915 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4916 if (err)
4917 goto done;
4918 err = got_ref_write(commit_ref, repo);
4919 if (err)
4920 goto done;
4921 } else {
4922 struct got_object_id *stored_id;
4923 int cmp;
4925 err = got_ref_resolve(&stored_id, repo, commit_ref);
4926 if (err)
4927 goto done;
4928 cmp = got_object_id_cmp(commit_id, stored_id);
4929 free(stored_id);
4930 if (cmp != 0) {
4931 err = got_error(GOT_ERR_REBASE_COMMITID);
4932 goto done;
4935 done:
4936 if (commit_ref)
4937 got_ref_close(commit_ref);
4938 return err;
4941 static const struct got_error *
4942 rebase_merge_files(struct got_pathlist_head *merged_paths,
4943 const char *commit_ref_name, struct got_worktree *worktree,
4944 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4945 struct got_object_id *commit_id, struct got_repository *repo,
4946 got_worktree_checkout_cb progress_cb, void *progress_arg,
4947 got_cancel_cb cancel_cb, void *cancel_arg)
4949 const struct got_error *err;
4950 struct got_reference *commit_ref = NULL;
4951 struct collect_merged_paths_arg cmp_arg;
4952 char *fileindex_path;
4954 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4956 err = get_fileindex_path(&fileindex_path, worktree);
4957 if (err)
4958 return err;
4960 cmp_arg.progress_cb = progress_cb;
4961 cmp_arg.progress_arg = progress_arg;
4962 cmp_arg.merged_paths = merged_paths;
4963 err = merge_files(worktree, fileindex, fileindex_path,
4964 parent_commit_id, commit_id, repo, collect_merged_paths,
4965 &cmp_arg, cancel_cb, cancel_arg);
4966 if (commit_ref)
4967 got_ref_close(commit_ref);
4968 return err;
4971 const struct got_error *
4972 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4973 struct got_worktree *worktree, struct got_fileindex *fileindex,
4974 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4975 struct got_repository *repo,
4976 got_worktree_checkout_cb progress_cb, void *progress_arg,
4977 got_cancel_cb cancel_cb, void *cancel_arg)
4979 const struct got_error *err;
4980 char *commit_ref_name;
4982 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4983 if (err)
4984 return err;
4986 err = store_commit_id(commit_ref_name, commit_id, repo);
4987 if (err)
4988 goto done;
4990 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4991 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4992 progress_arg, cancel_cb, cancel_arg);
4993 done:
4994 free(commit_ref_name);
4995 return err;
4998 const struct got_error *
4999 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5000 struct got_worktree *worktree, struct got_fileindex *fileindex,
5001 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5002 struct got_repository *repo,
5003 got_worktree_checkout_cb progress_cb, void *progress_arg,
5004 got_cancel_cb cancel_cb, void *cancel_arg)
5006 const struct got_error *err;
5007 char *commit_ref_name;
5009 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5010 if (err)
5011 return err;
5013 err = store_commit_id(commit_ref_name, commit_id, repo);
5014 if (err)
5015 goto done;
5017 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5018 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5019 progress_arg, cancel_cb, cancel_arg);
5020 done:
5021 free(commit_ref_name);
5022 return err;
5025 static const struct got_error *
5026 rebase_commit(struct got_object_id **new_commit_id,
5027 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5028 struct got_worktree *worktree, struct got_fileindex *fileindex,
5029 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5030 const char *new_logmsg, struct got_repository *repo)
5032 const struct got_error *err, *sync_err;
5033 struct got_pathlist_head commitable_paths;
5034 struct collect_commitables_arg cc_arg;
5035 char *fileindex_path = NULL;
5036 struct got_reference *head_ref = NULL;
5037 struct got_object_id *head_commit_id = NULL;
5038 char *logmsg = NULL;
5040 TAILQ_INIT(&commitable_paths);
5041 *new_commit_id = NULL;
5043 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5045 err = get_fileindex_path(&fileindex_path, worktree);
5046 if (err)
5047 return err;
5049 cc_arg.commitable_paths = &commitable_paths;
5050 cc_arg.worktree = worktree;
5051 cc_arg.repo = repo;
5052 cc_arg.have_staged_files = 0;
5054 * If possible get the status of individual files directly to
5055 * avoid crawling the entire work tree once per rebased commit.
5056 * TODO: Ideally, merged_paths would contain a list of commitables
5057 * we could use so we could skip worktree_status() entirely.
5059 if (merged_paths) {
5060 struct got_pathlist_entry *pe;
5061 if (TAILQ_EMPTY(merged_paths)) {
5062 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5063 goto done;
5065 TAILQ_FOREACH(pe, merged_paths, entry) {
5066 err = worktree_status(worktree, pe->path, fileindex,
5067 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5068 0);
5069 if (err)
5070 goto done;
5072 } else {
5073 err = worktree_status(worktree, "", fileindex, repo,
5074 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5075 if (err)
5076 goto done;
5079 if (TAILQ_EMPTY(&commitable_paths)) {
5080 /* No-op change; commit will be elided. */
5081 err = got_ref_delete(commit_ref, repo);
5082 if (err)
5083 goto done;
5084 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5085 goto done;
5088 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5089 if (err)
5090 goto done;
5092 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5093 if (err)
5094 goto done;
5096 if (new_logmsg) {
5097 logmsg = strdup(new_logmsg);
5098 if (logmsg == NULL) {
5099 err = got_error_from_errno("strdup");
5100 goto done;
5102 } else {
5103 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5104 if (err)
5105 goto done;
5108 /* NB: commit_worktree will call free(logmsg) */
5109 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5110 worktree, got_object_commit_get_author(orig_commit),
5111 got_object_commit_get_committer(orig_commit),
5112 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5113 if (err)
5114 goto done;
5116 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5117 if (err)
5118 goto done;
5120 err = got_ref_delete(commit_ref, repo);
5121 if (err)
5122 goto done;
5124 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5125 fileindex, 0);
5126 sync_err = sync_fileindex(fileindex, fileindex_path);
5127 if (sync_err && err == NULL)
5128 err = sync_err;
5129 done:
5130 free(fileindex_path);
5131 free(head_commit_id);
5132 if (head_ref)
5133 got_ref_close(head_ref);
5134 if (err) {
5135 free(*new_commit_id);
5136 *new_commit_id = NULL;
5138 return err;
5141 const struct got_error *
5142 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5143 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5144 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5145 struct got_commit_object *orig_commit,
5146 struct got_object_id *orig_commit_id, struct got_repository *repo)
5148 const struct got_error *err;
5149 char *commit_ref_name;
5150 struct got_reference *commit_ref = NULL;
5151 struct got_object_id *commit_id = NULL;
5153 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5154 if (err)
5155 return err;
5157 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5158 if (err)
5159 goto done;
5160 err = got_ref_resolve(&commit_id, repo, commit_ref);
5161 if (err)
5162 goto done;
5163 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5164 err = got_error(GOT_ERR_REBASE_COMMITID);
5165 goto done;
5168 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5169 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5170 done:
5171 if (commit_ref)
5172 got_ref_close(commit_ref);
5173 free(commit_ref_name);
5174 free(commit_id);
5175 return err;
5178 const struct got_error *
5179 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5180 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5182 struct got_commit_object *orig_commit,
5183 struct got_object_id *orig_commit_id, const char *new_logmsg,
5184 struct got_repository *repo)
5186 const struct got_error *err;
5187 char *commit_ref_name;
5188 struct got_reference *commit_ref = NULL;
5189 struct got_object_id *commit_id = NULL;
5191 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5192 if (err)
5193 return err;
5195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5196 if (err)
5197 goto done;
5198 err = got_ref_resolve(&commit_id, repo, commit_ref);
5199 if (err)
5200 goto done;
5201 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5202 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5203 goto done;
5206 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5207 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5208 done:
5209 if (commit_ref)
5210 got_ref_close(commit_ref);
5211 free(commit_ref_name);
5212 free(commit_id);
5213 return err;
5216 const struct got_error *
5217 got_worktree_rebase_postpone(struct got_worktree *worktree,
5218 struct got_fileindex *fileindex)
5220 if (fileindex)
5221 got_fileindex_free(fileindex);
5222 return lock_worktree(worktree, LOCK_SH);
5225 static const struct got_error *
5226 delete_ref(const char *name, struct got_repository *repo)
5228 const struct got_error *err;
5229 struct got_reference *ref;
5231 err = got_ref_open(&ref, repo, name, 0);
5232 if (err) {
5233 if (err->code == GOT_ERR_NOT_REF)
5234 return NULL;
5235 return err;
5238 err = got_ref_delete(ref, repo);
5239 got_ref_close(ref);
5240 return err;
5243 static const struct got_error *
5244 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5246 const struct got_error *err;
5247 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5248 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5250 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5251 if (err)
5252 goto done;
5253 err = delete_ref(tmp_branch_name, repo);
5254 if (err)
5255 goto done;
5257 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5258 if (err)
5259 goto done;
5260 err = delete_ref(new_base_branch_ref_name, repo);
5261 if (err)
5262 goto done;
5264 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5265 if (err)
5266 goto done;
5267 err = delete_ref(branch_ref_name, repo);
5268 if (err)
5269 goto done;
5271 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5272 if (err)
5273 goto done;
5274 err = delete_ref(commit_ref_name, repo);
5275 if (err)
5276 goto done;
5278 done:
5279 free(tmp_branch_name);
5280 free(new_base_branch_ref_name);
5281 free(branch_ref_name);
5282 free(commit_ref_name);
5283 return err;
5286 const struct got_error *
5287 got_worktree_rebase_complete(struct got_worktree *worktree,
5288 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5289 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5290 struct got_repository *repo)
5292 const struct got_error *err, *unlockerr;
5293 struct got_object_id *new_head_commit_id = NULL;
5295 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5296 if (err)
5297 return err;
5299 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5300 if (err)
5301 goto done;
5303 err = got_ref_write(rebased_branch, repo);
5304 if (err)
5305 goto done;
5307 err = got_worktree_set_head_ref(worktree, rebased_branch);
5308 if (err)
5309 goto done;
5311 err = delete_rebase_refs(worktree, repo);
5312 done:
5313 if (fileindex)
5314 got_fileindex_free(fileindex);
5315 free(new_head_commit_id);
5316 unlockerr = lock_worktree(worktree, LOCK_SH);
5317 if (unlockerr && err == NULL)
5318 err = unlockerr;
5319 return err;
5322 const struct got_error *
5323 got_worktree_rebase_abort(struct got_worktree *worktree,
5324 struct got_fileindex *fileindex, struct got_repository *repo,
5325 struct got_reference *new_base_branch,
5326 got_worktree_checkout_cb progress_cb, void *progress_arg)
5328 const struct got_error *err, *unlockerr, *sync_err;
5329 struct got_reference *resolved = NULL;
5330 struct got_object_id *commit_id = NULL;
5331 char *fileindex_path = NULL;
5332 struct revert_file_args rfa;
5333 struct got_object_id *tree_id = NULL;
5335 err = lock_worktree(worktree, LOCK_EX);
5336 if (err)
5337 return err;
5339 err = got_ref_open(&resolved, repo,
5340 got_ref_get_symref_target(new_base_branch), 0);
5341 if (err)
5342 goto done;
5344 err = got_worktree_set_head_ref(worktree, resolved);
5345 if (err)
5346 goto done;
5349 * XXX commits to the base branch could have happened while
5350 * we were busy rebasing; should we store the original commit ID
5351 * when rebase begins and read it back here?
5353 err = got_ref_resolve(&commit_id, repo, resolved);
5354 if (err)
5355 goto done;
5357 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5358 if (err)
5359 goto done;
5361 err = got_object_id_by_path(&tree_id, repo,
5362 worktree->base_commit_id, worktree->path_prefix);
5363 if (err)
5364 goto done;
5366 err = delete_rebase_refs(worktree, repo);
5367 if (err)
5368 goto done;
5370 err = get_fileindex_path(&fileindex_path, worktree);
5371 if (err)
5372 goto done;
5374 rfa.worktree = worktree;
5375 rfa.fileindex = fileindex;
5376 rfa.progress_cb = progress_cb;
5377 rfa.progress_arg = progress_arg;
5378 rfa.patch_cb = NULL;
5379 rfa.patch_arg = NULL;
5380 rfa.repo = repo;
5381 err = worktree_status(worktree, "", fileindex, repo,
5382 revert_file, &rfa, NULL, NULL, 0, 0);
5383 if (err)
5384 goto sync;
5386 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5387 repo, progress_cb, progress_arg, NULL, NULL);
5388 sync:
5389 sync_err = sync_fileindex(fileindex, fileindex_path);
5390 if (sync_err && err == NULL)
5391 err = sync_err;
5392 done:
5393 got_ref_close(resolved);
5394 free(tree_id);
5395 free(commit_id);
5396 if (fileindex)
5397 got_fileindex_free(fileindex);
5398 free(fileindex_path);
5400 unlockerr = lock_worktree(worktree, LOCK_SH);
5401 if (unlockerr && err == NULL)
5402 err = unlockerr;
5403 return err;
5406 const struct got_error *
5407 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5408 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5409 struct got_fileindex **fileindex, struct got_worktree *worktree,
5410 struct got_repository *repo)
5412 const struct got_error *err = NULL;
5413 char *tmp_branch_name = NULL;
5414 char *branch_ref_name = NULL;
5415 char *base_commit_ref_name = NULL;
5416 char *fileindex_path = NULL;
5417 struct check_rebase_ok_arg ok_arg;
5418 struct got_reference *wt_branch = NULL;
5419 struct got_reference *base_commit_ref = NULL;
5421 *tmp_branch = NULL;
5422 *branch_ref = NULL;
5423 *base_commit_id = NULL;
5424 *fileindex = NULL;
5426 err = lock_worktree(worktree, LOCK_EX);
5427 if (err)
5428 return err;
5430 err = open_fileindex(fileindex, &fileindex_path, worktree);
5431 if (err)
5432 goto done;
5434 ok_arg.worktree = worktree;
5435 ok_arg.repo = repo;
5436 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5437 &ok_arg);
5438 if (err)
5439 goto done;
5441 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5442 if (err)
5443 goto done;
5445 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5446 if (err)
5447 goto done;
5449 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5450 worktree);
5451 if (err)
5452 goto done;
5454 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5455 0);
5456 if (err)
5457 goto done;
5459 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5460 if (err)
5461 goto done;
5463 err = got_ref_write(*branch_ref, repo);
5464 if (err)
5465 goto done;
5467 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5468 worktree->base_commit_id);
5469 if (err)
5470 goto done;
5471 err = got_ref_write(base_commit_ref, repo);
5472 if (err)
5473 goto done;
5474 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5475 if (*base_commit_id == NULL) {
5476 err = got_error_from_errno("got_object_id_dup");
5477 goto done;
5480 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5481 worktree->base_commit_id);
5482 if (err)
5483 goto done;
5484 err = got_ref_write(*tmp_branch, repo);
5485 if (err)
5486 goto done;
5488 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5489 if (err)
5490 goto done;
5491 done:
5492 free(fileindex_path);
5493 free(tmp_branch_name);
5494 free(branch_ref_name);
5495 free(base_commit_ref_name);
5496 if (wt_branch)
5497 got_ref_close(wt_branch);
5498 if (err) {
5499 if (*branch_ref) {
5500 got_ref_close(*branch_ref);
5501 *branch_ref = NULL;
5503 if (*tmp_branch) {
5504 got_ref_close(*tmp_branch);
5505 *tmp_branch = NULL;
5507 free(*base_commit_id);
5508 if (*fileindex) {
5509 got_fileindex_free(*fileindex);
5510 *fileindex = NULL;
5512 lock_worktree(worktree, LOCK_SH);
5514 return err;
5517 const struct got_error *
5518 got_worktree_histedit_postpone(struct got_worktree *worktree,
5519 struct got_fileindex *fileindex)
5521 if (fileindex)
5522 got_fileindex_free(fileindex);
5523 return lock_worktree(worktree, LOCK_SH);
5526 const struct got_error *
5527 got_worktree_histedit_in_progress(int *in_progress,
5528 struct got_worktree *worktree)
5530 const struct got_error *err;
5531 char *tmp_branch_name = NULL;
5533 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5534 if (err)
5535 return err;
5537 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5538 free(tmp_branch_name);
5539 return NULL;
5542 const struct got_error *
5543 got_worktree_histedit_continue(struct got_object_id **commit_id,
5544 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5545 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5546 struct got_worktree *worktree, struct got_repository *repo)
5548 const struct got_error *err;
5549 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5550 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5551 struct got_reference *commit_ref = NULL;
5552 struct got_reference *base_commit_ref = NULL;
5553 char *fileindex_path = NULL;
5554 int have_staged_files = 0;
5556 *commit_id = NULL;
5557 *tmp_branch = NULL;
5558 *base_commit_id = NULL;
5559 *fileindex = NULL;
5561 err = lock_worktree(worktree, LOCK_EX);
5562 if (err)
5563 return err;
5565 err = open_fileindex(fileindex, &fileindex_path, worktree);
5566 if (err)
5567 goto done;
5569 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5570 &have_staged_files);
5571 if (err && err->code != GOT_ERR_CANCELLED)
5572 goto done;
5573 if (have_staged_files) {
5574 err = got_error(GOT_ERR_STAGED_PATHS);
5575 goto done;
5578 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5579 if (err)
5580 goto done;
5582 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5583 if (err)
5584 goto done;
5586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5587 if (err)
5588 goto done;
5590 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5591 worktree);
5592 if (err)
5593 goto done;
5595 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5596 if (err)
5597 goto done;
5599 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5600 if (err)
5601 goto done;
5602 err = got_ref_resolve(commit_id, repo, commit_ref);
5603 if (err)
5604 goto done;
5606 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5607 if (err)
5608 goto done;
5609 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5610 if (err)
5611 goto done;
5613 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5614 if (err)
5615 goto done;
5616 done:
5617 free(commit_ref_name);
5618 free(branch_ref_name);
5619 free(fileindex_path);
5620 if (commit_ref)
5621 got_ref_close(commit_ref);
5622 if (base_commit_ref)
5623 got_ref_close(base_commit_ref);
5624 if (err) {
5625 free(*commit_id);
5626 *commit_id = NULL;
5627 free(*base_commit_id);
5628 *base_commit_id = NULL;
5629 if (*tmp_branch) {
5630 got_ref_close(*tmp_branch);
5631 *tmp_branch = NULL;
5633 if (*fileindex) {
5634 got_fileindex_free(*fileindex);
5635 *fileindex = NULL;
5637 lock_worktree(worktree, LOCK_EX);
5639 return err;
5642 static const struct got_error *
5643 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5645 const struct got_error *err;
5646 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5647 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5649 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5650 if (err)
5651 goto done;
5652 err = delete_ref(tmp_branch_name, repo);
5653 if (err)
5654 goto done;
5656 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5657 worktree);
5658 if (err)
5659 goto done;
5660 err = delete_ref(base_commit_ref_name, repo);
5661 if (err)
5662 goto done;
5664 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5665 if (err)
5666 goto done;
5667 err = delete_ref(branch_ref_name, repo);
5668 if (err)
5669 goto done;
5671 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5672 if (err)
5673 goto done;
5674 err = delete_ref(commit_ref_name, repo);
5675 if (err)
5676 goto done;
5677 done:
5678 free(tmp_branch_name);
5679 free(base_commit_ref_name);
5680 free(branch_ref_name);
5681 free(commit_ref_name);
5682 return err;
5685 const struct got_error *
5686 got_worktree_histedit_abort(struct got_worktree *worktree,
5687 struct got_fileindex *fileindex, struct got_repository *repo,
5688 struct got_reference *branch, struct got_object_id *base_commit_id,
5689 got_worktree_checkout_cb progress_cb, void *progress_arg)
5691 const struct got_error *err, *unlockerr, *sync_err;
5692 struct got_reference *resolved = NULL;
5693 char *fileindex_path = NULL;
5694 struct got_object_id *tree_id = NULL;
5695 struct revert_file_args rfa;
5697 err = lock_worktree(worktree, LOCK_EX);
5698 if (err)
5699 return err;
5701 err = got_ref_open(&resolved, repo,
5702 got_ref_get_symref_target(branch), 0);
5703 if (err)
5704 goto done;
5706 err = got_worktree_set_head_ref(worktree, resolved);
5707 if (err)
5708 goto done;
5710 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5711 if (err)
5712 goto done;
5714 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5715 worktree->path_prefix);
5716 if (err)
5717 goto done;
5719 err = delete_histedit_refs(worktree, repo);
5720 if (err)
5721 goto done;
5723 err = get_fileindex_path(&fileindex_path, worktree);
5724 if (err)
5725 goto done;
5727 rfa.worktree = worktree;
5728 rfa.fileindex = fileindex;
5729 rfa.progress_cb = progress_cb;
5730 rfa.progress_arg = progress_arg;
5731 rfa.patch_cb = NULL;
5732 rfa.patch_arg = NULL;
5733 rfa.repo = repo;
5734 err = worktree_status(worktree, "", fileindex, repo,
5735 revert_file, &rfa, NULL, NULL, 0, 0);
5736 if (err)
5737 goto sync;
5739 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5740 repo, progress_cb, progress_arg, NULL, NULL);
5741 sync:
5742 sync_err = sync_fileindex(fileindex, fileindex_path);
5743 if (sync_err && err == NULL)
5744 err = sync_err;
5745 done:
5746 got_ref_close(resolved);
5747 free(tree_id);
5748 free(fileindex_path);
5750 unlockerr = lock_worktree(worktree, LOCK_SH);
5751 if (unlockerr && err == NULL)
5752 err = unlockerr;
5753 return err;
5756 const struct got_error *
5757 got_worktree_histedit_complete(struct got_worktree *worktree,
5758 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5759 struct got_reference *edited_branch, struct got_repository *repo)
5761 const struct got_error *err, *unlockerr;
5762 struct got_object_id *new_head_commit_id = NULL;
5763 struct got_reference *resolved = NULL;
5765 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5766 if (err)
5767 return err;
5769 err = got_ref_open(&resolved, repo,
5770 got_ref_get_symref_target(edited_branch), 0);
5771 if (err)
5772 goto done;
5774 err = got_ref_change_ref(resolved, new_head_commit_id);
5775 if (err)
5776 goto done;
5778 err = got_ref_write(resolved, repo);
5779 if (err)
5780 goto done;
5782 err = got_worktree_set_head_ref(worktree, resolved);
5783 if (err)
5784 goto done;
5786 err = delete_histedit_refs(worktree, repo);
5787 done:
5788 if (fileindex)
5789 got_fileindex_free(fileindex);
5790 free(new_head_commit_id);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5793 err = unlockerr;
5794 return err;
5797 const struct got_error *
5798 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5799 struct got_object_id *commit_id, struct got_repository *repo)
5801 const struct got_error *err;
5802 char *commit_ref_name;
5804 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5805 if (err)
5806 return err;
5808 err = store_commit_id(commit_ref_name, commit_id, repo);
5809 if (err)
5810 goto done;
5812 err = delete_ref(commit_ref_name, repo);
5813 done:
5814 free(commit_ref_name);
5815 return err;
5818 const struct got_error *
5819 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5820 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5821 struct got_worktree *worktree, const char *refname,
5822 struct got_repository *repo)
5824 const struct got_error *err = NULL;
5825 char *fileindex_path = NULL;
5826 struct check_rebase_ok_arg ok_arg;
5828 *fileindex = NULL;
5829 *branch_ref = NULL;
5830 *base_branch_ref = NULL;
5832 err = lock_worktree(worktree, LOCK_EX);
5833 if (err)
5834 return err;
5836 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5837 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5838 "cannot integrate a branch into itself; "
5839 "update -b or different branch name required");
5840 goto done;
5843 err = open_fileindex(fileindex, &fileindex_path, worktree);
5844 if (err)
5845 goto done;
5847 /* Preconditions are the same as for rebase. */
5848 ok_arg.worktree = worktree;
5849 ok_arg.repo = repo;
5850 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5851 &ok_arg);
5852 if (err)
5853 goto done;
5855 err = got_ref_open(branch_ref, repo, refname, 1);
5856 if (err)
5857 goto done;
5859 err = got_ref_open(base_branch_ref, repo,
5860 got_worktree_get_head_ref_name(worktree), 1);
5861 done:
5862 if (err) {
5863 if (*branch_ref) {
5864 got_ref_close(*branch_ref);
5865 *branch_ref = NULL;
5867 if (*base_branch_ref) {
5868 got_ref_close(*base_branch_ref);
5869 *base_branch_ref = NULL;
5871 if (*fileindex) {
5872 got_fileindex_free(*fileindex);
5873 *fileindex = NULL;
5875 lock_worktree(worktree, LOCK_SH);
5877 return err;
5880 const struct got_error *
5881 got_worktree_integrate_continue(struct got_worktree *worktree,
5882 struct got_fileindex *fileindex, struct got_repository *repo,
5883 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5884 got_worktree_checkout_cb progress_cb, void *progress_arg,
5885 got_cancel_cb cancel_cb, void *cancel_arg)
5887 const struct got_error *err = NULL, *sync_err, *unlockerr;
5888 char *fileindex_path = NULL;
5889 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5891 err = get_fileindex_path(&fileindex_path, worktree);
5892 if (err)
5893 goto done;
5895 err = got_ref_resolve(&commit_id, repo, branch_ref);
5896 if (err)
5897 goto done;
5899 err = got_object_id_by_path(&tree_id, repo, commit_id,
5900 worktree->path_prefix);
5901 if (err)
5902 goto done;
5904 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5905 if (err)
5906 goto done;
5908 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5909 progress_cb, progress_arg, cancel_cb, cancel_arg);
5910 if (err)
5911 goto sync;
5913 err = got_ref_change_ref(base_branch_ref, commit_id);
5914 if (err)
5915 goto sync;
5917 err = got_ref_write(base_branch_ref, repo);
5918 sync:
5919 sync_err = sync_fileindex(fileindex, fileindex_path);
5920 if (sync_err && err == NULL)
5921 err = sync_err;
5923 done:
5924 unlockerr = got_ref_unlock(branch_ref);
5925 if (unlockerr && err == NULL)
5926 err = unlockerr;
5927 got_ref_close(branch_ref);
5929 unlockerr = got_ref_unlock(base_branch_ref);
5930 if (unlockerr && err == NULL)
5931 err = unlockerr;
5932 got_ref_close(base_branch_ref);
5934 got_fileindex_free(fileindex);
5935 free(fileindex_path);
5936 free(tree_id);
5938 unlockerr = lock_worktree(worktree, LOCK_SH);
5939 if (unlockerr && err == NULL)
5940 err = unlockerr;
5941 return err;
5944 const struct got_error *
5945 got_worktree_integrate_abort(struct got_worktree *worktree,
5946 struct got_fileindex *fileindex, struct got_repository *repo,
5947 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5949 const struct got_error *err = NULL, *unlockerr = NULL;
5951 got_fileindex_free(fileindex);
5953 err = lock_worktree(worktree, LOCK_SH);
5955 unlockerr = got_ref_unlock(branch_ref);
5956 if (unlockerr && err == NULL)
5957 err = unlockerr;
5958 got_ref_close(branch_ref);
5960 unlockerr = got_ref_unlock(base_branch_ref);
5961 if (unlockerr && err == NULL)
5962 err = unlockerr;
5963 got_ref_close(base_branch_ref);
5965 return err;
5968 struct check_stage_ok_arg {
5969 struct got_object_id *head_commit_id;
5970 struct got_worktree *worktree;
5971 struct got_fileindex *fileindex;
5972 struct got_repository *repo;
5973 int have_changes;
5976 const struct got_error *
5977 check_stage_ok(void *arg, unsigned char status,
5978 unsigned char staged_status, const char *relpath,
5979 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5980 struct got_object_id *commit_id, int dirfd, const char *de_name)
5982 struct check_stage_ok_arg *a = arg;
5983 const struct got_error *err = NULL;
5984 struct got_fileindex_entry *ie;
5985 struct got_object_id base_commit_id;
5986 struct got_object_id *base_commit_idp = NULL;
5987 char *in_repo_path = NULL, *p;
5989 if (status == GOT_STATUS_UNVERSIONED ||
5990 status == GOT_STATUS_NO_CHANGE)
5991 return NULL;
5992 if (status == GOT_STATUS_NONEXISTENT)
5993 return got_error_set_errno(ENOENT, relpath);
5995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5996 if (ie == NULL)
5997 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5999 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6000 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6001 relpath) == -1)
6002 return got_error_from_errno("asprintf");
6004 if (got_fileindex_entry_has_commit(ie)) {
6005 memcpy(base_commit_id.sha1, ie->commit_sha1,
6006 SHA1_DIGEST_LENGTH);
6007 base_commit_idp = &base_commit_id;
6010 if (status == GOT_STATUS_CONFLICT) {
6011 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6012 goto done;
6013 } else if (status != GOT_STATUS_ADD &&
6014 status != GOT_STATUS_MODIFY &&
6015 status != GOT_STATUS_DELETE) {
6016 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6017 goto done;
6020 a->have_changes = 1;
6022 p = in_repo_path;
6023 while (p[0] == '/')
6024 p++;
6025 err = check_out_of_date(p, status, staged_status,
6026 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6027 GOT_ERR_STAGE_OUT_OF_DATE);
6028 done:
6029 free(in_repo_path);
6030 return err;
6033 struct stage_path_arg {
6034 struct got_worktree *worktree;
6035 struct got_fileindex *fileindex;
6036 struct got_repository *repo;
6037 got_worktree_status_cb status_cb;
6038 void *status_arg;
6039 got_worktree_patch_cb patch_cb;
6040 void *patch_arg;
6041 int staged_something;
6044 static const struct got_error *
6045 stage_path(void *arg, unsigned char status,
6046 unsigned char staged_status, const char *relpath,
6047 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6048 struct got_object_id *commit_id, int dirfd, const char *de_name)
6050 struct stage_path_arg *a = arg;
6051 const struct got_error *err = NULL;
6052 struct got_fileindex_entry *ie;
6053 char *ondisk_path = NULL, *path_content = NULL;
6054 uint32_t stage;
6055 struct got_object_id *new_staged_blob_id = NULL;
6057 if (status == GOT_STATUS_UNVERSIONED)
6058 return NULL;
6060 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6061 if (ie == NULL)
6062 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6065 relpath)== -1)
6066 return got_error_from_errno("asprintf");
6068 switch (status) {
6069 case GOT_STATUS_ADD:
6070 case GOT_STATUS_MODIFY:
6071 if (a->patch_cb) {
6072 if (status == GOT_STATUS_ADD) {
6073 int choice = GOT_PATCH_CHOICE_NONE;
6074 err = (*a->patch_cb)(&choice, a->patch_arg,
6075 status, ie->path, NULL, 1, 1);
6076 if (err)
6077 break;
6078 if (choice != GOT_PATCH_CHOICE_YES)
6079 break;
6080 } else {
6081 err = create_patched_content(&path_content, 0,
6082 staged_blob_id ? staged_blob_id : blob_id,
6083 ondisk_path, dirfd, de_name, ie->path,
6084 a->repo, a->patch_cb, a->patch_arg);
6085 if (err || path_content == NULL)
6086 break;
6089 err = got_object_blob_create(&new_staged_blob_id,
6090 path_content ? path_content : ondisk_path, a->repo);
6091 if (err)
6092 break;
6093 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6094 SHA1_DIGEST_LENGTH);
6095 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6096 stage = GOT_FILEIDX_STAGE_ADD;
6097 else
6098 stage = GOT_FILEIDX_STAGE_MODIFY;
6099 got_fileindex_entry_stage_set(ie, stage);
6100 a->staged_something = 1;
6101 if (a->status_cb == NULL)
6102 break;
6103 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6104 get_staged_status(ie), relpath, blob_id,
6105 new_staged_blob_id, NULL, dirfd, de_name);
6106 break;
6107 case GOT_STATUS_DELETE:
6108 if (staged_status == GOT_STATUS_DELETE)
6109 break;
6110 if (a->patch_cb) {
6111 int choice = GOT_PATCH_CHOICE_NONE;
6112 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6113 ie->path, NULL, 1, 1);
6114 if (err)
6115 break;
6116 if (choice == GOT_PATCH_CHOICE_NO)
6117 break;
6118 if (choice != GOT_PATCH_CHOICE_YES) {
6119 err = got_error(GOT_ERR_PATCH_CHOICE);
6120 break;
6123 stage = GOT_FILEIDX_STAGE_DELETE;
6124 got_fileindex_entry_stage_set(ie, stage);
6125 a->staged_something = 1;
6126 if (a->status_cb == NULL)
6127 break;
6128 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6129 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6130 de_name);
6131 break;
6132 case GOT_STATUS_NO_CHANGE:
6133 break;
6134 case GOT_STATUS_CONFLICT:
6135 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6136 break;
6137 case GOT_STATUS_NONEXISTENT:
6138 err = got_error_set_errno(ENOENT, relpath);
6139 break;
6140 default:
6141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6142 break;
6145 if (path_content && unlink(path_content) == -1 && err == NULL)
6146 err = got_error_from_errno2("unlink", path_content);
6147 free(path_content);
6148 free(ondisk_path);
6149 free(new_staged_blob_id);
6150 return err;
6153 const struct got_error *
6154 got_worktree_stage(struct got_worktree *worktree,
6155 struct got_pathlist_head *paths,
6156 got_worktree_status_cb status_cb, void *status_arg,
6157 got_worktree_patch_cb patch_cb, void *patch_arg,
6158 struct got_repository *repo)
6160 const struct got_error *err = NULL, *sync_err, *unlockerr;
6161 struct got_pathlist_entry *pe;
6162 struct got_fileindex *fileindex = NULL;
6163 char *fileindex_path = NULL;
6164 struct got_reference *head_ref = NULL;
6165 struct got_object_id *head_commit_id = NULL;
6166 struct check_stage_ok_arg oka;
6167 struct stage_path_arg spa;
6169 err = lock_worktree(worktree, LOCK_EX);
6170 if (err)
6171 return err;
6173 err = got_ref_open(&head_ref, repo,
6174 got_worktree_get_head_ref_name(worktree), 0);
6175 if (err)
6176 goto done;
6177 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6178 if (err)
6179 goto done;
6180 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6181 if (err)
6182 goto done;
6184 /* Check pre-conditions before staging anything. */
6185 oka.head_commit_id = head_commit_id;
6186 oka.worktree = worktree;
6187 oka.fileindex = fileindex;
6188 oka.repo = repo;
6189 oka.have_changes = 0;
6190 TAILQ_FOREACH(pe, paths, entry) {
6191 err = worktree_status(worktree, pe->path, fileindex, repo,
6192 check_stage_ok, &oka, NULL, NULL, 0, 0);
6193 if (err)
6194 goto done;
6196 if (!oka.have_changes) {
6197 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6198 goto done;
6201 spa.worktree = worktree;
6202 spa.fileindex = fileindex;
6203 spa.repo = repo;
6204 spa.patch_cb = patch_cb;
6205 spa.patch_arg = patch_arg;
6206 spa.status_cb = status_cb;
6207 spa.status_arg = status_arg;
6208 spa.staged_something = 0;
6209 TAILQ_FOREACH(pe, paths, entry) {
6210 err = worktree_status(worktree, pe->path, fileindex, repo,
6211 stage_path, &spa, NULL, NULL, 0, 0);
6212 if (err)
6213 goto done;
6215 if (!spa.staged_something) {
6216 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6217 goto done;
6220 sync_err = sync_fileindex(fileindex, fileindex_path);
6221 if (sync_err && err == NULL)
6222 err = sync_err;
6223 done:
6224 if (head_ref)
6225 got_ref_close(head_ref);
6226 free(head_commit_id);
6227 free(fileindex_path);
6228 if (fileindex)
6229 got_fileindex_free(fileindex);
6230 unlockerr = lock_worktree(worktree, LOCK_SH);
6231 if (unlockerr && err == NULL)
6232 err = unlockerr;
6233 return err;
6236 struct unstage_path_arg {
6237 struct got_worktree *worktree;
6238 struct got_fileindex *fileindex;
6239 struct got_repository *repo;
6240 got_worktree_checkout_cb progress_cb;
6241 void *progress_arg;
6242 got_worktree_patch_cb patch_cb;
6243 void *patch_arg;
6246 static const struct got_error *
6247 create_unstaged_content(char **path_unstaged_content,
6248 char **path_new_staged_content, struct got_object_id *blob_id,
6249 struct got_object_id *staged_blob_id, const char *relpath,
6250 struct got_repository *repo,
6251 got_worktree_patch_cb patch_cb, void *patch_arg)
6253 const struct got_error *err;
6254 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6255 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6256 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6257 struct stat sb1, sb2;
6258 struct got_diff_changes *changes = NULL;
6259 struct got_diff_state *ds = NULL;
6260 struct got_diff_args *args = NULL;
6261 struct got_diff_change *change;
6262 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6263 int have_content = 0, have_rejected_content = 0;
6265 *path_unstaged_content = NULL;
6266 *path_new_staged_content = NULL;
6268 err = got_object_id_str(&label1, blob_id);
6269 if (err)
6270 return err;
6271 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6272 if (err)
6273 goto done;
6275 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6276 if (err)
6277 goto done;
6279 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6280 if (err)
6281 goto done;
6283 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6284 if (err)
6285 goto done;
6287 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6288 if (err)
6289 goto done;
6291 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6292 if (err)
6293 goto done;
6295 if (stat(path1, &sb1) == -1) {
6296 err = got_error_from_errno2("stat", path1);
6297 goto done;
6300 if (stat(path2, &sb2) == -1) {
6301 err = got_error_from_errno2("stat", path2);
6302 goto done;
6305 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6306 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6307 if (err)
6308 goto done;
6310 err = got_opentemp_named(path_unstaged_content, &outfile,
6311 "got-unstaged-content");
6312 if (err)
6313 goto done;
6314 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6315 "got-new-staged-content");
6316 if (err)
6317 goto done;
6319 if (fseek(f1, 0L, SEEK_SET) == -1) {
6320 err = got_ferror(f1, GOT_ERR_IO);
6321 goto done;
6323 if (fseek(f2, 0L, SEEK_SET) == -1) {
6324 err = got_ferror(f2, GOT_ERR_IO);
6325 goto done;
6327 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6328 int choice;
6329 err = apply_or_reject_change(&choice, change, ++n,
6330 changes->nchanges, ds, args, diff_flags, relpath,
6331 f1, f2, &line_cur1, &line_cur2,
6332 outfile, rejectfile, patch_cb, patch_arg);
6333 if (err)
6334 goto done;
6335 if (choice == GOT_PATCH_CHOICE_YES)
6336 have_content = 1;
6337 else
6338 have_rejected_content = 1;
6339 if (choice == GOT_PATCH_CHOICE_QUIT)
6340 break;
6342 if (have_content || have_rejected_content)
6343 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6344 outfile, rejectfile);
6345 done:
6346 free(label1);
6347 if (blob)
6348 got_object_blob_close(blob);
6349 if (staged_blob)
6350 got_object_blob_close(staged_blob);
6351 if (f1 && fclose(f1) == EOF && err == NULL)
6352 err = got_error_from_errno2("fclose", path1);
6353 if (f2 && fclose(f2) == EOF && err == NULL)
6354 err = got_error_from_errno2("fclose", path2);
6355 if (outfile && fclose(outfile) == EOF && err == NULL)
6356 err = got_error_from_errno2("fclose", *path_unstaged_content);
6357 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6358 err = got_error_from_errno2("fclose", *path_new_staged_content);
6359 if (path1 && unlink(path1) == -1 && err == NULL)
6360 err = got_error_from_errno2("unlink", path1);
6361 if (path2 && unlink(path2) == -1 && err == NULL)
6362 err = got_error_from_errno2("unlink", path2);
6363 if (err || !have_content) {
6364 if (*path_unstaged_content &&
6365 unlink(*path_unstaged_content) == -1 && err == NULL)
6366 err = got_error_from_errno2("unlink",
6367 *path_unstaged_content);
6368 free(*path_unstaged_content);
6369 *path_unstaged_content = NULL;
6371 if (err || !have_rejected_content) {
6372 if (*path_new_staged_content &&
6373 unlink(*path_new_staged_content) == -1 && err == NULL)
6374 err = got_error_from_errno2("unlink",
6375 *path_new_staged_content);
6376 free(*path_new_staged_content);
6377 *path_new_staged_content = NULL;
6379 free(args);
6380 if (ds) {
6381 got_diff_state_free(ds);
6382 free(ds);
6384 if (changes)
6385 got_diff_free_changes(changes);
6386 free(path1);
6387 free(path2);
6388 return err;
6391 static const struct got_error *
6392 unstage_path(void *arg, unsigned char status,
6393 unsigned char staged_status, const char *relpath,
6394 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6395 struct got_object_id *commit_id, int dirfd, const char *de_name)
6397 const struct got_error *err = NULL;
6398 struct unstage_path_arg *a = arg;
6399 struct got_fileindex_entry *ie;
6400 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6401 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6402 char *path_new_staged_content = NULL;
6403 char *id_str = NULL, *label_orig = NULL;
6404 int local_changes_subsumed;
6405 struct stat sb;
6407 if (staged_status != GOT_STATUS_ADD &&
6408 staged_status != GOT_STATUS_MODIFY &&
6409 staged_status != GOT_STATUS_DELETE)
6410 return NULL;
6412 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6413 if (ie == NULL)
6414 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6416 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6417 == -1)
6418 return got_error_from_errno("asprintf");
6420 err = got_object_id_str(&id_str,
6421 commit_id ? commit_id : a->worktree->base_commit_id);
6422 if (err)
6423 goto done;
6424 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6425 id_str) == -1) {
6426 err = got_error_from_errno("asprintf");
6427 goto done;
6430 switch (staged_status) {
6431 case GOT_STATUS_MODIFY:
6432 err = got_object_open_as_blob(&blob_base, a->repo,
6433 blob_id, 8192);
6434 if (err)
6435 break;
6436 /* fall through */
6437 case GOT_STATUS_ADD:
6438 if (a->patch_cb) {
6439 if (staged_status == GOT_STATUS_ADD) {
6440 int choice = GOT_PATCH_CHOICE_NONE;
6441 err = (*a->patch_cb)(&choice, a->patch_arg,
6442 staged_status, ie->path, NULL, 1, 1);
6443 if (err)
6444 break;
6445 if (choice != GOT_PATCH_CHOICE_YES)
6446 break;
6447 } else {
6448 err = create_unstaged_content(
6449 &path_unstaged_content,
6450 &path_new_staged_content, blob_id,
6451 staged_blob_id, ie->path, a->repo,
6452 a->patch_cb, a->patch_arg);
6453 if (err || path_unstaged_content == NULL)
6454 break;
6455 if (path_new_staged_content) {
6456 err = got_object_blob_create(
6457 &staged_blob_id,
6458 path_new_staged_content,
6459 a->repo);
6460 if (err)
6461 break;
6462 memcpy(ie->staged_blob_sha1,
6463 staged_blob_id->sha1,
6464 SHA1_DIGEST_LENGTH);
6466 err = merge_file(&local_changes_subsumed,
6467 a->worktree, blob_base, ondisk_path,
6468 relpath, got_fileindex_perms_to_st(ie),
6469 path_unstaged_content, label_orig,
6470 "unstaged", a->repo, a->progress_cb,
6471 a->progress_arg);
6472 if (err == NULL &&
6473 path_new_staged_content == NULL)
6474 got_fileindex_entry_stage_set(ie,
6475 GOT_FILEIDX_STAGE_NONE);
6476 break; /* Done with this file. */
6479 err = got_object_open_as_blob(&blob_staged, a->repo,
6480 staged_blob_id, 8192);
6481 if (err)
6482 break;
6483 err = merge_blob(&local_changes_subsumed, a->worktree,
6484 blob_base, ondisk_path, relpath,
6485 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6486 commit_id ? commit_id : a->worktree->base_commit_id,
6487 a->repo, a->progress_cb, a->progress_arg);
6488 if (err == NULL)
6489 got_fileindex_entry_stage_set(ie,
6490 GOT_FILEIDX_STAGE_NONE);
6491 break;
6492 case GOT_STATUS_DELETE:
6493 if (a->patch_cb) {
6494 int choice = GOT_PATCH_CHOICE_NONE;
6495 err = (*a->patch_cb)(&choice, a->patch_arg,
6496 staged_status, ie->path, NULL, 1, 1);
6497 if (err)
6498 break;
6499 if (choice == GOT_PATCH_CHOICE_NO)
6500 break;
6501 if (choice != GOT_PATCH_CHOICE_YES) {
6502 err = got_error(GOT_ERR_PATCH_CHOICE);
6503 break;
6506 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6507 err = get_file_status(&status, &sb, ie, ondisk_path,
6508 dirfd, de_name, a->repo);
6509 if (err)
6510 break;
6511 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6512 break;
6514 done:
6515 free(ondisk_path);
6516 if (path_unstaged_content &&
6517 unlink(path_unstaged_content) == -1 && err == NULL)
6518 err = got_error_from_errno2("unlink", path_unstaged_content);
6519 if (path_new_staged_content &&
6520 unlink(path_new_staged_content) == -1 && err == NULL)
6521 err = got_error_from_errno2("unlink", path_new_staged_content);
6522 free(path_unstaged_content);
6523 free(path_new_staged_content);
6524 if (blob_base)
6525 got_object_blob_close(blob_base);
6526 if (blob_staged)
6527 got_object_blob_close(blob_staged);
6528 free(id_str);
6529 free(label_orig);
6530 return err;
6533 const struct got_error *
6534 got_worktree_unstage(struct got_worktree *worktree,
6535 struct got_pathlist_head *paths,
6536 got_worktree_checkout_cb progress_cb, void *progress_arg,
6537 got_worktree_patch_cb patch_cb, void *patch_arg,
6538 struct got_repository *repo)
6540 const struct got_error *err = NULL, *sync_err, *unlockerr;
6541 struct got_pathlist_entry *pe;
6542 struct got_fileindex *fileindex = NULL;
6543 char *fileindex_path = NULL;
6544 struct unstage_path_arg upa;
6546 err = lock_worktree(worktree, LOCK_EX);
6547 if (err)
6548 return err;
6550 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6551 if (err)
6552 goto done;
6554 upa.worktree = worktree;
6555 upa.fileindex = fileindex;
6556 upa.repo = repo;
6557 upa.progress_cb = progress_cb;
6558 upa.progress_arg = progress_arg;
6559 upa.patch_cb = patch_cb;
6560 upa.patch_arg = patch_arg;
6561 TAILQ_FOREACH(pe, paths, entry) {
6562 err = worktree_status(worktree, pe->path, fileindex, repo,
6563 unstage_path, &upa, NULL, NULL, 0, 0);
6564 if (err)
6565 goto done;
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6570 err = sync_err;
6571 done:
6572 free(fileindex_path);
6573 if (fileindex)
6574 got_fileindex_free(fileindex);
6575 unlockerr = lock_worktree(worktree, LOCK_SH);
6576 if (unlockerr && err == NULL)
6577 err = unlockerr;
6578 return err;