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 <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
45 #include "got_diff.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 static const struct got_error *
63 create_meta_file(const char *path_got, const char *name, const char *content)
64 {
65 const struct got_error *err = NULL;
66 char *path;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1)
69 return got_error_from_errno("asprintf");
71 err = got_path_create_file(path, content);
72 free(path);
73 return err;
74 }
76 static const struct got_error *
77 update_meta_file(const char *path_got, const char *name, const char *content)
78 {
79 const struct got_error *err = NULL;
80 FILE *tmpfile = NULL;
81 char *tmppath = NULL;
82 char *path = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
85 err = got_error_from_errno("asprintf");
86 path = NULL;
87 goto done;
88 }
90 err = got_opentemp_named(&tmppath, &tmpfile, path);
91 if (err)
92 goto done;
94 if (content) {
95 int len = fprintf(tmpfile, "%s\n", content);
96 if (len != strlen(content) + 1) {
97 err = got_error_from_errno2("fprintf", tmppath);
98 goto done;
99 }
102 if (rename(tmppath, path) != 0) {
103 err = got_error_from_errno3("rename", tmppath, path);
104 unlink(tmppath);
105 goto done;
108 done:
109 if (fclose(tmpfile) != 0 && err == NULL)
110 err = got_error_from_errno2("fclose", tmppath);
111 free(tmppath);
112 return err;
115 static const struct got_error *
116 read_meta_file(char **content, const char *path_got, const char *name)
118 const struct got_error *err = NULL;
119 char *path;
120 int fd = -1;
121 ssize_t n;
122 struct stat sb;
124 *content = NULL;
126 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
127 err = got_error_from_errno("asprintf");
128 path = NULL;
129 goto done;
132 fd = open(path, O_RDONLY | O_NOFOLLOW);
133 if (fd == -1) {
134 if (errno == ENOENT)
135 err = got_error_path(path, GOT_ERR_WORKTREE_META);
136 else
137 err = got_error_from_errno2("open", path);
138 goto done;
140 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
141 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
142 : got_error_from_errno2("flock", path));
143 goto done;
146 if (fstat(fd, &sb) != 0) {
147 err = got_error_from_errno2("fstat", path);
148 goto done;
150 *content = calloc(1, sb.st_size);
151 if (*content == NULL) {
152 err = got_error_from_errno("calloc");
153 goto done;
156 n = read(fd, *content, sb.st_size);
157 if (n != sb.st_size) {
158 err = (n == -1 ? got_error_from_errno2("read", path) :
159 got_error_path(path, GOT_ERR_WORKTREE_META));
160 goto done;
162 if ((*content)[sb.st_size - 1] != '\n') {
163 err = got_error_path(path, GOT_ERR_WORKTREE_META);
164 goto done;
166 (*content)[sb.st_size - 1] = '\0';
168 done:
169 if (fd != -1 && close(fd) == -1 && err == NULL)
170 err = got_error_from_errno2("close", path_got);
171 free(path);
172 if (err) {
173 free(*content);
174 *content = NULL;
176 return err;
179 static const struct got_error *
180 write_head_ref(const char *path_got, struct got_reference *head_ref)
182 const struct got_error *err = NULL;
183 char *refstr = NULL;
185 if (got_ref_is_symbolic(head_ref)) {
186 refstr = got_ref_to_str(head_ref);
187 if (refstr == NULL)
188 return got_error_from_errno("got_ref_to_str");
189 } else {
190 refstr = strdup(got_ref_get_name(head_ref));
191 if (refstr == NULL)
192 return got_error_from_errno("strdup");
194 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
195 free(refstr);
196 return err;
199 const struct got_error *
200 got_worktree_init(const char *path, struct got_reference *head_ref,
201 const char *prefix, struct got_repository *repo)
203 const struct got_error *err = NULL;
204 struct got_object_id *commit_id = NULL;
205 uuid_t uuid;
206 uint32_t uuid_status;
207 int obj_type;
208 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
216 goto done;
219 err = got_ref_resolve(&commit_id, repo, head_ref);
220 if (err)
221 return err;
222 err = got_object_get_type(&obj_type, repo, commit_id);
223 if (err)
224 return err;
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno2("mkdir", path);
236 goto done;
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno("asprintf");
242 goto done;
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno2("mkdir", path_got);
246 goto done;
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
251 if (err)
252 goto done;
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
256 if (err)
257 goto done;
259 /* Write the HEAD reference. */
260 err = write_head_ref(path_got, head_ref);
261 if (err)
262 goto done;
264 /* Record our base commit. */
265 err = got_object_id_str(&basestr, commit_id);
266 if (err)
267 goto done;
268 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
269 if (err)
270 goto done;
272 /* Store path to repository. */
273 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
274 got_repo_get_path(repo));
275 if (err)
276 goto done;
278 /* Store in-repository path prefix. */
279 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
280 absprefix ? absprefix : prefix);
281 if (err)
282 goto done;
284 /* Generate UUID. */
285 uuid_create(&uuid, &uuid_status);
286 if (uuid_status != uuid_s_ok) {
287 err = got_error_uuid(uuid_status, "uuid_create");
288 goto done;
290 uuid_to_string(&uuid, &uuidstr, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_to_string");
293 goto done;
295 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
296 if (err)
297 goto done;
299 /* Stamp work tree with format file. */
300 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
301 err = got_error_from_errno("asprintf");
302 goto done;
304 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
305 if (err)
306 goto done;
308 done:
309 free(commit_id);
310 free(path_got);
311 free(formatstr);
312 free(absprefix);
313 free(basestr);
314 free(uuidstr);
315 return err;
318 static const struct got_error *
319 open_worktree(struct got_worktree **worktree, const char *path)
321 const struct got_error *err = NULL;
322 char *path_got;
323 char *formatstr = NULL;
324 char *uuidstr = NULL;
325 char *path_lock = NULL;
326 char *base_commit_id_str = NULL;
327 int version, fd = -1;
328 const char *errstr;
329 struct got_repository *repo = NULL;
330 uint32_t uuid_status;
332 *worktree = NULL;
334 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
335 err = got_error_from_errno("asprintf");
336 path_got = NULL;
337 goto done;
340 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
341 err = got_error_from_errno("asprintf");
342 path_lock = NULL;
343 goto done;
346 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 if (fd == -1) {
348 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 : got_error_from_errno2("open", path_lock));
350 goto done;
353 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
354 if (err)
355 goto done;
357 version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 if (errstr) {
359 err = got_error_msg(GOT_ERR_WORKTREE_META,
360 "could not parse work tree format version number");
361 goto done;
363 if (version != GOT_WORKTREE_FORMAT_VERSION) {
364 err = got_error(GOT_ERR_WORKTREE_VERS);
365 goto done;
368 *worktree = calloc(1, sizeof(**worktree));
369 if (*worktree == NULL) {
370 err = got_error_from_errno("calloc");
371 goto done;
373 (*worktree)->lockfd = -1;
375 (*worktree)->root_path = strdup(path);
376 if ((*worktree)->root_path == NULL) {
377 err = got_error_from_errno("strdup");
378 goto done;
380 err = read_meta_file(&(*worktree)->repo_path, path_got,
381 GOT_WORKTREE_REPOSITORY);
382 if (err)
383 goto done;
385 err = read_meta_file(&(*worktree)->path_prefix, path_got,
386 GOT_WORKTREE_PATH_PREFIX);
387 if (err)
388 goto done;
390 err = read_meta_file(&base_commit_id_str, path_got,
391 GOT_WORKTREE_BASE_COMMIT);
392 if (err)
393 goto done;
395 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 if (err)
397 goto done;
398 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
399 if (uuid_status != uuid_s_ok) {
400 err = got_error_uuid(uuid_status, "uuid_from_string");
401 goto done;
404 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
405 if (err)
406 goto done;
408 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
409 base_commit_id_str);
410 if (err)
411 goto done;
413 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
414 GOT_WORKTREE_HEAD_REF);
415 done:
416 if (repo)
417 got_repo_close(repo);
418 free(path_got);
419 free(path_lock);
420 free(base_commit_id_str);
421 free(uuidstr);
422 free(formatstr);
423 if (err) {
424 if (fd != -1)
425 close(fd);
426 if (*worktree != NULL)
427 got_worktree_close(*worktree);
428 *worktree = NULL;
429 } else
430 (*worktree)->lockfd = fd;
432 return err;
435 const struct got_error *
436 got_worktree_open(struct got_worktree **worktree, const char *path)
438 const struct got_error *err = NULL;
440 do {
441 err = open_worktree(worktree, path);
442 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
443 return err;
444 if (*worktree)
445 return NULL;
446 path = dirname(path);
447 if (path == NULL)
448 return got_error_from_errno2("dirname", path);
449 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
451 return got_error(GOT_ERR_NOT_WORKTREE);
454 const struct got_error *
455 got_worktree_close(struct got_worktree *worktree)
457 const struct got_error *err = NULL;
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree->root_path);
467 free(worktree);
468 return err;
471 const char *
472 got_worktree_get_root_path(struct got_worktree *worktree)
474 return worktree->root_path;
477 const char *
478 got_worktree_get_repo_path(struct got_worktree *worktree)
480 return worktree->repo_path;
483 const char *
484 got_worktree_get_path_prefix(struct got_worktree *worktree)
486 return worktree->path_prefix;
489 const struct got_error *
490 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
491 const char *path_prefix)
493 char *absprefix = NULL;
495 if (!got_path_is_absolute(path_prefix)) {
496 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
497 return got_error_from_errno("asprintf");
499 *match = (strcmp(absprefix ? absprefix : path_prefix,
500 worktree->path_prefix) == 0);
501 free(absprefix);
502 return NULL;
505 const char *
506 got_worktree_get_head_ref_name(struct got_worktree *worktree)
508 return worktree->head_ref_name;
511 const struct got_error *
512 got_worktree_set_head_ref(struct got_worktree *worktree,
513 struct got_reference *head_ref)
515 const struct got_error *err = NULL;
516 char *path_got = NULL, *head_ref_name = NULL;
518 if (asprintf(&path_got, "%s/%s", worktree->root_path,
519 GOT_WORKTREE_GOT_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
521 path_got = NULL;
522 goto done;
525 head_ref_name = strdup(got_ref_get_name(head_ref));
526 if (head_ref_name == NULL) {
527 err = got_error_from_errno("strdup");
528 goto done;
531 err = write_head_ref(path_got, head_ref);
532 if (err)
533 goto done;
535 free(worktree->head_ref_name);
536 worktree->head_ref_name = head_ref_name;
537 done:
538 free(path_got);
539 if (err)
540 free(head_ref_name);
541 return err;
544 struct got_object_id *
545 got_worktree_get_base_commit_id(struct got_worktree *worktree)
547 return worktree->base_commit_id;
550 const struct got_error *
551 got_worktree_set_base_commit_id(struct got_worktree *worktree,
552 struct got_repository *repo, struct got_object_id *commit_id)
554 const struct got_error *err;
555 struct got_object *obj = NULL;
556 char *id_str = NULL;
557 char *path_got = NULL;
559 if (asprintf(&path_got, "%s/%s", worktree->root_path,
560 GOT_WORKTREE_GOT_DIR) == -1) {
561 err = got_error_from_errno("asprintf");
562 path_got = NULL;
563 goto done;
566 err = got_object_open(&obj, repo, commit_id);
567 if (err)
568 return err;
570 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
571 err = got_error(GOT_ERR_OBJ_TYPE);
572 goto done;
575 /* Record our base commit. */
576 err = got_object_id_str(&id_str, commit_id);
577 if (err)
578 goto done;
579 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
580 if (err)
581 goto done;
583 free(worktree->base_commit_id);
584 worktree->base_commit_id = got_object_id_dup(commit_id);
585 if (worktree->base_commit_id == NULL) {
586 err = got_error_from_errno("got_object_id_dup");
587 goto done;
589 done:
590 if (obj)
591 got_object_close(obj);
592 free(id_str);
593 free(path_got);
594 return err;
597 static const struct got_error *
598 lock_worktree(struct got_worktree *worktree, int operation)
600 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
601 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
602 : got_error_from_errno2("flock",
603 got_worktree_get_root_path(worktree)));
604 return NULL;
607 static const struct got_error *
608 add_dir_on_disk(struct got_worktree *worktree, const char *path)
610 const struct got_error *err = NULL;
611 char *abspath;
613 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
614 return got_error_from_errno("asprintf");
616 err = got_path_mkdir(abspath);
617 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
618 struct stat sb;
619 err = NULL;
620 if (lstat(abspath, &sb) == -1) {
621 err = got_error_from_errno2("lstat", abspath);
622 } else if (!S_ISDIR(sb.st_mode)) {
623 /* TODO directory is obstructed; do something */
624 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
627 free(abspath);
628 return err;
631 static const struct got_error *
632 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
634 const struct got_error *err = NULL;
635 uint8_t fbuf1[8192];
636 uint8_t fbuf2[8192];
637 size_t flen1 = 0, flen2 = 0;
639 *same = 1;
641 for (;;) {
642 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
643 if (flen1 == 0 && ferror(f1)) {
644 err = got_error_from_errno("fread");
645 break;
647 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
648 if (flen2 == 0 && ferror(f2)) {
649 err = got_error_from_errno("fread");
650 break;
652 if (flen1 == 0) {
653 if (flen2 != 0)
654 *same = 0;
655 break;
656 } else if (flen2 == 0) {
657 if (flen1 != 0)
658 *same = 0;
659 break;
660 } else if (flen1 == flen2) {
661 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
662 *same = 0;
663 break;
665 } else {
666 *same = 0;
667 break;
671 return err;
674 static const struct got_error *
675 check_files_equal(int *same, const char *f1_path, const char *f2_path)
677 const struct got_error *err = NULL;
678 struct stat sb;
679 size_t size1, size2;
680 FILE *f1 = NULL, *f2 = NULL;
682 *same = 1;
684 if (lstat(f1_path, &sb) != 0) {
685 err = got_error_from_errno2("lstat", f1_path);
686 goto done;
688 size1 = sb.st_size;
690 if (lstat(f2_path, &sb) != 0) {
691 err = got_error_from_errno2("lstat", f2_path);
692 goto done;
694 size2 = sb.st_size;
696 if (size1 != size2) {
697 *same = 0;
698 return NULL;
701 f1 = fopen(f1_path, "r");
702 if (f1 == NULL)
703 return got_error_from_errno2("open", f1_path);
705 f2 = fopen(f2_path, "r");
706 if (f2 == NULL) {
707 err = got_error_from_errno2("open", f2_path);
708 goto done;
711 err = check_file_contents_equal(same, f1, f2);
712 done:
713 if (f1 && fclose(f1) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
715 if (f2 && fclose(f2) != 0 && err == NULL)
716 err = got_error_from_errno("fclose");
718 return err;
721 /*
722 * Perform a 3-way merge where blob_orig acts as the common ancestor,
723 * the file at deriv_path acts as the first derived version, and the
724 * file on disk acts as the second derived version.
725 */
726 static const struct got_error *
727 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
728 struct got_blob_object *blob_orig, const char *ondisk_path,
729 const char *path, uint16_t st_mode, const char *deriv_path,
730 const char *label_deriv, struct got_repository *repo,
731 got_worktree_checkout_cb progress_cb, void *progress_arg)
733 const struct got_error *err = NULL;
734 int merged_fd = -1;
735 FILE *f_orig = NULL;
736 char *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
738 int overlapcnt = 0;
739 char *parent;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
744 if (parent == NULL)
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
751 if (err)
752 goto done;
754 free(base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
756 err = got_error_from_errno("asprintf");
757 base_path = NULL;
758 goto done;
761 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
762 if (err)
763 goto done;
764 if (blob_orig) {
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
766 blob_orig);
767 if (err)
768 goto done;
769 } else {
770 /*
771 * If the file has no blob, this is an "add vs add" conflict,
772 * and we simply use an empty ancestor file to make both files
773 * appear in the merged result in their entirety.
774 */
777 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
778 blob_orig_path, ondisk_path, label_deriv, path);
779 if (err)
780 goto done;
782 err = (*progress_cb)(progress_arg,
783 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (err)
785 goto done;
787 if (fsync(merged_fd) != 0) {
788 err = got_error_from_errno("fsync");
789 goto done;
792 /* Check if a clean merge has subsumed all local changes. */
793 if (overlapcnt == 0) {
794 err = check_files_equal(local_changes_subsumed, deriv_path,
795 merged_path);
796 if (err)
797 goto done;
800 if (chmod(merged_path, st_mode) != 0) {
801 err = got_error_from_errno2("chmod", merged_path);
802 goto done;
805 if (rename(merged_path, ondisk_path) != 0) {
806 err = got_error_from_errno3("rename", merged_path,
807 ondisk_path);
808 unlink(merged_path);
809 goto done;
812 done:
813 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
814 err = got_error_from_errno("close");
815 if (f_orig && fclose(f_orig) != 0 && err == NULL)
816 err = got_error_from_errno("fclose");
817 free(merged_path);
818 free(base_path);
819 if (blob_orig_path) {
820 unlink(blob_orig_path);
821 free(blob_orig_path);
823 return err;
826 /*
827 * Perform a 3-way merge where blob_orig acts as the common ancestor,
828 * blob_deriv acts as the first derived version, and the file on disk
829 * acts as the second derived version.
830 */
831 static const struct got_error *
832 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
833 struct got_blob_object *blob_orig, const char *ondisk_path,
834 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
835 struct got_object_id *deriv_base_commit_id,
836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
837 void *progress_arg)
839 const struct got_error *err = NULL;
840 FILE *f_deriv = NULL;
841 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
842 char *label_deriv = NULL, *parent;
844 *local_changes_subsumed = 0;
846 parent = dirname(ondisk_path);
847 if (parent == NULL)
848 return got_error_from_errno2("dirname", ondisk_path);
850 free(base_path);
851 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
852 err = got_error_from_errno("asprintf");
853 base_path = NULL;
854 goto done;
857 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
858 if (err)
859 goto done;
860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
861 blob_deriv);
862 if (err)
863 goto done;
865 err = got_object_id_str(&id_str, deriv_base_commit_id);
866 if (err)
867 goto done;
868 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
869 err = got_error_from_errno("asprintf");
870 goto done;
873 err = merge_file(local_changes_subsumed, worktree, blob_orig,
874 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
875 repo, progress_cb, progress_arg);
876 done:
877 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
878 err = got_error_from_errno("fclose");
879 free(base_path);
880 if (blob_deriv_path) {
881 unlink(blob_deriv_path);
882 free(blob_deriv_path);
884 free(id_str);
885 free(label_deriv);
886 return err;
889 static const struct got_error *
890 update_blob_fileindex_entry(struct got_worktree *worktree,
891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
892 const char *ondisk_path, const char *path, struct got_blob_object *blob,
893 int update_timestamps)
895 const struct got_error *err = NULL;
897 if (ie == NULL)
898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
899 if (ie)
900 err = got_fileindex_entry_update(ie, ondisk_path,
901 blob->id.sha1, worktree->base_commit_id->sha1,
902 update_timestamps);
903 else {
904 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
906 path, blob->id.sha1, worktree->base_commit_id->sha1);
907 if (!err)
908 err = got_fileindex_entry_add(fileindex, new_ie);
910 return err;
913 static const struct got_error *
914 install_blob(struct got_worktree *worktree, const char *ondisk_path,
915 const char *path, uint16_t te_mode, uint16_t st_mode,
916 struct got_blob_object *blob, int restoring_missing_file,
917 int reverting_versioned_file, struct got_repository *repo,
918 got_worktree_checkout_cb progress_cb, void *progress_arg)
920 const struct got_error *err = NULL;
921 int fd = -1;
922 size_t len, hdrlen;
923 int update = 0;
924 char *tmppath = NULL;
926 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
927 GOT_DEFAULT_FILE_MODE);
928 if (fd == -1) {
929 if (errno == ENOENT) {
930 char *parent = dirname(path);
931 if (parent == NULL)
932 return got_error_from_errno2("dirname", path);
933 err = add_dir_on_disk(worktree, parent);
934 if (err)
935 return err;
936 fd = open(ondisk_path,
937 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
938 GOT_DEFAULT_FILE_MODE);
939 if (fd == -1)
940 return got_error_from_errno2("open",
941 ondisk_path);
942 } else if (errno == EEXIST) {
943 if (!S_ISREG(st_mode)) {
944 /* TODO file is obstructed; do something */
945 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
946 goto done;
947 } else {
948 err = got_opentemp_named_fd(&tmppath, &fd,
949 ondisk_path);
950 if (err)
951 goto done;
952 update = 1;
954 } else
955 return got_error_from_errno2("open", ondisk_path);
958 if (restoring_missing_file)
959 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
960 else if (reverting_versioned_file)
961 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
962 else
963 err = (*progress_cb)(progress_arg,
964 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
965 if (err)
966 goto done;
968 hdrlen = got_object_blob_get_hdrlen(blob);
969 do {
970 const uint8_t *buf = got_object_blob_get_read_buf(blob);
971 err = got_object_blob_read_block(&len, blob);
972 if (err)
973 break;
974 if (len > 0) {
975 /* Skip blob object header first time around. */
976 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
977 if (outlen == -1) {
978 err = got_error_from_errno("write");
979 goto done;
980 } else if (outlen != len - hdrlen) {
981 err = got_error(GOT_ERR_IO);
982 goto done;
984 hdrlen = 0;
986 } while (len != 0);
988 if (fsync(fd) != 0) {
989 err = got_error_from_errno("fsync");
990 goto done;
993 if (update) {
994 if (rename(tmppath, ondisk_path) != 0) {
995 err = got_error_from_errno3("rename", tmppath,
996 ondisk_path);
997 unlink(tmppath);
998 goto done;
1002 if (te_mode & S_IXUSR) {
1003 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1004 err = got_error_from_errno2("chmod", ondisk_path);
1005 goto done;
1007 } else {
1008 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1009 err = got_error_from_errno2("chmod", ondisk_path);
1010 goto done;
1014 done:
1015 if (fd != -1 && close(fd) != 0 && err == NULL)
1016 err = got_error_from_errno("close");
1017 free(tmppath);
1018 return err;
1021 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1022 static const struct got_error *
1023 get_modified_file_content_status(unsigned char *status, FILE *f)
1025 const struct got_error *err = NULL;
1026 const char *markers[3] = {
1027 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1028 GOT_DIFF_CONFLICT_MARKER_SEP,
1029 GOT_DIFF_CONFLICT_MARKER_END
1031 int i = 0;
1032 char *line;
1033 size_t len;
1034 const char delim[3] = {'\0', '\0', '\0'};
1036 while (*status == GOT_STATUS_MODIFY) {
1037 line = fparseln(f, &len, NULL, delim, 0);
1038 if (line == NULL) {
1039 if (feof(f))
1040 break;
1041 err = got_ferror(f, GOT_ERR_IO);
1042 break;
1045 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1046 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1047 == 0)
1048 *status = GOT_STATUS_CONFLICT;
1049 else
1050 i++;
1054 return err;
1057 static int
1058 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1060 return !(ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_nsec == sb->st_mtimensec &&
1064 ie->size == (sb->st_size & 0xffffffff));
1067 static unsigned char
1068 get_staged_status(struct got_fileindex_entry *ie)
1070 switch (got_fileindex_entry_stage_get(ie)) {
1071 case GOT_FILEIDX_STAGE_ADD:
1072 return GOT_STATUS_ADD;
1073 case GOT_FILEIDX_STAGE_DELETE:
1074 return GOT_STATUS_DELETE;
1075 case GOT_FILEIDX_STAGE_MODIFY:
1076 return GOT_STATUS_MODIFY;
1077 default:
1078 return GOT_STATUS_NO_CHANGE;
1082 static const struct got_error *
1083 get_file_status(unsigned char *status, struct stat *sb,
1084 struct got_fileindex_entry *ie, const char *abspath,
1085 struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 struct got_object_id id;
1089 size_t hdrlen;
1090 FILE *f = NULL;
1091 uint8_t fbuf[8192];
1092 struct got_blob_object *blob = NULL;
1093 size_t flen, blen;
1094 unsigned char staged_status = get_staged_status(ie);
1096 *status = GOT_STATUS_NO_CHANGE;
1098 if (lstat(abspath, sb) == -1) {
1099 if (errno == ENOENT) {
1100 if (got_fileindex_entry_has_file_on_disk(ie))
1101 *status = GOT_STATUS_MISSING;
1102 else
1103 *status = GOT_STATUS_DELETE;
1104 return NULL;
1106 return got_error_from_errno2("lstat", abspath);
1109 if (!S_ISREG(sb->st_mode)) {
1110 *status = GOT_STATUS_OBSTRUCTED;
1111 return NULL;
1114 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1115 *status = GOT_STATUS_DELETE;
1116 return NULL;
1117 } else if (!got_fileindex_entry_has_blob(ie) &&
1118 staged_status != GOT_STATUS_ADD) {
1119 *status = GOT_STATUS_ADD;
1120 return NULL;
1123 if (!stat_info_differs(ie, sb))
1124 return NULL;
1126 if (staged_status == GOT_STATUS_MODIFY ||
1127 staged_status == GOT_STATUS_ADD)
1128 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1129 else
1130 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1132 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1133 if (err)
1134 return err;
1136 f = fopen(abspath, "r");
1137 if (f == NULL) {
1138 err = got_error_from_errno2("fopen", abspath);
1139 goto done;
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1142 for (;;) {
1143 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1144 err = got_object_blob_read_block(&blen, blob);
1145 if (err)
1146 goto done;
1147 /* Skip length of blob object header first time around. */
1148 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1149 if (flen == 0 && ferror(f)) {
1150 err = got_error_from_errno("fread");
1151 goto done;
1153 if (blen == 0) {
1154 if (flen != 0)
1155 *status = GOT_STATUS_MODIFY;
1156 break;
1157 } else if (flen == 0) {
1158 if (blen != 0)
1159 *status = GOT_STATUS_MODIFY;
1160 break;
1161 } else if (blen - hdrlen == flen) {
1162 /* Skip blob object header first time around. */
1163 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1164 *status = GOT_STATUS_MODIFY;
1165 break;
1167 } else {
1168 *status = GOT_STATUS_MODIFY;
1169 break;
1171 hdrlen = 0;
1174 if (*status == GOT_STATUS_MODIFY) {
1175 rewind(f);
1176 err = get_modified_file_content_status(status, f);
1178 done:
1179 if (blob)
1180 got_object_blob_close(blob);
1181 if (f)
1182 fclose(f);
1183 return err;
1187 * Update timestamps in the file index if a file is unmodified and
1188 * we had to run a full content comparison to find out.
1190 static const struct got_error *
1191 sync_timestamps(char *ondisk_path, unsigned char status,
1192 struct got_fileindex_entry *ie, struct stat *sb)
1194 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1195 return got_fileindex_entry_update(ie, ondisk_path,
1196 ie->blob_sha1, ie->commit_sha1, 1);
1198 return NULL;
1201 static const struct got_error *
1202 update_blob(struct got_worktree *worktree,
1203 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1204 struct got_tree_entry *te, const char *path,
1205 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1206 void *progress_arg)
1208 const struct got_error *err = NULL;
1209 struct got_blob_object *blob = NULL;
1210 char *ondisk_path;
1211 unsigned char status = GOT_STATUS_NO_CHANGE;
1212 struct stat sb;
1214 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1215 return got_error_from_errno("asprintf");
1217 if (ie) {
1218 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1219 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1220 goto done;
1222 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1223 if (err)
1224 goto done;
1225 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1226 sb.st_mode = got_fileindex_perms_to_st(ie);
1227 } else
1228 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 if (status == GOT_STATUS_OBSTRUCTED) {
1231 err = (*progress_cb)(progress_arg, status, path);
1232 goto done;
1235 if (ie && status != GOT_STATUS_MISSING) {
1236 if (got_fileindex_entry_has_commit(ie) &&
1237 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1238 SHA1_DIGEST_LENGTH) == 0) {
1239 err = sync_timestamps(ondisk_path, status, ie, &sb);
1240 if (err)
1241 goto done;
1242 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1243 path);
1244 goto done;
1246 if (got_fileindex_entry_has_blob(ie) &&
1247 memcmp(ie->blob_sha1, te->id->sha1,
1248 SHA1_DIGEST_LENGTH) == 0) {
1249 err = sync_timestamps(ondisk_path, status, ie, &sb);
1250 goto done;
1254 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1255 if (err)
1256 goto done;
1258 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1259 int update_timestamps;
1260 struct got_blob_object *blob2 = NULL;
1261 if (got_fileindex_entry_has_blob(ie)) {
1262 struct got_object_id id2;
1263 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1264 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1265 if (err)
1266 goto done;
1268 err = merge_blob(&update_timestamps, worktree, blob2,
1269 ondisk_path, path, sb.st_mode, blob,
1270 worktree->base_commit_id, repo,
1271 progress_cb, progress_arg);
1272 if (blob2)
1273 got_object_blob_close(blob2);
1275 * Do not update timestamps of files with local changes.
1276 * Otherwise, a future status walk would treat them as
1277 * unmodified files again.
1279 err = got_fileindex_entry_update(ie, ondisk_path,
1280 blob->id.sha1, worktree->base_commit_id->sha1,
1281 update_timestamps);
1282 } else if (status == GOT_STATUS_DELETE) {
1283 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1284 if (err)
1285 goto done;
1286 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1287 ondisk_path, path, blob, 0);
1288 if (err)
1289 goto done;
1290 } else {
1291 err = install_blob(worktree, ondisk_path, path, te->mode,
1292 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1293 repo, progress_cb, progress_arg);
1294 if (err)
1295 goto done;
1296 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1297 ondisk_path, path, blob, 1);
1298 if (err)
1299 goto done;
1301 got_object_blob_close(blob);
1302 done:
1303 free(ondisk_path);
1304 return err;
1307 static const struct got_error *
1308 remove_ondisk_file(const char *root_path, const char *path)
1310 const struct got_error *err = NULL;
1311 char *ondisk_path = NULL;
1313 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1314 return got_error_from_errno("asprintf");
1316 if (unlink(ondisk_path) == -1) {
1317 if (errno != ENOENT)
1318 err = got_error_from_errno2("unlink", ondisk_path);
1319 } else {
1320 char *parent = dirname(ondisk_path);
1321 while (parent && strcmp(parent, root_path) != 0) {
1322 if (rmdir(parent) == -1) {
1323 if (errno != ENOTEMPTY)
1324 err = got_error_from_errno2("rmdir",
1325 parent);
1326 break;
1328 parent = dirname(parent);
1331 free(ondisk_path);
1332 return err;
1335 static const struct got_error *
1336 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1337 struct got_fileindex_entry *ie, struct got_repository *repo,
1338 got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 const struct got_error *err = NULL;
1341 unsigned char status;
1342 struct stat sb;
1343 char *ondisk_path;
1345 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1346 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1348 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1349 == -1)
1350 return got_error_from_errno("asprintf");
1352 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1353 if (err)
1354 return err;
1356 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1357 status == GOT_STATUS_ADD) {
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1359 if (err)
1360 return err;
1362 * Preserve the working file and change the deleted blob's
1363 * entry into a schedule-add entry.
1365 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1366 0);
1367 if (err)
1368 return err;
1369 } else {
1370 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1371 if (err)
1372 return err;
1373 if (status == GOT_STATUS_NO_CHANGE) {
1374 err = remove_ondisk_file(worktree->root_path, ie->path);
1375 if (err)
1376 return err;
1378 got_fileindex_entry_remove(fileindex, ie);
1381 return err;
1384 struct diff_cb_arg {
1385 struct got_fileindex *fileindex;
1386 struct got_worktree *worktree;
1387 struct got_repository *repo;
1388 got_worktree_checkout_cb progress_cb;
1389 void *progress_arg;
1390 got_cancel_cb cancel_cb;
1391 void *cancel_arg;
1394 static const struct got_error *
1395 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1396 struct got_tree_entry *te, const char *parent_path)
1398 struct diff_cb_arg *a = arg;
1400 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1401 return got_error(GOT_ERR_CANCELLED);
1403 return update_blob(a->worktree, a->fileindex, ie, te,
1404 ie->path, a->repo, a->progress_cb, a->progress_arg);
1407 static const struct got_error *
1408 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1410 struct diff_cb_arg *a = arg;
1412 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1413 return got_error(GOT_ERR_CANCELLED);
1415 return delete_blob(a->worktree, a->fileindex, ie,
1416 a->repo, a->progress_cb, a->progress_arg);
1419 static const struct got_error *
1420 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1422 struct diff_cb_arg *a = arg;
1423 const struct got_error *err;
1424 char *path;
1426 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1427 return got_error(GOT_ERR_CANCELLED);
1429 if (got_object_tree_entry_is_submodule(te))
1430 return NULL;
1432 if (asprintf(&path, "%s%s%s", parent_path,
1433 parent_path[0] ? "/" : "", te->name)
1434 == -1)
1435 return got_error_from_errno("asprintf");
1437 if (S_ISDIR(te->mode))
1438 err = add_dir_on_disk(a->worktree, path);
1439 else
1440 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1441 a->repo, a->progress_cb, a->progress_arg);
1443 free(path);
1444 return err;
1447 static const struct got_error *
1448 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1450 const struct got_error *err = NULL;
1451 char *uuidstr = NULL;
1452 uint32_t uuid_status;
1454 *refname = NULL;
1456 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1457 if (uuid_status != uuid_s_ok)
1458 return got_error_uuid(uuid_status, "uuid_to_string");
1460 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1461 == -1) {
1462 err = got_error_from_errno("asprintf");
1463 *refname = NULL;
1465 free(uuidstr);
1466 return err;
1469 const struct got_error *
1470 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1475 static const struct got_error *
1476 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1478 return get_ref_name(refname, worktree,
1479 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1482 static const struct got_error *
1483 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1485 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1488 static const struct got_error *
1489 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1491 return get_ref_name(refname, worktree,
1492 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1495 static const struct got_error *
1496 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1498 return get_ref_name(refname, worktree,
1499 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1502 static const struct got_error *
1503 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1505 return get_ref_name(refname, worktree,
1506 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1509 static const struct got_error *
1510 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1512 return get_ref_name(refname, worktree,
1513 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1516 static const struct got_error *
1517 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1519 return get_ref_name(refname, worktree,
1520 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1523 static const struct got_error *
1524 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1530 const struct got_error *
1531 got_worktree_get_histedit_script_path(char **path,
1532 struct got_worktree *worktree)
1534 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1535 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1536 *path = NULL;
1537 return got_error_from_errno("asprintf");
1539 return NULL;
1543 * Prevent Git's garbage collector from deleting our base commit by
1544 * setting a reference to our base commit's ID.
1546 static const struct got_error *
1547 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_reference *ref = NULL;
1551 char *refname;
1553 err = got_worktree_get_base_ref_name(&refname, worktree);
1554 if (err)
1555 return err;
1557 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1558 if (err)
1559 goto done;
1561 err = got_ref_write(ref, repo);
1562 done:
1563 free(refname);
1564 if (ref)
1565 got_ref_close(ref);
1566 return err;
1569 static const struct got_error *
1570 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1572 const struct got_error *err = NULL;
1574 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1575 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 *fileindex_path = NULL;
1579 return err;
1583 static const struct got_error *
1584 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1585 struct got_worktree *worktree)
1587 const struct got_error *err = NULL;
1588 FILE *index = NULL;
1590 *fileindex_path = NULL;
1591 *fileindex = got_fileindex_alloc();
1592 if (*fileindex == NULL)
1593 return got_error_from_errno("got_fileindex_alloc");
1595 err = get_fileindex_path(fileindex_path, worktree);
1596 if (err)
1597 goto done;
1599 index = fopen(*fileindex_path, "rb");
1600 if (index == NULL) {
1601 if (errno != ENOENT)
1602 err = got_error_from_errno2("fopen", *fileindex_path);
1603 } else {
1604 err = got_fileindex_read(*fileindex, index);
1605 if (fclose(index) != 0 && err == NULL)
1606 err = got_error_from_errno("fclose");
1608 done:
1609 if (err) {
1610 free(*fileindex_path);
1611 *fileindex_path = NULL;
1612 got_fileindex_free(*fileindex);
1613 *fileindex = NULL;
1615 return err;
1618 struct bump_base_commit_id_arg {
1619 struct got_object_id *base_commit_id;
1620 const char *path;
1621 size_t path_len;
1622 const char *entry_name;
1623 got_worktree_checkout_cb progress_cb;
1624 void *progress_arg;
1627 /* Bump base commit ID of all files within an updated part of the work tree. */
1628 static const struct got_error *
1629 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1631 const struct got_error *err;
1632 struct bump_base_commit_id_arg *a = arg;
1634 if (a->entry_name) {
1635 if (strcmp(ie->path, a->path) != 0)
1636 return NULL;
1637 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1638 return NULL;
1640 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1641 SHA1_DIGEST_LENGTH) == 0)
1642 return NULL;
1644 if (a->progress_cb) {
1645 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1646 ie->path);
1647 if (err)
1648 return err;
1650 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1651 return NULL;
1654 static const struct got_error *
1655 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1657 const struct got_error *err = NULL;
1658 char *new_fileindex_path = NULL;
1659 FILE *new_index = NULL;
1661 err = got_opentemp_named(&new_fileindex_path, &new_index,
1662 fileindex_path);
1663 if (err)
1664 goto done;
1666 err = got_fileindex_write(fileindex, new_index);
1667 if (err)
1668 goto done;
1670 if (rename(new_fileindex_path, fileindex_path) != 0) {
1671 err = got_error_from_errno3("rename", new_fileindex_path,
1672 fileindex_path);
1673 unlink(new_fileindex_path);
1675 done:
1676 if (new_index)
1677 fclose(new_index);
1678 free(new_fileindex_path);
1679 return err;
1682 static const struct got_error *
1683 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1684 struct got_object_id **tree_id, const char *wt_relpath,
1685 struct got_worktree *worktree, struct got_repository *repo)
1687 const struct got_error *err = NULL;
1688 struct got_object_id *id = NULL;
1689 char *in_repo_path = NULL;
1690 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1692 *entry_type = GOT_OBJ_TYPE_ANY;
1693 *tree_relpath = NULL;
1694 *tree_id = NULL;
1696 if (wt_relpath[0] == '\0') {
1697 /* Check out all files within the work tree. */
1698 *entry_type = GOT_OBJ_TYPE_TREE;
1699 *tree_relpath = strdup("");
1700 if (*tree_relpath == NULL) {
1701 err = got_error_from_errno("strdup");
1702 goto done;
1704 err = got_object_id_by_path(tree_id, repo,
1705 worktree->base_commit_id, worktree->path_prefix);
1706 if (err)
1707 goto done;
1708 return NULL;
1711 /* Check out a subset of files in the work tree. */
1713 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1714 is_root_wt ? "" : "/", wt_relpath) == -1) {
1715 err = got_error_from_errno("asprintf");
1716 goto done;
1719 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1720 in_repo_path);
1721 if (err)
1722 goto done;
1724 free(in_repo_path);
1725 in_repo_path = NULL;
1727 err = got_object_get_type(entry_type, repo, id);
1728 if (err)
1729 goto done;
1731 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1732 /* Check out a single file. */
1733 if (strchr(wt_relpath, '/') == NULL) {
1734 /* Check out a single file in work tree's root dir. */
1735 in_repo_path = strdup(worktree->path_prefix);
1736 if (in_repo_path == NULL) {
1737 err = got_error_from_errno("strdup");
1738 goto done;
1740 *tree_relpath = strdup("");
1741 if (*tree_relpath == NULL) {
1742 err = got_error_from_errno("strdup");
1743 goto done;
1745 } else {
1746 /* Check out a single file in a subdirectory. */
1747 err = got_path_dirname(tree_relpath, wt_relpath);
1748 if (err)
1749 return err;
1750 if (asprintf(&in_repo_path, "%s%s%s",
1751 worktree->path_prefix, is_root_wt ? "" : "/",
1752 *tree_relpath) == -1) {
1753 err = got_error_from_errno("asprintf");
1754 goto done;
1757 err = got_object_id_by_path(tree_id, repo,
1758 worktree->base_commit_id, in_repo_path);
1759 } else {
1760 /* Check out all files within a subdirectory. */
1761 *tree_id = got_object_id_dup(id);
1762 if (*tree_id == NULL) {
1763 err = got_error_from_errno("got_object_id_dup");
1764 goto done;
1766 *tree_relpath = strdup(wt_relpath);
1767 if (*tree_relpath == NULL) {
1768 err = got_error_from_errno("strdup");
1769 goto done;
1772 done:
1773 free(id);
1774 free(in_repo_path);
1775 if (err) {
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 free(*tree_relpath);
1778 *tree_relpath = NULL;
1779 free(*tree_id);
1780 *tree_id = NULL;
1782 return err;
1785 static const struct got_error *
1786 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1787 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1788 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1789 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1791 const struct got_error *err = NULL;
1792 struct got_commit_object *commit = NULL;
1793 struct got_tree_object *tree = NULL;
1794 struct got_fileindex_diff_tree_cb diff_cb;
1795 struct diff_cb_arg arg;
1797 err = ref_base_commit(worktree, repo);
1798 if (err)
1799 goto done;
1801 err = got_object_open_as_commit(&commit, repo,
1802 worktree->base_commit_id);
1803 if (err)
1804 goto done;
1806 err = got_object_open_as_tree(&tree, repo, tree_id);
1807 if (err)
1808 goto done;
1810 if (entry_name &&
1811 got_object_tree_find_entry(tree, entry_name) == NULL) {
1812 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1813 goto done;
1816 diff_cb.diff_old_new = diff_old_new;
1817 diff_cb.diff_old = diff_old;
1818 diff_cb.diff_new = diff_new;
1819 arg.fileindex = fileindex;
1820 arg.worktree = worktree;
1821 arg.repo = repo;
1822 arg.progress_cb = progress_cb;
1823 arg.progress_arg = progress_arg;
1824 arg.cancel_cb = cancel_cb;
1825 arg.cancel_arg = cancel_arg;
1826 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1827 entry_name, repo, &diff_cb, &arg);
1828 done:
1829 if (tree)
1830 got_object_tree_close(tree);
1831 if (commit)
1832 got_object_commit_close(commit);
1833 return err;
1836 const struct got_error *
1837 got_worktree_checkout_files(struct got_worktree *worktree,
1838 struct got_pathlist_head *paths, struct got_repository *repo,
1839 got_worktree_checkout_cb progress_cb, void *progress_arg,
1840 got_cancel_cb cancel_cb, void *cancel_arg)
1842 const struct got_error *err = NULL, *sync_err, *unlockerr;
1843 struct got_commit_object *commit = NULL;
1844 struct got_tree_object *tree = NULL;
1845 struct got_fileindex *fileindex = NULL;
1846 char *fileindex_path = NULL;
1847 struct got_pathlist_entry *pe;
1848 struct tree_path_data {
1849 SIMPLEQ_ENTRY(tree_path_data) entry;
1850 struct got_object_id *tree_id;
1851 int entry_type;
1852 char *relpath;
1853 char *entry_name;
1854 } *tpd = NULL;
1855 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1857 SIMPLEQ_INIT(&tree_paths);
1859 err = lock_worktree(worktree, LOCK_EX);
1860 if (err)
1861 return err;
1863 /* Map all specified paths to in-repository trees. */
1864 TAILQ_FOREACH(pe, paths, entry) {
1865 tpd = malloc(sizeof(*tpd));
1866 if (tpd == NULL) {
1867 err = got_error_from_errno("malloc");
1868 goto done;
1871 err = find_tree_entry_for_checkout(&tpd->entry_type,
1872 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1873 if (err) {
1874 free(tpd);
1875 goto done;
1878 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1879 err = got_path_basename(&tpd->entry_name, pe->path);
1880 if (err) {
1881 free(tpd->relpath);
1882 free(tpd->tree_id);
1883 free(tpd);
1884 goto done;
1886 } else
1887 tpd->entry_name = NULL;
1889 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1893 * Read the file index.
1894 * Checking out files is supposed to be an idempotent operation.
1895 * If the on-disk file index is incomplete we will try to complete it.
1897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1898 if (err)
1899 goto done;
1901 tpd = SIMPLEQ_FIRST(&tree_paths);
1902 TAILQ_FOREACH(pe, paths, entry) {
1903 struct bump_base_commit_id_arg bbc_arg;
1905 err = checkout_files(worktree, fileindex, tpd->relpath,
1906 tpd->tree_id, tpd->entry_name, repo,
1907 progress_cb, progress_arg, cancel_cb, cancel_arg);
1908 if (err)
1909 break;
1911 bbc_arg.base_commit_id = worktree->base_commit_id;
1912 bbc_arg.entry_name = tpd->entry_name;
1913 bbc_arg.path = pe->path;
1914 bbc_arg.path_len = pe->path_len;
1915 bbc_arg.progress_cb = progress_cb;
1916 bbc_arg.progress_arg = progress_arg;
1917 err = got_fileindex_for_each_entry_safe(fileindex,
1918 bump_base_commit_id, &bbc_arg);
1919 if (err)
1920 break;
1922 tpd = SIMPLEQ_NEXT(tpd, entry);
1924 sync_err = sync_fileindex(fileindex, fileindex_path);
1925 if (sync_err && err == NULL)
1926 err = sync_err;
1927 done:
1928 free(fileindex_path);
1929 if (tree)
1930 got_object_tree_close(tree);
1931 if (commit)
1932 got_object_commit_close(commit);
1933 if (fileindex)
1934 got_fileindex_free(fileindex);
1935 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1936 tpd = SIMPLEQ_FIRST(&tree_paths);
1937 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1938 free(tpd->relpath);
1939 free(tpd->tree_id);
1940 free(tpd);
1942 unlockerr = lock_worktree(worktree, LOCK_SH);
1943 if (unlockerr && err == NULL)
1944 err = unlockerr;
1945 return err;
1948 struct merge_file_cb_arg {
1949 struct got_worktree *worktree;
1950 struct got_fileindex *fileindex;
1951 got_worktree_checkout_cb progress_cb;
1952 void *progress_arg;
1953 got_cancel_cb cancel_cb;
1954 void *cancel_arg;
1955 struct got_object_id *commit_id2;
1958 static const struct got_error *
1959 merge_file_cb(void *arg, struct got_blob_object *blob1,
1960 struct got_blob_object *blob2, struct got_object_id *id1,
1961 struct got_object_id *id2, const char *path1, const char *path2,
1962 struct got_repository *repo)
1964 static const struct got_error *err = NULL;
1965 struct merge_file_cb_arg *a = arg;
1966 struct got_fileindex_entry *ie;
1967 char *ondisk_path = NULL;
1968 struct stat sb;
1969 unsigned char status;
1970 int local_changes_subsumed;
1972 if (blob1 && blob2) {
1973 ie = got_fileindex_entry_get(a->fileindex, path2,
1974 strlen(path2));
1975 if (ie == NULL)
1976 return (*a->progress_cb)(a->progress_arg,
1977 GOT_STATUS_MISSING, path2);
1979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1980 path2) == -1)
1981 return got_error_from_errno("asprintf");
1983 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1984 if (err)
1985 goto done;
1987 if (status == GOT_STATUS_DELETE) {
1988 err = (*a->progress_cb)(a->progress_arg,
1989 GOT_STATUS_MERGE, path2);
1990 goto done;
1992 if (status != GOT_STATUS_NO_CHANGE &&
1993 status != GOT_STATUS_MODIFY &&
1994 status != GOT_STATUS_CONFLICT &&
1995 status != GOT_STATUS_ADD) {
1996 err = (*a->progress_cb)(a->progress_arg, status, path2);
1997 goto done;
2000 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2001 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2002 a->progress_cb, a->progress_arg);
2003 } else if (blob1) {
2004 ie = got_fileindex_entry_get(a->fileindex, path1,
2005 strlen(path1));
2006 if (ie == NULL)
2007 return (*a->progress_cb)(a->progress_arg,
2008 GOT_STATUS_MISSING, path2);
2010 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2011 path1) == -1)
2012 return got_error_from_errno("asprintf");
2014 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2015 if (err)
2016 goto done;
2018 switch (status) {
2019 case GOT_STATUS_NO_CHANGE:
2020 err = (*a->progress_cb)(a->progress_arg,
2021 GOT_STATUS_DELETE, path1);
2022 if (err)
2023 goto done;
2024 err = remove_ondisk_file(a->worktree->root_path, path1);
2025 if (err)
2026 goto done;
2027 if (ie)
2028 got_fileindex_entry_mark_deleted_from_disk(ie);
2029 break;
2030 case GOT_STATUS_DELETE:
2031 case GOT_STATUS_MISSING:
2032 err = (*a->progress_cb)(a->progress_arg,
2033 GOT_STATUS_DELETE, path1);
2034 if (err)
2035 goto done;
2036 if (ie)
2037 got_fileindex_entry_mark_deleted_from_disk(ie);
2038 break;
2039 case GOT_STATUS_ADD:
2040 case GOT_STATUS_MODIFY:
2041 case GOT_STATUS_CONFLICT:
2042 err = (*a->progress_cb)(a->progress_arg,
2043 GOT_STATUS_CANNOT_DELETE, path1);
2044 if (err)
2045 goto done;
2046 break;
2047 case GOT_STATUS_OBSTRUCTED:
2048 err = (*a->progress_cb)(a->progress_arg, status, path1);
2049 if (err)
2050 goto done;
2051 break;
2052 default:
2053 break;
2055 } else if (blob2) {
2056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2057 path2) == -1)
2058 return got_error_from_errno("asprintf");
2059 ie = got_fileindex_entry_get(a->fileindex, path2,
2060 strlen(path2));
2061 if (ie) {
2062 err = get_file_status(&status, &sb, ie, ondisk_path,
2063 repo);
2064 if (err)
2065 goto done;
2066 if (status != GOT_STATUS_NO_CHANGE &&
2067 status != GOT_STATUS_MODIFY &&
2068 status != GOT_STATUS_CONFLICT &&
2069 status != GOT_STATUS_ADD) {
2070 err = (*a->progress_cb)(a->progress_arg,
2071 status, path2);
2072 goto done;
2074 err = merge_blob(&local_changes_subsumed, a->worktree,
2075 NULL, ondisk_path, path2, sb.st_mode, blob2,
2076 a->commit_id2, repo,
2077 a->progress_cb, a->progress_arg);
2078 if (status == GOT_STATUS_DELETE) {
2079 err = update_blob_fileindex_entry(a->worktree,
2080 a->fileindex, ie, ondisk_path, ie->path,
2081 blob2, 0);
2082 if (err)
2083 goto done;
2085 } else {
2086 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2087 err = install_blob(a->worktree, ondisk_path, path2,
2088 /* XXX get this from parent tree! */
2089 GOT_DEFAULT_FILE_MODE,
2090 sb.st_mode, blob2, 0, 0, repo,
2091 a->progress_cb, a->progress_arg);
2092 if (err)
2093 goto done;
2094 err = got_fileindex_entry_alloc(&ie,
2095 ondisk_path, path2, NULL, NULL);
2096 if (err)
2097 goto done;
2098 err = got_fileindex_entry_add(a->fileindex, ie);
2099 if (err) {
2100 got_fileindex_entry_free(ie);
2101 goto done;
2105 done:
2106 free(ondisk_path);
2107 return err;
2110 struct check_merge_ok_arg {
2111 struct got_worktree *worktree;
2112 struct got_repository *repo;
2115 static const struct got_error *
2116 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2118 const struct got_error *err = NULL;
2119 struct check_merge_ok_arg *a = arg;
2120 unsigned char status;
2121 struct stat sb;
2122 char *ondisk_path;
2124 /* Reject merges into a work tree with mixed base commits. */
2125 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2126 SHA1_DIGEST_LENGTH))
2127 return got_error(GOT_ERR_MIXED_COMMITS);
2129 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2130 == -1)
2131 return got_error_from_errno("asprintf");
2133 /* Reject merges into a work tree with conflicted files. */
2134 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2135 if (err)
2136 return err;
2137 if (status == GOT_STATUS_CONFLICT)
2138 return got_error(GOT_ERR_CONFLICTS);
2140 return NULL;
2143 static const struct got_error *
2144 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2145 const char *fileindex_path, struct got_object_id *commit_id1,
2146 struct got_object_id *commit_id2, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg,
2148 got_cancel_cb cancel_cb, void *cancel_arg)
2150 const struct got_error *err = NULL, *sync_err;
2151 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2152 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2153 struct merge_file_cb_arg arg;
2155 if (commit_id1) {
2156 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2157 worktree->path_prefix);
2158 if (err)
2159 goto done;
2161 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2162 if (err)
2163 goto done;
2166 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2167 worktree->path_prefix);
2168 if (err)
2169 goto done;
2171 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2172 if (err)
2173 goto done;
2175 arg.worktree = worktree;
2176 arg.fileindex = fileindex;
2177 arg.progress_cb = progress_cb;
2178 arg.progress_arg = progress_arg;
2179 arg.cancel_cb = cancel_cb;
2180 arg.cancel_arg = cancel_arg;
2181 arg.commit_id2 = commit_id2;
2182 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2183 sync_err = sync_fileindex(fileindex, fileindex_path);
2184 if (sync_err && err == NULL)
2185 err = sync_err;
2186 done:
2187 if (tree1)
2188 got_object_tree_close(tree1);
2189 if (tree2)
2190 got_object_tree_close(tree2);
2191 return err;
2194 const struct got_error *
2195 got_worktree_merge_files(struct got_worktree *worktree,
2196 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2197 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2198 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err, *unlockerr;
2201 char *fileindex_path = NULL;
2202 struct got_fileindex *fileindex = NULL;
2203 struct check_merge_ok_arg mok_arg;
2205 err = lock_worktree(worktree, LOCK_EX);
2206 if (err)
2207 return err;
2209 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2210 if (err)
2211 goto done;
2213 mok_arg.worktree = worktree;
2214 mok_arg.repo = repo;
2215 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2216 &mok_arg);
2217 if (err)
2218 goto done;
2220 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2221 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2222 done:
2223 if (fileindex)
2224 got_fileindex_free(fileindex);
2225 free(fileindex_path);
2226 unlockerr = lock_worktree(worktree, LOCK_SH);
2227 if (unlockerr && err == NULL)
2228 err = unlockerr;
2229 return err;
2232 struct diff_dir_cb_arg {
2233 struct got_fileindex *fileindex;
2234 struct got_worktree *worktree;
2235 const char *status_path;
2236 size_t status_path_len;
2237 struct got_repository *repo;
2238 got_worktree_status_cb status_cb;
2239 void *status_arg;
2240 got_cancel_cb cancel_cb;
2241 void *cancel_arg;
2242 /* A pathlist containing per-directory pathlists of ignore patterns. */
2243 struct got_pathlist_head ignores;
2246 static const struct got_error *
2247 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2248 got_worktree_status_cb status_cb, void *status_arg,
2249 struct got_repository *repo)
2251 const struct got_error *err = NULL;
2252 unsigned char status = GOT_STATUS_NO_CHANGE;
2253 unsigned char staged_status = get_staged_status(ie);
2254 struct stat sb;
2255 struct got_object_id blob_id, commit_id, staged_blob_id;
2256 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2257 struct got_object_id *staged_blob_idp = NULL;
2259 err = get_file_status(&status, &sb, ie, abspath, repo);
2260 if (err)
2261 return err;
2263 if (status == GOT_STATUS_NO_CHANGE &&
2264 staged_status == GOT_STATUS_NO_CHANGE)
2265 return NULL;
2267 if (got_fileindex_entry_has_blob(ie)) {
2268 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2269 blob_idp = &blob_id;
2271 if (got_fileindex_entry_has_commit(ie)) {
2272 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2273 commit_idp = &commit_id;
2275 if (staged_status == GOT_STATUS_ADD ||
2276 staged_status == GOT_STATUS_MODIFY) {
2277 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2278 SHA1_DIGEST_LENGTH);
2279 staged_blob_idp = &staged_blob_id;
2282 return (*status_cb)(status_arg, status, staged_status,
2283 ie->path, blob_idp, staged_blob_idp, commit_idp);
2286 static const struct got_error *
2287 status_old_new(void *arg, struct got_fileindex_entry *ie,
2288 struct dirent *de, const char *parent_path)
2290 const struct got_error *err = NULL;
2291 struct diff_dir_cb_arg *a = arg;
2292 char *abspath;
2294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2295 return got_error(GOT_ERR_CANCELLED);
2297 if (got_path_cmp(parent_path, a->status_path,
2298 strlen(parent_path), a->status_path_len) != 0 &&
2299 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2300 return NULL;
2302 if (parent_path[0]) {
2303 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2304 parent_path, de->d_name) == -1)
2305 return got_error_from_errno("asprintf");
2306 } else {
2307 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2308 de->d_name) == -1)
2309 return got_error_from_errno("asprintf");
2312 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2313 a->repo);
2314 free(abspath);
2315 return err;
2318 static const struct got_error *
2319 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2321 struct diff_dir_cb_arg *a = arg;
2322 struct got_object_id blob_id, commit_id;
2323 unsigned char status;
2325 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2326 return got_error(GOT_ERR_CANCELLED);
2328 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2329 return NULL;
2331 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2332 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2333 if (got_fileindex_entry_has_file_on_disk(ie))
2334 status = GOT_STATUS_MISSING;
2335 else
2336 status = GOT_STATUS_DELETE;
2337 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2338 ie->path, &blob_id, NULL, &commit_id);
2341 void
2342 free_ignorelist(struct got_pathlist_head *ignorelist)
2344 struct got_pathlist_entry *pe;
2346 TAILQ_FOREACH(pe, ignorelist, entry)
2347 free((char *)pe->path);
2348 got_pathlist_free(ignorelist);
2351 void
2352 free_ignores(struct got_pathlist_head *ignores)
2354 struct got_pathlist_entry *pe;
2356 TAILQ_FOREACH(pe, ignores, entry) {
2357 struct got_pathlist_head *ignorelist = pe->data;
2358 free_ignorelist(ignorelist);
2359 free((char *)pe->path);
2361 got_pathlist_free(ignores);
2364 static const struct got_error *
2365 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2367 const struct got_error *err = NULL;
2368 struct got_pathlist_entry *pe = NULL;
2369 struct got_pathlist_head *ignorelist;
2370 char *line = NULL, *pattern, *dirpath = NULL;
2371 size_t linesize = 0;
2372 ssize_t linelen;
2374 ignorelist = calloc(1, sizeof(*ignorelist));
2375 if (ignorelist == NULL)
2376 return got_error_from_errno("calloc");
2377 TAILQ_INIT(ignorelist);
2379 while ((linelen = getline(&line, &linesize, f)) != -1) {
2380 if (linelen > 0 && line[linelen - 1] == '\n')
2381 line[linelen - 1] = '\0';
2382 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2383 line) == -1) {
2384 err = got_error_from_errno("asprintf");
2385 goto done;
2387 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2388 if (err)
2389 goto done;
2391 if (ferror(f)) {
2392 err = got_error_from_errno("getline");
2393 goto done;
2396 dirpath = strdup(path);
2397 if (dirpath == NULL) {
2398 err = got_error_from_errno("strdup");
2399 goto done;
2401 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2402 done:
2403 free(line);
2404 if (err || pe == NULL) {
2405 free(dirpath);
2406 free_ignorelist(ignorelist);
2408 return err;
2411 int
2412 match_ignores(struct got_pathlist_head *ignores, const char *path)
2414 struct got_pathlist_entry *pe;
2417 * The ignores pathlist contains ignore lists from children before
2418 * parents, so we can find the most specific ignorelist by walking
2419 * ignores backwards.
2421 pe = TAILQ_LAST(ignores, got_pathlist_head);
2422 while (pe) {
2423 if (got_path_is_child(path, pe->path, pe->path_len)) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 struct got_pathlist_entry *pi;
2426 TAILQ_FOREACH(pi, ignorelist, entry) {
2427 if (fnmatch(pi->path, path,
2428 FNM_PATHNAME | FNM_LEADING_DIR))
2429 continue;
2430 return 1;
2433 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2436 return 0;
2439 static const struct got_error *
2440 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2441 const char *path)
2443 const struct got_error *err = NULL;
2444 char *ignorespath;
2445 FILE *ignoresfile = NULL;
2447 /* TODO: read .gitignores as well... */
2448 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2449 path[0] ? "/" : "") == -1)
2450 return got_error_from_errno("asprintf");
2452 ignoresfile = fopen(ignorespath, "r");
2453 if (ignoresfile == NULL) {
2454 if (errno != ENOENT && errno != EACCES)
2455 err = got_error_from_errno2("fopen",
2456 ignorespath);
2457 } else
2458 err = read_ignores(ignores, path, ignoresfile);
2460 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2461 err = got_error_from_errno2("flose", path);
2462 free(ignorespath);
2463 return err;
2466 static const struct got_error *
2467 status_new(void *arg, struct dirent *de, const char *parent_path)
2469 const struct got_error *err = NULL;
2470 struct diff_dir_cb_arg *a = arg;
2471 char *path = NULL;
2473 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2474 return got_error(GOT_ERR_CANCELLED);
2476 /* XXX ignore symlinks for now */
2477 if (de->d_type == DT_LNK)
2478 return NULL;
2480 if (parent_path[0]) {
2481 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2482 return got_error_from_errno("asprintf");
2483 } else {
2484 path = de->d_name;
2487 if (de->d_type == DT_DIR)
2488 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2489 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2490 && !match_ignores(&a->ignores, path))
2491 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2492 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2493 if (parent_path[0])
2494 free(path);
2495 return err;
2498 static const struct got_error *
2499 report_single_file_status(const char *path, const char *ondisk_path,
2500 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2501 void *status_arg, struct got_repository *repo)
2503 struct got_fileindex_entry *ie;
2504 struct stat sb;
2506 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2507 if (ie)
2508 return report_file_status(ie, ondisk_path, status_cb,
2509 status_arg, repo);
2511 if (lstat(ondisk_path, &sb) == -1) {
2512 if (errno != ENOENT)
2513 return got_error_from_errno2("lstat", ondisk_path);
2514 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2516 return NULL;
2519 if (S_ISREG(sb.st_mode))
2520 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2521 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2523 return NULL;
2526 static const struct got_error *
2527 worktree_status(struct got_worktree *worktree, const char *path,
2528 struct got_fileindex *fileindex, struct got_repository *repo,
2529 got_worktree_status_cb status_cb, void *status_arg,
2530 got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 DIR *workdir = NULL;
2534 struct got_fileindex_diff_dir_cb fdiff_cb;
2535 struct diff_dir_cb_arg arg;
2536 char *ondisk_path = NULL;
2538 if (asprintf(&ondisk_path, "%s%s%s",
2539 worktree->root_path, path[0] ? "/" : "", path) == -1)
2540 return got_error_from_errno("asprintf");
2542 workdir = opendir(ondisk_path);
2543 if (workdir == NULL) {
2544 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2545 err = got_error_from_errno2("opendir", ondisk_path);
2546 else
2547 err = report_single_file_status(path, ondisk_path,
2548 fileindex, status_cb, status_arg, repo);
2549 } else {
2550 fdiff_cb.diff_old_new = status_old_new;
2551 fdiff_cb.diff_old = status_old;
2552 fdiff_cb.diff_new = status_new;
2553 arg.fileindex = fileindex;
2554 arg.worktree = worktree;
2555 arg.status_path = path;
2556 arg.status_path_len = strlen(path);
2557 arg.repo = repo;
2558 arg.status_cb = status_cb;
2559 arg.status_arg = status_arg;
2560 arg.cancel_cb = cancel_cb;
2561 arg.cancel_arg = cancel_arg;
2562 TAILQ_INIT(&arg.ignores);
2563 err = add_ignores(&arg.ignores, worktree->root_path, path);
2564 if (err == NULL)
2565 err = got_fileindex_diff_dir(fileindex, workdir,
2566 worktree->root_path, path, repo, &fdiff_cb, &arg);
2567 free_ignores(&arg.ignores);
2570 if (workdir)
2571 closedir(workdir);
2572 free(ondisk_path);
2573 return err;
2576 const struct got_error *
2577 got_worktree_status(struct got_worktree *worktree,
2578 struct got_pathlist_head *paths, struct got_repository *repo,
2579 got_worktree_status_cb status_cb, void *status_arg,
2580 got_cancel_cb cancel_cb, void *cancel_arg)
2582 const struct got_error *err = NULL;
2583 char *fileindex_path = NULL;
2584 struct got_fileindex *fileindex = NULL;
2585 struct got_pathlist_entry *pe;
2587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2588 if (err)
2589 return err;
2591 TAILQ_FOREACH(pe, paths, entry) {
2592 err = worktree_status(worktree, pe->path, fileindex, repo,
2593 status_cb, status_arg, cancel_cb, cancel_arg);
2594 if (err)
2595 break;
2597 free(fileindex_path);
2598 got_fileindex_free(fileindex);
2599 return err;
2602 const struct got_error *
2603 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2604 const char *arg)
2606 const struct got_error *err = NULL;
2607 char *resolved, *cwd = NULL, *path = NULL;
2608 size_t len;
2610 *wt_path = NULL;
2612 resolved = realpath(arg, NULL);
2613 if (resolved == NULL) {
2614 if (errno != ENOENT)
2615 return got_error_from_errno2("realpath", arg);
2616 cwd = getcwd(NULL, 0);
2617 if (cwd == NULL)
2618 return got_error_from_errno("getcwd");
2619 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2620 err = got_error_from_errno("asprintf");
2621 goto done;
2625 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2626 strlen(got_worktree_get_root_path(worktree)))) {
2627 err = got_error(GOT_ERR_BAD_PATH);
2628 goto done;
2631 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2632 err = got_path_skip_common_ancestor(&path,
2633 got_worktree_get_root_path(worktree), resolved);
2634 if (err)
2635 goto done;
2636 } else {
2637 path = strdup("");
2638 if (path == NULL) {
2639 err = got_error_from_errno("strdup");
2640 goto done;
2644 /* XXX status walk can't deal with trailing slash! */
2645 len = strlen(path);
2646 while (len > 0 && path[len - 1] == '/') {
2647 path[len - 1] = '\0';
2648 len--;
2650 done:
2651 free(resolved);
2652 free(cwd);
2653 if (err == NULL)
2654 *wt_path = path;
2655 else
2656 free(path);
2657 return err;
2660 static const struct got_error *
2661 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2662 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2663 struct got_repository *repo)
2665 const struct got_error *err = NULL;
2666 struct got_fileindex_entry *ie;
2667 unsigned char status;
2668 struct stat sb;
2670 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2671 if (ie) {
2672 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2673 if (err)
2674 return err;
2675 /* Re-adding an existing entry is a no-op. */
2676 if (status == GOT_STATUS_ADD)
2677 return NULL;
2678 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2681 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2682 if (err)
2683 return err;
2685 err = got_fileindex_entry_add(fileindex, ie);
2686 if (err) {
2687 got_fileindex_entry_free(ie);
2688 return err;
2691 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2694 const struct got_error *
2695 got_worktree_schedule_add(struct got_worktree *worktree,
2696 struct got_pathlist_head *paths,
2697 got_worktree_status_cb status_cb, void *status_arg,
2698 struct got_repository *repo)
2700 struct got_fileindex *fileindex = NULL;
2701 char *fileindex_path = NULL;
2702 const struct got_error *err = NULL, *sync_err, *unlockerr;
2703 struct got_pathlist_entry *pe;
2705 err = lock_worktree(worktree, LOCK_EX);
2706 if (err)
2707 return err;
2709 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2710 if (err)
2711 goto done;
2713 TAILQ_FOREACH(pe, paths, entry) {
2714 char *ondisk_path;
2715 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2716 pe->path) == -1)
2717 return got_error_from_errno("asprintf");
2718 err = schedule_addition(ondisk_path, fileindex, pe->path,
2719 status_cb, status_arg, repo);
2720 free(ondisk_path);
2721 if (err)
2722 break;
2724 sync_err = sync_fileindex(fileindex, fileindex_path);
2725 if (sync_err && err == NULL)
2726 err = sync_err;
2727 done:
2728 free(fileindex_path);
2729 if (fileindex)
2730 got_fileindex_free(fileindex);
2731 unlockerr = lock_worktree(worktree, LOCK_SH);
2732 if (unlockerr && err == NULL)
2733 err = unlockerr;
2734 return err;
2737 static const struct got_error *
2738 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2739 const char *relpath, int delete_local_mods,
2740 got_worktree_status_cb status_cb, void *status_arg,
2741 struct got_repository *repo)
2743 const struct got_error *err = NULL;
2744 struct got_fileindex_entry *ie = NULL;
2745 unsigned char status, staged_status;
2746 struct stat sb;
2748 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2749 if (ie == NULL)
2750 return got_error(GOT_ERR_BAD_PATH);
2752 staged_status = get_staged_status(ie);
2753 if (staged_status != GOT_STATUS_NO_CHANGE) {
2754 if (staged_status == GOT_STATUS_DELETE)
2755 return NULL;
2756 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2759 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2760 if (err)
2761 return err;
2763 if (status != GOT_STATUS_NO_CHANGE) {
2764 if (status == GOT_STATUS_DELETE)
2765 return NULL;
2766 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2767 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2768 if (status != GOT_STATUS_MODIFY &&
2769 status != GOT_STATUS_MISSING)
2770 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2773 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2774 return got_error_from_errno2("unlink", ondisk_path);
2776 got_fileindex_entry_mark_deleted_from_disk(ie);
2777 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2780 const struct got_error *
2781 got_worktree_schedule_delete(struct got_worktree *worktree,
2782 struct got_pathlist_head *paths, int delete_local_mods,
2783 got_worktree_status_cb status_cb, void *status_arg,
2784 struct got_repository *repo)
2786 struct got_fileindex *fileindex = NULL;
2787 char *fileindex_path = NULL;
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_pathlist_entry *pe;
2791 err = lock_worktree(worktree, LOCK_EX);
2792 if (err)
2793 return err;
2795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2796 if (err)
2797 goto done;
2799 TAILQ_FOREACH(pe, paths, entry) {
2800 char *ondisk_path;
2801 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2802 pe->path) == -1)
2803 return got_error_from_errno("asprintf");
2804 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2805 delete_local_mods, status_cb, status_arg, repo);
2806 free(ondisk_path);
2807 if (err)
2808 break;
2810 sync_err = sync_fileindex(fileindex, fileindex_path);
2811 if (sync_err && err == NULL)
2812 err = sync_err;
2813 done:
2814 free(fileindex_path);
2815 if (fileindex)
2816 got_fileindex_free(fileindex);
2817 unlockerr = lock_worktree(worktree, LOCK_SH);
2818 if (unlockerr && err == NULL)
2819 err = unlockerr;
2820 return err;
2823 static const struct got_error *
2824 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2826 const struct got_error *err = NULL;
2827 char *line = NULL;
2828 size_t linesize = 0, n;
2829 ssize_t linelen;
2831 linelen = getline(&line, &linesize, infile);
2832 if (linelen == -1) {
2833 if (ferror(infile)) {
2834 err = got_error_from_errno("getline");
2835 goto done;
2837 return NULL;
2839 if (outfile) {
2840 n = fwrite(line, 1, linelen, outfile);
2841 if (n != linelen) {
2842 err = got_ferror(outfile, GOT_ERR_IO);
2843 goto done;
2846 if (rejectfile) {
2847 n = fwrite(line, 1, linelen, rejectfile);
2848 if (n != linelen)
2849 err = got_ferror(outfile, GOT_ERR_IO);
2851 done:
2852 free(line);
2853 return err;
2856 static const struct got_error *
2857 skip_one_line(FILE *f)
2859 char *line = NULL;
2860 size_t linesize = 0;
2861 ssize_t linelen;
2863 linelen = getline(&line, &linesize, f);
2864 free(line);
2865 if (linelen == -1 && ferror(f))
2866 return got_error_from_errno("getline");
2867 return NULL;
2870 static const struct got_error *
2871 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2872 int start_old, int end_old, int start_new, int end_new,
2873 FILE *outfile, FILE *rejectfile)
2875 const struct got_error *err;
2877 /* Copy old file's lines leading up to patch. */
2878 while (!feof(f1) && *line_cur1 < start_old) {
2879 err = copy_one_line(f1, outfile, NULL);
2880 if (err)
2881 return err;
2882 (*line_cur1)++;
2884 /* Skip new file's lines leading up to patch. */
2885 while (!feof(f2) && *line_cur2 < start_new) {
2886 if (rejectfile)
2887 err = copy_one_line(f2, NULL, rejectfile);
2888 else
2889 err = skip_one_line(f2);
2890 if (err)
2891 return err;
2892 (*line_cur2)++;
2894 /* Copy patched lines. */
2895 while (!feof(f2) && *line_cur2 <= end_new) {
2896 err = copy_one_line(f2, outfile, NULL);
2897 if (err)
2898 return err;
2899 (*line_cur2)++;
2901 /* Skip over old file's replaced lines. */
2902 while (!feof(f1) && *line_cur1 <= end_old) {
2903 if (rejectfile)
2904 err = copy_one_line(f1, NULL, rejectfile);
2905 else
2906 err = skip_one_line(f1);
2907 if (err)
2908 return err;
2909 (*line_cur1)++;
2912 return NULL;
2915 static const struct got_error *
2916 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2917 FILE *outfile, FILE *rejectfile)
2919 const struct got_error *err;
2921 if (outfile) {
2922 /* Copy old file's lines until EOF. */
2923 while (!feof(f1)) {
2924 err = copy_one_line(f1, outfile, NULL);
2925 if (err)
2926 return err;
2927 (*line_cur1)++;
2930 if (rejectfile) {
2931 /* Copy new file's lines until EOF. */
2932 while (!feof(f2)) {
2933 err = copy_one_line(f2, NULL, rejectfile);
2934 if (err)
2935 return err;
2936 (*line_cur2)++;
2940 return NULL;
2943 static const struct got_error *
2944 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2945 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2946 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2947 int *line_cur2, FILE *outfile, FILE *rejectfile,
2948 got_worktree_patch_cb patch_cb, void *patch_arg)
2950 const struct got_error *err = NULL;
2951 int start_old = change->cv.a;
2952 int end_old = change->cv.b;
2953 int start_new = change->cv.c;
2954 int end_new = change->cv.d;
2955 long pos1, pos2;
2956 FILE *hunkfile;
2958 *choice = GOT_PATCH_CHOICE_NONE;
2960 hunkfile = got_opentemp();
2961 if (hunkfile == NULL)
2962 return got_error_from_errno("got_opentemp");
2964 pos1 = ftell(f1);
2965 pos2 = ftell(f2);
2967 /* XXX TODO needs error checking */
2968 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2970 if (fseek(f1, pos1, SEEK_SET) == -1) {
2971 err = got_ferror(f1, GOT_ERR_IO);
2972 goto done;
2974 if (fseek(f2, pos2, SEEK_SET) == -1) {
2975 err = got_ferror(f1, GOT_ERR_IO);
2976 goto done;
2978 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2979 err = got_ferror(hunkfile, GOT_ERR_IO);
2980 goto done;
2983 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2984 hunkfile, n, nchanges);
2985 if (err)
2986 goto done;
2988 switch (*choice) {
2989 case GOT_PATCH_CHOICE_YES:
2990 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2991 end_old, start_new, end_new, outfile, rejectfile);
2992 break;
2993 case GOT_PATCH_CHOICE_NO:
2994 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2995 end_old, start_new, end_new, rejectfile, outfile);
2996 break;
2997 case GOT_PATCH_CHOICE_QUIT:
2998 break;
2999 default:
3000 err = got_error(GOT_ERR_PATCH_CHOICE);
3001 break;
3003 done:
3004 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3005 err = got_error_from_errno("fclose");
3006 return err;
3009 struct revert_file_args {
3010 struct got_worktree *worktree;
3011 struct got_fileindex *fileindex;
3012 got_worktree_checkout_cb progress_cb;
3013 void *progress_arg;
3014 got_worktree_patch_cb patch_cb;
3015 void *patch_arg;
3016 struct got_repository *repo;
3019 static const struct got_error *
3020 create_patched_content(char **path_outfile, int reverse_patch,
3021 struct got_object_id *blob_id, const char *path2,
3022 const char *relpath, struct got_repository *repo,
3023 got_worktree_patch_cb patch_cb, void *patch_arg)
3025 const struct got_error *err;
3026 struct got_blob_object *blob = NULL;
3027 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3028 char *path1 = NULL, *id_str = NULL;
3029 struct stat sb1, sb2;
3030 struct got_diff_changes *changes = NULL;
3031 struct got_diff_state *ds = NULL;
3032 struct got_diff_args *args = NULL;
3033 struct got_diff_change *change;
3034 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3035 int n = 0;
3037 *path_outfile = NULL;
3039 err = got_object_id_str(&id_str, blob_id);
3040 if (err)
3041 return err;
3043 f2 = fopen(path2, "r");
3044 if (f2 == NULL) {
3045 err = got_error_from_errno2("fopen", path2);
3046 goto done;
3049 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3050 if (err)
3051 goto done;
3053 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3054 if (err)
3055 goto done;
3057 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3058 if (err)
3059 goto done;
3061 if (stat(path1, &sb1) == -1) {
3062 err = got_error_from_errno2("stat", path1);
3063 goto done;
3065 if (stat(path2, &sb2) == -1) {
3066 err = got_error_from_errno2("stat", path2);
3067 goto done;
3070 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3071 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3072 if (err)
3073 goto done;
3075 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3076 if (err)
3077 goto done;
3079 if (fseek(f1, 0L, SEEK_SET) == -1)
3080 return got_ferror(f1, GOT_ERR_IO);
3081 if (fseek(f2, 0L, SEEK_SET) == -1)
3082 return got_ferror(f2, GOT_ERR_IO);
3083 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3084 int choice;
3085 err = apply_or_reject_change(&choice, change, ++n,
3086 changes->nchanges, ds, args, diff_flags, relpath,
3087 f1, f2, &line_cur1, &line_cur2,
3088 reverse_patch ? NULL : outfile,
3089 reverse_patch ? outfile : NULL,
3090 patch_cb, patch_arg);
3091 if (err)
3092 goto done;
3093 if (choice == GOT_PATCH_CHOICE_YES)
3094 have_content = 1;
3095 else if (choice == GOT_PATCH_CHOICE_QUIT)
3096 break;
3098 if (have_content)
3099 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3100 reverse_patch ? NULL : outfile,
3101 reverse_patch ? outfile : NULL);
3102 done:
3103 free(id_str);
3104 if (blob)
3105 got_object_blob_close(blob);
3106 if (f1 && fclose(f1) == EOF && err == NULL)
3107 err = got_error_from_errno2("fclose", path1);
3108 if (f2 && fclose(f2) == EOF && err == NULL)
3109 err = got_error_from_errno2("fclose", path2);
3110 if (outfile && fclose(outfile) == EOF && err == NULL)
3111 err = got_error_from_errno2("fclose", *path_outfile);
3112 if (path1 && unlink(path1) == -1 && err == NULL)
3113 err = got_error_from_errno2("unlink", path1);
3114 if (err || !have_content) {
3115 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3116 err = got_error_from_errno2("unlink", *path_outfile);
3117 free(*path_outfile);
3118 *path_outfile = NULL;
3120 free(args);
3121 if (ds) {
3122 got_diff_state_free(ds);
3123 free(ds);
3125 if (changes)
3126 got_diff_free_changes(changes);
3127 free(path1);
3128 return err;
3131 static const struct got_error *
3132 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3133 const char *relpath, struct got_object_id *blob_id,
3134 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3136 struct revert_file_args *a = arg;
3137 const struct got_error *err = NULL;
3138 char *parent_path = NULL;
3139 struct got_fileindex_entry *ie;
3140 struct got_tree_object *tree = NULL;
3141 struct got_object_id *tree_id = NULL;
3142 const struct got_tree_entry *te = NULL;
3143 char *tree_path = NULL, *te_name;
3144 char *ondisk_path = NULL, *path_content = NULL;
3145 struct got_blob_object *blob = NULL;
3147 /* Reverting a staged deletion is a no-op. */
3148 if (status == GOT_STATUS_DELETE &&
3149 staged_status != GOT_STATUS_NO_CHANGE)
3150 return NULL;
3152 if (status == GOT_STATUS_UNVERSIONED)
3153 return (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_UNVERSIONED, relpath);
3156 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3157 if (ie == NULL)
3158 return got_error(GOT_ERR_BAD_PATH);
3160 /* Construct in-repository path of tree which contains this blob. */
3161 err = got_path_dirname(&parent_path, ie->path);
3162 if (err) {
3163 if (err->code != GOT_ERR_BAD_PATH)
3164 goto done;
3165 parent_path = strdup("/");
3166 if (parent_path == NULL) {
3167 err = got_error_from_errno("strdup");
3168 goto done;
3171 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3172 tree_path = strdup(parent_path);
3173 if (tree_path == NULL) {
3174 err = got_error_from_errno("strdup");
3175 goto done;
3177 } else {
3178 if (got_path_is_root_dir(parent_path)) {
3179 tree_path = strdup(a->worktree->path_prefix);
3180 if (tree_path == NULL) {
3181 err = got_error_from_errno("strdup");
3182 goto done;
3184 } else {
3185 if (asprintf(&tree_path, "%s/%s",
3186 a->worktree->path_prefix, parent_path) == -1) {
3187 err = got_error_from_errno("asprintf");
3188 goto done;
3193 err = got_object_id_by_path(&tree_id, a->repo,
3194 a->worktree->base_commit_id, tree_path);
3195 if (err) {
3196 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3197 (status == GOT_STATUS_ADD ||
3198 staged_status == GOT_STATUS_ADD)))
3199 goto done;
3200 } else {
3201 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3202 if (err)
3203 goto done;
3205 te_name = basename(ie->path);
3206 if (te_name == NULL) {
3207 err = got_error_from_errno2("basename", ie->path);
3208 goto done;
3211 te = got_object_tree_find_entry(tree, te_name);
3212 if (te == NULL && status != GOT_STATUS_ADD &&
3213 staged_status != GOT_STATUS_ADD) {
3214 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3215 goto done;
3219 switch (status) {
3220 case GOT_STATUS_ADD:
3221 if (a->patch_cb) {
3222 int choice = GOT_PATCH_CHOICE_NONE;
3223 err = (*a->patch_cb)(&choice, a->patch_arg,
3224 status, ie->path, NULL, 1, 1);
3225 if (err)
3226 goto done;
3227 if (choice != GOT_PATCH_CHOICE_YES)
3228 break;
3230 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3231 ie->path);
3232 if (err)
3233 goto done;
3234 got_fileindex_entry_remove(a->fileindex, ie);
3235 break;
3236 case GOT_STATUS_DELETE:
3237 if (a->patch_cb) {
3238 int choice = GOT_PATCH_CHOICE_NONE;
3239 err = (*a->patch_cb)(&choice, a->patch_arg,
3240 status, ie->path, NULL, 1, 1);
3241 if (err)
3242 goto done;
3243 if (choice != GOT_PATCH_CHOICE_YES)
3244 break;
3246 /* fall through */
3247 case GOT_STATUS_MODIFY:
3248 case GOT_STATUS_CONFLICT:
3249 case GOT_STATUS_MISSING: {
3250 struct got_object_id id;
3251 if (staged_status == GOT_STATUS_ADD ||
3252 staged_status == GOT_STATUS_MODIFY) {
3253 memcpy(id.sha1, ie->staged_blob_sha1,
3254 SHA1_DIGEST_LENGTH);
3255 } else
3256 memcpy(id.sha1, ie->blob_sha1,
3257 SHA1_DIGEST_LENGTH);
3258 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3259 if (err)
3260 goto done;
3262 if (asprintf(&ondisk_path, "%s/%s",
3263 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3264 err = got_error_from_errno("asprintf");
3265 goto done;
3268 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3269 status == GOT_STATUS_CONFLICT)) {
3270 err = create_patched_content(&path_content, 1, &id,
3271 ondisk_path, ie->path, a->repo,
3272 a->patch_cb, a->patch_arg);
3273 if (err || path_content == NULL)
3274 break;
3275 if (rename(path_content, ondisk_path) == -1) {
3276 err = got_error_from_errno3("rename",
3277 path_content, ondisk_path);
3278 goto done;
3280 } else {
3281 err = install_blob(a->worktree, ondisk_path, ie->path,
3282 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3283 got_fileindex_perms_to_st(ie), blob, 0, 1,
3284 a->repo, a->progress_cb, a->progress_arg);
3285 if (err)
3286 goto done;
3287 if (status == GOT_STATUS_DELETE) {
3288 err = update_blob_fileindex_entry(a->worktree,
3289 a->fileindex, ie, ondisk_path, ie->path,
3290 blob, 1);
3291 if (err)
3292 goto done;
3295 break;
3297 default:
3298 break;
3300 done:
3301 free(ondisk_path);
3302 free(path_content);
3303 free(parent_path);
3304 free(tree_path);
3305 if (blob)
3306 got_object_blob_close(blob);
3307 if (tree)
3308 got_object_tree_close(tree);
3309 free(tree_id);
3310 return err;
3313 const struct got_error *
3314 got_worktree_revert(struct got_worktree *worktree,
3315 struct got_pathlist_head *paths,
3316 got_worktree_checkout_cb progress_cb, void *progress_arg,
3317 got_worktree_patch_cb patch_cb, void *patch_arg,
3318 struct got_repository *repo)
3320 struct got_fileindex *fileindex = NULL;
3321 char *fileindex_path = NULL;
3322 const struct got_error *err = NULL, *unlockerr = NULL;
3323 const struct got_error *sync_err = NULL;
3324 struct got_pathlist_entry *pe;
3325 struct revert_file_args rfa;
3327 err = lock_worktree(worktree, LOCK_EX);
3328 if (err)
3329 return err;
3331 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3332 if (err)
3333 goto done;
3335 rfa.worktree = worktree;
3336 rfa.fileindex = fileindex;
3337 rfa.progress_cb = progress_cb;
3338 rfa.progress_arg = progress_arg;
3339 rfa.patch_cb = patch_cb;
3340 rfa.patch_arg = patch_arg;
3341 rfa.repo = repo;
3342 TAILQ_FOREACH(pe, paths, entry) {
3343 err = worktree_status(worktree, pe->path, fileindex, repo,
3344 revert_file, &rfa, NULL, NULL);
3345 if (err)
3346 break;
3348 sync_err = sync_fileindex(fileindex, fileindex_path);
3349 if (sync_err && err == NULL)
3350 err = sync_err;
3351 done:
3352 free(fileindex_path);
3353 if (fileindex)
3354 got_fileindex_free(fileindex);
3355 unlockerr = lock_worktree(worktree, LOCK_SH);
3356 if (unlockerr && err == NULL)
3357 err = unlockerr;
3358 return err;
3361 static void
3362 free_commitable(struct got_commitable *ct)
3364 free(ct->path);
3365 free(ct->in_repo_path);
3366 free(ct->ondisk_path);
3367 free(ct->blob_id);
3368 free(ct->base_blob_id);
3369 free(ct->staged_blob_id);
3370 free(ct->base_commit_id);
3371 free(ct);
3374 struct collect_commitables_arg {
3375 struct got_pathlist_head *commitable_paths;
3376 struct got_repository *repo;
3377 struct got_worktree *worktree;
3378 int have_staged_files;
3381 static const struct got_error *
3382 collect_commitables(void *arg, unsigned char status,
3383 unsigned char staged_status, const char *relpath,
3384 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3385 struct got_object_id *commit_id)
3387 struct collect_commitables_arg *a = arg;
3388 const struct got_error *err = NULL;
3389 struct got_commitable *ct = NULL;
3390 struct got_pathlist_entry *new = NULL;
3391 char *parent_path = NULL, *path = NULL;
3392 struct stat sb;
3394 if (a->have_staged_files) {
3395 if (staged_status != GOT_STATUS_MODIFY &&
3396 staged_status != GOT_STATUS_ADD &&
3397 staged_status != GOT_STATUS_DELETE)
3398 return NULL;
3399 } else {
3400 if (status == GOT_STATUS_CONFLICT)
3401 return got_error(GOT_ERR_COMMIT_CONFLICT);
3403 if (status != GOT_STATUS_MODIFY &&
3404 status != GOT_STATUS_ADD &&
3405 status != GOT_STATUS_DELETE)
3406 return NULL;
3409 if (asprintf(&path, "/%s", relpath) == -1) {
3410 err = got_error_from_errno("asprintf");
3411 goto done;
3413 if (strcmp(path, "/") == 0) {
3414 parent_path = strdup("");
3415 if (parent_path == NULL)
3416 return got_error_from_errno("strdup");
3417 } else {
3418 err = got_path_dirname(&parent_path, path);
3419 if (err)
3420 return err;
3423 ct = calloc(1, sizeof(*ct));
3424 if (ct == NULL) {
3425 err = got_error_from_errno("calloc");
3426 goto done;
3429 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3430 relpath) == -1) {
3431 err = got_error_from_errno("asprintf");
3432 goto done;
3434 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3435 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3436 } else {
3437 if (lstat(ct->ondisk_path, &sb) != 0) {
3438 err = got_error_from_errno2("lstat", ct->ondisk_path);
3439 goto done;
3441 ct->mode = sb.st_mode;
3444 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3445 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3446 relpath) == -1) {
3447 err = got_error_from_errno("asprintf");
3448 goto done;
3451 ct->status = status;
3452 ct->staged_status = staged_status;
3453 ct->blob_id = NULL; /* will be filled in when blob gets created */
3454 if (ct->status != GOT_STATUS_ADD &&
3455 ct->staged_status != GOT_STATUS_ADD) {
3456 ct->base_blob_id = got_object_id_dup(blob_id);
3457 if (ct->base_blob_id == NULL) {
3458 err = got_error_from_errno("got_object_id_dup");
3459 goto done;
3461 ct->base_commit_id = got_object_id_dup(commit_id);
3462 if (ct->base_commit_id == NULL) {
3463 err = got_error_from_errno("got_object_id_dup");
3464 goto done;
3467 if (ct->staged_status == GOT_STATUS_ADD ||
3468 ct->staged_status == GOT_STATUS_MODIFY) {
3469 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3470 if (ct->staged_blob_id == NULL) {
3471 err = got_error_from_errno("got_object_id_dup");
3472 goto done;
3475 ct->path = strdup(path);
3476 if (ct->path == NULL) {
3477 err = got_error_from_errno("strdup");
3478 goto done;
3480 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3481 done:
3482 if (ct && (err || new == NULL))
3483 free_commitable(ct);
3484 free(parent_path);
3485 free(path);
3486 return err;
3489 static const struct got_error *write_tree(struct got_object_id **,
3490 struct got_tree_object *, const char *, struct got_pathlist_head *,
3491 got_worktree_status_cb status_cb, void *status_arg,
3492 struct got_repository *);
3494 static const struct got_error *
3495 write_subtree(struct got_object_id **new_subtree_id,
3496 struct got_tree_entry *te, const char *parent_path,
3497 struct got_pathlist_head *commitable_paths,
3498 got_worktree_status_cb status_cb, void *status_arg,
3499 struct got_repository *repo)
3501 const struct got_error *err = NULL;
3502 struct got_tree_object *subtree;
3503 char *subpath;
3505 if (asprintf(&subpath, "%s%s%s", parent_path,
3506 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3507 return got_error_from_errno("asprintf");
3509 err = got_object_open_as_tree(&subtree, repo, te->id);
3510 if (err)
3511 return err;
3513 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3514 status_cb, status_arg, repo);
3515 got_object_tree_close(subtree);
3516 free(subpath);
3517 return err;
3520 static const struct got_error *
3521 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3523 const struct got_error *err = NULL;
3524 char *ct_parent_path = NULL;
3526 *match = 0;
3528 if (strchr(ct->in_repo_path, '/') == NULL) {
3529 *match = got_path_is_root_dir(path);
3530 return NULL;
3533 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3534 if (err)
3535 return err;
3536 *match = (strcmp(path, ct_parent_path) == 0);
3537 free(ct_parent_path);
3538 return err;
3541 static mode_t
3542 get_ct_file_mode(struct got_commitable *ct)
3544 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3547 static const struct got_error *
3548 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3549 struct got_tree_entry *te, struct got_commitable *ct)
3551 const struct got_error *err = NULL;
3553 *new_te = NULL;
3555 err = got_object_tree_entry_dup(new_te, te);
3556 if (err)
3557 goto done;
3559 (*new_te)->mode = get_ct_file_mode(ct);
3561 free((*new_te)->id);
3562 if (ct->staged_status == GOT_STATUS_MODIFY)
3563 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3564 else
3565 (*new_te)->id = got_object_id_dup(ct->blob_id);
3566 if ((*new_te)->id == NULL) {
3567 err = got_error_from_errno("got_object_id_dup");
3568 goto done;
3570 done:
3571 if (err && *new_te) {
3572 got_object_tree_entry_close(*new_te);
3573 *new_te = NULL;
3575 return err;
3578 static const struct got_error *
3579 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3580 struct got_commitable *ct)
3582 const struct got_error *err = NULL;
3583 char *ct_name;
3585 *new_te = NULL;
3587 *new_te = calloc(1, sizeof(**new_te));
3588 if (*new_te == NULL)
3589 return got_error_from_errno("calloc");
3591 ct_name = basename(ct->path);
3592 if (ct_name == NULL) {
3593 err = got_error_from_errno2("basename", ct->path);
3594 goto done;
3596 (*new_te)->name = strdup(ct_name);
3597 if ((*new_te)->name == NULL) {
3598 err = got_error_from_errno("strdup");
3599 goto done;
3602 (*new_te)->mode = get_ct_file_mode(ct);
3604 if (ct->staged_status == GOT_STATUS_ADD)
3605 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3606 else
3607 (*new_te)->id = got_object_id_dup(ct->blob_id);
3608 if ((*new_te)->id == NULL) {
3609 err = got_error_from_errno("got_object_id_dup");
3610 goto done;
3612 done:
3613 if (err && *new_te) {
3614 got_object_tree_entry_close(*new_te);
3615 *new_te = NULL;
3617 return err;
3620 static const struct got_error *
3621 insert_tree_entry(struct got_tree_entry *new_te,
3622 struct got_pathlist_head *paths)
3624 const struct got_error *err = NULL;
3625 struct got_pathlist_entry *new_pe;
3627 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3628 if (err)
3629 return err;
3630 if (new_pe == NULL)
3631 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3632 return NULL;
3635 static const struct got_error *
3636 report_ct_status(struct got_commitable *ct,
3637 got_worktree_status_cb status_cb, void *status_arg)
3639 const char *ct_path = ct->path;
3640 unsigned char status;
3642 while (ct_path[0] == '/')
3643 ct_path++;
3645 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3646 status = ct->staged_status;
3647 else
3648 status = ct->status;
3650 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3651 ct_path, ct->blob_id, NULL, NULL);
3654 static const struct got_error *
3655 match_modified_subtree(int *modified, struct got_tree_entry *te,
3656 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3658 const struct got_error *err = NULL;
3659 struct got_pathlist_entry *pe;
3660 char *te_path;
3662 *modified = 0;
3664 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3665 got_path_is_root_dir(base_tree_path) ? "" : "/",
3666 te->name) == -1)
3667 return got_error_from_errno("asprintf");
3669 TAILQ_FOREACH(pe, commitable_paths, entry) {
3670 struct got_commitable *ct = pe->data;
3671 *modified = got_path_is_child(ct->in_repo_path, te_path,
3672 strlen(te_path));
3673 if (*modified)
3674 break;
3677 free(te_path);
3678 return err;
3681 static const struct got_error *
3682 match_deleted_or_modified_ct(struct got_commitable **ctp,
3683 struct got_tree_entry *te, const char *base_tree_path,
3684 struct got_pathlist_head *commitable_paths)
3686 const struct got_error *err = NULL;
3687 struct got_pathlist_entry *pe;
3689 *ctp = NULL;
3691 TAILQ_FOREACH(pe, commitable_paths, entry) {
3692 struct got_commitable *ct = pe->data;
3693 char *ct_name = NULL;
3694 int path_matches;
3696 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3697 if (ct->status != GOT_STATUS_MODIFY &&
3698 ct->status != GOT_STATUS_DELETE)
3699 continue;
3700 } else {
3701 if (ct->staged_status != GOT_STATUS_MODIFY &&
3702 ct->staged_status != GOT_STATUS_DELETE)
3703 continue;
3706 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3707 continue;
3709 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3710 if (err)
3711 return err;
3712 if (!path_matches)
3713 continue;
3715 ct_name = basename(pe->path);
3716 if (ct_name == NULL)
3717 return got_error_from_errno2("basename", pe->path);
3719 if (strcmp(te->name, ct_name) != 0)
3720 continue;
3722 *ctp = ct;
3723 break;
3726 return err;
3729 static const struct got_error *
3730 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3731 const char *child_path, const char *path_base_tree,
3732 struct got_pathlist_head *commitable_paths,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 struct got_repository *repo)
3736 const struct got_error *err = NULL;
3737 struct got_tree_entry *new_te;
3738 char *subtree_path;
3740 *new_tep = NULL;
3742 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3743 got_path_is_root_dir(path_base_tree) ? "" : "/",
3744 child_path) == -1)
3745 return got_error_from_errno("asprintf");
3747 new_te = calloc(1, sizeof(*new_te));
3748 if (new_te == NULL)
3749 return got_error_from_errno("calloc");
3750 new_te->mode = S_IFDIR;
3751 new_te->name = strdup(child_path);
3752 if (new_te->name == NULL) {
3753 err = got_error_from_errno("strdup");
3754 got_object_tree_entry_close(new_te);
3755 goto done;
3757 err = write_tree(&new_te->id, NULL, subtree_path,
3758 commitable_paths, status_cb, status_arg, repo);
3759 if (err) {
3760 got_object_tree_entry_close(new_te);
3761 goto done;
3763 done:
3764 free(subtree_path);
3765 if (err == NULL)
3766 *new_tep = new_te;
3767 return err;
3770 static const struct got_error *
3771 write_tree(struct got_object_id **new_tree_id,
3772 struct got_tree_object *base_tree, const char *path_base_tree,
3773 struct got_pathlist_head *commitable_paths,
3774 got_worktree_status_cb status_cb, void *status_arg,
3775 struct got_repository *repo)
3777 const struct got_error *err = NULL;
3778 const struct got_tree_entries *base_entries = NULL;
3779 struct got_pathlist_head paths;
3780 struct got_tree_entries new_tree_entries;
3781 struct got_tree_entry *te, *new_te = NULL;
3782 struct got_pathlist_entry *pe;
3784 TAILQ_INIT(&paths);
3785 new_tree_entries.nentries = 0;
3786 SIMPLEQ_INIT(&new_tree_entries.head);
3788 /* Insert, and recurse into, newly added entries first. */
3789 TAILQ_FOREACH(pe, commitable_paths, entry) {
3790 struct got_commitable *ct = pe->data;
3791 char *child_path = NULL, *slash;
3793 if ((ct->status != GOT_STATUS_ADD &&
3794 ct->staged_status != GOT_STATUS_ADD) ||
3795 (ct->flags & GOT_COMMITABLE_ADDED))
3796 continue;
3798 if (!got_path_is_child(pe->path, path_base_tree,
3799 strlen(path_base_tree)))
3800 continue;
3802 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3803 pe->path);
3804 if (err)
3805 goto done;
3807 slash = strchr(child_path, '/');
3808 if (slash == NULL) {
3809 err = alloc_added_blob_tree_entry(&new_te, ct);
3810 if (err)
3811 goto done;
3812 err = report_ct_status(ct, status_cb, status_arg);
3813 if (err)
3814 goto done;
3815 ct->flags |= GOT_COMMITABLE_ADDED;
3816 err = insert_tree_entry(new_te, &paths);
3817 if (err)
3818 goto done;
3819 } else {
3820 *slash = '\0'; /* trim trailing path components */
3821 if (base_tree == NULL ||
3822 got_object_tree_find_entry(base_tree, child_path)
3823 == NULL) {
3824 err = make_subtree_for_added_blob(&new_te,
3825 child_path, path_base_tree,
3826 commitable_paths, status_cb, status_arg,
3827 repo);
3828 if (err)
3829 goto done;
3830 err = insert_tree_entry(new_te, &paths);
3831 if (err)
3832 goto done;
3837 if (base_tree) {
3838 /* Handle modified and deleted entries. */
3839 base_entries = got_object_tree_get_entries(base_tree);
3840 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3841 struct got_commitable *ct = NULL;
3843 if (got_object_tree_entry_is_submodule(te)) {
3844 /* Entry is a submodule; just copy it. */
3845 err = got_object_tree_entry_dup(&new_te, te);
3846 if (err)
3847 goto done;
3848 err = insert_tree_entry(new_te, &paths);
3849 if (err)
3850 goto done;
3851 continue;
3854 if (S_ISDIR(te->mode)) {
3855 int modified;
3856 err = got_object_tree_entry_dup(&new_te, te);
3857 if (err)
3858 goto done;
3859 err = match_modified_subtree(&modified, te,
3860 path_base_tree, commitable_paths);
3861 if (err)
3862 goto done;
3863 /* Avoid recursion into unmodified subtrees. */
3864 if (modified) {
3865 free(new_te->id);
3866 err = write_subtree(&new_te->id, te,
3867 path_base_tree, commitable_paths,
3868 status_cb, status_arg, repo);
3869 if (err)
3870 goto done;
3872 err = insert_tree_entry(new_te, &paths);
3873 if (err)
3874 goto done;
3875 continue;
3878 err = match_deleted_or_modified_ct(&ct, te,
3879 path_base_tree, commitable_paths);
3880 if (ct) {
3881 /* NB: Deleted entries get dropped here. */
3882 if (ct->status == GOT_STATUS_MODIFY ||
3883 ct->staged_status == GOT_STATUS_MODIFY) {
3884 err = alloc_modified_blob_tree_entry(
3885 &new_te, te, ct);
3886 if (err)
3887 goto done;
3888 err = insert_tree_entry(new_te, &paths);
3889 if (err)
3890 goto done;
3892 err = report_ct_status(ct, status_cb,
3893 status_arg);
3894 if (err)
3895 goto done;
3896 } else {
3897 /* Entry is unchanged; just copy it. */
3898 err = got_object_tree_entry_dup(&new_te, te);
3899 if (err)
3900 goto done;
3901 err = insert_tree_entry(new_te, &paths);
3902 if (err)
3903 goto done;
3908 /* Write new list of entries; deleted entries have been dropped. */
3909 TAILQ_FOREACH(pe, &paths, entry) {
3910 struct got_tree_entry *te = pe->data;
3911 new_tree_entries.nentries++;
3912 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3914 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3915 done:
3916 got_object_tree_entries_close(&new_tree_entries);
3917 got_pathlist_free(&paths);
3918 return err;
3921 static const struct got_error *
3922 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3923 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3924 int have_staged_files)
3926 const struct got_error *err = NULL;
3927 struct got_pathlist_entry *pe;
3929 TAILQ_FOREACH(pe, commitable_paths, entry) {
3930 struct got_fileindex_entry *ie;
3931 struct got_commitable *ct = pe->data;
3933 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3934 if (ie) {
3935 if (ct->status == GOT_STATUS_DELETE ||
3936 ct->staged_status == GOT_STATUS_DELETE) {
3937 got_fileindex_entry_remove(fileindex, ie);
3938 got_fileindex_entry_free(ie);
3939 } else if (ct->staged_status == GOT_STATUS_ADD ||
3940 ct->staged_status == GOT_STATUS_MODIFY) {
3941 got_fileindex_entry_stage_set(ie,
3942 GOT_FILEIDX_STAGE_NONE);
3943 err = got_fileindex_entry_update(ie,
3944 ct->ondisk_path, ct->staged_blob_id->sha1,
3945 new_base_commit_id->sha1,
3946 !have_staged_files);
3947 } else
3948 err = got_fileindex_entry_update(ie,
3949 ct->ondisk_path, ct->blob_id->sha1,
3950 new_base_commit_id->sha1,
3951 !have_staged_files);
3952 } else {
3953 err = got_fileindex_entry_alloc(&ie,
3954 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3955 new_base_commit_id->sha1);
3956 if (err)
3957 break;
3958 err = got_fileindex_entry_add(fileindex, ie);
3959 if (err)
3960 break;
3963 return err;
3967 static const struct got_error *
3968 check_out_of_date(const char *in_repo_path, unsigned char status,
3969 unsigned char staged_status, struct got_object_id *base_blob_id,
3970 struct got_object_id *base_commit_id,
3971 struct got_object_id *head_commit_id, struct got_repository *repo,
3972 int ood_errcode)
3974 const struct got_error *err = NULL;
3975 struct got_object_id *id = NULL;
3977 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3978 /* Trivial case: base commit == head commit */
3979 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3980 return NULL;
3982 * Ensure file content which local changes were based
3983 * on matches file content in the branch head.
3985 err = got_object_id_by_path(&id, repo, head_commit_id,
3986 in_repo_path);
3987 if (err) {
3988 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3989 err = got_error(ood_errcode);
3990 goto done;
3991 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3992 err = got_error(ood_errcode);
3993 } else {
3994 /* Require that added files don't exist in the branch head. */
3995 err = got_object_id_by_path(&id, repo, head_commit_id,
3996 in_repo_path);
3997 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3998 goto done;
3999 err = id ? got_error(ood_errcode) : NULL;
4001 done:
4002 free(id);
4003 return err;
4006 const struct got_error *
4007 commit_worktree(struct got_object_id **new_commit_id,
4008 struct got_pathlist_head *commitable_paths,
4009 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4010 const char *author, const char *committer,
4011 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4012 got_worktree_status_cb status_cb, void *status_arg,
4013 struct got_repository *repo)
4015 const struct got_error *err = NULL, *unlockerr = NULL;
4016 struct got_pathlist_entry *pe;
4017 const char *head_ref_name = NULL;
4018 struct got_commit_object *head_commit = NULL;
4019 struct got_reference *head_ref2 = NULL;
4020 struct got_object_id *head_commit_id2 = NULL;
4021 struct got_tree_object *head_tree = NULL;
4022 struct got_object_id *new_tree_id = NULL;
4023 struct got_object_id_queue parent_ids;
4024 struct got_object_qid *pid = NULL;
4025 char *logmsg = NULL;
4027 *new_commit_id = NULL;
4029 SIMPLEQ_INIT(&parent_ids);
4031 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4032 if (err)
4033 goto done;
4035 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4036 if (err)
4037 goto done;
4039 if (commit_msg_cb != NULL) {
4040 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4041 if (err)
4042 goto done;
4045 if (logmsg == NULL || strlen(logmsg) == 0) {
4046 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4047 goto done;
4050 /* Create blobs from added and modified files and record their IDs. */
4051 TAILQ_FOREACH(pe, commitable_paths, entry) {
4052 struct got_commitable *ct = pe->data;
4053 char *ondisk_path;
4055 /* Blobs for staged files already exist. */
4056 if (ct->staged_status == GOT_STATUS_ADD ||
4057 ct->staged_status == GOT_STATUS_MODIFY)
4058 continue;
4060 if (ct->status != GOT_STATUS_ADD &&
4061 ct->status != GOT_STATUS_MODIFY)
4062 continue;
4064 if (asprintf(&ondisk_path, "%s/%s",
4065 worktree->root_path, pe->path) == -1) {
4066 err = got_error_from_errno("asprintf");
4067 goto done;
4069 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4070 free(ondisk_path);
4071 if (err)
4072 goto done;
4075 /* Recursively write new tree objects. */
4076 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4077 status_cb, status_arg, repo);
4078 if (err)
4079 goto done;
4081 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4082 if (err)
4083 goto done;
4084 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4085 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4086 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4087 got_object_qid_free(pid);
4088 if (logmsg != NULL)
4089 free(logmsg);
4090 if (err)
4091 goto done;
4093 /* Check if a concurrent commit to our branch has occurred. */
4094 head_ref_name = got_worktree_get_head_ref_name(worktree);
4095 if (head_ref_name == NULL) {
4096 err = got_error_from_errno("got_worktree_get_head_ref_name");
4097 goto done;
4099 /* Lock the reference here to prevent concurrent modification. */
4100 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4101 if (err)
4102 goto done;
4103 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4104 if (err)
4105 goto done;
4106 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4107 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4108 goto done;
4110 /* Update branch head in repository. */
4111 err = got_ref_change_ref(head_ref2, *new_commit_id);
4112 if (err)
4113 goto done;
4114 err = got_ref_write(head_ref2, repo);
4115 if (err)
4116 goto done;
4118 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4119 if (err)
4120 goto done;
4122 err = ref_base_commit(worktree, repo);
4123 if (err)
4124 goto done;
4125 done:
4126 if (head_tree)
4127 got_object_tree_close(head_tree);
4128 if (head_commit)
4129 got_object_commit_close(head_commit);
4130 free(head_commit_id2);
4131 if (head_ref2) {
4132 unlockerr = got_ref_unlock(head_ref2);
4133 if (unlockerr && err == NULL)
4134 err = unlockerr;
4135 got_ref_close(head_ref2);
4137 return err;
4140 static const struct got_error *
4141 check_path_is_commitable(const char *path,
4142 struct got_pathlist_head *commitable_paths)
4144 struct got_pathlist_entry *cpe = NULL;
4145 size_t path_len = strlen(path);
4147 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4148 struct got_commitable *ct = cpe->data;
4149 const char *ct_path = ct->path;
4151 while (ct_path[0] == '/')
4152 ct_path++;
4154 if (strcmp(path, ct_path) == 0 ||
4155 got_path_is_child(ct_path, path, path_len))
4156 break;
4159 if (cpe == NULL)
4160 return got_error_path(path, GOT_ERR_BAD_PATH);
4162 return NULL;
4165 static const struct got_error *
4166 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4168 int *have_staged_files = arg;
4170 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4171 *have_staged_files = 1;
4172 return got_error(GOT_ERR_CANCELLED);
4175 return NULL;
4178 static const struct got_error *
4179 check_non_staged_files(struct got_fileindex *fileindex,
4180 struct got_pathlist_head *paths)
4182 struct got_pathlist_entry *pe;
4183 struct got_fileindex_entry *ie;
4185 TAILQ_FOREACH(pe, paths, entry) {
4186 if (pe->path[0] == '\0')
4187 continue;
4188 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4189 if (ie == NULL)
4190 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4191 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4192 return got_error_path(pe->path,
4193 GOT_ERR_FILE_NOT_STAGED);
4196 return NULL;
4199 const struct got_error *
4200 got_worktree_commit(struct got_object_id **new_commit_id,
4201 struct got_worktree *worktree, struct got_pathlist_head *paths,
4202 const char *author, const char *committer,
4203 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4204 got_worktree_status_cb status_cb, void *status_arg,
4205 struct got_repository *repo)
4207 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4208 struct got_fileindex *fileindex = NULL;
4209 char *fileindex_path = NULL;
4210 struct got_pathlist_head commitable_paths;
4211 struct collect_commitables_arg cc_arg;
4212 struct got_pathlist_entry *pe;
4213 struct got_reference *head_ref = NULL;
4214 struct got_object_id *head_commit_id = NULL;
4215 int have_staged_files = 0;
4217 *new_commit_id = NULL;
4219 TAILQ_INIT(&commitable_paths);
4221 err = lock_worktree(worktree, LOCK_EX);
4222 if (err)
4223 goto done;
4225 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4226 if (err)
4227 goto done;
4229 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4230 if (err)
4231 goto done;
4233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4234 if (err)
4235 goto done;
4237 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4238 &have_staged_files);
4239 if (err && err->code != GOT_ERR_CANCELLED)
4240 goto done;
4241 if (have_staged_files) {
4242 err = check_non_staged_files(fileindex, paths);
4243 if (err)
4244 goto done;
4247 cc_arg.commitable_paths = &commitable_paths;
4248 cc_arg.worktree = worktree;
4249 cc_arg.repo = repo;
4250 cc_arg.have_staged_files = have_staged_files;
4251 TAILQ_FOREACH(pe, paths, entry) {
4252 err = worktree_status(worktree, pe->path, fileindex, repo,
4253 collect_commitables, &cc_arg, NULL, NULL);
4254 if (err)
4255 goto done;
4258 if (TAILQ_EMPTY(&commitable_paths)) {
4259 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4260 goto done;
4263 TAILQ_FOREACH(pe, paths, entry) {
4264 err = check_path_is_commitable(pe->path, &commitable_paths);
4265 if (err)
4266 goto done;
4269 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4270 struct got_commitable *ct = pe->data;
4271 const char *ct_path = ct->in_repo_path;
4273 while (ct_path[0] == '/')
4274 ct_path++;
4275 err = check_out_of_date(ct_path, ct->status,
4276 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4277 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4278 if (err)
4279 goto done;
4283 err = commit_worktree(new_commit_id, &commitable_paths,
4284 head_commit_id, worktree, author, committer,
4285 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4286 if (err)
4287 goto done;
4289 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4290 fileindex, have_staged_files);
4291 sync_err = sync_fileindex(fileindex, fileindex_path);
4292 if (sync_err && err == NULL)
4293 err = sync_err;
4294 done:
4295 if (fileindex)
4296 got_fileindex_free(fileindex);
4297 free(fileindex_path);
4298 unlockerr = lock_worktree(worktree, LOCK_SH);
4299 if (unlockerr && err == NULL)
4300 err = unlockerr;
4301 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4302 struct got_commitable *ct = pe->data;
4303 free_commitable(ct);
4305 got_pathlist_free(&commitable_paths);
4306 return err;
4309 const char *
4310 got_commitable_get_path(struct got_commitable *ct)
4312 return ct->path;
4315 unsigned int
4316 got_commitable_get_status(struct got_commitable *ct)
4318 return ct->status;
4321 struct check_rebase_ok_arg {
4322 struct got_worktree *worktree;
4323 struct got_repository *repo;
4326 static const struct got_error *
4327 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4329 const struct got_error *err = NULL;
4330 struct check_rebase_ok_arg *a = arg;
4331 unsigned char status;
4332 struct stat sb;
4333 char *ondisk_path;
4335 /* Reject rebase of a work tree with mixed base commits. */
4336 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4337 SHA1_DIGEST_LENGTH))
4338 return got_error(GOT_ERR_MIXED_COMMITS);
4340 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4341 == -1)
4342 return got_error_from_errno("asprintf");
4344 /* Reject rebase of a work tree with modified or staged files. */
4345 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4346 free(ondisk_path);
4347 if (err)
4348 return err;
4350 if (status != GOT_STATUS_NO_CHANGE)
4351 return got_error(GOT_ERR_MODIFIED);
4352 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4353 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4355 return NULL;
4358 const struct got_error *
4359 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4360 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4361 struct got_worktree *worktree, struct got_reference *branch,
4362 struct got_repository *repo)
4364 const struct got_error *err = NULL;
4365 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4366 char *branch_ref_name = NULL;
4367 char *fileindex_path = NULL;
4368 struct check_rebase_ok_arg ok_arg;
4369 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4371 *new_base_branch_ref = NULL;
4372 *tmp_branch = NULL;
4373 *fileindex = NULL;
4375 err = lock_worktree(worktree, LOCK_EX);
4376 if (err)
4377 return err;
4379 err = open_fileindex(fileindex, &fileindex_path, worktree);
4380 if (err)
4381 goto done;
4383 ok_arg.worktree = worktree;
4384 ok_arg.repo = repo;
4385 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4386 &ok_arg);
4387 if (err)
4388 goto done;
4390 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4391 if (err)
4392 goto done;
4394 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4395 if (err)
4396 goto done;
4398 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4399 if (err)
4400 goto done;
4402 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4403 0);
4404 if (err)
4405 goto done;
4407 err = got_ref_alloc_symref(new_base_branch_ref,
4408 new_base_branch_ref_name, wt_branch);
4409 if (err)
4410 goto done;
4411 err = got_ref_write(*new_base_branch_ref, repo);
4412 if (err)
4413 goto done;
4415 /* TODO Lock original branch's ref while rebasing? */
4417 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4418 if (err)
4419 goto done;
4421 err = got_ref_write(branch_ref, repo);
4422 if (err)
4423 goto done;
4425 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4426 worktree->base_commit_id);
4427 if (err)
4428 goto done;
4429 err = got_ref_write(*tmp_branch, repo);
4430 if (err)
4431 goto done;
4433 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4434 if (err)
4435 goto done;
4436 done:
4437 free(fileindex_path);
4438 free(tmp_branch_name);
4439 free(new_base_branch_ref_name);
4440 free(branch_ref_name);
4441 if (branch_ref)
4442 got_ref_close(branch_ref);
4443 if (wt_branch)
4444 got_ref_close(wt_branch);
4445 if (err) {
4446 if (*new_base_branch_ref) {
4447 got_ref_close(*new_base_branch_ref);
4448 *new_base_branch_ref = NULL;
4450 if (*tmp_branch) {
4451 got_ref_close(*tmp_branch);
4452 *tmp_branch = NULL;
4454 if (*fileindex) {
4455 got_fileindex_free(*fileindex);
4456 *fileindex = NULL;
4458 lock_worktree(worktree, LOCK_SH);
4460 return err;
4463 const struct got_error *
4464 got_worktree_rebase_continue(struct got_object_id **commit_id,
4465 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4466 struct got_reference **branch, struct got_fileindex **fileindex,
4467 struct got_worktree *worktree, struct got_repository *repo)
4469 const struct got_error *err;
4470 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4471 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4472 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4473 char *fileindex_path = NULL;
4474 int have_staged_files = 0;
4476 *commit_id = NULL;
4477 *new_base_branch = NULL;
4478 *tmp_branch = NULL;
4479 *branch = NULL;
4480 *fileindex = NULL;
4482 err = lock_worktree(worktree, LOCK_EX);
4483 if (err)
4484 return err;
4486 err = open_fileindex(fileindex, &fileindex_path, worktree);
4487 if (err)
4488 goto done;
4490 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4491 &have_staged_files);
4492 if (err && err->code != GOT_ERR_CANCELLED)
4493 goto done;
4494 if (have_staged_files) {
4495 err = got_error(GOT_ERR_STAGED_PATHS);
4496 goto done;
4499 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4500 if (err)
4501 goto done;
4503 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4504 if (err)
4505 goto done;
4507 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4508 if (err)
4509 goto done;
4511 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4512 if (err)
4513 goto done;
4515 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4516 if (err)
4517 goto done;
4519 err = got_ref_open(branch, repo,
4520 got_ref_get_symref_target(branch_ref), 0);
4521 if (err)
4522 goto done;
4524 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4525 if (err)
4526 goto done;
4528 err = got_ref_resolve(commit_id, repo, commit_ref);
4529 if (err)
4530 goto done;
4532 err = got_ref_open(new_base_branch, repo,
4533 new_base_branch_ref_name, 0);
4534 if (err)
4535 goto done;
4537 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4538 if (err)
4539 goto done;
4540 done:
4541 free(commit_ref_name);
4542 free(branch_ref_name);
4543 free(fileindex_path);
4544 if (commit_ref)
4545 got_ref_close(commit_ref);
4546 if (branch_ref)
4547 got_ref_close(branch_ref);
4548 if (err) {
4549 free(*commit_id);
4550 *commit_id = NULL;
4551 if (*tmp_branch) {
4552 got_ref_close(*tmp_branch);
4553 *tmp_branch = NULL;
4555 if (*new_base_branch) {
4556 got_ref_close(*new_base_branch);
4557 *new_base_branch = NULL;
4559 if (*branch) {
4560 got_ref_close(*branch);
4561 *branch = NULL;
4563 if (*fileindex) {
4564 got_fileindex_free(*fileindex);
4565 *fileindex = NULL;
4567 lock_worktree(worktree, LOCK_SH);
4569 return err;
4572 const struct got_error *
4573 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4575 const struct got_error *err;
4576 char *tmp_branch_name = NULL;
4578 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4579 if (err)
4580 return err;
4582 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4583 free(tmp_branch_name);
4584 return NULL;
4587 static const struct got_error *
4588 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4589 char **logmsg, void *arg)
4591 *logmsg = arg;
4592 return NULL;
4595 static const struct got_error *
4596 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4597 const char *path, struct got_object_id *blob_id,
4598 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4600 return NULL;
4603 struct collect_merged_paths_arg {
4604 got_worktree_checkout_cb progress_cb;
4605 void *progress_arg;
4606 struct got_pathlist_head *merged_paths;
4609 static const struct got_error *
4610 collect_merged_paths(void *arg, unsigned char status, const char *path)
4612 const struct got_error *err;
4613 struct collect_merged_paths_arg *a = arg;
4614 char *p;
4615 struct got_pathlist_entry *new;
4617 err = (*a->progress_cb)(a->progress_arg, status, path);
4618 if (err)
4619 return err;
4621 if (status != GOT_STATUS_MERGE &&
4622 status != GOT_STATUS_ADD &&
4623 status != GOT_STATUS_DELETE &&
4624 status != GOT_STATUS_CONFLICT)
4625 return NULL;
4627 p = strdup(path);
4628 if (p == NULL)
4629 return got_error_from_errno("strdup");
4631 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4632 if (err || new == NULL)
4633 free(p);
4634 return err;
4637 void
4638 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4640 struct got_pathlist_entry *pe;
4642 TAILQ_FOREACH(pe, merged_paths, entry)
4643 free((char *)pe->path);
4645 got_pathlist_free(merged_paths);
4648 static const struct got_error *
4649 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4650 struct got_repository *repo)
4652 const struct got_error *err;
4653 struct got_reference *commit_ref = NULL;
4655 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4656 if (err) {
4657 if (err->code != GOT_ERR_NOT_REF)
4658 goto done;
4659 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4660 if (err)
4661 goto done;
4662 err = got_ref_write(commit_ref, repo);
4663 if (err)
4664 goto done;
4665 } else {
4666 struct got_object_id *stored_id;
4667 int cmp;
4669 err = got_ref_resolve(&stored_id, repo, commit_ref);
4670 if (err)
4671 goto done;
4672 cmp = got_object_id_cmp(commit_id, stored_id);
4673 free(stored_id);
4674 if (cmp != 0) {
4675 err = got_error(GOT_ERR_REBASE_COMMITID);
4676 goto done;
4679 done:
4680 if (commit_ref)
4681 got_ref_close(commit_ref);
4682 return err;
4685 static const struct got_error *
4686 rebase_merge_files(struct got_pathlist_head *merged_paths,
4687 const char *commit_ref_name, struct got_worktree *worktree,
4688 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4689 struct got_object_id *commit_id, struct got_repository *repo,
4690 got_worktree_checkout_cb progress_cb, void *progress_arg,
4691 got_cancel_cb cancel_cb, void *cancel_arg)
4693 const struct got_error *err;
4694 struct got_reference *commit_ref = NULL;
4695 struct collect_merged_paths_arg cmp_arg;
4696 char *fileindex_path;
4698 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4700 err = get_fileindex_path(&fileindex_path, worktree);
4701 if (err)
4702 return err;
4704 cmp_arg.progress_cb = progress_cb;
4705 cmp_arg.progress_arg = progress_arg;
4706 cmp_arg.merged_paths = merged_paths;
4707 err = merge_files(worktree, fileindex, fileindex_path,
4708 parent_commit_id, commit_id, repo, collect_merged_paths,
4709 &cmp_arg, cancel_cb, cancel_arg);
4710 if (commit_ref)
4711 got_ref_close(commit_ref);
4712 return err;
4715 const struct got_error *
4716 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4717 struct got_worktree *worktree, struct got_fileindex *fileindex,
4718 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4719 struct got_repository *repo,
4720 got_worktree_checkout_cb progress_cb, void *progress_arg,
4721 got_cancel_cb cancel_cb, void *cancel_arg)
4723 const struct got_error *err;
4724 char *commit_ref_name;
4726 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4727 if (err)
4728 return err;
4730 err = store_commit_id(commit_ref_name, commit_id, repo);
4731 if (err)
4732 goto done;
4734 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4735 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4736 progress_arg, cancel_cb, cancel_arg);
4737 done:
4738 free(commit_ref_name);
4739 return err;
4742 const struct got_error *
4743 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4744 struct got_worktree *worktree, struct got_fileindex *fileindex,
4745 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4746 struct got_repository *repo,
4747 got_worktree_checkout_cb progress_cb, void *progress_arg,
4748 got_cancel_cb cancel_cb, void *cancel_arg)
4750 const struct got_error *err;
4751 char *commit_ref_name;
4753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4754 if (err)
4755 return err;
4757 err = store_commit_id(commit_ref_name, commit_id, repo);
4758 if (err)
4759 goto done;
4761 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4762 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4763 progress_arg, cancel_cb, cancel_arg);
4764 done:
4765 free(commit_ref_name);
4766 return err;
4769 static const struct got_error *
4770 rebase_commit(struct got_object_id **new_commit_id,
4771 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4772 struct got_worktree *worktree, struct got_fileindex *fileindex,
4773 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4774 const char *new_logmsg, struct got_repository *repo)
4776 const struct got_error *err, *sync_err;
4777 struct got_pathlist_head commitable_paths;
4778 struct collect_commitables_arg cc_arg;
4779 char *fileindex_path = NULL;
4780 struct got_reference *head_ref = NULL;
4781 struct got_object_id *head_commit_id = NULL;
4782 char *logmsg = NULL;
4784 TAILQ_INIT(&commitable_paths);
4785 *new_commit_id = NULL;
4787 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4789 err = get_fileindex_path(&fileindex_path, worktree);
4790 if (err)
4791 return err;
4793 cc_arg.commitable_paths = &commitable_paths;
4794 cc_arg.worktree = worktree;
4795 cc_arg.repo = repo;
4796 cc_arg.have_staged_files = 0;
4798 * If possible get the status of individual files directly to
4799 * avoid crawling the entire work tree once per rebased commit.
4800 * TODO: Ideally, merged_paths would contain a list of commitables
4801 * we could use so we could skip worktree_status() entirely.
4803 if (merged_paths) {
4804 struct got_pathlist_entry *pe;
4805 if (TAILQ_EMPTY(merged_paths)) {
4806 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4807 goto done;
4809 TAILQ_FOREACH(pe, merged_paths, entry) {
4810 err = worktree_status(worktree, pe->path, fileindex,
4811 repo, collect_commitables, &cc_arg, NULL, NULL);
4812 if (err)
4813 goto done;
4815 } else {
4816 err = worktree_status(worktree, "", fileindex, repo,
4817 collect_commitables, &cc_arg, NULL, NULL);
4818 if (err)
4819 goto done;
4822 if (TAILQ_EMPTY(&commitable_paths)) {
4823 /* No-op change; commit will be elided. */
4824 err = got_ref_delete(commit_ref, repo);
4825 if (err)
4826 goto done;
4827 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4828 goto done;
4831 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4832 if (err)
4833 goto done;
4835 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4836 if (err)
4837 goto done;
4839 if (new_logmsg) {
4840 logmsg = strdup(new_logmsg);
4841 if (logmsg == NULL) {
4842 err = got_error_from_errno("strdup");
4843 goto done;
4845 } else {
4846 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4847 if (err)
4848 goto done;
4851 /* NB: commit_worktree will call free(logmsg) */
4852 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4853 worktree, got_object_commit_get_author(orig_commit),
4854 got_object_commit_get_committer(orig_commit),
4855 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4856 if (err)
4857 goto done;
4859 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4860 if (err)
4861 goto done;
4863 err = got_ref_delete(commit_ref, repo);
4864 if (err)
4865 goto done;
4867 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4868 fileindex, 0);
4869 sync_err = sync_fileindex(fileindex, fileindex_path);
4870 if (sync_err && err == NULL)
4871 err = sync_err;
4872 done:
4873 free(fileindex_path);
4874 free(head_commit_id);
4875 if (head_ref)
4876 got_ref_close(head_ref);
4877 if (err) {
4878 free(*new_commit_id);
4879 *new_commit_id = NULL;
4881 return err;
4884 const struct got_error *
4885 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4886 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4887 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4888 struct got_commit_object *orig_commit,
4889 struct got_object_id *orig_commit_id, struct got_repository *repo)
4891 const struct got_error *err;
4892 char *commit_ref_name;
4893 struct got_reference *commit_ref = NULL;
4894 struct got_object_id *commit_id = NULL;
4896 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4897 if (err)
4898 return err;
4900 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4901 if (err)
4902 goto done;
4903 err = got_ref_resolve(&commit_id, repo, commit_ref);
4904 if (err)
4905 goto done;
4906 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4907 err = got_error(GOT_ERR_REBASE_COMMITID);
4908 goto done;
4911 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4912 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4913 done:
4914 if (commit_ref)
4915 got_ref_close(commit_ref);
4916 free(commit_ref_name);
4917 free(commit_id);
4918 return err;
4921 const struct got_error *
4922 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4923 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4924 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4925 struct got_commit_object *orig_commit,
4926 struct got_object_id *orig_commit_id, const char *new_logmsg,
4927 struct got_repository *repo)
4929 const struct got_error *err;
4930 char *commit_ref_name;
4931 struct got_reference *commit_ref = NULL;
4932 struct got_object_id *commit_id = NULL;
4934 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4935 if (err)
4936 return err;
4938 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4939 if (err)
4940 goto done;
4941 err = got_ref_resolve(&commit_id, repo, commit_ref);
4942 if (err)
4943 goto done;
4944 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4945 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4946 goto done;
4949 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4950 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4951 done:
4952 if (commit_ref)
4953 got_ref_close(commit_ref);
4954 free(commit_ref_name);
4955 free(commit_id);
4956 return err;
4959 const struct got_error *
4960 got_worktree_rebase_postpone(struct got_worktree *worktree,
4961 struct got_fileindex *fileindex)
4963 if (fileindex)
4964 got_fileindex_free(fileindex);
4965 return lock_worktree(worktree, LOCK_SH);
4968 static const struct got_error *
4969 delete_ref(const char *name, struct got_repository *repo)
4971 const struct got_error *err;
4972 struct got_reference *ref;
4974 err = got_ref_open(&ref, repo, name, 0);
4975 if (err) {
4976 if (err->code == GOT_ERR_NOT_REF)
4977 return NULL;
4978 return err;
4981 err = got_ref_delete(ref, repo);
4982 got_ref_close(ref);
4983 return err;
4986 static const struct got_error *
4987 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4989 const struct got_error *err;
4990 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4991 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4993 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4994 if (err)
4995 goto done;
4996 err = delete_ref(tmp_branch_name, repo);
4997 if (err)
4998 goto done;
5000 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5001 if (err)
5002 goto done;
5003 err = delete_ref(new_base_branch_ref_name, repo);
5004 if (err)
5005 goto done;
5007 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5008 if (err)
5009 goto done;
5010 err = delete_ref(branch_ref_name, repo);
5011 if (err)
5012 goto done;
5014 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5015 if (err)
5016 goto done;
5017 err = delete_ref(commit_ref_name, repo);
5018 if (err)
5019 goto done;
5021 done:
5022 free(tmp_branch_name);
5023 free(new_base_branch_ref_name);
5024 free(branch_ref_name);
5025 free(commit_ref_name);
5026 return err;
5029 const struct got_error *
5030 got_worktree_rebase_complete(struct got_worktree *worktree,
5031 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5032 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5033 struct got_repository *repo)
5035 const struct got_error *err, *unlockerr;
5036 struct got_object_id *new_head_commit_id = NULL;
5038 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5039 if (err)
5040 return err;
5042 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5043 if (err)
5044 goto done;
5046 err = got_ref_write(rebased_branch, repo);
5047 if (err)
5048 goto done;
5050 err = got_worktree_set_head_ref(worktree, rebased_branch);
5051 if (err)
5052 goto done;
5054 err = delete_rebase_refs(worktree, repo);
5055 done:
5056 if (fileindex)
5057 got_fileindex_free(fileindex);
5058 free(new_head_commit_id);
5059 unlockerr = lock_worktree(worktree, LOCK_SH);
5060 if (unlockerr && err == NULL)
5061 err = unlockerr;
5062 return err;
5065 const struct got_error *
5066 got_worktree_rebase_abort(struct got_worktree *worktree,
5067 struct got_fileindex *fileindex, struct got_repository *repo,
5068 struct got_reference *new_base_branch,
5069 got_worktree_checkout_cb progress_cb, void *progress_arg)
5071 const struct got_error *err, *unlockerr, *sync_err;
5072 struct got_reference *resolved = NULL;
5073 struct got_object_id *commit_id = NULL;
5074 char *fileindex_path = NULL;
5075 struct revert_file_args rfa;
5076 struct got_object_id *tree_id = NULL;
5078 err = lock_worktree(worktree, LOCK_EX);
5079 if (err)
5080 return err;
5082 err = got_ref_open(&resolved, repo,
5083 got_ref_get_symref_target(new_base_branch), 0);
5084 if (err)
5085 goto done;
5087 err = got_worktree_set_head_ref(worktree, resolved);
5088 if (err)
5089 goto done;
5092 * XXX commits to the base branch could have happened while
5093 * we were busy rebasing; should we store the original commit ID
5094 * when rebase begins and read it back here?
5096 err = got_ref_resolve(&commit_id, repo, resolved);
5097 if (err)
5098 goto done;
5100 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5101 if (err)
5102 goto done;
5104 err = got_object_id_by_path(&tree_id, repo,
5105 worktree->base_commit_id, worktree->path_prefix);
5106 if (err)
5107 goto done;
5109 err = delete_rebase_refs(worktree, repo);
5110 if (err)
5111 goto done;
5113 err = get_fileindex_path(&fileindex_path, worktree);
5114 if (err)
5115 goto done;
5117 rfa.worktree = worktree;
5118 rfa.fileindex = fileindex;
5119 rfa.progress_cb = progress_cb;
5120 rfa.progress_arg = progress_arg;
5121 rfa.patch_cb = NULL;
5122 rfa.patch_arg = NULL;
5123 rfa.repo = repo;
5124 err = worktree_status(worktree, "", fileindex, repo,
5125 revert_file, &rfa, NULL, NULL);
5126 if (err)
5127 goto sync;
5129 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5130 repo, progress_cb, progress_arg, NULL, NULL);
5131 sync:
5132 sync_err = sync_fileindex(fileindex, fileindex_path);
5133 if (sync_err && err == NULL)
5134 err = sync_err;
5135 done:
5136 got_ref_close(resolved);
5137 free(tree_id);
5138 free(commit_id);
5139 if (fileindex)
5140 got_fileindex_free(fileindex);
5141 free(fileindex_path);
5143 unlockerr = lock_worktree(worktree, LOCK_SH);
5144 if (unlockerr && err == NULL)
5145 err = unlockerr;
5146 return err;
5149 const struct got_error *
5150 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5151 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5152 struct got_fileindex **fileindex, struct got_worktree *worktree,
5153 struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 char *tmp_branch_name = NULL;
5157 char *branch_ref_name = NULL;
5158 char *base_commit_ref_name = NULL;
5159 char *fileindex_path = NULL;
5160 struct check_rebase_ok_arg ok_arg;
5161 struct got_reference *wt_branch = NULL;
5162 struct got_reference *base_commit_ref = NULL;
5164 *tmp_branch = NULL;
5165 *branch_ref = NULL;
5166 *base_commit_id = NULL;
5167 *fileindex = NULL;
5169 err = lock_worktree(worktree, LOCK_EX);
5170 if (err)
5171 return err;
5173 err = open_fileindex(fileindex, &fileindex_path, worktree);
5174 if (err)
5175 goto done;
5177 ok_arg.worktree = worktree;
5178 ok_arg.repo = repo;
5179 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5180 &ok_arg);
5181 if (err)
5182 goto done;
5184 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5185 if (err)
5186 goto done;
5188 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5189 if (err)
5190 goto done;
5192 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5193 worktree);
5194 if (err)
5195 goto done;
5197 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5198 0);
5199 if (err)
5200 goto done;
5202 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5203 if (err)
5204 goto done;
5206 err = got_ref_write(*branch_ref, repo);
5207 if (err)
5208 goto done;
5210 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5211 worktree->base_commit_id);
5212 if (err)
5213 goto done;
5214 err = got_ref_write(base_commit_ref, repo);
5215 if (err)
5216 goto done;
5217 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5218 if (*base_commit_id == NULL) {
5219 err = got_error_from_errno("got_object_id_dup");
5220 goto done;
5223 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5224 worktree->base_commit_id);
5225 if (err)
5226 goto done;
5227 err = got_ref_write(*tmp_branch, repo);
5228 if (err)
5229 goto done;
5231 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5232 if (err)
5233 goto done;
5234 done:
5235 free(fileindex_path);
5236 free(tmp_branch_name);
5237 free(branch_ref_name);
5238 free(base_commit_ref_name);
5239 if (wt_branch)
5240 got_ref_close(wt_branch);
5241 if (err) {
5242 if (*branch_ref) {
5243 got_ref_close(*branch_ref);
5244 *branch_ref = NULL;
5246 if (*tmp_branch) {
5247 got_ref_close(*tmp_branch);
5248 *tmp_branch = NULL;
5250 free(*base_commit_id);
5251 if (*fileindex) {
5252 got_fileindex_free(*fileindex);
5253 *fileindex = NULL;
5255 lock_worktree(worktree, LOCK_SH);
5257 return err;
5260 const struct got_error *
5261 got_worktree_histedit_postpone(struct got_worktree *worktree,
5262 struct got_fileindex *fileindex)
5264 if (fileindex)
5265 got_fileindex_free(fileindex);
5266 return lock_worktree(worktree, LOCK_SH);
5269 const struct got_error *
5270 got_worktree_histedit_in_progress(int *in_progress,
5271 struct got_worktree *worktree)
5273 const struct got_error *err;
5274 char *tmp_branch_name = NULL;
5276 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5277 if (err)
5278 return err;
5280 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5281 free(tmp_branch_name);
5282 return NULL;
5285 const struct got_error *
5286 got_worktree_histedit_continue(struct got_object_id **commit_id,
5287 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5288 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5289 struct got_worktree *worktree, struct got_repository *repo)
5291 const struct got_error *err;
5292 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5293 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5294 struct got_reference *commit_ref = NULL;
5295 struct got_reference *base_commit_ref = NULL;
5296 char *fileindex_path = NULL;
5297 int have_staged_files = 0;
5299 *commit_id = NULL;
5300 *tmp_branch = NULL;
5301 *base_commit_id = NULL;
5302 *fileindex = NULL;
5304 err = lock_worktree(worktree, LOCK_EX);
5305 if (err)
5306 return err;
5308 err = open_fileindex(fileindex, &fileindex_path, worktree);
5309 if (err)
5310 goto done;
5312 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5313 &have_staged_files);
5314 if (err && err->code != GOT_ERR_CANCELLED)
5315 goto done;
5316 if (have_staged_files) {
5317 err = got_error(GOT_ERR_STAGED_PATHS);
5318 goto done;
5321 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5322 if (err)
5323 goto done;
5325 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5326 if (err)
5327 goto done;
5329 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5330 if (err)
5331 goto done;
5333 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5334 worktree);
5335 if (err)
5336 goto done;
5338 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5339 if (err)
5340 goto done;
5342 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5343 if (err)
5344 goto done;
5345 err = got_ref_resolve(commit_id, repo, commit_ref);
5346 if (err)
5347 goto done;
5349 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5350 if (err)
5351 goto done;
5352 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5353 if (err)
5354 goto done;
5356 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5357 if (err)
5358 goto done;
5359 done:
5360 free(commit_ref_name);
5361 free(branch_ref_name);
5362 free(fileindex_path);
5363 if (commit_ref)
5364 got_ref_close(commit_ref);
5365 if (base_commit_ref)
5366 got_ref_close(base_commit_ref);
5367 if (err) {
5368 free(*commit_id);
5369 *commit_id = NULL;
5370 free(*base_commit_id);
5371 *base_commit_id = NULL;
5372 if (*tmp_branch) {
5373 got_ref_close(*tmp_branch);
5374 *tmp_branch = NULL;
5376 if (*fileindex) {
5377 got_fileindex_free(*fileindex);
5378 *fileindex = NULL;
5380 lock_worktree(worktree, LOCK_EX);
5382 return err;
5385 static const struct got_error *
5386 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5388 const struct got_error *err;
5389 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5390 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5392 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5393 if (err)
5394 goto done;
5395 err = delete_ref(tmp_branch_name, repo);
5396 if (err)
5397 goto done;
5399 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5400 worktree);
5401 if (err)
5402 goto done;
5403 err = delete_ref(base_commit_ref_name, repo);
5404 if (err)
5405 goto done;
5407 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5408 if (err)
5409 goto done;
5410 err = delete_ref(branch_ref_name, repo);
5411 if (err)
5412 goto done;
5414 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5415 if (err)
5416 goto done;
5417 err = delete_ref(commit_ref_name, repo);
5418 if (err)
5419 goto done;
5420 done:
5421 free(tmp_branch_name);
5422 free(base_commit_ref_name);
5423 free(branch_ref_name);
5424 free(commit_ref_name);
5425 return err;
5428 const struct got_error *
5429 got_worktree_histedit_abort(struct got_worktree *worktree,
5430 struct got_fileindex *fileindex, struct got_repository *repo,
5431 struct got_reference *branch, struct got_object_id *base_commit_id,
5432 got_worktree_checkout_cb progress_cb, void *progress_arg)
5434 const struct got_error *err, *unlockerr, *sync_err;
5435 struct got_reference *resolved = NULL;
5436 char *fileindex_path = NULL;
5437 struct got_object_id *tree_id = NULL;
5438 struct revert_file_args rfa;
5440 err = lock_worktree(worktree, LOCK_EX);
5441 if (err)
5442 return err;
5444 err = got_ref_open(&resolved, repo,
5445 got_ref_get_symref_target(branch), 0);
5446 if (err)
5447 goto done;
5449 err = got_worktree_set_head_ref(worktree, resolved);
5450 if (err)
5451 goto done;
5453 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5454 if (err)
5455 goto done;
5457 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5458 worktree->path_prefix);
5459 if (err)
5460 goto done;
5462 err = delete_histedit_refs(worktree, repo);
5463 if (err)
5464 goto done;
5466 err = get_fileindex_path(&fileindex_path, worktree);
5467 if (err)
5468 goto done;
5470 rfa.worktree = worktree;
5471 rfa.fileindex = fileindex;
5472 rfa.progress_cb = progress_cb;
5473 rfa.progress_arg = progress_arg;
5474 rfa.patch_cb = NULL;
5475 rfa.patch_arg = NULL;
5476 rfa.repo = repo;
5477 err = worktree_status(worktree, "", fileindex, repo,
5478 revert_file, &rfa, NULL, NULL);
5479 if (err)
5480 goto sync;
5482 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5483 repo, progress_cb, progress_arg, NULL, NULL);
5484 sync:
5485 sync_err = sync_fileindex(fileindex, fileindex_path);
5486 if (sync_err && err == NULL)
5487 err = sync_err;
5488 done:
5489 got_ref_close(resolved);
5490 free(tree_id);
5491 free(fileindex_path);
5493 unlockerr = lock_worktree(worktree, LOCK_SH);
5494 if (unlockerr && err == NULL)
5495 err = unlockerr;
5496 return err;
5499 const struct got_error *
5500 got_worktree_histedit_complete(struct got_worktree *worktree,
5501 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5502 struct got_reference *edited_branch, struct got_repository *repo)
5504 const struct got_error *err, *unlockerr;
5505 struct got_object_id *new_head_commit_id = NULL;
5506 struct got_reference *resolved = NULL;
5508 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5509 if (err)
5510 return err;
5512 err = got_ref_open(&resolved, repo,
5513 got_ref_get_symref_target(edited_branch), 0);
5514 if (err)
5515 goto done;
5517 err = got_ref_change_ref(resolved, new_head_commit_id);
5518 if (err)
5519 goto done;
5521 err = got_ref_write(resolved, repo);
5522 if (err)
5523 goto done;
5525 err = got_worktree_set_head_ref(worktree, resolved);
5526 if (err)
5527 goto done;
5529 err = delete_histedit_refs(worktree, repo);
5530 done:
5531 if (fileindex)
5532 got_fileindex_free(fileindex);
5533 free(new_head_commit_id);
5534 unlockerr = lock_worktree(worktree, LOCK_SH);
5535 if (unlockerr && err == NULL)
5536 err = unlockerr;
5537 return err;
5540 const struct got_error *
5541 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5542 struct got_object_id *commit_id, struct got_repository *repo)
5544 const struct got_error *err;
5545 char *commit_ref_name;
5547 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5548 if (err)
5549 return err;
5551 err = store_commit_id(commit_ref_name, commit_id, repo);
5552 if (err)
5553 goto done;
5555 err = delete_ref(commit_ref_name, repo);
5556 done:
5557 free(commit_ref_name);
5558 return err;
5561 struct check_stage_ok_arg {
5562 struct got_object_id *head_commit_id;
5563 struct got_worktree *worktree;
5564 struct got_fileindex *fileindex;
5565 struct got_repository *repo;
5566 int have_changes;
5569 const struct got_error *
5570 check_stage_ok(void *arg, unsigned char status,
5571 unsigned char staged_status, const char *relpath,
5572 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5573 struct got_object_id *commit_id)
5575 struct check_stage_ok_arg *a = arg;
5576 const struct got_error *err = NULL;
5577 struct got_fileindex_entry *ie;
5578 struct got_object_id base_commit_id;
5579 struct got_object_id *base_commit_idp = NULL;
5580 char *in_repo_path = NULL, *p;
5582 if (status == GOT_STATUS_UNVERSIONED)
5583 return NULL;
5584 if (status == GOT_STATUS_NONEXISTENT)
5585 return got_error_set_errno(ENOENT, relpath);
5587 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5588 if (ie == NULL)
5589 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5591 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5592 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5593 relpath) == -1)
5594 return got_error_from_errno("asprintf");
5596 if (got_fileindex_entry_has_commit(ie)) {
5597 memcpy(base_commit_id.sha1, ie->commit_sha1,
5598 SHA1_DIGEST_LENGTH);
5599 base_commit_idp = &base_commit_id;
5602 if (status == GOT_STATUS_NO_CHANGE) {
5603 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5604 goto done;
5605 } else if (status == GOT_STATUS_CONFLICT) {
5606 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5607 goto done;
5608 } else if (status != GOT_STATUS_ADD &&
5609 status != GOT_STATUS_MODIFY &&
5610 status != GOT_STATUS_DELETE) {
5611 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5612 goto done;
5615 a->have_changes = 1;
5617 p = in_repo_path;
5618 while (p[0] == '/')
5619 p++;
5620 err = check_out_of_date(p, status, staged_status,
5621 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5622 GOT_ERR_STAGE_OUT_OF_DATE);
5623 done:
5624 free(in_repo_path);
5625 return err;
5628 struct stage_path_arg {
5629 struct got_worktree *worktree;
5630 struct got_fileindex *fileindex;
5631 struct got_repository *repo;
5632 got_worktree_status_cb status_cb;
5633 void *status_arg;
5634 got_worktree_patch_cb patch_cb;
5635 void *patch_arg;
5638 static const struct got_error *
5639 stage_path(void *arg, unsigned char status,
5640 unsigned char staged_status, const char *relpath,
5641 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5642 struct got_object_id *commit_id)
5644 struct stage_path_arg *a = arg;
5645 const struct got_error *err = NULL;
5646 struct got_fileindex_entry *ie;
5647 char *ondisk_path = NULL, *path_content = NULL;
5648 uint32_t stage;
5649 struct got_object_id *new_staged_blob_id = NULL;
5651 if (status == GOT_STATUS_UNVERSIONED)
5652 return NULL;
5654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5655 if (ie == NULL)
5656 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5658 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5659 relpath)== -1)
5660 return got_error_from_errno("asprintf");
5662 switch (status) {
5663 case GOT_STATUS_ADD:
5664 case GOT_STATUS_MODIFY:
5665 if (a->patch_cb) {
5666 if (status == GOT_STATUS_ADD) {
5667 int choice = GOT_PATCH_CHOICE_NONE;
5668 err = (*a->patch_cb)(&choice, a->patch_arg,
5669 status, ie->path, NULL, 1, 1);
5670 if (err)
5671 break;
5672 if (choice != GOT_PATCH_CHOICE_YES)
5673 break;
5674 } else {
5675 err = create_patched_content(&path_content, 0,
5676 staged_blob_id ? staged_blob_id : blob_id,
5677 ondisk_path, ie->path, a->repo,
5678 a->patch_cb, a->patch_arg);
5679 if (err || path_content == NULL)
5680 break;
5683 err = got_object_blob_create(&new_staged_blob_id,
5684 path_content ? path_content : ondisk_path, a->repo);
5685 if (err)
5686 break;
5687 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5688 SHA1_DIGEST_LENGTH);
5689 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5690 stage = GOT_FILEIDX_STAGE_ADD;
5691 else
5692 stage = GOT_FILEIDX_STAGE_MODIFY;
5693 got_fileindex_entry_stage_set(ie, stage);
5694 if (a->status_cb == NULL)
5695 break;
5696 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5697 get_staged_status(ie), relpath, blob_id,
5698 new_staged_blob_id, NULL);
5699 break;
5700 case GOT_STATUS_DELETE:
5701 if (staged_status == GOT_STATUS_DELETE)
5702 break;
5703 if (a->patch_cb) {
5704 int choice = GOT_PATCH_CHOICE_NONE;
5705 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5706 ie->path, NULL, 1, 1);
5707 if (err)
5708 break;
5709 if (choice == GOT_PATCH_CHOICE_NO)
5710 break;
5711 if (choice != GOT_PATCH_CHOICE_YES) {
5712 err = got_error(GOT_ERR_PATCH_CHOICE);
5713 break;
5716 stage = GOT_FILEIDX_STAGE_DELETE;
5717 got_fileindex_entry_stage_set(ie, stage);
5718 if (a->status_cb == NULL)
5719 break;
5720 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5721 get_staged_status(ie), relpath, NULL, NULL, NULL);
5722 break;
5723 case GOT_STATUS_NO_CHANGE:
5724 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5725 break;
5726 case GOT_STATUS_CONFLICT:
5727 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5728 break;
5729 case GOT_STATUS_NONEXISTENT:
5730 err = got_error_set_errno(ENOENT, relpath);
5731 break;
5732 default:
5733 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5734 break;
5737 if (path_content && unlink(path_content) == -1 && err == NULL)
5738 err = got_error_from_errno2("unlink", path_content);
5739 free(path_content);
5740 free(ondisk_path);
5741 free(new_staged_blob_id);
5742 return err;
5745 const struct got_error *
5746 got_worktree_stage(struct got_worktree *worktree,
5747 struct got_pathlist_head *paths,
5748 got_worktree_status_cb status_cb, void *status_arg,
5749 got_worktree_patch_cb patch_cb, void *patch_arg,
5750 struct got_repository *repo)
5752 const struct got_error *err = NULL, *sync_err, *unlockerr;
5753 struct got_pathlist_entry *pe;
5754 struct got_fileindex *fileindex = NULL;
5755 char *fileindex_path = NULL;
5756 struct got_reference *head_ref = NULL;
5757 struct got_object_id *head_commit_id = NULL;
5758 struct check_stage_ok_arg oka;
5759 struct stage_path_arg spa;
5761 err = lock_worktree(worktree, LOCK_EX);
5762 if (err)
5763 return err;
5765 err = got_ref_open(&head_ref, repo,
5766 got_worktree_get_head_ref_name(worktree), 0);
5767 if (err)
5768 goto done;
5769 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5770 if (err)
5771 goto done;
5772 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5773 if (err)
5774 goto done;
5776 /* Check pre-conditions before staging anything. */
5777 oka.head_commit_id = head_commit_id;
5778 oka.worktree = worktree;
5779 oka.fileindex = fileindex;
5780 oka.repo = repo;
5781 oka.have_changes = 0;
5782 TAILQ_FOREACH(pe, paths, entry) {
5783 err = worktree_status(worktree, pe->path, fileindex, repo,
5784 check_stage_ok, &oka, NULL, NULL);
5785 if (err)
5786 goto done;
5788 if (!oka.have_changes) {
5789 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5790 goto done;
5793 spa.worktree = worktree;
5794 spa.fileindex = fileindex;
5795 spa.repo = repo;
5796 spa.patch_cb = patch_cb;
5797 spa.patch_arg = patch_arg;
5798 spa.status_cb = status_cb;
5799 spa.status_arg = status_arg;
5800 TAILQ_FOREACH(pe, paths, entry) {
5801 err = worktree_status(worktree, pe->path, fileindex, repo,
5802 stage_path, &spa, NULL, NULL);
5803 if (err)
5804 goto done;
5807 sync_err = sync_fileindex(fileindex, fileindex_path);
5808 if (sync_err && err == NULL)
5809 err = sync_err;
5810 done:
5811 if (head_ref)
5812 got_ref_close(head_ref);
5813 free(head_commit_id);
5814 free(fileindex_path);
5815 if (fileindex)
5816 got_fileindex_free(fileindex);
5817 unlockerr = lock_worktree(worktree, LOCK_SH);
5818 if (unlockerr && err == NULL)
5819 err = unlockerr;
5820 return err;
5823 struct unstage_path_arg {
5824 struct got_worktree *worktree;
5825 struct got_fileindex *fileindex;
5826 struct got_repository *repo;
5827 got_worktree_checkout_cb progress_cb;
5828 void *progress_arg;
5829 got_worktree_patch_cb patch_cb;
5830 void *patch_arg;
5833 static const struct got_error *
5834 create_unstaged_content(char **path_unstaged_content,
5835 char **path_new_staged_content, struct got_object_id *blob_id,
5836 struct got_object_id *staged_blob_id, const char *relpath,
5837 struct got_repository *repo,
5838 got_worktree_patch_cb patch_cb, void *patch_arg)
5840 const struct got_error *err;
5841 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5842 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5843 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5844 struct stat sb1, sb2;
5845 struct got_diff_changes *changes = NULL;
5846 struct got_diff_state *ds = NULL;
5847 struct got_diff_args *args = NULL;
5848 struct got_diff_change *change;
5849 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5850 int have_content = 0, have_rejected_content = 0;
5852 *path_unstaged_content = NULL;
5853 *path_new_staged_content = NULL;
5855 err = got_object_id_str(&label1, blob_id);
5856 if (err)
5857 return err;
5858 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5859 if (err)
5860 goto done;
5862 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5863 if (err)
5864 goto done;
5866 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5867 if (err)
5868 goto done;
5870 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5871 if (err)
5872 goto done;
5874 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5875 if (err)
5876 goto done;
5878 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5879 if (err)
5880 goto done;
5882 if (stat(path1, &sb1) == -1) {
5883 err = got_error_from_errno2("stat", path1);
5884 goto done;
5887 if (stat(path2, &sb2) == -1) {
5888 err = got_error_from_errno2("stat", path2);
5889 goto done;
5892 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5893 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5894 if (err)
5895 goto done;
5897 err = got_opentemp_named(path_unstaged_content, &outfile,
5898 "got-unstaged-content");
5899 if (err)
5900 goto done;
5901 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5902 "got-new-staged-content");
5903 if (err)
5904 goto done;
5906 if (fseek(f1, 0L, SEEK_SET) == -1) {
5907 err = got_ferror(f1, GOT_ERR_IO);
5908 goto done;
5910 if (fseek(f2, 0L, SEEK_SET) == -1) {
5911 err = got_ferror(f2, GOT_ERR_IO);
5912 goto done;
5914 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5915 int choice;
5916 err = apply_or_reject_change(&choice, change, ++n,
5917 changes->nchanges, ds, args, diff_flags, relpath,
5918 f1, f2, &line_cur1, &line_cur2,
5919 outfile, rejectfile, patch_cb, patch_arg);
5920 if (err)
5921 goto done;
5922 if (choice == GOT_PATCH_CHOICE_YES)
5923 have_content = 1;
5924 else
5925 have_rejected_content = 1;
5926 if (choice == GOT_PATCH_CHOICE_QUIT)
5927 break;
5929 if (have_content || have_rejected_content)
5930 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5931 outfile, rejectfile);
5932 done:
5933 free(label1);
5934 if (blob)
5935 got_object_blob_close(blob);
5936 if (staged_blob)
5937 got_object_blob_close(staged_blob);
5938 if (f1 && fclose(f1) == EOF && err == NULL)
5939 err = got_error_from_errno2("fclose", path1);
5940 if (f2 && fclose(f2) == EOF && err == NULL)
5941 err = got_error_from_errno2("fclose", path2);
5942 if (outfile && fclose(outfile) == EOF && err == NULL)
5943 err = got_error_from_errno2("fclose", *path_unstaged_content);
5944 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5945 err = got_error_from_errno2("fclose", *path_new_staged_content);
5946 if (path1 && unlink(path1) == -1 && err == NULL)
5947 err = got_error_from_errno2("unlink", path1);
5948 if (path2 && unlink(path2) == -1 && err == NULL)
5949 err = got_error_from_errno2("unlink", path2);
5950 if (err || !have_content) {
5951 if (*path_unstaged_content &&
5952 unlink(*path_unstaged_content) == -1 && err == NULL)
5953 err = got_error_from_errno2("unlink",
5954 *path_unstaged_content);
5955 free(*path_unstaged_content);
5956 *path_unstaged_content = NULL;
5958 if (err || !have_rejected_content) {
5959 if (*path_new_staged_content &&
5960 unlink(*path_new_staged_content) == -1 && err == NULL)
5961 err = got_error_from_errno2("unlink",
5962 *path_new_staged_content);
5963 free(*path_new_staged_content);
5964 *path_new_staged_content = NULL;
5966 free(args);
5967 if (ds) {
5968 got_diff_state_free(ds);
5969 free(ds);
5971 if (changes)
5972 got_diff_free_changes(changes);
5973 free(path1);
5974 free(path2);
5975 return err;
5978 static const struct got_error *
5979 unstage_path(void *arg, unsigned char status,
5980 unsigned char staged_status, const char *relpath,
5981 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5982 struct got_object_id *commit_id)
5984 const struct got_error *err = NULL;
5985 struct unstage_path_arg *a = arg;
5986 struct got_fileindex_entry *ie;
5987 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5988 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5989 char *path_new_staged_content = NULL;
5990 int local_changes_subsumed;
5991 struct stat sb;
5993 if (staged_status != GOT_STATUS_ADD &&
5994 staged_status != GOT_STATUS_MODIFY &&
5995 staged_status != GOT_STATUS_DELETE)
5996 return NULL;
5998 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5999 if (ie == NULL)
6000 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6003 == -1)
6004 return got_error_from_errno("asprintf");
6006 switch (staged_status) {
6007 case GOT_STATUS_MODIFY:
6008 err = got_object_open_as_blob(&blob_base, a->repo,
6009 blob_id, 8192);
6010 if (err)
6011 break;
6012 /* fall through */
6013 case GOT_STATUS_ADD:
6014 if (a->patch_cb) {
6015 if (staged_status == GOT_STATUS_ADD) {
6016 int choice = GOT_PATCH_CHOICE_NONE;
6017 err = (*a->patch_cb)(&choice, a->patch_arg,
6018 staged_status, ie->path, NULL, 1, 1);
6019 if (err)
6020 break;
6021 if (choice != GOT_PATCH_CHOICE_YES)
6022 break;
6023 } else {
6024 err = create_unstaged_content(
6025 &path_unstaged_content,
6026 &path_new_staged_content, blob_id,
6027 staged_blob_id, ie->path, a->repo,
6028 a->patch_cb, a->patch_arg);
6029 if (err || path_unstaged_content == NULL)
6030 break;
6031 if (path_new_staged_content) {
6032 err = got_object_blob_create(
6033 &staged_blob_id,
6034 path_new_staged_content,
6035 a->repo);
6036 if (err)
6037 break;
6038 memcpy(ie->staged_blob_sha1,
6039 staged_blob_id->sha1,
6040 SHA1_DIGEST_LENGTH);
6042 err = merge_file(&local_changes_subsumed,
6043 a->worktree, blob_base, ondisk_path,
6044 relpath, got_fileindex_perms_to_st(ie),
6045 path_unstaged_content, "unstaged",
6046 a->repo, a->progress_cb, a->progress_arg);
6047 if (err == NULL &&
6048 path_new_staged_content == NULL)
6049 got_fileindex_entry_stage_set(ie,
6050 GOT_FILEIDX_STAGE_NONE);
6051 break; /* Done with this file. */
6054 err = got_object_open_as_blob(&blob_staged, a->repo,
6055 staged_blob_id, 8192);
6056 if (err)
6057 break;
6058 err = merge_blob(&local_changes_subsumed, a->worktree,
6059 blob_base, ondisk_path, relpath,
6060 got_fileindex_perms_to_st(ie), blob_staged,
6061 commit_id ? commit_id : a->worktree->base_commit_id,
6062 a->repo, a->progress_cb, a->progress_arg);
6063 if (err == NULL)
6064 got_fileindex_entry_stage_set(ie,
6065 GOT_FILEIDX_STAGE_NONE);
6066 break;
6067 case GOT_STATUS_DELETE:
6068 if (a->patch_cb) {
6069 int choice = GOT_PATCH_CHOICE_NONE;
6070 err = (*a->patch_cb)(&choice, a->patch_arg,
6071 staged_status, ie->path, NULL, 1, 1);
6072 if (err)
6073 break;
6074 if (choice == GOT_PATCH_CHOICE_NO)
6075 break;
6076 if (choice != GOT_PATCH_CHOICE_YES) {
6077 err = got_error(GOT_ERR_PATCH_CHOICE);
6078 break;
6081 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6082 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6083 if (err)
6084 break;
6085 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6086 break;
6089 free(ondisk_path);
6090 if (path_unstaged_content &&
6091 unlink(path_unstaged_content) == -1 && err == NULL)
6092 err = got_error_from_errno2("unlink", path_unstaged_content);
6093 if (path_new_staged_content &&
6094 unlink(path_new_staged_content) == -1 && err == NULL)
6095 err = got_error_from_errno2("unlink", path_new_staged_content);
6096 free(path_unstaged_content);
6097 free(path_new_staged_content);
6098 if (blob_base)
6099 got_object_blob_close(blob_base);
6100 if (blob_staged)
6101 got_object_blob_close(blob_staged);
6102 return err;
6105 const struct got_error *
6106 got_worktree_unstage(struct got_worktree *worktree,
6107 struct got_pathlist_head *paths,
6108 got_worktree_checkout_cb progress_cb, void *progress_arg,
6109 got_worktree_patch_cb patch_cb, void *patch_arg,
6110 struct got_repository *repo)
6112 const struct got_error *err = NULL, *sync_err, *unlockerr;
6113 struct got_pathlist_entry *pe;
6114 struct got_fileindex *fileindex = NULL;
6115 char *fileindex_path = NULL;
6116 struct unstage_path_arg upa;
6118 err = lock_worktree(worktree, LOCK_EX);
6119 if (err)
6120 return err;
6122 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6123 if (err)
6124 goto done;
6126 upa.worktree = worktree;
6127 upa.fileindex = fileindex;
6128 upa.repo = repo;
6129 upa.progress_cb = progress_cb;
6130 upa.progress_arg = progress_arg;
6131 upa.patch_cb = patch_cb;
6132 upa.patch_arg = patch_arg;
6133 TAILQ_FOREACH(pe, paths, entry) {
6134 err = worktree_status(worktree, pe->path, fileindex, repo,
6135 unstage_path, &upa, NULL, NULL);
6136 if (err)
6137 goto done;
6140 sync_err = sync_fileindex(fileindex, fileindex_path);
6141 if (sync_err && err == NULL)
6142 err = sync_err;
6143 done:
6144 free(fileindex_path);
6145 if (fileindex)
6146 got_fileindex_free(fileindex);
6147 unlockerr = lock_worktree(worktree, LOCK_SH);
6148 if (unlockerr && err == NULL)
6149 err = unlockerr;
6150 return err;