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 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 FILE *f = NULL;
1116 uint8_t fbuf[8192];
1117 struct got_blob_object *blob = NULL;
1118 size_t flen, blen;
1119 unsigned char staged_status = get_staged_status(ie);
1121 *status = GOT_STATUS_NO_CHANGE;
1123 if (lstat(abspath, sb) == -1) {
1124 if (errno == ENOENT) {
1125 if (got_fileindex_entry_has_file_on_disk(ie))
1126 *status = GOT_STATUS_MISSING;
1127 else
1128 *status = GOT_STATUS_DELETE;
1129 return NULL;
1131 return got_error_from_errno2("lstat", abspath);
1134 if (!S_ISREG(sb->st_mode)) {
1135 *status = GOT_STATUS_OBSTRUCTED;
1136 return NULL;
1139 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1140 *status = GOT_STATUS_DELETE;
1141 return NULL;
1142 } else if (!got_fileindex_entry_has_blob(ie) &&
1143 staged_status != GOT_STATUS_ADD) {
1144 *status = GOT_STATUS_ADD;
1145 return NULL;
1148 if (!stat_info_differs(ie, sb))
1149 return NULL;
1151 if (staged_status == GOT_STATUS_MODIFY ||
1152 staged_status == GOT_STATUS_ADD)
1153 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1154 else
1155 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1157 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1158 if (err)
1159 return err;
1161 f = fopen(abspath, "r");
1162 if (f == NULL) {
1163 err = got_error_from_errno2("fopen", abspath);
1164 goto done;
1166 hdrlen = got_object_blob_get_hdrlen(blob);
1167 for (;;) {
1168 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1169 err = got_object_blob_read_block(&blen, blob);
1170 if (err)
1171 goto done;
1172 /* Skip length of blob object header first time around. */
1173 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1174 if (flen == 0 && ferror(f)) {
1175 err = got_error_from_errno("fread");
1176 goto done;
1178 if (blen == 0) {
1179 if (flen != 0)
1180 *status = GOT_STATUS_MODIFY;
1181 break;
1182 } else if (flen == 0) {
1183 if (blen != 0)
1184 *status = GOT_STATUS_MODIFY;
1185 break;
1186 } else if (blen - hdrlen == flen) {
1187 /* Skip blob object header first time around. */
1188 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1189 *status = GOT_STATUS_MODIFY;
1190 break;
1192 } else {
1193 *status = GOT_STATUS_MODIFY;
1194 break;
1196 hdrlen = 0;
1199 if (*status == GOT_STATUS_MODIFY) {
1200 rewind(f);
1201 err = get_modified_file_content_status(status, f);
1202 } else if (xbit_differs(ie, sb->st_mode))
1203 *status = GOT_STATUS_MODE_CHANGE;
1204 done:
1205 if (blob)
1206 got_object_blob_close(blob);
1207 if (f)
1208 fclose(f);
1209 return err;
1213 * Update timestamps in the file index if a file is unmodified and
1214 * we had to run a full content comparison to find out.
1216 static const struct got_error *
1217 sync_timestamps(char *ondisk_path, unsigned char status,
1218 struct got_fileindex_entry *ie, struct stat *sb)
1220 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1221 return got_fileindex_entry_update(ie, ondisk_path,
1222 ie->blob_sha1, ie->commit_sha1, 1);
1224 return NULL;
1227 static const struct got_error *
1228 update_blob(struct got_worktree *worktree,
1229 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1230 struct got_tree_entry *te, const char *path,
1231 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1232 void *progress_arg)
1234 const struct got_error *err = NULL;
1235 struct got_blob_object *blob = NULL;
1236 char *ondisk_path;
1237 unsigned char status = GOT_STATUS_NO_CHANGE;
1238 struct stat sb;
1240 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1241 return got_error_from_errno("asprintf");
1243 if (ie) {
1244 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1245 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1246 goto done;
1248 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1249 if (err)
1250 goto done;
1251 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1252 sb.st_mode = got_fileindex_perms_to_st(ie);
1253 } else
1254 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1256 if (status == GOT_STATUS_OBSTRUCTED) {
1257 err = (*progress_cb)(progress_arg, status, path);
1258 goto done;
1261 if (ie && status != GOT_STATUS_MISSING &&
1262 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1263 if (got_fileindex_entry_has_commit(ie) &&
1264 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1265 SHA1_DIGEST_LENGTH) == 0) {
1266 err = sync_timestamps(ondisk_path, status, ie, &sb);
1267 if (err)
1268 goto done;
1269 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1270 path);
1271 goto done;
1273 if (got_fileindex_entry_has_blob(ie) &&
1274 memcmp(ie->blob_sha1, te->id->sha1,
1275 SHA1_DIGEST_LENGTH) == 0) {
1276 err = sync_timestamps(ondisk_path, status, ie, &sb);
1277 goto done;
1281 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1282 if (err)
1283 goto done;
1285 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1286 int update_timestamps;
1287 struct got_blob_object *blob2 = NULL;
1288 char *label_orig = NULL;
1289 if (got_fileindex_entry_has_blob(ie)) {
1290 struct got_object_id id2;
1291 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1292 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1293 if (err)
1294 goto done;
1296 if (got_fileindex_entry_has_commit(ie)) {
1297 char id_str[SHA1_DIGEST_STRING_LENGTH];
1298 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1299 sizeof(id_str)) == NULL) {
1300 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1301 goto done;
1303 if (asprintf(&label_orig, "%s: commit %s",
1304 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1309 err = merge_blob(&update_timestamps, worktree, blob2,
1310 ondisk_path, path, sb.st_mode, label_orig, blob,
1311 worktree->base_commit_id, repo,
1312 progress_cb, progress_arg);
1313 free(label_orig);
1314 if (blob2)
1315 got_object_blob_close(blob2);
1316 if (err)
1317 goto done;
1319 * Do not update timestamps of files with local changes.
1320 * Otherwise, a future status walk would treat them as
1321 * unmodified files again.
1323 err = got_fileindex_entry_update(ie, ondisk_path,
1324 blob->id.sha1, worktree->base_commit_id->sha1,
1325 update_timestamps);
1326 } else if (status == GOT_STATUS_MODE_CHANGE) {
1327 err = got_fileindex_entry_update(ie, ondisk_path,
1328 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1329 } else if (status == GOT_STATUS_DELETE) {
1330 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1331 if (err)
1332 goto done;
1333 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1334 ondisk_path, path, blob, 0);
1335 if (err)
1336 goto done;
1337 } else {
1338 err = install_blob(worktree, ondisk_path, path, te->mode,
1339 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1340 repo, progress_cb, progress_arg);
1341 if (err)
1342 goto done;
1343 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1344 ondisk_path, path, blob, 1);
1345 if (err)
1346 goto done;
1348 got_object_blob_close(blob);
1349 done:
1350 free(ondisk_path);
1351 return err;
1354 static const struct got_error *
1355 remove_ondisk_file(const char *root_path, const char *path)
1357 const struct got_error *err = NULL;
1358 char *ondisk_path = NULL;
1360 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1361 return got_error_from_errno("asprintf");
1363 if (unlink(ondisk_path) == -1) {
1364 if (errno != ENOENT)
1365 err = got_error_from_errno2("unlink", ondisk_path);
1366 } else {
1367 char *parent = dirname(ondisk_path);
1368 while (parent && strcmp(parent, root_path) != 0) {
1369 if (rmdir(parent) == -1) {
1370 if (errno != ENOTEMPTY)
1371 err = got_error_from_errno2("rmdir",
1372 parent);
1373 break;
1375 parent = dirname(parent);
1378 free(ondisk_path);
1379 return err;
1382 static const struct got_error *
1383 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1384 struct got_fileindex_entry *ie, struct got_repository *repo,
1385 got_worktree_checkout_cb progress_cb, void *progress_arg)
1387 const struct got_error *err = NULL;
1388 unsigned char status;
1389 struct stat sb;
1390 char *ondisk_path;
1392 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1393 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1395 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1396 == -1)
1397 return got_error_from_errno("asprintf");
1399 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1400 if (err)
1401 return err;
1403 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1404 status == GOT_STATUS_ADD) {
1405 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1406 if (err)
1407 return err;
1409 * Preserve the working file and change the deleted blob's
1410 * entry into a schedule-add entry.
1412 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1413 0);
1414 if (err)
1415 return err;
1416 } else {
1417 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1418 if (err)
1419 return err;
1420 if (status == GOT_STATUS_NO_CHANGE) {
1421 err = remove_ondisk_file(worktree->root_path, ie->path);
1422 if (err)
1423 return err;
1425 got_fileindex_entry_remove(fileindex, ie);
1428 return err;
1431 struct diff_cb_arg {
1432 struct got_fileindex *fileindex;
1433 struct got_worktree *worktree;
1434 struct got_repository *repo;
1435 got_worktree_checkout_cb progress_cb;
1436 void *progress_arg;
1437 got_cancel_cb cancel_cb;
1438 void *cancel_arg;
1441 static const struct got_error *
1442 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1443 struct got_tree_entry *te, const char *parent_path)
1445 struct diff_cb_arg *a = arg;
1447 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1448 return got_error(GOT_ERR_CANCELLED);
1450 return update_blob(a->worktree, a->fileindex, ie, te,
1451 ie->path, a->repo, a->progress_cb, a->progress_arg);
1454 static const struct got_error *
1455 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1457 struct diff_cb_arg *a = arg;
1459 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1460 return got_error(GOT_ERR_CANCELLED);
1462 return delete_blob(a->worktree, a->fileindex, ie,
1463 a->repo, a->progress_cb, a->progress_arg);
1466 static const struct got_error *
1467 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1469 struct diff_cb_arg *a = arg;
1470 const struct got_error *err;
1471 char *path;
1473 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1474 return got_error(GOT_ERR_CANCELLED);
1476 if (got_object_tree_entry_is_submodule(te))
1477 return NULL;
1479 if (asprintf(&path, "%s%s%s", parent_path,
1480 parent_path[0] ? "/" : "", te->name)
1481 == -1)
1482 return got_error_from_errno("asprintf");
1484 if (S_ISDIR(te->mode))
1485 err = add_dir_on_disk(a->worktree, path);
1486 else
1487 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1488 a->repo, a->progress_cb, a->progress_arg);
1490 free(path);
1491 return err;
1494 static const struct got_error *
1495 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1497 const struct got_error *err = NULL;
1498 char *uuidstr = NULL;
1499 uint32_t uuid_status;
1501 *refname = NULL;
1503 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1504 if (uuid_status != uuid_s_ok)
1505 return got_error_uuid(uuid_status, "uuid_to_string");
1507 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1508 == -1) {
1509 err = got_error_from_errno("asprintf");
1510 *refname = NULL;
1512 free(uuidstr);
1513 return err;
1516 const struct got_error *
1517 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1519 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1522 static const struct got_error *
1523 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1525 return get_ref_name(refname, worktree,
1526 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1529 static const struct got_error *
1530 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1532 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1535 static const struct got_error *
1536 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1538 return get_ref_name(refname, worktree,
1539 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1542 static const struct got_error *
1543 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1545 return get_ref_name(refname, worktree,
1546 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1549 static const struct got_error *
1550 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1552 return get_ref_name(refname, worktree,
1553 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1556 static const struct got_error *
1557 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1559 return get_ref_name(refname, worktree,
1560 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1563 static const struct got_error *
1564 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1570 static const struct got_error *
1571 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1577 const struct got_error *
1578 got_worktree_get_histedit_script_path(char **path,
1579 struct got_worktree *worktree)
1581 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1582 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1583 *path = NULL;
1584 return got_error_from_errno("asprintf");
1586 return NULL;
1590 * Prevent Git's garbage collector from deleting our base commit by
1591 * setting a reference to our base commit's ID.
1593 static const struct got_error *
1594 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_reference *ref = NULL;
1598 char *refname;
1600 err = got_worktree_get_base_ref_name(&refname, worktree);
1601 if (err)
1602 return err;
1604 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1605 if (err)
1606 goto done;
1608 err = got_ref_write(ref, repo);
1609 done:
1610 free(refname);
1611 if (ref)
1612 got_ref_close(ref);
1613 return err;
1616 static const struct got_error *
1617 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1619 const struct got_error *err = NULL;
1621 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1622 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1623 err = got_error_from_errno("asprintf");
1624 *fileindex_path = NULL;
1626 return err;
1630 static const struct got_error *
1631 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1632 struct got_worktree *worktree)
1634 const struct got_error *err = NULL;
1635 FILE *index = NULL;
1637 *fileindex_path = NULL;
1638 *fileindex = got_fileindex_alloc();
1639 if (*fileindex == NULL)
1640 return got_error_from_errno("got_fileindex_alloc");
1642 err = get_fileindex_path(fileindex_path, worktree);
1643 if (err)
1644 goto done;
1646 index = fopen(*fileindex_path, "rb");
1647 if (index == NULL) {
1648 if (errno != ENOENT)
1649 err = got_error_from_errno2("fopen", *fileindex_path);
1650 } else {
1651 err = got_fileindex_read(*fileindex, index);
1652 if (fclose(index) != 0 && err == NULL)
1653 err = got_error_from_errno("fclose");
1655 done:
1656 if (err) {
1657 free(*fileindex_path);
1658 *fileindex_path = NULL;
1659 got_fileindex_free(*fileindex);
1660 *fileindex = NULL;
1662 return err;
1665 struct bump_base_commit_id_arg {
1666 struct got_object_id *base_commit_id;
1667 const char *path;
1668 size_t path_len;
1669 const char *entry_name;
1670 got_worktree_checkout_cb progress_cb;
1671 void *progress_arg;
1674 /* Bump base commit ID of all files within an updated part of the work tree. */
1675 static const struct got_error *
1676 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1678 const struct got_error *err;
1679 struct bump_base_commit_id_arg *a = arg;
1681 if (a->entry_name) {
1682 if (strcmp(ie->path, a->path) != 0)
1683 return NULL;
1684 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1685 return NULL;
1687 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1688 SHA1_DIGEST_LENGTH) == 0)
1689 return NULL;
1691 if (a->progress_cb) {
1692 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1693 ie->path);
1694 if (err)
1695 return err;
1697 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1698 return NULL;
1701 static const struct got_error *
1702 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1704 const struct got_error *err = NULL;
1705 char *new_fileindex_path = NULL;
1706 FILE *new_index = NULL;
1708 err = got_opentemp_named(&new_fileindex_path, &new_index,
1709 fileindex_path);
1710 if (err)
1711 goto done;
1713 err = got_fileindex_write(fileindex, new_index);
1714 if (err)
1715 goto done;
1717 if (rename(new_fileindex_path, fileindex_path) != 0) {
1718 err = got_error_from_errno3("rename", new_fileindex_path,
1719 fileindex_path);
1720 unlink(new_fileindex_path);
1722 done:
1723 if (new_index)
1724 fclose(new_index);
1725 free(new_fileindex_path);
1726 return err;
1729 static const struct got_error *
1730 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1731 struct got_object_id **tree_id, const char *wt_relpath,
1732 struct got_worktree *worktree, struct got_repository *repo)
1734 const struct got_error *err = NULL;
1735 struct got_object_id *id = NULL;
1736 char *in_repo_path = NULL;
1737 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1739 *entry_type = GOT_OBJ_TYPE_ANY;
1740 *tree_relpath = NULL;
1741 *tree_id = NULL;
1743 if (wt_relpath[0] == '\0') {
1744 /* Check out all files within the work tree. */
1745 *entry_type = GOT_OBJ_TYPE_TREE;
1746 *tree_relpath = strdup("");
1747 if (*tree_relpath == NULL) {
1748 err = got_error_from_errno("strdup");
1749 goto done;
1751 err = got_object_id_by_path(tree_id, repo,
1752 worktree->base_commit_id, worktree->path_prefix);
1753 if (err)
1754 goto done;
1755 return NULL;
1758 /* Check out a subset of files in the work tree. */
1760 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1761 is_root_wt ? "" : "/", wt_relpath) == -1) {
1762 err = got_error_from_errno("asprintf");
1763 goto done;
1766 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1767 in_repo_path);
1768 if (err)
1769 goto done;
1771 free(in_repo_path);
1772 in_repo_path = NULL;
1774 err = got_object_get_type(entry_type, repo, id);
1775 if (err)
1776 goto done;
1778 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1779 /* Check out a single file. */
1780 if (strchr(wt_relpath, '/') == NULL) {
1781 /* Check out a single file in work tree's root dir. */
1782 in_repo_path = strdup(worktree->path_prefix);
1783 if (in_repo_path == NULL) {
1784 err = got_error_from_errno("strdup");
1785 goto done;
1787 *tree_relpath = strdup("");
1788 if (*tree_relpath == NULL) {
1789 err = got_error_from_errno("strdup");
1790 goto done;
1792 } else {
1793 /* Check out a single file in a subdirectory. */
1794 err = got_path_dirname(tree_relpath, wt_relpath);
1795 if (err)
1796 return err;
1797 if (asprintf(&in_repo_path, "%s%s%s",
1798 worktree->path_prefix, is_root_wt ? "" : "/",
1799 *tree_relpath) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 goto done;
1804 err = got_object_id_by_path(tree_id, repo,
1805 worktree->base_commit_id, in_repo_path);
1806 } else {
1807 /* Check out all files within a subdirectory. */
1808 *tree_id = got_object_id_dup(id);
1809 if (*tree_id == NULL) {
1810 err = got_error_from_errno("got_object_id_dup");
1811 goto done;
1813 *tree_relpath = strdup(wt_relpath);
1814 if (*tree_relpath == NULL) {
1815 err = got_error_from_errno("strdup");
1816 goto done;
1819 done:
1820 free(id);
1821 free(in_repo_path);
1822 if (err) {
1823 *entry_type = GOT_OBJ_TYPE_ANY;
1824 free(*tree_relpath);
1825 *tree_relpath = NULL;
1826 free(*tree_id);
1827 *tree_id = NULL;
1829 return err;
1832 static const struct got_error *
1833 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1834 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1835 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1836 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1838 const struct got_error *err = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_tree_object *tree = NULL;
1841 struct got_fileindex_diff_tree_cb diff_cb;
1842 struct diff_cb_arg arg;
1844 err = ref_base_commit(worktree, repo);
1845 if (err)
1846 goto done;
1848 err = got_object_open_as_commit(&commit, repo,
1849 worktree->base_commit_id);
1850 if (err)
1851 goto done;
1853 err = got_object_open_as_tree(&tree, repo, tree_id);
1854 if (err)
1855 goto done;
1857 if (entry_name &&
1858 got_object_tree_find_entry(tree, entry_name) == NULL) {
1859 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1860 goto done;
1863 diff_cb.diff_old_new = diff_old_new;
1864 diff_cb.diff_old = diff_old;
1865 diff_cb.diff_new = diff_new;
1866 arg.fileindex = fileindex;
1867 arg.worktree = worktree;
1868 arg.repo = repo;
1869 arg.progress_cb = progress_cb;
1870 arg.progress_arg = progress_arg;
1871 arg.cancel_cb = cancel_cb;
1872 arg.cancel_arg = cancel_arg;
1873 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1874 entry_name, repo, &diff_cb, &arg);
1875 done:
1876 if (tree)
1877 got_object_tree_close(tree);
1878 if (commit)
1879 got_object_commit_close(commit);
1880 return err;
1883 const struct got_error *
1884 got_worktree_checkout_files(struct got_worktree *worktree,
1885 struct got_pathlist_head *paths, struct got_repository *repo,
1886 got_worktree_checkout_cb progress_cb, void *progress_arg,
1887 got_cancel_cb cancel_cb, void *cancel_arg)
1889 const struct got_error *err = NULL, *sync_err, *unlockerr;
1890 struct got_commit_object *commit = NULL;
1891 struct got_tree_object *tree = NULL;
1892 struct got_fileindex *fileindex = NULL;
1893 char *fileindex_path = NULL;
1894 struct got_pathlist_entry *pe;
1895 struct tree_path_data {
1896 SIMPLEQ_ENTRY(tree_path_data) entry;
1897 struct got_object_id *tree_id;
1898 int entry_type;
1899 char *relpath;
1900 char *entry_name;
1901 } *tpd = NULL;
1902 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1904 SIMPLEQ_INIT(&tree_paths);
1906 err = lock_worktree(worktree, LOCK_EX);
1907 if (err)
1908 return err;
1910 /* Map all specified paths to in-repository trees. */
1911 TAILQ_FOREACH(pe, paths, entry) {
1912 tpd = malloc(sizeof(*tpd));
1913 if (tpd == NULL) {
1914 err = got_error_from_errno("malloc");
1915 goto done;
1918 err = find_tree_entry_for_checkout(&tpd->entry_type,
1919 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1920 if (err) {
1921 free(tpd);
1922 goto done;
1925 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1926 err = got_path_basename(&tpd->entry_name, pe->path);
1927 if (err) {
1928 free(tpd->relpath);
1929 free(tpd->tree_id);
1930 free(tpd);
1931 goto done;
1933 } else
1934 tpd->entry_name = NULL;
1936 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1940 * Read the file index.
1941 * Checking out files is supposed to be an idempotent operation.
1942 * If the on-disk file index is incomplete we will try to complete it.
1944 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1945 if (err)
1946 goto done;
1948 tpd = SIMPLEQ_FIRST(&tree_paths);
1949 TAILQ_FOREACH(pe, paths, entry) {
1950 struct bump_base_commit_id_arg bbc_arg;
1952 err = checkout_files(worktree, fileindex, tpd->relpath,
1953 tpd->tree_id, tpd->entry_name, repo,
1954 progress_cb, progress_arg, cancel_cb, cancel_arg);
1955 if (err)
1956 break;
1958 bbc_arg.base_commit_id = worktree->base_commit_id;
1959 bbc_arg.entry_name = tpd->entry_name;
1960 bbc_arg.path = pe->path;
1961 bbc_arg.path_len = pe->path_len;
1962 bbc_arg.progress_cb = progress_cb;
1963 bbc_arg.progress_arg = progress_arg;
1964 err = got_fileindex_for_each_entry_safe(fileindex,
1965 bump_base_commit_id, &bbc_arg);
1966 if (err)
1967 break;
1969 tpd = SIMPLEQ_NEXT(tpd, entry);
1971 sync_err = sync_fileindex(fileindex, fileindex_path);
1972 if (sync_err && err == NULL)
1973 err = sync_err;
1974 done:
1975 free(fileindex_path);
1976 if (tree)
1977 got_object_tree_close(tree);
1978 if (commit)
1979 got_object_commit_close(commit);
1980 if (fileindex)
1981 got_fileindex_free(fileindex);
1982 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1983 tpd = SIMPLEQ_FIRST(&tree_paths);
1984 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1985 free(tpd->relpath);
1986 free(tpd->tree_id);
1987 free(tpd);
1989 unlockerr = lock_worktree(worktree, LOCK_SH);
1990 if (unlockerr && err == NULL)
1991 err = unlockerr;
1992 return err;
1995 struct merge_file_cb_arg {
1996 struct got_worktree *worktree;
1997 struct got_fileindex *fileindex;
1998 got_worktree_checkout_cb progress_cb;
1999 void *progress_arg;
2000 got_cancel_cb cancel_cb;
2001 void *cancel_arg;
2002 const char *label_orig;
2003 struct got_object_id *commit_id2;
2006 static const struct got_error *
2007 merge_file_cb(void *arg, struct got_blob_object *blob1,
2008 struct got_blob_object *blob2, struct got_object_id *id1,
2009 struct got_object_id *id2, const char *path1, const char *path2,
2010 mode_t mode1, mode_t mode2, struct got_repository *repo)
2012 static const struct got_error *err = NULL;
2013 struct merge_file_cb_arg *a = arg;
2014 struct got_fileindex_entry *ie;
2015 char *ondisk_path = NULL;
2016 struct stat sb;
2017 unsigned char status;
2018 int local_changes_subsumed;
2020 if (blob1 && blob2) {
2021 ie = got_fileindex_entry_get(a->fileindex, path2,
2022 strlen(path2));
2023 if (ie == NULL)
2024 return (*a->progress_cb)(a->progress_arg,
2025 GOT_STATUS_MISSING, path2);
2027 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2028 path2) == -1)
2029 return got_error_from_errno("asprintf");
2031 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2032 if (err)
2033 goto done;
2035 if (status == GOT_STATUS_DELETE) {
2036 err = (*a->progress_cb)(a->progress_arg,
2037 GOT_STATUS_MERGE, path2);
2038 goto done;
2040 if (status != GOT_STATUS_NO_CHANGE &&
2041 status != GOT_STATUS_MODIFY &&
2042 status != GOT_STATUS_CONFLICT &&
2043 status != GOT_STATUS_ADD) {
2044 err = (*a->progress_cb)(a->progress_arg, status, path2);
2045 goto done;
2048 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2049 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2050 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2051 } else if (blob1) {
2052 ie = got_fileindex_entry_get(a->fileindex, path1,
2053 strlen(path1));
2054 if (ie == NULL)
2055 return (*a->progress_cb)(a->progress_arg,
2056 GOT_STATUS_MISSING, path2);
2058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2059 path1) == -1)
2060 return got_error_from_errno("asprintf");
2062 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2063 if (err)
2064 goto done;
2066 switch (status) {
2067 case GOT_STATUS_NO_CHANGE:
2068 err = (*a->progress_cb)(a->progress_arg,
2069 GOT_STATUS_DELETE, path1);
2070 if (err)
2071 goto done;
2072 err = remove_ondisk_file(a->worktree->root_path, path1);
2073 if (err)
2074 goto done;
2075 if (ie)
2076 got_fileindex_entry_mark_deleted_from_disk(ie);
2077 break;
2078 case GOT_STATUS_DELETE:
2079 case GOT_STATUS_MISSING:
2080 err = (*a->progress_cb)(a->progress_arg,
2081 GOT_STATUS_DELETE, path1);
2082 if (err)
2083 goto done;
2084 if (ie)
2085 got_fileindex_entry_mark_deleted_from_disk(ie);
2086 break;
2087 case GOT_STATUS_ADD:
2088 case GOT_STATUS_MODIFY:
2089 case GOT_STATUS_CONFLICT:
2090 err = (*a->progress_cb)(a->progress_arg,
2091 GOT_STATUS_CANNOT_DELETE, path1);
2092 if (err)
2093 goto done;
2094 break;
2095 case GOT_STATUS_OBSTRUCTED:
2096 err = (*a->progress_cb)(a->progress_arg, status, path1);
2097 if (err)
2098 goto done;
2099 break;
2100 default:
2101 break;
2103 } else if (blob2) {
2104 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2105 path2) == -1)
2106 return got_error_from_errno("asprintf");
2107 ie = got_fileindex_entry_get(a->fileindex, path2,
2108 strlen(path2));
2109 if (ie) {
2110 err = get_file_status(&status, &sb, ie, ondisk_path,
2111 repo);
2112 if (err)
2113 goto done;
2114 if (status != GOT_STATUS_NO_CHANGE &&
2115 status != GOT_STATUS_MODIFY &&
2116 status != GOT_STATUS_CONFLICT &&
2117 status != GOT_STATUS_ADD) {
2118 err = (*a->progress_cb)(a->progress_arg,
2119 status, path2);
2120 goto done;
2122 err = merge_blob(&local_changes_subsumed, a->worktree,
2123 NULL, ondisk_path, path2, sb.st_mode,
2124 a->label_orig, blob2, a->commit_id2, repo,
2125 a->progress_cb,
2126 a->progress_arg);
2127 if (status == GOT_STATUS_DELETE) {
2128 err = update_blob_fileindex_entry(a->worktree,
2129 a->fileindex, ie, ondisk_path, ie->path,
2130 blob2, 0);
2131 if (err)
2132 goto done;
2134 } else {
2135 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2136 err = install_blob(a->worktree, ondisk_path, path2,
2137 /* XXX get this from parent tree! */
2138 GOT_DEFAULT_FILE_MODE,
2139 sb.st_mode, blob2, 0, 0, repo,
2140 a->progress_cb, a->progress_arg);
2141 if (err)
2142 goto done;
2143 err = got_fileindex_entry_alloc(&ie,
2144 ondisk_path, path2, NULL, NULL);
2145 if (err)
2146 goto done;
2147 err = got_fileindex_entry_add(a->fileindex, ie);
2148 if (err) {
2149 got_fileindex_entry_free(ie);
2150 goto done;
2154 done:
2155 free(ondisk_path);
2156 return err;
2159 struct check_merge_ok_arg {
2160 struct got_worktree *worktree;
2161 struct got_repository *repo;
2164 static const struct got_error *
2165 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2167 const struct got_error *err = NULL;
2168 struct check_merge_ok_arg *a = arg;
2169 unsigned char status;
2170 struct stat sb;
2171 char *ondisk_path;
2173 /* Reject merges into a work tree with mixed base commits. */
2174 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2175 SHA1_DIGEST_LENGTH))
2176 return got_error(GOT_ERR_MIXED_COMMITS);
2178 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2179 == -1)
2180 return got_error_from_errno("asprintf");
2182 /* Reject merges into a work tree with conflicted files. */
2183 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2184 if (err)
2185 return err;
2186 if (status == GOT_STATUS_CONFLICT)
2187 return got_error(GOT_ERR_CONFLICTS);
2189 return NULL;
2192 static const struct got_error *
2193 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 const char *fileindex_path, struct got_object_id *commit_id1,
2195 struct got_object_id *commit_id2, struct got_repository *repo,
2196 got_worktree_checkout_cb progress_cb, void *progress_arg,
2197 got_cancel_cb cancel_cb, void *cancel_arg)
2199 const struct got_error *err = NULL, *sync_err;
2200 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2201 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2202 struct merge_file_cb_arg arg;
2203 char *label_orig = NULL;
2205 if (commit_id1) {
2206 char *id_str;
2208 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2209 worktree->path_prefix);
2210 if (err)
2211 goto done;
2213 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2214 if (err)
2215 goto done;
2217 err = got_object_id_str(&id_str, commit_id1);
2218 if (err)
2219 goto done;
2221 if (asprintf(&label_orig, "%s: commit %s",
2222 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2223 err = got_error_from_errno("asprintf");
2224 free(id_str);
2225 goto done;
2227 free(id_str);
2230 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2231 worktree->path_prefix);
2232 if (err)
2233 goto done;
2235 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2236 if (err)
2237 goto done;
2239 arg.worktree = worktree;
2240 arg.fileindex = fileindex;
2241 arg.progress_cb = progress_cb;
2242 arg.progress_arg = progress_arg;
2243 arg.cancel_cb = cancel_cb;
2244 arg.cancel_arg = cancel_arg;
2245 arg.label_orig = label_orig;
2246 arg.commit_id2 = commit_id2;
2247 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2248 sync_err = sync_fileindex(fileindex, fileindex_path);
2249 if (sync_err && err == NULL)
2250 err = sync_err;
2251 done:
2252 if (tree1)
2253 got_object_tree_close(tree1);
2254 if (tree2)
2255 got_object_tree_close(tree2);
2256 free(label_orig);
2257 return err;
2260 const struct got_error *
2261 got_worktree_merge_files(struct got_worktree *worktree,
2262 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2263 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2264 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2266 const struct got_error *err, *unlockerr;
2267 char *fileindex_path = NULL;
2268 struct got_fileindex *fileindex = NULL;
2269 struct check_merge_ok_arg mok_arg;
2271 err = lock_worktree(worktree, LOCK_EX);
2272 if (err)
2273 return err;
2275 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2276 if (err)
2277 goto done;
2279 mok_arg.worktree = worktree;
2280 mok_arg.repo = repo;
2281 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2282 &mok_arg);
2283 if (err)
2284 goto done;
2286 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2287 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2288 done:
2289 if (fileindex)
2290 got_fileindex_free(fileindex);
2291 free(fileindex_path);
2292 unlockerr = lock_worktree(worktree, LOCK_SH);
2293 if (unlockerr && err == NULL)
2294 err = unlockerr;
2295 return err;
2298 struct diff_dir_cb_arg {
2299 struct got_fileindex *fileindex;
2300 struct got_worktree *worktree;
2301 const char *status_path;
2302 size_t status_path_len;
2303 struct got_repository *repo;
2304 got_worktree_status_cb status_cb;
2305 void *status_arg;
2306 got_cancel_cb cancel_cb;
2307 void *cancel_arg;
2308 /* A pathlist containing per-directory pathlists of ignore patterns. */
2309 struct got_pathlist_head ignores;
2312 static const struct got_error *
2313 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2314 got_worktree_status_cb status_cb, void *status_arg,
2315 struct got_repository *repo)
2317 const struct got_error *err = NULL;
2318 unsigned char status = GOT_STATUS_NO_CHANGE;
2319 unsigned char staged_status = get_staged_status(ie);
2320 struct stat sb;
2321 struct got_object_id blob_id, commit_id, staged_blob_id;
2322 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2323 struct got_object_id *staged_blob_idp = NULL;
2325 err = get_file_status(&status, &sb, ie, abspath, repo);
2326 if (err)
2327 return err;
2329 if (status == GOT_STATUS_NO_CHANGE &&
2330 staged_status == GOT_STATUS_NO_CHANGE)
2331 return NULL;
2333 if (got_fileindex_entry_has_blob(ie)) {
2334 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2335 blob_idp = &blob_id;
2337 if (got_fileindex_entry_has_commit(ie)) {
2338 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2339 commit_idp = &commit_id;
2341 if (staged_status == GOT_STATUS_ADD ||
2342 staged_status == GOT_STATUS_MODIFY) {
2343 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2344 SHA1_DIGEST_LENGTH);
2345 staged_blob_idp = &staged_blob_id;
2348 return (*status_cb)(status_arg, status, staged_status,
2349 ie->path, blob_idp, staged_blob_idp, commit_idp);
2352 static const struct got_error *
2353 status_old_new(void *arg, struct got_fileindex_entry *ie,
2354 struct dirent *de, const char *parent_path)
2356 const struct got_error *err = NULL;
2357 struct diff_dir_cb_arg *a = arg;
2358 char *abspath;
2360 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2361 return got_error(GOT_ERR_CANCELLED);
2363 if (got_path_cmp(parent_path, a->status_path,
2364 strlen(parent_path), a->status_path_len) != 0 &&
2365 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2366 return NULL;
2368 if (parent_path[0]) {
2369 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2370 parent_path, de->d_name) == -1)
2371 return got_error_from_errno("asprintf");
2372 } else {
2373 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2374 de->d_name) == -1)
2375 return got_error_from_errno("asprintf");
2378 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2379 a->repo);
2380 free(abspath);
2381 return err;
2384 static const struct got_error *
2385 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2387 struct diff_dir_cb_arg *a = arg;
2388 struct got_object_id blob_id, commit_id;
2389 unsigned char status;
2391 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2392 return got_error(GOT_ERR_CANCELLED);
2394 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2395 return NULL;
2397 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2398 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2399 if (got_fileindex_entry_has_file_on_disk(ie))
2400 status = GOT_STATUS_MISSING;
2401 else
2402 status = GOT_STATUS_DELETE;
2403 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2404 ie->path, &blob_id, NULL, &commit_id);
2407 void
2408 free_ignorelist(struct got_pathlist_head *ignorelist)
2410 struct got_pathlist_entry *pe;
2412 TAILQ_FOREACH(pe, ignorelist, entry)
2413 free((char *)pe->path);
2414 got_pathlist_free(ignorelist);
2417 void
2418 free_ignores(struct got_pathlist_head *ignores)
2420 struct got_pathlist_entry *pe;
2422 TAILQ_FOREACH(pe, ignores, entry) {
2423 struct got_pathlist_head *ignorelist = pe->data;
2424 free_ignorelist(ignorelist);
2425 free((char *)pe->path);
2427 got_pathlist_free(ignores);
2430 static const struct got_error *
2431 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2433 const struct got_error *err = NULL;
2434 struct got_pathlist_entry *pe = NULL;
2435 struct got_pathlist_head *ignorelist;
2436 char *line = NULL, *pattern, *dirpath = NULL;
2437 size_t linesize = 0;
2438 ssize_t linelen;
2440 ignorelist = calloc(1, sizeof(*ignorelist));
2441 if (ignorelist == NULL)
2442 return got_error_from_errno("calloc");
2443 TAILQ_INIT(ignorelist);
2445 while ((linelen = getline(&line, &linesize, f)) != -1) {
2446 if (linelen > 0 && line[linelen - 1] == '\n')
2447 line[linelen - 1] = '\0';
2449 /* Git's ignores may contain comments. */
2450 if (line[0] == '#')
2451 continue;
2453 /* Git's negated patterns are not (yet?) supported. */
2454 if (line[0] == '!')
2455 continue;
2457 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2458 line) == -1) {
2459 err = got_error_from_errno("asprintf");
2460 goto done;
2462 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2463 if (err)
2464 goto done;
2466 if (ferror(f)) {
2467 err = got_error_from_errno("getline");
2468 goto done;
2471 dirpath = strdup(path);
2472 if (dirpath == NULL) {
2473 err = got_error_from_errno("strdup");
2474 goto done;
2476 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2477 done:
2478 free(line);
2479 if (err || pe == NULL) {
2480 free(dirpath);
2481 free_ignorelist(ignorelist);
2483 return err;
2486 int
2487 match_ignores(struct got_pathlist_head *ignores, const char *path)
2489 struct got_pathlist_entry *pe;
2491 /* Handle patterns which match in all directories. */
2492 TAILQ_FOREACH(pe, ignores, entry) {
2493 struct got_pathlist_head *ignorelist = pe->data;
2494 struct got_pathlist_entry *pi;
2496 TAILQ_FOREACH(pi, ignorelist, entry) {
2497 const char *p, *pattern = pi->path;
2499 if (strncmp(pattern, "**/", 3) != 0)
2500 continue;
2501 pattern += 3;
2502 p = path;
2503 while (*p) {
2504 if (fnmatch(pattern, p,
2505 FNM_PATHNAME | FNM_LEADING_DIR)) {
2506 /* Retry in next directory. */
2507 while (*p && *p != '/')
2508 p++;
2509 while (*p == '/')
2510 p++;
2511 continue;
2513 return 1;
2519 * The ignores pathlist contains ignore lists from children before
2520 * parents, so we can find the most specific ignorelist by walking
2521 * ignores backwards.
2523 pe = TAILQ_LAST(ignores, got_pathlist_head);
2524 while (pe) {
2525 if (got_path_is_child(path, pe->path, pe->path_len)) {
2526 struct got_pathlist_head *ignorelist = pe->data;
2527 struct got_pathlist_entry *pi;
2528 TAILQ_FOREACH(pi, ignorelist, entry) {
2529 const char *pattern = pi->path;
2530 int flags = FNM_LEADING_DIR;
2531 if (strstr(pattern, "/**/") == NULL)
2532 flags |= FNM_PATHNAME;
2533 if (fnmatch(pattern, path, flags))
2534 continue;
2535 return 1;
2538 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2541 return 0;
2544 static const struct got_error *
2545 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2546 const char *path, const char *ignores_filename)
2548 const struct got_error *err = NULL;
2549 char *ignorespath;
2550 FILE *ignoresfile = NULL;
2552 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2553 path[0] ? "/" : "", ignores_filename) == -1)
2554 return got_error_from_errno("asprintf");
2556 ignoresfile = fopen(ignorespath, "r");
2557 if (ignoresfile == NULL) {
2558 if (errno != ENOENT && errno != EACCES)
2559 err = got_error_from_errno2("fopen",
2560 ignorespath);
2561 } else
2562 err = read_ignores(ignores, path, ignoresfile);
2564 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2565 err = got_error_from_errno2("flose", path);
2566 free(ignorespath);
2567 return err;
2570 static const struct got_error *
2571 status_new(void *arg, struct dirent *de, const char *parent_path)
2573 const struct got_error *err = NULL;
2574 struct diff_dir_cb_arg *a = arg;
2575 char *path = NULL;
2577 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2578 return got_error(GOT_ERR_CANCELLED);
2580 /* XXX ignore symlinks for now */
2581 if (de->d_type == DT_LNK)
2582 return NULL;
2584 if (parent_path[0]) {
2585 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2586 return got_error_from_errno("asprintf");
2587 } else {
2588 path = de->d_name;
2591 if (de->d_type == DT_DIR) {
2592 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2593 ".cvsignore");
2594 if (err == NULL)
2595 err = add_ignores(&a->ignores, a->worktree->root_path,
2596 path, ".gitignore");
2598 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2599 && !match_ignores(&a->ignores, path))
2600 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2601 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2602 if (parent_path[0])
2603 free(path);
2604 return err;
2607 static const struct got_error *
2608 report_single_file_status(const char *path, const char *ondisk_path,
2609 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2610 void *status_arg, struct got_repository *repo)
2612 struct got_fileindex_entry *ie;
2613 struct stat sb;
2615 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2616 if (ie)
2617 return report_file_status(ie, ondisk_path, status_cb,
2618 status_arg, repo);
2620 if (lstat(ondisk_path, &sb) == -1) {
2621 if (errno != ENOENT)
2622 return got_error_from_errno2("lstat", ondisk_path);
2623 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2624 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2625 return NULL;
2628 if (S_ISREG(sb.st_mode))
2629 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2630 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2632 return NULL;
2635 static const struct got_error *
2636 worktree_status(struct got_worktree *worktree, const char *path,
2637 struct got_fileindex *fileindex, struct got_repository *repo,
2638 got_worktree_status_cb status_cb, void *status_arg,
2639 got_cancel_cb cancel_cb, void *cancel_arg)
2641 const struct got_error *err = NULL;
2642 DIR *workdir = NULL;
2643 struct got_fileindex_diff_dir_cb fdiff_cb;
2644 struct diff_dir_cb_arg arg;
2645 char *ondisk_path = NULL;
2647 if (asprintf(&ondisk_path, "%s%s%s",
2648 worktree->root_path, path[0] ? "/" : "", path) == -1)
2649 return got_error_from_errno("asprintf");
2651 workdir = opendir(ondisk_path);
2652 if (workdir == NULL) {
2653 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2654 err = got_error_from_errno2("opendir", ondisk_path);
2655 else
2656 err = report_single_file_status(path, ondisk_path,
2657 fileindex, status_cb, status_arg, repo);
2658 } else {
2659 fdiff_cb.diff_old_new = status_old_new;
2660 fdiff_cb.diff_old = status_old;
2661 fdiff_cb.diff_new = status_new;
2662 arg.fileindex = fileindex;
2663 arg.worktree = worktree;
2664 arg.status_path = path;
2665 arg.status_path_len = strlen(path);
2666 arg.repo = repo;
2667 arg.status_cb = status_cb;
2668 arg.status_arg = status_arg;
2669 arg.cancel_cb = cancel_cb;
2670 arg.cancel_arg = cancel_arg;
2671 TAILQ_INIT(&arg.ignores);
2672 err = add_ignores(&arg.ignores, worktree->root_path, path,
2673 ".cvsignore");
2674 if (err == NULL)
2675 err = add_ignores(&arg.ignores, worktree->root_path,
2676 path, ".gitignore");
2677 if (err == NULL)
2678 err = got_fileindex_diff_dir(fileindex, workdir,
2679 worktree->root_path, path, repo, &fdiff_cb, &arg);
2680 free_ignores(&arg.ignores);
2683 if (workdir)
2684 closedir(workdir);
2685 free(ondisk_path);
2686 return err;
2689 const struct got_error *
2690 got_worktree_status(struct got_worktree *worktree,
2691 struct got_pathlist_head *paths, struct got_repository *repo,
2692 got_worktree_status_cb status_cb, void *status_arg,
2693 got_cancel_cb cancel_cb, void *cancel_arg)
2695 const struct got_error *err = NULL;
2696 char *fileindex_path = NULL;
2697 struct got_fileindex *fileindex = NULL;
2698 struct got_pathlist_entry *pe;
2700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2701 if (err)
2702 return err;
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 err = worktree_status(worktree, pe->path, fileindex, repo,
2706 status_cb, status_arg, cancel_cb, cancel_arg);
2707 if (err)
2708 break;
2710 free(fileindex_path);
2711 got_fileindex_free(fileindex);
2712 return err;
2715 const struct got_error *
2716 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2717 const char *arg)
2719 const struct got_error *err = NULL;
2720 char *resolved, *cwd = NULL, *path = NULL;
2721 size_t len;
2723 *wt_path = NULL;
2725 resolved = realpath(arg, NULL);
2726 if (resolved == NULL) {
2727 if (errno != ENOENT)
2728 return got_error_from_errno2("realpath", arg);
2729 cwd = getcwd(NULL, 0);
2730 if (cwd == NULL)
2731 return got_error_from_errno("getcwd");
2732 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2733 err = got_error_from_errno("asprintf");
2734 goto done;
2738 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2739 strlen(got_worktree_get_root_path(worktree)))) {
2740 err = got_error(GOT_ERR_BAD_PATH);
2741 goto done;
2744 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2745 err = got_path_skip_common_ancestor(&path,
2746 got_worktree_get_root_path(worktree), resolved);
2747 if (err)
2748 goto done;
2749 } else {
2750 path = strdup("");
2751 if (path == NULL) {
2752 err = got_error_from_errno("strdup");
2753 goto done;
2757 /* XXX status walk can't deal with trailing slash! */
2758 len = strlen(path);
2759 while (len > 0 && path[len - 1] == '/') {
2760 path[len - 1] = '\0';
2761 len--;
2763 done:
2764 free(resolved);
2765 free(cwd);
2766 if (err == NULL)
2767 *wt_path = path;
2768 else
2769 free(path);
2770 return err;
2773 static const struct got_error *
2774 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2775 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2776 struct got_repository *repo)
2778 const struct got_error *err = NULL;
2779 struct got_fileindex_entry *ie;
2780 unsigned char status;
2781 struct stat sb;
2783 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2784 if (ie) {
2785 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2786 if (err)
2787 return err;
2788 /* Re-adding an existing entry is a no-op. */
2789 if (status == GOT_STATUS_ADD)
2790 return NULL;
2791 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2794 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2795 if (err)
2796 return err;
2798 err = got_fileindex_entry_add(fileindex, ie);
2799 if (err) {
2800 got_fileindex_entry_free(ie);
2801 return err;
2804 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2807 const struct got_error *
2808 got_worktree_schedule_add(struct got_worktree *worktree,
2809 struct got_pathlist_head *paths,
2810 got_worktree_status_cb status_cb, void *status_arg,
2811 struct got_repository *repo)
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 const struct got_error *err = NULL, *sync_err, *unlockerr;
2816 struct got_pathlist_entry *pe;
2818 err = lock_worktree(worktree, LOCK_EX);
2819 if (err)
2820 return err;
2822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2823 if (err)
2824 goto done;
2826 TAILQ_FOREACH(pe, paths, entry) {
2827 char *ondisk_path;
2828 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2829 pe->path) == -1)
2830 return got_error_from_errno("asprintf");
2831 err = schedule_addition(ondisk_path, fileindex, pe->path,
2832 status_cb, status_arg, repo);
2833 free(ondisk_path);
2834 if (err)
2835 break;
2837 sync_err = sync_fileindex(fileindex, fileindex_path);
2838 if (sync_err && err == NULL)
2839 err = sync_err;
2840 done:
2841 free(fileindex_path);
2842 if (fileindex)
2843 got_fileindex_free(fileindex);
2844 unlockerr = lock_worktree(worktree, LOCK_SH);
2845 if (unlockerr && err == NULL)
2846 err = unlockerr;
2847 return err;
2850 static const struct got_error *
2851 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2852 const char *relpath, int delete_local_mods,
2853 got_worktree_status_cb status_cb, void *status_arg,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_fileindex_entry *ie = NULL;
2858 unsigned char status, staged_status;
2859 struct stat sb;
2861 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2862 if (ie == NULL)
2863 return got_error(GOT_ERR_BAD_PATH);
2865 staged_status = get_staged_status(ie);
2866 if (staged_status != GOT_STATUS_NO_CHANGE) {
2867 if (staged_status == GOT_STATUS_DELETE)
2868 return NULL;
2869 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2872 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2873 if (err)
2874 return err;
2876 if (status != GOT_STATUS_NO_CHANGE) {
2877 if (status == GOT_STATUS_DELETE)
2878 return NULL;
2879 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2880 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2881 if (status != GOT_STATUS_MODIFY &&
2882 status != GOT_STATUS_MISSING)
2883 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2886 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2887 return got_error_from_errno2("unlink", ondisk_path);
2889 got_fileindex_entry_mark_deleted_from_disk(ie);
2890 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2893 const struct got_error *
2894 got_worktree_schedule_delete(struct got_worktree *worktree,
2895 struct got_pathlist_head *paths, int delete_local_mods,
2896 got_worktree_status_cb status_cb, void *status_arg,
2897 struct got_repository *repo)
2899 struct got_fileindex *fileindex = NULL;
2900 char *fileindex_path = NULL;
2901 const struct got_error *err = NULL, *sync_err, *unlockerr;
2902 struct got_pathlist_entry *pe;
2904 err = lock_worktree(worktree, LOCK_EX);
2905 if (err)
2906 return err;
2908 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2909 if (err)
2910 goto done;
2912 TAILQ_FOREACH(pe, paths, entry) {
2913 char *ondisk_path;
2914 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2915 pe->path) == -1)
2916 return got_error_from_errno("asprintf");
2917 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2918 delete_local_mods, status_cb, status_arg, repo);
2919 free(ondisk_path);
2920 if (err)
2921 break;
2923 sync_err = sync_fileindex(fileindex, fileindex_path);
2924 if (sync_err && err == NULL)
2925 err = sync_err;
2926 done:
2927 free(fileindex_path);
2928 if (fileindex)
2929 got_fileindex_free(fileindex);
2930 unlockerr = lock_worktree(worktree, LOCK_SH);
2931 if (unlockerr && err == NULL)
2932 err = unlockerr;
2933 return err;
2936 static const struct got_error *
2937 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2939 const struct got_error *err = NULL;
2940 char *line = NULL;
2941 size_t linesize = 0, n;
2942 ssize_t linelen;
2944 linelen = getline(&line, &linesize, infile);
2945 if (linelen == -1) {
2946 if (ferror(infile)) {
2947 err = got_error_from_errno("getline");
2948 goto done;
2950 return NULL;
2952 if (outfile) {
2953 n = fwrite(line, 1, linelen, outfile);
2954 if (n != linelen) {
2955 err = got_ferror(outfile, GOT_ERR_IO);
2956 goto done;
2959 if (rejectfile) {
2960 n = fwrite(line, 1, linelen, rejectfile);
2961 if (n != linelen)
2962 err = got_ferror(outfile, GOT_ERR_IO);
2964 done:
2965 free(line);
2966 return err;
2969 static const struct got_error *
2970 skip_one_line(FILE *f)
2972 char *line = NULL;
2973 size_t linesize = 0;
2974 ssize_t linelen;
2976 linelen = getline(&line, &linesize, f);
2977 if (linelen == -1) {
2978 if (ferror(f))
2979 return got_error_from_errno("getline");
2980 return NULL;
2982 free(line);
2983 return NULL;
2986 static const struct got_error *
2987 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2988 int start_old, int end_old, int start_new, int end_new,
2989 FILE *outfile, FILE *rejectfile)
2991 const struct got_error *err;
2993 /* Copy old file's lines leading up to patch. */
2994 while (!feof(f1) && *line_cur1 < start_old) {
2995 err = copy_one_line(f1, outfile, NULL);
2996 if (err)
2997 return err;
2998 (*line_cur1)++;
3000 /* Skip new file's lines leading up to patch. */
3001 while (!feof(f2) && *line_cur2 < start_new) {
3002 if (rejectfile)
3003 err = copy_one_line(f2, NULL, rejectfile);
3004 else
3005 err = skip_one_line(f2);
3006 if (err)
3007 return err;
3008 (*line_cur2)++;
3010 /* Copy patched lines. */
3011 while (!feof(f2) && *line_cur2 <= end_new) {
3012 err = copy_one_line(f2, outfile, NULL);
3013 if (err)
3014 return err;
3015 (*line_cur2)++;
3017 /* Skip over old file's replaced lines. */
3018 while (!feof(f1) && *line_cur1 <= end_old) {
3019 if (rejectfile)
3020 err = copy_one_line(f1, NULL, rejectfile);
3021 else
3022 err = skip_one_line(f1);
3023 if (err)
3024 return err;
3025 (*line_cur1)++;
3028 return NULL;
3031 static const struct got_error *
3032 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3033 FILE *outfile, FILE *rejectfile)
3035 const struct got_error *err;
3037 if (outfile) {
3038 /* Copy old file's lines until EOF. */
3039 while (!feof(f1)) {
3040 err = copy_one_line(f1, outfile, NULL);
3041 if (err)
3042 return err;
3043 (*line_cur1)++;
3046 if (rejectfile) {
3047 /* Copy new file's lines until EOF. */
3048 while (!feof(f2)) {
3049 err = copy_one_line(f2, NULL, rejectfile);
3050 if (err)
3051 return err;
3052 (*line_cur2)++;
3056 return NULL;
3059 static const struct got_error *
3060 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3061 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3062 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3063 int *line_cur2, FILE *outfile, FILE *rejectfile,
3064 got_worktree_patch_cb patch_cb, void *patch_arg)
3066 const struct got_error *err = NULL;
3067 int start_old = change->cv.a;
3068 int end_old = change->cv.b;
3069 int start_new = change->cv.c;
3070 int end_new = change->cv.d;
3071 long pos1, pos2;
3072 FILE *hunkfile;
3074 *choice = GOT_PATCH_CHOICE_NONE;
3076 hunkfile = got_opentemp();
3077 if (hunkfile == NULL)
3078 return got_error_from_errno("got_opentemp");
3080 pos1 = ftell(f1);
3081 pos2 = ftell(f2);
3083 /* XXX TODO needs error checking */
3084 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3086 if (fseek(f1, pos1, SEEK_SET) == -1) {
3087 err = got_ferror(f1, GOT_ERR_IO);
3088 goto done;
3090 if (fseek(f2, pos2, SEEK_SET) == -1) {
3091 err = got_ferror(f1, GOT_ERR_IO);
3092 goto done;
3094 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3095 err = got_ferror(hunkfile, GOT_ERR_IO);
3096 goto done;
3099 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3100 hunkfile, n, nchanges);
3101 if (err)
3102 goto done;
3104 switch (*choice) {
3105 case GOT_PATCH_CHOICE_YES:
3106 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3107 end_old, start_new, end_new, outfile, rejectfile);
3108 break;
3109 case GOT_PATCH_CHOICE_NO:
3110 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3111 end_old, start_new, end_new, rejectfile, outfile);
3112 break;
3113 case GOT_PATCH_CHOICE_QUIT:
3114 break;
3115 default:
3116 err = got_error(GOT_ERR_PATCH_CHOICE);
3117 break;
3119 done:
3120 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3121 err = got_error_from_errno("fclose");
3122 return err;
3125 struct revert_file_args {
3126 struct got_worktree *worktree;
3127 struct got_fileindex *fileindex;
3128 got_worktree_checkout_cb progress_cb;
3129 void *progress_arg;
3130 got_worktree_patch_cb patch_cb;
3131 void *patch_arg;
3132 struct got_repository *repo;
3135 static const struct got_error *
3136 create_patched_content(char **path_outfile, int reverse_patch,
3137 struct got_object_id *blob_id, const char *path2,
3138 const char *relpath, struct got_repository *repo,
3139 got_worktree_patch_cb patch_cb, void *patch_arg)
3141 const struct got_error *err;
3142 struct got_blob_object *blob = NULL;
3143 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3144 int fd2 = -1;
3145 char *path1 = NULL, *id_str = NULL;
3146 struct stat sb1, sb2;
3147 struct got_diff_changes *changes = NULL;
3148 struct got_diff_state *ds = NULL;
3149 struct got_diff_args *args = NULL;
3150 struct got_diff_change *change;
3151 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3152 int n = 0;
3154 *path_outfile = NULL;
3156 err = got_object_id_str(&id_str, blob_id);
3157 if (err)
3158 return err;
3160 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3161 if (fd2 == -1) {
3162 err = got_error_from_errno2("open", path2);
3163 goto done;
3165 if (fstat(fd2, &sb2) == -1) {
3166 err = got_error_from_errno2("fstat", path2);
3167 goto done;
3170 f2 = fdopen(fd2, "r");
3171 if (f2 == NULL) {
3172 err = got_error_from_errno2("fopen", path2);
3173 goto done;
3175 fd2 = -1;
3177 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3178 if (err)
3179 goto done;
3181 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3182 if (err)
3183 goto done;
3185 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3186 if (err)
3187 goto done;
3189 if (stat(path1, &sb1) == -1) {
3190 err = got_error_from_errno2("stat", path1);
3191 goto done;
3194 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3195 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3196 if (err)
3197 goto done;
3199 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3200 if (err)
3201 goto done;
3203 if (fseek(f1, 0L, SEEK_SET) == -1)
3204 return got_ferror(f1, GOT_ERR_IO);
3205 if (fseek(f2, 0L, SEEK_SET) == -1)
3206 return got_ferror(f2, GOT_ERR_IO);
3207 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3208 int choice;
3209 err = apply_or_reject_change(&choice, change, ++n,
3210 changes->nchanges, ds, args, diff_flags, relpath,
3211 f1, f2, &line_cur1, &line_cur2,
3212 reverse_patch ? NULL : outfile,
3213 reverse_patch ? outfile : NULL,
3214 patch_cb, patch_arg);
3215 if (err)
3216 goto done;
3217 if (choice == GOT_PATCH_CHOICE_YES)
3218 have_content = 1;
3219 else if (choice == GOT_PATCH_CHOICE_QUIT)
3220 break;
3222 if (have_content) {
3223 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3224 reverse_patch ? NULL : outfile,
3225 reverse_patch ? outfile : NULL);
3226 if (err)
3227 goto done;
3229 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3230 err = got_error_from_errno2("chmod", path2);
3231 goto done;
3234 done:
3235 free(id_str);
3236 if (blob)
3237 got_object_blob_close(blob);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno2("fclose", path1);
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno2("fclose", path2);
3242 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3243 err = got_error_from_errno2("close", path2);
3244 if (outfile && fclose(outfile) == EOF && err == NULL)
3245 err = got_error_from_errno2("fclose", *path_outfile);
3246 if (path1 && unlink(path1) == -1 && err == NULL)
3247 err = got_error_from_errno2("unlink", path1);
3248 if (err || !have_content) {
3249 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3250 err = got_error_from_errno2("unlink", *path_outfile);
3251 free(*path_outfile);
3252 *path_outfile = NULL;
3254 free(args);
3255 if (ds) {
3256 got_diff_state_free(ds);
3257 free(ds);
3259 if (changes)
3260 got_diff_free_changes(changes);
3261 free(path1);
3262 return err;
3265 static const struct got_error *
3266 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3267 const char *relpath, struct got_object_id *blob_id,
3268 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3270 struct revert_file_args *a = arg;
3271 const struct got_error *err = NULL;
3272 char *parent_path = NULL;
3273 struct got_fileindex_entry *ie;
3274 struct got_tree_object *tree = NULL;
3275 struct got_object_id *tree_id = NULL;
3276 const struct got_tree_entry *te = NULL;
3277 char *tree_path = NULL, *te_name;
3278 char *ondisk_path = NULL, *path_content = NULL;
3279 struct got_blob_object *blob = NULL;
3281 /* Reverting a staged deletion is a no-op. */
3282 if (status == GOT_STATUS_DELETE &&
3283 staged_status != GOT_STATUS_NO_CHANGE)
3284 return NULL;
3286 if (status == GOT_STATUS_UNVERSIONED)
3287 return (*a->progress_cb)(a->progress_arg,
3288 GOT_STATUS_UNVERSIONED, relpath);
3290 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3291 if (ie == NULL)
3292 return got_error(GOT_ERR_BAD_PATH);
3294 /* Construct in-repository path of tree which contains this blob. */
3295 err = got_path_dirname(&parent_path, ie->path);
3296 if (err) {
3297 if (err->code != GOT_ERR_BAD_PATH)
3298 goto done;
3299 parent_path = strdup("/");
3300 if (parent_path == NULL) {
3301 err = got_error_from_errno("strdup");
3302 goto done;
3305 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3306 tree_path = strdup(parent_path);
3307 if (tree_path == NULL) {
3308 err = got_error_from_errno("strdup");
3309 goto done;
3311 } else {
3312 if (got_path_is_root_dir(parent_path)) {
3313 tree_path = strdup(a->worktree->path_prefix);
3314 if (tree_path == NULL) {
3315 err = got_error_from_errno("strdup");
3316 goto done;
3318 } else {
3319 if (asprintf(&tree_path, "%s/%s",
3320 a->worktree->path_prefix, parent_path) == -1) {
3321 err = got_error_from_errno("asprintf");
3322 goto done;
3327 err = got_object_id_by_path(&tree_id, a->repo,
3328 a->worktree->base_commit_id, tree_path);
3329 if (err) {
3330 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3331 (status == GOT_STATUS_ADD ||
3332 staged_status == GOT_STATUS_ADD)))
3333 goto done;
3334 } else {
3335 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3336 if (err)
3337 goto done;
3339 te_name = basename(ie->path);
3340 if (te_name == NULL) {
3341 err = got_error_from_errno2("basename", ie->path);
3342 goto done;
3345 te = got_object_tree_find_entry(tree, te_name);
3346 if (te == NULL && status != GOT_STATUS_ADD &&
3347 staged_status != GOT_STATUS_ADD) {
3348 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3349 goto done;
3353 switch (status) {
3354 case GOT_STATUS_ADD:
3355 if (a->patch_cb) {
3356 int choice = GOT_PATCH_CHOICE_NONE;
3357 err = (*a->patch_cb)(&choice, a->patch_arg,
3358 status, ie->path, NULL, 1, 1);
3359 if (err)
3360 goto done;
3361 if (choice != GOT_PATCH_CHOICE_YES)
3362 break;
3364 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3365 ie->path);
3366 if (err)
3367 goto done;
3368 got_fileindex_entry_remove(a->fileindex, ie);
3369 break;
3370 case GOT_STATUS_DELETE:
3371 if (a->patch_cb) {
3372 int choice = GOT_PATCH_CHOICE_NONE;
3373 err = (*a->patch_cb)(&choice, a->patch_arg,
3374 status, ie->path, NULL, 1, 1);
3375 if (err)
3376 goto done;
3377 if (choice != GOT_PATCH_CHOICE_YES)
3378 break;
3380 /* fall through */
3381 case GOT_STATUS_MODIFY:
3382 case GOT_STATUS_MODE_CHANGE:
3383 case GOT_STATUS_CONFLICT:
3384 case GOT_STATUS_MISSING: {
3385 struct got_object_id id;
3386 if (staged_status == GOT_STATUS_ADD ||
3387 staged_status == GOT_STATUS_MODIFY) {
3388 memcpy(id.sha1, ie->staged_blob_sha1,
3389 SHA1_DIGEST_LENGTH);
3390 } else
3391 memcpy(id.sha1, ie->blob_sha1,
3392 SHA1_DIGEST_LENGTH);
3393 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3394 if (err)
3395 goto done;
3397 if (asprintf(&ondisk_path, "%s/%s",
3398 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3399 err = got_error_from_errno("asprintf");
3400 goto done;
3403 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3404 status == GOT_STATUS_CONFLICT)) {
3405 err = create_patched_content(&path_content, 1, &id,
3406 ondisk_path, ie->path, a->repo,
3407 a->patch_cb, a->patch_arg);
3408 if (err || path_content == NULL)
3409 break;
3410 if (rename(path_content, ondisk_path) == -1) {
3411 err = got_error_from_errno3("rename",
3412 path_content, ondisk_path);
3413 goto done;
3415 } else {
3416 err = install_blob(a->worktree, ondisk_path, ie->path,
3417 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3418 got_fileindex_perms_to_st(ie), blob, 0, 1,
3419 a->repo, a->progress_cb, a->progress_arg);
3420 if (err)
3421 goto done;
3422 if (status == GOT_STATUS_DELETE ||
3423 status == GOT_STATUS_MODE_CHANGE) {
3424 err = update_blob_fileindex_entry(a->worktree,
3425 a->fileindex, ie, ondisk_path, ie->path,
3426 blob, 1);
3427 if (err)
3428 goto done;
3431 break;
3433 default:
3434 break;
3436 done:
3437 free(ondisk_path);
3438 free(path_content);
3439 free(parent_path);
3440 free(tree_path);
3441 if (blob)
3442 got_object_blob_close(blob);
3443 if (tree)
3444 got_object_tree_close(tree);
3445 free(tree_id);
3446 return err;
3449 const struct got_error *
3450 got_worktree_revert(struct got_worktree *worktree,
3451 struct got_pathlist_head *paths,
3452 got_worktree_checkout_cb progress_cb, void *progress_arg,
3453 got_worktree_patch_cb patch_cb, void *patch_arg,
3454 struct got_repository *repo)
3456 struct got_fileindex *fileindex = NULL;
3457 char *fileindex_path = NULL;
3458 const struct got_error *err = NULL, *unlockerr = NULL;
3459 const struct got_error *sync_err = NULL;
3460 struct got_pathlist_entry *pe;
3461 struct revert_file_args rfa;
3463 err = lock_worktree(worktree, LOCK_EX);
3464 if (err)
3465 return err;
3467 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3468 if (err)
3469 goto done;
3471 rfa.worktree = worktree;
3472 rfa.fileindex = fileindex;
3473 rfa.progress_cb = progress_cb;
3474 rfa.progress_arg = progress_arg;
3475 rfa.patch_cb = patch_cb;
3476 rfa.patch_arg = patch_arg;
3477 rfa.repo = repo;
3478 TAILQ_FOREACH(pe, paths, entry) {
3479 err = worktree_status(worktree, pe->path, fileindex, repo,
3480 revert_file, &rfa, NULL, NULL);
3481 if (err)
3482 break;
3484 sync_err = sync_fileindex(fileindex, fileindex_path);
3485 if (sync_err && err == NULL)
3486 err = sync_err;
3487 done:
3488 free(fileindex_path);
3489 if (fileindex)
3490 got_fileindex_free(fileindex);
3491 unlockerr = lock_worktree(worktree, LOCK_SH);
3492 if (unlockerr && err == NULL)
3493 err = unlockerr;
3494 return err;
3497 static void
3498 free_commitable(struct got_commitable *ct)
3500 free(ct->path);
3501 free(ct->in_repo_path);
3502 free(ct->ondisk_path);
3503 free(ct->blob_id);
3504 free(ct->base_blob_id);
3505 free(ct->staged_blob_id);
3506 free(ct->base_commit_id);
3507 free(ct);
3510 struct collect_commitables_arg {
3511 struct got_pathlist_head *commitable_paths;
3512 struct got_repository *repo;
3513 struct got_worktree *worktree;
3514 int have_staged_files;
3517 static const struct got_error *
3518 collect_commitables(void *arg, unsigned char status,
3519 unsigned char staged_status, const char *relpath,
3520 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3521 struct got_object_id *commit_id)
3523 struct collect_commitables_arg *a = arg;
3524 const struct got_error *err = NULL;
3525 struct got_commitable *ct = NULL;
3526 struct got_pathlist_entry *new = NULL;
3527 char *parent_path = NULL, *path = NULL;
3528 struct stat sb;
3530 if (a->have_staged_files) {
3531 if (staged_status != GOT_STATUS_MODIFY &&
3532 staged_status != GOT_STATUS_ADD &&
3533 staged_status != GOT_STATUS_DELETE)
3534 return NULL;
3535 } else {
3536 if (status == GOT_STATUS_CONFLICT)
3537 return got_error(GOT_ERR_COMMIT_CONFLICT);
3539 if (status != GOT_STATUS_MODIFY &&
3540 status != GOT_STATUS_MODE_CHANGE &&
3541 status != GOT_STATUS_ADD &&
3542 status != GOT_STATUS_DELETE)
3543 return NULL;
3546 if (asprintf(&path, "/%s", relpath) == -1) {
3547 err = got_error_from_errno("asprintf");
3548 goto done;
3550 if (strcmp(path, "/") == 0) {
3551 parent_path = strdup("");
3552 if (parent_path == NULL)
3553 return got_error_from_errno("strdup");
3554 } else {
3555 err = got_path_dirname(&parent_path, path);
3556 if (err)
3557 return err;
3560 ct = calloc(1, sizeof(*ct));
3561 if (ct == NULL) {
3562 err = got_error_from_errno("calloc");
3563 goto done;
3566 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3567 relpath) == -1) {
3568 err = got_error_from_errno("asprintf");
3569 goto done;
3571 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3572 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3573 } else {
3574 if (lstat(ct->ondisk_path, &sb) != 0) {
3575 err = got_error_from_errno2("lstat", ct->ondisk_path);
3576 goto done;
3578 ct->mode = sb.st_mode;
3581 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3582 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3583 relpath) == -1) {
3584 err = got_error_from_errno("asprintf");
3585 goto done;
3588 ct->status = status;
3589 ct->staged_status = staged_status;
3590 ct->blob_id = NULL; /* will be filled in when blob gets created */
3591 if (ct->status != GOT_STATUS_ADD &&
3592 ct->staged_status != GOT_STATUS_ADD) {
3593 ct->base_blob_id = got_object_id_dup(blob_id);
3594 if (ct->base_blob_id == NULL) {
3595 err = got_error_from_errno("got_object_id_dup");
3596 goto done;
3598 ct->base_commit_id = got_object_id_dup(commit_id);
3599 if (ct->base_commit_id == NULL) {
3600 err = got_error_from_errno("got_object_id_dup");
3601 goto done;
3604 if (ct->staged_status == GOT_STATUS_ADD ||
3605 ct->staged_status == GOT_STATUS_MODIFY) {
3606 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3607 if (ct->staged_blob_id == NULL) {
3608 err = got_error_from_errno("got_object_id_dup");
3609 goto done;
3612 ct->path = strdup(path);
3613 if (ct->path == NULL) {
3614 err = got_error_from_errno("strdup");
3615 goto done;
3617 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3618 done:
3619 if (ct && (err || new == NULL))
3620 free_commitable(ct);
3621 free(parent_path);
3622 free(path);
3623 return err;
3626 static const struct got_error *write_tree(struct got_object_id **,
3627 struct got_tree_object *, const char *, struct got_pathlist_head *,
3628 got_worktree_status_cb status_cb, void *status_arg,
3629 struct got_repository *);
3631 static const struct got_error *
3632 write_subtree(struct got_object_id **new_subtree_id,
3633 struct got_tree_entry *te, const char *parent_path,
3634 struct got_pathlist_head *commitable_paths,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 struct got_repository *repo)
3638 const struct got_error *err = NULL;
3639 struct got_tree_object *subtree;
3640 char *subpath;
3642 if (asprintf(&subpath, "%s%s%s", parent_path,
3643 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3644 return got_error_from_errno("asprintf");
3646 err = got_object_open_as_tree(&subtree, repo, te->id);
3647 if (err)
3648 return err;
3650 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3651 status_cb, status_arg, repo);
3652 got_object_tree_close(subtree);
3653 free(subpath);
3654 return err;
3657 static const struct got_error *
3658 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3660 const struct got_error *err = NULL;
3661 char *ct_parent_path = NULL;
3663 *match = 0;
3665 if (strchr(ct->in_repo_path, '/') == NULL) {
3666 *match = got_path_is_root_dir(path);
3667 return NULL;
3670 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3671 if (err)
3672 return err;
3673 *match = (strcmp(path, ct_parent_path) == 0);
3674 free(ct_parent_path);
3675 return err;
3678 static mode_t
3679 get_ct_file_mode(struct got_commitable *ct)
3681 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3684 static const struct got_error *
3685 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3686 struct got_tree_entry *te, struct got_commitable *ct)
3688 const struct got_error *err = NULL;
3690 *new_te = NULL;
3692 err = got_object_tree_entry_dup(new_te, te);
3693 if (err)
3694 goto done;
3696 (*new_te)->mode = get_ct_file_mode(ct);
3698 free((*new_te)->id);
3699 if (ct->staged_status == GOT_STATUS_MODIFY)
3700 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3701 else
3702 (*new_te)->id = got_object_id_dup(ct->blob_id);
3703 if ((*new_te)->id == NULL) {
3704 err = got_error_from_errno("got_object_id_dup");
3705 goto done;
3707 done:
3708 if (err && *new_te) {
3709 got_object_tree_entry_close(*new_te);
3710 *new_te = NULL;
3712 return err;
3715 static const struct got_error *
3716 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3717 struct got_commitable *ct)
3719 const struct got_error *err = NULL;
3720 char *ct_name;
3722 *new_te = NULL;
3724 *new_te = calloc(1, sizeof(**new_te));
3725 if (*new_te == NULL)
3726 return got_error_from_errno("calloc");
3728 ct_name = basename(ct->path);
3729 if (ct_name == NULL) {
3730 err = got_error_from_errno2("basename", ct->path);
3731 goto done;
3733 (*new_te)->name = strdup(ct_name);
3734 if ((*new_te)->name == NULL) {
3735 err = got_error_from_errno("strdup");
3736 goto done;
3739 (*new_te)->mode = get_ct_file_mode(ct);
3741 if (ct->staged_status == GOT_STATUS_ADD)
3742 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3743 else
3744 (*new_te)->id = got_object_id_dup(ct->blob_id);
3745 if ((*new_te)->id == NULL) {
3746 err = got_error_from_errno("got_object_id_dup");
3747 goto done;
3749 done:
3750 if (err && *new_te) {
3751 got_object_tree_entry_close(*new_te);
3752 *new_te = NULL;
3754 return err;
3757 static const struct got_error *
3758 insert_tree_entry(struct got_tree_entry *new_te,
3759 struct got_pathlist_head *paths)
3761 const struct got_error *err = NULL;
3762 struct got_pathlist_entry *new_pe;
3764 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3765 if (err)
3766 return err;
3767 if (new_pe == NULL)
3768 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3769 return NULL;
3772 static const struct got_error *
3773 report_ct_status(struct got_commitable *ct,
3774 got_worktree_status_cb status_cb, void *status_arg)
3776 const char *ct_path = ct->path;
3777 unsigned char status;
3779 while (ct_path[0] == '/')
3780 ct_path++;
3782 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3783 status = ct->staged_status;
3784 else
3785 status = ct->status;
3787 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3788 ct_path, ct->blob_id, NULL, NULL);
3791 static const struct got_error *
3792 match_modified_subtree(int *modified, struct got_tree_entry *te,
3793 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3795 const struct got_error *err = NULL;
3796 struct got_pathlist_entry *pe;
3797 char *te_path;
3799 *modified = 0;
3801 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3802 got_path_is_root_dir(base_tree_path) ? "" : "/",
3803 te->name) == -1)
3804 return got_error_from_errno("asprintf");
3806 TAILQ_FOREACH(pe, commitable_paths, entry) {
3807 struct got_commitable *ct = pe->data;
3808 *modified = got_path_is_child(ct->in_repo_path, te_path,
3809 strlen(te_path));
3810 if (*modified)
3811 break;
3814 free(te_path);
3815 return err;
3818 static const struct got_error *
3819 match_deleted_or_modified_ct(struct got_commitable **ctp,
3820 struct got_tree_entry *te, const char *base_tree_path,
3821 struct got_pathlist_head *commitable_paths)
3823 const struct got_error *err = NULL;
3824 struct got_pathlist_entry *pe;
3826 *ctp = NULL;
3828 TAILQ_FOREACH(pe, commitable_paths, entry) {
3829 struct got_commitable *ct = pe->data;
3830 char *ct_name = NULL;
3831 int path_matches;
3833 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3834 if (ct->status != GOT_STATUS_MODIFY &&
3835 ct->status != GOT_STATUS_MODE_CHANGE &&
3836 ct->status != GOT_STATUS_DELETE)
3837 continue;
3838 } else {
3839 if (ct->staged_status != GOT_STATUS_MODIFY &&
3840 ct->staged_status != GOT_STATUS_DELETE)
3841 continue;
3844 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3845 continue;
3847 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3848 if (err)
3849 return err;
3850 if (!path_matches)
3851 continue;
3853 ct_name = basename(pe->path);
3854 if (ct_name == NULL)
3855 return got_error_from_errno2("basename", pe->path);
3857 if (strcmp(te->name, ct_name) != 0)
3858 continue;
3860 *ctp = ct;
3861 break;
3864 return err;
3867 static const struct got_error *
3868 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3869 const char *child_path, const char *path_base_tree,
3870 struct got_pathlist_head *commitable_paths,
3871 got_worktree_status_cb status_cb, void *status_arg,
3872 struct got_repository *repo)
3874 const struct got_error *err = NULL;
3875 struct got_tree_entry *new_te;
3876 char *subtree_path;
3878 *new_tep = NULL;
3880 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3881 got_path_is_root_dir(path_base_tree) ? "" : "/",
3882 child_path) == -1)
3883 return got_error_from_errno("asprintf");
3885 new_te = calloc(1, sizeof(*new_te));
3886 if (new_te == NULL)
3887 return got_error_from_errno("calloc");
3888 new_te->mode = S_IFDIR;
3889 new_te->name = strdup(child_path);
3890 if (new_te->name == NULL) {
3891 err = got_error_from_errno("strdup");
3892 got_object_tree_entry_close(new_te);
3893 goto done;
3895 err = write_tree(&new_te->id, NULL, subtree_path,
3896 commitable_paths, status_cb, status_arg, repo);
3897 if (err) {
3898 got_object_tree_entry_close(new_te);
3899 goto done;
3901 done:
3902 free(subtree_path);
3903 if (err == NULL)
3904 *new_tep = new_te;
3905 return err;
3908 static const struct got_error *
3909 write_tree(struct got_object_id **new_tree_id,
3910 struct got_tree_object *base_tree, const char *path_base_tree,
3911 struct got_pathlist_head *commitable_paths,
3912 got_worktree_status_cb status_cb, void *status_arg,
3913 struct got_repository *repo)
3915 const struct got_error *err = NULL;
3916 const struct got_tree_entries *base_entries = NULL;
3917 struct got_pathlist_head paths;
3918 struct got_tree_entries new_tree_entries;
3919 struct got_tree_entry *te, *new_te = NULL;
3920 struct got_pathlist_entry *pe;
3922 TAILQ_INIT(&paths);
3923 new_tree_entries.nentries = 0;
3924 SIMPLEQ_INIT(&new_tree_entries.head);
3926 /* Insert, and recurse into, newly added entries first. */
3927 TAILQ_FOREACH(pe, commitable_paths, entry) {
3928 struct got_commitable *ct = pe->data;
3929 char *child_path = NULL, *slash;
3931 if ((ct->status != GOT_STATUS_ADD &&
3932 ct->staged_status != GOT_STATUS_ADD) ||
3933 (ct->flags & GOT_COMMITABLE_ADDED))
3934 continue;
3936 if (!got_path_is_child(pe->path, path_base_tree,
3937 strlen(path_base_tree)))
3938 continue;
3940 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3941 pe->path);
3942 if (err)
3943 goto done;
3945 slash = strchr(child_path, '/');
3946 if (slash == NULL) {
3947 err = alloc_added_blob_tree_entry(&new_te, ct);
3948 if (err)
3949 goto done;
3950 err = report_ct_status(ct, status_cb, status_arg);
3951 if (err)
3952 goto done;
3953 ct->flags |= GOT_COMMITABLE_ADDED;
3954 err = insert_tree_entry(new_te, &paths);
3955 if (err)
3956 goto done;
3957 } else {
3958 *slash = '\0'; /* trim trailing path components */
3959 if (base_tree == NULL ||
3960 got_object_tree_find_entry(base_tree, child_path)
3961 == NULL) {
3962 err = make_subtree_for_added_blob(&new_te,
3963 child_path, path_base_tree,
3964 commitable_paths, status_cb, status_arg,
3965 repo);
3966 if (err)
3967 goto done;
3968 err = insert_tree_entry(new_te, &paths);
3969 if (err)
3970 goto done;
3975 if (base_tree) {
3976 /* Handle modified and deleted entries. */
3977 base_entries = got_object_tree_get_entries(base_tree);
3978 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3979 struct got_commitable *ct = NULL;
3981 if (got_object_tree_entry_is_submodule(te)) {
3982 /* Entry is a submodule; just copy it. */
3983 err = got_object_tree_entry_dup(&new_te, te);
3984 if (err)
3985 goto done;
3986 err = insert_tree_entry(new_te, &paths);
3987 if (err)
3988 goto done;
3989 continue;
3992 if (S_ISDIR(te->mode)) {
3993 int modified;
3994 err = got_object_tree_entry_dup(&new_te, te);
3995 if (err)
3996 goto done;
3997 err = match_modified_subtree(&modified, te,
3998 path_base_tree, commitable_paths);
3999 if (err)
4000 goto done;
4001 /* Avoid recursion into unmodified subtrees. */
4002 if (modified) {
4003 free(new_te->id);
4004 err = write_subtree(&new_te->id, te,
4005 path_base_tree, commitable_paths,
4006 status_cb, status_arg, repo);
4007 if (err)
4008 goto done;
4010 err = insert_tree_entry(new_te, &paths);
4011 if (err)
4012 goto done;
4013 continue;
4016 err = match_deleted_or_modified_ct(&ct, te,
4017 path_base_tree, commitable_paths);
4018 if (err)
4019 goto done;
4020 if (ct) {
4021 /* NB: Deleted entries get dropped here. */
4022 if (ct->status == GOT_STATUS_MODIFY ||
4023 ct->status == GOT_STATUS_MODE_CHANGE ||
4024 ct->staged_status == GOT_STATUS_MODIFY) {
4025 err = alloc_modified_blob_tree_entry(
4026 &new_te, te, ct);
4027 if (err)
4028 goto done;
4029 err = insert_tree_entry(new_te, &paths);
4030 if (err)
4031 goto done;
4033 err = report_ct_status(ct, status_cb,
4034 status_arg);
4035 if (err)
4036 goto done;
4037 } else {
4038 /* Entry is unchanged; just copy it. */
4039 err = got_object_tree_entry_dup(&new_te, te);
4040 if (err)
4041 goto done;
4042 err = insert_tree_entry(new_te, &paths);
4043 if (err)
4044 goto done;
4049 /* Write new list of entries; deleted entries have been dropped. */
4050 TAILQ_FOREACH(pe, &paths, entry) {
4051 struct got_tree_entry *te = pe->data;
4052 new_tree_entries.nentries++;
4053 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
4055 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
4056 done:
4057 got_object_tree_entries_close(&new_tree_entries);
4058 got_pathlist_free(&paths);
4059 return err;
4062 static const struct got_error *
4063 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4064 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4065 int have_staged_files)
4067 const struct got_error *err = NULL;
4068 struct got_pathlist_entry *pe;
4070 TAILQ_FOREACH(pe, commitable_paths, entry) {
4071 struct got_fileindex_entry *ie;
4072 struct got_commitable *ct = pe->data;
4074 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4075 if (ie) {
4076 if (ct->status == GOT_STATUS_DELETE ||
4077 ct->staged_status == GOT_STATUS_DELETE) {
4078 got_fileindex_entry_remove(fileindex, ie);
4079 got_fileindex_entry_free(ie);
4080 } else if (ct->staged_status == GOT_STATUS_ADD ||
4081 ct->staged_status == GOT_STATUS_MODIFY) {
4082 got_fileindex_entry_stage_set(ie,
4083 GOT_FILEIDX_STAGE_NONE);
4084 err = got_fileindex_entry_update(ie,
4085 ct->ondisk_path, ct->staged_blob_id->sha1,
4086 new_base_commit_id->sha1,
4087 !have_staged_files);
4088 } else
4089 err = got_fileindex_entry_update(ie,
4090 ct->ondisk_path, ct->blob_id->sha1,
4091 new_base_commit_id->sha1,
4092 !have_staged_files);
4093 } else {
4094 err = got_fileindex_entry_alloc(&ie,
4095 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4096 new_base_commit_id->sha1);
4097 if (err)
4098 break;
4099 err = got_fileindex_entry_add(fileindex, ie);
4100 if (err)
4101 break;
4104 return err;
4108 static const struct got_error *
4109 check_out_of_date(const char *in_repo_path, unsigned char status,
4110 unsigned char staged_status, struct got_object_id *base_blob_id,
4111 struct got_object_id *base_commit_id,
4112 struct got_object_id *head_commit_id, struct got_repository *repo,
4113 int ood_errcode)
4115 const struct got_error *err = NULL;
4116 struct got_object_id *id = NULL;
4118 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4119 /* Trivial case: base commit == head commit */
4120 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4121 return NULL;
4123 * Ensure file content which local changes were based
4124 * on matches file content in the branch head.
4126 err = got_object_id_by_path(&id, repo, head_commit_id,
4127 in_repo_path);
4128 if (err) {
4129 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4130 err = got_error(ood_errcode);
4131 goto done;
4132 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4133 err = got_error(ood_errcode);
4134 } else {
4135 /* Require that added files don't exist in the branch head. */
4136 err = got_object_id_by_path(&id, repo, head_commit_id,
4137 in_repo_path);
4138 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4139 goto done;
4140 err = id ? got_error(ood_errcode) : NULL;
4142 done:
4143 free(id);
4144 return err;
4147 const struct got_error *
4148 commit_worktree(struct got_object_id **new_commit_id,
4149 struct got_pathlist_head *commitable_paths,
4150 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4151 const char *author, const char *committer,
4152 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4153 got_worktree_status_cb status_cb, void *status_arg,
4154 struct got_repository *repo)
4156 const struct got_error *err = NULL, *unlockerr = NULL;
4157 struct got_pathlist_entry *pe;
4158 const char *head_ref_name = NULL;
4159 struct got_commit_object *head_commit = NULL;
4160 struct got_reference *head_ref2 = NULL;
4161 struct got_object_id *head_commit_id2 = NULL;
4162 struct got_tree_object *head_tree = NULL;
4163 struct got_object_id *new_tree_id = NULL;
4164 struct got_object_id_queue parent_ids;
4165 struct got_object_qid *pid = NULL;
4166 char *logmsg = NULL;
4168 *new_commit_id = NULL;
4170 SIMPLEQ_INIT(&parent_ids);
4172 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4173 if (err)
4174 goto done;
4176 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4177 if (err)
4178 goto done;
4180 if (commit_msg_cb != NULL) {
4181 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4182 if (err)
4183 goto done;
4186 if (logmsg == NULL || strlen(logmsg) == 0) {
4187 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4188 goto done;
4191 /* Create blobs from added and modified files and record their IDs. */
4192 TAILQ_FOREACH(pe, commitable_paths, entry) {
4193 struct got_commitable *ct = pe->data;
4194 char *ondisk_path;
4196 /* Blobs for staged files already exist. */
4197 if (ct->staged_status == GOT_STATUS_ADD ||
4198 ct->staged_status == GOT_STATUS_MODIFY)
4199 continue;
4201 if (ct->status != GOT_STATUS_ADD &&
4202 ct->status != GOT_STATUS_MODIFY &&
4203 ct->status != GOT_STATUS_MODE_CHANGE)
4204 continue;
4206 if (asprintf(&ondisk_path, "%s/%s",
4207 worktree->root_path, pe->path) == -1) {
4208 err = got_error_from_errno("asprintf");
4209 goto done;
4211 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4212 free(ondisk_path);
4213 if (err)
4214 goto done;
4217 /* Recursively write new tree objects. */
4218 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4219 status_cb, status_arg, repo);
4220 if (err)
4221 goto done;
4223 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4224 if (err)
4225 goto done;
4226 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4227 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4228 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4229 got_object_qid_free(pid);
4230 if (logmsg != NULL)
4231 free(logmsg);
4232 if (err)
4233 goto done;
4235 /* Check if a concurrent commit to our branch has occurred. */
4236 head_ref_name = got_worktree_get_head_ref_name(worktree);
4237 if (head_ref_name == NULL) {
4238 err = got_error_from_errno("got_worktree_get_head_ref_name");
4239 goto done;
4241 /* Lock the reference here to prevent concurrent modification. */
4242 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4243 if (err)
4244 goto done;
4245 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4246 if (err)
4247 goto done;
4248 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4249 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4250 goto done;
4252 /* Update branch head in repository. */
4253 err = got_ref_change_ref(head_ref2, *new_commit_id);
4254 if (err)
4255 goto done;
4256 err = got_ref_write(head_ref2, repo);
4257 if (err)
4258 goto done;
4260 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4261 if (err)
4262 goto done;
4264 err = ref_base_commit(worktree, repo);
4265 if (err)
4266 goto done;
4267 done:
4268 if (head_tree)
4269 got_object_tree_close(head_tree);
4270 if (head_commit)
4271 got_object_commit_close(head_commit);
4272 free(head_commit_id2);
4273 if (head_ref2) {
4274 unlockerr = got_ref_unlock(head_ref2);
4275 if (unlockerr && err == NULL)
4276 err = unlockerr;
4277 got_ref_close(head_ref2);
4279 return err;
4282 static const struct got_error *
4283 check_path_is_commitable(const char *path,
4284 struct got_pathlist_head *commitable_paths)
4286 struct got_pathlist_entry *cpe = NULL;
4287 size_t path_len = strlen(path);
4289 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4290 struct got_commitable *ct = cpe->data;
4291 const char *ct_path = ct->path;
4293 while (ct_path[0] == '/')
4294 ct_path++;
4296 if (strcmp(path, ct_path) == 0 ||
4297 got_path_is_child(ct_path, path, path_len))
4298 break;
4301 if (cpe == NULL)
4302 return got_error_path(path, GOT_ERR_BAD_PATH);
4304 return NULL;
4307 static const struct got_error *
4308 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4310 int *have_staged_files = arg;
4312 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4313 *have_staged_files = 1;
4314 return got_error(GOT_ERR_CANCELLED);
4317 return NULL;
4320 static const struct got_error *
4321 check_non_staged_files(struct got_fileindex *fileindex,
4322 struct got_pathlist_head *paths)
4324 struct got_pathlist_entry *pe;
4325 struct got_fileindex_entry *ie;
4327 TAILQ_FOREACH(pe, paths, entry) {
4328 if (pe->path[0] == '\0')
4329 continue;
4330 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4331 if (ie == NULL)
4332 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4333 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4334 return got_error_path(pe->path,
4335 GOT_ERR_FILE_NOT_STAGED);
4338 return NULL;
4341 const struct got_error *
4342 got_worktree_commit(struct got_object_id **new_commit_id,
4343 struct got_worktree *worktree, struct got_pathlist_head *paths,
4344 const char *author, const char *committer,
4345 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4346 got_worktree_status_cb status_cb, void *status_arg,
4347 struct got_repository *repo)
4349 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4350 struct got_fileindex *fileindex = NULL;
4351 char *fileindex_path = NULL;
4352 struct got_pathlist_head commitable_paths;
4353 struct collect_commitables_arg cc_arg;
4354 struct got_pathlist_entry *pe;
4355 struct got_reference *head_ref = NULL;
4356 struct got_object_id *head_commit_id = NULL;
4357 int have_staged_files = 0;
4359 *new_commit_id = NULL;
4361 TAILQ_INIT(&commitable_paths);
4363 err = lock_worktree(worktree, LOCK_EX);
4364 if (err)
4365 goto done;
4367 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4368 if (err)
4369 goto done;
4371 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4372 if (err)
4373 goto done;
4375 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4376 if (err)
4377 goto done;
4379 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4380 &have_staged_files);
4381 if (err && err->code != GOT_ERR_CANCELLED)
4382 goto done;
4383 if (have_staged_files) {
4384 err = check_non_staged_files(fileindex, paths);
4385 if (err)
4386 goto done;
4389 cc_arg.commitable_paths = &commitable_paths;
4390 cc_arg.worktree = worktree;
4391 cc_arg.repo = repo;
4392 cc_arg.have_staged_files = have_staged_files;
4393 TAILQ_FOREACH(pe, paths, entry) {
4394 err = worktree_status(worktree, pe->path, fileindex, repo,
4395 collect_commitables, &cc_arg, NULL, NULL);
4396 if (err)
4397 goto done;
4400 if (TAILQ_EMPTY(&commitable_paths)) {
4401 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4402 goto done;
4405 TAILQ_FOREACH(pe, paths, entry) {
4406 err = check_path_is_commitable(pe->path, &commitable_paths);
4407 if (err)
4408 goto done;
4411 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4412 struct got_commitable *ct = pe->data;
4413 const char *ct_path = ct->in_repo_path;
4415 while (ct_path[0] == '/')
4416 ct_path++;
4417 err = check_out_of_date(ct_path, ct->status,
4418 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4419 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4420 if (err)
4421 goto done;
4425 err = commit_worktree(new_commit_id, &commitable_paths,
4426 head_commit_id, worktree, author, committer,
4427 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4428 if (err)
4429 goto done;
4431 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4432 fileindex, have_staged_files);
4433 sync_err = sync_fileindex(fileindex, fileindex_path);
4434 if (sync_err && err == NULL)
4435 err = sync_err;
4436 done:
4437 if (fileindex)
4438 got_fileindex_free(fileindex);
4439 free(fileindex_path);
4440 unlockerr = lock_worktree(worktree, LOCK_SH);
4441 if (unlockerr && err == NULL)
4442 err = unlockerr;
4443 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4444 struct got_commitable *ct = pe->data;
4445 free_commitable(ct);
4447 got_pathlist_free(&commitable_paths);
4448 return err;
4451 const char *
4452 got_commitable_get_path(struct got_commitable *ct)
4454 return ct->path;
4457 unsigned int
4458 got_commitable_get_status(struct got_commitable *ct)
4460 return ct->status;
4463 struct check_rebase_ok_arg {
4464 struct got_worktree *worktree;
4465 struct got_repository *repo;
4468 static const struct got_error *
4469 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4471 const struct got_error *err = NULL;
4472 struct check_rebase_ok_arg *a = arg;
4473 unsigned char status;
4474 struct stat sb;
4475 char *ondisk_path;
4477 /* Reject rebase of a work tree with mixed base commits. */
4478 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4479 SHA1_DIGEST_LENGTH))
4480 return got_error(GOT_ERR_MIXED_COMMITS);
4482 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4483 == -1)
4484 return got_error_from_errno("asprintf");
4486 /* Reject rebase of a work tree with modified or staged files. */
4487 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4488 free(ondisk_path);
4489 if (err)
4490 return err;
4492 if (status != GOT_STATUS_NO_CHANGE)
4493 return got_error(GOT_ERR_MODIFIED);
4494 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4495 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4497 return NULL;
4500 const struct got_error *
4501 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4502 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4503 struct got_worktree *worktree, struct got_reference *branch,
4504 struct got_repository *repo)
4506 const struct got_error *err = NULL;
4507 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4508 char *branch_ref_name = NULL;
4509 char *fileindex_path = NULL;
4510 struct check_rebase_ok_arg ok_arg;
4511 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4513 *new_base_branch_ref = NULL;
4514 *tmp_branch = NULL;
4515 *fileindex = NULL;
4517 err = lock_worktree(worktree, LOCK_EX);
4518 if (err)
4519 return err;
4521 err = open_fileindex(fileindex, &fileindex_path, worktree);
4522 if (err)
4523 goto done;
4525 ok_arg.worktree = worktree;
4526 ok_arg.repo = repo;
4527 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4528 &ok_arg);
4529 if (err)
4530 goto done;
4532 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4533 if (err)
4534 goto done;
4536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4537 if (err)
4538 goto done;
4540 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4541 if (err)
4542 goto done;
4544 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4545 0);
4546 if (err)
4547 goto done;
4549 err = got_ref_alloc_symref(new_base_branch_ref,
4550 new_base_branch_ref_name, wt_branch);
4551 if (err)
4552 goto done;
4553 err = got_ref_write(*new_base_branch_ref, repo);
4554 if (err)
4555 goto done;
4557 /* TODO Lock original branch's ref while rebasing? */
4559 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4560 if (err)
4561 goto done;
4563 err = got_ref_write(branch_ref, repo);
4564 if (err)
4565 goto done;
4567 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4568 worktree->base_commit_id);
4569 if (err)
4570 goto done;
4571 err = got_ref_write(*tmp_branch, repo);
4572 if (err)
4573 goto done;
4575 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4576 if (err)
4577 goto done;
4578 done:
4579 free(fileindex_path);
4580 free(tmp_branch_name);
4581 free(new_base_branch_ref_name);
4582 free(branch_ref_name);
4583 if (branch_ref)
4584 got_ref_close(branch_ref);
4585 if (wt_branch)
4586 got_ref_close(wt_branch);
4587 if (err) {
4588 if (*new_base_branch_ref) {
4589 got_ref_close(*new_base_branch_ref);
4590 *new_base_branch_ref = NULL;
4592 if (*tmp_branch) {
4593 got_ref_close(*tmp_branch);
4594 *tmp_branch = NULL;
4596 if (*fileindex) {
4597 got_fileindex_free(*fileindex);
4598 *fileindex = NULL;
4600 lock_worktree(worktree, LOCK_SH);
4602 return err;
4605 const struct got_error *
4606 got_worktree_rebase_continue(struct got_object_id **commit_id,
4607 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4608 struct got_reference **branch, struct got_fileindex **fileindex,
4609 struct got_worktree *worktree, struct got_repository *repo)
4611 const struct got_error *err;
4612 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4613 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4614 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4615 char *fileindex_path = NULL;
4616 int have_staged_files = 0;
4618 *commit_id = NULL;
4619 *new_base_branch = NULL;
4620 *tmp_branch = NULL;
4621 *branch = NULL;
4622 *fileindex = NULL;
4624 err = lock_worktree(worktree, LOCK_EX);
4625 if (err)
4626 return err;
4628 err = open_fileindex(fileindex, &fileindex_path, worktree);
4629 if (err)
4630 goto done;
4632 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4633 &have_staged_files);
4634 if (err && err->code != GOT_ERR_CANCELLED)
4635 goto done;
4636 if (have_staged_files) {
4637 err = got_error(GOT_ERR_STAGED_PATHS);
4638 goto done;
4641 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4642 if (err)
4643 goto done;
4645 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4646 if (err)
4647 goto done;
4649 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4650 if (err)
4651 goto done;
4653 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4654 if (err)
4655 goto done;
4657 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4658 if (err)
4659 goto done;
4661 err = got_ref_open(branch, repo,
4662 got_ref_get_symref_target(branch_ref), 0);
4663 if (err)
4664 goto done;
4666 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4667 if (err)
4668 goto done;
4670 err = got_ref_resolve(commit_id, repo, commit_ref);
4671 if (err)
4672 goto done;
4674 err = got_ref_open(new_base_branch, repo,
4675 new_base_branch_ref_name, 0);
4676 if (err)
4677 goto done;
4679 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4680 if (err)
4681 goto done;
4682 done:
4683 free(commit_ref_name);
4684 free(branch_ref_name);
4685 free(fileindex_path);
4686 if (commit_ref)
4687 got_ref_close(commit_ref);
4688 if (branch_ref)
4689 got_ref_close(branch_ref);
4690 if (err) {
4691 free(*commit_id);
4692 *commit_id = NULL;
4693 if (*tmp_branch) {
4694 got_ref_close(*tmp_branch);
4695 *tmp_branch = NULL;
4697 if (*new_base_branch) {
4698 got_ref_close(*new_base_branch);
4699 *new_base_branch = NULL;
4701 if (*branch) {
4702 got_ref_close(*branch);
4703 *branch = NULL;
4705 if (*fileindex) {
4706 got_fileindex_free(*fileindex);
4707 *fileindex = NULL;
4709 lock_worktree(worktree, LOCK_SH);
4711 return err;
4714 const struct got_error *
4715 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4717 const struct got_error *err;
4718 char *tmp_branch_name = NULL;
4720 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4721 if (err)
4722 return err;
4724 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4725 free(tmp_branch_name);
4726 return NULL;
4729 static const struct got_error *
4730 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4731 char **logmsg, void *arg)
4733 *logmsg = arg;
4734 return NULL;
4737 static const struct got_error *
4738 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4739 const char *path, struct got_object_id *blob_id,
4740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4742 return NULL;
4745 struct collect_merged_paths_arg {
4746 got_worktree_checkout_cb progress_cb;
4747 void *progress_arg;
4748 struct got_pathlist_head *merged_paths;
4751 static const struct got_error *
4752 collect_merged_paths(void *arg, unsigned char status, const char *path)
4754 const struct got_error *err;
4755 struct collect_merged_paths_arg *a = arg;
4756 char *p;
4757 struct got_pathlist_entry *new;
4759 err = (*a->progress_cb)(a->progress_arg, status, path);
4760 if (err)
4761 return err;
4763 if (status != GOT_STATUS_MERGE &&
4764 status != GOT_STATUS_ADD &&
4765 status != GOT_STATUS_DELETE &&
4766 status != GOT_STATUS_CONFLICT)
4767 return NULL;
4769 p = strdup(path);
4770 if (p == NULL)
4771 return got_error_from_errno("strdup");
4773 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4774 if (err || new == NULL)
4775 free(p);
4776 return err;
4779 void
4780 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4782 struct got_pathlist_entry *pe;
4784 TAILQ_FOREACH(pe, merged_paths, entry)
4785 free((char *)pe->path);
4787 got_pathlist_free(merged_paths);
4790 static const struct got_error *
4791 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4792 struct got_repository *repo)
4794 const struct got_error *err;
4795 struct got_reference *commit_ref = NULL;
4797 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4798 if (err) {
4799 if (err->code != GOT_ERR_NOT_REF)
4800 goto done;
4801 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4802 if (err)
4803 goto done;
4804 err = got_ref_write(commit_ref, repo);
4805 if (err)
4806 goto done;
4807 } else {
4808 struct got_object_id *stored_id;
4809 int cmp;
4811 err = got_ref_resolve(&stored_id, repo, commit_ref);
4812 if (err)
4813 goto done;
4814 cmp = got_object_id_cmp(commit_id, stored_id);
4815 free(stored_id);
4816 if (cmp != 0) {
4817 err = got_error(GOT_ERR_REBASE_COMMITID);
4818 goto done;
4821 done:
4822 if (commit_ref)
4823 got_ref_close(commit_ref);
4824 return err;
4827 static const struct got_error *
4828 rebase_merge_files(struct got_pathlist_head *merged_paths,
4829 const char *commit_ref_name, struct got_worktree *worktree,
4830 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4831 struct got_object_id *commit_id, struct got_repository *repo,
4832 got_worktree_checkout_cb progress_cb, void *progress_arg,
4833 got_cancel_cb cancel_cb, void *cancel_arg)
4835 const struct got_error *err;
4836 struct got_reference *commit_ref = NULL;
4837 struct collect_merged_paths_arg cmp_arg;
4838 char *fileindex_path;
4840 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4842 err = get_fileindex_path(&fileindex_path, worktree);
4843 if (err)
4844 return err;
4846 cmp_arg.progress_cb = progress_cb;
4847 cmp_arg.progress_arg = progress_arg;
4848 cmp_arg.merged_paths = merged_paths;
4849 err = merge_files(worktree, fileindex, fileindex_path,
4850 parent_commit_id, commit_id, repo, collect_merged_paths,
4851 &cmp_arg, cancel_cb, cancel_arg);
4852 if (commit_ref)
4853 got_ref_close(commit_ref);
4854 return err;
4857 const struct got_error *
4858 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4859 struct got_worktree *worktree, struct got_fileindex *fileindex,
4860 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4861 struct got_repository *repo,
4862 got_worktree_checkout_cb progress_cb, void *progress_arg,
4863 got_cancel_cb cancel_cb, void *cancel_arg)
4865 const struct got_error *err;
4866 char *commit_ref_name;
4868 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4869 if (err)
4870 return err;
4872 err = store_commit_id(commit_ref_name, commit_id, repo);
4873 if (err)
4874 goto done;
4876 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4877 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4878 progress_arg, cancel_cb, cancel_arg);
4879 done:
4880 free(commit_ref_name);
4881 return err;
4884 const struct got_error *
4885 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4886 struct got_worktree *worktree, struct got_fileindex *fileindex,
4887 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4888 struct got_repository *repo,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_cancel_cb cancel_cb, void *cancel_arg)
4892 const struct got_error *err;
4893 char *commit_ref_name;
4895 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4896 if (err)
4897 return err;
4899 err = store_commit_id(commit_ref_name, commit_id, repo);
4900 if (err)
4901 goto done;
4903 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4904 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4905 progress_arg, cancel_cb, cancel_arg);
4906 done:
4907 free(commit_ref_name);
4908 return err;
4911 static const struct got_error *
4912 rebase_commit(struct got_object_id **new_commit_id,
4913 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4914 struct got_worktree *worktree, struct got_fileindex *fileindex,
4915 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4916 const char *new_logmsg, struct got_repository *repo)
4918 const struct got_error *err, *sync_err;
4919 struct got_pathlist_head commitable_paths;
4920 struct collect_commitables_arg cc_arg;
4921 char *fileindex_path = NULL;
4922 struct got_reference *head_ref = NULL;
4923 struct got_object_id *head_commit_id = NULL;
4924 char *logmsg = NULL;
4926 TAILQ_INIT(&commitable_paths);
4927 *new_commit_id = NULL;
4929 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4931 err = get_fileindex_path(&fileindex_path, worktree);
4932 if (err)
4933 return err;
4935 cc_arg.commitable_paths = &commitable_paths;
4936 cc_arg.worktree = worktree;
4937 cc_arg.repo = repo;
4938 cc_arg.have_staged_files = 0;
4940 * If possible get the status of individual files directly to
4941 * avoid crawling the entire work tree once per rebased commit.
4942 * TODO: Ideally, merged_paths would contain a list of commitables
4943 * we could use so we could skip worktree_status() entirely.
4945 if (merged_paths) {
4946 struct got_pathlist_entry *pe;
4947 if (TAILQ_EMPTY(merged_paths)) {
4948 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4949 goto done;
4951 TAILQ_FOREACH(pe, merged_paths, entry) {
4952 err = worktree_status(worktree, pe->path, fileindex,
4953 repo, collect_commitables, &cc_arg, NULL, NULL);
4954 if (err)
4955 goto done;
4957 } else {
4958 err = worktree_status(worktree, "", fileindex, repo,
4959 collect_commitables, &cc_arg, NULL, NULL);
4960 if (err)
4961 goto done;
4964 if (TAILQ_EMPTY(&commitable_paths)) {
4965 /* No-op change; commit will be elided. */
4966 err = got_ref_delete(commit_ref, repo);
4967 if (err)
4968 goto done;
4969 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4970 goto done;
4973 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4974 if (err)
4975 goto done;
4977 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4978 if (err)
4979 goto done;
4981 if (new_logmsg) {
4982 logmsg = strdup(new_logmsg);
4983 if (logmsg == NULL) {
4984 err = got_error_from_errno("strdup");
4985 goto done;
4987 } else {
4988 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4989 if (err)
4990 goto done;
4993 /* NB: commit_worktree will call free(logmsg) */
4994 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4995 worktree, got_object_commit_get_author(orig_commit),
4996 got_object_commit_get_committer(orig_commit),
4997 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4998 if (err)
4999 goto done;
5001 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5002 if (err)
5003 goto done;
5005 err = got_ref_delete(commit_ref, repo);
5006 if (err)
5007 goto done;
5009 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5010 fileindex, 0);
5011 sync_err = sync_fileindex(fileindex, fileindex_path);
5012 if (sync_err && err == NULL)
5013 err = sync_err;
5014 done:
5015 free(fileindex_path);
5016 free(head_commit_id);
5017 if (head_ref)
5018 got_ref_close(head_ref);
5019 if (err) {
5020 free(*new_commit_id);
5021 *new_commit_id = NULL;
5023 return err;
5026 const struct got_error *
5027 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5028 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5029 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5030 struct got_commit_object *orig_commit,
5031 struct got_object_id *orig_commit_id, struct got_repository *repo)
5033 const struct got_error *err;
5034 char *commit_ref_name;
5035 struct got_reference *commit_ref = NULL;
5036 struct got_object_id *commit_id = NULL;
5038 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5039 if (err)
5040 return err;
5042 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5043 if (err)
5044 goto done;
5045 err = got_ref_resolve(&commit_id, repo, commit_ref);
5046 if (err)
5047 goto done;
5048 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5049 err = got_error(GOT_ERR_REBASE_COMMITID);
5050 goto done;
5053 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5054 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5055 done:
5056 if (commit_ref)
5057 got_ref_close(commit_ref);
5058 free(commit_ref_name);
5059 free(commit_id);
5060 return err;
5063 const struct got_error *
5064 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5065 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5066 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5067 struct got_commit_object *orig_commit,
5068 struct got_object_id *orig_commit_id, const char *new_logmsg,
5069 struct got_repository *repo)
5071 const struct got_error *err;
5072 char *commit_ref_name;
5073 struct got_reference *commit_ref = NULL;
5074 struct got_object_id *commit_id = NULL;
5076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5077 if (err)
5078 return err;
5080 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5081 if (err)
5082 goto done;
5083 err = got_ref_resolve(&commit_id, repo, commit_ref);
5084 if (err)
5085 goto done;
5086 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5087 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5088 goto done;
5091 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5092 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5093 done:
5094 if (commit_ref)
5095 got_ref_close(commit_ref);
5096 free(commit_ref_name);
5097 free(commit_id);
5098 return err;
5101 const struct got_error *
5102 got_worktree_rebase_postpone(struct got_worktree *worktree,
5103 struct got_fileindex *fileindex)
5105 if (fileindex)
5106 got_fileindex_free(fileindex);
5107 return lock_worktree(worktree, LOCK_SH);
5110 static const struct got_error *
5111 delete_ref(const char *name, struct got_repository *repo)
5113 const struct got_error *err;
5114 struct got_reference *ref;
5116 err = got_ref_open(&ref, repo, name, 0);
5117 if (err) {
5118 if (err->code == GOT_ERR_NOT_REF)
5119 return NULL;
5120 return err;
5123 err = got_ref_delete(ref, repo);
5124 got_ref_close(ref);
5125 return err;
5128 static const struct got_error *
5129 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5131 const struct got_error *err;
5132 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5133 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5135 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5136 if (err)
5137 goto done;
5138 err = delete_ref(tmp_branch_name, repo);
5139 if (err)
5140 goto done;
5142 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5143 if (err)
5144 goto done;
5145 err = delete_ref(new_base_branch_ref_name, repo);
5146 if (err)
5147 goto done;
5149 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5150 if (err)
5151 goto done;
5152 err = delete_ref(branch_ref_name, repo);
5153 if (err)
5154 goto done;
5156 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5157 if (err)
5158 goto done;
5159 err = delete_ref(commit_ref_name, repo);
5160 if (err)
5161 goto done;
5163 done:
5164 free(tmp_branch_name);
5165 free(new_base_branch_ref_name);
5166 free(branch_ref_name);
5167 free(commit_ref_name);
5168 return err;
5171 const struct got_error *
5172 got_worktree_rebase_complete(struct got_worktree *worktree,
5173 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5174 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5175 struct got_repository *repo)
5177 const struct got_error *err, *unlockerr;
5178 struct got_object_id *new_head_commit_id = NULL;
5180 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5181 if (err)
5182 return err;
5184 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5185 if (err)
5186 goto done;
5188 err = got_ref_write(rebased_branch, repo);
5189 if (err)
5190 goto done;
5192 err = got_worktree_set_head_ref(worktree, rebased_branch);
5193 if (err)
5194 goto done;
5196 err = delete_rebase_refs(worktree, repo);
5197 done:
5198 if (fileindex)
5199 got_fileindex_free(fileindex);
5200 free(new_head_commit_id);
5201 unlockerr = lock_worktree(worktree, LOCK_SH);
5202 if (unlockerr && err == NULL)
5203 err = unlockerr;
5204 return err;
5207 const struct got_error *
5208 got_worktree_rebase_abort(struct got_worktree *worktree,
5209 struct got_fileindex *fileindex, struct got_repository *repo,
5210 struct got_reference *new_base_branch,
5211 got_worktree_checkout_cb progress_cb, void *progress_arg)
5213 const struct got_error *err, *unlockerr, *sync_err;
5214 struct got_reference *resolved = NULL;
5215 struct got_object_id *commit_id = NULL;
5216 char *fileindex_path = NULL;
5217 struct revert_file_args rfa;
5218 struct got_object_id *tree_id = NULL;
5220 err = lock_worktree(worktree, LOCK_EX);
5221 if (err)
5222 return err;
5224 err = got_ref_open(&resolved, repo,
5225 got_ref_get_symref_target(new_base_branch), 0);
5226 if (err)
5227 goto done;
5229 err = got_worktree_set_head_ref(worktree, resolved);
5230 if (err)
5231 goto done;
5234 * XXX commits to the base branch could have happened while
5235 * we were busy rebasing; should we store the original commit ID
5236 * when rebase begins and read it back here?
5238 err = got_ref_resolve(&commit_id, repo, resolved);
5239 if (err)
5240 goto done;
5242 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5243 if (err)
5244 goto done;
5246 err = got_object_id_by_path(&tree_id, repo,
5247 worktree->base_commit_id, worktree->path_prefix);
5248 if (err)
5249 goto done;
5251 err = delete_rebase_refs(worktree, repo);
5252 if (err)
5253 goto done;
5255 err = get_fileindex_path(&fileindex_path, worktree);
5256 if (err)
5257 goto done;
5259 rfa.worktree = worktree;
5260 rfa.fileindex = fileindex;
5261 rfa.progress_cb = progress_cb;
5262 rfa.progress_arg = progress_arg;
5263 rfa.patch_cb = NULL;
5264 rfa.patch_arg = NULL;
5265 rfa.repo = repo;
5266 err = worktree_status(worktree, "", fileindex, repo,
5267 revert_file, &rfa, NULL, NULL);
5268 if (err)
5269 goto sync;
5271 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5272 repo, progress_cb, progress_arg, NULL, NULL);
5273 sync:
5274 sync_err = sync_fileindex(fileindex, fileindex_path);
5275 if (sync_err && err == NULL)
5276 err = sync_err;
5277 done:
5278 got_ref_close(resolved);
5279 free(tree_id);
5280 free(commit_id);
5281 if (fileindex)
5282 got_fileindex_free(fileindex);
5283 free(fileindex_path);
5285 unlockerr = lock_worktree(worktree, LOCK_SH);
5286 if (unlockerr && err == NULL)
5287 err = unlockerr;
5288 return err;
5291 const struct got_error *
5292 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5293 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5294 struct got_fileindex **fileindex, struct got_worktree *worktree,
5295 struct got_repository *repo)
5297 const struct got_error *err = NULL;
5298 char *tmp_branch_name = NULL;
5299 char *branch_ref_name = NULL;
5300 char *base_commit_ref_name = NULL;
5301 char *fileindex_path = NULL;
5302 struct check_rebase_ok_arg ok_arg;
5303 struct got_reference *wt_branch = NULL;
5304 struct got_reference *base_commit_ref = NULL;
5306 *tmp_branch = NULL;
5307 *branch_ref = NULL;
5308 *base_commit_id = NULL;
5309 *fileindex = NULL;
5311 err = lock_worktree(worktree, LOCK_EX);
5312 if (err)
5313 return err;
5315 err = open_fileindex(fileindex, &fileindex_path, worktree);
5316 if (err)
5317 goto done;
5319 ok_arg.worktree = worktree;
5320 ok_arg.repo = repo;
5321 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5322 &ok_arg);
5323 if (err)
5324 goto done;
5326 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5327 if (err)
5328 goto done;
5330 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5331 if (err)
5332 goto done;
5334 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5335 worktree);
5336 if (err)
5337 goto done;
5339 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5340 0);
5341 if (err)
5342 goto done;
5344 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5345 if (err)
5346 goto done;
5348 err = got_ref_write(*branch_ref, repo);
5349 if (err)
5350 goto done;
5352 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5353 worktree->base_commit_id);
5354 if (err)
5355 goto done;
5356 err = got_ref_write(base_commit_ref, repo);
5357 if (err)
5358 goto done;
5359 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5360 if (*base_commit_id == NULL) {
5361 err = got_error_from_errno("got_object_id_dup");
5362 goto done;
5365 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5366 worktree->base_commit_id);
5367 if (err)
5368 goto done;
5369 err = got_ref_write(*tmp_branch, repo);
5370 if (err)
5371 goto done;
5373 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5374 if (err)
5375 goto done;
5376 done:
5377 free(fileindex_path);
5378 free(tmp_branch_name);
5379 free(branch_ref_name);
5380 free(base_commit_ref_name);
5381 if (wt_branch)
5382 got_ref_close(wt_branch);
5383 if (err) {
5384 if (*branch_ref) {
5385 got_ref_close(*branch_ref);
5386 *branch_ref = NULL;
5388 if (*tmp_branch) {
5389 got_ref_close(*tmp_branch);
5390 *tmp_branch = NULL;
5392 free(*base_commit_id);
5393 if (*fileindex) {
5394 got_fileindex_free(*fileindex);
5395 *fileindex = NULL;
5397 lock_worktree(worktree, LOCK_SH);
5399 return err;
5402 const struct got_error *
5403 got_worktree_histedit_postpone(struct got_worktree *worktree,
5404 struct got_fileindex *fileindex)
5406 if (fileindex)
5407 got_fileindex_free(fileindex);
5408 return lock_worktree(worktree, LOCK_SH);
5411 const struct got_error *
5412 got_worktree_histedit_in_progress(int *in_progress,
5413 struct got_worktree *worktree)
5415 const struct got_error *err;
5416 char *tmp_branch_name = NULL;
5418 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5419 if (err)
5420 return err;
5422 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5423 free(tmp_branch_name);
5424 return NULL;
5427 const struct got_error *
5428 got_worktree_histedit_continue(struct got_object_id **commit_id,
5429 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5430 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5431 struct got_worktree *worktree, struct got_repository *repo)
5433 const struct got_error *err;
5434 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5435 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5436 struct got_reference *commit_ref = NULL;
5437 struct got_reference *base_commit_ref = NULL;
5438 char *fileindex_path = NULL;
5439 int have_staged_files = 0;
5441 *commit_id = NULL;
5442 *tmp_branch = NULL;
5443 *base_commit_id = NULL;
5444 *fileindex = NULL;
5446 err = lock_worktree(worktree, LOCK_EX);
5447 if (err)
5448 return err;
5450 err = open_fileindex(fileindex, &fileindex_path, worktree);
5451 if (err)
5452 goto done;
5454 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5455 &have_staged_files);
5456 if (err && err->code != GOT_ERR_CANCELLED)
5457 goto done;
5458 if (have_staged_files) {
5459 err = got_error(GOT_ERR_STAGED_PATHS);
5460 goto done;
5463 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5464 if (err)
5465 goto done;
5467 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5468 if (err)
5469 goto done;
5471 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5472 if (err)
5473 goto done;
5475 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5476 worktree);
5477 if (err)
5478 goto done;
5480 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5481 if (err)
5482 goto done;
5484 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5485 if (err)
5486 goto done;
5487 err = got_ref_resolve(commit_id, repo, commit_ref);
5488 if (err)
5489 goto done;
5491 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5492 if (err)
5493 goto done;
5494 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5495 if (err)
5496 goto done;
5498 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5499 if (err)
5500 goto done;
5501 done:
5502 free(commit_ref_name);
5503 free(branch_ref_name);
5504 free(fileindex_path);
5505 if (commit_ref)
5506 got_ref_close(commit_ref);
5507 if (base_commit_ref)
5508 got_ref_close(base_commit_ref);
5509 if (err) {
5510 free(*commit_id);
5511 *commit_id = NULL;
5512 free(*base_commit_id);
5513 *base_commit_id = NULL;
5514 if (*tmp_branch) {
5515 got_ref_close(*tmp_branch);
5516 *tmp_branch = NULL;
5518 if (*fileindex) {
5519 got_fileindex_free(*fileindex);
5520 *fileindex = NULL;
5522 lock_worktree(worktree, LOCK_EX);
5524 return err;
5527 static const struct got_error *
5528 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5530 const struct got_error *err;
5531 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5532 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5534 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5535 if (err)
5536 goto done;
5537 err = delete_ref(tmp_branch_name, repo);
5538 if (err)
5539 goto done;
5541 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5542 worktree);
5543 if (err)
5544 goto done;
5545 err = delete_ref(base_commit_ref_name, repo);
5546 if (err)
5547 goto done;
5549 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5550 if (err)
5551 goto done;
5552 err = delete_ref(branch_ref_name, repo);
5553 if (err)
5554 goto done;
5556 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5557 if (err)
5558 goto done;
5559 err = delete_ref(commit_ref_name, repo);
5560 if (err)
5561 goto done;
5562 done:
5563 free(tmp_branch_name);
5564 free(base_commit_ref_name);
5565 free(branch_ref_name);
5566 free(commit_ref_name);
5567 return err;
5570 const struct got_error *
5571 got_worktree_histedit_abort(struct got_worktree *worktree,
5572 struct got_fileindex *fileindex, struct got_repository *repo,
5573 struct got_reference *branch, struct got_object_id *base_commit_id,
5574 got_worktree_checkout_cb progress_cb, void *progress_arg)
5576 const struct got_error *err, *unlockerr, *sync_err;
5577 struct got_reference *resolved = NULL;
5578 char *fileindex_path = NULL;
5579 struct got_object_id *tree_id = NULL;
5580 struct revert_file_args rfa;
5582 err = lock_worktree(worktree, LOCK_EX);
5583 if (err)
5584 return err;
5586 err = got_ref_open(&resolved, repo,
5587 got_ref_get_symref_target(branch), 0);
5588 if (err)
5589 goto done;
5591 err = got_worktree_set_head_ref(worktree, resolved);
5592 if (err)
5593 goto done;
5595 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5596 if (err)
5597 goto done;
5599 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5600 worktree->path_prefix);
5601 if (err)
5602 goto done;
5604 err = delete_histedit_refs(worktree, repo);
5605 if (err)
5606 goto done;
5608 err = get_fileindex_path(&fileindex_path, worktree);
5609 if (err)
5610 goto done;
5612 rfa.worktree = worktree;
5613 rfa.fileindex = fileindex;
5614 rfa.progress_cb = progress_cb;
5615 rfa.progress_arg = progress_arg;
5616 rfa.patch_cb = NULL;
5617 rfa.patch_arg = NULL;
5618 rfa.repo = repo;
5619 err = worktree_status(worktree, "", fileindex, repo,
5620 revert_file, &rfa, NULL, NULL);
5621 if (err)
5622 goto sync;
5624 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5625 repo, progress_cb, progress_arg, NULL, NULL);
5626 sync:
5627 sync_err = sync_fileindex(fileindex, fileindex_path);
5628 if (sync_err && err == NULL)
5629 err = sync_err;
5630 done:
5631 got_ref_close(resolved);
5632 free(tree_id);
5633 free(fileindex_path);
5635 unlockerr = lock_worktree(worktree, LOCK_SH);
5636 if (unlockerr && err == NULL)
5637 err = unlockerr;
5638 return err;
5641 const struct got_error *
5642 got_worktree_histedit_complete(struct got_worktree *worktree,
5643 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5644 struct got_reference *edited_branch, struct got_repository *repo)
5646 const struct got_error *err, *unlockerr;
5647 struct got_object_id *new_head_commit_id = NULL;
5648 struct got_reference *resolved = NULL;
5650 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5651 if (err)
5652 return err;
5654 err = got_ref_open(&resolved, repo,
5655 got_ref_get_symref_target(edited_branch), 0);
5656 if (err)
5657 goto done;
5659 err = got_ref_change_ref(resolved, new_head_commit_id);
5660 if (err)
5661 goto done;
5663 err = got_ref_write(resolved, repo);
5664 if (err)
5665 goto done;
5667 err = got_worktree_set_head_ref(worktree, resolved);
5668 if (err)
5669 goto done;
5671 err = delete_histedit_refs(worktree, repo);
5672 done:
5673 if (fileindex)
5674 got_fileindex_free(fileindex);
5675 free(new_head_commit_id);
5676 unlockerr = lock_worktree(worktree, LOCK_SH);
5677 if (unlockerr && err == NULL)
5678 err = unlockerr;
5679 return err;
5682 const struct got_error *
5683 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5684 struct got_object_id *commit_id, struct got_repository *repo)
5686 const struct got_error *err;
5687 char *commit_ref_name;
5689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5690 if (err)
5691 return err;
5693 err = store_commit_id(commit_ref_name, commit_id, repo);
5694 if (err)
5695 goto done;
5697 err = delete_ref(commit_ref_name, repo);
5698 done:
5699 free(commit_ref_name);
5700 return err;
5703 const struct got_error *
5704 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5705 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5706 struct got_worktree *worktree, const char *refname,
5707 struct got_repository *repo)
5709 const struct got_error *err = NULL;
5710 char *fileindex_path = NULL;
5711 struct check_rebase_ok_arg ok_arg;
5713 *fileindex = NULL;
5714 *branch_ref = NULL;
5715 *base_branch_ref = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5718 if (err)
5719 return err;
5721 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5722 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5723 "cannot integrate a branch into itself; "
5724 "update -b or different branch name required");
5725 goto done;
5728 err = open_fileindex(fileindex, &fileindex_path, worktree);
5729 if (err)
5730 goto done;
5732 /* Preconditions are the same as for rebase. */
5733 ok_arg.worktree = worktree;
5734 ok_arg.repo = repo;
5735 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5736 &ok_arg);
5737 if (err)
5738 goto done;
5740 err = got_ref_open(branch_ref, repo, refname, 1);
5741 if (err)
5742 goto done;
5744 err = got_ref_open(base_branch_ref, repo,
5745 got_worktree_get_head_ref_name(worktree), 1);
5746 done:
5747 if (err) {
5748 if (*branch_ref) {
5749 got_ref_close(*branch_ref);
5750 *branch_ref = NULL;
5752 if (*base_branch_ref) {
5753 got_ref_close(*base_branch_ref);
5754 *base_branch_ref = NULL;
5756 if (*fileindex) {
5757 got_fileindex_free(*fileindex);
5758 *fileindex = NULL;
5760 lock_worktree(worktree, LOCK_SH);
5762 return err;
5765 const struct got_error *
5766 got_worktree_integrate_continue(struct got_worktree *worktree,
5767 struct got_fileindex *fileindex, struct got_repository *repo,
5768 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5769 got_worktree_checkout_cb progress_cb, void *progress_arg,
5770 got_cancel_cb cancel_cb, void *cancel_arg)
5772 const struct got_error *err = NULL, *sync_err, *unlockerr;
5773 char *fileindex_path = NULL;
5774 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5776 err = get_fileindex_path(&fileindex_path, worktree);
5777 if (err)
5778 goto done;
5780 err = got_ref_resolve(&commit_id, repo, branch_ref);
5781 if (err)
5782 goto done;
5784 err = got_object_id_by_path(&tree_id, repo, commit_id,
5785 worktree->path_prefix);
5786 if (err)
5787 goto done;
5789 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5790 if (err)
5791 goto done;
5793 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5794 progress_cb, progress_arg, cancel_cb, cancel_arg);
5795 if (err)
5796 goto sync;
5798 err = got_ref_change_ref(base_branch_ref, commit_id);
5799 if (err)
5800 goto sync;
5802 err = got_ref_write(base_branch_ref, repo);
5803 sync:
5804 sync_err = sync_fileindex(fileindex, fileindex_path);
5805 if (sync_err && err == NULL)
5806 err = sync_err;
5808 done:
5809 unlockerr = got_ref_unlock(branch_ref);
5810 if (unlockerr && err == NULL)
5811 err = unlockerr;
5812 got_ref_close(branch_ref);
5814 unlockerr = got_ref_unlock(base_branch_ref);
5815 if (unlockerr && err == NULL)
5816 err = unlockerr;
5817 got_ref_close(base_branch_ref);
5819 got_fileindex_free(fileindex);
5820 free(fileindex_path);
5821 free(tree_id);
5823 unlockerr = lock_worktree(worktree, LOCK_SH);
5824 if (unlockerr && err == NULL)
5825 err = unlockerr;
5826 return err;
5829 const struct got_error *
5830 got_worktree_integrate_abort(struct got_worktree *worktree,
5831 struct got_fileindex *fileindex, struct got_repository *repo,
5832 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5834 const struct got_error *err = NULL, *unlockerr = NULL;
5836 got_fileindex_free(fileindex);
5838 err = lock_worktree(worktree, LOCK_SH);
5840 unlockerr = got_ref_unlock(branch_ref);
5841 if (unlockerr && err == NULL)
5842 err = unlockerr;
5843 got_ref_close(branch_ref);
5845 unlockerr = got_ref_unlock(base_branch_ref);
5846 if (unlockerr && err == NULL)
5847 err = unlockerr;
5848 got_ref_close(base_branch_ref);
5850 return err;
5853 struct check_stage_ok_arg {
5854 struct got_object_id *head_commit_id;
5855 struct got_worktree *worktree;
5856 struct got_fileindex *fileindex;
5857 struct got_repository *repo;
5858 int have_changes;
5861 const struct got_error *
5862 check_stage_ok(void *arg, unsigned char status,
5863 unsigned char staged_status, const char *relpath,
5864 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5865 struct got_object_id *commit_id)
5867 struct check_stage_ok_arg *a = arg;
5868 const struct got_error *err = NULL;
5869 struct got_fileindex_entry *ie;
5870 struct got_object_id base_commit_id;
5871 struct got_object_id *base_commit_idp = NULL;
5872 char *in_repo_path = NULL, *p;
5874 if (status == GOT_STATUS_UNVERSIONED)
5875 return NULL;
5876 if (status == GOT_STATUS_NONEXISTENT)
5877 return got_error_set_errno(ENOENT, relpath);
5879 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5880 if (ie == NULL)
5881 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5883 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5884 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5885 relpath) == -1)
5886 return got_error_from_errno("asprintf");
5888 if (got_fileindex_entry_has_commit(ie)) {
5889 memcpy(base_commit_id.sha1, ie->commit_sha1,
5890 SHA1_DIGEST_LENGTH);
5891 base_commit_idp = &base_commit_id;
5894 if (status == GOT_STATUS_NO_CHANGE) {
5895 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5896 goto done;
5897 } else if (status == GOT_STATUS_CONFLICT) {
5898 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5899 goto done;
5900 } else if (status != GOT_STATUS_ADD &&
5901 status != GOT_STATUS_MODIFY &&
5902 status != GOT_STATUS_DELETE) {
5903 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5904 goto done;
5907 a->have_changes = 1;
5909 p = in_repo_path;
5910 while (p[0] == '/')
5911 p++;
5912 err = check_out_of_date(p, status, staged_status,
5913 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5914 GOT_ERR_STAGE_OUT_OF_DATE);
5915 done:
5916 free(in_repo_path);
5917 return err;
5920 struct stage_path_arg {
5921 struct got_worktree *worktree;
5922 struct got_fileindex *fileindex;
5923 struct got_repository *repo;
5924 got_worktree_status_cb status_cb;
5925 void *status_arg;
5926 got_worktree_patch_cb patch_cb;
5927 void *patch_arg;
5930 static const struct got_error *
5931 stage_path(void *arg, unsigned char status,
5932 unsigned char staged_status, const char *relpath,
5933 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5934 struct got_object_id *commit_id)
5936 struct stage_path_arg *a = arg;
5937 const struct got_error *err = NULL;
5938 struct got_fileindex_entry *ie;
5939 char *ondisk_path = NULL, *path_content = NULL;
5940 uint32_t stage;
5941 struct got_object_id *new_staged_blob_id = NULL;
5943 if (status == GOT_STATUS_UNVERSIONED)
5944 return NULL;
5946 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5947 if (ie == NULL)
5948 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5951 relpath)== -1)
5952 return got_error_from_errno("asprintf");
5954 switch (status) {
5955 case GOT_STATUS_ADD:
5956 case GOT_STATUS_MODIFY:
5957 if (a->patch_cb) {
5958 if (status == GOT_STATUS_ADD) {
5959 int choice = GOT_PATCH_CHOICE_NONE;
5960 err = (*a->patch_cb)(&choice, a->patch_arg,
5961 status, ie->path, NULL, 1, 1);
5962 if (err)
5963 break;
5964 if (choice != GOT_PATCH_CHOICE_YES)
5965 break;
5966 } else {
5967 err = create_patched_content(&path_content, 0,
5968 staged_blob_id ? staged_blob_id : blob_id,
5969 ondisk_path, ie->path, a->repo,
5970 a->patch_cb, a->patch_arg);
5971 if (err || path_content == NULL)
5972 break;
5975 err = got_object_blob_create(&new_staged_blob_id,
5976 path_content ? path_content : ondisk_path, a->repo);
5977 if (err)
5978 break;
5979 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5980 SHA1_DIGEST_LENGTH);
5981 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5982 stage = GOT_FILEIDX_STAGE_ADD;
5983 else
5984 stage = GOT_FILEIDX_STAGE_MODIFY;
5985 got_fileindex_entry_stage_set(ie, stage);
5986 if (a->status_cb == NULL)
5987 break;
5988 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5989 get_staged_status(ie), relpath, blob_id,
5990 new_staged_blob_id, NULL);
5991 break;
5992 case GOT_STATUS_DELETE:
5993 if (staged_status == GOT_STATUS_DELETE)
5994 break;
5995 if (a->patch_cb) {
5996 int choice = GOT_PATCH_CHOICE_NONE;
5997 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5998 ie->path, NULL, 1, 1);
5999 if (err)
6000 break;
6001 if (choice == GOT_PATCH_CHOICE_NO)
6002 break;
6003 if (choice != GOT_PATCH_CHOICE_YES) {
6004 err = got_error(GOT_ERR_PATCH_CHOICE);
6005 break;
6008 stage = GOT_FILEIDX_STAGE_DELETE;
6009 got_fileindex_entry_stage_set(ie, stage);
6010 if (a->status_cb == NULL)
6011 break;
6012 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6013 get_staged_status(ie), relpath, NULL, NULL, NULL);
6014 break;
6015 case GOT_STATUS_NO_CHANGE:
6016 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
6017 break;
6018 case GOT_STATUS_CONFLICT:
6019 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6020 break;
6021 case GOT_STATUS_NONEXISTENT:
6022 err = got_error_set_errno(ENOENT, relpath);
6023 break;
6024 default:
6025 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6026 break;
6029 if (path_content && unlink(path_content) == -1 && err == NULL)
6030 err = got_error_from_errno2("unlink", path_content);
6031 free(path_content);
6032 free(ondisk_path);
6033 free(new_staged_blob_id);
6034 return err;
6037 const struct got_error *
6038 got_worktree_stage(struct got_worktree *worktree,
6039 struct got_pathlist_head *paths,
6040 got_worktree_status_cb status_cb, void *status_arg,
6041 got_worktree_patch_cb patch_cb, void *patch_arg,
6042 struct got_repository *repo)
6044 const struct got_error *err = NULL, *sync_err, *unlockerr;
6045 struct got_pathlist_entry *pe;
6046 struct got_fileindex *fileindex = NULL;
6047 char *fileindex_path = NULL;
6048 struct got_reference *head_ref = NULL;
6049 struct got_object_id *head_commit_id = NULL;
6050 struct check_stage_ok_arg oka;
6051 struct stage_path_arg spa;
6053 err = lock_worktree(worktree, LOCK_EX);
6054 if (err)
6055 return err;
6057 err = got_ref_open(&head_ref, repo,
6058 got_worktree_get_head_ref_name(worktree), 0);
6059 if (err)
6060 goto done;
6061 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6062 if (err)
6063 goto done;
6064 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6065 if (err)
6066 goto done;
6068 /* Check pre-conditions before staging anything. */
6069 oka.head_commit_id = head_commit_id;
6070 oka.worktree = worktree;
6071 oka.fileindex = fileindex;
6072 oka.repo = repo;
6073 oka.have_changes = 0;
6074 TAILQ_FOREACH(pe, paths, entry) {
6075 err = worktree_status(worktree, pe->path, fileindex, repo,
6076 check_stage_ok, &oka, NULL, NULL);
6077 if (err)
6078 goto done;
6080 if (!oka.have_changes) {
6081 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6082 goto done;
6085 spa.worktree = worktree;
6086 spa.fileindex = fileindex;
6087 spa.repo = repo;
6088 spa.patch_cb = patch_cb;
6089 spa.patch_arg = patch_arg;
6090 spa.status_cb = status_cb;
6091 spa.status_arg = status_arg;
6092 TAILQ_FOREACH(pe, paths, entry) {
6093 err = worktree_status(worktree, pe->path, fileindex, repo,
6094 stage_path, &spa, NULL, NULL);
6095 if (err)
6096 goto done;
6099 sync_err = sync_fileindex(fileindex, fileindex_path);
6100 if (sync_err && err == NULL)
6101 err = sync_err;
6102 done:
6103 if (head_ref)
6104 got_ref_close(head_ref);
6105 free(head_commit_id);
6106 free(fileindex_path);
6107 if (fileindex)
6108 got_fileindex_free(fileindex);
6109 unlockerr = lock_worktree(worktree, LOCK_SH);
6110 if (unlockerr && err == NULL)
6111 err = unlockerr;
6112 return err;
6115 struct unstage_path_arg {
6116 struct got_worktree *worktree;
6117 struct got_fileindex *fileindex;
6118 struct got_repository *repo;
6119 got_worktree_checkout_cb progress_cb;
6120 void *progress_arg;
6121 got_worktree_patch_cb patch_cb;
6122 void *patch_arg;
6125 static const struct got_error *
6126 create_unstaged_content(char **path_unstaged_content,
6127 char **path_new_staged_content, struct got_object_id *blob_id,
6128 struct got_object_id *staged_blob_id, const char *relpath,
6129 struct got_repository *repo,
6130 got_worktree_patch_cb patch_cb, void *patch_arg)
6132 const struct got_error *err;
6133 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6134 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6135 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6136 struct stat sb1, sb2;
6137 struct got_diff_changes *changes = NULL;
6138 struct got_diff_state *ds = NULL;
6139 struct got_diff_args *args = NULL;
6140 struct got_diff_change *change;
6141 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6142 int have_content = 0, have_rejected_content = 0;
6144 *path_unstaged_content = NULL;
6145 *path_new_staged_content = NULL;
6147 err = got_object_id_str(&label1, blob_id);
6148 if (err)
6149 return err;
6150 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6151 if (err)
6152 goto done;
6154 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6155 if (err)
6156 goto done;
6158 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6159 if (err)
6160 goto done;
6162 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6163 if (err)
6164 goto done;
6166 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6167 if (err)
6168 goto done;
6170 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6171 if (err)
6172 goto done;
6174 if (stat(path1, &sb1) == -1) {
6175 err = got_error_from_errno2("stat", path1);
6176 goto done;
6179 if (stat(path2, &sb2) == -1) {
6180 err = got_error_from_errno2("stat", path2);
6181 goto done;
6184 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6185 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6186 if (err)
6187 goto done;
6189 err = got_opentemp_named(path_unstaged_content, &outfile,
6190 "got-unstaged-content");
6191 if (err)
6192 goto done;
6193 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6194 "got-new-staged-content");
6195 if (err)
6196 goto done;
6198 if (fseek(f1, 0L, SEEK_SET) == -1) {
6199 err = got_ferror(f1, GOT_ERR_IO);
6200 goto done;
6202 if (fseek(f2, 0L, SEEK_SET) == -1) {
6203 err = got_ferror(f2, GOT_ERR_IO);
6204 goto done;
6206 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6207 int choice;
6208 err = apply_or_reject_change(&choice, change, ++n,
6209 changes->nchanges, ds, args, diff_flags, relpath,
6210 f1, f2, &line_cur1, &line_cur2,
6211 outfile, rejectfile, patch_cb, patch_arg);
6212 if (err)
6213 goto done;
6214 if (choice == GOT_PATCH_CHOICE_YES)
6215 have_content = 1;
6216 else
6217 have_rejected_content = 1;
6218 if (choice == GOT_PATCH_CHOICE_QUIT)
6219 break;
6221 if (have_content || have_rejected_content)
6222 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6223 outfile, rejectfile);
6224 done:
6225 free(label1);
6226 if (blob)
6227 got_object_blob_close(blob);
6228 if (staged_blob)
6229 got_object_blob_close(staged_blob);
6230 if (f1 && fclose(f1) == EOF && err == NULL)
6231 err = got_error_from_errno2("fclose", path1);
6232 if (f2 && fclose(f2) == EOF && err == NULL)
6233 err = got_error_from_errno2("fclose", path2);
6234 if (outfile && fclose(outfile) == EOF && err == NULL)
6235 err = got_error_from_errno2("fclose", *path_unstaged_content);
6236 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6237 err = got_error_from_errno2("fclose", *path_new_staged_content);
6238 if (path1 && unlink(path1) == -1 && err == NULL)
6239 err = got_error_from_errno2("unlink", path1);
6240 if (path2 && unlink(path2) == -1 && err == NULL)
6241 err = got_error_from_errno2("unlink", path2);
6242 if (err || !have_content) {
6243 if (*path_unstaged_content &&
6244 unlink(*path_unstaged_content) == -1 && err == NULL)
6245 err = got_error_from_errno2("unlink",
6246 *path_unstaged_content);
6247 free(*path_unstaged_content);
6248 *path_unstaged_content = NULL;
6250 if (err || !have_rejected_content) {
6251 if (*path_new_staged_content &&
6252 unlink(*path_new_staged_content) == -1 && err == NULL)
6253 err = got_error_from_errno2("unlink",
6254 *path_new_staged_content);
6255 free(*path_new_staged_content);
6256 *path_new_staged_content = NULL;
6258 free(args);
6259 if (ds) {
6260 got_diff_state_free(ds);
6261 free(ds);
6263 if (changes)
6264 got_diff_free_changes(changes);
6265 free(path1);
6266 free(path2);
6267 return err;
6270 static const struct got_error *
6271 unstage_path(void *arg, unsigned char status,
6272 unsigned char staged_status, const char *relpath,
6273 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6274 struct got_object_id *commit_id)
6276 const struct got_error *err = NULL;
6277 struct unstage_path_arg *a = arg;
6278 struct got_fileindex_entry *ie;
6279 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6280 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6281 char *path_new_staged_content = NULL;
6282 char *id_str = NULL, *label_orig = NULL;
6283 int local_changes_subsumed;
6284 struct stat sb;
6286 if (staged_status != GOT_STATUS_ADD &&
6287 staged_status != GOT_STATUS_MODIFY &&
6288 staged_status != GOT_STATUS_DELETE)
6289 return NULL;
6291 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6292 if (ie == NULL)
6293 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6295 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6296 == -1)
6297 return got_error_from_errno("asprintf");
6299 err = got_object_id_str(&id_str,
6300 commit_id ? commit_id : a->worktree->base_commit_id);
6301 if (err)
6302 goto done;
6303 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6304 id_str) == -1) {
6305 err = got_error_from_errno("asprintf");
6306 goto done;
6309 switch (staged_status) {
6310 case GOT_STATUS_MODIFY:
6311 err = got_object_open_as_blob(&blob_base, a->repo,
6312 blob_id, 8192);
6313 if (err)
6314 break;
6315 /* fall through */
6316 case GOT_STATUS_ADD:
6317 if (a->patch_cb) {
6318 if (staged_status == GOT_STATUS_ADD) {
6319 int choice = GOT_PATCH_CHOICE_NONE;
6320 err = (*a->patch_cb)(&choice, a->patch_arg,
6321 staged_status, ie->path, NULL, 1, 1);
6322 if (err)
6323 break;
6324 if (choice != GOT_PATCH_CHOICE_YES)
6325 break;
6326 } else {
6327 err = create_unstaged_content(
6328 &path_unstaged_content,
6329 &path_new_staged_content, blob_id,
6330 staged_blob_id, ie->path, a->repo,
6331 a->patch_cb, a->patch_arg);
6332 if (err || path_unstaged_content == NULL)
6333 break;
6334 if (path_new_staged_content) {
6335 err = got_object_blob_create(
6336 &staged_blob_id,
6337 path_new_staged_content,
6338 a->repo);
6339 if (err)
6340 break;
6341 memcpy(ie->staged_blob_sha1,
6342 staged_blob_id->sha1,
6343 SHA1_DIGEST_LENGTH);
6345 err = merge_file(&local_changes_subsumed,
6346 a->worktree, blob_base, ondisk_path,
6347 relpath, got_fileindex_perms_to_st(ie),
6348 path_unstaged_content, label_orig,
6349 "unstaged", a->repo, a->progress_cb,
6350 a->progress_arg);
6351 if (err == NULL &&
6352 path_new_staged_content == NULL)
6353 got_fileindex_entry_stage_set(ie,
6354 GOT_FILEIDX_STAGE_NONE);
6355 break; /* Done with this file. */
6358 err = got_object_open_as_blob(&blob_staged, a->repo,
6359 staged_blob_id, 8192);
6360 if (err)
6361 break;
6362 err = merge_blob(&local_changes_subsumed, a->worktree,
6363 blob_base, ondisk_path, relpath,
6364 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6365 commit_id ? commit_id : a->worktree->base_commit_id,
6366 a->repo, a->progress_cb, a->progress_arg);
6367 if (err == NULL)
6368 got_fileindex_entry_stage_set(ie,
6369 GOT_FILEIDX_STAGE_NONE);
6370 break;
6371 case GOT_STATUS_DELETE:
6372 if (a->patch_cb) {
6373 int choice = GOT_PATCH_CHOICE_NONE;
6374 err = (*a->patch_cb)(&choice, a->patch_arg,
6375 staged_status, ie->path, NULL, 1, 1);
6376 if (err)
6377 break;
6378 if (choice == GOT_PATCH_CHOICE_NO)
6379 break;
6380 if (choice != GOT_PATCH_CHOICE_YES) {
6381 err = got_error(GOT_ERR_PATCH_CHOICE);
6382 break;
6385 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6386 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6387 if (err)
6388 break;
6389 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6390 break;
6392 done:
6393 free(ondisk_path);
6394 if (path_unstaged_content &&
6395 unlink(path_unstaged_content) == -1 && err == NULL)
6396 err = got_error_from_errno2("unlink", path_unstaged_content);
6397 if (path_new_staged_content &&
6398 unlink(path_new_staged_content) == -1 && err == NULL)
6399 err = got_error_from_errno2("unlink", path_new_staged_content);
6400 free(path_unstaged_content);
6401 free(path_new_staged_content);
6402 if (blob_base)
6403 got_object_blob_close(blob_base);
6404 if (blob_staged)
6405 got_object_blob_close(blob_staged);
6406 free(id_str);
6407 free(label_orig);
6408 return err;
6411 const struct got_error *
6412 got_worktree_unstage(struct got_worktree *worktree,
6413 struct got_pathlist_head *paths,
6414 got_worktree_checkout_cb progress_cb, void *progress_arg,
6415 got_worktree_patch_cb patch_cb, void *patch_arg,
6416 struct got_repository *repo)
6418 const struct got_error *err = NULL, *sync_err, *unlockerr;
6419 struct got_pathlist_entry *pe;
6420 struct got_fileindex *fileindex = NULL;
6421 char *fileindex_path = NULL;
6422 struct unstage_path_arg upa;
6424 err = lock_worktree(worktree, LOCK_EX);
6425 if (err)
6426 return err;
6428 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6429 if (err)
6430 goto done;
6432 upa.worktree = worktree;
6433 upa.fileindex = fileindex;
6434 upa.repo = repo;
6435 upa.progress_cb = progress_cb;
6436 upa.progress_arg = progress_arg;
6437 upa.patch_cb = patch_cb;
6438 upa.patch_arg = patch_arg;
6439 TAILQ_FOREACH(pe, paths, entry) {
6440 err = worktree_status(worktree, pe->path, fileindex, repo,
6441 unstage_path, &upa, NULL, NULL);
6442 if (err)
6443 goto done;
6446 sync_err = sync_fileindex(fileindex, fileindex_path);
6447 if (sync_err && err == NULL)
6448 err = sync_err;
6449 done:
6450 free(fileindex_path);
6451 if (fileindex)
6452 got_fileindex_free(fileindex);
6453 unlockerr = lock_worktree(worktree, LOCK_SH);
6454 if (unlockerr && err == NULL)
6455 err = unlockerr;
6456 return err;