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 mode_t
915 get_ondisk_perms(int executable, mode_t st_mode)
917 mode_t xbits = S_IXUSR;
919 if (executable) {
920 /* Map read bits to execute bits. */
921 if (st_mode & S_IRGRP)
922 xbits |= S_IXGRP;
923 if (st_mode & S_IROTH)
924 xbits |= S_IXOTH;
925 return st_mode | xbits;
928 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
931 static const struct got_error *
932 install_blob(struct got_worktree *worktree, const char *ondisk_path,
933 const char *path, uint16_t te_mode, uint16_t st_mode,
934 struct got_blob_object *blob, int restoring_missing_file,
935 int reverting_versioned_file, struct got_repository *repo,
936 got_worktree_checkout_cb progress_cb, void *progress_arg)
938 const struct got_error *err = NULL;
939 int fd = -1;
940 size_t len, hdrlen;
941 int update = 0;
942 char *tmppath = NULL;
944 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
945 GOT_DEFAULT_FILE_MODE);
946 if (fd == -1) {
947 if (errno == ENOENT) {
948 char *parent = dirname(path);
949 if (parent == NULL)
950 return got_error_from_errno2("dirname", path);
951 err = add_dir_on_disk(worktree, parent);
952 if (err)
953 return err;
954 fd = open(ondisk_path,
955 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
956 GOT_DEFAULT_FILE_MODE);
957 if (fd == -1)
958 return got_error_from_errno2("open",
959 ondisk_path);
960 } else if (errno == EEXIST) {
961 if (!S_ISREG(st_mode)) {
962 /* TODO file is obstructed; do something */
963 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
964 goto done;
965 } else {
966 err = got_opentemp_named_fd(&tmppath, &fd,
967 ondisk_path);
968 if (err)
969 goto done;
970 update = 1;
972 } else
973 return got_error_from_errno2("open", ondisk_path);
976 if (restoring_missing_file)
977 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
978 else if (reverting_versioned_file)
979 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
980 else
981 err = (*progress_cb)(progress_arg,
982 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
983 if (err)
984 goto done;
986 hdrlen = got_object_blob_get_hdrlen(blob);
987 do {
988 const uint8_t *buf = got_object_blob_get_read_buf(blob);
989 err = got_object_blob_read_block(&len, blob);
990 if (err)
991 break;
992 if (len > 0) {
993 /* Skip blob object header first time around. */
994 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
995 if (outlen == -1) {
996 err = got_error_from_errno("write");
997 goto done;
998 } else if (outlen != len - hdrlen) {
999 err = got_error(GOT_ERR_IO);
1000 goto done;
1002 hdrlen = 0;
1004 } while (len != 0);
1006 if (fsync(fd) != 0) {
1007 err = got_error_from_errno("fsync");
1008 goto done;
1011 if (update) {
1012 if (rename(tmppath, ondisk_path) != 0) {
1013 err = got_error_from_errno3("rename", tmppath,
1014 ondisk_path);
1015 unlink(tmppath);
1016 goto done;
1020 if (chmod(ondisk_path,
1021 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1022 err = got_error_from_errno2("chmod", ondisk_path);
1023 goto done;
1026 done:
1027 if (fd != -1 && close(fd) != 0 && err == NULL)
1028 err = got_error_from_errno("close");
1029 free(tmppath);
1030 return err;
1033 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1034 static const struct got_error *
1035 get_modified_file_content_status(unsigned char *status, FILE *f)
1037 const struct got_error *err = NULL;
1038 const char *markers[3] = {
1039 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1040 GOT_DIFF_CONFLICT_MARKER_SEP,
1041 GOT_DIFF_CONFLICT_MARKER_END
1043 int i = 0;
1044 char *line;
1045 size_t len;
1046 const char delim[3] = {'\0', '\0', '\0'};
1048 while (*status == GOT_STATUS_MODIFY) {
1049 line = fparseln(f, &len, NULL, delim, 0);
1050 if (line == NULL) {
1051 if (feof(f))
1052 break;
1053 err = got_ferror(f, GOT_ERR_IO);
1054 break;
1057 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1058 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1059 == 0)
1060 *status = GOT_STATUS_CONFLICT;
1061 else
1062 i++;
1066 return err;
1069 static int
1070 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1072 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1073 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1076 static int
1077 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1079 return !(ie->ctime_sec == sb->st_ctime &&
1080 ie->ctime_nsec == sb->st_ctimensec &&
1081 ie->mtime_sec == sb->st_mtime &&
1082 ie->mtime_nsec == sb->st_mtimensec &&
1083 ie->size == (sb->st_size & 0xffffffff) &&
1084 !xbit_differs(ie, sb->st_mode));
1087 static unsigned char
1088 get_staged_status(struct got_fileindex_entry *ie)
1090 switch (got_fileindex_entry_stage_get(ie)) {
1091 case GOT_FILEIDX_STAGE_ADD:
1092 return GOT_STATUS_ADD;
1093 case GOT_FILEIDX_STAGE_DELETE:
1094 return GOT_STATUS_DELETE;
1095 case GOT_FILEIDX_STAGE_MODIFY:
1096 return GOT_STATUS_MODIFY;
1097 default:
1098 return GOT_STATUS_NO_CHANGE;
1102 static const struct got_error *
1103 get_file_status(unsigned char *status, struct stat *sb,
1104 struct got_fileindex_entry *ie, const char *abspath,
1105 struct got_repository *repo)
1107 const struct got_error *err = NULL;
1108 struct got_object_id id;
1109 size_t hdrlen;
1110 FILE *f = NULL;
1111 uint8_t fbuf[8192];
1112 struct got_blob_object *blob = NULL;
1113 size_t flen, blen;
1114 unsigned char staged_status = get_staged_status(ie);
1116 *status = GOT_STATUS_NO_CHANGE;
1118 if (lstat(abspath, sb) == -1) {
1119 if (errno == ENOENT) {
1120 if (got_fileindex_entry_has_file_on_disk(ie))
1121 *status = GOT_STATUS_MISSING;
1122 else
1123 *status = GOT_STATUS_DELETE;
1124 return NULL;
1126 return got_error_from_errno2("lstat", abspath);
1129 if (!S_ISREG(sb->st_mode)) {
1130 *status = GOT_STATUS_OBSTRUCTED;
1131 return NULL;
1134 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1135 *status = GOT_STATUS_DELETE;
1136 return NULL;
1137 } else if (!got_fileindex_entry_has_blob(ie) &&
1138 staged_status != GOT_STATUS_ADD) {
1139 *status = GOT_STATUS_ADD;
1140 return NULL;
1143 if (!stat_info_differs(ie, sb))
1144 return NULL;
1146 if (staged_status == GOT_STATUS_MODIFY ||
1147 staged_status == GOT_STATUS_ADD)
1148 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1149 else
1150 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1152 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1153 if (err)
1154 return err;
1156 f = fopen(abspath, "r");
1157 if (f == NULL) {
1158 err = got_error_from_errno2("fopen", abspath);
1159 goto done;
1161 hdrlen = got_object_blob_get_hdrlen(blob);
1162 for (;;) {
1163 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1164 err = got_object_blob_read_block(&blen, blob);
1165 if (err)
1166 goto done;
1167 /* Skip length of blob object header first time around. */
1168 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1169 if (flen == 0 && ferror(f)) {
1170 err = got_error_from_errno("fread");
1171 goto done;
1173 if (blen == 0) {
1174 if (flen != 0)
1175 *status = GOT_STATUS_MODIFY;
1176 break;
1177 } else if (flen == 0) {
1178 if (blen != 0)
1179 *status = GOT_STATUS_MODIFY;
1180 break;
1181 } else if (blen - hdrlen == flen) {
1182 /* Skip blob object header first time around. */
1183 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1184 *status = GOT_STATUS_MODIFY;
1185 break;
1187 } else {
1188 *status = GOT_STATUS_MODIFY;
1189 break;
1191 hdrlen = 0;
1194 if (*status == GOT_STATUS_MODIFY) {
1195 rewind(f);
1196 err = get_modified_file_content_status(status, f);
1197 } else if (xbit_differs(ie, sb->st_mode))
1198 *status = GOT_STATUS_MODE_CHANGE;
1199 done:
1200 if (blob)
1201 got_object_blob_close(blob);
1202 if (f)
1203 fclose(f);
1204 return err;
1208 * Update timestamps in the file index if a file is unmodified and
1209 * we had to run a full content comparison to find out.
1211 static const struct got_error *
1212 sync_timestamps(char *ondisk_path, unsigned char status,
1213 struct got_fileindex_entry *ie, struct stat *sb)
1215 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1216 return got_fileindex_entry_update(ie, ondisk_path,
1217 ie->blob_sha1, ie->commit_sha1, 1);
1219 return NULL;
1222 static const struct got_error *
1223 update_blob(struct got_worktree *worktree,
1224 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1225 struct got_tree_entry *te, const char *path,
1226 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1227 void *progress_arg)
1229 const struct got_error *err = NULL;
1230 struct got_blob_object *blob = NULL;
1231 char *ondisk_path;
1232 unsigned char status = GOT_STATUS_NO_CHANGE;
1233 struct stat sb;
1235 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1236 return got_error_from_errno("asprintf");
1238 if (ie) {
1239 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1240 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1241 goto done;
1243 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1244 if (err)
1245 goto done;
1246 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1247 sb.st_mode = got_fileindex_perms_to_st(ie);
1248 } else
1249 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1251 if (status == GOT_STATUS_OBSTRUCTED) {
1252 err = (*progress_cb)(progress_arg, status, path);
1253 goto done;
1256 if (ie && status != GOT_STATUS_MISSING &&
1257 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1258 if (got_fileindex_entry_has_commit(ie) &&
1259 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1260 SHA1_DIGEST_LENGTH) == 0) {
1261 err = sync_timestamps(ondisk_path, status, ie, &sb);
1262 if (err)
1263 goto done;
1264 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1265 path);
1266 goto done;
1268 if (got_fileindex_entry_has_blob(ie) &&
1269 memcmp(ie->blob_sha1, te->id->sha1,
1270 SHA1_DIGEST_LENGTH) == 0) {
1271 err = sync_timestamps(ondisk_path, status, ie, &sb);
1272 goto done;
1276 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1277 if (err)
1278 goto done;
1280 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1281 int update_timestamps;
1282 struct got_blob_object *blob2 = NULL;
1283 if (got_fileindex_entry_has_blob(ie)) {
1284 struct got_object_id id2;
1285 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1286 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1287 if (err)
1288 goto done;
1290 err = merge_blob(&update_timestamps, worktree, blob2,
1291 ondisk_path, path, sb.st_mode, blob,
1292 worktree->base_commit_id, repo,
1293 progress_cb, progress_arg);
1294 if (blob2)
1295 got_object_blob_close(blob2);
1296 if (err)
1297 goto done;
1299 * Do not update timestamps of files with local changes.
1300 * Otherwise, a future status walk would treat them as
1301 * unmodified files again.
1303 err = got_fileindex_entry_update(ie, ondisk_path,
1304 blob->id.sha1, worktree->base_commit_id->sha1,
1305 update_timestamps);
1306 } else if (status == GOT_STATUS_MODE_CHANGE) {
1307 err = got_fileindex_entry_update(ie, ondisk_path,
1308 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1309 } else if (status == GOT_STATUS_DELETE) {
1310 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1311 if (err)
1312 goto done;
1313 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1314 ondisk_path, path, blob, 0);
1315 if (err)
1316 goto done;
1317 } else {
1318 err = install_blob(worktree, ondisk_path, path, te->mode,
1319 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1320 repo, progress_cb, progress_arg);
1321 if (err)
1322 goto done;
1323 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1324 ondisk_path, path, blob, 1);
1325 if (err)
1326 goto done;
1328 got_object_blob_close(blob);
1329 done:
1330 free(ondisk_path);
1331 return err;
1334 static const struct got_error *
1335 remove_ondisk_file(const char *root_path, const char *path)
1337 const struct got_error *err = NULL;
1338 char *ondisk_path = NULL;
1340 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1341 return got_error_from_errno("asprintf");
1343 if (unlink(ondisk_path) == -1) {
1344 if (errno != ENOENT)
1345 err = got_error_from_errno2("unlink", ondisk_path);
1346 } else {
1347 char *parent = dirname(ondisk_path);
1348 while (parent && strcmp(parent, root_path) != 0) {
1349 if (rmdir(parent) == -1) {
1350 if (errno != ENOTEMPTY)
1351 err = got_error_from_errno2("rmdir",
1352 parent);
1353 break;
1355 parent = dirname(parent);
1358 free(ondisk_path);
1359 return err;
1362 static const struct got_error *
1363 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1364 struct got_fileindex_entry *ie, struct got_repository *repo,
1365 got_worktree_checkout_cb progress_cb, void *progress_arg)
1367 const struct got_error *err = NULL;
1368 unsigned char status;
1369 struct stat sb;
1370 char *ondisk_path;
1372 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1373 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1375 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1376 == -1)
1377 return got_error_from_errno("asprintf");
1379 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1380 if (err)
1381 return err;
1383 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1384 status == GOT_STATUS_ADD) {
1385 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1386 if (err)
1387 return err;
1389 * Preserve the working file and change the deleted blob's
1390 * entry into a schedule-add entry.
1392 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1393 0);
1394 if (err)
1395 return err;
1396 } else {
1397 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1398 if (err)
1399 return err;
1400 if (status == GOT_STATUS_NO_CHANGE) {
1401 err = remove_ondisk_file(worktree->root_path, ie->path);
1402 if (err)
1403 return err;
1405 got_fileindex_entry_remove(fileindex, ie);
1408 return err;
1411 struct diff_cb_arg {
1412 struct got_fileindex *fileindex;
1413 struct got_worktree *worktree;
1414 struct got_repository *repo;
1415 got_worktree_checkout_cb progress_cb;
1416 void *progress_arg;
1417 got_cancel_cb cancel_cb;
1418 void *cancel_arg;
1421 static const struct got_error *
1422 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1423 struct got_tree_entry *te, const char *parent_path)
1425 struct diff_cb_arg *a = arg;
1427 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1428 return got_error(GOT_ERR_CANCELLED);
1430 return update_blob(a->worktree, a->fileindex, ie, te,
1431 ie->path, a->repo, a->progress_cb, a->progress_arg);
1434 static const struct got_error *
1435 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1437 struct diff_cb_arg *a = arg;
1439 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1440 return got_error(GOT_ERR_CANCELLED);
1442 return delete_blob(a->worktree, a->fileindex, ie,
1443 a->repo, a->progress_cb, a->progress_arg);
1446 static const struct got_error *
1447 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1449 struct diff_cb_arg *a = arg;
1450 const struct got_error *err;
1451 char *path;
1453 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1454 return got_error(GOT_ERR_CANCELLED);
1456 if (got_object_tree_entry_is_submodule(te))
1457 return NULL;
1459 if (asprintf(&path, "%s%s%s", parent_path,
1460 parent_path[0] ? "/" : "", te->name)
1461 == -1)
1462 return got_error_from_errno("asprintf");
1464 if (S_ISDIR(te->mode))
1465 err = add_dir_on_disk(a->worktree, path);
1466 else
1467 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1468 a->repo, a->progress_cb, a->progress_arg);
1470 free(path);
1471 return err;
1474 static const struct got_error *
1475 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1477 const struct got_error *err = NULL;
1478 char *uuidstr = NULL;
1479 uint32_t uuid_status;
1481 *refname = NULL;
1483 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1484 if (uuid_status != uuid_s_ok)
1485 return got_error_uuid(uuid_status, "uuid_to_string");
1487 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1488 == -1) {
1489 err = got_error_from_errno("asprintf");
1490 *refname = NULL;
1492 free(uuidstr);
1493 return err;
1496 const struct got_error *
1497 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1499 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1502 static const struct got_error *
1503 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1505 return get_ref_name(refname, worktree,
1506 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1509 static const struct got_error *
1510 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1512 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1515 static const struct got_error *
1516 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1518 return get_ref_name(refname, worktree,
1519 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1522 static const struct got_error *
1523 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1525 return get_ref_name(refname, worktree,
1526 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1529 static const struct got_error *
1530 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1532 return get_ref_name(refname, worktree,
1533 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1536 static const struct got_error *
1537 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1539 return get_ref_name(refname, worktree,
1540 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1543 static const struct got_error *
1544 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1550 static const struct got_error *
1551 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1557 const struct got_error *
1558 got_worktree_get_histedit_script_path(char **path,
1559 struct got_worktree *worktree)
1561 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1562 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1563 *path = NULL;
1564 return got_error_from_errno("asprintf");
1566 return NULL;
1570 * Prevent Git's garbage collector from deleting our base commit by
1571 * setting a reference to our base commit's ID.
1573 static const struct got_error *
1574 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 struct got_reference *ref = NULL;
1578 char *refname;
1580 err = got_worktree_get_base_ref_name(&refname, worktree);
1581 if (err)
1582 return err;
1584 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1585 if (err)
1586 goto done;
1588 err = got_ref_write(ref, repo);
1589 done:
1590 free(refname);
1591 if (ref)
1592 got_ref_close(ref);
1593 return err;
1596 static const struct got_error *
1597 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1599 const struct got_error *err = NULL;
1601 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1602 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1603 err = got_error_from_errno("asprintf");
1604 *fileindex_path = NULL;
1606 return err;
1610 static const struct got_error *
1611 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1612 struct got_worktree *worktree)
1614 const struct got_error *err = NULL;
1615 FILE *index = NULL;
1617 *fileindex_path = NULL;
1618 *fileindex = got_fileindex_alloc();
1619 if (*fileindex == NULL)
1620 return got_error_from_errno("got_fileindex_alloc");
1622 err = get_fileindex_path(fileindex_path, worktree);
1623 if (err)
1624 goto done;
1626 index = fopen(*fileindex_path, "rb");
1627 if (index == NULL) {
1628 if (errno != ENOENT)
1629 err = got_error_from_errno2("fopen", *fileindex_path);
1630 } else {
1631 err = got_fileindex_read(*fileindex, index);
1632 if (fclose(index) != 0 && err == NULL)
1633 err = got_error_from_errno("fclose");
1635 done:
1636 if (err) {
1637 free(*fileindex_path);
1638 *fileindex_path = NULL;
1639 got_fileindex_free(*fileindex);
1640 *fileindex = NULL;
1642 return err;
1645 struct bump_base_commit_id_arg {
1646 struct got_object_id *base_commit_id;
1647 const char *path;
1648 size_t path_len;
1649 const char *entry_name;
1650 got_worktree_checkout_cb progress_cb;
1651 void *progress_arg;
1654 /* Bump base commit ID of all files within an updated part of the work tree. */
1655 static const struct got_error *
1656 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1658 const struct got_error *err;
1659 struct bump_base_commit_id_arg *a = arg;
1661 if (a->entry_name) {
1662 if (strcmp(ie->path, a->path) != 0)
1663 return NULL;
1664 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1665 return NULL;
1667 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1668 SHA1_DIGEST_LENGTH) == 0)
1669 return NULL;
1671 if (a->progress_cb) {
1672 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1673 ie->path);
1674 if (err)
1675 return err;
1677 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1678 return NULL;
1681 static const struct got_error *
1682 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1684 const struct got_error *err = NULL;
1685 char *new_fileindex_path = NULL;
1686 FILE *new_index = NULL;
1688 err = got_opentemp_named(&new_fileindex_path, &new_index,
1689 fileindex_path);
1690 if (err)
1691 goto done;
1693 err = got_fileindex_write(fileindex, new_index);
1694 if (err)
1695 goto done;
1697 if (rename(new_fileindex_path, fileindex_path) != 0) {
1698 err = got_error_from_errno3("rename", new_fileindex_path,
1699 fileindex_path);
1700 unlink(new_fileindex_path);
1702 done:
1703 if (new_index)
1704 fclose(new_index);
1705 free(new_fileindex_path);
1706 return err;
1709 static const struct got_error *
1710 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1711 struct got_object_id **tree_id, const char *wt_relpath,
1712 struct got_worktree *worktree, struct got_repository *repo)
1714 const struct got_error *err = NULL;
1715 struct got_object_id *id = NULL;
1716 char *in_repo_path = NULL;
1717 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1719 *entry_type = GOT_OBJ_TYPE_ANY;
1720 *tree_relpath = NULL;
1721 *tree_id = NULL;
1723 if (wt_relpath[0] == '\0') {
1724 /* Check out all files within the work tree. */
1725 *entry_type = GOT_OBJ_TYPE_TREE;
1726 *tree_relpath = strdup("");
1727 if (*tree_relpath == NULL) {
1728 err = got_error_from_errno("strdup");
1729 goto done;
1731 err = got_object_id_by_path(tree_id, repo,
1732 worktree->base_commit_id, worktree->path_prefix);
1733 if (err)
1734 goto done;
1735 return NULL;
1738 /* Check out a subset of files in the work tree. */
1740 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1741 is_root_wt ? "" : "/", wt_relpath) == -1) {
1742 err = got_error_from_errno("asprintf");
1743 goto done;
1746 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1747 in_repo_path);
1748 if (err)
1749 goto done;
1751 free(in_repo_path);
1752 in_repo_path = NULL;
1754 err = got_object_get_type(entry_type, repo, id);
1755 if (err)
1756 goto done;
1758 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1759 /* Check out a single file. */
1760 if (strchr(wt_relpath, '/') == NULL) {
1761 /* Check out a single file in work tree's root dir. */
1762 in_repo_path = strdup(worktree->path_prefix);
1763 if (in_repo_path == NULL) {
1764 err = got_error_from_errno("strdup");
1765 goto done;
1767 *tree_relpath = strdup("");
1768 if (*tree_relpath == NULL) {
1769 err = got_error_from_errno("strdup");
1770 goto done;
1772 } else {
1773 /* Check out a single file in a subdirectory. */
1774 err = got_path_dirname(tree_relpath, wt_relpath);
1775 if (err)
1776 return err;
1777 if (asprintf(&in_repo_path, "%s%s%s",
1778 worktree->path_prefix, is_root_wt ? "" : "/",
1779 *tree_relpath) == -1) {
1780 err = got_error_from_errno("asprintf");
1781 goto done;
1784 err = got_object_id_by_path(tree_id, repo,
1785 worktree->base_commit_id, in_repo_path);
1786 } else {
1787 /* Check out all files within a subdirectory. */
1788 *tree_id = got_object_id_dup(id);
1789 if (*tree_id == NULL) {
1790 err = got_error_from_errno("got_object_id_dup");
1791 goto done;
1793 *tree_relpath = strdup(wt_relpath);
1794 if (*tree_relpath == NULL) {
1795 err = got_error_from_errno("strdup");
1796 goto done;
1799 done:
1800 free(id);
1801 free(in_repo_path);
1802 if (err) {
1803 *entry_type = GOT_OBJ_TYPE_ANY;
1804 free(*tree_relpath);
1805 *tree_relpath = NULL;
1806 free(*tree_id);
1807 *tree_id = NULL;
1809 return err;
1812 static const struct got_error *
1813 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1814 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1815 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1816 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1818 const struct got_error *err = NULL;
1819 struct got_commit_object *commit = NULL;
1820 struct got_tree_object *tree = NULL;
1821 struct got_fileindex_diff_tree_cb diff_cb;
1822 struct diff_cb_arg arg;
1824 err = ref_base_commit(worktree, repo);
1825 if (err)
1826 goto done;
1828 err = got_object_open_as_commit(&commit, repo,
1829 worktree->base_commit_id);
1830 if (err)
1831 goto done;
1833 err = got_object_open_as_tree(&tree, repo, tree_id);
1834 if (err)
1835 goto done;
1837 if (entry_name &&
1838 got_object_tree_find_entry(tree, entry_name) == NULL) {
1839 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1840 goto done;
1843 diff_cb.diff_old_new = diff_old_new;
1844 diff_cb.diff_old = diff_old;
1845 diff_cb.diff_new = diff_new;
1846 arg.fileindex = fileindex;
1847 arg.worktree = worktree;
1848 arg.repo = repo;
1849 arg.progress_cb = progress_cb;
1850 arg.progress_arg = progress_arg;
1851 arg.cancel_cb = cancel_cb;
1852 arg.cancel_arg = cancel_arg;
1853 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1854 entry_name, repo, &diff_cb, &arg);
1855 done:
1856 if (tree)
1857 got_object_tree_close(tree);
1858 if (commit)
1859 got_object_commit_close(commit);
1860 return err;
1863 const struct got_error *
1864 got_worktree_checkout_files(struct got_worktree *worktree,
1865 struct got_pathlist_head *paths, struct got_repository *repo,
1866 got_worktree_checkout_cb progress_cb, void *progress_arg,
1867 got_cancel_cb cancel_cb, void *cancel_arg)
1869 const struct got_error *err = NULL, *sync_err, *unlockerr;
1870 struct got_commit_object *commit = NULL;
1871 struct got_tree_object *tree = NULL;
1872 struct got_fileindex *fileindex = NULL;
1873 char *fileindex_path = NULL;
1874 struct got_pathlist_entry *pe;
1875 struct tree_path_data {
1876 SIMPLEQ_ENTRY(tree_path_data) entry;
1877 struct got_object_id *tree_id;
1878 int entry_type;
1879 char *relpath;
1880 char *entry_name;
1881 } *tpd = NULL;
1882 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1884 SIMPLEQ_INIT(&tree_paths);
1886 err = lock_worktree(worktree, LOCK_EX);
1887 if (err)
1888 return err;
1890 /* Map all specified paths to in-repository trees. */
1891 TAILQ_FOREACH(pe, paths, entry) {
1892 tpd = malloc(sizeof(*tpd));
1893 if (tpd == NULL) {
1894 err = got_error_from_errno("malloc");
1895 goto done;
1898 err = find_tree_entry_for_checkout(&tpd->entry_type,
1899 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1900 if (err) {
1901 free(tpd);
1902 goto done;
1905 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1906 err = got_path_basename(&tpd->entry_name, pe->path);
1907 if (err) {
1908 free(tpd->relpath);
1909 free(tpd->tree_id);
1910 free(tpd);
1911 goto done;
1913 } else
1914 tpd->entry_name = NULL;
1916 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1920 * Read the file index.
1921 * Checking out files is supposed to be an idempotent operation.
1922 * If the on-disk file index is incomplete we will try to complete it.
1924 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1925 if (err)
1926 goto done;
1928 tpd = SIMPLEQ_FIRST(&tree_paths);
1929 TAILQ_FOREACH(pe, paths, entry) {
1930 struct bump_base_commit_id_arg bbc_arg;
1932 err = checkout_files(worktree, fileindex, tpd->relpath,
1933 tpd->tree_id, tpd->entry_name, repo,
1934 progress_cb, progress_arg, cancel_cb, cancel_arg);
1935 if (err)
1936 break;
1938 bbc_arg.base_commit_id = worktree->base_commit_id;
1939 bbc_arg.entry_name = tpd->entry_name;
1940 bbc_arg.path = pe->path;
1941 bbc_arg.path_len = pe->path_len;
1942 bbc_arg.progress_cb = progress_cb;
1943 bbc_arg.progress_arg = progress_arg;
1944 err = got_fileindex_for_each_entry_safe(fileindex,
1945 bump_base_commit_id, &bbc_arg);
1946 if (err)
1947 break;
1949 tpd = SIMPLEQ_NEXT(tpd, entry);
1951 sync_err = sync_fileindex(fileindex, fileindex_path);
1952 if (sync_err && err == NULL)
1953 err = sync_err;
1954 done:
1955 free(fileindex_path);
1956 if (tree)
1957 got_object_tree_close(tree);
1958 if (commit)
1959 got_object_commit_close(commit);
1960 if (fileindex)
1961 got_fileindex_free(fileindex);
1962 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1963 tpd = SIMPLEQ_FIRST(&tree_paths);
1964 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1965 free(tpd->relpath);
1966 free(tpd->tree_id);
1967 free(tpd);
1969 unlockerr = lock_worktree(worktree, LOCK_SH);
1970 if (unlockerr && err == NULL)
1971 err = unlockerr;
1972 return err;
1975 struct merge_file_cb_arg {
1976 struct got_worktree *worktree;
1977 struct got_fileindex *fileindex;
1978 got_worktree_checkout_cb progress_cb;
1979 void *progress_arg;
1980 got_cancel_cb cancel_cb;
1981 void *cancel_arg;
1982 struct got_object_id *commit_id2;
1985 static const struct got_error *
1986 merge_file_cb(void *arg, struct got_blob_object *blob1,
1987 struct got_blob_object *blob2, struct got_object_id *id1,
1988 struct got_object_id *id2, const char *path1, const char *path2,
1989 mode_t mode1, mode_t mode2, struct got_repository *repo)
1991 static const struct got_error *err = NULL;
1992 struct merge_file_cb_arg *a = arg;
1993 struct got_fileindex_entry *ie;
1994 char *ondisk_path = NULL;
1995 struct stat sb;
1996 unsigned char status;
1997 int local_changes_subsumed;
1999 if (blob1 && blob2) {
2000 ie = got_fileindex_entry_get(a->fileindex, path2,
2001 strlen(path2));
2002 if (ie == NULL)
2003 return (*a->progress_cb)(a->progress_arg,
2004 GOT_STATUS_MISSING, path2);
2006 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2007 path2) == -1)
2008 return got_error_from_errno("asprintf");
2010 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2011 if (err)
2012 goto done;
2014 if (status == GOT_STATUS_DELETE) {
2015 err = (*a->progress_cb)(a->progress_arg,
2016 GOT_STATUS_MERGE, path2);
2017 goto done;
2019 if (status != GOT_STATUS_NO_CHANGE &&
2020 status != GOT_STATUS_MODIFY &&
2021 status != GOT_STATUS_CONFLICT &&
2022 status != GOT_STATUS_ADD) {
2023 err = (*a->progress_cb)(a->progress_arg, status, path2);
2024 goto done;
2027 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2028 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2029 a->progress_cb, a->progress_arg);
2030 } else if (blob1) {
2031 ie = got_fileindex_entry_get(a->fileindex, path1,
2032 strlen(path1));
2033 if (ie == NULL)
2034 return (*a->progress_cb)(a->progress_arg,
2035 GOT_STATUS_MISSING, path2);
2037 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2038 path1) == -1)
2039 return got_error_from_errno("asprintf");
2041 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2042 if (err)
2043 goto done;
2045 switch (status) {
2046 case GOT_STATUS_NO_CHANGE:
2047 err = (*a->progress_cb)(a->progress_arg,
2048 GOT_STATUS_DELETE, path1);
2049 if (err)
2050 goto done;
2051 err = remove_ondisk_file(a->worktree->root_path, path1);
2052 if (err)
2053 goto done;
2054 if (ie)
2055 got_fileindex_entry_mark_deleted_from_disk(ie);
2056 break;
2057 case GOT_STATUS_DELETE:
2058 case GOT_STATUS_MISSING:
2059 err = (*a->progress_cb)(a->progress_arg,
2060 GOT_STATUS_DELETE, path1);
2061 if (err)
2062 goto done;
2063 if (ie)
2064 got_fileindex_entry_mark_deleted_from_disk(ie);
2065 break;
2066 case GOT_STATUS_ADD:
2067 case GOT_STATUS_MODIFY:
2068 case GOT_STATUS_CONFLICT:
2069 err = (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_CANNOT_DELETE, path1);
2071 if (err)
2072 goto done;
2073 break;
2074 case GOT_STATUS_OBSTRUCTED:
2075 err = (*a->progress_cb)(a->progress_arg, status, path1);
2076 if (err)
2077 goto done;
2078 break;
2079 default:
2080 break;
2082 } else if (blob2) {
2083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2084 path2) == -1)
2085 return got_error_from_errno("asprintf");
2086 ie = got_fileindex_entry_get(a->fileindex, path2,
2087 strlen(path2));
2088 if (ie) {
2089 err = get_file_status(&status, &sb, ie, ondisk_path,
2090 repo);
2091 if (err)
2092 goto done;
2093 if (status != GOT_STATUS_NO_CHANGE &&
2094 status != GOT_STATUS_MODIFY &&
2095 status != GOT_STATUS_CONFLICT &&
2096 status != GOT_STATUS_ADD) {
2097 err = (*a->progress_cb)(a->progress_arg,
2098 status, path2);
2099 goto done;
2101 err = merge_blob(&local_changes_subsumed, a->worktree,
2102 NULL, ondisk_path, path2, sb.st_mode, blob2,
2103 a->commit_id2, repo,
2104 a->progress_cb, a->progress_arg);
2105 if (status == GOT_STATUS_DELETE) {
2106 err = update_blob_fileindex_entry(a->worktree,
2107 a->fileindex, ie, ondisk_path, ie->path,
2108 blob2, 0);
2109 if (err)
2110 goto done;
2112 } else {
2113 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2114 err = install_blob(a->worktree, ondisk_path, path2,
2115 /* XXX get this from parent tree! */
2116 GOT_DEFAULT_FILE_MODE,
2117 sb.st_mode, blob2, 0, 0, repo,
2118 a->progress_cb, a->progress_arg);
2119 if (err)
2120 goto done;
2121 err = got_fileindex_entry_alloc(&ie,
2122 ondisk_path, path2, NULL, NULL);
2123 if (err)
2124 goto done;
2125 err = got_fileindex_entry_add(a->fileindex, ie);
2126 if (err) {
2127 got_fileindex_entry_free(ie);
2128 goto done;
2132 done:
2133 free(ondisk_path);
2134 return err;
2137 struct check_merge_ok_arg {
2138 struct got_worktree *worktree;
2139 struct got_repository *repo;
2142 static const struct got_error *
2143 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2145 const struct got_error *err = NULL;
2146 struct check_merge_ok_arg *a = arg;
2147 unsigned char status;
2148 struct stat sb;
2149 char *ondisk_path;
2151 /* Reject merges into a work tree with mixed base commits. */
2152 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2153 SHA1_DIGEST_LENGTH))
2154 return got_error(GOT_ERR_MIXED_COMMITS);
2156 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2157 == -1)
2158 return got_error_from_errno("asprintf");
2160 /* Reject merges into a work tree with conflicted files. */
2161 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2162 if (err)
2163 return err;
2164 if (status == GOT_STATUS_CONFLICT)
2165 return got_error(GOT_ERR_CONFLICTS);
2167 return NULL;
2170 static const struct got_error *
2171 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2172 const char *fileindex_path, struct got_object_id *commit_id1,
2173 struct got_object_id *commit_id2, struct got_repository *repo,
2174 got_worktree_checkout_cb progress_cb, void *progress_arg,
2175 got_cancel_cb cancel_cb, void *cancel_arg)
2177 const struct got_error *err = NULL, *sync_err;
2178 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2179 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2180 struct merge_file_cb_arg arg;
2182 if (commit_id1) {
2183 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2184 worktree->path_prefix);
2185 if (err)
2186 goto done;
2188 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2189 if (err)
2190 goto done;
2193 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2194 worktree->path_prefix);
2195 if (err)
2196 goto done;
2198 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2199 if (err)
2200 goto done;
2202 arg.worktree = worktree;
2203 arg.fileindex = fileindex;
2204 arg.progress_cb = progress_cb;
2205 arg.progress_arg = progress_arg;
2206 arg.cancel_cb = cancel_cb;
2207 arg.cancel_arg = cancel_arg;
2208 arg.commit_id2 = commit_id2;
2209 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2210 sync_err = sync_fileindex(fileindex, fileindex_path);
2211 if (sync_err && err == NULL)
2212 err = sync_err;
2213 done:
2214 if (tree1)
2215 got_object_tree_close(tree1);
2216 if (tree2)
2217 got_object_tree_close(tree2);
2218 return err;
2221 const struct got_error *
2222 got_worktree_merge_files(struct got_worktree *worktree,
2223 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2224 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2225 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2227 const struct got_error *err, *unlockerr;
2228 char *fileindex_path = NULL;
2229 struct got_fileindex *fileindex = NULL;
2230 struct check_merge_ok_arg mok_arg;
2232 err = lock_worktree(worktree, LOCK_EX);
2233 if (err)
2234 return err;
2236 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2237 if (err)
2238 goto done;
2240 mok_arg.worktree = worktree;
2241 mok_arg.repo = repo;
2242 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2243 &mok_arg);
2244 if (err)
2245 goto done;
2247 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2248 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2249 done:
2250 if (fileindex)
2251 got_fileindex_free(fileindex);
2252 free(fileindex_path);
2253 unlockerr = lock_worktree(worktree, LOCK_SH);
2254 if (unlockerr && err == NULL)
2255 err = unlockerr;
2256 return err;
2259 struct diff_dir_cb_arg {
2260 struct got_fileindex *fileindex;
2261 struct got_worktree *worktree;
2262 const char *status_path;
2263 size_t status_path_len;
2264 struct got_repository *repo;
2265 got_worktree_status_cb status_cb;
2266 void *status_arg;
2267 got_cancel_cb cancel_cb;
2268 void *cancel_arg;
2269 /* A pathlist containing per-directory pathlists of ignore patterns. */
2270 struct got_pathlist_head ignores;
2273 static const struct got_error *
2274 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2275 got_worktree_status_cb status_cb, void *status_arg,
2276 struct got_repository *repo)
2278 const struct got_error *err = NULL;
2279 unsigned char status = GOT_STATUS_NO_CHANGE;
2280 unsigned char staged_status = get_staged_status(ie);
2281 struct stat sb;
2282 struct got_object_id blob_id, commit_id, staged_blob_id;
2283 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2284 struct got_object_id *staged_blob_idp = NULL;
2286 err = get_file_status(&status, &sb, ie, abspath, repo);
2287 if (err)
2288 return err;
2290 if (status == GOT_STATUS_NO_CHANGE &&
2291 staged_status == GOT_STATUS_NO_CHANGE)
2292 return NULL;
2294 if (got_fileindex_entry_has_blob(ie)) {
2295 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2296 blob_idp = &blob_id;
2298 if (got_fileindex_entry_has_commit(ie)) {
2299 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2300 commit_idp = &commit_id;
2302 if (staged_status == GOT_STATUS_ADD ||
2303 staged_status == GOT_STATUS_MODIFY) {
2304 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2305 SHA1_DIGEST_LENGTH);
2306 staged_blob_idp = &staged_blob_id;
2309 return (*status_cb)(status_arg, status, staged_status,
2310 ie->path, blob_idp, staged_blob_idp, commit_idp);
2313 static const struct got_error *
2314 status_old_new(void *arg, struct got_fileindex_entry *ie,
2315 struct dirent *de, const char *parent_path)
2317 const struct got_error *err = NULL;
2318 struct diff_dir_cb_arg *a = arg;
2319 char *abspath;
2321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2322 return got_error(GOT_ERR_CANCELLED);
2324 if (got_path_cmp(parent_path, a->status_path,
2325 strlen(parent_path), a->status_path_len) != 0 &&
2326 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2327 return NULL;
2329 if (parent_path[0]) {
2330 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2331 parent_path, de->d_name) == -1)
2332 return got_error_from_errno("asprintf");
2333 } else {
2334 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2335 de->d_name) == -1)
2336 return got_error_from_errno("asprintf");
2339 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2340 a->repo);
2341 free(abspath);
2342 return err;
2345 static const struct got_error *
2346 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2348 struct diff_dir_cb_arg *a = arg;
2349 struct got_object_id blob_id, commit_id;
2350 unsigned char status;
2352 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2353 return got_error(GOT_ERR_CANCELLED);
2355 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2356 return NULL;
2358 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2359 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2360 if (got_fileindex_entry_has_file_on_disk(ie))
2361 status = GOT_STATUS_MISSING;
2362 else
2363 status = GOT_STATUS_DELETE;
2364 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2365 ie->path, &blob_id, NULL, &commit_id);
2368 void
2369 free_ignorelist(struct got_pathlist_head *ignorelist)
2371 struct got_pathlist_entry *pe;
2373 TAILQ_FOREACH(pe, ignorelist, entry)
2374 free((char *)pe->path);
2375 got_pathlist_free(ignorelist);
2378 void
2379 free_ignores(struct got_pathlist_head *ignores)
2381 struct got_pathlist_entry *pe;
2383 TAILQ_FOREACH(pe, ignores, entry) {
2384 struct got_pathlist_head *ignorelist = pe->data;
2385 free_ignorelist(ignorelist);
2386 free((char *)pe->path);
2388 got_pathlist_free(ignores);
2391 static const struct got_error *
2392 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2394 const struct got_error *err = NULL;
2395 struct got_pathlist_entry *pe = NULL;
2396 struct got_pathlist_head *ignorelist;
2397 char *line = NULL, *pattern, *dirpath = NULL;
2398 size_t linesize = 0;
2399 ssize_t linelen;
2401 ignorelist = calloc(1, sizeof(*ignorelist));
2402 if (ignorelist == NULL)
2403 return got_error_from_errno("calloc");
2404 TAILQ_INIT(ignorelist);
2406 while ((linelen = getline(&line, &linesize, f)) != -1) {
2407 if (linelen > 0 && line[linelen - 1] == '\n')
2408 line[linelen - 1] = '\0';
2410 /* Git's ignores may contain comments. */
2411 if (line[0] == '#')
2412 continue;
2414 /* Git's negated patterns are not (yet?) supported. */
2415 if (line[0] == '!')
2416 continue;
2418 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2419 line) == -1) {
2420 err = got_error_from_errno("asprintf");
2421 goto done;
2423 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2424 if (err)
2425 goto done;
2427 if (ferror(f)) {
2428 err = got_error_from_errno("getline");
2429 goto done;
2432 dirpath = strdup(path);
2433 if (dirpath == NULL) {
2434 err = got_error_from_errno("strdup");
2435 goto done;
2437 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2438 done:
2439 free(line);
2440 if (err || pe == NULL) {
2441 free(dirpath);
2442 free_ignorelist(ignorelist);
2444 return err;
2447 int
2448 match_ignores(struct got_pathlist_head *ignores, const char *path)
2450 struct got_pathlist_entry *pe;
2452 /* Handle patterns which match in all directories. */
2453 TAILQ_FOREACH(pe, ignores, entry) {
2454 struct got_pathlist_head *ignorelist = pe->data;
2455 struct got_pathlist_entry *pi;
2457 TAILQ_FOREACH(pi, ignorelist, entry) {
2458 const char *p, *pattern = pi->path;
2460 if (strncmp(pattern, "**/", 3) != 0)
2461 continue;
2462 pattern += 3;
2463 p = path;
2464 while (*p) {
2465 if (fnmatch(pattern, p,
2466 FNM_PATHNAME | FNM_LEADING_DIR)) {
2467 /* Retry in next directory. */
2468 while (*p && *p != '/')
2469 p++;
2470 while (*p == '/')
2471 p++;
2472 continue;
2474 return 1;
2480 * The ignores pathlist contains ignore lists from children before
2481 * parents, so we can find the most specific ignorelist by walking
2482 * ignores backwards.
2484 pe = TAILQ_LAST(ignores, got_pathlist_head);
2485 while (pe) {
2486 if (got_path_is_child(path, pe->path, pe->path_len)) {
2487 struct got_pathlist_head *ignorelist = pe->data;
2488 struct got_pathlist_entry *pi;
2489 TAILQ_FOREACH(pi, ignorelist, entry) {
2490 const char *pattern = pi->path;
2491 int flags = FNM_LEADING_DIR;
2492 if (strstr(pattern, "/**/") == NULL)
2493 flags |= FNM_PATHNAME;
2494 if (fnmatch(pattern, path, flags))
2495 continue;
2496 return 1;
2499 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2502 return 0;
2505 static const struct got_error *
2506 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2507 const char *path, const char *ignores_filename)
2509 const struct got_error *err = NULL;
2510 char *ignorespath;
2511 FILE *ignoresfile = NULL;
2513 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2514 path[0] ? "/" : "", ignores_filename) == -1)
2515 return got_error_from_errno("asprintf");
2517 ignoresfile = fopen(ignorespath, "r");
2518 if (ignoresfile == NULL) {
2519 if (errno != ENOENT && errno != EACCES)
2520 err = got_error_from_errno2("fopen",
2521 ignorespath);
2522 } else
2523 err = read_ignores(ignores, path, ignoresfile);
2525 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2526 err = got_error_from_errno2("flose", path);
2527 free(ignorespath);
2528 return err;
2531 static const struct got_error *
2532 status_new(void *arg, struct dirent *de, const char *parent_path)
2534 const struct got_error *err = NULL;
2535 struct diff_dir_cb_arg *a = arg;
2536 char *path = NULL;
2538 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2539 return got_error(GOT_ERR_CANCELLED);
2541 /* XXX ignore symlinks for now */
2542 if (de->d_type == DT_LNK)
2543 return NULL;
2545 if (parent_path[0]) {
2546 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2547 return got_error_from_errno("asprintf");
2548 } else {
2549 path = de->d_name;
2552 if (de->d_type == DT_DIR) {
2553 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2554 ".cvsignore");
2555 if (err == NULL)
2556 err = add_ignores(&a->ignores, a->worktree->root_path,
2557 path, ".gitignore");
2559 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2560 && !match_ignores(&a->ignores, path))
2561 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2562 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2563 if (parent_path[0])
2564 free(path);
2565 return err;
2568 static const struct got_error *
2569 report_single_file_status(const char *path, const char *ondisk_path,
2570 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2571 void *status_arg, struct got_repository *repo)
2573 struct got_fileindex_entry *ie;
2574 struct stat sb;
2576 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2577 if (ie)
2578 return report_file_status(ie, ondisk_path, status_cb,
2579 status_arg, repo);
2581 if (lstat(ondisk_path, &sb) == -1) {
2582 if (errno != ENOENT)
2583 return got_error_from_errno2("lstat", ondisk_path);
2584 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2585 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2586 return NULL;
2589 if (S_ISREG(sb.st_mode))
2590 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2591 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2593 return NULL;
2596 static const struct got_error *
2597 worktree_status(struct got_worktree *worktree, const char *path,
2598 struct got_fileindex *fileindex, struct got_repository *repo,
2599 got_worktree_status_cb status_cb, void *status_arg,
2600 got_cancel_cb cancel_cb, void *cancel_arg)
2602 const struct got_error *err = NULL;
2603 DIR *workdir = NULL;
2604 struct got_fileindex_diff_dir_cb fdiff_cb;
2605 struct diff_dir_cb_arg arg;
2606 char *ondisk_path = NULL;
2608 if (asprintf(&ondisk_path, "%s%s%s",
2609 worktree->root_path, path[0] ? "/" : "", path) == -1)
2610 return got_error_from_errno("asprintf");
2612 workdir = opendir(ondisk_path);
2613 if (workdir == NULL) {
2614 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2615 err = got_error_from_errno2("opendir", ondisk_path);
2616 else
2617 err = report_single_file_status(path, ondisk_path,
2618 fileindex, status_cb, status_arg, repo);
2619 } else {
2620 fdiff_cb.diff_old_new = status_old_new;
2621 fdiff_cb.diff_old = status_old;
2622 fdiff_cb.diff_new = status_new;
2623 arg.fileindex = fileindex;
2624 arg.worktree = worktree;
2625 arg.status_path = path;
2626 arg.status_path_len = strlen(path);
2627 arg.repo = repo;
2628 arg.status_cb = status_cb;
2629 arg.status_arg = status_arg;
2630 arg.cancel_cb = cancel_cb;
2631 arg.cancel_arg = cancel_arg;
2632 TAILQ_INIT(&arg.ignores);
2633 err = add_ignores(&arg.ignores, worktree->root_path, path,
2634 ".cvsignore");
2635 if (err == NULL)
2636 err = add_ignores(&arg.ignores, worktree->root_path,
2637 path, ".gitignore");
2638 if (err == NULL)
2639 err = got_fileindex_diff_dir(fileindex, workdir,
2640 worktree->root_path, path, repo, &fdiff_cb, &arg);
2641 free_ignores(&arg.ignores);
2644 if (workdir)
2645 closedir(workdir);
2646 free(ondisk_path);
2647 return err;
2650 const struct got_error *
2651 got_worktree_status(struct got_worktree *worktree,
2652 struct got_pathlist_head *paths, struct got_repository *repo,
2653 got_worktree_status_cb status_cb, void *status_arg,
2654 got_cancel_cb cancel_cb, void *cancel_arg)
2656 const struct got_error *err = NULL;
2657 char *fileindex_path = NULL;
2658 struct got_fileindex *fileindex = NULL;
2659 struct got_pathlist_entry *pe;
2661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2662 if (err)
2663 return err;
2665 TAILQ_FOREACH(pe, paths, entry) {
2666 err = worktree_status(worktree, pe->path, fileindex, repo,
2667 status_cb, status_arg, cancel_cb, cancel_arg);
2668 if (err)
2669 break;
2671 free(fileindex_path);
2672 got_fileindex_free(fileindex);
2673 return err;
2676 const struct got_error *
2677 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2678 const char *arg)
2680 const struct got_error *err = NULL;
2681 char *resolved, *cwd = NULL, *path = NULL;
2682 size_t len;
2684 *wt_path = NULL;
2686 resolved = realpath(arg, NULL);
2687 if (resolved == NULL) {
2688 if (errno != ENOENT)
2689 return got_error_from_errno2("realpath", arg);
2690 cwd = getcwd(NULL, 0);
2691 if (cwd == NULL)
2692 return got_error_from_errno("getcwd");
2693 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2694 err = got_error_from_errno("asprintf");
2695 goto done;
2699 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2700 strlen(got_worktree_get_root_path(worktree)))) {
2701 err = got_error(GOT_ERR_BAD_PATH);
2702 goto done;
2705 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2706 err = got_path_skip_common_ancestor(&path,
2707 got_worktree_get_root_path(worktree), resolved);
2708 if (err)
2709 goto done;
2710 } else {
2711 path = strdup("");
2712 if (path == NULL) {
2713 err = got_error_from_errno("strdup");
2714 goto done;
2718 /* XXX status walk can't deal with trailing slash! */
2719 len = strlen(path);
2720 while (len > 0 && path[len - 1] == '/') {
2721 path[len - 1] = '\0';
2722 len--;
2724 done:
2725 free(resolved);
2726 free(cwd);
2727 if (err == NULL)
2728 *wt_path = path;
2729 else
2730 free(path);
2731 return err;
2734 static const struct got_error *
2735 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2736 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2737 struct got_repository *repo)
2739 const struct got_error *err = NULL;
2740 struct got_fileindex_entry *ie;
2741 unsigned char status;
2742 struct stat sb;
2744 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2745 if (ie) {
2746 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2747 if (err)
2748 return err;
2749 /* Re-adding an existing entry is a no-op. */
2750 if (status == GOT_STATUS_ADD)
2751 return NULL;
2752 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2755 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2756 if (err)
2757 return err;
2759 err = got_fileindex_entry_add(fileindex, ie);
2760 if (err) {
2761 got_fileindex_entry_free(ie);
2762 return err;
2765 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2768 const struct got_error *
2769 got_worktree_schedule_add(struct got_worktree *worktree,
2770 struct got_pathlist_head *paths,
2771 got_worktree_status_cb status_cb, void *status_arg,
2772 struct got_repository *repo)
2774 struct got_fileindex *fileindex = NULL;
2775 char *fileindex_path = NULL;
2776 const struct got_error *err = NULL, *sync_err, *unlockerr;
2777 struct got_pathlist_entry *pe;
2779 err = lock_worktree(worktree, LOCK_EX);
2780 if (err)
2781 return err;
2783 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2784 if (err)
2785 goto done;
2787 TAILQ_FOREACH(pe, paths, entry) {
2788 char *ondisk_path;
2789 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2790 pe->path) == -1)
2791 return got_error_from_errno("asprintf");
2792 err = schedule_addition(ondisk_path, fileindex, pe->path,
2793 status_cb, status_arg, repo);
2794 free(ondisk_path);
2795 if (err)
2796 break;
2798 sync_err = sync_fileindex(fileindex, fileindex_path);
2799 if (sync_err && err == NULL)
2800 err = sync_err;
2801 done:
2802 free(fileindex_path);
2803 if (fileindex)
2804 got_fileindex_free(fileindex);
2805 unlockerr = lock_worktree(worktree, LOCK_SH);
2806 if (unlockerr && err == NULL)
2807 err = unlockerr;
2808 return err;
2811 static const struct got_error *
2812 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2813 const char *relpath, int delete_local_mods,
2814 got_worktree_status_cb status_cb, void *status_arg,
2815 struct got_repository *repo)
2817 const struct got_error *err = NULL;
2818 struct got_fileindex_entry *ie = NULL;
2819 unsigned char status, staged_status;
2820 struct stat sb;
2822 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2823 if (ie == NULL)
2824 return got_error(GOT_ERR_BAD_PATH);
2826 staged_status = get_staged_status(ie);
2827 if (staged_status != GOT_STATUS_NO_CHANGE) {
2828 if (staged_status == GOT_STATUS_DELETE)
2829 return NULL;
2830 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2833 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2834 if (err)
2835 return err;
2837 if (status != GOT_STATUS_NO_CHANGE) {
2838 if (status == GOT_STATUS_DELETE)
2839 return NULL;
2840 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2841 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2842 if (status != GOT_STATUS_MODIFY &&
2843 status != GOT_STATUS_MISSING)
2844 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2847 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2848 return got_error_from_errno2("unlink", ondisk_path);
2850 got_fileindex_entry_mark_deleted_from_disk(ie);
2851 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2854 const struct got_error *
2855 got_worktree_schedule_delete(struct got_worktree *worktree,
2856 struct got_pathlist_head *paths, int delete_local_mods,
2857 got_worktree_status_cb status_cb, void *status_arg,
2858 struct got_repository *repo)
2860 struct got_fileindex *fileindex = NULL;
2861 char *fileindex_path = NULL;
2862 const struct got_error *err = NULL, *sync_err, *unlockerr;
2863 struct got_pathlist_entry *pe;
2865 err = lock_worktree(worktree, LOCK_EX);
2866 if (err)
2867 return err;
2869 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2870 if (err)
2871 goto done;
2873 TAILQ_FOREACH(pe, paths, entry) {
2874 char *ondisk_path;
2875 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2876 pe->path) == -1)
2877 return got_error_from_errno("asprintf");
2878 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2879 delete_local_mods, status_cb, status_arg, repo);
2880 free(ondisk_path);
2881 if (err)
2882 break;
2884 sync_err = sync_fileindex(fileindex, fileindex_path);
2885 if (sync_err && err == NULL)
2886 err = sync_err;
2887 done:
2888 free(fileindex_path);
2889 if (fileindex)
2890 got_fileindex_free(fileindex);
2891 unlockerr = lock_worktree(worktree, LOCK_SH);
2892 if (unlockerr && err == NULL)
2893 err = unlockerr;
2894 return err;
2897 static const struct got_error *
2898 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2900 const struct got_error *err = NULL;
2901 char *line = NULL;
2902 size_t linesize = 0, n;
2903 ssize_t linelen;
2905 linelen = getline(&line, &linesize, infile);
2906 if (linelen == -1) {
2907 if (ferror(infile)) {
2908 err = got_error_from_errno("getline");
2909 goto done;
2911 return NULL;
2913 if (outfile) {
2914 n = fwrite(line, 1, linelen, outfile);
2915 if (n != linelen) {
2916 err = got_ferror(outfile, GOT_ERR_IO);
2917 goto done;
2920 if (rejectfile) {
2921 n = fwrite(line, 1, linelen, rejectfile);
2922 if (n != linelen)
2923 err = got_ferror(outfile, GOT_ERR_IO);
2925 done:
2926 free(line);
2927 return err;
2930 static const struct got_error *
2931 skip_one_line(FILE *f)
2933 char *line = NULL;
2934 size_t linesize = 0;
2935 ssize_t linelen;
2937 linelen = getline(&line, &linesize, f);
2938 if (linelen == -1) {
2939 if (ferror(f))
2940 return got_error_from_errno("getline");
2941 return NULL;
2943 free(line);
2944 return NULL;
2947 static const struct got_error *
2948 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2949 int start_old, int end_old, int start_new, int end_new,
2950 FILE *outfile, FILE *rejectfile)
2952 const struct got_error *err;
2954 /* Copy old file's lines leading up to patch. */
2955 while (!feof(f1) && *line_cur1 < start_old) {
2956 err = copy_one_line(f1, outfile, NULL);
2957 if (err)
2958 return err;
2959 (*line_cur1)++;
2961 /* Skip new file's lines leading up to patch. */
2962 while (!feof(f2) && *line_cur2 < start_new) {
2963 if (rejectfile)
2964 err = copy_one_line(f2, NULL, rejectfile);
2965 else
2966 err = skip_one_line(f2);
2967 if (err)
2968 return err;
2969 (*line_cur2)++;
2971 /* Copy patched lines. */
2972 while (!feof(f2) && *line_cur2 <= end_new) {
2973 err = copy_one_line(f2, outfile, NULL);
2974 if (err)
2975 return err;
2976 (*line_cur2)++;
2978 /* Skip over old file's replaced lines. */
2979 while (!feof(f1) && *line_cur1 <= end_old) {
2980 if (rejectfile)
2981 err = copy_one_line(f1, NULL, rejectfile);
2982 else
2983 err = skip_one_line(f1);
2984 if (err)
2985 return err;
2986 (*line_cur1)++;
2989 return NULL;
2992 static const struct got_error *
2993 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2994 FILE *outfile, FILE *rejectfile)
2996 const struct got_error *err;
2998 if (outfile) {
2999 /* Copy old file's lines until EOF. */
3000 while (!feof(f1)) {
3001 err = copy_one_line(f1, outfile, NULL);
3002 if (err)
3003 return err;
3004 (*line_cur1)++;
3007 if (rejectfile) {
3008 /* Copy new file's lines until EOF. */
3009 while (!feof(f2)) {
3010 err = copy_one_line(f2, NULL, rejectfile);
3011 if (err)
3012 return err;
3013 (*line_cur2)++;
3017 return NULL;
3020 static const struct got_error *
3021 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3022 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3023 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3024 int *line_cur2, FILE *outfile, FILE *rejectfile,
3025 got_worktree_patch_cb patch_cb, void *patch_arg)
3027 const struct got_error *err = NULL;
3028 int start_old = change->cv.a;
3029 int end_old = change->cv.b;
3030 int start_new = change->cv.c;
3031 int end_new = change->cv.d;
3032 long pos1, pos2;
3033 FILE *hunkfile;
3035 *choice = GOT_PATCH_CHOICE_NONE;
3037 hunkfile = got_opentemp();
3038 if (hunkfile == NULL)
3039 return got_error_from_errno("got_opentemp");
3041 pos1 = ftell(f1);
3042 pos2 = ftell(f2);
3044 /* XXX TODO needs error checking */
3045 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3047 if (fseek(f1, pos1, SEEK_SET) == -1) {
3048 err = got_ferror(f1, GOT_ERR_IO);
3049 goto done;
3051 if (fseek(f2, pos2, SEEK_SET) == -1) {
3052 err = got_ferror(f1, GOT_ERR_IO);
3053 goto done;
3055 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3056 err = got_ferror(hunkfile, GOT_ERR_IO);
3057 goto done;
3060 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3061 hunkfile, n, nchanges);
3062 if (err)
3063 goto done;
3065 switch (*choice) {
3066 case GOT_PATCH_CHOICE_YES:
3067 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3068 end_old, start_new, end_new, outfile, rejectfile);
3069 break;
3070 case GOT_PATCH_CHOICE_NO:
3071 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3072 end_old, start_new, end_new, rejectfile, outfile);
3073 break;
3074 case GOT_PATCH_CHOICE_QUIT:
3075 break;
3076 default:
3077 err = got_error(GOT_ERR_PATCH_CHOICE);
3078 break;
3080 done:
3081 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 return err;
3086 struct revert_file_args {
3087 struct got_worktree *worktree;
3088 struct got_fileindex *fileindex;
3089 got_worktree_checkout_cb progress_cb;
3090 void *progress_arg;
3091 got_worktree_patch_cb patch_cb;
3092 void *patch_arg;
3093 struct got_repository *repo;
3096 static const struct got_error *
3097 create_patched_content(char **path_outfile, int reverse_patch,
3098 struct got_object_id *blob_id, const char *path2,
3099 const char *relpath, struct got_repository *repo,
3100 got_worktree_patch_cb patch_cb, void *patch_arg)
3102 const struct got_error *err;
3103 struct got_blob_object *blob = NULL;
3104 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3105 int fd2 = -1;
3106 char *path1 = NULL, *id_str = NULL;
3107 struct stat sb1, sb2;
3108 struct got_diff_changes *changes = NULL;
3109 struct got_diff_state *ds = NULL;
3110 struct got_diff_args *args = NULL;
3111 struct got_diff_change *change;
3112 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3113 int n = 0;
3115 *path_outfile = NULL;
3117 err = got_object_id_str(&id_str, blob_id);
3118 if (err)
3119 return err;
3121 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3122 if (fd2 == -1) {
3123 err = got_error_from_errno2("open", path2);
3124 goto done;
3126 if (fstat(fd2, &sb2) == -1) {
3127 err = got_error_from_errno2("fstat", path2);
3128 goto done;
3131 f2 = fdopen(fd2, "r");
3132 if (f2 == NULL) {
3133 err = got_error_from_errno2("fopen", path2);
3134 goto done;
3136 fd2 = -1;
3138 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3139 if (err)
3140 goto done;
3142 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3143 if (err)
3144 goto done;
3146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3147 if (err)
3148 goto done;
3150 if (stat(path1, &sb1) == -1) {
3151 err = got_error_from_errno2("stat", path1);
3152 goto done;
3155 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3156 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3157 if (err)
3158 goto done;
3160 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3161 if (err)
3162 goto done;
3164 if (fseek(f1, 0L, SEEK_SET) == -1)
3165 return got_ferror(f1, GOT_ERR_IO);
3166 if (fseek(f2, 0L, SEEK_SET) == -1)
3167 return got_ferror(f2, GOT_ERR_IO);
3168 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3169 int choice;
3170 err = apply_or_reject_change(&choice, change, ++n,
3171 changes->nchanges, ds, args, diff_flags, relpath,
3172 f1, f2, &line_cur1, &line_cur2,
3173 reverse_patch ? NULL : outfile,
3174 reverse_patch ? outfile : NULL,
3175 patch_cb, patch_arg);
3176 if (err)
3177 goto done;
3178 if (choice == GOT_PATCH_CHOICE_YES)
3179 have_content = 1;
3180 else if (choice == GOT_PATCH_CHOICE_QUIT)
3181 break;
3183 if (have_content) {
3184 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3185 reverse_patch ? NULL : outfile,
3186 reverse_patch ? outfile : NULL);
3187 if (err)
3188 goto done;
3190 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3191 err = got_error_from_errno2("chmod", path2);
3192 goto done;
3195 done:
3196 free(id_str);
3197 if (blob)
3198 got_object_blob_close(blob);
3199 if (f1 && fclose(f1) == EOF && err == NULL)
3200 err = got_error_from_errno2("fclose", path1);
3201 if (f2 && fclose(f2) == EOF && err == NULL)
3202 err = got_error_from_errno2("fclose", path2);
3203 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3204 err = got_error_from_errno2("close", path2);
3205 if (outfile && fclose(outfile) == EOF && err == NULL)
3206 err = got_error_from_errno2("fclose", *path_outfile);
3207 if (path1 && unlink(path1) == -1 && err == NULL)
3208 err = got_error_from_errno2("unlink", path1);
3209 if (err || !have_content) {
3210 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3211 err = got_error_from_errno2("unlink", *path_outfile);
3212 free(*path_outfile);
3213 *path_outfile = NULL;
3215 free(args);
3216 if (ds) {
3217 got_diff_state_free(ds);
3218 free(ds);
3220 if (changes)
3221 got_diff_free_changes(changes);
3222 free(path1);
3223 return err;
3226 static const struct got_error *
3227 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3228 const char *relpath, struct got_object_id *blob_id,
3229 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3231 struct revert_file_args *a = arg;
3232 const struct got_error *err = NULL;
3233 char *parent_path = NULL;
3234 struct got_fileindex_entry *ie;
3235 struct got_tree_object *tree = NULL;
3236 struct got_object_id *tree_id = NULL;
3237 const struct got_tree_entry *te = NULL;
3238 char *tree_path = NULL, *te_name;
3239 char *ondisk_path = NULL, *path_content = NULL;
3240 struct got_blob_object *blob = NULL;
3242 /* Reverting a staged deletion is a no-op. */
3243 if (status == GOT_STATUS_DELETE &&
3244 staged_status != GOT_STATUS_NO_CHANGE)
3245 return NULL;
3247 if (status == GOT_STATUS_UNVERSIONED)
3248 return (*a->progress_cb)(a->progress_arg,
3249 GOT_STATUS_UNVERSIONED, relpath);
3251 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3252 if (ie == NULL)
3253 return got_error(GOT_ERR_BAD_PATH);
3255 /* Construct in-repository path of tree which contains this blob. */
3256 err = got_path_dirname(&parent_path, ie->path);
3257 if (err) {
3258 if (err->code != GOT_ERR_BAD_PATH)
3259 goto done;
3260 parent_path = strdup("/");
3261 if (parent_path == NULL) {
3262 err = got_error_from_errno("strdup");
3263 goto done;
3266 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3267 tree_path = strdup(parent_path);
3268 if (tree_path == NULL) {
3269 err = got_error_from_errno("strdup");
3270 goto done;
3272 } else {
3273 if (got_path_is_root_dir(parent_path)) {
3274 tree_path = strdup(a->worktree->path_prefix);
3275 if (tree_path == NULL) {
3276 err = got_error_from_errno("strdup");
3277 goto done;
3279 } else {
3280 if (asprintf(&tree_path, "%s/%s",
3281 a->worktree->path_prefix, parent_path) == -1) {
3282 err = got_error_from_errno("asprintf");
3283 goto done;
3288 err = got_object_id_by_path(&tree_id, a->repo,
3289 a->worktree->base_commit_id, tree_path);
3290 if (err) {
3291 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3292 (status == GOT_STATUS_ADD ||
3293 staged_status == GOT_STATUS_ADD)))
3294 goto done;
3295 } else {
3296 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3297 if (err)
3298 goto done;
3300 te_name = basename(ie->path);
3301 if (te_name == NULL) {
3302 err = got_error_from_errno2("basename", ie->path);
3303 goto done;
3306 te = got_object_tree_find_entry(tree, te_name);
3307 if (te == NULL && status != GOT_STATUS_ADD &&
3308 staged_status != GOT_STATUS_ADD) {
3309 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3310 goto done;
3314 switch (status) {
3315 case GOT_STATUS_ADD:
3316 if (a->patch_cb) {
3317 int choice = GOT_PATCH_CHOICE_NONE;
3318 err = (*a->patch_cb)(&choice, a->patch_arg,
3319 status, ie->path, NULL, 1, 1);
3320 if (err)
3321 goto done;
3322 if (choice != GOT_PATCH_CHOICE_YES)
3323 break;
3325 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3326 ie->path);
3327 if (err)
3328 goto done;
3329 got_fileindex_entry_remove(a->fileindex, ie);
3330 break;
3331 case GOT_STATUS_DELETE:
3332 if (a->patch_cb) {
3333 int choice = GOT_PATCH_CHOICE_NONE;
3334 err = (*a->patch_cb)(&choice, a->patch_arg,
3335 status, ie->path, NULL, 1, 1);
3336 if (err)
3337 goto done;
3338 if (choice != GOT_PATCH_CHOICE_YES)
3339 break;
3341 /* fall through */
3342 case GOT_STATUS_MODIFY:
3343 case GOT_STATUS_MODE_CHANGE:
3344 case GOT_STATUS_CONFLICT:
3345 case GOT_STATUS_MISSING: {
3346 struct got_object_id id;
3347 if (staged_status == GOT_STATUS_ADD ||
3348 staged_status == GOT_STATUS_MODIFY) {
3349 memcpy(id.sha1, ie->staged_blob_sha1,
3350 SHA1_DIGEST_LENGTH);
3351 } else
3352 memcpy(id.sha1, ie->blob_sha1,
3353 SHA1_DIGEST_LENGTH);
3354 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3355 if (err)
3356 goto done;
3358 if (asprintf(&ondisk_path, "%s/%s",
3359 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3360 err = got_error_from_errno("asprintf");
3361 goto done;
3364 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3365 status == GOT_STATUS_CONFLICT)) {
3366 err = create_patched_content(&path_content, 1, &id,
3367 ondisk_path, ie->path, a->repo,
3368 a->patch_cb, a->patch_arg);
3369 if (err || path_content == NULL)
3370 break;
3371 if (rename(path_content, ondisk_path) == -1) {
3372 err = got_error_from_errno3("rename",
3373 path_content, ondisk_path);
3374 goto done;
3376 } else {
3377 err = install_blob(a->worktree, ondisk_path, ie->path,
3378 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3379 got_fileindex_perms_to_st(ie), blob, 0, 1,
3380 a->repo, a->progress_cb, a->progress_arg);
3381 if (err)
3382 goto done;
3383 if (status == GOT_STATUS_DELETE ||
3384 status == GOT_STATUS_MODE_CHANGE) {
3385 err = update_blob_fileindex_entry(a->worktree,
3386 a->fileindex, ie, ondisk_path, ie->path,
3387 blob, 1);
3388 if (err)
3389 goto done;
3392 break;
3394 default:
3395 break;
3397 done:
3398 free(ondisk_path);
3399 free(path_content);
3400 free(parent_path);
3401 free(tree_path);
3402 if (blob)
3403 got_object_blob_close(blob);
3404 if (tree)
3405 got_object_tree_close(tree);
3406 free(tree_id);
3407 return err;
3410 const struct got_error *
3411 got_worktree_revert(struct got_worktree *worktree,
3412 struct got_pathlist_head *paths,
3413 got_worktree_checkout_cb progress_cb, void *progress_arg,
3414 got_worktree_patch_cb patch_cb, void *patch_arg,
3415 struct got_repository *repo)
3417 struct got_fileindex *fileindex = NULL;
3418 char *fileindex_path = NULL;
3419 const struct got_error *err = NULL, *unlockerr = NULL;
3420 const struct got_error *sync_err = NULL;
3421 struct got_pathlist_entry *pe;
3422 struct revert_file_args rfa;
3424 err = lock_worktree(worktree, LOCK_EX);
3425 if (err)
3426 return err;
3428 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3429 if (err)
3430 goto done;
3432 rfa.worktree = worktree;
3433 rfa.fileindex = fileindex;
3434 rfa.progress_cb = progress_cb;
3435 rfa.progress_arg = progress_arg;
3436 rfa.patch_cb = patch_cb;
3437 rfa.patch_arg = patch_arg;
3438 rfa.repo = repo;
3439 TAILQ_FOREACH(pe, paths, entry) {
3440 err = worktree_status(worktree, pe->path, fileindex, repo,
3441 revert_file, &rfa, NULL, NULL);
3442 if (err)
3443 break;
3445 sync_err = sync_fileindex(fileindex, fileindex_path);
3446 if (sync_err && err == NULL)
3447 err = sync_err;
3448 done:
3449 free(fileindex_path);
3450 if (fileindex)
3451 got_fileindex_free(fileindex);
3452 unlockerr = lock_worktree(worktree, LOCK_SH);
3453 if (unlockerr && err == NULL)
3454 err = unlockerr;
3455 return err;
3458 static void
3459 free_commitable(struct got_commitable *ct)
3461 free(ct->path);
3462 free(ct->in_repo_path);
3463 free(ct->ondisk_path);
3464 free(ct->blob_id);
3465 free(ct->base_blob_id);
3466 free(ct->staged_blob_id);
3467 free(ct->base_commit_id);
3468 free(ct);
3471 struct collect_commitables_arg {
3472 struct got_pathlist_head *commitable_paths;
3473 struct got_repository *repo;
3474 struct got_worktree *worktree;
3475 int have_staged_files;
3478 static const struct got_error *
3479 collect_commitables(void *arg, unsigned char status,
3480 unsigned char staged_status, const char *relpath,
3481 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3482 struct got_object_id *commit_id)
3484 struct collect_commitables_arg *a = arg;
3485 const struct got_error *err = NULL;
3486 struct got_commitable *ct = NULL;
3487 struct got_pathlist_entry *new = NULL;
3488 char *parent_path = NULL, *path = NULL;
3489 struct stat sb;
3491 if (a->have_staged_files) {
3492 if (staged_status != GOT_STATUS_MODIFY &&
3493 staged_status != GOT_STATUS_ADD &&
3494 staged_status != GOT_STATUS_DELETE)
3495 return NULL;
3496 } else {
3497 if (status == GOT_STATUS_CONFLICT)
3498 return got_error(GOT_ERR_COMMIT_CONFLICT);
3500 if (status != GOT_STATUS_MODIFY &&
3501 status != GOT_STATUS_MODE_CHANGE &&
3502 status != GOT_STATUS_ADD &&
3503 status != GOT_STATUS_DELETE)
3504 return NULL;
3507 if (asprintf(&path, "/%s", relpath) == -1) {
3508 err = got_error_from_errno("asprintf");
3509 goto done;
3511 if (strcmp(path, "/") == 0) {
3512 parent_path = strdup("");
3513 if (parent_path == NULL)
3514 return got_error_from_errno("strdup");
3515 } else {
3516 err = got_path_dirname(&parent_path, path);
3517 if (err)
3518 return err;
3521 ct = calloc(1, sizeof(*ct));
3522 if (ct == NULL) {
3523 err = got_error_from_errno("calloc");
3524 goto done;
3527 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3528 relpath) == -1) {
3529 err = got_error_from_errno("asprintf");
3530 goto done;
3532 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3533 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3534 } else {
3535 if (lstat(ct->ondisk_path, &sb) != 0) {
3536 err = got_error_from_errno2("lstat", ct->ondisk_path);
3537 goto done;
3539 ct->mode = sb.st_mode;
3542 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3543 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3544 relpath) == -1) {
3545 err = got_error_from_errno("asprintf");
3546 goto done;
3549 ct->status = status;
3550 ct->staged_status = staged_status;
3551 ct->blob_id = NULL; /* will be filled in when blob gets created */
3552 if (ct->status != GOT_STATUS_ADD &&
3553 ct->staged_status != GOT_STATUS_ADD) {
3554 ct->base_blob_id = got_object_id_dup(blob_id);
3555 if (ct->base_blob_id == NULL) {
3556 err = got_error_from_errno("got_object_id_dup");
3557 goto done;
3559 ct->base_commit_id = got_object_id_dup(commit_id);
3560 if (ct->base_commit_id == NULL) {
3561 err = got_error_from_errno("got_object_id_dup");
3562 goto done;
3565 if (ct->staged_status == GOT_STATUS_ADD ||
3566 ct->staged_status == GOT_STATUS_MODIFY) {
3567 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3568 if (ct->staged_blob_id == NULL) {
3569 err = got_error_from_errno("got_object_id_dup");
3570 goto done;
3573 ct->path = strdup(path);
3574 if (ct->path == NULL) {
3575 err = got_error_from_errno("strdup");
3576 goto done;
3578 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3579 done:
3580 if (ct && (err || new == NULL))
3581 free_commitable(ct);
3582 free(parent_path);
3583 free(path);
3584 return err;
3587 static const struct got_error *write_tree(struct got_object_id **,
3588 struct got_tree_object *, const char *, struct got_pathlist_head *,
3589 got_worktree_status_cb status_cb, void *status_arg,
3590 struct got_repository *);
3592 static const struct got_error *
3593 write_subtree(struct got_object_id **new_subtree_id,
3594 struct got_tree_entry *te, const char *parent_path,
3595 struct got_pathlist_head *commitable_paths,
3596 got_worktree_status_cb status_cb, void *status_arg,
3597 struct got_repository *repo)
3599 const struct got_error *err = NULL;
3600 struct got_tree_object *subtree;
3601 char *subpath;
3603 if (asprintf(&subpath, "%s%s%s", parent_path,
3604 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3605 return got_error_from_errno("asprintf");
3607 err = got_object_open_as_tree(&subtree, repo, te->id);
3608 if (err)
3609 return err;
3611 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3612 status_cb, status_arg, repo);
3613 got_object_tree_close(subtree);
3614 free(subpath);
3615 return err;
3618 static const struct got_error *
3619 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3621 const struct got_error *err = NULL;
3622 char *ct_parent_path = NULL;
3624 *match = 0;
3626 if (strchr(ct->in_repo_path, '/') == NULL) {
3627 *match = got_path_is_root_dir(path);
3628 return NULL;
3631 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3632 if (err)
3633 return err;
3634 *match = (strcmp(path, ct_parent_path) == 0);
3635 free(ct_parent_path);
3636 return err;
3639 static mode_t
3640 get_ct_file_mode(struct got_commitable *ct)
3642 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3645 static const struct got_error *
3646 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3647 struct got_tree_entry *te, struct got_commitable *ct)
3649 const struct got_error *err = NULL;
3651 *new_te = NULL;
3653 err = got_object_tree_entry_dup(new_te, te);
3654 if (err)
3655 goto done;
3657 (*new_te)->mode = get_ct_file_mode(ct);
3659 free((*new_te)->id);
3660 if (ct->staged_status == GOT_STATUS_MODIFY)
3661 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3662 else
3663 (*new_te)->id = got_object_id_dup(ct->blob_id);
3664 if ((*new_te)->id == NULL) {
3665 err = got_error_from_errno("got_object_id_dup");
3666 goto done;
3668 done:
3669 if (err && *new_te) {
3670 got_object_tree_entry_close(*new_te);
3671 *new_te = NULL;
3673 return err;
3676 static const struct got_error *
3677 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3678 struct got_commitable *ct)
3680 const struct got_error *err = NULL;
3681 char *ct_name;
3683 *new_te = NULL;
3685 *new_te = calloc(1, sizeof(**new_te));
3686 if (*new_te == NULL)
3687 return got_error_from_errno("calloc");
3689 ct_name = basename(ct->path);
3690 if (ct_name == NULL) {
3691 err = got_error_from_errno2("basename", ct->path);
3692 goto done;
3694 (*new_te)->name = strdup(ct_name);
3695 if ((*new_te)->name == NULL) {
3696 err = got_error_from_errno("strdup");
3697 goto done;
3700 (*new_te)->mode = get_ct_file_mode(ct);
3702 if (ct->staged_status == GOT_STATUS_ADD)
3703 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3704 else
3705 (*new_te)->id = got_object_id_dup(ct->blob_id);
3706 if ((*new_te)->id == NULL) {
3707 err = got_error_from_errno("got_object_id_dup");
3708 goto done;
3710 done:
3711 if (err && *new_te) {
3712 got_object_tree_entry_close(*new_te);
3713 *new_te = NULL;
3715 return err;
3718 static const struct got_error *
3719 insert_tree_entry(struct got_tree_entry *new_te,
3720 struct got_pathlist_head *paths)
3722 const struct got_error *err = NULL;
3723 struct got_pathlist_entry *new_pe;
3725 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3726 if (err)
3727 return err;
3728 if (new_pe == NULL)
3729 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3730 return NULL;
3733 static const struct got_error *
3734 report_ct_status(struct got_commitable *ct,
3735 got_worktree_status_cb status_cb, void *status_arg)
3737 const char *ct_path = ct->path;
3738 unsigned char status;
3740 while (ct_path[0] == '/')
3741 ct_path++;
3743 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3744 status = ct->staged_status;
3745 else
3746 status = ct->status;
3748 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3749 ct_path, ct->blob_id, NULL, NULL);
3752 static const struct got_error *
3753 match_modified_subtree(int *modified, struct got_tree_entry *te,
3754 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3756 const struct got_error *err = NULL;
3757 struct got_pathlist_entry *pe;
3758 char *te_path;
3760 *modified = 0;
3762 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3763 got_path_is_root_dir(base_tree_path) ? "" : "/",
3764 te->name) == -1)
3765 return got_error_from_errno("asprintf");
3767 TAILQ_FOREACH(pe, commitable_paths, entry) {
3768 struct got_commitable *ct = pe->data;
3769 *modified = got_path_is_child(ct->in_repo_path, te_path,
3770 strlen(te_path));
3771 if (*modified)
3772 break;
3775 free(te_path);
3776 return err;
3779 static const struct got_error *
3780 match_deleted_or_modified_ct(struct got_commitable **ctp,
3781 struct got_tree_entry *te, const char *base_tree_path,
3782 struct got_pathlist_head *commitable_paths)
3784 const struct got_error *err = NULL;
3785 struct got_pathlist_entry *pe;
3787 *ctp = NULL;
3789 TAILQ_FOREACH(pe, commitable_paths, entry) {
3790 struct got_commitable *ct = pe->data;
3791 char *ct_name = NULL;
3792 int path_matches;
3794 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3795 if (ct->status != GOT_STATUS_MODIFY &&
3796 ct->status != GOT_STATUS_MODE_CHANGE &&
3797 ct->status != GOT_STATUS_DELETE)
3798 continue;
3799 } else {
3800 if (ct->staged_status != GOT_STATUS_MODIFY &&
3801 ct->staged_status != GOT_STATUS_DELETE)
3802 continue;
3805 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3806 continue;
3808 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3809 if (err)
3810 return err;
3811 if (!path_matches)
3812 continue;
3814 ct_name = basename(pe->path);
3815 if (ct_name == NULL)
3816 return got_error_from_errno2("basename", pe->path);
3818 if (strcmp(te->name, ct_name) != 0)
3819 continue;
3821 *ctp = ct;
3822 break;
3825 return err;
3828 static const struct got_error *
3829 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3830 const char *child_path, const char *path_base_tree,
3831 struct got_pathlist_head *commitable_paths,
3832 got_worktree_status_cb status_cb, void *status_arg,
3833 struct got_repository *repo)
3835 const struct got_error *err = NULL;
3836 struct got_tree_entry *new_te;
3837 char *subtree_path;
3839 *new_tep = NULL;
3841 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3842 got_path_is_root_dir(path_base_tree) ? "" : "/",
3843 child_path) == -1)
3844 return got_error_from_errno("asprintf");
3846 new_te = calloc(1, sizeof(*new_te));
3847 if (new_te == NULL)
3848 return got_error_from_errno("calloc");
3849 new_te->mode = S_IFDIR;
3850 new_te->name = strdup(child_path);
3851 if (new_te->name == NULL) {
3852 err = got_error_from_errno("strdup");
3853 got_object_tree_entry_close(new_te);
3854 goto done;
3856 err = write_tree(&new_te->id, NULL, subtree_path,
3857 commitable_paths, status_cb, status_arg, repo);
3858 if (err) {
3859 got_object_tree_entry_close(new_te);
3860 goto done;
3862 done:
3863 free(subtree_path);
3864 if (err == NULL)
3865 *new_tep = new_te;
3866 return err;
3869 static const struct got_error *
3870 write_tree(struct got_object_id **new_tree_id,
3871 struct got_tree_object *base_tree, const char *path_base_tree,
3872 struct got_pathlist_head *commitable_paths,
3873 got_worktree_status_cb status_cb, void *status_arg,
3874 struct got_repository *repo)
3876 const struct got_error *err = NULL;
3877 const struct got_tree_entries *base_entries = NULL;
3878 struct got_pathlist_head paths;
3879 struct got_tree_entries new_tree_entries;
3880 struct got_tree_entry *te, *new_te = NULL;
3881 struct got_pathlist_entry *pe;
3883 TAILQ_INIT(&paths);
3884 new_tree_entries.nentries = 0;
3885 SIMPLEQ_INIT(&new_tree_entries.head);
3887 /* Insert, and recurse into, newly added entries first. */
3888 TAILQ_FOREACH(pe, commitable_paths, entry) {
3889 struct got_commitable *ct = pe->data;
3890 char *child_path = NULL, *slash;
3892 if ((ct->status != GOT_STATUS_ADD &&
3893 ct->staged_status != GOT_STATUS_ADD) ||
3894 (ct->flags & GOT_COMMITABLE_ADDED))
3895 continue;
3897 if (!got_path_is_child(pe->path, path_base_tree,
3898 strlen(path_base_tree)))
3899 continue;
3901 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3902 pe->path);
3903 if (err)
3904 goto done;
3906 slash = strchr(child_path, '/');
3907 if (slash == NULL) {
3908 err = alloc_added_blob_tree_entry(&new_te, ct);
3909 if (err)
3910 goto done;
3911 err = report_ct_status(ct, status_cb, status_arg);
3912 if (err)
3913 goto done;
3914 ct->flags |= GOT_COMMITABLE_ADDED;
3915 err = insert_tree_entry(new_te, &paths);
3916 if (err)
3917 goto done;
3918 } else {
3919 *slash = '\0'; /* trim trailing path components */
3920 if (base_tree == NULL ||
3921 got_object_tree_find_entry(base_tree, child_path)
3922 == NULL) {
3923 err = make_subtree_for_added_blob(&new_te,
3924 child_path, path_base_tree,
3925 commitable_paths, status_cb, status_arg,
3926 repo);
3927 if (err)
3928 goto done;
3929 err = insert_tree_entry(new_te, &paths);
3930 if (err)
3931 goto done;
3936 if (base_tree) {
3937 /* Handle modified and deleted entries. */
3938 base_entries = got_object_tree_get_entries(base_tree);
3939 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3940 struct got_commitable *ct = NULL;
3942 if (got_object_tree_entry_is_submodule(te)) {
3943 /* Entry is a submodule; just copy it. */
3944 err = got_object_tree_entry_dup(&new_te, te);
3945 if (err)
3946 goto done;
3947 err = insert_tree_entry(new_te, &paths);
3948 if (err)
3949 goto done;
3950 continue;
3953 if (S_ISDIR(te->mode)) {
3954 int modified;
3955 err = got_object_tree_entry_dup(&new_te, te);
3956 if (err)
3957 goto done;
3958 err = match_modified_subtree(&modified, te,
3959 path_base_tree, commitable_paths);
3960 if (err)
3961 goto done;
3962 /* Avoid recursion into unmodified subtrees. */
3963 if (modified) {
3964 free(new_te->id);
3965 err = write_subtree(&new_te->id, te,
3966 path_base_tree, commitable_paths,
3967 status_cb, status_arg, repo);
3968 if (err)
3969 goto done;
3971 err = insert_tree_entry(new_te, &paths);
3972 if (err)
3973 goto done;
3974 continue;
3977 err = match_deleted_or_modified_ct(&ct, te,
3978 path_base_tree, commitable_paths);
3979 if (err)
3980 goto done;
3981 if (ct) {
3982 /* NB: Deleted entries get dropped here. */
3983 if (ct->status == GOT_STATUS_MODIFY ||
3984 ct->status == GOT_STATUS_MODE_CHANGE ||
3985 ct->staged_status == GOT_STATUS_MODIFY) {
3986 err = alloc_modified_blob_tree_entry(
3987 &new_te, te, ct);
3988 if (err)
3989 goto done;
3990 err = insert_tree_entry(new_te, &paths);
3991 if (err)
3992 goto done;
3994 err = report_ct_status(ct, status_cb,
3995 status_arg);
3996 if (err)
3997 goto done;
3998 } else {
3999 /* Entry is unchanged; just copy it. */
4000 err = got_object_tree_entry_dup(&new_te, te);
4001 if (err)
4002 goto done;
4003 err = insert_tree_entry(new_te, &paths);
4004 if (err)
4005 goto done;
4010 /* Write new list of entries; deleted entries have been dropped. */
4011 TAILQ_FOREACH(pe, &paths, entry) {
4012 struct got_tree_entry *te = pe->data;
4013 new_tree_entries.nentries++;
4014 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
4016 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
4017 done:
4018 got_object_tree_entries_close(&new_tree_entries);
4019 got_pathlist_free(&paths);
4020 return err;
4023 static const struct got_error *
4024 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4025 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4026 int have_staged_files)
4028 const struct got_error *err = NULL;
4029 struct got_pathlist_entry *pe;
4031 TAILQ_FOREACH(pe, commitable_paths, entry) {
4032 struct got_fileindex_entry *ie;
4033 struct got_commitable *ct = pe->data;
4035 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4036 if (ie) {
4037 if (ct->status == GOT_STATUS_DELETE ||
4038 ct->staged_status == GOT_STATUS_DELETE) {
4039 got_fileindex_entry_remove(fileindex, ie);
4040 got_fileindex_entry_free(ie);
4041 } else if (ct->staged_status == GOT_STATUS_ADD ||
4042 ct->staged_status == GOT_STATUS_MODIFY) {
4043 got_fileindex_entry_stage_set(ie,
4044 GOT_FILEIDX_STAGE_NONE);
4045 err = got_fileindex_entry_update(ie,
4046 ct->ondisk_path, ct->staged_blob_id->sha1,
4047 new_base_commit_id->sha1,
4048 !have_staged_files);
4049 } else
4050 err = got_fileindex_entry_update(ie,
4051 ct->ondisk_path, ct->blob_id->sha1,
4052 new_base_commit_id->sha1,
4053 !have_staged_files);
4054 } else {
4055 err = got_fileindex_entry_alloc(&ie,
4056 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4057 new_base_commit_id->sha1);
4058 if (err)
4059 break;
4060 err = got_fileindex_entry_add(fileindex, ie);
4061 if (err)
4062 break;
4065 return err;
4069 static const struct got_error *
4070 check_out_of_date(const char *in_repo_path, unsigned char status,
4071 unsigned char staged_status, struct got_object_id *base_blob_id,
4072 struct got_object_id *base_commit_id,
4073 struct got_object_id *head_commit_id, struct got_repository *repo,
4074 int ood_errcode)
4076 const struct got_error *err = NULL;
4077 struct got_object_id *id = NULL;
4079 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4080 /* Trivial case: base commit == head commit */
4081 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4082 return NULL;
4084 * Ensure file content which local changes were based
4085 * on matches file content in the branch head.
4087 err = got_object_id_by_path(&id, repo, head_commit_id,
4088 in_repo_path);
4089 if (err) {
4090 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4091 err = got_error(ood_errcode);
4092 goto done;
4093 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4094 err = got_error(ood_errcode);
4095 } else {
4096 /* Require that added files don't exist in the branch head. */
4097 err = got_object_id_by_path(&id, repo, head_commit_id,
4098 in_repo_path);
4099 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4100 goto done;
4101 err = id ? got_error(ood_errcode) : NULL;
4103 done:
4104 free(id);
4105 return err;
4108 const struct got_error *
4109 commit_worktree(struct got_object_id **new_commit_id,
4110 struct got_pathlist_head *commitable_paths,
4111 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4112 const char *author, const char *committer,
4113 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4114 got_worktree_status_cb status_cb, void *status_arg,
4115 struct got_repository *repo)
4117 const struct got_error *err = NULL, *unlockerr = NULL;
4118 struct got_pathlist_entry *pe;
4119 const char *head_ref_name = NULL;
4120 struct got_commit_object *head_commit = NULL;
4121 struct got_reference *head_ref2 = NULL;
4122 struct got_object_id *head_commit_id2 = NULL;
4123 struct got_tree_object *head_tree = NULL;
4124 struct got_object_id *new_tree_id = NULL;
4125 struct got_object_id_queue parent_ids;
4126 struct got_object_qid *pid = NULL;
4127 char *logmsg = NULL;
4129 *new_commit_id = NULL;
4131 SIMPLEQ_INIT(&parent_ids);
4133 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4134 if (err)
4135 goto done;
4137 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4138 if (err)
4139 goto done;
4141 if (commit_msg_cb != NULL) {
4142 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4143 if (err)
4144 goto done;
4147 if (logmsg == NULL || strlen(logmsg) == 0) {
4148 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4149 goto done;
4152 /* Create blobs from added and modified files and record their IDs. */
4153 TAILQ_FOREACH(pe, commitable_paths, entry) {
4154 struct got_commitable *ct = pe->data;
4155 char *ondisk_path;
4157 /* Blobs for staged files already exist. */
4158 if (ct->staged_status == GOT_STATUS_ADD ||
4159 ct->staged_status == GOT_STATUS_MODIFY)
4160 continue;
4162 if (ct->status != GOT_STATUS_ADD &&
4163 ct->status != GOT_STATUS_MODIFY &&
4164 ct->status != GOT_STATUS_MODE_CHANGE)
4165 continue;
4167 if (asprintf(&ondisk_path, "%s/%s",
4168 worktree->root_path, pe->path) == -1) {
4169 err = got_error_from_errno("asprintf");
4170 goto done;
4172 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4173 free(ondisk_path);
4174 if (err)
4175 goto done;
4178 /* Recursively write new tree objects. */
4179 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4180 status_cb, status_arg, repo);
4181 if (err)
4182 goto done;
4184 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4185 if (err)
4186 goto done;
4187 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4188 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4189 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4190 got_object_qid_free(pid);
4191 if (logmsg != NULL)
4192 free(logmsg);
4193 if (err)
4194 goto done;
4196 /* Check if a concurrent commit to our branch has occurred. */
4197 head_ref_name = got_worktree_get_head_ref_name(worktree);
4198 if (head_ref_name == NULL) {
4199 err = got_error_from_errno("got_worktree_get_head_ref_name");
4200 goto done;
4202 /* Lock the reference here to prevent concurrent modification. */
4203 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4204 if (err)
4205 goto done;
4206 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4207 if (err)
4208 goto done;
4209 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4210 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4211 goto done;
4213 /* Update branch head in repository. */
4214 err = got_ref_change_ref(head_ref2, *new_commit_id);
4215 if (err)
4216 goto done;
4217 err = got_ref_write(head_ref2, repo);
4218 if (err)
4219 goto done;
4221 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4222 if (err)
4223 goto done;
4225 err = ref_base_commit(worktree, repo);
4226 if (err)
4227 goto done;
4228 done:
4229 if (head_tree)
4230 got_object_tree_close(head_tree);
4231 if (head_commit)
4232 got_object_commit_close(head_commit);
4233 free(head_commit_id2);
4234 if (head_ref2) {
4235 unlockerr = got_ref_unlock(head_ref2);
4236 if (unlockerr && err == NULL)
4237 err = unlockerr;
4238 got_ref_close(head_ref2);
4240 return err;
4243 static const struct got_error *
4244 check_path_is_commitable(const char *path,
4245 struct got_pathlist_head *commitable_paths)
4247 struct got_pathlist_entry *cpe = NULL;
4248 size_t path_len = strlen(path);
4250 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4251 struct got_commitable *ct = cpe->data;
4252 const char *ct_path = ct->path;
4254 while (ct_path[0] == '/')
4255 ct_path++;
4257 if (strcmp(path, ct_path) == 0 ||
4258 got_path_is_child(ct_path, path, path_len))
4259 break;
4262 if (cpe == NULL)
4263 return got_error_path(path, GOT_ERR_BAD_PATH);
4265 return NULL;
4268 static const struct got_error *
4269 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4271 int *have_staged_files = arg;
4273 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4274 *have_staged_files = 1;
4275 return got_error(GOT_ERR_CANCELLED);
4278 return NULL;
4281 static const struct got_error *
4282 check_non_staged_files(struct got_fileindex *fileindex,
4283 struct got_pathlist_head *paths)
4285 struct got_pathlist_entry *pe;
4286 struct got_fileindex_entry *ie;
4288 TAILQ_FOREACH(pe, paths, entry) {
4289 if (pe->path[0] == '\0')
4290 continue;
4291 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4292 if (ie == NULL)
4293 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4294 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4295 return got_error_path(pe->path,
4296 GOT_ERR_FILE_NOT_STAGED);
4299 return NULL;
4302 const struct got_error *
4303 got_worktree_commit(struct got_object_id **new_commit_id,
4304 struct got_worktree *worktree, struct got_pathlist_head *paths,
4305 const char *author, const char *committer,
4306 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4307 got_worktree_status_cb status_cb, void *status_arg,
4308 struct got_repository *repo)
4310 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4311 struct got_fileindex *fileindex = NULL;
4312 char *fileindex_path = NULL;
4313 struct got_pathlist_head commitable_paths;
4314 struct collect_commitables_arg cc_arg;
4315 struct got_pathlist_entry *pe;
4316 struct got_reference *head_ref = NULL;
4317 struct got_object_id *head_commit_id = NULL;
4318 int have_staged_files = 0;
4320 *new_commit_id = NULL;
4322 TAILQ_INIT(&commitable_paths);
4324 err = lock_worktree(worktree, LOCK_EX);
4325 if (err)
4326 goto done;
4328 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4329 if (err)
4330 goto done;
4332 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4333 if (err)
4334 goto done;
4336 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4337 if (err)
4338 goto done;
4340 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4341 &have_staged_files);
4342 if (err && err->code != GOT_ERR_CANCELLED)
4343 goto done;
4344 if (have_staged_files) {
4345 err = check_non_staged_files(fileindex, paths);
4346 if (err)
4347 goto done;
4350 cc_arg.commitable_paths = &commitable_paths;
4351 cc_arg.worktree = worktree;
4352 cc_arg.repo = repo;
4353 cc_arg.have_staged_files = have_staged_files;
4354 TAILQ_FOREACH(pe, paths, entry) {
4355 err = worktree_status(worktree, pe->path, fileindex, repo,
4356 collect_commitables, &cc_arg, NULL, NULL);
4357 if (err)
4358 goto done;
4361 if (TAILQ_EMPTY(&commitable_paths)) {
4362 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4363 goto done;
4366 TAILQ_FOREACH(pe, paths, entry) {
4367 err = check_path_is_commitable(pe->path, &commitable_paths);
4368 if (err)
4369 goto done;
4372 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4373 struct got_commitable *ct = pe->data;
4374 const char *ct_path = ct->in_repo_path;
4376 while (ct_path[0] == '/')
4377 ct_path++;
4378 err = check_out_of_date(ct_path, ct->status,
4379 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4380 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4381 if (err)
4382 goto done;
4386 err = commit_worktree(new_commit_id, &commitable_paths,
4387 head_commit_id, worktree, author, committer,
4388 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4389 if (err)
4390 goto done;
4392 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4393 fileindex, have_staged_files);
4394 sync_err = sync_fileindex(fileindex, fileindex_path);
4395 if (sync_err && err == NULL)
4396 err = sync_err;
4397 done:
4398 if (fileindex)
4399 got_fileindex_free(fileindex);
4400 free(fileindex_path);
4401 unlockerr = lock_worktree(worktree, LOCK_SH);
4402 if (unlockerr && err == NULL)
4403 err = unlockerr;
4404 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4405 struct got_commitable *ct = pe->data;
4406 free_commitable(ct);
4408 got_pathlist_free(&commitable_paths);
4409 return err;
4412 const char *
4413 got_commitable_get_path(struct got_commitable *ct)
4415 return ct->path;
4418 unsigned int
4419 got_commitable_get_status(struct got_commitable *ct)
4421 return ct->status;
4424 struct check_rebase_ok_arg {
4425 struct got_worktree *worktree;
4426 struct got_repository *repo;
4429 static const struct got_error *
4430 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4432 const struct got_error *err = NULL;
4433 struct check_rebase_ok_arg *a = arg;
4434 unsigned char status;
4435 struct stat sb;
4436 char *ondisk_path;
4438 /* Reject rebase of a work tree with mixed base commits. */
4439 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4440 SHA1_DIGEST_LENGTH))
4441 return got_error(GOT_ERR_MIXED_COMMITS);
4443 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4444 == -1)
4445 return got_error_from_errno("asprintf");
4447 /* Reject rebase of a work tree with modified or staged files. */
4448 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4449 free(ondisk_path);
4450 if (err)
4451 return err;
4453 if (status != GOT_STATUS_NO_CHANGE)
4454 return got_error(GOT_ERR_MODIFIED);
4455 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4456 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4458 return NULL;
4461 const struct got_error *
4462 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4463 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4464 struct got_worktree *worktree, struct got_reference *branch,
4465 struct got_repository *repo)
4467 const struct got_error *err = NULL;
4468 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4469 char *branch_ref_name = NULL;
4470 char *fileindex_path = NULL;
4471 struct check_rebase_ok_arg ok_arg;
4472 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4474 *new_base_branch_ref = NULL;
4475 *tmp_branch = NULL;
4476 *fileindex = NULL;
4478 err = lock_worktree(worktree, LOCK_EX);
4479 if (err)
4480 return err;
4482 err = open_fileindex(fileindex, &fileindex_path, worktree);
4483 if (err)
4484 goto done;
4486 ok_arg.worktree = worktree;
4487 ok_arg.repo = repo;
4488 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4489 &ok_arg);
4490 if (err)
4491 goto done;
4493 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4494 if (err)
4495 goto done;
4497 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4498 if (err)
4499 goto done;
4501 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4502 if (err)
4503 goto done;
4505 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4506 0);
4507 if (err)
4508 goto done;
4510 err = got_ref_alloc_symref(new_base_branch_ref,
4511 new_base_branch_ref_name, wt_branch);
4512 if (err)
4513 goto done;
4514 err = got_ref_write(*new_base_branch_ref, repo);
4515 if (err)
4516 goto done;
4518 /* TODO Lock original branch's ref while rebasing? */
4520 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4521 if (err)
4522 goto done;
4524 err = got_ref_write(branch_ref, repo);
4525 if (err)
4526 goto done;
4528 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4529 worktree->base_commit_id);
4530 if (err)
4531 goto done;
4532 err = got_ref_write(*tmp_branch, repo);
4533 if (err)
4534 goto done;
4536 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4537 if (err)
4538 goto done;
4539 done:
4540 free(fileindex_path);
4541 free(tmp_branch_name);
4542 free(new_base_branch_ref_name);
4543 free(branch_ref_name);
4544 if (branch_ref)
4545 got_ref_close(branch_ref);
4546 if (wt_branch)
4547 got_ref_close(wt_branch);
4548 if (err) {
4549 if (*new_base_branch_ref) {
4550 got_ref_close(*new_base_branch_ref);
4551 *new_base_branch_ref = NULL;
4553 if (*tmp_branch) {
4554 got_ref_close(*tmp_branch);
4555 *tmp_branch = NULL;
4557 if (*fileindex) {
4558 got_fileindex_free(*fileindex);
4559 *fileindex = NULL;
4561 lock_worktree(worktree, LOCK_SH);
4563 return err;
4566 const struct got_error *
4567 got_worktree_rebase_continue(struct got_object_id **commit_id,
4568 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4569 struct got_reference **branch, struct got_fileindex **fileindex,
4570 struct got_worktree *worktree, struct got_repository *repo)
4572 const struct got_error *err;
4573 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4574 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4575 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4576 char *fileindex_path = NULL;
4577 int have_staged_files = 0;
4579 *commit_id = NULL;
4580 *new_base_branch = NULL;
4581 *tmp_branch = NULL;
4582 *branch = NULL;
4583 *fileindex = NULL;
4585 err = lock_worktree(worktree, LOCK_EX);
4586 if (err)
4587 return err;
4589 err = open_fileindex(fileindex, &fileindex_path, worktree);
4590 if (err)
4591 goto done;
4593 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4594 &have_staged_files);
4595 if (err && err->code != GOT_ERR_CANCELLED)
4596 goto done;
4597 if (have_staged_files) {
4598 err = got_error(GOT_ERR_STAGED_PATHS);
4599 goto done;
4602 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4603 if (err)
4604 goto done;
4606 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4607 if (err)
4608 goto done;
4610 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4611 if (err)
4612 goto done;
4614 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4615 if (err)
4616 goto done;
4618 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4619 if (err)
4620 goto done;
4622 err = got_ref_open(branch, repo,
4623 got_ref_get_symref_target(branch_ref), 0);
4624 if (err)
4625 goto done;
4627 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4628 if (err)
4629 goto done;
4631 err = got_ref_resolve(commit_id, repo, commit_ref);
4632 if (err)
4633 goto done;
4635 err = got_ref_open(new_base_branch, repo,
4636 new_base_branch_ref_name, 0);
4637 if (err)
4638 goto done;
4640 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4641 if (err)
4642 goto done;
4643 done:
4644 free(commit_ref_name);
4645 free(branch_ref_name);
4646 free(fileindex_path);
4647 if (commit_ref)
4648 got_ref_close(commit_ref);
4649 if (branch_ref)
4650 got_ref_close(branch_ref);
4651 if (err) {
4652 free(*commit_id);
4653 *commit_id = NULL;
4654 if (*tmp_branch) {
4655 got_ref_close(*tmp_branch);
4656 *tmp_branch = NULL;
4658 if (*new_base_branch) {
4659 got_ref_close(*new_base_branch);
4660 *new_base_branch = NULL;
4662 if (*branch) {
4663 got_ref_close(*branch);
4664 *branch = NULL;
4666 if (*fileindex) {
4667 got_fileindex_free(*fileindex);
4668 *fileindex = NULL;
4670 lock_worktree(worktree, LOCK_SH);
4672 return err;
4675 const struct got_error *
4676 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4678 const struct got_error *err;
4679 char *tmp_branch_name = NULL;
4681 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4682 if (err)
4683 return err;
4685 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4686 free(tmp_branch_name);
4687 return NULL;
4690 static const struct got_error *
4691 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4692 char **logmsg, void *arg)
4694 *logmsg = arg;
4695 return NULL;
4698 static const struct got_error *
4699 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4700 const char *path, struct got_object_id *blob_id,
4701 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4703 return NULL;
4706 struct collect_merged_paths_arg {
4707 got_worktree_checkout_cb progress_cb;
4708 void *progress_arg;
4709 struct got_pathlist_head *merged_paths;
4712 static const struct got_error *
4713 collect_merged_paths(void *arg, unsigned char status, const char *path)
4715 const struct got_error *err;
4716 struct collect_merged_paths_arg *a = arg;
4717 char *p;
4718 struct got_pathlist_entry *new;
4720 err = (*a->progress_cb)(a->progress_arg, status, path);
4721 if (err)
4722 return err;
4724 if (status != GOT_STATUS_MERGE &&
4725 status != GOT_STATUS_ADD &&
4726 status != GOT_STATUS_DELETE &&
4727 status != GOT_STATUS_CONFLICT)
4728 return NULL;
4730 p = strdup(path);
4731 if (p == NULL)
4732 return got_error_from_errno("strdup");
4734 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4735 if (err || new == NULL)
4736 free(p);
4737 return err;
4740 void
4741 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4743 struct got_pathlist_entry *pe;
4745 TAILQ_FOREACH(pe, merged_paths, entry)
4746 free((char *)pe->path);
4748 got_pathlist_free(merged_paths);
4751 static const struct got_error *
4752 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4753 struct got_repository *repo)
4755 const struct got_error *err;
4756 struct got_reference *commit_ref = NULL;
4758 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4759 if (err) {
4760 if (err->code != GOT_ERR_NOT_REF)
4761 goto done;
4762 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4763 if (err)
4764 goto done;
4765 err = got_ref_write(commit_ref, repo);
4766 if (err)
4767 goto done;
4768 } else {
4769 struct got_object_id *stored_id;
4770 int cmp;
4772 err = got_ref_resolve(&stored_id, repo, commit_ref);
4773 if (err)
4774 goto done;
4775 cmp = got_object_id_cmp(commit_id, stored_id);
4776 free(stored_id);
4777 if (cmp != 0) {
4778 err = got_error(GOT_ERR_REBASE_COMMITID);
4779 goto done;
4782 done:
4783 if (commit_ref)
4784 got_ref_close(commit_ref);
4785 return err;
4788 static const struct got_error *
4789 rebase_merge_files(struct got_pathlist_head *merged_paths,
4790 const char *commit_ref_name, struct got_worktree *worktree,
4791 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4792 struct got_object_id *commit_id, struct got_repository *repo,
4793 got_worktree_checkout_cb progress_cb, void *progress_arg,
4794 got_cancel_cb cancel_cb, void *cancel_arg)
4796 const struct got_error *err;
4797 struct got_reference *commit_ref = NULL;
4798 struct collect_merged_paths_arg cmp_arg;
4799 char *fileindex_path;
4801 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4803 err = get_fileindex_path(&fileindex_path, worktree);
4804 if (err)
4805 return err;
4807 cmp_arg.progress_cb = progress_cb;
4808 cmp_arg.progress_arg = progress_arg;
4809 cmp_arg.merged_paths = merged_paths;
4810 err = merge_files(worktree, fileindex, fileindex_path,
4811 parent_commit_id, commit_id, repo, collect_merged_paths,
4812 &cmp_arg, cancel_cb, cancel_arg);
4813 if (commit_ref)
4814 got_ref_close(commit_ref);
4815 return err;
4818 const struct got_error *
4819 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4820 struct got_worktree *worktree, struct got_fileindex *fileindex,
4821 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4822 struct got_repository *repo,
4823 got_worktree_checkout_cb progress_cb, void *progress_arg,
4824 got_cancel_cb cancel_cb, void *cancel_arg)
4826 const struct got_error *err;
4827 char *commit_ref_name;
4829 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4830 if (err)
4831 return err;
4833 err = store_commit_id(commit_ref_name, commit_id, repo);
4834 if (err)
4835 goto done;
4837 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4838 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4839 progress_arg, cancel_cb, cancel_arg);
4840 done:
4841 free(commit_ref_name);
4842 return err;
4845 const struct got_error *
4846 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4847 struct got_worktree *worktree, struct got_fileindex *fileindex,
4848 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4849 struct got_repository *repo,
4850 got_worktree_checkout_cb progress_cb, void *progress_arg,
4851 got_cancel_cb cancel_cb, void *cancel_arg)
4853 const struct got_error *err;
4854 char *commit_ref_name;
4856 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4857 if (err)
4858 return err;
4860 err = store_commit_id(commit_ref_name, commit_id, repo);
4861 if (err)
4862 goto done;
4864 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4865 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4866 progress_arg, cancel_cb, cancel_arg);
4867 done:
4868 free(commit_ref_name);
4869 return err;
4872 static const struct got_error *
4873 rebase_commit(struct got_object_id **new_commit_id,
4874 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4875 struct got_worktree *worktree, struct got_fileindex *fileindex,
4876 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4877 const char *new_logmsg, struct got_repository *repo)
4879 const struct got_error *err, *sync_err;
4880 struct got_pathlist_head commitable_paths;
4881 struct collect_commitables_arg cc_arg;
4882 char *fileindex_path = NULL;
4883 struct got_reference *head_ref = NULL;
4884 struct got_object_id *head_commit_id = NULL;
4885 char *logmsg = NULL;
4887 TAILQ_INIT(&commitable_paths);
4888 *new_commit_id = NULL;
4890 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4892 err = get_fileindex_path(&fileindex_path, worktree);
4893 if (err)
4894 return err;
4896 cc_arg.commitable_paths = &commitable_paths;
4897 cc_arg.worktree = worktree;
4898 cc_arg.repo = repo;
4899 cc_arg.have_staged_files = 0;
4901 * If possible get the status of individual files directly to
4902 * avoid crawling the entire work tree once per rebased commit.
4903 * TODO: Ideally, merged_paths would contain a list of commitables
4904 * we could use so we could skip worktree_status() entirely.
4906 if (merged_paths) {
4907 struct got_pathlist_entry *pe;
4908 if (TAILQ_EMPTY(merged_paths)) {
4909 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4910 goto done;
4912 TAILQ_FOREACH(pe, merged_paths, entry) {
4913 err = worktree_status(worktree, pe->path, fileindex,
4914 repo, collect_commitables, &cc_arg, NULL, NULL);
4915 if (err)
4916 goto done;
4918 } else {
4919 err = worktree_status(worktree, "", fileindex, repo,
4920 collect_commitables, &cc_arg, NULL, NULL);
4921 if (err)
4922 goto done;
4925 if (TAILQ_EMPTY(&commitable_paths)) {
4926 /* No-op change; commit will be elided. */
4927 err = got_ref_delete(commit_ref, repo);
4928 if (err)
4929 goto done;
4930 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4931 goto done;
4934 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4935 if (err)
4936 goto done;
4938 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4939 if (err)
4940 goto done;
4942 if (new_logmsg) {
4943 logmsg = strdup(new_logmsg);
4944 if (logmsg == NULL) {
4945 err = got_error_from_errno("strdup");
4946 goto done;
4948 } else {
4949 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4950 if (err)
4951 goto done;
4954 /* NB: commit_worktree will call free(logmsg) */
4955 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4956 worktree, got_object_commit_get_author(orig_commit),
4957 got_object_commit_get_committer(orig_commit),
4958 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4959 if (err)
4960 goto done;
4962 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4963 if (err)
4964 goto done;
4966 err = got_ref_delete(commit_ref, repo);
4967 if (err)
4968 goto done;
4970 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4971 fileindex, 0);
4972 sync_err = sync_fileindex(fileindex, fileindex_path);
4973 if (sync_err && err == NULL)
4974 err = sync_err;
4975 done:
4976 free(fileindex_path);
4977 free(head_commit_id);
4978 if (head_ref)
4979 got_ref_close(head_ref);
4980 if (err) {
4981 free(*new_commit_id);
4982 *new_commit_id = NULL;
4984 return err;
4987 const struct got_error *
4988 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4989 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4990 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4991 struct got_commit_object *orig_commit,
4992 struct got_object_id *orig_commit_id, struct got_repository *repo)
4994 const struct got_error *err;
4995 char *commit_ref_name;
4996 struct got_reference *commit_ref = NULL;
4997 struct got_object_id *commit_id = NULL;
4999 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5000 if (err)
5001 return err;
5003 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5004 if (err)
5005 goto done;
5006 err = got_ref_resolve(&commit_id, repo, commit_ref);
5007 if (err)
5008 goto done;
5009 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5010 err = got_error(GOT_ERR_REBASE_COMMITID);
5011 goto done;
5014 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5015 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5016 done:
5017 if (commit_ref)
5018 got_ref_close(commit_ref);
5019 free(commit_ref_name);
5020 free(commit_id);
5021 return err;
5024 const struct got_error *
5025 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5026 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5027 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5028 struct got_commit_object *orig_commit,
5029 struct got_object_id *orig_commit_id, const char *new_logmsg,
5030 struct got_repository *repo)
5032 const struct got_error *err;
5033 char *commit_ref_name;
5034 struct got_reference *commit_ref = NULL;
5035 struct got_object_id *commit_id = NULL;
5037 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5038 if (err)
5039 return err;
5041 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5042 if (err)
5043 goto done;
5044 err = got_ref_resolve(&commit_id, repo, commit_ref);
5045 if (err)
5046 goto done;
5047 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5048 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5049 goto done;
5052 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5053 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5054 done:
5055 if (commit_ref)
5056 got_ref_close(commit_ref);
5057 free(commit_ref_name);
5058 free(commit_id);
5059 return err;
5062 const struct got_error *
5063 got_worktree_rebase_postpone(struct got_worktree *worktree,
5064 struct got_fileindex *fileindex)
5066 if (fileindex)
5067 got_fileindex_free(fileindex);
5068 return lock_worktree(worktree, LOCK_SH);
5071 static const struct got_error *
5072 delete_ref(const char *name, struct got_repository *repo)
5074 const struct got_error *err;
5075 struct got_reference *ref;
5077 err = got_ref_open(&ref, repo, name, 0);
5078 if (err) {
5079 if (err->code == GOT_ERR_NOT_REF)
5080 return NULL;
5081 return err;
5084 err = got_ref_delete(ref, repo);
5085 got_ref_close(ref);
5086 return err;
5089 static const struct got_error *
5090 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5092 const struct got_error *err;
5093 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5094 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5096 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5097 if (err)
5098 goto done;
5099 err = delete_ref(tmp_branch_name, repo);
5100 if (err)
5101 goto done;
5103 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5104 if (err)
5105 goto done;
5106 err = delete_ref(new_base_branch_ref_name, repo);
5107 if (err)
5108 goto done;
5110 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5111 if (err)
5112 goto done;
5113 err = delete_ref(branch_ref_name, repo);
5114 if (err)
5115 goto done;
5117 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5118 if (err)
5119 goto done;
5120 err = delete_ref(commit_ref_name, repo);
5121 if (err)
5122 goto done;
5124 done:
5125 free(tmp_branch_name);
5126 free(new_base_branch_ref_name);
5127 free(branch_ref_name);
5128 free(commit_ref_name);
5129 return err;
5132 const struct got_error *
5133 got_worktree_rebase_complete(struct got_worktree *worktree,
5134 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5135 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5136 struct got_repository *repo)
5138 const struct got_error *err, *unlockerr;
5139 struct got_object_id *new_head_commit_id = NULL;
5141 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5142 if (err)
5143 return err;
5145 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5146 if (err)
5147 goto done;
5149 err = got_ref_write(rebased_branch, repo);
5150 if (err)
5151 goto done;
5153 err = got_worktree_set_head_ref(worktree, rebased_branch);
5154 if (err)
5155 goto done;
5157 err = delete_rebase_refs(worktree, repo);
5158 done:
5159 if (fileindex)
5160 got_fileindex_free(fileindex);
5161 free(new_head_commit_id);
5162 unlockerr = lock_worktree(worktree, LOCK_SH);
5163 if (unlockerr && err == NULL)
5164 err = unlockerr;
5165 return err;
5168 const struct got_error *
5169 got_worktree_rebase_abort(struct got_worktree *worktree,
5170 struct got_fileindex *fileindex, struct got_repository *repo,
5171 struct got_reference *new_base_branch,
5172 got_worktree_checkout_cb progress_cb, void *progress_arg)
5174 const struct got_error *err, *unlockerr, *sync_err;
5175 struct got_reference *resolved = NULL;
5176 struct got_object_id *commit_id = NULL;
5177 char *fileindex_path = NULL;
5178 struct revert_file_args rfa;
5179 struct got_object_id *tree_id = NULL;
5181 err = lock_worktree(worktree, LOCK_EX);
5182 if (err)
5183 return err;
5185 err = got_ref_open(&resolved, repo,
5186 got_ref_get_symref_target(new_base_branch), 0);
5187 if (err)
5188 goto done;
5190 err = got_worktree_set_head_ref(worktree, resolved);
5191 if (err)
5192 goto done;
5195 * XXX commits to the base branch could have happened while
5196 * we were busy rebasing; should we store the original commit ID
5197 * when rebase begins and read it back here?
5199 err = got_ref_resolve(&commit_id, repo, resolved);
5200 if (err)
5201 goto done;
5203 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5204 if (err)
5205 goto done;
5207 err = got_object_id_by_path(&tree_id, repo,
5208 worktree->base_commit_id, worktree->path_prefix);
5209 if (err)
5210 goto done;
5212 err = delete_rebase_refs(worktree, repo);
5213 if (err)
5214 goto done;
5216 err = get_fileindex_path(&fileindex_path, worktree);
5217 if (err)
5218 goto done;
5220 rfa.worktree = worktree;
5221 rfa.fileindex = fileindex;
5222 rfa.progress_cb = progress_cb;
5223 rfa.progress_arg = progress_arg;
5224 rfa.patch_cb = NULL;
5225 rfa.patch_arg = NULL;
5226 rfa.repo = repo;
5227 err = worktree_status(worktree, "", fileindex, repo,
5228 revert_file, &rfa, NULL, NULL);
5229 if (err)
5230 goto sync;
5232 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5233 repo, progress_cb, progress_arg, NULL, NULL);
5234 sync:
5235 sync_err = sync_fileindex(fileindex, fileindex_path);
5236 if (sync_err && err == NULL)
5237 err = sync_err;
5238 done:
5239 got_ref_close(resolved);
5240 free(tree_id);
5241 free(commit_id);
5242 if (fileindex)
5243 got_fileindex_free(fileindex);
5244 free(fileindex_path);
5246 unlockerr = lock_worktree(worktree, LOCK_SH);
5247 if (unlockerr && err == NULL)
5248 err = unlockerr;
5249 return err;
5252 const struct got_error *
5253 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5254 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5255 struct got_fileindex **fileindex, struct got_worktree *worktree,
5256 struct got_repository *repo)
5258 const struct got_error *err = NULL;
5259 char *tmp_branch_name = NULL;
5260 char *branch_ref_name = NULL;
5261 char *base_commit_ref_name = NULL;
5262 char *fileindex_path = NULL;
5263 struct check_rebase_ok_arg ok_arg;
5264 struct got_reference *wt_branch = NULL;
5265 struct got_reference *base_commit_ref = NULL;
5267 *tmp_branch = NULL;
5268 *branch_ref = NULL;
5269 *base_commit_id = NULL;
5270 *fileindex = NULL;
5272 err = lock_worktree(worktree, LOCK_EX);
5273 if (err)
5274 return err;
5276 err = open_fileindex(fileindex, &fileindex_path, worktree);
5277 if (err)
5278 goto done;
5280 ok_arg.worktree = worktree;
5281 ok_arg.repo = repo;
5282 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5283 &ok_arg);
5284 if (err)
5285 goto done;
5287 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5288 if (err)
5289 goto done;
5291 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5292 if (err)
5293 goto done;
5295 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5296 worktree);
5297 if (err)
5298 goto done;
5300 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5301 0);
5302 if (err)
5303 goto done;
5305 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5306 if (err)
5307 goto done;
5309 err = got_ref_write(*branch_ref, repo);
5310 if (err)
5311 goto done;
5313 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5314 worktree->base_commit_id);
5315 if (err)
5316 goto done;
5317 err = got_ref_write(base_commit_ref, repo);
5318 if (err)
5319 goto done;
5320 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5321 if (*base_commit_id == NULL) {
5322 err = got_error_from_errno("got_object_id_dup");
5323 goto done;
5326 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5327 worktree->base_commit_id);
5328 if (err)
5329 goto done;
5330 err = got_ref_write(*tmp_branch, repo);
5331 if (err)
5332 goto done;
5334 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5335 if (err)
5336 goto done;
5337 done:
5338 free(fileindex_path);
5339 free(tmp_branch_name);
5340 free(branch_ref_name);
5341 free(base_commit_ref_name);
5342 if (wt_branch)
5343 got_ref_close(wt_branch);
5344 if (err) {
5345 if (*branch_ref) {
5346 got_ref_close(*branch_ref);
5347 *branch_ref = NULL;
5349 if (*tmp_branch) {
5350 got_ref_close(*tmp_branch);
5351 *tmp_branch = NULL;
5353 free(*base_commit_id);
5354 if (*fileindex) {
5355 got_fileindex_free(*fileindex);
5356 *fileindex = NULL;
5358 lock_worktree(worktree, LOCK_SH);
5360 return err;
5363 const struct got_error *
5364 got_worktree_histedit_postpone(struct got_worktree *worktree,
5365 struct got_fileindex *fileindex)
5367 if (fileindex)
5368 got_fileindex_free(fileindex);
5369 return lock_worktree(worktree, LOCK_SH);
5372 const struct got_error *
5373 got_worktree_histedit_in_progress(int *in_progress,
5374 struct got_worktree *worktree)
5376 const struct got_error *err;
5377 char *tmp_branch_name = NULL;
5379 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5380 if (err)
5381 return err;
5383 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5384 free(tmp_branch_name);
5385 return NULL;
5388 const struct got_error *
5389 got_worktree_histedit_continue(struct got_object_id **commit_id,
5390 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5391 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5392 struct got_worktree *worktree, struct got_repository *repo)
5394 const struct got_error *err;
5395 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5396 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5397 struct got_reference *commit_ref = NULL;
5398 struct got_reference *base_commit_ref = NULL;
5399 char *fileindex_path = NULL;
5400 int have_staged_files = 0;
5402 *commit_id = NULL;
5403 *tmp_branch = NULL;
5404 *base_commit_id = NULL;
5405 *fileindex = NULL;
5407 err = lock_worktree(worktree, LOCK_EX);
5408 if (err)
5409 return err;
5411 err = open_fileindex(fileindex, &fileindex_path, worktree);
5412 if (err)
5413 goto done;
5415 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5416 &have_staged_files);
5417 if (err && err->code != GOT_ERR_CANCELLED)
5418 goto done;
5419 if (have_staged_files) {
5420 err = got_error(GOT_ERR_STAGED_PATHS);
5421 goto done;
5424 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5425 if (err)
5426 goto done;
5428 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5429 if (err)
5430 goto done;
5432 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5433 if (err)
5434 goto done;
5436 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5437 worktree);
5438 if (err)
5439 goto done;
5441 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5442 if (err)
5443 goto done;
5445 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5446 if (err)
5447 goto done;
5448 err = got_ref_resolve(commit_id, repo, commit_ref);
5449 if (err)
5450 goto done;
5452 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5453 if (err)
5454 goto done;
5455 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5456 if (err)
5457 goto done;
5459 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5460 if (err)
5461 goto done;
5462 done:
5463 free(commit_ref_name);
5464 free(branch_ref_name);
5465 free(fileindex_path);
5466 if (commit_ref)
5467 got_ref_close(commit_ref);
5468 if (base_commit_ref)
5469 got_ref_close(base_commit_ref);
5470 if (err) {
5471 free(*commit_id);
5472 *commit_id = NULL;
5473 free(*base_commit_id);
5474 *base_commit_id = NULL;
5475 if (*tmp_branch) {
5476 got_ref_close(*tmp_branch);
5477 *tmp_branch = NULL;
5479 if (*fileindex) {
5480 got_fileindex_free(*fileindex);
5481 *fileindex = NULL;
5483 lock_worktree(worktree, LOCK_EX);
5485 return err;
5488 static const struct got_error *
5489 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5491 const struct got_error *err;
5492 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5493 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5495 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5496 if (err)
5497 goto done;
5498 err = delete_ref(tmp_branch_name, repo);
5499 if (err)
5500 goto done;
5502 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5503 worktree);
5504 if (err)
5505 goto done;
5506 err = delete_ref(base_commit_ref_name, repo);
5507 if (err)
5508 goto done;
5510 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5511 if (err)
5512 goto done;
5513 err = delete_ref(branch_ref_name, repo);
5514 if (err)
5515 goto done;
5517 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5518 if (err)
5519 goto done;
5520 err = delete_ref(commit_ref_name, repo);
5521 if (err)
5522 goto done;
5523 done:
5524 free(tmp_branch_name);
5525 free(base_commit_ref_name);
5526 free(branch_ref_name);
5527 free(commit_ref_name);
5528 return err;
5531 const struct got_error *
5532 got_worktree_histedit_abort(struct got_worktree *worktree,
5533 struct got_fileindex *fileindex, struct got_repository *repo,
5534 struct got_reference *branch, struct got_object_id *base_commit_id,
5535 got_worktree_checkout_cb progress_cb, void *progress_arg)
5537 const struct got_error *err, *unlockerr, *sync_err;
5538 struct got_reference *resolved = NULL;
5539 char *fileindex_path = NULL;
5540 struct got_object_id *tree_id = NULL;
5541 struct revert_file_args rfa;
5543 err = lock_worktree(worktree, LOCK_EX);
5544 if (err)
5545 return err;
5547 err = got_ref_open(&resolved, repo,
5548 got_ref_get_symref_target(branch), 0);
5549 if (err)
5550 goto done;
5552 err = got_worktree_set_head_ref(worktree, resolved);
5553 if (err)
5554 goto done;
5556 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5557 if (err)
5558 goto done;
5560 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5561 worktree->path_prefix);
5562 if (err)
5563 goto done;
5565 err = delete_histedit_refs(worktree, repo);
5566 if (err)
5567 goto done;
5569 err = get_fileindex_path(&fileindex_path, worktree);
5570 if (err)
5571 goto done;
5573 rfa.worktree = worktree;
5574 rfa.fileindex = fileindex;
5575 rfa.progress_cb = progress_cb;
5576 rfa.progress_arg = progress_arg;
5577 rfa.patch_cb = NULL;
5578 rfa.patch_arg = NULL;
5579 rfa.repo = repo;
5580 err = worktree_status(worktree, "", fileindex, repo,
5581 revert_file, &rfa, NULL, NULL);
5582 if (err)
5583 goto sync;
5585 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5586 repo, progress_cb, progress_arg, NULL, NULL);
5587 sync:
5588 sync_err = sync_fileindex(fileindex, fileindex_path);
5589 if (sync_err && err == NULL)
5590 err = sync_err;
5591 done:
5592 got_ref_close(resolved);
5593 free(tree_id);
5594 free(fileindex_path);
5596 unlockerr = lock_worktree(worktree, LOCK_SH);
5597 if (unlockerr && err == NULL)
5598 err = unlockerr;
5599 return err;
5602 const struct got_error *
5603 got_worktree_histedit_complete(struct got_worktree *worktree,
5604 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5605 struct got_reference *edited_branch, struct got_repository *repo)
5607 const struct got_error *err, *unlockerr;
5608 struct got_object_id *new_head_commit_id = NULL;
5609 struct got_reference *resolved = NULL;
5611 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5612 if (err)
5613 return err;
5615 err = got_ref_open(&resolved, repo,
5616 got_ref_get_symref_target(edited_branch), 0);
5617 if (err)
5618 goto done;
5620 err = got_ref_change_ref(resolved, new_head_commit_id);
5621 if (err)
5622 goto done;
5624 err = got_ref_write(resolved, repo);
5625 if (err)
5626 goto done;
5628 err = got_worktree_set_head_ref(worktree, resolved);
5629 if (err)
5630 goto done;
5632 err = delete_histedit_refs(worktree, repo);
5633 done:
5634 if (fileindex)
5635 got_fileindex_free(fileindex);
5636 free(new_head_commit_id);
5637 unlockerr = lock_worktree(worktree, LOCK_SH);
5638 if (unlockerr && err == NULL)
5639 err = unlockerr;
5640 return err;
5643 const struct got_error *
5644 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5645 struct got_object_id *commit_id, struct got_repository *repo)
5647 const struct got_error *err;
5648 char *commit_ref_name;
5650 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5651 if (err)
5652 return err;
5654 err = store_commit_id(commit_ref_name, commit_id, repo);
5655 if (err)
5656 goto done;
5658 err = delete_ref(commit_ref_name, repo);
5659 done:
5660 free(commit_ref_name);
5661 return err;
5664 const struct got_error *
5665 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5666 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5667 struct got_worktree *worktree, const char *refname,
5668 struct got_repository *repo)
5670 const struct got_error *err = NULL;
5671 char *fileindex_path = NULL;
5672 struct check_rebase_ok_arg ok_arg;
5674 *fileindex = NULL;
5675 *branch_ref = NULL;
5676 *base_branch_ref = NULL;
5678 err = lock_worktree(worktree, LOCK_EX);
5679 if (err)
5680 return err;
5682 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5683 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5684 "cannot integrate a branch into itself; "
5685 "update -b or different branch name required");
5686 goto done;
5689 err = open_fileindex(fileindex, &fileindex_path, worktree);
5690 if (err)
5691 goto done;
5693 /* Preconditions are the same as for rebase. */
5694 ok_arg.worktree = worktree;
5695 ok_arg.repo = repo;
5696 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5697 &ok_arg);
5698 if (err)
5699 goto done;
5701 err = got_ref_open(branch_ref, repo, refname, 1);
5702 if (err)
5703 goto done;
5705 err = got_ref_open(base_branch_ref, repo,
5706 got_worktree_get_head_ref_name(worktree), 1);
5707 done:
5708 if (err) {
5709 if (*branch_ref) {
5710 got_ref_close(*branch_ref);
5711 *branch_ref = NULL;
5713 if (*base_branch_ref) {
5714 got_ref_close(*base_branch_ref);
5715 *base_branch_ref = NULL;
5717 if (*fileindex) {
5718 got_fileindex_free(*fileindex);
5719 *fileindex = NULL;
5721 lock_worktree(worktree, LOCK_SH);
5723 return err;
5726 const struct got_error *
5727 got_worktree_integrate_continue(struct got_worktree *worktree,
5728 struct got_fileindex *fileindex, struct got_repository *repo,
5729 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5730 got_worktree_checkout_cb progress_cb, void *progress_arg,
5731 got_cancel_cb cancel_cb, void *cancel_arg)
5733 const struct got_error *err = NULL, *sync_err, *unlockerr;
5734 char *fileindex_path = NULL;
5735 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5737 err = get_fileindex_path(&fileindex_path, worktree);
5738 if (err)
5739 goto done;
5741 err = got_ref_resolve(&commit_id, repo, branch_ref);
5742 if (err)
5743 goto done;
5745 err = got_object_id_by_path(&tree_id, repo, commit_id,
5746 worktree->path_prefix);
5747 if (err)
5748 goto done;
5750 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5751 if (err)
5752 goto done;
5754 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5755 progress_cb, progress_arg, cancel_cb, cancel_arg);
5756 if (err)
5757 goto sync;
5759 err = got_ref_change_ref(base_branch_ref, commit_id);
5760 if (err)
5761 goto sync;
5763 err = got_ref_write(base_branch_ref, repo);
5764 sync:
5765 sync_err = sync_fileindex(fileindex, fileindex_path);
5766 if (sync_err && err == NULL)
5767 err = sync_err;
5769 done:
5770 unlockerr = got_ref_unlock(branch_ref);
5771 if (unlockerr && err == NULL)
5772 err = unlockerr;
5773 got_ref_close(branch_ref);
5775 unlockerr = got_ref_unlock(base_branch_ref);
5776 if (unlockerr && err == NULL)
5777 err = unlockerr;
5778 got_ref_close(base_branch_ref);
5780 got_fileindex_free(fileindex);
5781 free(fileindex_path);
5782 free(tree_id);
5784 unlockerr = lock_worktree(worktree, LOCK_SH);
5785 if (unlockerr && err == NULL)
5786 err = unlockerr;
5787 return err;
5790 const struct got_error *
5791 got_worktree_integrate_abort(struct got_worktree *worktree,
5792 struct got_fileindex *fileindex, struct got_repository *repo,
5793 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5795 got_ref_close(branch_ref);
5796 got_ref_close(base_branch_ref);
5797 got_fileindex_free(fileindex);
5798 return lock_worktree(worktree, LOCK_SH);
5801 struct check_stage_ok_arg {
5802 struct got_object_id *head_commit_id;
5803 struct got_worktree *worktree;
5804 struct got_fileindex *fileindex;
5805 struct got_repository *repo;
5806 int have_changes;
5809 const struct got_error *
5810 check_stage_ok(void *arg, unsigned char status,
5811 unsigned char staged_status, const char *relpath,
5812 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5813 struct got_object_id *commit_id)
5815 struct check_stage_ok_arg *a = arg;
5816 const struct got_error *err = NULL;
5817 struct got_fileindex_entry *ie;
5818 struct got_object_id base_commit_id;
5819 struct got_object_id *base_commit_idp = NULL;
5820 char *in_repo_path = NULL, *p;
5822 if (status == GOT_STATUS_UNVERSIONED)
5823 return NULL;
5824 if (status == GOT_STATUS_NONEXISTENT)
5825 return got_error_set_errno(ENOENT, relpath);
5827 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5828 if (ie == NULL)
5829 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5831 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5832 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5833 relpath) == -1)
5834 return got_error_from_errno("asprintf");
5836 if (got_fileindex_entry_has_commit(ie)) {
5837 memcpy(base_commit_id.sha1, ie->commit_sha1,
5838 SHA1_DIGEST_LENGTH);
5839 base_commit_idp = &base_commit_id;
5842 if (status == GOT_STATUS_NO_CHANGE) {
5843 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5844 goto done;
5845 } else if (status == GOT_STATUS_CONFLICT) {
5846 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5847 goto done;
5848 } else if (status != GOT_STATUS_ADD &&
5849 status != GOT_STATUS_MODIFY &&
5850 status != GOT_STATUS_DELETE) {
5851 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5852 goto done;
5855 a->have_changes = 1;
5857 p = in_repo_path;
5858 while (p[0] == '/')
5859 p++;
5860 err = check_out_of_date(p, status, staged_status,
5861 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5862 GOT_ERR_STAGE_OUT_OF_DATE);
5863 done:
5864 free(in_repo_path);
5865 return err;
5868 struct stage_path_arg {
5869 struct got_worktree *worktree;
5870 struct got_fileindex *fileindex;
5871 struct got_repository *repo;
5872 got_worktree_status_cb status_cb;
5873 void *status_arg;
5874 got_worktree_patch_cb patch_cb;
5875 void *patch_arg;
5878 static const struct got_error *
5879 stage_path(void *arg, unsigned char status,
5880 unsigned char staged_status, const char *relpath,
5881 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5882 struct got_object_id *commit_id)
5884 struct stage_path_arg *a = arg;
5885 const struct got_error *err = NULL;
5886 struct got_fileindex_entry *ie;
5887 char *ondisk_path = NULL, *path_content = NULL;
5888 uint32_t stage;
5889 struct got_object_id *new_staged_blob_id = NULL;
5891 if (status == GOT_STATUS_UNVERSIONED)
5892 return NULL;
5894 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5895 if (ie == NULL)
5896 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5899 relpath)== -1)
5900 return got_error_from_errno("asprintf");
5902 switch (status) {
5903 case GOT_STATUS_ADD:
5904 case GOT_STATUS_MODIFY:
5905 if (a->patch_cb) {
5906 if (status == GOT_STATUS_ADD) {
5907 int choice = GOT_PATCH_CHOICE_NONE;
5908 err = (*a->patch_cb)(&choice, a->patch_arg,
5909 status, ie->path, NULL, 1, 1);
5910 if (err)
5911 break;
5912 if (choice != GOT_PATCH_CHOICE_YES)
5913 break;
5914 } else {
5915 err = create_patched_content(&path_content, 0,
5916 staged_blob_id ? staged_blob_id : blob_id,
5917 ondisk_path, ie->path, a->repo,
5918 a->patch_cb, a->patch_arg);
5919 if (err || path_content == NULL)
5920 break;
5923 err = got_object_blob_create(&new_staged_blob_id,
5924 path_content ? path_content : ondisk_path, a->repo);
5925 if (err)
5926 break;
5927 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5928 SHA1_DIGEST_LENGTH);
5929 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5930 stage = GOT_FILEIDX_STAGE_ADD;
5931 else
5932 stage = GOT_FILEIDX_STAGE_MODIFY;
5933 got_fileindex_entry_stage_set(ie, stage);
5934 if (a->status_cb == NULL)
5935 break;
5936 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5937 get_staged_status(ie), relpath, blob_id,
5938 new_staged_blob_id, NULL);
5939 break;
5940 case GOT_STATUS_DELETE:
5941 if (staged_status == GOT_STATUS_DELETE)
5942 break;
5943 if (a->patch_cb) {
5944 int choice = GOT_PATCH_CHOICE_NONE;
5945 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5946 ie->path, NULL, 1, 1);
5947 if (err)
5948 break;
5949 if (choice == GOT_PATCH_CHOICE_NO)
5950 break;
5951 if (choice != GOT_PATCH_CHOICE_YES) {
5952 err = got_error(GOT_ERR_PATCH_CHOICE);
5953 break;
5956 stage = GOT_FILEIDX_STAGE_DELETE;
5957 got_fileindex_entry_stage_set(ie, stage);
5958 if (a->status_cb == NULL)
5959 break;
5960 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5961 get_staged_status(ie), relpath, NULL, NULL, NULL);
5962 break;
5963 case GOT_STATUS_NO_CHANGE:
5964 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5965 break;
5966 case GOT_STATUS_CONFLICT:
5967 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5968 break;
5969 case GOT_STATUS_NONEXISTENT:
5970 err = got_error_set_errno(ENOENT, relpath);
5971 break;
5972 default:
5973 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5974 break;
5977 if (path_content && unlink(path_content) == -1 && err == NULL)
5978 err = got_error_from_errno2("unlink", path_content);
5979 free(path_content);
5980 free(ondisk_path);
5981 free(new_staged_blob_id);
5982 return err;
5985 const struct got_error *
5986 got_worktree_stage(struct got_worktree *worktree,
5987 struct got_pathlist_head *paths,
5988 got_worktree_status_cb status_cb, void *status_arg,
5989 got_worktree_patch_cb patch_cb, void *patch_arg,
5990 struct got_repository *repo)
5992 const struct got_error *err = NULL, *sync_err, *unlockerr;
5993 struct got_pathlist_entry *pe;
5994 struct got_fileindex *fileindex = NULL;
5995 char *fileindex_path = NULL;
5996 struct got_reference *head_ref = NULL;
5997 struct got_object_id *head_commit_id = NULL;
5998 struct check_stage_ok_arg oka;
5999 struct stage_path_arg spa;
6001 err = lock_worktree(worktree, LOCK_EX);
6002 if (err)
6003 return err;
6005 err = got_ref_open(&head_ref, repo,
6006 got_worktree_get_head_ref_name(worktree), 0);
6007 if (err)
6008 goto done;
6009 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6010 if (err)
6011 goto done;
6012 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6013 if (err)
6014 goto done;
6016 /* Check pre-conditions before staging anything. */
6017 oka.head_commit_id = head_commit_id;
6018 oka.worktree = worktree;
6019 oka.fileindex = fileindex;
6020 oka.repo = repo;
6021 oka.have_changes = 0;
6022 TAILQ_FOREACH(pe, paths, entry) {
6023 err = worktree_status(worktree, pe->path, fileindex, repo,
6024 check_stage_ok, &oka, NULL, NULL);
6025 if (err)
6026 goto done;
6028 if (!oka.have_changes) {
6029 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6030 goto done;
6033 spa.worktree = worktree;
6034 spa.fileindex = fileindex;
6035 spa.repo = repo;
6036 spa.patch_cb = patch_cb;
6037 spa.patch_arg = patch_arg;
6038 spa.status_cb = status_cb;
6039 spa.status_arg = status_arg;
6040 TAILQ_FOREACH(pe, paths, entry) {
6041 err = worktree_status(worktree, pe->path, fileindex, repo,
6042 stage_path, &spa, NULL, NULL);
6043 if (err)
6044 goto done;
6047 sync_err = sync_fileindex(fileindex, fileindex_path);
6048 if (sync_err && err == NULL)
6049 err = sync_err;
6050 done:
6051 if (head_ref)
6052 got_ref_close(head_ref);
6053 free(head_commit_id);
6054 free(fileindex_path);
6055 if (fileindex)
6056 got_fileindex_free(fileindex);
6057 unlockerr = lock_worktree(worktree, LOCK_SH);
6058 if (unlockerr && err == NULL)
6059 err = unlockerr;
6060 return err;
6063 struct unstage_path_arg {
6064 struct got_worktree *worktree;
6065 struct got_fileindex *fileindex;
6066 struct got_repository *repo;
6067 got_worktree_checkout_cb progress_cb;
6068 void *progress_arg;
6069 got_worktree_patch_cb patch_cb;
6070 void *patch_arg;
6073 static const struct got_error *
6074 create_unstaged_content(char **path_unstaged_content,
6075 char **path_new_staged_content, struct got_object_id *blob_id,
6076 struct got_object_id *staged_blob_id, const char *relpath,
6077 struct got_repository *repo,
6078 got_worktree_patch_cb patch_cb, void *patch_arg)
6080 const struct got_error *err;
6081 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6082 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6083 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6084 struct stat sb1, sb2;
6085 struct got_diff_changes *changes = NULL;
6086 struct got_diff_state *ds = NULL;
6087 struct got_diff_args *args = NULL;
6088 struct got_diff_change *change;
6089 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6090 int have_content = 0, have_rejected_content = 0;
6092 *path_unstaged_content = NULL;
6093 *path_new_staged_content = NULL;
6095 err = got_object_id_str(&label1, blob_id);
6096 if (err)
6097 return err;
6098 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6099 if (err)
6100 goto done;
6102 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6103 if (err)
6104 goto done;
6106 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6107 if (err)
6108 goto done;
6110 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6111 if (err)
6112 goto done;
6114 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6115 if (err)
6116 goto done;
6118 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6119 if (err)
6120 goto done;
6122 if (stat(path1, &sb1) == -1) {
6123 err = got_error_from_errno2("stat", path1);
6124 goto done;
6127 if (stat(path2, &sb2) == -1) {
6128 err = got_error_from_errno2("stat", path2);
6129 goto done;
6132 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6133 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6134 if (err)
6135 goto done;
6137 err = got_opentemp_named(path_unstaged_content, &outfile,
6138 "got-unstaged-content");
6139 if (err)
6140 goto done;
6141 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6142 "got-new-staged-content");
6143 if (err)
6144 goto done;
6146 if (fseek(f1, 0L, SEEK_SET) == -1) {
6147 err = got_ferror(f1, GOT_ERR_IO);
6148 goto done;
6150 if (fseek(f2, 0L, SEEK_SET) == -1) {
6151 err = got_ferror(f2, GOT_ERR_IO);
6152 goto done;
6154 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6155 int choice;
6156 err = apply_or_reject_change(&choice, change, ++n,
6157 changes->nchanges, ds, args, diff_flags, relpath,
6158 f1, f2, &line_cur1, &line_cur2,
6159 outfile, rejectfile, patch_cb, patch_arg);
6160 if (err)
6161 goto done;
6162 if (choice == GOT_PATCH_CHOICE_YES)
6163 have_content = 1;
6164 else
6165 have_rejected_content = 1;
6166 if (choice == GOT_PATCH_CHOICE_QUIT)
6167 break;
6169 if (have_content || have_rejected_content)
6170 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6171 outfile, rejectfile);
6172 done:
6173 free(label1);
6174 if (blob)
6175 got_object_blob_close(blob);
6176 if (staged_blob)
6177 got_object_blob_close(staged_blob);
6178 if (f1 && fclose(f1) == EOF && err == NULL)
6179 err = got_error_from_errno2("fclose", path1);
6180 if (f2 && fclose(f2) == EOF && err == NULL)
6181 err = got_error_from_errno2("fclose", path2);
6182 if (outfile && fclose(outfile) == EOF && err == NULL)
6183 err = got_error_from_errno2("fclose", *path_unstaged_content);
6184 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6185 err = got_error_from_errno2("fclose", *path_new_staged_content);
6186 if (path1 && unlink(path1) == -1 && err == NULL)
6187 err = got_error_from_errno2("unlink", path1);
6188 if (path2 && unlink(path2) == -1 && err == NULL)
6189 err = got_error_from_errno2("unlink", path2);
6190 if (err || !have_content) {
6191 if (*path_unstaged_content &&
6192 unlink(*path_unstaged_content) == -1 && err == NULL)
6193 err = got_error_from_errno2("unlink",
6194 *path_unstaged_content);
6195 free(*path_unstaged_content);
6196 *path_unstaged_content = NULL;
6198 if (err || !have_rejected_content) {
6199 if (*path_new_staged_content &&
6200 unlink(*path_new_staged_content) == -1 && err == NULL)
6201 err = got_error_from_errno2("unlink",
6202 *path_new_staged_content);
6203 free(*path_new_staged_content);
6204 *path_new_staged_content = NULL;
6206 free(args);
6207 if (ds) {
6208 got_diff_state_free(ds);
6209 free(ds);
6211 if (changes)
6212 got_diff_free_changes(changes);
6213 free(path1);
6214 free(path2);
6215 return err;
6218 static const struct got_error *
6219 unstage_path(void *arg, unsigned char status,
6220 unsigned char staged_status, const char *relpath,
6221 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6222 struct got_object_id *commit_id)
6224 const struct got_error *err = NULL;
6225 struct unstage_path_arg *a = arg;
6226 struct got_fileindex_entry *ie;
6227 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6228 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6229 char *path_new_staged_content = NULL;
6230 int local_changes_subsumed;
6231 struct stat sb;
6233 if (staged_status != GOT_STATUS_ADD &&
6234 staged_status != GOT_STATUS_MODIFY &&
6235 staged_status != GOT_STATUS_DELETE)
6236 return NULL;
6238 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6239 if (ie == NULL)
6240 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6242 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6243 == -1)
6244 return got_error_from_errno("asprintf");
6246 switch (staged_status) {
6247 case GOT_STATUS_MODIFY:
6248 err = got_object_open_as_blob(&blob_base, a->repo,
6249 blob_id, 8192);
6250 if (err)
6251 break;
6252 /* fall through */
6253 case GOT_STATUS_ADD:
6254 if (a->patch_cb) {
6255 if (staged_status == GOT_STATUS_ADD) {
6256 int choice = GOT_PATCH_CHOICE_NONE;
6257 err = (*a->patch_cb)(&choice, a->patch_arg,
6258 staged_status, ie->path, NULL, 1, 1);
6259 if (err)
6260 break;
6261 if (choice != GOT_PATCH_CHOICE_YES)
6262 break;
6263 } else {
6264 err = create_unstaged_content(
6265 &path_unstaged_content,
6266 &path_new_staged_content, blob_id,
6267 staged_blob_id, ie->path, a->repo,
6268 a->patch_cb, a->patch_arg);
6269 if (err || path_unstaged_content == NULL)
6270 break;
6271 if (path_new_staged_content) {
6272 err = got_object_blob_create(
6273 &staged_blob_id,
6274 path_new_staged_content,
6275 a->repo);
6276 if (err)
6277 break;
6278 memcpy(ie->staged_blob_sha1,
6279 staged_blob_id->sha1,
6280 SHA1_DIGEST_LENGTH);
6282 err = merge_file(&local_changes_subsumed,
6283 a->worktree, blob_base, ondisk_path,
6284 relpath, got_fileindex_perms_to_st(ie),
6285 path_unstaged_content, "unstaged",
6286 a->repo, a->progress_cb, a->progress_arg);
6287 if (err == NULL &&
6288 path_new_staged_content == NULL)
6289 got_fileindex_entry_stage_set(ie,
6290 GOT_FILEIDX_STAGE_NONE);
6291 break; /* Done with this file. */
6294 err = got_object_open_as_blob(&blob_staged, a->repo,
6295 staged_blob_id, 8192);
6296 if (err)
6297 break;
6298 err = merge_blob(&local_changes_subsumed, a->worktree,
6299 blob_base, ondisk_path, relpath,
6300 got_fileindex_perms_to_st(ie), blob_staged,
6301 commit_id ? commit_id : a->worktree->base_commit_id,
6302 a->repo, a->progress_cb, a->progress_arg);
6303 if (err == NULL)
6304 got_fileindex_entry_stage_set(ie,
6305 GOT_FILEIDX_STAGE_NONE);
6306 break;
6307 case GOT_STATUS_DELETE:
6308 if (a->patch_cb) {
6309 int choice = GOT_PATCH_CHOICE_NONE;
6310 err = (*a->patch_cb)(&choice, a->patch_arg,
6311 staged_status, ie->path, NULL, 1, 1);
6312 if (err)
6313 break;
6314 if (choice == GOT_PATCH_CHOICE_NO)
6315 break;
6316 if (choice != GOT_PATCH_CHOICE_YES) {
6317 err = got_error(GOT_ERR_PATCH_CHOICE);
6318 break;
6321 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6322 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6323 if (err)
6324 break;
6325 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6326 break;
6329 free(ondisk_path);
6330 if (path_unstaged_content &&
6331 unlink(path_unstaged_content) == -1 && err == NULL)
6332 err = got_error_from_errno2("unlink", path_unstaged_content);
6333 if (path_new_staged_content &&
6334 unlink(path_new_staged_content) == -1 && err == NULL)
6335 err = got_error_from_errno2("unlink", path_new_staged_content);
6336 free(path_unstaged_content);
6337 free(path_new_staged_content);
6338 if (blob_base)
6339 got_object_blob_close(blob_base);
6340 if (blob_staged)
6341 got_object_blob_close(blob_staged);
6342 return err;
6345 const struct got_error *
6346 got_worktree_unstage(struct got_worktree *worktree,
6347 struct got_pathlist_head *paths,
6348 got_worktree_checkout_cb progress_cb, void *progress_arg,
6349 got_worktree_patch_cb patch_cb, void *patch_arg,
6350 struct got_repository *repo)
6352 const struct got_error *err = NULL, *sync_err, *unlockerr;
6353 struct got_pathlist_entry *pe;
6354 struct got_fileindex *fileindex = NULL;
6355 char *fileindex_path = NULL;
6356 struct unstage_path_arg upa;
6358 err = lock_worktree(worktree, LOCK_EX);
6359 if (err)
6360 return err;
6362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6363 if (err)
6364 goto done;
6366 upa.worktree = worktree;
6367 upa.fileindex = fileindex;
6368 upa.repo = repo;
6369 upa.progress_cb = progress_cb;
6370 upa.progress_arg = progress_arg;
6371 upa.patch_cb = patch_cb;
6372 upa.patch_arg = patch_arg;
6373 TAILQ_FOREACH(pe, paths, entry) {
6374 err = worktree_status(worktree, pe->path, fileindex, repo,
6375 unstage_path, &upa, NULL, NULL);
6376 if (err)
6377 goto done;
6380 sync_err = sync_fileindex(fileindex, fileindex_path);
6381 if (sync_err && err == NULL)
6382 err = sync_err;
6383 done:
6384 free(fileindex_path);
6385 if (fileindex)
6386 got_fileindex_free(fileindex);
6387 unlockerr = lock_worktree(worktree, LOCK_SH);
6388 if (unlockerr && err == NULL)
6389 err = unlockerr;
6390 return err;