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);
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);
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);
401 goto done;
404 err = got_repo_open(&repo, (*worktree)->repo_path);
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->root_path);
459 free(worktree->repo_path);
460 free(worktree->path_prefix);
461 free(worktree->base_commit_id);
462 free(worktree->head_ref_name);
463 if (worktree->lockfd != -1)
464 if (close(worktree->lockfd) != 0)
465 err = got_error_from_errno2("close",
466 got_worktree_get_root_path(worktree));
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 (asprintf(&path, "%s%s%s", parent_path,
1430 parent_path[0] ? "/" : "", te->name)
1431 == -1)
1432 return got_error_from_errno("asprintf");
1434 if (S_ISDIR(te->mode))
1435 err = add_dir_on_disk(a->worktree, path);
1436 else
1437 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1438 a->repo, a->progress_cb, a->progress_arg);
1440 free(path);
1441 return err;
1444 static const struct got_error *
1445 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1447 const struct got_error *err = NULL;
1448 char *uuidstr = NULL;
1449 uint32_t uuid_status;
1451 *refname = NULL;
1453 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1454 if (uuid_status != uuid_s_ok)
1455 return got_error_uuid(uuid_status);
1457 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1458 == -1) {
1459 err = got_error_from_errno("asprintf");
1460 *refname = NULL;
1462 free(uuidstr);
1463 return err;
1466 const struct got_error *
1467 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1469 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1472 static const struct got_error *
1473 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1475 return get_ref_name(refname, worktree,
1476 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1479 static const struct got_error *
1480 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1482 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1485 static const struct got_error *
1486 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1488 return get_ref_name(refname, worktree,
1489 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1492 static const struct got_error *
1493 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1495 return get_ref_name(refname, worktree,
1496 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1499 static const struct got_error *
1500 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1502 return get_ref_name(refname, worktree,
1503 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1506 static const struct got_error *
1507 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1509 return get_ref_name(refname, worktree,
1510 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1513 static const struct got_error *
1514 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1516 return get_ref_name(refname, worktree,
1517 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1520 static const struct got_error *
1521 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1523 return get_ref_name(refname, worktree,
1524 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1527 const struct got_error *
1528 got_worktree_get_histedit_script_path(char **path,
1529 struct got_worktree *worktree)
1531 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1532 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1533 *path = NULL;
1534 return got_error_from_errno("asprintf");
1536 return NULL;
1540 * Prevent Git's garbage collector from deleting our base commit by
1541 * setting a reference to our base commit's ID.
1543 static const struct got_error *
1544 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1546 const struct got_error *err = NULL;
1547 struct got_reference *ref = NULL;
1548 char *refname;
1550 err = got_worktree_get_base_ref_name(&refname, worktree);
1551 if (err)
1552 return err;
1554 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1555 if (err)
1556 goto done;
1558 err = got_ref_write(ref, repo);
1559 done:
1560 free(refname);
1561 if (ref)
1562 got_ref_close(ref);
1563 return err;
1566 static const struct got_error *
1567 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1569 const struct got_error *err = NULL;
1571 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1572 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1573 err = got_error_from_errno("asprintf");
1574 *fileindex_path = NULL;
1576 return err;
1580 static const struct got_error *
1581 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1582 struct got_worktree *worktree)
1584 const struct got_error *err = NULL;
1585 FILE *index = NULL;
1587 *fileindex_path = NULL;
1588 *fileindex = got_fileindex_alloc();
1589 if (*fileindex == NULL)
1590 return got_error_from_errno("got_fileindex_alloc");
1592 err = get_fileindex_path(fileindex_path, worktree);
1593 if (err)
1594 goto done;
1596 index = fopen(*fileindex_path, "rb");
1597 if (index == NULL) {
1598 if (errno != ENOENT)
1599 err = got_error_from_errno2("fopen", *fileindex_path);
1600 } else {
1601 err = got_fileindex_read(*fileindex, index);
1602 if (fclose(index) != 0 && err == NULL)
1603 err = got_error_from_errno("fclose");
1605 done:
1606 if (err) {
1607 free(*fileindex_path);
1608 *fileindex_path = NULL;
1609 got_fileindex_free(*fileindex);
1610 *fileindex = NULL;
1612 return err;
1615 struct bump_base_commit_id_arg {
1616 struct got_object_id *base_commit_id;
1617 const char *path;
1618 size_t path_len;
1619 const char *entry_name;
1620 got_worktree_checkout_cb progress_cb;
1621 void *progress_arg;
1624 /* Bump base commit ID of all files within an updated part of the work tree. */
1625 static const struct got_error *
1626 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1628 const struct got_error *err;
1629 struct bump_base_commit_id_arg *a = arg;
1631 if (a->entry_name) {
1632 if (strcmp(ie->path, a->path) != 0)
1633 return NULL;
1634 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1635 return NULL;
1637 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1638 SHA1_DIGEST_LENGTH) == 0)
1639 return NULL;
1641 if (a->progress_cb) {
1642 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1643 ie->path);
1644 if (err)
1645 return err;
1647 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1648 return NULL;
1651 static const struct got_error *
1652 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1654 const struct got_error *err = NULL;
1655 char *new_fileindex_path = NULL;
1656 FILE *new_index = NULL;
1658 err = got_opentemp_named(&new_fileindex_path, &new_index,
1659 fileindex_path);
1660 if (err)
1661 goto done;
1663 err = got_fileindex_write(fileindex, new_index);
1664 if (err)
1665 goto done;
1667 if (rename(new_fileindex_path, fileindex_path) != 0) {
1668 err = got_error_from_errno3("rename", new_fileindex_path,
1669 fileindex_path);
1670 unlink(new_fileindex_path);
1672 done:
1673 if (new_index)
1674 fclose(new_index);
1675 free(new_fileindex_path);
1676 return err;
1679 static const struct got_error *
1680 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1681 struct got_object_id **tree_id, const char *wt_relpath,
1682 struct got_worktree *worktree, struct got_repository *repo)
1684 const struct got_error *err = NULL;
1685 struct got_object_id *id = NULL;
1686 char *in_repo_path = NULL;
1687 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1689 *entry_type = GOT_OBJ_TYPE_ANY;
1690 *tree_relpath = NULL;
1691 *tree_id = NULL;
1693 if (wt_relpath[0] == '\0') {
1694 /* Check out all files within the work tree. */
1695 *entry_type = GOT_OBJ_TYPE_TREE;
1696 *tree_relpath = strdup("");
1697 if (*tree_relpath == NULL) {
1698 err = got_error_from_errno("strdup");
1699 goto done;
1701 err = got_object_id_by_path(tree_id, repo,
1702 worktree->base_commit_id, worktree->path_prefix);
1703 if (err)
1704 goto done;
1705 return NULL;
1708 /* Check out a subset of files in the work tree. */
1710 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1711 is_root_wt ? "" : "/", wt_relpath) == -1) {
1712 err = got_error_from_errno("asprintf");
1713 goto done;
1716 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1717 in_repo_path);
1718 if (err)
1719 goto done;
1721 free(in_repo_path);
1722 in_repo_path = NULL;
1724 err = got_object_get_type(entry_type, repo, id);
1725 if (err)
1726 goto done;
1728 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1729 /* Check out a single file. */
1730 if (strchr(wt_relpath, '/') == NULL) {
1731 /* Check out a single file in work tree's root dir. */
1732 in_repo_path = strdup(worktree->path_prefix);
1733 if (in_repo_path == NULL) {
1734 err = got_error_from_errno("strdup");
1735 goto done;
1737 *tree_relpath = strdup("");
1738 if (*tree_relpath == NULL) {
1739 err = got_error_from_errno("strdup");
1740 goto done;
1742 } else {
1743 /* Check out a single file in a subdirectory. */
1744 err = got_path_dirname(tree_relpath, wt_relpath);
1745 if (err)
1746 return err;
1747 if (asprintf(&in_repo_path, "%s%s%s",
1748 worktree->path_prefix, is_root_wt ? "" : "/",
1749 *tree_relpath) == -1) {
1750 err = got_error_from_errno("asprintf");
1751 goto done;
1754 err = got_object_id_by_path(tree_id, repo,
1755 worktree->base_commit_id, in_repo_path);
1756 } else {
1757 /* Check out all files within a subdirectory. */
1758 *tree_id = got_object_id_dup(id);
1759 if (*tree_id == NULL) {
1760 err = got_error_from_errno("got_object_id_dup");
1761 goto done;
1763 *tree_relpath = strdup(wt_relpath);
1764 if (*tree_relpath == NULL) {
1765 err = got_error_from_errno("strdup");
1766 goto done;
1769 done:
1770 free(id);
1771 free(in_repo_path);
1772 if (err) {
1773 *entry_type = GOT_OBJ_TYPE_ANY;
1774 free(*tree_relpath);
1775 *tree_relpath = NULL;
1776 free(*tree_id);
1777 *tree_id = NULL;
1779 return err;
1782 static const struct got_error *
1783 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1784 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1785 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1786 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1788 const struct got_error *err = NULL;
1789 struct got_commit_object *commit = NULL;
1790 struct got_tree_object *tree = NULL;
1791 struct got_fileindex_diff_tree_cb diff_cb;
1792 struct diff_cb_arg arg;
1794 err = ref_base_commit(worktree, repo);
1795 if (err)
1796 goto done;
1798 err = got_object_open_as_commit(&commit, repo,
1799 worktree->base_commit_id);
1800 if (err)
1801 goto done;
1803 err = got_object_open_as_tree(&tree, repo, tree_id);
1804 if (err)
1805 goto done;
1807 if (entry_name &&
1808 got_object_tree_find_entry(tree, entry_name) == NULL) {
1809 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1810 goto done;
1813 diff_cb.diff_old_new = diff_old_new;
1814 diff_cb.diff_old = diff_old;
1815 diff_cb.diff_new = diff_new;
1816 arg.fileindex = fileindex;
1817 arg.worktree = worktree;
1818 arg.repo = repo;
1819 arg.progress_cb = progress_cb;
1820 arg.progress_arg = progress_arg;
1821 arg.cancel_cb = cancel_cb;
1822 arg.cancel_arg = cancel_arg;
1823 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1824 entry_name, repo, &diff_cb, &arg);
1825 done:
1826 if (tree)
1827 got_object_tree_close(tree);
1828 if (commit)
1829 got_object_commit_close(commit);
1830 return err;
1833 const struct got_error *
1834 got_worktree_checkout_files(struct got_worktree *worktree,
1835 struct got_pathlist_head *paths, struct got_repository *repo,
1836 got_worktree_checkout_cb progress_cb, void *progress_arg,
1837 got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL, *sync_err, *unlockerr;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex *fileindex = NULL;
1843 char *fileindex_path = NULL;
1844 struct got_pathlist_entry *pe;
1845 struct tree_path_data {
1846 SIMPLEQ_ENTRY(tree_path_data) entry;
1847 struct got_object_id *tree_id;
1848 int entry_type;
1849 char *relpath;
1850 char *entry_name;
1851 } *tpd = NULL;
1852 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1854 SIMPLEQ_INIT(&tree_paths);
1856 err = lock_worktree(worktree, LOCK_EX);
1857 if (err)
1858 return err;
1860 /* Map all specified paths to in-repository trees. */
1861 TAILQ_FOREACH(pe, paths, entry) {
1862 tpd = malloc(sizeof(*tpd));
1863 if (tpd == NULL) {
1864 err = got_error_from_errno("malloc");
1865 goto done;
1868 err = find_tree_entry_for_checkout(&tpd->entry_type,
1869 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1870 if (err) {
1871 free(tpd);
1872 goto done;
1875 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1876 err = got_path_basename(&tpd->entry_name, pe->path);
1877 if (err) {
1878 free(tpd->relpath);
1879 free(tpd->tree_id);
1880 free(tpd);
1881 goto done;
1883 } else
1884 tpd->entry_name = NULL;
1886 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1890 * Read the file index.
1891 * Checking out files is supposed to be an idempotent operation.
1892 * If the on-disk file index is incomplete we will try to complete it.
1894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1895 if (err)
1896 goto done;
1898 tpd = SIMPLEQ_FIRST(&tree_paths);
1899 TAILQ_FOREACH(pe, paths, entry) {
1900 struct bump_base_commit_id_arg bbc_arg;
1902 err = checkout_files(worktree, fileindex, tpd->relpath,
1903 tpd->tree_id, tpd->entry_name, repo,
1904 progress_cb, progress_arg, cancel_cb, cancel_arg);
1905 if (err)
1906 break;
1908 bbc_arg.base_commit_id = worktree->base_commit_id;
1909 bbc_arg.entry_name = tpd->entry_name;
1910 bbc_arg.path = pe->path;
1911 bbc_arg.path_len = pe->path_len;
1912 bbc_arg.progress_cb = progress_cb;
1913 bbc_arg.progress_arg = progress_arg;
1914 err = got_fileindex_for_each_entry_safe(fileindex,
1915 bump_base_commit_id, &bbc_arg);
1916 if (err)
1917 break;
1919 tpd = SIMPLEQ_NEXT(tpd, entry);
1921 sync_err = sync_fileindex(fileindex, fileindex_path);
1922 if (sync_err && err == NULL)
1923 err = sync_err;
1924 done:
1925 free(fileindex_path);
1926 if (tree)
1927 got_object_tree_close(tree);
1928 if (commit)
1929 got_object_commit_close(commit);
1930 if (fileindex)
1931 got_fileindex_free(fileindex);
1932 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1933 tpd = SIMPLEQ_FIRST(&tree_paths);
1934 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1935 free(tpd->relpath);
1936 free(tpd->tree_id);
1937 free(tpd);
1939 unlockerr = lock_worktree(worktree, LOCK_SH);
1940 if (unlockerr && err == NULL)
1941 err = unlockerr;
1942 return err;
1945 struct merge_file_cb_arg {
1946 struct got_worktree *worktree;
1947 struct got_fileindex *fileindex;
1948 got_worktree_checkout_cb progress_cb;
1949 void *progress_arg;
1950 got_cancel_cb cancel_cb;
1951 void *cancel_arg;
1952 struct got_object_id *commit_id2;
1955 static const struct got_error *
1956 merge_file_cb(void *arg, struct got_blob_object *blob1,
1957 struct got_blob_object *blob2, struct got_object_id *id1,
1958 struct got_object_id *id2, const char *path1, const char *path2,
1959 struct got_repository *repo)
1961 static const struct got_error *err = NULL;
1962 struct merge_file_cb_arg *a = arg;
1963 struct got_fileindex_entry *ie;
1964 char *ondisk_path = NULL;
1965 struct stat sb;
1966 unsigned char status;
1967 int local_changes_subsumed;
1969 if (blob1 && blob2) {
1970 ie = got_fileindex_entry_get(a->fileindex, path2,
1971 strlen(path2));
1972 if (ie == NULL)
1973 return (*a->progress_cb)(a->progress_arg,
1974 GOT_STATUS_MISSING, path2);
1976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1977 path2) == -1)
1978 return got_error_from_errno("asprintf");
1980 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1981 if (err)
1982 goto done;
1984 if (status == GOT_STATUS_DELETE) {
1985 err = (*a->progress_cb)(a->progress_arg,
1986 GOT_STATUS_MERGE, path2);
1987 goto done;
1989 if (status != GOT_STATUS_NO_CHANGE &&
1990 status != GOT_STATUS_MODIFY &&
1991 status != GOT_STATUS_CONFLICT &&
1992 status != GOT_STATUS_ADD) {
1993 err = (*a->progress_cb)(a->progress_arg, status, path2);
1994 goto done;
1997 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1998 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1999 a->progress_cb, a->progress_arg);
2000 } else if (blob1) {
2001 ie = got_fileindex_entry_get(a->fileindex, path1,
2002 strlen(path1));
2003 if (ie == NULL)
2004 return (*a->progress_cb)(a->progress_arg,
2005 GOT_STATUS_MISSING, path2);
2007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2008 path1) == -1)
2009 return got_error_from_errno("asprintf");
2011 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2012 if (err)
2013 goto done;
2015 switch (status) {
2016 case GOT_STATUS_NO_CHANGE:
2017 err = (*a->progress_cb)(a->progress_arg,
2018 GOT_STATUS_DELETE, path1);
2019 if (err)
2020 goto done;
2021 err = remove_ondisk_file(a->worktree->root_path, path1);
2022 if (err)
2023 goto done;
2024 if (ie)
2025 got_fileindex_entry_mark_deleted_from_disk(ie);
2026 break;
2027 case GOT_STATUS_DELETE:
2028 case GOT_STATUS_MISSING:
2029 err = (*a->progress_cb)(a->progress_arg,
2030 GOT_STATUS_DELETE, path1);
2031 if (err)
2032 goto done;
2033 if (ie)
2034 got_fileindex_entry_mark_deleted_from_disk(ie);
2035 break;
2036 case GOT_STATUS_ADD:
2037 case GOT_STATUS_MODIFY:
2038 case GOT_STATUS_CONFLICT:
2039 err = (*a->progress_cb)(a->progress_arg,
2040 GOT_STATUS_CANNOT_DELETE, path1);
2041 if (err)
2042 goto done;
2043 break;
2044 case GOT_STATUS_OBSTRUCTED:
2045 err = (*a->progress_cb)(a->progress_arg, status, path1);
2046 if (err)
2047 goto done;
2048 break;
2049 default:
2050 break;
2052 } else if (blob2) {
2053 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2054 path2) == -1)
2055 return got_error_from_errno("asprintf");
2056 ie = got_fileindex_entry_get(a->fileindex, path2,
2057 strlen(path2));
2058 if (ie) {
2059 err = get_file_status(&status, &sb, ie, ondisk_path,
2060 repo);
2061 if (err)
2062 goto done;
2063 if (status != GOT_STATUS_NO_CHANGE &&
2064 status != GOT_STATUS_MODIFY &&
2065 status != GOT_STATUS_CONFLICT &&
2066 status != GOT_STATUS_ADD) {
2067 err = (*a->progress_cb)(a->progress_arg,
2068 status, path2);
2069 goto done;
2071 err = merge_blob(&local_changes_subsumed, a->worktree,
2072 NULL, ondisk_path, path2, sb.st_mode, blob2,
2073 a->commit_id2, repo,
2074 a->progress_cb, a->progress_arg);
2075 if (status == GOT_STATUS_DELETE) {
2076 err = update_blob_fileindex_entry(a->worktree,
2077 a->fileindex, ie, ondisk_path, ie->path,
2078 blob2, 0);
2079 if (err)
2080 goto done;
2082 } else {
2083 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2084 err = install_blob(a->worktree, ondisk_path, path2,
2085 /* XXX get this from parent tree! */
2086 GOT_DEFAULT_FILE_MODE,
2087 sb.st_mode, blob2, 0, 0, repo,
2088 a->progress_cb, a->progress_arg);
2089 if (err)
2090 goto done;
2091 err = got_fileindex_entry_alloc(&ie,
2092 ondisk_path, path2, NULL, NULL);
2093 if (err)
2094 goto done;
2095 err = got_fileindex_entry_add(a->fileindex, ie);
2096 if (err) {
2097 got_fileindex_entry_free(ie);
2098 goto done;
2102 done:
2103 free(ondisk_path);
2104 return err;
2107 struct check_merge_ok_arg {
2108 struct got_worktree *worktree;
2109 struct got_repository *repo;
2112 static const struct got_error *
2113 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2115 const struct got_error *err = NULL;
2116 struct check_merge_ok_arg *a = arg;
2117 unsigned char status;
2118 struct stat sb;
2119 char *ondisk_path;
2121 /* Reject merges into a work tree with mixed base commits. */
2122 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2123 SHA1_DIGEST_LENGTH))
2124 return got_error(GOT_ERR_MIXED_COMMITS);
2126 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2127 == -1)
2128 return got_error_from_errno("asprintf");
2130 /* Reject merges into a work tree with conflicted files. */
2131 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2132 if (err)
2133 return err;
2134 if (status == GOT_STATUS_CONFLICT)
2135 return got_error(GOT_ERR_CONFLICTS);
2137 return NULL;
2140 static const struct got_error *
2141 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2142 const char *fileindex_path, struct got_object_id *commit_id1,
2143 struct got_object_id *commit_id2, struct got_repository *repo,
2144 got_worktree_checkout_cb progress_cb, void *progress_arg,
2145 got_cancel_cb cancel_cb, void *cancel_arg)
2147 const struct got_error *err = NULL, *sync_err;
2148 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2149 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2150 struct merge_file_cb_arg arg;
2152 if (commit_id1) {
2153 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2154 worktree->path_prefix);
2155 if (err)
2156 goto done;
2158 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2159 if (err)
2160 goto done;
2163 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2164 worktree->path_prefix);
2165 if (err)
2166 goto done;
2168 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2169 if (err)
2170 goto done;
2172 arg.worktree = worktree;
2173 arg.fileindex = fileindex;
2174 arg.progress_cb = progress_cb;
2175 arg.progress_arg = progress_arg;
2176 arg.cancel_cb = cancel_cb;
2177 arg.cancel_arg = cancel_arg;
2178 arg.commit_id2 = commit_id2;
2179 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2180 sync_err = sync_fileindex(fileindex, fileindex_path);
2181 if (sync_err && err == NULL)
2182 err = sync_err;
2183 done:
2184 if (tree1)
2185 got_object_tree_close(tree1);
2186 if (tree2)
2187 got_object_tree_close(tree2);
2188 return err;
2191 const struct got_error *
2192 got_worktree_merge_files(struct got_worktree *worktree,
2193 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2194 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2195 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2197 const struct got_error *err, *unlockerr;
2198 char *fileindex_path = NULL;
2199 struct got_fileindex *fileindex = NULL;
2200 struct check_merge_ok_arg mok_arg;
2202 err = lock_worktree(worktree, LOCK_EX);
2203 if (err)
2204 return err;
2206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2207 if (err)
2208 goto done;
2210 mok_arg.worktree = worktree;
2211 mok_arg.repo = repo;
2212 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2213 &mok_arg);
2214 if (err)
2215 goto done;
2217 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2218 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2219 done:
2220 if (fileindex)
2221 got_fileindex_free(fileindex);
2222 free(fileindex_path);
2223 unlockerr = lock_worktree(worktree, LOCK_SH);
2224 if (unlockerr && err == NULL)
2225 err = unlockerr;
2226 return err;
2229 struct diff_dir_cb_arg {
2230 struct got_fileindex *fileindex;
2231 struct got_worktree *worktree;
2232 const char *status_path;
2233 size_t status_path_len;
2234 struct got_repository *repo;
2235 got_worktree_status_cb status_cb;
2236 void *status_arg;
2237 got_cancel_cb cancel_cb;
2238 void *cancel_arg;
2239 /* A pathlist containing per-directory pathlists of ignore patterns. */
2240 struct got_pathlist_head ignores;
2243 static const struct got_error *
2244 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2245 got_worktree_status_cb status_cb, void *status_arg,
2246 struct got_repository *repo)
2248 const struct got_error *err = NULL;
2249 unsigned char status = GOT_STATUS_NO_CHANGE;
2250 unsigned char staged_status = get_staged_status(ie);
2251 struct stat sb;
2252 struct got_object_id blob_id, commit_id, staged_blob_id;
2253 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2254 struct got_object_id *staged_blob_idp = NULL;
2256 err = get_file_status(&status, &sb, ie, abspath, repo);
2257 if (err)
2258 return err;
2260 if (status == GOT_STATUS_NO_CHANGE &&
2261 staged_status == GOT_STATUS_NO_CHANGE)
2262 return NULL;
2264 if (got_fileindex_entry_has_blob(ie)) {
2265 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2266 blob_idp = &blob_id;
2268 if (got_fileindex_entry_has_commit(ie)) {
2269 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2270 commit_idp = &commit_id;
2272 if (staged_status == GOT_STATUS_ADD ||
2273 staged_status == GOT_STATUS_MODIFY) {
2274 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2275 SHA1_DIGEST_LENGTH);
2276 staged_blob_idp = &staged_blob_id;
2279 return (*status_cb)(status_arg, status, staged_status,
2280 ie->path, blob_idp, staged_blob_idp, commit_idp);
2283 static const struct got_error *
2284 status_old_new(void *arg, struct got_fileindex_entry *ie,
2285 struct dirent *de, const char *parent_path)
2287 const struct got_error *err = NULL;
2288 struct diff_dir_cb_arg *a = arg;
2289 char *abspath;
2291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2292 return got_error(GOT_ERR_CANCELLED);
2294 if (got_path_cmp(parent_path, a->status_path,
2295 strlen(parent_path), a->status_path_len) != 0 &&
2296 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2297 return NULL;
2299 if (parent_path[0]) {
2300 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2301 parent_path, de->d_name) == -1)
2302 return got_error_from_errno("asprintf");
2303 } else {
2304 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2305 de->d_name) == -1)
2306 return got_error_from_errno("asprintf");
2309 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2310 a->repo);
2311 free(abspath);
2312 return err;
2315 static const struct got_error *
2316 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2318 struct diff_dir_cb_arg *a = arg;
2319 struct got_object_id blob_id, commit_id;
2320 unsigned char status;
2322 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2323 return got_error(GOT_ERR_CANCELLED);
2325 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2326 return NULL;
2328 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2330 if (got_fileindex_entry_has_file_on_disk(ie))
2331 status = GOT_STATUS_MISSING;
2332 else
2333 status = GOT_STATUS_DELETE;
2334 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2335 ie->path, &blob_id, NULL, &commit_id);
2338 void
2339 free_ignorelist(struct got_pathlist_head *ignorelist)
2341 struct got_pathlist_entry *pe;
2343 TAILQ_FOREACH(pe, ignorelist, entry)
2344 free((char *)pe->path);
2345 got_pathlist_free(ignorelist);
2348 void
2349 free_ignores(struct got_pathlist_head *ignores)
2351 struct got_pathlist_entry *pe;
2353 TAILQ_FOREACH(pe, ignores, entry) {
2354 struct got_pathlist_head *ignorelist = pe->data;
2355 free_ignorelist(ignorelist);
2356 free((char *)pe->path);
2358 got_pathlist_free(ignores);
2361 static const struct got_error *
2362 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2364 const struct got_error *err = NULL;
2365 struct got_pathlist_entry *pe = NULL;
2366 struct got_pathlist_head *ignorelist;
2367 char *line = NULL, *pattern, *dirpath = NULL;
2368 size_t linesize = 0;
2369 ssize_t linelen;
2371 ignorelist = calloc(1, sizeof(*ignorelist));
2372 if (ignorelist == NULL)
2373 return got_error_from_errno("calloc");
2374 TAILQ_INIT(ignorelist);
2376 while ((linelen = getline(&line, &linesize, f)) != -1) {
2377 if (linelen > 0 && line[linelen - 1] == '\n')
2378 line[linelen - 1] = '\0';
2379 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2380 line) == -1) {
2381 err = got_error_from_errno("asprintf");
2382 goto done;
2384 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2385 if (err)
2386 goto done;
2388 if (ferror(f)) {
2389 err = got_error_from_errno("getline");
2390 goto done;
2393 dirpath = strdup(path);
2394 if (dirpath == NULL) {
2395 err = got_error_from_errno("strdup");
2396 goto done;
2398 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2399 done:
2400 free(line);
2401 if (err || pe == NULL) {
2402 free(dirpath);
2403 free_ignorelist(ignorelist);
2405 return err;
2408 int
2409 match_ignores(struct got_pathlist_head *ignores, const char *path)
2411 struct got_pathlist_entry *pe;
2414 * The ignores pathlist contains ignore lists from children before
2415 * parents, so we can find the most specific ignorelist by walking
2416 * ignores backwards.
2418 pe = TAILQ_LAST(ignores, got_pathlist_head);
2419 while (pe) {
2420 if (got_path_is_child(path, pe->path, pe->path_len)) {
2421 struct got_pathlist_head *ignorelist = pe->data;
2422 struct got_pathlist_entry *pi;
2423 TAILQ_FOREACH(pi, ignorelist, entry) {
2424 if (fnmatch(pi->path, path,
2425 FNM_PATHNAME | FNM_LEADING_DIR))
2426 continue;
2427 return 1;
2430 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2433 return 0;
2436 static const struct got_error *
2437 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2438 const char *path)
2440 const struct got_error *err = NULL;
2441 char *ignorespath;
2442 FILE *ignoresfile = NULL;
2444 /* TODO: read .gitignores as well... */
2445 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2446 path[0] ? "/" : "") == -1)
2447 return got_error_from_errno("asprintf");
2449 ignoresfile = fopen(ignorespath, "r");
2450 if (ignoresfile == NULL) {
2451 if (errno != ENOENT)
2452 err = got_error_from_errno2("fopen",
2453 ignorespath);
2454 } else
2455 err = read_ignores(ignores, path, ignoresfile);
2457 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2458 err = got_error_from_errno2("flose", path);
2459 free(ignorespath);
2460 return err;
2463 static const struct got_error *
2464 status_new(void *arg, struct dirent *de, const char *parent_path)
2466 const struct got_error *err = NULL;
2467 struct diff_dir_cb_arg *a = arg;
2468 char *path = NULL;
2470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2471 return got_error(GOT_ERR_CANCELLED);
2473 /* XXX ignore symlinks for now */
2474 if (de->d_type == DT_LNK)
2475 return NULL;
2477 if (parent_path[0]) {
2478 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2479 return got_error_from_errno("asprintf");
2480 } else {
2481 path = de->d_name;
2484 if (de->d_type == DT_DIR)
2485 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2486 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2487 && !match_ignores(&a->ignores, path))
2488 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2489 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2490 if (parent_path[0])
2491 free(path);
2492 return err;
2495 static const struct got_error *
2496 report_single_file_status(const char *path, const char *ondisk_path,
2497 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2498 void *status_arg, struct got_repository *repo)
2500 struct got_fileindex_entry *ie;
2501 struct stat sb;
2503 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2504 if (ie)
2505 return report_file_status(ie, ondisk_path, status_cb,
2506 status_arg, repo);
2508 if (lstat(ondisk_path, &sb) == -1) {
2509 if (errno != ENOENT)
2510 return got_error_from_errno2("lstat", ondisk_path);
2511 return NULL;
2514 if (S_ISREG(sb.st_mode))
2515 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2518 return NULL;
2521 static const struct got_error *
2522 worktree_status(struct got_worktree *worktree, const char *path,
2523 struct got_fileindex *fileindex, struct got_repository *repo,
2524 got_worktree_status_cb status_cb, void *status_arg,
2525 got_cancel_cb cancel_cb, void *cancel_arg)
2527 const struct got_error *err = NULL;
2528 DIR *workdir = NULL;
2529 struct got_fileindex_diff_dir_cb fdiff_cb;
2530 struct diff_dir_cb_arg arg;
2531 char *ondisk_path = NULL;
2533 if (asprintf(&ondisk_path, "%s%s%s",
2534 worktree->root_path, path[0] ? "/" : "", path) == -1)
2535 return got_error_from_errno("asprintf");
2537 workdir = opendir(ondisk_path);
2538 if (workdir == NULL) {
2539 if (errno != ENOTDIR && errno != ENOENT)
2540 err = got_error_from_errno2("opendir", ondisk_path);
2541 else
2542 err = report_single_file_status(path, ondisk_path,
2543 fileindex, status_cb, status_arg, repo);
2544 } else {
2545 fdiff_cb.diff_old_new = status_old_new;
2546 fdiff_cb.diff_old = status_old;
2547 fdiff_cb.diff_new = status_new;
2548 arg.fileindex = fileindex;
2549 arg.worktree = worktree;
2550 arg.status_path = path;
2551 arg.status_path_len = strlen(path);
2552 arg.repo = repo;
2553 arg.status_cb = status_cb;
2554 arg.status_arg = status_arg;
2555 arg.cancel_cb = cancel_cb;
2556 arg.cancel_arg = cancel_arg;
2557 TAILQ_INIT(&arg.ignores);
2558 err = add_ignores(&arg.ignores, worktree->root_path, path);
2559 if (err == NULL)
2560 err = got_fileindex_diff_dir(fileindex, workdir,
2561 worktree->root_path, path, repo, &fdiff_cb, &arg);
2562 free_ignores(&arg.ignores);
2565 if (workdir)
2566 closedir(workdir);
2567 free(ondisk_path);
2568 return err;
2571 const struct got_error *
2572 got_worktree_status(struct got_worktree *worktree,
2573 struct got_pathlist_head *paths, struct got_repository *repo,
2574 got_worktree_status_cb status_cb, void *status_arg,
2575 got_cancel_cb cancel_cb, void *cancel_arg)
2577 const struct got_error *err = NULL;
2578 char *fileindex_path = NULL;
2579 struct got_fileindex *fileindex = NULL;
2580 struct got_pathlist_entry *pe;
2582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2583 if (err)
2584 return err;
2586 TAILQ_FOREACH(pe, paths, entry) {
2587 err = worktree_status(worktree, pe->path, fileindex, repo,
2588 status_cb, status_arg, cancel_cb, cancel_arg);
2589 if (err)
2590 break;
2592 free(fileindex_path);
2593 got_fileindex_free(fileindex);
2594 return err;
2597 const struct got_error *
2598 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2599 const char *arg)
2601 const struct got_error *err = NULL;
2602 char *resolved, *cwd = NULL, *path = NULL;
2603 size_t len;
2605 *wt_path = NULL;
2607 resolved = realpath(arg, NULL);
2608 if (resolved == NULL) {
2609 if (errno != ENOENT)
2610 return got_error_from_errno2("realpath", arg);
2611 cwd = getcwd(NULL, 0);
2612 if (cwd == NULL)
2613 return got_error_from_errno("getcwd");
2614 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2615 err = got_error_from_errno("asprintf");
2616 goto done;
2620 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2621 strlen(got_worktree_get_root_path(worktree)))) {
2622 err = got_error(GOT_ERR_BAD_PATH);
2623 goto done;
2626 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2627 err = got_path_skip_common_ancestor(&path,
2628 got_worktree_get_root_path(worktree), resolved);
2629 if (err)
2630 goto done;
2631 } else {
2632 path = strdup("");
2633 if (path == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2639 /* XXX status walk can't deal with trailing slash! */
2640 len = strlen(path);
2641 while (len > 0 && path[len - 1] == '/') {
2642 path[len - 1] = '\0';
2643 len--;
2645 done:
2646 free(resolved);
2647 free(cwd);
2648 if (err == NULL)
2649 *wt_path = path;
2650 else
2651 free(path);
2652 return err;
2655 static const struct got_error *
2656 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2657 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2658 struct got_repository *repo)
2660 const struct got_error *err = NULL;
2661 struct got_fileindex_entry *ie;
2662 unsigned char status;
2663 struct stat sb;
2665 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2666 if (ie) {
2667 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2668 if (err)
2669 return err;
2670 /* Re-adding an existing entry is a no-op. */
2671 if (status == GOT_STATUS_ADD)
2672 return NULL;
2673 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2676 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2677 if (err)
2678 return err;
2680 err = got_fileindex_entry_add(fileindex, ie);
2681 if (err) {
2682 got_fileindex_entry_free(ie);
2683 return err;
2686 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2689 const struct got_error *
2690 got_worktree_schedule_add(struct got_worktree *worktree,
2691 struct got_pathlist_head *paths,
2692 got_worktree_status_cb status_cb, void *status_arg,
2693 struct got_repository *repo)
2695 struct got_fileindex *fileindex = NULL;
2696 char *fileindex_path = NULL;
2697 const struct got_error *err = NULL, *sync_err, *unlockerr;
2698 struct got_pathlist_entry *pe;
2700 err = lock_worktree(worktree, LOCK_EX);
2701 if (err)
2702 return err;
2704 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2705 if (err)
2706 goto done;
2708 TAILQ_FOREACH(pe, paths, entry) {
2709 char *ondisk_path;
2710 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2711 pe->path) == -1)
2712 return got_error_from_errno("asprintf");
2713 err = schedule_addition(ondisk_path, fileindex, pe->path,
2714 status_cb, status_arg, repo);
2715 free(ondisk_path);
2716 if (err)
2717 break;
2719 sync_err = sync_fileindex(fileindex, fileindex_path);
2720 if (sync_err && err == NULL)
2721 err = sync_err;
2722 done:
2723 free(fileindex_path);
2724 if (fileindex)
2725 got_fileindex_free(fileindex);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2728 err = unlockerr;
2729 return err;
2732 static const struct got_error *
2733 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2734 const char *relpath, int delete_local_mods,
2735 got_worktree_status_cb status_cb, void *status_arg,
2736 struct got_repository *repo)
2738 const struct got_error *err = NULL;
2739 struct got_fileindex_entry *ie = NULL;
2740 unsigned char status, staged_status;
2741 struct stat sb;
2743 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2744 if (ie == NULL)
2745 return got_error(GOT_ERR_BAD_PATH);
2747 staged_status = get_staged_status(ie);
2748 if (staged_status != GOT_STATUS_NO_CHANGE) {
2749 if (staged_status == GOT_STATUS_DELETE)
2750 return NULL;
2751 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2754 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2755 if (err)
2756 return err;
2758 if (status != GOT_STATUS_NO_CHANGE) {
2759 if (status == GOT_STATUS_DELETE)
2760 return NULL;
2761 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2762 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2763 if (status != GOT_STATUS_MODIFY &&
2764 status != GOT_STATUS_MISSING)
2765 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2768 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2769 return got_error_from_errno2("unlink", ondisk_path);
2771 got_fileindex_entry_mark_deleted_from_disk(ie);
2772 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2775 const struct got_error *
2776 got_worktree_schedule_delete(struct got_worktree *worktree,
2777 struct got_pathlist_head *paths, int delete_local_mods,
2778 got_worktree_status_cb status_cb, void *status_arg,
2779 struct got_repository *repo)
2781 struct got_fileindex *fileindex = NULL;
2782 char *fileindex_path = NULL;
2783 const struct got_error *err = NULL, *sync_err, *unlockerr;
2784 struct got_pathlist_entry *pe;
2786 err = lock_worktree(worktree, LOCK_EX);
2787 if (err)
2788 return err;
2790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2791 if (err)
2792 goto done;
2794 TAILQ_FOREACH(pe, paths, entry) {
2795 char *ondisk_path;
2796 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2797 pe->path) == -1)
2798 return got_error_from_errno("asprintf");
2799 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2800 delete_local_mods, status_cb, status_arg, repo);
2801 free(ondisk_path);
2802 if (err)
2803 break;
2805 sync_err = sync_fileindex(fileindex, fileindex_path);
2806 if (sync_err && err == NULL)
2807 err = sync_err;
2808 done:
2809 free(fileindex_path);
2810 if (fileindex)
2811 got_fileindex_free(fileindex);
2812 unlockerr = lock_worktree(worktree, LOCK_SH);
2813 if (unlockerr && err == NULL)
2814 err = unlockerr;
2815 return err;
2818 static const struct got_error *
2819 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2821 const struct got_error *err = NULL;
2822 char *line = NULL;
2823 size_t linesize = 0, n;
2824 ssize_t linelen;
2826 linelen = getline(&line, &linesize, infile);
2827 if (linelen == -1) {
2828 if (ferror(infile)) {
2829 err = got_error_from_errno("getline");
2830 goto done;
2832 return NULL;
2834 if (outfile) {
2835 n = fwrite(line, 1, linelen, outfile);
2836 if (n != linelen) {
2837 err = got_ferror(outfile, GOT_ERR_IO);
2838 goto done;
2841 if (rejectfile) {
2842 n = fwrite(line, 1, linelen, rejectfile);
2843 if (n != linelen)
2844 err = got_ferror(outfile, GOT_ERR_IO);
2846 done:
2847 free(line);
2848 return err;
2851 static const struct got_error *
2852 skip_one_line(FILE *f)
2854 char *line = NULL;
2855 size_t linesize = 0;
2856 ssize_t linelen;
2858 linelen = getline(&line, &linesize, f);
2859 free(line);
2860 if (linelen == -1 && ferror(f))
2861 return got_error_from_errno("getline");
2862 return NULL;
2865 static const struct got_error *
2866 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2867 int start_old, int end_old, int start_new, int end_new,
2868 FILE *outfile, FILE *rejectfile)
2870 const struct got_error *err;
2872 /* Copy old file's lines leading up to patch. */
2873 while (!feof(f1) && *line_cur1 < start_old) {
2874 err = copy_one_line(f1, outfile, NULL);
2875 if (err)
2876 return err;
2877 (*line_cur1)++;
2879 /* Skip new file's lines leading up to patch. */
2880 while (!feof(f2) && *line_cur2 < start_new) {
2881 if (rejectfile)
2882 err = copy_one_line(f2, NULL, rejectfile);
2883 else
2884 err = skip_one_line(f2);
2885 if (err)
2886 return err;
2887 (*line_cur2)++;
2889 /* Copy patched lines. */
2890 while (!feof(f2) && *line_cur2 <= end_new) {
2891 err = copy_one_line(f2, outfile, NULL);
2892 if (err)
2893 return err;
2894 (*line_cur2)++;
2896 /* Skip over old file's replaced lines. */
2897 while (!feof(f1) && *line_cur1 <= end_old) {
2898 if (rejectfile)
2899 err = copy_one_line(f1, NULL, rejectfile);
2900 else
2901 err = skip_one_line(f1);
2902 if (err)
2903 return err;
2904 (*line_cur1)++;
2907 return NULL;
2910 static const struct got_error *
2911 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2912 FILE *outfile, FILE *rejectfile)
2914 const struct got_error *err;
2916 if (outfile) {
2917 /* Copy old file's lines until EOF. */
2918 while (!feof(f1)) {
2919 err = copy_one_line(f1, outfile, NULL);
2920 if (err)
2921 return err;
2922 (*line_cur1)++;
2925 if (rejectfile) {
2926 /* Copy new file's lines until EOF. */
2927 while (!feof(f2)) {
2928 err = copy_one_line(f2, NULL, rejectfile);
2929 if (err)
2930 return err;
2931 (*line_cur2)++;
2935 return NULL;
2938 static const struct got_error *
2939 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2940 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2941 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2942 int *line_cur2, FILE *outfile, FILE *rejectfile,
2943 got_worktree_patch_cb patch_cb, void *patch_arg)
2945 const struct got_error *err = NULL;
2946 int start_old = change->cv.a;
2947 int end_old = change->cv.b;
2948 int start_new = change->cv.c;
2949 int end_new = change->cv.d;
2950 long pos1, pos2;
2951 FILE *hunkfile;
2953 *choice = GOT_PATCH_CHOICE_NONE;
2955 hunkfile = got_opentemp();
2956 if (hunkfile == NULL)
2957 return got_error_from_errno("got_opentemp");
2959 pos1 = ftell(f1);
2960 pos2 = ftell(f2);
2962 /* XXX TODO needs error checking */
2963 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2965 if (fseek(f1, pos1, SEEK_SET) == -1) {
2966 err = got_ferror(f1, GOT_ERR_IO);
2967 goto done;
2969 if (fseek(f2, pos2, SEEK_SET) == -1) {
2970 err = got_ferror(f1, GOT_ERR_IO);
2971 goto done;
2973 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2974 err = got_ferror(hunkfile, GOT_ERR_IO);
2975 goto done;
2978 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2979 hunkfile, n, nchanges);
2980 if (err)
2981 goto done;
2983 switch (*choice) {
2984 case GOT_PATCH_CHOICE_YES:
2985 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2986 end_old, start_new, end_new, outfile, rejectfile);
2987 break;
2988 case GOT_PATCH_CHOICE_NO:
2989 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2990 end_old, start_new, end_new, rejectfile, outfile);
2991 break;
2992 case GOT_PATCH_CHOICE_QUIT:
2993 break;
2994 default:
2995 err = got_error(GOT_ERR_PATCH_CHOICE);
2996 break;
2998 done:
2999 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3000 err = got_error_from_errno("fclose");
3001 return err;
3004 struct revert_file_args {
3005 struct got_worktree *worktree;
3006 struct got_fileindex *fileindex;
3007 got_worktree_checkout_cb progress_cb;
3008 void *progress_arg;
3009 got_worktree_patch_cb patch_cb;
3010 void *patch_arg;
3011 struct got_repository *repo;
3014 static const struct got_error *
3015 create_patched_content(char **path_outfile, int reverse_patch,
3016 struct got_object_id *blob_id, const char *path2,
3017 const char *relpath, struct got_repository *repo,
3018 got_worktree_patch_cb patch_cb, void *patch_arg)
3020 const struct got_error *err;
3021 struct got_blob_object *blob = NULL;
3022 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3023 char *path1 = NULL, *id_str = NULL;
3024 struct stat sb1, sb2;
3025 struct got_diff_changes *changes = NULL;
3026 struct got_diff_state *ds = NULL;
3027 struct got_diff_args *args = NULL;
3028 struct got_diff_change *change;
3029 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3030 int n = 0;
3032 *path_outfile = NULL;
3034 err = got_object_id_str(&id_str, blob_id);
3035 if (err)
3036 return err;
3038 f2 = fopen(path2, "r");
3039 if (f2 == NULL) {
3040 err = got_error_from_errno2("fopen", path2);
3041 goto done;
3044 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3045 if (err)
3046 goto done;
3048 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3049 if (err)
3050 goto done;
3052 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3053 if (err)
3054 goto done;
3056 if (stat(path1, &sb1) == -1) {
3057 err = got_error_from_errno2("stat", path1);
3058 goto done;
3060 if (stat(path2, &sb2) == -1) {
3061 err = got_error_from_errno2("stat", path2);
3062 goto done;
3065 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3066 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3067 if (err)
3068 goto done;
3070 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3071 if (err)
3072 goto done;
3074 if (fseek(f1, 0L, SEEK_SET) == -1)
3075 return got_ferror(f1, GOT_ERR_IO);
3076 if (fseek(f2, 0L, SEEK_SET) == -1)
3077 return got_ferror(f2, GOT_ERR_IO);
3078 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3079 int choice;
3080 err = apply_or_reject_change(&choice, change, ++n,
3081 changes->nchanges, ds, args, diff_flags, relpath,
3082 f1, f2, &line_cur1, &line_cur2,
3083 reverse_patch ? NULL : outfile,
3084 reverse_patch ? outfile : NULL,
3085 patch_cb, patch_arg);
3086 if (err)
3087 goto done;
3088 if (choice == GOT_PATCH_CHOICE_YES)
3089 have_content = 1;
3090 else if (choice == GOT_PATCH_CHOICE_QUIT)
3091 break;
3093 if (have_content)
3094 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3095 reverse_patch ? NULL : outfile,
3096 reverse_patch ? outfile : NULL);
3097 done:
3098 free(id_str);
3099 if (blob)
3100 got_object_blob_close(blob);
3101 if (f1 && fclose(f1) == EOF && err == NULL)
3102 err = got_error_from_errno2("fclose", path1);
3103 if (f2 && fclose(f2) == EOF && err == NULL)
3104 err = got_error_from_errno2("fclose", path2);
3105 if (outfile && fclose(outfile) == EOF && err == NULL)
3106 err = got_error_from_errno2("fclose", *path_outfile);
3107 if (path1 && unlink(path1) == -1 && err == NULL)
3108 err = got_error_from_errno2("unlink", path1);
3109 if (err || !have_content) {
3110 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3111 err = got_error_from_errno2("unlink", *path_outfile);
3112 free(*path_outfile);
3113 *path_outfile = NULL;
3115 free(args);
3116 if (ds) {
3117 got_diff_state_free(ds);
3118 free(ds);
3120 if (changes)
3121 got_diff_free_changes(changes);
3122 free(path1);
3123 return err;
3126 static const struct got_error *
3127 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3128 const char *relpath, struct got_object_id *blob_id,
3129 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3131 struct revert_file_args *a = arg;
3132 const struct got_error *err = NULL;
3133 char *parent_path = NULL;
3134 struct got_fileindex_entry *ie;
3135 struct got_tree_object *tree = NULL;
3136 struct got_object_id *tree_id = NULL;
3137 const struct got_tree_entry *te = NULL;
3138 char *tree_path = NULL, *te_name;
3139 char *ondisk_path = NULL, *path_content = NULL;
3140 struct got_blob_object *blob = NULL;
3142 /* Reverting a staged deletion is a no-op. */
3143 if (status == GOT_STATUS_DELETE &&
3144 staged_status != GOT_STATUS_NO_CHANGE)
3145 return NULL;
3147 if (status == GOT_STATUS_UNVERSIONED)
3148 return (*a->progress_cb)(a->progress_arg,
3149 GOT_STATUS_UNVERSIONED, relpath);
3151 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3152 if (ie == NULL)
3153 return got_error(GOT_ERR_BAD_PATH);
3155 /* Construct in-repository path of tree which contains this blob. */
3156 err = got_path_dirname(&parent_path, ie->path);
3157 if (err) {
3158 if (err->code != GOT_ERR_BAD_PATH)
3159 goto done;
3160 parent_path = strdup("/");
3161 if (parent_path == NULL) {
3162 err = got_error_from_errno("strdup");
3163 goto done;
3166 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3167 tree_path = strdup(parent_path);
3168 if (tree_path == NULL) {
3169 err = got_error_from_errno("strdup");
3170 goto done;
3172 } else {
3173 if (got_path_is_root_dir(parent_path)) {
3174 tree_path = strdup(a->worktree->path_prefix);
3175 if (tree_path == NULL) {
3176 err = got_error_from_errno("strdup");
3177 goto done;
3179 } else {
3180 if (asprintf(&tree_path, "%s/%s",
3181 a->worktree->path_prefix, parent_path) == -1) {
3182 err = got_error_from_errno("asprintf");
3183 goto done;
3188 err = got_object_id_by_path(&tree_id, a->repo,
3189 a->worktree->base_commit_id, tree_path);
3190 if (err) {
3191 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3192 (status == GOT_STATUS_ADD ||
3193 staged_status == GOT_STATUS_ADD)))
3194 goto done;
3195 } else {
3196 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3197 if (err)
3198 goto done;
3200 te_name = basename(ie->path);
3201 if (te_name == NULL) {
3202 err = got_error_from_errno2("basename", ie->path);
3203 goto done;
3206 te = got_object_tree_find_entry(tree, te_name);
3207 if (te == NULL && status != GOT_STATUS_ADD &&
3208 staged_status != GOT_STATUS_ADD) {
3209 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3210 goto done;
3214 switch (status) {
3215 case GOT_STATUS_ADD:
3216 if (a->patch_cb) {
3217 int choice = GOT_PATCH_CHOICE_NONE;
3218 err = (*a->patch_cb)(&choice, a->patch_arg,
3219 status, ie->path, NULL, 1, 1);
3220 if (err)
3221 goto done;
3222 if (choice != GOT_PATCH_CHOICE_YES)
3223 break;
3225 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3226 ie->path);
3227 if (err)
3228 goto done;
3229 got_fileindex_entry_remove(a->fileindex, ie);
3230 break;
3231 case GOT_STATUS_DELETE:
3232 if (a->patch_cb) {
3233 int choice = GOT_PATCH_CHOICE_NONE;
3234 err = (*a->patch_cb)(&choice, a->patch_arg,
3235 status, ie->path, NULL, 1, 1);
3236 if (err)
3237 goto done;
3238 if (choice != GOT_PATCH_CHOICE_YES)
3239 break;
3241 /* fall through */
3242 case GOT_STATUS_MODIFY:
3243 case GOT_STATUS_CONFLICT:
3244 case GOT_STATUS_MISSING: {
3245 struct got_object_id id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3250 } else
3251 memcpy(id.sha1, ie->blob_sha1,
3252 SHA1_DIGEST_LENGTH);
3253 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3254 if (err)
3255 goto done;
3257 if (asprintf(&ondisk_path, "%s/%s",
3258 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3259 err = got_error_from_errno("asprintf");
3260 goto done;
3263 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3264 status == GOT_STATUS_CONFLICT)) {
3265 err = create_patched_content(&path_content, 1, &id,
3266 ondisk_path, ie->path, a->repo,
3267 a->patch_cb, a->patch_arg);
3268 if (err || path_content == NULL)
3269 break;
3270 if (rename(path_content, ondisk_path) == -1) {
3271 err = got_error_from_errno3("rename",
3272 path_content, ondisk_path);
3273 goto done;
3275 } else {
3276 err = install_blob(a->worktree, ondisk_path, ie->path,
3277 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3278 got_fileindex_perms_to_st(ie), blob, 0, 1,
3279 a->repo, a->progress_cb, a->progress_arg);
3280 if (err)
3281 goto done;
3282 if (status == GOT_STATUS_DELETE) {
3283 err = update_blob_fileindex_entry(a->worktree,
3284 a->fileindex, ie, ondisk_path, ie->path,
3285 blob, 1);
3286 if (err)
3287 goto done;
3290 break;
3292 default:
3293 break;
3295 done:
3296 free(ondisk_path);
3297 free(path_content);
3298 free(parent_path);
3299 free(tree_path);
3300 if (blob)
3301 got_object_blob_close(blob);
3302 if (tree)
3303 got_object_tree_close(tree);
3304 free(tree_id);
3305 return err;
3308 const struct got_error *
3309 got_worktree_revert(struct got_worktree *worktree,
3310 struct got_pathlist_head *paths,
3311 got_worktree_checkout_cb progress_cb, void *progress_arg,
3312 got_worktree_patch_cb patch_cb, void *patch_arg,
3313 struct got_repository *repo)
3315 struct got_fileindex *fileindex = NULL;
3316 char *fileindex_path = NULL;
3317 const struct got_error *err = NULL, *unlockerr = NULL;
3318 const struct got_error *sync_err = NULL;
3319 struct got_pathlist_entry *pe;
3320 struct revert_file_args rfa;
3322 err = lock_worktree(worktree, LOCK_EX);
3323 if (err)
3324 return err;
3326 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3327 if (err)
3328 goto done;
3330 rfa.worktree = worktree;
3331 rfa.fileindex = fileindex;
3332 rfa.progress_cb = progress_cb;
3333 rfa.progress_arg = progress_arg;
3334 rfa.patch_cb = patch_cb;
3335 rfa.patch_arg = patch_arg;
3336 rfa.repo = repo;
3337 TAILQ_FOREACH(pe, paths, entry) {
3338 err = worktree_status(worktree, pe->path, fileindex, repo,
3339 revert_file, &rfa, NULL, NULL);
3340 if (err)
3341 break;
3343 sync_err = sync_fileindex(fileindex, fileindex_path);
3344 if (sync_err && err == NULL)
3345 err = sync_err;
3346 done:
3347 free(fileindex_path);
3348 if (fileindex)
3349 got_fileindex_free(fileindex);
3350 unlockerr = lock_worktree(worktree, LOCK_SH);
3351 if (unlockerr && err == NULL)
3352 err = unlockerr;
3353 return err;
3356 static void
3357 free_commitable(struct got_commitable *ct)
3359 free(ct->path);
3360 free(ct->in_repo_path);
3361 free(ct->ondisk_path);
3362 free(ct->blob_id);
3363 free(ct->base_blob_id);
3364 free(ct->staged_blob_id);
3365 free(ct->base_commit_id);
3366 free(ct);
3369 struct collect_commitables_arg {
3370 struct got_pathlist_head *commitable_paths;
3371 struct got_repository *repo;
3372 struct got_worktree *worktree;
3373 int have_staged_files;
3376 static const struct got_error *
3377 collect_commitables(void *arg, unsigned char status,
3378 unsigned char staged_status, const char *relpath,
3379 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3380 struct got_object_id *commit_id)
3382 struct collect_commitables_arg *a = arg;
3383 const struct got_error *err = NULL;
3384 struct got_commitable *ct = NULL;
3385 struct got_pathlist_entry *new = NULL;
3386 char *parent_path = NULL, *path = NULL;
3387 struct stat sb;
3389 if (a->have_staged_files) {
3390 if (staged_status != GOT_STATUS_MODIFY &&
3391 staged_status != GOT_STATUS_ADD &&
3392 staged_status != GOT_STATUS_DELETE)
3393 return NULL;
3394 } else {
3395 if (status == GOT_STATUS_CONFLICT)
3396 return got_error(GOT_ERR_COMMIT_CONFLICT);
3398 if (status != GOT_STATUS_MODIFY &&
3399 status != GOT_STATUS_ADD &&
3400 status != GOT_STATUS_DELETE)
3401 return NULL;
3404 if (asprintf(&path, "/%s", relpath) == -1) {
3405 err = got_error_from_errno("asprintf");
3406 goto done;
3408 if (strcmp(path, "/") == 0) {
3409 parent_path = strdup("");
3410 if (parent_path == NULL)
3411 return got_error_from_errno("strdup");
3412 } else {
3413 err = got_path_dirname(&parent_path, path);
3414 if (err)
3415 return err;
3418 ct = calloc(1, sizeof(*ct));
3419 if (ct == NULL) {
3420 err = got_error_from_errno("calloc");
3421 goto done;
3424 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3425 relpath) == -1) {
3426 err = got_error_from_errno("asprintf");
3427 goto done;
3429 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3430 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3431 } else {
3432 if (lstat(ct->ondisk_path, &sb) != 0) {
3433 err = got_error_from_errno2("lstat", ct->ondisk_path);
3434 goto done;
3436 ct->mode = sb.st_mode;
3439 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3440 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3441 relpath) == -1) {
3442 err = got_error_from_errno("asprintf");
3443 goto done;
3446 ct->status = status;
3447 ct->staged_status = staged_status;
3448 ct->blob_id = NULL; /* will be filled in when blob gets created */
3449 if (ct->status != GOT_STATUS_ADD &&
3450 ct->staged_status != GOT_STATUS_ADD) {
3451 ct->base_blob_id = got_object_id_dup(blob_id);
3452 if (ct->base_blob_id == NULL) {
3453 err = got_error_from_errno("got_object_id_dup");
3454 goto done;
3456 ct->base_commit_id = got_object_id_dup(commit_id);
3457 if (ct->base_commit_id == NULL) {
3458 err = got_error_from_errno("got_object_id_dup");
3459 goto done;
3462 if (ct->staged_status == GOT_STATUS_ADD ||
3463 ct->staged_status == GOT_STATUS_MODIFY) {
3464 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3465 if (ct->staged_blob_id == NULL) {
3466 err = got_error_from_errno("got_object_id_dup");
3467 goto done;
3470 ct->path = strdup(path);
3471 if (ct->path == NULL) {
3472 err = got_error_from_errno("strdup");
3473 goto done;
3475 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3476 done:
3477 if (ct && (err || new == NULL))
3478 free_commitable(ct);
3479 free(parent_path);
3480 free(path);
3481 return err;
3484 static const struct got_error *write_tree(struct got_object_id **,
3485 struct got_tree_object *, const char *, struct got_pathlist_head *,
3486 got_worktree_status_cb status_cb, void *status_arg,
3487 struct got_repository *);
3489 static const struct got_error *
3490 write_subtree(struct got_object_id **new_subtree_id,
3491 struct got_tree_entry *te, const char *parent_path,
3492 struct got_pathlist_head *commitable_paths,
3493 got_worktree_status_cb status_cb, void *status_arg,
3494 struct got_repository *repo)
3496 const struct got_error *err = NULL;
3497 struct got_tree_object *subtree;
3498 char *subpath;
3500 if (asprintf(&subpath, "%s%s%s", parent_path,
3501 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3502 return got_error_from_errno("asprintf");
3504 err = got_object_open_as_tree(&subtree, repo, te->id);
3505 if (err)
3506 return err;
3508 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3509 status_cb, status_arg, repo);
3510 got_object_tree_close(subtree);
3511 free(subpath);
3512 return err;
3515 static const struct got_error *
3516 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3518 const struct got_error *err = NULL;
3519 char *ct_parent_path = NULL;
3521 *match = 0;
3523 if (strchr(ct->in_repo_path, '/') == NULL) {
3524 *match = got_path_is_root_dir(path);
3525 return NULL;
3528 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3529 if (err)
3530 return err;
3531 *match = (strcmp(path, ct_parent_path) == 0);
3532 free(ct_parent_path);
3533 return err;
3536 static mode_t
3537 get_ct_file_mode(struct got_commitable *ct)
3539 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3542 static const struct got_error *
3543 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3544 struct got_tree_entry *te, struct got_commitable *ct)
3546 const struct got_error *err = NULL;
3548 *new_te = NULL;
3550 err = got_object_tree_entry_dup(new_te, te);
3551 if (err)
3552 goto done;
3554 (*new_te)->mode = get_ct_file_mode(ct);
3556 free((*new_te)->id);
3557 if (ct->staged_status == GOT_STATUS_MODIFY)
3558 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3559 else
3560 (*new_te)->id = got_object_id_dup(ct->blob_id);
3561 if ((*new_te)->id == NULL) {
3562 err = got_error_from_errno("got_object_id_dup");
3563 goto done;
3565 done:
3566 if (err && *new_te) {
3567 got_object_tree_entry_close(*new_te);
3568 *new_te = NULL;
3570 return err;
3573 static const struct got_error *
3574 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3575 struct got_commitable *ct)
3577 const struct got_error *err = NULL;
3578 char *ct_name;
3580 *new_te = NULL;
3582 *new_te = calloc(1, sizeof(**new_te));
3583 if (*new_te == NULL)
3584 return got_error_from_errno("calloc");
3586 ct_name = basename(ct->path);
3587 if (ct_name == NULL) {
3588 err = got_error_from_errno2("basename", ct->path);
3589 goto done;
3591 (*new_te)->name = strdup(ct_name);
3592 if ((*new_te)->name == NULL) {
3593 err = got_error_from_errno("strdup");
3594 goto done;
3597 (*new_te)->mode = get_ct_file_mode(ct);
3599 if (ct->staged_status == GOT_STATUS_ADD)
3600 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3601 else
3602 (*new_te)->id = got_object_id_dup(ct->blob_id);
3603 if ((*new_te)->id == NULL) {
3604 err = got_error_from_errno("got_object_id_dup");
3605 goto done;
3607 done:
3608 if (err && *new_te) {
3609 got_object_tree_entry_close(*new_te);
3610 *new_te = NULL;
3612 return err;
3615 static const struct got_error *
3616 insert_tree_entry(struct got_tree_entry *new_te,
3617 struct got_pathlist_head *paths)
3619 const struct got_error *err = NULL;
3620 struct got_pathlist_entry *new_pe;
3622 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3623 if (err)
3624 return err;
3625 if (new_pe == NULL)
3626 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3627 return NULL;
3630 static const struct got_error *
3631 report_ct_status(struct got_commitable *ct,
3632 got_worktree_status_cb status_cb, void *status_arg)
3634 const char *ct_path = ct->path;
3635 unsigned char status;
3637 while (ct_path[0] == '/')
3638 ct_path++;
3640 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3641 status = ct->staged_status;
3642 else
3643 status = ct->status;
3645 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3646 ct_path, ct->blob_id, NULL, NULL);
3649 static const struct got_error *
3650 match_modified_subtree(int *modified, struct got_tree_entry *te,
3651 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3653 const struct got_error *err = NULL;
3654 struct got_pathlist_entry *pe;
3655 char *te_path;
3657 *modified = 0;
3659 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3660 got_path_is_root_dir(base_tree_path) ? "" : "/",
3661 te->name) == -1)
3662 return got_error_from_errno("asprintf");
3664 TAILQ_FOREACH(pe, commitable_paths, entry) {
3665 struct got_commitable *ct = pe->data;
3666 *modified = got_path_is_child(ct->in_repo_path, te_path,
3667 strlen(te_path));
3668 if (*modified)
3669 break;
3672 free(te_path);
3673 return err;
3676 static const struct got_error *
3677 match_deleted_or_modified_ct(struct got_commitable **ctp,
3678 struct got_tree_entry *te, const char *base_tree_path,
3679 struct got_pathlist_head *commitable_paths)
3681 const struct got_error *err = NULL;
3682 struct got_pathlist_entry *pe;
3684 *ctp = NULL;
3686 TAILQ_FOREACH(pe, commitable_paths, entry) {
3687 struct got_commitable *ct = pe->data;
3688 char *ct_name = NULL;
3689 int path_matches;
3691 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3692 if (ct->status != GOT_STATUS_MODIFY &&
3693 ct->status != GOT_STATUS_DELETE)
3694 continue;
3695 } else {
3696 if (ct->staged_status != GOT_STATUS_MODIFY &&
3697 ct->staged_status != GOT_STATUS_DELETE)
3698 continue;
3701 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3702 continue;
3704 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3705 if (err)
3706 return err;
3707 if (!path_matches)
3708 continue;
3710 ct_name = basename(pe->path);
3711 if (ct_name == NULL)
3712 return got_error_from_errno2("basename", pe->path);
3714 if (strcmp(te->name, ct_name) != 0)
3715 continue;
3717 *ctp = ct;
3718 break;
3721 return err;
3724 static const struct got_error *
3725 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3726 const char *child_path, const char *path_base_tree,
3727 struct got_pathlist_head *commitable_paths,
3728 got_worktree_status_cb status_cb, void *status_arg,
3729 struct got_repository *repo)
3731 const struct got_error *err = NULL;
3732 struct got_tree_entry *new_te;
3733 char *subtree_path;
3735 *new_tep = NULL;
3737 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3738 got_path_is_root_dir(path_base_tree) ? "" : "/",
3739 child_path) == -1)
3740 return got_error_from_errno("asprintf");
3742 new_te = calloc(1, sizeof(*new_te));
3743 new_te->mode = S_IFDIR;
3744 new_te->name = strdup(child_path);
3745 if (new_te->name == NULL) {
3746 err = got_error_from_errno("strdup");
3747 got_object_tree_entry_close(new_te);
3748 goto done;
3750 err = write_tree(&new_te->id, NULL, subtree_path,
3751 commitable_paths, status_cb, status_arg, repo);
3752 if (err) {
3753 got_object_tree_entry_close(new_te);
3754 goto done;
3756 done:
3757 free(subtree_path);
3758 if (err == NULL)
3759 *new_tep = new_te;
3760 return err;
3763 static const struct got_error *
3764 write_tree(struct got_object_id **new_tree_id,
3765 struct got_tree_object *base_tree, const char *path_base_tree,
3766 struct got_pathlist_head *commitable_paths,
3767 got_worktree_status_cb status_cb, void *status_arg,
3768 struct got_repository *repo)
3770 const struct got_error *err = NULL;
3771 const struct got_tree_entries *base_entries = NULL;
3772 struct got_pathlist_head paths;
3773 struct got_tree_entries new_tree_entries;
3774 struct got_tree_entry *te, *new_te = NULL;
3775 struct got_pathlist_entry *pe;
3777 TAILQ_INIT(&paths);
3778 new_tree_entries.nentries = 0;
3779 SIMPLEQ_INIT(&new_tree_entries.head);
3781 /* Insert, and recurse into, newly added entries first. */
3782 TAILQ_FOREACH(pe, commitable_paths, entry) {
3783 struct got_commitable *ct = pe->data;
3784 char *child_path = NULL, *slash;
3786 if ((ct->status != GOT_STATUS_ADD &&
3787 ct->staged_status != GOT_STATUS_ADD) ||
3788 (ct->flags & GOT_COMMITABLE_ADDED))
3789 continue;
3791 if (!got_path_is_child(pe->path, path_base_tree,
3792 strlen(path_base_tree)))
3793 continue;
3795 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3796 pe->path);
3797 if (err)
3798 goto done;
3800 slash = strchr(child_path, '/');
3801 if (slash == NULL) {
3802 err = alloc_added_blob_tree_entry(&new_te, ct);
3803 if (err)
3804 goto done;
3805 err = report_ct_status(ct, status_cb, status_arg);
3806 if (err)
3807 goto done;
3808 ct->flags |= GOT_COMMITABLE_ADDED;
3809 err = insert_tree_entry(new_te, &paths);
3810 if (err)
3811 goto done;
3812 } else {
3813 *slash = '\0'; /* trim trailing path components */
3814 if (base_tree == NULL ||
3815 got_object_tree_find_entry(base_tree, child_path)
3816 == NULL) {
3817 err = make_subtree_for_added_blob(&new_te,
3818 child_path, path_base_tree,
3819 commitable_paths, status_cb, status_arg,
3820 repo);
3821 if (err)
3822 goto done;
3823 err = insert_tree_entry(new_te, &paths);
3824 if (err)
3825 goto done;
3830 if (base_tree) {
3831 /* Handle modified and deleted entries. */
3832 base_entries = got_object_tree_get_entries(base_tree);
3833 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3834 struct got_commitable *ct = NULL;
3836 if (S_ISDIR(te->mode)) {
3837 int modified;
3838 err = got_object_tree_entry_dup(&new_te, te);
3839 if (err)
3840 goto done;
3841 err = match_modified_subtree(&modified, te,
3842 path_base_tree, commitable_paths);
3843 if (err)
3844 goto done;
3845 /* Avoid recursion into unmodified subtrees. */
3846 if (modified) {
3847 free(new_te->id);
3848 err = write_subtree(&new_te->id, te,
3849 path_base_tree, commitable_paths,
3850 status_cb, status_arg, repo);
3851 if (err)
3852 goto done;
3854 err = insert_tree_entry(new_te, &paths);
3855 if (err)
3856 goto done;
3857 continue;
3860 err = match_deleted_or_modified_ct(&ct, te,
3861 path_base_tree, commitable_paths);
3862 if (ct) {
3863 /* NB: Deleted entries get dropped here. */
3864 if (ct->status == GOT_STATUS_MODIFY ||
3865 ct->staged_status == GOT_STATUS_MODIFY) {
3866 err = alloc_modified_blob_tree_entry(
3867 &new_te, te, ct);
3868 if (err)
3869 goto done;
3870 err = insert_tree_entry(new_te, &paths);
3871 if (err)
3872 goto done;
3874 err = report_ct_status(ct, status_cb,
3875 status_arg);
3876 if (err)
3877 goto done;
3878 } else {
3879 /* Entry is unchanged; just copy it. */
3880 err = got_object_tree_entry_dup(&new_te, te);
3881 if (err)
3882 goto done;
3883 err = insert_tree_entry(new_te, &paths);
3884 if (err)
3885 goto done;
3890 /* Write new list of entries; deleted entries have been dropped. */
3891 TAILQ_FOREACH(pe, &paths, entry) {
3892 struct got_tree_entry *te = pe->data;
3893 new_tree_entries.nentries++;
3894 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3896 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3897 done:
3898 got_object_tree_entries_close(&new_tree_entries);
3899 got_pathlist_free(&paths);
3900 return err;
3903 static const struct got_error *
3904 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3905 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3907 const struct got_error *err = NULL;
3908 struct got_pathlist_entry *pe;
3910 TAILQ_FOREACH(pe, commitable_paths, entry) {
3911 struct got_fileindex_entry *ie;
3912 struct got_commitable *ct = pe->data;
3914 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3915 if (ie) {
3916 if (ct->status == GOT_STATUS_DELETE ||
3917 ct->staged_status == GOT_STATUS_DELETE) {
3918 got_fileindex_entry_remove(fileindex, ie);
3919 got_fileindex_entry_free(ie);
3920 } else if (ct->staged_status == GOT_STATUS_ADD ||
3921 ct->staged_status == GOT_STATUS_MODIFY) {
3922 got_fileindex_entry_stage_set(ie,
3923 GOT_FILEIDX_STAGE_NONE);
3924 err = got_fileindex_entry_update(ie,
3925 ct->ondisk_path, ct->staged_blob_id->sha1,
3926 new_base_commit_id->sha1, 1);
3927 } else
3928 err = got_fileindex_entry_update(ie,
3929 ct->ondisk_path, ct->blob_id->sha1,
3930 new_base_commit_id->sha1, 1);
3931 } else {
3932 err = got_fileindex_entry_alloc(&ie,
3933 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3934 new_base_commit_id->sha1);
3935 if (err)
3936 break;
3937 err = got_fileindex_entry_add(fileindex, ie);
3938 if (err)
3939 break;
3942 return err;
3946 static const struct got_error *
3947 check_out_of_date(const char *in_repo_path, unsigned char status,
3948 unsigned char staged_status, struct got_object_id *base_blob_id,
3949 struct got_object_id *base_commit_id,
3950 struct got_object_id *head_commit_id, struct got_repository *repo,
3951 int ood_errcode)
3953 const struct got_error *err = NULL;
3954 struct got_object_id *id = NULL;
3956 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3957 /* Trivial case: base commit == head commit */
3958 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3959 return NULL;
3961 * Ensure file content which local changes were based
3962 * on matches file content in the branch head.
3964 err = got_object_id_by_path(&id, repo, head_commit_id,
3965 in_repo_path);
3966 if (err) {
3967 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3968 err = got_error(ood_errcode);
3969 goto done;
3970 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3971 err = got_error(ood_errcode);
3972 } else {
3973 /* Require that added files don't exist in the branch head. */
3974 err = got_object_id_by_path(&id, repo, head_commit_id,
3975 in_repo_path);
3976 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3977 goto done;
3978 err = id ? got_error(ood_errcode) : NULL;
3980 done:
3981 free(id);
3982 return err;
3985 const struct got_error *
3986 commit_worktree(struct got_object_id **new_commit_id,
3987 struct got_pathlist_head *commitable_paths,
3988 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3989 const char *author, const char *committer,
3990 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3991 got_worktree_status_cb status_cb, void *status_arg,
3992 struct got_repository *repo)
3994 const struct got_error *err = NULL, *unlockerr = NULL;
3995 struct got_pathlist_entry *pe;
3996 const char *head_ref_name = NULL;
3997 struct got_commit_object *head_commit = NULL;
3998 struct got_reference *head_ref2 = NULL;
3999 struct got_object_id *head_commit_id2 = NULL;
4000 struct got_tree_object *head_tree = NULL;
4001 struct got_object_id *new_tree_id = NULL;
4002 struct got_object_id_queue parent_ids;
4003 struct got_object_qid *pid = NULL;
4004 char *logmsg = NULL;
4006 *new_commit_id = NULL;
4008 SIMPLEQ_INIT(&parent_ids);
4010 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4011 if (err)
4012 goto done;
4014 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4015 if (err)
4016 goto done;
4018 if (commit_msg_cb != NULL) {
4019 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4020 if (err)
4021 goto done;
4024 if (logmsg == NULL || strlen(logmsg) == 0) {
4025 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4026 goto done;
4029 /* Create blobs from added and modified files and record their IDs. */
4030 TAILQ_FOREACH(pe, commitable_paths, entry) {
4031 struct got_commitable *ct = pe->data;
4032 char *ondisk_path;
4034 /* Blobs for staged files already exist. */
4035 if (ct->staged_status == GOT_STATUS_ADD ||
4036 ct->staged_status == GOT_STATUS_MODIFY)
4037 continue;
4039 if (ct->status != GOT_STATUS_ADD &&
4040 ct->status != GOT_STATUS_MODIFY)
4041 continue;
4043 if (asprintf(&ondisk_path, "%s/%s",
4044 worktree->root_path, pe->path) == -1) {
4045 err = got_error_from_errno("asprintf");
4046 goto done;
4048 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4049 free(ondisk_path);
4050 if (err)
4051 goto done;
4054 /* Recursively write new tree objects. */
4055 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4056 status_cb, status_arg, repo);
4057 if (err)
4058 goto done;
4060 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4061 if (err)
4062 goto done;
4063 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4064 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4065 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4066 got_object_qid_free(pid);
4067 if (logmsg != NULL)
4068 free(logmsg);
4069 if (err)
4070 goto done;
4072 /* Check if a concurrent commit to our branch has occurred. */
4073 head_ref_name = got_worktree_get_head_ref_name(worktree);
4074 if (head_ref_name == NULL) {
4075 err = got_error_from_errno("got_worktree_get_head_ref_name");
4076 goto done;
4078 /* Lock the reference here to prevent concurrent modification. */
4079 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4080 if (err)
4081 goto done;
4082 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4083 if (err)
4084 goto done;
4085 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4086 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4087 goto done;
4089 /* Update branch head in repository. */
4090 err = got_ref_change_ref(head_ref2, *new_commit_id);
4091 if (err)
4092 goto done;
4093 err = got_ref_write(head_ref2, repo);
4094 if (err)
4095 goto done;
4097 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4098 if (err)
4099 goto done;
4101 err = ref_base_commit(worktree, repo);
4102 if (err)
4103 goto done;
4104 done:
4105 if (head_tree)
4106 got_object_tree_close(head_tree);
4107 if (head_commit)
4108 got_object_commit_close(head_commit);
4109 free(head_commit_id2);
4110 if (head_ref2) {
4111 unlockerr = got_ref_unlock(head_ref2);
4112 if (unlockerr && err == NULL)
4113 err = unlockerr;
4114 got_ref_close(head_ref2);
4116 return err;
4119 static const struct got_error *
4120 check_path_is_commitable(const char *path,
4121 struct got_pathlist_head *commitable_paths)
4123 struct got_pathlist_entry *cpe = NULL;
4124 size_t path_len = strlen(path);
4126 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4127 struct got_commitable *ct = cpe->data;
4128 const char *ct_path = ct->path;
4130 while (ct_path[0] == '/')
4131 ct_path++;
4133 if (strcmp(path, ct_path) == 0 ||
4134 got_path_is_child(ct_path, path, path_len))
4135 break;
4138 if (cpe == NULL)
4139 return got_error_path(path, GOT_ERR_BAD_PATH);
4141 return NULL;
4144 static const struct got_error *
4145 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4147 int *have_staged_files = arg;
4149 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4150 *have_staged_files = 1;
4151 return got_error(GOT_ERR_CANCELLED);
4154 return NULL;
4157 static const struct got_error *
4158 check_non_staged_files(struct got_fileindex *fileindex,
4159 struct got_pathlist_head *paths)
4161 struct got_pathlist_entry *pe;
4162 struct got_fileindex_entry *ie;
4164 TAILQ_FOREACH(pe, paths, entry) {
4165 if (pe->path[0] == '\0')
4166 continue;
4167 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4168 if (ie == NULL)
4169 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4170 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4171 return got_error_path(pe->path,
4172 GOT_ERR_FILE_NOT_STAGED);
4175 return NULL;
4178 const struct got_error *
4179 got_worktree_commit(struct got_object_id **new_commit_id,
4180 struct got_worktree *worktree, struct got_pathlist_head *paths,
4181 const char *author, const char *committer,
4182 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4183 got_worktree_status_cb status_cb, void *status_arg,
4184 struct got_repository *repo)
4186 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4187 struct got_fileindex *fileindex = NULL;
4188 char *fileindex_path = NULL;
4189 struct got_pathlist_head commitable_paths;
4190 struct collect_commitables_arg cc_arg;
4191 struct got_pathlist_entry *pe;
4192 struct got_reference *head_ref = NULL;
4193 struct got_object_id *head_commit_id = NULL;
4194 int have_staged_files = 0;
4196 *new_commit_id = NULL;
4198 TAILQ_INIT(&commitable_paths);
4200 err = lock_worktree(worktree, LOCK_EX);
4201 if (err)
4202 goto done;
4204 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4205 if (err)
4206 goto done;
4208 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4209 if (err)
4210 goto done;
4212 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4213 if (err)
4214 goto done;
4216 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4217 &have_staged_files);
4218 if (err && err->code != GOT_ERR_CANCELLED)
4219 goto done;
4220 if (have_staged_files) {
4221 err = check_non_staged_files(fileindex, paths);
4222 if (err)
4223 goto done;
4226 cc_arg.commitable_paths = &commitable_paths;
4227 cc_arg.worktree = worktree;
4228 cc_arg.repo = repo;
4229 cc_arg.have_staged_files = have_staged_files;
4230 TAILQ_FOREACH(pe, paths, entry) {
4231 err = worktree_status(worktree, pe->path, fileindex, repo,
4232 collect_commitables, &cc_arg, NULL, NULL);
4233 if (err)
4234 goto done;
4237 if (TAILQ_EMPTY(&commitable_paths)) {
4238 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4239 goto done;
4242 TAILQ_FOREACH(pe, paths, entry) {
4243 err = check_path_is_commitable(pe->path, &commitable_paths);
4244 if (err)
4245 goto done;
4248 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4249 struct got_commitable *ct = pe->data;
4250 const char *ct_path = ct->in_repo_path;
4252 while (ct_path[0] == '/')
4253 ct_path++;
4254 err = check_out_of_date(ct_path, ct->status,
4255 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4256 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4257 if (err)
4258 goto done;
4262 err = commit_worktree(new_commit_id, &commitable_paths,
4263 head_commit_id, worktree, author, committer,
4264 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4265 if (err)
4266 goto done;
4268 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4269 fileindex);
4270 sync_err = sync_fileindex(fileindex, fileindex_path);
4271 if (sync_err && err == NULL)
4272 err = sync_err;
4273 done:
4274 if (fileindex)
4275 got_fileindex_free(fileindex);
4276 free(fileindex_path);
4277 unlockerr = lock_worktree(worktree, LOCK_SH);
4278 if (unlockerr && err == NULL)
4279 err = unlockerr;
4280 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4281 struct got_commitable *ct = pe->data;
4282 free_commitable(ct);
4284 got_pathlist_free(&commitable_paths);
4285 return err;
4288 const char *
4289 got_commitable_get_path(struct got_commitable *ct)
4291 return ct->path;
4294 unsigned int
4295 got_commitable_get_status(struct got_commitable *ct)
4297 return ct->status;
4300 struct check_rebase_ok_arg {
4301 struct got_worktree *worktree;
4302 struct got_repository *repo;
4305 static const struct got_error *
4306 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4308 const struct got_error *err = NULL;
4309 struct check_rebase_ok_arg *a = arg;
4310 unsigned char status;
4311 struct stat sb;
4312 char *ondisk_path;
4314 /* Reject rebase of a work tree with mixed base commits. */
4315 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4316 SHA1_DIGEST_LENGTH))
4317 return got_error(GOT_ERR_MIXED_COMMITS);
4319 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4320 == -1)
4321 return got_error_from_errno("asprintf");
4323 /* Reject rebase of a work tree with modified or staged files. */
4324 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4325 free(ondisk_path);
4326 if (err)
4327 return err;
4329 if (status != GOT_STATUS_NO_CHANGE)
4330 return got_error(GOT_ERR_MODIFIED);
4331 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4332 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4334 return NULL;
4337 const struct got_error *
4338 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4339 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4340 struct got_worktree *worktree, struct got_reference *branch,
4341 struct got_repository *repo)
4343 const struct got_error *err = NULL;
4344 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4345 char *branch_ref_name = NULL;
4346 char *fileindex_path = NULL;
4347 struct check_rebase_ok_arg ok_arg;
4348 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4350 *new_base_branch_ref = NULL;
4351 *tmp_branch = NULL;
4352 *fileindex = NULL;
4354 err = lock_worktree(worktree, LOCK_EX);
4355 if (err)
4356 return err;
4358 err = open_fileindex(fileindex, &fileindex_path, worktree);
4359 if (err)
4360 goto done;
4362 ok_arg.worktree = worktree;
4363 ok_arg.repo = repo;
4364 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4365 &ok_arg);
4366 if (err)
4367 goto done;
4369 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4370 if (err)
4371 goto done;
4373 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4374 if (err)
4375 goto done;
4377 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4378 if (err)
4379 goto done;
4381 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4382 0);
4383 if (err)
4384 goto done;
4386 err = got_ref_alloc_symref(new_base_branch_ref,
4387 new_base_branch_ref_name, wt_branch);
4388 if (err)
4389 goto done;
4390 err = got_ref_write(*new_base_branch_ref, repo);
4391 if (err)
4392 goto done;
4394 /* TODO Lock original branch's ref while rebasing? */
4396 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4397 if (err)
4398 goto done;
4400 err = got_ref_write(branch_ref, repo);
4401 if (err)
4402 goto done;
4404 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4405 worktree->base_commit_id);
4406 if (err)
4407 goto done;
4408 err = got_ref_write(*tmp_branch, repo);
4409 if (err)
4410 goto done;
4412 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4413 if (err)
4414 goto done;
4415 done:
4416 free(fileindex_path);
4417 free(tmp_branch_name);
4418 free(new_base_branch_ref_name);
4419 free(branch_ref_name);
4420 if (branch_ref)
4421 got_ref_close(branch_ref);
4422 if (wt_branch)
4423 got_ref_close(wt_branch);
4424 if (err) {
4425 if (*new_base_branch_ref) {
4426 got_ref_close(*new_base_branch_ref);
4427 *new_base_branch_ref = NULL;
4429 if (*tmp_branch) {
4430 got_ref_close(*tmp_branch);
4431 *tmp_branch = NULL;
4433 if (*fileindex) {
4434 got_fileindex_free(*fileindex);
4435 *fileindex = NULL;
4437 lock_worktree(worktree, LOCK_SH);
4439 return err;
4442 const struct got_error *
4443 got_worktree_rebase_continue(struct got_object_id **commit_id,
4444 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4445 struct got_reference **branch, struct got_fileindex **fileindex,
4446 struct got_worktree *worktree, struct got_repository *repo)
4448 const struct got_error *err;
4449 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4450 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4451 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4452 char *fileindex_path = NULL;
4453 int have_staged_files = 0;
4455 *commit_id = NULL;
4456 *new_base_branch = NULL;
4457 *tmp_branch = NULL;
4458 *branch = NULL;
4459 *fileindex = NULL;
4461 err = lock_worktree(worktree, LOCK_EX);
4462 if (err)
4463 return err;
4465 err = open_fileindex(fileindex, &fileindex_path, worktree);
4466 if (err)
4467 goto done;
4469 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4470 &have_staged_files);
4471 if (err && err->code != GOT_ERR_CANCELLED)
4472 goto done;
4473 if (have_staged_files) {
4474 err = got_error(GOT_ERR_STAGED_PATHS);
4475 goto done;
4478 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4479 if (err)
4480 goto done;
4482 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4483 if (err)
4484 goto done;
4486 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4487 if (err)
4488 goto done;
4490 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4491 if (err)
4492 goto done;
4494 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4495 if (err)
4496 goto done;
4498 err = got_ref_open(branch, repo,
4499 got_ref_get_symref_target(branch_ref), 0);
4500 if (err)
4501 goto done;
4503 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4504 if (err)
4505 goto done;
4507 err = got_ref_resolve(commit_id, repo, commit_ref);
4508 if (err)
4509 goto done;
4511 err = got_ref_open(new_base_branch, repo,
4512 new_base_branch_ref_name, 0);
4513 if (err)
4514 goto done;
4516 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4517 if (err)
4518 goto done;
4519 done:
4520 free(commit_ref_name);
4521 free(branch_ref_name);
4522 free(fileindex_path);
4523 if (commit_ref)
4524 got_ref_close(commit_ref);
4525 if (branch_ref)
4526 got_ref_close(branch_ref);
4527 if (err) {
4528 free(*commit_id);
4529 *commit_id = NULL;
4530 if (*tmp_branch) {
4531 got_ref_close(*tmp_branch);
4532 *tmp_branch = NULL;
4534 if (*new_base_branch) {
4535 got_ref_close(*new_base_branch);
4536 *new_base_branch = NULL;
4538 if (*branch) {
4539 got_ref_close(*branch);
4540 *branch = NULL;
4542 if (*fileindex) {
4543 got_fileindex_free(*fileindex);
4544 *fileindex = NULL;
4546 lock_worktree(worktree, LOCK_SH);
4548 return err;
4551 const struct got_error *
4552 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4554 const struct got_error *err;
4555 char *tmp_branch_name = NULL;
4557 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4558 if (err)
4559 return err;
4561 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4562 free(tmp_branch_name);
4563 return NULL;
4566 static const struct got_error *
4567 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4568 char **logmsg, void *arg)
4570 *logmsg = arg;
4571 return NULL;
4574 static const struct got_error *
4575 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4576 const char *path, struct got_object_id *blob_id,
4577 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4579 return NULL;
4582 struct collect_merged_paths_arg {
4583 got_worktree_checkout_cb progress_cb;
4584 void *progress_arg;
4585 struct got_pathlist_head *merged_paths;
4588 static const struct got_error *
4589 collect_merged_paths(void *arg, unsigned char status, const char *path)
4591 const struct got_error *err;
4592 struct collect_merged_paths_arg *a = arg;
4593 char *p;
4594 struct got_pathlist_entry *new;
4596 err = (*a->progress_cb)(a->progress_arg, status, path);
4597 if (err)
4598 return err;
4600 if (status != GOT_STATUS_MERGE &&
4601 status != GOT_STATUS_ADD &&
4602 status != GOT_STATUS_DELETE &&
4603 status != GOT_STATUS_CONFLICT)
4604 return NULL;
4606 p = strdup(path);
4607 if (p == NULL)
4608 return got_error_from_errno("strdup");
4610 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4611 if (err || new == NULL)
4612 free(p);
4613 return err;
4616 void
4617 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4619 struct got_pathlist_entry *pe;
4621 TAILQ_FOREACH(pe, merged_paths, entry)
4622 free((char *)pe->path);
4624 got_pathlist_free(merged_paths);
4627 static const struct got_error *
4628 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4629 struct got_repository *repo)
4631 const struct got_error *err;
4632 struct got_reference *commit_ref = NULL;
4634 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4635 if (err) {
4636 if (err->code != GOT_ERR_NOT_REF)
4637 goto done;
4638 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4639 if (err)
4640 goto done;
4641 err = got_ref_write(commit_ref, repo);
4642 if (err)
4643 goto done;
4644 } else {
4645 struct got_object_id *stored_id;
4646 int cmp;
4648 err = got_ref_resolve(&stored_id, repo, commit_ref);
4649 if (err)
4650 goto done;
4651 cmp = got_object_id_cmp(commit_id, stored_id);
4652 free(stored_id);
4653 if (cmp != 0) {
4654 err = got_error(GOT_ERR_REBASE_COMMITID);
4655 goto done;
4658 done:
4659 if (commit_ref)
4660 got_ref_close(commit_ref);
4661 return err;
4664 static const struct got_error *
4665 rebase_merge_files(struct got_pathlist_head *merged_paths,
4666 const char *commit_ref_name, struct got_worktree *worktree,
4667 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4668 struct got_object_id *commit_id, struct got_repository *repo,
4669 got_worktree_checkout_cb progress_cb, void *progress_arg,
4670 got_cancel_cb cancel_cb, void *cancel_arg)
4672 const struct got_error *err;
4673 struct got_reference *commit_ref = NULL;
4674 struct collect_merged_paths_arg cmp_arg;
4675 char *fileindex_path;
4677 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4679 err = get_fileindex_path(&fileindex_path, worktree);
4680 if (err)
4681 return err;
4683 cmp_arg.progress_cb = progress_cb;
4684 cmp_arg.progress_arg = progress_arg;
4685 cmp_arg.merged_paths = merged_paths;
4686 err = merge_files(worktree, fileindex, fileindex_path,
4687 parent_commit_id, commit_id, repo, collect_merged_paths,
4688 &cmp_arg, cancel_cb, cancel_arg);
4689 if (commit_ref)
4690 got_ref_close(commit_ref);
4691 return err;
4694 const struct got_error *
4695 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4696 struct got_worktree *worktree, struct got_fileindex *fileindex,
4697 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4698 struct got_repository *repo,
4699 got_worktree_checkout_cb progress_cb, void *progress_arg,
4700 got_cancel_cb cancel_cb, void *cancel_arg)
4702 const struct got_error *err;
4703 char *commit_ref_name;
4705 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4706 if (err)
4707 return err;
4709 err = store_commit_id(commit_ref_name, commit_id, repo);
4710 if (err)
4711 goto done;
4713 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4714 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4715 progress_arg, cancel_cb, cancel_arg);
4716 done:
4717 free(commit_ref_name);
4718 return err;
4721 const struct got_error *
4722 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4723 struct got_worktree *worktree, struct got_fileindex *fileindex,
4724 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4725 struct got_repository *repo,
4726 got_worktree_checkout_cb progress_cb, void *progress_arg,
4727 got_cancel_cb cancel_cb, void *cancel_arg)
4729 const struct got_error *err;
4730 char *commit_ref_name;
4732 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4733 if (err)
4734 return err;
4736 err = store_commit_id(commit_ref_name, commit_id, repo);
4737 if (err)
4738 goto done;
4740 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4741 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4742 progress_arg, cancel_cb, cancel_arg);
4743 done:
4744 free(commit_ref_name);
4745 return err;
4748 static const struct got_error *
4749 rebase_commit(struct got_object_id **new_commit_id,
4750 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4751 struct got_worktree *worktree, struct got_fileindex *fileindex,
4752 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4753 const char *new_logmsg, struct got_repository *repo)
4755 const struct got_error *err, *sync_err;
4756 struct got_pathlist_head commitable_paths;
4757 struct collect_commitables_arg cc_arg;
4758 char *fileindex_path = NULL;
4759 struct got_reference *head_ref = NULL;
4760 struct got_object_id *head_commit_id = NULL;
4761 char *logmsg = NULL;
4763 TAILQ_INIT(&commitable_paths);
4764 *new_commit_id = NULL;
4766 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4768 err = get_fileindex_path(&fileindex_path, worktree);
4769 if (err)
4770 return err;
4772 cc_arg.commitable_paths = &commitable_paths;
4773 cc_arg.worktree = worktree;
4774 cc_arg.repo = repo;
4775 cc_arg.have_staged_files = 0;
4777 * If possible get the status of individual files directly to
4778 * avoid crawling the entire work tree once per rebased commit.
4779 * TODO: Ideally, merged_paths would contain a list of commitables
4780 * we could use so we could skip worktree_status() entirely.
4782 if (merged_paths) {
4783 struct got_pathlist_entry *pe;
4784 if (TAILQ_EMPTY(merged_paths)) {
4785 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4786 goto done;
4788 TAILQ_FOREACH(pe, merged_paths, entry) {
4789 err = worktree_status(worktree, pe->path, fileindex,
4790 repo, collect_commitables, &cc_arg, NULL, NULL);
4791 if (err)
4792 goto done;
4794 } else {
4795 err = worktree_status(worktree, "", fileindex, repo,
4796 collect_commitables, &cc_arg, NULL, NULL);
4797 if (err)
4798 goto done;
4801 if (TAILQ_EMPTY(&commitable_paths)) {
4802 /* No-op change; commit will be elided. */
4803 err = got_ref_delete(commit_ref, repo);
4804 if (err)
4805 goto done;
4806 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4807 goto done;
4810 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4811 if (err)
4812 goto done;
4814 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4815 if (err)
4816 goto done;
4818 if (new_logmsg) {
4819 logmsg = strdup(new_logmsg);
4820 if (logmsg == NULL) {
4821 err = got_error_from_errno("strdup");
4822 goto done;
4824 } else {
4825 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4826 if (err)
4827 goto done;
4830 /* NB: commit_worktree will call free(logmsg) */
4831 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4832 worktree, got_object_commit_get_author(orig_commit),
4833 got_object_commit_get_committer(orig_commit),
4834 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4835 if (err)
4836 goto done;
4838 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4839 if (err)
4840 goto done;
4842 err = got_ref_delete(commit_ref, repo);
4843 if (err)
4844 goto done;
4846 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4847 fileindex);
4848 sync_err = sync_fileindex(fileindex, fileindex_path);
4849 if (sync_err && err == NULL)
4850 err = sync_err;
4851 done:
4852 free(fileindex_path);
4853 free(head_commit_id);
4854 if (head_ref)
4855 got_ref_close(head_ref);
4856 if (err) {
4857 free(*new_commit_id);
4858 *new_commit_id = NULL;
4860 return err;
4863 const struct got_error *
4864 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4865 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4866 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4867 struct got_commit_object *orig_commit,
4868 struct got_object_id *orig_commit_id, struct got_repository *repo)
4870 const struct got_error *err;
4871 char *commit_ref_name;
4872 struct got_reference *commit_ref = NULL;
4873 struct got_object_id *commit_id = NULL;
4875 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4876 if (err)
4877 return err;
4879 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4880 if (err)
4881 goto done;
4882 err = got_ref_resolve(&commit_id, repo, commit_ref);
4883 if (err)
4884 goto done;
4885 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4886 err = got_error(GOT_ERR_REBASE_COMMITID);
4887 goto done;
4890 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4891 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4892 done:
4893 if (commit_ref)
4894 got_ref_close(commit_ref);
4895 free(commit_ref_name);
4896 free(commit_id);
4897 return err;
4900 const struct got_error *
4901 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4902 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4903 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4904 struct got_commit_object *orig_commit,
4905 struct got_object_id *orig_commit_id, const char *new_logmsg,
4906 struct got_repository *repo)
4908 const struct got_error *err;
4909 char *commit_ref_name;
4910 struct got_reference *commit_ref = NULL;
4911 struct got_object_id *commit_id = NULL;
4913 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4914 if (err)
4915 return err;
4917 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4918 if (err)
4919 goto done;
4920 err = got_ref_resolve(&commit_id, repo, commit_ref);
4921 if (err)
4922 goto done;
4923 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4924 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4925 goto done;
4928 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4929 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4930 done:
4931 if (commit_ref)
4932 got_ref_close(commit_ref);
4933 free(commit_ref_name);
4934 free(commit_id);
4935 return err;
4938 const struct got_error *
4939 got_worktree_rebase_postpone(struct got_worktree *worktree,
4940 struct got_fileindex *fileindex)
4942 if (fileindex)
4943 got_fileindex_free(fileindex);
4944 return lock_worktree(worktree, LOCK_SH);
4947 static const struct got_error *
4948 delete_ref(const char *name, struct got_repository *repo)
4950 const struct got_error *err;
4951 struct got_reference *ref;
4953 err = got_ref_open(&ref, repo, name, 0);
4954 if (err) {
4955 if (err->code == GOT_ERR_NOT_REF)
4956 return NULL;
4957 return err;
4960 err = got_ref_delete(ref, repo);
4961 got_ref_close(ref);
4962 return err;
4965 static const struct got_error *
4966 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4968 const struct got_error *err;
4969 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4970 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4972 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4973 if (err)
4974 goto done;
4975 err = delete_ref(tmp_branch_name, repo);
4976 if (err)
4977 goto done;
4979 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4980 if (err)
4981 goto done;
4982 err = delete_ref(new_base_branch_ref_name, repo);
4983 if (err)
4984 goto done;
4986 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4987 if (err)
4988 goto done;
4989 err = delete_ref(branch_ref_name, repo);
4990 if (err)
4991 goto done;
4993 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4994 if (err)
4995 goto done;
4996 err = delete_ref(commit_ref_name, repo);
4997 if (err)
4998 goto done;
5000 done:
5001 free(tmp_branch_name);
5002 free(new_base_branch_ref_name);
5003 free(branch_ref_name);
5004 free(commit_ref_name);
5005 return err;
5008 const struct got_error *
5009 got_worktree_rebase_complete(struct got_worktree *worktree,
5010 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5011 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5012 struct got_repository *repo)
5014 const struct got_error *err, *unlockerr;
5015 struct got_object_id *new_head_commit_id = NULL;
5017 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5018 if (err)
5019 return err;
5021 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5022 if (err)
5023 goto done;
5025 err = got_ref_write(rebased_branch, repo);
5026 if (err)
5027 goto done;
5029 err = got_worktree_set_head_ref(worktree, rebased_branch);
5030 if (err)
5031 goto done;
5033 err = delete_rebase_refs(worktree, repo);
5034 done:
5035 if (fileindex)
5036 got_fileindex_free(fileindex);
5037 free(new_head_commit_id);
5038 unlockerr = lock_worktree(worktree, LOCK_SH);
5039 if (unlockerr && err == NULL)
5040 err = unlockerr;
5041 return err;
5044 const struct got_error *
5045 got_worktree_rebase_abort(struct got_worktree *worktree,
5046 struct got_fileindex *fileindex, struct got_repository *repo,
5047 struct got_reference *new_base_branch,
5048 got_worktree_checkout_cb progress_cb, void *progress_arg)
5050 const struct got_error *err, *unlockerr, *sync_err;
5051 struct got_reference *resolved = NULL;
5052 struct got_object_id *commit_id = NULL;
5053 char *fileindex_path = NULL;
5054 struct revert_file_args rfa;
5055 struct got_object_id *tree_id = NULL;
5057 err = lock_worktree(worktree, LOCK_EX);
5058 if (err)
5059 return err;
5061 err = got_ref_open(&resolved, repo,
5062 got_ref_get_symref_target(new_base_branch), 0);
5063 if (err)
5064 goto done;
5066 err = got_worktree_set_head_ref(worktree, resolved);
5067 if (err)
5068 goto done;
5071 * XXX commits to the base branch could have happened while
5072 * we were busy rebasing; should we store the original commit ID
5073 * when rebase begins and read it back here?
5075 err = got_ref_resolve(&commit_id, repo, resolved);
5076 if (err)
5077 goto done;
5079 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5080 if (err)
5081 goto done;
5083 err = got_object_id_by_path(&tree_id, repo,
5084 worktree->base_commit_id, worktree->path_prefix);
5085 if (err)
5086 goto done;
5088 err = delete_rebase_refs(worktree, repo);
5089 if (err)
5090 goto done;
5092 err = get_fileindex_path(&fileindex_path, worktree);
5093 if (err)
5094 goto done;
5096 rfa.worktree = worktree;
5097 rfa.fileindex = fileindex;
5098 rfa.progress_cb = progress_cb;
5099 rfa.progress_arg = progress_arg;
5100 rfa.patch_cb = NULL;
5101 rfa.patch_arg = NULL;
5102 rfa.repo = repo;
5103 err = worktree_status(worktree, "", fileindex, repo,
5104 revert_file, &rfa, NULL, NULL);
5105 if (err)
5106 goto sync;
5108 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5109 repo, progress_cb, progress_arg, NULL, NULL);
5110 sync:
5111 sync_err = sync_fileindex(fileindex, fileindex_path);
5112 if (sync_err && err == NULL)
5113 err = sync_err;
5114 done:
5115 got_ref_close(resolved);
5116 free(tree_id);
5117 free(commit_id);
5118 if (fileindex)
5119 got_fileindex_free(fileindex);
5120 free(fileindex_path);
5122 unlockerr = lock_worktree(worktree, LOCK_SH);
5123 if (unlockerr && err == NULL)
5124 err = unlockerr;
5125 return err;
5128 const struct got_error *
5129 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5130 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5131 struct got_fileindex **fileindex, struct got_worktree *worktree,
5132 struct got_repository *repo)
5134 const struct got_error *err = NULL;
5135 char *tmp_branch_name = NULL;
5136 char *branch_ref_name = NULL;
5137 char *base_commit_ref_name = NULL;
5138 char *fileindex_path = NULL;
5139 struct check_rebase_ok_arg ok_arg;
5140 struct got_reference *wt_branch = NULL;
5141 struct got_reference *base_commit_ref = NULL;
5143 *tmp_branch = NULL;
5144 *branch_ref = NULL;
5145 *base_commit_id = NULL;
5146 *fileindex = NULL;
5148 err = lock_worktree(worktree, LOCK_EX);
5149 if (err)
5150 return err;
5152 err = open_fileindex(fileindex, &fileindex_path, worktree);
5153 if (err)
5154 goto done;
5156 ok_arg.worktree = worktree;
5157 ok_arg.repo = repo;
5158 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5159 &ok_arg);
5160 if (err)
5161 goto done;
5163 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5164 if (err)
5165 goto done;
5167 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5168 if (err)
5169 goto done;
5171 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5172 worktree);
5173 if (err)
5174 goto done;
5176 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5177 0);
5178 if (err)
5179 goto done;
5181 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5182 if (err)
5183 goto done;
5185 err = got_ref_write(*branch_ref, repo);
5186 if (err)
5187 goto done;
5189 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5190 worktree->base_commit_id);
5191 if (err)
5192 goto done;
5193 err = got_ref_write(base_commit_ref, repo);
5194 if (err)
5195 goto done;
5196 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5197 if (*base_commit_id == NULL) {
5198 err = got_error_from_errno("got_object_id_dup");
5199 goto done;
5202 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5203 worktree->base_commit_id);
5204 if (err)
5205 goto done;
5206 err = got_ref_write(*tmp_branch, repo);
5207 if (err)
5208 goto done;
5210 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5211 if (err)
5212 goto done;
5213 done:
5214 free(fileindex_path);
5215 free(tmp_branch_name);
5216 free(branch_ref_name);
5217 free(base_commit_ref_name);
5218 if (wt_branch)
5219 got_ref_close(wt_branch);
5220 if (err) {
5221 if (*branch_ref) {
5222 got_ref_close(*branch_ref);
5223 *branch_ref = NULL;
5225 if (*tmp_branch) {
5226 got_ref_close(*tmp_branch);
5227 *tmp_branch = NULL;
5229 free(*base_commit_id);
5230 if (*fileindex) {
5231 got_fileindex_free(*fileindex);
5232 *fileindex = NULL;
5234 lock_worktree(worktree, LOCK_SH);
5236 return err;
5239 const struct got_error *
5240 got_worktree_histedit_postpone(struct got_worktree *worktree,
5241 struct got_fileindex *fileindex)
5243 if (fileindex)
5244 got_fileindex_free(fileindex);
5245 return lock_worktree(worktree, LOCK_SH);
5248 const struct got_error *
5249 got_worktree_histedit_in_progress(int *in_progress,
5250 struct got_worktree *worktree)
5252 const struct got_error *err;
5253 char *tmp_branch_name = NULL;
5255 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5256 if (err)
5257 return err;
5259 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5260 free(tmp_branch_name);
5261 return NULL;
5264 const struct got_error *
5265 got_worktree_histedit_continue(struct got_object_id **commit_id,
5266 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5267 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5268 struct got_worktree *worktree, struct got_repository *repo)
5270 const struct got_error *err;
5271 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5272 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5273 struct got_reference *commit_ref = NULL;
5274 struct got_reference *base_commit_ref = NULL;
5275 char *fileindex_path = NULL;
5276 int have_staged_files = 0;
5278 *commit_id = NULL;
5279 *tmp_branch = NULL;
5280 *base_commit_id = NULL;
5281 *fileindex = NULL;
5283 err = lock_worktree(worktree, LOCK_EX);
5284 if (err)
5285 return err;
5287 err = open_fileindex(fileindex, &fileindex_path, worktree);
5288 if (err)
5289 goto done;
5291 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5292 &have_staged_files);
5293 if (err && err->code != GOT_ERR_CANCELLED)
5294 goto done;
5295 if (have_staged_files) {
5296 err = got_error(GOT_ERR_STAGED_PATHS);
5297 goto done;
5300 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5301 if (err)
5302 goto done;
5304 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5305 if (err)
5306 goto done;
5308 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5309 if (err)
5310 goto done;
5312 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5313 worktree);
5314 if (err)
5315 goto done;
5317 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5318 if (err)
5319 goto done;
5321 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5322 if (err)
5323 goto done;
5324 err = got_ref_resolve(commit_id, repo, commit_ref);
5325 if (err)
5326 goto done;
5328 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5329 if (err)
5330 goto done;
5331 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5332 if (err)
5333 goto done;
5335 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5336 if (err)
5337 goto done;
5338 done:
5339 free(commit_ref_name);
5340 free(branch_ref_name);
5341 free(fileindex_path);
5342 if (commit_ref)
5343 got_ref_close(commit_ref);
5344 if (base_commit_ref)
5345 got_ref_close(base_commit_ref);
5346 if (err) {
5347 free(*commit_id);
5348 *commit_id = NULL;
5349 free(*base_commit_id);
5350 *base_commit_id = NULL;
5351 if (*tmp_branch) {
5352 got_ref_close(*tmp_branch);
5353 *tmp_branch = NULL;
5355 if (*fileindex) {
5356 got_fileindex_free(*fileindex);
5357 *fileindex = NULL;
5359 lock_worktree(worktree, LOCK_EX);
5361 return err;
5364 static const struct got_error *
5365 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 const struct got_error *err;
5368 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5369 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5372 if (err)
5373 goto done;
5374 err = delete_ref(tmp_branch_name, repo);
5375 if (err)
5376 goto done;
5378 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5379 worktree);
5380 if (err)
5381 goto done;
5382 err = delete_ref(base_commit_ref_name, repo);
5383 if (err)
5384 goto done;
5386 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5387 if (err)
5388 goto done;
5389 err = delete_ref(branch_ref_name, repo);
5390 if (err)
5391 goto done;
5393 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5394 if (err)
5395 goto done;
5396 err = delete_ref(commit_ref_name, repo);
5397 if (err)
5398 goto done;
5399 done:
5400 free(tmp_branch_name);
5401 free(base_commit_ref_name);
5402 free(branch_ref_name);
5403 free(commit_ref_name);
5404 return err;
5407 const struct got_error *
5408 got_worktree_histedit_abort(struct got_worktree *worktree,
5409 struct got_fileindex *fileindex, struct got_repository *repo,
5410 struct got_reference *branch, struct got_object_id *base_commit_id,
5411 got_worktree_checkout_cb progress_cb, void *progress_arg)
5413 const struct got_error *err, *unlockerr, *sync_err;
5414 struct got_reference *resolved = NULL;
5415 char *fileindex_path = NULL;
5416 struct got_object_id *tree_id = NULL;
5417 struct revert_file_args rfa;
5419 err = lock_worktree(worktree, LOCK_EX);
5420 if (err)
5421 return err;
5423 err = got_ref_open(&resolved, repo,
5424 got_ref_get_symref_target(branch), 0);
5425 if (err)
5426 goto done;
5428 err = got_worktree_set_head_ref(worktree, resolved);
5429 if (err)
5430 goto done;
5432 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5433 if (err)
5434 goto done;
5436 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5437 worktree->path_prefix);
5438 if (err)
5439 goto done;
5441 err = delete_histedit_refs(worktree, repo);
5442 if (err)
5443 goto done;
5445 err = get_fileindex_path(&fileindex_path, worktree);
5446 if (err)
5447 goto done;
5449 rfa.worktree = worktree;
5450 rfa.fileindex = fileindex;
5451 rfa.progress_cb = progress_cb;
5452 rfa.progress_arg = progress_arg;
5453 rfa.patch_cb = NULL;
5454 rfa.patch_arg = NULL;
5455 rfa.repo = repo;
5456 err = worktree_status(worktree, "", fileindex, repo,
5457 revert_file, &rfa, NULL, NULL);
5458 if (err)
5459 goto sync;
5461 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5462 repo, progress_cb, progress_arg, NULL, NULL);
5463 sync:
5464 sync_err = sync_fileindex(fileindex, fileindex_path);
5465 if (sync_err && err == NULL)
5466 err = sync_err;
5467 done:
5468 got_ref_close(resolved);
5469 free(tree_id);
5470 free(fileindex_path);
5472 unlockerr = lock_worktree(worktree, LOCK_SH);
5473 if (unlockerr && err == NULL)
5474 err = unlockerr;
5475 return err;
5478 const struct got_error *
5479 got_worktree_histedit_complete(struct got_worktree *worktree,
5480 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5481 struct got_reference *edited_branch, struct got_repository *repo)
5483 const struct got_error *err, *unlockerr;
5484 struct got_object_id *new_head_commit_id = NULL;
5485 struct got_reference *resolved = NULL;
5487 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5488 if (err)
5489 return err;
5491 err = got_ref_open(&resolved, repo,
5492 got_ref_get_symref_target(edited_branch), 0);
5493 if (err)
5494 goto done;
5496 err = got_ref_change_ref(resolved, new_head_commit_id);
5497 if (err)
5498 goto done;
5500 err = got_ref_write(resolved, repo);
5501 if (err)
5502 goto done;
5504 err = got_worktree_set_head_ref(worktree, resolved);
5505 if (err)
5506 goto done;
5508 err = delete_histedit_refs(worktree, repo);
5509 done:
5510 if (fileindex)
5511 got_fileindex_free(fileindex);
5512 free(new_head_commit_id);
5513 unlockerr = lock_worktree(worktree, LOCK_SH);
5514 if (unlockerr && err == NULL)
5515 err = unlockerr;
5516 return err;
5519 const struct got_error *
5520 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5521 struct got_object_id *commit_id, struct got_repository *repo)
5523 const struct got_error *err;
5524 char *commit_ref_name;
5526 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5527 if (err)
5528 return err;
5530 err = store_commit_id(commit_ref_name, commit_id, repo);
5531 if (err)
5532 goto done;
5534 err = delete_ref(commit_ref_name, repo);
5535 done:
5536 free(commit_ref_name);
5537 return err;
5540 struct check_stage_ok_arg {
5541 struct got_object_id *head_commit_id;
5542 struct got_worktree *worktree;
5543 struct got_fileindex *fileindex;
5544 struct got_repository *repo;
5545 int have_changes;
5548 const struct got_error *
5549 check_stage_ok(void *arg, unsigned char status,
5550 unsigned char staged_status, const char *relpath,
5551 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5552 struct got_object_id *commit_id)
5554 struct check_stage_ok_arg *a = arg;
5555 const struct got_error *err = NULL;
5556 struct got_fileindex_entry *ie;
5557 struct got_object_id base_commit_id;
5558 struct got_object_id *base_commit_idp = NULL;
5559 char *in_repo_path = NULL, *p;
5561 if (status == GOT_STATUS_UNVERSIONED)
5562 return NULL;
5564 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5565 if (ie == NULL)
5566 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5568 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5569 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5570 relpath) == -1)
5571 return got_error_from_errno("asprintf");
5573 if (got_fileindex_entry_has_commit(ie)) {
5574 memcpy(base_commit_id.sha1, ie->commit_sha1,
5575 SHA1_DIGEST_LENGTH);
5576 base_commit_idp = &base_commit_id;
5579 if (status == GOT_STATUS_NO_CHANGE) {
5580 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5581 goto done;
5582 } else if (status == GOT_STATUS_CONFLICT) {
5583 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5584 goto done;
5585 } else if (status != GOT_STATUS_ADD &&
5586 status != GOT_STATUS_MODIFY &&
5587 status != GOT_STATUS_DELETE) {
5588 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5589 goto done;
5592 a->have_changes = 1;
5594 p = in_repo_path;
5595 while (p[0] == '/')
5596 p++;
5597 err = check_out_of_date(p, status, staged_status,
5598 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5599 GOT_ERR_STAGE_OUT_OF_DATE);
5600 done:
5601 free(in_repo_path);
5602 return err;
5605 struct stage_path_arg {
5606 struct got_worktree *worktree;
5607 struct got_fileindex *fileindex;
5608 struct got_repository *repo;
5609 got_worktree_status_cb status_cb;
5610 void *status_arg;
5611 got_worktree_patch_cb patch_cb;
5612 void *patch_arg;
5615 static const struct got_error *
5616 stage_path(void *arg, unsigned char status,
5617 unsigned char staged_status, const char *relpath,
5618 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5619 struct got_object_id *commit_id)
5621 struct stage_path_arg *a = arg;
5622 const struct got_error *err = NULL;
5623 struct got_fileindex_entry *ie;
5624 char *ondisk_path = NULL, *path_content = NULL;
5625 uint32_t stage;
5626 struct got_object_id *new_staged_blob_id = NULL;
5628 if (status == GOT_STATUS_UNVERSIONED)
5629 return NULL;
5631 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5632 if (ie == NULL)
5633 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5636 relpath)== -1)
5637 return got_error_from_errno("asprintf");
5639 switch (status) {
5640 case GOT_STATUS_ADD:
5641 case GOT_STATUS_MODIFY:
5642 if (a->patch_cb) {
5643 if (status == GOT_STATUS_ADD) {
5644 int choice = GOT_PATCH_CHOICE_NONE;
5645 err = (*a->patch_cb)(&choice, a->patch_arg,
5646 status, ie->path, NULL, 1, 1);
5647 if (err)
5648 break;
5649 if (choice != GOT_PATCH_CHOICE_YES)
5650 break;
5651 } else {
5652 err = create_patched_content(&path_content, 0,
5653 staged_blob_id ? staged_blob_id : blob_id,
5654 ondisk_path, ie->path, a->repo,
5655 a->patch_cb, a->patch_arg);
5656 if (err || path_content == NULL)
5657 break;
5660 err = got_object_blob_create(&new_staged_blob_id,
5661 path_content ? path_content : ondisk_path, a->repo);
5662 if (err)
5663 break;
5664 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5665 SHA1_DIGEST_LENGTH);
5666 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5667 stage = GOT_FILEIDX_STAGE_ADD;
5668 else
5669 stage = GOT_FILEIDX_STAGE_MODIFY;
5670 got_fileindex_entry_stage_set(ie, stage);
5671 if (a->status_cb == NULL)
5672 break;
5673 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5674 get_staged_status(ie), relpath, blob_id,
5675 new_staged_blob_id, NULL);
5676 break;
5677 case GOT_STATUS_DELETE:
5678 if (staged_status == GOT_STATUS_DELETE)
5679 break;
5680 if (a->patch_cb) {
5681 int choice = GOT_PATCH_CHOICE_NONE;
5682 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5683 ie->path, NULL, 1, 1);
5684 if (err)
5685 break;
5686 if (choice == GOT_PATCH_CHOICE_NO)
5687 break;
5688 if (choice != GOT_PATCH_CHOICE_YES) {
5689 err = got_error(GOT_ERR_PATCH_CHOICE);
5690 break;
5693 stage = GOT_FILEIDX_STAGE_DELETE;
5694 got_fileindex_entry_stage_set(ie, stage);
5695 if (a->status_cb == NULL)
5696 break;
5697 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5698 get_staged_status(ie), relpath, NULL, NULL, NULL);
5699 break;
5700 case GOT_STATUS_NO_CHANGE:
5701 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5702 break;
5703 case GOT_STATUS_CONFLICT:
5704 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5705 break;
5706 default:
5707 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5708 break;
5711 if (path_content && unlink(path_content) == -1 && err == NULL)
5712 err = got_error_from_errno2("unlink", path_content);
5713 free(path_content);
5714 free(ondisk_path);
5715 free(new_staged_blob_id);
5716 return err;
5719 const struct got_error *
5720 got_worktree_stage(struct got_worktree *worktree,
5721 struct got_pathlist_head *paths,
5722 got_worktree_status_cb status_cb, void *status_arg,
5723 got_worktree_patch_cb patch_cb, void *patch_arg,
5724 struct got_repository *repo)
5726 const struct got_error *err = NULL, *sync_err, *unlockerr;
5727 struct got_pathlist_entry *pe;
5728 struct got_fileindex *fileindex = NULL;
5729 char *fileindex_path = NULL;
5730 struct got_reference *head_ref = NULL;
5731 struct got_object_id *head_commit_id = NULL;
5732 struct check_stage_ok_arg oka;
5733 struct stage_path_arg spa;
5735 err = lock_worktree(worktree, LOCK_EX);
5736 if (err)
5737 return err;
5739 err = got_ref_open(&head_ref, repo,
5740 got_worktree_get_head_ref_name(worktree), 0);
5741 if (err)
5742 goto done;
5743 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5744 if (err)
5745 goto done;
5746 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5747 if (err)
5748 goto done;
5750 /* Check pre-conditions before staging anything. */
5751 oka.head_commit_id = head_commit_id;
5752 oka.worktree = worktree;
5753 oka.fileindex = fileindex;
5754 oka.repo = repo;
5755 oka.have_changes = 0;
5756 TAILQ_FOREACH(pe, paths, entry) {
5757 err = worktree_status(worktree, pe->path, fileindex, repo,
5758 check_stage_ok, &oka, NULL, NULL);
5759 if (err)
5760 goto done;
5762 if (!oka.have_changes) {
5763 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5764 goto done;
5767 spa.worktree = worktree;
5768 spa.fileindex = fileindex;
5769 spa.repo = repo;
5770 spa.patch_cb = patch_cb;
5771 spa.patch_arg = patch_arg;
5772 spa.status_cb = status_cb;
5773 spa.status_arg = status_arg;
5774 TAILQ_FOREACH(pe, paths, entry) {
5775 err = worktree_status(worktree, pe->path, fileindex, repo,
5776 stage_path, &spa, NULL, NULL);
5777 if (err)
5778 goto done;
5781 sync_err = sync_fileindex(fileindex, fileindex_path);
5782 if (sync_err && err == NULL)
5783 err = sync_err;
5784 done:
5785 if (head_ref)
5786 got_ref_close(head_ref);
5787 free(head_commit_id);
5788 free(fileindex_path);
5789 if (fileindex)
5790 got_fileindex_free(fileindex);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5793 err = unlockerr;
5794 return err;
5797 struct unstage_path_arg {
5798 struct got_worktree *worktree;
5799 struct got_fileindex *fileindex;
5800 struct got_repository *repo;
5801 got_worktree_checkout_cb progress_cb;
5802 void *progress_arg;
5803 got_worktree_patch_cb patch_cb;
5804 void *patch_arg;
5807 static const struct got_error *
5808 create_unstaged_content(char **path_unstaged_content,
5809 char **path_new_staged_content, struct got_object_id *blob_id,
5810 struct got_object_id *staged_blob_id, const char *relpath,
5811 struct got_repository *repo,
5812 got_worktree_patch_cb patch_cb, void *patch_arg)
5814 const struct got_error *err;
5815 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5816 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5817 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5818 struct stat sb1, sb2;
5819 struct got_diff_changes *changes = NULL;
5820 struct got_diff_state *ds = NULL;
5821 struct got_diff_args *args = NULL;
5822 struct got_diff_change *change;
5823 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5824 int have_content = 0, have_rejected_content = 0;
5826 *path_unstaged_content = NULL;
5827 *path_new_staged_content = NULL;
5829 err = got_object_id_str(&label1, blob_id);
5830 if (err)
5831 return err;
5832 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5833 if (err)
5834 goto done;
5836 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5837 if (err)
5838 goto done;
5840 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5841 if (err)
5842 goto done;
5844 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5845 if (err)
5846 goto done;
5848 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5849 if (err)
5850 goto done;
5852 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5853 if (err)
5854 goto done;
5856 if (stat(path1, &sb1) == -1) {
5857 err = got_error_from_errno2("stat", path1);
5858 goto done;
5861 if (stat(path2, &sb2) == -1) {
5862 err = got_error_from_errno2("stat", path2);
5863 goto done;
5866 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5867 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5868 if (err)
5869 goto done;
5871 err = got_opentemp_named(path_unstaged_content, &outfile,
5872 "got-unstaged-content");
5873 if (err)
5874 goto done;
5875 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5876 "got-new-staged-content");
5877 if (err)
5878 goto done;
5880 if (fseek(f1, 0L, SEEK_SET) == -1) {
5881 err = got_ferror(f1, GOT_ERR_IO);
5882 goto done;
5884 if (fseek(f2, 0L, SEEK_SET) == -1) {
5885 err = got_ferror(f2, GOT_ERR_IO);
5886 goto done;
5888 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5889 int choice;
5890 err = apply_or_reject_change(&choice, change, ++n,
5891 changes->nchanges, ds, args, diff_flags, relpath,
5892 f1, f2, &line_cur1, &line_cur2,
5893 outfile, rejectfile, patch_cb, patch_arg);
5894 if (err)
5895 goto done;
5896 if (choice == GOT_PATCH_CHOICE_YES)
5897 have_content = 1;
5898 else
5899 have_rejected_content = 1;
5900 if (choice == GOT_PATCH_CHOICE_QUIT)
5901 break;
5903 if (have_content || have_rejected_content)
5904 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5905 outfile, rejectfile);
5906 done:
5907 free(label1);
5908 if (blob)
5909 got_object_blob_close(blob);
5910 if (staged_blob)
5911 got_object_blob_close(staged_blob);
5912 if (f1 && fclose(f1) == EOF && err == NULL)
5913 err = got_error_from_errno2("fclose", path1);
5914 if (f2 && fclose(f2) == EOF && err == NULL)
5915 err = got_error_from_errno2("fclose", path2);
5916 if (outfile && fclose(outfile) == EOF && err == NULL)
5917 err = got_error_from_errno2("fclose", *path_unstaged_content);
5918 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5919 err = got_error_from_errno2("fclose", *path_new_staged_content);
5920 if (path1 && unlink(path1) == -1 && err == NULL)
5921 err = got_error_from_errno2("unlink", path1);
5922 if (path2 && unlink(path2) == -1 && err == NULL)
5923 err = got_error_from_errno2("unlink", path2);
5924 if (err || !have_content) {
5925 if (*path_unstaged_content &&
5926 unlink(*path_unstaged_content) == -1 && err == NULL)
5927 err = got_error_from_errno2("unlink",
5928 *path_unstaged_content);
5929 free(*path_unstaged_content);
5930 *path_unstaged_content = NULL;
5932 if (err || !have_rejected_content) {
5933 if (*path_new_staged_content &&
5934 unlink(*path_new_staged_content) == -1 && err == NULL)
5935 err = got_error_from_errno2("unlink",
5936 *path_new_staged_content);
5937 free(*path_new_staged_content);
5938 *path_new_staged_content = NULL;
5940 free(args);
5941 if (ds) {
5942 got_diff_state_free(ds);
5943 free(ds);
5945 if (changes)
5946 got_diff_free_changes(changes);
5947 free(path1);
5948 free(path2);
5949 return err;
5952 static const struct got_error *
5953 unstage_path(void *arg, unsigned char status,
5954 unsigned char staged_status, const char *relpath,
5955 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5956 struct got_object_id *commit_id)
5958 const struct got_error *err = NULL;
5959 struct unstage_path_arg *a = arg;
5960 struct got_fileindex_entry *ie;
5961 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5962 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5963 char *path_new_staged_content = NULL;
5964 int local_changes_subsumed;
5965 struct stat sb;
5967 if (staged_status != GOT_STATUS_ADD &&
5968 staged_status != GOT_STATUS_MODIFY &&
5969 staged_status != GOT_STATUS_DELETE)
5970 return NULL;
5972 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5973 if (ie == NULL)
5974 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5977 == -1)
5978 return got_error_from_errno("asprintf");
5980 switch (staged_status) {
5981 case GOT_STATUS_MODIFY:
5982 err = got_object_open_as_blob(&blob_base, a->repo,
5983 blob_id, 8192);
5984 if (err)
5985 break;
5986 /* fall through */
5987 case GOT_STATUS_ADD:
5988 if (a->patch_cb) {
5989 if (staged_status == GOT_STATUS_ADD) {
5990 int choice = GOT_PATCH_CHOICE_NONE;
5991 err = (*a->patch_cb)(&choice, a->patch_arg,
5992 staged_status, ie->path, NULL, 1, 1);
5993 if (err)
5994 break;
5995 if (choice != GOT_PATCH_CHOICE_YES)
5996 break;
5997 } else {
5998 err = create_unstaged_content(
5999 &path_unstaged_content,
6000 &path_new_staged_content, blob_id,
6001 staged_blob_id, ie->path, a->repo,
6002 a->patch_cb, a->patch_arg);
6003 if (err || path_unstaged_content == NULL)
6004 break;
6005 if (path_new_staged_content) {
6006 err = got_object_blob_create(
6007 &staged_blob_id,
6008 path_new_staged_content,
6009 a->repo);
6010 if (err)
6011 break;
6012 memcpy(ie->staged_blob_sha1,
6013 staged_blob_id->sha1,
6014 SHA1_DIGEST_LENGTH);
6016 err = merge_file(&local_changes_subsumed,
6017 a->worktree, blob_base, ondisk_path,
6018 relpath, got_fileindex_perms_to_st(ie),
6019 path_unstaged_content, "unstaged",
6020 a->repo, a->progress_cb, a->progress_arg);
6021 if (err == NULL &&
6022 path_new_staged_content == NULL)
6023 got_fileindex_entry_stage_set(ie,
6024 GOT_FILEIDX_STAGE_NONE);
6025 break; /* Done with this file. */
6028 err = got_object_open_as_blob(&blob_staged, a->repo,
6029 staged_blob_id, 8192);
6030 if (err)
6031 break;
6032 err = merge_blob(&local_changes_subsumed, a->worktree,
6033 blob_base, ondisk_path, relpath,
6034 got_fileindex_perms_to_st(ie), blob_staged,
6035 commit_id ? commit_id : a->worktree->base_commit_id,
6036 a->repo, a->progress_cb, a->progress_arg);
6037 if (err == NULL)
6038 got_fileindex_entry_stage_set(ie,
6039 GOT_FILEIDX_STAGE_NONE);
6040 break;
6041 case GOT_STATUS_DELETE:
6042 if (a->patch_cb) {
6043 int choice = GOT_PATCH_CHOICE_NONE;
6044 err = (*a->patch_cb)(&choice, a->patch_arg,
6045 staged_status, ie->path, NULL, 1, 1);
6046 if (err)
6047 break;
6048 if (choice == GOT_PATCH_CHOICE_NO)
6049 break;
6050 if (choice != GOT_PATCH_CHOICE_YES) {
6051 err = got_error(GOT_ERR_PATCH_CHOICE);
6052 break;
6055 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6056 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6057 if (err)
6058 break;
6059 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6060 break;
6063 free(ondisk_path);
6064 if (path_unstaged_content &&
6065 unlink(path_unstaged_content) == -1 && err == NULL)
6066 err = got_error_from_errno2("unlink", path_unstaged_content);
6067 if (path_new_staged_content &&
6068 unlink(path_new_staged_content) == -1 && err == NULL)
6069 err = got_error_from_errno2("unlink", path_new_staged_content);
6070 free(path_unstaged_content);
6071 free(path_new_staged_content);
6072 if (blob_base)
6073 got_object_blob_close(blob_base);
6074 if (blob_staged)
6075 got_object_blob_close(blob_staged);
6076 return err;
6079 const struct got_error *
6080 got_worktree_unstage(struct got_worktree *worktree,
6081 struct got_pathlist_head *paths,
6082 got_worktree_checkout_cb progress_cb, void *progress_arg,
6083 got_worktree_patch_cb patch_cb, void *patch_arg,
6084 struct got_repository *repo)
6086 const struct got_error *err = NULL, *sync_err, *unlockerr;
6087 struct got_pathlist_entry *pe;
6088 struct got_fileindex *fileindex = NULL;
6089 char *fileindex_path = NULL;
6090 struct unstage_path_arg upa;
6092 err = lock_worktree(worktree, LOCK_EX);
6093 if (err)
6094 return err;
6096 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6097 if (err)
6098 goto done;
6100 upa.worktree = worktree;
6101 upa.fileindex = fileindex;
6102 upa.repo = repo;
6103 upa.progress_cb = progress_cb;
6104 upa.progress_arg = progress_arg;
6105 upa.patch_cb = patch_cb;
6106 upa.patch_arg = patch_arg;
6107 TAILQ_FOREACH(pe, paths, entry) {
6108 err = worktree_status(worktree, pe->path, fileindex, repo,
6109 unstage_path, &upa, NULL, NULL);
6110 if (err)
6111 goto done;
6114 sync_err = sync_fileindex(fileindex, fileindex_path);
6115 if (sync_err && err == NULL)
6116 err = sync_err;
6117 done:
6118 free(fileindex_path);
6119 if (fileindex)
6120 got_fileindex_free(fileindex);
6121 unlockerr = lock_worktree(worktree, LOCK_SH);
6122 if (unlockerr && err == NULL)
6123 err = unlockerr;
6124 return err;