Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
45 #include "got_diff.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 static const struct got_error *
63 create_meta_file(const char *path_got, const char *name, const char *content)
64 {
65 const struct got_error *err = NULL;
66 char *path;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1)
69 return got_error_from_errno("asprintf");
71 err = got_path_create_file(path, content);
72 free(path);
73 return err;
74 }
76 static const struct got_error *
77 update_meta_file(const char *path_got, const char *name, const char *content)
78 {
79 const struct got_error *err = NULL;
80 FILE *tmpfile = NULL;
81 char *tmppath = NULL;
82 char *path = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
85 err = got_error_from_errno("asprintf");
86 path = NULL;
87 goto done;
88 }
90 err = got_opentemp_named(&tmppath, &tmpfile, path);
91 if (err)
92 goto done;
94 if (content) {
95 int len = fprintf(tmpfile, "%s\n", content);
96 if (len != strlen(content) + 1) {
97 err = got_error_from_errno2("fprintf", tmppath);
98 goto done;
99 }
102 if (rename(tmppath, path) != 0) {
103 err = got_error_from_errno3("rename", tmppath, path);
104 unlink(tmppath);
105 goto done;
108 done:
109 if (fclose(tmpfile) != 0 && err == NULL)
110 err = got_error_from_errno2("fclose", tmppath);
111 free(tmppath);
112 return err;
115 static const struct got_error *
116 read_meta_file(char **content, const char *path_got, const char *name)
118 const struct got_error *err = NULL;
119 char *path;
120 int fd = -1;
121 ssize_t n;
122 struct stat sb;
124 *content = NULL;
126 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
127 err = got_error_from_errno("asprintf");
128 path = NULL;
129 goto done;
132 fd = open(path, O_RDONLY | O_NOFOLLOW);
133 if (fd == -1) {
134 if (errno == ENOENT)
135 err = got_error_path(path, GOT_ERR_WORKTREE_META);
136 else
137 err = got_error_from_errno2("open", path);
138 goto done;
140 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
141 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
142 : got_error_from_errno2("flock", path));
143 goto done;
146 if (fstat(fd, &sb) != 0) {
147 err = got_error_from_errno2("fstat", path);
148 goto done;
150 *content = calloc(1, sb.st_size);
151 if (*content == NULL) {
152 err = got_error_from_errno("calloc");
153 goto done;
156 n = read(fd, *content, sb.st_size);
157 if (n != sb.st_size) {
158 err = (n == -1 ? got_error_from_errno2("read", path) :
159 got_error_path(path, GOT_ERR_WORKTREE_META));
160 goto done;
162 if ((*content)[sb.st_size - 1] != '\n') {
163 err = got_error_path(path, GOT_ERR_WORKTREE_META);
164 goto done;
166 (*content)[sb.st_size - 1] = '\0';
168 done:
169 if (fd != -1 && close(fd) == -1 && err == NULL)
170 err = got_error_from_errno2("close", path_got);
171 free(path);
172 if (err) {
173 free(*content);
174 *content = NULL;
176 return err;
179 static const struct got_error *
180 write_head_ref(const char *path_got, struct got_reference *head_ref)
182 const struct got_error *err = NULL;
183 char *refstr = NULL;
185 if (got_ref_is_symbolic(head_ref)) {
186 refstr = got_ref_to_str(head_ref);
187 if (refstr == NULL)
188 return got_error_from_errno("got_ref_to_str");
189 } else {
190 refstr = strdup(got_ref_get_name(head_ref));
191 if (refstr == NULL)
192 return got_error_from_errno("strdup");
194 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
195 free(refstr);
196 return err;
199 const struct got_error *
200 got_worktree_init(const char *path, struct got_reference *head_ref,
201 const char *prefix, struct got_repository *repo)
203 const struct got_error *err = NULL;
204 struct got_object_id *commit_id = NULL;
205 uuid_t uuid;
206 uint32_t uuid_status;
207 int obj_type;
208 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
216 goto done;
219 err = got_ref_resolve(&commit_id, repo, head_ref);
220 if (err)
221 return err;
222 err = got_object_get_type(&obj_type, repo, commit_id);
223 if (err)
224 return err;
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno2("mkdir", path);
236 goto done;
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno("asprintf");
242 goto done;
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno2("mkdir", path_got);
246 goto done;
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
251 if (err)
252 goto done;
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
256 if (err)
257 goto done;
259 /* Write the HEAD reference. */
260 err = write_head_ref(path_got, head_ref);
261 if (err)
262 goto done;
264 /* Record our base commit. */
265 err = got_object_id_str(&basestr, commit_id);
266 if (err)
267 goto done;
268 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
269 if (err)
270 goto done;
272 /* Store path to repository. */
273 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
274 got_repo_get_path(repo));
275 if (err)
276 goto done;
278 /* Store in-repository path prefix. */
279 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
280 absprefix ? absprefix : prefix);
281 if (err)
282 goto done;
284 /* Generate UUID. */
285 uuid_create(&uuid, &uuid_status);
286 if (uuid_status != uuid_s_ok) {
287 err = got_error_uuid(uuid_status, "uuid_create");
288 goto done;
290 uuid_to_string(&uuid, &uuidstr, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_to_string");
293 goto done;
295 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
296 if (err)
297 goto done;
299 /* Stamp work tree with format file. */
300 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
301 err = got_error_from_errno("asprintf");
302 goto done;
304 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
305 if (err)
306 goto done;
308 done:
309 free(commit_id);
310 free(path_got);
311 free(formatstr);
312 free(absprefix);
313 free(basestr);
314 free(uuidstr);
315 return err;
318 static const struct got_error *
319 open_worktree(struct got_worktree **worktree, const char *path)
321 const struct got_error *err = NULL;
322 char *path_got;
323 char *formatstr = NULL;
324 char *uuidstr = NULL;
325 char *path_lock = NULL;
326 char *base_commit_id_str = NULL;
327 int version, fd = -1;
328 const char *errstr;
329 struct got_repository *repo = NULL;
330 uint32_t uuid_status;
332 *worktree = NULL;
334 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
335 err = got_error_from_errno("asprintf");
336 path_got = NULL;
337 goto done;
340 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
341 err = got_error_from_errno("asprintf");
342 path_lock = NULL;
343 goto done;
346 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 if (fd == -1) {
348 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 : got_error_from_errno2("open", path_lock));
350 goto done;
353 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
354 if (err)
355 goto done;
357 version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 if (errstr) {
359 err = got_error_msg(GOT_ERR_WORKTREE_META,
360 "could not parse work tree format version number");
361 goto done;
363 if (version != GOT_WORKTREE_FORMAT_VERSION) {
364 err = got_error(GOT_ERR_WORKTREE_VERS);
365 goto done;
368 *worktree = calloc(1, sizeof(**worktree));
369 if (*worktree == NULL) {
370 err = got_error_from_errno("calloc");
371 goto done;
373 (*worktree)->lockfd = -1;
375 (*worktree)->root_path = strdup(path);
376 if ((*worktree)->root_path == NULL) {
377 err = got_error_from_errno("strdup");
378 goto done;
380 err = read_meta_file(&(*worktree)->repo_path, path_got,
381 GOT_WORKTREE_REPOSITORY);
382 if (err)
383 goto done;
385 err = read_meta_file(&(*worktree)->path_prefix, path_got,
386 GOT_WORKTREE_PATH_PREFIX);
387 if (err)
388 goto done;
390 err = read_meta_file(&base_commit_id_str, path_got,
391 GOT_WORKTREE_BASE_COMMIT);
392 if (err)
393 goto done;
395 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 if (err)
397 goto done;
398 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
399 if (uuid_status != uuid_s_ok) {
400 err = got_error_uuid(uuid_status, "uuid_from_string");
401 goto done;
404 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
405 if (err)
406 goto done;
408 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
409 base_commit_id_str);
410 if (err)
411 goto done;
413 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
414 GOT_WORKTREE_HEAD_REF);
415 done:
416 if (repo)
417 got_repo_close(repo);
418 free(path_got);
419 free(path_lock);
420 free(base_commit_id_str);
421 free(uuidstr);
422 free(formatstr);
423 if (err) {
424 if (fd != -1)
425 close(fd);
426 if (*worktree != NULL)
427 got_worktree_close(*worktree);
428 *worktree = NULL;
429 } else
430 (*worktree)->lockfd = fd;
432 return err;
435 const struct got_error *
436 got_worktree_open(struct got_worktree **worktree, const char *path)
438 const struct got_error *err = NULL;
440 do {
441 err = open_worktree(worktree, path);
442 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
443 return err;
444 if (*worktree)
445 return NULL;
446 path = dirname(path);
447 if (path == NULL)
448 return got_error_from_errno2("dirname", path);
449 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
451 return got_error(GOT_ERR_NOT_WORKTREE);
454 const struct got_error *
455 got_worktree_close(struct got_worktree *worktree)
457 const struct got_error *err = NULL;
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree->root_path);
467 free(worktree);
468 return err;
471 const char *
472 got_worktree_get_root_path(struct got_worktree *worktree)
474 return worktree->root_path;
477 const char *
478 got_worktree_get_repo_path(struct got_worktree *worktree)
480 return worktree->repo_path;
483 const char *
484 got_worktree_get_path_prefix(struct got_worktree *worktree)
486 return worktree->path_prefix;
489 const struct got_error *
490 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
491 const char *path_prefix)
493 char *absprefix = NULL;
495 if (!got_path_is_absolute(path_prefix)) {
496 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
497 return got_error_from_errno("asprintf");
499 *match = (strcmp(absprefix ? absprefix : path_prefix,
500 worktree->path_prefix) == 0);
501 free(absprefix);
502 return NULL;
505 const char *
506 got_worktree_get_head_ref_name(struct got_worktree *worktree)
508 return worktree->head_ref_name;
511 const struct got_error *
512 got_worktree_set_head_ref(struct got_worktree *worktree,
513 struct got_reference *head_ref)
515 const struct got_error *err = NULL;
516 char *path_got = NULL, *head_ref_name = NULL;
518 if (asprintf(&path_got, "%s/%s", worktree->root_path,
519 GOT_WORKTREE_GOT_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
521 path_got = NULL;
522 goto done;
525 head_ref_name = strdup(got_ref_get_name(head_ref));
526 if (head_ref_name == NULL) {
527 err = got_error_from_errno("strdup");
528 goto done;
531 err = write_head_ref(path_got, head_ref);
532 if (err)
533 goto done;
535 free(worktree->head_ref_name);
536 worktree->head_ref_name = head_ref_name;
537 done:
538 free(path_got);
539 if (err)
540 free(head_ref_name);
541 return err;
544 struct got_object_id *
545 got_worktree_get_base_commit_id(struct got_worktree *worktree)
547 return worktree->base_commit_id;
550 const struct got_error *
551 got_worktree_set_base_commit_id(struct got_worktree *worktree,
552 struct got_repository *repo, struct got_object_id *commit_id)
554 const struct got_error *err;
555 struct got_object *obj = NULL;
556 char *id_str = NULL;
557 char *path_got = NULL;
559 if (asprintf(&path_got, "%s/%s", worktree->root_path,
560 GOT_WORKTREE_GOT_DIR) == -1) {
561 err = got_error_from_errno("asprintf");
562 path_got = NULL;
563 goto done;
566 err = got_object_open(&obj, repo, commit_id);
567 if (err)
568 return err;
570 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
571 err = got_error(GOT_ERR_OBJ_TYPE);
572 goto done;
575 /* Record our base commit. */
576 err = got_object_id_str(&id_str, commit_id);
577 if (err)
578 goto done;
579 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
580 if (err)
581 goto done;
583 free(worktree->base_commit_id);
584 worktree->base_commit_id = got_object_id_dup(commit_id);
585 if (worktree->base_commit_id == NULL) {
586 err = got_error_from_errno("got_object_id_dup");
587 goto done;
589 done:
590 if (obj)
591 got_object_close(obj);
592 free(id_str);
593 free(path_got);
594 return err;
597 static const struct got_error *
598 lock_worktree(struct got_worktree *worktree, int operation)
600 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
601 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
602 : got_error_from_errno2("flock",
603 got_worktree_get_root_path(worktree)));
604 return NULL;
607 static const struct got_error *
608 add_dir_on_disk(struct got_worktree *worktree, const char *path)
610 const struct got_error *err = NULL;
611 char *abspath;
613 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
614 return got_error_from_errno("asprintf");
616 err = got_path_mkdir(abspath);
617 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
618 struct stat sb;
619 err = NULL;
620 if (lstat(abspath, &sb) == -1) {
621 err = got_error_from_errno2("lstat", abspath);
622 } else if (!S_ISDIR(sb.st_mode)) {
623 /* TODO directory is obstructed; do something */
624 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
627 free(abspath);
628 return err;
631 static const struct got_error *
632 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
634 const struct got_error *err = NULL;
635 uint8_t fbuf1[8192];
636 uint8_t fbuf2[8192];
637 size_t flen1 = 0, flen2 = 0;
639 *same = 1;
641 for (;;) {
642 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
643 if (flen1 == 0 && ferror(f1)) {
644 err = got_error_from_errno("fread");
645 break;
647 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
648 if (flen2 == 0 && ferror(f2)) {
649 err = got_error_from_errno("fread");
650 break;
652 if (flen1 == 0) {
653 if (flen2 != 0)
654 *same = 0;
655 break;
656 } else if (flen2 == 0) {
657 if (flen1 != 0)
658 *same = 0;
659 break;
660 } else if (flen1 == flen2) {
661 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
662 *same = 0;
663 break;
665 } else {
666 *same = 0;
667 break;
671 return err;
674 static const struct got_error *
675 check_files_equal(int *same, const char *f1_path, const char *f2_path)
677 const struct got_error *err = NULL;
678 struct stat sb;
679 size_t size1, size2;
680 FILE *f1 = NULL, *f2 = NULL;
682 *same = 1;
684 if (lstat(f1_path, &sb) != 0) {
685 err = got_error_from_errno2("lstat", f1_path);
686 goto done;
688 size1 = sb.st_size;
690 if (lstat(f2_path, &sb) != 0) {
691 err = got_error_from_errno2("lstat", f2_path);
692 goto done;
694 size2 = sb.st_size;
696 if (size1 != size2) {
697 *same = 0;
698 return NULL;
701 f1 = fopen(f1_path, "r");
702 if (f1 == NULL)
703 return got_error_from_errno2("open", f1_path);
705 f2 = fopen(f2_path, "r");
706 if (f2 == NULL) {
707 err = got_error_from_errno2("open", f2_path);
708 goto done;
711 err = check_file_contents_equal(same, f1, f2);
712 done:
713 if (f1 && fclose(f1) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
715 if (f2 && fclose(f2) != 0 && err == NULL)
716 err = got_error_from_errno("fclose");
718 return err;
721 /*
722 * Perform a 3-way merge where blob_orig acts as the common ancestor,
723 * the file at deriv_path acts as the first derived version, and the
724 * file on disk acts as the second derived version.
725 */
726 static const struct got_error *
727 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
728 struct got_blob_object *blob_orig, const char *ondisk_path,
729 const char *path, uint16_t st_mode, const char *deriv_path,
730 const char *label_deriv, struct got_repository *repo,
731 got_worktree_checkout_cb progress_cb, void *progress_arg)
733 const struct got_error *err = NULL;
734 int merged_fd = -1;
735 FILE *f_orig = NULL;
736 char *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
738 int overlapcnt = 0;
739 char *parent;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
744 if (parent == NULL)
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
751 if (err)
752 goto done;
754 free(base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
756 err = got_error_from_errno("asprintf");
757 base_path = NULL;
758 goto done;
761 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
762 if (err)
763 goto done;
764 if (blob_orig) {
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
766 blob_orig);
767 if (err)
768 goto done;
769 } else {
770 /*
771 * If the file has no blob, this is an "add vs add" conflict,
772 * and we simply use an empty ancestor file to make both files
773 * appear in the merged result in their entirety.
774 */
777 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
778 blob_orig_path, ondisk_path, label_deriv, path);
779 if (err)
780 goto done;
782 err = (*progress_cb)(progress_arg,
783 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (err)
785 goto done;
787 if (fsync(merged_fd) != 0) {
788 err = got_error_from_errno("fsync");
789 goto done;
792 /* Check if a clean merge has subsumed all local changes. */
793 if (overlapcnt == 0) {
794 err = check_files_equal(local_changes_subsumed, deriv_path,
795 merged_path);
796 if (err)
797 goto done;
800 if (chmod(merged_path, st_mode) != 0) {
801 err = got_error_from_errno2("chmod", merged_path);
802 goto done;
805 if (rename(merged_path, ondisk_path) != 0) {
806 err = got_error_from_errno3("rename", merged_path,
807 ondisk_path);
808 unlink(merged_path);
809 goto done;
812 done:
813 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
814 err = got_error_from_errno("close");
815 if (f_orig && fclose(f_orig) != 0 && err == NULL)
816 err = got_error_from_errno("fclose");
817 free(merged_path);
818 free(base_path);
819 if (blob_orig_path) {
820 unlink(blob_orig_path);
821 free(blob_orig_path);
823 return err;
826 /*
827 * Perform a 3-way merge where blob_orig acts as the common ancestor,
828 * blob_deriv acts as the first derived version, and the file on disk
829 * acts as the second derived version.
830 */
831 static const struct got_error *
832 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
833 struct got_blob_object *blob_orig, const char *ondisk_path,
834 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
835 struct got_object_id *deriv_base_commit_id,
836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
837 void *progress_arg)
839 const struct got_error *err = NULL;
840 FILE *f_deriv = NULL;
841 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
842 char *label_deriv = NULL, *parent;
844 *local_changes_subsumed = 0;
846 parent = dirname(ondisk_path);
847 if (parent == NULL)
848 return got_error_from_errno2("dirname", ondisk_path);
850 free(base_path);
851 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
852 err = got_error_from_errno("asprintf");
853 base_path = NULL;
854 goto done;
857 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
858 if (err)
859 goto done;
860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
861 blob_deriv);
862 if (err)
863 goto done;
865 err = got_object_id_str(&id_str, deriv_base_commit_id);
866 if (err)
867 goto done;
868 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
869 err = got_error_from_errno("asprintf");
870 goto done;
873 err = merge_file(local_changes_subsumed, worktree, blob_orig,
874 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
875 repo, progress_cb, progress_arg);
876 done:
877 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
878 err = got_error_from_errno("fclose");
879 free(base_path);
880 if (blob_deriv_path) {
881 unlink(blob_deriv_path);
882 free(blob_deriv_path);
884 free(id_str);
885 free(label_deriv);
886 return err;
889 static const struct got_error *
890 update_blob_fileindex_entry(struct got_worktree *worktree,
891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
892 const char *ondisk_path, const char *path, struct got_blob_object *blob,
893 int update_timestamps)
895 const struct got_error *err = NULL;
897 if (ie == NULL)
898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
899 if (ie)
900 err = got_fileindex_entry_update(ie, ondisk_path,
901 blob->id.sha1, worktree->base_commit_id->sha1,
902 update_timestamps);
903 else {
904 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
906 path, blob->id.sha1, worktree->base_commit_id->sha1);
907 if (!err)
908 err = got_fileindex_entry_add(fileindex, new_ie);
910 return err;
913 static const struct got_error *
914 install_blob(struct got_worktree *worktree, const char *ondisk_path,
915 const char *path, uint16_t te_mode, uint16_t st_mode,
916 struct got_blob_object *blob, int restoring_missing_file,
917 int reverting_versioned_file, struct got_repository *repo,
918 got_worktree_checkout_cb progress_cb, void *progress_arg)
920 const struct got_error *err = NULL;
921 int fd = -1;
922 size_t len, hdrlen;
923 int update = 0;
924 char *tmppath = NULL;
926 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
927 GOT_DEFAULT_FILE_MODE);
928 if (fd == -1) {
929 if (errno == ENOENT) {
930 char *parent = dirname(path);
931 if (parent == NULL)
932 return got_error_from_errno2("dirname", path);
933 err = add_dir_on_disk(worktree, parent);
934 if (err)
935 return err;
936 fd = open(ondisk_path,
937 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
938 GOT_DEFAULT_FILE_MODE);
939 if (fd == -1)
940 return got_error_from_errno2("open",
941 ondisk_path);
942 } else if (errno == EEXIST) {
943 if (!S_ISREG(st_mode)) {
944 /* TODO file is obstructed; do something */
945 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
946 goto done;
947 } else {
948 err = got_opentemp_named_fd(&tmppath, &fd,
949 ondisk_path);
950 if (err)
951 goto done;
952 update = 1;
954 } else
955 return got_error_from_errno2("open", ondisk_path);
958 if (restoring_missing_file)
959 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
960 else if (reverting_versioned_file)
961 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
962 else
963 err = (*progress_cb)(progress_arg,
964 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
965 if (err)
966 goto done;
968 hdrlen = got_object_blob_get_hdrlen(blob);
969 do {
970 const uint8_t *buf = got_object_blob_get_read_buf(blob);
971 err = got_object_blob_read_block(&len, blob);
972 if (err)
973 break;
974 if (len > 0) {
975 /* Skip blob object header first time around. */
976 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
977 if (outlen == -1) {
978 err = got_error_from_errno("write");
979 goto done;
980 } else if (outlen != len - hdrlen) {
981 err = got_error(GOT_ERR_IO);
982 goto done;
984 hdrlen = 0;
986 } while (len != 0);
988 if (fsync(fd) != 0) {
989 err = got_error_from_errno("fsync");
990 goto done;
993 if (update) {
994 if (rename(tmppath, ondisk_path) != 0) {
995 err = got_error_from_errno3("rename", tmppath,
996 ondisk_path);
997 unlink(tmppath);
998 goto done;
1002 if (te_mode & S_IXUSR) {
1003 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1004 err = got_error_from_errno2("chmod", ondisk_path);
1005 goto done;
1007 } else {
1008 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1009 err = got_error_from_errno2("chmod", ondisk_path);
1010 goto done;
1014 done:
1015 if (fd != -1 && close(fd) != 0 && err == NULL)
1016 err = got_error_from_errno("close");
1017 free(tmppath);
1018 return err;
1021 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1022 static const struct got_error *
1023 get_modified_file_content_status(unsigned char *status, FILE *f)
1025 const struct got_error *err = NULL;
1026 const char *markers[3] = {
1027 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1028 GOT_DIFF_CONFLICT_MARKER_SEP,
1029 GOT_DIFF_CONFLICT_MARKER_END
1031 int i = 0;
1032 char *line;
1033 size_t len;
1034 const char delim[3] = {'\0', '\0', '\0'};
1036 while (*status == GOT_STATUS_MODIFY) {
1037 line = fparseln(f, &len, NULL, delim, 0);
1038 if (line == NULL) {
1039 if (feof(f))
1040 break;
1041 err = got_ferror(f, GOT_ERR_IO);
1042 break;
1045 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1046 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1047 == 0)
1048 *status = GOT_STATUS_CONFLICT;
1049 else
1050 i++;
1054 return err;
1057 static int
1058 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1060 return !(ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_nsec == sb->st_mtimensec &&
1064 ie->size == (sb->st_size & 0xffffffff));
1067 static unsigned char
1068 get_staged_status(struct got_fileindex_entry *ie)
1070 switch (got_fileindex_entry_stage_get(ie)) {
1071 case GOT_FILEIDX_STAGE_ADD:
1072 return GOT_STATUS_ADD;
1073 case GOT_FILEIDX_STAGE_DELETE:
1074 return GOT_STATUS_DELETE;
1075 case GOT_FILEIDX_STAGE_MODIFY:
1076 return GOT_STATUS_MODIFY;
1077 default:
1078 return GOT_STATUS_NO_CHANGE;
1082 static const struct got_error *
1083 get_file_status(unsigned char *status, struct stat *sb,
1084 struct got_fileindex_entry *ie, const char *abspath,
1085 struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 struct got_object_id id;
1089 size_t hdrlen;
1090 FILE *f = NULL;
1091 uint8_t fbuf[8192];
1092 struct got_blob_object *blob = NULL;
1093 size_t flen, blen;
1094 unsigned char staged_status = get_staged_status(ie);
1096 *status = GOT_STATUS_NO_CHANGE;
1098 if (lstat(abspath, sb) == -1) {
1099 if (errno == ENOENT) {
1100 if (got_fileindex_entry_has_file_on_disk(ie))
1101 *status = GOT_STATUS_MISSING;
1102 else
1103 *status = GOT_STATUS_DELETE;
1104 return NULL;
1106 return got_error_from_errno2("lstat", abspath);
1109 if (!S_ISREG(sb->st_mode)) {
1110 *status = GOT_STATUS_OBSTRUCTED;
1111 return NULL;
1114 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1115 *status = GOT_STATUS_DELETE;
1116 return NULL;
1117 } else if (!got_fileindex_entry_has_blob(ie) &&
1118 staged_status != GOT_STATUS_ADD) {
1119 *status = GOT_STATUS_ADD;
1120 return NULL;
1123 if (!stat_info_differs(ie, sb))
1124 return NULL;
1126 if (staged_status == GOT_STATUS_MODIFY ||
1127 staged_status == GOT_STATUS_ADD)
1128 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1129 else
1130 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1132 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1133 if (err)
1134 return err;
1136 f = fopen(abspath, "r");
1137 if (f == NULL) {
1138 err = got_error_from_errno2("fopen", abspath);
1139 goto done;
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1142 for (;;) {
1143 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1144 err = got_object_blob_read_block(&blen, blob);
1145 if (err)
1146 goto done;
1147 /* Skip length of blob object header first time around. */
1148 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1149 if (flen == 0 && ferror(f)) {
1150 err = got_error_from_errno("fread");
1151 goto done;
1153 if (blen == 0) {
1154 if (flen != 0)
1155 *status = GOT_STATUS_MODIFY;
1156 break;
1157 } else if (flen == 0) {
1158 if (blen != 0)
1159 *status = GOT_STATUS_MODIFY;
1160 break;
1161 } else if (blen - hdrlen == flen) {
1162 /* Skip blob object header first time around. */
1163 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1164 *status = GOT_STATUS_MODIFY;
1165 break;
1167 } else {
1168 *status = GOT_STATUS_MODIFY;
1169 break;
1171 hdrlen = 0;
1174 if (*status == GOT_STATUS_MODIFY) {
1175 rewind(f);
1176 err = get_modified_file_content_status(status, f);
1178 done:
1179 if (blob)
1180 got_object_blob_close(blob);
1181 if (f)
1182 fclose(f);
1183 return err;
1187 * Update timestamps in the file index if a file is unmodified and
1188 * we had to run a full content comparison to find out.
1190 static const struct got_error *
1191 sync_timestamps(char *ondisk_path, unsigned char status,
1192 struct got_fileindex_entry *ie, struct stat *sb)
1194 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1195 return got_fileindex_entry_update(ie, ondisk_path,
1196 ie->blob_sha1, ie->commit_sha1, 1);
1198 return NULL;
1201 static const struct got_error *
1202 update_blob(struct got_worktree *worktree,
1203 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1204 struct got_tree_entry *te, const char *path,
1205 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1206 void *progress_arg)
1208 const struct got_error *err = NULL;
1209 struct got_blob_object *blob = NULL;
1210 char *ondisk_path;
1211 unsigned char status = GOT_STATUS_NO_CHANGE;
1212 struct stat sb;
1214 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1215 return got_error_from_errno("asprintf");
1217 if (ie) {
1218 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1219 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1220 goto done;
1222 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1223 if (err)
1224 goto done;
1225 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1226 sb.st_mode = got_fileindex_perms_to_st(ie);
1227 } else
1228 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 if (status == GOT_STATUS_OBSTRUCTED) {
1231 err = (*progress_cb)(progress_arg, status, path);
1232 goto done;
1235 if (ie && status != GOT_STATUS_MISSING) {
1236 if (got_fileindex_entry_has_commit(ie) &&
1237 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1238 SHA1_DIGEST_LENGTH) == 0) {
1239 err = sync_timestamps(ondisk_path, status, ie, &sb);
1240 if (err)
1241 goto done;
1242 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1243 path);
1244 goto done;
1246 if (got_fileindex_entry_has_blob(ie) &&
1247 memcmp(ie->blob_sha1, te->id->sha1,
1248 SHA1_DIGEST_LENGTH) == 0) {
1249 err = sync_timestamps(ondisk_path, status, ie, &sb);
1250 goto done;
1254 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1255 if (err)
1256 goto done;
1258 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1259 int update_timestamps;
1260 struct got_blob_object *blob2 = NULL;
1261 if (got_fileindex_entry_has_blob(ie)) {
1262 struct got_object_id id2;
1263 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1264 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1265 if (err)
1266 goto done;
1268 err = merge_blob(&update_timestamps, worktree, blob2,
1269 ondisk_path, path, sb.st_mode, blob,
1270 worktree->base_commit_id, repo,
1271 progress_cb, progress_arg);
1272 if (blob2)
1273 got_object_blob_close(blob2);
1274 if (err)
1275 goto done;
1277 * Do not update timestamps of files with local changes.
1278 * Otherwise, a future status walk would treat them as
1279 * unmodified files again.
1281 err = got_fileindex_entry_update(ie, ondisk_path,
1282 blob->id.sha1, worktree->base_commit_id->sha1,
1283 update_timestamps);
1284 } else if (status == GOT_STATUS_DELETE) {
1285 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1286 if (err)
1287 goto done;
1288 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1289 ondisk_path, path, blob, 0);
1290 if (err)
1291 goto done;
1292 } else {
1293 err = install_blob(worktree, ondisk_path, path, te->mode,
1294 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1295 repo, progress_cb, progress_arg);
1296 if (err)
1297 goto done;
1298 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1299 ondisk_path, path, blob, 1);
1300 if (err)
1301 goto done;
1303 got_object_blob_close(blob);
1304 done:
1305 free(ondisk_path);
1306 return err;
1309 static const struct got_error *
1310 remove_ondisk_file(const char *root_path, const char *path)
1312 const struct got_error *err = NULL;
1313 char *ondisk_path = NULL;
1315 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1316 return got_error_from_errno("asprintf");
1318 if (unlink(ondisk_path) == -1) {
1319 if (errno != ENOENT)
1320 err = got_error_from_errno2("unlink", ondisk_path);
1321 } else {
1322 char *parent = dirname(ondisk_path);
1323 while (parent && strcmp(parent, root_path) != 0) {
1324 if (rmdir(parent) == -1) {
1325 if (errno != ENOTEMPTY)
1326 err = got_error_from_errno2("rmdir",
1327 parent);
1328 break;
1330 parent = dirname(parent);
1333 free(ondisk_path);
1334 return err;
1337 static const struct got_error *
1338 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1339 struct got_fileindex_entry *ie, struct got_repository *repo,
1340 got_worktree_checkout_cb progress_cb, void *progress_arg)
1342 const struct got_error *err = NULL;
1343 unsigned char status;
1344 struct stat sb;
1345 char *ondisk_path;
1347 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1348 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1350 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1351 == -1)
1352 return got_error_from_errno("asprintf");
1354 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1355 if (err)
1356 return err;
1358 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1359 status == GOT_STATUS_ADD) {
1360 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1361 if (err)
1362 return err;
1364 * Preserve the working file and change the deleted blob's
1365 * entry into a schedule-add entry.
1367 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1368 0);
1369 if (err)
1370 return err;
1371 } else {
1372 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1373 if (err)
1374 return err;
1375 if (status == GOT_STATUS_NO_CHANGE) {
1376 err = remove_ondisk_file(worktree->root_path, ie->path);
1377 if (err)
1378 return err;
1380 got_fileindex_entry_remove(fileindex, ie);
1383 return err;
1386 struct diff_cb_arg {
1387 struct got_fileindex *fileindex;
1388 struct got_worktree *worktree;
1389 struct got_repository *repo;
1390 got_worktree_checkout_cb progress_cb;
1391 void *progress_arg;
1392 got_cancel_cb cancel_cb;
1393 void *cancel_arg;
1396 static const struct got_error *
1397 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1398 struct got_tree_entry *te, const char *parent_path)
1400 struct diff_cb_arg *a = arg;
1402 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1403 return got_error(GOT_ERR_CANCELLED);
1405 return update_blob(a->worktree, a->fileindex, ie, te,
1406 ie->path, a->repo, a->progress_cb, a->progress_arg);
1409 static const struct got_error *
1410 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1412 struct diff_cb_arg *a = arg;
1414 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1415 return got_error(GOT_ERR_CANCELLED);
1417 return delete_blob(a->worktree, a->fileindex, ie,
1418 a->repo, a->progress_cb, a->progress_arg);
1421 static const struct got_error *
1422 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1424 struct diff_cb_arg *a = arg;
1425 const struct got_error *err;
1426 char *path;
1428 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1429 return got_error(GOT_ERR_CANCELLED);
1431 if (got_object_tree_entry_is_submodule(te))
1432 return NULL;
1434 if (asprintf(&path, "%s%s%s", parent_path,
1435 parent_path[0] ? "/" : "", te->name)
1436 == -1)
1437 return got_error_from_errno("asprintf");
1439 if (S_ISDIR(te->mode))
1440 err = add_dir_on_disk(a->worktree, path);
1441 else
1442 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1443 a->repo, a->progress_cb, a->progress_arg);
1445 free(path);
1446 return err;
1449 static const struct got_error *
1450 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1452 const struct got_error *err = NULL;
1453 char *uuidstr = NULL;
1454 uint32_t uuid_status;
1456 *refname = NULL;
1458 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1459 if (uuid_status != uuid_s_ok)
1460 return got_error_uuid(uuid_status, "uuid_to_string");
1462 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1463 == -1) {
1464 err = got_error_from_errno("asprintf");
1465 *refname = NULL;
1467 free(uuidstr);
1468 return err;
1471 const struct got_error *
1472 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1474 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1477 static const struct got_error *
1478 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1480 return get_ref_name(refname, worktree,
1481 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1484 static const struct got_error *
1485 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1487 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1490 static const struct got_error *
1491 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1493 return get_ref_name(refname, worktree,
1494 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1497 static const struct got_error *
1498 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1500 return get_ref_name(refname, worktree,
1501 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1504 static const struct got_error *
1505 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1507 return get_ref_name(refname, worktree,
1508 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1511 static const struct got_error *
1512 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1514 return get_ref_name(refname, worktree,
1515 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1518 static const struct got_error *
1519 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1521 return get_ref_name(refname, worktree,
1522 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1525 static const struct got_error *
1526 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1528 return get_ref_name(refname, worktree,
1529 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1532 const struct got_error *
1533 got_worktree_get_histedit_script_path(char **path,
1534 struct got_worktree *worktree)
1536 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1537 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1538 *path = NULL;
1539 return got_error_from_errno("asprintf");
1541 return NULL;
1545 * Prevent Git's garbage collector from deleting our base commit by
1546 * setting a reference to our base commit's ID.
1548 static const struct got_error *
1549 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1551 const struct got_error *err = NULL;
1552 struct got_reference *ref = NULL;
1553 char *refname;
1555 err = got_worktree_get_base_ref_name(&refname, worktree);
1556 if (err)
1557 return err;
1559 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1560 if (err)
1561 goto done;
1563 err = got_ref_write(ref, repo);
1564 done:
1565 free(refname);
1566 if (ref)
1567 got_ref_close(ref);
1568 return err;
1571 static const struct got_error *
1572 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1574 const struct got_error *err = NULL;
1576 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1577 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1578 err = got_error_from_errno("asprintf");
1579 *fileindex_path = NULL;
1581 return err;
1585 static const struct got_error *
1586 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1587 struct got_worktree *worktree)
1589 const struct got_error *err = NULL;
1590 FILE *index = NULL;
1592 *fileindex_path = NULL;
1593 *fileindex = got_fileindex_alloc();
1594 if (*fileindex == NULL)
1595 return got_error_from_errno("got_fileindex_alloc");
1597 err = get_fileindex_path(fileindex_path, worktree);
1598 if (err)
1599 goto done;
1601 index = fopen(*fileindex_path, "rb");
1602 if (index == NULL) {
1603 if (errno != ENOENT)
1604 err = got_error_from_errno2("fopen", *fileindex_path);
1605 } else {
1606 err = got_fileindex_read(*fileindex, index);
1607 if (fclose(index) != 0 && err == NULL)
1608 err = got_error_from_errno("fclose");
1610 done:
1611 if (err) {
1612 free(*fileindex_path);
1613 *fileindex_path = NULL;
1614 got_fileindex_free(*fileindex);
1615 *fileindex = NULL;
1617 return err;
1620 struct bump_base_commit_id_arg {
1621 struct got_object_id *base_commit_id;
1622 const char *path;
1623 size_t path_len;
1624 const char *entry_name;
1625 got_worktree_checkout_cb progress_cb;
1626 void *progress_arg;
1629 /* Bump base commit ID of all files within an updated part of the work tree. */
1630 static const struct got_error *
1631 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1633 const struct got_error *err;
1634 struct bump_base_commit_id_arg *a = arg;
1636 if (a->entry_name) {
1637 if (strcmp(ie->path, a->path) != 0)
1638 return NULL;
1639 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1640 return NULL;
1642 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1643 SHA1_DIGEST_LENGTH) == 0)
1644 return NULL;
1646 if (a->progress_cb) {
1647 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1648 ie->path);
1649 if (err)
1650 return err;
1652 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1653 return NULL;
1656 static const struct got_error *
1657 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1659 const struct got_error *err = NULL;
1660 char *new_fileindex_path = NULL;
1661 FILE *new_index = NULL;
1663 err = got_opentemp_named(&new_fileindex_path, &new_index,
1664 fileindex_path);
1665 if (err)
1666 goto done;
1668 err = got_fileindex_write(fileindex, new_index);
1669 if (err)
1670 goto done;
1672 if (rename(new_fileindex_path, fileindex_path) != 0) {
1673 err = got_error_from_errno3("rename", new_fileindex_path,
1674 fileindex_path);
1675 unlink(new_fileindex_path);
1677 done:
1678 if (new_index)
1679 fclose(new_index);
1680 free(new_fileindex_path);
1681 return err;
1684 static const struct got_error *
1685 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1686 struct got_object_id **tree_id, const char *wt_relpath,
1687 struct got_worktree *worktree, struct got_repository *repo)
1689 const struct got_error *err = NULL;
1690 struct got_object_id *id = NULL;
1691 char *in_repo_path = NULL;
1692 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1694 *entry_type = GOT_OBJ_TYPE_ANY;
1695 *tree_relpath = NULL;
1696 *tree_id = NULL;
1698 if (wt_relpath[0] == '\0') {
1699 /* Check out all files within the work tree. */
1700 *entry_type = GOT_OBJ_TYPE_TREE;
1701 *tree_relpath = strdup("");
1702 if (*tree_relpath == NULL) {
1703 err = got_error_from_errno("strdup");
1704 goto done;
1706 err = got_object_id_by_path(tree_id, repo,
1707 worktree->base_commit_id, worktree->path_prefix);
1708 if (err)
1709 goto done;
1710 return NULL;
1713 /* Check out a subset of files in the work tree. */
1715 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1716 is_root_wt ? "" : "/", wt_relpath) == -1) {
1717 err = got_error_from_errno("asprintf");
1718 goto done;
1721 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1722 in_repo_path);
1723 if (err)
1724 goto done;
1726 free(in_repo_path);
1727 in_repo_path = NULL;
1729 err = got_object_get_type(entry_type, repo, id);
1730 if (err)
1731 goto done;
1733 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1734 /* Check out a single file. */
1735 if (strchr(wt_relpath, '/') == NULL) {
1736 /* Check out a single file in work tree's root dir. */
1737 in_repo_path = strdup(worktree->path_prefix);
1738 if (in_repo_path == NULL) {
1739 err = got_error_from_errno("strdup");
1740 goto done;
1742 *tree_relpath = strdup("");
1743 if (*tree_relpath == NULL) {
1744 err = got_error_from_errno("strdup");
1745 goto done;
1747 } else {
1748 /* Check out a single file in a subdirectory. */
1749 err = got_path_dirname(tree_relpath, wt_relpath);
1750 if (err)
1751 return err;
1752 if (asprintf(&in_repo_path, "%s%s%s",
1753 worktree->path_prefix, is_root_wt ? "" : "/",
1754 *tree_relpath) == -1) {
1755 err = got_error_from_errno("asprintf");
1756 goto done;
1759 err = got_object_id_by_path(tree_id, repo,
1760 worktree->base_commit_id, in_repo_path);
1761 } else {
1762 /* Check out all files within a subdirectory. */
1763 *tree_id = got_object_id_dup(id);
1764 if (*tree_id == NULL) {
1765 err = got_error_from_errno("got_object_id_dup");
1766 goto done;
1768 *tree_relpath = strdup(wt_relpath);
1769 if (*tree_relpath == NULL) {
1770 err = got_error_from_errno("strdup");
1771 goto done;
1774 done:
1775 free(id);
1776 free(in_repo_path);
1777 if (err) {
1778 *entry_type = GOT_OBJ_TYPE_ANY;
1779 free(*tree_relpath);
1780 *tree_relpath = NULL;
1781 free(*tree_id);
1782 *tree_id = NULL;
1784 return err;
1787 static const struct got_error *
1788 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1789 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1790 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1791 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1793 const struct got_error *err = NULL;
1794 struct got_commit_object *commit = NULL;
1795 struct got_tree_object *tree = NULL;
1796 struct got_fileindex_diff_tree_cb diff_cb;
1797 struct diff_cb_arg arg;
1799 err = ref_base_commit(worktree, repo);
1800 if (err)
1801 goto done;
1803 err = got_object_open_as_commit(&commit, repo,
1804 worktree->base_commit_id);
1805 if (err)
1806 goto done;
1808 err = got_object_open_as_tree(&tree, repo, tree_id);
1809 if (err)
1810 goto done;
1812 if (entry_name &&
1813 got_object_tree_find_entry(tree, entry_name) == NULL) {
1814 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1815 goto done;
1818 diff_cb.diff_old_new = diff_old_new;
1819 diff_cb.diff_old = diff_old;
1820 diff_cb.diff_new = diff_new;
1821 arg.fileindex = fileindex;
1822 arg.worktree = worktree;
1823 arg.repo = repo;
1824 arg.progress_cb = progress_cb;
1825 arg.progress_arg = progress_arg;
1826 arg.cancel_cb = cancel_cb;
1827 arg.cancel_arg = cancel_arg;
1828 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1829 entry_name, repo, &diff_cb, &arg);
1830 done:
1831 if (tree)
1832 got_object_tree_close(tree);
1833 if (commit)
1834 got_object_commit_close(commit);
1835 return err;
1838 const struct got_error *
1839 got_worktree_checkout_files(struct got_worktree *worktree,
1840 struct got_pathlist_head *paths, struct got_repository *repo,
1841 got_worktree_checkout_cb progress_cb, void *progress_arg,
1842 got_cancel_cb cancel_cb, void *cancel_arg)
1844 const struct got_error *err = NULL, *sync_err, *unlockerr;
1845 struct got_commit_object *commit = NULL;
1846 struct got_tree_object *tree = NULL;
1847 struct got_fileindex *fileindex = NULL;
1848 char *fileindex_path = NULL;
1849 struct got_pathlist_entry *pe;
1850 struct tree_path_data {
1851 SIMPLEQ_ENTRY(tree_path_data) entry;
1852 struct got_object_id *tree_id;
1853 int entry_type;
1854 char *relpath;
1855 char *entry_name;
1856 } *tpd = NULL;
1857 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1859 SIMPLEQ_INIT(&tree_paths);
1861 err = lock_worktree(worktree, LOCK_EX);
1862 if (err)
1863 return err;
1865 /* Map all specified paths to in-repository trees. */
1866 TAILQ_FOREACH(pe, paths, entry) {
1867 tpd = malloc(sizeof(*tpd));
1868 if (tpd == NULL) {
1869 err = got_error_from_errno("malloc");
1870 goto done;
1873 err = find_tree_entry_for_checkout(&tpd->entry_type,
1874 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1875 if (err) {
1876 free(tpd);
1877 goto done;
1880 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1881 err = got_path_basename(&tpd->entry_name, pe->path);
1882 if (err) {
1883 free(tpd->relpath);
1884 free(tpd->tree_id);
1885 free(tpd);
1886 goto done;
1888 } else
1889 tpd->entry_name = NULL;
1891 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1895 * Read the file index.
1896 * Checking out files is supposed to be an idempotent operation.
1897 * If the on-disk file index is incomplete we will try to complete it.
1899 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1900 if (err)
1901 goto done;
1903 tpd = SIMPLEQ_FIRST(&tree_paths);
1904 TAILQ_FOREACH(pe, paths, entry) {
1905 struct bump_base_commit_id_arg bbc_arg;
1907 err = checkout_files(worktree, fileindex, tpd->relpath,
1908 tpd->tree_id, tpd->entry_name, repo,
1909 progress_cb, progress_arg, cancel_cb, cancel_arg);
1910 if (err)
1911 break;
1913 bbc_arg.base_commit_id = worktree->base_commit_id;
1914 bbc_arg.entry_name = tpd->entry_name;
1915 bbc_arg.path = pe->path;
1916 bbc_arg.path_len = pe->path_len;
1917 bbc_arg.progress_cb = progress_cb;
1918 bbc_arg.progress_arg = progress_arg;
1919 err = got_fileindex_for_each_entry_safe(fileindex,
1920 bump_base_commit_id, &bbc_arg);
1921 if (err)
1922 break;
1924 tpd = SIMPLEQ_NEXT(tpd, entry);
1926 sync_err = sync_fileindex(fileindex, fileindex_path);
1927 if (sync_err && err == NULL)
1928 err = sync_err;
1929 done:
1930 free(fileindex_path);
1931 if (tree)
1932 got_object_tree_close(tree);
1933 if (commit)
1934 got_object_commit_close(commit);
1935 if (fileindex)
1936 got_fileindex_free(fileindex);
1937 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1938 tpd = SIMPLEQ_FIRST(&tree_paths);
1939 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1940 free(tpd->relpath);
1941 free(tpd->tree_id);
1942 free(tpd);
1944 unlockerr = lock_worktree(worktree, LOCK_SH);
1945 if (unlockerr && err == NULL)
1946 err = unlockerr;
1947 return err;
1950 struct merge_file_cb_arg {
1951 struct got_worktree *worktree;
1952 struct got_fileindex *fileindex;
1953 got_worktree_checkout_cb progress_cb;
1954 void *progress_arg;
1955 got_cancel_cb cancel_cb;
1956 void *cancel_arg;
1957 struct got_object_id *commit_id2;
1960 static const struct got_error *
1961 merge_file_cb(void *arg, struct got_blob_object *blob1,
1962 struct got_blob_object *blob2, struct got_object_id *id1,
1963 struct got_object_id *id2, const char *path1, const char *path2,
1964 struct got_repository *repo)
1966 static const struct got_error *err = NULL;
1967 struct merge_file_cb_arg *a = arg;
1968 struct got_fileindex_entry *ie;
1969 char *ondisk_path = NULL;
1970 struct stat sb;
1971 unsigned char status;
1972 int local_changes_subsumed;
1974 if (blob1 && blob2) {
1975 ie = got_fileindex_entry_get(a->fileindex, path2,
1976 strlen(path2));
1977 if (ie == NULL)
1978 return (*a->progress_cb)(a->progress_arg,
1979 GOT_STATUS_MISSING, path2);
1981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1982 path2) == -1)
1983 return got_error_from_errno("asprintf");
1985 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1986 if (err)
1987 goto done;
1989 if (status == GOT_STATUS_DELETE) {
1990 err = (*a->progress_cb)(a->progress_arg,
1991 GOT_STATUS_MERGE, path2);
1992 goto done;
1994 if (status != GOT_STATUS_NO_CHANGE &&
1995 status != GOT_STATUS_MODIFY &&
1996 status != GOT_STATUS_CONFLICT &&
1997 status != GOT_STATUS_ADD) {
1998 err = (*a->progress_cb)(a->progress_arg, status, path2);
1999 goto done;
2002 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2003 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2004 a->progress_cb, a->progress_arg);
2005 } else if (blob1) {
2006 ie = got_fileindex_entry_get(a->fileindex, path1,
2007 strlen(path1));
2008 if (ie == NULL)
2009 return (*a->progress_cb)(a->progress_arg,
2010 GOT_STATUS_MISSING, path2);
2012 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2013 path1) == -1)
2014 return got_error_from_errno("asprintf");
2016 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2017 if (err)
2018 goto done;
2020 switch (status) {
2021 case GOT_STATUS_NO_CHANGE:
2022 err = (*a->progress_cb)(a->progress_arg,
2023 GOT_STATUS_DELETE, path1);
2024 if (err)
2025 goto done;
2026 err = remove_ondisk_file(a->worktree->root_path, path1);
2027 if (err)
2028 goto done;
2029 if (ie)
2030 got_fileindex_entry_mark_deleted_from_disk(ie);
2031 break;
2032 case GOT_STATUS_DELETE:
2033 case GOT_STATUS_MISSING:
2034 err = (*a->progress_cb)(a->progress_arg,
2035 GOT_STATUS_DELETE, path1);
2036 if (err)
2037 goto done;
2038 if (ie)
2039 got_fileindex_entry_mark_deleted_from_disk(ie);
2040 break;
2041 case GOT_STATUS_ADD:
2042 case GOT_STATUS_MODIFY:
2043 case GOT_STATUS_CONFLICT:
2044 err = (*a->progress_cb)(a->progress_arg,
2045 GOT_STATUS_CANNOT_DELETE, path1);
2046 if (err)
2047 goto done;
2048 break;
2049 case GOT_STATUS_OBSTRUCTED:
2050 err = (*a->progress_cb)(a->progress_arg, status, path1);
2051 if (err)
2052 goto done;
2053 break;
2054 default:
2055 break;
2057 } else if (blob2) {
2058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2059 path2) == -1)
2060 return got_error_from_errno("asprintf");
2061 ie = got_fileindex_entry_get(a->fileindex, path2,
2062 strlen(path2));
2063 if (ie) {
2064 err = get_file_status(&status, &sb, ie, ondisk_path,
2065 repo);
2066 if (err)
2067 goto done;
2068 if (status != GOT_STATUS_NO_CHANGE &&
2069 status != GOT_STATUS_MODIFY &&
2070 status != GOT_STATUS_CONFLICT &&
2071 status != GOT_STATUS_ADD) {
2072 err = (*a->progress_cb)(a->progress_arg,
2073 status, path2);
2074 goto done;
2076 err = merge_blob(&local_changes_subsumed, a->worktree,
2077 NULL, ondisk_path, path2, sb.st_mode, blob2,
2078 a->commit_id2, repo,
2079 a->progress_cb, a->progress_arg);
2080 if (status == GOT_STATUS_DELETE) {
2081 err = update_blob_fileindex_entry(a->worktree,
2082 a->fileindex, ie, ondisk_path, ie->path,
2083 blob2, 0);
2084 if (err)
2085 goto done;
2087 } else {
2088 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2089 err = install_blob(a->worktree, ondisk_path, path2,
2090 /* XXX get this from parent tree! */
2091 GOT_DEFAULT_FILE_MODE,
2092 sb.st_mode, blob2, 0, 0, repo,
2093 a->progress_cb, a->progress_arg);
2094 if (err)
2095 goto done;
2096 err = got_fileindex_entry_alloc(&ie,
2097 ondisk_path, path2, NULL, NULL);
2098 if (err)
2099 goto done;
2100 err = got_fileindex_entry_add(a->fileindex, ie);
2101 if (err) {
2102 got_fileindex_entry_free(ie);
2103 goto done;
2107 done:
2108 free(ondisk_path);
2109 return err;
2112 struct check_merge_ok_arg {
2113 struct got_worktree *worktree;
2114 struct got_repository *repo;
2117 static const struct got_error *
2118 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2120 const struct got_error *err = NULL;
2121 struct check_merge_ok_arg *a = arg;
2122 unsigned char status;
2123 struct stat sb;
2124 char *ondisk_path;
2126 /* Reject merges into a work tree with mixed base commits. */
2127 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2128 SHA1_DIGEST_LENGTH))
2129 return got_error(GOT_ERR_MIXED_COMMITS);
2131 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2132 == -1)
2133 return got_error_from_errno("asprintf");
2135 /* Reject merges into a work tree with conflicted files. */
2136 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2137 if (err)
2138 return err;
2139 if (status == GOT_STATUS_CONFLICT)
2140 return got_error(GOT_ERR_CONFLICTS);
2142 return NULL;
2145 static const struct got_error *
2146 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 const char *fileindex_path, struct got_object_id *commit_id1,
2148 struct got_object_id *commit_id2, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg,
2150 got_cancel_cb cancel_cb, void *cancel_arg)
2152 const struct got_error *err = NULL, *sync_err;
2153 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2154 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2155 struct merge_file_cb_arg arg;
2157 if (commit_id1) {
2158 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2159 worktree->path_prefix);
2160 if (err)
2161 goto done;
2163 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2164 if (err)
2165 goto done;
2168 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2169 worktree->path_prefix);
2170 if (err)
2171 goto done;
2173 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2174 if (err)
2175 goto done;
2177 arg.worktree = worktree;
2178 arg.fileindex = fileindex;
2179 arg.progress_cb = progress_cb;
2180 arg.progress_arg = progress_arg;
2181 arg.cancel_cb = cancel_cb;
2182 arg.cancel_arg = cancel_arg;
2183 arg.commit_id2 = commit_id2;
2184 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2185 sync_err = sync_fileindex(fileindex, fileindex_path);
2186 if (sync_err && err == NULL)
2187 err = sync_err;
2188 done:
2189 if (tree1)
2190 got_object_tree_close(tree1);
2191 if (tree2)
2192 got_object_tree_close(tree2);
2193 return err;
2196 const struct got_error *
2197 got_worktree_merge_files(struct got_worktree *worktree,
2198 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2199 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2200 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2202 const struct got_error *err, *unlockerr;
2203 char *fileindex_path = NULL;
2204 struct got_fileindex *fileindex = NULL;
2205 struct check_merge_ok_arg mok_arg;
2207 err = lock_worktree(worktree, LOCK_EX);
2208 if (err)
2209 return err;
2211 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2212 if (err)
2213 goto done;
2215 mok_arg.worktree = worktree;
2216 mok_arg.repo = repo;
2217 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2218 &mok_arg);
2219 if (err)
2220 goto done;
2222 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2223 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2224 done:
2225 if (fileindex)
2226 got_fileindex_free(fileindex);
2227 free(fileindex_path);
2228 unlockerr = lock_worktree(worktree, LOCK_SH);
2229 if (unlockerr && err == NULL)
2230 err = unlockerr;
2231 return err;
2234 struct diff_dir_cb_arg {
2235 struct got_fileindex *fileindex;
2236 struct got_worktree *worktree;
2237 const char *status_path;
2238 size_t status_path_len;
2239 struct got_repository *repo;
2240 got_worktree_status_cb status_cb;
2241 void *status_arg;
2242 got_cancel_cb cancel_cb;
2243 void *cancel_arg;
2244 /* A pathlist containing per-directory pathlists of ignore patterns. */
2245 struct got_pathlist_head ignores;
2248 static const struct got_error *
2249 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2250 got_worktree_status_cb status_cb, void *status_arg,
2251 struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 unsigned char status = GOT_STATUS_NO_CHANGE;
2255 unsigned char staged_status = get_staged_status(ie);
2256 struct stat sb;
2257 struct got_object_id blob_id, commit_id, staged_blob_id;
2258 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2259 struct got_object_id *staged_blob_idp = NULL;
2261 err = get_file_status(&status, &sb, ie, abspath, repo);
2262 if (err)
2263 return err;
2265 if (status == GOT_STATUS_NO_CHANGE &&
2266 staged_status == GOT_STATUS_NO_CHANGE)
2267 return NULL;
2269 if (got_fileindex_entry_has_blob(ie)) {
2270 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2271 blob_idp = &blob_id;
2273 if (got_fileindex_entry_has_commit(ie)) {
2274 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2275 commit_idp = &commit_id;
2277 if (staged_status == GOT_STATUS_ADD ||
2278 staged_status == GOT_STATUS_MODIFY) {
2279 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2280 SHA1_DIGEST_LENGTH);
2281 staged_blob_idp = &staged_blob_id;
2284 return (*status_cb)(status_arg, status, staged_status,
2285 ie->path, blob_idp, staged_blob_idp, commit_idp);
2288 static const struct got_error *
2289 status_old_new(void *arg, struct got_fileindex_entry *ie,
2290 struct dirent *de, const char *parent_path)
2292 const struct got_error *err = NULL;
2293 struct diff_dir_cb_arg *a = arg;
2294 char *abspath;
2296 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2297 return got_error(GOT_ERR_CANCELLED);
2299 if (got_path_cmp(parent_path, a->status_path,
2300 strlen(parent_path), a->status_path_len) != 0 &&
2301 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2302 return NULL;
2304 if (parent_path[0]) {
2305 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2306 parent_path, de->d_name) == -1)
2307 return got_error_from_errno("asprintf");
2308 } else {
2309 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2310 de->d_name) == -1)
2311 return got_error_from_errno("asprintf");
2314 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2315 a->repo);
2316 free(abspath);
2317 return err;
2320 static const struct got_error *
2321 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2323 struct diff_dir_cb_arg *a = arg;
2324 struct got_object_id blob_id, commit_id;
2325 unsigned char status;
2327 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2328 return got_error(GOT_ERR_CANCELLED);
2330 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2331 return NULL;
2333 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2334 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2335 if (got_fileindex_entry_has_file_on_disk(ie))
2336 status = GOT_STATUS_MISSING;
2337 else
2338 status = GOT_STATUS_DELETE;
2339 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2340 ie->path, &blob_id, NULL, &commit_id);
2343 void
2344 free_ignorelist(struct got_pathlist_head *ignorelist)
2346 struct got_pathlist_entry *pe;
2348 TAILQ_FOREACH(pe, ignorelist, entry)
2349 free((char *)pe->path);
2350 got_pathlist_free(ignorelist);
2353 void
2354 free_ignores(struct got_pathlist_head *ignores)
2356 struct got_pathlist_entry *pe;
2358 TAILQ_FOREACH(pe, ignores, entry) {
2359 struct got_pathlist_head *ignorelist = pe->data;
2360 free_ignorelist(ignorelist);
2361 free((char *)pe->path);
2363 got_pathlist_free(ignores);
2366 static const struct got_error *
2367 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2369 const struct got_error *err = NULL;
2370 struct got_pathlist_entry *pe = NULL;
2371 struct got_pathlist_head *ignorelist;
2372 char *line = NULL, *pattern, *dirpath = NULL;
2373 size_t linesize = 0;
2374 ssize_t linelen;
2376 ignorelist = calloc(1, sizeof(*ignorelist));
2377 if (ignorelist == NULL)
2378 return got_error_from_errno("calloc");
2379 TAILQ_INIT(ignorelist);
2381 while ((linelen = getline(&line, &linesize, f)) != -1) {
2382 if (linelen > 0 && line[linelen - 1] == '\n')
2383 line[linelen - 1] = '\0';
2384 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2385 line) == -1) {
2386 err = got_error_from_errno("asprintf");
2387 goto done;
2389 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2390 if (err)
2391 goto done;
2393 if (ferror(f)) {
2394 err = got_error_from_errno("getline");
2395 goto done;
2398 dirpath = strdup(path);
2399 if (dirpath == NULL) {
2400 err = got_error_from_errno("strdup");
2401 goto done;
2403 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2404 done:
2405 free(line);
2406 if (err || pe == NULL) {
2407 free(dirpath);
2408 free_ignorelist(ignorelist);
2410 return err;
2413 int
2414 match_ignores(struct got_pathlist_head *ignores, const char *path)
2416 struct got_pathlist_entry *pe;
2419 * The ignores pathlist contains ignore lists from children before
2420 * parents, so we can find the most specific ignorelist by walking
2421 * ignores backwards.
2423 pe = TAILQ_LAST(ignores, got_pathlist_head);
2424 while (pe) {
2425 if (got_path_is_child(path, pe->path, pe->path_len)) {
2426 struct got_pathlist_head *ignorelist = pe->data;
2427 struct got_pathlist_entry *pi;
2428 TAILQ_FOREACH(pi, ignorelist, entry) {
2429 if (fnmatch(pi->path, path,
2430 FNM_PATHNAME | FNM_LEADING_DIR))
2431 continue;
2432 return 1;
2435 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2438 return 0;
2441 static const struct got_error *
2442 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2443 const char *path)
2445 const struct got_error *err = NULL;
2446 char *ignorespath;
2447 FILE *ignoresfile = NULL;
2449 /* TODO: read .gitignores as well... */
2450 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2451 path[0] ? "/" : "") == -1)
2452 return got_error_from_errno("asprintf");
2454 ignoresfile = fopen(ignorespath, "r");
2455 if (ignoresfile == NULL) {
2456 if (errno != ENOENT && errno != EACCES)
2457 err = got_error_from_errno2("fopen",
2458 ignorespath);
2459 } else
2460 err = read_ignores(ignores, path, ignoresfile);
2462 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2463 err = got_error_from_errno2("flose", path);
2464 free(ignorespath);
2465 return err;
2468 static const struct got_error *
2469 status_new(void *arg, struct dirent *de, const char *parent_path)
2471 const struct got_error *err = NULL;
2472 struct diff_dir_cb_arg *a = arg;
2473 char *path = NULL;
2475 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2476 return got_error(GOT_ERR_CANCELLED);
2478 /* XXX ignore symlinks for now */
2479 if (de->d_type == DT_LNK)
2480 return NULL;
2482 if (parent_path[0]) {
2483 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2484 return got_error_from_errno("asprintf");
2485 } else {
2486 path = de->d_name;
2489 if (de->d_type == DT_DIR)
2490 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2491 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2492 && !match_ignores(&a->ignores, path))
2493 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2494 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2495 if (parent_path[0])
2496 free(path);
2497 return err;
2500 static const struct got_error *
2501 report_single_file_status(const char *path, const char *ondisk_path,
2502 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2503 void *status_arg, struct got_repository *repo)
2505 struct got_fileindex_entry *ie;
2506 struct stat sb;
2508 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2509 if (ie)
2510 return report_file_status(ie, ondisk_path, status_cb,
2511 status_arg, repo);
2513 if (lstat(ondisk_path, &sb) == -1) {
2514 if (errno != ENOENT)
2515 return got_error_from_errno2("lstat", ondisk_path);
2516 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2517 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2518 return NULL;
2521 if (S_ISREG(sb.st_mode))
2522 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2523 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2525 return NULL;
2528 static const struct got_error *
2529 worktree_status(struct got_worktree *worktree, const char *path,
2530 struct got_fileindex *fileindex, struct got_repository *repo,
2531 got_worktree_status_cb status_cb, void *status_arg,
2532 got_cancel_cb cancel_cb, void *cancel_arg)
2534 const struct got_error *err = NULL;
2535 DIR *workdir = NULL;
2536 struct got_fileindex_diff_dir_cb fdiff_cb;
2537 struct diff_dir_cb_arg arg;
2538 char *ondisk_path = NULL;
2540 if (asprintf(&ondisk_path, "%s%s%s",
2541 worktree->root_path, path[0] ? "/" : "", path) == -1)
2542 return got_error_from_errno("asprintf");
2544 workdir = opendir(ondisk_path);
2545 if (workdir == NULL) {
2546 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2547 err = got_error_from_errno2("opendir", ondisk_path);
2548 else
2549 err = report_single_file_status(path, ondisk_path,
2550 fileindex, status_cb, status_arg, repo);
2551 } else {
2552 fdiff_cb.diff_old_new = status_old_new;
2553 fdiff_cb.diff_old = status_old;
2554 fdiff_cb.diff_new = status_new;
2555 arg.fileindex = fileindex;
2556 arg.worktree = worktree;
2557 arg.status_path = path;
2558 arg.status_path_len = strlen(path);
2559 arg.repo = repo;
2560 arg.status_cb = status_cb;
2561 arg.status_arg = status_arg;
2562 arg.cancel_cb = cancel_cb;
2563 arg.cancel_arg = cancel_arg;
2564 TAILQ_INIT(&arg.ignores);
2565 err = add_ignores(&arg.ignores, worktree->root_path, path);
2566 if (err == NULL)
2567 err = got_fileindex_diff_dir(fileindex, workdir,
2568 worktree->root_path, path, repo, &fdiff_cb, &arg);
2569 free_ignores(&arg.ignores);
2572 if (workdir)
2573 closedir(workdir);
2574 free(ondisk_path);
2575 return err;
2578 const struct got_error *
2579 got_worktree_status(struct got_worktree *worktree,
2580 struct got_pathlist_head *paths, struct got_repository *repo,
2581 got_worktree_status_cb status_cb, void *status_arg,
2582 got_cancel_cb cancel_cb, void *cancel_arg)
2584 const struct got_error *err = NULL;
2585 char *fileindex_path = NULL;
2586 struct got_fileindex *fileindex = NULL;
2587 struct got_pathlist_entry *pe;
2589 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2590 if (err)
2591 return err;
2593 TAILQ_FOREACH(pe, paths, entry) {
2594 err = worktree_status(worktree, pe->path, fileindex, repo,
2595 status_cb, status_arg, cancel_cb, cancel_arg);
2596 if (err)
2597 break;
2599 free(fileindex_path);
2600 got_fileindex_free(fileindex);
2601 return err;
2604 const struct got_error *
2605 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2606 const char *arg)
2608 const struct got_error *err = NULL;
2609 char *resolved, *cwd = NULL, *path = NULL;
2610 size_t len;
2612 *wt_path = NULL;
2614 resolved = realpath(arg, NULL);
2615 if (resolved == NULL) {
2616 if (errno != ENOENT)
2617 return got_error_from_errno2("realpath", arg);
2618 cwd = getcwd(NULL, 0);
2619 if (cwd == NULL)
2620 return got_error_from_errno("getcwd");
2621 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2622 err = got_error_from_errno("asprintf");
2623 goto done;
2627 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2628 strlen(got_worktree_get_root_path(worktree)))) {
2629 err = got_error(GOT_ERR_BAD_PATH);
2630 goto done;
2633 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2634 err = got_path_skip_common_ancestor(&path,
2635 got_worktree_get_root_path(worktree), resolved);
2636 if (err)
2637 goto done;
2638 } else {
2639 path = strdup("");
2640 if (path == NULL) {
2641 err = got_error_from_errno("strdup");
2642 goto done;
2646 /* XXX status walk can't deal with trailing slash! */
2647 len = strlen(path);
2648 while (len > 0 && path[len - 1] == '/') {
2649 path[len - 1] = '\0';
2650 len--;
2652 done:
2653 free(resolved);
2654 free(cwd);
2655 if (err == NULL)
2656 *wt_path = path;
2657 else
2658 free(path);
2659 return err;
2662 static const struct got_error *
2663 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2664 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2665 struct got_repository *repo)
2667 const struct got_error *err = NULL;
2668 struct got_fileindex_entry *ie;
2669 unsigned char status;
2670 struct stat sb;
2672 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2673 if (ie) {
2674 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2675 if (err)
2676 return err;
2677 /* Re-adding an existing entry is a no-op. */
2678 if (status == GOT_STATUS_ADD)
2679 return NULL;
2680 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2683 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2684 if (err)
2685 return err;
2687 err = got_fileindex_entry_add(fileindex, ie);
2688 if (err) {
2689 got_fileindex_entry_free(ie);
2690 return err;
2693 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2696 const struct got_error *
2697 got_worktree_schedule_add(struct got_worktree *worktree,
2698 struct got_pathlist_head *paths,
2699 got_worktree_status_cb status_cb, void *status_arg,
2700 struct got_repository *repo)
2702 struct got_fileindex *fileindex = NULL;
2703 char *fileindex_path = NULL;
2704 const struct got_error *err = NULL, *sync_err, *unlockerr;
2705 struct got_pathlist_entry *pe;
2707 err = lock_worktree(worktree, LOCK_EX);
2708 if (err)
2709 return err;
2711 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2712 if (err)
2713 goto done;
2715 TAILQ_FOREACH(pe, paths, entry) {
2716 char *ondisk_path;
2717 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2718 pe->path) == -1)
2719 return got_error_from_errno("asprintf");
2720 err = schedule_addition(ondisk_path, fileindex, pe->path,
2721 status_cb, status_arg, repo);
2722 free(ondisk_path);
2723 if (err)
2724 break;
2726 sync_err = sync_fileindex(fileindex, fileindex_path);
2727 if (sync_err && err == NULL)
2728 err = sync_err;
2729 done:
2730 free(fileindex_path);
2731 if (fileindex)
2732 got_fileindex_free(fileindex);
2733 unlockerr = lock_worktree(worktree, LOCK_SH);
2734 if (unlockerr && err == NULL)
2735 err = unlockerr;
2736 return err;
2739 static const struct got_error *
2740 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2741 const char *relpath, int delete_local_mods,
2742 got_worktree_status_cb status_cb, void *status_arg,
2743 struct got_repository *repo)
2745 const struct got_error *err = NULL;
2746 struct got_fileindex_entry *ie = NULL;
2747 unsigned char status, staged_status;
2748 struct stat sb;
2750 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2751 if (ie == NULL)
2752 return got_error(GOT_ERR_BAD_PATH);
2754 staged_status = get_staged_status(ie);
2755 if (staged_status != GOT_STATUS_NO_CHANGE) {
2756 if (staged_status == GOT_STATUS_DELETE)
2757 return NULL;
2758 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2761 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2762 if (err)
2763 return err;
2765 if (status != GOT_STATUS_NO_CHANGE) {
2766 if (status == GOT_STATUS_DELETE)
2767 return NULL;
2768 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2769 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2770 if (status != GOT_STATUS_MODIFY &&
2771 status != GOT_STATUS_MISSING)
2772 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2775 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2776 return got_error_from_errno2("unlink", ondisk_path);
2778 got_fileindex_entry_mark_deleted_from_disk(ie);
2779 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2782 const struct got_error *
2783 got_worktree_schedule_delete(struct got_worktree *worktree,
2784 struct got_pathlist_head *paths, int delete_local_mods,
2785 got_worktree_status_cb status_cb, void *status_arg,
2786 struct got_repository *repo)
2788 struct got_fileindex *fileindex = NULL;
2789 char *fileindex_path = NULL;
2790 const struct got_error *err = NULL, *sync_err, *unlockerr;
2791 struct got_pathlist_entry *pe;
2793 err = lock_worktree(worktree, LOCK_EX);
2794 if (err)
2795 return err;
2797 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2798 if (err)
2799 goto done;
2801 TAILQ_FOREACH(pe, paths, entry) {
2802 char *ondisk_path;
2803 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2804 pe->path) == -1)
2805 return got_error_from_errno("asprintf");
2806 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2807 delete_local_mods, status_cb, status_arg, repo);
2808 free(ondisk_path);
2809 if (err)
2810 break;
2812 sync_err = sync_fileindex(fileindex, fileindex_path);
2813 if (sync_err && err == NULL)
2814 err = sync_err;
2815 done:
2816 free(fileindex_path);
2817 if (fileindex)
2818 got_fileindex_free(fileindex);
2819 unlockerr = lock_worktree(worktree, LOCK_SH);
2820 if (unlockerr && err == NULL)
2821 err = unlockerr;
2822 return err;
2825 static const struct got_error *
2826 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2828 const struct got_error *err = NULL;
2829 char *line = NULL;
2830 size_t linesize = 0, n;
2831 ssize_t linelen;
2833 linelen = getline(&line, &linesize, infile);
2834 if (linelen == -1) {
2835 if (ferror(infile)) {
2836 err = got_error_from_errno("getline");
2837 goto done;
2839 return NULL;
2841 if (outfile) {
2842 n = fwrite(line, 1, linelen, outfile);
2843 if (n != linelen) {
2844 err = got_ferror(outfile, GOT_ERR_IO);
2845 goto done;
2848 if (rejectfile) {
2849 n = fwrite(line, 1, linelen, rejectfile);
2850 if (n != linelen)
2851 err = got_ferror(outfile, GOT_ERR_IO);
2853 done:
2854 free(line);
2855 return err;
2858 static const struct got_error *
2859 skip_one_line(FILE *f)
2861 char *line = NULL;
2862 size_t linesize = 0;
2863 ssize_t linelen;
2865 linelen = getline(&line, &linesize, f);
2866 free(line);
2867 if (linelen == -1 && ferror(f))
2868 return got_error_from_errno("getline");
2869 return NULL;
2872 static const struct got_error *
2873 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2874 int start_old, int end_old, int start_new, int end_new,
2875 FILE *outfile, FILE *rejectfile)
2877 const struct got_error *err;
2879 /* Copy old file's lines leading up to patch. */
2880 while (!feof(f1) && *line_cur1 < start_old) {
2881 err = copy_one_line(f1, outfile, NULL);
2882 if (err)
2883 return err;
2884 (*line_cur1)++;
2886 /* Skip new file's lines leading up to patch. */
2887 while (!feof(f2) && *line_cur2 < start_new) {
2888 if (rejectfile)
2889 err = copy_one_line(f2, NULL, rejectfile);
2890 else
2891 err = skip_one_line(f2);
2892 if (err)
2893 return err;
2894 (*line_cur2)++;
2896 /* Copy patched lines. */
2897 while (!feof(f2) && *line_cur2 <= end_new) {
2898 err = copy_one_line(f2, outfile, NULL);
2899 if (err)
2900 return err;
2901 (*line_cur2)++;
2903 /* Skip over old file's replaced lines. */
2904 while (!feof(f1) && *line_cur1 <= end_old) {
2905 if (rejectfile)
2906 err = copy_one_line(f1, NULL, rejectfile);
2907 else
2908 err = skip_one_line(f1);
2909 if (err)
2910 return err;
2911 (*line_cur1)++;
2914 return NULL;
2917 static const struct got_error *
2918 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2919 FILE *outfile, FILE *rejectfile)
2921 const struct got_error *err;
2923 if (outfile) {
2924 /* Copy old file's lines until EOF. */
2925 while (!feof(f1)) {
2926 err = copy_one_line(f1, outfile, NULL);
2927 if (err)
2928 return err;
2929 (*line_cur1)++;
2932 if (rejectfile) {
2933 /* Copy new file's lines until EOF. */
2934 while (!feof(f2)) {
2935 err = copy_one_line(f2, NULL, rejectfile);
2936 if (err)
2937 return err;
2938 (*line_cur2)++;
2942 return NULL;
2945 static const struct got_error *
2946 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2947 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2948 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2949 int *line_cur2, FILE *outfile, FILE *rejectfile,
2950 got_worktree_patch_cb patch_cb, void *patch_arg)
2952 const struct got_error *err = NULL;
2953 int start_old = change->cv.a;
2954 int end_old = change->cv.b;
2955 int start_new = change->cv.c;
2956 int end_new = change->cv.d;
2957 long pos1, pos2;
2958 FILE *hunkfile;
2960 *choice = GOT_PATCH_CHOICE_NONE;
2962 hunkfile = got_opentemp();
2963 if (hunkfile == NULL)
2964 return got_error_from_errno("got_opentemp");
2966 pos1 = ftell(f1);
2967 pos2 = ftell(f2);
2969 /* XXX TODO needs error checking */
2970 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2972 if (fseek(f1, pos1, SEEK_SET) == -1) {
2973 err = got_ferror(f1, GOT_ERR_IO);
2974 goto done;
2976 if (fseek(f2, pos2, SEEK_SET) == -1) {
2977 err = got_ferror(f1, GOT_ERR_IO);
2978 goto done;
2980 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2981 err = got_ferror(hunkfile, GOT_ERR_IO);
2982 goto done;
2985 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2986 hunkfile, n, nchanges);
2987 if (err)
2988 goto done;
2990 switch (*choice) {
2991 case GOT_PATCH_CHOICE_YES:
2992 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2993 end_old, start_new, end_new, outfile, rejectfile);
2994 break;
2995 case GOT_PATCH_CHOICE_NO:
2996 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2997 end_old, start_new, end_new, rejectfile, outfile);
2998 break;
2999 case GOT_PATCH_CHOICE_QUIT:
3000 break;
3001 default:
3002 err = got_error(GOT_ERR_PATCH_CHOICE);
3003 break;
3005 done:
3006 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3007 err = got_error_from_errno("fclose");
3008 return err;
3011 struct revert_file_args {
3012 struct got_worktree *worktree;
3013 struct got_fileindex *fileindex;
3014 got_worktree_checkout_cb progress_cb;
3015 void *progress_arg;
3016 got_worktree_patch_cb patch_cb;
3017 void *patch_arg;
3018 struct got_repository *repo;
3021 static const struct got_error *
3022 create_patched_content(char **path_outfile, int reverse_patch,
3023 struct got_object_id *blob_id, const char *path2,
3024 const char *relpath, struct got_repository *repo,
3025 got_worktree_patch_cb patch_cb, void *patch_arg)
3027 const struct got_error *err;
3028 struct got_blob_object *blob = NULL;
3029 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3030 char *path1 = NULL, *id_str = NULL;
3031 struct stat sb1, sb2;
3032 struct got_diff_changes *changes = NULL;
3033 struct got_diff_state *ds = NULL;
3034 struct got_diff_args *args = NULL;
3035 struct got_diff_change *change;
3036 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3037 int n = 0;
3039 *path_outfile = NULL;
3041 err = got_object_id_str(&id_str, blob_id);
3042 if (err)
3043 return err;
3045 f2 = fopen(path2, "r");
3046 if (f2 == NULL) {
3047 err = got_error_from_errno2("fopen", path2);
3048 goto done;
3051 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3052 if (err)
3053 goto done;
3055 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3056 if (err)
3057 goto done;
3059 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3060 if (err)
3061 goto done;
3063 if (stat(path1, &sb1) == -1) {
3064 err = got_error_from_errno2("stat", path1);
3065 goto done;
3067 if (stat(path2, &sb2) == -1) {
3068 err = got_error_from_errno2("stat", path2);
3069 goto done;
3072 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3073 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3074 if (err)
3075 goto done;
3077 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3078 if (err)
3079 goto done;
3081 if (fseek(f1, 0L, SEEK_SET) == -1)
3082 return got_ferror(f1, GOT_ERR_IO);
3083 if (fseek(f2, 0L, SEEK_SET) == -1)
3084 return got_ferror(f2, GOT_ERR_IO);
3085 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3086 int choice;
3087 err = apply_or_reject_change(&choice, change, ++n,
3088 changes->nchanges, ds, args, diff_flags, relpath,
3089 f1, f2, &line_cur1, &line_cur2,
3090 reverse_patch ? NULL : outfile,
3091 reverse_patch ? outfile : NULL,
3092 patch_cb, patch_arg);
3093 if (err)
3094 goto done;
3095 if (choice == GOT_PATCH_CHOICE_YES)
3096 have_content = 1;
3097 else if (choice == GOT_PATCH_CHOICE_QUIT)
3098 break;
3100 if (have_content)
3101 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3102 reverse_patch ? NULL : outfile,
3103 reverse_patch ? outfile : NULL);
3104 done:
3105 free(id_str);
3106 if (blob)
3107 got_object_blob_close(blob);
3108 if (f1 && fclose(f1) == EOF && err == NULL)
3109 err = got_error_from_errno2("fclose", path1);
3110 if (f2 && fclose(f2) == EOF && err == NULL)
3111 err = got_error_from_errno2("fclose", path2);
3112 if (outfile && fclose(outfile) == EOF && err == NULL)
3113 err = got_error_from_errno2("fclose", *path_outfile);
3114 if (path1 && unlink(path1) == -1 && err == NULL)
3115 err = got_error_from_errno2("unlink", path1);
3116 if (err || !have_content) {
3117 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3118 err = got_error_from_errno2("unlink", *path_outfile);
3119 free(*path_outfile);
3120 *path_outfile = NULL;
3122 free(args);
3123 if (ds) {
3124 got_diff_state_free(ds);
3125 free(ds);
3127 if (changes)
3128 got_diff_free_changes(changes);
3129 free(path1);
3130 return err;
3133 static const struct got_error *
3134 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3135 const char *relpath, struct got_object_id *blob_id,
3136 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3138 struct revert_file_args *a = arg;
3139 const struct got_error *err = NULL;
3140 char *parent_path = NULL;
3141 struct got_fileindex_entry *ie;
3142 struct got_tree_object *tree = NULL;
3143 struct got_object_id *tree_id = NULL;
3144 const struct got_tree_entry *te = NULL;
3145 char *tree_path = NULL, *te_name;
3146 char *ondisk_path = NULL, *path_content = NULL;
3147 struct got_blob_object *blob = NULL;
3149 /* Reverting a staged deletion is a no-op. */
3150 if (status == GOT_STATUS_DELETE &&
3151 staged_status != GOT_STATUS_NO_CHANGE)
3152 return NULL;
3154 if (status == GOT_STATUS_UNVERSIONED)
3155 return (*a->progress_cb)(a->progress_arg,
3156 GOT_STATUS_UNVERSIONED, relpath);
3158 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3159 if (ie == NULL)
3160 return got_error(GOT_ERR_BAD_PATH);
3162 /* Construct in-repository path of tree which contains this blob. */
3163 err = got_path_dirname(&parent_path, ie->path);
3164 if (err) {
3165 if (err->code != GOT_ERR_BAD_PATH)
3166 goto done;
3167 parent_path = strdup("/");
3168 if (parent_path == NULL) {
3169 err = got_error_from_errno("strdup");
3170 goto done;
3173 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3174 tree_path = strdup(parent_path);
3175 if (tree_path == NULL) {
3176 err = got_error_from_errno("strdup");
3177 goto done;
3179 } else {
3180 if (got_path_is_root_dir(parent_path)) {
3181 tree_path = strdup(a->worktree->path_prefix);
3182 if (tree_path == NULL) {
3183 err = got_error_from_errno("strdup");
3184 goto done;
3186 } else {
3187 if (asprintf(&tree_path, "%s/%s",
3188 a->worktree->path_prefix, parent_path) == -1) {
3189 err = got_error_from_errno("asprintf");
3190 goto done;
3195 err = got_object_id_by_path(&tree_id, a->repo,
3196 a->worktree->base_commit_id, tree_path);
3197 if (err) {
3198 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3199 (status == GOT_STATUS_ADD ||
3200 staged_status == GOT_STATUS_ADD)))
3201 goto done;
3202 } else {
3203 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3204 if (err)
3205 goto done;
3207 te_name = basename(ie->path);
3208 if (te_name == NULL) {
3209 err = got_error_from_errno2("basename", ie->path);
3210 goto done;
3213 te = got_object_tree_find_entry(tree, te_name);
3214 if (te == NULL && status != GOT_STATUS_ADD &&
3215 staged_status != GOT_STATUS_ADD) {
3216 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3217 goto done;
3221 switch (status) {
3222 case GOT_STATUS_ADD:
3223 if (a->patch_cb) {
3224 int choice = GOT_PATCH_CHOICE_NONE;
3225 err = (*a->patch_cb)(&choice, a->patch_arg,
3226 status, ie->path, NULL, 1, 1);
3227 if (err)
3228 goto done;
3229 if (choice != GOT_PATCH_CHOICE_YES)
3230 break;
3232 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3233 ie->path);
3234 if (err)
3235 goto done;
3236 got_fileindex_entry_remove(a->fileindex, ie);
3237 break;
3238 case GOT_STATUS_DELETE:
3239 if (a->patch_cb) {
3240 int choice = GOT_PATCH_CHOICE_NONE;
3241 err = (*a->patch_cb)(&choice, a->patch_arg,
3242 status, ie->path, NULL, 1, 1);
3243 if (err)
3244 goto done;
3245 if (choice != GOT_PATCH_CHOICE_YES)
3246 break;
3248 /* fall through */
3249 case GOT_STATUS_MODIFY:
3250 case GOT_STATUS_CONFLICT:
3251 case GOT_STATUS_MISSING: {
3252 struct got_object_id id;
3253 if (staged_status == GOT_STATUS_ADD ||
3254 staged_status == GOT_STATUS_MODIFY) {
3255 memcpy(id.sha1, ie->staged_blob_sha1,
3256 SHA1_DIGEST_LENGTH);
3257 } else
3258 memcpy(id.sha1, ie->blob_sha1,
3259 SHA1_DIGEST_LENGTH);
3260 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3261 if (err)
3262 goto done;
3264 if (asprintf(&ondisk_path, "%s/%s",
3265 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3266 err = got_error_from_errno("asprintf");
3267 goto done;
3270 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3271 status == GOT_STATUS_CONFLICT)) {
3272 err = create_patched_content(&path_content, 1, &id,
3273 ondisk_path, ie->path, a->repo,
3274 a->patch_cb, a->patch_arg);
3275 if (err || path_content == NULL)
3276 break;
3277 if (rename(path_content, ondisk_path) == -1) {
3278 err = got_error_from_errno3("rename",
3279 path_content, ondisk_path);
3280 goto done;
3282 } else {
3283 err = install_blob(a->worktree, ondisk_path, ie->path,
3284 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3285 got_fileindex_perms_to_st(ie), blob, 0, 1,
3286 a->repo, a->progress_cb, a->progress_arg);
3287 if (err)
3288 goto done;
3289 if (status == GOT_STATUS_DELETE) {
3290 err = update_blob_fileindex_entry(a->worktree,
3291 a->fileindex, ie, ondisk_path, ie->path,
3292 blob, 1);
3293 if (err)
3294 goto done;
3297 break;
3299 default:
3300 break;
3302 done:
3303 free(ondisk_path);
3304 free(path_content);
3305 free(parent_path);
3306 free(tree_path);
3307 if (blob)
3308 got_object_blob_close(blob);
3309 if (tree)
3310 got_object_tree_close(tree);
3311 free(tree_id);
3312 return err;
3315 const struct got_error *
3316 got_worktree_revert(struct got_worktree *worktree,
3317 struct got_pathlist_head *paths,
3318 got_worktree_checkout_cb progress_cb, void *progress_arg,
3319 got_worktree_patch_cb patch_cb, void *patch_arg,
3320 struct got_repository *repo)
3322 struct got_fileindex *fileindex = NULL;
3323 char *fileindex_path = NULL;
3324 const struct got_error *err = NULL, *unlockerr = NULL;
3325 const struct got_error *sync_err = NULL;
3326 struct got_pathlist_entry *pe;
3327 struct revert_file_args rfa;
3329 err = lock_worktree(worktree, LOCK_EX);
3330 if (err)
3331 return err;
3333 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3334 if (err)
3335 goto done;
3337 rfa.worktree = worktree;
3338 rfa.fileindex = fileindex;
3339 rfa.progress_cb = progress_cb;
3340 rfa.progress_arg = progress_arg;
3341 rfa.patch_cb = patch_cb;
3342 rfa.patch_arg = patch_arg;
3343 rfa.repo = repo;
3344 TAILQ_FOREACH(pe, paths, entry) {
3345 err = worktree_status(worktree, pe->path, fileindex, repo,
3346 revert_file, &rfa, NULL, NULL);
3347 if (err)
3348 break;
3350 sync_err = sync_fileindex(fileindex, fileindex_path);
3351 if (sync_err && err == NULL)
3352 err = sync_err;
3353 done:
3354 free(fileindex_path);
3355 if (fileindex)
3356 got_fileindex_free(fileindex);
3357 unlockerr = lock_worktree(worktree, LOCK_SH);
3358 if (unlockerr && err == NULL)
3359 err = unlockerr;
3360 return err;
3363 static void
3364 free_commitable(struct got_commitable *ct)
3366 free(ct->path);
3367 free(ct->in_repo_path);
3368 free(ct->ondisk_path);
3369 free(ct->blob_id);
3370 free(ct->base_blob_id);
3371 free(ct->staged_blob_id);
3372 free(ct->base_commit_id);
3373 free(ct);
3376 struct collect_commitables_arg {
3377 struct got_pathlist_head *commitable_paths;
3378 struct got_repository *repo;
3379 struct got_worktree *worktree;
3380 int have_staged_files;
3383 static const struct got_error *
3384 collect_commitables(void *arg, unsigned char status,
3385 unsigned char staged_status, const char *relpath,
3386 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3387 struct got_object_id *commit_id)
3389 struct collect_commitables_arg *a = arg;
3390 const struct got_error *err = NULL;
3391 struct got_commitable *ct = NULL;
3392 struct got_pathlist_entry *new = NULL;
3393 char *parent_path = NULL, *path = NULL;
3394 struct stat sb;
3396 if (a->have_staged_files) {
3397 if (staged_status != GOT_STATUS_MODIFY &&
3398 staged_status != GOT_STATUS_ADD &&
3399 staged_status != GOT_STATUS_DELETE)
3400 return NULL;
3401 } else {
3402 if (status == GOT_STATUS_CONFLICT)
3403 return got_error(GOT_ERR_COMMIT_CONFLICT);
3405 if (status != GOT_STATUS_MODIFY &&
3406 status != GOT_STATUS_ADD &&
3407 status != GOT_STATUS_DELETE)
3408 return NULL;
3411 if (asprintf(&path, "/%s", relpath) == -1) {
3412 err = got_error_from_errno("asprintf");
3413 goto done;
3415 if (strcmp(path, "/") == 0) {
3416 parent_path = strdup("");
3417 if (parent_path == NULL)
3418 return got_error_from_errno("strdup");
3419 } else {
3420 err = got_path_dirname(&parent_path, path);
3421 if (err)
3422 return err;
3425 ct = calloc(1, sizeof(*ct));
3426 if (ct == NULL) {
3427 err = got_error_from_errno("calloc");
3428 goto done;
3431 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3432 relpath) == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto done;
3436 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3437 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3438 } else {
3439 if (lstat(ct->ondisk_path, &sb) != 0) {
3440 err = got_error_from_errno2("lstat", ct->ondisk_path);
3441 goto done;
3443 ct->mode = sb.st_mode;
3446 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3447 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3448 relpath) == -1) {
3449 err = got_error_from_errno("asprintf");
3450 goto done;
3453 ct->status = status;
3454 ct->staged_status = staged_status;
3455 ct->blob_id = NULL; /* will be filled in when blob gets created */
3456 if (ct->status != GOT_STATUS_ADD &&
3457 ct->staged_status != GOT_STATUS_ADD) {
3458 ct->base_blob_id = got_object_id_dup(blob_id);
3459 if (ct->base_blob_id == NULL) {
3460 err = got_error_from_errno("got_object_id_dup");
3461 goto done;
3463 ct->base_commit_id = got_object_id_dup(commit_id);
3464 if (ct->base_commit_id == NULL) {
3465 err = got_error_from_errno("got_object_id_dup");
3466 goto done;
3469 if (ct->staged_status == GOT_STATUS_ADD ||
3470 ct->staged_status == GOT_STATUS_MODIFY) {
3471 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3472 if (ct->staged_blob_id == NULL) {
3473 err = got_error_from_errno("got_object_id_dup");
3474 goto done;
3477 ct->path = strdup(path);
3478 if (ct->path == NULL) {
3479 err = got_error_from_errno("strdup");
3480 goto done;
3482 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3483 done:
3484 if (ct && (err || new == NULL))
3485 free_commitable(ct);
3486 free(parent_path);
3487 free(path);
3488 return err;
3491 static const struct got_error *write_tree(struct got_object_id **,
3492 struct got_tree_object *, const char *, struct got_pathlist_head *,
3493 got_worktree_status_cb status_cb, void *status_arg,
3494 struct got_repository *);
3496 static const struct got_error *
3497 write_subtree(struct got_object_id **new_subtree_id,
3498 struct got_tree_entry *te, const char *parent_path,
3499 struct got_pathlist_head *commitable_paths,
3500 got_worktree_status_cb status_cb, void *status_arg,
3501 struct got_repository *repo)
3503 const struct got_error *err = NULL;
3504 struct got_tree_object *subtree;
3505 char *subpath;
3507 if (asprintf(&subpath, "%s%s%s", parent_path,
3508 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3509 return got_error_from_errno("asprintf");
3511 err = got_object_open_as_tree(&subtree, repo, te->id);
3512 if (err)
3513 return err;
3515 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3516 status_cb, status_arg, repo);
3517 got_object_tree_close(subtree);
3518 free(subpath);
3519 return err;
3522 static const struct got_error *
3523 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3525 const struct got_error *err = NULL;
3526 char *ct_parent_path = NULL;
3528 *match = 0;
3530 if (strchr(ct->in_repo_path, '/') == NULL) {
3531 *match = got_path_is_root_dir(path);
3532 return NULL;
3535 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3536 if (err)
3537 return err;
3538 *match = (strcmp(path, ct_parent_path) == 0);
3539 free(ct_parent_path);
3540 return err;
3543 static mode_t
3544 get_ct_file_mode(struct got_commitable *ct)
3546 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3549 static const struct got_error *
3550 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3551 struct got_tree_entry *te, struct got_commitable *ct)
3553 const struct got_error *err = NULL;
3555 *new_te = NULL;
3557 err = got_object_tree_entry_dup(new_te, te);
3558 if (err)
3559 goto done;
3561 (*new_te)->mode = get_ct_file_mode(ct);
3563 free((*new_te)->id);
3564 if (ct->staged_status == GOT_STATUS_MODIFY)
3565 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3566 else
3567 (*new_te)->id = got_object_id_dup(ct->blob_id);
3568 if ((*new_te)->id == NULL) {
3569 err = got_error_from_errno("got_object_id_dup");
3570 goto done;
3572 done:
3573 if (err && *new_te) {
3574 got_object_tree_entry_close(*new_te);
3575 *new_te = NULL;
3577 return err;
3580 static const struct got_error *
3581 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3582 struct got_commitable *ct)
3584 const struct got_error *err = NULL;
3585 char *ct_name;
3587 *new_te = NULL;
3589 *new_te = calloc(1, sizeof(**new_te));
3590 if (*new_te == NULL)
3591 return got_error_from_errno("calloc");
3593 ct_name = basename(ct->path);
3594 if (ct_name == NULL) {
3595 err = got_error_from_errno2("basename", ct->path);
3596 goto done;
3598 (*new_te)->name = strdup(ct_name);
3599 if ((*new_te)->name == NULL) {
3600 err = got_error_from_errno("strdup");
3601 goto done;
3604 (*new_te)->mode = get_ct_file_mode(ct);
3606 if (ct->staged_status == GOT_STATUS_ADD)
3607 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3608 else
3609 (*new_te)->id = got_object_id_dup(ct->blob_id);
3610 if ((*new_te)->id == NULL) {
3611 err = got_error_from_errno("got_object_id_dup");
3612 goto done;
3614 done:
3615 if (err && *new_te) {
3616 got_object_tree_entry_close(*new_te);
3617 *new_te = NULL;
3619 return err;
3622 static const struct got_error *
3623 insert_tree_entry(struct got_tree_entry *new_te,
3624 struct got_pathlist_head *paths)
3626 const struct got_error *err = NULL;
3627 struct got_pathlist_entry *new_pe;
3629 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3630 if (err)
3631 return err;
3632 if (new_pe == NULL)
3633 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3634 return NULL;
3637 static const struct got_error *
3638 report_ct_status(struct got_commitable *ct,
3639 got_worktree_status_cb status_cb, void *status_arg)
3641 const char *ct_path = ct->path;
3642 unsigned char status;
3644 while (ct_path[0] == '/')
3645 ct_path++;
3647 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3648 status = ct->staged_status;
3649 else
3650 status = ct->status;
3652 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3653 ct_path, ct->blob_id, NULL, NULL);
3656 static const struct got_error *
3657 match_modified_subtree(int *modified, struct got_tree_entry *te,
3658 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3660 const struct got_error *err = NULL;
3661 struct got_pathlist_entry *pe;
3662 char *te_path;
3664 *modified = 0;
3666 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3667 got_path_is_root_dir(base_tree_path) ? "" : "/",
3668 te->name) == -1)
3669 return got_error_from_errno("asprintf");
3671 TAILQ_FOREACH(pe, commitable_paths, entry) {
3672 struct got_commitable *ct = pe->data;
3673 *modified = got_path_is_child(ct->in_repo_path, te_path,
3674 strlen(te_path));
3675 if (*modified)
3676 break;
3679 free(te_path);
3680 return err;
3683 static const struct got_error *
3684 match_deleted_or_modified_ct(struct got_commitable **ctp,
3685 struct got_tree_entry *te, const char *base_tree_path,
3686 struct got_pathlist_head *commitable_paths)
3688 const struct got_error *err = NULL;
3689 struct got_pathlist_entry *pe;
3691 *ctp = NULL;
3693 TAILQ_FOREACH(pe, commitable_paths, entry) {
3694 struct got_commitable *ct = pe->data;
3695 char *ct_name = NULL;
3696 int path_matches;
3698 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3699 if (ct->status != GOT_STATUS_MODIFY &&
3700 ct->status != GOT_STATUS_DELETE)
3701 continue;
3702 } else {
3703 if (ct->staged_status != GOT_STATUS_MODIFY &&
3704 ct->staged_status != GOT_STATUS_DELETE)
3705 continue;
3708 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3709 continue;
3711 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3712 if (err)
3713 return err;
3714 if (!path_matches)
3715 continue;
3717 ct_name = basename(pe->path);
3718 if (ct_name == NULL)
3719 return got_error_from_errno2("basename", pe->path);
3721 if (strcmp(te->name, ct_name) != 0)
3722 continue;
3724 *ctp = ct;
3725 break;
3728 return err;
3731 static const struct got_error *
3732 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3733 const char *child_path, const char *path_base_tree,
3734 struct got_pathlist_head *commitable_paths,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 struct got_repository *repo)
3738 const struct got_error *err = NULL;
3739 struct got_tree_entry *new_te;
3740 char *subtree_path;
3742 *new_tep = NULL;
3744 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3745 got_path_is_root_dir(path_base_tree) ? "" : "/",
3746 child_path) == -1)
3747 return got_error_from_errno("asprintf");
3749 new_te = calloc(1, sizeof(*new_te));
3750 if (new_te == NULL)
3751 return got_error_from_errno("calloc");
3752 new_te->mode = S_IFDIR;
3753 new_te->name = strdup(child_path);
3754 if (new_te->name == NULL) {
3755 err = got_error_from_errno("strdup");
3756 got_object_tree_entry_close(new_te);
3757 goto done;
3759 err = write_tree(&new_te->id, NULL, subtree_path,
3760 commitable_paths, status_cb, status_arg, repo);
3761 if (err) {
3762 got_object_tree_entry_close(new_te);
3763 goto done;
3765 done:
3766 free(subtree_path);
3767 if (err == NULL)
3768 *new_tep = new_te;
3769 return err;
3772 static const struct got_error *
3773 write_tree(struct got_object_id **new_tree_id,
3774 struct got_tree_object *base_tree, const char *path_base_tree,
3775 struct got_pathlist_head *commitable_paths,
3776 got_worktree_status_cb status_cb, void *status_arg,
3777 struct got_repository *repo)
3779 const struct got_error *err = NULL;
3780 const struct got_tree_entries *base_entries = NULL;
3781 struct got_pathlist_head paths;
3782 struct got_tree_entries new_tree_entries;
3783 struct got_tree_entry *te, *new_te = NULL;
3784 struct got_pathlist_entry *pe;
3786 TAILQ_INIT(&paths);
3787 new_tree_entries.nentries = 0;
3788 SIMPLEQ_INIT(&new_tree_entries.head);
3790 /* Insert, and recurse into, newly added entries first. */
3791 TAILQ_FOREACH(pe, commitable_paths, entry) {
3792 struct got_commitable *ct = pe->data;
3793 char *child_path = NULL, *slash;
3795 if ((ct->status != GOT_STATUS_ADD &&
3796 ct->staged_status != GOT_STATUS_ADD) ||
3797 (ct->flags & GOT_COMMITABLE_ADDED))
3798 continue;
3800 if (!got_path_is_child(pe->path, path_base_tree,
3801 strlen(path_base_tree)))
3802 continue;
3804 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3805 pe->path);
3806 if (err)
3807 goto done;
3809 slash = strchr(child_path, '/');
3810 if (slash == NULL) {
3811 err = alloc_added_blob_tree_entry(&new_te, ct);
3812 if (err)
3813 goto done;
3814 err = report_ct_status(ct, status_cb, status_arg);
3815 if (err)
3816 goto done;
3817 ct->flags |= GOT_COMMITABLE_ADDED;
3818 err = insert_tree_entry(new_te, &paths);
3819 if (err)
3820 goto done;
3821 } else {
3822 *slash = '\0'; /* trim trailing path components */
3823 if (base_tree == NULL ||
3824 got_object_tree_find_entry(base_tree, child_path)
3825 == NULL) {
3826 err = make_subtree_for_added_blob(&new_te,
3827 child_path, path_base_tree,
3828 commitable_paths, status_cb, status_arg,
3829 repo);
3830 if (err)
3831 goto done;
3832 err = insert_tree_entry(new_te, &paths);
3833 if (err)
3834 goto done;
3839 if (base_tree) {
3840 /* Handle modified and deleted entries. */
3841 base_entries = got_object_tree_get_entries(base_tree);
3842 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3843 struct got_commitable *ct = NULL;
3845 if (got_object_tree_entry_is_submodule(te)) {
3846 /* Entry is a submodule; just copy it. */
3847 err = got_object_tree_entry_dup(&new_te, te);
3848 if (err)
3849 goto done;
3850 err = insert_tree_entry(new_te, &paths);
3851 if (err)
3852 goto done;
3853 continue;
3856 if (S_ISDIR(te->mode)) {
3857 int modified;
3858 err = got_object_tree_entry_dup(&new_te, te);
3859 if (err)
3860 goto done;
3861 err = match_modified_subtree(&modified, te,
3862 path_base_tree, commitable_paths);
3863 if (err)
3864 goto done;
3865 /* Avoid recursion into unmodified subtrees. */
3866 if (modified) {
3867 free(new_te->id);
3868 err = write_subtree(&new_te->id, te,
3869 path_base_tree, commitable_paths,
3870 status_cb, status_arg, repo);
3871 if (err)
3872 goto done;
3874 err = insert_tree_entry(new_te, &paths);
3875 if (err)
3876 goto done;
3877 continue;
3880 err = match_deleted_or_modified_ct(&ct, te,
3881 path_base_tree, commitable_paths);
3882 if (err)
3883 goto done;
3884 if (ct) {
3885 /* NB: Deleted entries get dropped here. */
3886 if (ct->status == GOT_STATUS_MODIFY ||
3887 ct->staged_status == GOT_STATUS_MODIFY) {
3888 err = alloc_modified_blob_tree_entry(
3889 &new_te, te, ct);
3890 if (err)
3891 goto done;
3892 err = insert_tree_entry(new_te, &paths);
3893 if (err)
3894 goto done;
3896 err = report_ct_status(ct, status_cb,
3897 status_arg);
3898 if (err)
3899 goto done;
3900 } else {
3901 /* Entry is unchanged; just copy it. */
3902 err = got_object_tree_entry_dup(&new_te, te);
3903 if (err)
3904 goto done;
3905 err = insert_tree_entry(new_te, &paths);
3906 if (err)
3907 goto done;
3912 /* Write new list of entries; deleted entries have been dropped. */
3913 TAILQ_FOREACH(pe, &paths, entry) {
3914 struct got_tree_entry *te = pe->data;
3915 new_tree_entries.nentries++;
3916 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3918 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3919 done:
3920 got_object_tree_entries_close(&new_tree_entries);
3921 got_pathlist_free(&paths);
3922 return err;
3925 static const struct got_error *
3926 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3927 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3928 int have_staged_files)
3930 const struct got_error *err = NULL;
3931 struct got_pathlist_entry *pe;
3933 TAILQ_FOREACH(pe, commitable_paths, entry) {
3934 struct got_fileindex_entry *ie;
3935 struct got_commitable *ct = pe->data;
3937 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3938 if (ie) {
3939 if (ct->status == GOT_STATUS_DELETE ||
3940 ct->staged_status == GOT_STATUS_DELETE) {
3941 got_fileindex_entry_remove(fileindex, ie);
3942 got_fileindex_entry_free(ie);
3943 } else if (ct->staged_status == GOT_STATUS_ADD ||
3944 ct->staged_status == GOT_STATUS_MODIFY) {
3945 got_fileindex_entry_stage_set(ie,
3946 GOT_FILEIDX_STAGE_NONE);
3947 err = got_fileindex_entry_update(ie,
3948 ct->ondisk_path, ct->staged_blob_id->sha1,
3949 new_base_commit_id->sha1,
3950 !have_staged_files);
3951 } else
3952 err = got_fileindex_entry_update(ie,
3953 ct->ondisk_path, ct->blob_id->sha1,
3954 new_base_commit_id->sha1,
3955 !have_staged_files);
3956 } else {
3957 err = got_fileindex_entry_alloc(&ie,
3958 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3959 new_base_commit_id->sha1);
3960 if (err)
3961 break;
3962 err = got_fileindex_entry_add(fileindex, ie);
3963 if (err)
3964 break;
3967 return err;
3971 static const struct got_error *
3972 check_out_of_date(const char *in_repo_path, unsigned char status,
3973 unsigned char staged_status, struct got_object_id *base_blob_id,
3974 struct got_object_id *base_commit_id,
3975 struct got_object_id *head_commit_id, struct got_repository *repo,
3976 int ood_errcode)
3978 const struct got_error *err = NULL;
3979 struct got_object_id *id = NULL;
3981 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3982 /* Trivial case: base commit == head commit */
3983 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3984 return NULL;
3986 * Ensure file content which local changes were based
3987 * on matches file content in the branch head.
3989 err = got_object_id_by_path(&id, repo, head_commit_id,
3990 in_repo_path);
3991 if (err) {
3992 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3993 err = got_error(ood_errcode);
3994 goto done;
3995 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3996 err = got_error(ood_errcode);
3997 } else {
3998 /* Require that added files don't exist in the branch head. */
3999 err = got_object_id_by_path(&id, repo, head_commit_id,
4000 in_repo_path);
4001 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4002 goto done;
4003 err = id ? got_error(ood_errcode) : NULL;
4005 done:
4006 free(id);
4007 return err;
4010 const struct got_error *
4011 commit_worktree(struct got_object_id **new_commit_id,
4012 struct got_pathlist_head *commitable_paths,
4013 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4014 const char *author, const char *committer,
4015 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4016 got_worktree_status_cb status_cb, void *status_arg,
4017 struct got_repository *repo)
4019 const struct got_error *err = NULL, *unlockerr = NULL;
4020 struct got_pathlist_entry *pe;
4021 const char *head_ref_name = NULL;
4022 struct got_commit_object *head_commit = NULL;
4023 struct got_reference *head_ref2 = NULL;
4024 struct got_object_id *head_commit_id2 = NULL;
4025 struct got_tree_object *head_tree = NULL;
4026 struct got_object_id *new_tree_id = NULL;
4027 struct got_object_id_queue parent_ids;
4028 struct got_object_qid *pid = NULL;
4029 char *logmsg = NULL;
4031 *new_commit_id = NULL;
4033 SIMPLEQ_INIT(&parent_ids);
4035 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4036 if (err)
4037 goto done;
4039 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4040 if (err)
4041 goto done;
4043 if (commit_msg_cb != NULL) {
4044 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4045 if (err)
4046 goto done;
4049 if (logmsg == NULL || strlen(logmsg) == 0) {
4050 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4051 goto done;
4054 /* Create blobs from added and modified files and record their IDs. */
4055 TAILQ_FOREACH(pe, commitable_paths, entry) {
4056 struct got_commitable *ct = pe->data;
4057 char *ondisk_path;
4059 /* Blobs for staged files already exist. */
4060 if (ct->staged_status == GOT_STATUS_ADD ||
4061 ct->staged_status == GOT_STATUS_MODIFY)
4062 continue;
4064 if (ct->status != GOT_STATUS_ADD &&
4065 ct->status != GOT_STATUS_MODIFY)
4066 continue;
4068 if (asprintf(&ondisk_path, "%s/%s",
4069 worktree->root_path, pe->path) == -1) {
4070 err = got_error_from_errno("asprintf");
4071 goto done;
4073 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4074 free(ondisk_path);
4075 if (err)
4076 goto done;
4079 /* Recursively write new tree objects. */
4080 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4081 status_cb, status_arg, repo);
4082 if (err)
4083 goto done;
4085 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4086 if (err)
4087 goto done;
4088 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4089 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4090 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4091 got_object_qid_free(pid);
4092 if (logmsg != NULL)
4093 free(logmsg);
4094 if (err)
4095 goto done;
4097 /* Check if a concurrent commit to our branch has occurred. */
4098 head_ref_name = got_worktree_get_head_ref_name(worktree);
4099 if (head_ref_name == NULL) {
4100 err = got_error_from_errno("got_worktree_get_head_ref_name");
4101 goto done;
4103 /* Lock the reference here to prevent concurrent modification. */
4104 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4105 if (err)
4106 goto done;
4107 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4108 if (err)
4109 goto done;
4110 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4111 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4112 goto done;
4114 /* Update branch head in repository. */
4115 err = got_ref_change_ref(head_ref2, *new_commit_id);
4116 if (err)
4117 goto done;
4118 err = got_ref_write(head_ref2, repo);
4119 if (err)
4120 goto done;
4122 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4123 if (err)
4124 goto done;
4126 err = ref_base_commit(worktree, repo);
4127 if (err)
4128 goto done;
4129 done:
4130 if (head_tree)
4131 got_object_tree_close(head_tree);
4132 if (head_commit)
4133 got_object_commit_close(head_commit);
4134 free(head_commit_id2);
4135 if (head_ref2) {
4136 unlockerr = got_ref_unlock(head_ref2);
4137 if (unlockerr && err == NULL)
4138 err = unlockerr;
4139 got_ref_close(head_ref2);
4141 return err;
4144 static const struct got_error *
4145 check_path_is_commitable(const char *path,
4146 struct got_pathlist_head *commitable_paths)
4148 struct got_pathlist_entry *cpe = NULL;
4149 size_t path_len = strlen(path);
4151 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4152 struct got_commitable *ct = cpe->data;
4153 const char *ct_path = ct->path;
4155 while (ct_path[0] == '/')
4156 ct_path++;
4158 if (strcmp(path, ct_path) == 0 ||
4159 got_path_is_child(ct_path, path, path_len))
4160 break;
4163 if (cpe == NULL)
4164 return got_error_path(path, GOT_ERR_BAD_PATH);
4166 return NULL;
4169 static const struct got_error *
4170 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4172 int *have_staged_files = arg;
4174 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4175 *have_staged_files = 1;
4176 return got_error(GOT_ERR_CANCELLED);
4179 return NULL;
4182 static const struct got_error *
4183 check_non_staged_files(struct got_fileindex *fileindex,
4184 struct got_pathlist_head *paths)
4186 struct got_pathlist_entry *pe;
4187 struct got_fileindex_entry *ie;
4189 TAILQ_FOREACH(pe, paths, entry) {
4190 if (pe->path[0] == '\0')
4191 continue;
4192 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4193 if (ie == NULL)
4194 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4195 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4196 return got_error_path(pe->path,
4197 GOT_ERR_FILE_NOT_STAGED);
4200 return NULL;
4203 const struct got_error *
4204 got_worktree_commit(struct got_object_id **new_commit_id,
4205 struct got_worktree *worktree, struct got_pathlist_head *paths,
4206 const char *author, const char *committer,
4207 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4208 got_worktree_status_cb status_cb, void *status_arg,
4209 struct got_repository *repo)
4211 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4212 struct got_fileindex *fileindex = NULL;
4213 char *fileindex_path = NULL;
4214 struct got_pathlist_head commitable_paths;
4215 struct collect_commitables_arg cc_arg;
4216 struct got_pathlist_entry *pe;
4217 struct got_reference *head_ref = NULL;
4218 struct got_object_id *head_commit_id = NULL;
4219 int have_staged_files = 0;
4221 *new_commit_id = NULL;
4223 TAILQ_INIT(&commitable_paths);
4225 err = lock_worktree(worktree, LOCK_EX);
4226 if (err)
4227 goto done;
4229 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4230 if (err)
4231 goto done;
4233 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4234 if (err)
4235 goto done;
4237 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4238 if (err)
4239 goto done;
4241 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4242 &have_staged_files);
4243 if (err && err->code != GOT_ERR_CANCELLED)
4244 goto done;
4245 if (have_staged_files) {
4246 err = check_non_staged_files(fileindex, paths);
4247 if (err)
4248 goto done;
4251 cc_arg.commitable_paths = &commitable_paths;
4252 cc_arg.worktree = worktree;
4253 cc_arg.repo = repo;
4254 cc_arg.have_staged_files = have_staged_files;
4255 TAILQ_FOREACH(pe, paths, entry) {
4256 err = worktree_status(worktree, pe->path, fileindex, repo,
4257 collect_commitables, &cc_arg, NULL, NULL);
4258 if (err)
4259 goto done;
4262 if (TAILQ_EMPTY(&commitable_paths)) {
4263 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4264 goto done;
4267 TAILQ_FOREACH(pe, paths, entry) {
4268 err = check_path_is_commitable(pe->path, &commitable_paths);
4269 if (err)
4270 goto done;
4273 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4274 struct got_commitable *ct = pe->data;
4275 const char *ct_path = ct->in_repo_path;
4277 while (ct_path[0] == '/')
4278 ct_path++;
4279 err = check_out_of_date(ct_path, ct->status,
4280 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4281 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4282 if (err)
4283 goto done;
4287 err = commit_worktree(new_commit_id, &commitable_paths,
4288 head_commit_id, worktree, author, committer,
4289 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4290 if (err)
4291 goto done;
4293 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4294 fileindex, have_staged_files);
4295 sync_err = sync_fileindex(fileindex, fileindex_path);
4296 if (sync_err && err == NULL)
4297 err = sync_err;
4298 done:
4299 if (fileindex)
4300 got_fileindex_free(fileindex);
4301 free(fileindex_path);
4302 unlockerr = lock_worktree(worktree, LOCK_SH);
4303 if (unlockerr && err == NULL)
4304 err = unlockerr;
4305 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4306 struct got_commitable *ct = pe->data;
4307 free_commitable(ct);
4309 got_pathlist_free(&commitable_paths);
4310 return err;
4313 const char *
4314 got_commitable_get_path(struct got_commitable *ct)
4316 return ct->path;
4319 unsigned int
4320 got_commitable_get_status(struct got_commitable *ct)
4322 return ct->status;
4325 struct check_rebase_ok_arg {
4326 struct got_worktree *worktree;
4327 struct got_repository *repo;
4330 static const struct got_error *
4331 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4333 const struct got_error *err = NULL;
4334 struct check_rebase_ok_arg *a = arg;
4335 unsigned char status;
4336 struct stat sb;
4337 char *ondisk_path;
4339 /* Reject rebase of a work tree with mixed base commits. */
4340 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4341 SHA1_DIGEST_LENGTH))
4342 return got_error(GOT_ERR_MIXED_COMMITS);
4344 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4345 == -1)
4346 return got_error_from_errno("asprintf");
4348 /* Reject rebase of a work tree with modified or staged files. */
4349 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4350 free(ondisk_path);
4351 if (err)
4352 return err;
4354 if (status != GOT_STATUS_NO_CHANGE)
4355 return got_error(GOT_ERR_MODIFIED);
4356 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4357 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4359 return NULL;
4362 const struct got_error *
4363 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4364 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4365 struct got_worktree *worktree, struct got_reference *branch,
4366 struct got_repository *repo)
4368 const struct got_error *err = NULL;
4369 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4370 char *branch_ref_name = NULL;
4371 char *fileindex_path = NULL;
4372 struct check_rebase_ok_arg ok_arg;
4373 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4375 *new_base_branch_ref = NULL;
4376 *tmp_branch = NULL;
4377 *fileindex = NULL;
4379 err = lock_worktree(worktree, LOCK_EX);
4380 if (err)
4381 return err;
4383 err = open_fileindex(fileindex, &fileindex_path, worktree);
4384 if (err)
4385 goto done;
4387 ok_arg.worktree = worktree;
4388 ok_arg.repo = repo;
4389 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4390 &ok_arg);
4391 if (err)
4392 goto done;
4394 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4395 if (err)
4396 goto done;
4398 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4399 if (err)
4400 goto done;
4402 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4403 if (err)
4404 goto done;
4406 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4407 0);
4408 if (err)
4409 goto done;
4411 err = got_ref_alloc_symref(new_base_branch_ref,
4412 new_base_branch_ref_name, wt_branch);
4413 if (err)
4414 goto done;
4415 err = got_ref_write(*new_base_branch_ref, repo);
4416 if (err)
4417 goto done;
4419 /* TODO Lock original branch's ref while rebasing? */
4421 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4422 if (err)
4423 goto done;
4425 err = got_ref_write(branch_ref, repo);
4426 if (err)
4427 goto done;
4429 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4430 worktree->base_commit_id);
4431 if (err)
4432 goto done;
4433 err = got_ref_write(*tmp_branch, repo);
4434 if (err)
4435 goto done;
4437 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4438 if (err)
4439 goto done;
4440 done:
4441 free(fileindex_path);
4442 free(tmp_branch_name);
4443 free(new_base_branch_ref_name);
4444 free(branch_ref_name);
4445 if (branch_ref)
4446 got_ref_close(branch_ref);
4447 if (wt_branch)
4448 got_ref_close(wt_branch);
4449 if (err) {
4450 if (*new_base_branch_ref) {
4451 got_ref_close(*new_base_branch_ref);
4452 *new_base_branch_ref = NULL;
4454 if (*tmp_branch) {
4455 got_ref_close(*tmp_branch);
4456 *tmp_branch = NULL;
4458 if (*fileindex) {
4459 got_fileindex_free(*fileindex);
4460 *fileindex = NULL;
4462 lock_worktree(worktree, LOCK_SH);
4464 return err;
4467 const struct got_error *
4468 got_worktree_rebase_continue(struct got_object_id **commit_id,
4469 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4470 struct got_reference **branch, struct got_fileindex **fileindex,
4471 struct got_worktree *worktree, struct got_repository *repo)
4473 const struct got_error *err;
4474 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4475 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4476 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4477 char *fileindex_path = NULL;
4478 int have_staged_files = 0;
4480 *commit_id = NULL;
4481 *new_base_branch = NULL;
4482 *tmp_branch = NULL;
4483 *branch = NULL;
4484 *fileindex = NULL;
4486 err = lock_worktree(worktree, LOCK_EX);
4487 if (err)
4488 return err;
4490 err = open_fileindex(fileindex, &fileindex_path, worktree);
4491 if (err)
4492 goto done;
4494 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4495 &have_staged_files);
4496 if (err && err->code != GOT_ERR_CANCELLED)
4497 goto done;
4498 if (have_staged_files) {
4499 err = got_error(GOT_ERR_STAGED_PATHS);
4500 goto done;
4503 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4504 if (err)
4505 goto done;
4507 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4508 if (err)
4509 goto done;
4511 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4512 if (err)
4513 goto done;
4515 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4516 if (err)
4517 goto done;
4519 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4520 if (err)
4521 goto done;
4523 err = got_ref_open(branch, repo,
4524 got_ref_get_symref_target(branch_ref), 0);
4525 if (err)
4526 goto done;
4528 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4529 if (err)
4530 goto done;
4532 err = got_ref_resolve(commit_id, repo, commit_ref);
4533 if (err)
4534 goto done;
4536 err = got_ref_open(new_base_branch, repo,
4537 new_base_branch_ref_name, 0);
4538 if (err)
4539 goto done;
4541 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4542 if (err)
4543 goto done;
4544 done:
4545 free(commit_ref_name);
4546 free(branch_ref_name);
4547 free(fileindex_path);
4548 if (commit_ref)
4549 got_ref_close(commit_ref);
4550 if (branch_ref)
4551 got_ref_close(branch_ref);
4552 if (err) {
4553 free(*commit_id);
4554 *commit_id = NULL;
4555 if (*tmp_branch) {
4556 got_ref_close(*tmp_branch);
4557 *tmp_branch = NULL;
4559 if (*new_base_branch) {
4560 got_ref_close(*new_base_branch);
4561 *new_base_branch = NULL;
4563 if (*branch) {
4564 got_ref_close(*branch);
4565 *branch = NULL;
4567 if (*fileindex) {
4568 got_fileindex_free(*fileindex);
4569 *fileindex = NULL;
4571 lock_worktree(worktree, LOCK_SH);
4573 return err;
4576 const struct got_error *
4577 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4579 const struct got_error *err;
4580 char *tmp_branch_name = NULL;
4582 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4583 if (err)
4584 return err;
4586 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4587 free(tmp_branch_name);
4588 return NULL;
4591 static const struct got_error *
4592 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4593 char **logmsg, void *arg)
4595 *logmsg = arg;
4596 return NULL;
4599 static const struct got_error *
4600 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4601 const char *path, struct got_object_id *blob_id,
4602 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4604 return NULL;
4607 struct collect_merged_paths_arg {
4608 got_worktree_checkout_cb progress_cb;
4609 void *progress_arg;
4610 struct got_pathlist_head *merged_paths;
4613 static const struct got_error *
4614 collect_merged_paths(void *arg, unsigned char status, const char *path)
4616 const struct got_error *err;
4617 struct collect_merged_paths_arg *a = arg;
4618 char *p;
4619 struct got_pathlist_entry *new;
4621 err = (*a->progress_cb)(a->progress_arg, status, path);
4622 if (err)
4623 return err;
4625 if (status != GOT_STATUS_MERGE &&
4626 status != GOT_STATUS_ADD &&
4627 status != GOT_STATUS_DELETE &&
4628 status != GOT_STATUS_CONFLICT)
4629 return NULL;
4631 p = strdup(path);
4632 if (p == NULL)
4633 return got_error_from_errno("strdup");
4635 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4636 if (err || new == NULL)
4637 free(p);
4638 return err;
4641 void
4642 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4644 struct got_pathlist_entry *pe;
4646 TAILQ_FOREACH(pe, merged_paths, entry)
4647 free((char *)pe->path);
4649 got_pathlist_free(merged_paths);
4652 static const struct got_error *
4653 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4654 struct got_repository *repo)
4656 const struct got_error *err;
4657 struct got_reference *commit_ref = NULL;
4659 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4660 if (err) {
4661 if (err->code != GOT_ERR_NOT_REF)
4662 goto done;
4663 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4664 if (err)
4665 goto done;
4666 err = got_ref_write(commit_ref, repo);
4667 if (err)
4668 goto done;
4669 } else {
4670 struct got_object_id *stored_id;
4671 int cmp;
4673 err = got_ref_resolve(&stored_id, repo, commit_ref);
4674 if (err)
4675 goto done;
4676 cmp = got_object_id_cmp(commit_id, stored_id);
4677 free(stored_id);
4678 if (cmp != 0) {
4679 err = got_error(GOT_ERR_REBASE_COMMITID);
4680 goto done;
4683 done:
4684 if (commit_ref)
4685 got_ref_close(commit_ref);
4686 return err;
4689 static const struct got_error *
4690 rebase_merge_files(struct got_pathlist_head *merged_paths,
4691 const char *commit_ref_name, struct got_worktree *worktree,
4692 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4693 struct got_object_id *commit_id, struct got_repository *repo,
4694 got_worktree_checkout_cb progress_cb, void *progress_arg,
4695 got_cancel_cb cancel_cb, void *cancel_arg)
4697 const struct got_error *err;
4698 struct got_reference *commit_ref = NULL;
4699 struct collect_merged_paths_arg cmp_arg;
4700 char *fileindex_path;
4702 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4704 err = get_fileindex_path(&fileindex_path, worktree);
4705 if (err)
4706 return err;
4708 cmp_arg.progress_cb = progress_cb;
4709 cmp_arg.progress_arg = progress_arg;
4710 cmp_arg.merged_paths = merged_paths;
4711 err = merge_files(worktree, fileindex, fileindex_path,
4712 parent_commit_id, commit_id, repo, collect_merged_paths,
4713 &cmp_arg, cancel_cb, cancel_arg);
4714 if (commit_ref)
4715 got_ref_close(commit_ref);
4716 return err;
4719 const struct got_error *
4720 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4721 struct got_worktree *worktree, struct got_fileindex *fileindex,
4722 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4723 struct got_repository *repo,
4724 got_worktree_checkout_cb progress_cb, void *progress_arg,
4725 got_cancel_cb cancel_cb, void *cancel_arg)
4727 const struct got_error *err;
4728 char *commit_ref_name;
4730 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4731 if (err)
4732 return err;
4734 err = store_commit_id(commit_ref_name, commit_id, repo);
4735 if (err)
4736 goto done;
4738 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4739 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4740 progress_arg, cancel_cb, cancel_arg);
4741 done:
4742 free(commit_ref_name);
4743 return err;
4746 const struct got_error *
4747 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4748 struct got_worktree *worktree, struct got_fileindex *fileindex,
4749 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4750 struct got_repository *repo,
4751 got_worktree_checkout_cb progress_cb, void *progress_arg,
4752 got_cancel_cb cancel_cb, void *cancel_arg)
4754 const struct got_error *err;
4755 char *commit_ref_name;
4757 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4758 if (err)
4759 return err;
4761 err = store_commit_id(commit_ref_name, commit_id, repo);
4762 if (err)
4763 goto done;
4765 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4766 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4767 progress_arg, cancel_cb, cancel_arg);
4768 done:
4769 free(commit_ref_name);
4770 return err;
4773 static const struct got_error *
4774 rebase_commit(struct got_object_id **new_commit_id,
4775 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4776 struct got_worktree *worktree, struct got_fileindex *fileindex,
4777 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4778 const char *new_logmsg, struct got_repository *repo)
4780 const struct got_error *err, *sync_err;
4781 struct got_pathlist_head commitable_paths;
4782 struct collect_commitables_arg cc_arg;
4783 char *fileindex_path = NULL;
4784 struct got_reference *head_ref = NULL;
4785 struct got_object_id *head_commit_id = NULL;
4786 char *logmsg = NULL;
4788 TAILQ_INIT(&commitable_paths);
4789 *new_commit_id = NULL;
4791 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4793 err = get_fileindex_path(&fileindex_path, worktree);
4794 if (err)
4795 return err;
4797 cc_arg.commitable_paths = &commitable_paths;
4798 cc_arg.worktree = worktree;
4799 cc_arg.repo = repo;
4800 cc_arg.have_staged_files = 0;
4802 * If possible get the status of individual files directly to
4803 * avoid crawling the entire work tree once per rebased commit.
4804 * TODO: Ideally, merged_paths would contain a list of commitables
4805 * we could use so we could skip worktree_status() entirely.
4807 if (merged_paths) {
4808 struct got_pathlist_entry *pe;
4809 if (TAILQ_EMPTY(merged_paths)) {
4810 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4811 goto done;
4813 TAILQ_FOREACH(pe, merged_paths, entry) {
4814 err = worktree_status(worktree, pe->path, fileindex,
4815 repo, collect_commitables, &cc_arg, NULL, NULL);
4816 if (err)
4817 goto done;
4819 } else {
4820 err = worktree_status(worktree, "", fileindex, repo,
4821 collect_commitables, &cc_arg, NULL, NULL);
4822 if (err)
4823 goto done;
4826 if (TAILQ_EMPTY(&commitable_paths)) {
4827 /* No-op change; commit will be elided. */
4828 err = got_ref_delete(commit_ref, repo);
4829 if (err)
4830 goto done;
4831 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4832 goto done;
4835 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4836 if (err)
4837 goto done;
4839 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4840 if (err)
4841 goto done;
4843 if (new_logmsg) {
4844 logmsg = strdup(new_logmsg);
4845 if (logmsg == NULL) {
4846 err = got_error_from_errno("strdup");
4847 goto done;
4849 } else {
4850 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4851 if (err)
4852 goto done;
4855 /* NB: commit_worktree will call free(logmsg) */
4856 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4857 worktree, got_object_commit_get_author(orig_commit),
4858 got_object_commit_get_committer(orig_commit),
4859 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4860 if (err)
4861 goto done;
4863 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4864 if (err)
4865 goto done;
4867 err = got_ref_delete(commit_ref, repo);
4868 if (err)
4869 goto done;
4871 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4872 fileindex, 0);
4873 sync_err = sync_fileindex(fileindex, fileindex_path);
4874 if (sync_err && err == NULL)
4875 err = sync_err;
4876 done:
4877 free(fileindex_path);
4878 free(head_commit_id);
4879 if (head_ref)
4880 got_ref_close(head_ref);
4881 if (err) {
4882 free(*new_commit_id);
4883 *new_commit_id = NULL;
4885 return err;
4888 const struct got_error *
4889 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4890 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4891 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4892 struct got_commit_object *orig_commit,
4893 struct got_object_id *orig_commit_id, struct got_repository *repo)
4895 const struct got_error *err;
4896 char *commit_ref_name;
4897 struct got_reference *commit_ref = NULL;
4898 struct got_object_id *commit_id = NULL;
4900 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4901 if (err)
4902 return err;
4904 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4905 if (err)
4906 goto done;
4907 err = got_ref_resolve(&commit_id, repo, commit_ref);
4908 if (err)
4909 goto done;
4910 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4911 err = got_error(GOT_ERR_REBASE_COMMITID);
4912 goto done;
4915 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4916 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4917 done:
4918 if (commit_ref)
4919 got_ref_close(commit_ref);
4920 free(commit_ref_name);
4921 free(commit_id);
4922 return err;
4925 const struct got_error *
4926 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4927 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4928 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4929 struct got_commit_object *orig_commit,
4930 struct got_object_id *orig_commit_id, const char *new_logmsg,
4931 struct got_repository *repo)
4933 const struct got_error *err;
4934 char *commit_ref_name;
4935 struct got_reference *commit_ref = NULL;
4936 struct got_object_id *commit_id = NULL;
4938 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4939 if (err)
4940 return err;
4942 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4943 if (err)
4944 goto done;
4945 err = got_ref_resolve(&commit_id, repo, commit_ref);
4946 if (err)
4947 goto done;
4948 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4949 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4950 goto done;
4953 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4954 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4955 done:
4956 if (commit_ref)
4957 got_ref_close(commit_ref);
4958 free(commit_ref_name);
4959 free(commit_id);
4960 return err;
4963 const struct got_error *
4964 got_worktree_rebase_postpone(struct got_worktree *worktree,
4965 struct got_fileindex *fileindex)
4967 if (fileindex)
4968 got_fileindex_free(fileindex);
4969 return lock_worktree(worktree, LOCK_SH);
4972 static const struct got_error *
4973 delete_ref(const char *name, struct got_repository *repo)
4975 const struct got_error *err;
4976 struct got_reference *ref;
4978 err = got_ref_open(&ref, repo, name, 0);
4979 if (err) {
4980 if (err->code == GOT_ERR_NOT_REF)
4981 return NULL;
4982 return err;
4985 err = got_ref_delete(ref, repo);
4986 got_ref_close(ref);
4987 return err;
4990 static const struct got_error *
4991 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4993 const struct got_error *err;
4994 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4995 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4997 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4998 if (err)
4999 goto done;
5000 err = delete_ref(tmp_branch_name, repo);
5001 if (err)
5002 goto done;
5004 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5005 if (err)
5006 goto done;
5007 err = delete_ref(new_base_branch_ref_name, repo);
5008 if (err)
5009 goto done;
5011 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5012 if (err)
5013 goto done;
5014 err = delete_ref(branch_ref_name, repo);
5015 if (err)
5016 goto done;
5018 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5019 if (err)
5020 goto done;
5021 err = delete_ref(commit_ref_name, repo);
5022 if (err)
5023 goto done;
5025 done:
5026 free(tmp_branch_name);
5027 free(new_base_branch_ref_name);
5028 free(branch_ref_name);
5029 free(commit_ref_name);
5030 return err;
5033 const struct got_error *
5034 got_worktree_rebase_complete(struct got_worktree *worktree,
5035 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5036 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5037 struct got_repository *repo)
5039 const struct got_error *err, *unlockerr;
5040 struct got_object_id *new_head_commit_id = NULL;
5042 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5043 if (err)
5044 return err;
5046 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5047 if (err)
5048 goto done;
5050 err = got_ref_write(rebased_branch, repo);
5051 if (err)
5052 goto done;
5054 err = got_worktree_set_head_ref(worktree, rebased_branch);
5055 if (err)
5056 goto done;
5058 err = delete_rebase_refs(worktree, repo);
5059 done:
5060 if (fileindex)
5061 got_fileindex_free(fileindex);
5062 free(new_head_commit_id);
5063 unlockerr = lock_worktree(worktree, LOCK_SH);
5064 if (unlockerr && err == NULL)
5065 err = unlockerr;
5066 return err;
5069 const struct got_error *
5070 got_worktree_rebase_abort(struct got_worktree *worktree,
5071 struct got_fileindex *fileindex, struct got_repository *repo,
5072 struct got_reference *new_base_branch,
5073 got_worktree_checkout_cb progress_cb, void *progress_arg)
5075 const struct got_error *err, *unlockerr, *sync_err;
5076 struct got_reference *resolved = NULL;
5077 struct got_object_id *commit_id = NULL;
5078 char *fileindex_path = NULL;
5079 struct revert_file_args rfa;
5080 struct got_object_id *tree_id = NULL;
5082 err = lock_worktree(worktree, LOCK_EX);
5083 if (err)
5084 return err;
5086 err = got_ref_open(&resolved, repo,
5087 got_ref_get_symref_target(new_base_branch), 0);
5088 if (err)
5089 goto done;
5091 err = got_worktree_set_head_ref(worktree, resolved);
5092 if (err)
5093 goto done;
5096 * XXX commits to the base branch could have happened while
5097 * we were busy rebasing; should we store the original commit ID
5098 * when rebase begins and read it back here?
5100 err = got_ref_resolve(&commit_id, repo, resolved);
5101 if (err)
5102 goto done;
5104 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5105 if (err)
5106 goto done;
5108 err = got_object_id_by_path(&tree_id, repo,
5109 worktree->base_commit_id, worktree->path_prefix);
5110 if (err)
5111 goto done;
5113 err = delete_rebase_refs(worktree, repo);
5114 if (err)
5115 goto done;
5117 err = get_fileindex_path(&fileindex_path, worktree);
5118 if (err)
5119 goto done;
5121 rfa.worktree = worktree;
5122 rfa.fileindex = fileindex;
5123 rfa.progress_cb = progress_cb;
5124 rfa.progress_arg = progress_arg;
5125 rfa.patch_cb = NULL;
5126 rfa.patch_arg = NULL;
5127 rfa.repo = repo;
5128 err = worktree_status(worktree, "", fileindex, repo,
5129 revert_file, &rfa, NULL, NULL);
5130 if (err)
5131 goto sync;
5133 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5134 repo, progress_cb, progress_arg, NULL, NULL);
5135 sync:
5136 sync_err = sync_fileindex(fileindex, fileindex_path);
5137 if (sync_err && err == NULL)
5138 err = sync_err;
5139 done:
5140 got_ref_close(resolved);
5141 free(tree_id);
5142 free(commit_id);
5143 if (fileindex)
5144 got_fileindex_free(fileindex);
5145 free(fileindex_path);
5147 unlockerr = lock_worktree(worktree, LOCK_SH);
5148 if (unlockerr && err == NULL)
5149 err = unlockerr;
5150 return err;
5153 const struct got_error *
5154 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5155 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5156 struct got_fileindex **fileindex, struct got_worktree *worktree,
5157 struct got_repository *repo)
5159 const struct got_error *err = NULL;
5160 char *tmp_branch_name = NULL;
5161 char *branch_ref_name = NULL;
5162 char *base_commit_ref_name = NULL;
5163 char *fileindex_path = NULL;
5164 struct check_rebase_ok_arg ok_arg;
5165 struct got_reference *wt_branch = NULL;
5166 struct got_reference *base_commit_ref = NULL;
5168 *tmp_branch = NULL;
5169 *branch_ref = NULL;
5170 *base_commit_id = NULL;
5171 *fileindex = NULL;
5173 err = lock_worktree(worktree, LOCK_EX);
5174 if (err)
5175 return err;
5177 err = open_fileindex(fileindex, &fileindex_path, worktree);
5178 if (err)
5179 goto done;
5181 ok_arg.worktree = worktree;
5182 ok_arg.repo = repo;
5183 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5184 &ok_arg);
5185 if (err)
5186 goto done;
5188 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5189 if (err)
5190 goto done;
5192 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5193 if (err)
5194 goto done;
5196 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5197 worktree);
5198 if (err)
5199 goto done;
5201 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5202 0);
5203 if (err)
5204 goto done;
5206 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5207 if (err)
5208 goto done;
5210 err = got_ref_write(*branch_ref, repo);
5211 if (err)
5212 goto done;
5214 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5215 worktree->base_commit_id);
5216 if (err)
5217 goto done;
5218 err = got_ref_write(base_commit_ref, repo);
5219 if (err)
5220 goto done;
5221 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5222 if (*base_commit_id == NULL) {
5223 err = got_error_from_errno("got_object_id_dup");
5224 goto done;
5227 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5228 worktree->base_commit_id);
5229 if (err)
5230 goto done;
5231 err = got_ref_write(*tmp_branch, repo);
5232 if (err)
5233 goto done;
5235 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5236 if (err)
5237 goto done;
5238 done:
5239 free(fileindex_path);
5240 free(tmp_branch_name);
5241 free(branch_ref_name);
5242 free(base_commit_ref_name);
5243 if (wt_branch)
5244 got_ref_close(wt_branch);
5245 if (err) {
5246 if (*branch_ref) {
5247 got_ref_close(*branch_ref);
5248 *branch_ref = NULL;
5250 if (*tmp_branch) {
5251 got_ref_close(*tmp_branch);
5252 *tmp_branch = NULL;
5254 free(*base_commit_id);
5255 if (*fileindex) {
5256 got_fileindex_free(*fileindex);
5257 *fileindex = NULL;
5259 lock_worktree(worktree, LOCK_SH);
5261 return err;
5264 const struct got_error *
5265 got_worktree_histedit_postpone(struct got_worktree *worktree,
5266 struct got_fileindex *fileindex)
5268 if (fileindex)
5269 got_fileindex_free(fileindex);
5270 return lock_worktree(worktree, LOCK_SH);
5273 const struct got_error *
5274 got_worktree_histedit_in_progress(int *in_progress,
5275 struct got_worktree *worktree)
5277 const struct got_error *err;
5278 char *tmp_branch_name = NULL;
5280 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5281 if (err)
5282 return err;
5284 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5285 free(tmp_branch_name);
5286 return NULL;
5289 const struct got_error *
5290 got_worktree_histedit_continue(struct got_object_id **commit_id,
5291 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5292 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5293 struct got_worktree *worktree, struct got_repository *repo)
5295 const struct got_error *err;
5296 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5297 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5298 struct got_reference *commit_ref = NULL;
5299 struct got_reference *base_commit_ref = NULL;
5300 char *fileindex_path = NULL;
5301 int have_staged_files = 0;
5303 *commit_id = NULL;
5304 *tmp_branch = NULL;
5305 *base_commit_id = NULL;
5306 *fileindex = NULL;
5308 err = lock_worktree(worktree, LOCK_EX);
5309 if (err)
5310 return err;
5312 err = open_fileindex(fileindex, &fileindex_path, worktree);
5313 if (err)
5314 goto done;
5316 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5317 &have_staged_files);
5318 if (err && err->code != GOT_ERR_CANCELLED)
5319 goto done;
5320 if (have_staged_files) {
5321 err = got_error(GOT_ERR_STAGED_PATHS);
5322 goto done;
5325 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5326 if (err)
5327 goto done;
5329 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5330 if (err)
5331 goto done;
5333 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5334 if (err)
5335 goto done;
5337 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5338 worktree);
5339 if (err)
5340 goto done;
5342 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5343 if (err)
5344 goto done;
5346 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5347 if (err)
5348 goto done;
5349 err = got_ref_resolve(commit_id, repo, commit_ref);
5350 if (err)
5351 goto done;
5353 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5354 if (err)
5355 goto done;
5356 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5357 if (err)
5358 goto done;
5360 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5361 if (err)
5362 goto done;
5363 done:
5364 free(commit_ref_name);
5365 free(branch_ref_name);
5366 free(fileindex_path);
5367 if (commit_ref)
5368 got_ref_close(commit_ref);
5369 if (base_commit_ref)
5370 got_ref_close(base_commit_ref);
5371 if (err) {
5372 free(*commit_id);
5373 *commit_id = NULL;
5374 free(*base_commit_id);
5375 *base_commit_id = NULL;
5376 if (*tmp_branch) {
5377 got_ref_close(*tmp_branch);
5378 *tmp_branch = NULL;
5380 if (*fileindex) {
5381 got_fileindex_free(*fileindex);
5382 *fileindex = NULL;
5384 lock_worktree(worktree, LOCK_EX);
5386 return err;
5389 static const struct got_error *
5390 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5392 const struct got_error *err;
5393 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5394 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5396 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5397 if (err)
5398 goto done;
5399 err = delete_ref(tmp_branch_name, repo);
5400 if (err)
5401 goto done;
5403 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5404 worktree);
5405 if (err)
5406 goto done;
5407 err = delete_ref(base_commit_ref_name, repo);
5408 if (err)
5409 goto done;
5411 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5412 if (err)
5413 goto done;
5414 err = delete_ref(branch_ref_name, repo);
5415 if (err)
5416 goto done;
5418 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5419 if (err)
5420 goto done;
5421 err = delete_ref(commit_ref_name, repo);
5422 if (err)
5423 goto done;
5424 done:
5425 free(tmp_branch_name);
5426 free(base_commit_ref_name);
5427 free(branch_ref_name);
5428 free(commit_ref_name);
5429 return err;
5432 const struct got_error *
5433 got_worktree_histedit_abort(struct got_worktree *worktree,
5434 struct got_fileindex *fileindex, struct got_repository *repo,
5435 struct got_reference *branch, struct got_object_id *base_commit_id,
5436 got_worktree_checkout_cb progress_cb, void *progress_arg)
5438 const struct got_error *err, *unlockerr, *sync_err;
5439 struct got_reference *resolved = NULL;
5440 char *fileindex_path = NULL;
5441 struct got_object_id *tree_id = NULL;
5442 struct revert_file_args rfa;
5444 err = lock_worktree(worktree, LOCK_EX);
5445 if (err)
5446 return err;
5448 err = got_ref_open(&resolved, repo,
5449 got_ref_get_symref_target(branch), 0);
5450 if (err)
5451 goto done;
5453 err = got_worktree_set_head_ref(worktree, resolved);
5454 if (err)
5455 goto done;
5457 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5458 if (err)
5459 goto done;
5461 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5462 worktree->path_prefix);
5463 if (err)
5464 goto done;
5466 err = delete_histedit_refs(worktree, repo);
5467 if (err)
5468 goto done;
5470 err = get_fileindex_path(&fileindex_path, worktree);
5471 if (err)
5472 goto done;
5474 rfa.worktree = worktree;
5475 rfa.fileindex = fileindex;
5476 rfa.progress_cb = progress_cb;
5477 rfa.progress_arg = progress_arg;
5478 rfa.patch_cb = NULL;
5479 rfa.patch_arg = NULL;
5480 rfa.repo = repo;
5481 err = worktree_status(worktree, "", fileindex, repo,
5482 revert_file, &rfa, NULL, NULL);
5483 if (err)
5484 goto sync;
5486 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5487 repo, progress_cb, progress_arg, NULL, NULL);
5488 sync:
5489 sync_err = sync_fileindex(fileindex, fileindex_path);
5490 if (sync_err && err == NULL)
5491 err = sync_err;
5492 done:
5493 got_ref_close(resolved);
5494 free(tree_id);
5495 free(fileindex_path);
5497 unlockerr = lock_worktree(worktree, LOCK_SH);
5498 if (unlockerr && err == NULL)
5499 err = unlockerr;
5500 return err;
5503 const struct got_error *
5504 got_worktree_histedit_complete(struct got_worktree *worktree,
5505 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5506 struct got_reference *edited_branch, struct got_repository *repo)
5508 const struct got_error *err, *unlockerr;
5509 struct got_object_id *new_head_commit_id = NULL;
5510 struct got_reference *resolved = NULL;
5512 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5513 if (err)
5514 return err;
5516 err = got_ref_open(&resolved, repo,
5517 got_ref_get_symref_target(edited_branch), 0);
5518 if (err)
5519 goto done;
5521 err = got_ref_change_ref(resolved, new_head_commit_id);
5522 if (err)
5523 goto done;
5525 err = got_ref_write(resolved, repo);
5526 if (err)
5527 goto done;
5529 err = got_worktree_set_head_ref(worktree, resolved);
5530 if (err)
5531 goto done;
5533 err = delete_histedit_refs(worktree, repo);
5534 done:
5535 if (fileindex)
5536 got_fileindex_free(fileindex);
5537 free(new_head_commit_id);
5538 unlockerr = lock_worktree(worktree, LOCK_SH);
5539 if (unlockerr && err == NULL)
5540 err = unlockerr;
5541 return err;
5544 const struct got_error *
5545 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5546 struct got_object_id *commit_id, struct got_repository *repo)
5548 const struct got_error *err;
5549 char *commit_ref_name;
5551 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5552 if (err)
5553 return err;
5555 err = store_commit_id(commit_ref_name, commit_id, repo);
5556 if (err)
5557 goto done;
5559 err = delete_ref(commit_ref_name, repo);
5560 done:
5561 free(commit_ref_name);
5562 return err;
5565 struct check_stage_ok_arg {
5566 struct got_object_id *head_commit_id;
5567 struct got_worktree *worktree;
5568 struct got_fileindex *fileindex;
5569 struct got_repository *repo;
5570 int have_changes;
5573 const struct got_error *
5574 check_stage_ok(void *arg, unsigned char status,
5575 unsigned char staged_status, const char *relpath,
5576 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5577 struct got_object_id *commit_id)
5579 struct check_stage_ok_arg *a = arg;
5580 const struct got_error *err = NULL;
5581 struct got_fileindex_entry *ie;
5582 struct got_object_id base_commit_id;
5583 struct got_object_id *base_commit_idp = NULL;
5584 char *in_repo_path = NULL, *p;
5586 if (status == GOT_STATUS_UNVERSIONED)
5587 return NULL;
5588 if (status == GOT_STATUS_NONEXISTENT)
5589 return got_error_set_errno(ENOENT, relpath);
5591 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5592 if (ie == NULL)
5593 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5595 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5596 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5597 relpath) == -1)
5598 return got_error_from_errno("asprintf");
5600 if (got_fileindex_entry_has_commit(ie)) {
5601 memcpy(base_commit_id.sha1, ie->commit_sha1,
5602 SHA1_DIGEST_LENGTH);
5603 base_commit_idp = &base_commit_id;
5606 if (status == GOT_STATUS_NO_CHANGE) {
5607 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5608 goto done;
5609 } else if (status == GOT_STATUS_CONFLICT) {
5610 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5611 goto done;
5612 } else if (status != GOT_STATUS_ADD &&
5613 status != GOT_STATUS_MODIFY &&
5614 status != GOT_STATUS_DELETE) {
5615 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5616 goto done;
5619 a->have_changes = 1;
5621 p = in_repo_path;
5622 while (p[0] == '/')
5623 p++;
5624 err = check_out_of_date(p, status, staged_status,
5625 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5626 GOT_ERR_STAGE_OUT_OF_DATE);
5627 done:
5628 free(in_repo_path);
5629 return err;
5632 struct stage_path_arg {
5633 struct got_worktree *worktree;
5634 struct got_fileindex *fileindex;
5635 struct got_repository *repo;
5636 got_worktree_status_cb status_cb;
5637 void *status_arg;
5638 got_worktree_patch_cb patch_cb;
5639 void *patch_arg;
5642 static const struct got_error *
5643 stage_path(void *arg, unsigned char status,
5644 unsigned char staged_status, const char *relpath,
5645 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5646 struct got_object_id *commit_id)
5648 struct stage_path_arg *a = arg;
5649 const struct got_error *err = NULL;
5650 struct got_fileindex_entry *ie;
5651 char *ondisk_path = NULL, *path_content = NULL;
5652 uint32_t stage;
5653 struct got_object_id *new_staged_blob_id = NULL;
5655 if (status == GOT_STATUS_UNVERSIONED)
5656 return NULL;
5658 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5659 if (ie == NULL)
5660 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5662 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5663 relpath)== -1)
5664 return got_error_from_errno("asprintf");
5666 switch (status) {
5667 case GOT_STATUS_ADD:
5668 case GOT_STATUS_MODIFY:
5669 if (a->patch_cb) {
5670 if (status == GOT_STATUS_ADD) {
5671 int choice = GOT_PATCH_CHOICE_NONE;
5672 err = (*a->patch_cb)(&choice, a->patch_arg,
5673 status, ie->path, NULL, 1, 1);
5674 if (err)
5675 break;
5676 if (choice != GOT_PATCH_CHOICE_YES)
5677 break;
5678 } else {
5679 err = create_patched_content(&path_content, 0,
5680 staged_blob_id ? staged_blob_id : blob_id,
5681 ondisk_path, ie->path, a->repo,
5682 a->patch_cb, a->patch_arg);
5683 if (err || path_content == NULL)
5684 break;
5687 err = got_object_blob_create(&new_staged_blob_id,
5688 path_content ? path_content : ondisk_path, a->repo);
5689 if (err)
5690 break;
5691 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5692 SHA1_DIGEST_LENGTH);
5693 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5694 stage = GOT_FILEIDX_STAGE_ADD;
5695 else
5696 stage = GOT_FILEIDX_STAGE_MODIFY;
5697 got_fileindex_entry_stage_set(ie, stage);
5698 if (a->status_cb == NULL)
5699 break;
5700 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5701 get_staged_status(ie), relpath, blob_id,
5702 new_staged_blob_id, NULL);
5703 break;
5704 case GOT_STATUS_DELETE:
5705 if (staged_status == GOT_STATUS_DELETE)
5706 break;
5707 if (a->patch_cb) {
5708 int choice = GOT_PATCH_CHOICE_NONE;
5709 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5710 ie->path, NULL, 1, 1);
5711 if (err)
5712 break;
5713 if (choice == GOT_PATCH_CHOICE_NO)
5714 break;
5715 if (choice != GOT_PATCH_CHOICE_YES) {
5716 err = got_error(GOT_ERR_PATCH_CHOICE);
5717 break;
5720 stage = GOT_FILEIDX_STAGE_DELETE;
5721 got_fileindex_entry_stage_set(ie, stage);
5722 if (a->status_cb == NULL)
5723 break;
5724 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5725 get_staged_status(ie), relpath, NULL, NULL, NULL);
5726 break;
5727 case GOT_STATUS_NO_CHANGE:
5728 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5729 break;
5730 case GOT_STATUS_CONFLICT:
5731 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5732 break;
5733 case GOT_STATUS_NONEXISTENT:
5734 err = got_error_set_errno(ENOENT, relpath);
5735 break;
5736 default:
5737 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5738 break;
5741 if (path_content && unlink(path_content) == -1 && err == NULL)
5742 err = got_error_from_errno2("unlink", path_content);
5743 free(path_content);
5744 free(ondisk_path);
5745 free(new_staged_blob_id);
5746 return err;
5749 const struct got_error *
5750 got_worktree_stage(struct got_worktree *worktree,
5751 struct got_pathlist_head *paths,
5752 got_worktree_status_cb status_cb, void *status_arg,
5753 got_worktree_patch_cb patch_cb, void *patch_arg,
5754 struct got_repository *repo)
5756 const struct got_error *err = NULL, *sync_err, *unlockerr;
5757 struct got_pathlist_entry *pe;
5758 struct got_fileindex *fileindex = NULL;
5759 char *fileindex_path = NULL;
5760 struct got_reference *head_ref = NULL;
5761 struct got_object_id *head_commit_id = NULL;
5762 struct check_stage_ok_arg oka;
5763 struct stage_path_arg spa;
5765 err = lock_worktree(worktree, LOCK_EX);
5766 if (err)
5767 return err;
5769 err = got_ref_open(&head_ref, repo,
5770 got_worktree_get_head_ref_name(worktree), 0);
5771 if (err)
5772 goto done;
5773 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5774 if (err)
5775 goto done;
5776 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5777 if (err)
5778 goto done;
5780 /* Check pre-conditions before staging anything. */
5781 oka.head_commit_id = head_commit_id;
5782 oka.worktree = worktree;
5783 oka.fileindex = fileindex;
5784 oka.repo = repo;
5785 oka.have_changes = 0;
5786 TAILQ_FOREACH(pe, paths, entry) {
5787 err = worktree_status(worktree, pe->path, fileindex, repo,
5788 check_stage_ok, &oka, NULL, NULL);
5789 if (err)
5790 goto done;
5792 if (!oka.have_changes) {
5793 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5794 goto done;
5797 spa.worktree = worktree;
5798 spa.fileindex = fileindex;
5799 spa.repo = repo;
5800 spa.patch_cb = patch_cb;
5801 spa.patch_arg = patch_arg;
5802 spa.status_cb = status_cb;
5803 spa.status_arg = status_arg;
5804 TAILQ_FOREACH(pe, paths, entry) {
5805 err = worktree_status(worktree, pe->path, fileindex, repo,
5806 stage_path, &spa, NULL, NULL);
5807 if (err)
5808 goto done;
5811 sync_err = sync_fileindex(fileindex, fileindex_path);
5812 if (sync_err && err == NULL)
5813 err = sync_err;
5814 done:
5815 if (head_ref)
5816 got_ref_close(head_ref);
5817 free(head_commit_id);
5818 free(fileindex_path);
5819 if (fileindex)
5820 got_fileindex_free(fileindex);
5821 unlockerr = lock_worktree(worktree, LOCK_SH);
5822 if (unlockerr && err == NULL)
5823 err = unlockerr;
5824 return err;
5827 struct unstage_path_arg {
5828 struct got_worktree *worktree;
5829 struct got_fileindex *fileindex;
5830 struct got_repository *repo;
5831 got_worktree_checkout_cb progress_cb;
5832 void *progress_arg;
5833 got_worktree_patch_cb patch_cb;
5834 void *patch_arg;
5837 static const struct got_error *
5838 create_unstaged_content(char **path_unstaged_content,
5839 char **path_new_staged_content, struct got_object_id *blob_id,
5840 struct got_object_id *staged_blob_id, const char *relpath,
5841 struct got_repository *repo,
5842 got_worktree_patch_cb patch_cb, void *patch_arg)
5844 const struct got_error *err;
5845 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5846 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5847 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5848 struct stat sb1, sb2;
5849 struct got_diff_changes *changes = NULL;
5850 struct got_diff_state *ds = NULL;
5851 struct got_diff_args *args = NULL;
5852 struct got_diff_change *change;
5853 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5854 int have_content = 0, have_rejected_content = 0;
5856 *path_unstaged_content = NULL;
5857 *path_new_staged_content = NULL;
5859 err = got_object_id_str(&label1, blob_id);
5860 if (err)
5861 return err;
5862 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5863 if (err)
5864 goto done;
5866 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5867 if (err)
5868 goto done;
5870 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5871 if (err)
5872 goto done;
5874 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5875 if (err)
5876 goto done;
5878 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5879 if (err)
5880 goto done;
5882 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5883 if (err)
5884 goto done;
5886 if (stat(path1, &sb1) == -1) {
5887 err = got_error_from_errno2("stat", path1);
5888 goto done;
5891 if (stat(path2, &sb2) == -1) {
5892 err = got_error_from_errno2("stat", path2);
5893 goto done;
5896 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5897 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5898 if (err)
5899 goto done;
5901 err = got_opentemp_named(path_unstaged_content, &outfile,
5902 "got-unstaged-content");
5903 if (err)
5904 goto done;
5905 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5906 "got-new-staged-content");
5907 if (err)
5908 goto done;
5910 if (fseek(f1, 0L, SEEK_SET) == -1) {
5911 err = got_ferror(f1, GOT_ERR_IO);
5912 goto done;
5914 if (fseek(f2, 0L, SEEK_SET) == -1) {
5915 err = got_ferror(f2, GOT_ERR_IO);
5916 goto done;
5918 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5919 int choice;
5920 err = apply_or_reject_change(&choice, change, ++n,
5921 changes->nchanges, ds, args, diff_flags, relpath,
5922 f1, f2, &line_cur1, &line_cur2,
5923 outfile, rejectfile, patch_cb, patch_arg);
5924 if (err)
5925 goto done;
5926 if (choice == GOT_PATCH_CHOICE_YES)
5927 have_content = 1;
5928 else
5929 have_rejected_content = 1;
5930 if (choice == GOT_PATCH_CHOICE_QUIT)
5931 break;
5933 if (have_content || have_rejected_content)
5934 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5935 outfile, rejectfile);
5936 done:
5937 free(label1);
5938 if (blob)
5939 got_object_blob_close(blob);
5940 if (staged_blob)
5941 got_object_blob_close(staged_blob);
5942 if (f1 && fclose(f1) == EOF && err == NULL)
5943 err = got_error_from_errno2("fclose", path1);
5944 if (f2 && fclose(f2) == EOF && err == NULL)
5945 err = got_error_from_errno2("fclose", path2);
5946 if (outfile && fclose(outfile) == EOF && err == NULL)
5947 err = got_error_from_errno2("fclose", *path_unstaged_content);
5948 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5949 err = got_error_from_errno2("fclose", *path_new_staged_content);
5950 if (path1 && unlink(path1) == -1 && err == NULL)
5951 err = got_error_from_errno2("unlink", path1);
5952 if (path2 && unlink(path2) == -1 && err == NULL)
5953 err = got_error_from_errno2("unlink", path2);
5954 if (err || !have_content) {
5955 if (*path_unstaged_content &&
5956 unlink(*path_unstaged_content) == -1 && err == NULL)
5957 err = got_error_from_errno2("unlink",
5958 *path_unstaged_content);
5959 free(*path_unstaged_content);
5960 *path_unstaged_content = NULL;
5962 if (err || !have_rejected_content) {
5963 if (*path_new_staged_content &&
5964 unlink(*path_new_staged_content) == -1 && err == NULL)
5965 err = got_error_from_errno2("unlink",
5966 *path_new_staged_content);
5967 free(*path_new_staged_content);
5968 *path_new_staged_content = NULL;
5970 free(args);
5971 if (ds) {
5972 got_diff_state_free(ds);
5973 free(ds);
5975 if (changes)
5976 got_diff_free_changes(changes);
5977 free(path1);
5978 free(path2);
5979 return err;
5982 static const struct got_error *
5983 unstage_path(void *arg, unsigned char status,
5984 unsigned char staged_status, const char *relpath,
5985 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5986 struct got_object_id *commit_id)
5988 const struct got_error *err = NULL;
5989 struct unstage_path_arg *a = arg;
5990 struct got_fileindex_entry *ie;
5991 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5992 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5993 char *path_new_staged_content = NULL;
5994 int local_changes_subsumed;
5995 struct stat sb;
5997 if (staged_status != GOT_STATUS_ADD &&
5998 staged_status != GOT_STATUS_MODIFY &&
5999 staged_status != GOT_STATUS_DELETE)
6000 return NULL;
6002 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6003 if (ie == NULL)
6004 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6006 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6007 == -1)
6008 return got_error_from_errno("asprintf");
6010 switch (staged_status) {
6011 case GOT_STATUS_MODIFY:
6012 err = got_object_open_as_blob(&blob_base, a->repo,
6013 blob_id, 8192);
6014 if (err)
6015 break;
6016 /* fall through */
6017 case GOT_STATUS_ADD:
6018 if (a->patch_cb) {
6019 if (staged_status == GOT_STATUS_ADD) {
6020 int choice = GOT_PATCH_CHOICE_NONE;
6021 err = (*a->patch_cb)(&choice, a->patch_arg,
6022 staged_status, ie->path, NULL, 1, 1);
6023 if (err)
6024 break;
6025 if (choice != GOT_PATCH_CHOICE_YES)
6026 break;
6027 } else {
6028 err = create_unstaged_content(
6029 &path_unstaged_content,
6030 &path_new_staged_content, blob_id,
6031 staged_blob_id, ie->path, a->repo,
6032 a->patch_cb, a->patch_arg);
6033 if (err || path_unstaged_content == NULL)
6034 break;
6035 if (path_new_staged_content) {
6036 err = got_object_blob_create(
6037 &staged_blob_id,
6038 path_new_staged_content,
6039 a->repo);
6040 if (err)
6041 break;
6042 memcpy(ie->staged_blob_sha1,
6043 staged_blob_id->sha1,
6044 SHA1_DIGEST_LENGTH);
6046 err = merge_file(&local_changes_subsumed,
6047 a->worktree, blob_base, ondisk_path,
6048 relpath, got_fileindex_perms_to_st(ie),
6049 path_unstaged_content, "unstaged",
6050 a->repo, a->progress_cb, a->progress_arg);
6051 if (err == NULL &&
6052 path_new_staged_content == NULL)
6053 got_fileindex_entry_stage_set(ie,
6054 GOT_FILEIDX_STAGE_NONE);
6055 break; /* Done with this file. */
6058 err = got_object_open_as_blob(&blob_staged, a->repo,
6059 staged_blob_id, 8192);
6060 if (err)
6061 break;
6062 err = merge_blob(&local_changes_subsumed, a->worktree,
6063 blob_base, ondisk_path, relpath,
6064 got_fileindex_perms_to_st(ie), blob_staged,
6065 commit_id ? commit_id : a->worktree->base_commit_id,
6066 a->repo, a->progress_cb, a->progress_arg);
6067 if (err == NULL)
6068 got_fileindex_entry_stage_set(ie,
6069 GOT_FILEIDX_STAGE_NONE);
6070 break;
6071 case GOT_STATUS_DELETE:
6072 if (a->patch_cb) {
6073 int choice = GOT_PATCH_CHOICE_NONE;
6074 err = (*a->patch_cb)(&choice, a->patch_arg,
6075 staged_status, ie->path, NULL, 1, 1);
6076 if (err)
6077 break;
6078 if (choice == GOT_PATCH_CHOICE_NO)
6079 break;
6080 if (choice != GOT_PATCH_CHOICE_YES) {
6081 err = got_error(GOT_ERR_PATCH_CHOICE);
6082 break;
6085 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6086 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6087 if (err)
6088 break;
6089 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6090 break;
6093 free(ondisk_path);
6094 if (path_unstaged_content &&
6095 unlink(path_unstaged_content) == -1 && err == NULL)
6096 err = got_error_from_errno2("unlink", path_unstaged_content);
6097 if (path_new_staged_content &&
6098 unlink(path_new_staged_content) == -1 && err == NULL)
6099 err = got_error_from_errno2("unlink", path_new_staged_content);
6100 free(path_unstaged_content);
6101 free(path_new_staged_content);
6102 if (blob_base)
6103 got_object_blob_close(blob_base);
6104 if (blob_staged)
6105 got_object_blob_close(blob_staged);
6106 return err;
6109 const struct got_error *
6110 got_worktree_unstage(struct got_worktree *worktree,
6111 struct got_pathlist_head *paths,
6112 got_worktree_checkout_cb progress_cb, void *progress_arg,
6113 got_worktree_patch_cb patch_cb, void *patch_arg,
6114 struct got_repository *repo)
6116 const struct got_error *err = NULL, *sync_err, *unlockerr;
6117 struct got_pathlist_entry *pe;
6118 struct got_fileindex *fileindex = NULL;
6119 char *fileindex_path = NULL;
6120 struct unstage_path_arg upa;
6122 err = lock_worktree(worktree, LOCK_EX);
6123 if (err)
6124 return err;
6126 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6127 if (err)
6128 goto done;
6130 upa.worktree = worktree;
6131 upa.fileindex = fileindex;
6132 upa.repo = repo;
6133 upa.progress_cb = progress_cb;
6134 upa.progress_arg = progress_arg;
6135 upa.patch_cb = patch_cb;
6136 upa.patch_arg = patch_arg;
6137 TAILQ_FOREACH(pe, paths, entry) {
6138 err = worktree_status(worktree, pe->path, fileindex, repo,
6139 unstage_path, &upa, NULL, NULL);
6140 if (err)
6141 goto done;
6144 sync_err = sync_fileindex(fileindex, fileindex_path);
6145 if (sync_err && err == NULL)
6146 err = sync_err;
6147 done:
6148 free(fileindex_path);
6149 if (fileindex)
6150 got_fileindex_free(fileindex);
6151 unlockerr = lock_worktree(worktree, LOCK_SH);
6152 if (unlockerr && err == NULL)
6153 err = unlockerr;
6154 return err;