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 free(line);
2868 if (linelen == -1 && ferror(f))
2869 return got_error_from_errno("getline");
2870 return NULL;
2873 static const struct got_error *
2874 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2875 int start_old, int end_old, int start_new, int end_new,
2876 FILE *outfile, FILE *rejectfile)
2878 const struct got_error *err;
2880 /* Copy old file's lines leading up to patch. */
2881 while (!feof(f1) && *line_cur1 < start_old) {
2882 err = copy_one_line(f1, outfile, NULL);
2883 if (err)
2884 return err;
2885 (*line_cur1)++;
2887 /* Skip new file's lines leading up to patch. */
2888 while (!feof(f2) && *line_cur2 < start_new) {
2889 if (rejectfile)
2890 err = copy_one_line(f2, NULL, rejectfile);
2891 else
2892 err = skip_one_line(f2);
2893 if (err)
2894 return err;
2895 (*line_cur2)++;
2897 /* Copy patched lines. */
2898 while (!feof(f2) && *line_cur2 <= end_new) {
2899 err = copy_one_line(f2, outfile, NULL);
2900 if (err)
2901 return err;
2902 (*line_cur2)++;
2904 /* Skip over old file's replaced lines. */
2905 while (!feof(f1) && *line_cur1 <= end_old) {
2906 if (rejectfile)
2907 err = copy_one_line(f1, NULL, rejectfile);
2908 else
2909 err = skip_one_line(f1);
2910 if (err)
2911 return err;
2912 (*line_cur1)++;
2915 return NULL;
2918 static const struct got_error *
2919 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2920 FILE *outfile, FILE *rejectfile)
2922 const struct got_error *err;
2924 if (outfile) {
2925 /* Copy old file's lines until EOF. */
2926 while (!feof(f1)) {
2927 err = copy_one_line(f1, outfile, NULL);
2928 if (err)
2929 return err;
2930 (*line_cur1)++;
2933 if (rejectfile) {
2934 /* Copy new file's lines until EOF. */
2935 while (!feof(f2)) {
2936 err = copy_one_line(f2, NULL, rejectfile);
2937 if (err)
2938 return err;
2939 (*line_cur2)++;
2943 return NULL;
2946 static const struct got_error *
2947 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2948 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2949 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2950 int *line_cur2, FILE *outfile, FILE *rejectfile,
2951 got_worktree_patch_cb patch_cb, void *patch_arg)
2953 const struct got_error *err = NULL;
2954 int start_old = change->cv.a;
2955 int end_old = change->cv.b;
2956 int start_new = change->cv.c;
2957 int end_new = change->cv.d;
2958 long pos1, pos2;
2959 FILE *hunkfile;
2961 *choice = GOT_PATCH_CHOICE_NONE;
2963 hunkfile = got_opentemp();
2964 if (hunkfile == NULL)
2965 return got_error_from_errno("got_opentemp");
2967 pos1 = ftell(f1);
2968 pos2 = ftell(f2);
2970 /* XXX TODO needs error checking */
2971 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2973 if (fseek(f1, pos1, SEEK_SET) == -1) {
2974 err = got_ferror(f1, GOT_ERR_IO);
2975 goto done;
2977 if (fseek(f2, pos2, SEEK_SET) == -1) {
2978 err = got_ferror(f1, GOT_ERR_IO);
2979 goto done;
2981 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2982 err = got_ferror(hunkfile, GOT_ERR_IO);
2983 goto done;
2986 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2987 hunkfile, n, nchanges);
2988 if (err)
2989 goto done;
2991 switch (*choice) {
2992 case GOT_PATCH_CHOICE_YES:
2993 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2994 end_old, start_new, end_new, outfile, rejectfile);
2995 break;
2996 case GOT_PATCH_CHOICE_NO:
2997 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2998 end_old, start_new, end_new, rejectfile, outfile);
2999 break;
3000 case GOT_PATCH_CHOICE_QUIT:
3001 break;
3002 default:
3003 err = got_error(GOT_ERR_PATCH_CHOICE);
3004 break;
3006 done:
3007 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3008 err = got_error_from_errno("fclose");
3009 return err;
3012 struct revert_file_args {
3013 struct got_worktree *worktree;
3014 struct got_fileindex *fileindex;
3015 got_worktree_checkout_cb progress_cb;
3016 void *progress_arg;
3017 got_worktree_patch_cb patch_cb;
3018 void *patch_arg;
3019 struct got_repository *repo;
3022 static const struct got_error *
3023 create_patched_content(char **path_outfile, int reverse_patch,
3024 struct got_object_id *blob_id, const char *path2,
3025 const char *relpath, struct got_repository *repo,
3026 got_worktree_patch_cb patch_cb, void *patch_arg)
3028 const struct got_error *err;
3029 struct got_blob_object *blob = NULL;
3030 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3031 char *path1 = NULL, *id_str = NULL;
3032 struct stat sb1, sb2;
3033 struct got_diff_changes *changes = NULL;
3034 struct got_diff_state *ds = NULL;
3035 struct got_diff_args *args = NULL;
3036 struct got_diff_change *change;
3037 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3038 int n = 0;
3040 *path_outfile = NULL;
3042 err = got_object_id_str(&id_str, blob_id);
3043 if (err)
3044 return err;
3046 f2 = fopen(path2, "r");
3047 if (f2 == NULL) {
3048 err = got_error_from_errno2("fopen", path2);
3049 goto done;
3052 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3053 if (err)
3054 goto done;
3056 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3057 if (err)
3058 goto done;
3060 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3061 if (err)
3062 goto done;
3064 if (stat(path1, &sb1) == -1) {
3065 err = got_error_from_errno2("stat", path1);
3066 goto done;
3068 if (stat(path2, &sb2) == -1) {
3069 err = got_error_from_errno2("stat", path2);
3070 goto done;
3073 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3074 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3075 if (err)
3076 goto done;
3078 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3079 if (err)
3080 goto done;
3082 if (fseek(f1, 0L, SEEK_SET) == -1)
3083 return got_ferror(f1, GOT_ERR_IO);
3084 if (fseek(f2, 0L, SEEK_SET) == -1)
3085 return got_ferror(f2, GOT_ERR_IO);
3086 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3087 int choice;
3088 err = apply_or_reject_change(&choice, change, ++n,
3089 changes->nchanges, ds, args, diff_flags, relpath,
3090 f1, f2, &line_cur1, &line_cur2,
3091 reverse_patch ? NULL : outfile,
3092 reverse_patch ? outfile : NULL,
3093 patch_cb, patch_arg);
3094 if (err)
3095 goto done;
3096 if (choice == GOT_PATCH_CHOICE_YES)
3097 have_content = 1;
3098 else if (choice == GOT_PATCH_CHOICE_QUIT)
3099 break;
3101 if (have_content)
3102 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3103 reverse_patch ? NULL : outfile,
3104 reverse_patch ? outfile : NULL);
3105 done:
3106 free(id_str);
3107 if (blob)
3108 got_object_blob_close(blob);
3109 if (f1 && fclose(f1) == EOF && err == NULL)
3110 err = got_error_from_errno2("fclose", path1);
3111 if (f2 && fclose(f2) == EOF && err == NULL)
3112 err = got_error_from_errno2("fclose", path2);
3113 if (outfile && fclose(outfile) == EOF && err == NULL)
3114 err = got_error_from_errno2("fclose", *path_outfile);
3115 if (path1 && unlink(path1) == -1 && err == NULL)
3116 err = got_error_from_errno2("unlink", path1);
3117 if (err || !have_content) {
3118 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3119 err = got_error_from_errno2("unlink", *path_outfile);
3120 free(*path_outfile);
3121 *path_outfile = NULL;
3123 free(args);
3124 if (ds) {
3125 got_diff_state_free(ds);
3126 free(ds);
3128 if (changes)
3129 got_diff_free_changes(changes);
3130 free(path1);
3131 return err;
3134 static const struct got_error *
3135 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3136 const char *relpath, struct got_object_id *blob_id,
3137 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3139 struct revert_file_args *a = arg;
3140 const struct got_error *err = NULL;
3141 char *parent_path = NULL;
3142 struct got_fileindex_entry *ie;
3143 struct got_tree_object *tree = NULL;
3144 struct got_object_id *tree_id = NULL;
3145 const struct got_tree_entry *te = NULL;
3146 char *tree_path = NULL, *te_name;
3147 char *ondisk_path = NULL, *path_content = NULL;
3148 struct got_blob_object *blob = NULL;
3150 /* Reverting a staged deletion is a no-op. */
3151 if (status == GOT_STATUS_DELETE &&
3152 staged_status != GOT_STATUS_NO_CHANGE)
3153 return NULL;
3155 if (status == GOT_STATUS_UNVERSIONED)
3156 return (*a->progress_cb)(a->progress_arg,
3157 GOT_STATUS_UNVERSIONED, relpath);
3159 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3160 if (ie == NULL)
3161 return got_error(GOT_ERR_BAD_PATH);
3163 /* Construct in-repository path of tree which contains this blob. */
3164 err = got_path_dirname(&parent_path, ie->path);
3165 if (err) {
3166 if (err->code != GOT_ERR_BAD_PATH)
3167 goto done;
3168 parent_path = strdup("/");
3169 if (parent_path == NULL) {
3170 err = got_error_from_errno("strdup");
3171 goto done;
3174 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3175 tree_path = strdup(parent_path);
3176 if (tree_path == NULL) {
3177 err = got_error_from_errno("strdup");
3178 goto done;
3180 } else {
3181 if (got_path_is_root_dir(parent_path)) {
3182 tree_path = strdup(a->worktree->path_prefix);
3183 if (tree_path == NULL) {
3184 err = got_error_from_errno("strdup");
3185 goto done;
3187 } else {
3188 if (asprintf(&tree_path, "%s/%s",
3189 a->worktree->path_prefix, parent_path) == -1) {
3190 err = got_error_from_errno("asprintf");
3191 goto done;
3196 err = got_object_id_by_path(&tree_id, a->repo,
3197 a->worktree->base_commit_id, tree_path);
3198 if (err) {
3199 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3200 (status == GOT_STATUS_ADD ||
3201 staged_status == GOT_STATUS_ADD)))
3202 goto done;
3203 } else {
3204 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3205 if (err)
3206 goto done;
3208 te_name = basename(ie->path);
3209 if (te_name == NULL) {
3210 err = got_error_from_errno2("basename", ie->path);
3211 goto done;
3214 te = got_object_tree_find_entry(tree, te_name);
3215 if (te == NULL && status != GOT_STATUS_ADD &&
3216 staged_status != GOT_STATUS_ADD) {
3217 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3218 goto done;
3222 switch (status) {
3223 case GOT_STATUS_ADD:
3224 if (a->patch_cb) {
3225 int choice = GOT_PATCH_CHOICE_NONE;
3226 err = (*a->patch_cb)(&choice, a->patch_arg,
3227 status, ie->path, NULL, 1, 1);
3228 if (err)
3229 goto done;
3230 if (choice != GOT_PATCH_CHOICE_YES)
3231 break;
3233 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3234 ie->path);
3235 if (err)
3236 goto done;
3237 got_fileindex_entry_remove(a->fileindex, ie);
3238 break;
3239 case GOT_STATUS_DELETE:
3240 if (a->patch_cb) {
3241 int choice = GOT_PATCH_CHOICE_NONE;
3242 err = (*a->patch_cb)(&choice, a->patch_arg,
3243 status, ie->path, NULL, 1, 1);
3244 if (err)
3245 goto done;
3246 if (choice != GOT_PATCH_CHOICE_YES)
3247 break;
3249 /* fall through */
3250 case GOT_STATUS_MODIFY:
3251 case GOT_STATUS_CONFLICT:
3252 case GOT_STATUS_MISSING: {
3253 struct got_object_id id;
3254 if (staged_status == GOT_STATUS_ADD ||
3255 staged_status == GOT_STATUS_MODIFY) {
3256 memcpy(id.sha1, ie->staged_blob_sha1,
3257 SHA1_DIGEST_LENGTH);
3258 } else
3259 memcpy(id.sha1, ie->blob_sha1,
3260 SHA1_DIGEST_LENGTH);
3261 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3262 if (err)
3263 goto done;
3265 if (asprintf(&ondisk_path, "%s/%s",
3266 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3267 err = got_error_from_errno("asprintf");
3268 goto done;
3271 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3272 status == GOT_STATUS_CONFLICT)) {
3273 err = create_patched_content(&path_content, 1, &id,
3274 ondisk_path, ie->path, a->repo,
3275 a->patch_cb, a->patch_arg);
3276 if (err || path_content == NULL)
3277 break;
3278 if (rename(path_content, ondisk_path) == -1) {
3279 err = got_error_from_errno3("rename",
3280 path_content, ondisk_path);
3281 goto done;
3283 } else {
3284 err = install_blob(a->worktree, ondisk_path, ie->path,
3285 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3286 got_fileindex_perms_to_st(ie), blob, 0, 1,
3287 a->repo, a->progress_cb, a->progress_arg);
3288 if (err)
3289 goto done;
3290 if (status == GOT_STATUS_DELETE) {
3291 err = update_blob_fileindex_entry(a->worktree,
3292 a->fileindex, ie, ondisk_path, ie->path,
3293 blob, 1);
3294 if (err)
3295 goto done;
3298 break;
3300 default:
3301 break;
3303 done:
3304 free(ondisk_path);
3305 free(path_content);
3306 free(parent_path);
3307 free(tree_path);
3308 if (blob)
3309 got_object_blob_close(blob);
3310 if (tree)
3311 got_object_tree_close(tree);
3312 free(tree_id);
3313 return err;
3316 const struct got_error *
3317 got_worktree_revert(struct got_worktree *worktree,
3318 struct got_pathlist_head *paths,
3319 got_worktree_checkout_cb progress_cb, void *progress_arg,
3320 got_worktree_patch_cb patch_cb, void *patch_arg,
3321 struct got_repository *repo)
3323 struct got_fileindex *fileindex = NULL;
3324 char *fileindex_path = NULL;
3325 const struct got_error *err = NULL, *unlockerr = NULL;
3326 const struct got_error *sync_err = NULL;
3327 struct got_pathlist_entry *pe;
3328 struct revert_file_args rfa;
3330 err = lock_worktree(worktree, LOCK_EX);
3331 if (err)
3332 return err;
3334 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3335 if (err)
3336 goto done;
3338 rfa.worktree = worktree;
3339 rfa.fileindex = fileindex;
3340 rfa.progress_cb = progress_cb;
3341 rfa.progress_arg = progress_arg;
3342 rfa.patch_cb = patch_cb;
3343 rfa.patch_arg = patch_arg;
3344 rfa.repo = repo;
3345 TAILQ_FOREACH(pe, paths, entry) {
3346 err = worktree_status(worktree, pe->path, fileindex, repo,
3347 revert_file, &rfa, NULL, NULL);
3348 if (err)
3349 break;
3351 sync_err = sync_fileindex(fileindex, fileindex_path);
3352 if (sync_err && err == NULL)
3353 err = sync_err;
3354 done:
3355 free(fileindex_path);
3356 if (fileindex)
3357 got_fileindex_free(fileindex);
3358 unlockerr = lock_worktree(worktree, LOCK_SH);
3359 if (unlockerr && err == NULL)
3360 err = unlockerr;
3361 return err;
3364 static void
3365 free_commitable(struct got_commitable *ct)
3367 free(ct->path);
3368 free(ct->in_repo_path);
3369 free(ct->ondisk_path);
3370 free(ct->blob_id);
3371 free(ct->base_blob_id);
3372 free(ct->staged_blob_id);
3373 free(ct->base_commit_id);
3374 free(ct);
3377 struct collect_commitables_arg {
3378 struct got_pathlist_head *commitable_paths;
3379 struct got_repository *repo;
3380 struct got_worktree *worktree;
3381 int have_staged_files;
3384 static const struct got_error *
3385 collect_commitables(void *arg, unsigned char status,
3386 unsigned char staged_status, const char *relpath,
3387 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3388 struct got_object_id *commit_id)
3390 struct collect_commitables_arg *a = arg;
3391 const struct got_error *err = NULL;
3392 struct got_commitable *ct = NULL;
3393 struct got_pathlist_entry *new = NULL;
3394 char *parent_path = NULL, *path = NULL;
3395 struct stat sb;
3397 if (a->have_staged_files) {
3398 if (staged_status != GOT_STATUS_MODIFY &&
3399 staged_status != GOT_STATUS_ADD &&
3400 staged_status != GOT_STATUS_DELETE)
3401 return NULL;
3402 } else {
3403 if (status == GOT_STATUS_CONFLICT)
3404 return got_error(GOT_ERR_COMMIT_CONFLICT);
3406 if (status != GOT_STATUS_MODIFY &&
3407 status != GOT_STATUS_ADD &&
3408 status != GOT_STATUS_DELETE)
3409 return NULL;
3412 if (asprintf(&path, "/%s", relpath) == -1) {
3413 err = got_error_from_errno("asprintf");
3414 goto done;
3416 if (strcmp(path, "/") == 0) {
3417 parent_path = strdup("");
3418 if (parent_path == NULL)
3419 return got_error_from_errno("strdup");
3420 } else {
3421 err = got_path_dirname(&parent_path, path);
3422 if (err)
3423 return err;
3426 ct = calloc(1, sizeof(*ct));
3427 if (ct == NULL) {
3428 err = got_error_from_errno("calloc");
3429 goto done;
3432 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3433 relpath) == -1) {
3434 err = got_error_from_errno("asprintf");
3435 goto done;
3437 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3438 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3439 } else {
3440 if (lstat(ct->ondisk_path, &sb) != 0) {
3441 err = got_error_from_errno2("lstat", ct->ondisk_path);
3442 goto done;
3444 ct->mode = sb.st_mode;
3447 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3448 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3449 relpath) == -1) {
3450 err = got_error_from_errno("asprintf");
3451 goto done;
3454 ct->status = status;
3455 ct->staged_status = staged_status;
3456 ct->blob_id = NULL; /* will be filled in when blob gets created */
3457 if (ct->status != GOT_STATUS_ADD &&
3458 ct->staged_status != GOT_STATUS_ADD) {
3459 ct->base_blob_id = got_object_id_dup(blob_id);
3460 if (ct->base_blob_id == NULL) {
3461 err = got_error_from_errno("got_object_id_dup");
3462 goto done;
3464 ct->base_commit_id = got_object_id_dup(commit_id);
3465 if (ct->base_commit_id == NULL) {
3466 err = got_error_from_errno("got_object_id_dup");
3467 goto done;
3470 if (ct->staged_status == GOT_STATUS_ADD ||
3471 ct->staged_status == GOT_STATUS_MODIFY) {
3472 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3473 if (ct->staged_blob_id == NULL) {
3474 err = got_error_from_errno("got_object_id_dup");
3475 goto done;
3478 ct->path = strdup(path);
3479 if (ct->path == NULL) {
3480 err = got_error_from_errno("strdup");
3481 goto done;
3483 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3484 done:
3485 if (ct && (err || new == NULL))
3486 free_commitable(ct);
3487 free(parent_path);
3488 free(path);
3489 return err;
3492 static const struct got_error *write_tree(struct got_object_id **,
3493 struct got_tree_object *, const char *, struct got_pathlist_head *,
3494 got_worktree_status_cb status_cb, void *status_arg,
3495 struct got_repository *);
3497 static const struct got_error *
3498 write_subtree(struct got_object_id **new_subtree_id,
3499 struct got_tree_entry *te, const char *parent_path,
3500 struct got_pathlist_head *commitable_paths,
3501 got_worktree_status_cb status_cb, void *status_arg,
3502 struct got_repository *repo)
3504 const struct got_error *err = NULL;
3505 struct got_tree_object *subtree;
3506 char *subpath;
3508 if (asprintf(&subpath, "%s%s%s", parent_path,
3509 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3510 return got_error_from_errno("asprintf");
3512 err = got_object_open_as_tree(&subtree, repo, te->id);
3513 if (err)
3514 return err;
3516 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3517 status_cb, status_arg, repo);
3518 got_object_tree_close(subtree);
3519 free(subpath);
3520 return err;
3523 static const struct got_error *
3524 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3526 const struct got_error *err = NULL;
3527 char *ct_parent_path = NULL;
3529 *match = 0;
3531 if (strchr(ct->in_repo_path, '/') == NULL) {
3532 *match = got_path_is_root_dir(path);
3533 return NULL;
3536 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3537 if (err)
3538 return err;
3539 *match = (strcmp(path, ct_parent_path) == 0);
3540 free(ct_parent_path);
3541 return err;
3544 static mode_t
3545 get_ct_file_mode(struct got_commitable *ct)
3547 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3550 static const struct got_error *
3551 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3552 struct got_tree_entry *te, struct got_commitable *ct)
3554 const struct got_error *err = NULL;
3556 *new_te = NULL;
3558 err = got_object_tree_entry_dup(new_te, te);
3559 if (err)
3560 goto done;
3562 (*new_te)->mode = get_ct_file_mode(ct);
3564 free((*new_te)->id);
3565 if (ct->staged_status == GOT_STATUS_MODIFY)
3566 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3567 else
3568 (*new_te)->id = got_object_id_dup(ct->blob_id);
3569 if ((*new_te)->id == NULL) {
3570 err = got_error_from_errno("got_object_id_dup");
3571 goto done;
3573 done:
3574 if (err && *new_te) {
3575 got_object_tree_entry_close(*new_te);
3576 *new_te = NULL;
3578 return err;
3581 static const struct got_error *
3582 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3583 struct got_commitable *ct)
3585 const struct got_error *err = NULL;
3586 char *ct_name;
3588 *new_te = NULL;
3590 *new_te = calloc(1, sizeof(**new_te));
3591 if (*new_te == NULL)
3592 return got_error_from_errno("calloc");
3594 ct_name = basename(ct->path);
3595 if (ct_name == NULL) {
3596 err = got_error_from_errno2("basename", ct->path);
3597 goto done;
3599 (*new_te)->name = strdup(ct_name);
3600 if ((*new_te)->name == NULL) {
3601 err = got_error_from_errno("strdup");
3602 goto done;
3605 (*new_te)->mode = get_ct_file_mode(ct);
3607 if (ct->staged_status == GOT_STATUS_ADD)
3608 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3609 else
3610 (*new_te)->id = got_object_id_dup(ct->blob_id);
3611 if ((*new_te)->id == NULL) {
3612 err = got_error_from_errno("got_object_id_dup");
3613 goto done;
3615 done:
3616 if (err && *new_te) {
3617 got_object_tree_entry_close(*new_te);
3618 *new_te = NULL;
3620 return err;
3623 static const struct got_error *
3624 insert_tree_entry(struct got_tree_entry *new_te,
3625 struct got_pathlist_head *paths)
3627 const struct got_error *err = NULL;
3628 struct got_pathlist_entry *new_pe;
3630 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3631 if (err)
3632 return err;
3633 if (new_pe == NULL)
3634 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3635 return NULL;
3638 static const struct got_error *
3639 report_ct_status(struct got_commitable *ct,
3640 got_worktree_status_cb status_cb, void *status_arg)
3642 const char *ct_path = ct->path;
3643 unsigned char status;
3645 while (ct_path[0] == '/')
3646 ct_path++;
3648 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3649 status = ct->staged_status;
3650 else
3651 status = ct->status;
3653 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3654 ct_path, ct->blob_id, NULL, NULL);
3657 static const struct got_error *
3658 match_modified_subtree(int *modified, struct got_tree_entry *te,
3659 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3661 const struct got_error *err = NULL;
3662 struct got_pathlist_entry *pe;
3663 char *te_path;
3665 *modified = 0;
3667 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3668 got_path_is_root_dir(base_tree_path) ? "" : "/",
3669 te->name) == -1)
3670 return got_error_from_errno("asprintf");
3672 TAILQ_FOREACH(pe, commitable_paths, entry) {
3673 struct got_commitable *ct = pe->data;
3674 *modified = got_path_is_child(ct->in_repo_path, te_path,
3675 strlen(te_path));
3676 if (*modified)
3677 break;
3680 free(te_path);
3681 return err;
3684 static const struct got_error *
3685 match_deleted_or_modified_ct(struct got_commitable **ctp,
3686 struct got_tree_entry *te, const char *base_tree_path,
3687 struct got_pathlist_head *commitable_paths)
3689 const struct got_error *err = NULL;
3690 struct got_pathlist_entry *pe;
3692 *ctp = NULL;
3694 TAILQ_FOREACH(pe, commitable_paths, entry) {
3695 struct got_commitable *ct = pe->data;
3696 char *ct_name = NULL;
3697 int path_matches;
3699 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3700 if (ct->status != GOT_STATUS_MODIFY &&
3701 ct->status != GOT_STATUS_DELETE)
3702 continue;
3703 } else {
3704 if (ct->staged_status != GOT_STATUS_MODIFY &&
3705 ct->staged_status != GOT_STATUS_DELETE)
3706 continue;
3709 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3710 continue;
3712 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3713 if (err)
3714 return err;
3715 if (!path_matches)
3716 continue;
3718 ct_name = basename(pe->path);
3719 if (ct_name == NULL)
3720 return got_error_from_errno2("basename", pe->path);
3722 if (strcmp(te->name, ct_name) != 0)
3723 continue;
3725 *ctp = ct;
3726 break;
3729 return err;
3732 static const struct got_error *
3733 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3734 const char *child_path, const char *path_base_tree,
3735 struct got_pathlist_head *commitable_paths,
3736 got_worktree_status_cb status_cb, void *status_arg,
3737 struct got_repository *repo)
3739 const struct got_error *err = NULL;
3740 struct got_tree_entry *new_te;
3741 char *subtree_path;
3743 *new_tep = NULL;
3745 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3746 got_path_is_root_dir(path_base_tree) ? "" : "/",
3747 child_path) == -1)
3748 return got_error_from_errno("asprintf");
3750 new_te = calloc(1, sizeof(*new_te));
3751 if (new_te == NULL)
3752 return got_error_from_errno("calloc");
3753 new_te->mode = S_IFDIR;
3754 new_te->name = strdup(child_path);
3755 if (new_te->name == NULL) {
3756 err = got_error_from_errno("strdup");
3757 got_object_tree_entry_close(new_te);
3758 goto done;
3760 err = write_tree(&new_te->id, NULL, subtree_path,
3761 commitable_paths, status_cb, status_arg, repo);
3762 if (err) {
3763 got_object_tree_entry_close(new_te);
3764 goto done;
3766 done:
3767 free(subtree_path);
3768 if (err == NULL)
3769 *new_tep = new_te;
3770 return err;
3773 static const struct got_error *
3774 write_tree(struct got_object_id **new_tree_id,
3775 struct got_tree_object *base_tree, const char *path_base_tree,
3776 struct got_pathlist_head *commitable_paths,
3777 got_worktree_status_cb status_cb, void *status_arg,
3778 struct got_repository *repo)
3780 const struct got_error *err = NULL;
3781 const struct got_tree_entries *base_entries = NULL;
3782 struct got_pathlist_head paths;
3783 struct got_tree_entries new_tree_entries;
3784 struct got_tree_entry *te, *new_te = NULL;
3785 struct got_pathlist_entry *pe;
3787 TAILQ_INIT(&paths);
3788 new_tree_entries.nentries = 0;
3789 SIMPLEQ_INIT(&new_tree_entries.head);
3791 /* Insert, and recurse into, newly added entries first. */
3792 TAILQ_FOREACH(pe, commitable_paths, entry) {
3793 struct got_commitable *ct = pe->data;
3794 char *child_path = NULL, *slash;
3796 if ((ct->status != GOT_STATUS_ADD &&
3797 ct->staged_status != GOT_STATUS_ADD) ||
3798 (ct->flags & GOT_COMMITABLE_ADDED))
3799 continue;
3801 if (!got_path_is_child(pe->path, path_base_tree,
3802 strlen(path_base_tree)))
3803 continue;
3805 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3806 pe->path);
3807 if (err)
3808 goto done;
3810 slash = strchr(child_path, '/');
3811 if (slash == NULL) {
3812 err = alloc_added_blob_tree_entry(&new_te, ct);
3813 if (err)
3814 goto done;
3815 err = report_ct_status(ct, status_cb, status_arg);
3816 if (err)
3817 goto done;
3818 ct->flags |= GOT_COMMITABLE_ADDED;
3819 err = insert_tree_entry(new_te, &paths);
3820 if (err)
3821 goto done;
3822 } else {
3823 *slash = '\0'; /* trim trailing path components */
3824 if (base_tree == NULL ||
3825 got_object_tree_find_entry(base_tree, child_path)
3826 == NULL) {
3827 err = make_subtree_for_added_blob(&new_te,
3828 child_path, path_base_tree,
3829 commitable_paths, status_cb, status_arg,
3830 repo);
3831 if (err)
3832 goto done;
3833 err = insert_tree_entry(new_te, &paths);
3834 if (err)
3835 goto done;
3840 if (base_tree) {
3841 /* Handle modified and deleted entries. */
3842 base_entries = got_object_tree_get_entries(base_tree);
3843 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3844 struct got_commitable *ct = NULL;
3846 if (got_object_tree_entry_is_submodule(te)) {
3847 /* Entry is a submodule; just copy it. */
3848 err = got_object_tree_entry_dup(&new_te, te);
3849 if (err)
3850 goto done;
3851 err = insert_tree_entry(new_te, &paths);
3852 if (err)
3853 goto done;
3854 continue;
3857 if (S_ISDIR(te->mode)) {
3858 int modified;
3859 err = got_object_tree_entry_dup(&new_te, te);
3860 if (err)
3861 goto done;
3862 err = match_modified_subtree(&modified, te,
3863 path_base_tree, commitable_paths);
3864 if (err)
3865 goto done;
3866 /* Avoid recursion into unmodified subtrees. */
3867 if (modified) {
3868 free(new_te->id);
3869 err = write_subtree(&new_te->id, te,
3870 path_base_tree, commitable_paths,
3871 status_cb, status_arg, repo);
3872 if (err)
3873 goto done;
3875 err = insert_tree_entry(new_te, &paths);
3876 if (err)
3877 goto done;
3878 continue;
3881 err = match_deleted_or_modified_ct(&ct, te,
3882 path_base_tree, commitable_paths);
3883 if (err)
3884 goto done;
3885 if (ct) {
3886 /* NB: Deleted entries get dropped here. */
3887 if (ct->status == GOT_STATUS_MODIFY ||
3888 ct->staged_status == GOT_STATUS_MODIFY) {
3889 err = alloc_modified_blob_tree_entry(
3890 &new_te, te, ct);
3891 if (err)
3892 goto done;
3893 err = insert_tree_entry(new_te, &paths);
3894 if (err)
3895 goto done;
3897 err = report_ct_status(ct, status_cb,
3898 status_arg);
3899 if (err)
3900 goto done;
3901 } else {
3902 /* Entry is unchanged; just copy it. */
3903 err = got_object_tree_entry_dup(&new_te, te);
3904 if (err)
3905 goto done;
3906 err = insert_tree_entry(new_te, &paths);
3907 if (err)
3908 goto done;
3913 /* Write new list of entries; deleted entries have been dropped. */
3914 TAILQ_FOREACH(pe, &paths, entry) {
3915 struct got_tree_entry *te = pe->data;
3916 new_tree_entries.nentries++;
3917 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3919 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3920 done:
3921 got_object_tree_entries_close(&new_tree_entries);
3922 got_pathlist_free(&paths);
3923 return err;
3926 static const struct got_error *
3927 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3928 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3929 int have_staged_files)
3931 const struct got_error *err = NULL;
3932 struct got_pathlist_entry *pe;
3934 TAILQ_FOREACH(pe, commitable_paths, entry) {
3935 struct got_fileindex_entry *ie;
3936 struct got_commitable *ct = pe->data;
3938 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3939 if (ie) {
3940 if (ct->status == GOT_STATUS_DELETE ||
3941 ct->staged_status == GOT_STATUS_DELETE) {
3942 got_fileindex_entry_remove(fileindex, ie);
3943 got_fileindex_entry_free(ie);
3944 } else if (ct->staged_status == GOT_STATUS_ADD ||
3945 ct->staged_status == GOT_STATUS_MODIFY) {
3946 got_fileindex_entry_stage_set(ie,
3947 GOT_FILEIDX_STAGE_NONE);
3948 err = got_fileindex_entry_update(ie,
3949 ct->ondisk_path, ct->staged_blob_id->sha1,
3950 new_base_commit_id->sha1,
3951 !have_staged_files);
3952 } else
3953 err = got_fileindex_entry_update(ie,
3954 ct->ondisk_path, ct->blob_id->sha1,
3955 new_base_commit_id->sha1,
3956 !have_staged_files);
3957 } else {
3958 err = got_fileindex_entry_alloc(&ie,
3959 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3960 new_base_commit_id->sha1);
3961 if (err)
3962 break;
3963 err = got_fileindex_entry_add(fileindex, ie);
3964 if (err)
3965 break;
3968 return err;
3972 static const struct got_error *
3973 check_out_of_date(const char *in_repo_path, unsigned char status,
3974 unsigned char staged_status, struct got_object_id *base_blob_id,
3975 struct got_object_id *base_commit_id,
3976 struct got_object_id *head_commit_id, struct got_repository *repo,
3977 int ood_errcode)
3979 const struct got_error *err = NULL;
3980 struct got_object_id *id = NULL;
3982 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3983 /* Trivial case: base commit == head commit */
3984 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3985 return NULL;
3987 * Ensure file content which local changes were based
3988 * on matches file content in the branch head.
3990 err = got_object_id_by_path(&id, repo, head_commit_id,
3991 in_repo_path);
3992 if (err) {
3993 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3994 err = got_error(ood_errcode);
3995 goto done;
3996 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3997 err = got_error(ood_errcode);
3998 } else {
3999 /* Require that added files don't exist in the branch head. */
4000 err = got_object_id_by_path(&id, repo, head_commit_id,
4001 in_repo_path);
4002 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4003 goto done;
4004 err = id ? got_error(ood_errcode) : NULL;
4006 done:
4007 free(id);
4008 return err;
4011 const struct got_error *
4012 commit_worktree(struct got_object_id **new_commit_id,
4013 struct got_pathlist_head *commitable_paths,
4014 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4015 const char *author, const char *committer,
4016 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4017 got_worktree_status_cb status_cb, void *status_arg,
4018 struct got_repository *repo)
4020 const struct got_error *err = NULL, *unlockerr = NULL;
4021 struct got_pathlist_entry *pe;
4022 const char *head_ref_name = NULL;
4023 struct got_commit_object *head_commit = NULL;
4024 struct got_reference *head_ref2 = NULL;
4025 struct got_object_id *head_commit_id2 = NULL;
4026 struct got_tree_object *head_tree = NULL;
4027 struct got_object_id *new_tree_id = NULL;
4028 struct got_object_id_queue parent_ids;
4029 struct got_object_qid *pid = NULL;
4030 char *logmsg = NULL;
4032 *new_commit_id = NULL;
4034 SIMPLEQ_INIT(&parent_ids);
4036 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4037 if (err)
4038 goto done;
4040 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4041 if (err)
4042 goto done;
4044 if (commit_msg_cb != NULL) {
4045 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4046 if (err)
4047 goto done;
4050 if (logmsg == NULL || strlen(logmsg) == 0) {
4051 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4052 goto done;
4055 /* Create blobs from added and modified files and record their IDs. */
4056 TAILQ_FOREACH(pe, commitable_paths, entry) {
4057 struct got_commitable *ct = pe->data;
4058 char *ondisk_path;
4060 /* Blobs for staged files already exist. */
4061 if (ct->staged_status == GOT_STATUS_ADD ||
4062 ct->staged_status == GOT_STATUS_MODIFY)
4063 continue;
4065 if (ct->status != GOT_STATUS_ADD &&
4066 ct->status != GOT_STATUS_MODIFY)
4067 continue;
4069 if (asprintf(&ondisk_path, "%s/%s",
4070 worktree->root_path, pe->path) == -1) {
4071 err = got_error_from_errno("asprintf");
4072 goto done;
4074 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4075 free(ondisk_path);
4076 if (err)
4077 goto done;
4080 /* Recursively write new tree objects. */
4081 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4082 status_cb, status_arg, repo);
4083 if (err)
4084 goto done;
4086 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4087 if (err)
4088 goto done;
4089 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4090 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4091 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4092 got_object_qid_free(pid);
4093 if (logmsg != NULL)
4094 free(logmsg);
4095 if (err)
4096 goto done;
4098 /* Check if a concurrent commit to our branch has occurred. */
4099 head_ref_name = got_worktree_get_head_ref_name(worktree);
4100 if (head_ref_name == NULL) {
4101 err = got_error_from_errno("got_worktree_get_head_ref_name");
4102 goto done;
4104 /* Lock the reference here to prevent concurrent modification. */
4105 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4106 if (err)
4107 goto done;
4108 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4109 if (err)
4110 goto done;
4111 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4112 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4113 goto done;
4115 /* Update branch head in repository. */
4116 err = got_ref_change_ref(head_ref2, *new_commit_id);
4117 if (err)
4118 goto done;
4119 err = got_ref_write(head_ref2, repo);
4120 if (err)
4121 goto done;
4123 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4124 if (err)
4125 goto done;
4127 err = ref_base_commit(worktree, repo);
4128 if (err)
4129 goto done;
4130 done:
4131 if (head_tree)
4132 got_object_tree_close(head_tree);
4133 if (head_commit)
4134 got_object_commit_close(head_commit);
4135 free(head_commit_id2);
4136 if (head_ref2) {
4137 unlockerr = got_ref_unlock(head_ref2);
4138 if (unlockerr && err == NULL)
4139 err = unlockerr;
4140 got_ref_close(head_ref2);
4142 return err;
4145 static const struct got_error *
4146 check_path_is_commitable(const char *path,
4147 struct got_pathlist_head *commitable_paths)
4149 struct got_pathlist_entry *cpe = NULL;
4150 size_t path_len = strlen(path);
4152 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4153 struct got_commitable *ct = cpe->data;
4154 const char *ct_path = ct->path;
4156 while (ct_path[0] == '/')
4157 ct_path++;
4159 if (strcmp(path, ct_path) == 0 ||
4160 got_path_is_child(ct_path, path, path_len))
4161 break;
4164 if (cpe == NULL)
4165 return got_error_path(path, GOT_ERR_BAD_PATH);
4167 return NULL;
4170 static const struct got_error *
4171 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4173 int *have_staged_files = arg;
4175 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4176 *have_staged_files = 1;
4177 return got_error(GOT_ERR_CANCELLED);
4180 return NULL;
4183 static const struct got_error *
4184 check_non_staged_files(struct got_fileindex *fileindex,
4185 struct got_pathlist_head *paths)
4187 struct got_pathlist_entry *pe;
4188 struct got_fileindex_entry *ie;
4190 TAILQ_FOREACH(pe, paths, entry) {
4191 if (pe->path[0] == '\0')
4192 continue;
4193 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4194 if (ie == NULL)
4195 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4196 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4197 return got_error_path(pe->path,
4198 GOT_ERR_FILE_NOT_STAGED);
4201 return NULL;
4204 const struct got_error *
4205 got_worktree_commit(struct got_object_id **new_commit_id,
4206 struct got_worktree *worktree, struct got_pathlist_head *paths,
4207 const char *author, const char *committer,
4208 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4209 got_worktree_status_cb status_cb, void *status_arg,
4210 struct got_repository *repo)
4212 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4213 struct got_fileindex *fileindex = NULL;
4214 char *fileindex_path = NULL;
4215 struct got_pathlist_head commitable_paths;
4216 struct collect_commitables_arg cc_arg;
4217 struct got_pathlist_entry *pe;
4218 struct got_reference *head_ref = NULL;
4219 struct got_object_id *head_commit_id = NULL;
4220 int have_staged_files = 0;
4222 *new_commit_id = NULL;
4224 TAILQ_INIT(&commitable_paths);
4226 err = lock_worktree(worktree, LOCK_EX);
4227 if (err)
4228 goto done;
4230 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4231 if (err)
4232 goto done;
4234 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4235 if (err)
4236 goto done;
4238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4239 if (err)
4240 goto done;
4242 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4243 &have_staged_files);
4244 if (err && err->code != GOT_ERR_CANCELLED)
4245 goto done;
4246 if (have_staged_files) {
4247 err = check_non_staged_files(fileindex, paths);
4248 if (err)
4249 goto done;
4252 cc_arg.commitable_paths = &commitable_paths;
4253 cc_arg.worktree = worktree;
4254 cc_arg.repo = repo;
4255 cc_arg.have_staged_files = have_staged_files;
4256 TAILQ_FOREACH(pe, paths, entry) {
4257 err = worktree_status(worktree, pe->path, fileindex, repo,
4258 collect_commitables, &cc_arg, NULL, NULL);
4259 if (err)
4260 goto done;
4263 if (TAILQ_EMPTY(&commitable_paths)) {
4264 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4265 goto done;
4268 TAILQ_FOREACH(pe, paths, entry) {
4269 err = check_path_is_commitable(pe->path, &commitable_paths);
4270 if (err)
4271 goto done;
4274 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4275 struct got_commitable *ct = pe->data;
4276 const char *ct_path = ct->in_repo_path;
4278 while (ct_path[0] == '/')
4279 ct_path++;
4280 err = check_out_of_date(ct_path, ct->status,
4281 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4282 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4283 if (err)
4284 goto done;
4288 err = commit_worktree(new_commit_id, &commitable_paths,
4289 head_commit_id, worktree, author, committer,
4290 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4291 if (err)
4292 goto done;
4294 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4295 fileindex, have_staged_files);
4296 sync_err = sync_fileindex(fileindex, fileindex_path);
4297 if (sync_err && err == NULL)
4298 err = sync_err;
4299 done:
4300 if (fileindex)
4301 got_fileindex_free(fileindex);
4302 free(fileindex_path);
4303 unlockerr = lock_worktree(worktree, LOCK_SH);
4304 if (unlockerr && err == NULL)
4305 err = unlockerr;
4306 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4307 struct got_commitable *ct = pe->data;
4308 free_commitable(ct);
4310 got_pathlist_free(&commitable_paths);
4311 return err;
4314 const char *
4315 got_commitable_get_path(struct got_commitable *ct)
4317 return ct->path;
4320 unsigned int
4321 got_commitable_get_status(struct got_commitable *ct)
4323 return ct->status;
4326 struct check_rebase_ok_arg {
4327 struct got_worktree *worktree;
4328 struct got_repository *repo;
4331 static const struct got_error *
4332 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4334 const struct got_error *err = NULL;
4335 struct check_rebase_ok_arg *a = arg;
4336 unsigned char status;
4337 struct stat sb;
4338 char *ondisk_path;
4340 /* Reject rebase of a work tree with mixed base commits. */
4341 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4342 SHA1_DIGEST_LENGTH))
4343 return got_error(GOT_ERR_MIXED_COMMITS);
4345 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4346 == -1)
4347 return got_error_from_errno("asprintf");
4349 /* Reject rebase of a work tree with modified or staged files. */
4350 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4351 free(ondisk_path);
4352 if (err)
4353 return err;
4355 if (status != GOT_STATUS_NO_CHANGE)
4356 return got_error(GOT_ERR_MODIFIED);
4357 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4358 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4360 return NULL;
4363 const struct got_error *
4364 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4365 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4366 struct got_worktree *worktree, struct got_reference *branch,
4367 struct got_repository *repo)
4369 const struct got_error *err = NULL;
4370 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4371 char *branch_ref_name = NULL;
4372 char *fileindex_path = NULL;
4373 struct check_rebase_ok_arg ok_arg;
4374 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4376 *new_base_branch_ref = NULL;
4377 *tmp_branch = NULL;
4378 *fileindex = NULL;
4380 err = lock_worktree(worktree, LOCK_EX);
4381 if (err)
4382 return err;
4384 err = open_fileindex(fileindex, &fileindex_path, worktree);
4385 if (err)
4386 goto done;
4388 ok_arg.worktree = worktree;
4389 ok_arg.repo = repo;
4390 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4391 &ok_arg);
4392 if (err)
4393 goto done;
4395 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4396 if (err)
4397 goto done;
4399 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4400 if (err)
4401 goto done;
4403 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4404 if (err)
4405 goto done;
4407 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4408 0);
4409 if (err)
4410 goto done;
4412 err = got_ref_alloc_symref(new_base_branch_ref,
4413 new_base_branch_ref_name, wt_branch);
4414 if (err)
4415 goto done;
4416 err = got_ref_write(*new_base_branch_ref, repo);
4417 if (err)
4418 goto done;
4420 /* TODO Lock original branch's ref while rebasing? */
4422 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4423 if (err)
4424 goto done;
4426 err = got_ref_write(branch_ref, repo);
4427 if (err)
4428 goto done;
4430 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4431 worktree->base_commit_id);
4432 if (err)
4433 goto done;
4434 err = got_ref_write(*tmp_branch, repo);
4435 if (err)
4436 goto done;
4438 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4439 if (err)
4440 goto done;
4441 done:
4442 free(fileindex_path);
4443 free(tmp_branch_name);
4444 free(new_base_branch_ref_name);
4445 free(branch_ref_name);
4446 if (branch_ref)
4447 got_ref_close(branch_ref);
4448 if (wt_branch)
4449 got_ref_close(wt_branch);
4450 if (err) {
4451 if (*new_base_branch_ref) {
4452 got_ref_close(*new_base_branch_ref);
4453 *new_base_branch_ref = NULL;
4455 if (*tmp_branch) {
4456 got_ref_close(*tmp_branch);
4457 *tmp_branch = NULL;
4459 if (*fileindex) {
4460 got_fileindex_free(*fileindex);
4461 *fileindex = NULL;
4463 lock_worktree(worktree, LOCK_SH);
4465 return err;
4468 const struct got_error *
4469 got_worktree_rebase_continue(struct got_object_id **commit_id,
4470 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4471 struct got_reference **branch, struct got_fileindex **fileindex,
4472 struct got_worktree *worktree, struct got_repository *repo)
4474 const struct got_error *err;
4475 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4476 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4477 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4478 char *fileindex_path = NULL;
4479 int have_staged_files = 0;
4481 *commit_id = NULL;
4482 *new_base_branch = NULL;
4483 *tmp_branch = NULL;
4484 *branch = NULL;
4485 *fileindex = NULL;
4487 err = lock_worktree(worktree, LOCK_EX);
4488 if (err)
4489 return err;
4491 err = open_fileindex(fileindex, &fileindex_path, worktree);
4492 if (err)
4493 goto done;
4495 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4496 &have_staged_files);
4497 if (err && err->code != GOT_ERR_CANCELLED)
4498 goto done;
4499 if (have_staged_files) {
4500 err = got_error(GOT_ERR_STAGED_PATHS);
4501 goto done;
4504 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4505 if (err)
4506 goto done;
4508 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4509 if (err)
4510 goto done;
4512 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4513 if (err)
4514 goto done;
4516 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4517 if (err)
4518 goto done;
4520 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4521 if (err)
4522 goto done;
4524 err = got_ref_open(branch, repo,
4525 got_ref_get_symref_target(branch_ref), 0);
4526 if (err)
4527 goto done;
4529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4530 if (err)
4531 goto done;
4533 err = got_ref_resolve(commit_id, repo, commit_ref);
4534 if (err)
4535 goto done;
4537 err = got_ref_open(new_base_branch, repo,
4538 new_base_branch_ref_name, 0);
4539 if (err)
4540 goto done;
4542 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4543 if (err)
4544 goto done;
4545 done:
4546 free(commit_ref_name);
4547 free(branch_ref_name);
4548 free(fileindex_path);
4549 if (commit_ref)
4550 got_ref_close(commit_ref);
4551 if (branch_ref)
4552 got_ref_close(branch_ref);
4553 if (err) {
4554 free(*commit_id);
4555 *commit_id = NULL;
4556 if (*tmp_branch) {
4557 got_ref_close(*tmp_branch);
4558 *tmp_branch = NULL;
4560 if (*new_base_branch) {
4561 got_ref_close(*new_base_branch);
4562 *new_base_branch = NULL;
4564 if (*branch) {
4565 got_ref_close(*branch);
4566 *branch = NULL;
4568 if (*fileindex) {
4569 got_fileindex_free(*fileindex);
4570 *fileindex = NULL;
4572 lock_worktree(worktree, LOCK_SH);
4574 return err;
4577 const struct got_error *
4578 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4580 const struct got_error *err;
4581 char *tmp_branch_name = NULL;
4583 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4584 if (err)
4585 return err;
4587 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4588 free(tmp_branch_name);
4589 return NULL;
4592 static const struct got_error *
4593 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4594 char **logmsg, void *arg)
4596 *logmsg = arg;
4597 return NULL;
4600 static const struct got_error *
4601 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4602 const char *path, struct got_object_id *blob_id,
4603 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4605 return NULL;
4608 struct collect_merged_paths_arg {
4609 got_worktree_checkout_cb progress_cb;
4610 void *progress_arg;
4611 struct got_pathlist_head *merged_paths;
4614 static const struct got_error *
4615 collect_merged_paths(void *arg, unsigned char status, const char *path)
4617 const struct got_error *err;
4618 struct collect_merged_paths_arg *a = arg;
4619 char *p;
4620 struct got_pathlist_entry *new;
4622 err = (*a->progress_cb)(a->progress_arg, status, path);
4623 if (err)
4624 return err;
4626 if (status != GOT_STATUS_MERGE &&
4627 status != GOT_STATUS_ADD &&
4628 status != GOT_STATUS_DELETE &&
4629 status != GOT_STATUS_CONFLICT)
4630 return NULL;
4632 p = strdup(path);
4633 if (p == NULL)
4634 return got_error_from_errno("strdup");
4636 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4637 if (err || new == NULL)
4638 free(p);
4639 return err;
4642 void
4643 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4645 struct got_pathlist_entry *pe;
4647 TAILQ_FOREACH(pe, merged_paths, entry)
4648 free((char *)pe->path);
4650 got_pathlist_free(merged_paths);
4653 static const struct got_error *
4654 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4655 struct got_repository *repo)
4657 const struct got_error *err;
4658 struct got_reference *commit_ref = NULL;
4660 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4661 if (err) {
4662 if (err->code != GOT_ERR_NOT_REF)
4663 goto done;
4664 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4665 if (err)
4666 goto done;
4667 err = got_ref_write(commit_ref, repo);
4668 if (err)
4669 goto done;
4670 } else {
4671 struct got_object_id *stored_id;
4672 int cmp;
4674 err = got_ref_resolve(&stored_id, repo, commit_ref);
4675 if (err)
4676 goto done;
4677 cmp = got_object_id_cmp(commit_id, stored_id);
4678 free(stored_id);
4679 if (cmp != 0) {
4680 err = got_error(GOT_ERR_REBASE_COMMITID);
4681 goto done;
4684 done:
4685 if (commit_ref)
4686 got_ref_close(commit_ref);
4687 return err;
4690 static const struct got_error *
4691 rebase_merge_files(struct got_pathlist_head *merged_paths,
4692 const char *commit_ref_name, struct got_worktree *worktree,
4693 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4694 struct got_object_id *commit_id, struct got_repository *repo,
4695 got_worktree_checkout_cb progress_cb, void *progress_arg,
4696 got_cancel_cb cancel_cb, void *cancel_arg)
4698 const struct got_error *err;
4699 struct got_reference *commit_ref = NULL;
4700 struct collect_merged_paths_arg cmp_arg;
4701 char *fileindex_path;
4703 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4705 err = get_fileindex_path(&fileindex_path, worktree);
4706 if (err)
4707 return err;
4709 cmp_arg.progress_cb = progress_cb;
4710 cmp_arg.progress_arg = progress_arg;
4711 cmp_arg.merged_paths = merged_paths;
4712 err = merge_files(worktree, fileindex, fileindex_path,
4713 parent_commit_id, commit_id, repo, collect_merged_paths,
4714 &cmp_arg, cancel_cb, cancel_arg);
4715 if (commit_ref)
4716 got_ref_close(commit_ref);
4717 return err;
4720 const struct got_error *
4721 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4722 struct got_worktree *worktree, struct got_fileindex *fileindex,
4723 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4724 struct got_repository *repo,
4725 got_worktree_checkout_cb progress_cb, void *progress_arg,
4726 got_cancel_cb cancel_cb, void *cancel_arg)
4728 const struct got_error *err;
4729 char *commit_ref_name;
4731 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4732 if (err)
4733 return err;
4735 err = store_commit_id(commit_ref_name, commit_id, repo);
4736 if (err)
4737 goto done;
4739 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4740 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4741 progress_arg, cancel_cb, cancel_arg);
4742 done:
4743 free(commit_ref_name);
4744 return err;
4747 const struct got_error *
4748 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4749 struct got_worktree *worktree, struct got_fileindex *fileindex,
4750 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4751 struct got_repository *repo,
4752 got_worktree_checkout_cb progress_cb, void *progress_arg,
4753 got_cancel_cb cancel_cb, void *cancel_arg)
4755 const struct got_error *err;
4756 char *commit_ref_name;
4758 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4759 if (err)
4760 return err;
4762 err = store_commit_id(commit_ref_name, commit_id, repo);
4763 if (err)
4764 goto done;
4766 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4767 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4768 progress_arg, cancel_cb, cancel_arg);
4769 done:
4770 free(commit_ref_name);
4771 return err;
4774 static const struct got_error *
4775 rebase_commit(struct got_object_id **new_commit_id,
4776 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4777 struct got_worktree *worktree, struct got_fileindex *fileindex,
4778 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4779 const char *new_logmsg, struct got_repository *repo)
4781 const struct got_error *err, *sync_err;
4782 struct got_pathlist_head commitable_paths;
4783 struct collect_commitables_arg cc_arg;
4784 char *fileindex_path = NULL;
4785 struct got_reference *head_ref = NULL;
4786 struct got_object_id *head_commit_id = NULL;
4787 char *logmsg = NULL;
4789 TAILQ_INIT(&commitable_paths);
4790 *new_commit_id = NULL;
4792 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4794 err = get_fileindex_path(&fileindex_path, worktree);
4795 if (err)
4796 return err;
4798 cc_arg.commitable_paths = &commitable_paths;
4799 cc_arg.worktree = worktree;
4800 cc_arg.repo = repo;
4801 cc_arg.have_staged_files = 0;
4803 * If possible get the status of individual files directly to
4804 * avoid crawling the entire work tree once per rebased commit.
4805 * TODO: Ideally, merged_paths would contain a list of commitables
4806 * we could use so we could skip worktree_status() entirely.
4808 if (merged_paths) {
4809 struct got_pathlist_entry *pe;
4810 if (TAILQ_EMPTY(merged_paths)) {
4811 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4812 goto done;
4814 TAILQ_FOREACH(pe, merged_paths, entry) {
4815 err = worktree_status(worktree, pe->path, fileindex,
4816 repo, collect_commitables, &cc_arg, NULL, NULL);
4817 if (err)
4818 goto done;
4820 } else {
4821 err = worktree_status(worktree, "", fileindex, repo,
4822 collect_commitables, &cc_arg, NULL, NULL);
4823 if (err)
4824 goto done;
4827 if (TAILQ_EMPTY(&commitable_paths)) {
4828 /* No-op change; commit will be elided. */
4829 err = got_ref_delete(commit_ref, repo);
4830 if (err)
4831 goto done;
4832 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4833 goto done;
4836 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4837 if (err)
4838 goto done;
4840 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4841 if (err)
4842 goto done;
4844 if (new_logmsg) {
4845 logmsg = strdup(new_logmsg);
4846 if (logmsg == NULL) {
4847 err = got_error_from_errno("strdup");
4848 goto done;
4850 } else {
4851 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4852 if (err)
4853 goto done;
4856 /* NB: commit_worktree will call free(logmsg) */
4857 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4858 worktree, got_object_commit_get_author(orig_commit),
4859 got_object_commit_get_committer(orig_commit),
4860 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4861 if (err)
4862 goto done;
4864 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4865 if (err)
4866 goto done;
4868 err = got_ref_delete(commit_ref, repo);
4869 if (err)
4870 goto done;
4872 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4873 fileindex, 0);
4874 sync_err = sync_fileindex(fileindex, fileindex_path);
4875 if (sync_err && err == NULL)
4876 err = sync_err;
4877 done:
4878 free(fileindex_path);
4879 free(head_commit_id);
4880 if (head_ref)
4881 got_ref_close(head_ref);
4882 if (err) {
4883 free(*new_commit_id);
4884 *new_commit_id = NULL;
4886 return err;
4889 const struct got_error *
4890 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4891 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4892 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4893 struct got_commit_object *orig_commit,
4894 struct got_object_id *orig_commit_id, struct got_repository *repo)
4896 const struct got_error *err;
4897 char *commit_ref_name;
4898 struct got_reference *commit_ref = NULL;
4899 struct got_object_id *commit_id = NULL;
4901 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4902 if (err)
4903 return err;
4905 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4906 if (err)
4907 goto done;
4908 err = got_ref_resolve(&commit_id, repo, commit_ref);
4909 if (err)
4910 goto done;
4911 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4912 err = got_error(GOT_ERR_REBASE_COMMITID);
4913 goto done;
4916 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4917 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4918 done:
4919 if (commit_ref)
4920 got_ref_close(commit_ref);
4921 free(commit_ref_name);
4922 free(commit_id);
4923 return err;
4926 const struct got_error *
4927 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4928 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4929 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4930 struct got_commit_object *orig_commit,
4931 struct got_object_id *orig_commit_id, const char *new_logmsg,
4932 struct got_repository *repo)
4934 const struct got_error *err;
4935 char *commit_ref_name;
4936 struct got_reference *commit_ref = NULL;
4937 struct got_object_id *commit_id = NULL;
4939 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4940 if (err)
4941 return err;
4943 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4944 if (err)
4945 goto done;
4946 err = got_ref_resolve(&commit_id, repo, commit_ref);
4947 if (err)
4948 goto done;
4949 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4950 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4951 goto done;
4954 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4955 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4956 done:
4957 if (commit_ref)
4958 got_ref_close(commit_ref);
4959 free(commit_ref_name);
4960 free(commit_id);
4961 return err;
4964 const struct got_error *
4965 got_worktree_rebase_postpone(struct got_worktree *worktree,
4966 struct got_fileindex *fileindex)
4968 if (fileindex)
4969 got_fileindex_free(fileindex);
4970 return lock_worktree(worktree, LOCK_SH);
4973 static const struct got_error *
4974 delete_ref(const char *name, struct got_repository *repo)
4976 const struct got_error *err;
4977 struct got_reference *ref;
4979 err = got_ref_open(&ref, repo, name, 0);
4980 if (err) {
4981 if (err->code == GOT_ERR_NOT_REF)
4982 return NULL;
4983 return err;
4986 err = got_ref_delete(ref, repo);
4987 got_ref_close(ref);
4988 return err;
4991 static const struct got_error *
4992 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4994 const struct got_error *err;
4995 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4996 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4998 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4999 if (err)
5000 goto done;
5001 err = delete_ref(tmp_branch_name, repo);
5002 if (err)
5003 goto done;
5005 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5006 if (err)
5007 goto done;
5008 err = delete_ref(new_base_branch_ref_name, repo);
5009 if (err)
5010 goto done;
5012 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5013 if (err)
5014 goto done;
5015 err = delete_ref(branch_ref_name, repo);
5016 if (err)
5017 goto done;
5019 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5020 if (err)
5021 goto done;
5022 err = delete_ref(commit_ref_name, repo);
5023 if (err)
5024 goto done;
5026 done:
5027 free(tmp_branch_name);
5028 free(new_base_branch_ref_name);
5029 free(branch_ref_name);
5030 free(commit_ref_name);
5031 return err;
5034 const struct got_error *
5035 got_worktree_rebase_complete(struct got_worktree *worktree,
5036 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5037 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5038 struct got_repository *repo)
5040 const struct got_error *err, *unlockerr;
5041 struct got_object_id *new_head_commit_id = NULL;
5043 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5044 if (err)
5045 return err;
5047 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5048 if (err)
5049 goto done;
5051 err = got_ref_write(rebased_branch, repo);
5052 if (err)
5053 goto done;
5055 err = got_worktree_set_head_ref(worktree, rebased_branch);
5056 if (err)
5057 goto done;
5059 err = delete_rebase_refs(worktree, repo);
5060 done:
5061 if (fileindex)
5062 got_fileindex_free(fileindex);
5063 free(new_head_commit_id);
5064 unlockerr = lock_worktree(worktree, LOCK_SH);
5065 if (unlockerr && err == NULL)
5066 err = unlockerr;
5067 return err;
5070 const struct got_error *
5071 got_worktree_rebase_abort(struct got_worktree *worktree,
5072 struct got_fileindex *fileindex, struct got_repository *repo,
5073 struct got_reference *new_base_branch,
5074 got_worktree_checkout_cb progress_cb, void *progress_arg)
5076 const struct got_error *err, *unlockerr, *sync_err;
5077 struct got_reference *resolved = NULL;
5078 struct got_object_id *commit_id = NULL;
5079 char *fileindex_path = NULL;
5080 struct revert_file_args rfa;
5081 struct got_object_id *tree_id = NULL;
5083 err = lock_worktree(worktree, LOCK_EX);
5084 if (err)
5085 return err;
5087 err = got_ref_open(&resolved, repo,
5088 got_ref_get_symref_target(new_base_branch), 0);
5089 if (err)
5090 goto done;
5092 err = got_worktree_set_head_ref(worktree, resolved);
5093 if (err)
5094 goto done;
5097 * XXX commits to the base branch could have happened while
5098 * we were busy rebasing; should we store the original commit ID
5099 * when rebase begins and read it back here?
5101 err = got_ref_resolve(&commit_id, repo, resolved);
5102 if (err)
5103 goto done;
5105 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5106 if (err)
5107 goto done;
5109 err = got_object_id_by_path(&tree_id, repo,
5110 worktree->base_commit_id, worktree->path_prefix);
5111 if (err)
5112 goto done;
5114 err = delete_rebase_refs(worktree, repo);
5115 if (err)
5116 goto done;
5118 err = get_fileindex_path(&fileindex_path, worktree);
5119 if (err)
5120 goto done;
5122 rfa.worktree = worktree;
5123 rfa.fileindex = fileindex;
5124 rfa.progress_cb = progress_cb;
5125 rfa.progress_arg = progress_arg;
5126 rfa.patch_cb = NULL;
5127 rfa.patch_arg = NULL;
5128 rfa.repo = repo;
5129 err = worktree_status(worktree, "", fileindex, repo,
5130 revert_file, &rfa, NULL, NULL);
5131 if (err)
5132 goto sync;
5134 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5135 repo, progress_cb, progress_arg, NULL, NULL);
5136 sync:
5137 sync_err = sync_fileindex(fileindex, fileindex_path);
5138 if (sync_err && err == NULL)
5139 err = sync_err;
5140 done:
5141 got_ref_close(resolved);
5142 free(tree_id);
5143 free(commit_id);
5144 if (fileindex)
5145 got_fileindex_free(fileindex);
5146 free(fileindex_path);
5148 unlockerr = lock_worktree(worktree, LOCK_SH);
5149 if (unlockerr && err == NULL)
5150 err = unlockerr;
5151 return err;
5154 const struct got_error *
5155 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5156 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5157 struct got_fileindex **fileindex, struct got_worktree *worktree,
5158 struct got_repository *repo)
5160 const struct got_error *err = NULL;
5161 char *tmp_branch_name = NULL;
5162 char *branch_ref_name = NULL;
5163 char *base_commit_ref_name = NULL;
5164 char *fileindex_path = NULL;
5165 struct check_rebase_ok_arg ok_arg;
5166 struct got_reference *wt_branch = NULL;
5167 struct got_reference *base_commit_ref = NULL;
5169 *tmp_branch = NULL;
5170 *branch_ref = NULL;
5171 *base_commit_id = NULL;
5172 *fileindex = NULL;
5174 err = lock_worktree(worktree, LOCK_EX);
5175 if (err)
5176 return err;
5178 err = open_fileindex(fileindex, &fileindex_path, worktree);
5179 if (err)
5180 goto done;
5182 ok_arg.worktree = worktree;
5183 ok_arg.repo = repo;
5184 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5185 &ok_arg);
5186 if (err)
5187 goto done;
5189 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5190 if (err)
5191 goto done;
5193 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5194 if (err)
5195 goto done;
5197 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5198 worktree);
5199 if (err)
5200 goto done;
5202 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5203 0);
5204 if (err)
5205 goto done;
5207 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5208 if (err)
5209 goto done;
5211 err = got_ref_write(*branch_ref, repo);
5212 if (err)
5213 goto done;
5215 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5216 worktree->base_commit_id);
5217 if (err)
5218 goto done;
5219 err = got_ref_write(base_commit_ref, repo);
5220 if (err)
5221 goto done;
5222 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5223 if (*base_commit_id == NULL) {
5224 err = got_error_from_errno("got_object_id_dup");
5225 goto done;
5228 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5229 worktree->base_commit_id);
5230 if (err)
5231 goto done;
5232 err = got_ref_write(*tmp_branch, repo);
5233 if (err)
5234 goto done;
5236 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5237 if (err)
5238 goto done;
5239 done:
5240 free(fileindex_path);
5241 free(tmp_branch_name);
5242 free(branch_ref_name);
5243 free(base_commit_ref_name);
5244 if (wt_branch)
5245 got_ref_close(wt_branch);
5246 if (err) {
5247 if (*branch_ref) {
5248 got_ref_close(*branch_ref);
5249 *branch_ref = NULL;
5251 if (*tmp_branch) {
5252 got_ref_close(*tmp_branch);
5253 *tmp_branch = NULL;
5255 free(*base_commit_id);
5256 if (*fileindex) {
5257 got_fileindex_free(*fileindex);
5258 *fileindex = NULL;
5260 lock_worktree(worktree, LOCK_SH);
5262 return err;
5265 const struct got_error *
5266 got_worktree_histedit_postpone(struct got_worktree *worktree,
5267 struct got_fileindex *fileindex)
5269 if (fileindex)
5270 got_fileindex_free(fileindex);
5271 return lock_worktree(worktree, LOCK_SH);
5274 const struct got_error *
5275 got_worktree_histedit_in_progress(int *in_progress,
5276 struct got_worktree *worktree)
5278 const struct got_error *err;
5279 char *tmp_branch_name = NULL;
5281 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5282 if (err)
5283 return err;
5285 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5286 free(tmp_branch_name);
5287 return NULL;
5290 const struct got_error *
5291 got_worktree_histedit_continue(struct got_object_id **commit_id,
5292 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5293 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5294 struct got_worktree *worktree, struct got_repository *repo)
5296 const struct got_error *err;
5297 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5298 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5299 struct got_reference *commit_ref = NULL;
5300 struct got_reference *base_commit_ref = NULL;
5301 char *fileindex_path = NULL;
5302 int have_staged_files = 0;
5304 *commit_id = NULL;
5305 *tmp_branch = NULL;
5306 *base_commit_id = NULL;
5307 *fileindex = NULL;
5309 err = lock_worktree(worktree, LOCK_EX);
5310 if (err)
5311 return err;
5313 err = open_fileindex(fileindex, &fileindex_path, worktree);
5314 if (err)
5315 goto done;
5317 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5318 &have_staged_files);
5319 if (err && err->code != GOT_ERR_CANCELLED)
5320 goto done;
5321 if (have_staged_files) {
5322 err = got_error(GOT_ERR_STAGED_PATHS);
5323 goto done;
5326 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5327 if (err)
5328 goto done;
5330 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5331 if (err)
5332 goto done;
5334 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5335 if (err)
5336 goto done;
5338 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5339 worktree);
5340 if (err)
5341 goto done;
5343 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5344 if (err)
5345 goto done;
5347 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5348 if (err)
5349 goto done;
5350 err = got_ref_resolve(commit_id, repo, commit_ref);
5351 if (err)
5352 goto done;
5354 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5355 if (err)
5356 goto done;
5357 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5358 if (err)
5359 goto done;
5361 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5362 if (err)
5363 goto done;
5364 done:
5365 free(commit_ref_name);
5366 free(branch_ref_name);
5367 free(fileindex_path);
5368 if (commit_ref)
5369 got_ref_close(commit_ref);
5370 if (base_commit_ref)
5371 got_ref_close(base_commit_ref);
5372 if (err) {
5373 free(*commit_id);
5374 *commit_id = NULL;
5375 free(*base_commit_id);
5376 *base_commit_id = NULL;
5377 if (*tmp_branch) {
5378 got_ref_close(*tmp_branch);
5379 *tmp_branch = NULL;
5381 if (*fileindex) {
5382 got_fileindex_free(*fileindex);
5383 *fileindex = NULL;
5385 lock_worktree(worktree, LOCK_EX);
5387 return err;
5390 static const struct got_error *
5391 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5393 const struct got_error *err;
5394 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5395 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5397 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5398 if (err)
5399 goto done;
5400 err = delete_ref(tmp_branch_name, repo);
5401 if (err)
5402 goto done;
5404 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5405 worktree);
5406 if (err)
5407 goto done;
5408 err = delete_ref(base_commit_ref_name, repo);
5409 if (err)
5410 goto done;
5412 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5413 if (err)
5414 goto done;
5415 err = delete_ref(branch_ref_name, repo);
5416 if (err)
5417 goto done;
5419 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5420 if (err)
5421 goto done;
5422 err = delete_ref(commit_ref_name, repo);
5423 if (err)
5424 goto done;
5425 done:
5426 free(tmp_branch_name);
5427 free(base_commit_ref_name);
5428 free(branch_ref_name);
5429 free(commit_ref_name);
5430 return err;
5433 const struct got_error *
5434 got_worktree_histedit_abort(struct got_worktree *worktree,
5435 struct got_fileindex *fileindex, struct got_repository *repo,
5436 struct got_reference *branch, struct got_object_id *base_commit_id,
5437 got_worktree_checkout_cb progress_cb, void *progress_arg)
5439 const struct got_error *err, *unlockerr, *sync_err;
5440 struct got_reference *resolved = NULL;
5441 char *fileindex_path = NULL;
5442 struct got_object_id *tree_id = NULL;
5443 struct revert_file_args rfa;
5445 err = lock_worktree(worktree, LOCK_EX);
5446 if (err)
5447 return err;
5449 err = got_ref_open(&resolved, repo,
5450 got_ref_get_symref_target(branch), 0);
5451 if (err)
5452 goto done;
5454 err = got_worktree_set_head_ref(worktree, resolved);
5455 if (err)
5456 goto done;
5458 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5459 if (err)
5460 goto done;
5462 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5463 worktree->path_prefix);
5464 if (err)
5465 goto done;
5467 err = delete_histedit_refs(worktree, repo);
5468 if (err)
5469 goto done;
5471 err = get_fileindex_path(&fileindex_path, worktree);
5472 if (err)
5473 goto done;
5475 rfa.worktree = worktree;
5476 rfa.fileindex = fileindex;
5477 rfa.progress_cb = progress_cb;
5478 rfa.progress_arg = progress_arg;
5479 rfa.patch_cb = NULL;
5480 rfa.patch_arg = NULL;
5481 rfa.repo = repo;
5482 err = worktree_status(worktree, "", fileindex, repo,
5483 revert_file, &rfa, NULL, NULL);
5484 if (err)
5485 goto sync;
5487 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5488 repo, progress_cb, progress_arg, NULL, NULL);
5489 sync:
5490 sync_err = sync_fileindex(fileindex, fileindex_path);
5491 if (sync_err && err == NULL)
5492 err = sync_err;
5493 done:
5494 got_ref_close(resolved);
5495 free(tree_id);
5496 free(fileindex_path);
5498 unlockerr = lock_worktree(worktree, LOCK_SH);
5499 if (unlockerr && err == NULL)
5500 err = unlockerr;
5501 return err;
5504 const struct got_error *
5505 got_worktree_histedit_complete(struct got_worktree *worktree,
5506 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5507 struct got_reference *edited_branch, struct got_repository *repo)
5509 const struct got_error *err, *unlockerr;
5510 struct got_object_id *new_head_commit_id = NULL;
5511 struct got_reference *resolved = NULL;
5513 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5514 if (err)
5515 return err;
5517 err = got_ref_open(&resolved, repo,
5518 got_ref_get_symref_target(edited_branch), 0);
5519 if (err)
5520 goto done;
5522 err = got_ref_change_ref(resolved, new_head_commit_id);
5523 if (err)
5524 goto done;
5526 err = got_ref_write(resolved, repo);
5527 if (err)
5528 goto done;
5530 err = got_worktree_set_head_ref(worktree, resolved);
5531 if (err)
5532 goto done;
5534 err = delete_histedit_refs(worktree, repo);
5535 done:
5536 if (fileindex)
5537 got_fileindex_free(fileindex);
5538 free(new_head_commit_id);
5539 unlockerr = lock_worktree(worktree, LOCK_SH);
5540 if (unlockerr && err == NULL)
5541 err = unlockerr;
5542 return err;
5545 const struct got_error *
5546 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5547 struct got_object_id *commit_id, struct got_repository *repo)
5549 const struct got_error *err;
5550 char *commit_ref_name;
5552 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5553 if (err)
5554 return err;
5556 err = store_commit_id(commit_ref_name, commit_id, repo);
5557 if (err)
5558 goto done;
5560 err = delete_ref(commit_ref_name, repo);
5561 done:
5562 free(commit_ref_name);
5563 return err;
5566 struct check_stage_ok_arg {
5567 struct got_object_id *head_commit_id;
5568 struct got_worktree *worktree;
5569 struct got_fileindex *fileindex;
5570 struct got_repository *repo;
5571 int have_changes;
5574 const struct got_error *
5575 check_stage_ok(void *arg, unsigned char status,
5576 unsigned char staged_status, const char *relpath,
5577 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5578 struct got_object_id *commit_id)
5580 struct check_stage_ok_arg *a = arg;
5581 const struct got_error *err = NULL;
5582 struct got_fileindex_entry *ie;
5583 struct got_object_id base_commit_id;
5584 struct got_object_id *base_commit_idp = NULL;
5585 char *in_repo_path = NULL, *p;
5587 if (status == GOT_STATUS_UNVERSIONED)
5588 return NULL;
5589 if (status == GOT_STATUS_NONEXISTENT)
5590 return got_error_set_errno(ENOENT, relpath);
5592 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5593 if (ie == NULL)
5594 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5596 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5597 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5598 relpath) == -1)
5599 return got_error_from_errno("asprintf");
5601 if (got_fileindex_entry_has_commit(ie)) {
5602 memcpy(base_commit_id.sha1, ie->commit_sha1,
5603 SHA1_DIGEST_LENGTH);
5604 base_commit_idp = &base_commit_id;
5607 if (status == GOT_STATUS_NO_CHANGE) {
5608 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5609 goto done;
5610 } else if (status == GOT_STATUS_CONFLICT) {
5611 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5612 goto done;
5613 } else if (status != GOT_STATUS_ADD &&
5614 status != GOT_STATUS_MODIFY &&
5615 status != GOT_STATUS_DELETE) {
5616 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5617 goto done;
5620 a->have_changes = 1;
5622 p = in_repo_path;
5623 while (p[0] == '/')
5624 p++;
5625 err = check_out_of_date(p, status, staged_status,
5626 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5627 GOT_ERR_STAGE_OUT_OF_DATE);
5628 done:
5629 free(in_repo_path);
5630 return err;
5633 struct stage_path_arg {
5634 struct got_worktree *worktree;
5635 struct got_fileindex *fileindex;
5636 struct got_repository *repo;
5637 got_worktree_status_cb status_cb;
5638 void *status_arg;
5639 got_worktree_patch_cb patch_cb;
5640 void *patch_arg;
5643 static const struct got_error *
5644 stage_path(void *arg, unsigned char status,
5645 unsigned char staged_status, const char *relpath,
5646 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5647 struct got_object_id *commit_id)
5649 struct stage_path_arg *a = arg;
5650 const struct got_error *err = NULL;
5651 struct got_fileindex_entry *ie;
5652 char *ondisk_path = NULL, *path_content = NULL;
5653 uint32_t stage;
5654 struct got_object_id *new_staged_blob_id = NULL;
5656 if (status == GOT_STATUS_UNVERSIONED)
5657 return NULL;
5659 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5660 if (ie == NULL)
5661 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5663 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5664 relpath)== -1)
5665 return got_error_from_errno("asprintf");
5667 switch (status) {
5668 case GOT_STATUS_ADD:
5669 case GOT_STATUS_MODIFY:
5670 if (a->patch_cb) {
5671 if (status == GOT_STATUS_ADD) {
5672 int choice = GOT_PATCH_CHOICE_NONE;
5673 err = (*a->patch_cb)(&choice, a->patch_arg,
5674 status, ie->path, NULL, 1, 1);
5675 if (err)
5676 break;
5677 if (choice != GOT_PATCH_CHOICE_YES)
5678 break;
5679 } else {
5680 err = create_patched_content(&path_content, 0,
5681 staged_blob_id ? staged_blob_id : blob_id,
5682 ondisk_path, ie->path, a->repo,
5683 a->patch_cb, a->patch_arg);
5684 if (err || path_content == NULL)
5685 break;
5688 err = got_object_blob_create(&new_staged_blob_id,
5689 path_content ? path_content : ondisk_path, a->repo);
5690 if (err)
5691 break;
5692 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5693 SHA1_DIGEST_LENGTH);
5694 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5695 stage = GOT_FILEIDX_STAGE_ADD;
5696 else
5697 stage = GOT_FILEIDX_STAGE_MODIFY;
5698 got_fileindex_entry_stage_set(ie, stage);
5699 if (a->status_cb == NULL)
5700 break;
5701 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5702 get_staged_status(ie), relpath, blob_id,
5703 new_staged_blob_id, NULL);
5704 break;
5705 case GOT_STATUS_DELETE:
5706 if (staged_status == GOT_STATUS_DELETE)
5707 break;
5708 if (a->patch_cb) {
5709 int choice = GOT_PATCH_CHOICE_NONE;
5710 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5711 ie->path, NULL, 1, 1);
5712 if (err)
5713 break;
5714 if (choice == GOT_PATCH_CHOICE_NO)
5715 break;
5716 if (choice != GOT_PATCH_CHOICE_YES) {
5717 err = got_error(GOT_ERR_PATCH_CHOICE);
5718 break;
5721 stage = GOT_FILEIDX_STAGE_DELETE;
5722 got_fileindex_entry_stage_set(ie, stage);
5723 if (a->status_cb == NULL)
5724 break;
5725 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5726 get_staged_status(ie), relpath, NULL, NULL, NULL);
5727 break;
5728 case GOT_STATUS_NO_CHANGE:
5729 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5730 break;
5731 case GOT_STATUS_CONFLICT:
5732 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5733 break;
5734 case GOT_STATUS_NONEXISTENT:
5735 err = got_error_set_errno(ENOENT, relpath);
5736 break;
5737 default:
5738 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5739 break;
5742 if (path_content && unlink(path_content) == -1 && err == NULL)
5743 err = got_error_from_errno2("unlink", path_content);
5744 free(path_content);
5745 free(ondisk_path);
5746 free(new_staged_blob_id);
5747 return err;
5750 const struct got_error *
5751 got_worktree_stage(struct got_worktree *worktree,
5752 struct got_pathlist_head *paths,
5753 got_worktree_status_cb status_cb, void *status_arg,
5754 got_worktree_patch_cb patch_cb, void *patch_arg,
5755 struct got_repository *repo)
5757 const struct got_error *err = NULL, *sync_err, *unlockerr;
5758 struct got_pathlist_entry *pe;
5759 struct got_fileindex *fileindex = NULL;
5760 char *fileindex_path = NULL;
5761 struct got_reference *head_ref = NULL;
5762 struct got_object_id *head_commit_id = NULL;
5763 struct check_stage_ok_arg oka;
5764 struct stage_path_arg spa;
5766 err = lock_worktree(worktree, LOCK_EX);
5767 if (err)
5768 return err;
5770 err = got_ref_open(&head_ref, repo,
5771 got_worktree_get_head_ref_name(worktree), 0);
5772 if (err)
5773 goto done;
5774 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5775 if (err)
5776 goto done;
5777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5778 if (err)
5779 goto done;
5781 /* Check pre-conditions before staging anything. */
5782 oka.head_commit_id = head_commit_id;
5783 oka.worktree = worktree;
5784 oka.fileindex = fileindex;
5785 oka.repo = repo;
5786 oka.have_changes = 0;
5787 TAILQ_FOREACH(pe, paths, entry) {
5788 err = worktree_status(worktree, pe->path, fileindex, repo,
5789 check_stage_ok, &oka, NULL, NULL);
5790 if (err)
5791 goto done;
5793 if (!oka.have_changes) {
5794 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5795 goto done;
5798 spa.worktree = worktree;
5799 spa.fileindex = fileindex;
5800 spa.repo = repo;
5801 spa.patch_cb = patch_cb;
5802 spa.patch_arg = patch_arg;
5803 spa.status_cb = status_cb;
5804 spa.status_arg = status_arg;
5805 TAILQ_FOREACH(pe, paths, entry) {
5806 err = worktree_status(worktree, pe->path, fileindex, repo,
5807 stage_path, &spa, NULL, NULL);
5808 if (err)
5809 goto done;
5812 sync_err = sync_fileindex(fileindex, fileindex_path);
5813 if (sync_err && err == NULL)
5814 err = sync_err;
5815 done:
5816 if (head_ref)
5817 got_ref_close(head_ref);
5818 free(head_commit_id);
5819 free(fileindex_path);
5820 if (fileindex)
5821 got_fileindex_free(fileindex);
5822 unlockerr = lock_worktree(worktree, LOCK_SH);
5823 if (unlockerr && err == NULL)
5824 err = unlockerr;
5825 return err;
5828 struct unstage_path_arg {
5829 struct got_worktree *worktree;
5830 struct got_fileindex *fileindex;
5831 struct got_repository *repo;
5832 got_worktree_checkout_cb progress_cb;
5833 void *progress_arg;
5834 got_worktree_patch_cb patch_cb;
5835 void *patch_arg;
5838 static const struct got_error *
5839 create_unstaged_content(char **path_unstaged_content,
5840 char **path_new_staged_content, struct got_object_id *blob_id,
5841 struct got_object_id *staged_blob_id, const char *relpath,
5842 struct got_repository *repo,
5843 got_worktree_patch_cb patch_cb, void *patch_arg)
5845 const struct got_error *err;
5846 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5847 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5848 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5849 struct stat sb1, sb2;
5850 struct got_diff_changes *changes = NULL;
5851 struct got_diff_state *ds = NULL;
5852 struct got_diff_args *args = NULL;
5853 struct got_diff_change *change;
5854 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5855 int have_content = 0, have_rejected_content = 0;
5857 *path_unstaged_content = NULL;
5858 *path_new_staged_content = NULL;
5860 err = got_object_id_str(&label1, blob_id);
5861 if (err)
5862 return err;
5863 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5864 if (err)
5865 goto done;
5867 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5868 if (err)
5869 goto done;
5871 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5872 if (err)
5873 goto done;
5875 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5876 if (err)
5877 goto done;
5879 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5880 if (err)
5881 goto done;
5883 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5884 if (err)
5885 goto done;
5887 if (stat(path1, &sb1) == -1) {
5888 err = got_error_from_errno2("stat", path1);
5889 goto done;
5892 if (stat(path2, &sb2) == -1) {
5893 err = got_error_from_errno2("stat", path2);
5894 goto done;
5897 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5898 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5899 if (err)
5900 goto done;
5902 err = got_opentemp_named(path_unstaged_content, &outfile,
5903 "got-unstaged-content");
5904 if (err)
5905 goto done;
5906 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5907 "got-new-staged-content");
5908 if (err)
5909 goto done;
5911 if (fseek(f1, 0L, SEEK_SET) == -1) {
5912 err = got_ferror(f1, GOT_ERR_IO);
5913 goto done;
5915 if (fseek(f2, 0L, SEEK_SET) == -1) {
5916 err = got_ferror(f2, GOT_ERR_IO);
5917 goto done;
5919 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5920 int choice;
5921 err = apply_or_reject_change(&choice, change, ++n,
5922 changes->nchanges, ds, args, diff_flags, relpath,
5923 f1, f2, &line_cur1, &line_cur2,
5924 outfile, rejectfile, patch_cb, patch_arg);
5925 if (err)
5926 goto done;
5927 if (choice == GOT_PATCH_CHOICE_YES)
5928 have_content = 1;
5929 else
5930 have_rejected_content = 1;
5931 if (choice == GOT_PATCH_CHOICE_QUIT)
5932 break;
5934 if (have_content || have_rejected_content)
5935 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5936 outfile, rejectfile);
5937 done:
5938 free(label1);
5939 if (blob)
5940 got_object_blob_close(blob);
5941 if (staged_blob)
5942 got_object_blob_close(staged_blob);
5943 if (f1 && fclose(f1) == EOF && err == NULL)
5944 err = got_error_from_errno2("fclose", path1);
5945 if (f2 && fclose(f2) == EOF && err == NULL)
5946 err = got_error_from_errno2("fclose", path2);
5947 if (outfile && fclose(outfile) == EOF && err == NULL)
5948 err = got_error_from_errno2("fclose", *path_unstaged_content);
5949 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5950 err = got_error_from_errno2("fclose", *path_new_staged_content);
5951 if (path1 && unlink(path1) == -1 && err == NULL)
5952 err = got_error_from_errno2("unlink", path1);
5953 if (path2 && unlink(path2) == -1 && err == NULL)
5954 err = got_error_from_errno2("unlink", path2);
5955 if (err || !have_content) {
5956 if (*path_unstaged_content &&
5957 unlink(*path_unstaged_content) == -1 && err == NULL)
5958 err = got_error_from_errno2("unlink",
5959 *path_unstaged_content);
5960 free(*path_unstaged_content);
5961 *path_unstaged_content = NULL;
5963 if (err || !have_rejected_content) {
5964 if (*path_new_staged_content &&
5965 unlink(*path_new_staged_content) == -1 && err == NULL)
5966 err = got_error_from_errno2("unlink",
5967 *path_new_staged_content);
5968 free(*path_new_staged_content);
5969 *path_new_staged_content = NULL;
5971 free(args);
5972 if (ds) {
5973 got_diff_state_free(ds);
5974 free(ds);
5976 if (changes)
5977 got_diff_free_changes(changes);
5978 free(path1);
5979 free(path2);
5980 return err;
5983 static const struct got_error *
5984 unstage_path(void *arg, unsigned char status,
5985 unsigned char staged_status, const char *relpath,
5986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5987 struct got_object_id *commit_id)
5989 const struct got_error *err = NULL;
5990 struct unstage_path_arg *a = arg;
5991 struct got_fileindex_entry *ie;
5992 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5993 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5994 char *path_new_staged_content = NULL;
5995 int local_changes_subsumed;
5996 struct stat sb;
5998 if (staged_status != GOT_STATUS_ADD &&
5999 staged_status != GOT_STATUS_MODIFY &&
6000 staged_status != GOT_STATUS_DELETE)
6001 return NULL;
6003 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6004 if (ie == NULL)
6005 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6008 == -1)
6009 return got_error_from_errno("asprintf");
6011 switch (staged_status) {
6012 case GOT_STATUS_MODIFY:
6013 err = got_object_open_as_blob(&blob_base, a->repo,
6014 blob_id, 8192);
6015 if (err)
6016 break;
6017 /* fall through */
6018 case GOT_STATUS_ADD:
6019 if (a->patch_cb) {
6020 if (staged_status == GOT_STATUS_ADD) {
6021 int choice = GOT_PATCH_CHOICE_NONE;
6022 err = (*a->patch_cb)(&choice, a->patch_arg,
6023 staged_status, ie->path, NULL, 1, 1);
6024 if (err)
6025 break;
6026 if (choice != GOT_PATCH_CHOICE_YES)
6027 break;
6028 } else {
6029 err = create_unstaged_content(
6030 &path_unstaged_content,
6031 &path_new_staged_content, blob_id,
6032 staged_blob_id, ie->path, a->repo,
6033 a->patch_cb, a->patch_arg);
6034 if (err || path_unstaged_content == NULL)
6035 break;
6036 if (path_new_staged_content) {
6037 err = got_object_blob_create(
6038 &staged_blob_id,
6039 path_new_staged_content,
6040 a->repo);
6041 if (err)
6042 break;
6043 memcpy(ie->staged_blob_sha1,
6044 staged_blob_id->sha1,
6045 SHA1_DIGEST_LENGTH);
6047 err = merge_file(&local_changes_subsumed,
6048 a->worktree, blob_base, ondisk_path,
6049 relpath, got_fileindex_perms_to_st(ie),
6050 path_unstaged_content, "unstaged",
6051 a->repo, a->progress_cb, a->progress_arg);
6052 if (err == NULL &&
6053 path_new_staged_content == NULL)
6054 got_fileindex_entry_stage_set(ie,
6055 GOT_FILEIDX_STAGE_NONE);
6056 break; /* Done with this file. */
6059 err = got_object_open_as_blob(&blob_staged, a->repo,
6060 staged_blob_id, 8192);
6061 if (err)
6062 break;
6063 err = merge_blob(&local_changes_subsumed, a->worktree,
6064 blob_base, ondisk_path, relpath,
6065 got_fileindex_perms_to_st(ie), blob_staged,
6066 commit_id ? commit_id : a->worktree->base_commit_id,
6067 a->repo, a->progress_cb, a->progress_arg);
6068 if (err == NULL)
6069 got_fileindex_entry_stage_set(ie,
6070 GOT_FILEIDX_STAGE_NONE);
6071 break;
6072 case GOT_STATUS_DELETE:
6073 if (a->patch_cb) {
6074 int choice = GOT_PATCH_CHOICE_NONE;
6075 err = (*a->patch_cb)(&choice, a->patch_arg,
6076 staged_status, ie->path, NULL, 1, 1);
6077 if (err)
6078 break;
6079 if (choice == GOT_PATCH_CHOICE_NO)
6080 break;
6081 if (choice != GOT_PATCH_CHOICE_YES) {
6082 err = got_error(GOT_ERR_PATCH_CHOICE);
6083 break;
6086 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6087 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6088 if (err)
6089 break;
6090 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6091 break;
6094 free(ondisk_path);
6095 if (path_unstaged_content &&
6096 unlink(path_unstaged_content) == -1 && err == NULL)
6097 err = got_error_from_errno2("unlink", path_unstaged_content);
6098 if (path_new_staged_content &&
6099 unlink(path_new_staged_content) == -1 && err == NULL)
6100 err = got_error_from_errno2("unlink", path_new_staged_content);
6101 free(path_unstaged_content);
6102 free(path_new_staged_content);
6103 if (blob_base)
6104 got_object_blob_close(blob_base);
6105 if (blob_staged)
6106 got_object_blob_close(blob_staged);
6107 return err;
6110 const struct got_error *
6111 got_worktree_unstage(struct got_worktree *worktree,
6112 struct got_pathlist_head *paths,
6113 got_worktree_checkout_cb progress_cb, void *progress_arg,
6114 got_worktree_patch_cb patch_cb, void *patch_arg,
6115 struct got_repository *repo)
6117 const struct got_error *err = NULL, *sync_err, *unlockerr;
6118 struct got_pathlist_entry *pe;
6119 struct got_fileindex *fileindex = NULL;
6120 char *fileindex_path = NULL;
6121 struct unstage_path_arg upa;
6123 err = lock_worktree(worktree, LOCK_EX);
6124 if (err)
6125 return err;
6127 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6128 if (err)
6129 goto done;
6131 upa.worktree = worktree;
6132 upa.fileindex = fileindex;
6133 upa.repo = repo;
6134 upa.progress_cb = progress_cb;
6135 upa.progress_arg = progress_arg;
6136 upa.patch_cb = patch_cb;
6137 upa.patch_arg = patch_arg;
6138 TAILQ_FOREACH(pe, paths, entry) {
6139 err = worktree_status(worktree, pe->path, fileindex, repo,
6140 unstage_path, &upa, NULL, NULL);
6141 if (err)
6142 goto done;
6145 sync_err = sync_fileindex(fileindex, fileindex_path);
6146 if (sync_err && err == NULL)
6147 err = sync_err;
6148 done:
6149 free(fileindex_path);
6150 if (fileindex)
6151 got_fileindex_free(fileindex);
6152 unlockerr = lock_worktree(worktree, LOCK_SH);
6153 if (unlockerr && err == NULL)
6154 err = unlockerr;
6155 return err;