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);
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);
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);
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);
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);
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)
2816 struct schedule_addition_args *a = arg;
2817 const struct got_error *err = NULL;
2818 struct got_fileindex_entry *ie;
2819 struct stat sb;
2820 char *ondisk_path;
2822 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2823 relpath) == -1)
2824 return got_error_from_errno("asprintf");
2826 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2827 if (ie) {
2828 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2829 a->repo);
2830 if (err)
2831 goto done;
2832 /* Re-adding an existing entry is a no-op. */
2833 if (status == GOT_STATUS_ADD)
2834 goto done;
2835 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2836 if (err)
2837 goto done;
2840 if (status != GOT_STATUS_UNVERSIONED) {
2841 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2842 goto done;
2845 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2846 if (err)
2847 goto done;
2849 err = got_fileindex_entry_add(a->fileindex, ie);
2850 if (err) {
2851 got_fileindex_entry_free(ie);
2852 goto done;
2854 done:
2855 free(ondisk_path);
2856 if (err)
2857 return err;
2858 if (status == GOT_STATUS_ADD)
2859 return NULL;
2860 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2863 const struct got_error *
2864 got_worktree_schedule_add(struct got_worktree *worktree,
2865 struct got_pathlist_head *paths,
2866 got_worktree_checkout_cb progress_cb, void *progress_arg,
2867 struct got_repository *repo, int no_ignores)
2869 struct got_fileindex *fileindex = NULL;
2870 char *fileindex_path = NULL;
2871 const struct got_error *err = NULL, *sync_err, *unlockerr;
2872 struct got_pathlist_entry *pe;
2873 struct schedule_addition_args saa;
2875 err = lock_worktree(worktree, LOCK_EX);
2876 if (err)
2877 return err;
2879 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2880 if (err)
2881 goto done;
2883 saa.worktree = worktree;
2884 saa.fileindex = fileindex;
2885 saa.progress_cb = progress_cb;
2886 saa.progress_arg = progress_arg;
2887 saa.repo = repo;
2889 TAILQ_FOREACH(pe, paths, entry) {
2890 err = worktree_status(worktree, pe->path, fileindex, repo,
2891 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2892 if (err)
2893 break;
2895 sync_err = sync_fileindex(fileindex, fileindex_path);
2896 if (sync_err && err == NULL)
2897 err = sync_err;
2898 done:
2899 free(fileindex_path);
2900 if (fileindex)
2901 got_fileindex_free(fileindex);
2902 unlockerr = lock_worktree(worktree, LOCK_SH);
2903 if (unlockerr && err == NULL)
2904 err = unlockerr;
2905 return err;
2908 struct schedule_deletion_args {
2909 struct got_worktree *worktree;
2910 struct got_fileindex *fileindex;
2911 got_worktree_delete_cb progress_cb;
2912 void *progress_arg;
2913 struct got_repository *repo;
2914 int delete_local_mods;
2917 static const struct got_error *
2918 schedule_for_deletion(void *arg, unsigned char status,
2919 unsigned char staged_status, const char *relpath,
2920 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2921 struct got_object_id *commit_id)
2923 struct schedule_deletion_args *a = arg;
2924 const struct got_error *err = NULL;
2925 struct got_fileindex_entry *ie = NULL;
2926 struct stat sb;
2927 char *ondisk_path;
2929 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2930 if (ie == NULL)
2931 return got_error(GOT_ERR_BAD_PATH);
2933 staged_status = get_staged_status(ie);
2934 if (staged_status != GOT_STATUS_NO_CHANGE) {
2935 if (staged_status == GOT_STATUS_DELETE)
2936 return NULL;
2937 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2940 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2941 relpath) == -1)
2942 return got_error_from_errno("asprintf");
2944 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2945 if (err)
2946 goto done;
2948 if (status != GOT_STATUS_NO_CHANGE) {
2949 if (status == GOT_STATUS_DELETE)
2950 goto done;
2951 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2952 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2953 goto done;
2955 if (status != GOT_STATUS_MODIFY &&
2956 status != GOT_STATUS_MISSING) {
2957 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2958 goto done;
2962 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0) {
2963 err = got_error_from_errno2("unlink", ondisk_path);
2964 goto done;
2967 got_fileindex_entry_mark_deleted_from_disk(ie);
2968 done:
2969 free(ondisk_path);
2970 if (err)
2971 return err;
2972 if (status == GOT_STATUS_DELETE)
2973 return NULL;
2974 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2975 staged_status, relpath);
2978 const struct got_error *
2979 got_worktree_schedule_delete(struct got_worktree *worktree,
2980 struct got_pathlist_head *paths, int delete_local_mods,
2981 got_worktree_delete_cb progress_cb, void *progress_arg,
2982 struct got_repository *repo)
2984 struct got_fileindex *fileindex = NULL;
2985 char *fileindex_path = NULL;
2986 const struct got_error *err = NULL, *sync_err, *unlockerr;
2987 struct got_pathlist_entry *pe;
2988 struct schedule_deletion_args sda;
2990 err = lock_worktree(worktree, LOCK_EX);
2991 if (err)
2992 return err;
2994 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2995 if (err)
2996 goto done;
2998 sda.worktree = worktree;
2999 sda.fileindex = fileindex;
3000 sda.progress_cb = progress_cb;
3001 sda.progress_arg = progress_arg;
3002 sda.repo = repo;
3003 sda.delete_local_mods = delete_local_mods;
3005 TAILQ_FOREACH(pe, paths, entry) {
3006 err = worktree_status(worktree, pe->path, fileindex, repo,
3007 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3008 if (err)
3009 break;
3011 sync_err = sync_fileindex(fileindex, fileindex_path);
3012 if (sync_err && err == NULL)
3013 err = sync_err;
3014 done:
3015 free(fileindex_path);
3016 if (fileindex)
3017 got_fileindex_free(fileindex);
3018 unlockerr = lock_worktree(worktree, LOCK_SH);
3019 if (unlockerr && err == NULL)
3020 err = unlockerr;
3021 return err;
3024 static const struct got_error *
3025 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3027 const struct got_error *err = NULL;
3028 char *line = NULL;
3029 size_t linesize = 0, n;
3030 ssize_t linelen;
3032 linelen = getline(&line, &linesize, infile);
3033 if (linelen == -1) {
3034 if (ferror(infile)) {
3035 err = got_error_from_errno("getline");
3036 goto done;
3038 return NULL;
3040 if (outfile) {
3041 n = fwrite(line, 1, linelen, outfile);
3042 if (n != linelen) {
3043 err = got_ferror(outfile, GOT_ERR_IO);
3044 goto done;
3047 if (rejectfile) {
3048 n = fwrite(line, 1, linelen, rejectfile);
3049 if (n != linelen)
3050 err = got_ferror(outfile, GOT_ERR_IO);
3052 done:
3053 free(line);
3054 return err;
3057 static const struct got_error *
3058 skip_one_line(FILE *f)
3060 char *line = NULL;
3061 size_t linesize = 0;
3062 ssize_t linelen;
3064 linelen = getline(&line, &linesize, f);
3065 if (linelen == -1) {
3066 if (ferror(f))
3067 return got_error_from_errno("getline");
3068 return NULL;
3070 free(line);
3071 return NULL;
3074 static const struct got_error *
3075 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3076 int start_old, int end_old, int start_new, int end_new,
3077 FILE *outfile, FILE *rejectfile)
3079 const struct got_error *err;
3081 /* Copy old file's lines leading up to patch. */
3082 while (!feof(f1) && *line_cur1 < start_old) {
3083 err = copy_one_line(f1, outfile, NULL);
3084 if (err)
3085 return err;
3086 (*line_cur1)++;
3088 /* Skip new file's lines leading up to patch. */
3089 while (!feof(f2) && *line_cur2 < start_new) {
3090 if (rejectfile)
3091 err = copy_one_line(f2, NULL, rejectfile);
3092 else
3093 err = skip_one_line(f2);
3094 if (err)
3095 return err;
3096 (*line_cur2)++;
3098 /* Copy patched lines. */
3099 while (!feof(f2) && *line_cur2 <= end_new) {
3100 err = copy_one_line(f2, outfile, NULL);
3101 if (err)
3102 return err;
3103 (*line_cur2)++;
3105 /* Skip over old file's replaced lines. */
3106 while (!feof(f1) && *line_cur1 <= end_old) {
3107 if (rejectfile)
3108 err = copy_one_line(f1, NULL, rejectfile);
3109 else
3110 err = skip_one_line(f1);
3111 if (err)
3112 return err;
3113 (*line_cur1)++;
3116 return NULL;
3119 static const struct got_error *
3120 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3121 FILE *outfile, FILE *rejectfile)
3123 const struct got_error *err;
3125 if (outfile) {
3126 /* Copy old file's lines until EOF. */
3127 while (!feof(f1)) {
3128 err = copy_one_line(f1, outfile, NULL);
3129 if (err)
3130 return err;
3131 (*line_cur1)++;
3134 if (rejectfile) {
3135 /* Copy new file's lines until EOF. */
3136 while (!feof(f2)) {
3137 err = copy_one_line(f2, NULL, rejectfile);
3138 if (err)
3139 return err;
3140 (*line_cur2)++;
3144 return NULL;
3147 static const struct got_error *
3148 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3149 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3150 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3151 int *line_cur2, FILE *outfile, FILE *rejectfile,
3152 got_worktree_patch_cb patch_cb, void *patch_arg)
3154 const struct got_error *err = NULL;
3155 int start_old = change->cv.a;
3156 int end_old = change->cv.b;
3157 int start_new = change->cv.c;
3158 int end_new = change->cv.d;
3159 long pos1, pos2;
3160 FILE *hunkfile;
3162 *choice = GOT_PATCH_CHOICE_NONE;
3164 hunkfile = got_opentemp();
3165 if (hunkfile == NULL)
3166 return got_error_from_errno("got_opentemp");
3168 pos1 = ftell(f1);
3169 pos2 = ftell(f2);
3171 /* XXX TODO needs error checking */
3172 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3174 if (fseek(f1, pos1, SEEK_SET) == -1) {
3175 err = got_ferror(f1, GOT_ERR_IO);
3176 goto done;
3178 if (fseek(f2, pos2, SEEK_SET) == -1) {
3179 err = got_ferror(f1, GOT_ERR_IO);
3180 goto done;
3182 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3183 err = got_ferror(hunkfile, GOT_ERR_IO);
3184 goto done;
3187 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3188 hunkfile, n, nchanges);
3189 if (err)
3190 goto done;
3192 switch (*choice) {
3193 case GOT_PATCH_CHOICE_YES:
3194 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3195 end_old, start_new, end_new, outfile, rejectfile);
3196 break;
3197 case GOT_PATCH_CHOICE_NO:
3198 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3199 end_old, start_new, end_new, rejectfile, outfile);
3200 break;
3201 case GOT_PATCH_CHOICE_QUIT:
3202 break;
3203 default:
3204 err = got_error(GOT_ERR_PATCH_CHOICE);
3205 break;
3207 done:
3208 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3209 err = got_error_from_errno("fclose");
3210 return err;
3213 struct revert_file_args {
3214 struct got_worktree *worktree;
3215 struct got_fileindex *fileindex;
3216 got_worktree_checkout_cb progress_cb;
3217 void *progress_arg;
3218 got_worktree_patch_cb patch_cb;
3219 void *patch_arg;
3220 struct got_repository *repo;
3223 static const struct got_error *
3224 create_patched_content(char **path_outfile, int reverse_patch,
3225 struct got_object_id *blob_id, const char *path2,
3226 const char *relpath, struct got_repository *repo,
3227 got_worktree_patch_cb patch_cb, void *patch_arg)
3229 const struct got_error *err;
3230 struct got_blob_object *blob = NULL;
3231 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3232 int fd2 = -1;
3233 char *path1 = NULL, *id_str = NULL;
3234 struct stat sb1, sb2;
3235 struct got_diff_changes *changes = NULL;
3236 struct got_diff_state *ds = NULL;
3237 struct got_diff_args *args = NULL;
3238 struct got_diff_change *change;
3239 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3240 int n = 0;
3242 *path_outfile = NULL;
3244 err = got_object_id_str(&id_str, blob_id);
3245 if (err)
3246 return err;
3248 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3249 if (fd2 == -1) {
3250 err = got_error_from_errno2("open", path2);
3251 goto done;
3253 if (fstat(fd2, &sb2) == -1) {
3254 err = got_error_from_errno2("fstat", path2);
3255 goto done;
3258 f2 = fdopen(fd2, "r");
3259 if (f2 == NULL) {
3260 err = got_error_from_errno2("fopen", path2);
3261 goto done;
3263 fd2 = -1;
3265 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3266 if (err)
3267 goto done;
3269 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3270 if (err)
3271 goto done;
3273 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3274 if (err)
3275 goto done;
3277 if (stat(path1, &sb1) == -1) {
3278 err = got_error_from_errno2("stat", path1);
3279 goto done;
3282 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3283 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3284 if (err)
3285 goto done;
3287 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3288 if (err)
3289 goto done;
3291 if (fseek(f1, 0L, SEEK_SET) == -1)
3292 return got_ferror(f1, GOT_ERR_IO);
3293 if (fseek(f2, 0L, SEEK_SET) == -1)
3294 return got_ferror(f2, GOT_ERR_IO);
3295 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3296 int choice;
3297 err = apply_or_reject_change(&choice, change, ++n,
3298 changes->nchanges, ds, args, diff_flags, relpath,
3299 f1, f2, &line_cur1, &line_cur2,
3300 reverse_patch ? NULL : outfile,
3301 reverse_patch ? outfile : NULL,
3302 patch_cb, patch_arg);
3303 if (err)
3304 goto done;
3305 if (choice == GOT_PATCH_CHOICE_YES)
3306 have_content = 1;
3307 else if (choice == GOT_PATCH_CHOICE_QUIT)
3308 break;
3310 if (have_content) {
3311 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3312 reverse_patch ? NULL : outfile,
3313 reverse_patch ? outfile : NULL);
3314 if (err)
3315 goto done;
3317 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3318 err = got_error_from_errno2("chmod", path2);
3319 goto done;
3322 done:
3323 free(id_str);
3324 if (blob)
3325 got_object_blob_close(blob);
3326 if (f1 && fclose(f1) == EOF && err == NULL)
3327 err = got_error_from_errno2("fclose", path1);
3328 if (f2 && fclose(f2) == EOF && err == NULL)
3329 err = got_error_from_errno2("fclose", path2);
3330 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3331 err = got_error_from_errno2("close", path2);
3332 if (outfile && fclose(outfile) == EOF && err == NULL)
3333 err = got_error_from_errno2("fclose", *path_outfile);
3334 if (path1 && unlink(path1) == -1 && err == NULL)
3335 err = got_error_from_errno2("unlink", path1);
3336 if (err || !have_content) {
3337 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3338 err = got_error_from_errno2("unlink", *path_outfile);
3339 free(*path_outfile);
3340 *path_outfile = NULL;
3342 free(args);
3343 if (ds) {
3344 got_diff_state_free(ds);
3345 free(ds);
3347 if (changes)
3348 got_diff_free_changes(changes);
3349 free(path1);
3350 return err;
3353 static const struct got_error *
3354 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3355 const char *relpath, struct got_object_id *blob_id,
3356 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3358 struct revert_file_args *a = arg;
3359 const struct got_error *err = NULL;
3360 char *parent_path = NULL;
3361 struct got_fileindex_entry *ie;
3362 struct got_tree_object *tree = NULL;
3363 struct got_object_id *tree_id = NULL;
3364 const struct got_tree_entry *te = NULL;
3365 char *tree_path = NULL, *te_name;
3366 char *ondisk_path = NULL, *path_content = NULL;
3367 struct got_blob_object *blob = NULL;
3369 /* Reverting a staged deletion is a no-op. */
3370 if (status == GOT_STATUS_DELETE &&
3371 staged_status != GOT_STATUS_NO_CHANGE)
3372 return NULL;
3374 if (status == GOT_STATUS_UNVERSIONED)
3375 return (*a->progress_cb)(a->progress_arg,
3376 GOT_STATUS_UNVERSIONED, relpath);
3378 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3379 if (ie == NULL)
3380 return got_error(GOT_ERR_BAD_PATH);
3382 /* Construct in-repository path of tree which contains this blob. */
3383 err = got_path_dirname(&parent_path, ie->path);
3384 if (err) {
3385 if (err->code != GOT_ERR_BAD_PATH)
3386 goto done;
3387 parent_path = strdup("/");
3388 if (parent_path == NULL) {
3389 err = got_error_from_errno("strdup");
3390 goto done;
3393 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3394 tree_path = strdup(parent_path);
3395 if (tree_path == NULL) {
3396 err = got_error_from_errno("strdup");
3397 goto done;
3399 } else {
3400 if (got_path_is_root_dir(parent_path)) {
3401 tree_path = strdup(a->worktree->path_prefix);
3402 if (tree_path == NULL) {
3403 err = got_error_from_errno("strdup");
3404 goto done;
3406 } else {
3407 if (asprintf(&tree_path, "%s/%s",
3408 a->worktree->path_prefix, parent_path) == -1) {
3409 err = got_error_from_errno("asprintf");
3410 goto done;
3415 err = got_object_id_by_path(&tree_id, a->repo,
3416 a->worktree->base_commit_id, tree_path);
3417 if (err) {
3418 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3419 (status == GOT_STATUS_ADD ||
3420 staged_status == GOT_STATUS_ADD)))
3421 goto done;
3422 } else {
3423 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3424 if (err)
3425 goto done;
3427 te_name = basename(ie->path);
3428 if (te_name == NULL) {
3429 err = got_error_from_errno2("basename", ie->path);
3430 goto done;
3433 te = got_object_tree_find_entry(tree, te_name);
3434 if (te == NULL && status != GOT_STATUS_ADD &&
3435 staged_status != GOT_STATUS_ADD) {
3436 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3437 goto done;
3441 switch (status) {
3442 case GOT_STATUS_ADD:
3443 if (a->patch_cb) {
3444 int choice = GOT_PATCH_CHOICE_NONE;
3445 err = (*a->patch_cb)(&choice, a->patch_arg,
3446 status, ie->path, NULL, 1, 1);
3447 if (err)
3448 goto done;
3449 if (choice != GOT_PATCH_CHOICE_YES)
3450 break;
3452 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3453 ie->path);
3454 if (err)
3455 goto done;
3456 got_fileindex_entry_remove(a->fileindex, ie);
3457 break;
3458 case GOT_STATUS_DELETE:
3459 if (a->patch_cb) {
3460 int choice = GOT_PATCH_CHOICE_NONE;
3461 err = (*a->patch_cb)(&choice, a->patch_arg,
3462 status, ie->path, NULL, 1, 1);
3463 if (err)
3464 goto done;
3465 if (choice != GOT_PATCH_CHOICE_YES)
3466 break;
3468 /* fall through */
3469 case GOT_STATUS_MODIFY:
3470 case GOT_STATUS_MODE_CHANGE:
3471 case GOT_STATUS_CONFLICT:
3472 case GOT_STATUS_MISSING: {
3473 struct got_object_id id;
3474 if (staged_status == GOT_STATUS_ADD ||
3475 staged_status == GOT_STATUS_MODIFY) {
3476 memcpy(id.sha1, ie->staged_blob_sha1,
3477 SHA1_DIGEST_LENGTH);
3478 } else
3479 memcpy(id.sha1, ie->blob_sha1,
3480 SHA1_DIGEST_LENGTH);
3481 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3482 if (err)
3483 goto done;
3485 if (asprintf(&ondisk_path, "%s/%s",
3486 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3487 err = got_error_from_errno("asprintf");
3488 goto done;
3491 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3492 status == GOT_STATUS_CONFLICT)) {
3493 err = create_patched_content(&path_content, 1, &id,
3494 ondisk_path, ie->path, a->repo,
3495 a->patch_cb, a->patch_arg);
3496 if (err || path_content == NULL)
3497 break;
3498 if (rename(path_content, ondisk_path) == -1) {
3499 err = got_error_from_errno3("rename",
3500 path_content, ondisk_path);
3501 goto done;
3503 } else {
3504 err = install_blob(a->worktree, ondisk_path, ie->path,
3505 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3506 got_fileindex_perms_to_st(ie), blob, 0, 1,
3507 a->repo, a->progress_cb, a->progress_arg);
3508 if (err)
3509 goto done;
3510 if (status == GOT_STATUS_DELETE ||
3511 status == GOT_STATUS_MODE_CHANGE) {
3512 err = update_blob_fileindex_entry(a->worktree,
3513 a->fileindex, ie, ondisk_path, ie->path,
3514 blob, 1);
3515 if (err)
3516 goto done;
3519 break;
3521 default:
3522 break;
3524 done:
3525 free(ondisk_path);
3526 free(path_content);
3527 free(parent_path);
3528 free(tree_path);
3529 if (blob)
3530 got_object_blob_close(blob);
3531 if (tree)
3532 got_object_tree_close(tree);
3533 free(tree_id);
3534 return err;
3537 const struct got_error *
3538 got_worktree_revert(struct got_worktree *worktree,
3539 struct got_pathlist_head *paths,
3540 got_worktree_checkout_cb progress_cb, void *progress_arg,
3541 got_worktree_patch_cb patch_cb, void *patch_arg,
3542 struct got_repository *repo)
3544 struct got_fileindex *fileindex = NULL;
3545 char *fileindex_path = NULL;
3546 const struct got_error *err = NULL, *unlockerr = NULL;
3547 const struct got_error *sync_err = NULL;
3548 struct got_pathlist_entry *pe;
3549 struct revert_file_args rfa;
3551 err = lock_worktree(worktree, LOCK_EX);
3552 if (err)
3553 return err;
3555 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3556 if (err)
3557 goto done;
3559 rfa.worktree = worktree;
3560 rfa.fileindex = fileindex;
3561 rfa.progress_cb = progress_cb;
3562 rfa.progress_arg = progress_arg;
3563 rfa.patch_cb = patch_cb;
3564 rfa.patch_arg = patch_arg;
3565 rfa.repo = repo;
3566 TAILQ_FOREACH(pe, paths, entry) {
3567 err = worktree_status(worktree, pe->path, fileindex, repo,
3568 revert_file, &rfa, NULL, NULL, 0, 0);
3569 if (err)
3570 break;
3572 sync_err = sync_fileindex(fileindex, fileindex_path);
3573 if (sync_err && err == NULL)
3574 err = sync_err;
3575 done:
3576 free(fileindex_path);
3577 if (fileindex)
3578 got_fileindex_free(fileindex);
3579 unlockerr = lock_worktree(worktree, LOCK_SH);
3580 if (unlockerr && err == NULL)
3581 err = unlockerr;
3582 return err;
3585 static void
3586 free_commitable(struct got_commitable *ct)
3588 free(ct->path);
3589 free(ct->in_repo_path);
3590 free(ct->ondisk_path);
3591 free(ct->blob_id);
3592 free(ct->base_blob_id);
3593 free(ct->staged_blob_id);
3594 free(ct->base_commit_id);
3595 free(ct);
3598 struct collect_commitables_arg {
3599 struct got_pathlist_head *commitable_paths;
3600 struct got_repository *repo;
3601 struct got_worktree *worktree;
3602 int have_staged_files;
3605 static const struct got_error *
3606 collect_commitables(void *arg, unsigned char status,
3607 unsigned char staged_status, const char *relpath,
3608 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3609 struct got_object_id *commit_id)
3611 struct collect_commitables_arg *a = arg;
3612 const struct got_error *err = NULL;
3613 struct got_commitable *ct = NULL;
3614 struct got_pathlist_entry *new = NULL;
3615 char *parent_path = NULL, *path = NULL;
3616 struct stat sb;
3618 if (a->have_staged_files) {
3619 if (staged_status != GOT_STATUS_MODIFY &&
3620 staged_status != GOT_STATUS_ADD &&
3621 staged_status != GOT_STATUS_DELETE)
3622 return NULL;
3623 } else {
3624 if (status == GOT_STATUS_CONFLICT)
3625 return got_error(GOT_ERR_COMMIT_CONFLICT);
3627 if (status != GOT_STATUS_MODIFY &&
3628 status != GOT_STATUS_MODE_CHANGE &&
3629 status != GOT_STATUS_ADD &&
3630 status != GOT_STATUS_DELETE)
3631 return NULL;
3634 if (asprintf(&path, "/%s", relpath) == -1) {
3635 err = got_error_from_errno("asprintf");
3636 goto done;
3638 if (strcmp(path, "/") == 0) {
3639 parent_path = strdup("");
3640 if (parent_path == NULL)
3641 return got_error_from_errno("strdup");
3642 } else {
3643 err = got_path_dirname(&parent_path, path);
3644 if (err)
3645 return err;
3648 ct = calloc(1, sizeof(*ct));
3649 if (ct == NULL) {
3650 err = got_error_from_errno("calloc");
3651 goto done;
3654 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3655 relpath) == -1) {
3656 err = got_error_from_errno("asprintf");
3657 goto done;
3659 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3660 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3661 } else {
3662 if (lstat(ct->ondisk_path, &sb) != 0) {
3663 err = got_error_from_errno2("lstat", ct->ondisk_path);
3664 goto done;
3666 ct->mode = sb.st_mode;
3669 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3670 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3671 relpath) == -1) {
3672 err = got_error_from_errno("asprintf");
3673 goto done;
3676 ct->status = status;
3677 ct->staged_status = staged_status;
3678 ct->blob_id = NULL; /* will be filled in when blob gets created */
3679 if (ct->status != GOT_STATUS_ADD &&
3680 ct->staged_status != GOT_STATUS_ADD) {
3681 ct->base_blob_id = got_object_id_dup(blob_id);
3682 if (ct->base_blob_id == NULL) {
3683 err = got_error_from_errno("got_object_id_dup");
3684 goto done;
3686 ct->base_commit_id = got_object_id_dup(commit_id);
3687 if (ct->base_commit_id == NULL) {
3688 err = got_error_from_errno("got_object_id_dup");
3689 goto done;
3692 if (ct->staged_status == GOT_STATUS_ADD ||
3693 ct->staged_status == GOT_STATUS_MODIFY) {
3694 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3695 if (ct->staged_blob_id == NULL) {
3696 err = got_error_from_errno("got_object_id_dup");
3697 goto done;
3700 ct->path = strdup(path);
3701 if (ct->path == NULL) {
3702 err = got_error_from_errno("strdup");
3703 goto done;
3705 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3706 done:
3707 if (ct && (err || new == NULL))
3708 free_commitable(ct);
3709 free(parent_path);
3710 free(path);
3711 return err;
3714 static const struct got_error *write_tree(struct got_object_id **,
3715 struct got_tree_object *, const char *, struct got_pathlist_head *,
3716 got_worktree_status_cb status_cb, void *status_arg,
3717 struct got_repository *);
3719 static const struct got_error *
3720 write_subtree(struct got_object_id **new_subtree_id,
3721 struct got_tree_entry *te, const char *parent_path,
3722 struct got_pathlist_head *commitable_paths,
3723 got_worktree_status_cb status_cb, void *status_arg,
3724 struct got_repository *repo)
3726 const struct got_error *err = NULL;
3727 struct got_tree_object *subtree;
3728 char *subpath;
3730 if (asprintf(&subpath, "%s%s%s", parent_path,
3731 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3732 return got_error_from_errno("asprintf");
3734 err = got_object_open_as_tree(&subtree, repo, &te->id);
3735 if (err)
3736 return err;
3738 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3739 status_cb, status_arg, repo);
3740 got_object_tree_close(subtree);
3741 free(subpath);
3742 return err;
3745 static const struct got_error *
3746 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3748 const struct got_error *err = NULL;
3749 char *ct_parent_path = NULL;
3751 *match = 0;
3753 if (strchr(ct->in_repo_path, '/') == NULL) {
3754 *match = got_path_is_root_dir(path);
3755 return NULL;
3758 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3759 if (err)
3760 return err;
3761 *match = (strcmp(path, ct_parent_path) == 0);
3762 free(ct_parent_path);
3763 return err;
3766 static mode_t
3767 get_ct_file_mode(struct got_commitable *ct)
3769 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3772 static const struct got_error *
3773 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3774 struct got_tree_entry *te, struct got_commitable *ct)
3776 const struct got_error *err = NULL;
3778 *new_te = NULL;
3780 err = got_object_tree_entry_dup(new_te, te);
3781 if (err)
3782 goto done;
3784 (*new_te)->mode = get_ct_file_mode(ct);
3786 if (ct->staged_status == GOT_STATUS_MODIFY)
3787 memcpy(&(*new_te)->id, ct->staged_blob_id,
3788 sizeof((*new_te)->id));
3789 else
3790 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3791 done:
3792 if (err && *new_te) {
3793 free(*new_te);
3794 *new_te = NULL;
3796 return err;
3799 static const struct got_error *
3800 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3801 struct got_commitable *ct)
3803 const struct got_error *err = NULL;
3804 char *ct_name;
3806 *new_te = NULL;
3808 *new_te = calloc(1, sizeof(**new_te));
3809 if (*new_te == NULL)
3810 return got_error_from_errno("calloc");
3812 ct_name = basename(ct->path);
3813 if (ct_name == NULL) {
3814 err = got_error_from_errno2("basename", ct->path);
3815 goto done;
3817 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3818 sizeof((*new_te)->name)) {
3819 err = got_error(GOT_ERR_NO_SPACE);
3820 goto done;
3823 (*new_te)->mode = get_ct_file_mode(ct);
3825 if (ct->staged_status == GOT_STATUS_ADD)
3826 memcpy(&(*new_te)->id, ct->staged_blob_id,
3827 sizeof((*new_te)->id));
3828 else
3829 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3830 done:
3831 if (err && *new_te) {
3832 free(*new_te);
3833 *new_te = NULL;
3835 return err;
3838 static const struct got_error *
3839 insert_tree_entry(struct got_tree_entry *new_te,
3840 struct got_pathlist_head *paths)
3842 const struct got_error *err = NULL;
3843 struct got_pathlist_entry *new_pe;
3845 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3846 if (err)
3847 return err;
3848 if (new_pe == NULL)
3849 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3850 return NULL;
3853 static const struct got_error *
3854 report_ct_status(struct got_commitable *ct,
3855 got_worktree_status_cb status_cb, void *status_arg)
3857 const char *ct_path = ct->path;
3858 unsigned char status;
3860 while (ct_path[0] == '/')
3861 ct_path++;
3863 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3864 status = ct->staged_status;
3865 else
3866 status = ct->status;
3868 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3869 ct_path, ct->blob_id, NULL, NULL);
3872 static const struct got_error *
3873 match_modified_subtree(int *modified, struct got_tree_entry *te,
3874 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3876 const struct got_error *err = NULL;
3877 struct got_pathlist_entry *pe;
3878 char *te_path;
3880 *modified = 0;
3882 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3883 got_path_is_root_dir(base_tree_path) ? "" : "/",
3884 te->name) == -1)
3885 return got_error_from_errno("asprintf");
3887 TAILQ_FOREACH(pe, commitable_paths, entry) {
3888 struct got_commitable *ct = pe->data;
3889 *modified = got_path_is_child(ct->in_repo_path, te_path,
3890 strlen(te_path));
3891 if (*modified)
3892 break;
3895 free(te_path);
3896 return err;
3899 static const struct got_error *
3900 match_deleted_or_modified_ct(struct got_commitable **ctp,
3901 struct got_tree_entry *te, const char *base_tree_path,
3902 struct got_pathlist_head *commitable_paths)
3904 const struct got_error *err = NULL;
3905 struct got_pathlist_entry *pe;
3907 *ctp = NULL;
3909 TAILQ_FOREACH(pe, commitable_paths, entry) {
3910 struct got_commitable *ct = pe->data;
3911 char *ct_name = NULL;
3912 int path_matches;
3914 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3915 if (ct->status != GOT_STATUS_MODIFY &&
3916 ct->status != GOT_STATUS_MODE_CHANGE &&
3917 ct->status != GOT_STATUS_DELETE)
3918 continue;
3919 } else {
3920 if (ct->staged_status != GOT_STATUS_MODIFY &&
3921 ct->staged_status != GOT_STATUS_DELETE)
3922 continue;
3925 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3926 continue;
3928 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3929 if (err)
3930 return err;
3931 if (!path_matches)
3932 continue;
3934 ct_name = basename(pe->path);
3935 if (ct_name == NULL)
3936 return got_error_from_errno2("basename", pe->path);
3938 if (strcmp(te->name, ct_name) != 0)
3939 continue;
3941 *ctp = ct;
3942 break;
3945 return err;
3948 static const struct got_error *
3949 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3950 const char *child_path, const char *path_base_tree,
3951 struct got_pathlist_head *commitable_paths,
3952 got_worktree_status_cb status_cb, void *status_arg,
3953 struct got_repository *repo)
3955 const struct got_error *err = NULL;
3956 struct got_tree_entry *new_te;
3957 char *subtree_path;
3958 struct got_object_id *id = NULL;
3960 *new_tep = NULL;
3962 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3963 got_path_is_root_dir(path_base_tree) ? "" : "/",
3964 child_path) == -1)
3965 return got_error_from_errno("asprintf");
3967 new_te = calloc(1, sizeof(*new_te));
3968 if (new_te == NULL)
3969 return got_error_from_errno("calloc");
3970 new_te->mode = S_IFDIR;
3972 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
3973 sizeof(new_te->name)) {
3974 err = got_error(GOT_ERR_NO_SPACE);
3975 goto done;
3977 err = write_tree(&id, NULL, subtree_path,
3978 commitable_paths, status_cb, status_arg, repo);
3979 if (err) {
3980 free(new_te);
3981 goto done;
3983 memcpy(&new_te->id, id, sizeof(new_te->id));
3984 done:
3985 free(id);
3986 free(subtree_path);
3987 if (err == NULL)
3988 *new_tep = new_te;
3989 return err;
3992 static const struct got_error *
3993 write_tree(struct got_object_id **new_tree_id,
3994 struct got_tree_object *base_tree, const char *path_base_tree,
3995 struct got_pathlist_head *commitable_paths,
3996 got_worktree_status_cb status_cb, void *status_arg,
3997 struct got_repository *repo)
3999 const struct got_error *err = NULL;
4000 struct got_pathlist_head paths;
4001 struct got_tree_entry *te, *new_te = NULL;
4002 struct got_pathlist_entry *pe;
4003 int nentries = 0;
4005 TAILQ_INIT(&paths);
4007 /* Insert, and recurse into, newly added entries first. */
4008 TAILQ_FOREACH(pe, commitable_paths, entry) {
4009 struct got_commitable *ct = pe->data;
4010 char *child_path = NULL, *slash;
4012 if ((ct->status != GOT_STATUS_ADD &&
4013 ct->staged_status != GOT_STATUS_ADD) ||
4014 (ct->flags & GOT_COMMITABLE_ADDED))
4015 continue;
4017 if (!got_path_is_child(pe->path, path_base_tree,
4018 strlen(path_base_tree)))
4019 continue;
4021 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4022 pe->path);
4023 if (err)
4024 goto done;
4026 slash = strchr(child_path, '/');
4027 if (slash == NULL) {
4028 err = alloc_added_blob_tree_entry(&new_te, ct);
4029 if (err)
4030 goto done;
4031 err = report_ct_status(ct, status_cb, status_arg);
4032 if (err)
4033 goto done;
4034 ct->flags |= GOT_COMMITABLE_ADDED;
4035 err = insert_tree_entry(new_te, &paths);
4036 if (err)
4037 goto done;
4038 nentries++;
4039 } else {
4040 *slash = '\0'; /* trim trailing path components */
4041 if (base_tree == NULL ||
4042 got_object_tree_find_entry(base_tree, child_path)
4043 == NULL) {
4044 err = make_subtree_for_added_blob(&new_te,
4045 child_path, path_base_tree,
4046 commitable_paths, status_cb, status_arg,
4047 repo);
4048 if (err)
4049 goto done;
4050 err = insert_tree_entry(new_te, &paths);
4051 if (err)
4052 goto done;
4053 nentries++;
4058 if (base_tree) {
4059 int i, nbase_entries;
4060 /* Handle modified and deleted entries. */
4061 nbase_entries = got_object_tree_get_nentries(base_tree);
4062 for (i = 0; i < nbase_entries; i++) {
4063 struct got_commitable *ct = NULL;
4065 te = got_object_tree_get_entry(base_tree, i);
4066 if (got_object_tree_entry_is_submodule(te)) {
4067 /* Entry is a submodule; just copy it. */
4068 err = got_object_tree_entry_dup(&new_te, te);
4069 if (err)
4070 goto done;
4071 err = insert_tree_entry(new_te, &paths);
4072 if (err)
4073 goto done;
4074 nentries++;
4075 continue;
4078 if (S_ISDIR(te->mode)) {
4079 int modified;
4080 err = got_object_tree_entry_dup(&new_te, te);
4081 if (err)
4082 goto done;
4083 err = match_modified_subtree(&modified, te,
4084 path_base_tree, commitable_paths);
4085 if (err)
4086 goto done;
4087 /* Avoid recursion into unmodified subtrees. */
4088 if (modified) {
4089 struct got_object_id *new_id;
4090 err = write_subtree(&new_id, te,
4091 path_base_tree, commitable_paths,
4092 status_cb, status_arg, repo);
4093 if (err)
4094 goto done;
4095 memcpy(&new_te->id, new_id,
4096 sizeof(new_te->id));
4097 free(new_id);
4099 err = insert_tree_entry(new_te, &paths);
4100 if (err)
4101 goto done;
4102 nentries++;
4103 continue;
4106 err = match_deleted_or_modified_ct(&ct, te,
4107 path_base_tree, commitable_paths);
4108 if (err)
4109 goto done;
4110 if (ct) {
4111 /* NB: Deleted entries get dropped here. */
4112 if (ct->status == GOT_STATUS_MODIFY ||
4113 ct->status == GOT_STATUS_MODE_CHANGE ||
4114 ct->staged_status == GOT_STATUS_MODIFY) {
4115 err = alloc_modified_blob_tree_entry(
4116 &new_te, te, ct);
4117 if (err)
4118 goto done;
4119 err = insert_tree_entry(new_te, &paths);
4120 if (err)
4121 goto done;
4122 nentries++;
4124 err = report_ct_status(ct, status_cb,
4125 status_arg);
4126 if (err)
4127 goto done;
4128 } else {
4129 /* Entry is unchanged; just copy it. */
4130 err = got_object_tree_entry_dup(&new_te, te);
4131 if (err)
4132 goto done;
4133 err = insert_tree_entry(new_te, &paths);
4134 if (err)
4135 goto done;
4136 nentries++;
4141 /* Write new list of entries; deleted entries have been dropped. */
4142 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4143 done:
4144 got_pathlist_free(&paths);
4145 return err;
4148 static const struct got_error *
4149 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4150 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4151 int have_staged_files)
4153 const struct got_error *err = NULL;
4154 struct got_pathlist_entry *pe;
4156 TAILQ_FOREACH(pe, commitable_paths, entry) {
4157 struct got_fileindex_entry *ie;
4158 struct got_commitable *ct = pe->data;
4160 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4161 if (ie) {
4162 if (ct->status == GOT_STATUS_DELETE ||
4163 ct->staged_status == GOT_STATUS_DELETE) {
4164 got_fileindex_entry_remove(fileindex, ie);
4165 got_fileindex_entry_free(ie);
4166 } else if (ct->staged_status == GOT_STATUS_ADD ||
4167 ct->staged_status == GOT_STATUS_MODIFY) {
4168 got_fileindex_entry_stage_set(ie,
4169 GOT_FILEIDX_STAGE_NONE);
4170 err = got_fileindex_entry_update(ie,
4171 ct->ondisk_path, ct->staged_blob_id->sha1,
4172 new_base_commit_id->sha1,
4173 !have_staged_files);
4174 } else
4175 err = got_fileindex_entry_update(ie,
4176 ct->ondisk_path, ct->blob_id->sha1,
4177 new_base_commit_id->sha1,
4178 !have_staged_files);
4179 } else {
4180 err = got_fileindex_entry_alloc(&ie,
4181 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4182 new_base_commit_id->sha1);
4183 if (err)
4184 break;
4185 err = got_fileindex_entry_add(fileindex, ie);
4186 if (err)
4187 break;
4190 return err;
4194 static const struct got_error *
4195 check_out_of_date(const char *in_repo_path, unsigned char status,
4196 unsigned char staged_status, struct got_object_id *base_blob_id,
4197 struct got_object_id *base_commit_id,
4198 struct got_object_id *head_commit_id, struct got_repository *repo,
4199 int ood_errcode)
4201 const struct got_error *err = NULL;
4202 struct got_object_id *id = NULL;
4204 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4205 /* Trivial case: base commit == head commit */
4206 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4207 return NULL;
4209 * Ensure file content which local changes were based
4210 * on matches file content in the branch head.
4212 err = got_object_id_by_path(&id, repo, head_commit_id,
4213 in_repo_path);
4214 if (err) {
4215 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4216 err = got_error(ood_errcode);
4217 goto done;
4218 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4219 err = got_error(ood_errcode);
4220 } else {
4221 /* Require that added files don't exist in the branch head. */
4222 err = got_object_id_by_path(&id, repo, head_commit_id,
4223 in_repo_path);
4224 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4225 goto done;
4226 err = id ? got_error(ood_errcode) : NULL;
4228 done:
4229 free(id);
4230 return err;
4233 const struct got_error *
4234 commit_worktree(struct got_object_id **new_commit_id,
4235 struct got_pathlist_head *commitable_paths,
4236 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4237 const char *author, const char *committer,
4238 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4239 got_worktree_status_cb status_cb, void *status_arg,
4240 struct got_repository *repo)
4242 const struct got_error *err = NULL, *unlockerr = NULL;
4243 struct got_pathlist_entry *pe;
4244 const char *head_ref_name = NULL;
4245 struct got_commit_object *head_commit = NULL;
4246 struct got_reference *head_ref2 = NULL;
4247 struct got_object_id *head_commit_id2 = NULL;
4248 struct got_tree_object *head_tree = NULL;
4249 struct got_object_id *new_tree_id = NULL;
4250 struct got_object_id_queue parent_ids;
4251 struct got_object_qid *pid = NULL;
4252 char *logmsg = NULL;
4254 *new_commit_id = NULL;
4256 SIMPLEQ_INIT(&parent_ids);
4258 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4259 if (err)
4260 goto done;
4262 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4263 if (err)
4264 goto done;
4266 if (commit_msg_cb != NULL) {
4267 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4268 if (err)
4269 goto done;
4272 if (logmsg == NULL || strlen(logmsg) == 0) {
4273 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4274 goto done;
4277 /* Create blobs from added and modified files and record their IDs. */
4278 TAILQ_FOREACH(pe, commitable_paths, entry) {
4279 struct got_commitable *ct = pe->data;
4280 char *ondisk_path;
4282 /* Blobs for staged files already exist. */
4283 if (ct->staged_status == GOT_STATUS_ADD ||
4284 ct->staged_status == GOT_STATUS_MODIFY)
4285 continue;
4287 if (ct->status != GOT_STATUS_ADD &&
4288 ct->status != GOT_STATUS_MODIFY &&
4289 ct->status != GOT_STATUS_MODE_CHANGE)
4290 continue;
4292 if (asprintf(&ondisk_path, "%s/%s",
4293 worktree->root_path, pe->path) == -1) {
4294 err = got_error_from_errno("asprintf");
4295 goto done;
4297 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4298 free(ondisk_path);
4299 if (err)
4300 goto done;
4303 /* Recursively write new tree objects. */
4304 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4305 status_cb, status_arg, repo);
4306 if (err)
4307 goto done;
4309 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4310 if (err)
4311 goto done;
4312 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4313 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4314 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4315 got_object_qid_free(pid);
4316 if (logmsg != NULL)
4317 free(logmsg);
4318 if (err)
4319 goto done;
4321 /* Check if a concurrent commit to our branch has occurred. */
4322 head_ref_name = got_worktree_get_head_ref_name(worktree);
4323 if (head_ref_name == NULL) {
4324 err = got_error_from_errno("got_worktree_get_head_ref_name");
4325 goto done;
4327 /* Lock the reference here to prevent concurrent modification. */
4328 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4329 if (err)
4330 goto done;
4331 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4332 if (err)
4333 goto done;
4334 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4335 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4336 goto done;
4338 /* Update branch head in repository. */
4339 err = got_ref_change_ref(head_ref2, *new_commit_id);
4340 if (err)
4341 goto done;
4342 err = got_ref_write(head_ref2, repo);
4343 if (err)
4344 goto done;
4346 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4347 if (err)
4348 goto done;
4350 err = ref_base_commit(worktree, repo);
4351 if (err)
4352 goto done;
4353 done:
4354 if (head_tree)
4355 got_object_tree_close(head_tree);
4356 if (head_commit)
4357 got_object_commit_close(head_commit);
4358 free(head_commit_id2);
4359 if (head_ref2) {
4360 unlockerr = got_ref_unlock(head_ref2);
4361 if (unlockerr && err == NULL)
4362 err = unlockerr;
4363 got_ref_close(head_ref2);
4365 return err;
4368 static const struct got_error *
4369 check_path_is_commitable(const char *path,
4370 struct got_pathlist_head *commitable_paths)
4372 struct got_pathlist_entry *cpe = NULL;
4373 size_t path_len = strlen(path);
4375 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4376 struct got_commitable *ct = cpe->data;
4377 const char *ct_path = ct->path;
4379 while (ct_path[0] == '/')
4380 ct_path++;
4382 if (strcmp(path, ct_path) == 0 ||
4383 got_path_is_child(ct_path, path, path_len))
4384 break;
4387 if (cpe == NULL)
4388 return got_error_path(path, GOT_ERR_BAD_PATH);
4390 return NULL;
4393 static const struct got_error *
4394 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4396 int *have_staged_files = arg;
4398 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4399 *have_staged_files = 1;
4400 return got_error(GOT_ERR_CANCELLED);
4403 return NULL;
4406 static const struct got_error *
4407 check_non_staged_files(struct got_fileindex *fileindex,
4408 struct got_pathlist_head *paths)
4410 struct got_pathlist_entry *pe;
4411 struct got_fileindex_entry *ie;
4413 TAILQ_FOREACH(pe, paths, entry) {
4414 if (pe->path[0] == '\0')
4415 continue;
4416 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4417 if (ie == NULL)
4418 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4419 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4420 return got_error_path(pe->path,
4421 GOT_ERR_FILE_NOT_STAGED);
4424 return NULL;
4427 const struct got_error *
4428 got_worktree_commit(struct got_object_id **new_commit_id,
4429 struct got_worktree *worktree, struct got_pathlist_head *paths,
4430 const char *author, const char *committer,
4431 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4432 got_worktree_status_cb status_cb, void *status_arg,
4433 struct got_repository *repo)
4435 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4436 struct got_fileindex *fileindex = NULL;
4437 char *fileindex_path = NULL;
4438 struct got_pathlist_head commitable_paths;
4439 struct collect_commitables_arg cc_arg;
4440 struct got_pathlist_entry *pe;
4441 struct got_reference *head_ref = NULL;
4442 struct got_object_id *head_commit_id = NULL;
4443 int have_staged_files = 0;
4445 *new_commit_id = NULL;
4447 TAILQ_INIT(&commitable_paths);
4449 err = lock_worktree(worktree, LOCK_EX);
4450 if (err)
4451 goto done;
4453 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4454 if (err)
4455 goto done;
4457 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4458 if (err)
4459 goto done;
4461 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4462 if (err)
4463 goto done;
4465 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4466 &have_staged_files);
4467 if (err && err->code != GOT_ERR_CANCELLED)
4468 goto done;
4469 if (have_staged_files) {
4470 err = check_non_staged_files(fileindex, paths);
4471 if (err)
4472 goto done;
4475 cc_arg.commitable_paths = &commitable_paths;
4476 cc_arg.worktree = worktree;
4477 cc_arg.repo = repo;
4478 cc_arg.have_staged_files = have_staged_files;
4479 TAILQ_FOREACH(pe, paths, entry) {
4480 err = worktree_status(worktree, pe->path, fileindex, repo,
4481 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4482 if (err)
4483 goto done;
4486 if (TAILQ_EMPTY(&commitable_paths)) {
4487 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4488 goto done;
4491 TAILQ_FOREACH(pe, paths, entry) {
4492 err = check_path_is_commitable(pe->path, &commitable_paths);
4493 if (err)
4494 goto done;
4497 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4498 struct got_commitable *ct = pe->data;
4499 const char *ct_path = ct->in_repo_path;
4501 while (ct_path[0] == '/')
4502 ct_path++;
4503 err = check_out_of_date(ct_path, ct->status,
4504 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4505 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4506 if (err)
4507 goto done;
4511 err = commit_worktree(new_commit_id, &commitable_paths,
4512 head_commit_id, worktree, author, committer,
4513 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4514 if (err)
4515 goto done;
4517 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4518 fileindex, have_staged_files);
4519 sync_err = sync_fileindex(fileindex, fileindex_path);
4520 if (sync_err && err == NULL)
4521 err = sync_err;
4522 done:
4523 if (fileindex)
4524 got_fileindex_free(fileindex);
4525 free(fileindex_path);
4526 unlockerr = lock_worktree(worktree, LOCK_SH);
4527 if (unlockerr && err == NULL)
4528 err = unlockerr;
4529 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4530 struct got_commitable *ct = pe->data;
4531 free_commitable(ct);
4533 got_pathlist_free(&commitable_paths);
4534 return err;
4537 const char *
4538 got_commitable_get_path(struct got_commitable *ct)
4540 return ct->path;
4543 unsigned int
4544 got_commitable_get_status(struct got_commitable *ct)
4546 return ct->status;
4549 struct check_rebase_ok_arg {
4550 struct got_worktree *worktree;
4551 struct got_repository *repo;
4554 static const struct got_error *
4555 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4557 const struct got_error *err = NULL;
4558 struct check_rebase_ok_arg *a = arg;
4559 unsigned char status;
4560 struct stat sb;
4561 char *ondisk_path;
4563 /* Reject rebase of a work tree with mixed base commits. */
4564 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4565 SHA1_DIGEST_LENGTH))
4566 return got_error(GOT_ERR_MIXED_COMMITS);
4568 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4569 == -1)
4570 return got_error_from_errno("asprintf");
4572 /* Reject rebase of a work tree with modified or staged files. */
4573 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4574 free(ondisk_path);
4575 if (err)
4576 return err;
4578 if (status != GOT_STATUS_NO_CHANGE)
4579 return got_error(GOT_ERR_MODIFIED);
4580 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4581 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4583 return NULL;
4586 const struct got_error *
4587 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4588 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4589 struct got_worktree *worktree, struct got_reference *branch,
4590 struct got_repository *repo)
4592 const struct got_error *err = NULL;
4593 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4594 char *branch_ref_name = NULL;
4595 char *fileindex_path = NULL;
4596 struct check_rebase_ok_arg ok_arg;
4597 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4599 *new_base_branch_ref = NULL;
4600 *tmp_branch = NULL;
4601 *fileindex = NULL;
4603 err = lock_worktree(worktree, LOCK_EX);
4604 if (err)
4605 return err;
4607 err = open_fileindex(fileindex, &fileindex_path, worktree);
4608 if (err)
4609 goto done;
4611 ok_arg.worktree = worktree;
4612 ok_arg.repo = repo;
4613 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4614 &ok_arg);
4615 if (err)
4616 goto done;
4618 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4619 if (err)
4620 goto done;
4622 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4623 if (err)
4624 goto done;
4626 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4627 if (err)
4628 goto done;
4630 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4631 0);
4632 if (err)
4633 goto done;
4635 err = got_ref_alloc_symref(new_base_branch_ref,
4636 new_base_branch_ref_name, wt_branch);
4637 if (err)
4638 goto done;
4639 err = got_ref_write(*new_base_branch_ref, repo);
4640 if (err)
4641 goto done;
4643 /* TODO Lock original branch's ref while rebasing? */
4645 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4646 if (err)
4647 goto done;
4649 err = got_ref_write(branch_ref, repo);
4650 if (err)
4651 goto done;
4653 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4654 worktree->base_commit_id);
4655 if (err)
4656 goto done;
4657 err = got_ref_write(*tmp_branch, repo);
4658 if (err)
4659 goto done;
4661 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4662 if (err)
4663 goto done;
4664 done:
4665 free(fileindex_path);
4666 free(tmp_branch_name);
4667 free(new_base_branch_ref_name);
4668 free(branch_ref_name);
4669 if (branch_ref)
4670 got_ref_close(branch_ref);
4671 if (wt_branch)
4672 got_ref_close(wt_branch);
4673 if (err) {
4674 if (*new_base_branch_ref) {
4675 got_ref_close(*new_base_branch_ref);
4676 *new_base_branch_ref = NULL;
4678 if (*tmp_branch) {
4679 got_ref_close(*tmp_branch);
4680 *tmp_branch = NULL;
4682 if (*fileindex) {
4683 got_fileindex_free(*fileindex);
4684 *fileindex = NULL;
4686 lock_worktree(worktree, LOCK_SH);
4688 return err;
4691 const struct got_error *
4692 got_worktree_rebase_continue(struct got_object_id **commit_id,
4693 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4694 struct got_reference **branch, struct got_fileindex **fileindex,
4695 struct got_worktree *worktree, struct got_repository *repo)
4697 const struct got_error *err;
4698 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4699 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4700 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4701 char *fileindex_path = NULL;
4702 int have_staged_files = 0;
4704 *commit_id = NULL;
4705 *new_base_branch = NULL;
4706 *tmp_branch = NULL;
4707 *branch = NULL;
4708 *fileindex = NULL;
4710 err = lock_worktree(worktree, LOCK_EX);
4711 if (err)
4712 return err;
4714 err = open_fileindex(fileindex, &fileindex_path, worktree);
4715 if (err)
4716 goto done;
4718 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4719 &have_staged_files);
4720 if (err && err->code != GOT_ERR_CANCELLED)
4721 goto done;
4722 if (have_staged_files) {
4723 err = got_error(GOT_ERR_STAGED_PATHS);
4724 goto done;
4727 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4728 if (err)
4729 goto done;
4731 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4732 if (err)
4733 goto done;
4735 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4736 if (err)
4737 goto done;
4739 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4740 if (err)
4741 goto done;
4743 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4744 if (err)
4745 goto done;
4747 err = got_ref_open(branch, repo,
4748 got_ref_get_symref_target(branch_ref), 0);
4749 if (err)
4750 goto done;
4752 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4753 if (err)
4754 goto done;
4756 err = got_ref_resolve(commit_id, repo, commit_ref);
4757 if (err)
4758 goto done;
4760 err = got_ref_open(new_base_branch, repo,
4761 new_base_branch_ref_name, 0);
4762 if (err)
4763 goto done;
4765 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4766 if (err)
4767 goto done;
4768 done:
4769 free(commit_ref_name);
4770 free(branch_ref_name);
4771 free(fileindex_path);
4772 if (commit_ref)
4773 got_ref_close(commit_ref);
4774 if (branch_ref)
4775 got_ref_close(branch_ref);
4776 if (err) {
4777 free(*commit_id);
4778 *commit_id = NULL;
4779 if (*tmp_branch) {
4780 got_ref_close(*tmp_branch);
4781 *tmp_branch = NULL;
4783 if (*new_base_branch) {
4784 got_ref_close(*new_base_branch);
4785 *new_base_branch = NULL;
4787 if (*branch) {
4788 got_ref_close(*branch);
4789 *branch = NULL;
4791 if (*fileindex) {
4792 got_fileindex_free(*fileindex);
4793 *fileindex = NULL;
4795 lock_worktree(worktree, LOCK_SH);
4797 return err;
4800 const struct got_error *
4801 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4803 const struct got_error *err;
4804 char *tmp_branch_name = NULL;
4806 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4807 if (err)
4808 return err;
4810 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4811 free(tmp_branch_name);
4812 return NULL;
4815 static const struct got_error *
4816 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4817 char **logmsg, void *arg)
4819 *logmsg = arg;
4820 return NULL;
4823 static const struct got_error *
4824 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4825 const char *path, struct got_object_id *blob_id,
4826 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4828 return NULL;
4831 struct collect_merged_paths_arg {
4832 got_worktree_checkout_cb progress_cb;
4833 void *progress_arg;
4834 struct got_pathlist_head *merged_paths;
4837 static const struct got_error *
4838 collect_merged_paths(void *arg, unsigned char status, const char *path)
4840 const struct got_error *err;
4841 struct collect_merged_paths_arg *a = arg;
4842 char *p;
4843 struct got_pathlist_entry *new;
4845 err = (*a->progress_cb)(a->progress_arg, status, path);
4846 if (err)
4847 return err;
4849 if (status != GOT_STATUS_MERGE &&
4850 status != GOT_STATUS_ADD &&
4851 status != GOT_STATUS_DELETE &&
4852 status != GOT_STATUS_CONFLICT)
4853 return NULL;
4855 p = strdup(path);
4856 if (p == NULL)
4857 return got_error_from_errno("strdup");
4859 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4860 if (err || new == NULL)
4861 free(p);
4862 return err;
4865 void
4866 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4868 struct got_pathlist_entry *pe;
4870 TAILQ_FOREACH(pe, merged_paths, entry)
4871 free((char *)pe->path);
4873 got_pathlist_free(merged_paths);
4876 static const struct got_error *
4877 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4878 struct got_repository *repo)
4880 const struct got_error *err;
4881 struct got_reference *commit_ref = NULL;
4883 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4884 if (err) {
4885 if (err->code != GOT_ERR_NOT_REF)
4886 goto done;
4887 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4888 if (err)
4889 goto done;
4890 err = got_ref_write(commit_ref, repo);
4891 if (err)
4892 goto done;
4893 } else {
4894 struct got_object_id *stored_id;
4895 int cmp;
4897 err = got_ref_resolve(&stored_id, repo, commit_ref);
4898 if (err)
4899 goto done;
4900 cmp = got_object_id_cmp(commit_id, stored_id);
4901 free(stored_id);
4902 if (cmp != 0) {
4903 err = got_error(GOT_ERR_REBASE_COMMITID);
4904 goto done;
4907 done:
4908 if (commit_ref)
4909 got_ref_close(commit_ref);
4910 return err;
4913 static const struct got_error *
4914 rebase_merge_files(struct got_pathlist_head *merged_paths,
4915 const char *commit_ref_name, struct got_worktree *worktree,
4916 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4917 struct got_object_id *commit_id, struct got_repository *repo,
4918 got_worktree_checkout_cb progress_cb, void *progress_arg,
4919 got_cancel_cb cancel_cb, void *cancel_arg)
4921 const struct got_error *err;
4922 struct got_reference *commit_ref = NULL;
4923 struct collect_merged_paths_arg cmp_arg;
4924 char *fileindex_path;
4926 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4928 err = get_fileindex_path(&fileindex_path, worktree);
4929 if (err)
4930 return err;
4932 cmp_arg.progress_cb = progress_cb;
4933 cmp_arg.progress_arg = progress_arg;
4934 cmp_arg.merged_paths = merged_paths;
4935 err = merge_files(worktree, fileindex, fileindex_path,
4936 parent_commit_id, commit_id, repo, collect_merged_paths,
4937 &cmp_arg, cancel_cb, cancel_arg);
4938 if (commit_ref)
4939 got_ref_close(commit_ref);
4940 return err;
4943 const struct got_error *
4944 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4945 struct got_worktree *worktree, struct got_fileindex *fileindex,
4946 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4947 struct got_repository *repo,
4948 got_worktree_checkout_cb progress_cb, void *progress_arg,
4949 got_cancel_cb cancel_cb, void *cancel_arg)
4951 const struct got_error *err;
4952 char *commit_ref_name;
4954 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4955 if (err)
4956 return err;
4958 err = store_commit_id(commit_ref_name, commit_id, repo);
4959 if (err)
4960 goto done;
4962 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4963 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4964 progress_arg, cancel_cb, cancel_arg);
4965 done:
4966 free(commit_ref_name);
4967 return err;
4970 const struct got_error *
4971 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4972 struct got_worktree *worktree, struct got_fileindex *fileindex,
4973 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4974 struct got_repository *repo,
4975 got_worktree_checkout_cb progress_cb, void *progress_arg,
4976 got_cancel_cb cancel_cb, void *cancel_arg)
4978 const struct got_error *err;
4979 char *commit_ref_name;
4981 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4982 if (err)
4983 return err;
4985 err = store_commit_id(commit_ref_name, commit_id, repo);
4986 if (err)
4987 goto done;
4989 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4990 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4991 progress_arg, cancel_cb, cancel_arg);
4992 done:
4993 free(commit_ref_name);
4994 return err;
4997 static const struct got_error *
4998 rebase_commit(struct got_object_id **new_commit_id,
4999 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5000 struct got_worktree *worktree, struct got_fileindex *fileindex,
5001 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5002 const char *new_logmsg, struct got_repository *repo)
5004 const struct got_error *err, *sync_err;
5005 struct got_pathlist_head commitable_paths;
5006 struct collect_commitables_arg cc_arg;
5007 char *fileindex_path = NULL;
5008 struct got_reference *head_ref = NULL;
5009 struct got_object_id *head_commit_id = NULL;
5010 char *logmsg = NULL;
5012 TAILQ_INIT(&commitable_paths);
5013 *new_commit_id = NULL;
5015 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5017 err = get_fileindex_path(&fileindex_path, worktree);
5018 if (err)
5019 return err;
5021 cc_arg.commitable_paths = &commitable_paths;
5022 cc_arg.worktree = worktree;
5023 cc_arg.repo = repo;
5024 cc_arg.have_staged_files = 0;
5026 * If possible get the status of individual files directly to
5027 * avoid crawling the entire work tree once per rebased commit.
5028 * TODO: Ideally, merged_paths would contain a list of commitables
5029 * we could use so we could skip worktree_status() entirely.
5031 if (merged_paths) {
5032 struct got_pathlist_entry *pe;
5033 if (TAILQ_EMPTY(merged_paths)) {
5034 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5035 goto done;
5037 TAILQ_FOREACH(pe, merged_paths, entry) {
5038 err = worktree_status(worktree, pe->path, fileindex,
5039 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5040 0);
5041 if (err)
5042 goto done;
5044 } else {
5045 err = worktree_status(worktree, "", fileindex, repo,
5046 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5047 if (err)
5048 goto done;
5051 if (TAILQ_EMPTY(&commitable_paths)) {
5052 /* No-op change; commit will be elided. */
5053 err = got_ref_delete(commit_ref, repo);
5054 if (err)
5055 goto done;
5056 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5057 goto done;
5060 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5061 if (err)
5062 goto done;
5064 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5065 if (err)
5066 goto done;
5068 if (new_logmsg) {
5069 logmsg = strdup(new_logmsg);
5070 if (logmsg == NULL) {
5071 err = got_error_from_errno("strdup");
5072 goto done;
5074 } else {
5075 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5076 if (err)
5077 goto done;
5080 /* NB: commit_worktree will call free(logmsg) */
5081 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5082 worktree, got_object_commit_get_author(orig_commit),
5083 got_object_commit_get_committer(orig_commit),
5084 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5085 if (err)
5086 goto done;
5088 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5089 if (err)
5090 goto done;
5092 err = got_ref_delete(commit_ref, repo);
5093 if (err)
5094 goto done;
5096 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5097 fileindex, 0);
5098 sync_err = sync_fileindex(fileindex, fileindex_path);
5099 if (sync_err && err == NULL)
5100 err = sync_err;
5101 done:
5102 free(fileindex_path);
5103 free(head_commit_id);
5104 if (head_ref)
5105 got_ref_close(head_ref);
5106 if (err) {
5107 free(*new_commit_id);
5108 *new_commit_id = NULL;
5110 return err;
5113 const struct got_error *
5114 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5115 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5116 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5117 struct got_commit_object *orig_commit,
5118 struct got_object_id *orig_commit_id, struct got_repository *repo)
5120 const struct got_error *err;
5121 char *commit_ref_name;
5122 struct got_reference *commit_ref = NULL;
5123 struct got_object_id *commit_id = NULL;
5125 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5126 if (err)
5127 return err;
5129 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5130 if (err)
5131 goto done;
5132 err = got_ref_resolve(&commit_id, repo, commit_ref);
5133 if (err)
5134 goto done;
5135 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5136 err = got_error(GOT_ERR_REBASE_COMMITID);
5137 goto done;
5140 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5141 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5142 done:
5143 if (commit_ref)
5144 got_ref_close(commit_ref);
5145 free(commit_ref_name);
5146 free(commit_id);
5147 return err;
5150 const struct got_error *
5151 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5152 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5153 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5154 struct got_commit_object *orig_commit,
5155 struct got_object_id *orig_commit_id, const char *new_logmsg,
5156 struct got_repository *repo)
5158 const struct got_error *err;
5159 char *commit_ref_name;
5160 struct got_reference *commit_ref = NULL;
5161 struct got_object_id *commit_id = NULL;
5163 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5164 if (err)
5165 return err;
5167 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5168 if (err)
5169 goto done;
5170 err = got_ref_resolve(&commit_id, repo, commit_ref);
5171 if (err)
5172 goto done;
5173 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5174 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5175 goto done;
5178 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5179 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5180 done:
5181 if (commit_ref)
5182 got_ref_close(commit_ref);
5183 free(commit_ref_name);
5184 free(commit_id);
5185 return err;
5188 const struct got_error *
5189 got_worktree_rebase_postpone(struct got_worktree *worktree,
5190 struct got_fileindex *fileindex)
5192 if (fileindex)
5193 got_fileindex_free(fileindex);
5194 return lock_worktree(worktree, LOCK_SH);
5197 static const struct got_error *
5198 delete_ref(const char *name, struct got_repository *repo)
5200 const struct got_error *err;
5201 struct got_reference *ref;
5203 err = got_ref_open(&ref, repo, name, 0);
5204 if (err) {
5205 if (err->code == GOT_ERR_NOT_REF)
5206 return NULL;
5207 return err;
5210 err = got_ref_delete(ref, repo);
5211 got_ref_close(ref);
5212 return err;
5215 static const struct got_error *
5216 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5218 const struct got_error *err;
5219 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5220 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5222 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5223 if (err)
5224 goto done;
5225 err = delete_ref(tmp_branch_name, repo);
5226 if (err)
5227 goto done;
5229 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5230 if (err)
5231 goto done;
5232 err = delete_ref(new_base_branch_ref_name, repo);
5233 if (err)
5234 goto done;
5236 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5237 if (err)
5238 goto done;
5239 err = delete_ref(branch_ref_name, repo);
5240 if (err)
5241 goto done;
5243 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5244 if (err)
5245 goto done;
5246 err = delete_ref(commit_ref_name, repo);
5247 if (err)
5248 goto done;
5250 done:
5251 free(tmp_branch_name);
5252 free(new_base_branch_ref_name);
5253 free(branch_ref_name);
5254 free(commit_ref_name);
5255 return err;
5258 const struct got_error *
5259 got_worktree_rebase_complete(struct got_worktree *worktree,
5260 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5261 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5262 struct got_repository *repo)
5264 const struct got_error *err, *unlockerr;
5265 struct got_object_id *new_head_commit_id = NULL;
5267 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5268 if (err)
5269 return err;
5271 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5272 if (err)
5273 goto done;
5275 err = got_ref_write(rebased_branch, repo);
5276 if (err)
5277 goto done;
5279 err = got_worktree_set_head_ref(worktree, rebased_branch);
5280 if (err)
5281 goto done;
5283 err = delete_rebase_refs(worktree, repo);
5284 done:
5285 if (fileindex)
5286 got_fileindex_free(fileindex);
5287 free(new_head_commit_id);
5288 unlockerr = lock_worktree(worktree, LOCK_SH);
5289 if (unlockerr && err == NULL)
5290 err = unlockerr;
5291 return err;
5294 const struct got_error *
5295 got_worktree_rebase_abort(struct got_worktree *worktree,
5296 struct got_fileindex *fileindex, struct got_repository *repo,
5297 struct got_reference *new_base_branch,
5298 got_worktree_checkout_cb progress_cb, void *progress_arg)
5300 const struct got_error *err, *unlockerr, *sync_err;
5301 struct got_reference *resolved = NULL;
5302 struct got_object_id *commit_id = NULL;
5303 char *fileindex_path = NULL;
5304 struct revert_file_args rfa;
5305 struct got_object_id *tree_id = NULL;
5307 err = lock_worktree(worktree, LOCK_EX);
5308 if (err)
5309 return err;
5311 err = got_ref_open(&resolved, repo,
5312 got_ref_get_symref_target(new_base_branch), 0);
5313 if (err)
5314 goto done;
5316 err = got_worktree_set_head_ref(worktree, resolved);
5317 if (err)
5318 goto done;
5321 * XXX commits to the base branch could have happened while
5322 * we were busy rebasing; should we store the original commit ID
5323 * when rebase begins and read it back here?
5325 err = got_ref_resolve(&commit_id, repo, resolved);
5326 if (err)
5327 goto done;
5329 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5330 if (err)
5331 goto done;
5333 err = got_object_id_by_path(&tree_id, repo,
5334 worktree->base_commit_id, worktree->path_prefix);
5335 if (err)
5336 goto done;
5338 err = delete_rebase_refs(worktree, repo);
5339 if (err)
5340 goto done;
5342 err = get_fileindex_path(&fileindex_path, worktree);
5343 if (err)
5344 goto done;
5346 rfa.worktree = worktree;
5347 rfa.fileindex = fileindex;
5348 rfa.progress_cb = progress_cb;
5349 rfa.progress_arg = progress_arg;
5350 rfa.patch_cb = NULL;
5351 rfa.patch_arg = NULL;
5352 rfa.repo = repo;
5353 err = worktree_status(worktree, "", fileindex, repo,
5354 revert_file, &rfa, NULL, NULL, 0, 0);
5355 if (err)
5356 goto sync;
5358 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5359 repo, progress_cb, progress_arg, NULL, NULL);
5360 sync:
5361 sync_err = sync_fileindex(fileindex, fileindex_path);
5362 if (sync_err && err == NULL)
5363 err = sync_err;
5364 done:
5365 got_ref_close(resolved);
5366 free(tree_id);
5367 free(commit_id);
5368 if (fileindex)
5369 got_fileindex_free(fileindex);
5370 free(fileindex_path);
5372 unlockerr = lock_worktree(worktree, LOCK_SH);
5373 if (unlockerr && err == NULL)
5374 err = unlockerr;
5375 return err;
5378 const struct got_error *
5379 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5380 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5381 struct got_fileindex **fileindex, struct got_worktree *worktree,
5382 struct got_repository *repo)
5384 const struct got_error *err = NULL;
5385 char *tmp_branch_name = NULL;
5386 char *branch_ref_name = NULL;
5387 char *base_commit_ref_name = NULL;
5388 char *fileindex_path = NULL;
5389 struct check_rebase_ok_arg ok_arg;
5390 struct got_reference *wt_branch = NULL;
5391 struct got_reference *base_commit_ref = NULL;
5393 *tmp_branch = NULL;
5394 *branch_ref = NULL;
5395 *base_commit_id = NULL;
5396 *fileindex = NULL;
5398 err = lock_worktree(worktree, LOCK_EX);
5399 if (err)
5400 return err;
5402 err = open_fileindex(fileindex, &fileindex_path, worktree);
5403 if (err)
5404 goto done;
5406 ok_arg.worktree = worktree;
5407 ok_arg.repo = repo;
5408 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5409 &ok_arg);
5410 if (err)
5411 goto done;
5413 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5414 if (err)
5415 goto done;
5417 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5418 if (err)
5419 goto done;
5421 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5422 worktree);
5423 if (err)
5424 goto done;
5426 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5427 0);
5428 if (err)
5429 goto done;
5431 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5432 if (err)
5433 goto done;
5435 err = got_ref_write(*branch_ref, repo);
5436 if (err)
5437 goto done;
5439 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5440 worktree->base_commit_id);
5441 if (err)
5442 goto done;
5443 err = got_ref_write(base_commit_ref, repo);
5444 if (err)
5445 goto done;
5446 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5447 if (*base_commit_id == NULL) {
5448 err = got_error_from_errno("got_object_id_dup");
5449 goto done;
5452 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5453 worktree->base_commit_id);
5454 if (err)
5455 goto done;
5456 err = got_ref_write(*tmp_branch, repo);
5457 if (err)
5458 goto done;
5460 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5461 if (err)
5462 goto done;
5463 done:
5464 free(fileindex_path);
5465 free(tmp_branch_name);
5466 free(branch_ref_name);
5467 free(base_commit_ref_name);
5468 if (wt_branch)
5469 got_ref_close(wt_branch);
5470 if (err) {
5471 if (*branch_ref) {
5472 got_ref_close(*branch_ref);
5473 *branch_ref = NULL;
5475 if (*tmp_branch) {
5476 got_ref_close(*tmp_branch);
5477 *tmp_branch = NULL;
5479 free(*base_commit_id);
5480 if (*fileindex) {
5481 got_fileindex_free(*fileindex);
5482 *fileindex = NULL;
5484 lock_worktree(worktree, LOCK_SH);
5486 return err;
5489 const struct got_error *
5490 got_worktree_histedit_postpone(struct got_worktree *worktree,
5491 struct got_fileindex *fileindex)
5493 if (fileindex)
5494 got_fileindex_free(fileindex);
5495 return lock_worktree(worktree, LOCK_SH);
5498 const struct got_error *
5499 got_worktree_histedit_in_progress(int *in_progress,
5500 struct got_worktree *worktree)
5502 const struct got_error *err;
5503 char *tmp_branch_name = NULL;
5505 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5506 if (err)
5507 return err;
5509 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5510 free(tmp_branch_name);
5511 return NULL;
5514 const struct got_error *
5515 got_worktree_histedit_continue(struct got_object_id **commit_id,
5516 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5517 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5518 struct got_worktree *worktree, struct got_repository *repo)
5520 const struct got_error *err;
5521 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5522 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5523 struct got_reference *commit_ref = NULL;
5524 struct got_reference *base_commit_ref = NULL;
5525 char *fileindex_path = NULL;
5526 int have_staged_files = 0;
5528 *commit_id = NULL;
5529 *tmp_branch = NULL;
5530 *base_commit_id = NULL;
5531 *fileindex = NULL;
5533 err = lock_worktree(worktree, LOCK_EX);
5534 if (err)
5535 return err;
5537 err = open_fileindex(fileindex, &fileindex_path, worktree);
5538 if (err)
5539 goto done;
5541 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5542 &have_staged_files);
5543 if (err && err->code != GOT_ERR_CANCELLED)
5544 goto done;
5545 if (have_staged_files) {
5546 err = got_error(GOT_ERR_STAGED_PATHS);
5547 goto done;
5550 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5551 if (err)
5552 goto done;
5554 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5555 if (err)
5556 goto done;
5558 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5559 if (err)
5560 goto done;
5562 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5563 worktree);
5564 if (err)
5565 goto done;
5567 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5568 if (err)
5569 goto done;
5571 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5572 if (err)
5573 goto done;
5574 err = got_ref_resolve(commit_id, repo, commit_ref);
5575 if (err)
5576 goto done;
5578 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5579 if (err)
5580 goto done;
5581 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5582 if (err)
5583 goto done;
5585 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5586 if (err)
5587 goto done;
5588 done:
5589 free(commit_ref_name);
5590 free(branch_ref_name);
5591 free(fileindex_path);
5592 if (commit_ref)
5593 got_ref_close(commit_ref);
5594 if (base_commit_ref)
5595 got_ref_close(base_commit_ref);
5596 if (err) {
5597 free(*commit_id);
5598 *commit_id = NULL;
5599 free(*base_commit_id);
5600 *base_commit_id = NULL;
5601 if (*tmp_branch) {
5602 got_ref_close(*tmp_branch);
5603 *tmp_branch = NULL;
5605 if (*fileindex) {
5606 got_fileindex_free(*fileindex);
5607 *fileindex = NULL;
5609 lock_worktree(worktree, LOCK_EX);
5611 return err;
5614 static const struct got_error *
5615 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5617 const struct got_error *err;
5618 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5619 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5621 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5622 if (err)
5623 goto done;
5624 err = delete_ref(tmp_branch_name, repo);
5625 if (err)
5626 goto done;
5628 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5629 worktree);
5630 if (err)
5631 goto done;
5632 err = delete_ref(base_commit_ref_name, repo);
5633 if (err)
5634 goto done;
5636 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5637 if (err)
5638 goto done;
5639 err = delete_ref(branch_ref_name, repo);
5640 if (err)
5641 goto done;
5643 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5644 if (err)
5645 goto done;
5646 err = delete_ref(commit_ref_name, repo);
5647 if (err)
5648 goto done;
5649 done:
5650 free(tmp_branch_name);
5651 free(base_commit_ref_name);
5652 free(branch_ref_name);
5653 free(commit_ref_name);
5654 return err;
5657 const struct got_error *
5658 got_worktree_histedit_abort(struct got_worktree *worktree,
5659 struct got_fileindex *fileindex, struct got_repository *repo,
5660 struct got_reference *branch, struct got_object_id *base_commit_id,
5661 got_worktree_checkout_cb progress_cb, void *progress_arg)
5663 const struct got_error *err, *unlockerr, *sync_err;
5664 struct got_reference *resolved = NULL;
5665 char *fileindex_path = NULL;
5666 struct got_object_id *tree_id = NULL;
5667 struct revert_file_args rfa;
5669 err = lock_worktree(worktree, LOCK_EX);
5670 if (err)
5671 return err;
5673 err = got_ref_open(&resolved, repo,
5674 got_ref_get_symref_target(branch), 0);
5675 if (err)
5676 goto done;
5678 err = got_worktree_set_head_ref(worktree, resolved);
5679 if (err)
5680 goto done;
5682 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5683 if (err)
5684 goto done;
5686 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5687 worktree->path_prefix);
5688 if (err)
5689 goto done;
5691 err = delete_histedit_refs(worktree, repo);
5692 if (err)
5693 goto done;
5695 err = get_fileindex_path(&fileindex_path, worktree);
5696 if (err)
5697 goto done;
5699 rfa.worktree = worktree;
5700 rfa.fileindex = fileindex;
5701 rfa.progress_cb = progress_cb;
5702 rfa.progress_arg = progress_arg;
5703 rfa.patch_cb = NULL;
5704 rfa.patch_arg = NULL;
5705 rfa.repo = repo;
5706 err = worktree_status(worktree, "", fileindex, repo,
5707 revert_file, &rfa, NULL, NULL, 0, 0);
5708 if (err)
5709 goto sync;
5711 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5712 repo, progress_cb, progress_arg, NULL, NULL);
5713 sync:
5714 sync_err = sync_fileindex(fileindex, fileindex_path);
5715 if (sync_err && err == NULL)
5716 err = sync_err;
5717 done:
5718 got_ref_close(resolved);
5719 free(tree_id);
5720 free(fileindex_path);
5722 unlockerr = lock_worktree(worktree, LOCK_SH);
5723 if (unlockerr && err == NULL)
5724 err = unlockerr;
5725 return err;
5728 const struct got_error *
5729 got_worktree_histedit_complete(struct got_worktree *worktree,
5730 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5731 struct got_reference *edited_branch, struct got_repository *repo)
5733 const struct got_error *err, *unlockerr;
5734 struct got_object_id *new_head_commit_id = NULL;
5735 struct got_reference *resolved = NULL;
5737 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5738 if (err)
5739 return err;
5741 err = got_ref_open(&resolved, repo,
5742 got_ref_get_symref_target(edited_branch), 0);
5743 if (err)
5744 goto done;
5746 err = got_ref_change_ref(resolved, new_head_commit_id);
5747 if (err)
5748 goto done;
5750 err = got_ref_write(resolved, repo);
5751 if (err)
5752 goto done;
5754 err = got_worktree_set_head_ref(worktree, resolved);
5755 if (err)
5756 goto done;
5758 err = delete_histedit_refs(worktree, repo);
5759 done:
5760 if (fileindex)
5761 got_fileindex_free(fileindex);
5762 free(new_head_commit_id);
5763 unlockerr = lock_worktree(worktree, LOCK_SH);
5764 if (unlockerr && err == NULL)
5765 err = unlockerr;
5766 return err;
5769 const struct got_error *
5770 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5771 struct got_object_id *commit_id, struct got_repository *repo)
5773 const struct got_error *err;
5774 char *commit_ref_name;
5776 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5777 if (err)
5778 return err;
5780 err = store_commit_id(commit_ref_name, commit_id, repo);
5781 if (err)
5782 goto done;
5784 err = delete_ref(commit_ref_name, repo);
5785 done:
5786 free(commit_ref_name);
5787 return err;
5790 const struct got_error *
5791 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5792 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5793 struct got_worktree *worktree, const char *refname,
5794 struct got_repository *repo)
5796 const struct got_error *err = NULL;
5797 char *fileindex_path = NULL;
5798 struct check_rebase_ok_arg ok_arg;
5800 *fileindex = NULL;
5801 *branch_ref = NULL;
5802 *base_branch_ref = NULL;
5804 err = lock_worktree(worktree, LOCK_EX);
5805 if (err)
5806 return err;
5808 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5809 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5810 "cannot integrate a branch into itself; "
5811 "update -b or different branch name required");
5812 goto done;
5815 err = open_fileindex(fileindex, &fileindex_path, worktree);
5816 if (err)
5817 goto done;
5819 /* Preconditions are the same as for rebase. */
5820 ok_arg.worktree = worktree;
5821 ok_arg.repo = repo;
5822 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5823 &ok_arg);
5824 if (err)
5825 goto done;
5827 err = got_ref_open(branch_ref, repo, refname, 1);
5828 if (err)
5829 goto done;
5831 err = got_ref_open(base_branch_ref, repo,
5832 got_worktree_get_head_ref_name(worktree), 1);
5833 done:
5834 if (err) {
5835 if (*branch_ref) {
5836 got_ref_close(*branch_ref);
5837 *branch_ref = NULL;
5839 if (*base_branch_ref) {
5840 got_ref_close(*base_branch_ref);
5841 *base_branch_ref = NULL;
5843 if (*fileindex) {
5844 got_fileindex_free(*fileindex);
5845 *fileindex = NULL;
5847 lock_worktree(worktree, LOCK_SH);
5849 return err;
5852 const struct got_error *
5853 got_worktree_integrate_continue(struct got_worktree *worktree,
5854 struct got_fileindex *fileindex, struct got_repository *repo,
5855 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5856 got_worktree_checkout_cb progress_cb, void *progress_arg,
5857 got_cancel_cb cancel_cb, void *cancel_arg)
5859 const struct got_error *err = NULL, *sync_err, *unlockerr;
5860 char *fileindex_path = NULL;
5861 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5863 err = get_fileindex_path(&fileindex_path, worktree);
5864 if (err)
5865 goto done;
5867 err = got_ref_resolve(&commit_id, repo, branch_ref);
5868 if (err)
5869 goto done;
5871 err = got_object_id_by_path(&tree_id, repo, commit_id,
5872 worktree->path_prefix);
5873 if (err)
5874 goto done;
5876 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5877 if (err)
5878 goto done;
5880 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5881 progress_cb, progress_arg, cancel_cb, cancel_arg);
5882 if (err)
5883 goto sync;
5885 err = got_ref_change_ref(base_branch_ref, commit_id);
5886 if (err)
5887 goto sync;
5889 err = got_ref_write(base_branch_ref, repo);
5890 sync:
5891 sync_err = sync_fileindex(fileindex, fileindex_path);
5892 if (sync_err && err == NULL)
5893 err = sync_err;
5895 done:
5896 unlockerr = got_ref_unlock(branch_ref);
5897 if (unlockerr && err == NULL)
5898 err = unlockerr;
5899 got_ref_close(branch_ref);
5901 unlockerr = got_ref_unlock(base_branch_ref);
5902 if (unlockerr && err == NULL)
5903 err = unlockerr;
5904 got_ref_close(base_branch_ref);
5906 got_fileindex_free(fileindex);
5907 free(fileindex_path);
5908 free(tree_id);
5910 unlockerr = lock_worktree(worktree, LOCK_SH);
5911 if (unlockerr && err == NULL)
5912 err = unlockerr;
5913 return err;
5916 const struct got_error *
5917 got_worktree_integrate_abort(struct got_worktree *worktree,
5918 struct got_fileindex *fileindex, struct got_repository *repo,
5919 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5921 const struct got_error *err = NULL, *unlockerr = NULL;
5923 got_fileindex_free(fileindex);
5925 err = lock_worktree(worktree, LOCK_SH);
5927 unlockerr = got_ref_unlock(branch_ref);
5928 if (unlockerr && err == NULL)
5929 err = unlockerr;
5930 got_ref_close(branch_ref);
5932 unlockerr = got_ref_unlock(base_branch_ref);
5933 if (unlockerr && err == NULL)
5934 err = unlockerr;
5935 got_ref_close(base_branch_ref);
5937 return err;
5940 struct check_stage_ok_arg {
5941 struct got_object_id *head_commit_id;
5942 struct got_worktree *worktree;
5943 struct got_fileindex *fileindex;
5944 struct got_repository *repo;
5945 int have_changes;
5948 const struct got_error *
5949 check_stage_ok(void *arg, unsigned char status,
5950 unsigned char staged_status, const char *relpath,
5951 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5952 struct got_object_id *commit_id)
5954 struct check_stage_ok_arg *a = arg;
5955 const struct got_error *err = NULL;
5956 struct got_fileindex_entry *ie;
5957 struct got_object_id base_commit_id;
5958 struct got_object_id *base_commit_idp = NULL;
5959 char *in_repo_path = NULL, *p;
5961 if (status == GOT_STATUS_UNVERSIONED ||
5962 status == GOT_STATUS_NO_CHANGE)
5963 return NULL;
5964 if (status == GOT_STATUS_NONEXISTENT)
5965 return got_error_set_errno(ENOENT, relpath);
5967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5968 if (ie == NULL)
5969 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5971 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5972 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5973 relpath) == -1)
5974 return got_error_from_errno("asprintf");
5976 if (got_fileindex_entry_has_commit(ie)) {
5977 memcpy(base_commit_id.sha1, ie->commit_sha1,
5978 SHA1_DIGEST_LENGTH);
5979 base_commit_idp = &base_commit_id;
5982 if (status == GOT_STATUS_CONFLICT) {
5983 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5984 goto done;
5985 } else if (status != GOT_STATUS_ADD &&
5986 status != GOT_STATUS_MODIFY &&
5987 status != GOT_STATUS_DELETE) {
5988 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5989 goto done;
5992 a->have_changes = 1;
5994 p = in_repo_path;
5995 while (p[0] == '/')
5996 p++;
5997 err = check_out_of_date(p, status, staged_status,
5998 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5999 GOT_ERR_STAGE_OUT_OF_DATE);
6000 done:
6001 free(in_repo_path);
6002 return err;
6005 struct stage_path_arg {
6006 struct got_worktree *worktree;
6007 struct got_fileindex *fileindex;
6008 struct got_repository *repo;
6009 got_worktree_status_cb status_cb;
6010 void *status_arg;
6011 got_worktree_patch_cb patch_cb;
6012 void *patch_arg;
6013 int staged_something;
6016 static const struct got_error *
6017 stage_path(void *arg, unsigned char status,
6018 unsigned char staged_status, const char *relpath,
6019 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6020 struct got_object_id *commit_id)
6022 struct stage_path_arg *a = arg;
6023 const struct got_error *err = NULL;
6024 struct got_fileindex_entry *ie;
6025 char *ondisk_path = NULL, *path_content = NULL;
6026 uint32_t stage;
6027 struct got_object_id *new_staged_blob_id = NULL;
6029 if (status == GOT_STATUS_UNVERSIONED)
6030 return NULL;
6032 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6033 if (ie == NULL)
6034 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6036 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6037 relpath)== -1)
6038 return got_error_from_errno("asprintf");
6040 switch (status) {
6041 case GOT_STATUS_ADD:
6042 case GOT_STATUS_MODIFY:
6043 if (a->patch_cb) {
6044 if (status == GOT_STATUS_ADD) {
6045 int choice = GOT_PATCH_CHOICE_NONE;
6046 err = (*a->patch_cb)(&choice, a->patch_arg,
6047 status, ie->path, NULL, 1, 1);
6048 if (err)
6049 break;
6050 if (choice != GOT_PATCH_CHOICE_YES)
6051 break;
6052 } else {
6053 err = create_patched_content(&path_content, 0,
6054 staged_blob_id ? staged_blob_id : blob_id,
6055 ondisk_path, ie->path, a->repo,
6056 a->patch_cb, a->patch_arg);
6057 if (err || path_content == NULL)
6058 break;
6061 err = got_object_blob_create(&new_staged_blob_id,
6062 path_content ? path_content : ondisk_path, a->repo);
6063 if (err)
6064 break;
6065 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6066 SHA1_DIGEST_LENGTH);
6067 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6068 stage = GOT_FILEIDX_STAGE_ADD;
6069 else
6070 stage = GOT_FILEIDX_STAGE_MODIFY;
6071 got_fileindex_entry_stage_set(ie, stage);
6072 a->staged_something = 1;
6073 if (a->status_cb == NULL)
6074 break;
6075 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6076 get_staged_status(ie), relpath, blob_id,
6077 new_staged_blob_id, NULL);
6078 break;
6079 case GOT_STATUS_DELETE:
6080 if (staged_status == GOT_STATUS_DELETE)
6081 break;
6082 if (a->patch_cb) {
6083 int choice = GOT_PATCH_CHOICE_NONE;
6084 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6085 ie->path, NULL, 1, 1);
6086 if (err)
6087 break;
6088 if (choice == GOT_PATCH_CHOICE_NO)
6089 break;
6090 if (choice != GOT_PATCH_CHOICE_YES) {
6091 err = got_error(GOT_ERR_PATCH_CHOICE);
6092 break;
6095 stage = GOT_FILEIDX_STAGE_DELETE;
6096 got_fileindex_entry_stage_set(ie, stage);
6097 a->staged_something = 1;
6098 if (a->status_cb == NULL)
6099 break;
6100 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6101 get_staged_status(ie), relpath, NULL, NULL, NULL);
6102 break;
6103 case GOT_STATUS_NO_CHANGE:
6104 break;
6105 case GOT_STATUS_CONFLICT:
6106 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6107 break;
6108 case GOT_STATUS_NONEXISTENT:
6109 err = got_error_set_errno(ENOENT, relpath);
6110 break;
6111 default:
6112 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6113 break;
6116 if (path_content && unlink(path_content) == -1 && err == NULL)
6117 err = got_error_from_errno2("unlink", path_content);
6118 free(path_content);
6119 free(ondisk_path);
6120 free(new_staged_blob_id);
6121 return err;
6124 const struct got_error *
6125 got_worktree_stage(struct got_worktree *worktree,
6126 struct got_pathlist_head *paths,
6127 got_worktree_status_cb status_cb, void *status_arg,
6128 got_worktree_patch_cb patch_cb, void *patch_arg,
6129 struct got_repository *repo)
6131 const struct got_error *err = NULL, *sync_err, *unlockerr;
6132 struct got_pathlist_entry *pe;
6133 struct got_fileindex *fileindex = NULL;
6134 char *fileindex_path = NULL;
6135 struct got_reference *head_ref = NULL;
6136 struct got_object_id *head_commit_id = NULL;
6137 struct check_stage_ok_arg oka;
6138 struct stage_path_arg spa;
6140 err = lock_worktree(worktree, LOCK_EX);
6141 if (err)
6142 return err;
6144 err = got_ref_open(&head_ref, repo,
6145 got_worktree_get_head_ref_name(worktree), 0);
6146 if (err)
6147 goto done;
6148 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6149 if (err)
6150 goto done;
6151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6152 if (err)
6153 goto done;
6155 /* Check pre-conditions before staging anything. */
6156 oka.head_commit_id = head_commit_id;
6157 oka.worktree = worktree;
6158 oka.fileindex = fileindex;
6159 oka.repo = repo;
6160 oka.have_changes = 0;
6161 TAILQ_FOREACH(pe, paths, entry) {
6162 err = worktree_status(worktree, pe->path, fileindex, repo,
6163 check_stage_ok, &oka, NULL, NULL, 0, 0);
6164 if (err)
6165 goto done;
6167 if (!oka.have_changes) {
6168 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6169 goto done;
6172 spa.worktree = worktree;
6173 spa.fileindex = fileindex;
6174 spa.repo = repo;
6175 spa.patch_cb = patch_cb;
6176 spa.patch_arg = patch_arg;
6177 spa.status_cb = status_cb;
6178 spa.status_arg = status_arg;
6179 spa.staged_something = 0;
6180 TAILQ_FOREACH(pe, paths, entry) {
6181 err = worktree_status(worktree, pe->path, fileindex, repo,
6182 stage_path, &spa, NULL, NULL, 0, 0);
6183 if (err)
6184 goto done;
6186 if (!spa.staged_something) {
6187 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6188 goto done;
6191 sync_err = sync_fileindex(fileindex, fileindex_path);
6192 if (sync_err && err == NULL)
6193 err = sync_err;
6194 done:
6195 if (head_ref)
6196 got_ref_close(head_ref);
6197 free(head_commit_id);
6198 free(fileindex_path);
6199 if (fileindex)
6200 got_fileindex_free(fileindex);
6201 unlockerr = lock_worktree(worktree, LOCK_SH);
6202 if (unlockerr && err == NULL)
6203 err = unlockerr;
6204 return err;
6207 struct unstage_path_arg {
6208 struct got_worktree *worktree;
6209 struct got_fileindex *fileindex;
6210 struct got_repository *repo;
6211 got_worktree_checkout_cb progress_cb;
6212 void *progress_arg;
6213 got_worktree_patch_cb patch_cb;
6214 void *patch_arg;
6217 static const struct got_error *
6218 create_unstaged_content(char **path_unstaged_content,
6219 char **path_new_staged_content, struct got_object_id *blob_id,
6220 struct got_object_id *staged_blob_id, const char *relpath,
6221 struct got_repository *repo,
6222 got_worktree_patch_cb patch_cb, void *patch_arg)
6224 const struct got_error *err;
6225 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6226 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6227 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6228 struct stat sb1, sb2;
6229 struct got_diff_changes *changes = NULL;
6230 struct got_diff_state *ds = NULL;
6231 struct got_diff_args *args = NULL;
6232 struct got_diff_change *change;
6233 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6234 int have_content = 0, have_rejected_content = 0;
6236 *path_unstaged_content = NULL;
6237 *path_new_staged_content = NULL;
6239 err = got_object_id_str(&label1, blob_id);
6240 if (err)
6241 return err;
6242 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6243 if (err)
6244 goto done;
6246 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6247 if (err)
6248 goto done;
6250 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6251 if (err)
6252 goto done;
6254 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6255 if (err)
6256 goto done;
6258 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6259 if (err)
6260 goto done;
6262 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6263 if (err)
6264 goto done;
6266 if (stat(path1, &sb1) == -1) {
6267 err = got_error_from_errno2("stat", path1);
6268 goto done;
6271 if (stat(path2, &sb2) == -1) {
6272 err = got_error_from_errno2("stat", path2);
6273 goto done;
6276 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6277 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6278 if (err)
6279 goto done;
6281 err = got_opentemp_named(path_unstaged_content, &outfile,
6282 "got-unstaged-content");
6283 if (err)
6284 goto done;
6285 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6286 "got-new-staged-content");
6287 if (err)
6288 goto done;
6290 if (fseek(f1, 0L, SEEK_SET) == -1) {
6291 err = got_ferror(f1, GOT_ERR_IO);
6292 goto done;
6294 if (fseek(f2, 0L, SEEK_SET) == -1) {
6295 err = got_ferror(f2, GOT_ERR_IO);
6296 goto done;
6298 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6299 int choice;
6300 err = apply_or_reject_change(&choice, change, ++n,
6301 changes->nchanges, ds, args, diff_flags, relpath,
6302 f1, f2, &line_cur1, &line_cur2,
6303 outfile, rejectfile, patch_cb, patch_arg);
6304 if (err)
6305 goto done;
6306 if (choice == GOT_PATCH_CHOICE_YES)
6307 have_content = 1;
6308 else
6309 have_rejected_content = 1;
6310 if (choice == GOT_PATCH_CHOICE_QUIT)
6311 break;
6313 if (have_content || have_rejected_content)
6314 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6315 outfile, rejectfile);
6316 done:
6317 free(label1);
6318 if (blob)
6319 got_object_blob_close(blob);
6320 if (staged_blob)
6321 got_object_blob_close(staged_blob);
6322 if (f1 && fclose(f1) == EOF && err == NULL)
6323 err = got_error_from_errno2("fclose", path1);
6324 if (f2 && fclose(f2) == EOF && err == NULL)
6325 err = got_error_from_errno2("fclose", path2);
6326 if (outfile && fclose(outfile) == EOF && err == NULL)
6327 err = got_error_from_errno2("fclose", *path_unstaged_content);
6328 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6329 err = got_error_from_errno2("fclose", *path_new_staged_content);
6330 if (path1 && unlink(path1) == -1 && err == NULL)
6331 err = got_error_from_errno2("unlink", path1);
6332 if (path2 && unlink(path2) == -1 && err == NULL)
6333 err = got_error_from_errno2("unlink", path2);
6334 if (err || !have_content) {
6335 if (*path_unstaged_content &&
6336 unlink(*path_unstaged_content) == -1 && err == NULL)
6337 err = got_error_from_errno2("unlink",
6338 *path_unstaged_content);
6339 free(*path_unstaged_content);
6340 *path_unstaged_content = NULL;
6342 if (err || !have_rejected_content) {
6343 if (*path_new_staged_content &&
6344 unlink(*path_new_staged_content) == -1 && err == NULL)
6345 err = got_error_from_errno2("unlink",
6346 *path_new_staged_content);
6347 free(*path_new_staged_content);
6348 *path_new_staged_content = NULL;
6350 free(args);
6351 if (ds) {
6352 got_diff_state_free(ds);
6353 free(ds);
6355 if (changes)
6356 got_diff_free_changes(changes);
6357 free(path1);
6358 free(path2);
6359 return err;
6362 static const struct got_error *
6363 unstage_path(void *arg, unsigned char status,
6364 unsigned char staged_status, const char *relpath,
6365 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6366 struct got_object_id *commit_id)
6368 const struct got_error *err = NULL;
6369 struct unstage_path_arg *a = arg;
6370 struct got_fileindex_entry *ie;
6371 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6372 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6373 char *path_new_staged_content = NULL;
6374 char *id_str = NULL, *label_orig = NULL;
6375 int local_changes_subsumed;
6376 struct stat sb;
6378 if (staged_status != GOT_STATUS_ADD &&
6379 staged_status != GOT_STATUS_MODIFY &&
6380 staged_status != GOT_STATUS_DELETE)
6381 return NULL;
6383 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6384 if (ie == NULL)
6385 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6387 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6388 == -1)
6389 return got_error_from_errno("asprintf");
6391 err = got_object_id_str(&id_str,
6392 commit_id ? commit_id : a->worktree->base_commit_id);
6393 if (err)
6394 goto done;
6395 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6396 id_str) == -1) {
6397 err = got_error_from_errno("asprintf");
6398 goto done;
6401 switch (staged_status) {
6402 case GOT_STATUS_MODIFY:
6403 err = got_object_open_as_blob(&blob_base, a->repo,
6404 blob_id, 8192);
6405 if (err)
6406 break;
6407 /* fall through */
6408 case GOT_STATUS_ADD:
6409 if (a->patch_cb) {
6410 if (staged_status == GOT_STATUS_ADD) {
6411 int choice = GOT_PATCH_CHOICE_NONE;
6412 err = (*a->patch_cb)(&choice, a->patch_arg,
6413 staged_status, ie->path, NULL, 1, 1);
6414 if (err)
6415 break;
6416 if (choice != GOT_PATCH_CHOICE_YES)
6417 break;
6418 } else {
6419 err = create_unstaged_content(
6420 &path_unstaged_content,
6421 &path_new_staged_content, blob_id,
6422 staged_blob_id, ie->path, a->repo,
6423 a->patch_cb, a->patch_arg);
6424 if (err || path_unstaged_content == NULL)
6425 break;
6426 if (path_new_staged_content) {
6427 err = got_object_blob_create(
6428 &staged_blob_id,
6429 path_new_staged_content,
6430 a->repo);
6431 if (err)
6432 break;
6433 memcpy(ie->staged_blob_sha1,
6434 staged_blob_id->sha1,
6435 SHA1_DIGEST_LENGTH);
6437 err = merge_file(&local_changes_subsumed,
6438 a->worktree, blob_base, ondisk_path,
6439 relpath, got_fileindex_perms_to_st(ie),
6440 path_unstaged_content, label_orig,
6441 "unstaged", a->repo, a->progress_cb,
6442 a->progress_arg);
6443 if (err == NULL &&
6444 path_new_staged_content == NULL)
6445 got_fileindex_entry_stage_set(ie,
6446 GOT_FILEIDX_STAGE_NONE);
6447 break; /* Done with this file. */
6450 err = got_object_open_as_blob(&blob_staged, a->repo,
6451 staged_blob_id, 8192);
6452 if (err)
6453 break;
6454 err = merge_blob(&local_changes_subsumed, a->worktree,
6455 blob_base, ondisk_path, relpath,
6456 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6457 commit_id ? commit_id : a->worktree->base_commit_id,
6458 a->repo, a->progress_cb, a->progress_arg);
6459 if (err == NULL)
6460 got_fileindex_entry_stage_set(ie,
6461 GOT_FILEIDX_STAGE_NONE);
6462 break;
6463 case GOT_STATUS_DELETE:
6464 if (a->patch_cb) {
6465 int choice = GOT_PATCH_CHOICE_NONE;
6466 err = (*a->patch_cb)(&choice, a->patch_arg,
6467 staged_status, ie->path, NULL, 1, 1);
6468 if (err)
6469 break;
6470 if (choice == GOT_PATCH_CHOICE_NO)
6471 break;
6472 if (choice != GOT_PATCH_CHOICE_YES) {
6473 err = got_error(GOT_ERR_PATCH_CHOICE);
6474 break;
6477 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6478 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
6479 a->repo);
6480 if (err)
6481 break;
6482 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6483 break;
6485 done:
6486 free(ondisk_path);
6487 if (path_unstaged_content &&
6488 unlink(path_unstaged_content) == -1 && err == NULL)
6489 err = got_error_from_errno2("unlink", path_unstaged_content);
6490 if (path_new_staged_content &&
6491 unlink(path_new_staged_content) == -1 && err == NULL)
6492 err = got_error_from_errno2("unlink", path_new_staged_content);
6493 free(path_unstaged_content);
6494 free(path_new_staged_content);
6495 if (blob_base)
6496 got_object_blob_close(blob_base);
6497 if (blob_staged)
6498 got_object_blob_close(blob_staged);
6499 free(id_str);
6500 free(label_orig);
6501 return err;
6504 const struct got_error *
6505 got_worktree_unstage(struct got_worktree *worktree,
6506 struct got_pathlist_head *paths,
6507 got_worktree_checkout_cb progress_cb, void *progress_arg,
6508 got_worktree_patch_cb patch_cb, void *patch_arg,
6509 struct got_repository *repo)
6511 const struct got_error *err = NULL, *sync_err, *unlockerr;
6512 struct got_pathlist_entry *pe;
6513 struct got_fileindex *fileindex = NULL;
6514 char *fileindex_path = NULL;
6515 struct unstage_path_arg upa;
6517 err = lock_worktree(worktree, LOCK_EX);
6518 if (err)
6519 return err;
6521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6522 if (err)
6523 goto done;
6525 upa.worktree = worktree;
6526 upa.fileindex = fileindex;
6527 upa.repo = repo;
6528 upa.progress_cb = progress_cb;
6529 upa.progress_arg = progress_arg;
6530 upa.patch_cb = patch_cb;
6531 upa.patch_arg = patch_arg;
6532 TAILQ_FOREACH(pe, paths, entry) {
6533 err = worktree_status(worktree, pe->path, fileindex, repo,
6534 unstage_path, &upa, NULL, NULL, 0, 0);
6535 if (err)
6536 goto done;
6539 sync_err = sync_fileindex(fileindex, fileindex_path);
6540 if (sync_err && err == NULL)
6541 err = sync_err;
6542 done:
6543 free(fileindex_path);
6544 if (fileindex)
6545 got_fileindex_free(fileindex);
6546 unlockerr = lock_worktree(worktree, LOCK_SH);
6547 if (unlockerr && err == NULL)
6548 err = unlockerr;
6549 return err;