Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 static const struct got_error *
64 create_meta_file(const char *path_got, const char *name, const char *content)
65 {
66 const struct got_error *err = NULL;
67 char *path;
69 if (asprintf(&path, "%s/%s", path_got, name) == -1)
70 return got_error_from_errno("asprintf");
72 err = got_path_create_file(path, content);
73 free(path);
74 return err;
75 }
77 static const struct got_error *
78 update_meta_file(const char *path_got, const char *name, const char *content)
79 {
80 const struct got_error *err = NULL;
81 FILE *tmpfile = NULL;
82 char *tmppath = NULL;
83 char *path = NULL;
85 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
86 err = got_error_from_errno("asprintf");
87 path = NULL;
88 goto done;
89 }
91 err = got_opentemp_named(&tmppath, &tmpfile, path);
92 if (err)
93 goto done;
95 if (content) {
96 int len = fprintf(tmpfile, "%s\n", content);
97 if (len != strlen(content) + 1) {
98 err = got_error_from_errno2("fprintf", tmppath);
99 goto done;
103 if (rename(tmppath, path) != 0) {
104 err = got_error_from_errno3("rename", tmppath, path);
105 unlink(tmppath);
106 goto done;
109 done:
110 if (fclose(tmpfile) != 0 && err == NULL)
111 err = got_error_from_errno2("fclose", tmppath);
112 free(tmppath);
113 return err;
116 static const struct got_error *
117 read_meta_file(char **content, const char *path_got, const char *name)
119 const struct got_error *err = NULL;
120 char *path;
121 int fd = -1;
122 ssize_t n;
123 struct stat sb;
125 *content = NULL;
127 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
128 err = got_error_from_errno("asprintf");
129 path = NULL;
130 goto done;
133 fd = open(path, O_RDONLY | O_NOFOLLOW);
134 if (fd == -1) {
135 if (errno == ENOENT)
136 err = got_error_path(path, GOT_ERR_WORKTREE_META);
137 else
138 err = got_error_from_errno2("open", path);
139 goto done;
141 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
142 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
143 : got_error_from_errno2("flock", path));
144 goto done;
147 if (fstat(fd, &sb) != 0) {
148 err = got_error_from_errno2("fstat", path);
149 goto done;
151 *content = calloc(1, sb.st_size);
152 if (*content == NULL) {
153 err = got_error_from_errno("calloc");
154 goto done;
157 n = read(fd, *content, sb.st_size);
158 if (n != sb.st_size) {
159 err = (n == -1 ? got_error_from_errno2("read", path) :
160 got_error_path(path, GOT_ERR_WORKTREE_META));
161 goto done;
163 if ((*content)[sb.st_size - 1] != '\n') {
164 err = got_error_path(path, GOT_ERR_WORKTREE_META);
165 goto done;
167 (*content)[sb.st_size - 1] = '\0';
169 done:
170 if (fd != -1 && close(fd) == -1 && err == NULL)
171 err = got_error_from_errno2("close", path_got);
172 free(path);
173 if (err) {
174 free(*content);
175 *content = NULL;
177 return err;
180 static const struct got_error *
181 write_head_ref(const char *path_got, struct got_reference *head_ref)
183 const struct got_error *err = NULL;
184 char *refstr = NULL;
186 if (got_ref_is_symbolic(head_ref)) {
187 refstr = got_ref_to_str(head_ref);
188 if (refstr == NULL)
189 return got_error_from_errno("got_ref_to_str");
190 } else {
191 refstr = strdup(got_ref_get_name(head_ref));
192 if (refstr == NULL)
193 return got_error_from_errno("strdup");
195 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
196 free(refstr);
197 return err;
200 const struct got_error *
201 got_worktree_init(const char *path, struct got_reference *head_ref,
202 const char *prefix, struct got_repository *repo)
204 const struct got_error *err = NULL;
205 struct got_object_id *commit_id = NULL;
206 uuid_t uuid;
207 uint32_t uuid_status;
208 int obj_type;
209 char *path_got = NULL;
210 char *formatstr = NULL;
211 char *absprefix = NULL;
212 char *basestr = NULL;
213 char *uuidstr = NULL;
215 if (strcmp(path, got_repo_get_path(repo)) == 0) {
216 err = got_error(GOT_ERR_WORKTREE_REPO);
217 goto done;
220 err = got_ref_resolve(&commit_id, repo, head_ref);
221 if (err)
222 return err;
223 err = got_object_get_type(&obj_type, repo, commit_id);
224 if (err)
225 return err;
226 if (obj_type != GOT_OBJ_TYPE_COMMIT)
227 return got_error(GOT_ERR_OBJ_TYPE);
229 if (!got_path_is_absolute(prefix)) {
230 if (asprintf(&absprefix, "/%s", prefix) == -1)
231 return got_error_from_errno("asprintf");
234 /* Create top-level directory (may already exist). */
235 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
236 err = got_error_from_errno2("mkdir", path);
237 goto done;
240 /* Create .got directory (may already exist). */
241 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
246 err = got_error_from_errno2("mkdir", path_got);
247 goto done;
250 /* Create an empty lock file. */
251 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
252 if (err)
253 goto done;
255 /* Create an empty file index. */
256 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
257 if (err)
258 goto done;
260 /* Write the HEAD reference. */
261 err = write_head_ref(path_got, head_ref);
262 if (err)
263 goto done;
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
267 if (err)
268 goto done;
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
270 if (err)
271 goto done;
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
276 if (err)
277 goto done;
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
282 if (err)
283 goto done;
285 /* Generate UUID. */
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status, "uuid_create");
289 goto done;
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status, "uuid_to_string");
294 goto done;
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
297 if (err)
298 goto done;
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno("asprintf");
303 goto done;
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
306 if (err)
307 goto done;
309 done:
310 free(commit_id);
311 free(path_got);
312 free(formatstr);
313 free(absprefix);
314 free(basestr);
315 free(uuidstr);
316 return err;
319 static const struct got_error *
320 open_worktree(struct got_worktree **worktree, const char *path)
322 const struct got_error *err = NULL;
323 char *path_got;
324 char *formatstr = NULL;
325 char *uuidstr = NULL;
326 char *path_lock = NULL;
327 char *base_commit_id_str = NULL;
328 int version, fd = -1;
329 const char *errstr;
330 struct got_repository *repo = NULL;
331 uint32_t uuid_status;
333 *worktree = NULL;
335 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
336 err = got_error_from_errno("asprintf");
337 path_got = NULL;
338 goto done;
341 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_lock = NULL;
344 goto done;
347 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
348 if (fd == -1) {
349 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
350 : got_error_from_errno2("open", path_lock));
351 goto done;
354 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
355 if (err)
356 goto done;
358 version = strtonum(formatstr, 1, INT_MAX, &errstr);
359 if (errstr) {
360 err = got_error_msg(GOT_ERR_WORKTREE_META,
361 "could not parse work tree format version number");
362 goto done;
364 if (version != GOT_WORKTREE_FORMAT_VERSION) {
365 err = got_error(GOT_ERR_WORKTREE_VERS);
366 goto done;
369 *worktree = calloc(1, sizeof(**worktree));
370 if (*worktree == NULL) {
371 err = got_error_from_errno("calloc");
372 goto done;
374 (*worktree)->lockfd = -1;
376 (*worktree)->root_path = strdup(path);
377 if ((*worktree)->root_path == NULL) {
378 err = got_error_from_errno("strdup");
379 goto done;
381 err = read_meta_file(&(*worktree)->repo_path, path_got,
382 GOT_WORKTREE_REPOSITORY);
383 if (err)
384 goto done;
386 err = read_meta_file(&(*worktree)->path_prefix, path_got,
387 GOT_WORKTREE_PATH_PREFIX);
388 if (err)
389 goto done;
391 err = read_meta_file(&base_commit_id_str, path_got,
392 GOT_WORKTREE_BASE_COMMIT);
393 if (err)
394 goto done;
396 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
397 if (err)
398 goto done;
399 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
400 if (uuid_status != uuid_s_ok) {
401 err = got_error_uuid(uuid_status, "uuid_from_string");
402 goto done;
405 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
406 if (err)
407 goto done;
409 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
410 base_commit_id_str);
411 if (err)
412 goto done;
414 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
415 GOT_WORKTREE_HEAD_REF);
416 done:
417 if (repo)
418 got_repo_close(repo);
419 free(path_got);
420 free(path_lock);
421 free(base_commit_id_str);
422 free(uuidstr);
423 free(formatstr);
424 if (err) {
425 if (fd != -1)
426 close(fd);
427 if (*worktree != NULL)
428 got_worktree_close(*worktree);
429 *worktree = NULL;
430 } else
431 (*worktree)->lockfd = fd;
433 return err;
436 const struct got_error *
437 got_worktree_open(struct got_worktree **worktree, const char *path)
439 const struct got_error *err = NULL;
441 do {
442 err = open_worktree(worktree, path);
443 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
444 return err;
445 if (*worktree)
446 return NULL;
447 path = dirname(path);
448 if (path == NULL)
449 return got_error_from_errno2("dirname", path);
450 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
452 return got_error(GOT_ERR_NOT_WORKTREE);
455 const struct got_error *
456 got_worktree_close(struct got_worktree *worktree)
458 const struct got_error *err = NULL;
459 free(worktree->repo_path);
460 free(worktree->path_prefix);
461 free(worktree->base_commit_id);
462 free(worktree->head_ref_name);
463 if (worktree->lockfd != -1)
464 if (close(worktree->lockfd) != 0)
465 err = got_error_from_errno2("close",
466 got_worktree_get_root_path(worktree));
467 free(worktree->root_path);
468 free(worktree);
469 return err;
472 const char *
473 got_worktree_get_root_path(struct got_worktree *worktree)
475 return worktree->root_path;
478 const char *
479 got_worktree_get_repo_path(struct got_worktree *worktree)
481 return worktree->repo_path;
484 const char *
485 got_worktree_get_path_prefix(struct got_worktree *worktree)
487 return worktree->path_prefix;
490 const struct got_error *
491 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
492 const char *path_prefix)
494 char *absprefix = NULL;
496 if (!got_path_is_absolute(path_prefix)) {
497 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
498 return got_error_from_errno("asprintf");
500 *match = (strcmp(absprefix ? absprefix : path_prefix,
501 worktree->path_prefix) == 0);
502 free(absprefix);
503 return NULL;
506 const char *
507 got_worktree_get_head_ref_name(struct got_worktree *worktree)
509 return worktree->head_ref_name;
512 const struct got_error *
513 got_worktree_set_head_ref(struct got_worktree *worktree,
514 struct got_reference *head_ref)
516 const struct got_error *err = NULL;
517 char *path_got = NULL, *head_ref_name = NULL;
519 if (asprintf(&path_got, "%s/%s", worktree->root_path,
520 GOT_WORKTREE_GOT_DIR) == -1) {
521 err = got_error_from_errno("asprintf");
522 path_got = NULL;
523 goto done;
526 head_ref_name = strdup(got_ref_get_name(head_ref));
527 if (head_ref_name == NULL) {
528 err = got_error_from_errno("strdup");
529 goto done;
532 err = write_head_ref(path_got, head_ref);
533 if (err)
534 goto done;
536 free(worktree->head_ref_name);
537 worktree->head_ref_name = head_ref_name;
538 done:
539 free(path_got);
540 if (err)
541 free(head_ref_name);
542 return err;
545 struct got_object_id *
546 got_worktree_get_base_commit_id(struct got_worktree *worktree)
548 return worktree->base_commit_id;
551 const struct got_error *
552 got_worktree_set_base_commit_id(struct got_worktree *worktree,
553 struct got_repository *repo, struct got_object_id *commit_id)
555 const struct got_error *err;
556 struct got_object *obj = NULL;
557 char *id_str = NULL;
558 char *path_got = NULL;
560 if (asprintf(&path_got, "%s/%s", worktree->root_path,
561 GOT_WORKTREE_GOT_DIR) == -1) {
562 err = got_error_from_errno("asprintf");
563 path_got = NULL;
564 goto done;
567 err = got_object_open(&obj, repo, commit_id);
568 if (err)
569 return err;
571 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
572 err = got_error(GOT_ERR_OBJ_TYPE);
573 goto done;
576 /* Record our base commit. */
577 err = got_object_id_str(&id_str, commit_id);
578 if (err)
579 goto done;
580 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 if (err)
582 goto done;
584 free(worktree->base_commit_id);
585 worktree->base_commit_id = got_object_id_dup(commit_id);
586 if (worktree->base_commit_id == NULL) {
587 err = got_error_from_errno("got_object_id_dup");
588 goto done;
590 done:
591 if (obj)
592 got_object_close(obj);
593 free(id_str);
594 free(path_got);
595 return err;
598 static const struct got_error *
599 lock_worktree(struct got_worktree *worktree, int operation)
601 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
602 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
603 : got_error_from_errno2("flock",
604 got_worktree_get_root_path(worktree)));
605 return NULL;
608 static const struct got_error *
609 add_dir_on_disk(struct got_worktree *worktree, const char *path)
611 const struct got_error *err = NULL;
612 char *abspath;
614 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
615 return got_error_from_errno("asprintf");
617 err = got_path_mkdir(abspath);
618 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
619 struct stat sb;
620 err = NULL;
621 if (lstat(abspath, &sb) == -1) {
622 err = got_error_from_errno2("lstat", abspath);
623 } else if (!S_ISDIR(sb.st_mode)) {
624 /* TODO directory is obstructed; do something */
625 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
628 free(abspath);
629 return err;
632 static const struct got_error *
633 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
635 const struct got_error *err = NULL;
636 uint8_t fbuf1[8192];
637 uint8_t fbuf2[8192];
638 size_t flen1 = 0, flen2 = 0;
640 *same = 1;
642 for (;;) {
643 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
644 if (flen1 == 0 && ferror(f1)) {
645 err = got_error_from_errno("fread");
646 break;
648 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
649 if (flen2 == 0 && ferror(f2)) {
650 err = got_error_from_errno("fread");
651 break;
653 if (flen1 == 0) {
654 if (flen2 != 0)
655 *same = 0;
656 break;
657 } else if (flen2 == 0) {
658 if (flen1 != 0)
659 *same = 0;
660 break;
661 } else if (flen1 == flen2) {
662 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
663 *same = 0;
664 break;
666 } else {
667 *same = 0;
668 break;
672 return err;
675 static const struct got_error *
676 check_files_equal(int *same, const char *f1_path, const char *f2_path)
678 const struct got_error *err = NULL;
679 struct stat sb;
680 size_t size1, size2;
681 FILE *f1 = NULL, *f2 = NULL;
683 *same = 1;
685 if (lstat(f1_path, &sb) != 0) {
686 err = got_error_from_errno2("lstat", f1_path);
687 goto done;
689 size1 = sb.st_size;
691 if (lstat(f2_path, &sb) != 0) {
692 err = got_error_from_errno2("lstat", f2_path);
693 goto done;
695 size2 = sb.st_size;
697 if (size1 != size2) {
698 *same = 0;
699 return NULL;
702 f1 = fopen(f1_path, "r");
703 if (f1 == NULL)
704 return got_error_from_errno2("open", f1_path);
706 f2 = fopen(f2_path, "r");
707 if (f2 == NULL) {
708 err = got_error_from_errno2("open", f2_path);
709 goto done;
712 err = check_file_contents_equal(same, f1, f2);
713 done:
714 if (f1 && fclose(f1) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
716 if (f2 && fclose(f2) != 0 && err == NULL)
717 err = got_error_from_errno("fclose");
719 return err;
722 /*
723 * Perform a 3-way merge where blob_orig acts as the common ancestor,
724 * the file at deriv_path acts as the first derived version, and the
725 * file on disk acts as the second derived version.
726 */
727 static const struct got_error *
728 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
729 struct got_blob_object *blob_orig, const char *ondisk_path,
730 const char *path, uint16_t st_mode, const char *deriv_path,
731 const char *label_deriv, struct got_repository *repo,
732 got_worktree_checkout_cb progress_cb, void *progress_arg)
734 const struct got_error *err = NULL;
735 int merged_fd = -1;
736 FILE *f_orig = NULL;
737 char *blob_orig_path = NULL;
738 char *merged_path = NULL, *base_path = NULL;
739 int overlapcnt = 0;
740 char *parent;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
745 if (parent == NULL)
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
752 if (err)
753 goto done;
755 free(base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
757 err = got_error_from_errno("asprintf");
758 base_path = NULL;
759 goto done;
762 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
763 if (err)
764 goto done;
765 if (blob_orig) {
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
767 blob_orig);
768 if (err)
769 goto done;
770 } else {
771 /*
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
775 */
778 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
779 blob_orig_path, ondisk_path, label_deriv, path);
780 if (err)
781 goto done;
783 err = (*progress_cb)(progress_arg,
784 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
785 if (err)
786 goto done;
788 if (fsync(merged_fd) != 0) {
789 err = got_error_from_errno("fsync");
790 goto done;
793 /* Check if a clean merge has subsumed all local changes. */
794 if (overlapcnt == 0) {
795 err = check_files_equal(local_changes_subsumed, deriv_path,
796 merged_path);
797 if (err)
798 goto done;
801 if (chmod(merged_path, st_mode) != 0) {
802 err = got_error_from_errno2("chmod", merged_path);
803 goto done;
806 if (rename(merged_path, ondisk_path) != 0) {
807 err = got_error_from_errno3("rename", merged_path,
808 ondisk_path);
809 unlink(merged_path);
810 goto done;
813 done:
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno("close");
816 if (f_orig && fclose(f_orig) != 0 && err == NULL)
817 err = got_error_from_errno("fclose");
818 free(merged_path);
819 free(base_path);
820 if (blob_orig_path) {
821 unlink(blob_orig_path);
822 free(blob_orig_path);
824 return err;
827 /*
828 * Perform a 3-way merge where blob_orig acts as the common ancestor,
829 * blob_deriv acts as the first derived version, and the file on disk
830 * acts as the second derived version.
831 */
832 static const struct got_error *
833 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
834 struct got_blob_object *blob_orig, const char *ondisk_path,
835 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
836 struct got_object_id *deriv_base_commit_id,
837 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
838 void *progress_arg)
840 const struct got_error *err = NULL;
841 FILE *f_deriv = NULL;
842 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
843 char *label_deriv = NULL, *parent;
845 *local_changes_subsumed = 0;
847 parent = dirname(ondisk_path);
848 if (parent == NULL)
849 return got_error_from_errno2("dirname", ondisk_path);
851 free(base_path);
852 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
853 err = got_error_from_errno("asprintf");
854 base_path = NULL;
855 goto done;
858 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
859 if (err)
860 goto done;
861 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
862 blob_deriv);
863 if (err)
864 goto done;
866 err = got_object_id_str(&id_str, deriv_base_commit_id);
867 if (err)
868 goto done;
869 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
870 err = got_error_from_errno("asprintf");
871 goto done;
874 err = merge_file(local_changes_subsumed, worktree, blob_orig,
875 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
876 repo, progress_cb, progress_arg);
877 done:
878 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
879 err = got_error_from_errno("fclose");
880 free(base_path);
881 if (blob_deriv_path) {
882 unlink(blob_deriv_path);
883 free(blob_deriv_path);
885 free(id_str);
886 free(label_deriv);
887 return err;
890 static const struct got_error *
891 update_blob_fileindex_entry(struct got_worktree *worktree,
892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
893 const char *ondisk_path, const char *path, struct got_blob_object *blob,
894 int update_timestamps)
896 const struct got_error *err = NULL;
898 if (ie == NULL)
899 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
900 if (ie)
901 err = got_fileindex_entry_update(ie, ondisk_path,
902 blob->id.sha1, worktree->base_commit_id->sha1,
903 update_timestamps);
904 else {
905 struct got_fileindex_entry *new_ie;
906 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
907 path, blob->id.sha1, worktree->base_commit_id->sha1);
908 if (!err)
909 err = got_fileindex_entry_add(fileindex, new_ie);
911 return err;
914 static const struct got_error *
915 install_blob(struct got_worktree *worktree, const char *ondisk_path,
916 const char *path, uint16_t te_mode, uint16_t st_mode,
917 struct got_blob_object *blob, int restoring_missing_file,
918 int reverting_versioned_file, struct got_repository *repo,
919 got_worktree_checkout_cb progress_cb, void *progress_arg)
921 const struct got_error *err = NULL;
922 int fd = -1;
923 size_t len, hdrlen;
924 int update = 0;
925 char *tmppath = NULL;
927 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
928 GOT_DEFAULT_FILE_MODE);
929 if (fd == -1) {
930 if (errno == ENOENT) {
931 char *parent = dirname(path);
932 if (parent == NULL)
933 return got_error_from_errno2("dirname", path);
934 err = add_dir_on_disk(worktree, parent);
935 if (err)
936 return err;
937 fd = open(ondisk_path,
938 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
939 GOT_DEFAULT_FILE_MODE);
940 if (fd == -1)
941 return got_error_from_errno2("open",
942 ondisk_path);
943 } else if (errno == EEXIST) {
944 if (!S_ISREG(st_mode)) {
945 /* TODO file is obstructed; do something */
946 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
947 goto done;
948 } else {
949 err = got_opentemp_named_fd(&tmppath, &fd,
950 ondisk_path);
951 if (err)
952 goto done;
953 update = 1;
955 } else
956 return got_error_from_errno2("open", ondisk_path);
959 if (restoring_missing_file)
960 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
961 else if (reverting_versioned_file)
962 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
963 else
964 err = (*progress_cb)(progress_arg,
965 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
966 if (err)
967 goto done;
969 hdrlen = got_object_blob_get_hdrlen(blob);
970 do {
971 const uint8_t *buf = got_object_blob_get_read_buf(blob);
972 err = got_object_blob_read_block(&len, blob);
973 if (err)
974 break;
975 if (len > 0) {
976 /* Skip blob object header first time around. */
977 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
978 if (outlen == -1) {
979 err = got_error_from_errno("write");
980 goto done;
981 } else if (outlen != len - hdrlen) {
982 err = got_error(GOT_ERR_IO);
983 goto done;
985 hdrlen = 0;
987 } while (len != 0);
989 if (fsync(fd) != 0) {
990 err = got_error_from_errno("fsync");
991 goto done;
994 if (update) {
995 if (rename(tmppath, ondisk_path) != 0) {
996 err = got_error_from_errno3("rename", tmppath,
997 ondisk_path);
998 unlink(tmppath);
999 goto done;
1003 if (te_mode & S_IXUSR) {
1004 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1005 err = got_error_from_errno2("chmod", ondisk_path);
1006 goto done;
1008 } else {
1009 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1010 err = got_error_from_errno2("chmod", ondisk_path);
1011 goto done;
1015 done:
1016 if (fd != -1 && close(fd) != 0 && err == NULL)
1017 err = got_error_from_errno("close");
1018 free(tmppath);
1019 return err;
1022 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1023 static const struct got_error *
1024 get_modified_file_content_status(unsigned char *status, FILE *f)
1026 const struct got_error *err = NULL;
1027 const char *markers[3] = {
1028 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1029 GOT_DIFF_CONFLICT_MARKER_SEP,
1030 GOT_DIFF_CONFLICT_MARKER_END
1032 int i = 0;
1033 char *line;
1034 size_t len;
1035 const char delim[3] = {'\0', '\0', '\0'};
1037 while (*status == GOT_STATUS_MODIFY) {
1038 line = fparseln(f, &len, NULL, delim, 0);
1039 if (line == NULL) {
1040 if (feof(f))
1041 break;
1042 err = got_ferror(f, GOT_ERR_IO);
1043 break;
1046 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1047 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1048 == 0)
1049 *status = GOT_STATUS_CONFLICT;
1050 else
1051 i++;
1055 return err;
1058 static int
1059 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1061 return !(ie->ctime_sec == sb->st_ctime &&
1062 ie->ctime_nsec == sb->st_ctimensec &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff));
1068 static unsigned char
1069 get_staged_status(struct got_fileindex_entry *ie)
1071 switch (got_fileindex_entry_stage_get(ie)) {
1072 case GOT_FILEIDX_STAGE_ADD:
1073 return GOT_STATUS_ADD;
1074 case GOT_FILEIDX_STAGE_DELETE:
1075 return GOT_STATUS_DELETE;
1076 case GOT_FILEIDX_STAGE_MODIFY:
1077 return GOT_STATUS_MODIFY;
1078 default:
1079 return GOT_STATUS_NO_CHANGE;
1083 static const struct got_error *
1084 get_file_status(unsigned char *status, struct stat *sb,
1085 struct got_fileindex_entry *ie, const char *abspath,
1086 struct got_repository *repo)
1088 const struct got_error *err = NULL;
1089 struct got_object_id id;
1090 size_t hdrlen;
1091 FILE *f = NULL;
1092 uint8_t fbuf[8192];
1093 struct got_blob_object *blob = NULL;
1094 size_t flen, blen;
1095 unsigned char staged_status = get_staged_status(ie);
1097 *status = GOT_STATUS_NO_CHANGE;
1099 if (lstat(abspath, sb) == -1) {
1100 if (errno == ENOENT) {
1101 if (got_fileindex_entry_has_file_on_disk(ie))
1102 *status = GOT_STATUS_MISSING;
1103 else
1104 *status = GOT_STATUS_DELETE;
1105 return NULL;
1107 return got_error_from_errno2("lstat", abspath);
1110 if (!S_ISREG(sb->st_mode)) {
1111 *status = GOT_STATUS_OBSTRUCTED;
1112 return NULL;
1115 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1116 *status = GOT_STATUS_DELETE;
1117 return NULL;
1118 } else if (!got_fileindex_entry_has_blob(ie) &&
1119 staged_status != GOT_STATUS_ADD) {
1120 *status = GOT_STATUS_ADD;
1121 return NULL;
1124 if (!stat_info_differs(ie, sb))
1125 return NULL;
1127 if (staged_status == GOT_STATUS_MODIFY ||
1128 staged_status == GOT_STATUS_ADD)
1129 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1130 else
1131 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1133 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1134 if (err)
1135 return err;
1137 f = fopen(abspath, "r");
1138 if (f == NULL) {
1139 err = got_error_from_errno2("fopen", abspath);
1140 goto done;
1142 hdrlen = got_object_blob_get_hdrlen(blob);
1143 for (;;) {
1144 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1145 err = got_object_blob_read_block(&blen, blob);
1146 if (err)
1147 goto done;
1148 /* Skip length of blob object header first time around. */
1149 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1150 if (flen == 0 && ferror(f)) {
1151 err = got_error_from_errno("fread");
1152 goto done;
1154 if (blen == 0) {
1155 if (flen != 0)
1156 *status = GOT_STATUS_MODIFY;
1157 break;
1158 } else if (flen == 0) {
1159 if (blen != 0)
1160 *status = GOT_STATUS_MODIFY;
1161 break;
1162 } else if (blen - hdrlen == flen) {
1163 /* Skip blob object header first time around. */
1164 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1165 *status = GOT_STATUS_MODIFY;
1166 break;
1168 } else {
1169 *status = GOT_STATUS_MODIFY;
1170 break;
1172 hdrlen = 0;
1175 if (*status == GOT_STATUS_MODIFY) {
1176 rewind(f);
1177 err = get_modified_file_content_status(status, f);
1179 done:
1180 if (blob)
1181 got_object_blob_close(blob);
1182 if (f)
1183 fclose(f);
1184 return err;
1188 * Update timestamps in the file index if a file is unmodified and
1189 * we had to run a full content comparison to find out.
1191 static const struct got_error *
1192 sync_timestamps(char *ondisk_path, unsigned char status,
1193 struct got_fileindex_entry *ie, struct stat *sb)
1195 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1196 return got_fileindex_entry_update(ie, ondisk_path,
1197 ie->blob_sha1, ie->commit_sha1, 1);
1199 return NULL;
1202 static const struct got_error *
1203 update_blob(struct got_worktree *worktree,
1204 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1205 struct got_tree_entry *te, const char *path,
1206 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1207 void *progress_arg)
1209 const struct got_error *err = NULL;
1210 struct got_blob_object *blob = NULL;
1211 char *ondisk_path;
1212 unsigned char status = GOT_STATUS_NO_CHANGE;
1213 struct stat sb;
1215 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1216 return got_error_from_errno("asprintf");
1218 if (ie) {
1219 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1220 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1221 goto done;
1223 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1224 if (err)
1225 goto done;
1226 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1227 sb.st_mode = got_fileindex_perms_to_st(ie);
1228 } else
1229 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1231 if (status == GOT_STATUS_OBSTRUCTED) {
1232 err = (*progress_cb)(progress_arg, status, path);
1233 goto done;
1236 if (ie && status != GOT_STATUS_MISSING) {
1237 if (got_fileindex_entry_has_commit(ie) &&
1238 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1239 SHA1_DIGEST_LENGTH) == 0) {
1240 err = sync_timestamps(ondisk_path, status, ie, &sb);
1241 if (err)
1242 goto done;
1243 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1244 path);
1245 goto done;
1247 if (got_fileindex_entry_has_blob(ie) &&
1248 memcmp(ie->blob_sha1, te->id->sha1,
1249 SHA1_DIGEST_LENGTH) == 0) {
1250 err = sync_timestamps(ondisk_path, status, ie, &sb);
1251 goto done;
1255 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1256 if (err)
1257 goto done;
1259 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1260 int update_timestamps;
1261 struct got_blob_object *blob2 = NULL;
1262 if (got_fileindex_entry_has_blob(ie)) {
1263 struct got_object_id id2;
1264 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1265 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1266 if (err)
1267 goto done;
1269 err = merge_blob(&update_timestamps, worktree, blob2,
1270 ondisk_path, path, sb.st_mode, blob,
1271 worktree->base_commit_id, repo,
1272 progress_cb, progress_arg);
1273 if (blob2)
1274 got_object_blob_close(blob2);
1275 if (err)
1276 goto done;
1278 * Do not update timestamps of files with local changes.
1279 * Otherwise, a future status walk would treat them as
1280 * unmodified files again.
1282 err = got_fileindex_entry_update(ie, ondisk_path,
1283 blob->id.sha1, worktree->base_commit_id->sha1,
1284 update_timestamps);
1285 } else if (status == GOT_STATUS_DELETE) {
1286 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1287 if (err)
1288 goto done;
1289 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1290 ondisk_path, path, blob, 0);
1291 if (err)
1292 goto done;
1293 } else {
1294 err = install_blob(worktree, ondisk_path, path, te->mode,
1295 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1296 repo, progress_cb, progress_arg);
1297 if (err)
1298 goto done;
1299 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1300 ondisk_path, path, blob, 1);
1301 if (err)
1302 goto done;
1304 got_object_blob_close(blob);
1305 done:
1306 free(ondisk_path);
1307 return err;
1310 static const struct got_error *
1311 remove_ondisk_file(const char *root_path, const char *path)
1313 const struct got_error *err = NULL;
1314 char *ondisk_path = NULL;
1316 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1317 return got_error_from_errno("asprintf");
1319 if (unlink(ondisk_path) == -1) {
1320 if (errno != ENOENT)
1321 err = got_error_from_errno2("unlink", ondisk_path);
1322 } else {
1323 char *parent = dirname(ondisk_path);
1324 while (parent && strcmp(parent, root_path) != 0) {
1325 if (rmdir(parent) == -1) {
1326 if (errno != ENOTEMPTY)
1327 err = got_error_from_errno2("rmdir",
1328 parent);
1329 break;
1331 parent = dirname(parent);
1334 free(ondisk_path);
1335 return err;
1338 static const struct got_error *
1339 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1340 struct got_fileindex_entry *ie, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 unsigned char status;
1345 struct stat sb;
1346 char *ondisk_path;
1348 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1349 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1351 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1352 == -1)
1353 return got_error_from_errno("asprintf");
1355 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1356 if (err)
1357 return err;
1359 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1360 status == GOT_STATUS_ADD) {
1361 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1362 if (err)
1363 return err;
1365 * Preserve the working file and change the deleted blob's
1366 * entry into a schedule-add entry.
1368 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1369 0);
1370 if (err)
1371 return err;
1372 } else {
1373 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1374 if (err)
1375 return err;
1376 if (status == GOT_STATUS_NO_CHANGE) {
1377 err = remove_ondisk_file(worktree->root_path, ie->path);
1378 if (err)
1379 return err;
1381 got_fileindex_entry_remove(fileindex, ie);
1384 return err;
1387 struct diff_cb_arg {
1388 struct got_fileindex *fileindex;
1389 struct got_worktree *worktree;
1390 struct got_repository *repo;
1391 got_worktree_checkout_cb progress_cb;
1392 void *progress_arg;
1393 got_cancel_cb cancel_cb;
1394 void *cancel_arg;
1397 static const struct got_error *
1398 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1399 struct got_tree_entry *te, const char *parent_path)
1401 struct diff_cb_arg *a = arg;
1403 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1404 return got_error(GOT_ERR_CANCELLED);
1406 return update_blob(a->worktree, a->fileindex, ie, te,
1407 ie->path, a->repo, a->progress_cb, a->progress_arg);
1410 static const struct got_error *
1411 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1413 struct diff_cb_arg *a = arg;
1415 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1416 return got_error(GOT_ERR_CANCELLED);
1418 return delete_blob(a->worktree, a->fileindex, ie,
1419 a->repo, a->progress_cb, a->progress_arg);
1422 static const struct got_error *
1423 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1425 struct diff_cb_arg *a = arg;
1426 const struct got_error *err;
1427 char *path;
1429 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1430 return got_error(GOT_ERR_CANCELLED);
1432 if (got_object_tree_entry_is_submodule(te))
1433 return NULL;
1435 if (asprintf(&path, "%s%s%s", parent_path,
1436 parent_path[0] ? "/" : "", te->name)
1437 == -1)
1438 return got_error_from_errno("asprintf");
1440 if (S_ISDIR(te->mode))
1441 err = add_dir_on_disk(a->worktree, path);
1442 else
1443 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1444 a->repo, a->progress_cb, a->progress_arg);
1446 free(path);
1447 return err;
1450 static const struct got_error *
1451 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1453 const struct got_error *err = NULL;
1454 char *uuidstr = NULL;
1455 uint32_t uuid_status;
1457 *refname = NULL;
1459 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1460 if (uuid_status != uuid_s_ok)
1461 return got_error_uuid(uuid_status, "uuid_to_string");
1463 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1464 == -1) {
1465 err = got_error_from_errno("asprintf");
1466 *refname = NULL;
1468 free(uuidstr);
1469 return err;
1472 const struct got_error *
1473 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1475 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1478 static const struct got_error *
1479 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1481 return get_ref_name(refname, worktree,
1482 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1485 static const struct got_error *
1486 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1488 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1491 static const struct got_error *
1492 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1494 return get_ref_name(refname, worktree,
1495 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1498 static const struct got_error *
1499 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1501 return get_ref_name(refname, worktree,
1502 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1505 static const struct got_error *
1506 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1508 return get_ref_name(refname, worktree,
1509 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1512 static const struct got_error *
1513 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1515 return get_ref_name(refname, worktree,
1516 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1519 static const struct got_error *
1520 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 return get_ref_name(refname, worktree,
1523 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1526 static const struct got_error *
1527 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1529 return get_ref_name(refname, worktree,
1530 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1533 const struct got_error *
1534 got_worktree_get_histedit_script_path(char **path,
1535 struct got_worktree *worktree)
1537 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1538 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1539 *path = NULL;
1540 return got_error_from_errno("asprintf");
1542 return NULL;
1546 * Prevent Git's garbage collector from deleting our base commit by
1547 * setting a reference to our base commit's ID.
1549 static const struct got_error *
1550 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_reference *ref = NULL;
1554 char *refname;
1556 err = got_worktree_get_base_ref_name(&refname, worktree);
1557 if (err)
1558 return err;
1560 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1561 if (err)
1562 goto done;
1564 err = got_ref_write(ref, repo);
1565 done:
1566 free(refname);
1567 if (ref)
1568 got_ref_close(ref);
1569 return err;
1572 static const struct got_error *
1573 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1575 const struct got_error *err = NULL;
1577 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1578 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1579 err = got_error_from_errno("asprintf");
1580 *fileindex_path = NULL;
1582 return err;
1586 static const struct got_error *
1587 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1588 struct got_worktree *worktree)
1590 const struct got_error *err = NULL;
1591 FILE *index = NULL;
1593 *fileindex_path = NULL;
1594 *fileindex = got_fileindex_alloc();
1595 if (*fileindex == NULL)
1596 return got_error_from_errno("got_fileindex_alloc");
1598 err = get_fileindex_path(fileindex_path, worktree);
1599 if (err)
1600 goto done;
1602 index = fopen(*fileindex_path, "rb");
1603 if (index == NULL) {
1604 if (errno != ENOENT)
1605 err = got_error_from_errno2("fopen", *fileindex_path);
1606 } else {
1607 err = got_fileindex_read(*fileindex, index);
1608 if (fclose(index) != 0 && err == NULL)
1609 err = got_error_from_errno("fclose");
1611 done:
1612 if (err) {
1613 free(*fileindex_path);
1614 *fileindex_path = NULL;
1615 got_fileindex_free(*fileindex);
1616 *fileindex = NULL;
1618 return err;
1621 struct bump_base_commit_id_arg {
1622 struct got_object_id *base_commit_id;
1623 const char *path;
1624 size_t path_len;
1625 const char *entry_name;
1626 got_worktree_checkout_cb progress_cb;
1627 void *progress_arg;
1630 /* Bump base commit ID of all files within an updated part of the work tree. */
1631 static const struct got_error *
1632 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1634 const struct got_error *err;
1635 struct bump_base_commit_id_arg *a = arg;
1637 if (a->entry_name) {
1638 if (strcmp(ie->path, a->path) != 0)
1639 return NULL;
1640 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1641 return NULL;
1643 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1644 SHA1_DIGEST_LENGTH) == 0)
1645 return NULL;
1647 if (a->progress_cb) {
1648 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1649 ie->path);
1650 if (err)
1651 return err;
1653 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1654 return NULL;
1657 static const struct got_error *
1658 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1660 const struct got_error *err = NULL;
1661 char *new_fileindex_path = NULL;
1662 FILE *new_index = NULL;
1664 err = got_opentemp_named(&new_fileindex_path, &new_index,
1665 fileindex_path);
1666 if (err)
1667 goto done;
1669 err = got_fileindex_write(fileindex, new_index);
1670 if (err)
1671 goto done;
1673 if (rename(new_fileindex_path, fileindex_path) != 0) {
1674 err = got_error_from_errno3("rename", new_fileindex_path,
1675 fileindex_path);
1676 unlink(new_fileindex_path);
1678 done:
1679 if (new_index)
1680 fclose(new_index);
1681 free(new_fileindex_path);
1682 return err;
1685 static const struct got_error *
1686 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1687 struct got_object_id **tree_id, const char *wt_relpath,
1688 struct got_worktree *worktree, struct got_repository *repo)
1690 const struct got_error *err = NULL;
1691 struct got_object_id *id = NULL;
1692 char *in_repo_path = NULL;
1693 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1695 *entry_type = GOT_OBJ_TYPE_ANY;
1696 *tree_relpath = NULL;
1697 *tree_id = NULL;
1699 if (wt_relpath[0] == '\0') {
1700 /* Check out all files within the work tree. */
1701 *entry_type = GOT_OBJ_TYPE_TREE;
1702 *tree_relpath = strdup("");
1703 if (*tree_relpath == NULL) {
1704 err = got_error_from_errno("strdup");
1705 goto done;
1707 err = got_object_id_by_path(tree_id, repo,
1708 worktree->base_commit_id, worktree->path_prefix);
1709 if (err)
1710 goto done;
1711 return NULL;
1714 /* Check out a subset of files in the work tree. */
1716 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1717 is_root_wt ? "" : "/", wt_relpath) == -1) {
1718 err = got_error_from_errno("asprintf");
1719 goto done;
1722 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1723 in_repo_path);
1724 if (err)
1725 goto done;
1727 free(in_repo_path);
1728 in_repo_path = NULL;
1730 err = got_object_get_type(entry_type, repo, id);
1731 if (err)
1732 goto done;
1734 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1735 /* Check out a single file. */
1736 if (strchr(wt_relpath, '/') == NULL) {
1737 /* Check out a single file in work tree's root dir. */
1738 in_repo_path = strdup(worktree->path_prefix);
1739 if (in_repo_path == NULL) {
1740 err = got_error_from_errno("strdup");
1741 goto done;
1743 *tree_relpath = strdup("");
1744 if (*tree_relpath == NULL) {
1745 err = got_error_from_errno("strdup");
1746 goto done;
1748 } else {
1749 /* Check out a single file in a subdirectory. */
1750 err = got_path_dirname(tree_relpath, wt_relpath);
1751 if (err)
1752 return err;
1753 if (asprintf(&in_repo_path, "%s%s%s",
1754 worktree->path_prefix, is_root_wt ? "" : "/",
1755 *tree_relpath) == -1) {
1756 err = got_error_from_errno("asprintf");
1757 goto done;
1760 err = got_object_id_by_path(tree_id, repo,
1761 worktree->base_commit_id, in_repo_path);
1762 } else {
1763 /* Check out all files within a subdirectory. */
1764 *tree_id = got_object_id_dup(id);
1765 if (*tree_id == NULL) {
1766 err = got_error_from_errno("got_object_id_dup");
1767 goto done;
1769 *tree_relpath = strdup(wt_relpath);
1770 if (*tree_relpath == NULL) {
1771 err = got_error_from_errno("strdup");
1772 goto done;
1775 done:
1776 free(id);
1777 free(in_repo_path);
1778 if (err) {
1779 *entry_type = GOT_OBJ_TYPE_ANY;
1780 free(*tree_relpath);
1781 *tree_relpath = NULL;
1782 free(*tree_id);
1783 *tree_id = NULL;
1785 return err;
1788 static const struct got_error *
1789 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1790 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1791 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1792 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1794 const struct got_error *err = NULL;
1795 struct got_commit_object *commit = NULL;
1796 struct got_tree_object *tree = NULL;
1797 struct got_fileindex_diff_tree_cb diff_cb;
1798 struct diff_cb_arg arg;
1800 err = ref_base_commit(worktree, repo);
1801 if (err)
1802 goto done;
1804 err = got_object_open_as_commit(&commit, repo,
1805 worktree->base_commit_id);
1806 if (err)
1807 goto done;
1809 err = got_object_open_as_tree(&tree, repo, tree_id);
1810 if (err)
1811 goto done;
1813 if (entry_name &&
1814 got_object_tree_find_entry(tree, entry_name) == NULL) {
1815 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1816 goto done;
1819 diff_cb.diff_old_new = diff_old_new;
1820 diff_cb.diff_old = diff_old;
1821 diff_cb.diff_new = diff_new;
1822 arg.fileindex = fileindex;
1823 arg.worktree = worktree;
1824 arg.repo = repo;
1825 arg.progress_cb = progress_cb;
1826 arg.progress_arg = progress_arg;
1827 arg.cancel_cb = cancel_cb;
1828 arg.cancel_arg = cancel_arg;
1829 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1830 entry_name, repo, &diff_cb, &arg);
1831 done:
1832 if (tree)
1833 got_object_tree_close(tree);
1834 if (commit)
1835 got_object_commit_close(commit);
1836 return err;
1839 const struct got_error *
1840 got_worktree_checkout_files(struct got_worktree *worktree,
1841 struct got_pathlist_head *paths, struct got_repository *repo,
1842 got_worktree_checkout_cb progress_cb, void *progress_arg,
1843 got_cancel_cb cancel_cb, void *cancel_arg)
1845 const struct got_error *err = NULL, *sync_err, *unlockerr;
1846 struct got_commit_object *commit = NULL;
1847 struct got_tree_object *tree = NULL;
1848 struct got_fileindex *fileindex = NULL;
1849 char *fileindex_path = NULL;
1850 struct got_pathlist_entry *pe;
1851 struct tree_path_data {
1852 SIMPLEQ_ENTRY(tree_path_data) entry;
1853 struct got_object_id *tree_id;
1854 int entry_type;
1855 char *relpath;
1856 char *entry_name;
1857 } *tpd = NULL;
1858 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1860 SIMPLEQ_INIT(&tree_paths);
1862 err = lock_worktree(worktree, LOCK_EX);
1863 if (err)
1864 return err;
1866 /* Map all specified paths to in-repository trees. */
1867 TAILQ_FOREACH(pe, paths, entry) {
1868 tpd = malloc(sizeof(*tpd));
1869 if (tpd == NULL) {
1870 err = got_error_from_errno("malloc");
1871 goto done;
1874 err = find_tree_entry_for_checkout(&tpd->entry_type,
1875 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1876 if (err) {
1877 free(tpd);
1878 goto done;
1881 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1882 err = got_path_basename(&tpd->entry_name, pe->path);
1883 if (err) {
1884 free(tpd->relpath);
1885 free(tpd->tree_id);
1886 free(tpd);
1887 goto done;
1889 } else
1890 tpd->entry_name = NULL;
1892 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1896 * Read the file index.
1897 * Checking out files is supposed to be an idempotent operation.
1898 * If the on-disk file index is incomplete we will try to complete it.
1900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1901 if (err)
1902 goto done;
1904 tpd = SIMPLEQ_FIRST(&tree_paths);
1905 TAILQ_FOREACH(pe, paths, entry) {
1906 struct bump_base_commit_id_arg bbc_arg;
1908 err = checkout_files(worktree, fileindex, tpd->relpath,
1909 tpd->tree_id, tpd->entry_name, repo,
1910 progress_cb, progress_arg, cancel_cb, cancel_arg);
1911 if (err)
1912 break;
1914 bbc_arg.base_commit_id = worktree->base_commit_id;
1915 bbc_arg.entry_name = tpd->entry_name;
1916 bbc_arg.path = pe->path;
1917 bbc_arg.path_len = pe->path_len;
1918 bbc_arg.progress_cb = progress_cb;
1919 bbc_arg.progress_arg = progress_arg;
1920 err = got_fileindex_for_each_entry_safe(fileindex,
1921 bump_base_commit_id, &bbc_arg);
1922 if (err)
1923 break;
1925 tpd = SIMPLEQ_NEXT(tpd, entry);
1927 sync_err = sync_fileindex(fileindex, fileindex_path);
1928 if (sync_err && err == NULL)
1929 err = sync_err;
1930 done:
1931 free(fileindex_path);
1932 if (tree)
1933 got_object_tree_close(tree);
1934 if (commit)
1935 got_object_commit_close(commit);
1936 if (fileindex)
1937 got_fileindex_free(fileindex);
1938 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1939 tpd = SIMPLEQ_FIRST(&tree_paths);
1940 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1941 free(tpd->relpath);
1942 free(tpd->tree_id);
1943 free(tpd);
1945 unlockerr = lock_worktree(worktree, LOCK_SH);
1946 if (unlockerr && err == NULL)
1947 err = unlockerr;
1948 return err;
1951 struct merge_file_cb_arg {
1952 struct got_worktree *worktree;
1953 struct got_fileindex *fileindex;
1954 got_worktree_checkout_cb progress_cb;
1955 void *progress_arg;
1956 got_cancel_cb cancel_cb;
1957 void *cancel_arg;
1958 struct got_object_id *commit_id2;
1961 static const struct got_error *
1962 merge_file_cb(void *arg, struct got_blob_object *blob1,
1963 struct got_blob_object *blob2, struct got_object_id *id1,
1964 struct got_object_id *id2, const char *path1, const char *path2,
1965 struct got_repository *repo)
1967 static const struct got_error *err = NULL;
1968 struct merge_file_cb_arg *a = arg;
1969 struct got_fileindex_entry *ie;
1970 char *ondisk_path = NULL;
1971 struct stat sb;
1972 unsigned char status;
1973 int local_changes_subsumed;
1975 if (blob1 && blob2) {
1976 ie = got_fileindex_entry_get(a->fileindex, path2,
1977 strlen(path2));
1978 if (ie == NULL)
1979 return (*a->progress_cb)(a->progress_arg,
1980 GOT_STATUS_MISSING, path2);
1982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1983 path2) == -1)
1984 return got_error_from_errno("asprintf");
1986 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1987 if (err)
1988 goto done;
1990 if (status == GOT_STATUS_DELETE) {
1991 err = (*a->progress_cb)(a->progress_arg,
1992 GOT_STATUS_MERGE, path2);
1993 goto done;
1995 if (status != GOT_STATUS_NO_CHANGE &&
1996 status != GOT_STATUS_MODIFY &&
1997 status != GOT_STATUS_CONFLICT &&
1998 status != GOT_STATUS_ADD) {
1999 err = (*a->progress_cb)(a->progress_arg, status, path2);
2000 goto done;
2003 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2004 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2005 a->progress_cb, a->progress_arg);
2006 } else if (blob1) {
2007 ie = got_fileindex_entry_get(a->fileindex, path1,
2008 strlen(path1));
2009 if (ie == NULL)
2010 return (*a->progress_cb)(a->progress_arg,
2011 GOT_STATUS_MISSING, path2);
2013 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2014 path1) == -1)
2015 return got_error_from_errno("asprintf");
2017 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2018 if (err)
2019 goto done;
2021 switch (status) {
2022 case GOT_STATUS_NO_CHANGE:
2023 err = (*a->progress_cb)(a->progress_arg,
2024 GOT_STATUS_DELETE, path1);
2025 if (err)
2026 goto done;
2027 err = remove_ondisk_file(a->worktree->root_path, path1);
2028 if (err)
2029 goto done;
2030 if (ie)
2031 got_fileindex_entry_mark_deleted_from_disk(ie);
2032 break;
2033 case GOT_STATUS_DELETE:
2034 case GOT_STATUS_MISSING:
2035 err = (*a->progress_cb)(a->progress_arg,
2036 GOT_STATUS_DELETE, path1);
2037 if (err)
2038 goto done;
2039 if (ie)
2040 got_fileindex_entry_mark_deleted_from_disk(ie);
2041 break;
2042 case GOT_STATUS_ADD:
2043 case GOT_STATUS_MODIFY:
2044 case GOT_STATUS_CONFLICT:
2045 err = (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_CANNOT_DELETE, path1);
2047 if (err)
2048 goto done;
2049 break;
2050 case GOT_STATUS_OBSTRUCTED:
2051 err = (*a->progress_cb)(a->progress_arg, status, path1);
2052 if (err)
2053 goto done;
2054 break;
2055 default:
2056 break;
2058 } else if (blob2) {
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2060 path2) == -1)
2061 return got_error_from_errno("asprintf");
2062 ie = got_fileindex_entry_get(a->fileindex, path2,
2063 strlen(path2));
2064 if (ie) {
2065 err = get_file_status(&status, &sb, ie, ondisk_path,
2066 repo);
2067 if (err)
2068 goto done;
2069 if (status != GOT_STATUS_NO_CHANGE &&
2070 status != GOT_STATUS_MODIFY &&
2071 status != GOT_STATUS_CONFLICT &&
2072 status != GOT_STATUS_ADD) {
2073 err = (*a->progress_cb)(a->progress_arg,
2074 status, path2);
2075 goto done;
2077 err = merge_blob(&local_changes_subsumed, a->worktree,
2078 NULL, ondisk_path, path2, sb.st_mode, blob2,
2079 a->commit_id2, repo,
2080 a->progress_cb, a->progress_arg);
2081 if (status == GOT_STATUS_DELETE) {
2082 err = update_blob_fileindex_entry(a->worktree,
2083 a->fileindex, ie, ondisk_path, ie->path,
2084 blob2, 0);
2085 if (err)
2086 goto done;
2088 } else {
2089 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2090 err = install_blob(a->worktree, ondisk_path, path2,
2091 /* XXX get this from parent tree! */
2092 GOT_DEFAULT_FILE_MODE,
2093 sb.st_mode, blob2, 0, 0, repo,
2094 a->progress_cb, a->progress_arg);
2095 if (err)
2096 goto done;
2097 err = got_fileindex_entry_alloc(&ie,
2098 ondisk_path, path2, NULL, NULL);
2099 if (err)
2100 goto done;
2101 err = got_fileindex_entry_add(a->fileindex, ie);
2102 if (err) {
2103 got_fileindex_entry_free(ie);
2104 goto done;
2108 done:
2109 free(ondisk_path);
2110 return err;
2113 struct check_merge_ok_arg {
2114 struct got_worktree *worktree;
2115 struct got_repository *repo;
2118 static const struct got_error *
2119 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2121 const struct got_error *err = NULL;
2122 struct check_merge_ok_arg *a = arg;
2123 unsigned char status;
2124 struct stat sb;
2125 char *ondisk_path;
2127 /* Reject merges into a work tree with mixed base commits. */
2128 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2129 SHA1_DIGEST_LENGTH))
2130 return got_error(GOT_ERR_MIXED_COMMITS);
2132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2133 == -1)
2134 return got_error_from_errno("asprintf");
2136 /* Reject merges into a work tree with conflicted files. */
2137 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2138 if (err)
2139 return err;
2140 if (status == GOT_STATUS_CONFLICT)
2141 return got_error(GOT_ERR_CONFLICTS);
2143 return NULL;
2146 static const struct got_error *
2147 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 const char *fileindex_path, struct got_object_id *commit_id1,
2149 struct got_object_id *commit_id2, struct got_repository *repo,
2150 got_worktree_checkout_cb progress_cb, void *progress_arg,
2151 got_cancel_cb cancel_cb, void *cancel_arg)
2153 const struct got_error *err = NULL, *sync_err;
2154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2156 struct merge_file_cb_arg arg;
2158 if (commit_id1) {
2159 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2160 worktree->path_prefix);
2161 if (err)
2162 goto done;
2164 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2165 if (err)
2166 goto done;
2169 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2170 worktree->path_prefix);
2171 if (err)
2172 goto done;
2174 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2175 if (err)
2176 goto done;
2178 arg.worktree = worktree;
2179 arg.fileindex = fileindex;
2180 arg.progress_cb = progress_cb;
2181 arg.progress_arg = progress_arg;
2182 arg.cancel_cb = cancel_cb;
2183 arg.cancel_arg = cancel_arg;
2184 arg.commit_id2 = commit_id2;
2185 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2186 sync_err = sync_fileindex(fileindex, fileindex_path);
2187 if (sync_err && err == NULL)
2188 err = sync_err;
2189 done:
2190 if (tree1)
2191 got_object_tree_close(tree1);
2192 if (tree2)
2193 got_object_tree_close(tree2);
2194 return err;
2197 const struct got_error *
2198 got_worktree_merge_files(struct got_worktree *worktree,
2199 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2200 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2201 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2203 const struct got_error *err, *unlockerr;
2204 char *fileindex_path = NULL;
2205 struct got_fileindex *fileindex = NULL;
2206 struct check_merge_ok_arg mok_arg;
2208 err = lock_worktree(worktree, LOCK_EX);
2209 if (err)
2210 return err;
2212 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2213 if (err)
2214 goto done;
2216 mok_arg.worktree = worktree;
2217 mok_arg.repo = repo;
2218 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2219 &mok_arg);
2220 if (err)
2221 goto done;
2223 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2224 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2225 done:
2226 if (fileindex)
2227 got_fileindex_free(fileindex);
2228 free(fileindex_path);
2229 unlockerr = lock_worktree(worktree, LOCK_SH);
2230 if (unlockerr && err == NULL)
2231 err = unlockerr;
2232 return err;
2235 struct diff_dir_cb_arg {
2236 struct got_fileindex *fileindex;
2237 struct got_worktree *worktree;
2238 const char *status_path;
2239 size_t status_path_len;
2240 struct got_repository *repo;
2241 got_worktree_status_cb status_cb;
2242 void *status_arg;
2243 got_cancel_cb cancel_cb;
2244 void *cancel_arg;
2245 /* A pathlist containing per-directory pathlists of ignore patterns. */
2246 struct got_pathlist_head ignores;
2249 static const struct got_error *
2250 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2251 got_worktree_status_cb status_cb, void *status_arg,
2252 struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 unsigned char status = GOT_STATUS_NO_CHANGE;
2256 unsigned char staged_status = get_staged_status(ie);
2257 struct stat sb;
2258 struct got_object_id blob_id, commit_id, staged_blob_id;
2259 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2260 struct got_object_id *staged_blob_idp = NULL;
2262 err = get_file_status(&status, &sb, ie, abspath, repo);
2263 if (err)
2264 return err;
2266 if (status == GOT_STATUS_NO_CHANGE &&
2267 staged_status == GOT_STATUS_NO_CHANGE)
2268 return NULL;
2270 if (got_fileindex_entry_has_blob(ie)) {
2271 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2272 blob_idp = &blob_id;
2274 if (got_fileindex_entry_has_commit(ie)) {
2275 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2276 commit_idp = &commit_id;
2278 if (staged_status == GOT_STATUS_ADD ||
2279 staged_status == GOT_STATUS_MODIFY) {
2280 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2281 SHA1_DIGEST_LENGTH);
2282 staged_blob_idp = &staged_blob_id;
2285 return (*status_cb)(status_arg, status, staged_status,
2286 ie->path, blob_idp, staged_blob_idp, commit_idp);
2289 static const struct got_error *
2290 status_old_new(void *arg, struct got_fileindex_entry *ie,
2291 struct dirent *de, const char *parent_path)
2293 const struct got_error *err = NULL;
2294 struct diff_dir_cb_arg *a = arg;
2295 char *abspath;
2297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2298 return got_error(GOT_ERR_CANCELLED);
2300 if (got_path_cmp(parent_path, a->status_path,
2301 strlen(parent_path), a->status_path_len) != 0 &&
2302 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2303 return NULL;
2305 if (parent_path[0]) {
2306 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2307 parent_path, de->d_name) == -1)
2308 return got_error_from_errno("asprintf");
2309 } else {
2310 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2311 de->d_name) == -1)
2312 return got_error_from_errno("asprintf");
2315 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2316 a->repo);
2317 free(abspath);
2318 return err;
2321 static const struct got_error *
2322 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2324 struct diff_dir_cb_arg *a = arg;
2325 struct got_object_id blob_id, commit_id;
2326 unsigned char status;
2328 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2329 return got_error(GOT_ERR_CANCELLED);
2331 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2332 return NULL;
2334 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2335 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2336 if (got_fileindex_entry_has_file_on_disk(ie))
2337 status = GOT_STATUS_MISSING;
2338 else
2339 status = GOT_STATUS_DELETE;
2340 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2341 ie->path, &blob_id, NULL, &commit_id);
2344 void
2345 free_ignorelist(struct got_pathlist_head *ignorelist)
2347 struct got_pathlist_entry *pe;
2349 TAILQ_FOREACH(pe, ignorelist, entry)
2350 free((char *)pe->path);
2351 got_pathlist_free(ignorelist);
2354 void
2355 free_ignores(struct got_pathlist_head *ignores)
2357 struct got_pathlist_entry *pe;
2359 TAILQ_FOREACH(pe, ignores, entry) {
2360 struct got_pathlist_head *ignorelist = pe->data;
2361 free_ignorelist(ignorelist);
2362 free((char *)pe->path);
2364 got_pathlist_free(ignores);
2367 static const struct got_error *
2368 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2370 const struct got_error *err = NULL;
2371 struct got_pathlist_entry *pe = NULL;
2372 struct got_pathlist_head *ignorelist;
2373 char *line = NULL, *pattern, *dirpath = NULL;
2374 size_t linesize = 0;
2375 ssize_t linelen;
2377 ignorelist = calloc(1, sizeof(*ignorelist));
2378 if (ignorelist == NULL)
2379 return got_error_from_errno("calloc");
2380 TAILQ_INIT(ignorelist);
2382 while ((linelen = getline(&line, &linesize, f)) != -1) {
2383 if (linelen > 0 && line[linelen - 1] == '\n')
2384 line[linelen - 1] = '\0';
2385 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2386 line) == -1) {
2387 err = got_error_from_errno("asprintf");
2388 goto done;
2390 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2391 if (err)
2392 goto done;
2394 if (ferror(f)) {
2395 err = got_error_from_errno("getline");
2396 goto done;
2399 dirpath = strdup(path);
2400 if (dirpath == NULL) {
2401 err = got_error_from_errno("strdup");
2402 goto done;
2404 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2405 done:
2406 free(line);
2407 if (err || pe == NULL) {
2408 free(dirpath);
2409 free_ignorelist(ignorelist);
2411 return err;
2414 int
2415 match_ignores(struct got_pathlist_head *ignores, const char *path)
2417 struct got_pathlist_entry *pe;
2420 * The ignores pathlist contains ignore lists from children before
2421 * parents, so we can find the most specific ignorelist by walking
2422 * ignores backwards.
2424 pe = TAILQ_LAST(ignores, got_pathlist_head);
2425 while (pe) {
2426 if (got_path_is_child(path, pe->path, pe->path_len)) {
2427 struct got_pathlist_head *ignorelist = pe->data;
2428 struct got_pathlist_entry *pi;
2429 TAILQ_FOREACH(pi, ignorelist, entry) {
2430 if (fnmatch(pi->path, path,
2431 FNM_PATHNAME | FNM_LEADING_DIR))
2432 continue;
2433 return 1;
2436 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2439 return 0;
2442 static const struct got_error *
2443 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2444 const char *path)
2446 const struct got_error *err = NULL;
2447 char *ignorespath;
2448 FILE *ignoresfile = NULL;
2450 /* TODO: read .gitignores as well... */
2451 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2452 path[0] ? "/" : "") == -1)
2453 return got_error_from_errno("asprintf");
2455 ignoresfile = fopen(ignorespath, "r");
2456 if (ignoresfile == NULL) {
2457 if (errno != ENOENT && errno != EACCES)
2458 err = got_error_from_errno2("fopen",
2459 ignorespath);
2460 } else
2461 err = read_ignores(ignores, path, ignoresfile);
2463 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2464 err = got_error_from_errno2("flose", path);
2465 free(ignorespath);
2466 return err;
2469 static const struct got_error *
2470 status_new(void *arg, struct dirent *de, const char *parent_path)
2472 const struct got_error *err = NULL;
2473 struct diff_dir_cb_arg *a = arg;
2474 char *path = NULL;
2476 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2477 return got_error(GOT_ERR_CANCELLED);
2479 /* XXX ignore symlinks for now */
2480 if (de->d_type == DT_LNK)
2481 return NULL;
2483 if (parent_path[0]) {
2484 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2485 return got_error_from_errno("asprintf");
2486 } else {
2487 path = de->d_name;
2490 if (de->d_type == DT_DIR)
2491 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2492 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2493 && !match_ignores(&a->ignores, path))
2494 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2495 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2496 if (parent_path[0])
2497 free(path);
2498 return err;
2501 static const struct got_error *
2502 report_single_file_status(const char *path, const char *ondisk_path,
2503 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2504 void *status_arg, struct got_repository *repo)
2506 struct got_fileindex_entry *ie;
2507 struct stat sb;
2509 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2510 if (ie)
2511 return report_file_status(ie, ondisk_path, status_cb,
2512 status_arg, repo);
2514 if (lstat(ondisk_path, &sb) == -1) {
2515 if (errno != ENOENT)
2516 return got_error_from_errno2("lstat", ondisk_path);
2517 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2518 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2519 return NULL;
2522 if (S_ISREG(sb.st_mode))
2523 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2526 return NULL;
2529 static const struct got_error *
2530 worktree_status(struct got_worktree *worktree, const char *path,
2531 struct got_fileindex *fileindex, struct got_repository *repo,
2532 got_worktree_status_cb status_cb, void *status_arg,
2533 got_cancel_cb cancel_cb, void *cancel_arg)
2535 const struct got_error *err = NULL;
2536 DIR *workdir = NULL;
2537 struct got_fileindex_diff_dir_cb fdiff_cb;
2538 struct diff_dir_cb_arg arg;
2539 char *ondisk_path = NULL;
2541 if (asprintf(&ondisk_path, "%s%s%s",
2542 worktree->root_path, path[0] ? "/" : "", path) == -1)
2543 return got_error_from_errno("asprintf");
2545 workdir = opendir(ondisk_path);
2546 if (workdir == NULL) {
2547 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2548 err = got_error_from_errno2("opendir", ondisk_path);
2549 else
2550 err = report_single_file_status(path, ondisk_path,
2551 fileindex, status_cb, status_arg, repo);
2552 } else {
2553 fdiff_cb.diff_old_new = status_old_new;
2554 fdiff_cb.diff_old = status_old;
2555 fdiff_cb.diff_new = status_new;
2556 arg.fileindex = fileindex;
2557 arg.worktree = worktree;
2558 arg.status_path = path;
2559 arg.status_path_len = strlen(path);
2560 arg.repo = repo;
2561 arg.status_cb = status_cb;
2562 arg.status_arg = status_arg;
2563 arg.cancel_cb = cancel_cb;
2564 arg.cancel_arg = cancel_arg;
2565 TAILQ_INIT(&arg.ignores);
2566 err = add_ignores(&arg.ignores, worktree->root_path, path);
2567 if (err == NULL)
2568 err = got_fileindex_diff_dir(fileindex, workdir,
2569 worktree->root_path, path, repo, &fdiff_cb, &arg);
2570 free_ignores(&arg.ignores);
2573 if (workdir)
2574 closedir(workdir);
2575 free(ondisk_path);
2576 return err;
2579 const struct got_error *
2580 got_worktree_status(struct got_worktree *worktree,
2581 struct got_pathlist_head *paths, struct got_repository *repo,
2582 got_worktree_status_cb status_cb, void *status_arg,
2583 got_cancel_cb cancel_cb, void *cancel_arg)
2585 const struct got_error *err = NULL;
2586 char *fileindex_path = NULL;
2587 struct got_fileindex *fileindex = NULL;
2588 struct got_pathlist_entry *pe;
2590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2591 if (err)
2592 return err;
2594 TAILQ_FOREACH(pe, paths, entry) {
2595 err = worktree_status(worktree, pe->path, fileindex, repo,
2596 status_cb, status_arg, cancel_cb, cancel_arg);
2597 if (err)
2598 break;
2600 free(fileindex_path);
2601 got_fileindex_free(fileindex);
2602 return err;
2605 const struct got_error *
2606 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2607 const char *arg)
2609 const struct got_error *err = NULL;
2610 char *resolved, *cwd = NULL, *path = NULL;
2611 size_t len;
2613 *wt_path = NULL;
2615 resolved = realpath(arg, NULL);
2616 if (resolved == NULL) {
2617 if (errno != ENOENT)
2618 return got_error_from_errno2("realpath", arg);
2619 cwd = getcwd(NULL, 0);
2620 if (cwd == NULL)
2621 return got_error_from_errno("getcwd");
2622 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2623 err = got_error_from_errno("asprintf");
2624 goto done;
2628 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2629 strlen(got_worktree_get_root_path(worktree)))) {
2630 err = got_error(GOT_ERR_BAD_PATH);
2631 goto done;
2634 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2635 err = got_path_skip_common_ancestor(&path,
2636 got_worktree_get_root_path(worktree), resolved);
2637 if (err)
2638 goto done;
2639 } else {
2640 path = strdup("");
2641 if (path == NULL) {
2642 err = got_error_from_errno("strdup");
2643 goto done;
2647 /* XXX status walk can't deal with trailing slash! */
2648 len = strlen(path);
2649 while (len > 0 && path[len - 1] == '/') {
2650 path[len - 1] = '\0';
2651 len--;
2653 done:
2654 free(resolved);
2655 free(cwd);
2656 if (err == NULL)
2657 *wt_path = path;
2658 else
2659 free(path);
2660 return err;
2663 static const struct got_error *
2664 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2665 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2666 struct got_repository *repo)
2668 const struct got_error *err = NULL;
2669 struct got_fileindex_entry *ie;
2670 unsigned char status;
2671 struct stat sb;
2673 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2674 if (ie) {
2675 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2676 if (err)
2677 return err;
2678 /* Re-adding an existing entry is a no-op. */
2679 if (status == GOT_STATUS_ADD)
2680 return NULL;
2681 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2684 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2685 if (err)
2686 return err;
2688 err = got_fileindex_entry_add(fileindex, ie);
2689 if (err) {
2690 got_fileindex_entry_free(ie);
2691 return err;
2694 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2697 const struct got_error *
2698 got_worktree_schedule_add(struct got_worktree *worktree,
2699 struct got_pathlist_head *paths,
2700 got_worktree_status_cb status_cb, void *status_arg,
2701 struct got_repository *repo)
2703 struct got_fileindex *fileindex = NULL;
2704 char *fileindex_path = NULL;
2705 const struct got_error *err = NULL, *sync_err, *unlockerr;
2706 struct got_pathlist_entry *pe;
2708 err = lock_worktree(worktree, LOCK_EX);
2709 if (err)
2710 return err;
2712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2713 if (err)
2714 goto done;
2716 TAILQ_FOREACH(pe, paths, entry) {
2717 char *ondisk_path;
2718 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2719 pe->path) == -1)
2720 return got_error_from_errno("asprintf");
2721 err = schedule_addition(ondisk_path, fileindex, pe->path,
2722 status_cb, status_arg, repo);
2723 free(ondisk_path);
2724 if (err)
2725 break;
2727 sync_err = sync_fileindex(fileindex, fileindex_path);
2728 if (sync_err && err == NULL)
2729 err = sync_err;
2730 done:
2731 free(fileindex_path);
2732 if (fileindex)
2733 got_fileindex_free(fileindex);
2734 unlockerr = lock_worktree(worktree, LOCK_SH);
2735 if (unlockerr && err == NULL)
2736 err = unlockerr;
2737 return err;
2740 static const struct got_error *
2741 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2742 const char *relpath, int delete_local_mods,
2743 got_worktree_status_cb status_cb, void *status_arg,
2744 struct got_repository *repo)
2746 const struct got_error *err = NULL;
2747 struct got_fileindex_entry *ie = NULL;
2748 unsigned char status, staged_status;
2749 struct stat sb;
2751 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2752 if (ie == NULL)
2753 return got_error(GOT_ERR_BAD_PATH);
2755 staged_status = get_staged_status(ie);
2756 if (staged_status != GOT_STATUS_NO_CHANGE) {
2757 if (staged_status == GOT_STATUS_DELETE)
2758 return NULL;
2759 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2762 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2763 if (err)
2764 return err;
2766 if (status != GOT_STATUS_NO_CHANGE) {
2767 if (status == GOT_STATUS_DELETE)
2768 return NULL;
2769 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2770 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2771 if (status != GOT_STATUS_MODIFY &&
2772 status != GOT_STATUS_MISSING)
2773 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2776 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2777 return got_error_from_errno2("unlink", ondisk_path);
2779 got_fileindex_entry_mark_deleted_from_disk(ie);
2780 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2783 const struct got_error *
2784 got_worktree_schedule_delete(struct got_worktree *worktree,
2785 struct got_pathlist_head *paths, int delete_local_mods,
2786 got_worktree_status_cb status_cb, void *status_arg,
2787 struct got_repository *repo)
2789 struct got_fileindex *fileindex = NULL;
2790 char *fileindex_path = NULL;
2791 const struct got_error *err = NULL, *sync_err, *unlockerr;
2792 struct got_pathlist_entry *pe;
2794 err = lock_worktree(worktree, LOCK_EX);
2795 if (err)
2796 return err;
2798 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2799 if (err)
2800 goto done;
2802 TAILQ_FOREACH(pe, paths, entry) {
2803 char *ondisk_path;
2804 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2805 pe->path) == -1)
2806 return got_error_from_errno("asprintf");
2807 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2808 delete_local_mods, status_cb, status_arg, repo);
2809 free(ondisk_path);
2810 if (err)
2811 break;
2813 sync_err = sync_fileindex(fileindex, fileindex_path);
2814 if (sync_err && err == NULL)
2815 err = sync_err;
2816 done:
2817 free(fileindex_path);
2818 if (fileindex)
2819 got_fileindex_free(fileindex);
2820 unlockerr = lock_worktree(worktree, LOCK_SH);
2821 if (unlockerr && err == NULL)
2822 err = unlockerr;
2823 return err;
2826 static const struct got_error *
2827 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2829 const struct got_error *err = NULL;
2830 char *line = NULL;
2831 size_t linesize = 0, n;
2832 ssize_t linelen;
2834 linelen = getline(&line, &linesize, infile);
2835 if (linelen == -1) {
2836 if (ferror(infile)) {
2837 err = got_error_from_errno("getline");
2838 goto done;
2840 return NULL;
2842 if (outfile) {
2843 n = fwrite(line, 1, linelen, outfile);
2844 if (n != linelen) {
2845 err = got_ferror(outfile, GOT_ERR_IO);
2846 goto done;
2849 if (rejectfile) {
2850 n = fwrite(line, 1, linelen, rejectfile);
2851 if (n != linelen)
2852 err = got_ferror(outfile, GOT_ERR_IO);
2854 done:
2855 free(line);
2856 return err;
2859 static const struct got_error *
2860 skip_one_line(FILE *f)
2862 char *line = NULL;
2863 size_t linesize = 0;
2864 ssize_t linelen;
2866 linelen = getline(&line, &linesize, f);
2867 if (linelen == -1) {
2868 if (ferror(f))
2869 return got_error_from_errno("getline");
2870 return NULL;
2872 free(line);
2873 return NULL;
2876 static const struct got_error *
2877 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2878 int start_old, int end_old, int start_new, int end_new,
2879 FILE *outfile, FILE *rejectfile)
2881 const struct got_error *err;
2883 /* Copy old file's lines leading up to patch. */
2884 while (!feof(f1) && *line_cur1 < start_old) {
2885 err = copy_one_line(f1, outfile, NULL);
2886 if (err)
2887 return err;
2888 (*line_cur1)++;
2890 /* Skip new file's lines leading up to patch. */
2891 while (!feof(f2) && *line_cur2 < start_new) {
2892 if (rejectfile)
2893 err = copy_one_line(f2, NULL, rejectfile);
2894 else
2895 err = skip_one_line(f2);
2896 if (err)
2897 return err;
2898 (*line_cur2)++;
2900 /* Copy patched lines. */
2901 while (!feof(f2) && *line_cur2 <= end_new) {
2902 err = copy_one_line(f2, outfile, NULL);
2903 if (err)
2904 return err;
2905 (*line_cur2)++;
2907 /* Skip over old file's replaced lines. */
2908 while (!feof(f1) && *line_cur1 <= end_old) {
2909 if (rejectfile)
2910 err = copy_one_line(f1, NULL, rejectfile);
2911 else
2912 err = skip_one_line(f1);
2913 if (err)
2914 return err;
2915 (*line_cur1)++;
2918 return NULL;
2921 static const struct got_error *
2922 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2923 FILE *outfile, FILE *rejectfile)
2925 const struct got_error *err;
2927 if (outfile) {
2928 /* Copy old file's lines until EOF. */
2929 while (!feof(f1)) {
2930 err = copy_one_line(f1, outfile, NULL);
2931 if (err)
2932 return err;
2933 (*line_cur1)++;
2936 if (rejectfile) {
2937 /* Copy new file's lines until EOF. */
2938 while (!feof(f2)) {
2939 err = copy_one_line(f2, NULL, rejectfile);
2940 if (err)
2941 return err;
2942 (*line_cur2)++;
2946 return NULL;
2949 static const struct got_error *
2950 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2951 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2952 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2953 int *line_cur2, FILE *outfile, FILE *rejectfile,
2954 got_worktree_patch_cb patch_cb, void *patch_arg)
2956 const struct got_error *err = NULL;
2957 int start_old = change->cv.a;
2958 int end_old = change->cv.b;
2959 int start_new = change->cv.c;
2960 int end_new = change->cv.d;
2961 long pos1, pos2;
2962 FILE *hunkfile;
2964 *choice = GOT_PATCH_CHOICE_NONE;
2966 hunkfile = got_opentemp();
2967 if (hunkfile == NULL)
2968 return got_error_from_errno("got_opentemp");
2970 pos1 = ftell(f1);
2971 pos2 = ftell(f2);
2973 /* XXX TODO needs error checking */
2974 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2976 if (fseek(f1, pos1, SEEK_SET) == -1) {
2977 err = got_ferror(f1, GOT_ERR_IO);
2978 goto done;
2980 if (fseek(f2, pos2, SEEK_SET) == -1) {
2981 err = got_ferror(f1, GOT_ERR_IO);
2982 goto done;
2984 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2985 err = got_ferror(hunkfile, GOT_ERR_IO);
2986 goto done;
2989 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2990 hunkfile, n, nchanges);
2991 if (err)
2992 goto done;
2994 switch (*choice) {
2995 case GOT_PATCH_CHOICE_YES:
2996 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2997 end_old, start_new, end_new, outfile, rejectfile);
2998 break;
2999 case GOT_PATCH_CHOICE_NO:
3000 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3001 end_old, start_new, end_new, rejectfile, outfile);
3002 break;
3003 case GOT_PATCH_CHOICE_QUIT:
3004 break;
3005 default:
3006 err = got_error(GOT_ERR_PATCH_CHOICE);
3007 break;
3009 done:
3010 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3011 err = got_error_from_errno("fclose");
3012 return err;
3015 struct revert_file_args {
3016 struct got_worktree *worktree;
3017 struct got_fileindex *fileindex;
3018 got_worktree_checkout_cb progress_cb;
3019 void *progress_arg;
3020 got_worktree_patch_cb patch_cb;
3021 void *patch_arg;
3022 struct got_repository *repo;
3025 static const struct got_error *
3026 create_patched_content(char **path_outfile, int reverse_patch,
3027 struct got_object_id *blob_id, const char *path2,
3028 const char *relpath, struct got_repository *repo,
3029 got_worktree_patch_cb patch_cb, void *patch_arg)
3031 const struct got_error *err;
3032 struct got_blob_object *blob = NULL;
3033 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3034 char *path1 = NULL, *id_str = NULL;
3035 struct stat sb1, sb2;
3036 struct got_diff_changes *changes = NULL;
3037 struct got_diff_state *ds = NULL;
3038 struct got_diff_args *args = NULL;
3039 struct got_diff_change *change;
3040 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3041 int n = 0;
3043 *path_outfile = NULL;
3045 err = got_object_id_str(&id_str, blob_id);
3046 if (err)
3047 return err;
3049 f2 = fopen(path2, "r");
3050 if (f2 == NULL) {
3051 err = got_error_from_errno2("fopen", path2);
3052 goto done;
3055 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3056 if (err)
3057 goto done;
3059 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3060 if (err)
3061 goto done;
3063 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3064 if (err)
3065 goto done;
3067 if (stat(path1, &sb1) == -1) {
3068 err = got_error_from_errno2("stat", path1);
3069 goto done;
3071 if (stat(path2, &sb2) == -1) {
3072 err = got_error_from_errno2("stat", path2);
3073 goto done;
3076 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3077 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3078 if (err)
3079 goto done;
3081 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3082 if (err)
3083 goto done;
3085 if (fseek(f1, 0L, SEEK_SET) == -1)
3086 return got_ferror(f1, GOT_ERR_IO);
3087 if (fseek(f2, 0L, SEEK_SET) == -1)
3088 return got_ferror(f2, GOT_ERR_IO);
3089 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3090 int choice;
3091 err = apply_or_reject_change(&choice, change, ++n,
3092 changes->nchanges, ds, args, diff_flags, relpath,
3093 f1, f2, &line_cur1, &line_cur2,
3094 reverse_patch ? NULL : outfile,
3095 reverse_patch ? outfile : NULL,
3096 patch_cb, patch_arg);
3097 if (err)
3098 goto done;
3099 if (choice == GOT_PATCH_CHOICE_YES)
3100 have_content = 1;
3101 else if (choice == GOT_PATCH_CHOICE_QUIT)
3102 break;
3104 if (have_content)
3105 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3106 reverse_patch ? NULL : outfile,
3107 reverse_patch ? outfile : NULL);
3108 done:
3109 free(id_str);
3110 if (blob)
3111 got_object_blob_close(blob);
3112 if (f1 && fclose(f1) == EOF && err == NULL)
3113 err = got_error_from_errno2("fclose", path1);
3114 if (f2 && fclose(f2) == EOF && err == NULL)
3115 err = got_error_from_errno2("fclose", path2);
3116 if (outfile && fclose(outfile) == EOF && err == NULL)
3117 err = got_error_from_errno2("fclose", *path_outfile);
3118 if (path1 && unlink(path1) == -1 && err == NULL)
3119 err = got_error_from_errno2("unlink", path1);
3120 if (err || !have_content) {
3121 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3122 err = got_error_from_errno2("unlink", *path_outfile);
3123 free(*path_outfile);
3124 *path_outfile = NULL;
3126 free(args);
3127 if (ds) {
3128 got_diff_state_free(ds);
3129 free(ds);
3131 if (changes)
3132 got_diff_free_changes(changes);
3133 free(path1);
3134 return err;
3137 static const struct got_error *
3138 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3139 const char *relpath, struct got_object_id *blob_id,
3140 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3142 struct revert_file_args *a = arg;
3143 const struct got_error *err = NULL;
3144 char *parent_path = NULL;
3145 struct got_fileindex_entry *ie;
3146 struct got_tree_object *tree = NULL;
3147 struct got_object_id *tree_id = NULL;
3148 const struct got_tree_entry *te = NULL;
3149 char *tree_path = NULL, *te_name;
3150 char *ondisk_path = NULL, *path_content = NULL;
3151 struct got_blob_object *blob = NULL;
3153 /* Reverting a staged deletion is a no-op. */
3154 if (status == GOT_STATUS_DELETE &&
3155 staged_status != GOT_STATUS_NO_CHANGE)
3156 return NULL;
3158 if (status == GOT_STATUS_UNVERSIONED)
3159 return (*a->progress_cb)(a->progress_arg,
3160 GOT_STATUS_UNVERSIONED, relpath);
3162 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3163 if (ie == NULL)
3164 return got_error(GOT_ERR_BAD_PATH);
3166 /* Construct in-repository path of tree which contains this blob. */
3167 err = got_path_dirname(&parent_path, ie->path);
3168 if (err) {
3169 if (err->code != GOT_ERR_BAD_PATH)
3170 goto done;
3171 parent_path = strdup("/");
3172 if (parent_path == NULL) {
3173 err = got_error_from_errno("strdup");
3174 goto done;
3177 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3178 tree_path = strdup(parent_path);
3179 if (tree_path == NULL) {
3180 err = got_error_from_errno("strdup");
3181 goto done;
3183 } else {
3184 if (got_path_is_root_dir(parent_path)) {
3185 tree_path = strdup(a->worktree->path_prefix);
3186 if (tree_path == NULL) {
3187 err = got_error_from_errno("strdup");
3188 goto done;
3190 } else {
3191 if (asprintf(&tree_path, "%s/%s",
3192 a->worktree->path_prefix, parent_path) == -1) {
3193 err = got_error_from_errno("asprintf");
3194 goto done;
3199 err = got_object_id_by_path(&tree_id, a->repo,
3200 a->worktree->base_commit_id, tree_path);
3201 if (err) {
3202 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3203 (status == GOT_STATUS_ADD ||
3204 staged_status == GOT_STATUS_ADD)))
3205 goto done;
3206 } else {
3207 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3208 if (err)
3209 goto done;
3211 te_name = basename(ie->path);
3212 if (te_name == NULL) {
3213 err = got_error_from_errno2("basename", ie->path);
3214 goto done;
3217 te = got_object_tree_find_entry(tree, te_name);
3218 if (te == NULL && status != GOT_STATUS_ADD &&
3219 staged_status != GOT_STATUS_ADD) {
3220 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3221 goto done;
3225 switch (status) {
3226 case GOT_STATUS_ADD:
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 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3237 ie->path);
3238 if (err)
3239 goto done;
3240 got_fileindex_entry_remove(a->fileindex, ie);
3241 break;
3242 case GOT_STATUS_DELETE:
3243 if (a->patch_cb) {
3244 int choice = GOT_PATCH_CHOICE_NONE;
3245 err = (*a->patch_cb)(&choice, a->patch_arg,
3246 status, ie->path, NULL, 1, 1);
3247 if (err)
3248 goto done;
3249 if (choice != GOT_PATCH_CHOICE_YES)
3250 break;
3252 /* fall through */
3253 case GOT_STATUS_MODIFY:
3254 case GOT_STATUS_CONFLICT:
3255 case GOT_STATUS_MISSING: {
3256 struct got_object_id id;
3257 if (staged_status == GOT_STATUS_ADD ||
3258 staged_status == GOT_STATUS_MODIFY) {
3259 memcpy(id.sha1, ie->staged_blob_sha1,
3260 SHA1_DIGEST_LENGTH);
3261 } else
3262 memcpy(id.sha1, ie->blob_sha1,
3263 SHA1_DIGEST_LENGTH);
3264 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3265 if (err)
3266 goto done;
3268 if (asprintf(&ondisk_path, "%s/%s",
3269 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3270 err = got_error_from_errno("asprintf");
3271 goto done;
3274 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3275 status == GOT_STATUS_CONFLICT)) {
3276 err = create_patched_content(&path_content, 1, &id,
3277 ondisk_path, ie->path, a->repo,
3278 a->patch_cb, a->patch_arg);
3279 if (err || path_content == NULL)
3280 break;
3281 if (rename(path_content, ondisk_path) == -1) {
3282 err = got_error_from_errno3("rename",
3283 path_content, ondisk_path);
3284 goto done;
3286 } else {
3287 err = install_blob(a->worktree, ondisk_path, ie->path,
3288 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3289 got_fileindex_perms_to_st(ie), blob, 0, 1,
3290 a->repo, a->progress_cb, a->progress_arg);
3291 if (err)
3292 goto done;
3293 if (status == GOT_STATUS_DELETE) {
3294 err = update_blob_fileindex_entry(a->worktree,
3295 a->fileindex, ie, ondisk_path, ie->path,
3296 blob, 1);
3297 if (err)
3298 goto done;
3301 break;
3303 default:
3304 break;
3306 done:
3307 free(ondisk_path);
3308 free(path_content);
3309 free(parent_path);
3310 free(tree_path);
3311 if (blob)
3312 got_object_blob_close(blob);
3313 if (tree)
3314 got_object_tree_close(tree);
3315 free(tree_id);
3316 return err;
3319 const struct got_error *
3320 got_worktree_revert(struct got_worktree *worktree,
3321 struct got_pathlist_head *paths,
3322 got_worktree_checkout_cb progress_cb, void *progress_arg,
3323 got_worktree_patch_cb patch_cb, void *patch_arg,
3324 struct got_repository *repo)
3326 struct got_fileindex *fileindex = NULL;
3327 char *fileindex_path = NULL;
3328 const struct got_error *err = NULL, *unlockerr = NULL;
3329 const struct got_error *sync_err = NULL;
3330 struct got_pathlist_entry *pe;
3331 struct revert_file_args rfa;
3333 err = lock_worktree(worktree, LOCK_EX);
3334 if (err)
3335 return err;
3337 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3338 if (err)
3339 goto done;
3341 rfa.worktree = worktree;
3342 rfa.fileindex = fileindex;
3343 rfa.progress_cb = progress_cb;
3344 rfa.progress_arg = progress_arg;
3345 rfa.patch_cb = patch_cb;
3346 rfa.patch_arg = patch_arg;
3347 rfa.repo = repo;
3348 TAILQ_FOREACH(pe, paths, entry) {
3349 err = worktree_status(worktree, pe->path, fileindex, repo,
3350 revert_file, &rfa, NULL, NULL);
3351 if (err)
3352 break;
3354 sync_err = sync_fileindex(fileindex, fileindex_path);
3355 if (sync_err && err == NULL)
3356 err = sync_err;
3357 done:
3358 free(fileindex_path);
3359 if (fileindex)
3360 got_fileindex_free(fileindex);
3361 unlockerr = lock_worktree(worktree, LOCK_SH);
3362 if (unlockerr && err == NULL)
3363 err = unlockerr;
3364 return err;
3367 static void
3368 free_commitable(struct got_commitable *ct)
3370 free(ct->path);
3371 free(ct->in_repo_path);
3372 free(ct->ondisk_path);
3373 free(ct->blob_id);
3374 free(ct->base_blob_id);
3375 free(ct->staged_blob_id);
3376 free(ct->base_commit_id);
3377 free(ct);
3380 struct collect_commitables_arg {
3381 struct got_pathlist_head *commitable_paths;
3382 struct got_repository *repo;
3383 struct got_worktree *worktree;
3384 int have_staged_files;
3387 static const struct got_error *
3388 collect_commitables(void *arg, unsigned char status,
3389 unsigned char staged_status, const char *relpath,
3390 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3391 struct got_object_id *commit_id)
3393 struct collect_commitables_arg *a = arg;
3394 const struct got_error *err = NULL;
3395 struct got_commitable *ct = NULL;
3396 struct got_pathlist_entry *new = NULL;
3397 char *parent_path = NULL, *path = NULL;
3398 struct stat sb;
3400 if (a->have_staged_files) {
3401 if (staged_status != GOT_STATUS_MODIFY &&
3402 staged_status != GOT_STATUS_ADD &&
3403 staged_status != GOT_STATUS_DELETE)
3404 return NULL;
3405 } else {
3406 if (status == GOT_STATUS_CONFLICT)
3407 return got_error(GOT_ERR_COMMIT_CONFLICT);
3409 if (status != GOT_STATUS_MODIFY &&
3410 status != GOT_STATUS_ADD &&
3411 status != GOT_STATUS_DELETE)
3412 return NULL;
3415 if (asprintf(&path, "/%s", relpath) == -1) {
3416 err = got_error_from_errno("asprintf");
3417 goto done;
3419 if (strcmp(path, "/") == 0) {
3420 parent_path = strdup("");
3421 if (parent_path == NULL)
3422 return got_error_from_errno("strdup");
3423 } else {
3424 err = got_path_dirname(&parent_path, path);
3425 if (err)
3426 return err;
3429 ct = calloc(1, sizeof(*ct));
3430 if (ct == NULL) {
3431 err = got_error_from_errno("calloc");
3432 goto done;
3435 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3436 relpath) == -1) {
3437 err = got_error_from_errno("asprintf");
3438 goto done;
3440 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3441 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3442 } else {
3443 if (lstat(ct->ondisk_path, &sb) != 0) {
3444 err = got_error_from_errno2("lstat", ct->ondisk_path);
3445 goto done;
3447 ct->mode = sb.st_mode;
3450 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3451 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3452 relpath) == -1) {
3453 err = got_error_from_errno("asprintf");
3454 goto done;
3457 ct->status = status;
3458 ct->staged_status = staged_status;
3459 ct->blob_id = NULL; /* will be filled in when blob gets created */
3460 if (ct->status != GOT_STATUS_ADD &&
3461 ct->staged_status != GOT_STATUS_ADD) {
3462 ct->base_blob_id = got_object_id_dup(blob_id);
3463 if (ct->base_blob_id == NULL) {
3464 err = got_error_from_errno("got_object_id_dup");
3465 goto done;
3467 ct->base_commit_id = got_object_id_dup(commit_id);
3468 if (ct->base_commit_id == NULL) {
3469 err = got_error_from_errno("got_object_id_dup");
3470 goto done;
3473 if (ct->staged_status == GOT_STATUS_ADD ||
3474 ct->staged_status == GOT_STATUS_MODIFY) {
3475 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3476 if (ct->staged_blob_id == NULL) {
3477 err = got_error_from_errno("got_object_id_dup");
3478 goto done;
3481 ct->path = strdup(path);
3482 if (ct->path == NULL) {
3483 err = got_error_from_errno("strdup");
3484 goto done;
3486 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3487 done:
3488 if (ct && (err || new == NULL))
3489 free_commitable(ct);
3490 free(parent_path);
3491 free(path);
3492 return err;
3495 static const struct got_error *write_tree(struct got_object_id **,
3496 struct got_tree_object *, const char *, struct got_pathlist_head *,
3497 got_worktree_status_cb status_cb, void *status_arg,
3498 struct got_repository *);
3500 static const struct got_error *
3501 write_subtree(struct got_object_id **new_subtree_id,
3502 struct got_tree_entry *te, const char *parent_path,
3503 struct got_pathlist_head *commitable_paths,
3504 got_worktree_status_cb status_cb, void *status_arg,
3505 struct got_repository *repo)
3507 const struct got_error *err = NULL;
3508 struct got_tree_object *subtree;
3509 char *subpath;
3511 if (asprintf(&subpath, "%s%s%s", parent_path,
3512 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3513 return got_error_from_errno("asprintf");
3515 err = got_object_open_as_tree(&subtree, repo, te->id);
3516 if (err)
3517 return err;
3519 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3520 status_cb, status_arg, repo);
3521 got_object_tree_close(subtree);
3522 free(subpath);
3523 return err;
3526 static const struct got_error *
3527 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3529 const struct got_error *err = NULL;
3530 char *ct_parent_path = NULL;
3532 *match = 0;
3534 if (strchr(ct->in_repo_path, '/') == NULL) {
3535 *match = got_path_is_root_dir(path);
3536 return NULL;
3539 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3540 if (err)
3541 return err;
3542 *match = (strcmp(path, ct_parent_path) == 0);
3543 free(ct_parent_path);
3544 return err;
3547 static mode_t
3548 get_ct_file_mode(struct got_commitable *ct)
3550 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3553 static const struct got_error *
3554 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3555 struct got_tree_entry *te, struct got_commitable *ct)
3557 const struct got_error *err = NULL;
3559 *new_te = NULL;
3561 err = got_object_tree_entry_dup(new_te, te);
3562 if (err)
3563 goto done;
3565 (*new_te)->mode = get_ct_file_mode(ct);
3567 free((*new_te)->id);
3568 if (ct->staged_status == GOT_STATUS_MODIFY)
3569 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3570 else
3571 (*new_te)->id = got_object_id_dup(ct->blob_id);
3572 if ((*new_te)->id == NULL) {
3573 err = got_error_from_errno("got_object_id_dup");
3574 goto done;
3576 done:
3577 if (err && *new_te) {
3578 got_object_tree_entry_close(*new_te);
3579 *new_te = NULL;
3581 return err;
3584 static const struct got_error *
3585 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3586 struct got_commitable *ct)
3588 const struct got_error *err = NULL;
3589 char *ct_name;
3591 *new_te = NULL;
3593 *new_te = calloc(1, sizeof(**new_te));
3594 if (*new_te == NULL)
3595 return got_error_from_errno("calloc");
3597 ct_name = basename(ct->path);
3598 if (ct_name == NULL) {
3599 err = got_error_from_errno2("basename", ct->path);
3600 goto done;
3602 (*new_te)->name = strdup(ct_name);
3603 if ((*new_te)->name == NULL) {
3604 err = got_error_from_errno("strdup");
3605 goto done;
3608 (*new_te)->mode = get_ct_file_mode(ct);
3610 if (ct->staged_status == GOT_STATUS_ADD)
3611 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3612 else
3613 (*new_te)->id = got_object_id_dup(ct->blob_id);
3614 if ((*new_te)->id == NULL) {
3615 err = got_error_from_errno("got_object_id_dup");
3616 goto done;
3618 done:
3619 if (err && *new_te) {
3620 got_object_tree_entry_close(*new_te);
3621 *new_te = NULL;
3623 return err;
3626 static const struct got_error *
3627 insert_tree_entry(struct got_tree_entry *new_te,
3628 struct got_pathlist_head *paths)
3630 const struct got_error *err = NULL;
3631 struct got_pathlist_entry *new_pe;
3633 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3634 if (err)
3635 return err;
3636 if (new_pe == NULL)
3637 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3638 return NULL;
3641 static const struct got_error *
3642 report_ct_status(struct got_commitable *ct,
3643 got_worktree_status_cb status_cb, void *status_arg)
3645 const char *ct_path = ct->path;
3646 unsigned char status;
3648 while (ct_path[0] == '/')
3649 ct_path++;
3651 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3652 status = ct->staged_status;
3653 else
3654 status = ct->status;
3656 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3657 ct_path, ct->blob_id, NULL, NULL);
3660 static const struct got_error *
3661 match_modified_subtree(int *modified, struct got_tree_entry *te,
3662 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3664 const struct got_error *err = NULL;
3665 struct got_pathlist_entry *pe;
3666 char *te_path;
3668 *modified = 0;
3670 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3671 got_path_is_root_dir(base_tree_path) ? "" : "/",
3672 te->name) == -1)
3673 return got_error_from_errno("asprintf");
3675 TAILQ_FOREACH(pe, commitable_paths, entry) {
3676 struct got_commitable *ct = pe->data;
3677 *modified = got_path_is_child(ct->in_repo_path, te_path,
3678 strlen(te_path));
3679 if (*modified)
3680 break;
3683 free(te_path);
3684 return err;
3687 static const struct got_error *
3688 match_deleted_or_modified_ct(struct got_commitable **ctp,
3689 struct got_tree_entry *te, const char *base_tree_path,
3690 struct got_pathlist_head *commitable_paths)
3692 const struct got_error *err = NULL;
3693 struct got_pathlist_entry *pe;
3695 *ctp = NULL;
3697 TAILQ_FOREACH(pe, commitable_paths, entry) {
3698 struct got_commitable *ct = pe->data;
3699 char *ct_name = NULL;
3700 int path_matches;
3702 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3703 if (ct->status != GOT_STATUS_MODIFY &&
3704 ct->status != GOT_STATUS_DELETE)
3705 continue;
3706 } else {
3707 if (ct->staged_status != GOT_STATUS_MODIFY &&
3708 ct->staged_status != GOT_STATUS_DELETE)
3709 continue;
3712 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3713 continue;
3715 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3716 if (err)
3717 return err;
3718 if (!path_matches)
3719 continue;
3721 ct_name = basename(pe->path);
3722 if (ct_name == NULL)
3723 return got_error_from_errno2("basename", pe->path);
3725 if (strcmp(te->name, ct_name) != 0)
3726 continue;
3728 *ctp = ct;
3729 break;
3732 return err;
3735 static const struct got_error *
3736 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3737 const char *child_path, const char *path_base_tree,
3738 struct got_pathlist_head *commitable_paths,
3739 got_worktree_status_cb status_cb, void *status_arg,
3740 struct got_repository *repo)
3742 const struct got_error *err = NULL;
3743 struct got_tree_entry *new_te;
3744 char *subtree_path;
3746 *new_tep = NULL;
3748 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3749 got_path_is_root_dir(path_base_tree) ? "" : "/",
3750 child_path) == -1)
3751 return got_error_from_errno("asprintf");
3753 new_te = calloc(1, sizeof(*new_te));
3754 if (new_te == NULL)
3755 return got_error_from_errno("calloc");
3756 new_te->mode = S_IFDIR;
3757 new_te->name = strdup(child_path);
3758 if (new_te->name == NULL) {
3759 err = got_error_from_errno("strdup");
3760 got_object_tree_entry_close(new_te);
3761 goto done;
3763 err = write_tree(&new_te->id, NULL, subtree_path,
3764 commitable_paths, status_cb, status_arg, repo);
3765 if (err) {
3766 got_object_tree_entry_close(new_te);
3767 goto done;
3769 done:
3770 free(subtree_path);
3771 if (err == NULL)
3772 *new_tep = new_te;
3773 return err;
3776 static const struct got_error *
3777 write_tree(struct got_object_id **new_tree_id,
3778 struct got_tree_object *base_tree, const char *path_base_tree,
3779 struct got_pathlist_head *commitable_paths,
3780 got_worktree_status_cb status_cb, void *status_arg,
3781 struct got_repository *repo)
3783 const struct got_error *err = NULL;
3784 const struct got_tree_entries *base_entries = NULL;
3785 struct got_pathlist_head paths;
3786 struct got_tree_entries new_tree_entries;
3787 struct got_tree_entry *te, *new_te = NULL;
3788 struct got_pathlist_entry *pe;
3790 TAILQ_INIT(&paths);
3791 new_tree_entries.nentries = 0;
3792 SIMPLEQ_INIT(&new_tree_entries.head);
3794 /* Insert, and recurse into, newly added entries first. */
3795 TAILQ_FOREACH(pe, commitable_paths, entry) {
3796 struct got_commitable *ct = pe->data;
3797 char *child_path = NULL, *slash;
3799 if ((ct->status != GOT_STATUS_ADD &&
3800 ct->staged_status != GOT_STATUS_ADD) ||
3801 (ct->flags & GOT_COMMITABLE_ADDED))
3802 continue;
3804 if (!got_path_is_child(pe->path, path_base_tree,
3805 strlen(path_base_tree)))
3806 continue;
3808 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3809 pe->path);
3810 if (err)
3811 goto done;
3813 slash = strchr(child_path, '/');
3814 if (slash == NULL) {
3815 err = alloc_added_blob_tree_entry(&new_te, ct);
3816 if (err)
3817 goto done;
3818 err = report_ct_status(ct, status_cb, status_arg);
3819 if (err)
3820 goto done;
3821 ct->flags |= GOT_COMMITABLE_ADDED;
3822 err = insert_tree_entry(new_te, &paths);
3823 if (err)
3824 goto done;
3825 } else {
3826 *slash = '\0'; /* trim trailing path components */
3827 if (base_tree == NULL ||
3828 got_object_tree_find_entry(base_tree, child_path)
3829 == NULL) {
3830 err = make_subtree_for_added_blob(&new_te,
3831 child_path, path_base_tree,
3832 commitable_paths, status_cb, status_arg,
3833 repo);
3834 if (err)
3835 goto done;
3836 err = insert_tree_entry(new_te, &paths);
3837 if (err)
3838 goto done;
3843 if (base_tree) {
3844 /* Handle modified and deleted entries. */
3845 base_entries = got_object_tree_get_entries(base_tree);
3846 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3847 struct got_commitable *ct = NULL;
3849 if (got_object_tree_entry_is_submodule(te)) {
3850 /* Entry is a submodule; just copy it. */
3851 err = got_object_tree_entry_dup(&new_te, te);
3852 if (err)
3853 goto done;
3854 err = insert_tree_entry(new_te, &paths);
3855 if (err)
3856 goto done;
3857 continue;
3860 if (S_ISDIR(te->mode)) {
3861 int modified;
3862 err = got_object_tree_entry_dup(&new_te, te);
3863 if (err)
3864 goto done;
3865 err = match_modified_subtree(&modified, te,
3866 path_base_tree, commitable_paths);
3867 if (err)
3868 goto done;
3869 /* Avoid recursion into unmodified subtrees. */
3870 if (modified) {
3871 free(new_te->id);
3872 err = write_subtree(&new_te->id, te,
3873 path_base_tree, commitable_paths,
3874 status_cb, status_arg, repo);
3875 if (err)
3876 goto done;
3878 err = insert_tree_entry(new_te, &paths);
3879 if (err)
3880 goto done;
3881 continue;
3884 err = match_deleted_or_modified_ct(&ct, te,
3885 path_base_tree, commitable_paths);
3886 if (err)
3887 goto done;
3888 if (ct) {
3889 /* NB: Deleted entries get dropped here. */
3890 if (ct->status == GOT_STATUS_MODIFY ||
3891 ct->staged_status == GOT_STATUS_MODIFY) {
3892 err = alloc_modified_blob_tree_entry(
3893 &new_te, te, ct);
3894 if (err)
3895 goto done;
3896 err = insert_tree_entry(new_te, &paths);
3897 if (err)
3898 goto done;
3900 err = report_ct_status(ct, status_cb,
3901 status_arg);
3902 if (err)
3903 goto done;
3904 } else {
3905 /* Entry is unchanged; just copy it. */
3906 err = got_object_tree_entry_dup(&new_te, te);
3907 if (err)
3908 goto done;
3909 err = insert_tree_entry(new_te, &paths);
3910 if (err)
3911 goto done;
3916 /* Write new list of entries; deleted entries have been dropped. */
3917 TAILQ_FOREACH(pe, &paths, entry) {
3918 struct got_tree_entry *te = pe->data;
3919 new_tree_entries.nentries++;
3920 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3922 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3923 done:
3924 got_object_tree_entries_close(&new_tree_entries);
3925 got_pathlist_free(&paths);
3926 return err;
3929 static const struct got_error *
3930 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3931 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3932 int have_staged_files)
3934 const struct got_error *err = NULL;
3935 struct got_pathlist_entry *pe;
3937 TAILQ_FOREACH(pe, commitable_paths, entry) {
3938 struct got_fileindex_entry *ie;
3939 struct got_commitable *ct = pe->data;
3941 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3942 if (ie) {
3943 if (ct->status == GOT_STATUS_DELETE ||
3944 ct->staged_status == GOT_STATUS_DELETE) {
3945 got_fileindex_entry_remove(fileindex, ie);
3946 got_fileindex_entry_free(ie);
3947 } else if (ct->staged_status == GOT_STATUS_ADD ||
3948 ct->staged_status == GOT_STATUS_MODIFY) {
3949 got_fileindex_entry_stage_set(ie,
3950 GOT_FILEIDX_STAGE_NONE);
3951 err = got_fileindex_entry_update(ie,
3952 ct->ondisk_path, ct->staged_blob_id->sha1,
3953 new_base_commit_id->sha1,
3954 !have_staged_files);
3955 } else
3956 err = got_fileindex_entry_update(ie,
3957 ct->ondisk_path, ct->blob_id->sha1,
3958 new_base_commit_id->sha1,
3959 !have_staged_files);
3960 } else {
3961 err = got_fileindex_entry_alloc(&ie,
3962 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3963 new_base_commit_id->sha1);
3964 if (err)
3965 break;
3966 err = got_fileindex_entry_add(fileindex, ie);
3967 if (err)
3968 break;
3971 return err;
3975 static const struct got_error *
3976 check_out_of_date(const char *in_repo_path, unsigned char status,
3977 unsigned char staged_status, struct got_object_id *base_blob_id,
3978 struct got_object_id *base_commit_id,
3979 struct got_object_id *head_commit_id, struct got_repository *repo,
3980 int ood_errcode)
3982 const struct got_error *err = NULL;
3983 struct got_object_id *id = NULL;
3985 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3986 /* Trivial case: base commit == head commit */
3987 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3988 return NULL;
3990 * Ensure file content which local changes were based
3991 * on matches file content in the branch head.
3993 err = got_object_id_by_path(&id, repo, head_commit_id,
3994 in_repo_path);
3995 if (err) {
3996 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3997 err = got_error(ood_errcode);
3998 goto done;
3999 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4000 err = got_error(ood_errcode);
4001 } else {
4002 /* Require that added files don't exist in the branch head. */
4003 err = got_object_id_by_path(&id, repo, head_commit_id,
4004 in_repo_path);
4005 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4006 goto done;
4007 err = id ? got_error(ood_errcode) : NULL;
4009 done:
4010 free(id);
4011 return err;
4014 const struct got_error *
4015 commit_worktree(struct got_object_id **new_commit_id,
4016 struct got_pathlist_head *commitable_paths,
4017 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4018 const char *author, const char *committer,
4019 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4020 got_worktree_status_cb status_cb, void *status_arg,
4021 struct got_repository *repo)
4023 const struct got_error *err = NULL, *unlockerr = NULL;
4024 struct got_pathlist_entry *pe;
4025 const char *head_ref_name = NULL;
4026 struct got_commit_object *head_commit = NULL;
4027 struct got_reference *head_ref2 = NULL;
4028 struct got_object_id *head_commit_id2 = NULL;
4029 struct got_tree_object *head_tree = NULL;
4030 struct got_object_id *new_tree_id = NULL;
4031 struct got_object_id_queue parent_ids;
4032 struct got_object_qid *pid = NULL;
4033 char *logmsg = NULL;
4035 *new_commit_id = NULL;
4037 SIMPLEQ_INIT(&parent_ids);
4039 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4040 if (err)
4041 goto done;
4043 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4044 if (err)
4045 goto done;
4047 if (commit_msg_cb != NULL) {
4048 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4049 if (err)
4050 goto done;
4053 if (logmsg == NULL || strlen(logmsg) == 0) {
4054 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4055 goto done;
4058 /* Create blobs from added and modified files and record their IDs. */
4059 TAILQ_FOREACH(pe, commitable_paths, entry) {
4060 struct got_commitable *ct = pe->data;
4061 char *ondisk_path;
4063 /* Blobs for staged files already exist. */
4064 if (ct->staged_status == GOT_STATUS_ADD ||
4065 ct->staged_status == GOT_STATUS_MODIFY)
4066 continue;
4068 if (ct->status != GOT_STATUS_ADD &&
4069 ct->status != GOT_STATUS_MODIFY)
4070 continue;
4072 if (asprintf(&ondisk_path, "%s/%s",
4073 worktree->root_path, pe->path) == -1) {
4074 err = got_error_from_errno("asprintf");
4075 goto done;
4077 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4078 free(ondisk_path);
4079 if (err)
4080 goto done;
4083 /* Recursively write new tree objects. */
4084 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4085 status_cb, status_arg, repo);
4086 if (err)
4087 goto done;
4089 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4090 if (err)
4091 goto done;
4092 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4093 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4094 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4095 got_object_qid_free(pid);
4096 if (logmsg != NULL)
4097 free(logmsg);
4098 if (err)
4099 goto done;
4101 /* Check if a concurrent commit to our branch has occurred. */
4102 head_ref_name = got_worktree_get_head_ref_name(worktree);
4103 if (head_ref_name == NULL) {
4104 err = got_error_from_errno("got_worktree_get_head_ref_name");
4105 goto done;
4107 /* Lock the reference here to prevent concurrent modification. */
4108 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4109 if (err)
4110 goto done;
4111 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4112 if (err)
4113 goto done;
4114 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4115 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4116 goto done;
4118 /* Update branch head in repository. */
4119 err = got_ref_change_ref(head_ref2, *new_commit_id);
4120 if (err)
4121 goto done;
4122 err = got_ref_write(head_ref2, repo);
4123 if (err)
4124 goto done;
4126 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4127 if (err)
4128 goto done;
4130 err = ref_base_commit(worktree, repo);
4131 if (err)
4132 goto done;
4133 done:
4134 if (head_tree)
4135 got_object_tree_close(head_tree);
4136 if (head_commit)
4137 got_object_commit_close(head_commit);
4138 free(head_commit_id2);
4139 if (head_ref2) {
4140 unlockerr = got_ref_unlock(head_ref2);
4141 if (unlockerr && err == NULL)
4142 err = unlockerr;
4143 got_ref_close(head_ref2);
4145 return err;
4148 static const struct got_error *
4149 check_path_is_commitable(const char *path,
4150 struct got_pathlist_head *commitable_paths)
4152 struct got_pathlist_entry *cpe = NULL;
4153 size_t path_len = strlen(path);
4155 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4156 struct got_commitable *ct = cpe->data;
4157 const char *ct_path = ct->path;
4159 while (ct_path[0] == '/')
4160 ct_path++;
4162 if (strcmp(path, ct_path) == 0 ||
4163 got_path_is_child(ct_path, path, path_len))
4164 break;
4167 if (cpe == NULL)
4168 return got_error_path(path, GOT_ERR_BAD_PATH);
4170 return NULL;
4173 static const struct got_error *
4174 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4176 int *have_staged_files = arg;
4178 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4179 *have_staged_files = 1;
4180 return got_error(GOT_ERR_CANCELLED);
4183 return NULL;
4186 static const struct got_error *
4187 check_non_staged_files(struct got_fileindex *fileindex,
4188 struct got_pathlist_head *paths)
4190 struct got_pathlist_entry *pe;
4191 struct got_fileindex_entry *ie;
4193 TAILQ_FOREACH(pe, paths, entry) {
4194 if (pe->path[0] == '\0')
4195 continue;
4196 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4197 if (ie == NULL)
4198 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4199 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4200 return got_error_path(pe->path,
4201 GOT_ERR_FILE_NOT_STAGED);
4204 return NULL;
4207 const struct got_error *
4208 got_worktree_commit(struct got_object_id **new_commit_id,
4209 struct got_worktree *worktree, struct got_pathlist_head *paths,
4210 const char *author, const char *committer,
4211 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4212 got_worktree_status_cb status_cb, void *status_arg,
4213 struct got_repository *repo)
4215 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4216 struct got_fileindex *fileindex = NULL;
4217 char *fileindex_path = NULL;
4218 struct got_pathlist_head commitable_paths;
4219 struct collect_commitables_arg cc_arg;
4220 struct got_pathlist_entry *pe;
4221 struct got_reference *head_ref = NULL;
4222 struct got_object_id *head_commit_id = NULL;
4223 int have_staged_files = 0;
4225 *new_commit_id = NULL;
4227 TAILQ_INIT(&commitable_paths);
4229 err = lock_worktree(worktree, LOCK_EX);
4230 if (err)
4231 goto done;
4233 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4234 if (err)
4235 goto done;
4237 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4238 if (err)
4239 goto done;
4241 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4242 if (err)
4243 goto done;
4245 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4246 &have_staged_files);
4247 if (err && err->code != GOT_ERR_CANCELLED)
4248 goto done;
4249 if (have_staged_files) {
4250 err = check_non_staged_files(fileindex, paths);
4251 if (err)
4252 goto done;
4255 cc_arg.commitable_paths = &commitable_paths;
4256 cc_arg.worktree = worktree;
4257 cc_arg.repo = repo;
4258 cc_arg.have_staged_files = have_staged_files;
4259 TAILQ_FOREACH(pe, paths, entry) {
4260 err = worktree_status(worktree, pe->path, fileindex, repo,
4261 collect_commitables, &cc_arg, NULL, NULL);
4262 if (err)
4263 goto done;
4266 if (TAILQ_EMPTY(&commitable_paths)) {
4267 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4268 goto done;
4271 TAILQ_FOREACH(pe, paths, entry) {
4272 err = check_path_is_commitable(pe->path, &commitable_paths);
4273 if (err)
4274 goto done;
4277 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4278 struct got_commitable *ct = pe->data;
4279 const char *ct_path = ct->in_repo_path;
4281 while (ct_path[0] == '/')
4282 ct_path++;
4283 err = check_out_of_date(ct_path, ct->status,
4284 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4285 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4286 if (err)
4287 goto done;
4291 err = commit_worktree(new_commit_id, &commitable_paths,
4292 head_commit_id, worktree, author, committer,
4293 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4294 if (err)
4295 goto done;
4297 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4298 fileindex, have_staged_files);
4299 sync_err = sync_fileindex(fileindex, fileindex_path);
4300 if (sync_err && err == NULL)
4301 err = sync_err;
4302 done:
4303 if (fileindex)
4304 got_fileindex_free(fileindex);
4305 free(fileindex_path);
4306 unlockerr = lock_worktree(worktree, LOCK_SH);
4307 if (unlockerr && err == NULL)
4308 err = unlockerr;
4309 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4310 struct got_commitable *ct = pe->data;
4311 free_commitable(ct);
4313 got_pathlist_free(&commitable_paths);
4314 return err;
4317 const char *
4318 got_commitable_get_path(struct got_commitable *ct)
4320 return ct->path;
4323 unsigned int
4324 got_commitable_get_status(struct got_commitable *ct)
4326 return ct->status;
4329 struct check_rebase_ok_arg {
4330 struct got_worktree *worktree;
4331 struct got_repository *repo;
4334 static const struct got_error *
4335 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4337 const struct got_error *err = NULL;
4338 struct check_rebase_ok_arg *a = arg;
4339 unsigned char status;
4340 struct stat sb;
4341 char *ondisk_path;
4343 /* Reject rebase of a work tree with mixed base commits. */
4344 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4345 SHA1_DIGEST_LENGTH))
4346 return got_error(GOT_ERR_MIXED_COMMITS);
4348 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4349 == -1)
4350 return got_error_from_errno("asprintf");
4352 /* Reject rebase of a work tree with modified or staged files. */
4353 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4354 free(ondisk_path);
4355 if (err)
4356 return err;
4358 if (status != GOT_STATUS_NO_CHANGE)
4359 return got_error(GOT_ERR_MODIFIED);
4360 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4361 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4363 return NULL;
4366 const struct got_error *
4367 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4368 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4369 struct got_worktree *worktree, struct got_reference *branch,
4370 struct got_repository *repo)
4372 const struct got_error *err = NULL;
4373 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4374 char *branch_ref_name = NULL;
4375 char *fileindex_path = NULL;
4376 struct check_rebase_ok_arg ok_arg;
4377 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4379 *new_base_branch_ref = NULL;
4380 *tmp_branch = NULL;
4381 *fileindex = NULL;
4383 err = lock_worktree(worktree, LOCK_EX);
4384 if (err)
4385 return err;
4387 err = open_fileindex(fileindex, &fileindex_path, worktree);
4388 if (err)
4389 goto done;
4391 ok_arg.worktree = worktree;
4392 ok_arg.repo = repo;
4393 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4394 &ok_arg);
4395 if (err)
4396 goto done;
4398 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4399 if (err)
4400 goto done;
4402 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4403 if (err)
4404 goto done;
4406 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4407 if (err)
4408 goto done;
4410 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4411 0);
4412 if (err)
4413 goto done;
4415 err = got_ref_alloc_symref(new_base_branch_ref,
4416 new_base_branch_ref_name, wt_branch);
4417 if (err)
4418 goto done;
4419 err = got_ref_write(*new_base_branch_ref, repo);
4420 if (err)
4421 goto done;
4423 /* TODO Lock original branch's ref while rebasing? */
4425 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4426 if (err)
4427 goto done;
4429 err = got_ref_write(branch_ref, repo);
4430 if (err)
4431 goto done;
4433 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4434 worktree->base_commit_id);
4435 if (err)
4436 goto done;
4437 err = got_ref_write(*tmp_branch, repo);
4438 if (err)
4439 goto done;
4441 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4442 if (err)
4443 goto done;
4444 done:
4445 free(fileindex_path);
4446 free(tmp_branch_name);
4447 free(new_base_branch_ref_name);
4448 free(branch_ref_name);
4449 if (branch_ref)
4450 got_ref_close(branch_ref);
4451 if (wt_branch)
4452 got_ref_close(wt_branch);
4453 if (err) {
4454 if (*new_base_branch_ref) {
4455 got_ref_close(*new_base_branch_ref);
4456 *new_base_branch_ref = NULL;
4458 if (*tmp_branch) {
4459 got_ref_close(*tmp_branch);
4460 *tmp_branch = NULL;
4462 if (*fileindex) {
4463 got_fileindex_free(*fileindex);
4464 *fileindex = NULL;
4466 lock_worktree(worktree, LOCK_SH);
4468 return err;
4471 const struct got_error *
4472 got_worktree_rebase_continue(struct got_object_id **commit_id,
4473 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4474 struct got_reference **branch, struct got_fileindex **fileindex,
4475 struct got_worktree *worktree, struct got_repository *repo)
4477 const struct got_error *err;
4478 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4479 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4480 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4481 char *fileindex_path = NULL;
4482 int have_staged_files = 0;
4484 *commit_id = NULL;
4485 *new_base_branch = NULL;
4486 *tmp_branch = NULL;
4487 *branch = NULL;
4488 *fileindex = NULL;
4490 err = lock_worktree(worktree, LOCK_EX);
4491 if (err)
4492 return err;
4494 err = open_fileindex(fileindex, &fileindex_path, worktree);
4495 if (err)
4496 goto done;
4498 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4499 &have_staged_files);
4500 if (err && err->code != GOT_ERR_CANCELLED)
4501 goto done;
4502 if (have_staged_files) {
4503 err = got_error(GOT_ERR_STAGED_PATHS);
4504 goto done;
4507 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4508 if (err)
4509 goto done;
4511 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4512 if (err)
4513 goto done;
4515 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4516 if (err)
4517 goto done;
4519 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4520 if (err)
4521 goto done;
4523 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4524 if (err)
4525 goto done;
4527 err = got_ref_open(branch, repo,
4528 got_ref_get_symref_target(branch_ref), 0);
4529 if (err)
4530 goto done;
4532 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4533 if (err)
4534 goto done;
4536 err = got_ref_resolve(commit_id, repo, commit_ref);
4537 if (err)
4538 goto done;
4540 err = got_ref_open(new_base_branch, repo,
4541 new_base_branch_ref_name, 0);
4542 if (err)
4543 goto done;
4545 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4546 if (err)
4547 goto done;
4548 done:
4549 free(commit_ref_name);
4550 free(branch_ref_name);
4551 free(fileindex_path);
4552 if (commit_ref)
4553 got_ref_close(commit_ref);
4554 if (branch_ref)
4555 got_ref_close(branch_ref);
4556 if (err) {
4557 free(*commit_id);
4558 *commit_id = NULL;
4559 if (*tmp_branch) {
4560 got_ref_close(*tmp_branch);
4561 *tmp_branch = NULL;
4563 if (*new_base_branch) {
4564 got_ref_close(*new_base_branch);
4565 *new_base_branch = NULL;
4567 if (*branch) {
4568 got_ref_close(*branch);
4569 *branch = NULL;
4571 if (*fileindex) {
4572 got_fileindex_free(*fileindex);
4573 *fileindex = NULL;
4575 lock_worktree(worktree, LOCK_SH);
4577 return err;
4580 const struct got_error *
4581 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4583 const struct got_error *err;
4584 char *tmp_branch_name = NULL;
4586 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4587 if (err)
4588 return err;
4590 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4591 free(tmp_branch_name);
4592 return NULL;
4595 static const struct got_error *
4596 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4597 char **logmsg, void *arg)
4599 *logmsg = arg;
4600 return NULL;
4603 static const struct got_error *
4604 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4605 const char *path, struct got_object_id *blob_id,
4606 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4608 return NULL;
4611 struct collect_merged_paths_arg {
4612 got_worktree_checkout_cb progress_cb;
4613 void *progress_arg;
4614 struct got_pathlist_head *merged_paths;
4617 static const struct got_error *
4618 collect_merged_paths(void *arg, unsigned char status, const char *path)
4620 const struct got_error *err;
4621 struct collect_merged_paths_arg *a = arg;
4622 char *p;
4623 struct got_pathlist_entry *new;
4625 err = (*a->progress_cb)(a->progress_arg, status, path);
4626 if (err)
4627 return err;
4629 if (status != GOT_STATUS_MERGE &&
4630 status != GOT_STATUS_ADD &&
4631 status != GOT_STATUS_DELETE &&
4632 status != GOT_STATUS_CONFLICT)
4633 return NULL;
4635 p = strdup(path);
4636 if (p == NULL)
4637 return got_error_from_errno("strdup");
4639 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4640 if (err || new == NULL)
4641 free(p);
4642 return err;
4645 void
4646 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4648 struct got_pathlist_entry *pe;
4650 TAILQ_FOREACH(pe, merged_paths, entry)
4651 free((char *)pe->path);
4653 got_pathlist_free(merged_paths);
4656 static const struct got_error *
4657 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4658 struct got_repository *repo)
4660 const struct got_error *err;
4661 struct got_reference *commit_ref = NULL;
4663 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4664 if (err) {
4665 if (err->code != GOT_ERR_NOT_REF)
4666 goto done;
4667 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4668 if (err)
4669 goto done;
4670 err = got_ref_write(commit_ref, repo);
4671 if (err)
4672 goto done;
4673 } else {
4674 struct got_object_id *stored_id;
4675 int cmp;
4677 err = got_ref_resolve(&stored_id, repo, commit_ref);
4678 if (err)
4679 goto done;
4680 cmp = got_object_id_cmp(commit_id, stored_id);
4681 free(stored_id);
4682 if (cmp != 0) {
4683 err = got_error(GOT_ERR_REBASE_COMMITID);
4684 goto done;
4687 done:
4688 if (commit_ref)
4689 got_ref_close(commit_ref);
4690 return err;
4693 static const struct got_error *
4694 rebase_merge_files(struct got_pathlist_head *merged_paths,
4695 const char *commit_ref_name, struct got_worktree *worktree,
4696 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4697 struct got_object_id *commit_id, struct got_repository *repo,
4698 got_worktree_checkout_cb progress_cb, void *progress_arg,
4699 got_cancel_cb cancel_cb, void *cancel_arg)
4701 const struct got_error *err;
4702 struct got_reference *commit_ref = NULL;
4703 struct collect_merged_paths_arg cmp_arg;
4704 char *fileindex_path;
4706 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4708 err = get_fileindex_path(&fileindex_path, worktree);
4709 if (err)
4710 return err;
4712 cmp_arg.progress_cb = progress_cb;
4713 cmp_arg.progress_arg = progress_arg;
4714 cmp_arg.merged_paths = merged_paths;
4715 err = merge_files(worktree, fileindex, fileindex_path,
4716 parent_commit_id, commit_id, repo, collect_merged_paths,
4717 &cmp_arg, cancel_cb, cancel_arg);
4718 if (commit_ref)
4719 got_ref_close(commit_ref);
4720 return err;
4723 const struct got_error *
4724 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4725 struct got_worktree *worktree, struct got_fileindex *fileindex,
4726 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4727 struct got_repository *repo,
4728 got_worktree_checkout_cb progress_cb, void *progress_arg,
4729 got_cancel_cb cancel_cb, void *cancel_arg)
4731 const struct got_error *err;
4732 char *commit_ref_name;
4734 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4735 if (err)
4736 return err;
4738 err = store_commit_id(commit_ref_name, commit_id, repo);
4739 if (err)
4740 goto done;
4742 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4743 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4744 progress_arg, cancel_cb, cancel_arg);
4745 done:
4746 free(commit_ref_name);
4747 return err;
4750 const struct got_error *
4751 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4752 struct got_worktree *worktree, struct got_fileindex *fileindex,
4753 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4754 struct got_repository *repo,
4755 got_worktree_checkout_cb progress_cb, void *progress_arg,
4756 got_cancel_cb cancel_cb, void *cancel_arg)
4758 const struct got_error *err;
4759 char *commit_ref_name;
4761 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4762 if (err)
4763 return err;
4765 err = store_commit_id(commit_ref_name, commit_id, repo);
4766 if (err)
4767 goto done;
4769 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4770 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4771 progress_arg, cancel_cb, cancel_arg);
4772 done:
4773 free(commit_ref_name);
4774 return err;
4777 static const struct got_error *
4778 rebase_commit(struct got_object_id **new_commit_id,
4779 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4780 struct got_worktree *worktree, struct got_fileindex *fileindex,
4781 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4782 const char *new_logmsg, struct got_repository *repo)
4784 const struct got_error *err, *sync_err;
4785 struct got_pathlist_head commitable_paths;
4786 struct collect_commitables_arg cc_arg;
4787 char *fileindex_path = NULL;
4788 struct got_reference *head_ref = NULL;
4789 struct got_object_id *head_commit_id = NULL;
4790 char *logmsg = NULL;
4792 TAILQ_INIT(&commitable_paths);
4793 *new_commit_id = NULL;
4795 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4797 err = get_fileindex_path(&fileindex_path, worktree);
4798 if (err)
4799 return err;
4801 cc_arg.commitable_paths = &commitable_paths;
4802 cc_arg.worktree = worktree;
4803 cc_arg.repo = repo;
4804 cc_arg.have_staged_files = 0;
4806 * If possible get the status of individual files directly to
4807 * avoid crawling the entire work tree once per rebased commit.
4808 * TODO: Ideally, merged_paths would contain a list of commitables
4809 * we could use so we could skip worktree_status() entirely.
4811 if (merged_paths) {
4812 struct got_pathlist_entry *pe;
4813 if (TAILQ_EMPTY(merged_paths)) {
4814 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4815 goto done;
4817 TAILQ_FOREACH(pe, merged_paths, entry) {
4818 err = worktree_status(worktree, pe->path, fileindex,
4819 repo, collect_commitables, &cc_arg, NULL, NULL);
4820 if (err)
4821 goto done;
4823 } else {
4824 err = worktree_status(worktree, "", fileindex, repo,
4825 collect_commitables, &cc_arg, NULL, NULL);
4826 if (err)
4827 goto done;
4830 if (TAILQ_EMPTY(&commitable_paths)) {
4831 /* No-op change; commit will be elided. */
4832 err = got_ref_delete(commit_ref, repo);
4833 if (err)
4834 goto done;
4835 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4836 goto done;
4839 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4840 if (err)
4841 goto done;
4843 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4844 if (err)
4845 goto done;
4847 if (new_logmsg) {
4848 logmsg = strdup(new_logmsg);
4849 if (logmsg == NULL) {
4850 err = got_error_from_errno("strdup");
4851 goto done;
4853 } else {
4854 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4855 if (err)
4856 goto done;
4859 /* NB: commit_worktree will call free(logmsg) */
4860 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4861 worktree, got_object_commit_get_author(orig_commit),
4862 got_object_commit_get_committer(orig_commit),
4863 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4864 if (err)
4865 goto done;
4867 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4868 if (err)
4869 goto done;
4871 err = got_ref_delete(commit_ref, repo);
4872 if (err)
4873 goto done;
4875 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4876 fileindex, 0);
4877 sync_err = sync_fileindex(fileindex, fileindex_path);
4878 if (sync_err && err == NULL)
4879 err = sync_err;
4880 done:
4881 free(fileindex_path);
4882 free(head_commit_id);
4883 if (head_ref)
4884 got_ref_close(head_ref);
4885 if (err) {
4886 free(*new_commit_id);
4887 *new_commit_id = NULL;
4889 return err;
4892 const struct got_error *
4893 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4894 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4895 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4896 struct got_commit_object *orig_commit,
4897 struct got_object_id *orig_commit_id, struct got_repository *repo)
4899 const struct got_error *err;
4900 char *commit_ref_name;
4901 struct got_reference *commit_ref = NULL;
4902 struct got_object_id *commit_id = NULL;
4904 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4905 if (err)
4906 return err;
4908 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4909 if (err)
4910 goto done;
4911 err = got_ref_resolve(&commit_id, repo, commit_ref);
4912 if (err)
4913 goto done;
4914 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4915 err = got_error(GOT_ERR_REBASE_COMMITID);
4916 goto done;
4919 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4920 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4921 done:
4922 if (commit_ref)
4923 got_ref_close(commit_ref);
4924 free(commit_ref_name);
4925 free(commit_id);
4926 return err;
4929 const struct got_error *
4930 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4931 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4932 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4933 struct got_commit_object *orig_commit,
4934 struct got_object_id *orig_commit_id, const char *new_logmsg,
4935 struct got_repository *repo)
4937 const struct got_error *err;
4938 char *commit_ref_name;
4939 struct got_reference *commit_ref = NULL;
4940 struct got_object_id *commit_id = NULL;
4942 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4943 if (err)
4944 return err;
4946 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4947 if (err)
4948 goto done;
4949 err = got_ref_resolve(&commit_id, repo, commit_ref);
4950 if (err)
4951 goto done;
4952 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4953 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4954 goto done;
4957 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4958 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4959 done:
4960 if (commit_ref)
4961 got_ref_close(commit_ref);
4962 free(commit_ref_name);
4963 free(commit_id);
4964 return err;
4967 const struct got_error *
4968 got_worktree_rebase_postpone(struct got_worktree *worktree,
4969 struct got_fileindex *fileindex)
4971 if (fileindex)
4972 got_fileindex_free(fileindex);
4973 return lock_worktree(worktree, LOCK_SH);
4976 static const struct got_error *
4977 delete_ref(const char *name, struct got_repository *repo)
4979 const struct got_error *err;
4980 struct got_reference *ref;
4982 err = got_ref_open(&ref, repo, name, 0);
4983 if (err) {
4984 if (err->code == GOT_ERR_NOT_REF)
4985 return NULL;
4986 return err;
4989 err = got_ref_delete(ref, repo);
4990 got_ref_close(ref);
4991 return err;
4994 static const struct got_error *
4995 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4997 const struct got_error *err;
4998 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4999 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5001 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5002 if (err)
5003 goto done;
5004 err = delete_ref(tmp_branch_name, repo);
5005 if (err)
5006 goto done;
5008 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5009 if (err)
5010 goto done;
5011 err = delete_ref(new_base_branch_ref_name, repo);
5012 if (err)
5013 goto done;
5015 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5016 if (err)
5017 goto done;
5018 err = delete_ref(branch_ref_name, repo);
5019 if (err)
5020 goto done;
5022 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5023 if (err)
5024 goto done;
5025 err = delete_ref(commit_ref_name, repo);
5026 if (err)
5027 goto done;
5029 done:
5030 free(tmp_branch_name);
5031 free(new_base_branch_ref_name);
5032 free(branch_ref_name);
5033 free(commit_ref_name);
5034 return err;
5037 const struct got_error *
5038 got_worktree_rebase_complete(struct got_worktree *worktree,
5039 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5040 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5041 struct got_repository *repo)
5043 const struct got_error *err, *unlockerr;
5044 struct got_object_id *new_head_commit_id = NULL;
5046 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5047 if (err)
5048 return err;
5050 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5051 if (err)
5052 goto done;
5054 err = got_ref_write(rebased_branch, repo);
5055 if (err)
5056 goto done;
5058 err = got_worktree_set_head_ref(worktree, rebased_branch);
5059 if (err)
5060 goto done;
5062 err = delete_rebase_refs(worktree, repo);
5063 done:
5064 if (fileindex)
5065 got_fileindex_free(fileindex);
5066 free(new_head_commit_id);
5067 unlockerr = lock_worktree(worktree, LOCK_SH);
5068 if (unlockerr && err == NULL)
5069 err = unlockerr;
5070 return err;
5073 const struct got_error *
5074 got_worktree_rebase_abort(struct got_worktree *worktree,
5075 struct got_fileindex *fileindex, struct got_repository *repo,
5076 struct got_reference *new_base_branch,
5077 got_worktree_checkout_cb progress_cb, void *progress_arg)
5079 const struct got_error *err, *unlockerr, *sync_err;
5080 struct got_reference *resolved = NULL;
5081 struct got_object_id *commit_id = NULL;
5082 char *fileindex_path = NULL;
5083 struct revert_file_args rfa;
5084 struct got_object_id *tree_id = NULL;
5086 err = lock_worktree(worktree, LOCK_EX);
5087 if (err)
5088 return err;
5090 err = got_ref_open(&resolved, repo,
5091 got_ref_get_symref_target(new_base_branch), 0);
5092 if (err)
5093 goto done;
5095 err = got_worktree_set_head_ref(worktree, resolved);
5096 if (err)
5097 goto done;
5100 * XXX commits to the base branch could have happened while
5101 * we were busy rebasing; should we store the original commit ID
5102 * when rebase begins and read it back here?
5104 err = got_ref_resolve(&commit_id, repo, resolved);
5105 if (err)
5106 goto done;
5108 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5109 if (err)
5110 goto done;
5112 err = got_object_id_by_path(&tree_id, repo,
5113 worktree->base_commit_id, worktree->path_prefix);
5114 if (err)
5115 goto done;
5117 err = delete_rebase_refs(worktree, repo);
5118 if (err)
5119 goto done;
5121 err = get_fileindex_path(&fileindex_path, worktree);
5122 if (err)
5123 goto done;
5125 rfa.worktree = worktree;
5126 rfa.fileindex = fileindex;
5127 rfa.progress_cb = progress_cb;
5128 rfa.progress_arg = progress_arg;
5129 rfa.patch_cb = NULL;
5130 rfa.patch_arg = NULL;
5131 rfa.repo = repo;
5132 err = worktree_status(worktree, "", fileindex, repo,
5133 revert_file, &rfa, NULL, NULL);
5134 if (err)
5135 goto sync;
5137 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5138 repo, progress_cb, progress_arg, NULL, NULL);
5139 sync:
5140 sync_err = sync_fileindex(fileindex, fileindex_path);
5141 if (sync_err && err == NULL)
5142 err = sync_err;
5143 done:
5144 got_ref_close(resolved);
5145 free(tree_id);
5146 free(commit_id);
5147 if (fileindex)
5148 got_fileindex_free(fileindex);
5149 free(fileindex_path);
5151 unlockerr = lock_worktree(worktree, LOCK_SH);
5152 if (unlockerr && err == NULL)
5153 err = unlockerr;
5154 return err;
5157 const struct got_error *
5158 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5159 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5160 struct got_fileindex **fileindex, struct got_worktree *worktree,
5161 struct got_repository *repo)
5163 const struct got_error *err = NULL;
5164 char *tmp_branch_name = NULL;
5165 char *branch_ref_name = NULL;
5166 char *base_commit_ref_name = NULL;
5167 char *fileindex_path = NULL;
5168 struct check_rebase_ok_arg ok_arg;
5169 struct got_reference *wt_branch = NULL;
5170 struct got_reference *base_commit_ref = NULL;
5172 *tmp_branch = NULL;
5173 *branch_ref = NULL;
5174 *base_commit_id = NULL;
5175 *fileindex = NULL;
5177 err = lock_worktree(worktree, LOCK_EX);
5178 if (err)
5179 return err;
5181 err = open_fileindex(fileindex, &fileindex_path, worktree);
5182 if (err)
5183 goto done;
5185 ok_arg.worktree = worktree;
5186 ok_arg.repo = repo;
5187 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5188 &ok_arg);
5189 if (err)
5190 goto done;
5192 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5193 if (err)
5194 goto done;
5196 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5197 if (err)
5198 goto done;
5200 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5201 worktree);
5202 if (err)
5203 goto done;
5205 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5206 0);
5207 if (err)
5208 goto done;
5210 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5211 if (err)
5212 goto done;
5214 err = got_ref_write(*branch_ref, repo);
5215 if (err)
5216 goto done;
5218 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5219 worktree->base_commit_id);
5220 if (err)
5221 goto done;
5222 err = got_ref_write(base_commit_ref, repo);
5223 if (err)
5224 goto done;
5225 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5226 if (*base_commit_id == NULL) {
5227 err = got_error_from_errno("got_object_id_dup");
5228 goto done;
5231 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5232 worktree->base_commit_id);
5233 if (err)
5234 goto done;
5235 err = got_ref_write(*tmp_branch, repo);
5236 if (err)
5237 goto done;
5239 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5240 if (err)
5241 goto done;
5242 done:
5243 free(fileindex_path);
5244 free(tmp_branch_name);
5245 free(branch_ref_name);
5246 free(base_commit_ref_name);
5247 if (wt_branch)
5248 got_ref_close(wt_branch);
5249 if (err) {
5250 if (*branch_ref) {
5251 got_ref_close(*branch_ref);
5252 *branch_ref = NULL;
5254 if (*tmp_branch) {
5255 got_ref_close(*tmp_branch);
5256 *tmp_branch = NULL;
5258 free(*base_commit_id);
5259 if (*fileindex) {
5260 got_fileindex_free(*fileindex);
5261 *fileindex = NULL;
5263 lock_worktree(worktree, LOCK_SH);
5265 return err;
5268 const struct got_error *
5269 got_worktree_histedit_postpone(struct got_worktree *worktree,
5270 struct got_fileindex *fileindex)
5272 if (fileindex)
5273 got_fileindex_free(fileindex);
5274 return lock_worktree(worktree, LOCK_SH);
5277 const struct got_error *
5278 got_worktree_histedit_in_progress(int *in_progress,
5279 struct got_worktree *worktree)
5281 const struct got_error *err;
5282 char *tmp_branch_name = NULL;
5284 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5285 if (err)
5286 return err;
5288 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5289 free(tmp_branch_name);
5290 return NULL;
5293 const struct got_error *
5294 got_worktree_histedit_continue(struct got_object_id **commit_id,
5295 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5296 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5297 struct got_worktree *worktree, struct got_repository *repo)
5299 const struct got_error *err;
5300 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5301 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5302 struct got_reference *commit_ref = NULL;
5303 struct got_reference *base_commit_ref = NULL;
5304 char *fileindex_path = NULL;
5305 int have_staged_files = 0;
5307 *commit_id = NULL;
5308 *tmp_branch = NULL;
5309 *base_commit_id = NULL;
5310 *fileindex = NULL;
5312 err = lock_worktree(worktree, LOCK_EX);
5313 if (err)
5314 return err;
5316 err = open_fileindex(fileindex, &fileindex_path, worktree);
5317 if (err)
5318 goto done;
5320 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5321 &have_staged_files);
5322 if (err && err->code != GOT_ERR_CANCELLED)
5323 goto done;
5324 if (have_staged_files) {
5325 err = got_error(GOT_ERR_STAGED_PATHS);
5326 goto done;
5329 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5330 if (err)
5331 goto done;
5333 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5334 if (err)
5335 goto done;
5337 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5338 if (err)
5339 goto done;
5341 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5342 worktree);
5343 if (err)
5344 goto done;
5346 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5347 if (err)
5348 goto done;
5350 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5351 if (err)
5352 goto done;
5353 err = got_ref_resolve(commit_id, repo, commit_ref);
5354 if (err)
5355 goto done;
5357 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5358 if (err)
5359 goto done;
5360 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5361 if (err)
5362 goto done;
5364 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5365 if (err)
5366 goto done;
5367 done:
5368 free(commit_ref_name);
5369 free(branch_ref_name);
5370 free(fileindex_path);
5371 if (commit_ref)
5372 got_ref_close(commit_ref);
5373 if (base_commit_ref)
5374 got_ref_close(base_commit_ref);
5375 if (err) {
5376 free(*commit_id);
5377 *commit_id = NULL;
5378 free(*base_commit_id);
5379 *base_commit_id = NULL;
5380 if (*tmp_branch) {
5381 got_ref_close(*tmp_branch);
5382 *tmp_branch = NULL;
5384 if (*fileindex) {
5385 got_fileindex_free(*fileindex);
5386 *fileindex = NULL;
5388 lock_worktree(worktree, LOCK_EX);
5390 return err;
5393 static const struct got_error *
5394 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5396 const struct got_error *err;
5397 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5398 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5400 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5401 if (err)
5402 goto done;
5403 err = delete_ref(tmp_branch_name, repo);
5404 if (err)
5405 goto done;
5407 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5408 worktree);
5409 if (err)
5410 goto done;
5411 err = delete_ref(base_commit_ref_name, repo);
5412 if (err)
5413 goto done;
5415 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5416 if (err)
5417 goto done;
5418 err = delete_ref(branch_ref_name, repo);
5419 if (err)
5420 goto done;
5422 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5423 if (err)
5424 goto done;
5425 err = delete_ref(commit_ref_name, repo);
5426 if (err)
5427 goto done;
5428 done:
5429 free(tmp_branch_name);
5430 free(base_commit_ref_name);
5431 free(branch_ref_name);
5432 free(commit_ref_name);
5433 return err;
5436 const struct got_error *
5437 got_worktree_histedit_abort(struct got_worktree *worktree,
5438 struct got_fileindex *fileindex, struct got_repository *repo,
5439 struct got_reference *branch, struct got_object_id *base_commit_id,
5440 got_worktree_checkout_cb progress_cb, void *progress_arg)
5442 const struct got_error *err, *unlockerr, *sync_err;
5443 struct got_reference *resolved = NULL;
5444 char *fileindex_path = NULL;
5445 struct got_object_id *tree_id = NULL;
5446 struct revert_file_args rfa;
5448 err = lock_worktree(worktree, LOCK_EX);
5449 if (err)
5450 return err;
5452 err = got_ref_open(&resolved, repo,
5453 got_ref_get_symref_target(branch), 0);
5454 if (err)
5455 goto done;
5457 err = got_worktree_set_head_ref(worktree, resolved);
5458 if (err)
5459 goto done;
5461 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5462 if (err)
5463 goto done;
5465 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5466 worktree->path_prefix);
5467 if (err)
5468 goto done;
5470 err = delete_histedit_refs(worktree, repo);
5471 if (err)
5472 goto done;
5474 err = get_fileindex_path(&fileindex_path, worktree);
5475 if (err)
5476 goto done;
5478 rfa.worktree = worktree;
5479 rfa.fileindex = fileindex;
5480 rfa.progress_cb = progress_cb;
5481 rfa.progress_arg = progress_arg;
5482 rfa.patch_cb = NULL;
5483 rfa.patch_arg = NULL;
5484 rfa.repo = repo;
5485 err = worktree_status(worktree, "", fileindex, repo,
5486 revert_file, &rfa, NULL, NULL);
5487 if (err)
5488 goto sync;
5490 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5491 repo, progress_cb, progress_arg, NULL, NULL);
5492 sync:
5493 sync_err = sync_fileindex(fileindex, fileindex_path);
5494 if (sync_err && err == NULL)
5495 err = sync_err;
5496 done:
5497 got_ref_close(resolved);
5498 free(tree_id);
5499 free(fileindex_path);
5501 unlockerr = lock_worktree(worktree, LOCK_SH);
5502 if (unlockerr && err == NULL)
5503 err = unlockerr;
5504 return err;
5507 const struct got_error *
5508 got_worktree_histedit_complete(struct got_worktree *worktree,
5509 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5510 struct got_reference *edited_branch, struct got_repository *repo)
5512 const struct got_error *err, *unlockerr;
5513 struct got_object_id *new_head_commit_id = NULL;
5514 struct got_reference *resolved = NULL;
5516 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5517 if (err)
5518 return err;
5520 err = got_ref_open(&resolved, repo,
5521 got_ref_get_symref_target(edited_branch), 0);
5522 if (err)
5523 goto done;
5525 err = got_ref_change_ref(resolved, new_head_commit_id);
5526 if (err)
5527 goto done;
5529 err = got_ref_write(resolved, repo);
5530 if (err)
5531 goto done;
5533 err = got_worktree_set_head_ref(worktree, resolved);
5534 if (err)
5535 goto done;
5537 err = delete_histedit_refs(worktree, repo);
5538 done:
5539 if (fileindex)
5540 got_fileindex_free(fileindex);
5541 free(new_head_commit_id);
5542 unlockerr = lock_worktree(worktree, LOCK_SH);
5543 if (unlockerr && err == NULL)
5544 err = unlockerr;
5545 return err;
5548 const struct got_error *
5549 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5550 struct got_object_id *commit_id, struct got_repository *repo)
5552 const struct got_error *err;
5553 char *commit_ref_name;
5555 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5556 if (err)
5557 return err;
5559 err = store_commit_id(commit_ref_name, commit_id, repo);
5560 if (err)
5561 goto done;
5563 err = delete_ref(commit_ref_name, repo);
5564 done:
5565 free(commit_ref_name);
5566 return err;
5569 struct check_stage_ok_arg {
5570 struct got_object_id *head_commit_id;
5571 struct got_worktree *worktree;
5572 struct got_fileindex *fileindex;
5573 struct got_repository *repo;
5574 int have_changes;
5577 const struct got_error *
5578 check_stage_ok(void *arg, unsigned char status,
5579 unsigned char staged_status, const char *relpath,
5580 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5581 struct got_object_id *commit_id)
5583 struct check_stage_ok_arg *a = arg;
5584 const struct got_error *err = NULL;
5585 struct got_fileindex_entry *ie;
5586 struct got_object_id base_commit_id;
5587 struct got_object_id *base_commit_idp = NULL;
5588 char *in_repo_path = NULL, *p;
5590 if (status == GOT_STATUS_UNVERSIONED)
5591 return NULL;
5592 if (status == GOT_STATUS_NONEXISTENT)
5593 return got_error_set_errno(ENOENT, relpath);
5595 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5596 if (ie == NULL)
5597 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5599 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5600 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5601 relpath) == -1)
5602 return got_error_from_errno("asprintf");
5604 if (got_fileindex_entry_has_commit(ie)) {
5605 memcpy(base_commit_id.sha1, ie->commit_sha1,
5606 SHA1_DIGEST_LENGTH);
5607 base_commit_idp = &base_commit_id;
5610 if (status == GOT_STATUS_NO_CHANGE) {
5611 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5612 goto done;
5613 } else if (status == GOT_STATUS_CONFLICT) {
5614 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5615 goto done;
5616 } else if (status != GOT_STATUS_ADD &&
5617 status != GOT_STATUS_MODIFY &&
5618 status != GOT_STATUS_DELETE) {
5619 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5620 goto done;
5623 a->have_changes = 1;
5625 p = in_repo_path;
5626 while (p[0] == '/')
5627 p++;
5628 err = check_out_of_date(p, status, staged_status,
5629 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5630 GOT_ERR_STAGE_OUT_OF_DATE);
5631 done:
5632 free(in_repo_path);
5633 return err;
5636 struct stage_path_arg {
5637 struct got_worktree *worktree;
5638 struct got_fileindex *fileindex;
5639 struct got_repository *repo;
5640 got_worktree_status_cb status_cb;
5641 void *status_arg;
5642 got_worktree_patch_cb patch_cb;
5643 void *patch_arg;
5646 static const struct got_error *
5647 stage_path(void *arg, unsigned char status,
5648 unsigned char staged_status, const char *relpath,
5649 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5650 struct got_object_id *commit_id)
5652 struct stage_path_arg *a = arg;
5653 const struct got_error *err = NULL;
5654 struct got_fileindex_entry *ie;
5655 char *ondisk_path = NULL, *path_content = NULL;
5656 uint32_t stage;
5657 struct got_object_id *new_staged_blob_id = NULL;
5659 if (status == GOT_STATUS_UNVERSIONED)
5660 return NULL;
5662 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5663 if (ie == NULL)
5664 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5666 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5667 relpath)== -1)
5668 return got_error_from_errno("asprintf");
5670 switch (status) {
5671 case GOT_STATUS_ADD:
5672 case GOT_STATUS_MODIFY:
5673 if (a->patch_cb) {
5674 if (status == GOT_STATUS_ADD) {
5675 int choice = GOT_PATCH_CHOICE_NONE;
5676 err = (*a->patch_cb)(&choice, a->patch_arg,
5677 status, ie->path, NULL, 1, 1);
5678 if (err)
5679 break;
5680 if (choice != GOT_PATCH_CHOICE_YES)
5681 break;
5682 } else {
5683 err = create_patched_content(&path_content, 0,
5684 staged_blob_id ? staged_blob_id : blob_id,
5685 ondisk_path, ie->path, a->repo,
5686 a->patch_cb, a->patch_arg);
5687 if (err || path_content == NULL)
5688 break;
5691 err = got_object_blob_create(&new_staged_blob_id,
5692 path_content ? path_content : ondisk_path, a->repo);
5693 if (err)
5694 break;
5695 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5696 SHA1_DIGEST_LENGTH);
5697 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5698 stage = GOT_FILEIDX_STAGE_ADD;
5699 else
5700 stage = GOT_FILEIDX_STAGE_MODIFY;
5701 got_fileindex_entry_stage_set(ie, stage);
5702 if (a->status_cb == NULL)
5703 break;
5704 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5705 get_staged_status(ie), relpath, blob_id,
5706 new_staged_blob_id, NULL);
5707 break;
5708 case GOT_STATUS_DELETE:
5709 if (staged_status == GOT_STATUS_DELETE)
5710 break;
5711 if (a->patch_cb) {
5712 int choice = GOT_PATCH_CHOICE_NONE;
5713 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5714 ie->path, NULL, 1, 1);
5715 if (err)
5716 break;
5717 if (choice == GOT_PATCH_CHOICE_NO)
5718 break;
5719 if (choice != GOT_PATCH_CHOICE_YES) {
5720 err = got_error(GOT_ERR_PATCH_CHOICE);
5721 break;
5724 stage = GOT_FILEIDX_STAGE_DELETE;
5725 got_fileindex_entry_stage_set(ie, stage);
5726 if (a->status_cb == NULL)
5727 break;
5728 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5729 get_staged_status(ie), relpath, NULL, NULL, NULL);
5730 break;
5731 case GOT_STATUS_NO_CHANGE:
5732 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5733 break;
5734 case GOT_STATUS_CONFLICT:
5735 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5736 break;
5737 case GOT_STATUS_NONEXISTENT:
5738 err = got_error_set_errno(ENOENT, relpath);
5739 break;
5740 default:
5741 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5742 break;
5745 if (path_content && unlink(path_content) == -1 && err == NULL)
5746 err = got_error_from_errno2("unlink", path_content);
5747 free(path_content);
5748 free(ondisk_path);
5749 free(new_staged_blob_id);
5750 return err;
5753 const struct got_error *
5754 got_worktree_stage(struct got_worktree *worktree,
5755 struct got_pathlist_head *paths,
5756 got_worktree_status_cb status_cb, void *status_arg,
5757 got_worktree_patch_cb patch_cb, void *patch_arg,
5758 struct got_repository *repo)
5760 const struct got_error *err = NULL, *sync_err, *unlockerr;
5761 struct got_pathlist_entry *pe;
5762 struct got_fileindex *fileindex = NULL;
5763 char *fileindex_path = NULL;
5764 struct got_reference *head_ref = NULL;
5765 struct got_object_id *head_commit_id = NULL;
5766 struct check_stage_ok_arg oka;
5767 struct stage_path_arg spa;
5769 err = lock_worktree(worktree, LOCK_EX);
5770 if (err)
5771 return err;
5773 err = got_ref_open(&head_ref, repo,
5774 got_worktree_get_head_ref_name(worktree), 0);
5775 if (err)
5776 goto done;
5777 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5778 if (err)
5779 goto done;
5780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5781 if (err)
5782 goto done;
5784 /* Check pre-conditions before staging anything. */
5785 oka.head_commit_id = head_commit_id;
5786 oka.worktree = worktree;
5787 oka.fileindex = fileindex;
5788 oka.repo = repo;
5789 oka.have_changes = 0;
5790 TAILQ_FOREACH(pe, paths, entry) {
5791 err = worktree_status(worktree, pe->path, fileindex, repo,
5792 check_stage_ok, &oka, NULL, NULL);
5793 if (err)
5794 goto done;
5796 if (!oka.have_changes) {
5797 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5798 goto done;
5801 spa.worktree = worktree;
5802 spa.fileindex = fileindex;
5803 spa.repo = repo;
5804 spa.patch_cb = patch_cb;
5805 spa.patch_arg = patch_arg;
5806 spa.status_cb = status_cb;
5807 spa.status_arg = status_arg;
5808 TAILQ_FOREACH(pe, paths, entry) {
5809 err = worktree_status(worktree, pe->path, fileindex, repo,
5810 stage_path, &spa, NULL, NULL);
5811 if (err)
5812 goto done;
5815 sync_err = sync_fileindex(fileindex, fileindex_path);
5816 if (sync_err && err == NULL)
5817 err = sync_err;
5818 done:
5819 if (head_ref)
5820 got_ref_close(head_ref);
5821 free(head_commit_id);
5822 free(fileindex_path);
5823 if (fileindex)
5824 got_fileindex_free(fileindex);
5825 unlockerr = lock_worktree(worktree, LOCK_SH);
5826 if (unlockerr && err == NULL)
5827 err = unlockerr;
5828 return err;
5831 struct unstage_path_arg {
5832 struct got_worktree *worktree;
5833 struct got_fileindex *fileindex;
5834 struct got_repository *repo;
5835 got_worktree_checkout_cb progress_cb;
5836 void *progress_arg;
5837 got_worktree_patch_cb patch_cb;
5838 void *patch_arg;
5841 static const struct got_error *
5842 create_unstaged_content(char **path_unstaged_content,
5843 char **path_new_staged_content, struct got_object_id *blob_id,
5844 struct got_object_id *staged_blob_id, const char *relpath,
5845 struct got_repository *repo,
5846 got_worktree_patch_cb patch_cb, void *patch_arg)
5848 const struct got_error *err;
5849 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5850 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5851 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5852 struct stat sb1, sb2;
5853 struct got_diff_changes *changes = NULL;
5854 struct got_diff_state *ds = NULL;
5855 struct got_diff_args *args = NULL;
5856 struct got_diff_change *change;
5857 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5858 int have_content = 0, have_rejected_content = 0;
5860 *path_unstaged_content = NULL;
5861 *path_new_staged_content = NULL;
5863 err = got_object_id_str(&label1, blob_id);
5864 if (err)
5865 return err;
5866 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5867 if (err)
5868 goto done;
5870 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5871 if (err)
5872 goto done;
5874 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5875 if (err)
5876 goto done;
5878 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5879 if (err)
5880 goto done;
5882 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5883 if (err)
5884 goto done;
5886 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5887 if (err)
5888 goto done;
5890 if (stat(path1, &sb1) == -1) {
5891 err = got_error_from_errno2("stat", path1);
5892 goto done;
5895 if (stat(path2, &sb2) == -1) {
5896 err = got_error_from_errno2("stat", path2);
5897 goto done;
5900 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5901 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5902 if (err)
5903 goto done;
5905 err = got_opentemp_named(path_unstaged_content, &outfile,
5906 "got-unstaged-content");
5907 if (err)
5908 goto done;
5909 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5910 "got-new-staged-content");
5911 if (err)
5912 goto done;
5914 if (fseek(f1, 0L, SEEK_SET) == -1) {
5915 err = got_ferror(f1, GOT_ERR_IO);
5916 goto done;
5918 if (fseek(f2, 0L, SEEK_SET) == -1) {
5919 err = got_ferror(f2, GOT_ERR_IO);
5920 goto done;
5922 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5923 int choice;
5924 err = apply_or_reject_change(&choice, change, ++n,
5925 changes->nchanges, ds, args, diff_flags, relpath,
5926 f1, f2, &line_cur1, &line_cur2,
5927 outfile, rejectfile, patch_cb, patch_arg);
5928 if (err)
5929 goto done;
5930 if (choice == GOT_PATCH_CHOICE_YES)
5931 have_content = 1;
5932 else
5933 have_rejected_content = 1;
5934 if (choice == GOT_PATCH_CHOICE_QUIT)
5935 break;
5937 if (have_content || have_rejected_content)
5938 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5939 outfile, rejectfile);
5940 done:
5941 free(label1);
5942 if (blob)
5943 got_object_blob_close(blob);
5944 if (staged_blob)
5945 got_object_blob_close(staged_blob);
5946 if (f1 && fclose(f1) == EOF && err == NULL)
5947 err = got_error_from_errno2("fclose", path1);
5948 if (f2 && fclose(f2) == EOF && err == NULL)
5949 err = got_error_from_errno2("fclose", path2);
5950 if (outfile && fclose(outfile) == EOF && err == NULL)
5951 err = got_error_from_errno2("fclose", *path_unstaged_content);
5952 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5953 err = got_error_from_errno2("fclose", *path_new_staged_content);
5954 if (path1 && unlink(path1) == -1 && err == NULL)
5955 err = got_error_from_errno2("unlink", path1);
5956 if (path2 && unlink(path2) == -1 && err == NULL)
5957 err = got_error_from_errno2("unlink", path2);
5958 if (err || !have_content) {
5959 if (*path_unstaged_content &&
5960 unlink(*path_unstaged_content) == -1 && err == NULL)
5961 err = got_error_from_errno2("unlink",
5962 *path_unstaged_content);
5963 free(*path_unstaged_content);
5964 *path_unstaged_content = NULL;
5966 if (err || !have_rejected_content) {
5967 if (*path_new_staged_content &&
5968 unlink(*path_new_staged_content) == -1 && err == NULL)
5969 err = got_error_from_errno2("unlink",
5970 *path_new_staged_content);
5971 free(*path_new_staged_content);
5972 *path_new_staged_content = NULL;
5974 free(args);
5975 if (ds) {
5976 got_diff_state_free(ds);
5977 free(ds);
5979 if (changes)
5980 got_diff_free_changes(changes);
5981 free(path1);
5982 free(path2);
5983 return err;
5986 static const struct got_error *
5987 unstage_path(void *arg, unsigned char status,
5988 unsigned char staged_status, const char *relpath,
5989 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5990 struct got_object_id *commit_id)
5992 const struct got_error *err = NULL;
5993 struct unstage_path_arg *a = arg;
5994 struct got_fileindex_entry *ie;
5995 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5996 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5997 char *path_new_staged_content = NULL;
5998 int local_changes_subsumed;
5999 struct stat sb;
6001 if (staged_status != GOT_STATUS_ADD &&
6002 staged_status != GOT_STATUS_MODIFY &&
6003 staged_status != GOT_STATUS_DELETE)
6004 return NULL;
6006 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6007 if (ie == NULL)
6008 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6010 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6011 == -1)
6012 return got_error_from_errno("asprintf");
6014 switch (staged_status) {
6015 case GOT_STATUS_MODIFY:
6016 err = got_object_open_as_blob(&blob_base, a->repo,
6017 blob_id, 8192);
6018 if (err)
6019 break;
6020 /* fall through */
6021 case GOT_STATUS_ADD:
6022 if (a->patch_cb) {
6023 if (staged_status == GOT_STATUS_ADD) {
6024 int choice = GOT_PATCH_CHOICE_NONE;
6025 err = (*a->patch_cb)(&choice, a->patch_arg,
6026 staged_status, ie->path, NULL, 1, 1);
6027 if (err)
6028 break;
6029 if (choice != GOT_PATCH_CHOICE_YES)
6030 break;
6031 } else {
6032 err = create_unstaged_content(
6033 &path_unstaged_content,
6034 &path_new_staged_content, blob_id,
6035 staged_blob_id, ie->path, a->repo,
6036 a->patch_cb, a->patch_arg);
6037 if (err || path_unstaged_content == NULL)
6038 break;
6039 if (path_new_staged_content) {
6040 err = got_object_blob_create(
6041 &staged_blob_id,
6042 path_new_staged_content,
6043 a->repo);
6044 if (err)
6045 break;
6046 memcpy(ie->staged_blob_sha1,
6047 staged_blob_id->sha1,
6048 SHA1_DIGEST_LENGTH);
6050 err = merge_file(&local_changes_subsumed,
6051 a->worktree, blob_base, ondisk_path,
6052 relpath, got_fileindex_perms_to_st(ie),
6053 path_unstaged_content, "unstaged",
6054 a->repo, a->progress_cb, a->progress_arg);
6055 if (err == NULL &&
6056 path_new_staged_content == NULL)
6057 got_fileindex_entry_stage_set(ie,
6058 GOT_FILEIDX_STAGE_NONE);
6059 break; /* Done with this file. */
6062 err = got_object_open_as_blob(&blob_staged, a->repo,
6063 staged_blob_id, 8192);
6064 if (err)
6065 break;
6066 err = merge_blob(&local_changes_subsumed, a->worktree,
6067 blob_base, ondisk_path, relpath,
6068 got_fileindex_perms_to_st(ie), blob_staged,
6069 commit_id ? commit_id : a->worktree->base_commit_id,
6070 a->repo, a->progress_cb, a->progress_arg);
6071 if (err == NULL)
6072 got_fileindex_entry_stage_set(ie,
6073 GOT_FILEIDX_STAGE_NONE);
6074 break;
6075 case GOT_STATUS_DELETE:
6076 if (a->patch_cb) {
6077 int choice = GOT_PATCH_CHOICE_NONE;
6078 err = (*a->patch_cb)(&choice, a->patch_arg,
6079 staged_status, ie->path, NULL, 1, 1);
6080 if (err)
6081 break;
6082 if (choice == GOT_PATCH_CHOICE_NO)
6083 break;
6084 if (choice != GOT_PATCH_CHOICE_YES) {
6085 err = got_error(GOT_ERR_PATCH_CHOICE);
6086 break;
6089 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6090 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6091 if (err)
6092 break;
6093 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6094 break;
6097 free(ondisk_path);
6098 if (path_unstaged_content &&
6099 unlink(path_unstaged_content) == -1 && err == NULL)
6100 err = got_error_from_errno2("unlink", path_unstaged_content);
6101 if (path_new_staged_content &&
6102 unlink(path_new_staged_content) == -1 && err == NULL)
6103 err = got_error_from_errno2("unlink", path_new_staged_content);
6104 free(path_unstaged_content);
6105 free(path_new_staged_content);
6106 if (blob_base)
6107 got_object_blob_close(blob_base);
6108 if (blob_staged)
6109 got_object_blob_close(blob_staged);
6110 return err;
6113 const struct got_error *
6114 got_worktree_unstage(struct got_worktree *worktree,
6115 struct got_pathlist_head *paths,
6116 got_worktree_checkout_cb progress_cb, void *progress_arg,
6117 got_worktree_patch_cb patch_cb, void *patch_arg,
6118 struct got_repository *repo)
6120 const struct got_error *err = NULL, *sync_err, *unlockerr;
6121 struct got_pathlist_entry *pe;
6122 struct got_fileindex *fileindex = NULL;
6123 char *fileindex_path = NULL;
6124 struct unstage_path_arg upa;
6126 err = lock_worktree(worktree, LOCK_EX);
6127 if (err)
6128 return err;
6130 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6131 if (err)
6132 goto done;
6134 upa.worktree = worktree;
6135 upa.fileindex = fileindex;
6136 upa.repo = repo;
6137 upa.progress_cb = progress_cb;
6138 upa.progress_arg = progress_arg;
6139 upa.patch_cb = patch_cb;
6140 upa.patch_arg = patch_arg;
6141 TAILQ_FOREACH(pe, paths, entry) {
6142 err = worktree_status(worktree, pe->path, fileindex, repo,
6143 unstage_path, &upa, NULL, NULL);
6144 if (err)
6145 goto done;
6148 sync_err = sync_fileindex(fileindex, fileindex_path);
6149 if (sync_err && err == NULL)
6150 err = sync_err;
6151 done:
6152 free(fileindex_path);
6153 if (fileindex)
6154 got_fileindex_free(fileindex);
6155 unlockerr = lock_worktree(worktree, LOCK_SH);
6156 if (unlockerr && err == NULL)
6157 err = unlockerr;
6158 return err;