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/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.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_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
63 {
64 const struct got_error *err = NULL;
65 char *path;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
71 free(path);
72 return err;
73 }
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 FILE *tmpfile = NULL;
80 char *tmppath = NULL;
81 char *path = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
85 path = NULL;
86 goto done;
87 }
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
90 if (err)
91 goto done;
93 if (content) {
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
97 goto done;
98 }
99 }
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
103 unlink(tmppath);
104 goto done;
107 done:
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
110 free(tmppath);
111 return err;
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
118 char *path;
119 int fd = -1;
120 ssize_t n;
121 struct stat sb;
123 *content = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
127 path = NULL;
128 goto done;
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
132 if (fd == -1) {
133 if (errno == ENOENT)
134 err = got_error_path(path, GOT_ERR_WORKTREE_META);
135 else
136 err = got_error_from_errno2("open", path);
137 goto done;
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
142 goto done;
145 if (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", path);
147 goto done;
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
152 goto done;
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error_path(path, GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, GOT_ERR_WORKTREE_META);
163 goto done;
165 (*content)[sb.st_size - 1] = '\0';
167 done:
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
170 free(path);
171 if (err) {
172 free(*content);
173 *content = NULL;
175 return err;
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
182 char *refstr = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL)
187 return got_error_from_errno("got_ref_to_str");
188 } else {
189 refstr = strdup(got_ref_get_name(head_ref));
190 if (refstr == NULL)
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 free(refstr);
195 return err;
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
204 uuid_t uuid;
205 uint32_t uuid_status;
206 int obj_type;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
215 goto done;
218 err = got_ref_resolve(&commit_id, repo, head_ref);
219 if (err)
220 return err;
221 err = got_object_get_type(&obj_type, repo, commit_id);
222 if (err)
223 return err;
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
235 goto done;
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
245 goto done;
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 if (err)
251 goto done;
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 if (err)
256 goto done;
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
260 if (err)
261 goto done;
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
265 if (err)
266 goto done;
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 if (err)
269 goto done;
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
274 if (err)
275 goto done;
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
280 if (err)
281 goto done;
283 /* Generate UUID. */
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
287 goto done;
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
292 goto done;
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 if (err)
296 goto done;
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
301 goto done;
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 if (err)
305 goto done;
307 done:
308 free(commit_id);
309 free(path_got);
310 free(formatstr);
311 free(absprefix);
312 free(basestr);
313 free(uuidstr);
314 return err;
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
321 char *path_got;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
327 const char *errstr;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
331 *worktree = NULL;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_lock = NULL;
342 goto done;
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 if (fd == -1) {
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
349 goto done;
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 if (err)
354 goto done;
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 if (errstr) {
358 err = got_error_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
360 goto done;
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
364 goto done;
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
370 goto done;
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
377 goto done;
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
381 if (err)
382 goto done;
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
386 if (err)
387 goto done;
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
391 if (err)
392 goto done;
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 if (err)
396 goto done;
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
400 goto done;
403 err = got_repo_open(&repo, (*worktree)->repo_path);
404 if (err)
405 goto done;
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 base_commit_id_str);
409 if (err)
410 goto done;
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
414 done:
415 if (repo)
416 got_repo_close(repo);
417 free(path_got);
418 free(path_lock);
419 free(base_commit_id_str);
420 free(uuidstr);
421 free(formatstr);
422 if (err) {
423 if (fd != -1)
424 close(fd);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
427 *worktree = NULL;
428 } else
429 (*worktree)->lockfd = fd;
431 return err;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
439 do {
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 return err;
443 if (*worktree)
444 return NULL;
445 path = dirname(path);
446 if (path == NULL)
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
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);
467 return err;
470 const char *
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
476 const char *
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
482 const char *
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
500 free(absprefix);
501 return NULL;
504 const char *
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
520 path_got = NULL;
521 goto done;
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
527 goto done;
530 err = write_head_ref(path_got, head_ref);
531 if (err)
532 goto done;
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
536 done:
537 free(path_got);
538 if (err)
539 free(head_ref_name);
540 return err;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
555 char *id_str = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
561 path_got = NULL;
562 goto done;
565 err = got_object_open(&obj, repo, commit_id);
566 if (err)
567 return err;
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
571 goto done;
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
576 if (err)
577 goto done;
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 if (err)
580 goto done;
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
586 goto done;
588 done:
589 if (obj)
590 got_object_close(obj);
591 free(id_str);
592 free(path_got);
593 return err;
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
603 return NULL;
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
610 char *abspath;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 struct stat sb;
618 err = NULL;
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
626 free(abspath);
627 return err;
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
634 uint8_t fbuf1[8192];
635 uint8_t fbuf2[8192];
636 size_t flen1 = 0, flen2 = 0;
638 *same = 1;
640 for (;;) {
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
644 break;
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
649 break;
651 if (flen1 == 0) {
652 if (flen2 != 0)
653 *same = 0;
654 break;
655 } else if (flen2 == 0) {
656 if (flen1 != 0)
657 *same = 0;
658 break;
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 *same = 0;
662 break;
664 } else {
665 *same = 0;
666 break;
670 return err;
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
677 struct stat sb;
678 size_t size1, size2;
679 FILE *f1 = NULL, *f2 = NULL;
681 *same = 1;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
685 goto done;
687 size1 = sb.st_size;
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
691 goto done;
693 size2 = sb.st_size;
695 if (size1 != size2) {
696 *same = 0;
697 return NULL;
700 f1 = fopen(f1_path, "r");
701 if (f1 == NULL)
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
705 if (f2 == NULL) {
706 err = got_error_from_errno2("open", f2_path);
707 goto done;
710 err = check_file_contents_equal(same, f1, f2);
711 done:
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
717 return err;
720 /*
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * the file at deriv_path acts as the first derived version, and the
723 * file on disk acts as the second derived version.
724 */
725 static const struct got_error *
726 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, const char *deriv_path,
729 const char *label_deriv, struct got_repository *repo,
730 got_worktree_checkout_cb progress_cb, void *progress_arg)
732 const struct got_error *err = NULL;
733 int merged_fd = -1;
734 FILE *f_orig = NULL;
735 char *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
737 int overlapcnt = 0;
738 char *parent;
740 *local_changes_subsumed = 0;
742 parent = dirname(ondisk_path);
743 if (parent == NULL)
744 return got_error_from_errno2("dirname", ondisk_path);
746 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
747 return got_error_from_errno("asprintf");
749 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
750 if (err)
751 goto done;
753 free(base_path);
754 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
755 err = got_error_from_errno("asprintf");
756 base_path = NULL;
757 goto done;
760 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
761 if (err)
762 goto done;
763 if (blob_orig) {
764 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
765 blob_orig);
766 if (err)
767 goto done;
768 } else {
769 /*
770 * If the file has no blob, this is an "add vs add" conflict,
771 * and we simply use an empty ancestor file to make both files
772 * appear in the merged result in their entirety.
773 */
776 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
777 blob_orig_path, ondisk_path, label_deriv, path);
778 if (err)
779 goto done;
781 err = (*progress_cb)(progress_arg,
782 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 if (err)
784 goto done;
786 if (fsync(merged_fd) != 0) {
787 err = got_error_from_errno("fsync");
788 goto done;
791 /* Check if a clean merge has subsumed all local changes. */
792 if (overlapcnt == 0) {
793 err = check_files_equal(local_changes_subsumed, deriv_path,
794 merged_path);
795 if (err)
796 goto done;
799 if (chmod(merged_path, st_mode) != 0) {
800 err = got_error_from_errno2("chmod", merged_path);
801 goto done;
804 if (rename(merged_path, ondisk_path) != 0) {
805 err = got_error_from_errno3("rename", merged_path,
806 ondisk_path);
807 unlink(merged_path);
808 goto done;
811 done:
812 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
813 err = got_error_from_errno("close");
814 if (f_orig && fclose(f_orig) != 0 && err == NULL)
815 err = got_error_from_errno("fclose");
816 free(merged_path);
817 free(base_path);
818 if (blob_orig_path) {
819 unlink(blob_orig_path);
820 free(blob_orig_path);
822 return err;
825 /*
826 * Perform a 3-way merge where blob_orig acts as the common ancestor,
827 * blob_deriv acts as the first derived version, and the file on disk
828 * acts as the second derived version.
829 */
830 static const struct got_error *
831 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
834 struct got_object_id *deriv_base_commit_id,
835 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
836 void *progress_arg)
838 const struct got_error *err = NULL;
839 FILE *f_deriv = NULL;
840 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
841 char *label_deriv = NULL, *parent;
843 *local_changes_subsumed = 0;
845 parent = dirname(ondisk_path);
846 if (parent == NULL)
847 return got_error_from_errno2("dirname", ondisk_path);
849 free(base_path);
850 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
851 err = got_error_from_errno("asprintf");
852 base_path = NULL;
853 goto done;
856 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
857 if (err)
858 goto done;
859 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
860 blob_deriv);
861 if (err)
862 goto done;
864 err = got_object_id_str(&id_str, deriv_base_commit_id);
865 if (err)
866 goto done;
867 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
868 err = got_error_from_errno("asprintf");
869 goto done;
872 err = merge_file(local_changes_subsumed, worktree, blob_orig,
873 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
874 repo, progress_cb, progress_arg);
875 done:
876 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
877 err = got_error_from_errno("fclose");
878 free(base_path);
879 if (blob_deriv_path) {
880 unlink(blob_deriv_path);
881 free(blob_deriv_path);
883 free(id_str);
884 free(label_deriv);
885 return err;
888 static const struct got_error *
889 update_blob_fileindex_entry(struct got_worktree *worktree,
890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
891 const char *ondisk_path, const char *path, struct got_blob_object *blob,
892 int update_timestamps)
894 const struct got_error *err = NULL;
896 if (ie == NULL)
897 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
898 if (ie)
899 err = got_fileindex_entry_update(ie, ondisk_path,
900 blob->id.sha1, worktree->base_commit_id->sha1,
901 update_timestamps);
902 else {
903 struct got_fileindex_entry *new_ie;
904 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
905 path, blob->id.sha1, worktree->base_commit_id->sha1);
906 if (!err)
907 err = got_fileindex_entry_add(fileindex, new_ie);
909 return err;
912 static const struct got_error *
913 install_blob(struct got_worktree *worktree, const char *ondisk_path,
914 const char *path, uint16_t te_mode, uint16_t st_mode,
915 struct got_blob_object *blob, int restoring_missing_file,
916 int reverting_versioned_file, struct got_repository *repo,
917 got_worktree_checkout_cb progress_cb, void *progress_arg)
919 const struct got_error *err = NULL;
920 int fd = -1;
921 size_t len, hdrlen;
922 int update = 0;
923 char *tmppath = NULL;
925 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
926 GOT_DEFAULT_FILE_MODE);
927 if (fd == -1) {
928 if (errno == ENOENT) {
929 char *parent = dirname(path);
930 if (parent == NULL)
931 return got_error_from_errno2("dirname", path);
932 err = add_dir_on_disk(worktree, parent);
933 if (err)
934 return err;
935 fd = open(ondisk_path,
936 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
937 GOT_DEFAULT_FILE_MODE);
938 if (fd == -1)
939 return got_error_from_errno2("open",
940 ondisk_path);
941 } else if (errno == EEXIST) {
942 if (!S_ISREG(st_mode)) {
943 /* TODO file is obstructed; do something */
944 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
945 goto done;
946 } else {
947 err = got_opentemp_named_fd(&tmppath, &fd,
948 ondisk_path);
949 if (err)
950 goto done;
951 update = 1;
953 } else
954 return got_error_from_errno2("open", ondisk_path);
957 if (restoring_missing_file)
958 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
959 else if (reverting_versioned_file)
960 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
961 else
962 err = (*progress_cb)(progress_arg,
963 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
964 if (err)
965 goto done;
967 hdrlen = got_object_blob_get_hdrlen(blob);
968 do {
969 const uint8_t *buf = got_object_blob_get_read_buf(blob);
970 err = got_object_blob_read_block(&len, blob);
971 if (err)
972 break;
973 if (len > 0) {
974 /* Skip blob object header first time around. */
975 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
976 if (outlen == -1) {
977 err = got_error_from_errno("write");
978 goto done;
979 } else if (outlen != len - hdrlen) {
980 err = got_error(GOT_ERR_IO);
981 goto done;
983 hdrlen = 0;
985 } while (len != 0);
987 if (fsync(fd) != 0) {
988 err = got_error_from_errno("fsync");
989 goto done;
992 if (update) {
993 if (rename(tmppath, ondisk_path) != 0) {
994 err = got_error_from_errno3("rename", tmppath,
995 ondisk_path);
996 unlink(tmppath);
997 goto done;
1001 if (te_mode & S_IXUSR) {
1002 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1003 err = got_error_from_errno2("chmod", ondisk_path);
1004 goto done;
1006 } else {
1007 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1008 err = got_error_from_errno2("chmod", ondisk_path);
1009 goto done;
1013 done:
1014 if (fd != -1 && close(fd) != 0 && err == NULL)
1015 err = got_error_from_errno("close");
1016 free(tmppath);
1017 return err;
1020 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1021 static const struct got_error *
1022 get_modified_file_content_status(unsigned char *status, FILE *f)
1024 const struct got_error *err = NULL;
1025 const char *markers[3] = {
1026 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1027 GOT_DIFF_CONFLICT_MARKER_SEP,
1028 GOT_DIFF_CONFLICT_MARKER_END
1030 int i = 0;
1031 char *line;
1032 size_t len;
1033 const char delim[3] = {'\0', '\0', '\0'};
1035 while (*status == GOT_STATUS_MODIFY) {
1036 line = fparseln(f, &len, NULL, delim, 0);
1037 if (line == NULL) {
1038 if (feof(f))
1039 break;
1040 err = got_ferror(f, GOT_ERR_IO);
1041 break;
1044 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1045 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1046 == 0)
1047 *status = GOT_STATUS_CONFLICT;
1048 else
1049 i++;
1053 return err;
1056 static int
1057 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1059 return !(ie->ctime_sec == sb->st_ctime &&
1060 ie->ctime_nsec == sb->st_ctimensec &&
1061 ie->mtime_sec == sb->st_mtime &&
1062 ie->mtime_nsec == sb->st_mtimensec &&
1063 ie->size == (sb->st_size & 0xffffffff));
1066 static unsigned char
1067 get_staged_status(struct got_fileindex_entry *ie)
1069 switch (got_fileindex_entry_stage_get(ie)) {
1070 case GOT_FILEIDX_STAGE_ADD:
1071 return GOT_STATUS_ADD;
1072 case GOT_FILEIDX_STAGE_DELETE:
1073 return GOT_STATUS_DELETE;
1074 case GOT_FILEIDX_STAGE_MODIFY:
1075 return GOT_STATUS_MODIFY;
1076 default:
1077 return GOT_STATUS_NO_CHANGE;
1081 static const struct got_error *
1082 get_file_status(unsigned char *status, struct stat *sb,
1083 struct got_fileindex_entry *ie, const char *abspath,
1084 struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_object_id id;
1088 size_t hdrlen;
1089 FILE *f = NULL;
1090 uint8_t fbuf[8192];
1091 struct got_blob_object *blob = NULL;
1092 size_t flen, blen;
1093 unsigned char staged_status = get_staged_status(ie);
1095 *status = GOT_STATUS_NO_CHANGE;
1097 if (lstat(abspath, sb) == -1) {
1098 if (errno == ENOENT) {
1099 if (got_fileindex_entry_has_file_on_disk(ie))
1100 *status = GOT_STATUS_MISSING;
1101 else
1102 *status = GOT_STATUS_DELETE;
1103 return NULL;
1105 return got_error_from_errno2("lstat", abspath);
1108 if (!S_ISREG(sb->st_mode)) {
1109 *status = GOT_STATUS_OBSTRUCTED;
1110 return NULL;
1113 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1114 *status = GOT_STATUS_DELETE;
1115 return NULL;
1116 } else if (!got_fileindex_entry_has_blob(ie) &&
1117 staged_status != GOT_STATUS_ADD) {
1118 *status = GOT_STATUS_ADD;
1119 return NULL;
1122 if (!stat_info_differs(ie, sb))
1123 return NULL;
1125 if (staged_status == GOT_STATUS_MODIFY ||
1126 staged_status == GOT_STATUS_ADD)
1127 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1128 else
1129 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1131 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1132 if (err)
1133 return err;
1135 f = fopen(abspath, "r");
1136 if (f == NULL) {
1137 err = got_error_from_errno2("fopen", abspath);
1138 goto done;
1140 hdrlen = got_object_blob_get_hdrlen(blob);
1141 for (;;) {
1142 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1143 err = got_object_blob_read_block(&blen, blob);
1144 if (err)
1145 goto done;
1146 /* Skip length of blob object header first time around. */
1147 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1148 if (flen == 0 && ferror(f)) {
1149 err = got_error_from_errno("fread");
1150 goto done;
1152 if (blen == 0) {
1153 if (flen != 0)
1154 *status = GOT_STATUS_MODIFY;
1155 break;
1156 } else if (flen == 0) {
1157 if (blen != 0)
1158 *status = GOT_STATUS_MODIFY;
1159 break;
1160 } else if (blen - hdrlen == flen) {
1161 /* Skip blob object header first time around. */
1162 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1163 *status = GOT_STATUS_MODIFY;
1164 break;
1166 } else {
1167 *status = GOT_STATUS_MODIFY;
1168 break;
1170 hdrlen = 0;
1173 if (*status == GOT_STATUS_MODIFY) {
1174 rewind(f);
1175 err = get_modified_file_content_status(status, f);
1177 done:
1178 if (blob)
1179 got_object_blob_close(blob);
1180 if (f)
1181 fclose(f);
1182 return err;
1186 * Update timestamps in the file index if a file is unmodified and
1187 * we had to run a full content comparison to find out.
1189 static const struct got_error *
1190 sync_timestamps(char *ondisk_path, unsigned char status,
1191 struct got_fileindex_entry *ie, struct stat *sb)
1193 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1194 return got_fileindex_entry_update(ie, ondisk_path,
1195 ie->blob_sha1, ie->commit_sha1, 1);
1197 return NULL;
1200 static const struct got_error *
1201 update_blob(struct got_worktree *worktree,
1202 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1203 struct got_tree_entry *te, const char *path,
1204 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1205 void *progress_arg)
1207 const struct got_error *err = NULL;
1208 struct got_blob_object *blob = NULL;
1209 char *ondisk_path;
1210 unsigned char status = GOT_STATUS_NO_CHANGE;
1211 struct stat sb;
1213 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1214 return got_error_from_errno("asprintf");
1216 if (ie) {
1217 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1218 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1219 goto done;
1221 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1222 if (err)
1223 goto done;
1224 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1225 sb.st_mode = got_fileindex_perms_to_st(ie);
1226 } else
1227 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1229 if (status == GOT_STATUS_OBSTRUCTED) {
1230 err = (*progress_cb)(progress_arg, status, path);
1231 goto done;
1234 if (ie && status != GOT_STATUS_MISSING) {
1235 if (got_fileindex_entry_has_commit(ie) &&
1236 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1237 SHA1_DIGEST_LENGTH) == 0) {
1238 err = sync_timestamps(ondisk_path, status, ie, &sb);
1239 if (err)
1240 goto done;
1241 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1242 path);
1243 goto done;
1245 if (got_fileindex_entry_has_blob(ie) &&
1246 memcmp(ie->blob_sha1, te->id->sha1,
1247 SHA1_DIGEST_LENGTH) == 0) {
1248 err = sync_timestamps(ondisk_path, status, ie, &sb);
1249 goto done;
1253 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1254 if (err)
1255 goto done;
1257 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1258 int update_timestamps;
1259 struct got_blob_object *blob2 = NULL;
1260 if (got_fileindex_entry_has_blob(ie)) {
1261 struct got_object_id id2;
1262 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1263 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1264 if (err)
1265 goto done;
1267 err = merge_blob(&update_timestamps, worktree, blob2,
1268 ondisk_path, path, sb.st_mode, blob,
1269 worktree->base_commit_id, repo,
1270 progress_cb, progress_arg);
1271 if (blob2)
1272 got_object_blob_close(blob2);
1274 * Do not update timestamps of files with local changes.
1275 * Otherwise, a future status walk would treat them as
1276 * unmodified files again.
1278 err = got_fileindex_entry_update(ie, ondisk_path,
1279 blob->id.sha1, worktree->base_commit_id->sha1,
1280 update_timestamps);
1281 } else if (status == GOT_STATUS_DELETE) {
1282 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1283 if (err)
1284 goto done;
1285 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1286 ondisk_path, path, blob, 0);
1287 if (err)
1288 goto done;
1289 } else {
1290 err = install_blob(worktree, ondisk_path, path, te->mode,
1291 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1292 repo, progress_cb, progress_arg);
1293 if (err)
1294 goto done;
1295 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1296 ondisk_path, path, blob, 1);
1297 if (err)
1298 goto done;
1300 got_object_blob_close(blob);
1301 done:
1302 free(ondisk_path);
1303 return err;
1306 static const struct got_error *
1307 remove_ondisk_file(const char *root_path, const char *path)
1309 const struct got_error *err = NULL;
1310 char *ondisk_path = NULL;
1312 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1313 return got_error_from_errno("asprintf");
1315 if (unlink(ondisk_path) == -1) {
1316 if (errno != ENOENT)
1317 err = got_error_from_errno2("unlink", ondisk_path);
1318 } else {
1319 char *parent = dirname(ondisk_path);
1320 while (parent && strcmp(parent, root_path) != 0) {
1321 if (rmdir(parent) == -1) {
1322 if (errno != ENOTEMPTY)
1323 err = got_error_from_errno2("rmdir",
1324 parent);
1325 break;
1327 parent = dirname(parent);
1330 free(ondisk_path);
1331 return err;
1334 static const struct got_error *
1335 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1336 struct got_fileindex_entry *ie, struct got_repository *repo,
1337 got_worktree_checkout_cb progress_cb, void *progress_arg)
1339 const struct got_error *err = NULL;
1340 unsigned char status;
1341 struct stat sb;
1342 char *ondisk_path;
1344 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1345 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1347 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1348 == -1)
1349 return got_error_from_errno("asprintf");
1351 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1352 if (err)
1353 return err;
1355 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1356 status == GOT_STATUS_ADD) {
1357 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1358 if (err)
1359 return err;
1361 * Preserve the working file and change the deleted blob's
1362 * entry into a schedule-add entry.
1364 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1365 0);
1366 if (err)
1367 return err;
1368 } else {
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1370 if (err)
1371 return err;
1372 if (status == GOT_STATUS_NO_CHANGE) {
1373 err = remove_ondisk_file(worktree->root_path, ie->path);
1374 if (err)
1375 return err;
1377 got_fileindex_entry_remove(fileindex, ie);
1380 return err;
1383 struct diff_cb_arg {
1384 struct got_fileindex *fileindex;
1385 struct got_worktree *worktree;
1386 struct got_repository *repo;
1387 got_worktree_checkout_cb progress_cb;
1388 void *progress_arg;
1389 got_worktree_cancel_cb cancel_cb;
1390 void *cancel_arg;
1393 static const struct got_error *
1394 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1395 struct got_tree_entry *te, const char *parent_path)
1397 struct diff_cb_arg *a = arg;
1399 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1400 return got_error(GOT_ERR_CANCELLED);
1402 return update_blob(a->worktree, a->fileindex, ie, te,
1403 ie->path, a->repo, a->progress_cb, a->progress_arg);
1406 static const struct got_error *
1407 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1409 struct diff_cb_arg *a = arg;
1411 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1412 return got_error(GOT_ERR_CANCELLED);
1414 return delete_blob(a->worktree, a->fileindex, ie,
1415 a->repo, a->progress_cb, a->progress_arg);
1418 static const struct got_error *
1419 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1421 struct diff_cb_arg *a = arg;
1422 const struct got_error *err;
1423 char *path;
1425 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1426 return got_error(GOT_ERR_CANCELLED);
1428 if (asprintf(&path, "%s%s%s", parent_path,
1429 parent_path[0] ? "/" : "", te->name)
1430 == -1)
1431 return got_error_from_errno("asprintf");
1433 if (S_ISDIR(te->mode))
1434 err = add_dir_on_disk(a->worktree, path);
1435 else
1436 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1437 a->repo, a->progress_cb, a->progress_arg);
1439 free(path);
1440 return err;
1443 static const struct got_error *
1444 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1446 const struct got_error *err = NULL;
1447 char *uuidstr = NULL;
1448 uint32_t uuid_status;
1450 *refname = NULL;
1452 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1453 if (uuid_status != uuid_s_ok)
1454 return got_error_uuid(uuid_status);
1456 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1457 == -1) {
1458 err = got_error_from_errno("asprintf");
1459 *refname = NULL;
1461 free(uuidstr);
1462 return err;
1465 const struct got_error *
1466 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1468 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1471 static const struct got_error *
1472 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1474 return get_ref_name(refname, worktree,
1475 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1478 static const struct got_error *
1479 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1481 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1484 static const struct got_error *
1485 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1487 return get_ref_name(refname, worktree,
1488 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1491 static const struct got_error *
1492 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1494 return get_ref_name(refname, worktree,
1495 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1498 static const struct got_error *
1499 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1501 return get_ref_name(refname, worktree,
1502 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1505 static const struct got_error *
1506 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1508 return get_ref_name(refname, worktree,
1509 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1512 static const struct got_error *
1513 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1515 return get_ref_name(refname, worktree,
1516 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1519 static const struct got_error *
1520 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 return get_ref_name(refname, worktree,
1523 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1526 const struct got_error *
1527 got_worktree_get_histedit_script_path(char **path,
1528 struct got_worktree *worktree)
1530 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1531 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1532 *path = NULL;
1533 return got_error_from_errno("asprintf");
1535 return NULL;
1539 * Prevent Git's garbage collector from deleting our base commit by
1540 * setting a reference to our base commit's ID.
1542 static const struct got_error *
1543 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1545 const struct got_error *err = NULL;
1546 struct got_reference *ref = NULL;
1547 char *refname;
1549 err = got_worktree_get_base_ref_name(&refname, worktree);
1550 if (err)
1551 return err;
1553 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1554 if (err)
1555 goto done;
1557 err = got_ref_write(ref, repo);
1558 done:
1559 free(refname);
1560 if (ref)
1561 got_ref_close(ref);
1562 return err;
1565 static const struct got_error *
1566 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1568 const struct got_error *err = NULL;
1570 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1571 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1572 err = got_error_from_errno("asprintf");
1573 *fileindex_path = NULL;
1575 return err;
1579 static const struct got_error *
1580 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1581 struct got_worktree *worktree)
1583 const struct got_error *err = NULL;
1584 FILE *index = NULL;
1586 *fileindex_path = NULL;
1587 *fileindex = got_fileindex_alloc();
1588 if (*fileindex == NULL)
1589 return got_error_from_errno("got_fileindex_alloc");
1591 err = get_fileindex_path(fileindex_path, worktree);
1592 if (err)
1593 goto done;
1595 index = fopen(*fileindex_path, "rb");
1596 if (index == NULL) {
1597 if (errno != ENOENT)
1598 err = got_error_from_errno2("fopen", *fileindex_path);
1599 } else {
1600 err = got_fileindex_read(*fileindex, index);
1601 if (fclose(index) != 0 && err == NULL)
1602 err = got_error_from_errno("fclose");
1604 done:
1605 if (err) {
1606 free(*fileindex_path);
1607 *fileindex_path = NULL;
1608 got_fileindex_free(*fileindex);
1609 *fileindex = NULL;
1611 return err;
1614 struct bump_base_commit_id_arg {
1615 struct got_object_id *base_commit_id;
1616 const char *path;
1617 size_t path_len;
1618 const char *entry_name;
1619 got_worktree_checkout_cb progress_cb;
1620 void *progress_arg;
1623 /* Bump base commit ID of all files within an updated part of the work tree. */
1624 static const struct got_error *
1625 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1627 const struct got_error *err;
1628 struct bump_base_commit_id_arg *a = arg;
1630 if (a->entry_name) {
1631 if (strcmp(ie->path, a->path) != 0)
1632 return NULL;
1633 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1634 return NULL;
1636 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1637 SHA1_DIGEST_LENGTH) == 0)
1638 return NULL;
1640 if (a->progress_cb) {
1641 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1642 ie->path);
1643 if (err)
1644 return err;
1646 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1647 return NULL;
1650 static const struct got_error *
1651 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1653 const struct got_error *err = NULL;
1654 char *new_fileindex_path = NULL;
1655 FILE *new_index = NULL;
1657 err = got_opentemp_named(&new_fileindex_path, &new_index,
1658 fileindex_path);
1659 if (err)
1660 goto done;
1662 err = got_fileindex_write(fileindex, new_index);
1663 if (err)
1664 goto done;
1666 if (rename(new_fileindex_path, fileindex_path) != 0) {
1667 err = got_error_from_errno3("rename", new_fileindex_path,
1668 fileindex_path);
1669 unlink(new_fileindex_path);
1671 done:
1672 if (new_index)
1673 fclose(new_index);
1674 free(new_fileindex_path);
1675 return err;
1678 static const struct got_error *
1679 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1680 struct got_object_id **tree_id, const char *wt_relpath,
1681 struct got_worktree *worktree, struct got_repository *repo)
1683 const struct got_error *err = NULL;
1684 struct got_object_id *id = NULL;
1685 char *in_repo_path = NULL;
1686 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1688 *entry_type = GOT_OBJ_TYPE_ANY;
1689 *tree_relpath = NULL;
1690 *tree_id = NULL;
1692 if (wt_relpath[0] == '\0') {
1693 /* Check out all files within the work tree. */
1694 *entry_type = GOT_OBJ_TYPE_TREE;
1695 *tree_relpath = strdup("");
1696 if (*tree_relpath == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 err = got_object_id_by_path(tree_id, repo,
1701 worktree->base_commit_id, worktree->path_prefix);
1702 if (err)
1703 goto done;
1704 return NULL;
1707 /* Check out a subset of files in the work tree. */
1709 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1710 is_root_wt ? "" : "/", wt_relpath) == -1) {
1711 err = got_error_from_errno("asprintf");
1712 goto done;
1715 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1716 in_repo_path);
1717 if (err)
1718 goto done;
1720 free(in_repo_path);
1721 in_repo_path = NULL;
1723 err = got_object_get_type(entry_type, repo, id);
1724 if (err)
1725 goto done;
1727 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1728 /* Check out a single file. */
1729 if (strchr(wt_relpath, '/') == NULL) {
1730 /* Check out a single file in work tree's root dir. */
1731 in_repo_path = strdup(worktree->path_prefix);
1732 if (in_repo_path == NULL) {
1733 err = got_error_from_errno("strdup");
1734 goto done;
1736 *tree_relpath = strdup("");
1737 if (*tree_relpath == NULL) {
1738 err = got_error_from_errno("strdup");
1739 goto done;
1741 } else {
1742 /* Check out a single file in a subdirectory. */
1743 err = got_path_dirname(tree_relpath, wt_relpath);
1744 if (err)
1745 return err;
1746 if (asprintf(&in_repo_path, "%s%s%s",
1747 worktree->path_prefix, is_root_wt ? "" : "/",
1748 *tree_relpath) == -1) {
1749 err = got_error_from_errno("asprintf");
1750 goto done;
1753 err = got_object_id_by_path(tree_id, repo,
1754 worktree->base_commit_id, in_repo_path);
1755 } else {
1756 /* Check out all files within a subdirectory. */
1757 *tree_id = got_object_id_dup(id);
1758 if (*tree_id == NULL) {
1759 err = got_error_from_errno("got_object_id_dup");
1760 goto done;
1762 *tree_relpath = strdup(wt_relpath);
1763 if (*tree_relpath == NULL) {
1764 err = got_error_from_errno("strdup");
1765 goto done;
1768 done:
1769 free(id);
1770 free(in_repo_path);
1771 if (err) {
1772 *entry_type = GOT_OBJ_TYPE_ANY;
1773 free(*tree_relpath);
1774 *tree_relpath = NULL;
1775 free(*tree_id);
1776 *tree_id = NULL;
1778 return err;
1781 static const struct got_error *
1782 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1783 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1784 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1785 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1787 const struct got_error *err = NULL;
1788 struct got_commit_object *commit = NULL;
1789 struct got_tree_object *tree = NULL;
1790 struct got_fileindex_diff_tree_cb diff_cb;
1791 struct diff_cb_arg arg;
1793 err = ref_base_commit(worktree, repo);
1794 if (err)
1795 goto done;
1797 err = got_object_open_as_commit(&commit, repo,
1798 worktree->base_commit_id);
1799 if (err)
1800 goto done;
1802 err = got_object_open_as_tree(&tree, repo, tree_id);
1803 if (err)
1804 goto done;
1806 if (entry_name &&
1807 got_object_tree_find_entry(tree, entry_name) == NULL) {
1808 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1809 goto done;
1812 diff_cb.diff_old_new = diff_old_new;
1813 diff_cb.diff_old = diff_old;
1814 diff_cb.diff_new = diff_new;
1815 arg.fileindex = fileindex;
1816 arg.worktree = worktree;
1817 arg.repo = repo;
1818 arg.progress_cb = progress_cb;
1819 arg.progress_arg = progress_arg;
1820 arg.cancel_cb = cancel_cb;
1821 arg.cancel_arg = cancel_arg;
1822 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1823 entry_name, repo, &diff_cb, &arg);
1824 done:
1825 if (tree)
1826 got_object_tree_close(tree);
1827 if (commit)
1828 got_object_commit_close(commit);
1829 return err;
1832 const struct got_error *
1833 got_worktree_checkout_files(struct got_worktree *worktree,
1834 struct got_pathlist_head *paths, struct got_repository *repo,
1835 got_worktree_checkout_cb progress_cb, void *progress_arg,
1836 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1838 const struct got_error *err = NULL, *sync_err, *unlockerr;
1839 struct got_commit_object *commit = NULL;
1840 struct got_tree_object *tree = NULL;
1841 struct got_fileindex *fileindex = NULL;
1842 char *fileindex_path = NULL;
1843 struct got_pathlist_entry *pe;
1844 struct tree_path_data {
1845 SIMPLEQ_ENTRY(tree_path_data) entry;
1846 struct got_object_id *tree_id;
1847 int entry_type;
1848 char *relpath;
1849 char *entry_name;
1850 } *tpd = NULL;
1851 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1853 SIMPLEQ_INIT(&tree_paths);
1855 err = lock_worktree(worktree, LOCK_EX);
1856 if (err)
1857 return err;
1859 /* Map all specified paths to in-repository trees. */
1860 TAILQ_FOREACH(pe, paths, entry) {
1861 tpd = malloc(sizeof(*tpd));
1862 if (tpd == NULL) {
1863 err = got_error_from_errno("malloc");
1864 goto done;
1867 err = find_tree_entry_for_checkout(&tpd->entry_type,
1868 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1869 if (err) {
1870 free(tpd);
1871 goto done;
1874 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1875 err = got_path_basename(&tpd->entry_name, pe->path);
1876 if (err) {
1877 free(tpd->relpath);
1878 free(tpd->tree_id);
1879 free(tpd);
1880 goto done;
1882 } else
1883 tpd->entry_name = NULL;
1885 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1889 * Read the file index.
1890 * Checking out files is supposed to be an idempotent operation.
1891 * If the on-disk file index is incomplete we will try to complete it.
1893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1894 if (err)
1895 goto done;
1897 tpd = SIMPLEQ_FIRST(&tree_paths);
1898 TAILQ_FOREACH(pe, paths, entry) {
1899 struct bump_base_commit_id_arg bbc_arg;
1901 err = checkout_files(worktree, fileindex, tpd->relpath,
1902 tpd->tree_id, tpd->entry_name, repo,
1903 progress_cb, progress_arg, cancel_cb, cancel_arg);
1904 if (err)
1905 break;
1907 bbc_arg.base_commit_id = worktree->base_commit_id;
1908 bbc_arg.entry_name = tpd->entry_name;
1909 bbc_arg.path = pe->path;
1910 bbc_arg.path_len = pe->path_len;
1911 bbc_arg.progress_cb = progress_cb;
1912 bbc_arg.progress_arg = progress_arg;
1913 err = got_fileindex_for_each_entry_safe(fileindex,
1914 bump_base_commit_id, &bbc_arg);
1915 if (err)
1916 break;
1918 tpd = SIMPLEQ_NEXT(tpd, entry);
1920 sync_err = sync_fileindex(fileindex, fileindex_path);
1921 if (sync_err && err == NULL)
1922 err = sync_err;
1923 done:
1924 free(fileindex_path);
1925 if (tree)
1926 got_object_tree_close(tree);
1927 if (commit)
1928 got_object_commit_close(commit);
1929 if (fileindex)
1930 got_fileindex_free(fileindex);
1931 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1932 tpd = SIMPLEQ_FIRST(&tree_paths);
1933 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1934 free(tpd->relpath);
1935 free(tpd->tree_id);
1936 free(tpd);
1938 unlockerr = lock_worktree(worktree, LOCK_SH);
1939 if (unlockerr && err == NULL)
1940 err = unlockerr;
1941 return err;
1944 struct merge_file_cb_arg {
1945 struct got_worktree *worktree;
1946 struct got_fileindex *fileindex;
1947 got_worktree_checkout_cb progress_cb;
1948 void *progress_arg;
1949 got_worktree_cancel_cb cancel_cb;
1950 void *cancel_arg;
1951 struct got_object_id *commit_id2;
1954 static const struct got_error *
1955 merge_file_cb(void *arg, struct got_blob_object *blob1,
1956 struct got_blob_object *blob2, struct got_object_id *id1,
1957 struct got_object_id *id2, const char *path1, const char *path2,
1958 struct got_repository *repo)
1960 static const struct got_error *err = NULL;
1961 struct merge_file_cb_arg *a = arg;
1962 struct got_fileindex_entry *ie;
1963 char *ondisk_path = NULL;
1964 struct stat sb;
1965 unsigned char status;
1966 int local_changes_subsumed;
1968 if (blob1 && blob2) {
1969 ie = got_fileindex_entry_get(a->fileindex, path2,
1970 strlen(path2));
1971 if (ie == NULL)
1972 return (*a->progress_cb)(a->progress_arg,
1973 GOT_STATUS_MISSING, path2);
1975 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1976 path2) == -1)
1977 return got_error_from_errno("asprintf");
1979 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1980 if (err)
1981 goto done;
1983 if (status == GOT_STATUS_DELETE) {
1984 err = (*a->progress_cb)(a->progress_arg,
1985 GOT_STATUS_MERGE, path2);
1986 goto done;
1988 if (status != GOT_STATUS_NO_CHANGE &&
1989 status != GOT_STATUS_MODIFY &&
1990 status != GOT_STATUS_CONFLICT &&
1991 status != GOT_STATUS_ADD) {
1992 err = (*a->progress_cb)(a->progress_arg, status, path2);
1993 goto done;
1996 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1997 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1998 a->progress_cb, a->progress_arg);
1999 } else if (blob1) {
2000 ie = got_fileindex_entry_get(a->fileindex, path1,
2001 strlen(path1));
2002 if (ie == NULL)
2003 return (*a->progress_cb)(a->progress_arg,
2004 GOT_STATUS_MISSING, path2);
2006 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2007 path1) == -1)
2008 return got_error_from_errno("asprintf");
2010 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2011 if (err)
2012 goto done;
2014 switch (status) {
2015 case GOT_STATUS_NO_CHANGE:
2016 err = (*a->progress_cb)(a->progress_arg,
2017 GOT_STATUS_DELETE, path1);
2018 if (err)
2019 goto done;
2020 err = remove_ondisk_file(a->worktree->root_path, path1);
2021 if (err)
2022 goto done;
2023 if (ie)
2024 got_fileindex_entry_mark_deleted_from_disk(ie);
2025 break;
2026 case GOT_STATUS_DELETE:
2027 case GOT_STATUS_MISSING:
2028 err = (*a->progress_cb)(a->progress_arg,
2029 GOT_STATUS_DELETE, path1);
2030 if (err)
2031 goto done;
2032 if (ie)
2033 got_fileindex_entry_mark_deleted_from_disk(ie);
2034 break;
2035 case GOT_STATUS_ADD:
2036 case GOT_STATUS_MODIFY:
2037 case GOT_STATUS_CONFLICT:
2038 err = (*a->progress_cb)(a->progress_arg,
2039 GOT_STATUS_CANNOT_DELETE, path1);
2040 if (err)
2041 goto done;
2042 break;
2043 case GOT_STATUS_OBSTRUCTED:
2044 err = (*a->progress_cb)(a->progress_arg, status, path1);
2045 if (err)
2046 goto done;
2047 break;
2048 default:
2049 break;
2051 } else if (blob2) {
2052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2053 path2) == -1)
2054 return got_error_from_errno("asprintf");
2055 ie = got_fileindex_entry_get(a->fileindex, path2,
2056 strlen(path2));
2057 if (ie) {
2058 err = get_file_status(&status, &sb, ie, ondisk_path,
2059 repo);
2060 if (err)
2061 goto done;
2062 if (status != GOT_STATUS_NO_CHANGE &&
2063 status != GOT_STATUS_MODIFY &&
2064 status != GOT_STATUS_CONFLICT &&
2065 status != GOT_STATUS_ADD) {
2066 err = (*a->progress_cb)(a->progress_arg,
2067 status, path2);
2068 goto done;
2070 err = merge_blob(&local_changes_subsumed, a->worktree,
2071 NULL, ondisk_path, path2, sb.st_mode, blob2,
2072 a->commit_id2, repo,
2073 a->progress_cb, a->progress_arg);
2074 if (status == GOT_STATUS_DELETE) {
2075 err = update_blob_fileindex_entry(a->worktree,
2076 a->fileindex, ie, ondisk_path, ie->path,
2077 blob2, 0);
2078 if (err)
2079 goto done;
2081 } else {
2082 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2083 err = install_blob(a->worktree, ondisk_path, path2,
2084 /* XXX get this from parent tree! */
2085 GOT_DEFAULT_FILE_MODE,
2086 sb.st_mode, blob2, 0, 0, repo,
2087 a->progress_cb, a->progress_arg);
2088 if (err)
2089 goto done;
2090 err = got_fileindex_entry_alloc(&ie,
2091 ondisk_path, path2, NULL, NULL);
2092 if (err)
2093 goto done;
2094 err = got_fileindex_entry_add(a->fileindex, ie);
2095 if (err) {
2096 got_fileindex_entry_free(ie);
2097 goto done;
2101 done:
2102 free(ondisk_path);
2103 return err;
2106 struct check_merge_ok_arg {
2107 struct got_worktree *worktree;
2108 struct got_repository *repo;
2111 static const struct got_error *
2112 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2114 const struct got_error *err = NULL;
2115 struct check_merge_ok_arg *a = arg;
2116 unsigned char status;
2117 struct stat sb;
2118 char *ondisk_path;
2120 /* Reject merges into a work tree with mixed base commits. */
2121 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2122 SHA1_DIGEST_LENGTH))
2123 return got_error(GOT_ERR_MIXED_COMMITS);
2125 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2126 == -1)
2127 return got_error_from_errno("asprintf");
2129 /* Reject merges into a work tree with conflicted files. */
2130 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2131 if (err)
2132 return err;
2133 if (status == GOT_STATUS_CONFLICT)
2134 return got_error(GOT_ERR_CONFLICTS);
2136 return NULL;
2139 static const struct got_error *
2140 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 const char *fileindex_path, struct got_object_id *commit_id1,
2142 struct got_object_id *commit_id2, struct got_repository *repo,
2143 got_worktree_checkout_cb progress_cb, void *progress_arg,
2144 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2146 const struct got_error *err = NULL, *sync_err;
2147 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2148 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2149 struct merge_file_cb_arg arg;
2151 if (commit_id1) {
2152 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2153 worktree->path_prefix);
2154 if (err)
2155 goto done;
2157 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2158 if (err)
2159 goto done;
2162 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2163 worktree->path_prefix);
2164 if (err)
2165 goto done;
2167 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2168 if (err)
2169 goto done;
2171 arg.worktree = worktree;
2172 arg.fileindex = fileindex;
2173 arg.progress_cb = progress_cb;
2174 arg.progress_arg = progress_arg;
2175 arg.cancel_cb = cancel_cb;
2176 arg.cancel_arg = cancel_arg;
2177 arg.commit_id2 = commit_id2;
2178 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2179 sync_err = sync_fileindex(fileindex, fileindex_path);
2180 if (sync_err && err == NULL)
2181 err = sync_err;
2182 done:
2183 if (tree1)
2184 got_object_tree_close(tree1);
2185 if (tree2)
2186 got_object_tree_close(tree2);
2187 return err;
2190 const struct got_error *
2191 got_worktree_merge_files(struct got_worktree *worktree,
2192 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2193 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2194 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2196 const struct got_error *err, *unlockerr;
2197 char *fileindex_path = NULL;
2198 struct got_fileindex *fileindex = NULL;
2199 struct check_merge_ok_arg mok_arg;
2201 err = lock_worktree(worktree, LOCK_EX);
2202 if (err)
2203 return err;
2205 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2206 if (err)
2207 goto done;
2209 mok_arg.worktree = worktree;
2210 mok_arg.repo = repo;
2211 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2212 &mok_arg);
2213 if (err)
2214 goto done;
2216 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2217 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2218 done:
2219 if (fileindex)
2220 got_fileindex_free(fileindex);
2221 free(fileindex_path);
2222 unlockerr = lock_worktree(worktree, LOCK_SH);
2223 if (unlockerr && err == NULL)
2224 err = unlockerr;
2225 return err;
2228 struct diff_dir_cb_arg {
2229 struct got_fileindex *fileindex;
2230 struct got_worktree *worktree;
2231 const char *status_path;
2232 size_t status_path_len;
2233 struct got_repository *repo;
2234 got_worktree_status_cb status_cb;
2235 void *status_arg;
2236 got_worktree_cancel_cb cancel_cb;
2237 void *cancel_arg;
2238 /* A pathlist containing per-directory pathlists of ignore patterns. */
2239 struct got_pathlist_head ignores;
2242 static const struct got_error *
2243 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2244 got_worktree_status_cb status_cb, void *status_arg,
2245 struct got_repository *repo)
2247 const struct got_error *err = NULL;
2248 unsigned char status = GOT_STATUS_NO_CHANGE;
2249 unsigned char staged_status = get_staged_status(ie);
2250 struct stat sb;
2251 struct got_object_id blob_id, commit_id, staged_blob_id;
2252 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2253 struct got_object_id *staged_blob_idp = NULL;
2255 err = get_file_status(&status, &sb, ie, abspath, repo);
2256 if (err)
2257 return err;
2259 if (status == GOT_STATUS_NO_CHANGE &&
2260 staged_status == GOT_STATUS_NO_CHANGE)
2261 return NULL;
2263 if (got_fileindex_entry_has_blob(ie)) {
2264 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2265 blob_idp = &blob_id;
2267 if (got_fileindex_entry_has_commit(ie)) {
2268 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2269 commit_idp = &commit_id;
2271 if (staged_status == GOT_STATUS_ADD ||
2272 staged_status == GOT_STATUS_MODIFY) {
2273 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2274 SHA1_DIGEST_LENGTH);
2275 staged_blob_idp = &staged_blob_id;
2278 return (*status_cb)(status_arg, status, staged_status,
2279 ie->path, blob_idp, staged_blob_idp, commit_idp);
2282 static const struct got_error *
2283 status_old_new(void *arg, struct got_fileindex_entry *ie,
2284 struct dirent *de, const char *parent_path)
2286 const struct got_error *err = NULL;
2287 struct diff_dir_cb_arg *a = arg;
2288 char *abspath;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (got_path_cmp(parent_path, a->status_path,
2294 strlen(parent_path), a->status_path_len) != 0 &&
2295 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2296 return NULL;
2298 if (parent_path[0]) {
2299 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2300 parent_path, de->d_name) == -1)
2301 return got_error_from_errno("asprintf");
2302 } else {
2303 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2304 de->d_name) == -1)
2305 return got_error_from_errno("asprintf");
2308 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2309 a->repo);
2310 free(abspath);
2311 return err;
2314 static const struct got_error *
2315 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2317 struct diff_dir_cb_arg *a = arg;
2318 struct got_object_id blob_id, commit_id;
2319 unsigned char status;
2321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2322 return got_error(GOT_ERR_CANCELLED);
2324 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2325 return NULL;
2327 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2328 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2329 if (got_fileindex_entry_has_file_on_disk(ie))
2330 status = GOT_STATUS_MISSING;
2331 else
2332 status = GOT_STATUS_DELETE;
2333 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2334 ie->path, &blob_id, NULL, &commit_id);
2337 void
2338 free_ignorelist(struct got_pathlist_head *ignorelist)
2340 struct got_pathlist_entry *pe;
2342 TAILQ_FOREACH(pe, ignorelist, entry)
2343 free((char *)pe->path);
2344 got_pathlist_free(ignorelist);
2347 void
2348 free_ignores(struct got_pathlist_head *ignores)
2350 struct got_pathlist_entry *pe;
2352 TAILQ_FOREACH(pe, ignores, entry) {
2353 struct got_pathlist_head *ignorelist = pe->data;
2354 free_ignorelist(ignorelist);
2355 free((char *)pe->path);
2357 got_pathlist_free(ignores);
2360 static const struct got_error *
2361 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2363 const struct got_error *err = NULL;
2364 struct got_pathlist_entry *pe = NULL;
2365 struct got_pathlist_head *ignorelist;
2366 char *line = NULL, *pattern, *dirpath = NULL;
2367 size_t linesize = 0;
2368 ssize_t linelen;
2370 ignorelist = calloc(1, sizeof(*ignorelist));
2371 if (ignorelist == NULL)
2372 return got_error_from_errno("calloc");
2373 TAILQ_INIT(ignorelist);
2375 while ((linelen = getline(&line, &linesize, f)) != -1) {
2376 if (linelen > 0 && line[linelen - 1] == '\n')
2377 line[linelen - 1] = '\0';
2378 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2379 line) == -1) {
2380 err = got_error_from_errno("asprintf");
2381 goto done;
2383 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2384 if (err)
2385 goto done;
2387 if (ferror(f)) {
2388 err = got_error_from_errno("getline");
2389 goto done;
2392 dirpath = strdup(path);
2393 if (dirpath == NULL) {
2394 err = got_error_from_errno("strdup");
2395 goto done;
2397 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2398 done:
2399 free(line);
2400 if (err || pe == NULL) {
2401 free(dirpath);
2402 free_ignorelist(ignorelist);
2404 return err;
2407 int
2408 match_ignores(struct got_pathlist_head *ignores, const char *path)
2410 struct got_pathlist_entry *pe;
2413 * The ignores pathlist contains ignore lists from children before
2414 * parents, so we can find the most specific ignorelist by walking
2415 * ignores backwards.
2417 pe = TAILQ_LAST(ignores, got_pathlist_head);
2418 while (pe) {
2419 if (got_path_is_child(path, pe->path, pe->path_len)) {
2420 struct got_pathlist_head *ignorelist = pe->data;
2421 struct got_pathlist_entry *pi;
2422 TAILQ_FOREACH(pi, ignorelist, entry) {
2423 if (fnmatch(pi->path, path,
2424 FNM_PATHNAME | FNM_LEADING_DIR))
2425 continue;
2426 return 1;
2429 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2432 return 0;
2435 static const struct got_error *
2436 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2437 const char *path)
2439 const struct got_error *err = NULL;
2440 char *ignorespath;
2441 FILE *ignoresfile = NULL;
2443 /* TODO: read .gitignores as well... */
2444 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2445 path[0] ? "/" : "") == -1)
2446 return got_error_from_errno("asprintf");
2448 ignoresfile = fopen(ignorespath, "r");
2449 if (ignoresfile == NULL) {
2450 if (errno != ENOENT)
2451 err = got_error_from_errno2("fopen",
2452 ignorespath);
2453 } else
2454 err = read_ignores(ignores, path, ignoresfile);
2456 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2457 err = got_error_from_errno2("flose", path);
2458 free(ignorespath);
2459 return err;
2462 static const struct got_error *
2463 status_new(void *arg, struct dirent *de, const char *parent_path)
2465 const struct got_error *err = NULL;
2466 struct diff_dir_cb_arg *a = arg;
2467 char *path = NULL;
2469 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2470 return got_error(GOT_ERR_CANCELLED);
2472 /* XXX ignore symlinks for now */
2473 if (de->d_type == DT_LNK)
2474 return NULL;
2476 if (parent_path[0]) {
2477 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2478 return got_error_from_errno("asprintf");
2479 } else {
2480 path = de->d_name;
2483 if (de->d_type == DT_DIR)
2484 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2485 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2486 && !match_ignores(&a->ignores, path))
2487 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2488 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2489 if (parent_path[0])
2490 free(path);
2491 return err;
2494 static const struct got_error *
2495 report_single_file_status(const char *path, const char *ondisk_path,
2496 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2497 void *status_arg, struct got_repository *repo)
2499 struct got_fileindex_entry *ie;
2500 struct stat sb;
2502 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2503 if (ie)
2504 return report_file_status(ie, ondisk_path, status_cb,
2505 status_arg, repo);
2507 if (lstat(ondisk_path, &sb) == -1) {
2508 if (errno != ENOENT)
2509 return got_error_from_errno2("lstat", ondisk_path);
2510 return NULL;
2513 if (S_ISREG(sb.st_mode))
2514 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2517 return NULL;
2520 static const struct got_error *
2521 worktree_status(struct got_worktree *worktree, const char *path,
2522 struct got_fileindex *fileindex, struct got_repository *repo,
2523 got_worktree_status_cb status_cb, void *status_arg,
2524 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2526 const struct got_error *err = NULL;
2527 DIR *workdir = NULL;
2528 struct got_fileindex_diff_dir_cb fdiff_cb;
2529 struct diff_dir_cb_arg arg;
2530 char *ondisk_path = NULL;
2532 if (asprintf(&ondisk_path, "%s%s%s",
2533 worktree->root_path, path[0] ? "/" : "", path) == -1)
2534 return got_error_from_errno("asprintf");
2536 workdir = opendir(ondisk_path);
2537 if (workdir == NULL) {
2538 if (errno != ENOTDIR && errno != ENOENT)
2539 err = got_error_from_errno2("opendir", ondisk_path);
2540 else
2541 err = report_single_file_status(path, ondisk_path,
2542 fileindex, status_cb, status_arg, repo);
2543 } else {
2544 fdiff_cb.diff_old_new = status_old_new;
2545 fdiff_cb.diff_old = status_old;
2546 fdiff_cb.diff_new = status_new;
2547 arg.fileindex = fileindex;
2548 arg.worktree = worktree;
2549 arg.status_path = path;
2550 arg.status_path_len = strlen(path);
2551 arg.repo = repo;
2552 arg.status_cb = status_cb;
2553 arg.status_arg = status_arg;
2554 arg.cancel_cb = cancel_cb;
2555 arg.cancel_arg = cancel_arg;
2556 TAILQ_INIT(&arg.ignores);
2557 err = add_ignores(&arg.ignores, worktree->root_path, path);
2558 if (err == NULL)
2559 err = got_fileindex_diff_dir(fileindex, workdir,
2560 worktree->root_path, path, repo, &fdiff_cb, &arg);
2561 free_ignores(&arg.ignores);
2564 if (workdir)
2565 closedir(workdir);
2566 free(ondisk_path);
2567 return err;
2570 const struct got_error *
2571 got_worktree_status(struct got_worktree *worktree,
2572 struct got_pathlist_head *paths, struct got_repository *repo,
2573 got_worktree_status_cb status_cb, void *status_arg,
2574 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2576 const struct got_error *err = NULL;
2577 char *fileindex_path = NULL;
2578 struct got_fileindex *fileindex = NULL;
2579 struct got_pathlist_entry *pe;
2581 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2582 if (err)
2583 return err;
2585 TAILQ_FOREACH(pe, paths, entry) {
2586 err = worktree_status(worktree, pe->path, fileindex, repo,
2587 status_cb, status_arg, cancel_cb, cancel_arg);
2588 if (err)
2589 break;
2591 free(fileindex_path);
2592 got_fileindex_free(fileindex);
2593 return err;
2596 const struct got_error *
2597 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2598 const char *arg)
2600 const struct got_error *err = NULL;
2601 char *resolved, *cwd = NULL, *path = NULL;
2602 size_t len;
2604 *wt_path = NULL;
2606 resolved = realpath(arg, NULL);
2607 if (resolved == NULL) {
2608 if (errno != ENOENT)
2609 return got_error_from_errno2("realpath", arg);
2610 cwd = getcwd(NULL, 0);
2611 if (cwd == NULL)
2612 return got_error_from_errno("getcwd");
2613 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2614 err = got_error_from_errno("asprintf");
2615 goto done;
2619 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2620 strlen(got_worktree_get_root_path(worktree)))) {
2621 err = got_error(GOT_ERR_BAD_PATH);
2622 goto done;
2625 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2626 err = got_path_skip_common_ancestor(&path,
2627 got_worktree_get_root_path(worktree), resolved);
2628 if (err)
2629 goto done;
2630 } else {
2631 path = strdup("");
2632 if (path == NULL) {
2633 err = got_error_from_errno("strdup");
2634 goto done;
2638 /* XXX status walk can't deal with trailing slash! */
2639 len = strlen(path);
2640 while (len > 0 && path[len - 1] == '/') {
2641 path[len - 1] = '\0';
2642 len--;
2644 done:
2645 free(resolved);
2646 free(cwd);
2647 if (err == NULL)
2648 *wt_path = path;
2649 else
2650 free(path);
2651 return err;
2654 static const struct got_error *
2655 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2656 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2657 struct got_repository *repo)
2659 const struct got_error *err = NULL;
2660 struct got_fileindex_entry *ie;
2661 unsigned char status;
2662 struct stat sb;
2664 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2665 if (ie) {
2666 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2667 if (err)
2668 return err;
2669 /* Re-adding an existing entry is a no-op. */
2670 if (status == GOT_STATUS_ADD)
2671 return NULL;
2672 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2675 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2676 if (err)
2677 return err;
2679 err = got_fileindex_entry_add(fileindex, ie);
2680 if (err) {
2681 got_fileindex_entry_free(ie);
2682 return err;
2685 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2688 const struct got_error *
2689 got_worktree_schedule_add(struct got_worktree *worktree,
2690 struct got_pathlist_head *paths,
2691 got_worktree_status_cb status_cb, void *status_arg,
2692 struct got_repository *repo)
2694 struct got_fileindex *fileindex = NULL;
2695 char *fileindex_path = NULL;
2696 const struct got_error *err = NULL, *sync_err, *unlockerr;
2697 struct got_pathlist_entry *pe;
2699 err = lock_worktree(worktree, LOCK_EX);
2700 if (err)
2701 return err;
2703 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2704 if (err)
2705 goto done;
2707 TAILQ_FOREACH(pe, paths, entry) {
2708 char *ondisk_path;
2709 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2710 pe->path) == -1)
2711 return got_error_from_errno("asprintf");
2712 err = schedule_addition(ondisk_path, fileindex, pe->path,
2713 status_cb, status_arg, repo);
2714 free(ondisk_path);
2715 if (err)
2716 break;
2718 sync_err = sync_fileindex(fileindex, fileindex_path);
2719 if (sync_err && err == NULL)
2720 err = sync_err;
2721 done:
2722 free(fileindex_path);
2723 if (fileindex)
2724 got_fileindex_free(fileindex);
2725 unlockerr = lock_worktree(worktree, LOCK_SH);
2726 if (unlockerr && err == NULL)
2727 err = unlockerr;
2728 return err;
2731 static const struct got_error *
2732 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2733 const char *relpath, int delete_local_mods,
2734 got_worktree_status_cb status_cb, void *status_arg,
2735 struct got_repository *repo)
2737 const struct got_error *err = NULL;
2738 struct got_fileindex_entry *ie = NULL;
2739 unsigned char status, staged_status;
2740 struct stat sb;
2742 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2743 if (ie == NULL)
2744 return got_error(GOT_ERR_BAD_PATH);
2746 staged_status = get_staged_status(ie);
2747 if (staged_status != GOT_STATUS_NO_CHANGE) {
2748 if (staged_status == GOT_STATUS_DELETE)
2749 return NULL;
2750 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2753 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2754 if (err)
2755 return err;
2757 if (status != GOT_STATUS_NO_CHANGE) {
2758 if (status == GOT_STATUS_DELETE)
2759 return NULL;
2760 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2761 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2762 if (status != GOT_STATUS_MODIFY &&
2763 status != GOT_STATUS_MISSING)
2764 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2767 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2768 return got_error_from_errno2("unlink", ondisk_path);
2770 got_fileindex_entry_mark_deleted_from_disk(ie);
2771 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2774 const struct got_error *
2775 got_worktree_schedule_delete(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, int delete_local_mods,
2777 got_worktree_status_cb status_cb, void *status_arg,
2778 struct got_repository *repo)
2780 struct got_fileindex *fileindex = NULL;
2781 char *fileindex_path = NULL;
2782 const struct got_error *err = NULL, *sync_err, *unlockerr;
2783 struct got_pathlist_entry *pe;
2785 err = lock_worktree(worktree, LOCK_EX);
2786 if (err)
2787 return err;
2789 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2790 if (err)
2791 goto done;
2793 TAILQ_FOREACH(pe, paths, entry) {
2794 char *ondisk_path;
2795 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2796 pe->path) == -1)
2797 return got_error_from_errno("asprintf");
2798 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2799 delete_local_mods, status_cb, status_arg, repo);
2800 free(ondisk_path);
2801 if (err)
2802 break;
2804 sync_err = sync_fileindex(fileindex, fileindex_path);
2805 if (sync_err && err == NULL)
2806 err = sync_err;
2807 done:
2808 free(fileindex_path);
2809 if (fileindex)
2810 got_fileindex_free(fileindex);
2811 unlockerr = lock_worktree(worktree, LOCK_SH);
2812 if (unlockerr && err == NULL)
2813 err = unlockerr;
2814 return err;
2817 static const struct got_error *
2818 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2820 const struct got_error *err = NULL;
2821 char *line = NULL;
2822 size_t linesize = 0, n;
2823 ssize_t linelen;
2825 linelen = getline(&line, &linesize, infile);
2826 if (linelen == -1) {
2827 if (ferror(infile)) {
2828 err = got_error_from_errno("getline");
2829 goto done;
2831 return NULL;
2833 if (outfile) {
2834 n = fwrite(line, 1, linelen, outfile);
2835 if (n != linelen) {
2836 err = got_ferror(outfile, GOT_ERR_IO);
2837 goto done;
2840 if (rejectfile) {
2841 n = fwrite(line, 1, linelen, rejectfile);
2842 if (n != linelen)
2843 err = got_ferror(outfile, GOT_ERR_IO);
2845 done:
2846 free(line);
2847 return err;
2850 static const struct got_error *
2851 skip_one_line(FILE *f)
2853 char *line = NULL;
2854 size_t linesize = 0;
2855 ssize_t linelen;
2857 linelen = getline(&line, &linesize, f);
2858 free(line);
2859 if (linelen == -1 && ferror(f))
2860 return got_error_from_errno("getline");
2861 return NULL;
2864 static const struct got_error *
2865 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2866 int start_old, int end_old, int start_new, int end_new,
2867 FILE *outfile, FILE *rejectfile)
2869 const struct got_error *err;
2871 /* Copy old file's lines leading up to patch. */
2872 while (!feof(f1) && *line_cur1 < start_old) {
2873 err = copy_one_line(f1, outfile, NULL);
2874 if (err)
2875 return err;
2876 (*line_cur1)++;
2878 /* Skip new file's lines leading up to patch. */
2879 while (!feof(f2) && *line_cur2 < start_new) {
2880 if (rejectfile)
2881 err = copy_one_line(f2, NULL, rejectfile);
2882 else
2883 err = skip_one_line(f2);
2884 if (err)
2885 return err;
2886 (*line_cur2)++;
2888 /* Copy patched lines. */
2889 while (!feof(f2) && *line_cur2 <= end_new) {
2890 err = copy_one_line(f2, outfile, NULL);
2891 if (err)
2892 return err;
2893 (*line_cur2)++;
2895 /* Skip over old file's replaced lines. */
2896 while (!feof(f1) && *line_cur1 <= end_old) {
2897 if (rejectfile)
2898 err = copy_one_line(f1, NULL, rejectfile);
2899 else
2900 err = skip_one_line(f1);
2901 if (err)
2902 return err;
2903 (*line_cur1)++;
2906 return NULL;
2909 static const struct got_error *
2910 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2911 FILE *outfile, FILE *rejectfile)
2913 const struct got_error *err;
2915 if (outfile) {
2916 /* Copy old file's lines until EOF. */
2917 while (!feof(f1)) {
2918 err = copy_one_line(f1, outfile, NULL);
2919 if (err)
2920 return err;
2921 (*line_cur1)++;
2924 if (rejectfile) {
2925 /* Copy new file's lines until EOF. */
2926 while (!feof(f2)) {
2927 err = copy_one_line(f2, NULL, rejectfile);
2928 if (err)
2929 return err;
2930 (*line_cur2)++;
2934 return NULL;
2937 static const struct got_error *
2938 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2939 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2940 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2941 int *line_cur2, FILE *outfile, FILE *rejectfile,
2942 got_worktree_patch_cb patch_cb, void *patch_arg)
2944 const struct got_error *err = NULL;
2945 int start_old = change->cv.a;
2946 int end_old = change->cv.b;
2947 int start_new = change->cv.c;
2948 int end_new = change->cv.d;
2949 long pos1, pos2;
2950 FILE *hunkfile;
2952 *choice = GOT_PATCH_CHOICE_NONE;
2954 hunkfile = got_opentemp();
2955 if (hunkfile == NULL)
2956 return got_error_from_errno("got_opentemp");
2958 pos1 = ftell(f1);
2959 pos2 = ftell(f2);
2961 /* XXX TODO needs error checking */
2962 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2964 if (fseek(f1, pos1, SEEK_SET) == -1) {
2965 err = got_ferror(f1, GOT_ERR_IO);
2966 goto done;
2968 if (fseek(f2, pos2, SEEK_SET) == -1) {
2969 err = got_ferror(f1, GOT_ERR_IO);
2970 goto done;
2972 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2973 err = got_ferror(hunkfile, GOT_ERR_IO);
2974 goto done;
2977 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2978 hunkfile, n, nchanges);
2979 if (err)
2980 goto done;
2982 switch (*choice) {
2983 case GOT_PATCH_CHOICE_YES:
2984 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2985 end_old, start_new, end_new, outfile, rejectfile);
2986 break;
2987 case GOT_PATCH_CHOICE_NO:
2988 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2989 end_old, start_new, end_new, rejectfile, outfile);
2990 break;
2991 case GOT_PATCH_CHOICE_QUIT:
2992 break;
2993 default:
2994 err = got_error(GOT_ERR_PATCH_CHOICE);
2995 break;
2997 done:
2998 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
2999 err = got_error_from_errno("fclose");
3000 return err;
3003 struct revert_file_args {
3004 struct got_worktree *worktree;
3005 struct got_fileindex *fileindex;
3006 got_worktree_checkout_cb progress_cb;
3007 void *progress_arg;
3008 got_worktree_patch_cb patch_cb;
3009 void *patch_arg;
3010 struct got_repository *repo;
3013 static const struct got_error *
3014 create_patched_content(char **path_outfile, int reverse_patch,
3015 struct got_object_id *blob_id, const char *path2,
3016 const char *relpath, struct got_repository *repo,
3017 got_worktree_patch_cb patch_cb, void *patch_arg)
3019 const struct got_error *err;
3020 struct got_blob_object *blob = NULL;
3021 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3022 char *path1 = NULL, *id_str = NULL;
3023 struct stat sb1, sb2;
3024 struct got_diff_changes *changes = NULL;
3025 struct got_diff_state *ds = NULL;
3026 struct got_diff_args *args = NULL;
3027 struct got_diff_change *change;
3028 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3029 int n = 0;
3031 *path_outfile = NULL;
3033 err = got_object_id_str(&id_str, blob_id);
3034 if (err)
3035 return err;
3037 f2 = fopen(path2, "r");
3038 if (f2 == NULL) {
3039 err = got_error_from_errno2("fopen", path2);
3040 goto done;
3043 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3044 if (err)
3045 goto done;
3047 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3048 if (err)
3049 goto done;
3051 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3052 if (err)
3053 goto done;
3055 if (stat(path1, &sb1) == -1) {
3056 err = got_error_from_errno2("stat", path1);
3057 goto done;
3059 if (stat(path2, &sb2) == -1) {
3060 err = got_error_from_errno2("stat", path2);
3061 goto done;
3064 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3065 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3066 if (err)
3067 goto done;
3069 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3070 if (err)
3071 goto done;
3073 if (fseek(f1, 0L, SEEK_SET) == -1)
3074 return got_ferror(f1, GOT_ERR_IO);
3075 if (fseek(f2, 0L, SEEK_SET) == -1)
3076 return got_ferror(f2, GOT_ERR_IO);
3077 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3078 int choice;
3079 err = apply_or_reject_change(&choice, change, ++n,
3080 changes->nchanges, ds, args, diff_flags, relpath,
3081 f1, f2, &line_cur1, &line_cur2,
3082 reverse_patch ? NULL : outfile,
3083 reverse_patch ? outfile : NULL,
3084 patch_cb, patch_arg);
3085 if (err)
3086 goto done;
3087 if (choice == GOT_PATCH_CHOICE_YES)
3088 have_content = 1;
3089 else if (choice == GOT_PATCH_CHOICE_QUIT)
3090 break;
3092 if (have_content)
3093 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3094 reverse_patch ? NULL : outfile,
3095 reverse_patch ? outfile : NULL);
3096 done:
3097 free(id_str);
3098 if (blob)
3099 got_object_blob_close(blob);
3100 if (f1 && fclose(f1) == EOF && err == NULL)
3101 err = got_error_from_errno2("fclose", path1);
3102 if (f2 && fclose(f2) == EOF && err == NULL)
3103 err = got_error_from_errno2("fclose", path2);
3104 if (outfile && fclose(outfile) == EOF && err == NULL)
3105 err = got_error_from_errno2("fclose", *path_outfile);
3106 if (path1 && unlink(path1) == -1 && err == NULL)
3107 err = got_error_from_errno2("unlink", path1);
3108 if (err || !have_content) {
3109 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3110 err = got_error_from_errno2("unlink", *path_outfile);
3111 free(*path_outfile);
3112 *path_outfile = NULL;
3114 free(args);
3115 if (ds) {
3116 got_diff_state_free(ds);
3117 free(ds);
3119 if (changes)
3120 got_diff_free_changes(changes);
3121 free(path1);
3122 return err;
3125 static const struct got_error *
3126 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3127 const char *relpath, struct got_object_id *blob_id,
3128 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3130 struct revert_file_args *a = arg;
3131 const struct got_error *err = NULL;
3132 char *parent_path = NULL;
3133 struct got_fileindex_entry *ie;
3134 struct got_tree_object *tree = NULL;
3135 struct got_object_id *tree_id = NULL;
3136 const struct got_tree_entry *te = NULL;
3137 char *tree_path = NULL, *te_name;
3138 char *ondisk_path = NULL, *path_content = NULL;
3139 struct got_blob_object *blob = NULL;
3141 /* Reverting a staged deletion is a no-op. */
3142 if (status == GOT_STATUS_DELETE &&
3143 staged_status != GOT_STATUS_NO_CHANGE)
3144 return NULL;
3146 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3147 if (ie == NULL)
3148 return got_error(GOT_ERR_BAD_PATH);
3150 /* Construct in-repository path of tree which contains this blob. */
3151 err = got_path_dirname(&parent_path, ie->path);
3152 if (err) {
3153 if (err->code != GOT_ERR_BAD_PATH)
3154 goto done;
3155 parent_path = strdup("/");
3156 if (parent_path == NULL) {
3157 err = got_error_from_errno("strdup");
3158 goto done;
3161 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3162 tree_path = strdup(parent_path);
3163 if (tree_path == NULL) {
3164 err = got_error_from_errno("strdup");
3165 goto done;
3167 } else {
3168 if (got_path_is_root_dir(parent_path)) {
3169 tree_path = strdup(a->worktree->path_prefix);
3170 if (tree_path == NULL) {
3171 err = got_error_from_errno("strdup");
3172 goto done;
3174 } else {
3175 if (asprintf(&tree_path, "%s/%s",
3176 a->worktree->path_prefix, parent_path) == -1) {
3177 err = got_error_from_errno("asprintf");
3178 goto done;
3183 err = got_object_id_by_path(&tree_id, a->repo,
3184 a->worktree->base_commit_id, tree_path);
3185 if (err) {
3186 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3187 (status == GOT_STATUS_ADD ||
3188 staged_status == GOT_STATUS_ADD)))
3189 goto done;
3190 } else {
3191 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3192 if (err)
3193 goto done;
3195 te_name = basename(ie->path);
3196 if (te_name == NULL) {
3197 err = got_error_from_errno2("basename", ie->path);
3198 goto done;
3201 te = got_object_tree_find_entry(tree, te_name);
3202 if (te == NULL && status != GOT_STATUS_ADD &&
3203 staged_status != GOT_STATUS_ADD) {
3204 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3205 goto done;
3209 switch (status) {
3210 case GOT_STATUS_ADD:
3211 if (a->patch_cb) {
3212 int choice = GOT_PATCH_CHOICE_NONE;
3213 err = (*a->patch_cb)(&choice, a->patch_arg,
3214 status, ie->path, NULL, 1, 1);
3215 if (err)
3216 goto done;
3217 if (choice != GOT_PATCH_CHOICE_YES)
3218 break;
3220 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3221 ie->path);
3222 if (err)
3223 goto done;
3224 got_fileindex_entry_remove(a->fileindex, ie);
3225 break;
3226 case GOT_STATUS_DELETE:
3227 if (a->patch_cb) {
3228 int choice = GOT_PATCH_CHOICE_NONE;
3229 err = (*a->patch_cb)(&choice, a->patch_arg,
3230 status, ie->path, NULL, 1, 1);
3231 if (err)
3232 goto done;
3233 if (choice != GOT_PATCH_CHOICE_YES)
3234 break;
3236 /* fall through */
3237 case GOT_STATUS_MODIFY:
3238 case GOT_STATUS_CONFLICT:
3239 case GOT_STATUS_MISSING: {
3240 struct got_object_id id;
3241 if (staged_status == GOT_STATUS_ADD ||
3242 staged_status == GOT_STATUS_MODIFY) {
3243 memcpy(id.sha1, ie->staged_blob_sha1,
3244 SHA1_DIGEST_LENGTH);
3245 } else
3246 memcpy(id.sha1, ie->blob_sha1,
3247 SHA1_DIGEST_LENGTH);
3248 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3249 if (err)
3250 goto done;
3252 if (asprintf(&ondisk_path, "%s/%s",
3253 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3254 err = got_error_from_errno("asprintf");
3255 goto done;
3258 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3259 status == GOT_STATUS_CONFLICT)) {
3260 err = create_patched_content(&path_content, 1, &id,
3261 ondisk_path, ie->path, a->repo,
3262 a->patch_cb, a->patch_arg);
3263 if (err || path_content == NULL)
3264 break;
3265 if (rename(path_content, ondisk_path) == -1) {
3266 err = got_error_from_errno3("rename",
3267 path_content, ondisk_path);
3268 goto done;
3270 } else {
3271 err = install_blob(a->worktree, ondisk_path, ie->path,
3272 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3273 got_fileindex_perms_to_st(ie), blob, 0, 1,
3274 a->repo, a->progress_cb, a->progress_arg);
3275 if (err)
3276 goto done;
3277 if (status == GOT_STATUS_DELETE) {
3278 err = update_blob_fileindex_entry(a->worktree,
3279 a->fileindex, ie, ondisk_path, ie->path,
3280 blob, 1);
3281 if (err)
3282 goto done;
3285 break;
3287 default:
3288 break;
3290 done:
3291 free(ondisk_path);
3292 free(path_content);
3293 free(parent_path);
3294 free(tree_path);
3295 if (blob)
3296 got_object_blob_close(blob);
3297 if (tree)
3298 got_object_tree_close(tree);
3299 free(tree_id);
3300 return err;
3303 const struct got_error *
3304 got_worktree_revert(struct got_worktree *worktree,
3305 struct got_pathlist_head *paths,
3306 got_worktree_checkout_cb progress_cb, void *progress_arg,
3307 got_worktree_patch_cb patch_cb, void *patch_arg,
3308 struct got_repository *repo)
3310 struct got_fileindex *fileindex = NULL;
3311 char *fileindex_path = NULL;
3312 const struct got_error *err = NULL, *unlockerr = NULL;
3313 const struct got_error *sync_err = NULL;
3314 struct got_pathlist_entry *pe;
3315 struct revert_file_args rfa;
3317 err = lock_worktree(worktree, LOCK_EX);
3318 if (err)
3319 return err;
3321 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3322 if (err)
3323 goto done;
3325 rfa.worktree = worktree;
3326 rfa.fileindex = fileindex;
3327 rfa.progress_cb = progress_cb;
3328 rfa.progress_arg = progress_arg;
3329 rfa.patch_cb = patch_cb;
3330 rfa.patch_arg = patch_arg;
3331 rfa.repo = repo;
3332 TAILQ_FOREACH(pe, paths, entry) {
3333 err = worktree_status(worktree, pe->path, fileindex, repo,
3334 revert_file, &rfa, NULL, NULL);
3335 if (err)
3336 break;
3338 sync_err = sync_fileindex(fileindex, fileindex_path);
3339 if (sync_err && err == NULL)
3340 err = sync_err;
3341 done:
3342 free(fileindex_path);
3343 if (fileindex)
3344 got_fileindex_free(fileindex);
3345 unlockerr = lock_worktree(worktree, LOCK_SH);
3346 if (unlockerr && err == NULL)
3347 err = unlockerr;
3348 return err;
3351 static void
3352 free_commitable(struct got_commitable *ct)
3354 free(ct->path);
3355 free(ct->in_repo_path);
3356 free(ct->ondisk_path);
3357 free(ct->blob_id);
3358 free(ct->base_blob_id);
3359 free(ct->staged_blob_id);
3360 free(ct->base_commit_id);
3361 free(ct);
3364 struct collect_commitables_arg {
3365 struct got_pathlist_head *commitable_paths;
3366 struct got_repository *repo;
3367 struct got_worktree *worktree;
3368 int have_staged_files;
3371 static const struct got_error *
3372 collect_commitables(void *arg, unsigned char status,
3373 unsigned char staged_status, const char *relpath,
3374 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3375 struct got_object_id *commit_id)
3377 struct collect_commitables_arg *a = arg;
3378 const struct got_error *err = NULL;
3379 struct got_commitable *ct = NULL;
3380 struct got_pathlist_entry *new = NULL;
3381 char *parent_path = NULL, *path = NULL;
3382 struct stat sb;
3384 if (a->have_staged_files) {
3385 if (staged_status != GOT_STATUS_MODIFY &&
3386 staged_status != GOT_STATUS_ADD &&
3387 staged_status != GOT_STATUS_DELETE)
3388 return NULL;
3389 } else {
3390 if (status == GOT_STATUS_CONFLICT)
3391 return got_error(GOT_ERR_COMMIT_CONFLICT);
3393 if (status != GOT_STATUS_MODIFY &&
3394 status != GOT_STATUS_ADD &&
3395 status != GOT_STATUS_DELETE)
3396 return NULL;
3399 if (asprintf(&path, "/%s", relpath) == -1) {
3400 err = got_error_from_errno("asprintf");
3401 goto done;
3403 if (strcmp(path, "/") == 0) {
3404 parent_path = strdup("");
3405 if (parent_path == NULL)
3406 return got_error_from_errno("strdup");
3407 } else {
3408 err = got_path_dirname(&parent_path, path);
3409 if (err)
3410 return err;
3413 ct = calloc(1, sizeof(*ct));
3414 if (ct == NULL) {
3415 err = got_error_from_errno("calloc");
3416 goto done;
3419 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3420 relpath) == -1) {
3421 err = got_error_from_errno("asprintf");
3422 goto done;
3424 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3425 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3426 } else {
3427 if (lstat(ct->ondisk_path, &sb) != 0) {
3428 err = got_error_from_errno2("lstat", ct->ondisk_path);
3429 goto done;
3431 ct->mode = sb.st_mode;
3434 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3435 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3436 relpath) == -1) {
3437 err = got_error_from_errno("asprintf");
3438 goto done;
3441 ct->status = status;
3442 ct->staged_status = staged_status;
3443 ct->blob_id = NULL; /* will be filled in when blob gets created */
3444 if (ct->status != GOT_STATUS_ADD &&
3445 ct->staged_status != GOT_STATUS_ADD) {
3446 ct->base_blob_id = got_object_id_dup(blob_id);
3447 if (ct->base_blob_id == NULL) {
3448 err = got_error_from_errno("got_object_id_dup");
3449 goto done;
3451 ct->base_commit_id = got_object_id_dup(commit_id);
3452 if (ct->base_commit_id == NULL) {
3453 err = got_error_from_errno("got_object_id_dup");
3454 goto done;
3457 if (ct->staged_status == GOT_STATUS_ADD ||
3458 ct->staged_status == GOT_STATUS_MODIFY) {
3459 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3460 if (ct->staged_blob_id == NULL) {
3461 err = got_error_from_errno("got_object_id_dup");
3462 goto done;
3465 ct->path = strdup(path);
3466 if (ct->path == NULL) {
3467 err = got_error_from_errno("strdup");
3468 goto done;
3470 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3471 done:
3472 if (ct && (err || new == NULL))
3473 free_commitable(ct);
3474 free(parent_path);
3475 free(path);
3476 return err;
3479 static const struct got_error *write_tree(struct got_object_id **,
3480 struct got_tree_object *, const char *, struct got_pathlist_head *,
3481 got_worktree_status_cb status_cb, void *status_arg,
3482 struct got_repository *);
3484 static const struct got_error *
3485 write_subtree(struct got_object_id **new_subtree_id,
3486 struct got_tree_entry *te, const char *parent_path,
3487 struct got_pathlist_head *commitable_paths,
3488 got_worktree_status_cb status_cb, void *status_arg,
3489 struct got_repository *repo)
3491 const struct got_error *err = NULL;
3492 struct got_tree_object *subtree;
3493 char *subpath;
3495 if (asprintf(&subpath, "%s%s%s", parent_path,
3496 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3497 return got_error_from_errno("asprintf");
3499 err = got_object_open_as_tree(&subtree, repo, te->id);
3500 if (err)
3501 return err;
3503 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3504 status_cb, status_arg, repo);
3505 got_object_tree_close(subtree);
3506 free(subpath);
3507 return err;
3510 static const struct got_error *
3511 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3513 const struct got_error *err = NULL;
3514 char *ct_parent_path = NULL;
3516 *match = 0;
3518 if (strchr(ct->in_repo_path, '/') == NULL) {
3519 *match = got_path_is_root_dir(path);
3520 return NULL;
3523 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3524 if (err)
3525 return err;
3526 *match = (strcmp(path, ct_parent_path) == 0);
3527 free(ct_parent_path);
3528 return err;
3531 static mode_t
3532 get_ct_file_mode(struct got_commitable *ct)
3534 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3537 static const struct got_error *
3538 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3539 struct got_tree_entry *te, struct got_commitable *ct)
3541 const struct got_error *err = NULL;
3543 *new_te = NULL;
3545 err = got_object_tree_entry_dup(new_te, te);
3546 if (err)
3547 goto done;
3549 (*new_te)->mode = get_ct_file_mode(ct);
3551 free((*new_te)->id);
3552 if (ct->staged_status == GOT_STATUS_MODIFY)
3553 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3554 else
3555 (*new_te)->id = got_object_id_dup(ct->blob_id);
3556 if ((*new_te)->id == NULL) {
3557 err = got_error_from_errno("got_object_id_dup");
3558 goto done;
3560 done:
3561 if (err && *new_te) {
3562 got_object_tree_entry_close(*new_te);
3563 *new_te = NULL;
3565 return err;
3568 static const struct got_error *
3569 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3570 struct got_commitable *ct)
3572 const struct got_error *err = NULL;
3573 char *ct_name;
3575 *new_te = NULL;
3577 *new_te = calloc(1, sizeof(**new_te));
3578 if (*new_te == NULL)
3579 return got_error_from_errno("calloc");
3581 ct_name = basename(ct->path);
3582 if (ct_name == NULL) {
3583 err = got_error_from_errno2("basename", ct->path);
3584 goto done;
3586 (*new_te)->name = strdup(ct_name);
3587 if ((*new_te)->name == NULL) {
3588 err = got_error_from_errno("strdup");
3589 goto done;
3592 (*new_te)->mode = get_ct_file_mode(ct);
3594 if (ct->staged_status == GOT_STATUS_ADD)
3595 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3596 else
3597 (*new_te)->id = got_object_id_dup(ct->blob_id);
3598 if ((*new_te)->id == NULL) {
3599 err = got_error_from_errno("got_object_id_dup");
3600 goto done;
3602 done:
3603 if (err && *new_te) {
3604 got_object_tree_entry_close(*new_te);
3605 *new_te = NULL;
3607 return err;
3610 static const struct got_error *
3611 insert_tree_entry(struct got_tree_entry *new_te,
3612 struct got_pathlist_head *paths)
3614 const struct got_error *err = NULL;
3615 struct got_pathlist_entry *new_pe;
3617 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3618 if (err)
3619 return err;
3620 if (new_pe == NULL)
3621 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3622 return NULL;
3625 static const struct got_error *
3626 report_ct_status(struct got_commitable *ct,
3627 got_worktree_status_cb status_cb, void *status_arg)
3629 const char *ct_path = ct->path;
3630 unsigned char status;
3632 while (ct_path[0] == '/')
3633 ct_path++;
3635 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3636 status = ct->staged_status;
3637 else
3638 status = ct->status;
3640 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3641 ct_path, ct->blob_id, NULL, NULL);
3644 static const struct got_error *
3645 match_modified_subtree(int *modified, struct got_tree_entry *te,
3646 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3648 const struct got_error *err = NULL;
3649 struct got_pathlist_entry *pe;
3650 char *te_path;
3652 *modified = 0;
3654 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3655 got_path_is_root_dir(base_tree_path) ? "" : "/",
3656 te->name) == -1)
3657 return got_error_from_errno("asprintf");
3659 TAILQ_FOREACH(pe, commitable_paths, entry) {
3660 struct got_commitable *ct = pe->data;
3661 *modified = got_path_is_child(ct->in_repo_path, te_path,
3662 strlen(te_path));
3663 if (*modified)
3664 break;
3667 free(te_path);
3668 return err;
3671 static const struct got_error *
3672 match_deleted_or_modified_ct(struct got_commitable **ctp,
3673 struct got_tree_entry *te, const char *base_tree_path,
3674 struct got_pathlist_head *commitable_paths)
3676 const struct got_error *err = NULL;
3677 struct got_pathlist_entry *pe;
3679 *ctp = NULL;
3681 TAILQ_FOREACH(pe, commitable_paths, entry) {
3682 struct got_commitable *ct = pe->data;
3683 char *ct_name = NULL;
3684 int path_matches;
3686 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3687 if (ct->status != GOT_STATUS_MODIFY &&
3688 ct->status != GOT_STATUS_DELETE)
3689 continue;
3690 } else {
3691 if (ct->staged_status != GOT_STATUS_MODIFY &&
3692 ct->staged_status != GOT_STATUS_DELETE)
3693 continue;
3696 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3697 continue;
3699 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3700 if (err)
3701 return err;
3702 if (!path_matches)
3703 continue;
3705 ct_name = basename(pe->path);
3706 if (ct_name == NULL)
3707 return got_error_from_errno2("basename", pe->path);
3709 if (strcmp(te->name, ct_name) != 0)
3710 continue;
3712 *ctp = ct;
3713 break;
3716 return err;
3719 static const struct got_error *
3720 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3721 const char *child_path, const char *path_base_tree,
3722 struct got_pathlist_head *commitable_paths,
3723 got_worktree_status_cb status_cb, void *status_arg,
3724 struct got_repository *repo)
3726 const struct got_error *err = NULL;
3727 struct got_tree_entry *new_te;
3728 char *subtree_path;
3730 *new_tep = NULL;
3732 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3733 got_path_is_root_dir(path_base_tree) ? "" : "/",
3734 child_path) == -1)
3735 return got_error_from_errno("asprintf");
3737 new_te = calloc(1, sizeof(*new_te));
3738 new_te->mode = S_IFDIR;
3739 new_te->name = strdup(child_path);
3740 if (new_te->name == NULL) {
3741 err = got_error_from_errno("strdup");
3742 got_object_tree_entry_close(new_te);
3743 goto done;
3745 err = write_tree(&new_te->id, NULL, subtree_path,
3746 commitable_paths, status_cb, status_arg, repo);
3747 if (err) {
3748 got_object_tree_entry_close(new_te);
3749 goto done;
3751 done:
3752 free(subtree_path);
3753 if (err == NULL)
3754 *new_tep = new_te;
3755 return err;
3758 static const struct got_error *
3759 write_tree(struct got_object_id **new_tree_id,
3760 struct got_tree_object *base_tree, const char *path_base_tree,
3761 struct got_pathlist_head *commitable_paths,
3762 got_worktree_status_cb status_cb, void *status_arg,
3763 struct got_repository *repo)
3765 const struct got_error *err = NULL;
3766 const struct got_tree_entries *base_entries = NULL;
3767 struct got_pathlist_head paths;
3768 struct got_tree_entries new_tree_entries;
3769 struct got_tree_entry *te, *new_te = NULL;
3770 struct got_pathlist_entry *pe;
3772 TAILQ_INIT(&paths);
3773 new_tree_entries.nentries = 0;
3774 SIMPLEQ_INIT(&new_tree_entries.head);
3776 /* Insert, and recurse into, newly added entries first. */
3777 TAILQ_FOREACH(pe, commitable_paths, entry) {
3778 struct got_commitable *ct = pe->data;
3779 char *child_path = NULL, *slash;
3781 if ((ct->status != GOT_STATUS_ADD &&
3782 ct->staged_status != GOT_STATUS_ADD) ||
3783 (ct->flags & GOT_COMMITABLE_ADDED))
3784 continue;
3786 if (!got_path_is_child(pe->path, path_base_tree,
3787 strlen(path_base_tree)))
3788 continue;
3790 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3791 pe->path);
3792 if (err)
3793 goto done;
3795 slash = strchr(child_path, '/');
3796 if (slash == NULL) {
3797 err = alloc_added_blob_tree_entry(&new_te, ct);
3798 if (err)
3799 goto done;
3800 err = report_ct_status(ct, status_cb, status_arg);
3801 if (err)
3802 goto done;
3803 ct->flags |= GOT_COMMITABLE_ADDED;
3804 err = insert_tree_entry(new_te, &paths);
3805 if (err)
3806 goto done;
3807 } else {
3808 *slash = '\0'; /* trim trailing path components */
3809 if (base_tree == NULL ||
3810 got_object_tree_find_entry(base_tree, child_path)
3811 == NULL) {
3812 err = make_subtree_for_added_blob(&new_te,
3813 child_path, path_base_tree,
3814 commitable_paths, status_cb, status_arg,
3815 repo);
3816 if (err)
3817 goto done;
3818 err = insert_tree_entry(new_te, &paths);
3819 if (err)
3820 goto done;
3825 if (base_tree) {
3826 /* Handle modified and deleted entries. */
3827 base_entries = got_object_tree_get_entries(base_tree);
3828 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3829 struct got_commitable *ct = NULL;
3831 if (S_ISDIR(te->mode)) {
3832 int modified;
3833 err = got_object_tree_entry_dup(&new_te, te);
3834 if (err)
3835 goto done;
3836 err = match_modified_subtree(&modified, te,
3837 path_base_tree, commitable_paths);
3838 if (err)
3839 goto done;
3840 /* Avoid recursion into unmodified subtrees. */
3841 if (modified) {
3842 free(new_te->id);
3843 err = write_subtree(&new_te->id, te,
3844 path_base_tree, commitable_paths,
3845 status_cb, status_arg, repo);
3846 if (err)
3847 goto done;
3849 err = insert_tree_entry(new_te, &paths);
3850 if (err)
3851 goto done;
3852 continue;
3855 err = match_deleted_or_modified_ct(&ct, te,
3856 path_base_tree, commitable_paths);
3857 if (ct) {
3858 /* NB: Deleted entries get dropped here. */
3859 if (ct->status == GOT_STATUS_MODIFY ||
3860 ct->staged_status == GOT_STATUS_MODIFY) {
3861 err = alloc_modified_blob_tree_entry(
3862 &new_te, te, ct);
3863 if (err)
3864 goto done;
3865 err = insert_tree_entry(new_te, &paths);
3866 if (err)
3867 goto done;
3869 err = report_ct_status(ct, status_cb,
3870 status_arg);
3871 if (err)
3872 goto done;
3873 } else {
3874 /* Entry is unchanged; just copy it. */
3875 err = got_object_tree_entry_dup(&new_te, te);
3876 if (err)
3877 goto done;
3878 err = insert_tree_entry(new_te, &paths);
3879 if (err)
3880 goto done;
3885 /* Write new list of entries; deleted entries have been dropped. */
3886 TAILQ_FOREACH(pe, &paths, entry) {
3887 struct got_tree_entry *te = pe->data;
3888 new_tree_entries.nentries++;
3889 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3891 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3892 done:
3893 got_object_tree_entries_close(&new_tree_entries);
3894 got_pathlist_free(&paths);
3895 return err;
3898 static const struct got_error *
3899 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3900 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3902 const struct got_error *err = NULL;
3903 struct got_pathlist_entry *pe;
3905 TAILQ_FOREACH(pe, commitable_paths, entry) {
3906 struct got_fileindex_entry *ie;
3907 struct got_commitable *ct = pe->data;
3909 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3910 if (ie) {
3911 if (ct->status == GOT_STATUS_DELETE ||
3912 ct->staged_status == GOT_STATUS_DELETE) {
3913 got_fileindex_entry_remove(fileindex, ie);
3914 got_fileindex_entry_free(ie);
3915 } else if (ct->staged_status == GOT_STATUS_ADD ||
3916 ct->staged_status == GOT_STATUS_MODIFY) {
3917 got_fileindex_entry_stage_set(ie,
3918 GOT_FILEIDX_STAGE_NONE);
3919 err = got_fileindex_entry_update(ie,
3920 ct->ondisk_path, ct->staged_blob_id->sha1,
3921 new_base_commit_id->sha1, 1);
3922 } else
3923 err = got_fileindex_entry_update(ie,
3924 ct->ondisk_path, ct->blob_id->sha1,
3925 new_base_commit_id->sha1, 1);
3926 } else {
3927 err = got_fileindex_entry_alloc(&ie,
3928 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3929 new_base_commit_id->sha1);
3930 if (err)
3931 break;
3932 err = got_fileindex_entry_add(fileindex, ie);
3933 if (err)
3934 break;
3937 return err;
3941 static const struct got_error *
3942 check_out_of_date(const char *in_repo_path, unsigned char status,
3943 unsigned char staged_status, struct got_object_id *base_blob_id,
3944 struct got_object_id *base_commit_id,
3945 struct got_object_id *head_commit_id, struct got_repository *repo,
3946 int ood_errcode)
3948 const struct got_error *err = NULL;
3949 struct got_object_id *id = NULL;
3951 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3952 /* Trivial case: base commit == head commit */
3953 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3954 return NULL;
3956 * Ensure file content which local changes were based
3957 * on matches file content in the branch head.
3959 err = got_object_id_by_path(&id, repo, head_commit_id,
3960 in_repo_path);
3961 if (err) {
3962 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3963 err = got_error(ood_errcode);
3964 goto done;
3965 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3966 err = got_error(ood_errcode);
3967 } else {
3968 /* Require that added files don't exist in the branch head. */
3969 err = got_object_id_by_path(&id, repo, head_commit_id,
3970 in_repo_path);
3971 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3972 goto done;
3973 err = id ? got_error(ood_errcode) : NULL;
3975 done:
3976 free(id);
3977 return err;
3980 const struct got_error *
3981 commit_worktree(struct got_object_id **new_commit_id,
3982 struct got_pathlist_head *commitable_paths,
3983 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3984 const char *author, const char *committer,
3985 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3986 got_worktree_status_cb status_cb, void *status_arg,
3987 struct got_repository *repo)
3989 const struct got_error *err = NULL, *unlockerr = NULL;
3990 struct got_pathlist_entry *pe;
3991 const char *head_ref_name = NULL;
3992 struct got_commit_object *head_commit = NULL;
3993 struct got_reference *head_ref2 = NULL;
3994 struct got_object_id *head_commit_id2 = NULL;
3995 struct got_tree_object *head_tree = NULL;
3996 struct got_object_id *new_tree_id = NULL;
3997 struct got_object_id_queue parent_ids;
3998 struct got_object_qid *pid = NULL;
3999 char *logmsg = NULL;
4001 *new_commit_id = NULL;
4003 SIMPLEQ_INIT(&parent_ids);
4005 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4006 if (err)
4007 goto done;
4009 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4010 if (err)
4011 goto done;
4013 if (commit_msg_cb != NULL) {
4014 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4015 if (err)
4016 goto done;
4019 if (logmsg == NULL || strlen(logmsg) == 0) {
4020 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4021 goto done;
4024 /* Create blobs from added and modified files and record their IDs. */
4025 TAILQ_FOREACH(pe, commitable_paths, entry) {
4026 struct got_commitable *ct = pe->data;
4027 char *ondisk_path;
4029 /* Blobs for staged files already exist. */
4030 if (ct->staged_status == GOT_STATUS_ADD ||
4031 ct->staged_status == GOT_STATUS_MODIFY)
4032 continue;
4034 if (ct->status != GOT_STATUS_ADD &&
4035 ct->status != GOT_STATUS_MODIFY)
4036 continue;
4038 if (asprintf(&ondisk_path, "%s/%s",
4039 worktree->root_path, pe->path) == -1) {
4040 err = got_error_from_errno("asprintf");
4041 goto done;
4043 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4044 free(ondisk_path);
4045 if (err)
4046 goto done;
4049 /* Recursively write new tree objects. */
4050 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4051 status_cb, status_arg, repo);
4052 if (err)
4053 goto done;
4055 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4056 if (err)
4057 goto done;
4058 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4059 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4060 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4061 got_object_qid_free(pid);
4062 if (logmsg != NULL)
4063 free(logmsg);
4064 if (err)
4065 goto done;
4067 /* Check if a concurrent commit to our branch has occurred. */
4068 head_ref_name = got_worktree_get_head_ref_name(worktree);
4069 if (head_ref_name == NULL) {
4070 err = got_error_from_errno("got_worktree_get_head_ref_name");
4071 goto done;
4073 /* Lock the reference here to prevent concurrent modification. */
4074 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4075 if (err)
4076 goto done;
4077 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4078 if (err)
4079 goto done;
4080 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4081 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4082 goto done;
4084 /* Update branch head in repository. */
4085 err = got_ref_change_ref(head_ref2, *new_commit_id);
4086 if (err)
4087 goto done;
4088 err = got_ref_write(head_ref2, repo);
4089 if (err)
4090 goto done;
4092 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4093 if (err)
4094 goto done;
4096 err = ref_base_commit(worktree, repo);
4097 if (err)
4098 goto done;
4099 done:
4100 if (head_tree)
4101 got_object_tree_close(head_tree);
4102 if (head_commit)
4103 got_object_commit_close(head_commit);
4104 free(head_commit_id2);
4105 if (head_ref2) {
4106 unlockerr = got_ref_unlock(head_ref2);
4107 if (unlockerr && err == NULL)
4108 err = unlockerr;
4109 got_ref_close(head_ref2);
4111 return err;
4114 static const struct got_error *
4115 check_path_is_commitable(const char *path,
4116 struct got_pathlist_head *commitable_paths)
4118 struct got_pathlist_entry *cpe = NULL;
4119 size_t path_len = strlen(path);
4121 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4122 struct got_commitable *ct = cpe->data;
4123 const char *ct_path = ct->path;
4125 while (ct_path[0] == '/')
4126 ct_path++;
4128 if (strcmp(path, ct_path) == 0 ||
4129 got_path_is_child(ct_path, path, path_len))
4130 break;
4133 if (cpe == NULL)
4134 return got_error_path(path, GOT_ERR_BAD_PATH);
4136 return NULL;
4139 static const struct got_error *
4140 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4142 int *have_staged_files = arg;
4144 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4145 *have_staged_files = 1;
4146 return got_error(GOT_ERR_CANCELLED);
4149 return NULL;
4152 static const struct got_error *
4153 check_non_staged_files(struct got_fileindex *fileindex,
4154 struct got_pathlist_head *paths)
4156 struct got_pathlist_entry *pe;
4157 struct got_fileindex_entry *ie;
4159 TAILQ_FOREACH(pe, paths, entry) {
4160 if (pe->path[0] == '\0')
4161 continue;
4162 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4163 if (ie == NULL)
4164 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4165 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4166 return got_error_path(pe->path,
4167 GOT_ERR_FILE_NOT_STAGED);
4170 return NULL;
4173 const struct got_error *
4174 got_worktree_commit(struct got_object_id **new_commit_id,
4175 struct got_worktree *worktree, struct got_pathlist_head *paths,
4176 const char *author, const char *committer,
4177 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4178 got_worktree_status_cb status_cb, void *status_arg,
4179 struct got_repository *repo)
4181 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4182 struct got_fileindex *fileindex = NULL;
4183 char *fileindex_path = NULL;
4184 struct got_pathlist_head commitable_paths;
4185 struct collect_commitables_arg cc_arg;
4186 struct got_pathlist_entry *pe;
4187 struct got_reference *head_ref = NULL;
4188 struct got_object_id *head_commit_id = NULL;
4189 int have_staged_files = 0;
4191 *new_commit_id = NULL;
4193 TAILQ_INIT(&commitable_paths);
4195 err = lock_worktree(worktree, LOCK_EX);
4196 if (err)
4197 goto done;
4199 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4200 if (err)
4201 goto done;
4203 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4204 if (err)
4205 goto done;
4207 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4208 if (err)
4209 goto done;
4211 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4212 &have_staged_files);
4213 if (err && err->code != GOT_ERR_CANCELLED)
4214 goto done;
4215 if (have_staged_files) {
4216 err = check_non_staged_files(fileindex, paths);
4217 if (err)
4218 goto done;
4221 cc_arg.commitable_paths = &commitable_paths;
4222 cc_arg.worktree = worktree;
4223 cc_arg.repo = repo;
4224 cc_arg.have_staged_files = have_staged_files;
4225 TAILQ_FOREACH(pe, paths, entry) {
4226 err = worktree_status(worktree, pe->path, fileindex, repo,
4227 collect_commitables, &cc_arg, NULL, NULL);
4228 if (err)
4229 goto done;
4232 if (TAILQ_EMPTY(&commitable_paths)) {
4233 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4234 goto done;
4237 TAILQ_FOREACH(pe, paths, entry) {
4238 err = check_path_is_commitable(pe->path, &commitable_paths);
4239 if (err)
4240 goto done;
4243 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4244 struct got_commitable *ct = pe->data;
4245 const char *ct_path = ct->in_repo_path;
4247 while (ct_path[0] == '/')
4248 ct_path++;
4249 err = check_out_of_date(ct_path, ct->status,
4250 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4251 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4252 if (err)
4253 goto done;
4257 err = commit_worktree(new_commit_id, &commitable_paths,
4258 head_commit_id, worktree, author, committer,
4259 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4260 if (err)
4261 goto done;
4263 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4264 fileindex);
4265 sync_err = sync_fileindex(fileindex, fileindex_path);
4266 if (sync_err && err == NULL)
4267 err = sync_err;
4268 done:
4269 if (fileindex)
4270 got_fileindex_free(fileindex);
4271 free(fileindex_path);
4272 unlockerr = lock_worktree(worktree, LOCK_SH);
4273 if (unlockerr && err == NULL)
4274 err = unlockerr;
4275 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4276 struct got_commitable *ct = pe->data;
4277 free_commitable(ct);
4279 got_pathlist_free(&commitable_paths);
4280 return err;
4283 const char *
4284 got_commitable_get_path(struct got_commitable *ct)
4286 return ct->path;
4289 unsigned int
4290 got_commitable_get_status(struct got_commitable *ct)
4292 return ct->status;
4295 struct check_rebase_ok_arg {
4296 struct got_worktree *worktree;
4297 struct got_repository *repo;
4300 static const struct got_error *
4301 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4303 const struct got_error *err = NULL;
4304 struct check_rebase_ok_arg *a = arg;
4305 unsigned char status;
4306 struct stat sb;
4307 char *ondisk_path;
4309 /* Reject rebase of a work tree with mixed base commits. */
4310 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4311 SHA1_DIGEST_LENGTH))
4312 return got_error(GOT_ERR_MIXED_COMMITS);
4314 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4315 == -1)
4316 return got_error_from_errno("asprintf");
4318 /* Reject rebase of a work tree with modified or staged files. */
4319 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4320 free(ondisk_path);
4321 if (err)
4322 return err;
4324 if (status != GOT_STATUS_NO_CHANGE)
4325 return got_error(GOT_ERR_MODIFIED);
4326 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4327 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4329 return NULL;
4332 const struct got_error *
4333 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4334 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4335 struct got_worktree *worktree, struct got_reference *branch,
4336 struct got_repository *repo)
4338 const struct got_error *err = NULL;
4339 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4340 char *branch_ref_name = NULL;
4341 char *fileindex_path = NULL;
4342 struct check_rebase_ok_arg ok_arg;
4343 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4345 *new_base_branch_ref = NULL;
4346 *tmp_branch = NULL;
4347 *fileindex = NULL;
4349 err = lock_worktree(worktree, LOCK_EX);
4350 if (err)
4351 return err;
4353 err = open_fileindex(fileindex, &fileindex_path, worktree);
4354 if (err)
4355 goto done;
4357 ok_arg.worktree = worktree;
4358 ok_arg.repo = repo;
4359 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4360 &ok_arg);
4361 if (err)
4362 goto done;
4364 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4365 if (err)
4366 goto done;
4368 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4369 if (err)
4370 goto done;
4372 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4373 if (err)
4374 goto done;
4376 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4377 0);
4378 if (err)
4379 goto done;
4381 err = got_ref_alloc_symref(new_base_branch_ref,
4382 new_base_branch_ref_name, wt_branch);
4383 if (err)
4384 goto done;
4385 err = got_ref_write(*new_base_branch_ref, repo);
4386 if (err)
4387 goto done;
4389 /* TODO Lock original branch's ref while rebasing? */
4391 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4392 if (err)
4393 goto done;
4395 err = got_ref_write(branch_ref, repo);
4396 if (err)
4397 goto done;
4399 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4400 worktree->base_commit_id);
4401 if (err)
4402 goto done;
4403 err = got_ref_write(*tmp_branch, repo);
4404 if (err)
4405 goto done;
4407 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4408 if (err)
4409 goto done;
4410 done:
4411 free(fileindex_path);
4412 free(tmp_branch_name);
4413 free(new_base_branch_ref_name);
4414 free(branch_ref_name);
4415 if (branch_ref)
4416 got_ref_close(branch_ref);
4417 if (wt_branch)
4418 got_ref_close(wt_branch);
4419 if (err) {
4420 if (*new_base_branch_ref) {
4421 got_ref_close(*new_base_branch_ref);
4422 *new_base_branch_ref = NULL;
4424 if (*tmp_branch) {
4425 got_ref_close(*tmp_branch);
4426 *tmp_branch = NULL;
4428 if (*fileindex) {
4429 got_fileindex_free(*fileindex);
4430 *fileindex = NULL;
4432 lock_worktree(worktree, LOCK_SH);
4434 return err;
4437 const struct got_error *
4438 got_worktree_rebase_continue(struct got_object_id **commit_id,
4439 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4440 struct got_reference **branch, struct got_fileindex **fileindex,
4441 struct got_worktree *worktree, struct got_repository *repo)
4443 const struct got_error *err;
4444 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4445 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4446 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4447 char *fileindex_path = NULL;
4448 int have_staged_files = 0;
4450 *commit_id = NULL;
4451 *new_base_branch = NULL;
4452 *tmp_branch = NULL;
4453 *branch = NULL;
4454 *fileindex = NULL;
4456 err = lock_worktree(worktree, LOCK_EX);
4457 if (err)
4458 return err;
4460 err = open_fileindex(fileindex, &fileindex_path, worktree);
4461 if (err)
4462 goto done;
4464 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4465 &have_staged_files);
4466 if (err && err->code != GOT_ERR_CANCELLED)
4467 goto done;
4468 if (have_staged_files) {
4469 err = got_error(GOT_ERR_STAGED_PATHS);
4470 goto done;
4473 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4474 if (err)
4475 goto done;
4477 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4478 if (err)
4479 goto done;
4481 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4482 if (err)
4483 goto done;
4485 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4486 if (err)
4487 goto done;
4489 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4490 if (err)
4491 goto done;
4493 err = got_ref_open(branch, repo,
4494 got_ref_get_symref_target(branch_ref), 0);
4495 if (err)
4496 goto done;
4498 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4499 if (err)
4500 goto done;
4502 err = got_ref_resolve(commit_id, repo, commit_ref);
4503 if (err)
4504 goto done;
4506 err = got_ref_open(new_base_branch, repo,
4507 new_base_branch_ref_name, 0);
4508 if (err)
4509 goto done;
4511 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4512 if (err)
4513 goto done;
4514 done:
4515 free(commit_ref_name);
4516 free(branch_ref_name);
4517 free(fileindex_path);
4518 if (commit_ref)
4519 got_ref_close(commit_ref);
4520 if (branch_ref)
4521 got_ref_close(branch_ref);
4522 if (err) {
4523 free(*commit_id);
4524 *commit_id = NULL;
4525 if (*tmp_branch) {
4526 got_ref_close(*tmp_branch);
4527 *tmp_branch = NULL;
4529 if (*new_base_branch) {
4530 got_ref_close(*new_base_branch);
4531 *new_base_branch = NULL;
4533 if (*branch) {
4534 got_ref_close(*branch);
4535 *branch = NULL;
4537 if (*fileindex) {
4538 got_fileindex_free(*fileindex);
4539 *fileindex = NULL;
4541 lock_worktree(worktree, LOCK_SH);
4543 return err;
4546 const struct got_error *
4547 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4549 const struct got_error *err;
4550 char *tmp_branch_name = NULL;
4552 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4553 if (err)
4554 return err;
4556 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4557 free(tmp_branch_name);
4558 return NULL;
4561 static const struct got_error *
4562 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4563 char **logmsg, void *arg)
4565 *logmsg = arg;
4566 return NULL;
4569 static const struct got_error *
4570 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4571 const char *path, struct got_object_id *blob_id,
4572 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4574 return NULL;
4577 struct collect_merged_paths_arg {
4578 got_worktree_checkout_cb progress_cb;
4579 void *progress_arg;
4580 struct got_pathlist_head *merged_paths;
4583 static const struct got_error *
4584 collect_merged_paths(void *arg, unsigned char status, const char *path)
4586 const struct got_error *err;
4587 struct collect_merged_paths_arg *a = arg;
4588 char *p;
4589 struct got_pathlist_entry *new;
4591 err = (*a->progress_cb)(a->progress_arg, status, path);
4592 if (err)
4593 return err;
4595 if (status != GOT_STATUS_MERGE &&
4596 status != GOT_STATUS_ADD &&
4597 status != GOT_STATUS_DELETE &&
4598 status != GOT_STATUS_CONFLICT)
4599 return NULL;
4601 p = strdup(path);
4602 if (p == NULL)
4603 return got_error_from_errno("strdup");
4605 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4606 if (err || new == NULL)
4607 free(p);
4608 return err;
4611 void
4612 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4614 struct got_pathlist_entry *pe;
4616 TAILQ_FOREACH(pe, merged_paths, entry)
4617 free((char *)pe->path);
4619 got_pathlist_free(merged_paths);
4622 static const struct got_error *
4623 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4624 struct got_repository *repo)
4626 const struct got_error *err;
4627 struct got_reference *commit_ref = NULL;
4629 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4630 if (err) {
4631 if (err->code != GOT_ERR_NOT_REF)
4632 goto done;
4633 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4634 if (err)
4635 goto done;
4636 err = got_ref_write(commit_ref, repo);
4637 if (err)
4638 goto done;
4639 } else {
4640 struct got_object_id *stored_id;
4641 int cmp;
4643 err = got_ref_resolve(&stored_id, repo, commit_ref);
4644 if (err)
4645 goto done;
4646 cmp = got_object_id_cmp(commit_id, stored_id);
4647 free(stored_id);
4648 if (cmp != 0) {
4649 err = got_error(GOT_ERR_REBASE_COMMITID);
4650 goto done;
4653 done:
4654 if (commit_ref)
4655 got_ref_close(commit_ref);
4656 return err;
4659 static const struct got_error *
4660 rebase_merge_files(struct got_pathlist_head *merged_paths,
4661 const char *commit_ref_name, struct got_worktree *worktree,
4662 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4663 struct got_object_id *commit_id, struct got_repository *repo,
4664 got_worktree_checkout_cb progress_cb, void *progress_arg,
4665 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4667 const struct got_error *err;
4668 struct got_reference *commit_ref = NULL;
4669 struct collect_merged_paths_arg cmp_arg;
4670 char *fileindex_path;
4672 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4674 err = get_fileindex_path(&fileindex_path, worktree);
4675 if (err)
4676 return err;
4678 cmp_arg.progress_cb = progress_cb;
4679 cmp_arg.progress_arg = progress_arg;
4680 cmp_arg.merged_paths = merged_paths;
4681 err = merge_files(worktree, fileindex, fileindex_path,
4682 parent_commit_id, commit_id, repo, collect_merged_paths,
4683 &cmp_arg, cancel_cb, cancel_arg);
4684 if (commit_ref)
4685 got_ref_close(commit_ref);
4686 return err;
4689 const struct got_error *
4690 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4691 struct got_worktree *worktree, struct got_fileindex *fileindex,
4692 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4693 struct got_repository *repo,
4694 got_worktree_checkout_cb progress_cb, void *progress_arg,
4695 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4697 const struct got_error *err;
4698 char *commit_ref_name;
4700 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4701 if (err)
4702 return err;
4704 err = store_commit_id(commit_ref_name, commit_id, repo);
4705 if (err)
4706 goto done;
4708 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4709 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4710 progress_arg, cancel_cb, cancel_arg);
4711 done:
4712 free(commit_ref_name);
4713 return err;
4716 const struct got_error *
4717 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4718 struct got_worktree *worktree, struct got_fileindex *fileindex,
4719 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4720 struct got_repository *repo,
4721 got_worktree_checkout_cb progress_cb, void *progress_arg,
4722 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4724 const struct got_error *err;
4725 char *commit_ref_name;
4727 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4728 if (err)
4729 return err;
4731 err = store_commit_id(commit_ref_name, commit_id, repo);
4732 if (err)
4733 goto done;
4735 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4736 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4737 progress_arg, cancel_cb, cancel_arg);
4738 done:
4739 free(commit_ref_name);
4740 return err;
4743 static const struct got_error *
4744 rebase_commit(struct got_object_id **new_commit_id,
4745 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4746 struct got_worktree *worktree, struct got_fileindex *fileindex,
4747 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4748 const char *new_logmsg, struct got_repository *repo)
4750 const struct got_error *err, *sync_err;
4751 struct got_pathlist_head commitable_paths;
4752 struct collect_commitables_arg cc_arg;
4753 char *fileindex_path = NULL;
4754 struct got_reference *head_ref = NULL;
4755 struct got_object_id *head_commit_id = NULL;
4756 char *logmsg = NULL;
4758 TAILQ_INIT(&commitable_paths);
4759 *new_commit_id = NULL;
4761 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4763 err = get_fileindex_path(&fileindex_path, worktree);
4764 if (err)
4765 return err;
4767 cc_arg.commitable_paths = &commitable_paths;
4768 cc_arg.worktree = worktree;
4769 cc_arg.repo = repo;
4770 cc_arg.have_staged_files = 0;
4772 * If possible get the status of individual files directly to
4773 * avoid crawling the entire work tree once per rebased commit.
4774 * TODO: Ideally, merged_paths would contain a list of commitables
4775 * we could use so we could skip worktree_status() entirely.
4777 if (merged_paths) {
4778 struct got_pathlist_entry *pe;
4779 if (TAILQ_EMPTY(merged_paths)) {
4780 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4781 goto done;
4783 TAILQ_FOREACH(pe, merged_paths, entry) {
4784 err = worktree_status(worktree, pe->path, fileindex,
4785 repo, collect_commitables, &cc_arg, NULL, NULL);
4786 if (err)
4787 goto done;
4789 } else {
4790 err = worktree_status(worktree, "", fileindex, repo,
4791 collect_commitables, &cc_arg, NULL, NULL);
4792 if (err)
4793 goto done;
4796 if (TAILQ_EMPTY(&commitable_paths)) {
4797 /* No-op change; commit will be elided. */
4798 err = got_ref_delete(commit_ref, repo);
4799 if (err)
4800 goto done;
4801 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4802 goto done;
4805 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4806 if (err)
4807 goto done;
4809 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4810 if (err)
4811 goto done;
4813 if (new_logmsg)
4814 logmsg = strdup(new_logmsg);
4815 else
4816 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4817 if (logmsg == NULL)
4818 return got_error_from_errno("strdup");
4820 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4821 worktree, got_object_commit_get_author(orig_commit),
4822 got_object_commit_get_committer(orig_commit),
4823 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4824 if (err)
4825 goto done;
4827 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4828 if (err)
4829 goto done;
4831 err = got_ref_delete(commit_ref, repo);
4832 if (err)
4833 goto done;
4835 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4836 fileindex);
4837 sync_err = sync_fileindex(fileindex, fileindex_path);
4838 if (sync_err && err == NULL)
4839 err = sync_err;
4840 done:
4841 free(fileindex_path);
4842 free(head_commit_id);
4843 if (head_ref)
4844 got_ref_close(head_ref);
4845 if (err) {
4846 free(*new_commit_id);
4847 *new_commit_id = NULL;
4849 return err;
4852 const struct got_error *
4853 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4854 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4855 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4856 struct got_commit_object *orig_commit,
4857 struct got_object_id *orig_commit_id, struct got_repository *repo)
4859 const struct got_error *err;
4860 char *commit_ref_name;
4861 struct got_reference *commit_ref = NULL;
4862 struct got_object_id *commit_id = NULL;
4864 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4865 if (err)
4866 return err;
4868 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4869 if (err)
4870 goto done;
4871 err = got_ref_resolve(&commit_id, repo, commit_ref);
4872 if (err)
4873 goto done;
4874 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4875 err = got_error(GOT_ERR_REBASE_COMMITID);
4876 goto done;
4879 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4880 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4881 done:
4882 if (commit_ref)
4883 got_ref_close(commit_ref);
4884 free(commit_ref_name);
4885 free(commit_id);
4886 return err;
4889 const struct got_error *
4890 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4891 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4892 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4893 struct got_commit_object *orig_commit,
4894 struct got_object_id *orig_commit_id, const char *new_logmsg,
4895 struct got_repository *repo)
4897 const struct got_error *err;
4898 char *commit_ref_name;
4899 struct got_reference *commit_ref = NULL;
4900 struct got_object_id *commit_id = NULL;
4902 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4903 if (err)
4904 return err;
4906 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4907 if (err)
4908 goto done;
4909 err = got_ref_resolve(&commit_id, repo, commit_ref);
4910 if (err)
4911 goto done;
4912 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4913 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4914 goto done;
4917 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4918 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4919 done:
4920 if (commit_ref)
4921 got_ref_close(commit_ref);
4922 free(commit_ref_name);
4923 free(commit_id);
4924 return err;
4927 const struct got_error *
4928 got_worktree_rebase_postpone(struct got_worktree *worktree,
4929 struct got_fileindex *fileindex)
4931 if (fileindex)
4932 got_fileindex_free(fileindex);
4933 return lock_worktree(worktree, LOCK_SH);
4936 static const struct got_error *
4937 delete_ref(const char *name, struct got_repository *repo)
4939 const struct got_error *err;
4940 struct got_reference *ref;
4942 err = got_ref_open(&ref, repo, name, 0);
4943 if (err) {
4944 if (err->code == GOT_ERR_NOT_REF)
4945 return NULL;
4946 return err;
4949 err = got_ref_delete(ref, repo);
4950 got_ref_close(ref);
4951 return err;
4954 static const struct got_error *
4955 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4957 const struct got_error *err;
4958 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4959 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4961 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4962 if (err)
4963 goto done;
4964 err = delete_ref(tmp_branch_name, repo);
4965 if (err)
4966 goto done;
4968 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4969 if (err)
4970 goto done;
4971 err = delete_ref(new_base_branch_ref_name, repo);
4972 if (err)
4973 goto done;
4975 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4976 if (err)
4977 goto done;
4978 err = delete_ref(branch_ref_name, repo);
4979 if (err)
4980 goto done;
4982 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4983 if (err)
4984 goto done;
4985 err = delete_ref(commit_ref_name, repo);
4986 if (err)
4987 goto done;
4989 done:
4990 free(tmp_branch_name);
4991 free(new_base_branch_ref_name);
4992 free(branch_ref_name);
4993 free(commit_ref_name);
4994 return err;
4997 const struct got_error *
4998 got_worktree_rebase_complete(struct got_worktree *worktree,
4999 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5000 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5001 struct got_repository *repo)
5003 const struct got_error *err, *unlockerr;
5004 struct got_object_id *new_head_commit_id = NULL;
5006 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5007 if (err)
5008 return err;
5010 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5011 if (err)
5012 goto done;
5014 err = got_ref_write(rebased_branch, repo);
5015 if (err)
5016 goto done;
5018 err = got_worktree_set_head_ref(worktree, rebased_branch);
5019 if (err)
5020 goto done;
5022 err = delete_rebase_refs(worktree, repo);
5023 done:
5024 if (fileindex)
5025 got_fileindex_free(fileindex);
5026 free(new_head_commit_id);
5027 unlockerr = lock_worktree(worktree, LOCK_SH);
5028 if (unlockerr && err == NULL)
5029 err = unlockerr;
5030 return err;
5033 const struct got_error *
5034 got_worktree_rebase_abort(struct got_worktree *worktree,
5035 struct got_fileindex *fileindex, struct got_repository *repo,
5036 struct got_reference *new_base_branch,
5037 got_worktree_checkout_cb progress_cb, void *progress_arg)
5039 const struct got_error *err, *unlockerr, *sync_err;
5040 struct got_reference *resolved = NULL;
5041 struct got_object_id *commit_id = NULL;
5042 char *fileindex_path = NULL;
5043 struct revert_file_args rfa;
5044 struct got_object_id *tree_id = NULL;
5046 err = lock_worktree(worktree, LOCK_EX);
5047 if (err)
5048 return err;
5050 err = got_ref_open(&resolved, repo,
5051 got_ref_get_symref_target(new_base_branch), 0);
5052 if (err)
5053 goto done;
5055 err = got_worktree_set_head_ref(worktree, resolved);
5056 if (err)
5057 goto done;
5060 * XXX commits to the base branch could have happened while
5061 * we were busy rebasing; should we store the original commit ID
5062 * when rebase begins and read it back here?
5064 err = got_ref_resolve(&commit_id, repo, resolved);
5065 if (err)
5066 goto done;
5068 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5069 if (err)
5070 goto done;
5072 err = got_object_id_by_path(&tree_id, repo,
5073 worktree->base_commit_id, worktree->path_prefix);
5074 if (err)
5075 goto done;
5077 err = delete_rebase_refs(worktree, repo);
5078 if (err)
5079 goto done;
5081 err = get_fileindex_path(&fileindex_path, worktree);
5082 if (err)
5083 goto done;
5085 rfa.worktree = worktree;
5086 rfa.fileindex = fileindex;
5087 rfa.progress_cb = progress_cb;
5088 rfa.progress_arg = progress_arg;
5089 rfa.patch_cb = NULL;
5090 rfa.patch_arg = NULL;
5091 rfa.repo = repo;
5092 err = worktree_status(worktree, "", fileindex, repo,
5093 revert_file, &rfa, NULL, NULL);
5094 if (err)
5095 goto sync;
5097 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5098 repo, progress_cb, progress_arg, NULL, NULL);
5099 sync:
5100 sync_err = sync_fileindex(fileindex, fileindex_path);
5101 if (sync_err && err == NULL)
5102 err = sync_err;
5103 done:
5104 got_ref_close(resolved);
5105 free(tree_id);
5106 free(commit_id);
5107 if (fileindex)
5108 got_fileindex_free(fileindex);
5109 free(fileindex_path);
5111 unlockerr = lock_worktree(worktree, LOCK_SH);
5112 if (unlockerr && err == NULL)
5113 err = unlockerr;
5114 return err;
5117 const struct got_error *
5118 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5119 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5120 struct got_fileindex **fileindex, struct got_worktree *worktree,
5121 struct got_repository *repo)
5123 const struct got_error *err = NULL;
5124 char *tmp_branch_name = NULL;
5125 char *branch_ref_name = NULL;
5126 char *base_commit_ref_name = NULL;
5127 char *fileindex_path = NULL;
5128 struct check_rebase_ok_arg ok_arg;
5129 struct got_reference *wt_branch = NULL;
5130 struct got_reference *base_commit_ref = NULL;
5132 *tmp_branch = NULL;
5133 *branch_ref = NULL;
5134 *base_commit_id = NULL;
5135 *fileindex = NULL;
5137 err = lock_worktree(worktree, LOCK_EX);
5138 if (err)
5139 return err;
5141 err = open_fileindex(fileindex, &fileindex_path, worktree);
5142 if (err)
5143 goto done;
5145 ok_arg.worktree = worktree;
5146 ok_arg.repo = repo;
5147 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5148 &ok_arg);
5149 if (err)
5150 goto done;
5152 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5153 if (err)
5154 goto done;
5156 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5157 if (err)
5158 goto done;
5160 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5161 worktree);
5162 if (err)
5163 goto done;
5165 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5166 0);
5167 if (err)
5168 goto done;
5170 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5171 if (err)
5172 goto done;
5174 err = got_ref_write(*branch_ref, repo);
5175 if (err)
5176 goto done;
5178 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5179 worktree->base_commit_id);
5180 if (err)
5181 goto done;
5182 err = got_ref_write(base_commit_ref, repo);
5183 if (err)
5184 goto done;
5185 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5186 if (*base_commit_id == NULL) {
5187 err = got_error_from_errno("got_object_id_dup");
5188 goto done;
5191 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5192 worktree->base_commit_id);
5193 if (err)
5194 goto done;
5195 err = got_ref_write(*tmp_branch, repo);
5196 if (err)
5197 goto done;
5199 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5200 if (err)
5201 goto done;
5202 done:
5203 free(fileindex_path);
5204 free(tmp_branch_name);
5205 free(branch_ref_name);
5206 free(base_commit_ref_name);
5207 if (wt_branch)
5208 got_ref_close(wt_branch);
5209 if (err) {
5210 if (*branch_ref) {
5211 got_ref_close(*branch_ref);
5212 *branch_ref = NULL;
5214 if (*tmp_branch) {
5215 got_ref_close(*tmp_branch);
5216 *tmp_branch = NULL;
5218 free(*base_commit_id);
5219 if (*fileindex) {
5220 got_fileindex_free(*fileindex);
5221 *fileindex = NULL;
5223 lock_worktree(worktree, LOCK_SH);
5225 return err;
5228 const struct got_error *
5229 got_worktree_histedit_postpone(struct got_worktree *worktree,
5230 struct got_fileindex *fileindex)
5232 if (fileindex)
5233 got_fileindex_free(fileindex);
5234 return lock_worktree(worktree, LOCK_SH);
5237 const struct got_error *
5238 got_worktree_histedit_in_progress(int *in_progress,
5239 struct got_worktree *worktree)
5241 const struct got_error *err;
5242 char *tmp_branch_name = NULL;
5244 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5245 if (err)
5246 return err;
5248 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5249 free(tmp_branch_name);
5250 return NULL;
5253 const struct got_error *
5254 got_worktree_histedit_continue(struct got_object_id **commit_id,
5255 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5256 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5257 struct got_worktree *worktree, struct got_repository *repo)
5259 const struct got_error *err;
5260 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5261 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5262 struct got_reference *commit_ref = NULL;
5263 struct got_reference *base_commit_ref = NULL;
5264 char *fileindex_path = NULL;
5265 int have_staged_files = 0;
5267 *commit_id = NULL;
5268 *tmp_branch = NULL;
5269 *base_commit_id = NULL;
5270 *fileindex = NULL;
5272 err = lock_worktree(worktree, LOCK_EX);
5273 if (err)
5274 return err;
5276 err = open_fileindex(fileindex, &fileindex_path, worktree);
5277 if (err)
5278 goto done;
5280 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5281 &have_staged_files);
5282 if (err && err->code != GOT_ERR_CANCELLED)
5283 goto done;
5284 if (have_staged_files) {
5285 err = got_error(GOT_ERR_STAGED_PATHS);
5286 goto done;
5289 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5290 if (err)
5291 goto done;
5293 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5294 if (err)
5295 goto done;
5297 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5298 if (err)
5299 goto done;
5301 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5302 worktree);
5303 if (err)
5304 goto done;
5306 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5307 if (err)
5308 goto done;
5310 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5311 if (err)
5312 goto done;
5313 err = got_ref_resolve(commit_id, repo, commit_ref);
5314 if (err)
5315 goto done;
5317 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5318 if (err)
5319 goto done;
5320 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5321 if (err)
5322 goto done;
5324 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5325 if (err)
5326 goto done;
5327 done:
5328 free(commit_ref_name);
5329 free(branch_ref_name);
5330 free(fileindex_path);
5331 if (commit_ref)
5332 got_ref_close(commit_ref);
5333 if (base_commit_ref)
5334 got_ref_close(base_commit_ref);
5335 if (err) {
5336 free(*commit_id);
5337 *commit_id = NULL;
5338 free(*base_commit_id);
5339 *base_commit_id = NULL;
5340 if (*tmp_branch) {
5341 got_ref_close(*tmp_branch);
5342 *tmp_branch = NULL;
5344 if (*fileindex) {
5345 got_fileindex_free(*fileindex);
5346 *fileindex = NULL;
5348 lock_worktree(worktree, LOCK_EX);
5350 return err;
5353 static const struct got_error *
5354 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5356 const struct got_error *err;
5357 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5358 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5360 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5361 if (err)
5362 goto done;
5363 err = delete_ref(tmp_branch_name, repo);
5364 if (err)
5365 goto done;
5367 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5368 worktree);
5369 if (err)
5370 goto done;
5371 err = delete_ref(base_commit_ref_name, repo);
5372 if (err)
5373 goto done;
5375 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5376 if (err)
5377 goto done;
5378 err = delete_ref(branch_ref_name, repo);
5379 if (err)
5380 goto done;
5382 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5383 if (err)
5384 goto done;
5385 err = delete_ref(commit_ref_name, repo);
5386 if (err)
5387 goto done;
5388 done:
5389 free(tmp_branch_name);
5390 free(base_commit_ref_name);
5391 free(branch_ref_name);
5392 free(commit_ref_name);
5393 return err;
5396 const struct got_error *
5397 got_worktree_histedit_abort(struct got_worktree *worktree,
5398 struct got_fileindex *fileindex, struct got_repository *repo,
5399 struct got_reference *branch, struct got_object_id *base_commit_id,
5400 got_worktree_checkout_cb progress_cb, void *progress_arg)
5402 const struct got_error *err, *unlockerr, *sync_err;
5403 struct got_reference *resolved = NULL;
5404 char *fileindex_path = NULL;
5405 struct got_object_id *tree_id = NULL;
5406 struct revert_file_args rfa;
5408 err = lock_worktree(worktree, LOCK_EX);
5409 if (err)
5410 return err;
5412 err = got_ref_open(&resolved, repo,
5413 got_ref_get_symref_target(branch), 0);
5414 if (err)
5415 goto done;
5417 err = got_worktree_set_head_ref(worktree, resolved);
5418 if (err)
5419 goto done;
5421 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5422 if (err)
5423 goto done;
5425 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5426 worktree->path_prefix);
5427 if (err)
5428 goto done;
5430 err = delete_histedit_refs(worktree, repo);
5431 if (err)
5432 goto done;
5434 err = get_fileindex_path(&fileindex_path, worktree);
5435 if (err)
5436 goto done;
5438 rfa.worktree = worktree;
5439 rfa.fileindex = fileindex;
5440 rfa.progress_cb = progress_cb;
5441 rfa.progress_arg = progress_arg;
5442 rfa.patch_cb = NULL;
5443 rfa.patch_arg = NULL;
5444 rfa.repo = repo;
5445 err = worktree_status(worktree, "", fileindex, repo,
5446 revert_file, &rfa, NULL, NULL);
5447 if (err)
5448 goto sync;
5450 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5451 repo, progress_cb, progress_arg, NULL, NULL);
5452 sync:
5453 sync_err = sync_fileindex(fileindex, fileindex_path);
5454 if (sync_err && err == NULL)
5455 err = sync_err;
5456 done:
5457 got_ref_close(resolved);
5458 free(tree_id);
5459 free(fileindex_path);
5461 unlockerr = lock_worktree(worktree, LOCK_SH);
5462 if (unlockerr && err == NULL)
5463 err = unlockerr;
5464 return err;
5467 const struct got_error *
5468 got_worktree_histedit_complete(struct got_worktree *worktree,
5469 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5470 struct got_reference *edited_branch, struct got_repository *repo)
5472 const struct got_error *err, *unlockerr;
5473 struct got_object_id *new_head_commit_id = NULL;
5474 struct got_reference *resolved = NULL;
5476 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5477 if (err)
5478 return err;
5480 err = got_ref_open(&resolved, repo,
5481 got_ref_get_symref_target(edited_branch), 0);
5482 if (err)
5483 goto done;
5485 err = got_ref_change_ref(resolved, new_head_commit_id);
5486 if (err)
5487 goto done;
5489 err = got_ref_write(resolved, repo);
5490 if (err)
5491 goto done;
5493 err = got_worktree_set_head_ref(worktree, resolved);
5494 if (err)
5495 goto done;
5497 err = delete_histedit_refs(worktree, repo);
5498 done:
5499 if (fileindex)
5500 got_fileindex_free(fileindex);
5501 free(new_head_commit_id);
5502 unlockerr = lock_worktree(worktree, LOCK_SH);
5503 if (unlockerr && err == NULL)
5504 err = unlockerr;
5505 return err;
5508 const struct got_error *
5509 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5510 struct got_object_id *commit_id, struct got_repository *repo)
5512 const struct got_error *err;
5513 char *commit_ref_name;
5515 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5516 if (err)
5517 return err;
5519 err = store_commit_id(commit_ref_name, commit_id, repo);
5520 if (err)
5521 goto done;
5523 err = delete_ref(commit_ref_name, repo);
5524 done:
5525 free(commit_ref_name);
5526 return err;
5529 struct check_stage_ok_arg {
5530 struct got_object_id *head_commit_id;
5531 struct got_worktree *worktree;
5532 struct got_fileindex *fileindex;
5533 struct got_repository *repo;
5534 int have_changes;
5537 const struct got_error *
5538 check_stage_ok(void *arg, unsigned char status,
5539 unsigned char staged_status, const char *relpath,
5540 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5541 struct got_object_id *commit_id)
5543 struct check_stage_ok_arg *a = arg;
5544 const struct got_error *err = NULL;
5545 struct got_fileindex_entry *ie;
5546 struct got_object_id base_commit_id;
5547 struct got_object_id *base_commit_idp = NULL;
5548 char *in_repo_path = NULL, *p;
5550 if (status == GOT_STATUS_UNVERSIONED)
5551 return NULL;
5553 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5554 if (ie == NULL)
5555 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5557 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5558 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5559 relpath) == -1)
5560 return got_error_from_errno("asprintf");
5562 if (got_fileindex_entry_has_commit(ie)) {
5563 memcpy(base_commit_id.sha1, ie->commit_sha1,
5564 SHA1_DIGEST_LENGTH);
5565 base_commit_idp = &base_commit_id;
5568 if (status == GOT_STATUS_NO_CHANGE) {
5569 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5570 goto done;
5571 } else if (status == GOT_STATUS_CONFLICT) {
5572 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5573 goto done;
5574 } else if (status != GOT_STATUS_ADD &&
5575 status != GOT_STATUS_MODIFY &&
5576 status != GOT_STATUS_DELETE) {
5577 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5578 goto done;
5581 a->have_changes = 1;
5583 p = in_repo_path;
5584 while (p[0] == '/')
5585 p++;
5586 err = check_out_of_date(p, status, staged_status,
5587 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5588 GOT_ERR_STAGE_OUT_OF_DATE);
5589 done:
5590 free(in_repo_path);
5591 return err;
5594 struct stage_path_arg {
5595 struct got_worktree *worktree;
5596 struct got_fileindex *fileindex;
5597 struct got_repository *repo;
5598 got_worktree_status_cb status_cb;
5599 void *status_arg;
5600 got_worktree_patch_cb patch_cb;
5601 void *patch_arg;
5604 static const struct got_error *
5605 stage_path(void *arg, unsigned char status,
5606 unsigned char staged_status, const char *relpath,
5607 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5608 struct got_object_id *commit_id)
5610 struct stage_path_arg *a = arg;
5611 const struct got_error *err = NULL;
5612 struct got_fileindex_entry *ie;
5613 char *ondisk_path = NULL, *path_content = NULL;
5614 uint32_t stage;
5615 struct got_object_id *new_staged_blob_id = NULL;
5617 if (status == GOT_STATUS_UNVERSIONED)
5618 return NULL;
5620 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5621 if (ie == NULL)
5622 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5624 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5625 relpath)== -1)
5626 return got_error_from_errno("asprintf");
5628 switch (status) {
5629 case GOT_STATUS_ADD:
5630 case GOT_STATUS_MODIFY:
5631 if (a->patch_cb) {
5632 if (status == GOT_STATUS_ADD) {
5633 int choice = GOT_PATCH_CHOICE_NONE;
5634 err = (*a->patch_cb)(&choice, a->patch_arg,
5635 status, ie->path, NULL, 1, 1);
5636 if (err)
5637 break;
5638 if (choice != GOT_PATCH_CHOICE_YES)
5639 break;
5640 } else {
5641 err = create_patched_content(&path_content, 0,
5642 staged_blob_id ? staged_blob_id : blob_id,
5643 ondisk_path, ie->path, a->repo,
5644 a->patch_cb, a->patch_arg);
5645 if (err || path_content == NULL)
5646 break;
5649 err = got_object_blob_create(&new_staged_blob_id,
5650 path_content ? path_content : ondisk_path, a->repo);
5651 if (err)
5652 break;
5653 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5654 SHA1_DIGEST_LENGTH);
5655 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5656 stage = GOT_FILEIDX_STAGE_ADD;
5657 else
5658 stage = GOT_FILEIDX_STAGE_MODIFY;
5659 got_fileindex_entry_stage_set(ie, stage);
5660 if (a->status_cb == NULL)
5661 break;
5662 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5663 get_staged_status(ie), relpath, blob_id,
5664 new_staged_blob_id, NULL);
5665 break;
5666 case GOT_STATUS_DELETE:
5667 if (staged_status == GOT_STATUS_DELETE)
5668 break;
5669 if (a->patch_cb) {
5670 int choice = GOT_PATCH_CHOICE_NONE;
5671 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5672 ie->path, NULL, 1, 1);
5673 if (err)
5674 break;
5675 if (choice == GOT_PATCH_CHOICE_NO)
5676 break;
5677 if (choice != GOT_PATCH_CHOICE_YES) {
5678 err = got_error(GOT_ERR_PATCH_CHOICE);
5679 break;
5682 stage = GOT_FILEIDX_STAGE_DELETE;
5683 got_fileindex_entry_stage_set(ie, stage);
5684 if (a->status_cb == NULL)
5685 break;
5686 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5687 get_staged_status(ie), relpath, NULL, NULL, NULL);
5688 break;
5689 case GOT_STATUS_NO_CHANGE:
5690 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5691 break;
5692 case GOT_STATUS_CONFLICT:
5693 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5694 break;
5695 default:
5696 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5697 break;
5700 if (path_content && unlink(path_content) == -1 && err == NULL)
5701 err = got_error_from_errno2("unlink", path_content);
5702 free(path_content);
5703 free(ondisk_path);
5704 free(new_staged_blob_id);
5705 return err;
5708 const struct got_error *
5709 got_worktree_stage(struct got_worktree *worktree,
5710 struct got_pathlist_head *paths,
5711 got_worktree_status_cb status_cb, void *status_arg,
5712 got_worktree_patch_cb patch_cb, void *patch_arg,
5713 struct got_repository *repo)
5715 const struct got_error *err = NULL, *sync_err, *unlockerr;
5716 struct got_pathlist_entry *pe;
5717 struct got_fileindex *fileindex = NULL;
5718 char *fileindex_path = NULL;
5719 struct got_reference *head_ref = NULL;
5720 struct got_object_id *head_commit_id = NULL;
5721 struct check_stage_ok_arg oka;
5722 struct stage_path_arg spa;
5724 err = lock_worktree(worktree, LOCK_EX);
5725 if (err)
5726 return err;
5728 err = got_ref_open(&head_ref, repo,
5729 got_worktree_get_head_ref_name(worktree), 0);
5730 if (err)
5731 goto done;
5732 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5733 if (err)
5734 goto done;
5735 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5736 if (err)
5737 goto done;
5739 /* Check pre-conditions before staging anything. */
5740 oka.head_commit_id = head_commit_id;
5741 oka.worktree = worktree;
5742 oka.fileindex = fileindex;
5743 oka.repo = repo;
5744 oka.have_changes = 0;
5745 TAILQ_FOREACH(pe, paths, entry) {
5746 err = worktree_status(worktree, pe->path, fileindex, repo,
5747 check_stage_ok, &oka, NULL, NULL);
5748 if (err)
5749 goto done;
5751 if (!oka.have_changes) {
5752 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5753 goto done;
5756 spa.worktree = worktree;
5757 spa.fileindex = fileindex;
5758 spa.repo = repo;
5759 spa.patch_cb = patch_cb;
5760 spa.patch_arg = patch_arg;
5761 spa.status_cb = status_cb;
5762 spa.status_arg = status_arg;
5763 TAILQ_FOREACH(pe, paths, entry) {
5764 err = worktree_status(worktree, pe->path, fileindex, repo,
5765 stage_path, &spa, NULL, NULL);
5766 if (err)
5767 goto done;
5770 sync_err = sync_fileindex(fileindex, fileindex_path);
5771 if (sync_err && err == NULL)
5772 err = sync_err;
5773 done:
5774 if (head_ref)
5775 got_ref_close(head_ref);
5776 free(head_commit_id);
5777 free(fileindex_path);
5778 if (fileindex)
5779 got_fileindex_free(fileindex);
5780 unlockerr = lock_worktree(worktree, LOCK_SH);
5781 if (unlockerr && err == NULL)
5782 err = unlockerr;
5783 return err;
5786 struct unstage_path_arg {
5787 struct got_worktree *worktree;
5788 struct got_fileindex *fileindex;
5789 struct got_repository *repo;
5790 got_worktree_checkout_cb progress_cb;
5791 void *progress_arg;
5792 got_worktree_patch_cb patch_cb;
5793 void *patch_arg;
5796 static const struct got_error *
5797 create_unstaged_content(char **path_unstaged_content,
5798 char **path_new_staged_content, struct got_object_id *blob_id,
5799 struct got_object_id *staged_blob_id, const char *relpath,
5800 struct got_repository *repo,
5801 got_worktree_patch_cb patch_cb, void *patch_arg)
5803 const struct got_error *err;
5804 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5805 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5806 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5807 struct stat sb1, sb2;
5808 struct got_diff_changes *changes = NULL;
5809 struct got_diff_state *ds = NULL;
5810 struct got_diff_args *args = NULL;
5811 struct got_diff_change *change;
5812 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5813 int have_content = 0, have_rejected_content = 0;
5815 *path_unstaged_content = NULL;
5816 *path_new_staged_content = NULL;
5818 err = got_object_id_str(&label1, blob_id);
5819 if (err)
5820 return err;
5821 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5822 if (err)
5823 goto done;
5825 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5826 if (err)
5827 goto done;
5829 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5830 if (err)
5831 goto done;
5833 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5834 if (err)
5835 goto done;
5837 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5838 if (err)
5839 goto done;
5841 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5842 if (err)
5843 goto done;
5845 if (stat(path1, &sb1) == -1) {
5846 err = got_error_from_errno2("stat", path1);
5847 goto done;
5850 if (stat(path2, &sb2) == -1) {
5851 err = got_error_from_errno2("stat", path2);
5852 goto done;
5855 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5856 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5857 if (err)
5858 goto done;
5860 err = got_opentemp_named(path_unstaged_content, &outfile,
5861 "got-unstaged-content");
5862 if (err)
5863 goto done;
5864 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5865 "got-new-staged-content");
5866 if (err)
5867 goto done;
5869 if (fseek(f1, 0L, SEEK_SET) == -1) {
5870 err = got_ferror(f1, GOT_ERR_IO);
5871 goto done;
5873 if (fseek(f2, 0L, SEEK_SET) == -1) {
5874 err = got_ferror(f2, GOT_ERR_IO);
5875 goto done;
5877 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5878 int choice;
5879 err = apply_or_reject_change(&choice, change, ++n,
5880 changes->nchanges, ds, args, diff_flags, relpath,
5881 f1, f2, &line_cur1, &line_cur2,
5882 outfile, rejectfile, patch_cb, patch_arg);
5883 if (err)
5884 goto done;
5885 if (choice == GOT_PATCH_CHOICE_YES)
5886 have_content = 1;
5887 else
5888 have_rejected_content = 1;
5889 if (choice == GOT_PATCH_CHOICE_QUIT)
5890 break;
5892 if (have_content || have_rejected_content)
5893 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5894 outfile, rejectfile);
5895 done:
5896 free(label1);
5897 if (blob)
5898 got_object_blob_close(blob);
5899 if (staged_blob)
5900 got_object_blob_close(staged_blob);
5901 if (f1 && fclose(f1) == EOF && err == NULL)
5902 err = got_error_from_errno2("fclose", path1);
5903 if (f2 && fclose(f2) == EOF && err == NULL)
5904 err = got_error_from_errno2("fclose", path2);
5905 if (outfile && fclose(outfile) == EOF && err == NULL)
5906 err = got_error_from_errno2("fclose", *path_unstaged_content);
5907 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5908 err = got_error_from_errno2("fclose", *path_new_staged_content);
5909 if (path1 && unlink(path1) == -1 && err == NULL)
5910 err = got_error_from_errno2("unlink", path1);
5911 if (path2 && unlink(path2) == -1 && err == NULL)
5912 err = got_error_from_errno2("unlink", path2);
5913 if (err || !have_content) {
5914 if (*path_unstaged_content &&
5915 unlink(*path_unstaged_content) == -1 && err == NULL)
5916 err = got_error_from_errno2("unlink",
5917 *path_unstaged_content);
5918 free(*path_unstaged_content);
5919 *path_unstaged_content = NULL;
5921 if (err || !have_rejected_content) {
5922 if (*path_new_staged_content &&
5923 unlink(*path_new_staged_content) == -1 && err == NULL)
5924 err = got_error_from_errno2("unlink",
5925 *path_new_staged_content);
5926 free(*path_new_staged_content);
5927 *path_new_staged_content = NULL;
5929 free(args);
5930 if (ds) {
5931 got_diff_state_free(ds);
5932 free(ds);
5934 if (changes)
5935 got_diff_free_changes(changes);
5936 free(path1);
5937 free(path2);
5938 return err;
5941 static const struct got_error *
5942 unstage_path(void *arg, unsigned char status,
5943 unsigned char staged_status, const char *relpath,
5944 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5945 struct got_object_id *commit_id)
5947 const struct got_error *err = NULL;
5948 struct unstage_path_arg *a = arg;
5949 struct got_fileindex_entry *ie;
5950 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5951 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5952 char *path_new_staged_content = NULL;
5953 int local_changes_subsumed;
5954 struct stat sb;
5956 if (staged_status != GOT_STATUS_ADD &&
5957 staged_status != GOT_STATUS_MODIFY &&
5958 staged_status != GOT_STATUS_DELETE)
5959 return NULL;
5961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5962 if (ie == NULL)
5963 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5965 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5966 == -1)
5967 return got_error_from_errno("asprintf");
5969 switch (staged_status) {
5970 case GOT_STATUS_MODIFY:
5971 err = got_object_open_as_blob(&blob_base, a->repo,
5972 blob_id, 8192);
5973 if (err)
5974 break;
5975 /* fall through */
5976 case GOT_STATUS_ADD:
5977 if (a->patch_cb) {
5978 if (staged_status == GOT_STATUS_ADD) {
5979 int choice = GOT_PATCH_CHOICE_NONE;
5980 err = (*a->patch_cb)(&choice, a->patch_arg,
5981 staged_status, ie->path, NULL, 1, 1);
5982 if (err)
5983 break;
5984 if (choice != GOT_PATCH_CHOICE_YES)
5985 break;
5986 } else {
5987 err = create_unstaged_content(
5988 &path_unstaged_content,
5989 &path_new_staged_content, blob_id,
5990 staged_blob_id, ie->path, a->repo,
5991 a->patch_cb, a->patch_arg);
5992 if (err || path_unstaged_content == NULL)
5993 break;
5994 if (path_new_staged_content) {
5995 err = got_object_blob_create(
5996 &staged_blob_id,
5997 path_new_staged_content,
5998 a->repo);
5999 if (err)
6000 break;
6001 memcpy(ie->staged_blob_sha1,
6002 staged_blob_id->sha1,
6003 SHA1_DIGEST_LENGTH);
6005 err = merge_file(&local_changes_subsumed,
6006 a->worktree, blob_base, ondisk_path,
6007 relpath, got_fileindex_perms_to_st(ie),
6008 path_unstaged_content, "unstaged",
6009 a->repo, a->progress_cb, a->progress_arg);
6010 if (err == NULL &&
6011 path_new_staged_content == NULL)
6012 got_fileindex_entry_stage_set(ie,
6013 GOT_FILEIDX_STAGE_NONE);
6014 break; /* Done with this file. */
6017 err = got_object_open_as_blob(&blob_staged, a->repo,
6018 staged_blob_id, 8192);
6019 if (err)
6020 break;
6021 err = merge_blob(&local_changes_subsumed, a->worktree,
6022 blob_base, ondisk_path, relpath,
6023 got_fileindex_perms_to_st(ie), blob_staged,
6024 commit_id ? commit_id : a->worktree->base_commit_id,
6025 a->repo, a->progress_cb, a->progress_arg);
6026 if (err == NULL)
6027 got_fileindex_entry_stage_set(ie,
6028 GOT_FILEIDX_STAGE_NONE);
6029 break;
6030 case GOT_STATUS_DELETE:
6031 if (a->patch_cb) {
6032 int choice = GOT_PATCH_CHOICE_NONE;
6033 err = (*a->patch_cb)(&choice, a->patch_arg,
6034 staged_status, ie->path, NULL, 1, 1);
6035 if (err)
6036 break;
6037 if (choice == GOT_PATCH_CHOICE_NO)
6038 break;
6039 if (choice != GOT_PATCH_CHOICE_YES) {
6040 err = got_error(GOT_ERR_PATCH_CHOICE);
6041 break;
6044 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6045 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6046 if (err)
6047 break;
6048 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6049 break;
6052 free(ondisk_path);
6053 if (path_unstaged_content &&
6054 unlink(path_unstaged_content) == -1 && err == NULL)
6055 err = got_error_from_errno2("unlink", path_unstaged_content);
6056 if (path_new_staged_content &&
6057 unlink(path_new_staged_content) == -1 && err == NULL)
6058 err = got_error_from_errno2("unlink", path_new_staged_content);
6059 free(path_unstaged_content);
6060 free(path_new_staged_content);
6061 if (blob_base)
6062 got_object_blob_close(blob_base);
6063 if (blob_staged)
6064 got_object_blob_close(blob_staged);
6065 return err;
6068 const struct got_error *
6069 got_worktree_unstage(struct got_worktree *worktree,
6070 struct got_pathlist_head *paths,
6071 got_worktree_checkout_cb progress_cb, void *progress_arg,
6072 got_worktree_patch_cb patch_cb, void *patch_arg,
6073 struct got_repository *repo)
6075 const struct got_error *err = NULL, *sync_err, *unlockerr;
6076 struct got_pathlist_entry *pe;
6077 struct got_fileindex *fileindex = NULL;
6078 char *fileindex_path = NULL;
6079 struct unstage_path_arg upa;
6081 err = lock_worktree(worktree, LOCK_EX);
6082 if (err)
6083 return err;
6085 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6086 if (err)
6087 goto done;
6089 upa.worktree = worktree;
6090 upa.fileindex = fileindex;
6091 upa.repo = repo;
6092 upa.progress_cb = progress_cb;
6093 upa.progress_arg = progress_arg;
6094 upa.patch_cb = patch_cb;
6095 upa.patch_arg = patch_arg;
6096 TAILQ_FOREACH(pe, paths, entry) {
6097 err = worktree_status(worktree, pe->path, fileindex, repo,
6098 unstage_path, &upa, NULL, NULL);
6099 if (err)
6100 goto done;
6103 sync_err = sync_fileindex(fileindex, fileindex_path);
6104 if (sync_err && err == NULL)
6105 err = sync_err;
6106 done:
6107 free(fileindex_path);
6108 if (fileindex)
6109 got_fileindex_free(fileindex);
6110 unlockerr = lock_worktree(worktree, LOCK_SH);
6111 if (unlockerr && err == NULL)
6112 err = unlockerr;
6113 return err;