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';
2386 /* Git's ignores may contain comments. */
2387 if (line[0] == '#')
2388 continue;
2390 /* Git's negated patterns are not (yet?) supported. */
2391 if (line[0] == '!')
2392 continue;
2394 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2395 line) == -1) {
2396 err = got_error_from_errno("asprintf");
2397 goto done;
2399 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2400 if (err)
2401 goto done;
2403 if (ferror(f)) {
2404 err = got_error_from_errno("getline");
2405 goto done;
2408 dirpath = strdup(path);
2409 if (dirpath == NULL) {
2410 err = got_error_from_errno("strdup");
2411 goto done;
2413 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2414 done:
2415 free(line);
2416 if (err || pe == NULL) {
2417 free(dirpath);
2418 free_ignorelist(ignorelist);
2420 return err;
2423 int
2424 match_ignores(struct got_pathlist_head *ignores, const char *path)
2426 struct got_pathlist_entry *pe;
2428 /* Handle patterns which match in all directories. */
2429 TAILQ_FOREACH(pe, ignores, entry) {
2430 struct got_pathlist_head *ignorelist = pe->data;
2431 struct got_pathlist_entry *pi;
2433 TAILQ_FOREACH(pi, ignorelist, entry) {
2434 const char *p, *pattern = pi->path;
2436 if (strncmp(pattern, "**/", 3) != 0)
2437 continue;
2438 pattern += 3;
2439 p = path;
2440 while (*p) {
2441 if (fnmatch(pattern, p,
2442 FNM_PATHNAME | FNM_LEADING_DIR)) {
2443 /* Retry in next directory. */
2444 while (*p && *p != '/')
2445 p++;
2446 while (*p == '/')
2447 p++;
2448 continue;
2450 return 1;
2456 * The ignores pathlist contains ignore lists from children before
2457 * parents, so we can find the most specific ignorelist by walking
2458 * ignores backwards.
2460 pe = TAILQ_LAST(ignores, got_pathlist_head);
2461 while (pe) {
2462 if (got_path_is_child(path, pe->path, pe->path_len)) {
2463 struct got_pathlist_head *ignorelist = pe->data;
2464 struct got_pathlist_entry *pi;
2465 TAILQ_FOREACH(pi, ignorelist, entry) {
2466 const char *pattern = pi->path;
2467 int flags = FNM_LEADING_DIR;
2468 if (strstr(pattern, "/**/") == NULL)
2469 flags |= FNM_PATHNAME;
2470 if (fnmatch(pattern, path, flags))
2471 continue;
2472 return 1;
2475 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2478 return 0;
2481 static const struct got_error *
2482 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2483 const char *path, const char *ignores_filename)
2485 const struct got_error *err = NULL;
2486 char *ignorespath;
2487 FILE *ignoresfile = NULL;
2489 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2490 path[0] ? "/" : "", ignores_filename) == -1)
2491 return got_error_from_errno("asprintf");
2493 ignoresfile = fopen(ignorespath, "r");
2494 if (ignoresfile == NULL) {
2495 if (errno != ENOENT && errno != EACCES)
2496 err = got_error_from_errno2("fopen",
2497 ignorespath);
2498 } else
2499 err = read_ignores(ignores, path, ignoresfile);
2501 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2502 err = got_error_from_errno2("flose", path);
2503 free(ignorespath);
2504 return err;
2507 static const struct got_error *
2508 status_new(void *arg, struct dirent *de, const char *parent_path)
2510 const struct got_error *err = NULL;
2511 struct diff_dir_cb_arg *a = arg;
2512 char *path = NULL;
2514 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2515 return got_error(GOT_ERR_CANCELLED);
2517 /* XXX ignore symlinks for now */
2518 if (de->d_type == DT_LNK)
2519 return NULL;
2521 if (parent_path[0]) {
2522 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2523 return got_error_from_errno("asprintf");
2524 } else {
2525 path = de->d_name;
2528 if (de->d_type == DT_DIR) {
2529 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2530 ".cvsignore");
2531 if (err == NULL)
2532 err = add_ignores(&a->ignores, a->worktree->root_path,
2533 path, ".gitignore");
2535 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2536 && !match_ignores(&a->ignores, path))
2537 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2538 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2539 if (parent_path[0])
2540 free(path);
2541 return err;
2544 static const struct got_error *
2545 report_single_file_status(const char *path, const char *ondisk_path,
2546 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2547 void *status_arg, struct got_repository *repo)
2549 struct got_fileindex_entry *ie;
2550 struct stat sb;
2552 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2553 if (ie)
2554 return report_file_status(ie, ondisk_path, status_cb,
2555 status_arg, repo);
2557 if (lstat(ondisk_path, &sb) == -1) {
2558 if (errno != ENOENT)
2559 return got_error_from_errno2("lstat", ondisk_path);
2560 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2561 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2562 return NULL;
2565 if (S_ISREG(sb.st_mode))
2566 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2567 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2569 return NULL;
2572 static const struct got_error *
2573 worktree_status(struct got_worktree *worktree, const char *path,
2574 struct got_fileindex *fileindex, struct got_repository *repo,
2575 got_worktree_status_cb status_cb, void *status_arg,
2576 got_cancel_cb cancel_cb, void *cancel_arg)
2578 const struct got_error *err = NULL;
2579 DIR *workdir = NULL;
2580 struct got_fileindex_diff_dir_cb fdiff_cb;
2581 struct diff_dir_cb_arg arg;
2582 char *ondisk_path = NULL;
2584 if (asprintf(&ondisk_path, "%s%s%s",
2585 worktree->root_path, path[0] ? "/" : "", path) == -1)
2586 return got_error_from_errno("asprintf");
2588 workdir = opendir(ondisk_path);
2589 if (workdir == NULL) {
2590 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2591 err = got_error_from_errno2("opendir", ondisk_path);
2592 else
2593 err = report_single_file_status(path, ondisk_path,
2594 fileindex, status_cb, status_arg, repo);
2595 } else {
2596 fdiff_cb.diff_old_new = status_old_new;
2597 fdiff_cb.diff_old = status_old;
2598 fdiff_cb.diff_new = status_new;
2599 arg.fileindex = fileindex;
2600 arg.worktree = worktree;
2601 arg.status_path = path;
2602 arg.status_path_len = strlen(path);
2603 arg.repo = repo;
2604 arg.status_cb = status_cb;
2605 arg.status_arg = status_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 TAILQ_INIT(&arg.ignores);
2609 err = add_ignores(&arg.ignores, worktree->root_path, path,
2610 ".cvsignore");
2611 if (err == NULL)
2612 err = add_ignores(&arg.ignores, worktree->root_path,
2613 path, ".gitignore");
2614 if (err == NULL)
2615 err = got_fileindex_diff_dir(fileindex, workdir,
2616 worktree->root_path, path, repo, &fdiff_cb, &arg);
2617 free_ignores(&arg.ignores);
2620 if (workdir)
2621 closedir(workdir);
2622 free(ondisk_path);
2623 return err;
2626 const struct got_error *
2627 got_worktree_status(struct got_worktree *worktree,
2628 struct got_pathlist_head *paths, struct got_repository *repo,
2629 got_worktree_status_cb status_cb, void *status_arg,
2630 got_cancel_cb cancel_cb, void *cancel_arg)
2632 const struct got_error *err = NULL;
2633 char *fileindex_path = NULL;
2634 struct got_fileindex *fileindex = NULL;
2635 struct got_pathlist_entry *pe;
2637 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2638 if (err)
2639 return err;
2641 TAILQ_FOREACH(pe, paths, entry) {
2642 err = worktree_status(worktree, pe->path, fileindex, repo,
2643 status_cb, status_arg, cancel_cb, cancel_arg);
2644 if (err)
2645 break;
2647 free(fileindex_path);
2648 got_fileindex_free(fileindex);
2649 return err;
2652 const struct got_error *
2653 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2654 const char *arg)
2656 const struct got_error *err = NULL;
2657 char *resolved, *cwd = NULL, *path = NULL;
2658 size_t len;
2660 *wt_path = NULL;
2662 resolved = realpath(arg, NULL);
2663 if (resolved == NULL) {
2664 if (errno != ENOENT)
2665 return got_error_from_errno2("realpath", arg);
2666 cwd = getcwd(NULL, 0);
2667 if (cwd == NULL)
2668 return got_error_from_errno("getcwd");
2669 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2670 err = got_error_from_errno("asprintf");
2671 goto done;
2675 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2676 strlen(got_worktree_get_root_path(worktree)))) {
2677 err = got_error(GOT_ERR_BAD_PATH);
2678 goto done;
2681 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2682 err = got_path_skip_common_ancestor(&path,
2683 got_worktree_get_root_path(worktree), resolved);
2684 if (err)
2685 goto done;
2686 } else {
2687 path = strdup("");
2688 if (path == NULL) {
2689 err = got_error_from_errno("strdup");
2690 goto done;
2694 /* XXX status walk can't deal with trailing slash! */
2695 len = strlen(path);
2696 while (len > 0 && path[len - 1] == '/') {
2697 path[len - 1] = '\0';
2698 len--;
2700 done:
2701 free(resolved);
2702 free(cwd);
2703 if (err == NULL)
2704 *wt_path = path;
2705 else
2706 free(path);
2707 return err;
2710 static const struct got_error *
2711 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2712 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2713 struct got_repository *repo)
2715 const struct got_error *err = NULL;
2716 struct got_fileindex_entry *ie;
2717 unsigned char status;
2718 struct stat sb;
2720 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2721 if (ie) {
2722 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2723 if (err)
2724 return err;
2725 /* Re-adding an existing entry is a no-op. */
2726 if (status == GOT_STATUS_ADD)
2727 return NULL;
2728 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2731 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2732 if (err)
2733 return err;
2735 err = got_fileindex_entry_add(fileindex, ie);
2736 if (err) {
2737 got_fileindex_entry_free(ie);
2738 return err;
2741 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2744 const struct got_error *
2745 got_worktree_schedule_add(struct got_worktree *worktree,
2746 struct got_pathlist_head *paths,
2747 got_worktree_status_cb status_cb, void *status_arg,
2748 struct got_repository *repo)
2750 struct got_fileindex *fileindex = NULL;
2751 char *fileindex_path = NULL;
2752 const struct got_error *err = NULL, *sync_err, *unlockerr;
2753 struct got_pathlist_entry *pe;
2755 err = lock_worktree(worktree, LOCK_EX);
2756 if (err)
2757 return err;
2759 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2760 if (err)
2761 goto done;
2763 TAILQ_FOREACH(pe, paths, entry) {
2764 char *ondisk_path;
2765 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2766 pe->path) == -1)
2767 return got_error_from_errno("asprintf");
2768 err = schedule_addition(ondisk_path, fileindex, pe->path,
2769 status_cb, status_arg, repo);
2770 free(ondisk_path);
2771 if (err)
2772 break;
2774 sync_err = sync_fileindex(fileindex, fileindex_path);
2775 if (sync_err && err == NULL)
2776 err = sync_err;
2777 done:
2778 free(fileindex_path);
2779 if (fileindex)
2780 got_fileindex_free(fileindex);
2781 unlockerr = lock_worktree(worktree, LOCK_SH);
2782 if (unlockerr && err == NULL)
2783 err = unlockerr;
2784 return err;
2787 static const struct got_error *
2788 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2789 const char *relpath, int delete_local_mods,
2790 got_worktree_status_cb status_cb, void *status_arg,
2791 struct got_repository *repo)
2793 const struct got_error *err = NULL;
2794 struct got_fileindex_entry *ie = NULL;
2795 unsigned char status, staged_status;
2796 struct stat sb;
2798 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2799 if (ie == NULL)
2800 return got_error(GOT_ERR_BAD_PATH);
2802 staged_status = get_staged_status(ie);
2803 if (staged_status != GOT_STATUS_NO_CHANGE) {
2804 if (staged_status == GOT_STATUS_DELETE)
2805 return NULL;
2806 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2809 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2810 if (err)
2811 return err;
2813 if (status != GOT_STATUS_NO_CHANGE) {
2814 if (status == GOT_STATUS_DELETE)
2815 return NULL;
2816 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2817 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2818 if (status != GOT_STATUS_MODIFY &&
2819 status != GOT_STATUS_MISSING)
2820 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2823 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2824 return got_error_from_errno2("unlink", ondisk_path);
2826 got_fileindex_entry_mark_deleted_from_disk(ie);
2827 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2830 const struct got_error *
2831 got_worktree_schedule_delete(struct got_worktree *worktree,
2832 struct got_pathlist_head *paths, int delete_local_mods,
2833 got_worktree_status_cb status_cb, void *status_arg,
2834 struct got_repository *repo)
2836 struct got_fileindex *fileindex = NULL;
2837 char *fileindex_path = NULL;
2838 const struct got_error *err = NULL, *sync_err, *unlockerr;
2839 struct got_pathlist_entry *pe;
2841 err = lock_worktree(worktree, LOCK_EX);
2842 if (err)
2843 return err;
2845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2846 if (err)
2847 goto done;
2849 TAILQ_FOREACH(pe, paths, entry) {
2850 char *ondisk_path;
2851 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2852 pe->path) == -1)
2853 return got_error_from_errno("asprintf");
2854 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2855 delete_local_mods, status_cb, status_arg, repo);
2856 free(ondisk_path);
2857 if (err)
2858 break;
2860 sync_err = sync_fileindex(fileindex, fileindex_path);
2861 if (sync_err && err == NULL)
2862 err = sync_err;
2863 done:
2864 free(fileindex_path);
2865 if (fileindex)
2866 got_fileindex_free(fileindex);
2867 unlockerr = lock_worktree(worktree, LOCK_SH);
2868 if (unlockerr && err == NULL)
2869 err = unlockerr;
2870 return err;
2873 static const struct got_error *
2874 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2876 const struct got_error *err = NULL;
2877 char *line = NULL;
2878 size_t linesize = 0, n;
2879 ssize_t linelen;
2881 linelen = getline(&line, &linesize, infile);
2882 if (linelen == -1) {
2883 if (ferror(infile)) {
2884 err = got_error_from_errno("getline");
2885 goto done;
2887 return NULL;
2889 if (outfile) {
2890 n = fwrite(line, 1, linelen, outfile);
2891 if (n != linelen) {
2892 err = got_ferror(outfile, GOT_ERR_IO);
2893 goto done;
2896 if (rejectfile) {
2897 n = fwrite(line, 1, linelen, rejectfile);
2898 if (n != linelen)
2899 err = got_ferror(outfile, GOT_ERR_IO);
2901 done:
2902 free(line);
2903 return err;
2906 static const struct got_error *
2907 skip_one_line(FILE *f)
2909 char *line = NULL;
2910 size_t linesize = 0;
2911 ssize_t linelen;
2913 linelen = getline(&line, &linesize, f);
2914 if (linelen == -1) {
2915 if (ferror(f))
2916 return got_error_from_errno("getline");
2917 return NULL;
2919 free(line);
2920 return NULL;
2923 static const struct got_error *
2924 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2925 int start_old, int end_old, int start_new, int end_new,
2926 FILE *outfile, FILE *rejectfile)
2928 const struct got_error *err;
2930 /* Copy old file's lines leading up to patch. */
2931 while (!feof(f1) && *line_cur1 < start_old) {
2932 err = copy_one_line(f1, outfile, NULL);
2933 if (err)
2934 return err;
2935 (*line_cur1)++;
2937 /* Skip new file's lines leading up to patch. */
2938 while (!feof(f2) && *line_cur2 < start_new) {
2939 if (rejectfile)
2940 err = copy_one_line(f2, NULL, rejectfile);
2941 else
2942 err = skip_one_line(f2);
2943 if (err)
2944 return err;
2945 (*line_cur2)++;
2947 /* Copy patched lines. */
2948 while (!feof(f2) && *line_cur2 <= end_new) {
2949 err = copy_one_line(f2, outfile, NULL);
2950 if (err)
2951 return err;
2952 (*line_cur2)++;
2954 /* Skip over old file's replaced lines. */
2955 while (!feof(f1) && *line_cur1 <= end_old) {
2956 if (rejectfile)
2957 err = copy_one_line(f1, NULL, rejectfile);
2958 else
2959 err = skip_one_line(f1);
2960 if (err)
2961 return err;
2962 (*line_cur1)++;
2965 return NULL;
2968 static const struct got_error *
2969 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2970 FILE *outfile, FILE *rejectfile)
2972 const struct got_error *err;
2974 if (outfile) {
2975 /* Copy old file's lines until EOF. */
2976 while (!feof(f1)) {
2977 err = copy_one_line(f1, outfile, NULL);
2978 if (err)
2979 return err;
2980 (*line_cur1)++;
2983 if (rejectfile) {
2984 /* Copy new file's lines until EOF. */
2985 while (!feof(f2)) {
2986 err = copy_one_line(f2, NULL, rejectfile);
2987 if (err)
2988 return err;
2989 (*line_cur2)++;
2993 return NULL;
2996 static const struct got_error *
2997 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2998 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2999 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3000 int *line_cur2, FILE *outfile, FILE *rejectfile,
3001 got_worktree_patch_cb patch_cb, void *patch_arg)
3003 const struct got_error *err = NULL;
3004 int start_old = change->cv.a;
3005 int end_old = change->cv.b;
3006 int start_new = change->cv.c;
3007 int end_new = change->cv.d;
3008 long pos1, pos2;
3009 FILE *hunkfile;
3011 *choice = GOT_PATCH_CHOICE_NONE;
3013 hunkfile = got_opentemp();
3014 if (hunkfile == NULL)
3015 return got_error_from_errno("got_opentemp");
3017 pos1 = ftell(f1);
3018 pos2 = ftell(f2);
3020 /* XXX TODO needs error checking */
3021 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3023 if (fseek(f1, pos1, SEEK_SET) == -1) {
3024 err = got_ferror(f1, GOT_ERR_IO);
3025 goto done;
3027 if (fseek(f2, pos2, SEEK_SET) == -1) {
3028 err = got_ferror(f1, GOT_ERR_IO);
3029 goto done;
3031 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3032 err = got_ferror(hunkfile, GOT_ERR_IO);
3033 goto done;
3036 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3037 hunkfile, n, nchanges);
3038 if (err)
3039 goto done;
3041 switch (*choice) {
3042 case GOT_PATCH_CHOICE_YES:
3043 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3044 end_old, start_new, end_new, outfile, rejectfile);
3045 break;
3046 case GOT_PATCH_CHOICE_NO:
3047 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3048 end_old, start_new, end_new, rejectfile, outfile);
3049 break;
3050 case GOT_PATCH_CHOICE_QUIT:
3051 break;
3052 default:
3053 err = got_error(GOT_ERR_PATCH_CHOICE);
3054 break;
3056 done:
3057 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3058 err = got_error_from_errno("fclose");
3059 return err;
3062 struct revert_file_args {
3063 struct got_worktree *worktree;
3064 struct got_fileindex *fileindex;
3065 got_worktree_checkout_cb progress_cb;
3066 void *progress_arg;
3067 got_worktree_patch_cb patch_cb;
3068 void *patch_arg;
3069 struct got_repository *repo;
3072 static const struct got_error *
3073 create_patched_content(char **path_outfile, int reverse_patch,
3074 struct got_object_id *blob_id, const char *path2,
3075 const char *relpath, struct got_repository *repo,
3076 got_worktree_patch_cb patch_cb, void *patch_arg)
3078 const struct got_error *err;
3079 struct got_blob_object *blob = NULL;
3080 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3081 char *path1 = NULL, *id_str = NULL;
3082 struct stat sb1, sb2;
3083 struct got_diff_changes *changes = NULL;
3084 struct got_diff_state *ds = NULL;
3085 struct got_diff_args *args = NULL;
3086 struct got_diff_change *change;
3087 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3088 int n = 0;
3090 *path_outfile = NULL;
3092 err = got_object_id_str(&id_str, blob_id);
3093 if (err)
3094 return err;
3096 f2 = fopen(path2, "r");
3097 if (f2 == NULL) {
3098 err = got_error_from_errno2("fopen", path2);
3099 goto done;
3102 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3103 if (err)
3104 goto done;
3106 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3107 if (err)
3108 goto done;
3110 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3111 if (err)
3112 goto done;
3114 if (stat(path1, &sb1) == -1) {
3115 err = got_error_from_errno2("stat", path1);
3116 goto done;
3118 if (stat(path2, &sb2) == -1) {
3119 err = got_error_from_errno2("stat", path2);
3120 goto done;
3123 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3124 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3125 if (err)
3126 goto done;
3128 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3129 if (err)
3130 goto done;
3132 if (fseek(f1, 0L, SEEK_SET) == -1)
3133 return got_ferror(f1, GOT_ERR_IO);
3134 if (fseek(f2, 0L, SEEK_SET) == -1)
3135 return got_ferror(f2, GOT_ERR_IO);
3136 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3137 int choice;
3138 err = apply_or_reject_change(&choice, change, ++n,
3139 changes->nchanges, ds, args, diff_flags, relpath,
3140 f1, f2, &line_cur1, &line_cur2,
3141 reverse_patch ? NULL : outfile,
3142 reverse_patch ? outfile : NULL,
3143 patch_cb, patch_arg);
3144 if (err)
3145 goto done;
3146 if (choice == GOT_PATCH_CHOICE_YES)
3147 have_content = 1;
3148 else if (choice == GOT_PATCH_CHOICE_QUIT)
3149 break;
3151 if (have_content)
3152 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3153 reverse_patch ? NULL : outfile,
3154 reverse_patch ? outfile : NULL);
3155 done:
3156 free(id_str);
3157 if (blob)
3158 got_object_blob_close(blob);
3159 if (f1 && fclose(f1) == EOF && err == NULL)
3160 err = got_error_from_errno2("fclose", path1);
3161 if (f2 && fclose(f2) == EOF && err == NULL)
3162 err = got_error_from_errno2("fclose", path2);
3163 if (outfile && fclose(outfile) == EOF && err == NULL)
3164 err = got_error_from_errno2("fclose", *path_outfile);
3165 if (path1 && unlink(path1) == -1 && err == NULL)
3166 err = got_error_from_errno2("unlink", path1);
3167 if (err || !have_content) {
3168 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3169 err = got_error_from_errno2("unlink", *path_outfile);
3170 free(*path_outfile);
3171 *path_outfile = NULL;
3173 free(args);
3174 if (ds) {
3175 got_diff_state_free(ds);
3176 free(ds);
3178 if (changes)
3179 got_diff_free_changes(changes);
3180 free(path1);
3181 return err;
3184 static const struct got_error *
3185 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3186 const char *relpath, struct got_object_id *blob_id,
3187 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3189 struct revert_file_args *a = arg;
3190 const struct got_error *err = NULL;
3191 char *parent_path = NULL;
3192 struct got_fileindex_entry *ie;
3193 struct got_tree_object *tree = NULL;
3194 struct got_object_id *tree_id = NULL;
3195 const struct got_tree_entry *te = NULL;
3196 char *tree_path = NULL, *te_name;
3197 char *ondisk_path = NULL, *path_content = NULL;
3198 struct got_blob_object *blob = NULL;
3200 /* Reverting a staged deletion is a no-op. */
3201 if (status == GOT_STATUS_DELETE &&
3202 staged_status != GOT_STATUS_NO_CHANGE)
3203 return NULL;
3205 if (status == GOT_STATUS_UNVERSIONED)
3206 return (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_UNVERSIONED, relpath);
3209 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3210 if (ie == NULL)
3211 return got_error(GOT_ERR_BAD_PATH);
3213 /* Construct in-repository path of tree which contains this blob. */
3214 err = got_path_dirname(&parent_path, ie->path);
3215 if (err) {
3216 if (err->code != GOT_ERR_BAD_PATH)
3217 goto done;
3218 parent_path = strdup("/");
3219 if (parent_path == NULL) {
3220 err = got_error_from_errno("strdup");
3221 goto done;
3224 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3225 tree_path = strdup(parent_path);
3226 if (tree_path == NULL) {
3227 err = got_error_from_errno("strdup");
3228 goto done;
3230 } else {
3231 if (got_path_is_root_dir(parent_path)) {
3232 tree_path = strdup(a->worktree->path_prefix);
3233 if (tree_path == NULL) {
3234 err = got_error_from_errno("strdup");
3235 goto done;
3237 } else {
3238 if (asprintf(&tree_path, "%s/%s",
3239 a->worktree->path_prefix, parent_path) == -1) {
3240 err = got_error_from_errno("asprintf");
3241 goto done;
3246 err = got_object_id_by_path(&tree_id, a->repo,
3247 a->worktree->base_commit_id, tree_path);
3248 if (err) {
3249 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3250 (status == GOT_STATUS_ADD ||
3251 staged_status == GOT_STATUS_ADD)))
3252 goto done;
3253 } else {
3254 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3255 if (err)
3256 goto done;
3258 te_name = basename(ie->path);
3259 if (te_name == NULL) {
3260 err = got_error_from_errno2("basename", ie->path);
3261 goto done;
3264 te = got_object_tree_find_entry(tree, te_name);
3265 if (te == NULL && status != GOT_STATUS_ADD &&
3266 staged_status != GOT_STATUS_ADD) {
3267 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3268 goto done;
3272 switch (status) {
3273 case GOT_STATUS_ADD:
3274 if (a->patch_cb) {
3275 int choice = GOT_PATCH_CHOICE_NONE;
3276 err = (*a->patch_cb)(&choice, a->patch_arg,
3277 status, ie->path, NULL, 1, 1);
3278 if (err)
3279 goto done;
3280 if (choice != GOT_PATCH_CHOICE_YES)
3281 break;
3283 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3284 ie->path);
3285 if (err)
3286 goto done;
3287 got_fileindex_entry_remove(a->fileindex, ie);
3288 break;
3289 case GOT_STATUS_DELETE:
3290 if (a->patch_cb) {
3291 int choice = GOT_PATCH_CHOICE_NONE;
3292 err = (*a->patch_cb)(&choice, a->patch_arg,
3293 status, ie->path, NULL, 1, 1);
3294 if (err)
3295 goto done;
3296 if (choice != GOT_PATCH_CHOICE_YES)
3297 break;
3299 /* fall through */
3300 case GOT_STATUS_MODIFY:
3301 case GOT_STATUS_CONFLICT:
3302 case GOT_STATUS_MISSING: {
3303 struct got_object_id id;
3304 if (staged_status == GOT_STATUS_ADD ||
3305 staged_status == GOT_STATUS_MODIFY) {
3306 memcpy(id.sha1, ie->staged_blob_sha1,
3307 SHA1_DIGEST_LENGTH);
3308 } else
3309 memcpy(id.sha1, ie->blob_sha1,
3310 SHA1_DIGEST_LENGTH);
3311 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3312 if (err)
3313 goto done;
3315 if (asprintf(&ondisk_path, "%s/%s",
3316 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3317 err = got_error_from_errno("asprintf");
3318 goto done;
3321 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3322 status == GOT_STATUS_CONFLICT)) {
3323 err = create_patched_content(&path_content, 1, &id,
3324 ondisk_path, ie->path, a->repo,
3325 a->patch_cb, a->patch_arg);
3326 if (err || path_content == NULL)
3327 break;
3328 if (rename(path_content, ondisk_path) == -1) {
3329 err = got_error_from_errno3("rename",
3330 path_content, ondisk_path);
3331 goto done;
3333 } else {
3334 err = install_blob(a->worktree, ondisk_path, ie->path,
3335 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3336 got_fileindex_perms_to_st(ie), blob, 0, 1,
3337 a->repo, a->progress_cb, a->progress_arg);
3338 if (err)
3339 goto done;
3340 if (status == GOT_STATUS_DELETE) {
3341 err = update_blob_fileindex_entry(a->worktree,
3342 a->fileindex, ie, ondisk_path, ie->path,
3343 blob, 1);
3344 if (err)
3345 goto done;
3348 break;
3350 default:
3351 break;
3353 done:
3354 free(ondisk_path);
3355 free(path_content);
3356 free(parent_path);
3357 free(tree_path);
3358 if (blob)
3359 got_object_blob_close(blob);
3360 if (tree)
3361 got_object_tree_close(tree);
3362 free(tree_id);
3363 return err;
3366 const struct got_error *
3367 got_worktree_revert(struct got_worktree *worktree,
3368 struct got_pathlist_head *paths,
3369 got_worktree_checkout_cb progress_cb, void *progress_arg,
3370 got_worktree_patch_cb patch_cb, void *patch_arg,
3371 struct got_repository *repo)
3373 struct got_fileindex *fileindex = NULL;
3374 char *fileindex_path = NULL;
3375 const struct got_error *err = NULL, *unlockerr = NULL;
3376 const struct got_error *sync_err = NULL;
3377 struct got_pathlist_entry *pe;
3378 struct revert_file_args rfa;
3380 err = lock_worktree(worktree, LOCK_EX);
3381 if (err)
3382 return err;
3384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3385 if (err)
3386 goto done;
3388 rfa.worktree = worktree;
3389 rfa.fileindex = fileindex;
3390 rfa.progress_cb = progress_cb;
3391 rfa.progress_arg = progress_arg;
3392 rfa.patch_cb = patch_cb;
3393 rfa.patch_arg = patch_arg;
3394 rfa.repo = repo;
3395 TAILQ_FOREACH(pe, paths, entry) {
3396 err = worktree_status(worktree, pe->path, fileindex, repo,
3397 revert_file, &rfa, NULL, NULL);
3398 if (err)
3399 break;
3401 sync_err = sync_fileindex(fileindex, fileindex_path);
3402 if (sync_err && err == NULL)
3403 err = sync_err;
3404 done:
3405 free(fileindex_path);
3406 if (fileindex)
3407 got_fileindex_free(fileindex);
3408 unlockerr = lock_worktree(worktree, LOCK_SH);
3409 if (unlockerr && err == NULL)
3410 err = unlockerr;
3411 return err;
3414 static void
3415 free_commitable(struct got_commitable *ct)
3417 free(ct->path);
3418 free(ct->in_repo_path);
3419 free(ct->ondisk_path);
3420 free(ct->blob_id);
3421 free(ct->base_blob_id);
3422 free(ct->staged_blob_id);
3423 free(ct->base_commit_id);
3424 free(ct);
3427 struct collect_commitables_arg {
3428 struct got_pathlist_head *commitable_paths;
3429 struct got_repository *repo;
3430 struct got_worktree *worktree;
3431 int have_staged_files;
3434 static const struct got_error *
3435 collect_commitables(void *arg, unsigned char status,
3436 unsigned char staged_status, const char *relpath,
3437 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3438 struct got_object_id *commit_id)
3440 struct collect_commitables_arg *a = arg;
3441 const struct got_error *err = NULL;
3442 struct got_commitable *ct = NULL;
3443 struct got_pathlist_entry *new = NULL;
3444 char *parent_path = NULL, *path = NULL;
3445 struct stat sb;
3447 if (a->have_staged_files) {
3448 if (staged_status != GOT_STATUS_MODIFY &&
3449 staged_status != GOT_STATUS_ADD &&
3450 staged_status != GOT_STATUS_DELETE)
3451 return NULL;
3452 } else {
3453 if (status == GOT_STATUS_CONFLICT)
3454 return got_error(GOT_ERR_COMMIT_CONFLICT);
3456 if (status != GOT_STATUS_MODIFY &&
3457 status != GOT_STATUS_ADD &&
3458 status != GOT_STATUS_DELETE)
3459 return NULL;
3462 if (asprintf(&path, "/%s", relpath) == -1) {
3463 err = got_error_from_errno("asprintf");
3464 goto done;
3466 if (strcmp(path, "/") == 0) {
3467 parent_path = strdup("");
3468 if (parent_path == NULL)
3469 return got_error_from_errno("strdup");
3470 } else {
3471 err = got_path_dirname(&parent_path, path);
3472 if (err)
3473 return err;
3476 ct = calloc(1, sizeof(*ct));
3477 if (ct == NULL) {
3478 err = got_error_from_errno("calloc");
3479 goto done;
3482 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3483 relpath) == -1) {
3484 err = got_error_from_errno("asprintf");
3485 goto done;
3487 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3488 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3489 } else {
3490 if (lstat(ct->ondisk_path, &sb) != 0) {
3491 err = got_error_from_errno2("lstat", ct->ondisk_path);
3492 goto done;
3494 ct->mode = sb.st_mode;
3497 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3498 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3499 relpath) == -1) {
3500 err = got_error_from_errno("asprintf");
3501 goto done;
3504 ct->status = status;
3505 ct->staged_status = staged_status;
3506 ct->blob_id = NULL; /* will be filled in when blob gets created */
3507 if (ct->status != GOT_STATUS_ADD &&
3508 ct->staged_status != GOT_STATUS_ADD) {
3509 ct->base_blob_id = got_object_id_dup(blob_id);
3510 if (ct->base_blob_id == NULL) {
3511 err = got_error_from_errno("got_object_id_dup");
3512 goto done;
3514 ct->base_commit_id = got_object_id_dup(commit_id);
3515 if (ct->base_commit_id == NULL) {
3516 err = got_error_from_errno("got_object_id_dup");
3517 goto done;
3520 if (ct->staged_status == GOT_STATUS_ADD ||
3521 ct->staged_status == GOT_STATUS_MODIFY) {
3522 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3523 if (ct->staged_blob_id == NULL) {
3524 err = got_error_from_errno("got_object_id_dup");
3525 goto done;
3528 ct->path = strdup(path);
3529 if (ct->path == NULL) {
3530 err = got_error_from_errno("strdup");
3531 goto done;
3533 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3534 done:
3535 if (ct && (err || new == NULL))
3536 free_commitable(ct);
3537 free(parent_path);
3538 free(path);
3539 return err;
3542 static const struct got_error *write_tree(struct got_object_id **,
3543 struct got_tree_object *, const char *, struct got_pathlist_head *,
3544 got_worktree_status_cb status_cb, void *status_arg,
3545 struct got_repository *);
3547 static const struct got_error *
3548 write_subtree(struct got_object_id **new_subtree_id,
3549 struct got_tree_entry *te, const char *parent_path,
3550 struct got_pathlist_head *commitable_paths,
3551 got_worktree_status_cb status_cb, void *status_arg,
3552 struct got_repository *repo)
3554 const struct got_error *err = NULL;
3555 struct got_tree_object *subtree;
3556 char *subpath;
3558 if (asprintf(&subpath, "%s%s%s", parent_path,
3559 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3560 return got_error_from_errno("asprintf");
3562 err = got_object_open_as_tree(&subtree, repo, te->id);
3563 if (err)
3564 return err;
3566 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3567 status_cb, status_arg, repo);
3568 got_object_tree_close(subtree);
3569 free(subpath);
3570 return err;
3573 static const struct got_error *
3574 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3576 const struct got_error *err = NULL;
3577 char *ct_parent_path = NULL;
3579 *match = 0;
3581 if (strchr(ct->in_repo_path, '/') == NULL) {
3582 *match = got_path_is_root_dir(path);
3583 return NULL;
3586 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3587 if (err)
3588 return err;
3589 *match = (strcmp(path, ct_parent_path) == 0);
3590 free(ct_parent_path);
3591 return err;
3594 static mode_t
3595 get_ct_file_mode(struct got_commitable *ct)
3597 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3600 static const struct got_error *
3601 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3602 struct got_tree_entry *te, struct got_commitable *ct)
3604 const struct got_error *err = NULL;
3606 *new_te = NULL;
3608 err = got_object_tree_entry_dup(new_te, te);
3609 if (err)
3610 goto done;
3612 (*new_te)->mode = get_ct_file_mode(ct);
3614 free((*new_te)->id);
3615 if (ct->staged_status == GOT_STATUS_MODIFY)
3616 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3617 else
3618 (*new_te)->id = got_object_id_dup(ct->blob_id);
3619 if ((*new_te)->id == NULL) {
3620 err = got_error_from_errno("got_object_id_dup");
3621 goto done;
3623 done:
3624 if (err && *new_te) {
3625 got_object_tree_entry_close(*new_te);
3626 *new_te = NULL;
3628 return err;
3631 static const struct got_error *
3632 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3633 struct got_commitable *ct)
3635 const struct got_error *err = NULL;
3636 char *ct_name;
3638 *new_te = NULL;
3640 *new_te = calloc(1, sizeof(**new_te));
3641 if (*new_te == NULL)
3642 return got_error_from_errno("calloc");
3644 ct_name = basename(ct->path);
3645 if (ct_name == NULL) {
3646 err = got_error_from_errno2("basename", ct->path);
3647 goto done;
3649 (*new_te)->name = strdup(ct_name);
3650 if ((*new_te)->name == NULL) {
3651 err = got_error_from_errno("strdup");
3652 goto done;
3655 (*new_te)->mode = get_ct_file_mode(ct);
3657 if (ct->staged_status == GOT_STATUS_ADD)
3658 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3659 else
3660 (*new_te)->id = got_object_id_dup(ct->blob_id);
3661 if ((*new_te)->id == NULL) {
3662 err = got_error_from_errno("got_object_id_dup");
3663 goto done;
3665 done:
3666 if (err && *new_te) {
3667 got_object_tree_entry_close(*new_te);
3668 *new_te = NULL;
3670 return err;
3673 static const struct got_error *
3674 insert_tree_entry(struct got_tree_entry *new_te,
3675 struct got_pathlist_head *paths)
3677 const struct got_error *err = NULL;
3678 struct got_pathlist_entry *new_pe;
3680 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3681 if (err)
3682 return err;
3683 if (new_pe == NULL)
3684 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3685 return NULL;
3688 static const struct got_error *
3689 report_ct_status(struct got_commitable *ct,
3690 got_worktree_status_cb status_cb, void *status_arg)
3692 const char *ct_path = ct->path;
3693 unsigned char status;
3695 while (ct_path[0] == '/')
3696 ct_path++;
3698 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3699 status = ct->staged_status;
3700 else
3701 status = ct->status;
3703 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3704 ct_path, ct->blob_id, NULL, NULL);
3707 static const struct got_error *
3708 match_modified_subtree(int *modified, struct got_tree_entry *te,
3709 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3711 const struct got_error *err = NULL;
3712 struct got_pathlist_entry *pe;
3713 char *te_path;
3715 *modified = 0;
3717 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3718 got_path_is_root_dir(base_tree_path) ? "" : "/",
3719 te->name) == -1)
3720 return got_error_from_errno("asprintf");
3722 TAILQ_FOREACH(pe, commitable_paths, entry) {
3723 struct got_commitable *ct = pe->data;
3724 *modified = got_path_is_child(ct->in_repo_path, te_path,
3725 strlen(te_path));
3726 if (*modified)
3727 break;
3730 free(te_path);
3731 return err;
3734 static const struct got_error *
3735 match_deleted_or_modified_ct(struct got_commitable **ctp,
3736 struct got_tree_entry *te, const char *base_tree_path,
3737 struct got_pathlist_head *commitable_paths)
3739 const struct got_error *err = NULL;
3740 struct got_pathlist_entry *pe;
3742 *ctp = NULL;
3744 TAILQ_FOREACH(pe, commitable_paths, entry) {
3745 struct got_commitable *ct = pe->data;
3746 char *ct_name = NULL;
3747 int path_matches;
3749 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3750 if (ct->status != GOT_STATUS_MODIFY &&
3751 ct->status != GOT_STATUS_DELETE)
3752 continue;
3753 } else {
3754 if (ct->staged_status != GOT_STATUS_MODIFY &&
3755 ct->staged_status != GOT_STATUS_DELETE)
3756 continue;
3759 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3760 continue;
3762 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3763 if (err)
3764 return err;
3765 if (!path_matches)
3766 continue;
3768 ct_name = basename(pe->path);
3769 if (ct_name == NULL)
3770 return got_error_from_errno2("basename", pe->path);
3772 if (strcmp(te->name, ct_name) != 0)
3773 continue;
3775 *ctp = ct;
3776 break;
3779 return err;
3782 static const struct got_error *
3783 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3784 const char *child_path, const char *path_base_tree,
3785 struct got_pathlist_head *commitable_paths,
3786 got_worktree_status_cb status_cb, void *status_arg,
3787 struct got_repository *repo)
3789 const struct got_error *err = NULL;
3790 struct got_tree_entry *new_te;
3791 char *subtree_path;
3793 *new_tep = NULL;
3795 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3796 got_path_is_root_dir(path_base_tree) ? "" : "/",
3797 child_path) == -1)
3798 return got_error_from_errno("asprintf");
3800 new_te = calloc(1, sizeof(*new_te));
3801 if (new_te == NULL)
3802 return got_error_from_errno("calloc");
3803 new_te->mode = S_IFDIR;
3804 new_te->name = strdup(child_path);
3805 if (new_te->name == NULL) {
3806 err = got_error_from_errno("strdup");
3807 got_object_tree_entry_close(new_te);
3808 goto done;
3810 err = write_tree(&new_te->id, NULL, subtree_path,
3811 commitable_paths, status_cb, status_arg, repo);
3812 if (err) {
3813 got_object_tree_entry_close(new_te);
3814 goto done;
3816 done:
3817 free(subtree_path);
3818 if (err == NULL)
3819 *new_tep = new_te;
3820 return err;
3823 static const struct got_error *
3824 write_tree(struct got_object_id **new_tree_id,
3825 struct got_tree_object *base_tree, const char *path_base_tree,
3826 struct got_pathlist_head *commitable_paths,
3827 got_worktree_status_cb status_cb, void *status_arg,
3828 struct got_repository *repo)
3830 const struct got_error *err = NULL;
3831 const struct got_tree_entries *base_entries = NULL;
3832 struct got_pathlist_head paths;
3833 struct got_tree_entries new_tree_entries;
3834 struct got_tree_entry *te, *new_te = NULL;
3835 struct got_pathlist_entry *pe;
3837 TAILQ_INIT(&paths);
3838 new_tree_entries.nentries = 0;
3839 SIMPLEQ_INIT(&new_tree_entries.head);
3841 /* Insert, and recurse into, newly added entries first. */
3842 TAILQ_FOREACH(pe, commitable_paths, entry) {
3843 struct got_commitable *ct = pe->data;
3844 char *child_path = NULL, *slash;
3846 if ((ct->status != GOT_STATUS_ADD &&
3847 ct->staged_status != GOT_STATUS_ADD) ||
3848 (ct->flags & GOT_COMMITABLE_ADDED))
3849 continue;
3851 if (!got_path_is_child(pe->path, path_base_tree,
3852 strlen(path_base_tree)))
3853 continue;
3855 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3856 pe->path);
3857 if (err)
3858 goto done;
3860 slash = strchr(child_path, '/');
3861 if (slash == NULL) {
3862 err = alloc_added_blob_tree_entry(&new_te, ct);
3863 if (err)
3864 goto done;
3865 err = report_ct_status(ct, status_cb, status_arg);
3866 if (err)
3867 goto done;
3868 ct->flags |= GOT_COMMITABLE_ADDED;
3869 err = insert_tree_entry(new_te, &paths);
3870 if (err)
3871 goto done;
3872 } else {
3873 *slash = '\0'; /* trim trailing path components */
3874 if (base_tree == NULL ||
3875 got_object_tree_find_entry(base_tree, child_path)
3876 == NULL) {
3877 err = make_subtree_for_added_blob(&new_te,
3878 child_path, path_base_tree,
3879 commitable_paths, status_cb, status_arg,
3880 repo);
3881 if (err)
3882 goto done;
3883 err = insert_tree_entry(new_te, &paths);
3884 if (err)
3885 goto done;
3890 if (base_tree) {
3891 /* Handle modified and deleted entries. */
3892 base_entries = got_object_tree_get_entries(base_tree);
3893 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3894 struct got_commitable *ct = NULL;
3896 if (got_object_tree_entry_is_submodule(te)) {
3897 /* Entry is a submodule; just copy it. */
3898 err = got_object_tree_entry_dup(&new_te, te);
3899 if (err)
3900 goto done;
3901 err = insert_tree_entry(new_te, &paths);
3902 if (err)
3903 goto done;
3904 continue;
3907 if (S_ISDIR(te->mode)) {
3908 int modified;
3909 err = got_object_tree_entry_dup(&new_te, te);
3910 if (err)
3911 goto done;
3912 err = match_modified_subtree(&modified, te,
3913 path_base_tree, commitable_paths);
3914 if (err)
3915 goto done;
3916 /* Avoid recursion into unmodified subtrees. */
3917 if (modified) {
3918 free(new_te->id);
3919 err = write_subtree(&new_te->id, te,
3920 path_base_tree, commitable_paths,
3921 status_cb, status_arg, repo);
3922 if (err)
3923 goto done;
3925 err = insert_tree_entry(new_te, &paths);
3926 if (err)
3927 goto done;
3928 continue;
3931 err = match_deleted_or_modified_ct(&ct, te,
3932 path_base_tree, commitable_paths);
3933 if (err)
3934 goto done;
3935 if (ct) {
3936 /* NB: Deleted entries get dropped here. */
3937 if (ct->status == GOT_STATUS_MODIFY ||
3938 ct->staged_status == GOT_STATUS_MODIFY) {
3939 err = alloc_modified_blob_tree_entry(
3940 &new_te, te, ct);
3941 if (err)
3942 goto done;
3943 err = insert_tree_entry(new_te, &paths);
3944 if (err)
3945 goto done;
3947 err = report_ct_status(ct, status_cb,
3948 status_arg);
3949 if (err)
3950 goto done;
3951 } else {
3952 /* Entry is unchanged; just copy it. */
3953 err = got_object_tree_entry_dup(&new_te, te);
3954 if (err)
3955 goto done;
3956 err = insert_tree_entry(new_te, &paths);
3957 if (err)
3958 goto done;
3963 /* Write new list of entries; deleted entries have been dropped. */
3964 TAILQ_FOREACH(pe, &paths, entry) {
3965 struct got_tree_entry *te = pe->data;
3966 new_tree_entries.nentries++;
3967 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3969 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3970 done:
3971 got_object_tree_entries_close(&new_tree_entries);
3972 got_pathlist_free(&paths);
3973 return err;
3976 static const struct got_error *
3977 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3978 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3979 int have_staged_files)
3981 const struct got_error *err = NULL;
3982 struct got_pathlist_entry *pe;
3984 TAILQ_FOREACH(pe, commitable_paths, entry) {
3985 struct got_fileindex_entry *ie;
3986 struct got_commitable *ct = pe->data;
3988 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3989 if (ie) {
3990 if (ct->status == GOT_STATUS_DELETE ||
3991 ct->staged_status == GOT_STATUS_DELETE) {
3992 got_fileindex_entry_remove(fileindex, ie);
3993 got_fileindex_entry_free(ie);
3994 } else if (ct->staged_status == GOT_STATUS_ADD ||
3995 ct->staged_status == GOT_STATUS_MODIFY) {
3996 got_fileindex_entry_stage_set(ie,
3997 GOT_FILEIDX_STAGE_NONE);
3998 err = got_fileindex_entry_update(ie,
3999 ct->ondisk_path, ct->staged_blob_id->sha1,
4000 new_base_commit_id->sha1,
4001 !have_staged_files);
4002 } else
4003 err = got_fileindex_entry_update(ie,
4004 ct->ondisk_path, ct->blob_id->sha1,
4005 new_base_commit_id->sha1,
4006 !have_staged_files);
4007 } else {
4008 err = got_fileindex_entry_alloc(&ie,
4009 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4010 new_base_commit_id->sha1);
4011 if (err)
4012 break;
4013 err = got_fileindex_entry_add(fileindex, ie);
4014 if (err)
4015 break;
4018 return err;
4022 static const struct got_error *
4023 check_out_of_date(const char *in_repo_path, unsigned char status,
4024 unsigned char staged_status, struct got_object_id *base_blob_id,
4025 struct got_object_id *base_commit_id,
4026 struct got_object_id *head_commit_id, struct got_repository *repo,
4027 int ood_errcode)
4029 const struct got_error *err = NULL;
4030 struct got_object_id *id = NULL;
4032 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4033 /* Trivial case: base commit == head commit */
4034 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4035 return NULL;
4037 * Ensure file content which local changes were based
4038 * on matches file content in the branch head.
4040 err = got_object_id_by_path(&id, repo, head_commit_id,
4041 in_repo_path);
4042 if (err) {
4043 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4044 err = got_error(ood_errcode);
4045 goto done;
4046 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4047 err = got_error(ood_errcode);
4048 } else {
4049 /* Require that added files don't exist in the branch head. */
4050 err = got_object_id_by_path(&id, repo, head_commit_id,
4051 in_repo_path);
4052 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4053 goto done;
4054 err = id ? got_error(ood_errcode) : NULL;
4056 done:
4057 free(id);
4058 return err;
4061 const struct got_error *
4062 commit_worktree(struct got_object_id **new_commit_id,
4063 struct got_pathlist_head *commitable_paths,
4064 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4065 const char *author, const char *committer,
4066 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4067 got_worktree_status_cb status_cb, void *status_arg,
4068 struct got_repository *repo)
4070 const struct got_error *err = NULL, *unlockerr = NULL;
4071 struct got_pathlist_entry *pe;
4072 const char *head_ref_name = NULL;
4073 struct got_commit_object *head_commit = NULL;
4074 struct got_reference *head_ref2 = NULL;
4075 struct got_object_id *head_commit_id2 = NULL;
4076 struct got_tree_object *head_tree = NULL;
4077 struct got_object_id *new_tree_id = NULL;
4078 struct got_object_id_queue parent_ids;
4079 struct got_object_qid *pid = NULL;
4080 char *logmsg = NULL;
4082 *new_commit_id = NULL;
4084 SIMPLEQ_INIT(&parent_ids);
4086 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4087 if (err)
4088 goto done;
4090 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4091 if (err)
4092 goto done;
4094 if (commit_msg_cb != NULL) {
4095 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4096 if (err)
4097 goto done;
4100 if (logmsg == NULL || strlen(logmsg) == 0) {
4101 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4102 goto done;
4105 /* Create blobs from added and modified files and record their IDs. */
4106 TAILQ_FOREACH(pe, commitable_paths, entry) {
4107 struct got_commitable *ct = pe->data;
4108 char *ondisk_path;
4110 /* Blobs for staged files already exist. */
4111 if (ct->staged_status == GOT_STATUS_ADD ||
4112 ct->staged_status == GOT_STATUS_MODIFY)
4113 continue;
4115 if (ct->status != GOT_STATUS_ADD &&
4116 ct->status != GOT_STATUS_MODIFY)
4117 continue;
4119 if (asprintf(&ondisk_path, "%s/%s",
4120 worktree->root_path, pe->path) == -1) {
4121 err = got_error_from_errno("asprintf");
4122 goto done;
4124 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4125 free(ondisk_path);
4126 if (err)
4127 goto done;
4130 /* Recursively write new tree objects. */
4131 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4132 status_cb, status_arg, repo);
4133 if (err)
4134 goto done;
4136 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4137 if (err)
4138 goto done;
4139 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4140 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4141 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4142 got_object_qid_free(pid);
4143 if (logmsg != NULL)
4144 free(logmsg);
4145 if (err)
4146 goto done;
4148 /* Check if a concurrent commit to our branch has occurred. */
4149 head_ref_name = got_worktree_get_head_ref_name(worktree);
4150 if (head_ref_name == NULL) {
4151 err = got_error_from_errno("got_worktree_get_head_ref_name");
4152 goto done;
4154 /* Lock the reference here to prevent concurrent modification. */
4155 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4156 if (err)
4157 goto done;
4158 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4159 if (err)
4160 goto done;
4161 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4162 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4163 goto done;
4165 /* Update branch head in repository. */
4166 err = got_ref_change_ref(head_ref2, *new_commit_id);
4167 if (err)
4168 goto done;
4169 err = got_ref_write(head_ref2, repo);
4170 if (err)
4171 goto done;
4173 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4174 if (err)
4175 goto done;
4177 err = ref_base_commit(worktree, repo);
4178 if (err)
4179 goto done;
4180 done:
4181 if (head_tree)
4182 got_object_tree_close(head_tree);
4183 if (head_commit)
4184 got_object_commit_close(head_commit);
4185 free(head_commit_id2);
4186 if (head_ref2) {
4187 unlockerr = got_ref_unlock(head_ref2);
4188 if (unlockerr && err == NULL)
4189 err = unlockerr;
4190 got_ref_close(head_ref2);
4192 return err;
4195 static const struct got_error *
4196 check_path_is_commitable(const char *path,
4197 struct got_pathlist_head *commitable_paths)
4199 struct got_pathlist_entry *cpe = NULL;
4200 size_t path_len = strlen(path);
4202 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4203 struct got_commitable *ct = cpe->data;
4204 const char *ct_path = ct->path;
4206 while (ct_path[0] == '/')
4207 ct_path++;
4209 if (strcmp(path, ct_path) == 0 ||
4210 got_path_is_child(ct_path, path, path_len))
4211 break;
4214 if (cpe == NULL)
4215 return got_error_path(path, GOT_ERR_BAD_PATH);
4217 return NULL;
4220 static const struct got_error *
4221 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4223 int *have_staged_files = arg;
4225 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4226 *have_staged_files = 1;
4227 return got_error(GOT_ERR_CANCELLED);
4230 return NULL;
4233 static const struct got_error *
4234 check_non_staged_files(struct got_fileindex *fileindex,
4235 struct got_pathlist_head *paths)
4237 struct got_pathlist_entry *pe;
4238 struct got_fileindex_entry *ie;
4240 TAILQ_FOREACH(pe, paths, entry) {
4241 if (pe->path[0] == '\0')
4242 continue;
4243 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4244 if (ie == NULL)
4245 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4246 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4247 return got_error_path(pe->path,
4248 GOT_ERR_FILE_NOT_STAGED);
4251 return NULL;
4254 const struct got_error *
4255 got_worktree_commit(struct got_object_id **new_commit_id,
4256 struct got_worktree *worktree, struct got_pathlist_head *paths,
4257 const char *author, const char *committer,
4258 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4259 got_worktree_status_cb status_cb, void *status_arg,
4260 struct got_repository *repo)
4262 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4263 struct got_fileindex *fileindex = NULL;
4264 char *fileindex_path = NULL;
4265 struct got_pathlist_head commitable_paths;
4266 struct collect_commitables_arg cc_arg;
4267 struct got_pathlist_entry *pe;
4268 struct got_reference *head_ref = NULL;
4269 struct got_object_id *head_commit_id = NULL;
4270 int have_staged_files = 0;
4272 *new_commit_id = NULL;
4274 TAILQ_INIT(&commitable_paths);
4276 err = lock_worktree(worktree, LOCK_EX);
4277 if (err)
4278 goto done;
4280 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4281 if (err)
4282 goto done;
4284 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4285 if (err)
4286 goto done;
4288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4289 if (err)
4290 goto done;
4292 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4293 &have_staged_files);
4294 if (err && err->code != GOT_ERR_CANCELLED)
4295 goto done;
4296 if (have_staged_files) {
4297 err = check_non_staged_files(fileindex, paths);
4298 if (err)
4299 goto done;
4302 cc_arg.commitable_paths = &commitable_paths;
4303 cc_arg.worktree = worktree;
4304 cc_arg.repo = repo;
4305 cc_arg.have_staged_files = have_staged_files;
4306 TAILQ_FOREACH(pe, paths, entry) {
4307 err = worktree_status(worktree, pe->path, fileindex, repo,
4308 collect_commitables, &cc_arg, NULL, NULL);
4309 if (err)
4310 goto done;
4313 if (TAILQ_EMPTY(&commitable_paths)) {
4314 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4315 goto done;
4318 TAILQ_FOREACH(pe, paths, entry) {
4319 err = check_path_is_commitable(pe->path, &commitable_paths);
4320 if (err)
4321 goto done;
4324 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4325 struct got_commitable *ct = pe->data;
4326 const char *ct_path = ct->in_repo_path;
4328 while (ct_path[0] == '/')
4329 ct_path++;
4330 err = check_out_of_date(ct_path, ct->status,
4331 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4332 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4333 if (err)
4334 goto done;
4338 err = commit_worktree(new_commit_id, &commitable_paths,
4339 head_commit_id, worktree, author, committer,
4340 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4341 if (err)
4342 goto done;
4344 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4345 fileindex, have_staged_files);
4346 sync_err = sync_fileindex(fileindex, fileindex_path);
4347 if (sync_err && err == NULL)
4348 err = sync_err;
4349 done:
4350 if (fileindex)
4351 got_fileindex_free(fileindex);
4352 free(fileindex_path);
4353 unlockerr = lock_worktree(worktree, LOCK_SH);
4354 if (unlockerr && err == NULL)
4355 err = unlockerr;
4356 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4357 struct got_commitable *ct = pe->data;
4358 free_commitable(ct);
4360 got_pathlist_free(&commitable_paths);
4361 return err;
4364 const char *
4365 got_commitable_get_path(struct got_commitable *ct)
4367 return ct->path;
4370 unsigned int
4371 got_commitable_get_status(struct got_commitable *ct)
4373 return ct->status;
4376 struct check_rebase_ok_arg {
4377 struct got_worktree *worktree;
4378 struct got_repository *repo;
4381 static const struct got_error *
4382 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4384 const struct got_error *err = NULL;
4385 struct check_rebase_ok_arg *a = arg;
4386 unsigned char status;
4387 struct stat sb;
4388 char *ondisk_path;
4390 /* Reject rebase of a work tree with mixed base commits. */
4391 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4392 SHA1_DIGEST_LENGTH))
4393 return got_error(GOT_ERR_MIXED_COMMITS);
4395 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4396 == -1)
4397 return got_error_from_errno("asprintf");
4399 /* Reject rebase of a work tree with modified or staged files. */
4400 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4401 free(ondisk_path);
4402 if (err)
4403 return err;
4405 if (status != GOT_STATUS_NO_CHANGE)
4406 return got_error(GOT_ERR_MODIFIED);
4407 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4408 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4410 return NULL;
4413 const struct got_error *
4414 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4415 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4416 struct got_worktree *worktree, struct got_reference *branch,
4417 struct got_repository *repo)
4419 const struct got_error *err = NULL;
4420 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4421 char *branch_ref_name = NULL;
4422 char *fileindex_path = NULL;
4423 struct check_rebase_ok_arg ok_arg;
4424 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4426 *new_base_branch_ref = NULL;
4427 *tmp_branch = NULL;
4428 *fileindex = NULL;
4430 err = lock_worktree(worktree, LOCK_EX);
4431 if (err)
4432 return err;
4434 err = open_fileindex(fileindex, &fileindex_path, worktree);
4435 if (err)
4436 goto done;
4438 ok_arg.worktree = worktree;
4439 ok_arg.repo = repo;
4440 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4441 &ok_arg);
4442 if (err)
4443 goto done;
4445 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4446 if (err)
4447 goto done;
4449 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4450 if (err)
4451 goto done;
4453 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4454 if (err)
4455 goto done;
4457 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4458 0);
4459 if (err)
4460 goto done;
4462 err = got_ref_alloc_symref(new_base_branch_ref,
4463 new_base_branch_ref_name, wt_branch);
4464 if (err)
4465 goto done;
4466 err = got_ref_write(*new_base_branch_ref, repo);
4467 if (err)
4468 goto done;
4470 /* TODO Lock original branch's ref while rebasing? */
4472 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4473 if (err)
4474 goto done;
4476 err = got_ref_write(branch_ref, repo);
4477 if (err)
4478 goto done;
4480 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4481 worktree->base_commit_id);
4482 if (err)
4483 goto done;
4484 err = got_ref_write(*tmp_branch, repo);
4485 if (err)
4486 goto done;
4488 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4489 if (err)
4490 goto done;
4491 done:
4492 free(fileindex_path);
4493 free(tmp_branch_name);
4494 free(new_base_branch_ref_name);
4495 free(branch_ref_name);
4496 if (branch_ref)
4497 got_ref_close(branch_ref);
4498 if (wt_branch)
4499 got_ref_close(wt_branch);
4500 if (err) {
4501 if (*new_base_branch_ref) {
4502 got_ref_close(*new_base_branch_ref);
4503 *new_base_branch_ref = NULL;
4505 if (*tmp_branch) {
4506 got_ref_close(*tmp_branch);
4507 *tmp_branch = NULL;
4509 if (*fileindex) {
4510 got_fileindex_free(*fileindex);
4511 *fileindex = NULL;
4513 lock_worktree(worktree, LOCK_SH);
4515 return err;
4518 const struct got_error *
4519 got_worktree_rebase_continue(struct got_object_id **commit_id,
4520 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4521 struct got_reference **branch, struct got_fileindex **fileindex,
4522 struct got_worktree *worktree, struct got_repository *repo)
4524 const struct got_error *err;
4525 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4526 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4527 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4528 char *fileindex_path = NULL;
4529 int have_staged_files = 0;
4531 *commit_id = NULL;
4532 *new_base_branch = NULL;
4533 *tmp_branch = NULL;
4534 *branch = NULL;
4535 *fileindex = NULL;
4537 err = lock_worktree(worktree, LOCK_EX);
4538 if (err)
4539 return err;
4541 err = open_fileindex(fileindex, &fileindex_path, worktree);
4542 if (err)
4543 goto done;
4545 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4546 &have_staged_files);
4547 if (err && err->code != GOT_ERR_CANCELLED)
4548 goto done;
4549 if (have_staged_files) {
4550 err = got_error(GOT_ERR_STAGED_PATHS);
4551 goto done;
4554 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4555 if (err)
4556 goto done;
4558 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4559 if (err)
4560 goto done;
4562 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4563 if (err)
4564 goto done;
4566 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4567 if (err)
4568 goto done;
4570 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4571 if (err)
4572 goto done;
4574 err = got_ref_open(branch, repo,
4575 got_ref_get_symref_target(branch_ref), 0);
4576 if (err)
4577 goto done;
4579 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4580 if (err)
4581 goto done;
4583 err = got_ref_resolve(commit_id, repo, commit_ref);
4584 if (err)
4585 goto done;
4587 err = got_ref_open(new_base_branch, repo,
4588 new_base_branch_ref_name, 0);
4589 if (err)
4590 goto done;
4592 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4593 if (err)
4594 goto done;
4595 done:
4596 free(commit_ref_name);
4597 free(branch_ref_name);
4598 free(fileindex_path);
4599 if (commit_ref)
4600 got_ref_close(commit_ref);
4601 if (branch_ref)
4602 got_ref_close(branch_ref);
4603 if (err) {
4604 free(*commit_id);
4605 *commit_id = NULL;
4606 if (*tmp_branch) {
4607 got_ref_close(*tmp_branch);
4608 *tmp_branch = NULL;
4610 if (*new_base_branch) {
4611 got_ref_close(*new_base_branch);
4612 *new_base_branch = NULL;
4614 if (*branch) {
4615 got_ref_close(*branch);
4616 *branch = NULL;
4618 if (*fileindex) {
4619 got_fileindex_free(*fileindex);
4620 *fileindex = NULL;
4622 lock_worktree(worktree, LOCK_SH);
4624 return err;
4627 const struct got_error *
4628 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4630 const struct got_error *err;
4631 char *tmp_branch_name = NULL;
4633 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4634 if (err)
4635 return err;
4637 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4638 free(tmp_branch_name);
4639 return NULL;
4642 static const struct got_error *
4643 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4644 char **logmsg, void *arg)
4646 *logmsg = arg;
4647 return NULL;
4650 static const struct got_error *
4651 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4652 const char *path, struct got_object_id *blob_id,
4653 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4655 return NULL;
4658 struct collect_merged_paths_arg {
4659 got_worktree_checkout_cb progress_cb;
4660 void *progress_arg;
4661 struct got_pathlist_head *merged_paths;
4664 static const struct got_error *
4665 collect_merged_paths(void *arg, unsigned char status, const char *path)
4667 const struct got_error *err;
4668 struct collect_merged_paths_arg *a = arg;
4669 char *p;
4670 struct got_pathlist_entry *new;
4672 err = (*a->progress_cb)(a->progress_arg, status, path);
4673 if (err)
4674 return err;
4676 if (status != GOT_STATUS_MERGE &&
4677 status != GOT_STATUS_ADD &&
4678 status != GOT_STATUS_DELETE &&
4679 status != GOT_STATUS_CONFLICT)
4680 return NULL;
4682 p = strdup(path);
4683 if (p == NULL)
4684 return got_error_from_errno("strdup");
4686 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4687 if (err || new == NULL)
4688 free(p);
4689 return err;
4692 void
4693 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4695 struct got_pathlist_entry *pe;
4697 TAILQ_FOREACH(pe, merged_paths, entry)
4698 free((char *)pe->path);
4700 got_pathlist_free(merged_paths);
4703 static const struct got_error *
4704 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4705 struct got_repository *repo)
4707 const struct got_error *err;
4708 struct got_reference *commit_ref = NULL;
4710 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4711 if (err) {
4712 if (err->code != GOT_ERR_NOT_REF)
4713 goto done;
4714 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4715 if (err)
4716 goto done;
4717 err = got_ref_write(commit_ref, repo);
4718 if (err)
4719 goto done;
4720 } else {
4721 struct got_object_id *stored_id;
4722 int cmp;
4724 err = got_ref_resolve(&stored_id, repo, commit_ref);
4725 if (err)
4726 goto done;
4727 cmp = got_object_id_cmp(commit_id, stored_id);
4728 free(stored_id);
4729 if (cmp != 0) {
4730 err = got_error(GOT_ERR_REBASE_COMMITID);
4731 goto done;
4734 done:
4735 if (commit_ref)
4736 got_ref_close(commit_ref);
4737 return err;
4740 static const struct got_error *
4741 rebase_merge_files(struct got_pathlist_head *merged_paths,
4742 const char *commit_ref_name, struct got_worktree *worktree,
4743 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4744 struct got_object_id *commit_id, struct got_repository *repo,
4745 got_worktree_checkout_cb progress_cb, void *progress_arg,
4746 got_cancel_cb cancel_cb, void *cancel_arg)
4748 const struct got_error *err;
4749 struct got_reference *commit_ref = NULL;
4750 struct collect_merged_paths_arg cmp_arg;
4751 char *fileindex_path;
4753 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4755 err = get_fileindex_path(&fileindex_path, worktree);
4756 if (err)
4757 return err;
4759 cmp_arg.progress_cb = progress_cb;
4760 cmp_arg.progress_arg = progress_arg;
4761 cmp_arg.merged_paths = merged_paths;
4762 err = merge_files(worktree, fileindex, fileindex_path,
4763 parent_commit_id, commit_id, repo, collect_merged_paths,
4764 &cmp_arg, cancel_cb, cancel_arg);
4765 if (commit_ref)
4766 got_ref_close(commit_ref);
4767 return err;
4770 const struct got_error *
4771 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4772 struct got_worktree *worktree, struct got_fileindex *fileindex,
4773 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4774 struct got_repository *repo,
4775 got_worktree_checkout_cb progress_cb, void *progress_arg,
4776 got_cancel_cb cancel_cb, void *cancel_arg)
4778 const struct got_error *err;
4779 char *commit_ref_name;
4781 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4782 if (err)
4783 return err;
4785 err = store_commit_id(commit_ref_name, commit_id, repo);
4786 if (err)
4787 goto done;
4789 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4790 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4791 progress_arg, cancel_cb, cancel_arg);
4792 done:
4793 free(commit_ref_name);
4794 return err;
4797 const struct got_error *
4798 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4799 struct got_worktree *worktree, struct got_fileindex *fileindex,
4800 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4801 struct got_repository *repo,
4802 got_worktree_checkout_cb progress_cb, void *progress_arg,
4803 got_cancel_cb cancel_cb, void *cancel_arg)
4805 const struct got_error *err;
4806 char *commit_ref_name;
4808 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4809 if (err)
4810 return err;
4812 err = store_commit_id(commit_ref_name, commit_id, repo);
4813 if (err)
4814 goto done;
4816 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4817 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4818 progress_arg, cancel_cb, cancel_arg);
4819 done:
4820 free(commit_ref_name);
4821 return err;
4824 static const struct got_error *
4825 rebase_commit(struct got_object_id **new_commit_id,
4826 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4827 struct got_worktree *worktree, struct got_fileindex *fileindex,
4828 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4829 const char *new_logmsg, struct got_repository *repo)
4831 const struct got_error *err, *sync_err;
4832 struct got_pathlist_head commitable_paths;
4833 struct collect_commitables_arg cc_arg;
4834 char *fileindex_path = NULL;
4835 struct got_reference *head_ref = NULL;
4836 struct got_object_id *head_commit_id = NULL;
4837 char *logmsg = NULL;
4839 TAILQ_INIT(&commitable_paths);
4840 *new_commit_id = NULL;
4842 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4844 err = get_fileindex_path(&fileindex_path, worktree);
4845 if (err)
4846 return err;
4848 cc_arg.commitable_paths = &commitable_paths;
4849 cc_arg.worktree = worktree;
4850 cc_arg.repo = repo;
4851 cc_arg.have_staged_files = 0;
4853 * If possible get the status of individual files directly to
4854 * avoid crawling the entire work tree once per rebased commit.
4855 * TODO: Ideally, merged_paths would contain a list of commitables
4856 * we could use so we could skip worktree_status() entirely.
4858 if (merged_paths) {
4859 struct got_pathlist_entry *pe;
4860 if (TAILQ_EMPTY(merged_paths)) {
4861 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4862 goto done;
4864 TAILQ_FOREACH(pe, merged_paths, entry) {
4865 err = worktree_status(worktree, pe->path, fileindex,
4866 repo, collect_commitables, &cc_arg, NULL, NULL);
4867 if (err)
4868 goto done;
4870 } else {
4871 err = worktree_status(worktree, "", fileindex, repo,
4872 collect_commitables, &cc_arg, NULL, NULL);
4873 if (err)
4874 goto done;
4877 if (TAILQ_EMPTY(&commitable_paths)) {
4878 /* No-op change; commit will be elided. */
4879 err = got_ref_delete(commit_ref, repo);
4880 if (err)
4881 goto done;
4882 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4883 goto done;
4886 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4887 if (err)
4888 goto done;
4890 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4891 if (err)
4892 goto done;
4894 if (new_logmsg) {
4895 logmsg = strdup(new_logmsg);
4896 if (logmsg == NULL) {
4897 err = got_error_from_errno("strdup");
4898 goto done;
4900 } else {
4901 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4902 if (err)
4903 goto done;
4906 /* NB: commit_worktree will call free(logmsg) */
4907 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4908 worktree, got_object_commit_get_author(orig_commit),
4909 got_object_commit_get_committer(orig_commit),
4910 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4911 if (err)
4912 goto done;
4914 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4915 if (err)
4916 goto done;
4918 err = got_ref_delete(commit_ref, repo);
4919 if (err)
4920 goto done;
4922 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4923 fileindex, 0);
4924 sync_err = sync_fileindex(fileindex, fileindex_path);
4925 if (sync_err && err == NULL)
4926 err = sync_err;
4927 done:
4928 free(fileindex_path);
4929 free(head_commit_id);
4930 if (head_ref)
4931 got_ref_close(head_ref);
4932 if (err) {
4933 free(*new_commit_id);
4934 *new_commit_id = NULL;
4936 return err;
4939 const struct got_error *
4940 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4941 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4942 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4943 struct got_commit_object *orig_commit,
4944 struct got_object_id *orig_commit_id, struct got_repository *repo)
4946 const struct got_error *err;
4947 char *commit_ref_name;
4948 struct got_reference *commit_ref = NULL;
4949 struct got_object_id *commit_id = NULL;
4951 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4952 if (err)
4953 return err;
4955 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4956 if (err)
4957 goto done;
4958 err = got_ref_resolve(&commit_id, repo, commit_ref);
4959 if (err)
4960 goto done;
4961 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4962 err = got_error(GOT_ERR_REBASE_COMMITID);
4963 goto done;
4966 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4967 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4968 done:
4969 if (commit_ref)
4970 got_ref_close(commit_ref);
4971 free(commit_ref_name);
4972 free(commit_id);
4973 return err;
4976 const struct got_error *
4977 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4978 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4979 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4980 struct got_commit_object *orig_commit,
4981 struct got_object_id *orig_commit_id, const char *new_logmsg,
4982 struct got_repository *repo)
4984 const struct got_error *err;
4985 char *commit_ref_name;
4986 struct got_reference *commit_ref = NULL;
4987 struct got_object_id *commit_id = NULL;
4989 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4990 if (err)
4991 return err;
4993 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4994 if (err)
4995 goto done;
4996 err = got_ref_resolve(&commit_id, repo, commit_ref);
4997 if (err)
4998 goto done;
4999 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5000 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5001 goto done;
5004 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5005 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5006 done:
5007 if (commit_ref)
5008 got_ref_close(commit_ref);
5009 free(commit_ref_name);
5010 free(commit_id);
5011 return err;
5014 const struct got_error *
5015 got_worktree_rebase_postpone(struct got_worktree *worktree,
5016 struct got_fileindex *fileindex)
5018 if (fileindex)
5019 got_fileindex_free(fileindex);
5020 return lock_worktree(worktree, LOCK_SH);
5023 static const struct got_error *
5024 delete_ref(const char *name, struct got_repository *repo)
5026 const struct got_error *err;
5027 struct got_reference *ref;
5029 err = got_ref_open(&ref, repo, name, 0);
5030 if (err) {
5031 if (err->code == GOT_ERR_NOT_REF)
5032 return NULL;
5033 return err;
5036 err = got_ref_delete(ref, repo);
5037 got_ref_close(ref);
5038 return err;
5041 static const struct got_error *
5042 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5044 const struct got_error *err;
5045 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5046 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5048 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5049 if (err)
5050 goto done;
5051 err = delete_ref(tmp_branch_name, repo);
5052 if (err)
5053 goto done;
5055 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5056 if (err)
5057 goto done;
5058 err = delete_ref(new_base_branch_ref_name, repo);
5059 if (err)
5060 goto done;
5062 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5063 if (err)
5064 goto done;
5065 err = delete_ref(branch_ref_name, repo);
5066 if (err)
5067 goto done;
5069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5070 if (err)
5071 goto done;
5072 err = delete_ref(commit_ref_name, repo);
5073 if (err)
5074 goto done;
5076 done:
5077 free(tmp_branch_name);
5078 free(new_base_branch_ref_name);
5079 free(branch_ref_name);
5080 free(commit_ref_name);
5081 return err;
5084 const struct got_error *
5085 got_worktree_rebase_complete(struct got_worktree *worktree,
5086 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5087 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5088 struct got_repository *repo)
5090 const struct got_error *err, *unlockerr;
5091 struct got_object_id *new_head_commit_id = NULL;
5093 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5094 if (err)
5095 return err;
5097 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5098 if (err)
5099 goto done;
5101 err = got_ref_write(rebased_branch, repo);
5102 if (err)
5103 goto done;
5105 err = got_worktree_set_head_ref(worktree, rebased_branch);
5106 if (err)
5107 goto done;
5109 err = delete_rebase_refs(worktree, repo);
5110 done:
5111 if (fileindex)
5112 got_fileindex_free(fileindex);
5113 free(new_head_commit_id);
5114 unlockerr = lock_worktree(worktree, LOCK_SH);
5115 if (unlockerr && err == NULL)
5116 err = unlockerr;
5117 return err;
5120 const struct got_error *
5121 got_worktree_rebase_abort(struct got_worktree *worktree,
5122 struct got_fileindex *fileindex, struct got_repository *repo,
5123 struct got_reference *new_base_branch,
5124 got_worktree_checkout_cb progress_cb, void *progress_arg)
5126 const struct got_error *err, *unlockerr, *sync_err;
5127 struct got_reference *resolved = NULL;
5128 struct got_object_id *commit_id = NULL;
5129 char *fileindex_path = NULL;
5130 struct revert_file_args rfa;
5131 struct got_object_id *tree_id = NULL;
5133 err = lock_worktree(worktree, LOCK_EX);
5134 if (err)
5135 return err;
5137 err = got_ref_open(&resolved, repo,
5138 got_ref_get_symref_target(new_base_branch), 0);
5139 if (err)
5140 goto done;
5142 err = got_worktree_set_head_ref(worktree, resolved);
5143 if (err)
5144 goto done;
5147 * XXX commits to the base branch could have happened while
5148 * we were busy rebasing; should we store the original commit ID
5149 * when rebase begins and read it back here?
5151 err = got_ref_resolve(&commit_id, repo, resolved);
5152 if (err)
5153 goto done;
5155 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5156 if (err)
5157 goto done;
5159 err = got_object_id_by_path(&tree_id, repo,
5160 worktree->base_commit_id, worktree->path_prefix);
5161 if (err)
5162 goto done;
5164 err = delete_rebase_refs(worktree, repo);
5165 if (err)
5166 goto done;
5168 err = get_fileindex_path(&fileindex_path, worktree);
5169 if (err)
5170 goto done;
5172 rfa.worktree = worktree;
5173 rfa.fileindex = fileindex;
5174 rfa.progress_cb = progress_cb;
5175 rfa.progress_arg = progress_arg;
5176 rfa.patch_cb = NULL;
5177 rfa.patch_arg = NULL;
5178 rfa.repo = repo;
5179 err = worktree_status(worktree, "", fileindex, repo,
5180 revert_file, &rfa, NULL, NULL);
5181 if (err)
5182 goto sync;
5184 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5185 repo, progress_cb, progress_arg, NULL, NULL);
5186 sync:
5187 sync_err = sync_fileindex(fileindex, fileindex_path);
5188 if (sync_err && err == NULL)
5189 err = sync_err;
5190 done:
5191 got_ref_close(resolved);
5192 free(tree_id);
5193 free(commit_id);
5194 if (fileindex)
5195 got_fileindex_free(fileindex);
5196 free(fileindex_path);
5198 unlockerr = lock_worktree(worktree, LOCK_SH);
5199 if (unlockerr && err == NULL)
5200 err = unlockerr;
5201 return err;
5204 const struct got_error *
5205 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5206 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5207 struct got_fileindex **fileindex, struct got_worktree *worktree,
5208 struct got_repository *repo)
5210 const struct got_error *err = NULL;
5211 char *tmp_branch_name = NULL;
5212 char *branch_ref_name = NULL;
5213 char *base_commit_ref_name = NULL;
5214 char *fileindex_path = NULL;
5215 struct check_rebase_ok_arg ok_arg;
5216 struct got_reference *wt_branch = NULL;
5217 struct got_reference *base_commit_ref = NULL;
5219 *tmp_branch = NULL;
5220 *branch_ref = NULL;
5221 *base_commit_id = NULL;
5222 *fileindex = NULL;
5224 err = lock_worktree(worktree, LOCK_EX);
5225 if (err)
5226 return err;
5228 err = open_fileindex(fileindex, &fileindex_path, worktree);
5229 if (err)
5230 goto done;
5232 ok_arg.worktree = worktree;
5233 ok_arg.repo = repo;
5234 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5235 &ok_arg);
5236 if (err)
5237 goto done;
5239 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5240 if (err)
5241 goto done;
5243 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5244 if (err)
5245 goto done;
5247 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5248 worktree);
5249 if (err)
5250 goto done;
5252 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5253 0);
5254 if (err)
5255 goto done;
5257 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5258 if (err)
5259 goto done;
5261 err = got_ref_write(*branch_ref, repo);
5262 if (err)
5263 goto done;
5265 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5266 worktree->base_commit_id);
5267 if (err)
5268 goto done;
5269 err = got_ref_write(base_commit_ref, repo);
5270 if (err)
5271 goto done;
5272 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5273 if (*base_commit_id == NULL) {
5274 err = got_error_from_errno("got_object_id_dup");
5275 goto done;
5278 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5279 worktree->base_commit_id);
5280 if (err)
5281 goto done;
5282 err = got_ref_write(*tmp_branch, repo);
5283 if (err)
5284 goto done;
5286 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5287 if (err)
5288 goto done;
5289 done:
5290 free(fileindex_path);
5291 free(tmp_branch_name);
5292 free(branch_ref_name);
5293 free(base_commit_ref_name);
5294 if (wt_branch)
5295 got_ref_close(wt_branch);
5296 if (err) {
5297 if (*branch_ref) {
5298 got_ref_close(*branch_ref);
5299 *branch_ref = NULL;
5301 if (*tmp_branch) {
5302 got_ref_close(*tmp_branch);
5303 *tmp_branch = NULL;
5305 free(*base_commit_id);
5306 if (*fileindex) {
5307 got_fileindex_free(*fileindex);
5308 *fileindex = NULL;
5310 lock_worktree(worktree, LOCK_SH);
5312 return err;
5315 const struct got_error *
5316 got_worktree_histedit_postpone(struct got_worktree *worktree,
5317 struct got_fileindex *fileindex)
5319 if (fileindex)
5320 got_fileindex_free(fileindex);
5321 return lock_worktree(worktree, LOCK_SH);
5324 const struct got_error *
5325 got_worktree_histedit_in_progress(int *in_progress,
5326 struct got_worktree *worktree)
5328 const struct got_error *err;
5329 char *tmp_branch_name = NULL;
5331 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5332 if (err)
5333 return err;
5335 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5336 free(tmp_branch_name);
5337 return NULL;
5340 const struct got_error *
5341 got_worktree_histedit_continue(struct got_object_id **commit_id,
5342 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5343 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5344 struct got_worktree *worktree, struct got_repository *repo)
5346 const struct got_error *err;
5347 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5348 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5349 struct got_reference *commit_ref = NULL;
5350 struct got_reference *base_commit_ref = NULL;
5351 char *fileindex_path = NULL;
5352 int have_staged_files = 0;
5354 *commit_id = NULL;
5355 *tmp_branch = NULL;
5356 *base_commit_id = NULL;
5357 *fileindex = NULL;
5359 err = lock_worktree(worktree, LOCK_EX);
5360 if (err)
5361 return err;
5363 err = open_fileindex(fileindex, &fileindex_path, worktree);
5364 if (err)
5365 goto done;
5367 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5368 &have_staged_files);
5369 if (err && err->code != GOT_ERR_CANCELLED)
5370 goto done;
5371 if (have_staged_files) {
5372 err = got_error(GOT_ERR_STAGED_PATHS);
5373 goto done;
5376 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5377 if (err)
5378 goto done;
5380 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5381 if (err)
5382 goto done;
5384 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5385 if (err)
5386 goto done;
5388 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5389 worktree);
5390 if (err)
5391 goto done;
5393 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5394 if (err)
5395 goto done;
5397 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5398 if (err)
5399 goto done;
5400 err = got_ref_resolve(commit_id, repo, commit_ref);
5401 if (err)
5402 goto done;
5404 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5405 if (err)
5406 goto done;
5407 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5408 if (err)
5409 goto done;
5411 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5412 if (err)
5413 goto done;
5414 done:
5415 free(commit_ref_name);
5416 free(branch_ref_name);
5417 free(fileindex_path);
5418 if (commit_ref)
5419 got_ref_close(commit_ref);
5420 if (base_commit_ref)
5421 got_ref_close(base_commit_ref);
5422 if (err) {
5423 free(*commit_id);
5424 *commit_id = NULL;
5425 free(*base_commit_id);
5426 *base_commit_id = NULL;
5427 if (*tmp_branch) {
5428 got_ref_close(*tmp_branch);
5429 *tmp_branch = NULL;
5431 if (*fileindex) {
5432 got_fileindex_free(*fileindex);
5433 *fileindex = NULL;
5435 lock_worktree(worktree, LOCK_EX);
5437 return err;
5440 static const struct got_error *
5441 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5443 const struct got_error *err;
5444 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5445 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5447 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5448 if (err)
5449 goto done;
5450 err = delete_ref(tmp_branch_name, repo);
5451 if (err)
5452 goto done;
5454 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5455 worktree);
5456 if (err)
5457 goto done;
5458 err = delete_ref(base_commit_ref_name, repo);
5459 if (err)
5460 goto done;
5462 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5463 if (err)
5464 goto done;
5465 err = delete_ref(branch_ref_name, repo);
5466 if (err)
5467 goto done;
5469 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5470 if (err)
5471 goto done;
5472 err = delete_ref(commit_ref_name, repo);
5473 if (err)
5474 goto done;
5475 done:
5476 free(tmp_branch_name);
5477 free(base_commit_ref_name);
5478 free(branch_ref_name);
5479 free(commit_ref_name);
5480 return err;
5483 const struct got_error *
5484 got_worktree_histedit_abort(struct got_worktree *worktree,
5485 struct got_fileindex *fileindex, struct got_repository *repo,
5486 struct got_reference *branch, struct got_object_id *base_commit_id,
5487 got_worktree_checkout_cb progress_cb, void *progress_arg)
5489 const struct got_error *err, *unlockerr, *sync_err;
5490 struct got_reference *resolved = NULL;
5491 char *fileindex_path = NULL;
5492 struct got_object_id *tree_id = NULL;
5493 struct revert_file_args rfa;
5495 err = lock_worktree(worktree, LOCK_EX);
5496 if (err)
5497 return err;
5499 err = got_ref_open(&resolved, repo,
5500 got_ref_get_symref_target(branch), 0);
5501 if (err)
5502 goto done;
5504 err = got_worktree_set_head_ref(worktree, resolved);
5505 if (err)
5506 goto done;
5508 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5509 if (err)
5510 goto done;
5512 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5513 worktree->path_prefix);
5514 if (err)
5515 goto done;
5517 err = delete_histedit_refs(worktree, repo);
5518 if (err)
5519 goto done;
5521 err = get_fileindex_path(&fileindex_path, worktree);
5522 if (err)
5523 goto done;
5525 rfa.worktree = worktree;
5526 rfa.fileindex = fileindex;
5527 rfa.progress_cb = progress_cb;
5528 rfa.progress_arg = progress_arg;
5529 rfa.patch_cb = NULL;
5530 rfa.patch_arg = NULL;
5531 rfa.repo = repo;
5532 err = worktree_status(worktree, "", fileindex, repo,
5533 revert_file, &rfa, NULL, NULL);
5534 if (err)
5535 goto sync;
5537 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5538 repo, progress_cb, progress_arg, NULL, NULL);
5539 sync:
5540 sync_err = sync_fileindex(fileindex, fileindex_path);
5541 if (sync_err && err == NULL)
5542 err = sync_err;
5543 done:
5544 got_ref_close(resolved);
5545 free(tree_id);
5546 free(fileindex_path);
5548 unlockerr = lock_worktree(worktree, LOCK_SH);
5549 if (unlockerr && err == NULL)
5550 err = unlockerr;
5551 return err;
5554 const struct got_error *
5555 got_worktree_histedit_complete(struct got_worktree *worktree,
5556 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5557 struct got_reference *edited_branch, struct got_repository *repo)
5559 const struct got_error *err, *unlockerr;
5560 struct got_object_id *new_head_commit_id = NULL;
5561 struct got_reference *resolved = NULL;
5563 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5564 if (err)
5565 return err;
5567 err = got_ref_open(&resolved, repo,
5568 got_ref_get_symref_target(edited_branch), 0);
5569 if (err)
5570 goto done;
5572 err = got_ref_change_ref(resolved, new_head_commit_id);
5573 if (err)
5574 goto done;
5576 err = got_ref_write(resolved, repo);
5577 if (err)
5578 goto done;
5580 err = got_worktree_set_head_ref(worktree, resolved);
5581 if (err)
5582 goto done;
5584 err = delete_histedit_refs(worktree, repo);
5585 done:
5586 if (fileindex)
5587 got_fileindex_free(fileindex);
5588 free(new_head_commit_id);
5589 unlockerr = lock_worktree(worktree, LOCK_SH);
5590 if (unlockerr && err == NULL)
5591 err = unlockerr;
5592 return err;
5595 const struct got_error *
5596 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5597 struct got_object_id *commit_id, struct got_repository *repo)
5599 const struct got_error *err;
5600 char *commit_ref_name;
5602 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5603 if (err)
5604 return err;
5606 err = store_commit_id(commit_ref_name, commit_id, repo);
5607 if (err)
5608 goto done;
5610 err = delete_ref(commit_ref_name, repo);
5611 done:
5612 free(commit_ref_name);
5613 return err;
5616 struct check_stage_ok_arg {
5617 struct got_object_id *head_commit_id;
5618 struct got_worktree *worktree;
5619 struct got_fileindex *fileindex;
5620 struct got_repository *repo;
5621 int have_changes;
5624 const struct got_error *
5625 check_stage_ok(void *arg, unsigned char status,
5626 unsigned char staged_status, const char *relpath,
5627 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5628 struct got_object_id *commit_id)
5630 struct check_stage_ok_arg *a = arg;
5631 const struct got_error *err = NULL;
5632 struct got_fileindex_entry *ie;
5633 struct got_object_id base_commit_id;
5634 struct got_object_id *base_commit_idp = NULL;
5635 char *in_repo_path = NULL, *p;
5637 if (status == GOT_STATUS_UNVERSIONED)
5638 return NULL;
5639 if (status == GOT_STATUS_NONEXISTENT)
5640 return got_error_set_errno(ENOENT, relpath);
5642 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5643 if (ie == NULL)
5644 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5646 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5647 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5648 relpath) == -1)
5649 return got_error_from_errno("asprintf");
5651 if (got_fileindex_entry_has_commit(ie)) {
5652 memcpy(base_commit_id.sha1, ie->commit_sha1,
5653 SHA1_DIGEST_LENGTH);
5654 base_commit_idp = &base_commit_id;
5657 if (status == GOT_STATUS_NO_CHANGE) {
5658 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5659 goto done;
5660 } else if (status == GOT_STATUS_CONFLICT) {
5661 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5662 goto done;
5663 } else if (status != GOT_STATUS_ADD &&
5664 status != GOT_STATUS_MODIFY &&
5665 status != GOT_STATUS_DELETE) {
5666 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5667 goto done;
5670 a->have_changes = 1;
5672 p = in_repo_path;
5673 while (p[0] == '/')
5674 p++;
5675 err = check_out_of_date(p, status, staged_status,
5676 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5677 GOT_ERR_STAGE_OUT_OF_DATE);
5678 done:
5679 free(in_repo_path);
5680 return err;
5683 struct stage_path_arg {
5684 struct got_worktree *worktree;
5685 struct got_fileindex *fileindex;
5686 struct got_repository *repo;
5687 got_worktree_status_cb status_cb;
5688 void *status_arg;
5689 got_worktree_patch_cb patch_cb;
5690 void *patch_arg;
5693 static const struct got_error *
5694 stage_path(void *arg, unsigned char status,
5695 unsigned char staged_status, const char *relpath,
5696 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5697 struct got_object_id *commit_id)
5699 struct stage_path_arg *a = arg;
5700 const struct got_error *err = NULL;
5701 struct got_fileindex_entry *ie;
5702 char *ondisk_path = NULL, *path_content = NULL;
5703 uint32_t stage;
5704 struct got_object_id *new_staged_blob_id = NULL;
5706 if (status == GOT_STATUS_UNVERSIONED)
5707 return NULL;
5709 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5710 if (ie == NULL)
5711 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5713 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5714 relpath)== -1)
5715 return got_error_from_errno("asprintf");
5717 switch (status) {
5718 case GOT_STATUS_ADD:
5719 case GOT_STATUS_MODIFY:
5720 if (a->patch_cb) {
5721 if (status == GOT_STATUS_ADD) {
5722 int choice = GOT_PATCH_CHOICE_NONE;
5723 err = (*a->patch_cb)(&choice, a->patch_arg,
5724 status, ie->path, NULL, 1, 1);
5725 if (err)
5726 break;
5727 if (choice != GOT_PATCH_CHOICE_YES)
5728 break;
5729 } else {
5730 err = create_patched_content(&path_content, 0,
5731 staged_blob_id ? staged_blob_id : blob_id,
5732 ondisk_path, ie->path, a->repo,
5733 a->patch_cb, a->patch_arg);
5734 if (err || path_content == NULL)
5735 break;
5738 err = got_object_blob_create(&new_staged_blob_id,
5739 path_content ? path_content : ondisk_path, a->repo);
5740 if (err)
5741 break;
5742 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5743 SHA1_DIGEST_LENGTH);
5744 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5745 stage = GOT_FILEIDX_STAGE_ADD;
5746 else
5747 stage = GOT_FILEIDX_STAGE_MODIFY;
5748 got_fileindex_entry_stage_set(ie, stage);
5749 if (a->status_cb == NULL)
5750 break;
5751 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5752 get_staged_status(ie), relpath, blob_id,
5753 new_staged_blob_id, NULL);
5754 break;
5755 case GOT_STATUS_DELETE:
5756 if (staged_status == GOT_STATUS_DELETE)
5757 break;
5758 if (a->patch_cb) {
5759 int choice = GOT_PATCH_CHOICE_NONE;
5760 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5761 ie->path, NULL, 1, 1);
5762 if (err)
5763 break;
5764 if (choice == GOT_PATCH_CHOICE_NO)
5765 break;
5766 if (choice != GOT_PATCH_CHOICE_YES) {
5767 err = got_error(GOT_ERR_PATCH_CHOICE);
5768 break;
5771 stage = GOT_FILEIDX_STAGE_DELETE;
5772 got_fileindex_entry_stage_set(ie, stage);
5773 if (a->status_cb == NULL)
5774 break;
5775 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5776 get_staged_status(ie), relpath, NULL, NULL, NULL);
5777 break;
5778 case GOT_STATUS_NO_CHANGE:
5779 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5780 break;
5781 case GOT_STATUS_CONFLICT:
5782 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5783 break;
5784 case GOT_STATUS_NONEXISTENT:
5785 err = got_error_set_errno(ENOENT, relpath);
5786 break;
5787 default:
5788 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5789 break;
5792 if (path_content && unlink(path_content) == -1 && err == NULL)
5793 err = got_error_from_errno2("unlink", path_content);
5794 free(path_content);
5795 free(ondisk_path);
5796 free(new_staged_blob_id);
5797 return err;
5800 const struct got_error *
5801 got_worktree_stage(struct got_worktree *worktree,
5802 struct got_pathlist_head *paths,
5803 got_worktree_status_cb status_cb, void *status_arg,
5804 got_worktree_patch_cb patch_cb, void *patch_arg,
5805 struct got_repository *repo)
5807 const struct got_error *err = NULL, *sync_err, *unlockerr;
5808 struct got_pathlist_entry *pe;
5809 struct got_fileindex *fileindex = NULL;
5810 char *fileindex_path = NULL;
5811 struct got_reference *head_ref = NULL;
5812 struct got_object_id *head_commit_id = NULL;
5813 struct check_stage_ok_arg oka;
5814 struct stage_path_arg spa;
5816 err = lock_worktree(worktree, LOCK_EX);
5817 if (err)
5818 return err;
5820 err = got_ref_open(&head_ref, repo,
5821 got_worktree_get_head_ref_name(worktree), 0);
5822 if (err)
5823 goto done;
5824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5825 if (err)
5826 goto done;
5827 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5828 if (err)
5829 goto done;
5831 /* Check pre-conditions before staging anything. */
5832 oka.head_commit_id = head_commit_id;
5833 oka.worktree = worktree;
5834 oka.fileindex = fileindex;
5835 oka.repo = repo;
5836 oka.have_changes = 0;
5837 TAILQ_FOREACH(pe, paths, entry) {
5838 err = worktree_status(worktree, pe->path, fileindex, repo,
5839 check_stage_ok, &oka, NULL, NULL);
5840 if (err)
5841 goto done;
5843 if (!oka.have_changes) {
5844 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5845 goto done;
5848 spa.worktree = worktree;
5849 spa.fileindex = fileindex;
5850 spa.repo = repo;
5851 spa.patch_cb = patch_cb;
5852 spa.patch_arg = patch_arg;
5853 spa.status_cb = status_cb;
5854 spa.status_arg = status_arg;
5855 TAILQ_FOREACH(pe, paths, entry) {
5856 err = worktree_status(worktree, pe->path, fileindex, repo,
5857 stage_path, &spa, NULL, NULL);
5858 if (err)
5859 goto done;
5862 sync_err = sync_fileindex(fileindex, fileindex_path);
5863 if (sync_err && err == NULL)
5864 err = sync_err;
5865 done:
5866 if (head_ref)
5867 got_ref_close(head_ref);
5868 free(head_commit_id);
5869 free(fileindex_path);
5870 if (fileindex)
5871 got_fileindex_free(fileindex);
5872 unlockerr = lock_worktree(worktree, LOCK_SH);
5873 if (unlockerr && err == NULL)
5874 err = unlockerr;
5875 return err;
5878 struct unstage_path_arg {
5879 struct got_worktree *worktree;
5880 struct got_fileindex *fileindex;
5881 struct got_repository *repo;
5882 got_worktree_checkout_cb progress_cb;
5883 void *progress_arg;
5884 got_worktree_patch_cb patch_cb;
5885 void *patch_arg;
5888 static const struct got_error *
5889 create_unstaged_content(char **path_unstaged_content,
5890 char **path_new_staged_content, struct got_object_id *blob_id,
5891 struct got_object_id *staged_blob_id, const char *relpath,
5892 struct got_repository *repo,
5893 got_worktree_patch_cb patch_cb, void *patch_arg)
5895 const struct got_error *err;
5896 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5897 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5898 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5899 struct stat sb1, sb2;
5900 struct got_diff_changes *changes = NULL;
5901 struct got_diff_state *ds = NULL;
5902 struct got_diff_args *args = NULL;
5903 struct got_diff_change *change;
5904 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5905 int have_content = 0, have_rejected_content = 0;
5907 *path_unstaged_content = NULL;
5908 *path_new_staged_content = NULL;
5910 err = got_object_id_str(&label1, blob_id);
5911 if (err)
5912 return err;
5913 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5914 if (err)
5915 goto done;
5917 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5918 if (err)
5919 goto done;
5921 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5922 if (err)
5923 goto done;
5925 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5926 if (err)
5927 goto done;
5929 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5930 if (err)
5931 goto done;
5933 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5934 if (err)
5935 goto done;
5937 if (stat(path1, &sb1) == -1) {
5938 err = got_error_from_errno2("stat", path1);
5939 goto done;
5942 if (stat(path2, &sb2) == -1) {
5943 err = got_error_from_errno2("stat", path2);
5944 goto done;
5947 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5948 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5949 if (err)
5950 goto done;
5952 err = got_opentemp_named(path_unstaged_content, &outfile,
5953 "got-unstaged-content");
5954 if (err)
5955 goto done;
5956 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5957 "got-new-staged-content");
5958 if (err)
5959 goto done;
5961 if (fseek(f1, 0L, SEEK_SET) == -1) {
5962 err = got_ferror(f1, GOT_ERR_IO);
5963 goto done;
5965 if (fseek(f2, 0L, SEEK_SET) == -1) {
5966 err = got_ferror(f2, GOT_ERR_IO);
5967 goto done;
5969 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5970 int choice;
5971 err = apply_or_reject_change(&choice, change, ++n,
5972 changes->nchanges, ds, args, diff_flags, relpath,
5973 f1, f2, &line_cur1, &line_cur2,
5974 outfile, rejectfile, patch_cb, patch_arg);
5975 if (err)
5976 goto done;
5977 if (choice == GOT_PATCH_CHOICE_YES)
5978 have_content = 1;
5979 else
5980 have_rejected_content = 1;
5981 if (choice == GOT_PATCH_CHOICE_QUIT)
5982 break;
5984 if (have_content || have_rejected_content)
5985 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5986 outfile, rejectfile);
5987 done:
5988 free(label1);
5989 if (blob)
5990 got_object_blob_close(blob);
5991 if (staged_blob)
5992 got_object_blob_close(staged_blob);
5993 if (f1 && fclose(f1) == EOF && err == NULL)
5994 err = got_error_from_errno2("fclose", path1);
5995 if (f2 && fclose(f2) == EOF && err == NULL)
5996 err = got_error_from_errno2("fclose", path2);
5997 if (outfile && fclose(outfile) == EOF && err == NULL)
5998 err = got_error_from_errno2("fclose", *path_unstaged_content);
5999 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6000 err = got_error_from_errno2("fclose", *path_new_staged_content);
6001 if (path1 && unlink(path1) == -1 && err == NULL)
6002 err = got_error_from_errno2("unlink", path1);
6003 if (path2 && unlink(path2) == -1 && err == NULL)
6004 err = got_error_from_errno2("unlink", path2);
6005 if (err || !have_content) {
6006 if (*path_unstaged_content &&
6007 unlink(*path_unstaged_content) == -1 && err == NULL)
6008 err = got_error_from_errno2("unlink",
6009 *path_unstaged_content);
6010 free(*path_unstaged_content);
6011 *path_unstaged_content = NULL;
6013 if (err || !have_rejected_content) {
6014 if (*path_new_staged_content &&
6015 unlink(*path_new_staged_content) == -1 && err == NULL)
6016 err = got_error_from_errno2("unlink",
6017 *path_new_staged_content);
6018 free(*path_new_staged_content);
6019 *path_new_staged_content = NULL;
6021 free(args);
6022 if (ds) {
6023 got_diff_state_free(ds);
6024 free(ds);
6026 if (changes)
6027 got_diff_free_changes(changes);
6028 free(path1);
6029 free(path2);
6030 return err;
6033 static const struct got_error *
6034 unstage_path(void *arg, unsigned char status,
6035 unsigned char staged_status, const char *relpath,
6036 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6037 struct got_object_id *commit_id)
6039 const struct got_error *err = NULL;
6040 struct unstage_path_arg *a = arg;
6041 struct got_fileindex_entry *ie;
6042 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6043 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6044 char *path_new_staged_content = NULL;
6045 int local_changes_subsumed;
6046 struct stat sb;
6048 if (staged_status != GOT_STATUS_ADD &&
6049 staged_status != GOT_STATUS_MODIFY &&
6050 staged_status != GOT_STATUS_DELETE)
6051 return NULL;
6053 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6054 if (ie == NULL)
6055 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6057 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6058 == -1)
6059 return got_error_from_errno("asprintf");
6061 switch (staged_status) {
6062 case GOT_STATUS_MODIFY:
6063 err = got_object_open_as_blob(&blob_base, a->repo,
6064 blob_id, 8192);
6065 if (err)
6066 break;
6067 /* fall through */
6068 case GOT_STATUS_ADD:
6069 if (a->patch_cb) {
6070 if (staged_status == GOT_STATUS_ADD) {
6071 int choice = GOT_PATCH_CHOICE_NONE;
6072 err = (*a->patch_cb)(&choice, a->patch_arg,
6073 staged_status, ie->path, NULL, 1, 1);
6074 if (err)
6075 break;
6076 if (choice != GOT_PATCH_CHOICE_YES)
6077 break;
6078 } else {
6079 err = create_unstaged_content(
6080 &path_unstaged_content,
6081 &path_new_staged_content, blob_id,
6082 staged_blob_id, ie->path, a->repo,
6083 a->patch_cb, a->patch_arg);
6084 if (err || path_unstaged_content == NULL)
6085 break;
6086 if (path_new_staged_content) {
6087 err = got_object_blob_create(
6088 &staged_blob_id,
6089 path_new_staged_content,
6090 a->repo);
6091 if (err)
6092 break;
6093 memcpy(ie->staged_blob_sha1,
6094 staged_blob_id->sha1,
6095 SHA1_DIGEST_LENGTH);
6097 err = merge_file(&local_changes_subsumed,
6098 a->worktree, blob_base, ondisk_path,
6099 relpath, got_fileindex_perms_to_st(ie),
6100 path_unstaged_content, "unstaged",
6101 a->repo, a->progress_cb, a->progress_arg);
6102 if (err == NULL &&
6103 path_new_staged_content == NULL)
6104 got_fileindex_entry_stage_set(ie,
6105 GOT_FILEIDX_STAGE_NONE);
6106 break; /* Done with this file. */
6109 err = got_object_open_as_blob(&blob_staged, a->repo,
6110 staged_blob_id, 8192);
6111 if (err)
6112 break;
6113 err = merge_blob(&local_changes_subsumed, a->worktree,
6114 blob_base, ondisk_path, relpath,
6115 got_fileindex_perms_to_st(ie), blob_staged,
6116 commit_id ? commit_id : a->worktree->base_commit_id,
6117 a->repo, a->progress_cb, a->progress_arg);
6118 if (err == NULL)
6119 got_fileindex_entry_stage_set(ie,
6120 GOT_FILEIDX_STAGE_NONE);
6121 break;
6122 case GOT_STATUS_DELETE:
6123 if (a->patch_cb) {
6124 int choice = GOT_PATCH_CHOICE_NONE;
6125 err = (*a->patch_cb)(&choice, a->patch_arg,
6126 staged_status, ie->path, NULL, 1, 1);
6127 if (err)
6128 break;
6129 if (choice == GOT_PATCH_CHOICE_NO)
6130 break;
6131 if (choice != GOT_PATCH_CHOICE_YES) {
6132 err = got_error(GOT_ERR_PATCH_CHOICE);
6133 break;
6136 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6137 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6138 if (err)
6139 break;
6140 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6141 break;
6144 free(ondisk_path);
6145 if (path_unstaged_content &&
6146 unlink(path_unstaged_content) == -1 && err == NULL)
6147 err = got_error_from_errno2("unlink", path_unstaged_content);
6148 if (path_new_staged_content &&
6149 unlink(path_new_staged_content) == -1 && err == NULL)
6150 err = got_error_from_errno2("unlink", path_new_staged_content);
6151 free(path_unstaged_content);
6152 free(path_new_staged_content);
6153 if (blob_base)
6154 got_object_blob_close(blob_base);
6155 if (blob_staged)
6156 got_object_blob_close(blob_staged);
6157 return err;
6160 const struct got_error *
6161 got_worktree_unstage(struct got_worktree *worktree,
6162 struct got_pathlist_head *paths,
6163 got_worktree_checkout_cb progress_cb, void *progress_arg,
6164 got_worktree_patch_cb patch_cb, void *patch_arg,
6165 struct got_repository *repo)
6167 const struct got_error *err = NULL, *sync_err, *unlockerr;
6168 struct got_pathlist_entry *pe;
6169 struct got_fileindex *fileindex = NULL;
6170 char *fileindex_path = NULL;
6171 struct unstage_path_arg upa;
6173 err = lock_worktree(worktree, LOCK_EX);
6174 if (err)
6175 return err;
6177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6178 if (err)
6179 goto done;
6181 upa.worktree = worktree;
6182 upa.fileindex = fileindex;
6183 upa.repo = repo;
6184 upa.progress_cb = progress_cb;
6185 upa.progress_arg = progress_arg;
6186 upa.patch_cb = patch_cb;
6187 upa.patch_arg = patch_arg;
6188 TAILQ_FOREACH(pe, paths, entry) {
6189 err = worktree_status(worktree, pe->path, fileindex, repo,
6190 unstage_path, &upa, NULL, NULL);
6191 if (err)
6192 goto done;
6195 sync_err = sync_fileindex(fileindex, fileindex_path);
6196 if (sync_err && err == NULL)
6197 err = sync_err;
6198 done:
6199 free(fileindex_path);
6200 if (fileindex)
6201 got_fileindex_free(fileindex);
6202 unlockerr = lock_worktree(worktree, LOCK_SH);
6203 if (unlockerr && err == NULL)
6204 err = unlockerr;
6205 return err;