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 (got_object_tree_entry_is_submodule(te))
1430 return NULL;
1432 if (asprintf(&path, "%s%s%s", parent_path,
1433 parent_path[0] ? "/" : "", te->name)
1434 == -1)
1435 return got_error_from_errno("asprintf");
1437 if (S_ISDIR(te->mode))
1438 err = add_dir_on_disk(a->worktree, path);
1439 else
1440 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1441 a->repo, a->progress_cb, a->progress_arg);
1443 free(path);
1444 return err;
1447 static const struct got_error *
1448 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1450 const struct got_error *err = NULL;
1451 char *uuidstr = NULL;
1452 uint32_t uuid_status;
1454 *refname = NULL;
1456 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1457 if (uuid_status != uuid_s_ok)
1458 return got_error_uuid(uuid_status);
1460 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1461 == -1) {
1462 err = got_error_from_errno("asprintf");
1463 *refname = NULL;
1465 free(uuidstr);
1466 return err;
1469 const struct got_error *
1470 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1475 static const struct got_error *
1476 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1478 return get_ref_name(refname, worktree,
1479 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1482 static const struct got_error *
1483 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1485 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1488 static const struct got_error *
1489 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1491 return get_ref_name(refname, worktree,
1492 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1495 static const struct got_error *
1496 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1498 return get_ref_name(refname, worktree,
1499 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1502 static const struct got_error *
1503 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1505 return get_ref_name(refname, worktree,
1506 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1509 static const struct got_error *
1510 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1512 return get_ref_name(refname, worktree,
1513 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1516 static const struct got_error *
1517 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1519 return get_ref_name(refname, worktree,
1520 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1523 static const struct got_error *
1524 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1530 const struct got_error *
1531 got_worktree_get_histedit_script_path(char **path,
1532 struct got_worktree *worktree)
1534 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1535 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1536 *path = NULL;
1537 return got_error_from_errno("asprintf");
1539 return NULL;
1543 * Prevent Git's garbage collector from deleting our base commit by
1544 * setting a reference to our base commit's ID.
1546 static const struct got_error *
1547 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_reference *ref = NULL;
1551 char *refname;
1553 err = got_worktree_get_base_ref_name(&refname, worktree);
1554 if (err)
1555 return err;
1557 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1558 if (err)
1559 goto done;
1561 err = got_ref_write(ref, repo);
1562 done:
1563 free(refname);
1564 if (ref)
1565 got_ref_close(ref);
1566 return err;
1569 static const struct got_error *
1570 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1572 const struct got_error *err = NULL;
1574 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1575 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 *fileindex_path = NULL;
1579 return err;
1583 static const struct got_error *
1584 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1585 struct got_worktree *worktree)
1587 const struct got_error *err = NULL;
1588 FILE *index = NULL;
1590 *fileindex_path = NULL;
1591 *fileindex = got_fileindex_alloc();
1592 if (*fileindex == NULL)
1593 return got_error_from_errno("got_fileindex_alloc");
1595 err = get_fileindex_path(fileindex_path, worktree);
1596 if (err)
1597 goto done;
1599 index = fopen(*fileindex_path, "rb");
1600 if (index == NULL) {
1601 if (errno != ENOENT)
1602 err = got_error_from_errno2("fopen", *fileindex_path);
1603 } else {
1604 err = got_fileindex_read(*fileindex, index);
1605 if (fclose(index) != 0 && err == NULL)
1606 err = got_error_from_errno("fclose");
1608 done:
1609 if (err) {
1610 free(*fileindex_path);
1611 *fileindex_path = NULL;
1612 got_fileindex_free(*fileindex);
1613 *fileindex = NULL;
1615 return err;
1618 struct bump_base_commit_id_arg {
1619 struct got_object_id *base_commit_id;
1620 const char *path;
1621 size_t path_len;
1622 const char *entry_name;
1623 got_worktree_checkout_cb progress_cb;
1624 void *progress_arg;
1627 /* Bump base commit ID of all files within an updated part of the work tree. */
1628 static const struct got_error *
1629 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1631 const struct got_error *err;
1632 struct bump_base_commit_id_arg *a = arg;
1634 if (a->entry_name) {
1635 if (strcmp(ie->path, a->path) != 0)
1636 return NULL;
1637 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1638 return NULL;
1640 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1641 SHA1_DIGEST_LENGTH) == 0)
1642 return NULL;
1644 if (a->progress_cb) {
1645 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1646 ie->path);
1647 if (err)
1648 return err;
1650 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1651 return NULL;
1654 static const struct got_error *
1655 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1657 const struct got_error *err = NULL;
1658 char *new_fileindex_path = NULL;
1659 FILE *new_index = NULL;
1661 err = got_opentemp_named(&new_fileindex_path, &new_index,
1662 fileindex_path);
1663 if (err)
1664 goto done;
1666 err = got_fileindex_write(fileindex, new_index);
1667 if (err)
1668 goto done;
1670 if (rename(new_fileindex_path, fileindex_path) != 0) {
1671 err = got_error_from_errno3("rename", new_fileindex_path,
1672 fileindex_path);
1673 unlink(new_fileindex_path);
1675 done:
1676 if (new_index)
1677 fclose(new_index);
1678 free(new_fileindex_path);
1679 return err;
1682 static const struct got_error *
1683 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1684 struct got_object_id **tree_id, const char *wt_relpath,
1685 struct got_worktree *worktree, struct got_repository *repo)
1687 const struct got_error *err = NULL;
1688 struct got_object_id *id = NULL;
1689 char *in_repo_path = NULL;
1690 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1692 *entry_type = GOT_OBJ_TYPE_ANY;
1693 *tree_relpath = NULL;
1694 *tree_id = NULL;
1696 if (wt_relpath[0] == '\0') {
1697 /* Check out all files within the work tree. */
1698 *entry_type = GOT_OBJ_TYPE_TREE;
1699 *tree_relpath = strdup("");
1700 if (*tree_relpath == NULL) {
1701 err = got_error_from_errno("strdup");
1702 goto done;
1704 err = got_object_id_by_path(tree_id, repo,
1705 worktree->base_commit_id, worktree->path_prefix);
1706 if (err)
1707 goto done;
1708 return NULL;
1711 /* Check out a subset of files in the work tree. */
1713 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1714 is_root_wt ? "" : "/", wt_relpath) == -1) {
1715 err = got_error_from_errno("asprintf");
1716 goto done;
1719 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1720 in_repo_path);
1721 if (err)
1722 goto done;
1724 free(in_repo_path);
1725 in_repo_path = NULL;
1727 err = got_object_get_type(entry_type, repo, id);
1728 if (err)
1729 goto done;
1731 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1732 /* Check out a single file. */
1733 if (strchr(wt_relpath, '/') == NULL) {
1734 /* Check out a single file in work tree's root dir. */
1735 in_repo_path = strdup(worktree->path_prefix);
1736 if (in_repo_path == NULL) {
1737 err = got_error_from_errno("strdup");
1738 goto done;
1740 *tree_relpath = strdup("");
1741 if (*tree_relpath == NULL) {
1742 err = got_error_from_errno("strdup");
1743 goto done;
1745 } else {
1746 /* Check out a single file in a subdirectory. */
1747 err = got_path_dirname(tree_relpath, wt_relpath);
1748 if (err)
1749 return err;
1750 if (asprintf(&in_repo_path, "%s%s%s",
1751 worktree->path_prefix, is_root_wt ? "" : "/",
1752 *tree_relpath) == -1) {
1753 err = got_error_from_errno("asprintf");
1754 goto done;
1757 err = got_object_id_by_path(tree_id, repo,
1758 worktree->base_commit_id, in_repo_path);
1759 } else {
1760 /* Check out all files within a subdirectory. */
1761 *tree_id = got_object_id_dup(id);
1762 if (*tree_id == NULL) {
1763 err = got_error_from_errno("got_object_id_dup");
1764 goto done;
1766 *tree_relpath = strdup(wt_relpath);
1767 if (*tree_relpath == NULL) {
1768 err = got_error_from_errno("strdup");
1769 goto done;
1772 done:
1773 free(id);
1774 free(in_repo_path);
1775 if (err) {
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 free(*tree_relpath);
1778 *tree_relpath = NULL;
1779 free(*tree_id);
1780 *tree_id = NULL;
1782 return err;
1785 static const struct got_error *
1786 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1787 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1788 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1789 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1791 const struct got_error *err = NULL;
1792 struct got_commit_object *commit = NULL;
1793 struct got_tree_object *tree = NULL;
1794 struct got_fileindex_diff_tree_cb diff_cb;
1795 struct diff_cb_arg arg;
1797 err = ref_base_commit(worktree, repo);
1798 if (err)
1799 goto done;
1801 err = got_object_open_as_commit(&commit, repo,
1802 worktree->base_commit_id);
1803 if (err)
1804 goto done;
1806 err = got_object_open_as_tree(&tree, repo, tree_id);
1807 if (err)
1808 goto done;
1810 if (entry_name &&
1811 got_object_tree_find_entry(tree, entry_name) == NULL) {
1812 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1813 goto done;
1816 diff_cb.diff_old_new = diff_old_new;
1817 diff_cb.diff_old = diff_old;
1818 diff_cb.diff_new = diff_new;
1819 arg.fileindex = fileindex;
1820 arg.worktree = worktree;
1821 arg.repo = repo;
1822 arg.progress_cb = progress_cb;
1823 arg.progress_arg = progress_arg;
1824 arg.cancel_cb = cancel_cb;
1825 arg.cancel_arg = cancel_arg;
1826 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1827 entry_name, repo, &diff_cb, &arg);
1828 done:
1829 if (tree)
1830 got_object_tree_close(tree);
1831 if (commit)
1832 got_object_commit_close(commit);
1833 return err;
1836 const struct got_error *
1837 got_worktree_checkout_files(struct got_worktree *worktree,
1838 struct got_pathlist_head *paths, struct got_repository *repo,
1839 got_worktree_checkout_cb progress_cb, void *progress_arg,
1840 got_cancel_cb cancel_cb, void *cancel_arg)
1842 const struct got_error *err = NULL, *sync_err, *unlockerr;
1843 struct got_commit_object *commit = NULL;
1844 struct got_tree_object *tree = NULL;
1845 struct got_fileindex *fileindex = NULL;
1846 char *fileindex_path = NULL;
1847 struct got_pathlist_entry *pe;
1848 struct tree_path_data {
1849 SIMPLEQ_ENTRY(tree_path_data) entry;
1850 struct got_object_id *tree_id;
1851 int entry_type;
1852 char *relpath;
1853 char *entry_name;
1854 } *tpd = NULL;
1855 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1857 SIMPLEQ_INIT(&tree_paths);
1859 err = lock_worktree(worktree, LOCK_EX);
1860 if (err)
1861 return err;
1863 /* Map all specified paths to in-repository trees. */
1864 TAILQ_FOREACH(pe, paths, entry) {
1865 tpd = malloc(sizeof(*tpd));
1866 if (tpd == NULL) {
1867 err = got_error_from_errno("malloc");
1868 goto done;
1871 err = find_tree_entry_for_checkout(&tpd->entry_type,
1872 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1873 if (err) {
1874 free(tpd);
1875 goto done;
1878 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1879 err = got_path_basename(&tpd->entry_name, pe->path);
1880 if (err) {
1881 free(tpd->relpath);
1882 free(tpd->tree_id);
1883 free(tpd);
1884 goto done;
1886 } else
1887 tpd->entry_name = NULL;
1889 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1893 * Read the file index.
1894 * Checking out files is supposed to be an idempotent operation.
1895 * If the on-disk file index is incomplete we will try to complete it.
1897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1898 if (err)
1899 goto done;
1901 tpd = SIMPLEQ_FIRST(&tree_paths);
1902 TAILQ_FOREACH(pe, paths, entry) {
1903 struct bump_base_commit_id_arg bbc_arg;
1905 err = checkout_files(worktree, fileindex, tpd->relpath,
1906 tpd->tree_id, tpd->entry_name, repo,
1907 progress_cb, progress_arg, cancel_cb, cancel_arg);
1908 if (err)
1909 break;
1911 bbc_arg.base_commit_id = worktree->base_commit_id;
1912 bbc_arg.entry_name = tpd->entry_name;
1913 bbc_arg.path = pe->path;
1914 bbc_arg.path_len = pe->path_len;
1915 bbc_arg.progress_cb = progress_cb;
1916 bbc_arg.progress_arg = progress_arg;
1917 err = got_fileindex_for_each_entry_safe(fileindex,
1918 bump_base_commit_id, &bbc_arg);
1919 if (err)
1920 break;
1922 tpd = SIMPLEQ_NEXT(tpd, entry);
1924 sync_err = sync_fileindex(fileindex, fileindex_path);
1925 if (sync_err && err == NULL)
1926 err = sync_err;
1927 done:
1928 free(fileindex_path);
1929 if (tree)
1930 got_object_tree_close(tree);
1931 if (commit)
1932 got_object_commit_close(commit);
1933 if (fileindex)
1934 got_fileindex_free(fileindex);
1935 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1936 tpd = SIMPLEQ_FIRST(&tree_paths);
1937 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1938 free(tpd->relpath);
1939 free(tpd->tree_id);
1940 free(tpd);
1942 unlockerr = lock_worktree(worktree, LOCK_SH);
1943 if (unlockerr && err == NULL)
1944 err = unlockerr;
1945 return err;
1948 struct merge_file_cb_arg {
1949 struct got_worktree *worktree;
1950 struct got_fileindex *fileindex;
1951 got_worktree_checkout_cb progress_cb;
1952 void *progress_arg;
1953 got_cancel_cb cancel_cb;
1954 void *cancel_arg;
1955 struct got_object_id *commit_id2;
1958 static const struct got_error *
1959 merge_file_cb(void *arg, struct got_blob_object *blob1,
1960 struct got_blob_object *blob2, struct got_object_id *id1,
1961 struct got_object_id *id2, const char *path1, const char *path2,
1962 struct got_repository *repo)
1964 static const struct got_error *err = NULL;
1965 struct merge_file_cb_arg *a = arg;
1966 struct got_fileindex_entry *ie;
1967 char *ondisk_path = NULL;
1968 struct stat sb;
1969 unsigned char status;
1970 int local_changes_subsumed;
1972 if (blob1 && blob2) {
1973 ie = got_fileindex_entry_get(a->fileindex, path2,
1974 strlen(path2));
1975 if (ie == NULL)
1976 return (*a->progress_cb)(a->progress_arg,
1977 GOT_STATUS_MISSING, path2);
1979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1980 path2) == -1)
1981 return got_error_from_errno("asprintf");
1983 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1984 if (err)
1985 goto done;
1987 if (status == GOT_STATUS_DELETE) {
1988 err = (*a->progress_cb)(a->progress_arg,
1989 GOT_STATUS_MERGE, path2);
1990 goto done;
1992 if (status != GOT_STATUS_NO_CHANGE &&
1993 status != GOT_STATUS_MODIFY &&
1994 status != GOT_STATUS_CONFLICT &&
1995 status != GOT_STATUS_ADD) {
1996 err = (*a->progress_cb)(a->progress_arg, status, path2);
1997 goto done;
2000 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2001 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2002 a->progress_cb, a->progress_arg);
2003 } else if (blob1) {
2004 ie = got_fileindex_entry_get(a->fileindex, path1,
2005 strlen(path1));
2006 if (ie == NULL)
2007 return (*a->progress_cb)(a->progress_arg,
2008 GOT_STATUS_MISSING, path2);
2010 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2011 path1) == -1)
2012 return got_error_from_errno("asprintf");
2014 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2015 if (err)
2016 goto done;
2018 switch (status) {
2019 case GOT_STATUS_NO_CHANGE:
2020 err = (*a->progress_cb)(a->progress_arg,
2021 GOT_STATUS_DELETE, path1);
2022 if (err)
2023 goto done;
2024 err = remove_ondisk_file(a->worktree->root_path, path1);
2025 if (err)
2026 goto done;
2027 if (ie)
2028 got_fileindex_entry_mark_deleted_from_disk(ie);
2029 break;
2030 case GOT_STATUS_DELETE:
2031 case GOT_STATUS_MISSING:
2032 err = (*a->progress_cb)(a->progress_arg,
2033 GOT_STATUS_DELETE, path1);
2034 if (err)
2035 goto done;
2036 if (ie)
2037 got_fileindex_entry_mark_deleted_from_disk(ie);
2038 break;
2039 case GOT_STATUS_ADD:
2040 case GOT_STATUS_MODIFY:
2041 case GOT_STATUS_CONFLICT:
2042 err = (*a->progress_cb)(a->progress_arg,
2043 GOT_STATUS_CANNOT_DELETE, path1);
2044 if (err)
2045 goto done;
2046 break;
2047 case GOT_STATUS_OBSTRUCTED:
2048 err = (*a->progress_cb)(a->progress_arg, status, path1);
2049 if (err)
2050 goto done;
2051 break;
2052 default:
2053 break;
2055 } else if (blob2) {
2056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2057 path2) == -1)
2058 return got_error_from_errno("asprintf");
2059 ie = got_fileindex_entry_get(a->fileindex, path2,
2060 strlen(path2));
2061 if (ie) {
2062 err = get_file_status(&status, &sb, ie, ondisk_path,
2063 repo);
2064 if (err)
2065 goto done;
2066 if (status != GOT_STATUS_NO_CHANGE &&
2067 status != GOT_STATUS_MODIFY &&
2068 status != GOT_STATUS_CONFLICT &&
2069 status != GOT_STATUS_ADD) {
2070 err = (*a->progress_cb)(a->progress_arg,
2071 status, path2);
2072 goto done;
2074 err = merge_blob(&local_changes_subsumed, a->worktree,
2075 NULL, ondisk_path, path2, sb.st_mode, blob2,
2076 a->commit_id2, repo,
2077 a->progress_cb, a->progress_arg);
2078 if (status == GOT_STATUS_DELETE) {
2079 err = update_blob_fileindex_entry(a->worktree,
2080 a->fileindex, ie, ondisk_path, ie->path,
2081 blob2, 0);
2082 if (err)
2083 goto done;
2085 } else {
2086 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2087 err = install_blob(a->worktree, ondisk_path, path2,
2088 /* XXX get this from parent tree! */
2089 GOT_DEFAULT_FILE_MODE,
2090 sb.st_mode, blob2, 0, 0, repo,
2091 a->progress_cb, a->progress_arg);
2092 if (err)
2093 goto done;
2094 err = got_fileindex_entry_alloc(&ie,
2095 ondisk_path, path2, NULL, NULL);
2096 if (err)
2097 goto done;
2098 err = got_fileindex_entry_add(a->fileindex, ie);
2099 if (err) {
2100 got_fileindex_entry_free(ie);
2101 goto done;
2105 done:
2106 free(ondisk_path);
2107 return err;
2110 struct check_merge_ok_arg {
2111 struct got_worktree *worktree;
2112 struct got_repository *repo;
2115 static const struct got_error *
2116 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2118 const struct got_error *err = NULL;
2119 struct check_merge_ok_arg *a = arg;
2120 unsigned char status;
2121 struct stat sb;
2122 char *ondisk_path;
2124 /* Reject merges into a work tree with mixed base commits. */
2125 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2126 SHA1_DIGEST_LENGTH))
2127 return got_error(GOT_ERR_MIXED_COMMITS);
2129 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2130 == -1)
2131 return got_error_from_errno("asprintf");
2133 /* Reject merges into a work tree with conflicted files. */
2134 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2135 if (err)
2136 return err;
2137 if (status == GOT_STATUS_CONFLICT)
2138 return got_error(GOT_ERR_CONFLICTS);
2140 return NULL;
2143 static const struct got_error *
2144 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2145 const char *fileindex_path, struct got_object_id *commit_id1,
2146 struct got_object_id *commit_id2, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg,
2148 got_cancel_cb cancel_cb, void *cancel_arg)
2150 const struct got_error *err = NULL, *sync_err;
2151 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2152 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2153 struct merge_file_cb_arg arg;
2155 if (commit_id1) {
2156 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2157 worktree->path_prefix);
2158 if (err)
2159 goto done;
2161 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2162 if (err)
2163 goto done;
2166 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2167 worktree->path_prefix);
2168 if (err)
2169 goto done;
2171 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2172 if (err)
2173 goto done;
2175 arg.worktree = worktree;
2176 arg.fileindex = fileindex;
2177 arg.progress_cb = progress_cb;
2178 arg.progress_arg = progress_arg;
2179 arg.cancel_cb = cancel_cb;
2180 arg.cancel_arg = cancel_arg;
2181 arg.commit_id2 = commit_id2;
2182 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2183 sync_err = sync_fileindex(fileindex, fileindex_path);
2184 if (sync_err && err == NULL)
2185 err = sync_err;
2186 done:
2187 if (tree1)
2188 got_object_tree_close(tree1);
2189 if (tree2)
2190 got_object_tree_close(tree2);
2191 return err;
2194 const struct got_error *
2195 got_worktree_merge_files(struct got_worktree *worktree,
2196 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2197 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2198 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err, *unlockerr;
2201 char *fileindex_path = NULL;
2202 struct got_fileindex *fileindex = NULL;
2203 struct check_merge_ok_arg mok_arg;
2205 err = lock_worktree(worktree, LOCK_EX);
2206 if (err)
2207 return err;
2209 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2210 if (err)
2211 goto done;
2213 mok_arg.worktree = worktree;
2214 mok_arg.repo = repo;
2215 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2216 &mok_arg);
2217 if (err)
2218 goto done;
2220 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2221 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2222 done:
2223 if (fileindex)
2224 got_fileindex_free(fileindex);
2225 free(fileindex_path);
2226 unlockerr = lock_worktree(worktree, LOCK_SH);
2227 if (unlockerr && err == NULL)
2228 err = unlockerr;
2229 return err;
2232 struct diff_dir_cb_arg {
2233 struct got_fileindex *fileindex;
2234 struct got_worktree *worktree;
2235 const char *status_path;
2236 size_t status_path_len;
2237 struct got_repository *repo;
2238 got_worktree_status_cb status_cb;
2239 void *status_arg;
2240 got_cancel_cb cancel_cb;
2241 void *cancel_arg;
2242 /* A pathlist containing per-directory pathlists of ignore patterns. */
2243 struct got_pathlist_head ignores;
2246 static const struct got_error *
2247 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2248 got_worktree_status_cb status_cb, void *status_arg,
2249 struct got_repository *repo)
2251 const struct got_error *err = NULL;
2252 unsigned char status = GOT_STATUS_NO_CHANGE;
2253 unsigned char staged_status = get_staged_status(ie);
2254 struct stat sb;
2255 struct got_object_id blob_id, commit_id, staged_blob_id;
2256 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2257 struct got_object_id *staged_blob_idp = NULL;
2259 err = get_file_status(&status, &sb, ie, abspath, repo);
2260 if (err)
2261 return err;
2263 if (status == GOT_STATUS_NO_CHANGE &&
2264 staged_status == GOT_STATUS_NO_CHANGE)
2265 return NULL;
2267 if (got_fileindex_entry_has_blob(ie)) {
2268 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2269 blob_idp = &blob_id;
2271 if (got_fileindex_entry_has_commit(ie)) {
2272 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2273 commit_idp = &commit_id;
2275 if (staged_status == GOT_STATUS_ADD ||
2276 staged_status == GOT_STATUS_MODIFY) {
2277 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2278 SHA1_DIGEST_LENGTH);
2279 staged_blob_idp = &staged_blob_id;
2282 return (*status_cb)(status_arg, status, staged_status,
2283 ie->path, blob_idp, staged_blob_idp, commit_idp);
2286 static const struct got_error *
2287 status_old_new(void *arg, struct got_fileindex_entry *ie,
2288 struct dirent *de, const char *parent_path)
2290 const struct got_error *err = NULL;
2291 struct diff_dir_cb_arg *a = arg;
2292 char *abspath;
2294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2295 return got_error(GOT_ERR_CANCELLED);
2297 if (got_path_cmp(parent_path, a->status_path,
2298 strlen(parent_path), a->status_path_len) != 0 &&
2299 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2300 return NULL;
2302 if (parent_path[0]) {
2303 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2304 parent_path, de->d_name) == -1)
2305 return got_error_from_errno("asprintf");
2306 } else {
2307 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2308 de->d_name) == -1)
2309 return got_error_from_errno("asprintf");
2312 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2313 a->repo);
2314 free(abspath);
2315 return err;
2318 static const struct got_error *
2319 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2321 struct diff_dir_cb_arg *a = arg;
2322 struct got_object_id blob_id, commit_id;
2323 unsigned char status;
2325 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2326 return got_error(GOT_ERR_CANCELLED);
2328 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2329 return NULL;
2331 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2332 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2333 if (got_fileindex_entry_has_file_on_disk(ie))
2334 status = GOT_STATUS_MISSING;
2335 else
2336 status = GOT_STATUS_DELETE;
2337 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2338 ie->path, &blob_id, NULL, &commit_id);
2341 void
2342 free_ignorelist(struct got_pathlist_head *ignorelist)
2344 struct got_pathlist_entry *pe;
2346 TAILQ_FOREACH(pe, ignorelist, entry)
2347 free((char *)pe->path);
2348 got_pathlist_free(ignorelist);
2351 void
2352 free_ignores(struct got_pathlist_head *ignores)
2354 struct got_pathlist_entry *pe;
2356 TAILQ_FOREACH(pe, ignores, entry) {
2357 struct got_pathlist_head *ignorelist = pe->data;
2358 free_ignorelist(ignorelist);
2359 free((char *)pe->path);
2361 got_pathlist_free(ignores);
2364 static const struct got_error *
2365 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2367 const struct got_error *err = NULL;
2368 struct got_pathlist_entry *pe = NULL;
2369 struct got_pathlist_head *ignorelist;
2370 char *line = NULL, *pattern, *dirpath = NULL;
2371 size_t linesize = 0;
2372 ssize_t linelen;
2374 ignorelist = calloc(1, sizeof(*ignorelist));
2375 if (ignorelist == NULL)
2376 return got_error_from_errno("calloc");
2377 TAILQ_INIT(ignorelist);
2379 while ((linelen = getline(&line, &linesize, f)) != -1) {
2380 if (linelen > 0 && line[linelen - 1] == '\n')
2381 line[linelen - 1] = '\0';
2382 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2383 line) == -1) {
2384 err = got_error_from_errno("asprintf");
2385 goto done;
2387 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2388 if (err)
2389 goto done;
2391 if (ferror(f)) {
2392 err = got_error_from_errno("getline");
2393 goto done;
2396 dirpath = strdup(path);
2397 if (dirpath == NULL) {
2398 err = got_error_from_errno("strdup");
2399 goto done;
2401 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2402 done:
2403 free(line);
2404 if (err || pe == NULL) {
2405 free(dirpath);
2406 free_ignorelist(ignorelist);
2408 return err;
2411 int
2412 match_ignores(struct got_pathlist_head *ignores, const char *path)
2414 struct got_pathlist_entry *pe;
2417 * The ignores pathlist contains ignore lists from children before
2418 * parents, so we can find the most specific ignorelist by walking
2419 * ignores backwards.
2421 pe = TAILQ_LAST(ignores, got_pathlist_head);
2422 while (pe) {
2423 if (got_path_is_child(path, pe->path, pe->path_len)) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 struct got_pathlist_entry *pi;
2426 TAILQ_FOREACH(pi, ignorelist, entry) {
2427 if (fnmatch(pi->path, path,
2428 FNM_PATHNAME | FNM_LEADING_DIR))
2429 continue;
2430 return 1;
2433 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2436 return 0;
2439 static const struct got_error *
2440 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2441 const char *path)
2443 const struct got_error *err = NULL;
2444 char *ignorespath;
2445 FILE *ignoresfile = NULL;
2447 /* TODO: read .gitignores as well... */
2448 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2449 path[0] ? "/" : "") == -1)
2450 return got_error_from_errno("asprintf");
2452 ignoresfile = fopen(ignorespath, "r");
2453 if (ignoresfile == NULL) {
2454 if (errno != ENOENT)
2455 err = got_error_from_errno2("fopen",
2456 ignorespath);
2457 } else
2458 err = read_ignores(ignores, path, ignoresfile);
2460 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2461 err = got_error_from_errno2("flose", path);
2462 free(ignorespath);
2463 return err;
2466 static const struct got_error *
2467 status_new(void *arg, struct dirent *de, const char *parent_path)
2469 const struct got_error *err = NULL;
2470 struct diff_dir_cb_arg *a = arg;
2471 char *path = NULL;
2473 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2474 return got_error(GOT_ERR_CANCELLED);
2476 /* XXX ignore symlinks for now */
2477 if (de->d_type == DT_LNK)
2478 return NULL;
2480 if (parent_path[0]) {
2481 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2482 return got_error_from_errno("asprintf");
2483 } else {
2484 path = de->d_name;
2487 if (de->d_type == DT_DIR)
2488 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2489 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2490 && !match_ignores(&a->ignores, path))
2491 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2492 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2493 if (parent_path[0])
2494 free(path);
2495 return err;
2498 static const struct got_error *
2499 report_single_file_status(const char *path, const char *ondisk_path,
2500 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2501 void *status_arg, struct got_repository *repo)
2503 struct got_fileindex_entry *ie;
2504 struct stat sb;
2506 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2507 if (ie)
2508 return report_file_status(ie, ondisk_path, status_cb,
2509 status_arg, repo);
2511 if (lstat(ondisk_path, &sb) == -1) {
2512 if (errno != ENOENT)
2513 return got_error_from_errno2("lstat", ondisk_path);
2514 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2516 return NULL;
2519 if (S_ISREG(sb.st_mode))
2520 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2521 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2523 return NULL;
2526 static const struct got_error *
2527 worktree_status(struct got_worktree *worktree, const char *path,
2528 struct got_fileindex *fileindex, struct got_repository *repo,
2529 got_worktree_status_cb status_cb, void *status_arg,
2530 got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 DIR *workdir = NULL;
2534 struct got_fileindex_diff_dir_cb fdiff_cb;
2535 struct diff_dir_cb_arg arg;
2536 char *ondisk_path = NULL;
2538 if (asprintf(&ondisk_path, "%s%s%s",
2539 worktree->root_path, path[0] ? "/" : "", path) == -1)
2540 return got_error_from_errno("asprintf");
2542 workdir = opendir(ondisk_path);
2543 if (workdir == NULL) {
2544 if (errno != ENOTDIR && errno != ENOENT)
2545 err = got_error_from_errno2("opendir", ondisk_path);
2546 else
2547 err = report_single_file_status(path, ondisk_path,
2548 fileindex, status_cb, status_arg, repo);
2549 } else {
2550 fdiff_cb.diff_old_new = status_old_new;
2551 fdiff_cb.diff_old = status_old;
2552 fdiff_cb.diff_new = status_new;
2553 arg.fileindex = fileindex;
2554 arg.worktree = worktree;
2555 arg.status_path = path;
2556 arg.status_path_len = strlen(path);
2557 arg.repo = repo;
2558 arg.status_cb = status_cb;
2559 arg.status_arg = status_arg;
2560 arg.cancel_cb = cancel_cb;
2561 arg.cancel_arg = cancel_arg;
2562 TAILQ_INIT(&arg.ignores);
2563 err = add_ignores(&arg.ignores, worktree->root_path, path);
2564 if (err == NULL)
2565 err = got_fileindex_diff_dir(fileindex, workdir,
2566 worktree->root_path, path, repo, &fdiff_cb, &arg);
2567 free_ignores(&arg.ignores);
2570 if (workdir)
2571 closedir(workdir);
2572 free(ondisk_path);
2573 return err;
2576 const struct got_error *
2577 got_worktree_status(struct got_worktree *worktree,
2578 struct got_pathlist_head *paths, struct got_repository *repo,
2579 got_worktree_status_cb status_cb, void *status_arg,
2580 got_cancel_cb cancel_cb, void *cancel_arg)
2582 const struct got_error *err = NULL;
2583 char *fileindex_path = NULL;
2584 struct got_fileindex *fileindex = NULL;
2585 struct got_pathlist_entry *pe;
2587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2588 if (err)
2589 return err;
2591 TAILQ_FOREACH(pe, paths, entry) {
2592 err = worktree_status(worktree, pe->path, fileindex, repo,
2593 status_cb, status_arg, cancel_cb, cancel_arg);
2594 if (err)
2595 break;
2597 free(fileindex_path);
2598 got_fileindex_free(fileindex);
2599 return err;
2602 const struct got_error *
2603 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2604 const char *arg)
2606 const struct got_error *err = NULL;
2607 char *resolved, *cwd = NULL, *path = NULL;
2608 size_t len;
2610 *wt_path = NULL;
2612 resolved = realpath(arg, NULL);
2613 if (resolved == NULL) {
2614 if (errno != ENOENT)
2615 return got_error_from_errno2("realpath", arg);
2616 cwd = getcwd(NULL, 0);
2617 if (cwd == NULL)
2618 return got_error_from_errno("getcwd");
2619 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2620 err = got_error_from_errno("asprintf");
2621 goto done;
2625 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2626 strlen(got_worktree_get_root_path(worktree)))) {
2627 err = got_error(GOT_ERR_BAD_PATH);
2628 goto done;
2631 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2632 err = got_path_skip_common_ancestor(&path,
2633 got_worktree_get_root_path(worktree), resolved);
2634 if (err)
2635 goto done;
2636 } else {
2637 path = strdup("");
2638 if (path == NULL) {
2639 err = got_error_from_errno("strdup");
2640 goto done;
2644 /* XXX status walk can't deal with trailing slash! */
2645 len = strlen(path);
2646 while (len > 0 && path[len - 1] == '/') {
2647 path[len - 1] = '\0';
2648 len--;
2650 done:
2651 free(resolved);
2652 free(cwd);
2653 if (err == NULL)
2654 *wt_path = path;
2655 else
2656 free(path);
2657 return err;
2660 static const struct got_error *
2661 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2662 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2663 struct got_repository *repo)
2665 const struct got_error *err = NULL;
2666 struct got_fileindex_entry *ie;
2667 unsigned char status;
2668 struct stat sb;
2670 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2671 if (ie) {
2672 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2673 if (err)
2674 return err;
2675 /* Re-adding an existing entry is a no-op. */
2676 if (status == GOT_STATUS_ADD)
2677 return NULL;
2678 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2681 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2682 if (err)
2683 return err;
2685 err = got_fileindex_entry_add(fileindex, ie);
2686 if (err) {
2687 got_fileindex_entry_free(ie);
2688 return err;
2691 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2694 const struct got_error *
2695 got_worktree_schedule_add(struct got_worktree *worktree,
2696 struct got_pathlist_head *paths,
2697 got_worktree_status_cb status_cb, void *status_arg,
2698 struct got_repository *repo)
2700 struct got_fileindex *fileindex = NULL;
2701 char *fileindex_path = NULL;
2702 const struct got_error *err = NULL, *sync_err, *unlockerr;
2703 struct got_pathlist_entry *pe;
2705 err = lock_worktree(worktree, LOCK_EX);
2706 if (err)
2707 return err;
2709 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2710 if (err)
2711 goto done;
2713 TAILQ_FOREACH(pe, paths, entry) {
2714 char *ondisk_path;
2715 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2716 pe->path) == -1)
2717 return got_error_from_errno("asprintf");
2718 err = schedule_addition(ondisk_path, fileindex, pe->path,
2719 status_cb, status_arg, repo);
2720 free(ondisk_path);
2721 if (err)
2722 break;
2724 sync_err = sync_fileindex(fileindex, fileindex_path);
2725 if (sync_err && err == NULL)
2726 err = sync_err;
2727 done:
2728 free(fileindex_path);
2729 if (fileindex)
2730 got_fileindex_free(fileindex);
2731 unlockerr = lock_worktree(worktree, LOCK_SH);
2732 if (unlockerr && err == NULL)
2733 err = unlockerr;
2734 return err;
2737 static const struct got_error *
2738 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2739 const char *relpath, int delete_local_mods,
2740 got_worktree_status_cb status_cb, void *status_arg,
2741 struct got_repository *repo)
2743 const struct got_error *err = NULL;
2744 struct got_fileindex_entry *ie = NULL;
2745 unsigned char status, staged_status;
2746 struct stat sb;
2748 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2749 if (ie == NULL)
2750 return got_error(GOT_ERR_BAD_PATH);
2752 staged_status = get_staged_status(ie);
2753 if (staged_status != GOT_STATUS_NO_CHANGE) {
2754 if (staged_status == GOT_STATUS_DELETE)
2755 return NULL;
2756 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2759 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2760 if (err)
2761 return err;
2763 if (status != GOT_STATUS_NO_CHANGE) {
2764 if (status == GOT_STATUS_DELETE)
2765 return NULL;
2766 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2767 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2768 if (status != GOT_STATUS_MODIFY &&
2769 status != GOT_STATUS_MISSING)
2770 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2773 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2774 return got_error_from_errno2("unlink", ondisk_path);
2776 got_fileindex_entry_mark_deleted_from_disk(ie);
2777 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2780 const struct got_error *
2781 got_worktree_schedule_delete(struct got_worktree *worktree,
2782 struct got_pathlist_head *paths, int delete_local_mods,
2783 got_worktree_status_cb status_cb, void *status_arg,
2784 struct got_repository *repo)
2786 struct got_fileindex *fileindex = NULL;
2787 char *fileindex_path = NULL;
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_pathlist_entry *pe;
2791 err = lock_worktree(worktree, LOCK_EX);
2792 if (err)
2793 return err;
2795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2796 if (err)
2797 goto done;
2799 TAILQ_FOREACH(pe, paths, entry) {
2800 char *ondisk_path;
2801 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2802 pe->path) == -1)
2803 return got_error_from_errno("asprintf");
2804 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2805 delete_local_mods, status_cb, status_arg, repo);
2806 free(ondisk_path);
2807 if (err)
2808 break;
2810 sync_err = sync_fileindex(fileindex, fileindex_path);
2811 if (sync_err && err == NULL)
2812 err = sync_err;
2813 done:
2814 free(fileindex_path);
2815 if (fileindex)
2816 got_fileindex_free(fileindex);
2817 unlockerr = lock_worktree(worktree, LOCK_SH);
2818 if (unlockerr && err == NULL)
2819 err = unlockerr;
2820 return err;
2823 static const struct got_error *
2824 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2826 const struct got_error *err = NULL;
2827 char *line = NULL;
2828 size_t linesize = 0, n;
2829 ssize_t linelen;
2831 linelen = getline(&line, &linesize, infile);
2832 if (linelen == -1) {
2833 if (ferror(infile)) {
2834 err = got_error_from_errno("getline");
2835 goto done;
2837 return NULL;
2839 if (outfile) {
2840 n = fwrite(line, 1, linelen, outfile);
2841 if (n != linelen) {
2842 err = got_ferror(outfile, GOT_ERR_IO);
2843 goto done;
2846 if (rejectfile) {
2847 n = fwrite(line, 1, linelen, rejectfile);
2848 if (n != linelen)
2849 err = got_ferror(outfile, GOT_ERR_IO);
2851 done:
2852 free(line);
2853 return err;
2856 static const struct got_error *
2857 skip_one_line(FILE *f)
2859 char *line = NULL;
2860 size_t linesize = 0;
2861 ssize_t linelen;
2863 linelen = getline(&line, &linesize, f);
2864 free(line);
2865 if (linelen == -1 && ferror(f))
2866 return got_error_from_errno("getline");
2867 return NULL;
2870 static const struct got_error *
2871 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2872 int start_old, int end_old, int start_new, int end_new,
2873 FILE *outfile, FILE *rejectfile)
2875 const struct got_error *err;
2877 /* Copy old file's lines leading up to patch. */
2878 while (!feof(f1) && *line_cur1 < start_old) {
2879 err = copy_one_line(f1, outfile, NULL);
2880 if (err)
2881 return err;
2882 (*line_cur1)++;
2884 /* Skip new file's lines leading up to patch. */
2885 while (!feof(f2) && *line_cur2 < start_new) {
2886 if (rejectfile)
2887 err = copy_one_line(f2, NULL, rejectfile);
2888 else
2889 err = skip_one_line(f2);
2890 if (err)
2891 return err;
2892 (*line_cur2)++;
2894 /* Copy patched lines. */
2895 while (!feof(f2) && *line_cur2 <= end_new) {
2896 err = copy_one_line(f2, outfile, NULL);
2897 if (err)
2898 return err;
2899 (*line_cur2)++;
2901 /* Skip over old file's replaced lines. */
2902 while (!feof(f1) && *line_cur1 <= end_old) {
2903 if (rejectfile)
2904 err = copy_one_line(f1, NULL, rejectfile);
2905 else
2906 err = skip_one_line(f1);
2907 if (err)
2908 return err;
2909 (*line_cur1)++;
2912 return NULL;
2915 static const struct got_error *
2916 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2917 FILE *outfile, FILE *rejectfile)
2919 const struct got_error *err;
2921 if (outfile) {
2922 /* Copy old file's lines until EOF. */
2923 while (!feof(f1)) {
2924 err = copy_one_line(f1, outfile, NULL);
2925 if (err)
2926 return err;
2927 (*line_cur1)++;
2930 if (rejectfile) {
2931 /* Copy new file's lines until EOF. */
2932 while (!feof(f2)) {
2933 err = copy_one_line(f2, NULL, rejectfile);
2934 if (err)
2935 return err;
2936 (*line_cur2)++;
2940 return NULL;
2943 static const struct got_error *
2944 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2945 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2946 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2947 int *line_cur2, FILE *outfile, FILE *rejectfile,
2948 got_worktree_patch_cb patch_cb, void *patch_arg)
2950 const struct got_error *err = NULL;
2951 int start_old = change->cv.a;
2952 int end_old = change->cv.b;
2953 int start_new = change->cv.c;
2954 int end_new = change->cv.d;
2955 long pos1, pos2;
2956 FILE *hunkfile;
2958 *choice = GOT_PATCH_CHOICE_NONE;
2960 hunkfile = got_opentemp();
2961 if (hunkfile == NULL)
2962 return got_error_from_errno("got_opentemp");
2964 pos1 = ftell(f1);
2965 pos2 = ftell(f2);
2967 /* XXX TODO needs error checking */
2968 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2970 if (fseek(f1, pos1, SEEK_SET) == -1) {
2971 err = got_ferror(f1, GOT_ERR_IO);
2972 goto done;
2974 if (fseek(f2, pos2, SEEK_SET) == -1) {
2975 err = got_ferror(f1, GOT_ERR_IO);
2976 goto done;
2978 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2979 err = got_ferror(hunkfile, GOT_ERR_IO);
2980 goto done;
2983 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2984 hunkfile, n, nchanges);
2985 if (err)
2986 goto done;
2988 switch (*choice) {
2989 case GOT_PATCH_CHOICE_YES:
2990 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2991 end_old, start_new, end_new, outfile, rejectfile);
2992 break;
2993 case GOT_PATCH_CHOICE_NO:
2994 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2995 end_old, start_new, end_new, rejectfile, outfile);
2996 break;
2997 case GOT_PATCH_CHOICE_QUIT:
2998 break;
2999 default:
3000 err = got_error(GOT_ERR_PATCH_CHOICE);
3001 break;
3003 done:
3004 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3005 err = got_error_from_errno("fclose");
3006 return err;
3009 struct revert_file_args {
3010 struct got_worktree *worktree;
3011 struct got_fileindex *fileindex;
3012 got_worktree_checkout_cb progress_cb;
3013 void *progress_arg;
3014 got_worktree_patch_cb patch_cb;
3015 void *patch_arg;
3016 struct got_repository *repo;
3019 static const struct got_error *
3020 create_patched_content(char **path_outfile, int reverse_patch,
3021 struct got_object_id *blob_id, const char *path2,
3022 const char *relpath, struct got_repository *repo,
3023 got_worktree_patch_cb patch_cb, void *patch_arg)
3025 const struct got_error *err;
3026 struct got_blob_object *blob = NULL;
3027 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3028 char *path1 = NULL, *id_str = NULL;
3029 struct stat sb1, sb2;
3030 struct got_diff_changes *changes = NULL;
3031 struct got_diff_state *ds = NULL;
3032 struct got_diff_args *args = NULL;
3033 struct got_diff_change *change;
3034 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3035 int n = 0;
3037 *path_outfile = NULL;
3039 err = got_object_id_str(&id_str, blob_id);
3040 if (err)
3041 return err;
3043 f2 = fopen(path2, "r");
3044 if (f2 == NULL) {
3045 err = got_error_from_errno2("fopen", path2);
3046 goto done;
3049 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3050 if (err)
3051 goto done;
3053 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3054 if (err)
3055 goto done;
3057 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3058 if (err)
3059 goto done;
3061 if (stat(path1, &sb1) == -1) {
3062 err = got_error_from_errno2("stat", path1);
3063 goto done;
3065 if (stat(path2, &sb2) == -1) {
3066 err = got_error_from_errno2("stat", path2);
3067 goto done;
3070 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3071 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3072 if (err)
3073 goto done;
3075 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3076 if (err)
3077 goto done;
3079 if (fseek(f1, 0L, SEEK_SET) == -1)
3080 return got_ferror(f1, GOT_ERR_IO);
3081 if (fseek(f2, 0L, SEEK_SET) == -1)
3082 return got_ferror(f2, GOT_ERR_IO);
3083 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3084 int choice;
3085 err = apply_or_reject_change(&choice, change, ++n,
3086 changes->nchanges, ds, args, diff_flags, relpath,
3087 f1, f2, &line_cur1, &line_cur2,
3088 reverse_patch ? NULL : outfile,
3089 reverse_patch ? outfile : NULL,
3090 patch_cb, patch_arg);
3091 if (err)
3092 goto done;
3093 if (choice == GOT_PATCH_CHOICE_YES)
3094 have_content = 1;
3095 else if (choice == GOT_PATCH_CHOICE_QUIT)
3096 break;
3098 if (have_content)
3099 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3100 reverse_patch ? NULL : outfile,
3101 reverse_patch ? outfile : NULL);
3102 done:
3103 free(id_str);
3104 if (blob)
3105 got_object_blob_close(blob);
3106 if (f1 && fclose(f1) == EOF && err == NULL)
3107 err = got_error_from_errno2("fclose", path1);
3108 if (f2 && fclose(f2) == EOF && err == NULL)
3109 err = got_error_from_errno2("fclose", path2);
3110 if (outfile && fclose(outfile) == EOF && err == NULL)
3111 err = got_error_from_errno2("fclose", *path_outfile);
3112 if (path1 && unlink(path1) == -1 && err == NULL)
3113 err = got_error_from_errno2("unlink", path1);
3114 if (err || !have_content) {
3115 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3116 err = got_error_from_errno2("unlink", *path_outfile);
3117 free(*path_outfile);
3118 *path_outfile = NULL;
3120 free(args);
3121 if (ds) {
3122 got_diff_state_free(ds);
3123 free(ds);
3125 if (changes)
3126 got_diff_free_changes(changes);
3127 free(path1);
3128 return err;
3131 static const struct got_error *
3132 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3133 const char *relpath, struct got_object_id *blob_id,
3134 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3136 struct revert_file_args *a = arg;
3137 const struct got_error *err = NULL;
3138 char *parent_path = NULL;
3139 struct got_fileindex_entry *ie;
3140 struct got_tree_object *tree = NULL;
3141 struct got_object_id *tree_id = NULL;
3142 const struct got_tree_entry *te = NULL;
3143 char *tree_path = NULL, *te_name;
3144 char *ondisk_path = NULL, *path_content = NULL;
3145 struct got_blob_object *blob = NULL;
3147 /* Reverting a staged deletion is a no-op. */
3148 if (status == GOT_STATUS_DELETE &&
3149 staged_status != GOT_STATUS_NO_CHANGE)
3150 return NULL;
3152 if (status == GOT_STATUS_UNVERSIONED)
3153 return (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_UNVERSIONED, relpath);
3156 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3157 if (ie == NULL)
3158 return got_error(GOT_ERR_BAD_PATH);
3160 /* Construct in-repository path of tree which contains this blob. */
3161 err = got_path_dirname(&parent_path, ie->path);
3162 if (err) {
3163 if (err->code != GOT_ERR_BAD_PATH)
3164 goto done;
3165 parent_path = strdup("/");
3166 if (parent_path == NULL) {
3167 err = got_error_from_errno("strdup");
3168 goto done;
3171 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3172 tree_path = strdup(parent_path);
3173 if (tree_path == NULL) {
3174 err = got_error_from_errno("strdup");
3175 goto done;
3177 } else {
3178 if (got_path_is_root_dir(parent_path)) {
3179 tree_path = strdup(a->worktree->path_prefix);
3180 if (tree_path == NULL) {
3181 err = got_error_from_errno("strdup");
3182 goto done;
3184 } else {
3185 if (asprintf(&tree_path, "%s/%s",
3186 a->worktree->path_prefix, parent_path) == -1) {
3187 err = got_error_from_errno("asprintf");
3188 goto done;
3193 err = got_object_id_by_path(&tree_id, a->repo,
3194 a->worktree->base_commit_id, tree_path);
3195 if (err) {
3196 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3197 (status == GOT_STATUS_ADD ||
3198 staged_status == GOT_STATUS_ADD)))
3199 goto done;
3200 } else {
3201 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3202 if (err)
3203 goto done;
3205 te_name = basename(ie->path);
3206 if (te_name == NULL) {
3207 err = got_error_from_errno2("basename", ie->path);
3208 goto done;
3211 te = got_object_tree_find_entry(tree, te_name);
3212 if (te == NULL && status != GOT_STATUS_ADD &&
3213 staged_status != GOT_STATUS_ADD) {
3214 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3215 goto done;
3219 switch (status) {
3220 case GOT_STATUS_ADD:
3221 if (a->patch_cb) {
3222 int choice = GOT_PATCH_CHOICE_NONE;
3223 err = (*a->patch_cb)(&choice, a->patch_arg,
3224 status, ie->path, NULL, 1, 1);
3225 if (err)
3226 goto done;
3227 if (choice != GOT_PATCH_CHOICE_YES)
3228 break;
3230 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3231 ie->path);
3232 if (err)
3233 goto done;
3234 got_fileindex_entry_remove(a->fileindex, ie);
3235 break;
3236 case GOT_STATUS_DELETE:
3237 if (a->patch_cb) {
3238 int choice = GOT_PATCH_CHOICE_NONE;
3239 err = (*a->patch_cb)(&choice, a->patch_arg,
3240 status, ie->path, NULL, 1, 1);
3241 if (err)
3242 goto done;
3243 if (choice != GOT_PATCH_CHOICE_YES)
3244 break;
3246 /* fall through */
3247 case GOT_STATUS_MODIFY:
3248 case GOT_STATUS_CONFLICT:
3249 case GOT_STATUS_MISSING: {
3250 struct got_object_id id;
3251 if (staged_status == GOT_STATUS_ADD ||
3252 staged_status == GOT_STATUS_MODIFY) {
3253 memcpy(id.sha1, ie->staged_blob_sha1,
3254 SHA1_DIGEST_LENGTH);
3255 } else
3256 memcpy(id.sha1, ie->blob_sha1,
3257 SHA1_DIGEST_LENGTH);
3258 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3259 if (err)
3260 goto done;
3262 if (asprintf(&ondisk_path, "%s/%s",
3263 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3264 err = got_error_from_errno("asprintf");
3265 goto done;
3268 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3269 status == GOT_STATUS_CONFLICT)) {
3270 err = create_patched_content(&path_content, 1, &id,
3271 ondisk_path, ie->path, a->repo,
3272 a->patch_cb, a->patch_arg);
3273 if (err || path_content == NULL)
3274 break;
3275 if (rename(path_content, ondisk_path) == -1) {
3276 err = got_error_from_errno3("rename",
3277 path_content, ondisk_path);
3278 goto done;
3280 } else {
3281 err = install_blob(a->worktree, ondisk_path, ie->path,
3282 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3283 got_fileindex_perms_to_st(ie), blob, 0, 1,
3284 a->repo, a->progress_cb, a->progress_arg);
3285 if (err)
3286 goto done;
3287 if (status == GOT_STATUS_DELETE) {
3288 err = update_blob_fileindex_entry(a->worktree,
3289 a->fileindex, ie, ondisk_path, ie->path,
3290 blob, 1);
3291 if (err)
3292 goto done;
3295 break;
3297 default:
3298 break;
3300 done:
3301 free(ondisk_path);
3302 free(path_content);
3303 free(parent_path);
3304 free(tree_path);
3305 if (blob)
3306 got_object_blob_close(blob);
3307 if (tree)
3308 got_object_tree_close(tree);
3309 free(tree_id);
3310 return err;
3313 const struct got_error *
3314 got_worktree_revert(struct got_worktree *worktree,
3315 struct got_pathlist_head *paths,
3316 got_worktree_checkout_cb progress_cb, void *progress_arg,
3317 got_worktree_patch_cb patch_cb, void *patch_arg,
3318 struct got_repository *repo)
3320 struct got_fileindex *fileindex = NULL;
3321 char *fileindex_path = NULL;
3322 const struct got_error *err = NULL, *unlockerr = NULL;
3323 const struct got_error *sync_err = NULL;
3324 struct got_pathlist_entry *pe;
3325 struct revert_file_args rfa;
3327 err = lock_worktree(worktree, LOCK_EX);
3328 if (err)
3329 return err;
3331 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3332 if (err)
3333 goto done;
3335 rfa.worktree = worktree;
3336 rfa.fileindex = fileindex;
3337 rfa.progress_cb = progress_cb;
3338 rfa.progress_arg = progress_arg;
3339 rfa.patch_cb = patch_cb;
3340 rfa.patch_arg = patch_arg;
3341 rfa.repo = repo;
3342 TAILQ_FOREACH(pe, paths, entry) {
3343 err = worktree_status(worktree, pe->path, fileindex, repo,
3344 revert_file, &rfa, NULL, NULL);
3345 if (err)
3346 break;
3348 sync_err = sync_fileindex(fileindex, fileindex_path);
3349 if (sync_err && err == NULL)
3350 err = sync_err;
3351 done:
3352 free(fileindex_path);
3353 if (fileindex)
3354 got_fileindex_free(fileindex);
3355 unlockerr = lock_worktree(worktree, LOCK_SH);
3356 if (unlockerr && err == NULL)
3357 err = unlockerr;
3358 return err;
3361 static void
3362 free_commitable(struct got_commitable *ct)
3364 free(ct->path);
3365 free(ct->in_repo_path);
3366 free(ct->ondisk_path);
3367 free(ct->blob_id);
3368 free(ct->base_blob_id);
3369 free(ct->staged_blob_id);
3370 free(ct->base_commit_id);
3371 free(ct);
3374 struct collect_commitables_arg {
3375 struct got_pathlist_head *commitable_paths;
3376 struct got_repository *repo;
3377 struct got_worktree *worktree;
3378 int have_staged_files;
3381 static const struct got_error *
3382 collect_commitables(void *arg, unsigned char status,
3383 unsigned char staged_status, const char *relpath,
3384 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3385 struct got_object_id *commit_id)
3387 struct collect_commitables_arg *a = arg;
3388 const struct got_error *err = NULL;
3389 struct got_commitable *ct = NULL;
3390 struct got_pathlist_entry *new = NULL;
3391 char *parent_path = NULL, *path = NULL;
3392 struct stat sb;
3394 if (a->have_staged_files) {
3395 if (staged_status != GOT_STATUS_MODIFY &&
3396 staged_status != GOT_STATUS_ADD &&
3397 staged_status != GOT_STATUS_DELETE)
3398 return NULL;
3399 } else {
3400 if (status == GOT_STATUS_CONFLICT)
3401 return got_error(GOT_ERR_COMMIT_CONFLICT);
3403 if (status != GOT_STATUS_MODIFY &&
3404 status != GOT_STATUS_ADD &&
3405 status != GOT_STATUS_DELETE)
3406 return NULL;
3409 if (asprintf(&path, "/%s", relpath) == -1) {
3410 err = got_error_from_errno("asprintf");
3411 goto done;
3413 if (strcmp(path, "/") == 0) {
3414 parent_path = strdup("");
3415 if (parent_path == NULL)
3416 return got_error_from_errno("strdup");
3417 } else {
3418 err = got_path_dirname(&parent_path, path);
3419 if (err)
3420 return err;
3423 ct = calloc(1, sizeof(*ct));
3424 if (ct == NULL) {
3425 err = got_error_from_errno("calloc");
3426 goto done;
3429 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3430 relpath) == -1) {
3431 err = got_error_from_errno("asprintf");
3432 goto done;
3434 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3435 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3436 } else {
3437 if (lstat(ct->ondisk_path, &sb) != 0) {
3438 err = got_error_from_errno2("lstat", ct->ondisk_path);
3439 goto done;
3441 ct->mode = sb.st_mode;
3444 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3445 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3446 relpath) == -1) {
3447 err = got_error_from_errno("asprintf");
3448 goto done;
3451 ct->status = status;
3452 ct->staged_status = staged_status;
3453 ct->blob_id = NULL; /* will be filled in when blob gets created */
3454 if (ct->status != GOT_STATUS_ADD &&
3455 ct->staged_status != GOT_STATUS_ADD) {
3456 ct->base_blob_id = got_object_id_dup(blob_id);
3457 if (ct->base_blob_id == NULL) {
3458 err = got_error_from_errno("got_object_id_dup");
3459 goto done;
3461 ct->base_commit_id = got_object_id_dup(commit_id);
3462 if (ct->base_commit_id == NULL) {
3463 err = got_error_from_errno("got_object_id_dup");
3464 goto done;
3467 if (ct->staged_status == GOT_STATUS_ADD ||
3468 ct->staged_status == GOT_STATUS_MODIFY) {
3469 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3470 if (ct->staged_blob_id == NULL) {
3471 err = got_error_from_errno("got_object_id_dup");
3472 goto done;
3475 ct->path = strdup(path);
3476 if (ct->path == NULL) {
3477 err = got_error_from_errno("strdup");
3478 goto done;
3480 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3481 done:
3482 if (ct && (err || new == NULL))
3483 free_commitable(ct);
3484 free(parent_path);
3485 free(path);
3486 return err;
3489 static const struct got_error *write_tree(struct got_object_id **,
3490 struct got_tree_object *, const char *, struct got_pathlist_head *,
3491 got_worktree_status_cb status_cb, void *status_arg,
3492 struct got_repository *);
3494 static const struct got_error *
3495 write_subtree(struct got_object_id **new_subtree_id,
3496 struct got_tree_entry *te, const char *parent_path,
3497 struct got_pathlist_head *commitable_paths,
3498 got_worktree_status_cb status_cb, void *status_arg,
3499 struct got_repository *repo)
3501 const struct got_error *err = NULL;
3502 struct got_tree_object *subtree;
3503 char *subpath;
3505 if (asprintf(&subpath, "%s%s%s", parent_path,
3506 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3507 return got_error_from_errno("asprintf");
3509 err = got_object_open_as_tree(&subtree, repo, te->id);
3510 if (err)
3511 return err;
3513 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3514 status_cb, status_arg, repo);
3515 got_object_tree_close(subtree);
3516 free(subpath);
3517 return err;
3520 static const struct got_error *
3521 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3523 const struct got_error *err = NULL;
3524 char *ct_parent_path = NULL;
3526 *match = 0;
3528 if (strchr(ct->in_repo_path, '/') == NULL) {
3529 *match = got_path_is_root_dir(path);
3530 return NULL;
3533 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3534 if (err)
3535 return err;
3536 *match = (strcmp(path, ct_parent_path) == 0);
3537 free(ct_parent_path);
3538 return err;
3541 static mode_t
3542 get_ct_file_mode(struct got_commitable *ct)
3544 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3547 static const struct got_error *
3548 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3549 struct got_tree_entry *te, struct got_commitable *ct)
3551 const struct got_error *err = NULL;
3553 *new_te = NULL;
3555 err = got_object_tree_entry_dup(new_te, te);
3556 if (err)
3557 goto done;
3559 (*new_te)->mode = get_ct_file_mode(ct);
3561 free((*new_te)->id);
3562 if (ct->staged_status == GOT_STATUS_MODIFY)
3563 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3564 else
3565 (*new_te)->id = got_object_id_dup(ct->blob_id);
3566 if ((*new_te)->id == NULL) {
3567 err = got_error_from_errno("got_object_id_dup");
3568 goto done;
3570 done:
3571 if (err && *new_te) {
3572 got_object_tree_entry_close(*new_te);
3573 *new_te = NULL;
3575 return err;
3578 static const struct got_error *
3579 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3580 struct got_commitable *ct)
3582 const struct got_error *err = NULL;
3583 char *ct_name;
3585 *new_te = NULL;
3587 *new_te = calloc(1, sizeof(**new_te));
3588 if (*new_te == NULL)
3589 return got_error_from_errno("calloc");
3591 ct_name = basename(ct->path);
3592 if (ct_name == NULL) {
3593 err = got_error_from_errno2("basename", ct->path);
3594 goto done;
3596 (*new_te)->name = strdup(ct_name);
3597 if ((*new_te)->name == NULL) {
3598 err = got_error_from_errno("strdup");
3599 goto done;
3602 (*new_te)->mode = get_ct_file_mode(ct);
3604 if (ct->staged_status == GOT_STATUS_ADD)
3605 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3606 else
3607 (*new_te)->id = got_object_id_dup(ct->blob_id);
3608 if ((*new_te)->id == NULL) {
3609 err = got_error_from_errno("got_object_id_dup");
3610 goto done;
3612 done:
3613 if (err && *new_te) {
3614 got_object_tree_entry_close(*new_te);
3615 *new_te = NULL;
3617 return err;
3620 static const struct got_error *
3621 insert_tree_entry(struct got_tree_entry *new_te,
3622 struct got_pathlist_head *paths)
3624 const struct got_error *err = NULL;
3625 struct got_pathlist_entry *new_pe;
3627 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3628 if (err)
3629 return err;
3630 if (new_pe == NULL)
3631 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3632 return NULL;
3635 static const struct got_error *
3636 report_ct_status(struct got_commitable *ct,
3637 got_worktree_status_cb status_cb, void *status_arg)
3639 const char *ct_path = ct->path;
3640 unsigned char status;
3642 while (ct_path[0] == '/')
3643 ct_path++;
3645 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3646 status = ct->staged_status;
3647 else
3648 status = ct->status;
3650 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3651 ct_path, ct->blob_id, NULL, NULL);
3654 static const struct got_error *
3655 match_modified_subtree(int *modified, struct got_tree_entry *te,
3656 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3658 const struct got_error *err = NULL;
3659 struct got_pathlist_entry *pe;
3660 char *te_path;
3662 *modified = 0;
3664 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3665 got_path_is_root_dir(base_tree_path) ? "" : "/",
3666 te->name) == -1)
3667 return got_error_from_errno("asprintf");
3669 TAILQ_FOREACH(pe, commitable_paths, entry) {
3670 struct got_commitable *ct = pe->data;
3671 *modified = got_path_is_child(ct->in_repo_path, te_path,
3672 strlen(te_path));
3673 if (*modified)
3674 break;
3677 free(te_path);
3678 return err;
3681 static const struct got_error *
3682 match_deleted_or_modified_ct(struct got_commitable **ctp,
3683 struct got_tree_entry *te, const char *base_tree_path,
3684 struct got_pathlist_head *commitable_paths)
3686 const struct got_error *err = NULL;
3687 struct got_pathlist_entry *pe;
3689 *ctp = NULL;
3691 TAILQ_FOREACH(pe, commitable_paths, entry) {
3692 struct got_commitable *ct = pe->data;
3693 char *ct_name = NULL;
3694 int path_matches;
3696 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3697 if (ct->status != GOT_STATUS_MODIFY &&
3698 ct->status != GOT_STATUS_DELETE)
3699 continue;
3700 } else {
3701 if (ct->staged_status != GOT_STATUS_MODIFY &&
3702 ct->staged_status != GOT_STATUS_DELETE)
3703 continue;
3706 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3707 continue;
3709 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3710 if (err)
3711 return err;
3712 if (!path_matches)
3713 continue;
3715 ct_name = basename(pe->path);
3716 if (ct_name == NULL)
3717 return got_error_from_errno2("basename", pe->path);
3719 if (strcmp(te->name, ct_name) != 0)
3720 continue;
3722 *ctp = ct;
3723 break;
3726 return err;
3729 static const struct got_error *
3730 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3731 const char *child_path, const char *path_base_tree,
3732 struct got_pathlist_head *commitable_paths,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 struct got_repository *repo)
3736 const struct got_error *err = NULL;
3737 struct got_tree_entry *new_te;
3738 char *subtree_path;
3740 *new_tep = NULL;
3742 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3743 got_path_is_root_dir(path_base_tree) ? "" : "/",
3744 child_path) == -1)
3745 return got_error_from_errno("asprintf");
3747 new_te = calloc(1, sizeof(*new_te));
3748 new_te->mode = S_IFDIR;
3749 new_te->name = strdup(child_path);
3750 if (new_te->name == NULL) {
3751 err = got_error_from_errno("strdup");
3752 got_object_tree_entry_close(new_te);
3753 goto done;
3755 err = write_tree(&new_te->id, NULL, subtree_path,
3756 commitable_paths, status_cb, status_arg, repo);
3757 if (err) {
3758 got_object_tree_entry_close(new_te);
3759 goto done;
3761 done:
3762 free(subtree_path);
3763 if (err == NULL)
3764 *new_tep = new_te;
3765 return err;
3768 static const struct got_error *
3769 write_tree(struct got_object_id **new_tree_id,
3770 struct got_tree_object *base_tree, const char *path_base_tree,
3771 struct got_pathlist_head *commitable_paths,
3772 got_worktree_status_cb status_cb, void *status_arg,
3773 struct got_repository *repo)
3775 const struct got_error *err = NULL;
3776 const struct got_tree_entries *base_entries = NULL;
3777 struct got_pathlist_head paths;
3778 struct got_tree_entries new_tree_entries;
3779 struct got_tree_entry *te, *new_te = NULL;
3780 struct got_pathlist_entry *pe;
3782 TAILQ_INIT(&paths);
3783 new_tree_entries.nentries = 0;
3784 SIMPLEQ_INIT(&new_tree_entries.head);
3786 /* Insert, and recurse into, newly added entries first. */
3787 TAILQ_FOREACH(pe, commitable_paths, entry) {
3788 struct got_commitable *ct = pe->data;
3789 char *child_path = NULL, *slash;
3791 if ((ct->status != GOT_STATUS_ADD &&
3792 ct->staged_status != GOT_STATUS_ADD) ||
3793 (ct->flags & GOT_COMMITABLE_ADDED))
3794 continue;
3796 if (!got_path_is_child(pe->path, path_base_tree,
3797 strlen(path_base_tree)))
3798 continue;
3800 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3801 pe->path);
3802 if (err)
3803 goto done;
3805 slash = strchr(child_path, '/');
3806 if (slash == NULL) {
3807 err = alloc_added_blob_tree_entry(&new_te, ct);
3808 if (err)
3809 goto done;
3810 err = report_ct_status(ct, status_cb, status_arg);
3811 if (err)
3812 goto done;
3813 ct->flags |= GOT_COMMITABLE_ADDED;
3814 err = insert_tree_entry(new_te, &paths);
3815 if (err)
3816 goto done;
3817 } else {
3818 *slash = '\0'; /* trim trailing path components */
3819 if (base_tree == NULL ||
3820 got_object_tree_find_entry(base_tree, child_path)
3821 == NULL) {
3822 err = make_subtree_for_added_blob(&new_te,
3823 child_path, path_base_tree,
3824 commitable_paths, status_cb, status_arg,
3825 repo);
3826 if (err)
3827 goto done;
3828 err = insert_tree_entry(new_te, &paths);
3829 if (err)
3830 goto done;
3835 if (base_tree) {
3836 /* Handle modified and deleted entries. */
3837 base_entries = got_object_tree_get_entries(base_tree);
3838 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3839 struct got_commitable *ct = NULL;
3841 if (got_object_tree_entry_is_submodule(te)) {
3842 /* Entry is a submodule; just copy it. */
3843 err = got_object_tree_entry_dup(&new_te, te);
3844 if (err)
3845 goto done;
3846 err = insert_tree_entry(new_te, &paths);
3847 if (err)
3848 goto done;
3849 continue;
3852 if (S_ISDIR(te->mode)) {
3853 int modified;
3854 err = got_object_tree_entry_dup(&new_te, te);
3855 if (err)
3856 goto done;
3857 err = match_modified_subtree(&modified, te,
3858 path_base_tree, commitable_paths);
3859 if (err)
3860 goto done;
3861 /* Avoid recursion into unmodified subtrees. */
3862 if (modified) {
3863 free(new_te->id);
3864 err = write_subtree(&new_te->id, te,
3865 path_base_tree, commitable_paths,
3866 status_cb, status_arg, repo);
3867 if (err)
3868 goto done;
3870 err = insert_tree_entry(new_te, &paths);
3871 if (err)
3872 goto done;
3873 continue;
3876 err = match_deleted_or_modified_ct(&ct, te,
3877 path_base_tree, commitable_paths);
3878 if (ct) {
3879 /* NB: Deleted entries get dropped here. */
3880 if (ct->status == GOT_STATUS_MODIFY ||
3881 ct->staged_status == GOT_STATUS_MODIFY) {
3882 err = alloc_modified_blob_tree_entry(
3883 &new_te, te, ct);
3884 if (err)
3885 goto done;
3886 err = insert_tree_entry(new_te, &paths);
3887 if (err)
3888 goto done;
3890 err = report_ct_status(ct, status_cb,
3891 status_arg);
3892 if (err)
3893 goto done;
3894 } else {
3895 /* Entry is unchanged; just copy it. */
3896 err = got_object_tree_entry_dup(&new_te, te);
3897 if (err)
3898 goto done;
3899 err = insert_tree_entry(new_te, &paths);
3900 if (err)
3901 goto done;
3906 /* Write new list of entries; deleted entries have been dropped. */
3907 TAILQ_FOREACH(pe, &paths, entry) {
3908 struct got_tree_entry *te = pe->data;
3909 new_tree_entries.nentries++;
3910 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3912 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3913 done:
3914 got_object_tree_entries_close(&new_tree_entries);
3915 got_pathlist_free(&paths);
3916 return err;
3919 static const struct got_error *
3920 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3921 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3923 const struct got_error *err = NULL;
3924 struct got_pathlist_entry *pe;
3926 TAILQ_FOREACH(pe, commitable_paths, entry) {
3927 struct got_fileindex_entry *ie;
3928 struct got_commitable *ct = pe->data;
3930 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3931 if (ie) {
3932 if (ct->status == GOT_STATUS_DELETE ||
3933 ct->staged_status == GOT_STATUS_DELETE) {
3934 got_fileindex_entry_remove(fileindex, ie);
3935 got_fileindex_entry_free(ie);
3936 } else if (ct->staged_status == GOT_STATUS_ADD ||
3937 ct->staged_status == GOT_STATUS_MODIFY) {
3938 got_fileindex_entry_stage_set(ie,
3939 GOT_FILEIDX_STAGE_NONE);
3940 err = got_fileindex_entry_update(ie,
3941 ct->ondisk_path, ct->staged_blob_id->sha1,
3942 new_base_commit_id->sha1, 1);
3943 } else
3944 err = got_fileindex_entry_update(ie,
3945 ct->ondisk_path, ct->blob_id->sha1,
3946 new_base_commit_id->sha1, 1);
3947 } else {
3948 err = got_fileindex_entry_alloc(&ie,
3949 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3950 new_base_commit_id->sha1);
3951 if (err)
3952 break;
3953 err = got_fileindex_entry_add(fileindex, ie);
3954 if (err)
3955 break;
3958 return err;
3962 static const struct got_error *
3963 check_out_of_date(const char *in_repo_path, unsigned char status,
3964 unsigned char staged_status, struct got_object_id *base_blob_id,
3965 struct got_object_id *base_commit_id,
3966 struct got_object_id *head_commit_id, struct got_repository *repo,
3967 int ood_errcode)
3969 const struct got_error *err = NULL;
3970 struct got_object_id *id = NULL;
3972 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3973 /* Trivial case: base commit == head commit */
3974 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3975 return NULL;
3977 * Ensure file content which local changes were based
3978 * on matches file content in the branch head.
3980 err = got_object_id_by_path(&id, repo, head_commit_id,
3981 in_repo_path);
3982 if (err) {
3983 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3984 err = got_error(ood_errcode);
3985 goto done;
3986 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3987 err = got_error(ood_errcode);
3988 } else {
3989 /* Require that added files don't exist in the branch head. */
3990 err = got_object_id_by_path(&id, repo, head_commit_id,
3991 in_repo_path);
3992 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3993 goto done;
3994 err = id ? got_error(ood_errcode) : NULL;
3996 done:
3997 free(id);
3998 return err;
4001 const struct got_error *
4002 commit_worktree(struct got_object_id **new_commit_id,
4003 struct got_pathlist_head *commitable_paths,
4004 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4005 const char *author, const char *committer,
4006 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4007 got_worktree_status_cb status_cb, void *status_arg,
4008 struct got_repository *repo)
4010 const struct got_error *err = NULL, *unlockerr = NULL;
4011 struct got_pathlist_entry *pe;
4012 const char *head_ref_name = NULL;
4013 struct got_commit_object *head_commit = NULL;
4014 struct got_reference *head_ref2 = NULL;
4015 struct got_object_id *head_commit_id2 = NULL;
4016 struct got_tree_object *head_tree = NULL;
4017 struct got_object_id *new_tree_id = NULL;
4018 struct got_object_id_queue parent_ids;
4019 struct got_object_qid *pid = NULL;
4020 char *logmsg = NULL;
4022 *new_commit_id = NULL;
4024 SIMPLEQ_INIT(&parent_ids);
4026 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4027 if (err)
4028 goto done;
4030 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4031 if (err)
4032 goto done;
4034 if (commit_msg_cb != NULL) {
4035 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4036 if (err)
4037 goto done;
4040 if (logmsg == NULL || strlen(logmsg) == 0) {
4041 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4042 goto done;
4045 /* Create blobs from added and modified files and record their IDs. */
4046 TAILQ_FOREACH(pe, commitable_paths, entry) {
4047 struct got_commitable *ct = pe->data;
4048 char *ondisk_path;
4050 /* Blobs for staged files already exist. */
4051 if (ct->staged_status == GOT_STATUS_ADD ||
4052 ct->staged_status == GOT_STATUS_MODIFY)
4053 continue;
4055 if (ct->status != GOT_STATUS_ADD &&
4056 ct->status != GOT_STATUS_MODIFY)
4057 continue;
4059 if (asprintf(&ondisk_path, "%s/%s",
4060 worktree->root_path, pe->path) == -1) {
4061 err = got_error_from_errno("asprintf");
4062 goto done;
4064 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4065 free(ondisk_path);
4066 if (err)
4067 goto done;
4070 /* Recursively write new tree objects. */
4071 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4072 status_cb, status_arg, repo);
4073 if (err)
4074 goto done;
4076 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4077 if (err)
4078 goto done;
4079 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4080 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4081 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4082 got_object_qid_free(pid);
4083 if (logmsg != NULL)
4084 free(logmsg);
4085 if (err)
4086 goto done;
4088 /* Check if a concurrent commit to our branch has occurred. */
4089 head_ref_name = got_worktree_get_head_ref_name(worktree);
4090 if (head_ref_name == NULL) {
4091 err = got_error_from_errno("got_worktree_get_head_ref_name");
4092 goto done;
4094 /* Lock the reference here to prevent concurrent modification. */
4095 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4096 if (err)
4097 goto done;
4098 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4099 if (err)
4100 goto done;
4101 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4102 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4103 goto done;
4105 /* Update branch head in repository. */
4106 err = got_ref_change_ref(head_ref2, *new_commit_id);
4107 if (err)
4108 goto done;
4109 err = got_ref_write(head_ref2, repo);
4110 if (err)
4111 goto done;
4113 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4114 if (err)
4115 goto done;
4117 err = ref_base_commit(worktree, repo);
4118 if (err)
4119 goto done;
4120 done:
4121 if (head_tree)
4122 got_object_tree_close(head_tree);
4123 if (head_commit)
4124 got_object_commit_close(head_commit);
4125 free(head_commit_id2);
4126 if (head_ref2) {
4127 unlockerr = got_ref_unlock(head_ref2);
4128 if (unlockerr && err == NULL)
4129 err = unlockerr;
4130 got_ref_close(head_ref2);
4132 return err;
4135 static const struct got_error *
4136 check_path_is_commitable(const char *path,
4137 struct got_pathlist_head *commitable_paths)
4139 struct got_pathlist_entry *cpe = NULL;
4140 size_t path_len = strlen(path);
4142 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4143 struct got_commitable *ct = cpe->data;
4144 const char *ct_path = ct->path;
4146 while (ct_path[0] == '/')
4147 ct_path++;
4149 if (strcmp(path, ct_path) == 0 ||
4150 got_path_is_child(ct_path, path, path_len))
4151 break;
4154 if (cpe == NULL)
4155 return got_error_path(path, GOT_ERR_BAD_PATH);
4157 return NULL;
4160 static const struct got_error *
4161 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4163 int *have_staged_files = arg;
4165 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4166 *have_staged_files = 1;
4167 return got_error(GOT_ERR_CANCELLED);
4170 return NULL;
4173 static const struct got_error *
4174 check_non_staged_files(struct got_fileindex *fileindex,
4175 struct got_pathlist_head *paths)
4177 struct got_pathlist_entry *pe;
4178 struct got_fileindex_entry *ie;
4180 TAILQ_FOREACH(pe, paths, entry) {
4181 if (pe->path[0] == '\0')
4182 continue;
4183 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4184 if (ie == NULL)
4185 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4186 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4187 return got_error_path(pe->path,
4188 GOT_ERR_FILE_NOT_STAGED);
4191 return NULL;
4194 const struct got_error *
4195 got_worktree_commit(struct got_object_id **new_commit_id,
4196 struct got_worktree *worktree, struct got_pathlist_head *paths,
4197 const char *author, const char *committer,
4198 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4199 got_worktree_status_cb status_cb, void *status_arg,
4200 struct got_repository *repo)
4202 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4203 struct got_fileindex *fileindex = NULL;
4204 char *fileindex_path = NULL;
4205 struct got_pathlist_head commitable_paths;
4206 struct collect_commitables_arg cc_arg;
4207 struct got_pathlist_entry *pe;
4208 struct got_reference *head_ref = NULL;
4209 struct got_object_id *head_commit_id = NULL;
4210 int have_staged_files = 0;
4212 *new_commit_id = NULL;
4214 TAILQ_INIT(&commitable_paths);
4216 err = lock_worktree(worktree, LOCK_EX);
4217 if (err)
4218 goto done;
4220 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4221 if (err)
4222 goto done;
4224 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4225 if (err)
4226 goto done;
4228 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4229 if (err)
4230 goto done;
4232 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4233 &have_staged_files);
4234 if (err && err->code != GOT_ERR_CANCELLED)
4235 goto done;
4236 if (have_staged_files) {
4237 err = check_non_staged_files(fileindex, paths);
4238 if (err)
4239 goto done;
4242 cc_arg.commitable_paths = &commitable_paths;
4243 cc_arg.worktree = worktree;
4244 cc_arg.repo = repo;
4245 cc_arg.have_staged_files = have_staged_files;
4246 TAILQ_FOREACH(pe, paths, entry) {
4247 err = worktree_status(worktree, pe->path, fileindex, repo,
4248 collect_commitables, &cc_arg, NULL, NULL);
4249 if (err)
4250 goto done;
4253 if (TAILQ_EMPTY(&commitable_paths)) {
4254 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4255 goto done;
4258 TAILQ_FOREACH(pe, paths, entry) {
4259 err = check_path_is_commitable(pe->path, &commitable_paths);
4260 if (err)
4261 goto done;
4264 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4265 struct got_commitable *ct = pe->data;
4266 const char *ct_path = ct->in_repo_path;
4268 while (ct_path[0] == '/')
4269 ct_path++;
4270 err = check_out_of_date(ct_path, ct->status,
4271 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4272 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4273 if (err)
4274 goto done;
4278 err = commit_worktree(new_commit_id, &commitable_paths,
4279 head_commit_id, worktree, author, committer,
4280 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4281 if (err)
4282 goto done;
4284 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4285 fileindex);
4286 sync_err = sync_fileindex(fileindex, fileindex_path);
4287 if (sync_err && err == NULL)
4288 err = sync_err;
4289 done:
4290 if (fileindex)
4291 got_fileindex_free(fileindex);
4292 free(fileindex_path);
4293 unlockerr = lock_worktree(worktree, LOCK_SH);
4294 if (unlockerr && err == NULL)
4295 err = unlockerr;
4296 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4297 struct got_commitable *ct = pe->data;
4298 free_commitable(ct);
4300 got_pathlist_free(&commitable_paths);
4301 return err;
4304 const char *
4305 got_commitable_get_path(struct got_commitable *ct)
4307 return ct->path;
4310 unsigned int
4311 got_commitable_get_status(struct got_commitable *ct)
4313 return ct->status;
4316 struct check_rebase_ok_arg {
4317 struct got_worktree *worktree;
4318 struct got_repository *repo;
4321 static const struct got_error *
4322 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4324 const struct got_error *err = NULL;
4325 struct check_rebase_ok_arg *a = arg;
4326 unsigned char status;
4327 struct stat sb;
4328 char *ondisk_path;
4330 /* Reject rebase of a work tree with mixed base commits. */
4331 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4332 SHA1_DIGEST_LENGTH))
4333 return got_error(GOT_ERR_MIXED_COMMITS);
4335 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4336 == -1)
4337 return got_error_from_errno("asprintf");
4339 /* Reject rebase of a work tree with modified or staged files. */
4340 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4341 free(ondisk_path);
4342 if (err)
4343 return err;
4345 if (status != GOT_STATUS_NO_CHANGE)
4346 return got_error(GOT_ERR_MODIFIED);
4347 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4348 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4350 return NULL;
4353 const struct got_error *
4354 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4355 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4356 struct got_worktree *worktree, struct got_reference *branch,
4357 struct got_repository *repo)
4359 const struct got_error *err = NULL;
4360 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4361 char *branch_ref_name = NULL;
4362 char *fileindex_path = NULL;
4363 struct check_rebase_ok_arg ok_arg;
4364 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4366 *new_base_branch_ref = NULL;
4367 *tmp_branch = NULL;
4368 *fileindex = NULL;
4370 err = lock_worktree(worktree, LOCK_EX);
4371 if (err)
4372 return err;
4374 err = open_fileindex(fileindex, &fileindex_path, worktree);
4375 if (err)
4376 goto done;
4378 ok_arg.worktree = worktree;
4379 ok_arg.repo = repo;
4380 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4381 &ok_arg);
4382 if (err)
4383 goto done;
4385 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4386 if (err)
4387 goto done;
4389 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4390 if (err)
4391 goto done;
4393 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4394 if (err)
4395 goto done;
4397 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4398 0);
4399 if (err)
4400 goto done;
4402 err = got_ref_alloc_symref(new_base_branch_ref,
4403 new_base_branch_ref_name, wt_branch);
4404 if (err)
4405 goto done;
4406 err = got_ref_write(*new_base_branch_ref, repo);
4407 if (err)
4408 goto done;
4410 /* TODO Lock original branch's ref while rebasing? */
4412 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4413 if (err)
4414 goto done;
4416 err = got_ref_write(branch_ref, repo);
4417 if (err)
4418 goto done;
4420 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4421 worktree->base_commit_id);
4422 if (err)
4423 goto done;
4424 err = got_ref_write(*tmp_branch, repo);
4425 if (err)
4426 goto done;
4428 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4429 if (err)
4430 goto done;
4431 done:
4432 free(fileindex_path);
4433 free(tmp_branch_name);
4434 free(new_base_branch_ref_name);
4435 free(branch_ref_name);
4436 if (branch_ref)
4437 got_ref_close(branch_ref);
4438 if (wt_branch)
4439 got_ref_close(wt_branch);
4440 if (err) {
4441 if (*new_base_branch_ref) {
4442 got_ref_close(*new_base_branch_ref);
4443 *new_base_branch_ref = NULL;
4445 if (*tmp_branch) {
4446 got_ref_close(*tmp_branch);
4447 *tmp_branch = NULL;
4449 if (*fileindex) {
4450 got_fileindex_free(*fileindex);
4451 *fileindex = NULL;
4453 lock_worktree(worktree, LOCK_SH);
4455 return err;
4458 const struct got_error *
4459 got_worktree_rebase_continue(struct got_object_id **commit_id,
4460 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4461 struct got_reference **branch, struct got_fileindex **fileindex,
4462 struct got_worktree *worktree, struct got_repository *repo)
4464 const struct got_error *err;
4465 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4466 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4467 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4468 char *fileindex_path = NULL;
4469 int have_staged_files = 0;
4471 *commit_id = NULL;
4472 *new_base_branch = NULL;
4473 *tmp_branch = NULL;
4474 *branch = NULL;
4475 *fileindex = NULL;
4477 err = lock_worktree(worktree, LOCK_EX);
4478 if (err)
4479 return err;
4481 err = open_fileindex(fileindex, &fileindex_path, worktree);
4482 if (err)
4483 goto done;
4485 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4486 &have_staged_files);
4487 if (err && err->code != GOT_ERR_CANCELLED)
4488 goto done;
4489 if (have_staged_files) {
4490 err = got_error(GOT_ERR_STAGED_PATHS);
4491 goto done;
4494 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4495 if (err)
4496 goto done;
4498 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4499 if (err)
4500 goto done;
4502 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4503 if (err)
4504 goto done;
4506 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4507 if (err)
4508 goto done;
4510 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4511 if (err)
4512 goto done;
4514 err = got_ref_open(branch, repo,
4515 got_ref_get_symref_target(branch_ref), 0);
4516 if (err)
4517 goto done;
4519 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4520 if (err)
4521 goto done;
4523 err = got_ref_resolve(commit_id, repo, commit_ref);
4524 if (err)
4525 goto done;
4527 err = got_ref_open(new_base_branch, repo,
4528 new_base_branch_ref_name, 0);
4529 if (err)
4530 goto done;
4532 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4533 if (err)
4534 goto done;
4535 done:
4536 free(commit_ref_name);
4537 free(branch_ref_name);
4538 free(fileindex_path);
4539 if (commit_ref)
4540 got_ref_close(commit_ref);
4541 if (branch_ref)
4542 got_ref_close(branch_ref);
4543 if (err) {
4544 free(*commit_id);
4545 *commit_id = NULL;
4546 if (*tmp_branch) {
4547 got_ref_close(*tmp_branch);
4548 *tmp_branch = NULL;
4550 if (*new_base_branch) {
4551 got_ref_close(*new_base_branch);
4552 *new_base_branch = NULL;
4554 if (*branch) {
4555 got_ref_close(*branch);
4556 *branch = NULL;
4558 if (*fileindex) {
4559 got_fileindex_free(*fileindex);
4560 *fileindex = NULL;
4562 lock_worktree(worktree, LOCK_SH);
4564 return err;
4567 const struct got_error *
4568 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4570 const struct got_error *err;
4571 char *tmp_branch_name = NULL;
4573 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4574 if (err)
4575 return err;
4577 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4578 free(tmp_branch_name);
4579 return NULL;
4582 static const struct got_error *
4583 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4584 char **logmsg, void *arg)
4586 *logmsg = arg;
4587 return NULL;
4590 static const struct got_error *
4591 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4592 const char *path, struct got_object_id *blob_id,
4593 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4595 return NULL;
4598 struct collect_merged_paths_arg {
4599 got_worktree_checkout_cb progress_cb;
4600 void *progress_arg;
4601 struct got_pathlist_head *merged_paths;
4604 static const struct got_error *
4605 collect_merged_paths(void *arg, unsigned char status, const char *path)
4607 const struct got_error *err;
4608 struct collect_merged_paths_arg *a = arg;
4609 char *p;
4610 struct got_pathlist_entry *new;
4612 err = (*a->progress_cb)(a->progress_arg, status, path);
4613 if (err)
4614 return err;
4616 if (status != GOT_STATUS_MERGE &&
4617 status != GOT_STATUS_ADD &&
4618 status != GOT_STATUS_DELETE &&
4619 status != GOT_STATUS_CONFLICT)
4620 return NULL;
4622 p = strdup(path);
4623 if (p == NULL)
4624 return got_error_from_errno("strdup");
4626 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4627 if (err || new == NULL)
4628 free(p);
4629 return err;
4632 void
4633 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4635 struct got_pathlist_entry *pe;
4637 TAILQ_FOREACH(pe, merged_paths, entry)
4638 free((char *)pe->path);
4640 got_pathlist_free(merged_paths);
4643 static const struct got_error *
4644 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4645 struct got_repository *repo)
4647 const struct got_error *err;
4648 struct got_reference *commit_ref = NULL;
4650 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4651 if (err) {
4652 if (err->code != GOT_ERR_NOT_REF)
4653 goto done;
4654 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4655 if (err)
4656 goto done;
4657 err = got_ref_write(commit_ref, repo);
4658 if (err)
4659 goto done;
4660 } else {
4661 struct got_object_id *stored_id;
4662 int cmp;
4664 err = got_ref_resolve(&stored_id, repo, commit_ref);
4665 if (err)
4666 goto done;
4667 cmp = got_object_id_cmp(commit_id, stored_id);
4668 free(stored_id);
4669 if (cmp != 0) {
4670 err = got_error(GOT_ERR_REBASE_COMMITID);
4671 goto done;
4674 done:
4675 if (commit_ref)
4676 got_ref_close(commit_ref);
4677 return err;
4680 static const struct got_error *
4681 rebase_merge_files(struct got_pathlist_head *merged_paths,
4682 const char *commit_ref_name, struct got_worktree *worktree,
4683 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4684 struct got_object_id *commit_id, struct got_repository *repo,
4685 got_worktree_checkout_cb progress_cb, void *progress_arg,
4686 got_cancel_cb cancel_cb, void *cancel_arg)
4688 const struct got_error *err;
4689 struct got_reference *commit_ref = NULL;
4690 struct collect_merged_paths_arg cmp_arg;
4691 char *fileindex_path;
4693 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4695 err = get_fileindex_path(&fileindex_path, worktree);
4696 if (err)
4697 return err;
4699 cmp_arg.progress_cb = progress_cb;
4700 cmp_arg.progress_arg = progress_arg;
4701 cmp_arg.merged_paths = merged_paths;
4702 err = merge_files(worktree, fileindex, fileindex_path,
4703 parent_commit_id, commit_id, repo, collect_merged_paths,
4704 &cmp_arg, cancel_cb, cancel_arg);
4705 if (commit_ref)
4706 got_ref_close(commit_ref);
4707 return err;
4710 const struct got_error *
4711 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4712 struct got_worktree *worktree, struct got_fileindex *fileindex,
4713 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4714 struct got_repository *repo,
4715 got_worktree_checkout_cb progress_cb, void *progress_arg,
4716 got_cancel_cb cancel_cb, void *cancel_arg)
4718 const struct got_error *err;
4719 char *commit_ref_name;
4721 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4722 if (err)
4723 return err;
4725 err = store_commit_id(commit_ref_name, commit_id, repo);
4726 if (err)
4727 goto done;
4729 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4730 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4731 progress_arg, cancel_cb, cancel_arg);
4732 done:
4733 free(commit_ref_name);
4734 return err;
4737 const struct got_error *
4738 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4739 struct got_worktree *worktree, struct got_fileindex *fileindex,
4740 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4741 struct got_repository *repo,
4742 got_worktree_checkout_cb progress_cb, void *progress_arg,
4743 got_cancel_cb cancel_cb, void *cancel_arg)
4745 const struct got_error *err;
4746 char *commit_ref_name;
4748 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4749 if (err)
4750 return err;
4752 err = store_commit_id(commit_ref_name, commit_id, repo);
4753 if (err)
4754 goto done;
4756 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4757 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4758 progress_arg, cancel_cb, cancel_arg);
4759 done:
4760 free(commit_ref_name);
4761 return err;
4764 static const struct got_error *
4765 rebase_commit(struct got_object_id **new_commit_id,
4766 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4767 struct got_worktree *worktree, struct got_fileindex *fileindex,
4768 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4769 const char *new_logmsg, struct got_repository *repo)
4771 const struct got_error *err, *sync_err;
4772 struct got_pathlist_head commitable_paths;
4773 struct collect_commitables_arg cc_arg;
4774 char *fileindex_path = NULL;
4775 struct got_reference *head_ref = NULL;
4776 struct got_object_id *head_commit_id = NULL;
4777 char *logmsg = NULL;
4779 TAILQ_INIT(&commitable_paths);
4780 *new_commit_id = NULL;
4782 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4784 err = get_fileindex_path(&fileindex_path, worktree);
4785 if (err)
4786 return err;
4788 cc_arg.commitable_paths = &commitable_paths;
4789 cc_arg.worktree = worktree;
4790 cc_arg.repo = repo;
4791 cc_arg.have_staged_files = 0;
4793 * If possible get the status of individual files directly to
4794 * avoid crawling the entire work tree once per rebased commit.
4795 * TODO: Ideally, merged_paths would contain a list of commitables
4796 * we could use so we could skip worktree_status() entirely.
4798 if (merged_paths) {
4799 struct got_pathlist_entry *pe;
4800 if (TAILQ_EMPTY(merged_paths)) {
4801 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4802 goto done;
4804 TAILQ_FOREACH(pe, merged_paths, entry) {
4805 err = worktree_status(worktree, pe->path, fileindex,
4806 repo, collect_commitables, &cc_arg, NULL, NULL);
4807 if (err)
4808 goto done;
4810 } else {
4811 err = worktree_status(worktree, "", fileindex, repo,
4812 collect_commitables, &cc_arg, NULL, NULL);
4813 if (err)
4814 goto done;
4817 if (TAILQ_EMPTY(&commitable_paths)) {
4818 /* No-op change; commit will be elided. */
4819 err = got_ref_delete(commit_ref, repo);
4820 if (err)
4821 goto done;
4822 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4823 goto done;
4826 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4827 if (err)
4828 goto done;
4830 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4831 if (err)
4832 goto done;
4834 if (new_logmsg) {
4835 logmsg = strdup(new_logmsg);
4836 if (logmsg == NULL) {
4837 err = got_error_from_errno("strdup");
4838 goto done;
4840 } else {
4841 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4842 if (err)
4843 goto done;
4846 /* NB: commit_worktree will call free(logmsg) */
4847 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4848 worktree, got_object_commit_get_author(orig_commit),
4849 got_object_commit_get_committer(orig_commit),
4850 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4851 if (err)
4852 goto done;
4854 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4855 if (err)
4856 goto done;
4858 err = got_ref_delete(commit_ref, repo);
4859 if (err)
4860 goto done;
4862 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4863 fileindex);
4864 sync_err = sync_fileindex(fileindex, fileindex_path);
4865 if (sync_err && err == NULL)
4866 err = sync_err;
4867 done:
4868 free(fileindex_path);
4869 free(head_commit_id);
4870 if (head_ref)
4871 got_ref_close(head_ref);
4872 if (err) {
4873 free(*new_commit_id);
4874 *new_commit_id = NULL;
4876 return err;
4879 const struct got_error *
4880 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4881 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4882 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4883 struct got_commit_object *orig_commit,
4884 struct got_object_id *orig_commit_id, struct got_repository *repo)
4886 const struct got_error *err;
4887 char *commit_ref_name;
4888 struct got_reference *commit_ref = NULL;
4889 struct got_object_id *commit_id = NULL;
4891 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4892 if (err)
4893 return err;
4895 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4896 if (err)
4897 goto done;
4898 err = got_ref_resolve(&commit_id, repo, commit_ref);
4899 if (err)
4900 goto done;
4901 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4902 err = got_error(GOT_ERR_REBASE_COMMITID);
4903 goto done;
4906 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4907 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4908 done:
4909 if (commit_ref)
4910 got_ref_close(commit_ref);
4911 free(commit_ref_name);
4912 free(commit_id);
4913 return err;
4916 const struct got_error *
4917 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4918 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4919 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4920 struct got_commit_object *orig_commit,
4921 struct got_object_id *orig_commit_id, const char *new_logmsg,
4922 struct got_repository *repo)
4924 const struct got_error *err;
4925 char *commit_ref_name;
4926 struct got_reference *commit_ref = NULL;
4927 struct got_object_id *commit_id = NULL;
4929 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4930 if (err)
4931 return err;
4933 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4934 if (err)
4935 goto done;
4936 err = got_ref_resolve(&commit_id, repo, commit_ref);
4937 if (err)
4938 goto done;
4939 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4940 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4941 goto done;
4944 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4945 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4946 done:
4947 if (commit_ref)
4948 got_ref_close(commit_ref);
4949 free(commit_ref_name);
4950 free(commit_id);
4951 return err;
4954 const struct got_error *
4955 got_worktree_rebase_postpone(struct got_worktree *worktree,
4956 struct got_fileindex *fileindex)
4958 if (fileindex)
4959 got_fileindex_free(fileindex);
4960 return lock_worktree(worktree, LOCK_SH);
4963 static const struct got_error *
4964 delete_ref(const char *name, struct got_repository *repo)
4966 const struct got_error *err;
4967 struct got_reference *ref;
4969 err = got_ref_open(&ref, repo, name, 0);
4970 if (err) {
4971 if (err->code == GOT_ERR_NOT_REF)
4972 return NULL;
4973 return err;
4976 err = got_ref_delete(ref, repo);
4977 got_ref_close(ref);
4978 return err;
4981 static const struct got_error *
4982 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4984 const struct got_error *err;
4985 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4986 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4988 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4989 if (err)
4990 goto done;
4991 err = delete_ref(tmp_branch_name, repo);
4992 if (err)
4993 goto done;
4995 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4996 if (err)
4997 goto done;
4998 err = delete_ref(new_base_branch_ref_name, repo);
4999 if (err)
5000 goto done;
5002 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5003 if (err)
5004 goto done;
5005 err = delete_ref(branch_ref_name, repo);
5006 if (err)
5007 goto done;
5009 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5010 if (err)
5011 goto done;
5012 err = delete_ref(commit_ref_name, repo);
5013 if (err)
5014 goto done;
5016 done:
5017 free(tmp_branch_name);
5018 free(new_base_branch_ref_name);
5019 free(branch_ref_name);
5020 free(commit_ref_name);
5021 return err;
5024 const struct got_error *
5025 got_worktree_rebase_complete(struct got_worktree *worktree,
5026 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5027 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5028 struct got_repository *repo)
5030 const struct got_error *err, *unlockerr;
5031 struct got_object_id *new_head_commit_id = NULL;
5033 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5034 if (err)
5035 return err;
5037 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5038 if (err)
5039 goto done;
5041 err = got_ref_write(rebased_branch, repo);
5042 if (err)
5043 goto done;
5045 err = got_worktree_set_head_ref(worktree, rebased_branch);
5046 if (err)
5047 goto done;
5049 err = delete_rebase_refs(worktree, repo);
5050 done:
5051 if (fileindex)
5052 got_fileindex_free(fileindex);
5053 free(new_head_commit_id);
5054 unlockerr = lock_worktree(worktree, LOCK_SH);
5055 if (unlockerr && err == NULL)
5056 err = unlockerr;
5057 return err;
5060 const struct got_error *
5061 got_worktree_rebase_abort(struct got_worktree *worktree,
5062 struct got_fileindex *fileindex, struct got_repository *repo,
5063 struct got_reference *new_base_branch,
5064 got_worktree_checkout_cb progress_cb, void *progress_arg)
5066 const struct got_error *err, *unlockerr, *sync_err;
5067 struct got_reference *resolved = NULL;
5068 struct got_object_id *commit_id = NULL;
5069 char *fileindex_path = NULL;
5070 struct revert_file_args rfa;
5071 struct got_object_id *tree_id = NULL;
5073 err = lock_worktree(worktree, LOCK_EX);
5074 if (err)
5075 return err;
5077 err = got_ref_open(&resolved, repo,
5078 got_ref_get_symref_target(new_base_branch), 0);
5079 if (err)
5080 goto done;
5082 err = got_worktree_set_head_ref(worktree, resolved);
5083 if (err)
5084 goto done;
5087 * XXX commits to the base branch could have happened while
5088 * we were busy rebasing; should we store the original commit ID
5089 * when rebase begins and read it back here?
5091 err = got_ref_resolve(&commit_id, repo, resolved);
5092 if (err)
5093 goto done;
5095 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5096 if (err)
5097 goto done;
5099 err = got_object_id_by_path(&tree_id, repo,
5100 worktree->base_commit_id, worktree->path_prefix);
5101 if (err)
5102 goto done;
5104 err = delete_rebase_refs(worktree, repo);
5105 if (err)
5106 goto done;
5108 err = get_fileindex_path(&fileindex_path, worktree);
5109 if (err)
5110 goto done;
5112 rfa.worktree = worktree;
5113 rfa.fileindex = fileindex;
5114 rfa.progress_cb = progress_cb;
5115 rfa.progress_arg = progress_arg;
5116 rfa.patch_cb = NULL;
5117 rfa.patch_arg = NULL;
5118 rfa.repo = repo;
5119 err = worktree_status(worktree, "", fileindex, repo,
5120 revert_file, &rfa, NULL, NULL);
5121 if (err)
5122 goto sync;
5124 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5125 repo, progress_cb, progress_arg, NULL, NULL);
5126 sync:
5127 sync_err = sync_fileindex(fileindex, fileindex_path);
5128 if (sync_err && err == NULL)
5129 err = sync_err;
5130 done:
5131 got_ref_close(resolved);
5132 free(tree_id);
5133 free(commit_id);
5134 if (fileindex)
5135 got_fileindex_free(fileindex);
5136 free(fileindex_path);
5138 unlockerr = lock_worktree(worktree, LOCK_SH);
5139 if (unlockerr && err == NULL)
5140 err = unlockerr;
5141 return err;
5144 const struct got_error *
5145 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5146 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5147 struct got_fileindex **fileindex, struct got_worktree *worktree,
5148 struct got_repository *repo)
5150 const struct got_error *err = NULL;
5151 char *tmp_branch_name = NULL;
5152 char *branch_ref_name = NULL;
5153 char *base_commit_ref_name = NULL;
5154 char *fileindex_path = NULL;
5155 struct check_rebase_ok_arg ok_arg;
5156 struct got_reference *wt_branch = NULL;
5157 struct got_reference *base_commit_ref = NULL;
5159 *tmp_branch = NULL;
5160 *branch_ref = NULL;
5161 *base_commit_id = NULL;
5162 *fileindex = NULL;
5164 err = lock_worktree(worktree, LOCK_EX);
5165 if (err)
5166 return err;
5168 err = open_fileindex(fileindex, &fileindex_path, worktree);
5169 if (err)
5170 goto done;
5172 ok_arg.worktree = worktree;
5173 ok_arg.repo = repo;
5174 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5175 &ok_arg);
5176 if (err)
5177 goto done;
5179 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5180 if (err)
5181 goto done;
5183 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5184 if (err)
5185 goto done;
5187 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5188 worktree);
5189 if (err)
5190 goto done;
5192 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5193 0);
5194 if (err)
5195 goto done;
5197 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5198 if (err)
5199 goto done;
5201 err = got_ref_write(*branch_ref, repo);
5202 if (err)
5203 goto done;
5205 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5206 worktree->base_commit_id);
5207 if (err)
5208 goto done;
5209 err = got_ref_write(base_commit_ref, repo);
5210 if (err)
5211 goto done;
5212 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5213 if (*base_commit_id == NULL) {
5214 err = got_error_from_errno("got_object_id_dup");
5215 goto done;
5218 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5219 worktree->base_commit_id);
5220 if (err)
5221 goto done;
5222 err = got_ref_write(*tmp_branch, repo);
5223 if (err)
5224 goto done;
5226 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5227 if (err)
5228 goto done;
5229 done:
5230 free(fileindex_path);
5231 free(tmp_branch_name);
5232 free(branch_ref_name);
5233 free(base_commit_ref_name);
5234 if (wt_branch)
5235 got_ref_close(wt_branch);
5236 if (err) {
5237 if (*branch_ref) {
5238 got_ref_close(*branch_ref);
5239 *branch_ref = NULL;
5241 if (*tmp_branch) {
5242 got_ref_close(*tmp_branch);
5243 *tmp_branch = NULL;
5245 free(*base_commit_id);
5246 if (*fileindex) {
5247 got_fileindex_free(*fileindex);
5248 *fileindex = NULL;
5250 lock_worktree(worktree, LOCK_SH);
5252 return err;
5255 const struct got_error *
5256 got_worktree_histedit_postpone(struct got_worktree *worktree,
5257 struct got_fileindex *fileindex)
5259 if (fileindex)
5260 got_fileindex_free(fileindex);
5261 return lock_worktree(worktree, LOCK_SH);
5264 const struct got_error *
5265 got_worktree_histedit_in_progress(int *in_progress,
5266 struct got_worktree *worktree)
5268 const struct got_error *err;
5269 char *tmp_branch_name = NULL;
5271 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5272 if (err)
5273 return err;
5275 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5276 free(tmp_branch_name);
5277 return NULL;
5280 const struct got_error *
5281 got_worktree_histedit_continue(struct got_object_id **commit_id,
5282 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5283 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5284 struct got_worktree *worktree, struct got_repository *repo)
5286 const struct got_error *err;
5287 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5288 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5289 struct got_reference *commit_ref = NULL;
5290 struct got_reference *base_commit_ref = NULL;
5291 char *fileindex_path = NULL;
5292 int have_staged_files = 0;
5294 *commit_id = NULL;
5295 *tmp_branch = NULL;
5296 *base_commit_id = NULL;
5297 *fileindex = NULL;
5299 err = lock_worktree(worktree, LOCK_EX);
5300 if (err)
5301 return err;
5303 err = open_fileindex(fileindex, &fileindex_path, worktree);
5304 if (err)
5305 goto done;
5307 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5308 &have_staged_files);
5309 if (err && err->code != GOT_ERR_CANCELLED)
5310 goto done;
5311 if (have_staged_files) {
5312 err = got_error(GOT_ERR_STAGED_PATHS);
5313 goto done;
5316 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5317 if (err)
5318 goto done;
5320 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5321 if (err)
5322 goto done;
5324 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5325 if (err)
5326 goto done;
5328 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5329 worktree);
5330 if (err)
5331 goto done;
5333 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5334 if (err)
5335 goto done;
5337 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5338 if (err)
5339 goto done;
5340 err = got_ref_resolve(commit_id, repo, commit_ref);
5341 if (err)
5342 goto done;
5344 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5345 if (err)
5346 goto done;
5347 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5348 if (err)
5349 goto done;
5351 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5352 if (err)
5353 goto done;
5354 done:
5355 free(commit_ref_name);
5356 free(branch_ref_name);
5357 free(fileindex_path);
5358 if (commit_ref)
5359 got_ref_close(commit_ref);
5360 if (base_commit_ref)
5361 got_ref_close(base_commit_ref);
5362 if (err) {
5363 free(*commit_id);
5364 *commit_id = NULL;
5365 free(*base_commit_id);
5366 *base_commit_id = NULL;
5367 if (*tmp_branch) {
5368 got_ref_close(*tmp_branch);
5369 *tmp_branch = NULL;
5371 if (*fileindex) {
5372 got_fileindex_free(*fileindex);
5373 *fileindex = NULL;
5375 lock_worktree(worktree, LOCK_EX);
5377 return err;
5380 static const struct got_error *
5381 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5383 const struct got_error *err;
5384 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5385 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5387 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5388 if (err)
5389 goto done;
5390 err = delete_ref(tmp_branch_name, repo);
5391 if (err)
5392 goto done;
5394 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5395 worktree);
5396 if (err)
5397 goto done;
5398 err = delete_ref(base_commit_ref_name, repo);
5399 if (err)
5400 goto done;
5402 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5403 if (err)
5404 goto done;
5405 err = delete_ref(branch_ref_name, repo);
5406 if (err)
5407 goto done;
5409 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5410 if (err)
5411 goto done;
5412 err = delete_ref(commit_ref_name, repo);
5413 if (err)
5414 goto done;
5415 done:
5416 free(tmp_branch_name);
5417 free(base_commit_ref_name);
5418 free(branch_ref_name);
5419 free(commit_ref_name);
5420 return err;
5423 const struct got_error *
5424 got_worktree_histedit_abort(struct got_worktree *worktree,
5425 struct got_fileindex *fileindex, struct got_repository *repo,
5426 struct got_reference *branch, struct got_object_id *base_commit_id,
5427 got_worktree_checkout_cb progress_cb, void *progress_arg)
5429 const struct got_error *err, *unlockerr, *sync_err;
5430 struct got_reference *resolved = NULL;
5431 char *fileindex_path = NULL;
5432 struct got_object_id *tree_id = NULL;
5433 struct revert_file_args rfa;
5435 err = lock_worktree(worktree, LOCK_EX);
5436 if (err)
5437 return err;
5439 err = got_ref_open(&resolved, repo,
5440 got_ref_get_symref_target(branch), 0);
5441 if (err)
5442 goto done;
5444 err = got_worktree_set_head_ref(worktree, resolved);
5445 if (err)
5446 goto done;
5448 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5449 if (err)
5450 goto done;
5452 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5453 worktree->path_prefix);
5454 if (err)
5455 goto done;
5457 err = delete_histedit_refs(worktree, repo);
5458 if (err)
5459 goto done;
5461 err = get_fileindex_path(&fileindex_path, worktree);
5462 if (err)
5463 goto done;
5465 rfa.worktree = worktree;
5466 rfa.fileindex = fileindex;
5467 rfa.progress_cb = progress_cb;
5468 rfa.progress_arg = progress_arg;
5469 rfa.patch_cb = NULL;
5470 rfa.patch_arg = NULL;
5471 rfa.repo = repo;
5472 err = worktree_status(worktree, "", fileindex, repo,
5473 revert_file, &rfa, NULL, NULL);
5474 if (err)
5475 goto sync;
5477 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5478 repo, progress_cb, progress_arg, NULL, NULL);
5479 sync:
5480 sync_err = sync_fileindex(fileindex, fileindex_path);
5481 if (sync_err && err == NULL)
5482 err = sync_err;
5483 done:
5484 got_ref_close(resolved);
5485 free(tree_id);
5486 free(fileindex_path);
5488 unlockerr = lock_worktree(worktree, LOCK_SH);
5489 if (unlockerr && err == NULL)
5490 err = unlockerr;
5491 return err;
5494 const struct got_error *
5495 got_worktree_histedit_complete(struct got_worktree *worktree,
5496 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5497 struct got_reference *edited_branch, struct got_repository *repo)
5499 const struct got_error *err, *unlockerr;
5500 struct got_object_id *new_head_commit_id = NULL;
5501 struct got_reference *resolved = NULL;
5503 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5504 if (err)
5505 return err;
5507 err = got_ref_open(&resolved, repo,
5508 got_ref_get_symref_target(edited_branch), 0);
5509 if (err)
5510 goto done;
5512 err = got_ref_change_ref(resolved, new_head_commit_id);
5513 if (err)
5514 goto done;
5516 err = got_ref_write(resolved, repo);
5517 if (err)
5518 goto done;
5520 err = got_worktree_set_head_ref(worktree, resolved);
5521 if (err)
5522 goto done;
5524 err = delete_histedit_refs(worktree, repo);
5525 done:
5526 if (fileindex)
5527 got_fileindex_free(fileindex);
5528 free(new_head_commit_id);
5529 unlockerr = lock_worktree(worktree, LOCK_SH);
5530 if (unlockerr && err == NULL)
5531 err = unlockerr;
5532 return err;
5535 const struct got_error *
5536 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5537 struct got_object_id *commit_id, struct got_repository *repo)
5539 const struct got_error *err;
5540 char *commit_ref_name;
5542 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5543 if (err)
5544 return err;
5546 err = store_commit_id(commit_ref_name, commit_id, repo);
5547 if (err)
5548 goto done;
5550 err = delete_ref(commit_ref_name, repo);
5551 done:
5552 free(commit_ref_name);
5553 return err;
5556 struct check_stage_ok_arg {
5557 struct got_object_id *head_commit_id;
5558 struct got_worktree *worktree;
5559 struct got_fileindex *fileindex;
5560 struct got_repository *repo;
5561 int have_changes;
5564 const struct got_error *
5565 check_stage_ok(void *arg, unsigned char status,
5566 unsigned char staged_status, const char *relpath,
5567 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5568 struct got_object_id *commit_id)
5570 struct check_stage_ok_arg *a = arg;
5571 const struct got_error *err = NULL;
5572 struct got_fileindex_entry *ie;
5573 struct got_object_id base_commit_id;
5574 struct got_object_id *base_commit_idp = NULL;
5575 char *in_repo_path = NULL, *p;
5577 if (status == GOT_STATUS_UNVERSIONED)
5578 return NULL;
5579 if (status == GOT_STATUS_NONEXISTENT)
5580 return got_error_set_errno(ENOENT, relpath);
5582 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5583 if (ie == NULL)
5584 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5586 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5587 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5588 relpath) == -1)
5589 return got_error_from_errno("asprintf");
5591 if (got_fileindex_entry_has_commit(ie)) {
5592 memcpy(base_commit_id.sha1, ie->commit_sha1,
5593 SHA1_DIGEST_LENGTH);
5594 base_commit_idp = &base_commit_id;
5597 if (status == GOT_STATUS_NO_CHANGE) {
5598 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5599 goto done;
5600 } else if (status == GOT_STATUS_CONFLICT) {
5601 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5602 goto done;
5603 } else if (status != GOT_STATUS_ADD &&
5604 status != GOT_STATUS_MODIFY &&
5605 status != GOT_STATUS_DELETE) {
5606 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5607 goto done;
5610 a->have_changes = 1;
5612 p = in_repo_path;
5613 while (p[0] == '/')
5614 p++;
5615 err = check_out_of_date(p, status, staged_status,
5616 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5617 GOT_ERR_STAGE_OUT_OF_DATE);
5618 done:
5619 free(in_repo_path);
5620 return err;
5623 struct stage_path_arg {
5624 struct got_worktree *worktree;
5625 struct got_fileindex *fileindex;
5626 struct got_repository *repo;
5627 got_worktree_status_cb status_cb;
5628 void *status_arg;
5629 got_worktree_patch_cb patch_cb;
5630 void *patch_arg;
5633 static const struct got_error *
5634 stage_path(void *arg, unsigned char status,
5635 unsigned char staged_status, const char *relpath,
5636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5637 struct got_object_id *commit_id)
5639 struct stage_path_arg *a = arg;
5640 const struct got_error *err = NULL;
5641 struct got_fileindex_entry *ie;
5642 char *ondisk_path = NULL, *path_content = NULL;
5643 uint32_t stage;
5644 struct got_object_id *new_staged_blob_id = NULL;
5646 if (status == GOT_STATUS_UNVERSIONED)
5647 return NULL;
5649 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5650 if (ie == NULL)
5651 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5653 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5654 relpath)== -1)
5655 return got_error_from_errno("asprintf");
5657 switch (status) {
5658 case GOT_STATUS_ADD:
5659 case GOT_STATUS_MODIFY:
5660 if (a->patch_cb) {
5661 if (status == GOT_STATUS_ADD) {
5662 int choice = GOT_PATCH_CHOICE_NONE;
5663 err = (*a->patch_cb)(&choice, a->patch_arg,
5664 status, ie->path, NULL, 1, 1);
5665 if (err)
5666 break;
5667 if (choice != GOT_PATCH_CHOICE_YES)
5668 break;
5669 } else {
5670 err = create_patched_content(&path_content, 0,
5671 staged_blob_id ? staged_blob_id : blob_id,
5672 ondisk_path, ie->path, a->repo,
5673 a->patch_cb, a->patch_arg);
5674 if (err || path_content == NULL)
5675 break;
5678 err = got_object_blob_create(&new_staged_blob_id,
5679 path_content ? path_content : ondisk_path, a->repo);
5680 if (err)
5681 break;
5682 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5683 SHA1_DIGEST_LENGTH);
5684 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5685 stage = GOT_FILEIDX_STAGE_ADD;
5686 else
5687 stage = GOT_FILEIDX_STAGE_MODIFY;
5688 got_fileindex_entry_stage_set(ie, stage);
5689 if (a->status_cb == NULL)
5690 break;
5691 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5692 get_staged_status(ie), relpath, blob_id,
5693 new_staged_blob_id, NULL);
5694 break;
5695 case GOT_STATUS_DELETE:
5696 if (staged_status == GOT_STATUS_DELETE)
5697 break;
5698 if (a->patch_cb) {
5699 int choice = GOT_PATCH_CHOICE_NONE;
5700 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5701 ie->path, NULL, 1, 1);
5702 if (err)
5703 break;
5704 if (choice == GOT_PATCH_CHOICE_NO)
5705 break;
5706 if (choice != GOT_PATCH_CHOICE_YES) {
5707 err = got_error(GOT_ERR_PATCH_CHOICE);
5708 break;
5711 stage = GOT_FILEIDX_STAGE_DELETE;
5712 got_fileindex_entry_stage_set(ie, stage);
5713 if (a->status_cb == NULL)
5714 break;
5715 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5716 get_staged_status(ie), relpath, NULL, NULL, NULL);
5717 break;
5718 case GOT_STATUS_NO_CHANGE:
5719 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5720 break;
5721 case GOT_STATUS_CONFLICT:
5722 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5723 break;
5724 case GOT_STATUS_NONEXISTENT:
5725 err = got_error_set_errno(ENOENT, relpath);
5726 break;
5727 default:
5728 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5729 break;
5732 if (path_content && unlink(path_content) == -1 && err == NULL)
5733 err = got_error_from_errno2("unlink", path_content);
5734 free(path_content);
5735 free(ondisk_path);
5736 free(new_staged_blob_id);
5737 return err;
5740 const struct got_error *
5741 got_worktree_stage(struct got_worktree *worktree,
5742 struct got_pathlist_head *paths,
5743 got_worktree_status_cb status_cb, void *status_arg,
5744 got_worktree_patch_cb patch_cb, void *patch_arg,
5745 struct got_repository *repo)
5747 const struct got_error *err = NULL, *sync_err, *unlockerr;
5748 struct got_pathlist_entry *pe;
5749 struct got_fileindex *fileindex = NULL;
5750 char *fileindex_path = NULL;
5751 struct got_reference *head_ref = NULL;
5752 struct got_object_id *head_commit_id = NULL;
5753 struct check_stage_ok_arg oka;
5754 struct stage_path_arg spa;
5756 err = lock_worktree(worktree, LOCK_EX);
5757 if (err)
5758 return err;
5760 err = got_ref_open(&head_ref, repo,
5761 got_worktree_get_head_ref_name(worktree), 0);
5762 if (err)
5763 goto done;
5764 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5765 if (err)
5766 goto done;
5767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5768 if (err)
5769 goto done;
5771 /* Check pre-conditions before staging anything. */
5772 oka.head_commit_id = head_commit_id;
5773 oka.worktree = worktree;
5774 oka.fileindex = fileindex;
5775 oka.repo = repo;
5776 oka.have_changes = 0;
5777 TAILQ_FOREACH(pe, paths, entry) {
5778 err = worktree_status(worktree, pe->path, fileindex, repo,
5779 check_stage_ok, &oka, NULL, NULL);
5780 if (err)
5781 goto done;
5783 if (!oka.have_changes) {
5784 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5785 goto done;
5788 spa.worktree = worktree;
5789 spa.fileindex = fileindex;
5790 spa.repo = repo;
5791 spa.patch_cb = patch_cb;
5792 spa.patch_arg = patch_arg;
5793 spa.status_cb = status_cb;
5794 spa.status_arg = status_arg;
5795 TAILQ_FOREACH(pe, paths, entry) {
5796 err = worktree_status(worktree, pe->path, fileindex, repo,
5797 stage_path, &spa, NULL, NULL);
5798 if (err)
5799 goto done;
5802 sync_err = sync_fileindex(fileindex, fileindex_path);
5803 if (sync_err && err == NULL)
5804 err = sync_err;
5805 done:
5806 if (head_ref)
5807 got_ref_close(head_ref);
5808 free(head_commit_id);
5809 free(fileindex_path);
5810 if (fileindex)
5811 got_fileindex_free(fileindex);
5812 unlockerr = lock_worktree(worktree, LOCK_SH);
5813 if (unlockerr && err == NULL)
5814 err = unlockerr;
5815 return err;
5818 struct unstage_path_arg {
5819 struct got_worktree *worktree;
5820 struct got_fileindex *fileindex;
5821 struct got_repository *repo;
5822 got_worktree_checkout_cb progress_cb;
5823 void *progress_arg;
5824 got_worktree_patch_cb patch_cb;
5825 void *patch_arg;
5828 static const struct got_error *
5829 create_unstaged_content(char **path_unstaged_content,
5830 char **path_new_staged_content, struct got_object_id *blob_id,
5831 struct got_object_id *staged_blob_id, const char *relpath,
5832 struct got_repository *repo,
5833 got_worktree_patch_cb patch_cb, void *patch_arg)
5835 const struct got_error *err;
5836 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5837 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5838 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5839 struct stat sb1, sb2;
5840 struct got_diff_changes *changes = NULL;
5841 struct got_diff_state *ds = NULL;
5842 struct got_diff_args *args = NULL;
5843 struct got_diff_change *change;
5844 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5845 int have_content = 0, have_rejected_content = 0;
5847 *path_unstaged_content = NULL;
5848 *path_new_staged_content = NULL;
5850 err = got_object_id_str(&label1, blob_id);
5851 if (err)
5852 return err;
5853 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5854 if (err)
5855 goto done;
5857 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5858 if (err)
5859 goto done;
5861 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5862 if (err)
5863 goto done;
5865 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5866 if (err)
5867 goto done;
5869 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5870 if (err)
5871 goto done;
5873 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5874 if (err)
5875 goto done;
5877 if (stat(path1, &sb1) == -1) {
5878 err = got_error_from_errno2("stat", path1);
5879 goto done;
5882 if (stat(path2, &sb2) == -1) {
5883 err = got_error_from_errno2("stat", path2);
5884 goto done;
5887 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5888 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5889 if (err)
5890 goto done;
5892 err = got_opentemp_named(path_unstaged_content, &outfile,
5893 "got-unstaged-content");
5894 if (err)
5895 goto done;
5896 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5897 "got-new-staged-content");
5898 if (err)
5899 goto done;
5901 if (fseek(f1, 0L, SEEK_SET) == -1) {
5902 err = got_ferror(f1, GOT_ERR_IO);
5903 goto done;
5905 if (fseek(f2, 0L, SEEK_SET) == -1) {
5906 err = got_ferror(f2, GOT_ERR_IO);
5907 goto done;
5909 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5910 int choice;
5911 err = apply_or_reject_change(&choice, change, ++n,
5912 changes->nchanges, ds, args, diff_flags, relpath,
5913 f1, f2, &line_cur1, &line_cur2,
5914 outfile, rejectfile, patch_cb, patch_arg);
5915 if (err)
5916 goto done;
5917 if (choice == GOT_PATCH_CHOICE_YES)
5918 have_content = 1;
5919 else
5920 have_rejected_content = 1;
5921 if (choice == GOT_PATCH_CHOICE_QUIT)
5922 break;
5924 if (have_content || have_rejected_content)
5925 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5926 outfile, rejectfile);
5927 done:
5928 free(label1);
5929 if (blob)
5930 got_object_blob_close(blob);
5931 if (staged_blob)
5932 got_object_blob_close(staged_blob);
5933 if (f1 && fclose(f1) == EOF && err == NULL)
5934 err = got_error_from_errno2("fclose", path1);
5935 if (f2 && fclose(f2) == EOF && err == NULL)
5936 err = got_error_from_errno2("fclose", path2);
5937 if (outfile && fclose(outfile) == EOF && err == NULL)
5938 err = got_error_from_errno2("fclose", *path_unstaged_content);
5939 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5940 err = got_error_from_errno2("fclose", *path_new_staged_content);
5941 if (path1 && unlink(path1) == -1 && err == NULL)
5942 err = got_error_from_errno2("unlink", path1);
5943 if (path2 && unlink(path2) == -1 && err == NULL)
5944 err = got_error_from_errno2("unlink", path2);
5945 if (err || !have_content) {
5946 if (*path_unstaged_content &&
5947 unlink(*path_unstaged_content) == -1 && err == NULL)
5948 err = got_error_from_errno2("unlink",
5949 *path_unstaged_content);
5950 free(*path_unstaged_content);
5951 *path_unstaged_content = NULL;
5953 if (err || !have_rejected_content) {
5954 if (*path_new_staged_content &&
5955 unlink(*path_new_staged_content) == -1 && err == NULL)
5956 err = got_error_from_errno2("unlink",
5957 *path_new_staged_content);
5958 free(*path_new_staged_content);
5959 *path_new_staged_content = NULL;
5961 free(args);
5962 if (ds) {
5963 got_diff_state_free(ds);
5964 free(ds);
5966 if (changes)
5967 got_diff_free_changes(changes);
5968 free(path1);
5969 free(path2);
5970 return err;
5973 static const struct got_error *
5974 unstage_path(void *arg, unsigned char status,
5975 unsigned char staged_status, const char *relpath,
5976 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5977 struct got_object_id *commit_id)
5979 const struct got_error *err = NULL;
5980 struct unstage_path_arg *a = arg;
5981 struct got_fileindex_entry *ie;
5982 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5983 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5984 char *path_new_staged_content = NULL;
5985 int local_changes_subsumed;
5986 struct stat sb;
5988 if (staged_status != GOT_STATUS_ADD &&
5989 staged_status != GOT_STATUS_MODIFY &&
5990 staged_status != GOT_STATUS_DELETE)
5991 return NULL;
5993 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5994 if (ie == NULL)
5995 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5998 == -1)
5999 return got_error_from_errno("asprintf");
6001 switch (staged_status) {
6002 case GOT_STATUS_MODIFY:
6003 err = got_object_open_as_blob(&blob_base, a->repo,
6004 blob_id, 8192);
6005 if (err)
6006 break;
6007 /* fall through */
6008 case GOT_STATUS_ADD:
6009 if (a->patch_cb) {
6010 if (staged_status == GOT_STATUS_ADD) {
6011 int choice = GOT_PATCH_CHOICE_NONE;
6012 err = (*a->patch_cb)(&choice, a->patch_arg,
6013 staged_status, ie->path, NULL, 1, 1);
6014 if (err)
6015 break;
6016 if (choice != GOT_PATCH_CHOICE_YES)
6017 break;
6018 } else {
6019 err = create_unstaged_content(
6020 &path_unstaged_content,
6021 &path_new_staged_content, blob_id,
6022 staged_blob_id, ie->path, a->repo,
6023 a->patch_cb, a->patch_arg);
6024 if (err || path_unstaged_content == NULL)
6025 break;
6026 if (path_new_staged_content) {
6027 err = got_object_blob_create(
6028 &staged_blob_id,
6029 path_new_staged_content,
6030 a->repo);
6031 if (err)
6032 break;
6033 memcpy(ie->staged_blob_sha1,
6034 staged_blob_id->sha1,
6035 SHA1_DIGEST_LENGTH);
6037 err = merge_file(&local_changes_subsumed,
6038 a->worktree, blob_base, ondisk_path,
6039 relpath, got_fileindex_perms_to_st(ie),
6040 path_unstaged_content, "unstaged",
6041 a->repo, a->progress_cb, a->progress_arg);
6042 if (err == NULL &&
6043 path_new_staged_content == NULL)
6044 got_fileindex_entry_stage_set(ie,
6045 GOT_FILEIDX_STAGE_NONE);
6046 break; /* Done with this file. */
6049 err = got_object_open_as_blob(&blob_staged, a->repo,
6050 staged_blob_id, 8192);
6051 if (err)
6052 break;
6053 err = merge_blob(&local_changes_subsumed, a->worktree,
6054 blob_base, ondisk_path, relpath,
6055 got_fileindex_perms_to_st(ie), blob_staged,
6056 commit_id ? commit_id : a->worktree->base_commit_id,
6057 a->repo, a->progress_cb, a->progress_arg);
6058 if (err == NULL)
6059 got_fileindex_entry_stage_set(ie,
6060 GOT_FILEIDX_STAGE_NONE);
6061 break;
6062 case GOT_STATUS_DELETE:
6063 if (a->patch_cb) {
6064 int choice = GOT_PATCH_CHOICE_NONE;
6065 err = (*a->patch_cb)(&choice, a->patch_arg,
6066 staged_status, ie->path, NULL, 1, 1);
6067 if (err)
6068 break;
6069 if (choice == GOT_PATCH_CHOICE_NO)
6070 break;
6071 if (choice != GOT_PATCH_CHOICE_YES) {
6072 err = got_error(GOT_ERR_PATCH_CHOICE);
6073 break;
6076 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6077 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6078 if (err)
6079 break;
6080 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6081 break;
6084 free(ondisk_path);
6085 if (path_unstaged_content &&
6086 unlink(path_unstaged_content) == -1 && err == NULL)
6087 err = got_error_from_errno2("unlink", path_unstaged_content);
6088 if (path_new_staged_content &&
6089 unlink(path_new_staged_content) == -1 && err == NULL)
6090 err = got_error_from_errno2("unlink", path_new_staged_content);
6091 free(path_unstaged_content);
6092 free(path_new_staged_content);
6093 if (blob_base)
6094 got_object_blob_close(blob_base);
6095 if (blob_staged)
6096 got_object_blob_close(blob_staged);
6097 return err;
6100 const struct got_error *
6101 got_worktree_unstage(struct got_worktree *worktree,
6102 struct got_pathlist_head *paths,
6103 got_worktree_checkout_cb progress_cb, void *progress_arg,
6104 got_worktree_patch_cb patch_cb, void *patch_arg,
6105 struct got_repository *repo)
6107 const struct got_error *err = NULL, *sync_err, *unlockerr;
6108 struct got_pathlist_entry *pe;
6109 struct got_fileindex *fileindex = NULL;
6110 char *fileindex_path = NULL;
6111 struct unstage_path_arg upa;
6113 err = lock_worktree(worktree, LOCK_EX);
6114 if (err)
6115 return err;
6117 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6118 if (err)
6119 goto done;
6121 upa.worktree = worktree;
6122 upa.fileindex = fileindex;
6123 upa.repo = repo;
6124 upa.progress_cb = progress_cb;
6125 upa.progress_arg = progress_arg;
6126 upa.patch_cb = patch_cb;
6127 upa.patch_arg = patch_arg;
6128 TAILQ_FOREACH(pe, paths, entry) {
6129 err = worktree_status(worktree, pe->path, fileindex, repo,
6130 unstage_path, &upa, NULL, NULL);
6131 if (err)
6132 goto done;
6135 sync_err = sync_fileindex(fileindex, fileindex_path);
6136 if (sync_err && err == NULL)
6137 err = sync_err;
6138 done:
6139 free(fileindex_path);
6140 if (fileindex)
6141 got_fileindex_free(fileindex);
6142 unlockerr = lock_worktree(worktree, LOCK_SH);
6143 if (unlockerr && err == NULL)
6144 err = unlockerr;
6145 return err;