Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", 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 (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", 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 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 /*
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
837 */
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
854 if (parent == NULL)
855 return got_error_from_errno2("dirname", ondisk_path);
857 free(base_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
860 base_path = NULL;
861 goto done;
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 if (err)
866 goto done;
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 blob_deriv);
869 if (err)
870 goto done;
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (err)
874 goto done;
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
878 goto done;
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
884 done:
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
887 free(base_path);
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
892 free(id_str);
893 free(label_deriv);
894 return err;
897 static const struct got_error *
898 update_blob_fileindex_entry(struct got_worktree *worktree,
899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 int update_timestamps)
903 const struct got_error *err = NULL;
905 if (ie == NULL)
906 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
907 if (ie)
908 err = got_fileindex_entry_update(ie, ondisk_path,
909 blob->id.sha1, worktree->base_commit_id->sha1,
910 update_timestamps);
911 else {
912 struct got_fileindex_entry *new_ie;
913 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
914 path, blob->id.sha1, worktree->base_commit_id->sha1);
915 if (!err)
916 err = got_fileindex_entry_add(fileindex, new_ie);
918 return err;
921 static mode_t
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
926 if (executable) {
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
929 xbits |= S_IXGRP;
930 if (st_mode & S_IROTH)
931 xbits |= S_IXOTH;
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 static const struct got_error *
939 install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 const char *path, mode_t te_mode, mode_t st_mode,
941 struct got_blob_object *blob, int restoring_missing_file,
942 int reverting_versioned_file, struct got_repository *repo,
943 got_worktree_checkout_cb progress_cb, void *progress_arg)
945 const struct got_error *err = NULL;
946 int fd = -1;
947 size_t len, hdrlen;
948 int update = 0;
949 char *tmppath = NULL;
951 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 GOT_DEFAULT_FILE_MODE);
953 if (fd == -1) {
954 if (errno == ENOENT) {
955 char *parent = dirname(path);
956 if (parent == NULL)
957 return got_error_from_errno2("dirname", path);
958 err = add_dir_on_disk(worktree, parent);
959 if (err)
960 return err;
961 fd = open(ondisk_path,
962 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 GOT_DEFAULT_FILE_MODE);
964 if (fd == -1)
965 return got_error_from_errno2("open",
966 ondisk_path);
967 } else if (errno == EEXIST) {
968 if (!S_ISREG(st_mode)) {
969 /* TODO file is obstructed; do something */
970 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
971 goto done;
972 } else {
973 err = got_opentemp_named_fd(&tmppath, &fd,
974 ondisk_path);
975 if (err)
976 goto done;
977 update = 1;
979 } else
980 return got_error_from_errno2("open", ondisk_path);
983 if (restoring_missing_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
985 else if (reverting_versioned_file)
986 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
987 else
988 err = (*progress_cb)(progress_arg,
989 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
990 if (err)
991 goto done;
993 hdrlen = got_object_blob_get_hdrlen(blob);
994 do {
995 const uint8_t *buf = got_object_blob_get_read_buf(blob);
996 err = got_object_blob_read_block(&len, blob);
997 if (err)
998 break;
999 if (len > 0) {
1000 /* Skip blob object header first time around. */
1001 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1002 if (outlen == -1) {
1003 err = got_error_from_errno("write");
1004 goto done;
1005 } else if (outlen != len - hdrlen) {
1006 err = got_error(GOT_ERR_IO);
1007 goto done;
1009 hdrlen = 0;
1011 } while (len != 0);
1013 if (fsync(fd) != 0) {
1014 err = got_error_from_errno("fsync");
1015 goto done;
1018 if (update) {
1019 if (rename(tmppath, ondisk_path) != 0) {
1020 err = got_error_from_errno3("rename", tmppath,
1021 ondisk_path);
1022 unlink(tmppath);
1023 goto done;
1027 if (chmod(ondisk_path,
1028 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1029 err = got_error_from_errno2("chmod", ondisk_path);
1030 goto done;
1033 done:
1034 if (fd != -1 && close(fd) != 0 && err == NULL)
1035 err = got_error_from_errno("close");
1036 free(tmppath);
1037 return err;
1040 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1041 static const struct got_error *
1042 get_modified_file_content_status(unsigned char *status, FILE *f)
1044 const struct got_error *err = NULL;
1045 const char *markers[3] = {
1046 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1047 GOT_DIFF_CONFLICT_MARKER_SEP,
1048 GOT_DIFF_CONFLICT_MARKER_END
1050 int i = 0;
1051 char *line;
1052 size_t len;
1053 const char delim[3] = {'\0', '\0', '\0'};
1055 while (*status == GOT_STATUS_MODIFY) {
1056 line = fparseln(f, &len, NULL, delim, 0);
1057 if (line == NULL) {
1058 if (feof(f))
1059 break;
1060 err = got_ferror(f, GOT_ERR_IO);
1061 break;
1064 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1065 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1066 == 0)
1067 *status = GOT_STATUS_CONFLICT;
1068 else
1069 i++;
1073 return err;
1076 static int
1077 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1079 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1080 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1083 static int
1084 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1086 return !(ie->ctime_sec == sb->st_ctime &&
1087 ie->ctime_nsec == sb->st_ctimensec &&
1088 ie->mtime_sec == sb->st_mtime &&
1089 ie->mtime_nsec == sb->st_mtimensec &&
1090 ie->size == (sb->st_size & 0xffffffff) &&
1091 !xbit_differs(ie, sb->st_mode));
1094 static unsigned char
1095 get_staged_status(struct got_fileindex_entry *ie)
1097 switch (got_fileindex_entry_stage_get(ie)) {
1098 case GOT_FILEIDX_STAGE_ADD:
1099 return GOT_STATUS_ADD;
1100 case GOT_FILEIDX_STAGE_DELETE:
1101 return GOT_STATUS_DELETE;
1102 case GOT_FILEIDX_STAGE_MODIFY:
1103 return GOT_STATUS_MODIFY;
1104 default:
1105 return GOT_STATUS_NO_CHANGE;
1109 static const struct got_error *
1110 get_file_status(unsigned char *status, struct stat *sb,
1111 struct got_fileindex_entry *ie, const char *abspath,
1112 int dirfd, const char *de_name, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_object_id id;
1116 size_t hdrlen;
1117 int fd = -1;
1118 FILE *f = NULL;
1119 uint8_t fbuf[8192];
1120 struct got_blob_object *blob = NULL;
1121 size_t flen, blen;
1122 unsigned char staged_status = get_staged_status(ie);
1124 *status = GOT_STATUS_NO_CHANGE;
1127 * Whenever the caller provides a directory descriptor and a
1128 * directory entry name for the file, use them! This prevents
1129 * race conditions if filesystem paths change beneath our feet.
1131 if (dirfd != -1) {
1132 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1133 if (errno == ENOENT) {
1134 if (got_fileindex_entry_has_file_on_disk(ie))
1135 *status = GOT_STATUS_MISSING;
1136 else
1137 *status = GOT_STATUS_DELETE;
1138 goto done;
1140 err = got_error_from_errno2("fstatat", abspath);
1141 goto done;
1143 } else {
1144 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1145 if (fd == -1 && errno != ENOENT)
1146 return got_error_from_errno2("open", abspath);
1147 if (fd == -1 || fstat(fd, sb) == -1) {
1148 if (errno == ENOENT) {
1149 if (got_fileindex_entry_has_file_on_disk(ie))
1150 *status = GOT_STATUS_MISSING;
1151 else
1152 *status = GOT_STATUS_DELETE;
1153 goto done;
1155 err = got_error_from_errno2("fstat", abspath);
1156 goto done;
1160 if (!S_ISREG(sb->st_mode)) {
1161 *status = GOT_STATUS_OBSTRUCTED;
1162 goto done;
1165 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1166 *status = GOT_STATUS_DELETE;
1167 goto done;
1168 } else if (!got_fileindex_entry_has_blob(ie) &&
1169 staged_status != GOT_STATUS_ADD) {
1170 *status = GOT_STATUS_ADD;
1171 goto done;
1174 if (!stat_info_differs(ie, sb))
1175 goto done;
1177 if (staged_status == GOT_STATUS_MODIFY ||
1178 staged_status == GOT_STATUS_ADD)
1179 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1180 else
1181 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1183 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1184 if (err)
1185 goto done;
1187 if (dirfd != -1) {
1188 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1189 if (fd == -1) {
1190 err = got_error_from_errno2("openat", abspath);
1191 goto done;
1195 f = fdopen(fd, "r");
1196 if (f == NULL) {
1197 err = got_error_from_errno2("fdopen", abspath);
1198 goto done;
1200 fd = -1;
1201 hdrlen = got_object_blob_get_hdrlen(blob);
1202 for (;;) {
1203 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1204 err = got_object_blob_read_block(&blen, blob);
1205 if (err)
1206 goto done;
1207 /* Skip length of blob object header first time around. */
1208 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1209 if (flen == 0 && ferror(f)) {
1210 err = got_error_from_errno("fread");
1211 goto done;
1213 if (blen == 0) {
1214 if (flen != 0)
1215 *status = GOT_STATUS_MODIFY;
1216 break;
1217 } else if (flen == 0) {
1218 if (blen != 0)
1219 *status = GOT_STATUS_MODIFY;
1220 break;
1221 } else if (blen - hdrlen == flen) {
1222 /* Skip blob object header first time around. */
1223 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1224 *status = GOT_STATUS_MODIFY;
1225 break;
1227 } else {
1228 *status = GOT_STATUS_MODIFY;
1229 break;
1231 hdrlen = 0;
1234 if (*status == GOT_STATUS_MODIFY) {
1235 rewind(f);
1236 err = get_modified_file_content_status(status, f);
1237 } else if (xbit_differs(ie, sb->st_mode))
1238 *status = GOT_STATUS_MODE_CHANGE;
1239 done:
1240 if (blob)
1241 got_object_blob_close(blob);
1242 if (f != NULL && fclose(f) == EOF && err == NULL)
1243 err = got_error_from_errno2("fclose", abspath);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", abspath);
1246 return err;
1250 * Update timestamps in the file index if a file is unmodified and
1251 * we had to run a full content comparison to find out.
1253 static const struct got_error *
1254 sync_timestamps(char *ondisk_path, unsigned char status,
1255 struct got_fileindex_entry *ie, struct stat *sb)
1257 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1258 return got_fileindex_entry_update(ie, ondisk_path,
1259 ie->blob_sha1, ie->commit_sha1, 1);
1261 return NULL;
1264 static const struct got_error *
1265 update_blob(struct got_worktree *worktree,
1266 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1267 struct got_tree_entry *te, const char *path,
1268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1269 void *progress_arg)
1271 const struct got_error *err = NULL;
1272 struct got_blob_object *blob = NULL;
1273 char *ondisk_path;
1274 unsigned char status = GOT_STATUS_NO_CHANGE;
1275 struct stat sb;
1277 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1278 return got_error_from_errno("asprintf");
1280 if (ie) {
1281 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1282 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1283 goto done;
1285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1286 repo);
1287 if (err)
1288 goto done;
1289 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1290 sb.st_mode = got_fileindex_perms_to_st(ie);
1291 } else
1292 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1294 if (status == GOT_STATUS_OBSTRUCTED) {
1295 err = (*progress_cb)(progress_arg, status, path);
1296 goto done;
1299 if (ie && status != GOT_STATUS_MISSING &&
1300 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1301 if (got_fileindex_entry_has_commit(ie) &&
1302 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1303 SHA1_DIGEST_LENGTH) == 0) {
1304 err = sync_timestamps(ondisk_path, status, ie, &sb);
1305 if (err)
1306 goto done;
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1308 path);
1309 goto done;
1311 if (got_fileindex_entry_has_blob(ie) &&
1312 memcmp(ie->blob_sha1, te->id.sha1,
1313 SHA1_DIGEST_LENGTH) == 0) {
1314 err = sync_timestamps(ondisk_path, status, ie, &sb);
1315 goto done;
1319 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1320 if (err)
1321 goto done;
1323 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1324 int update_timestamps;
1325 struct got_blob_object *blob2 = NULL;
1326 char *label_orig = NULL;
1327 if (got_fileindex_entry_has_blob(ie)) {
1328 struct got_object_id id2;
1329 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1330 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1331 if (err)
1332 goto done;
1334 if (got_fileindex_entry_has_commit(ie)) {
1335 char id_str[SHA1_DIGEST_STRING_LENGTH];
1336 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1337 sizeof(id_str)) == NULL) {
1338 err = got_error_path(id_str,
1339 GOT_ERR_BAD_OBJ_ID_STR);
1340 goto done;
1342 if (asprintf(&label_orig, "%s: commit %s",
1343 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1344 err = got_error_from_errno("asprintf");
1345 goto done;
1348 err = merge_blob(&update_timestamps, worktree, blob2,
1349 ondisk_path, path, sb.st_mode, label_orig, blob,
1350 worktree->base_commit_id, repo,
1351 progress_cb, progress_arg);
1352 free(label_orig);
1353 if (blob2)
1354 got_object_blob_close(blob2);
1355 if (err)
1356 goto done;
1358 * Do not update timestamps of files with local changes.
1359 * Otherwise, a future status walk would treat them as
1360 * unmodified files again.
1362 err = got_fileindex_entry_update(ie, ondisk_path,
1363 blob->id.sha1, worktree->base_commit_id->sha1,
1364 update_timestamps);
1365 } else if (status == GOT_STATUS_MODE_CHANGE) {
1366 err = got_fileindex_entry_update(ie, ondisk_path,
1367 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1368 } else if (status == GOT_STATUS_DELETE) {
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1370 if (err)
1371 goto done;
1372 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1373 ondisk_path, path, blob, 0);
1374 if (err)
1375 goto done;
1376 } else {
1377 err = install_blob(worktree, ondisk_path, path, te->mode,
1378 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1379 repo, progress_cb, progress_arg);
1380 if (err)
1381 goto done;
1382 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1383 ondisk_path, path, blob, 1);
1384 if (err)
1385 goto done;
1387 got_object_blob_close(blob);
1388 done:
1389 free(ondisk_path);
1390 return err;
1393 static const struct got_error *
1394 remove_ondisk_file(const char *root_path, const char *path)
1396 const struct got_error *err = NULL;
1397 char *ondisk_path = NULL;
1399 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1400 return got_error_from_errno("asprintf");
1402 if (unlink(ondisk_path) == -1) {
1403 if (errno != ENOENT)
1404 err = got_error_from_errno2("unlink", ondisk_path);
1405 } else {
1406 char *parent = dirname(ondisk_path);
1407 while (parent && strcmp(parent, root_path) != 0) {
1408 if (rmdir(parent) == -1) {
1409 if (errno != ENOTEMPTY)
1410 err = got_error_from_errno2("rmdir",
1411 parent);
1412 break;
1414 parent = dirname(parent);
1417 free(ondisk_path);
1418 return err;
1421 static const struct got_error *
1422 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1423 struct got_fileindex_entry *ie, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1427 unsigned char status;
1428 struct stat sb;
1429 char *ondisk_path;
1431 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1432 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1434 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1435 == -1)
1436 return got_error_from_errno("asprintf");
1438 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1439 if (err)
1440 return err;
1442 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1443 status == GOT_STATUS_ADD) {
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1445 if (err)
1446 return err;
1448 * Preserve the working file and change the deleted blob's
1449 * entry into a schedule-add entry.
1451 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1452 0);
1453 if (err)
1454 return err;
1455 } else {
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1457 if (err)
1458 return err;
1459 if (status == GOT_STATUS_NO_CHANGE) {
1460 err = remove_ondisk_file(worktree->root_path, ie->path);
1461 if (err)
1462 return err;
1464 got_fileindex_entry_remove(fileindex, ie);
1467 return err;
1470 struct diff_cb_arg {
1471 struct got_fileindex *fileindex;
1472 struct got_worktree *worktree;
1473 struct got_repository *repo;
1474 got_worktree_checkout_cb progress_cb;
1475 void *progress_arg;
1476 got_cancel_cb cancel_cb;
1477 void *cancel_arg;
1480 static const struct got_error *
1481 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1482 struct got_tree_entry *te, const char *parent_path)
1484 struct diff_cb_arg *a = arg;
1486 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1487 return got_error(GOT_ERR_CANCELLED);
1489 return update_blob(a->worktree, a->fileindex, ie, te,
1490 ie->path, a->repo, a->progress_cb, a->progress_arg);
1493 static const struct got_error *
1494 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1496 struct diff_cb_arg *a = arg;
1498 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 return got_error(GOT_ERR_CANCELLED);
1501 return delete_blob(a->worktree, a->fileindex, ie,
1502 a->repo, a->progress_cb, a->progress_arg);
1505 static const struct got_error *
1506 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1508 struct diff_cb_arg *a = arg;
1509 const struct got_error *err;
1510 char *path;
1512 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1513 return got_error(GOT_ERR_CANCELLED);
1515 if (got_object_tree_entry_is_submodule(te))
1516 return NULL;
1518 if (asprintf(&path, "%s%s%s", parent_path,
1519 parent_path[0] ? "/" : "", te->name)
1520 == -1)
1521 return got_error_from_errno("asprintf");
1523 if (S_ISDIR(te->mode))
1524 err = add_dir_on_disk(a->worktree, path);
1525 else
1526 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1527 a->repo, a->progress_cb, a->progress_arg);
1529 free(path);
1530 return err;
1533 static const struct got_error *
1534 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1536 const struct got_error *err = NULL;
1537 char *uuidstr = NULL;
1538 uint32_t uuid_status;
1540 *refname = NULL;
1542 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1543 if (uuid_status != uuid_s_ok)
1544 return got_error_uuid(uuid_status, "uuid_to_string");
1546 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1547 == -1) {
1548 err = got_error_from_errno("asprintf");
1549 *refname = NULL;
1551 free(uuidstr);
1552 return err;
1555 const struct got_error *
1556 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1558 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1561 static const struct got_error *
1562 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1564 return get_ref_name(refname, worktree,
1565 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1568 static const struct got_error *
1569 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1581 static const struct got_error *
1582 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree,
1585 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1588 static const struct got_error *
1589 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1591 return get_ref_name(refname, worktree,
1592 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1595 static const struct got_error *
1596 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1598 return get_ref_name(refname, worktree,
1599 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1602 static const struct got_error *
1603 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1605 return get_ref_name(refname, worktree,
1606 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1609 static const struct got_error *
1610 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1612 return get_ref_name(refname, worktree,
1613 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1616 const struct got_error *
1617 got_worktree_get_histedit_script_path(char **path,
1618 struct got_worktree *worktree)
1620 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1621 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1622 *path = NULL;
1623 return got_error_from_errno("asprintf");
1625 return NULL;
1629 * Prevent Git's garbage collector from deleting our base commit by
1630 * setting a reference to our base commit's ID.
1632 static const struct got_error *
1633 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1635 const struct got_error *err = NULL;
1636 struct got_reference *ref = NULL;
1637 char *refname;
1639 err = got_worktree_get_base_ref_name(&refname, worktree);
1640 if (err)
1641 return err;
1643 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1644 if (err)
1645 goto done;
1647 err = got_ref_write(ref, repo);
1648 done:
1649 free(refname);
1650 if (ref)
1651 got_ref_close(ref);
1652 return err;
1655 static const struct got_error *
1656 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1658 const struct got_error *err = NULL;
1660 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1661 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 *fileindex_path = NULL;
1665 return err;
1669 static const struct got_error *
1670 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1671 struct got_worktree *worktree)
1673 const struct got_error *err = NULL;
1674 FILE *index = NULL;
1676 *fileindex_path = NULL;
1677 *fileindex = got_fileindex_alloc();
1678 if (*fileindex == NULL)
1679 return got_error_from_errno("got_fileindex_alloc");
1681 err = get_fileindex_path(fileindex_path, worktree);
1682 if (err)
1683 goto done;
1685 index = fopen(*fileindex_path, "rb");
1686 if (index == NULL) {
1687 if (errno != ENOENT)
1688 err = got_error_from_errno2("fopen", *fileindex_path);
1689 } else {
1690 err = got_fileindex_read(*fileindex, index);
1691 if (fclose(index) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1694 done:
1695 if (err) {
1696 free(*fileindex_path);
1697 *fileindex_path = NULL;
1698 got_fileindex_free(*fileindex);
1699 *fileindex = NULL;
1701 return err;
1704 struct bump_base_commit_id_arg {
1705 struct got_object_id *base_commit_id;
1706 const char *path;
1707 size_t path_len;
1708 const char *entry_name;
1709 got_worktree_checkout_cb progress_cb;
1710 void *progress_arg;
1713 /* Bump base commit ID of all files within an updated part of the work tree. */
1714 static const struct got_error *
1715 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1717 const struct got_error *err;
1718 struct bump_base_commit_id_arg *a = arg;
1720 if (a->entry_name) {
1721 if (strcmp(ie->path, a->path) != 0)
1722 return NULL;
1723 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1724 return NULL;
1726 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1727 SHA1_DIGEST_LENGTH) == 0)
1728 return NULL;
1730 if (a->progress_cb) {
1731 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1732 ie->path);
1733 if (err)
1734 return err;
1736 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1737 return NULL;
1740 static const struct got_error *
1741 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1743 const struct got_error *err = NULL;
1744 char *new_fileindex_path = NULL;
1745 FILE *new_index = NULL;
1746 struct timespec timeout;
1748 err = got_opentemp_named(&new_fileindex_path, &new_index,
1749 fileindex_path);
1750 if (err)
1751 goto done;
1753 err = got_fileindex_write(fileindex, new_index);
1754 if (err)
1755 goto done;
1757 if (rename(new_fileindex_path, fileindex_path) != 0) {
1758 err = got_error_from_errno3("rename", new_fileindex_path,
1759 fileindex_path);
1760 unlink(new_fileindex_path);
1764 * Sleep for a short amount of time to ensure that files modified after
1765 * this program exits have a different time stamp from the one which
1766 * was recorded in the file index.
1768 timeout.tv_sec = 0;
1769 timeout.tv_nsec = 1;
1770 nanosleep(&timeout, NULL);
1771 done:
1772 if (new_index)
1773 fclose(new_index);
1774 free(new_fileindex_path);
1775 return err;
1778 static const struct got_error *
1779 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1780 struct got_object_id **tree_id, const char *wt_relpath,
1781 struct got_worktree *worktree, struct got_repository *repo)
1783 const struct got_error *err = NULL;
1784 struct got_object_id *id = NULL;
1785 char *in_repo_path = NULL;
1786 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1788 *entry_type = GOT_OBJ_TYPE_ANY;
1789 *tree_relpath = NULL;
1790 *tree_id = NULL;
1792 if (wt_relpath[0] == '\0') {
1793 /* Check out all files within the work tree. */
1794 *entry_type = GOT_OBJ_TYPE_TREE;
1795 *tree_relpath = strdup("");
1796 if (*tree_relpath == NULL) {
1797 err = got_error_from_errno("strdup");
1798 goto done;
1800 err = got_object_id_by_path(tree_id, repo,
1801 worktree->base_commit_id, worktree->path_prefix);
1802 if (err)
1803 goto done;
1804 return NULL;
1807 /* Check out a subset of files in the work tree. */
1809 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1810 is_root_wt ? "" : "/", wt_relpath) == -1) {
1811 err = got_error_from_errno("asprintf");
1812 goto done;
1815 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1816 in_repo_path);
1817 if (err)
1818 goto done;
1820 free(in_repo_path);
1821 in_repo_path = NULL;
1823 err = got_object_get_type(entry_type, repo, id);
1824 if (err)
1825 goto done;
1827 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1828 /* Check out a single file. */
1829 if (strchr(wt_relpath, '/') == NULL) {
1830 /* Check out a single file in work tree's root dir. */
1831 in_repo_path = strdup(worktree->path_prefix);
1832 if (in_repo_path == NULL) {
1833 err = got_error_from_errno("strdup");
1834 goto done;
1836 *tree_relpath = strdup("");
1837 if (*tree_relpath == NULL) {
1838 err = got_error_from_errno("strdup");
1839 goto done;
1841 } else {
1842 /* Check out a single file in a subdirectory. */
1843 err = got_path_dirname(tree_relpath, wt_relpath);
1844 if (err)
1845 return err;
1846 if (asprintf(&in_repo_path, "%s%s%s",
1847 worktree->path_prefix, is_root_wt ? "" : "/",
1848 *tree_relpath) == -1) {
1849 err = got_error_from_errno("asprintf");
1850 goto done;
1853 err = got_object_id_by_path(tree_id, repo,
1854 worktree->base_commit_id, in_repo_path);
1855 } else {
1856 /* Check out all files within a subdirectory. */
1857 *tree_id = got_object_id_dup(id);
1858 if (*tree_id == NULL) {
1859 err = got_error_from_errno("got_object_id_dup");
1860 goto done;
1862 *tree_relpath = strdup(wt_relpath);
1863 if (*tree_relpath == NULL) {
1864 err = got_error_from_errno("strdup");
1865 goto done;
1868 done:
1869 free(id);
1870 free(in_repo_path);
1871 if (err) {
1872 *entry_type = GOT_OBJ_TYPE_ANY;
1873 free(*tree_relpath);
1874 *tree_relpath = NULL;
1875 free(*tree_id);
1876 *tree_id = NULL;
1878 return err;
1881 static const struct got_error *
1882 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1883 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1884 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1885 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1887 const struct got_error *err = NULL;
1888 struct got_commit_object *commit = NULL;
1889 struct got_tree_object *tree = NULL;
1890 struct got_fileindex_diff_tree_cb diff_cb;
1891 struct diff_cb_arg arg;
1893 err = ref_base_commit(worktree, repo);
1894 if (err) {
1895 if (!(err->code == GOT_ERR_ERRNO && errno == EACCES))
1896 goto done;
1897 err = (*progress_cb)(progress_arg,
1898 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1899 if (err)
1900 return err;
1903 err = got_object_open_as_commit(&commit, repo,
1904 worktree->base_commit_id);
1905 if (err)
1906 goto done;
1908 err = got_object_open_as_tree(&tree, repo, tree_id);
1909 if (err)
1910 goto done;
1912 if (entry_name &&
1913 got_object_tree_find_entry(tree, entry_name) == NULL) {
1914 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1915 goto done;
1918 diff_cb.diff_old_new = diff_old_new;
1919 diff_cb.diff_old = diff_old;
1920 diff_cb.diff_new = diff_new;
1921 arg.fileindex = fileindex;
1922 arg.worktree = worktree;
1923 arg.repo = repo;
1924 arg.progress_cb = progress_cb;
1925 arg.progress_arg = progress_arg;
1926 arg.cancel_cb = cancel_cb;
1927 arg.cancel_arg = cancel_arg;
1928 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1929 entry_name, repo, &diff_cb, &arg);
1930 done:
1931 if (tree)
1932 got_object_tree_close(tree);
1933 if (commit)
1934 got_object_commit_close(commit);
1935 return err;
1938 const struct got_error *
1939 got_worktree_checkout_files(struct got_worktree *worktree,
1940 struct got_pathlist_head *paths, struct got_repository *repo,
1941 got_worktree_checkout_cb progress_cb, void *progress_arg,
1942 got_cancel_cb cancel_cb, void *cancel_arg)
1944 const struct got_error *err = NULL, *sync_err, *unlockerr;
1945 struct got_commit_object *commit = NULL;
1946 struct got_tree_object *tree = NULL;
1947 struct got_fileindex *fileindex = NULL;
1948 char *fileindex_path = NULL;
1949 struct got_pathlist_entry *pe;
1950 struct tree_path_data {
1951 SIMPLEQ_ENTRY(tree_path_data) entry;
1952 struct got_object_id *tree_id;
1953 int entry_type;
1954 char *relpath;
1955 char *entry_name;
1956 } *tpd = NULL;
1957 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1959 SIMPLEQ_INIT(&tree_paths);
1961 err = lock_worktree(worktree, LOCK_EX);
1962 if (err)
1963 return err;
1965 /* Map all specified paths to in-repository trees. */
1966 TAILQ_FOREACH(pe, paths, entry) {
1967 tpd = malloc(sizeof(*tpd));
1968 if (tpd == NULL) {
1969 err = got_error_from_errno("malloc");
1970 goto done;
1973 err = find_tree_entry_for_checkout(&tpd->entry_type,
1974 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1975 if (err) {
1976 free(tpd);
1977 goto done;
1980 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1981 err = got_path_basename(&tpd->entry_name, pe->path);
1982 if (err) {
1983 free(tpd->relpath);
1984 free(tpd->tree_id);
1985 free(tpd);
1986 goto done;
1988 } else
1989 tpd->entry_name = NULL;
1991 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1995 * Read the file index.
1996 * Checking out files is supposed to be an idempotent operation.
1997 * If the on-disk file index is incomplete we will try to complete it.
1999 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2000 if (err)
2001 goto done;
2003 tpd = SIMPLEQ_FIRST(&tree_paths);
2004 TAILQ_FOREACH(pe, paths, entry) {
2005 struct bump_base_commit_id_arg bbc_arg;
2007 err = checkout_files(worktree, fileindex, tpd->relpath,
2008 tpd->tree_id, tpd->entry_name, repo,
2009 progress_cb, progress_arg, cancel_cb, cancel_arg);
2010 if (err)
2011 break;
2013 bbc_arg.base_commit_id = worktree->base_commit_id;
2014 bbc_arg.entry_name = tpd->entry_name;
2015 bbc_arg.path = pe->path;
2016 bbc_arg.path_len = pe->path_len;
2017 bbc_arg.progress_cb = progress_cb;
2018 bbc_arg.progress_arg = progress_arg;
2019 err = got_fileindex_for_each_entry_safe(fileindex,
2020 bump_base_commit_id, &bbc_arg);
2021 if (err)
2022 break;
2024 tpd = SIMPLEQ_NEXT(tpd, entry);
2026 sync_err = sync_fileindex(fileindex, fileindex_path);
2027 if (sync_err && err == NULL)
2028 err = sync_err;
2029 done:
2030 free(fileindex_path);
2031 if (tree)
2032 got_object_tree_close(tree);
2033 if (commit)
2034 got_object_commit_close(commit);
2035 if (fileindex)
2036 got_fileindex_free(fileindex);
2037 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2038 tpd = SIMPLEQ_FIRST(&tree_paths);
2039 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2040 free(tpd->relpath);
2041 free(tpd->tree_id);
2042 free(tpd);
2044 unlockerr = lock_worktree(worktree, LOCK_SH);
2045 if (unlockerr && err == NULL)
2046 err = unlockerr;
2047 return err;
2050 struct merge_file_cb_arg {
2051 struct got_worktree *worktree;
2052 struct got_fileindex *fileindex;
2053 got_worktree_checkout_cb progress_cb;
2054 void *progress_arg;
2055 got_cancel_cb cancel_cb;
2056 void *cancel_arg;
2057 const char *label_orig;
2058 struct got_object_id *commit_id2;
2061 static const struct got_error *
2062 merge_file_cb(void *arg, struct got_blob_object *blob1,
2063 struct got_blob_object *blob2, struct got_object_id *id1,
2064 struct got_object_id *id2, const char *path1, const char *path2,
2065 mode_t mode1, mode_t mode2, struct got_repository *repo)
2067 static const struct got_error *err = NULL;
2068 struct merge_file_cb_arg *a = arg;
2069 struct got_fileindex_entry *ie;
2070 char *ondisk_path = NULL;
2071 struct stat sb;
2072 unsigned char status;
2073 int local_changes_subsumed;
2075 if (blob1 && blob2) {
2076 ie = got_fileindex_entry_get(a->fileindex, path2,
2077 strlen(path2));
2078 if (ie == NULL)
2079 return (*a->progress_cb)(a->progress_arg,
2080 GOT_STATUS_MISSING, path2);
2082 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2083 path2) == -1)
2084 return got_error_from_errno("asprintf");
2086 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2087 repo);
2088 if (err)
2089 goto done;
2091 if (status == GOT_STATUS_DELETE) {
2092 err = (*a->progress_cb)(a->progress_arg,
2093 GOT_STATUS_MERGE, path2);
2094 goto done;
2096 if (status != GOT_STATUS_NO_CHANGE &&
2097 status != GOT_STATUS_MODIFY &&
2098 status != GOT_STATUS_CONFLICT &&
2099 status != GOT_STATUS_ADD) {
2100 err = (*a->progress_cb)(a->progress_arg, status, path2);
2101 goto done;
2104 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2105 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2106 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2107 } else if (blob1) {
2108 ie = got_fileindex_entry_get(a->fileindex, path1,
2109 strlen(path1));
2110 if (ie == NULL)
2111 return (*a->progress_cb)(a->progress_arg,
2112 GOT_STATUS_MISSING, path2);
2114 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2115 path1) == -1)
2116 return got_error_from_errno("asprintf");
2118 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2119 repo);
2120 if (err)
2121 goto done;
2123 switch (status) {
2124 case GOT_STATUS_NO_CHANGE:
2125 err = (*a->progress_cb)(a->progress_arg,
2126 GOT_STATUS_DELETE, path1);
2127 if (err)
2128 goto done;
2129 err = remove_ondisk_file(a->worktree->root_path, path1);
2130 if (err)
2131 goto done;
2132 if (ie)
2133 got_fileindex_entry_mark_deleted_from_disk(ie);
2134 break;
2135 case GOT_STATUS_DELETE:
2136 case GOT_STATUS_MISSING:
2137 err = (*a->progress_cb)(a->progress_arg,
2138 GOT_STATUS_DELETE, path1);
2139 if (err)
2140 goto done;
2141 if (ie)
2142 got_fileindex_entry_mark_deleted_from_disk(ie);
2143 break;
2144 case GOT_STATUS_ADD:
2145 case GOT_STATUS_MODIFY:
2146 case GOT_STATUS_CONFLICT:
2147 err = (*a->progress_cb)(a->progress_arg,
2148 GOT_STATUS_CANNOT_DELETE, path1);
2149 if (err)
2150 goto done;
2151 break;
2152 case GOT_STATUS_OBSTRUCTED:
2153 err = (*a->progress_cb)(a->progress_arg, status, path1);
2154 if (err)
2155 goto done;
2156 break;
2157 default:
2158 break;
2160 } else if (blob2) {
2161 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2162 path2) == -1)
2163 return got_error_from_errno("asprintf");
2164 ie = got_fileindex_entry_get(a->fileindex, path2,
2165 strlen(path2));
2166 if (ie) {
2167 err = get_file_status(&status, &sb, ie, ondisk_path,
2168 -1, NULL, repo);
2169 if (err)
2170 goto done;
2171 if (status != GOT_STATUS_NO_CHANGE &&
2172 status != GOT_STATUS_MODIFY &&
2173 status != GOT_STATUS_CONFLICT &&
2174 status != GOT_STATUS_ADD) {
2175 err = (*a->progress_cb)(a->progress_arg,
2176 status, path2);
2177 goto done;
2179 err = merge_blob(&local_changes_subsumed, a->worktree,
2180 NULL, ondisk_path, path2, sb.st_mode,
2181 a->label_orig, blob2, a->commit_id2, repo,
2182 a->progress_cb,
2183 a->progress_arg);
2184 if (status == GOT_STATUS_DELETE) {
2185 err = update_blob_fileindex_entry(a->worktree,
2186 a->fileindex, ie, ondisk_path, ie->path,
2187 blob2, 0);
2188 if (err)
2189 goto done;
2191 } else {
2192 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2193 err = install_blob(a->worktree, ondisk_path, path2,
2194 /* XXX get this from parent tree! */
2195 GOT_DEFAULT_FILE_MODE,
2196 sb.st_mode, blob2, 0, 0, repo,
2197 a->progress_cb, a->progress_arg);
2198 if (err)
2199 goto done;
2200 err = got_fileindex_entry_alloc(&ie,
2201 ondisk_path, path2, NULL, NULL);
2202 if (err)
2203 goto done;
2204 err = got_fileindex_entry_add(a->fileindex, ie);
2205 if (err) {
2206 got_fileindex_entry_free(ie);
2207 goto done;
2211 done:
2212 free(ondisk_path);
2213 return err;
2216 struct check_merge_ok_arg {
2217 struct got_worktree *worktree;
2218 struct got_repository *repo;
2221 static const struct got_error *
2222 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2224 const struct got_error *err = NULL;
2225 struct check_merge_ok_arg *a = arg;
2226 unsigned char status;
2227 struct stat sb;
2228 char *ondisk_path;
2230 /* Reject merges into a work tree with mixed base commits. */
2231 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2232 SHA1_DIGEST_LENGTH))
2233 return got_error(GOT_ERR_MIXED_COMMITS);
2235 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2236 == -1)
2237 return got_error_from_errno("asprintf");
2239 /* Reject merges into a work tree with conflicted files. */
2240 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2241 if (err)
2242 return err;
2243 if (status == GOT_STATUS_CONFLICT)
2244 return got_error(GOT_ERR_CONFLICTS);
2246 return NULL;
2249 static const struct got_error *
2250 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2251 const char *fileindex_path, struct got_object_id *commit_id1,
2252 struct got_object_id *commit_id2, struct got_repository *repo,
2253 got_worktree_checkout_cb progress_cb, void *progress_arg,
2254 got_cancel_cb cancel_cb, void *cancel_arg)
2256 const struct got_error *err = NULL, *sync_err;
2257 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2258 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2259 struct merge_file_cb_arg arg;
2260 char *label_orig = NULL;
2262 if (commit_id1) {
2263 char *id_str;
2265 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2266 worktree->path_prefix);
2267 if (err)
2268 goto done;
2270 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2271 if (err)
2272 goto done;
2274 err = got_object_id_str(&id_str, commit_id1);
2275 if (err)
2276 goto done;
2278 if (asprintf(&label_orig, "%s: commit %s",
2279 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2280 err = got_error_from_errno("asprintf");
2281 free(id_str);
2282 goto done;
2284 free(id_str);
2287 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2288 worktree->path_prefix);
2289 if (err)
2290 goto done;
2292 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2293 if (err)
2294 goto done;
2296 arg.worktree = worktree;
2297 arg.fileindex = fileindex;
2298 arg.progress_cb = progress_cb;
2299 arg.progress_arg = progress_arg;
2300 arg.cancel_cb = cancel_cb;
2301 arg.cancel_arg = cancel_arg;
2302 arg.label_orig = label_orig;
2303 arg.commit_id2 = commit_id2;
2304 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2305 sync_err = sync_fileindex(fileindex, fileindex_path);
2306 if (sync_err && err == NULL)
2307 err = sync_err;
2308 done:
2309 if (tree1)
2310 got_object_tree_close(tree1);
2311 if (tree2)
2312 got_object_tree_close(tree2);
2313 free(label_orig);
2314 return err;
2317 const struct got_error *
2318 got_worktree_merge_files(struct got_worktree *worktree,
2319 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2320 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2321 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2323 const struct got_error *err, *unlockerr;
2324 char *fileindex_path = NULL;
2325 struct got_fileindex *fileindex = NULL;
2326 struct check_merge_ok_arg mok_arg;
2328 err = lock_worktree(worktree, LOCK_EX);
2329 if (err)
2330 return err;
2332 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2333 if (err)
2334 goto done;
2336 mok_arg.worktree = worktree;
2337 mok_arg.repo = repo;
2338 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2339 &mok_arg);
2340 if (err)
2341 goto done;
2343 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2344 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2345 done:
2346 if (fileindex)
2347 got_fileindex_free(fileindex);
2348 free(fileindex_path);
2349 unlockerr = lock_worktree(worktree, LOCK_SH);
2350 if (unlockerr && err == NULL)
2351 err = unlockerr;
2352 return err;
2355 struct diff_dir_cb_arg {
2356 struct got_fileindex *fileindex;
2357 struct got_worktree *worktree;
2358 const char *status_path;
2359 size_t status_path_len;
2360 struct got_repository *repo;
2361 got_worktree_status_cb status_cb;
2362 void *status_arg;
2363 got_cancel_cb cancel_cb;
2364 void *cancel_arg;
2365 /* A pathlist containing per-directory pathlists of ignore patterns. */
2366 struct got_pathlist_head ignores;
2367 int report_unchanged;
2370 static const struct got_error *
2371 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2372 int dirfd, const char *de_name,
2373 got_worktree_status_cb status_cb, void *status_arg,
2374 struct got_repository *repo, int report_unchanged)
2376 const struct got_error *err = NULL;
2377 unsigned char status = GOT_STATUS_NO_CHANGE;
2378 unsigned char staged_status = get_staged_status(ie);
2379 struct stat sb;
2380 struct got_object_id blob_id, commit_id, staged_blob_id;
2381 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2382 struct got_object_id *staged_blob_idp = NULL;
2384 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2385 if (err)
2386 return err;
2388 if (status == GOT_STATUS_NO_CHANGE &&
2389 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2390 return NULL;
2392 if (got_fileindex_entry_has_blob(ie)) {
2393 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2394 blob_idp = &blob_id;
2396 if (got_fileindex_entry_has_commit(ie)) {
2397 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2398 commit_idp = &commit_id;
2400 if (staged_status == GOT_STATUS_ADD ||
2401 staged_status == GOT_STATUS_MODIFY) {
2402 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2403 SHA1_DIGEST_LENGTH);
2404 staged_blob_idp = &staged_blob_id;
2407 return (*status_cb)(status_arg, status, staged_status,
2408 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2411 static const struct got_error *
2412 status_old_new(void *arg, struct got_fileindex_entry *ie,
2413 struct dirent *de, const char *parent_path, int dirfd)
2415 const struct got_error *err = NULL;
2416 struct diff_dir_cb_arg *a = arg;
2417 char *abspath;
2419 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2420 return got_error(GOT_ERR_CANCELLED);
2422 if (got_path_cmp(parent_path, a->status_path,
2423 strlen(parent_path), a->status_path_len) != 0 &&
2424 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2425 return NULL;
2427 if (parent_path[0]) {
2428 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2429 parent_path, de->d_name) == -1)
2430 return got_error_from_errno("asprintf");
2431 } else {
2432 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2433 de->d_name) == -1)
2434 return got_error_from_errno("asprintf");
2437 err = report_file_status(ie, abspath, dirfd, de->d_name,
2438 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2439 free(abspath);
2440 return err;
2443 static const struct got_error *
2444 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2446 struct diff_dir_cb_arg *a = arg;
2447 struct got_object_id blob_id, commit_id;
2448 unsigned char status;
2450 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2451 return got_error(GOT_ERR_CANCELLED);
2453 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2454 return NULL;
2456 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2457 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2458 if (got_fileindex_entry_has_file_on_disk(ie))
2459 status = GOT_STATUS_MISSING;
2460 else
2461 status = GOT_STATUS_DELETE;
2462 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2463 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2466 void
2467 free_ignorelist(struct got_pathlist_head *ignorelist)
2469 struct got_pathlist_entry *pe;
2471 TAILQ_FOREACH(pe, ignorelist, entry)
2472 free((char *)pe->path);
2473 got_pathlist_free(ignorelist);
2476 void
2477 free_ignores(struct got_pathlist_head *ignores)
2479 struct got_pathlist_entry *pe;
2481 TAILQ_FOREACH(pe, ignores, entry) {
2482 struct got_pathlist_head *ignorelist = pe->data;
2483 free_ignorelist(ignorelist);
2484 free((char *)pe->path);
2486 got_pathlist_free(ignores);
2489 static const struct got_error *
2490 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2492 const struct got_error *err = NULL;
2493 struct got_pathlist_entry *pe = NULL;
2494 struct got_pathlist_head *ignorelist;
2495 char *line = NULL, *pattern, *dirpath = NULL;
2496 size_t linesize = 0;
2497 ssize_t linelen;
2499 ignorelist = calloc(1, sizeof(*ignorelist));
2500 if (ignorelist == NULL)
2501 return got_error_from_errno("calloc");
2502 TAILQ_INIT(ignorelist);
2504 while ((linelen = getline(&line, &linesize, f)) != -1) {
2505 if (linelen > 0 && line[linelen - 1] == '\n')
2506 line[linelen - 1] = '\0';
2508 /* Git's ignores may contain comments. */
2509 if (line[0] == '#')
2510 continue;
2512 /* Git's negated patterns are not (yet?) supported. */
2513 if (line[0] == '!')
2514 continue;
2516 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2517 line) == -1) {
2518 err = got_error_from_errno("asprintf");
2519 goto done;
2521 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2522 if (err)
2523 goto done;
2525 if (ferror(f)) {
2526 err = got_error_from_errno("getline");
2527 goto done;
2530 dirpath = strdup(path);
2531 if (dirpath == NULL) {
2532 err = got_error_from_errno("strdup");
2533 goto done;
2535 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2536 done:
2537 free(line);
2538 if (err || pe == NULL) {
2539 free(dirpath);
2540 free_ignorelist(ignorelist);
2542 return err;
2545 int
2546 match_ignores(struct got_pathlist_head *ignores, const char *path)
2548 struct got_pathlist_entry *pe;
2550 /* Handle patterns which match in all directories. */
2551 TAILQ_FOREACH(pe, ignores, entry) {
2552 struct got_pathlist_head *ignorelist = pe->data;
2553 struct got_pathlist_entry *pi;
2555 TAILQ_FOREACH(pi, ignorelist, entry) {
2556 const char *p, *pattern = pi->path;
2558 if (strncmp(pattern, "**/", 3) != 0)
2559 continue;
2560 pattern += 3;
2561 p = path;
2562 while (*p) {
2563 if (fnmatch(pattern, p,
2564 FNM_PATHNAME | FNM_LEADING_DIR)) {
2565 /* Retry in next directory. */
2566 while (*p && *p != '/')
2567 p++;
2568 while (*p == '/')
2569 p++;
2570 continue;
2572 return 1;
2578 * The ignores pathlist contains ignore lists from children before
2579 * parents, so we can find the most specific ignorelist by walking
2580 * ignores backwards.
2582 pe = TAILQ_LAST(ignores, got_pathlist_head);
2583 while (pe) {
2584 if (got_path_is_child(path, pe->path, pe->path_len)) {
2585 struct got_pathlist_head *ignorelist = pe->data;
2586 struct got_pathlist_entry *pi;
2587 TAILQ_FOREACH(pi, ignorelist, entry) {
2588 const char *pattern = pi->path;
2589 int flags = FNM_LEADING_DIR;
2590 if (strstr(pattern, "/**/") == NULL)
2591 flags |= FNM_PATHNAME;
2592 if (fnmatch(pattern, path, flags))
2593 continue;
2594 return 1;
2597 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2600 return 0;
2603 static const struct got_error *
2604 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2605 const char *path, int dirfd, const char *ignores_filename)
2607 const struct got_error *err = NULL;
2608 char *ignorespath;
2609 int fd = -1;
2610 FILE *ignoresfile = NULL;
2612 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2613 path[0] ? "/" : "", ignores_filename) == -1)
2614 return got_error_from_errno("asprintf");
2616 if (dirfd != -1) {
2617 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2618 if (fd == -1) {
2619 if (errno != ENOENT && errno != EACCES)
2620 err = got_error_from_errno2("openat",
2621 ignorespath);
2622 } else {
2623 ignoresfile = fdopen(fd, "r");
2624 if (ignoresfile == NULL)
2625 err = got_error_from_errno2("fdopen",
2626 ignorespath);
2627 else {
2628 fd = -1;
2629 err = read_ignores(ignores, path, ignoresfile);
2632 } else {
2633 ignoresfile = fopen(ignorespath, "r");
2634 if (ignoresfile == NULL) {
2635 if (errno != ENOENT && errno != EACCES)
2636 err = got_error_from_errno2("fopen",
2637 ignorespath);
2638 } else
2639 err = read_ignores(ignores, path, ignoresfile);
2642 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2643 err = got_error_from_errno2("fclose", path);
2644 if (fd != -1 && close(fd) == -1 && err == NULL)
2645 err = got_error_from_errno2("close", path);
2646 free(ignorespath);
2647 return err;
2650 static const struct got_error *
2651 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2653 const struct got_error *err = NULL;
2654 struct diff_dir_cb_arg *a = arg;
2655 char *path = NULL;
2657 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2658 return got_error(GOT_ERR_CANCELLED);
2660 /* XXX ignore symlinks for now */
2661 if (de->d_type == DT_LNK)
2662 return NULL;
2664 if (parent_path[0]) {
2665 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2666 return got_error_from_errno("asprintf");
2667 } else {
2668 path = de->d_name;
2671 if (de->d_type == DT_DIR) {
2672 int subdirfd = openat(dirfd, de->d_name,
2673 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2674 if (subdirfd == -1) {
2675 if (errno != ENOENT && errno != EACCES)
2676 err = got_error_from_errno2("openat", path);
2677 } else {
2678 err = add_ignores(&a->ignores, a->worktree->root_path,
2679 path, subdirfd, ".cvsignore");
2680 if (err == NULL)
2681 err = add_ignores(&a->ignores,
2682 a->worktree->root_path, path,
2683 subdirfd, ".gitignore");
2684 if (close(subdirfd) == -1 && err == NULL)
2685 err = got_error_from_errno2("close", path);
2687 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2688 && !match_ignores(&a->ignores, path))
2689 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2690 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2691 if (parent_path[0])
2692 free(path);
2693 return err;
2696 static const struct got_error *
2697 report_single_file_status(const char *path, const char *ondisk_path,
2698 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2699 void *status_arg, struct got_repository *repo, int report_unchanged)
2701 struct got_fileindex_entry *ie;
2702 struct stat sb;
2704 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2705 if (ie)
2706 return report_file_status(ie, ondisk_path, -1, NULL,
2707 status_cb, status_arg, repo, report_unchanged);
2709 if (lstat(ondisk_path, &sb) == -1) {
2710 if (errno != ENOENT)
2711 return got_error_from_errno2("lstat", ondisk_path);
2712 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2713 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2714 return NULL;
2717 if (S_ISREG(sb.st_mode))
2718 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2719 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2721 return NULL;
2724 static const struct got_error *
2725 worktree_status(struct got_worktree *worktree, const char *path,
2726 struct got_fileindex *fileindex, struct got_repository *repo,
2727 got_worktree_status_cb status_cb, void *status_arg,
2728 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2729 int report_unchanged)
2731 const struct got_error *err = NULL;
2732 int fd = -1;
2733 struct got_fileindex_diff_dir_cb fdiff_cb;
2734 struct diff_dir_cb_arg arg;
2735 char *ondisk_path = NULL;
2737 if (asprintf(&ondisk_path, "%s%s%s",
2738 worktree->root_path, path[0] ? "/" : "", path) == -1)
2739 return got_error_from_errno("asprintf");
2741 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2742 if (fd == -1) {
2743 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2744 err = got_error_from_errno2("open", ondisk_path);
2745 else
2746 err = report_single_file_status(path, ondisk_path,
2747 fileindex, status_cb, status_arg, repo,
2748 report_unchanged);
2749 } else {
2750 fdiff_cb.diff_old_new = status_old_new;
2751 fdiff_cb.diff_old = status_old;
2752 fdiff_cb.diff_new = status_new;
2753 arg.fileindex = fileindex;
2754 arg.worktree = worktree;
2755 arg.status_path = path;
2756 arg.status_path_len = strlen(path);
2757 arg.repo = repo;
2758 arg.status_cb = status_cb;
2759 arg.status_arg = status_arg;
2760 arg.cancel_cb = cancel_cb;
2761 arg.cancel_arg = cancel_arg;
2762 arg.report_unchanged = report_unchanged;
2763 TAILQ_INIT(&arg.ignores);
2764 if (!no_ignores) {
2765 err = add_ignores(&arg.ignores, worktree->root_path,
2766 path, fd, ".cvsignore");
2767 if (err == NULL)
2768 err = add_ignores(&arg.ignores,
2769 worktree->root_path, path, fd,
2770 ".gitignore");
2772 if (err == NULL)
2773 err = got_fileindex_diff_dir(fileindex, fd,
2774 worktree->root_path, path, repo, &fdiff_cb, &arg);
2775 free_ignores(&arg.ignores);
2778 if (fd != -1 && close(fd) != 0 && err == NULL)
2779 err = got_error_from_errno("close");
2780 free(ondisk_path);
2781 return err;
2784 const struct got_error *
2785 got_worktree_status(struct got_worktree *worktree,
2786 struct got_pathlist_head *paths, struct got_repository *repo,
2787 got_worktree_status_cb status_cb, void *status_arg,
2788 got_cancel_cb cancel_cb, void *cancel_arg)
2790 const struct got_error *err = NULL;
2791 char *fileindex_path = NULL;
2792 struct got_fileindex *fileindex = NULL;
2793 struct got_pathlist_entry *pe;
2795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2796 if (err)
2797 return err;
2799 TAILQ_FOREACH(pe, paths, entry) {
2800 err = worktree_status(worktree, pe->path, fileindex, repo,
2801 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2802 if (err)
2803 break;
2805 free(fileindex_path);
2806 got_fileindex_free(fileindex);
2807 return err;
2810 const struct got_error *
2811 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2812 const char *arg)
2814 const struct got_error *err = NULL;
2815 char *resolved, *cwd = NULL, *path = NULL;
2816 size_t len;
2818 *wt_path = NULL;
2820 resolved = realpath(arg, NULL);
2821 if (resolved == NULL) {
2822 if (errno != ENOENT)
2823 return got_error_from_errno2("realpath", arg);
2824 cwd = getcwd(NULL, 0);
2825 if (cwd == NULL)
2826 return got_error_from_errno("getcwd");
2827 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2828 err = got_error_from_errno("asprintf");
2829 goto done;
2833 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2834 strlen(got_worktree_get_root_path(worktree)))) {
2835 err = got_error(GOT_ERR_BAD_PATH);
2836 goto done;
2839 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2840 err = got_path_skip_common_ancestor(&path,
2841 got_worktree_get_root_path(worktree), resolved);
2842 if (err)
2843 goto done;
2844 } else {
2845 path = strdup("");
2846 if (path == NULL) {
2847 err = got_error_from_errno("strdup");
2848 goto done;
2852 /* XXX status walk can't deal with trailing slash! */
2853 len = strlen(path);
2854 while (len > 0 && path[len - 1] == '/') {
2855 path[len - 1] = '\0';
2856 len--;
2858 done:
2859 free(resolved);
2860 free(cwd);
2861 if (err == NULL)
2862 *wt_path = path;
2863 else
2864 free(path);
2865 return err;
2868 struct schedule_addition_args {
2869 struct got_worktree *worktree;
2870 struct got_fileindex *fileindex;
2871 got_worktree_checkout_cb progress_cb;
2872 void *progress_arg;
2873 struct got_repository *repo;
2876 static const struct got_error *
2877 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2878 const char *relpath, struct got_object_id *blob_id,
2879 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2880 int dirfd, const char *de_name)
2882 struct schedule_addition_args *a = arg;
2883 const struct got_error *err = NULL;
2884 struct got_fileindex_entry *ie;
2885 struct stat sb;
2886 char *ondisk_path;
2888 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2889 relpath) == -1)
2890 return got_error_from_errno("asprintf");
2892 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2893 if (ie) {
2894 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2895 de_name, a->repo);
2896 if (err)
2897 goto done;
2898 /* Re-adding an existing entry is a no-op. */
2899 if (status == GOT_STATUS_ADD)
2900 goto done;
2901 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2902 if (err)
2903 goto done;
2906 if (status != GOT_STATUS_UNVERSIONED) {
2907 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2908 goto done;
2911 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2912 if (err)
2913 goto done;
2915 err = got_fileindex_entry_add(a->fileindex, ie);
2916 if (err) {
2917 got_fileindex_entry_free(ie);
2918 goto done;
2920 done:
2921 free(ondisk_path);
2922 if (err)
2923 return err;
2924 if (status == GOT_STATUS_ADD)
2925 return NULL;
2926 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2929 const struct got_error *
2930 got_worktree_schedule_add(struct got_worktree *worktree,
2931 struct got_pathlist_head *paths,
2932 got_worktree_checkout_cb progress_cb, void *progress_arg,
2933 struct got_repository *repo, int no_ignores)
2935 struct got_fileindex *fileindex = NULL;
2936 char *fileindex_path = NULL;
2937 const struct got_error *err = NULL, *sync_err, *unlockerr;
2938 struct got_pathlist_entry *pe;
2939 struct schedule_addition_args saa;
2941 err = lock_worktree(worktree, LOCK_EX);
2942 if (err)
2943 return err;
2945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2946 if (err)
2947 goto done;
2949 saa.worktree = worktree;
2950 saa.fileindex = fileindex;
2951 saa.progress_cb = progress_cb;
2952 saa.progress_arg = progress_arg;
2953 saa.repo = repo;
2955 TAILQ_FOREACH(pe, paths, entry) {
2956 err = worktree_status(worktree, pe->path, fileindex, repo,
2957 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2958 if (err)
2959 break;
2961 sync_err = sync_fileindex(fileindex, fileindex_path);
2962 if (sync_err && err == NULL)
2963 err = sync_err;
2964 done:
2965 free(fileindex_path);
2966 if (fileindex)
2967 got_fileindex_free(fileindex);
2968 unlockerr = lock_worktree(worktree, LOCK_SH);
2969 if (unlockerr && err == NULL)
2970 err = unlockerr;
2971 return err;
2974 struct schedule_deletion_args {
2975 struct got_worktree *worktree;
2976 struct got_fileindex *fileindex;
2977 got_worktree_delete_cb progress_cb;
2978 void *progress_arg;
2979 struct got_repository *repo;
2980 int delete_local_mods;
2981 int keep_on_disk;
2984 static const struct got_error *
2985 schedule_for_deletion(void *arg, unsigned char status,
2986 unsigned char staged_status, const char *relpath,
2987 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2988 struct got_object_id *commit_id, int dirfd, const char *de_name)
2990 struct schedule_deletion_args *a = arg;
2991 const struct got_error *err = NULL;
2992 struct got_fileindex_entry *ie = NULL;
2993 struct stat sb;
2994 char *ondisk_path;
2996 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2997 if (ie == NULL)
2998 return got_error(GOT_ERR_BAD_PATH);
3000 staged_status = get_staged_status(ie);
3001 if (staged_status != GOT_STATUS_NO_CHANGE) {
3002 if (staged_status == GOT_STATUS_DELETE)
3003 return NULL;
3004 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3008 relpath) == -1)
3009 return got_error_from_errno("asprintf");
3011 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3012 a->repo);
3013 if (err)
3014 goto done;
3016 if (status != GOT_STATUS_NO_CHANGE) {
3017 if (status == GOT_STATUS_DELETE)
3018 goto done;
3019 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3020 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3021 goto done;
3023 if (status != GOT_STATUS_MODIFY &&
3024 status != GOT_STATUS_MISSING) {
3025 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3026 goto done;
3030 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3031 if (dirfd != -1) {
3032 if (unlinkat(dirfd, de_name, 0) != 0) {
3033 err = got_error_from_errno2("unlinkat",
3034 ondisk_path);
3035 goto done;
3037 } else if (unlink(ondisk_path) != 0) {
3038 err = got_error_from_errno2("unlink", ondisk_path);
3039 goto done;
3043 got_fileindex_entry_mark_deleted_from_disk(ie);
3044 done:
3045 free(ondisk_path);
3046 if (err)
3047 return err;
3048 if (status == GOT_STATUS_DELETE)
3049 return NULL;
3050 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3051 staged_status, relpath);
3054 const struct got_error *
3055 got_worktree_schedule_delete(struct got_worktree *worktree,
3056 struct got_pathlist_head *paths, int delete_local_mods,
3057 got_worktree_delete_cb progress_cb, void *progress_arg,
3058 struct got_repository *repo, int keep_on_disk)
3060 struct got_fileindex *fileindex = NULL;
3061 char *fileindex_path = NULL;
3062 const struct got_error *err = NULL, *sync_err, *unlockerr;
3063 struct got_pathlist_entry *pe;
3064 struct schedule_deletion_args sda;
3066 err = lock_worktree(worktree, LOCK_EX);
3067 if (err)
3068 return err;
3070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3071 if (err)
3072 goto done;
3074 sda.worktree = worktree;
3075 sda.fileindex = fileindex;
3076 sda.progress_cb = progress_cb;
3077 sda.progress_arg = progress_arg;
3078 sda.repo = repo;
3079 sda.delete_local_mods = delete_local_mods;
3080 sda.keep_on_disk = keep_on_disk;
3082 TAILQ_FOREACH(pe, paths, entry) {
3083 err = worktree_status(worktree, pe->path, fileindex, repo,
3084 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3085 if (err)
3086 break;
3088 sync_err = sync_fileindex(fileindex, fileindex_path);
3089 if (sync_err && err == NULL)
3090 err = sync_err;
3091 done:
3092 free(fileindex_path);
3093 if (fileindex)
3094 got_fileindex_free(fileindex);
3095 unlockerr = lock_worktree(worktree, LOCK_SH);
3096 if (unlockerr && err == NULL)
3097 err = unlockerr;
3098 return err;
3101 static const struct got_error *
3102 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3104 const struct got_error *err = NULL;
3105 char *line = NULL;
3106 size_t linesize = 0, n;
3107 ssize_t linelen;
3109 linelen = getline(&line, &linesize, infile);
3110 if (linelen == -1) {
3111 if (ferror(infile)) {
3112 err = got_error_from_errno("getline");
3113 goto done;
3115 return NULL;
3117 if (outfile) {
3118 n = fwrite(line, 1, linelen, outfile);
3119 if (n != linelen) {
3120 err = got_ferror(outfile, GOT_ERR_IO);
3121 goto done;
3124 if (rejectfile) {
3125 n = fwrite(line, 1, linelen, rejectfile);
3126 if (n != linelen)
3127 err = got_ferror(outfile, GOT_ERR_IO);
3129 done:
3130 free(line);
3131 return err;
3134 static const struct got_error *
3135 skip_one_line(FILE *f)
3137 char *line = NULL;
3138 size_t linesize = 0;
3139 ssize_t linelen;
3141 linelen = getline(&line, &linesize, f);
3142 if (linelen == -1) {
3143 if (ferror(f))
3144 return got_error_from_errno("getline");
3145 return NULL;
3147 free(line);
3148 return NULL;
3151 static const struct got_error *
3152 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3153 int start_old, int end_old, int start_new, int end_new,
3154 FILE *outfile, FILE *rejectfile)
3156 const struct got_error *err;
3158 /* Copy old file's lines leading up to patch. */
3159 while (!feof(f1) && *line_cur1 < start_old) {
3160 err = copy_one_line(f1, outfile, NULL);
3161 if (err)
3162 return err;
3163 (*line_cur1)++;
3165 /* Skip new file's lines leading up to patch. */
3166 while (!feof(f2) && *line_cur2 < start_new) {
3167 if (rejectfile)
3168 err = copy_one_line(f2, NULL, rejectfile);
3169 else
3170 err = skip_one_line(f2);
3171 if (err)
3172 return err;
3173 (*line_cur2)++;
3175 /* Copy patched lines. */
3176 while (!feof(f2) && *line_cur2 <= end_new) {
3177 err = copy_one_line(f2, outfile, NULL);
3178 if (err)
3179 return err;
3180 (*line_cur2)++;
3182 /* Skip over old file's replaced lines. */
3183 while (!feof(f1) && *line_cur1 <= end_old) {
3184 if (rejectfile)
3185 err = copy_one_line(f1, NULL, rejectfile);
3186 else
3187 err = skip_one_line(f1);
3188 if (err)
3189 return err;
3190 (*line_cur1)++;
3193 return NULL;
3196 static const struct got_error *
3197 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3198 FILE *outfile, FILE *rejectfile)
3200 const struct got_error *err;
3202 if (outfile) {
3203 /* Copy old file's lines until EOF. */
3204 while (!feof(f1)) {
3205 err = copy_one_line(f1, outfile, NULL);
3206 if (err)
3207 return err;
3208 (*line_cur1)++;
3211 if (rejectfile) {
3212 /* Copy new file's lines until EOF. */
3213 while (!feof(f2)) {
3214 err = copy_one_line(f2, NULL, rejectfile);
3215 if (err)
3216 return err;
3217 (*line_cur2)++;
3221 return NULL;
3224 static const struct got_error *
3225 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3226 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3227 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3228 int *line_cur2, FILE *outfile, FILE *rejectfile,
3229 got_worktree_patch_cb patch_cb, void *patch_arg)
3231 const struct got_error *err = NULL;
3232 int start_old = change->cv.a;
3233 int end_old = change->cv.b;
3234 int start_new = change->cv.c;
3235 int end_new = change->cv.d;
3236 long pos1, pos2;
3237 FILE *hunkfile;
3239 *choice = GOT_PATCH_CHOICE_NONE;
3241 hunkfile = got_opentemp();
3242 if (hunkfile == NULL)
3243 return got_error_from_errno("got_opentemp");
3245 pos1 = ftell(f1);
3246 pos2 = ftell(f2);
3248 /* XXX TODO needs error checking */
3249 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3251 if (fseek(f1, pos1, SEEK_SET) == -1) {
3252 err = got_ferror(f1, GOT_ERR_IO);
3253 goto done;
3255 if (fseek(f2, pos2, SEEK_SET) == -1) {
3256 err = got_ferror(f1, GOT_ERR_IO);
3257 goto done;
3259 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3260 err = got_ferror(hunkfile, GOT_ERR_IO);
3261 goto done;
3264 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3265 hunkfile, n, nchanges);
3266 if (err)
3267 goto done;
3269 switch (*choice) {
3270 case GOT_PATCH_CHOICE_YES:
3271 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3272 end_old, start_new, end_new, outfile, rejectfile);
3273 break;
3274 case GOT_PATCH_CHOICE_NO:
3275 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3276 end_old, start_new, end_new, rejectfile, outfile);
3277 break;
3278 case GOT_PATCH_CHOICE_QUIT:
3279 break;
3280 default:
3281 err = got_error(GOT_ERR_PATCH_CHOICE);
3282 break;
3284 done:
3285 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3286 err = got_error_from_errno("fclose");
3287 return err;
3290 struct revert_file_args {
3291 struct got_worktree *worktree;
3292 struct got_fileindex *fileindex;
3293 got_worktree_checkout_cb progress_cb;
3294 void *progress_arg;
3295 got_worktree_patch_cb patch_cb;
3296 void *patch_arg;
3297 struct got_repository *repo;
3300 static const struct got_error *
3301 create_patched_content(char **path_outfile, int reverse_patch,
3302 struct got_object_id *blob_id, const char *path2,
3303 int dirfd2, const char *de_name2,
3304 const char *relpath, struct got_repository *repo,
3305 got_worktree_patch_cb patch_cb, void *patch_arg)
3307 const struct got_error *err;
3308 struct got_blob_object *blob = NULL;
3309 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3310 int fd2 = -1;
3311 char *path1 = NULL, *id_str = NULL;
3312 struct stat sb1, sb2;
3313 struct got_diff_changes *changes = NULL;
3314 struct got_diff_state *ds = NULL;
3315 struct got_diff_args *args = NULL;
3316 struct got_diff_change *change;
3317 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3318 int n = 0;
3320 *path_outfile = NULL;
3322 err = got_object_id_str(&id_str, blob_id);
3323 if (err)
3324 return err;
3326 if (dirfd2 != -1) {
3327 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3328 if (fd2 == -1) {
3329 err = got_error_from_errno2("openat", path2);
3330 goto done;
3332 } else {
3333 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3334 if (fd2 == -1) {
3335 err = got_error_from_errno2("open", path2);
3336 goto done;
3339 if (fstat(fd2, &sb2) == -1) {
3340 err = got_error_from_errno2("fstat", path2);
3341 goto done;
3344 f2 = fdopen(fd2, "r");
3345 if (f2 == NULL) {
3346 err = got_error_from_errno2("fdopen", path2);
3347 goto done;
3349 fd2 = -1;
3351 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3352 if (err)
3353 goto done;
3355 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3356 if (err)
3357 goto done;
3359 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3360 if (err)
3361 goto done;
3363 if (stat(path1, &sb1) == -1) {
3364 err = got_error_from_errno2("stat", path1);
3365 goto done;
3368 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3369 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3370 if (err)
3371 goto done;
3373 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3374 if (err)
3375 goto done;
3377 if (fseek(f1, 0L, SEEK_SET) == -1)
3378 return got_ferror(f1, GOT_ERR_IO);
3379 if (fseek(f2, 0L, SEEK_SET) == -1)
3380 return got_ferror(f2, GOT_ERR_IO);
3381 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3382 int choice;
3383 err = apply_or_reject_change(&choice, change, ++n,
3384 changes->nchanges, ds, args, diff_flags, relpath,
3385 f1, f2, &line_cur1, &line_cur2,
3386 reverse_patch ? NULL : outfile,
3387 reverse_patch ? outfile : NULL,
3388 patch_cb, patch_arg);
3389 if (err)
3390 goto done;
3391 if (choice == GOT_PATCH_CHOICE_YES)
3392 have_content = 1;
3393 else if (choice == GOT_PATCH_CHOICE_QUIT)
3394 break;
3396 if (have_content) {
3397 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3398 reverse_patch ? NULL : outfile,
3399 reverse_patch ? outfile : NULL);
3400 if (err)
3401 goto done;
3403 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3404 err = got_error_from_errno2("chmod", path2);
3405 goto done;
3408 done:
3409 free(id_str);
3410 if (blob)
3411 got_object_blob_close(blob);
3412 if (f1 && fclose(f1) == EOF && err == NULL)
3413 err = got_error_from_errno2("fclose", path1);
3414 if (f2 && fclose(f2) == EOF && err == NULL)
3415 err = got_error_from_errno2("fclose", path2);
3416 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3417 err = got_error_from_errno2("close", path2);
3418 if (outfile && fclose(outfile) == EOF && err == NULL)
3419 err = got_error_from_errno2("fclose", *path_outfile);
3420 if (path1 && unlink(path1) == -1 && err == NULL)
3421 err = got_error_from_errno2("unlink", path1);
3422 if (err || !have_content) {
3423 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3424 err = got_error_from_errno2("unlink", *path_outfile);
3425 free(*path_outfile);
3426 *path_outfile = NULL;
3428 free(args);
3429 if (ds) {
3430 got_diff_state_free(ds);
3431 free(ds);
3433 if (changes)
3434 got_diff_free_changes(changes);
3435 free(path1);
3436 return err;
3439 static const struct got_error *
3440 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3441 const char *relpath, struct got_object_id *blob_id,
3442 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3443 int dirfd, const char *de_name)
3445 struct revert_file_args *a = arg;
3446 const struct got_error *err = NULL;
3447 char *parent_path = NULL;
3448 struct got_fileindex_entry *ie;
3449 struct got_tree_object *tree = NULL;
3450 struct got_object_id *tree_id = NULL;
3451 const struct got_tree_entry *te = NULL;
3452 char *tree_path = NULL, *te_name;
3453 char *ondisk_path = NULL, *path_content = NULL;
3454 struct got_blob_object *blob = NULL;
3456 /* Reverting a staged deletion is a no-op. */
3457 if (status == GOT_STATUS_DELETE &&
3458 staged_status != GOT_STATUS_NO_CHANGE)
3459 return NULL;
3461 if (status == GOT_STATUS_UNVERSIONED)
3462 return (*a->progress_cb)(a->progress_arg,
3463 GOT_STATUS_UNVERSIONED, relpath);
3465 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3466 if (ie == NULL)
3467 return got_error(GOT_ERR_BAD_PATH);
3469 /* Construct in-repository path of tree which contains this blob. */
3470 err = got_path_dirname(&parent_path, ie->path);
3471 if (err) {
3472 if (err->code != GOT_ERR_BAD_PATH)
3473 goto done;
3474 parent_path = strdup("/");
3475 if (parent_path == NULL) {
3476 err = got_error_from_errno("strdup");
3477 goto done;
3480 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3481 tree_path = strdup(parent_path);
3482 if (tree_path == NULL) {
3483 err = got_error_from_errno("strdup");
3484 goto done;
3486 } else {
3487 if (got_path_is_root_dir(parent_path)) {
3488 tree_path = strdup(a->worktree->path_prefix);
3489 if (tree_path == NULL) {
3490 err = got_error_from_errno("strdup");
3491 goto done;
3493 } else {
3494 if (asprintf(&tree_path, "%s/%s",
3495 a->worktree->path_prefix, parent_path) == -1) {
3496 err = got_error_from_errno("asprintf");
3497 goto done;
3502 err = got_object_id_by_path(&tree_id, a->repo,
3503 a->worktree->base_commit_id, tree_path);
3504 if (err) {
3505 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3506 (status == GOT_STATUS_ADD ||
3507 staged_status == GOT_STATUS_ADD)))
3508 goto done;
3509 } else {
3510 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3511 if (err)
3512 goto done;
3514 te_name = basename(ie->path);
3515 if (te_name == NULL) {
3516 err = got_error_from_errno2("basename", ie->path);
3517 goto done;
3520 te = got_object_tree_find_entry(tree, te_name);
3521 if (te == NULL && status != GOT_STATUS_ADD &&
3522 staged_status != GOT_STATUS_ADD) {
3523 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3524 goto done;
3528 switch (status) {
3529 case GOT_STATUS_ADD:
3530 if (a->patch_cb) {
3531 int choice = GOT_PATCH_CHOICE_NONE;
3532 err = (*a->patch_cb)(&choice, a->patch_arg,
3533 status, ie->path, NULL, 1, 1);
3534 if (err)
3535 goto done;
3536 if (choice != GOT_PATCH_CHOICE_YES)
3537 break;
3539 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3540 ie->path);
3541 if (err)
3542 goto done;
3543 got_fileindex_entry_remove(a->fileindex, ie);
3544 break;
3545 case GOT_STATUS_DELETE:
3546 if (a->patch_cb) {
3547 int choice = GOT_PATCH_CHOICE_NONE;
3548 err = (*a->patch_cb)(&choice, a->patch_arg,
3549 status, ie->path, NULL, 1, 1);
3550 if (err)
3551 goto done;
3552 if (choice != GOT_PATCH_CHOICE_YES)
3553 break;
3555 /* fall through */
3556 case GOT_STATUS_MODIFY:
3557 case GOT_STATUS_MODE_CHANGE:
3558 case GOT_STATUS_CONFLICT:
3559 case GOT_STATUS_MISSING: {
3560 struct got_object_id id;
3561 if (staged_status == GOT_STATUS_ADD ||
3562 staged_status == GOT_STATUS_MODIFY) {
3563 memcpy(id.sha1, ie->staged_blob_sha1,
3564 SHA1_DIGEST_LENGTH);
3565 } else
3566 memcpy(id.sha1, ie->blob_sha1,
3567 SHA1_DIGEST_LENGTH);
3568 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3569 if (err)
3570 goto done;
3572 if (asprintf(&ondisk_path, "%s/%s",
3573 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3574 err = got_error_from_errno("asprintf");
3575 goto done;
3578 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3579 status == GOT_STATUS_CONFLICT)) {
3580 err = create_patched_content(&path_content, 1, &id,
3581 ondisk_path, dirfd, de_name, ie->path, a->repo,
3582 a->patch_cb, a->patch_arg);
3583 if (err || path_content == NULL)
3584 break;
3585 if (rename(path_content, ondisk_path) == -1) {
3586 err = got_error_from_errno3("rename",
3587 path_content, ondisk_path);
3588 goto done;
3590 } else {
3591 err = install_blob(a->worktree, ondisk_path, ie->path,
3592 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3593 got_fileindex_perms_to_st(ie), blob, 0, 1,
3594 a->repo, a->progress_cb, a->progress_arg);
3595 if (err)
3596 goto done;
3597 if (status == GOT_STATUS_DELETE ||
3598 status == GOT_STATUS_MODE_CHANGE) {
3599 err = update_blob_fileindex_entry(a->worktree,
3600 a->fileindex, ie, ondisk_path, ie->path,
3601 blob, 1);
3602 if (err)
3603 goto done;
3606 break;
3608 default:
3609 break;
3611 done:
3612 free(ondisk_path);
3613 free(path_content);
3614 free(parent_path);
3615 free(tree_path);
3616 if (blob)
3617 got_object_blob_close(blob);
3618 if (tree)
3619 got_object_tree_close(tree);
3620 free(tree_id);
3621 return err;
3624 const struct got_error *
3625 got_worktree_revert(struct got_worktree *worktree,
3626 struct got_pathlist_head *paths,
3627 got_worktree_checkout_cb progress_cb, void *progress_arg,
3628 got_worktree_patch_cb patch_cb, void *patch_arg,
3629 struct got_repository *repo)
3631 struct got_fileindex *fileindex = NULL;
3632 char *fileindex_path = NULL;
3633 const struct got_error *err = NULL, *unlockerr = NULL;
3634 const struct got_error *sync_err = NULL;
3635 struct got_pathlist_entry *pe;
3636 struct revert_file_args rfa;
3638 err = lock_worktree(worktree, LOCK_EX);
3639 if (err)
3640 return err;
3642 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3643 if (err)
3644 goto done;
3646 rfa.worktree = worktree;
3647 rfa.fileindex = fileindex;
3648 rfa.progress_cb = progress_cb;
3649 rfa.progress_arg = progress_arg;
3650 rfa.patch_cb = patch_cb;
3651 rfa.patch_arg = patch_arg;
3652 rfa.repo = repo;
3653 TAILQ_FOREACH(pe, paths, entry) {
3654 err = worktree_status(worktree, pe->path, fileindex, repo,
3655 revert_file, &rfa, NULL, NULL, 0, 0);
3656 if (err)
3657 break;
3659 sync_err = sync_fileindex(fileindex, fileindex_path);
3660 if (sync_err && err == NULL)
3661 err = sync_err;
3662 done:
3663 free(fileindex_path);
3664 if (fileindex)
3665 got_fileindex_free(fileindex);
3666 unlockerr = lock_worktree(worktree, LOCK_SH);
3667 if (unlockerr && err == NULL)
3668 err = unlockerr;
3669 return err;
3672 static void
3673 free_commitable(struct got_commitable *ct)
3675 free(ct->path);
3676 free(ct->in_repo_path);
3677 free(ct->ondisk_path);
3678 free(ct->blob_id);
3679 free(ct->base_blob_id);
3680 free(ct->staged_blob_id);
3681 free(ct->base_commit_id);
3682 free(ct);
3685 struct collect_commitables_arg {
3686 struct got_pathlist_head *commitable_paths;
3687 struct got_repository *repo;
3688 struct got_worktree *worktree;
3689 int have_staged_files;
3692 static const struct got_error *
3693 collect_commitables(void *arg, unsigned char status,
3694 unsigned char staged_status, const char *relpath,
3695 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3696 struct got_object_id *commit_id, int dirfd, const char *de_name)
3698 struct collect_commitables_arg *a = arg;
3699 const struct got_error *err = NULL;
3700 struct got_commitable *ct = NULL;
3701 struct got_pathlist_entry *new = NULL;
3702 char *parent_path = NULL, *path = NULL;
3703 struct stat sb;
3705 if (a->have_staged_files) {
3706 if (staged_status != GOT_STATUS_MODIFY &&
3707 staged_status != GOT_STATUS_ADD &&
3708 staged_status != GOT_STATUS_DELETE)
3709 return NULL;
3710 } else {
3711 if (status == GOT_STATUS_CONFLICT)
3712 return got_error(GOT_ERR_COMMIT_CONFLICT);
3714 if (status != GOT_STATUS_MODIFY &&
3715 status != GOT_STATUS_MODE_CHANGE &&
3716 status != GOT_STATUS_ADD &&
3717 status != GOT_STATUS_DELETE)
3718 return NULL;
3721 if (asprintf(&path, "/%s", relpath) == -1) {
3722 err = got_error_from_errno("asprintf");
3723 goto done;
3725 if (strcmp(path, "/") == 0) {
3726 parent_path = strdup("");
3727 if (parent_path == NULL)
3728 return got_error_from_errno("strdup");
3729 } else {
3730 err = got_path_dirname(&parent_path, path);
3731 if (err)
3732 return err;
3735 ct = calloc(1, sizeof(*ct));
3736 if (ct == NULL) {
3737 err = got_error_from_errno("calloc");
3738 goto done;
3741 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3742 relpath) == -1) {
3743 err = got_error_from_errno("asprintf");
3744 goto done;
3746 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3747 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3748 } else {
3749 if (dirfd != -1) {
3750 if (fstatat(dirfd, de_name, &sb,
3751 AT_SYMLINK_NOFOLLOW) == -1) {
3752 err = got_error_from_errno2("fstatat",
3753 ct->ondisk_path);
3754 goto done;
3756 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3757 err = got_error_from_errno2("lstat", ct->ondisk_path);
3758 goto done;
3760 ct->mode = sb.st_mode;
3763 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3764 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3765 relpath) == -1) {
3766 err = got_error_from_errno("asprintf");
3767 goto done;
3770 ct->status = status;
3771 ct->staged_status = staged_status;
3772 ct->blob_id = NULL; /* will be filled in when blob gets created */
3773 if (ct->status != GOT_STATUS_ADD &&
3774 ct->staged_status != GOT_STATUS_ADD) {
3775 ct->base_blob_id = got_object_id_dup(blob_id);
3776 if (ct->base_blob_id == NULL) {
3777 err = got_error_from_errno("got_object_id_dup");
3778 goto done;
3780 ct->base_commit_id = got_object_id_dup(commit_id);
3781 if (ct->base_commit_id == NULL) {
3782 err = got_error_from_errno("got_object_id_dup");
3783 goto done;
3786 if (ct->staged_status == GOT_STATUS_ADD ||
3787 ct->staged_status == GOT_STATUS_MODIFY) {
3788 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3789 if (ct->staged_blob_id == NULL) {
3790 err = got_error_from_errno("got_object_id_dup");
3791 goto done;
3794 ct->path = strdup(path);
3795 if (ct->path == NULL) {
3796 err = got_error_from_errno("strdup");
3797 goto done;
3799 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3800 done:
3801 if (ct && (err || new == NULL))
3802 free_commitable(ct);
3803 free(parent_path);
3804 free(path);
3805 return err;
3808 static const struct got_error *write_tree(struct got_object_id **,
3809 struct got_tree_object *, const char *, struct got_pathlist_head *,
3810 got_worktree_status_cb status_cb, void *status_arg,
3811 struct got_repository *);
3813 static const struct got_error *
3814 write_subtree(struct got_object_id **new_subtree_id,
3815 struct got_tree_entry *te, const char *parent_path,
3816 struct got_pathlist_head *commitable_paths,
3817 got_worktree_status_cb status_cb, void *status_arg,
3818 struct got_repository *repo)
3820 const struct got_error *err = NULL;
3821 struct got_tree_object *subtree;
3822 char *subpath;
3824 if (asprintf(&subpath, "%s%s%s", parent_path,
3825 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3826 return got_error_from_errno("asprintf");
3828 err = got_object_open_as_tree(&subtree, repo, &te->id);
3829 if (err)
3830 return err;
3832 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3833 status_cb, status_arg, repo);
3834 got_object_tree_close(subtree);
3835 free(subpath);
3836 return err;
3839 static const struct got_error *
3840 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3842 const struct got_error *err = NULL;
3843 char *ct_parent_path = NULL;
3845 *match = 0;
3847 if (strchr(ct->in_repo_path, '/') == NULL) {
3848 *match = got_path_is_root_dir(path);
3849 return NULL;
3852 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3853 if (err)
3854 return err;
3855 *match = (strcmp(path, ct_parent_path) == 0);
3856 free(ct_parent_path);
3857 return err;
3860 static mode_t
3861 get_ct_file_mode(struct got_commitable *ct)
3863 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3866 static const struct got_error *
3867 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3868 struct got_tree_entry *te, struct got_commitable *ct)
3870 const struct got_error *err = NULL;
3872 *new_te = NULL;
3874 err = got_object_tree_entry_dup(new_te, te);
3875 if (err)
3876 goto done;
3878 (*new_te)->mode = get_ct_file_mode(ct);
3880 if (ct->staged_status == GOT_STATUS_MODIFY)
3881 memcpy(&(*new_te)->id, ct->staged_blob_id,
3882 sizeof((*new_te)->id));
3883 else
3884 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3885 done:
3886 if (err && *new_te) {
3887 free(*new_te);
3888 *new_te = NULL;
3890 return err;
3893 static const struct got_error *
3894 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3895 struct got_commitable *ct)
3897 const struct got_error *err = NULL;
3898 char *ct_name;
3900 *new_te = NULL;
3902 *new_te = calloc(1, sizeof(**new_te));
3903 if (*new_te == NULL)
3904 return got_error_from_errno("calloc");
3906 ct_name = basename(ct->path);
3907 if (ct_name == NULL) {
3908 err = got_error_from_errno2("basename", ct->path);
3909 goto done;
3911 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3912 sizeof((*new_te)->name)) {
3913 err = got_error(GOT_ERR_NO_SPACE);
3914 goto done;
3917 (*new_te)->mode = get_ct_file_mode(ct);
3919 if (ct->staged_status == GOT_STATUS_ADD)
3920 memcpy(&(*new_te)->id, ct->staged_blob_id,
3921 sizeof((*new_te)->id));
3922 else
3923 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3924 done:
3925 if (err && *new_te) {
3926 free(*new_te);
3927 *new_te = NULL;
3929 return err;
3932 static const struct got_error *
3933 insert_tree_entry(struct got_tree_entry *new_te,
3934 struct got_pathlist_head *paths)
3936 const struct got_error *err = NULL;
3937 struct got_pathlist_entry *new_pe;
3939 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3940 if (err)
3941 return err;
3942 if (new_pe == NULL)
3943 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3944 return NULL;
3947 static const struct got_error *
3948 report_ct_status(struct got_commitable *ct,
3949 got_worktree_status_cb status_cb, void *status_arg)
3951 const char *ct_path = ct->path;
3952 unsigned char status;
3954 while (ct_path[0] == '/')
3955 ct_path++;
3957 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3958 status = ct->staged_status;
3959 else
3960 status = ct->status;
3962 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3963 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3966 static const struct got_error *
3967 match_modified_subtree(int *modified, struct got_tree_entry *te,
3968 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3970 const struct got_error *err = NULL;
3971 struct got_pathlist_entry *pe;
3972 char *te_path;
3974 *modified = 0;
3976 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3977 got_path_is_root_dir(base_tree_path) ? "" : "/",
3978 te->name) == -1)
3979 return got_error_from_errno("asprintf");
3981 TAILQ_FOREACH(pe, commitable_paths, entry) {
3982 struct got_commitable *ct = pe->data;
3983 *modified = got_path_is_child(ct->in_repo_path, te_path,
3984 strlen(te_path));
3985 if (*modified)
3986 break;
3989 free(te_path);
3990 return err;
3993 static const struct got_error *
3994 match_deleted_or_modified_ct(struct got_commitable **ctp,
3995 struct got_tree_entry *te, const char *base_tree_path,
3996 struct got_pathlist_head *commitable_paths)
3998 const struct got_error *err = NULL;
3999 struct got_pathlist_entry *pe;
4001 *ctp = NULL;
4003 TAILQ_FOREACH(pe, commitable_paths, entry) {
4004 struct got_commitable *ct = pe->data;
4005 char *ct_name = NULL;
4006 int path_matches;
4008 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4009 if (ct->status != GOT_STATUS_MODIFY &&
4010 ct->status != GOT_STATUS_MODE_CHANGE &&
4011 ct->status != GOT_STATUS_DELETE)
4012 continue;
4013 } else {
4014 if (ct->staged_status != GOT_STATUS_MODIFY &&
4015 ct->staged_status != GOT_STATUS_DELETE)
4016 continue;
4019 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4020 continue;
4022 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4023 if (err)
4024 return err;
4025 if (!path_matches)
4026 continue;
4028 ct_name = basename(pe->path);
4029 if (ct_name == NULL)
4030 return got_error_from_errno2("basename", pe->path);
4032 if (strcmp(te->name, ct_name) != 0)
4033 continue;
4035 *ctp = ct;
4036 break;
4039 return err;
4042 static const struct got_error *
4043 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4044 const char *child_path, const char *path_base_tree,
4045 struct got_pathlist_head *commitable_paths,
4046 got_worktree_status_cb status_cb, void *status_arg,
4047 struct got_repository *repo)
4049 const struct got_error *err = NULL;
4050 struct got_tree_entry *new_te;
4051 char *subtree_path;
4052 struct got_object_id *id = NULL;
4054 *new_tep = NULL;
4056 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4057 got_path_is_root_dir(path_base_tree) ? "" : "/",
4058 child_path) == -1)
4059 return got_error_from_errno("asprintf");
4061 new_te = calloc(1, sizeof(*new_te));
4062 if (new_te == NULL)
4063 return got_error_from_errno("calloc");
4064 new_te->mode = S_IFDIR;
4066 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4067 sizeof(new_te->name)) {
4068 err = got_error(GOT_ERR_NO_SPACE);
4069 goto done;
4071 err = write_tree(&id, NULL, subtree_path,
4072 commitable_paths, status_cb, status_arg, repo);
4073 if (err) {
4074 free(new_te);
4075 goto done;
4077 memcpy(&new_te->id, id, sizeof(new_te->id));
4078 done:
4079 free(id);
4080 free(subtree_path);
4081 if (err == NULL)
4082 *new_tep = new_te;
4083 return err;
4086 static const struct got_error *
4087 write_tree(struct got_object_id **new_tree_id,
4088 struct got_tree_object *base_tree, const char *path_base_tree,
4089 struct got_pathlist_head *commitable_paths,
4090 got_worktree_status_cb status_cb, void *status_arg,
4091 struct got_repository *repo)
4093 const struct got_error *err = NULL;
4094 struct got_pathlist_head paths;
4095 struct got_tree_entry *te, *new_te = NULL;
4096 struct got_pathlist_entry *pe;
4097 int nentries = 0;
4099 TAILQ_INIT(&paths);
4101 /* Insert, and recurse into, newly added entries first. */
4102 TAILQ_FOREACH(pe, commitable_paths, entry) {
4103 struct got_commitable *ct = pe->data;
4104 char *child_path = NULL, *slash;
4106 if ((ct->status != GOT_STATUS_ADD &&
4107 ct->staged_status != GOT_STATUS_ADD) ||
4108 (ct->flags & GOT_COMMITABLE_ADDED))
4109 continue;
4111 if (!got_path_is_child(pe->path, path_base_tree,
4112 strlen(path_base_tree)))
4113 continue;
4115 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4116 pe->path);
4117 if (err)
4118 goto done;
4120 slash = strchr(child_path, '/');
4121 if (slash == NULL) {
4122 err = alloc_added_blob_tree_entry(&new_te, ct);
4123 if (err)
4124 goto done;
4125 err = report_ct_status(ct, status_cb, status_arg);
4126 if (err)
4127 goto done;
4128 ct->flags |= GOT_COMMITABLE_ADDED;
4129 err = insert_tree_entry(new_te, &paths);
4130 if (err)
4131 goto done;
4132 nentries++;
4133 } else {
4134 *slash = '\0'; /* trim trailing path components */
4135 if (base_tree == NULL ||
4136 got_object_tree_find_entry(base_tree, child_path)
4137 == NULL) {
4138 err = make_subtree_for_added_blob(&new_te,
4139 child_path, path_base_tree,
4140 commitable_paths, status_cb, status_arg,
4141 repo);
4142 if (err)
4143 goto done;
4144 err = insert_tree_entry(new_te, &paths);
4145 if (err)
4146 goto done;
4147 nentries++;
4152 if (base_tree) {
4153 int i, nbase_entries;
4154 /* Handle modified and deleted entries. */
4155 nbase_entries = got_object_tree_get_nentries(base_tree);
4156 for (i = 0; i < nbase_entries; i++) {
4157 struct got_commitable *ct = NULL;
4159 te = got_object_tree_get_entry(base_tree, i);
4160 if (got_object_tree_entry_is_submodule(te)) {
4161 /* Entry is a submodule; just copy it. */
4162 err = got_object_tree_entry_dup(&new_te, te);
4163 if (err)
4164 goto done;
4165 err = insert_tree_entry(new_te, &paths);
4166 if (err)
4167 goto done;
4168 nentries++;
4169 continue;
4172 if (S_ISDIR(te->mode)) {
4173 int modified;
4174 err = got_object_tree_entry_dup(&new_te, te);
4175 if (err)
4176 goto done;
4177 err = match_modified_subtree(&modified, te,
4178 path_base_tree, commitable_paths);
4179 if (err)
4180 goto done;
4181 /* Avoid recursion into unmodified subtrees. */
4182 if (modified) {
4183 struct got_object_id *new_id;
4184 err = write_subtree(&new_id, te,
4185 path_base_tree, commitable_paths,
4186 status_cb, status_arg, repo);
4187 if (err)
4188 goto done;
4189 memcpy(&new_te->id, new_id,
4190 sizeof(new_te->id));
4191 free(new_id);
4193 err = insert_tree_entry(new_te, &paths);
4194 if (err)
4195 goto done;
4196 nentries++;
4197 continue;
4200 err = match_deleted_or_modified_ct(&ct, te,
4201 path_base_tree, commitable_paths);
4202 if (err)
4203 goto done;
4204 if (ct) {
4205 /* NB: Deleted entries get dropped here. */
4206 if (ct->status == GOT_STATUS_MODIFY ||
4207 ct->status == GOT_STATUS_MODE_CHANGE ||
4208 ct->staged_status == GOT_STATUS_MODIFY) {
4209 err = alloc_modified_blob_tree_entry(
4210 &new_te, te, ct);
4211 if (err)
4212 goto done;
4213 err = insert_tree_entry(new_te, &paths);
4214 if (err)
4215 goto done;
4216 nentries++;
4218 err = report_ct_status(ct, status_cb,
4219 status_arg);
4220 if (err)
4221 goto done;
4222 } else {
4223 /* Entry is unchanged; just copy it. */
4224 err = got_object_tree_entry_dup(&new_te, te);
4225 if (err)
4226 goto done;
4227 err = insert_tree_entry(new_te, &paths);
4228 if (err)
4229 goto done;
4230 nentries++;
4235 /* Write new list of entries; deleted entries have been dropped. */
4236 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4237 done:
4238 got_pathlist_free(&paths);
4239 return err;
4242 static const struct got_error *
4243 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4244 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4245 int have_staged_files)
4247 const struct got_error *err = NULL;
4248 struct got_pathlist_entry *pe;
4250 TAILQ_FOREACH(pe, commitable_paths, entry) {
4251 struct got_fileindex_entry *ie;
4252 struct got_commitable *ct = pe->data;
4254 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4255 if (ie) {
4256 if (ct->status == GOT_STATUS_DELETE ||
4257 ct->staged_status == GOT_STATUS_DELETE) {
4258 got_fileindex_entry_remove(fileindex, ie);
4259 } else if (ct->staged_status == GOT_STATUS_ADD ||
4260 ct->staged_status == GOT_STATUS_MODIFY) {
4261 got_fileindex_entry_stage_set(ie,
4262 GOT_FILEIDX_STAGE_NONE);
4263 err = got_fileindex_entry_update(ie,
4264 ct->ondisk_path, ct->staged_blob_id->sha1,
4265 new_base_commit_id->sha1,
4266 !have_staged_files);
4267 } else
4268 err = got_fileindex_entry_update(ie,
4269 ct->ondisk_path, ct->blob_id->sha1,
4270 new_base_commit_id->sha1,
4271 !have_staged_files);
4272 } else {
4273 err = got_fileindex_entry_alloc(&ie,
4274 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4275 new_base_commit_id->sha1);
4276 if (err)
4277 break;
4278 err = got_fileindex_entry_add(fileindex, ie);
4279 if (err)
4280 break;
4283 return err;
4287 static const struct got_error *
4288 check_out_of_date(const char *in_repo_path, unsigned char status,
4289 unsigned char staged_status, struct got_object_id *base_blob_id,
4290 struct got_object_id *base_commit_id,
4291 struct got_object_id *head_commit_id, struct got_repository *repo,
4292 int ood_errcode)
4294 const struct got_error *err = NULL;
4295 struct got_object_id *id = NULL;
4297 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4298 /* Trivial case: base commit == head commit */
4299 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4300 return NULL;
4302 * Ensure file content which local changes were based
4303 * on matches file content in the branch head.
4305 err = got_object_id_by_path(&id, repo, head_commit_id,
4306 in_repo_path);
4307 if (err) {
4308 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4309 err = got_error(ood_errcode);
4310 goto done;
4311 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4312 err = got_error(ood_errcode);
4313 } else {
4314 /* Require that added files don't exist in the branch head. */
4315 err = got_object_id_by_path(&id, repo, head_commit_id,
4316 in_repo_path);
4317 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4318 goto done;
4319 err = id ? got_error(ood_errcode) : NULL;
4321 done:
4322 free(id);
4323 return err;
4326 const struct got_error *
4327 commit_worktree(struct got_object_id **new_commit_id,
4328 struct got_pathlist_head *commitable_paths,
4329 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4330 const char *author, const char *committer,
4331 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4332 got_worktree_status_cb status_cb, void *status_arg,
4333 struct got_repository *repo)
4335 const struct got_error *err = NULL, *unlockerr = NULL;
4336 struct got_pathlist_entry *pe;
4337 const char *head_ref_name = NULL;
4338 struct got_commit_object *head_commit = NULL;
4339 struct got_reference *head_ref2 = NULL;
4340 struct got_object_id *head_commit_id2 = NULL;
4341 struct got_tree_object *head_tree = NULL;
4342 struct got_object_id *new_tree_id = NULL;
4343 struct got_object_id_queue parent_ids;
4344 struct got_object_qid *pid = NULL;
4345 char *logmsg = NULL;
4347 *new_commit_id = NULL;
4349 SIMPLEQ_INIT(&parent_ids);
4351 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4352 if (err)
4353 goto done;
4355 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4356 if (err)
4357 goto done;
4359 if (commit_msg_cb != NULL) {
4360 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4361 if (err)
4362 goto done;
4365 if (logmsg == NULL || strlen(logmsg) == 0) {
4366 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4367 goto done;
4370 /* Create blobs from added and modified files and record their IDs. */
4371 TAILQ_FOREACH(pe, commitable_paths, entry) {
4372 struct got_commitable *ct = pe->data;
4373 char *ondisk_path;
4375 /* Blobs for staged files already exist. */
4376 if (ct->staged_status == GOT_STATUS_ADD ||
4377 ct->staged_status == GOT_STATUS_MODIFY)
4378 continue;
4380 if (ct->status != GOT_STATUS_ADD &&
4381 ct->status != GOT_STATUS_MODIFY &&
4382 ct->status != GOT_STATUS_MODE_CHANGE)
4383 continue;
4385 if (asprintf(&ondisk_path, "%s/%s",
4386 worktree->root_path, pe->path) == -1) {
4387 err = got_error_from_errno("asprintf");
4388 goto done;
4390 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4391 free(ondisk_path);
4392 if (err)
4393 goto done;
4396 /* Recursively write new tree objects. */
4397 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4398 status_cb, status_arg, repo);
4399 if (err)
4400 goto done;
4402 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4403 if (err)
4404 goto done;
4405 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4406 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4407 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4408 got_object_qid_free(pid);
4409 if (logmsg != NULL)
4410 free(logmsg);
4411 if (err)
4412 goto done;
4414 /* Check if a concurrent commit to our branch has occurred. */
4415 head_ref_name = got_worktree_get_head_ref_name(worktree);
4416 if (head_ref_name == NULL) {
4417 err = got_error_from_errno("got_worktree_get_head_ref_name");
4418 goto done;
4420 /* Lock the reference here to prevent concurrent modification. */
4421 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4422 if (err)
4423 goto done;
4424 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4425 if (err)
4426 goto done;
4427 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4428 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4429 goto done;
4431 /* Update branch head in repository. */
4432 err = got_ref_change_ref(head_ref2, *new_commit_id);
4433 if (err)
4434 goto done;
4435 err = got_ref_write(head_ref2, repo);
4436 if (err)
4437 goto done;
4439 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4440 if (err)
4441 goto done;
4443 err = ref_base_commit(worktree, repo);
4444 if (err)
4445 goto done;
4446 done:
4447 if (head_tree)
4448 got_object_tree_close(head_tree);
4449 if (head_commit)
4450 got_object_commit_close(head_commit);
4451 free(head_commit_id2);
4452 if (head_ref2) {
4453 unlockerr = got_ref_unlock(head_ref2);
4454 if (unlockerr && err == NULL)
4455 err = unlockerr;
4456 got_ref_close(head_ref2);
4458 return err;
4461 static const struct got_error *
4462 check_path_is_commitable(const char *path,
4463 struct got_pathlist_head *commitable_paths)
4465 struct got_pathlist_entry *cpe = NULL;
4466 size_t path_len = strlen(path);
4468 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4469 struct got_commitable *ct = cpe->data;
4470 const char *ct_path = ct->path;
4472 while (ct_path[0] == '/')
4473 ct_path++;
4475 if (strcmp(path, ct_path) == 0 ||
4476 got_path_is_child(ct_path, path, path_len))
4477 break;
4480 if (cpe == NULL)
4481 return got_error_path(path, GOT_ERR_BAD_PATH);
4483 return NULL;
4486 static const struct got_error *
4487 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4489 int *have_staged_files = arg;
4491 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4492 *have_staged_files = 1;
4493 return got_error(GOT_ERR_CANCELLED);
4496 return NULL;
4499 static const struct got_error *
4500 check_non_staged_files(struct got_fileindex *fileindex,
4501 struct got_pathlist_head *paths)
4503 struct got_pathlist_entry *pe;
4504 struct got_fileindex_entry *ie;
4506 TAILQ_FOREACH(pe, paths, entry) {
4507 if (pe->path[0] == '\0')
4508 continue;
4509 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4510 if (ie == NULL)
4511 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4512 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4513 return got_error_path(pe->path,
4514 GOT_ERR_FILE_NOT_STAGED);
4517 return NULL;
4520 const struct got_error *
4521 got_worktree_commit(struct got_object_id **new_commit_id,
4522 struct got_worktree *worktree, struct got_pathlist_head *paths,
4523 const char *author, const char *committer,
4524 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4525 got_worktree_status_cb status_cb, void *status_arg,
4526 struct got_repository *repo)
4528 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4529 struct got_fileindex *fileindex = NULL;
4530 char *fileindex_path = NULL;
4531 struct got_pathlist_head commitable_paths;
4532 struct collect_commitables_arg cc_arg;
4533 struct got_pathlist_entry *pe;
4534 struct got_reference *head_ref = NULL;
4535 struct got_object_id *head_commit_id = NULL;
4536 int have_staged_files = 0;
4538 *new_commit_id = NULL;
4540 TAILQ_INIT(&commitable_paths);
4542 err = lock_worktree(worktree, LOCK_EX);
4543 if (err)
4544 goto done;
4546 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4547 if (err)
4548 goto done;
4550 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4551 if (err)
4552 goto done;
4554 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4555 if (err)
4556 goto done;
4558 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4559 &have_staged_files);
4560 if (err && err->code != GOT_ERR_CANCELLED)
4561 goto done;
4562 if (have_staged_files) {
4563 err = check_non_staged_files(fileindex, paths);
4564 if (err)
4565 goto done;
4568 cc_arg.commitable_paths = &commitable_paths;
4569 cc_arg.worktree = worktree;
4570 cc_arg.repo = repo;
4571 cc_arg.have_staged_files = have_staged_files;
4572 TAILQ_FOREACH(pe, paths, entry) {
4573 err = worktree_status(worktree, pe->path, fileindex, repo,
4574 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4575 if (err)
4576 goto done;
4579 if (TAILQ_EMPTY(&commitable_paths)) {
4580 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4581 goto done;
4584 TAILQ_FOREACH(pe, paths, entry) {
4585 err = check_path_is_commitable(pe->path, &commitable_paths);
4586 if (err)
4587 goto done;
4590 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4591 struct got_commitable *ct = pe->data;
4592 const char *ct_path = ct->in_repo_path;
4594 while (ct_path[0] == '/')
4595 ct_path++;
4596 err = check_out_of_date(ct_path, ct->status,
4597 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4598 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4599 if (err)
4600 goto done;
4604 err = commit_worktree(new_commit_id, &commitable_paths,
4605 head_commit_id, worktree, author, committer,
4606 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4607 if (err)
4608 goto done;
4610 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4611 fileindex, have_staged_files);
4612 sync_err = sync_fileindex(fileindex, fileindex_path);
4613 if (sync_err && err == NULL)
4614 err = sync_err;
4615 done:
4616 if (fileindex)
4617 got_fileindex_free(fileindex);
4618 free(fileindex_path);
4619 unlockerr = lock_worktree(worktree, LOCK_SH);
4620 if (unlockerr && err == NULL)
4621 err = unlockerr;
4622 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4623 struct got_commitable *ct = pe->data;
4624 free_commitable(ct);
4626 got_pathlist_free(&commitable_paths);
4627 return err;
4630 const char *
4631 got_commitable_get_path(struct got_commitable *ct)
4633 return ct->path;
4636 unsigned int
4637 got_commitable_get_status(struct got_commitable *ct)
4639 return ct->status;
4642 struct check_rebase_ok_arg {
4643 struct got_worktree *worktree;
4644 struct got_repository *repo;
4647 static const struct got_error *
4648 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4650 const struct got_error *err = NULL;
4651 struct check_rebase_ok_arg *a = arg;
4652 unsigned char status;
4653 struct stat sb;
4654 char *ondisk_path;
4656 /* Reject rebase of a work tree with mixed base commits. */
4657 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4658 SHA1_DIGEST_LENGTH))
4659 return got_error(GOT_ERR_MIXED_COMMITS);
4661 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4662 == -1)
4663 return got_error_from_errno("asprintf");
4665 /* Reject rebase of a work tree with modified or staged files. */
4666 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4667 free(ondisk_path);
4668 if (err)
4669 return err;
4671 if (status != GOT_STATUS_NO_CHANGE)
4672 return got_error(GOT_ERR_MODIFIED);
4673 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4674 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4676 return NULL;
4679 const struct got_error *
4680 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4681 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4682 struct got_worktree *worktree, struct got_reference *branch,
4683 struct got_repository *repo)
4685 const struct got_error *err = NULL;
4686 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4687 char *branch_ref_name = NULL;
4688 char *fileindex_path = NULL;
4689 struct check_rebase_ok_arg ok_arg;
4690 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4691 struct got_object_id *wt_branch_tip = NULL;
4693 *new_base_branch_ref = NULL;
4694 *tmp_branch = NULL;
4695 *fileindex = NULL;
4697 err = lock_worktree(worktree, LOCK_EX);
4698 if (err)
4699 return err;
4701 err = open_fileindex(fileindex, &fileindex_path, worktree);
4702 if (err)
4703 goto done;
4705 ok_arg.worktree = worktree;
4706 ok_arg.repo = repo;
4707 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4708 &ok_arg);
4709 if (err)
4710 goto done;
4712 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4713 if (err)
4714 goto done;
4716 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4717 if (err)
4718 goto done;
4720 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4721 if (err)
4722 goto done;
4724 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4725 0);
4726 if (err)
4727 goto done;
4729 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4730 if (err)
4731 goto done;
4732 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4733 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4734 goto done;
4737 err = got_ref_alloc_symref(new_base_branch_ref,
4738 new_base_branch_ref_name, wt_branch);
4739 if (err)
4740 goto done;
4741 err = got_ref_write(*new_base_branch_ref, repo);
4742 if (err)
4743 goto done;
4745 /* TODO Lock original branch's ref while rebasing? */
4747 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4748 if (err)
4749 goto done;
4751 err = got_ref_write(branch_ref, repo);
4752 if (err)
4753 goto done;
4755 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4756 worktree->base_commit_id);
4757 if (err)
4758 goto done;
4759 err = got_ref_write(*tmp_branch, repo);
4760 if (err)
4761 goto done;
4763 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4764 if (err)
4765 goto done;
4766 done:
4767 free(fileindex_path);
4768 free(tmp_branch_name);
4769 free(new_base_branch_ref_name);
4770 free(branch_ref_name);
4771 if (branch_ref)
4772 got_ref_close(branch_ref);
4773 if (wt_branch)
4774 got_ref_close(wt_branch);
4775 free(wt_branch_tip);
4776 if (err) {
4777 if (*new_base_branch_ref) {
4778 got_ref_close(*new_base_branch_ref);
4779 *new_base_branch_ref = NULL;
4781 if (*tmp_branch) {
4782 got_ref_close(*tmp_branch);
4783 *tmp_branch = NULL;
4785 if (*fileindex) {
4786 got_fileindex_free(*fileindex);
4787 *fileindex = NULL;
4789 lock_worktree(worktree, LOCK_SH);
4791 return err;
4794 const struct got_error *
4795 got_worktree_rebase_continue(struct got_object_id **commit_id,
4796 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4797 struct got_reference **branch, struct got_fileindex **fileindex,
4798 struct got_worktree *worktree, struct got_repository *repo)
4800 const struct got_error *err;
4801 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4802 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4803 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4804 char *fileindex_path = NULL;
4805 int have_staged_files = 0;
4807 *commit_id = NULL;
4808 *new_base_branch = NULL;
4809 *tmp_branch = NULL;
4810 *branch = NULL;
4811 *fileindex = NULL;
4813 err = lock_worktree(worktree, LOCK_EX);
4814 if (err)
4815 return err;
4817 err = open_fileindex(fileindex, &fileindex_path, worktree);
4818 if (err)
4819 goto done;
4821 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4822 &have_staged_files);
4823 if (err && err->code != GOT_ERR_CANCELLED)
4824 goto done;
4825 if (have_staged_files) {
4826 err = got_error(GOT_ERR_STAGED_PATHS);
4827 goto done;
4830 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4831 if (err)
4832 goto done;
4834 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4835 if (err)
4836 goto done;
4838 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4839 if (err)
4840 goto done;
4842 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4843 if (err)
4844 goto done;
4846 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4847 if (err)
4848 goto done;
4850 err = got_ref_open(branch, repo,
4851 got_ref_get_symref_target(branch_ref), 0);
4852 if (err)
4853 goto done;
4855 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4856 if (err)
4857 goto done;
4859 err = got_ref_resolve(commit_id, repo, commit_ref);
4860 if (err)
4861 goto done;
4863 err = got_ref_open(new_base_branch, repo,
4864 new_base_branch_ref_name, 0);
4865 if (err)
4866 goto done;
4868 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4869 if (err)
4870 goto done;
4871 done:
4872 free(commit_ref_name);
4873 free(branch_ref_name);
4874 free(fileindex_path);
4875 if (commit_ref)
4876 got_ref_close(commit_ref);
4877 if (branch_ref)
4878 got_ref_close(branch_ref);
4879 if (err) {
4880 free(*commit_id);
4881 *commit_id = NULL;
4882 if (*tmp_branch) {
4883 got_ref_close(*tmp_branch);
4884 *tmp_branch = NULL;
4886 if (*new_base_branch) {
4887 got_ref_close(*new_base_branch);
4888 *new_base_branch = NULL;
4890 if (*branch) {
4891 got_ref_close(*branch);
4892 *branch = NULL;
4894 if (*fileindex) {
4895 got_fileindex_free(*fileindex);
4896 *fileindex = NULL;
4898 lock_worktree(worktree, LOCK_SH);
4900 return err;
4903 const struct got_error *
4904 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4906 const struct got_error *err;
4907 char *tmp_branch_name = NULL;
4909 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4910 if (err)
4911 return err;
4913 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4914 free(tmp_branch_name);
4915 return NULL;
4918 static const struct got_error *
4919 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4920 char **logmsg, void *arg)
4922 *logmsg = arg;
4923 return NULL;
4926 static const struct got_error *
4927 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4928 const char *path, struct got_object_id *blob_id,
4929 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4930 int dirfd, const char *de_name)
4932 return NULL;
4935 struct collect_merged_paths_arg {
4936 got_worktree_checkout_cb progress_cb;
4937 void *progress_arg;
4938 struct got_pathlist_head *merged_paths;
4941 static const struct got_error *
4942 collect_merged_paths(void *arg, unsigned char status, const char *path)
4944 const struct got_error *err;
4945 struct collect_merged_paths_arg *a = arg;
4946 char *p;
4947 struct got_pathlist_entry *new;
4949 err = (*a->progress_cb)(a->progress_arg, status, path);
4950 if (err)
4951 return err;
4953 if (status != GOT_STATUS_MERGE &&
4954 status != GOT_STATUS_ADD &&
4955 status != GOT_STATUS_DELETE &&
4956 status != GOT_STATUS_CONFLICT)
4957 return NULL;
4959 p = strdup(path);
4960 if (p == NULL)
4961 return got_error_from_errno("strdup");
4963 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4964 if (err || new == NULL)
4965 free(p);
4966 return err;
4969 void
4970 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4972 struct got_pathlist_entry *pe;
4974 TAILQ_FOREACH(pe, merged_paths, entry)
4975 free((char *)pe->path);
4977 got_pathlist_free(merged_paths);
4980 static const struct got_error *
4981 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4982 struct got_repository *repo)
4984 const struct got_error *err;
4985 struct got_reference *commit_ref = NULL;
4987 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4988 if (err) {
4989 if (err->code != GOT_ERR_NOT_REF)
4990 goto done;
4991 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4992 if (err)
4993 goto done;
4994 err = got_ref_write(commit_ref, repo);
4995 if (err)
4996 goto done;
4997 } else {
4998 struct got_object_id *stored_id;
4999 int cmp;
5001 err = got_ref_resolve(&stored_id, repo, commit_ref);
5002 if (err)
5003 goto done;
5004 cmp = got_object_id_cmp(commit_id, stored_id);
5005 free(stored_id);
5006 if (cmp != 0) {
5007 err = got_error(GOT_ERR_REBASE_COMMITID);
5008 goto done;
5011 done:
5012 if (commit_ref)
5013 got_ref_close(commit_ref);
5014 return err;
5017 static const struct got_error *
5018 rebase_merge_files(struct got_pathlist_head *merged_paths,
5019 const char *commit_ref_name, struct got_worktree *worktree,
5020 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5021 struct got_object_id *commit_id, struct got_repository *repo,
5022 got_worktree_checkout_cb progress_cb, void *progress_arg,
5023 got_cancel_cb cancel_cb, void *cancel_arg)
5025 const struct got_error *err;
5026 struct got_reference *commit_ref = NULL;
5027 struct collect_merged_paths_arg cmp_arg;
5028 char *fileindex_path;
5030 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5032 err = get_fileindex_path(&fileindex_path, worktree);
5033 if (err)
5034 return err;
5036 cmp_arg.progress_cb = progress_cb;
5037 cmp_arg.progress_arg = progress_arg;
5038 cmp_arg.merged_paths = merged_paths;
5039 err = merge_files(worktree, fileindex, fileindex_path,
5040 parent_commit_id, commit_id, repo, collect_merged_paths,
5041 &cmp_arg, cancel_cb, cancel_arg);
5042 if (commit_ref)
5043 got_ref_close(commit_ref);
5044 return err;
5047 const struct got_error *
5048 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5049 struct got_worktree *worktree, struct got_fileindex *fileindex,
5050 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5051 struct got_repository *repo,
5052 got_worktree_checkout_cb progress_cb, void *progress_arg,
5053 got_cancel_cb cancel_cb, void *cancel_arg)
5055 const struct got_error *err;
5056 char *commit_ref_name;
5058 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5059 if (err)
5060 return err;
5062 err = store_commit_id(commit_ref_name, commit_id, repo);
5063 if (err)
5064 goto done;
5066 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5067 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5068 progress_arg, cancel_cb, cancel_arg);
5069 done:
5070 free(commit_ref_name);
5071 return err;
5074 const struct got_error *
5075 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5076 struct got_worktree *worktree, struct got_fileindex *fileindex,
5077 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5078 struct got_repository *repo,
5079 got_worktree_checkout_cb progress_cb, void *progress_arg,
5080 got_cancel_cb cancel_cb, void *cancel_arg)
5082 const struct got_error *err;
5083 char *commit_ref_name;
5085 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5086 if (err)
5087 return err;
5089 err = store_commit_id(commit_ref_name, commit_id, repo);
5090 if (err)
5091 goto done;
5093 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5094 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5095 progress_arg, cancel_cb, cancel_arg);
5096 done:
5097 free(commit_ref_name);
5098 return err;
5101 static const struct got_error *
5102 rebase_commit(struct got_object_id **new_commit_id,
5103 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5104 struct got_worktree *worktree, struct got_fileindex *fileindex,
5105 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5106 const char *new_logmsg, struct got_repository *repo)
5108 const struct got_error *err, *sync_err;
5109 struct got_pathlist_head commitable_paths;
5110 struct collect_commitables_arg cc_arg;
5111 char *fileindex_path = NULL;
5112 struct got_reference *head_ref = NULL;
5113 struct got_object_id *head_commit_id = NULL;
5114 char *logmsg = NULL;
5116 TAILQ_INIT(&commitable_paths);
5117 *new_commit_id = NULL;
5119 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5121 err = get_fileindex_path(&fileindex_path, worktree);
5122 if (err)
5123 return err;
5125 cc_arg.commitable_paths = &commitable_paths;
5126 cc_arg.worktree = worktree;
5127 cc_arg.repo = repo;
5128 cc_arg.have_staged_files = 0;
5130 * If possible get the status of individual files directly to
5131 * avoid crawling the entire work tree once per rebased commit.
5132 * TODO: Ideally, merged_paths would contain a list of commitables
5133 * we could use so we could skip worktree_status() entirely.
5135 if (merged_paths) {
5136 struct got_pathlist_entry *pe;
5137 if (TAILQ_EMPTY(merged_paths)) {
5138 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5139 goto done;
5141 TAILQ_FOREACH(pe, merged_paths, entry) {
5142 err = worktree_status(worktree, pe->path, fileindex,
5143 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5144 0);
5145 if (err)
5146 goto done;
5148 } else {
5149 err = worktree_status(worktree, "", fileindex, repo,
5150 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5151 if (err)
5152 goto done;
5155 if (TAILQ_EMPTY(&commitable_paths)) {
5156 /* No-op change; commit will be elided. */
5157 err = got_ref_delete(commit_ref, repo);
5158 if (err)
5159 goto done;
5160 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5161 goto done;
5164 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5165 if (err)
5166 goto done;
5168 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5169 if (err)
5170 goto done;
5172 if (new_logmsg) {
5173 logmsg = strdup(new_logmsg);
5174 if (logmsg == NULL) {
5175 err = got_error_from_errno("strdup");
5176 goto done;
5178 } else {
5179 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5180 if (err)
5181 goto done;
5184 /* NB: commit_worktree will call free(logmsg) */
5185 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5186 worktree, got_object_commit_get_author(orig_commit),
5187 got_object_commit_get_committer(orig_commit),
5188 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5189 if (err)
5190 goto done;
5192 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5193 if (err)
5194 goto done;
5196 err = got_ref_delete(commit_ref, repo);
5197 if (err)
5198 goto done;
5200 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5201 fileindex, 0);
5202 sync_err = sync_fileindex(fileindex, fileindex_path);
5203 if (sync_err && err == NULL)
5204 err = sync_err;
5205 done:
5206 free(fileindex_path);
5207 free(head_commit_id);
5208 if (head_ref)
5209 got_ref_close(head_ref);
5210 if (err) {
5211 free(*new_commit_id);
5212 *new_commit_id = NULL;
5214 return err;
5217 const struct got_error *
5218 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5219 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5220 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5221 struct got_commit_object *orig_commit,
5222 struct got_object_id *orig_commit_id, struct got_repository *repo)
5224 const struct got_error *err;
5225 char *commit_ref_name;
5226 struct got_reference *commit_ref = NULL;
5227 struct got_object_id *commit_id = NULL;
5229 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5230 if (err)
5231 return err;
5233 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5234 if (err)
5235 goto done;
5236 err = got_ref_resolve(&commit_id, repo, commit_ref);
5237 if (err)
5238 goto done;
5239 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5240 err = got_error(GOT_ERR_REBASE_COMMITID);
5241 goto done;
5244 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5245 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5246 done:
5247 if (commit_ref)
5248 got_ref_close(commit_ref);
5249 free(commit_ref_name);
5250 free(commit_id);
5251 return err;
5254 const struct got_error *
5255 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5256 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5257 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5258 struct got_commit_object *orig_commit,
5259 struct got_object_id *orig_commit_id, const char *new_logmsg,
5260 struct got_repository *repo)
5262 const struct got_error *err;
5263 char *commit_ref_name;
5264 struct got_reference *commit_ref = NULL;
5265 struct got_object_id *commit_id = NULL;
5267 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5268 if (err)
5269 return err;
5271 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5272 if (err)
5273 goto done;
5274 err = got_ref_resolve(&commit_id, repo, commit_ref);
5275 if (err)
5276 goto done;
5277 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5278 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5279 goto done;
5282 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5283 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5284 done:
5285 if (commit_ref)
5286 got_ref_close(commit_ref);
5287 free(commit_ref_name);
5288 free(commit_id);
5289 return err;
5292 const struct got_error *
5293 got_worktree_rebase_postpone(struct got_worktree *worktree,
5294 struct got_fileindex *fileindex)
5296 if (fileindex)
5297 got_fileindex_free(fileindex);
5298 return lock_worktree(worktree, LOCK_SH);
5301 static const struct got_error *
5302 delete_ref(const char *name, struct got_repository *repo)
5304 const struct got_error *err;
5305 struct got_reference *ref;
5307 err = got_ref_open(&ref, repo, name, 0);
5308 if (err) {
5309 if (err->code == GOT_ERR_NOT_REF)
5310 return NULL;
5311 return err;
5314 err = got_ref_delete(ref, repo);
5315 got_ref_close(ref);
5316 return err;
5319 static const struct got_error *
5320 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5322 const struct got_error *err;
5323 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5324 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5326 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5327 if (err)
5328 goto done;
5329 err = delete_ref(tmp_branch_name, repo);
5330 if (err)
5331 goto done;
5333 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5334 if (err)
5335 goto done;
5336 err = delete_ref(new_base_branch_ref_name, repo);
5337 if (err)
5338 goto done;
5340 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5341 if (err)
5342 goto done;
5343 err = delete_ref(branch_ref_name, repo);
5344 if (err)
5345 goto done;
5347 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5348 if (err)
5349 goto done;
5350 err = delete_ref(commit_ref_name, repo);
5351 if (err)
5352 goto done;
5354 done:
5355 free(tmp_branch_name);
5356 free(new_base_branch_ref_name);
5357 free(branch_ref_name);
5358 free(commit_ref_name);
5359 return err;
5362 const struct got_error *
5363 got_worktree_rebase_complete(struct got_worktree *worktree,
5364 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5365 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5366 struct got_repository *repo)
5368 const struct got_error *err, *unlockerr;
5369 struct got_object_id *new_head_commit_id = NULL;
5371 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5372 if (err)
5373 return err;
5375 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5376 if (err)
5377 goto done;
5379 err = got_ref_write(rebased_branch, repo);
5380 if (err)
5381 goto done;
5383 err = got_worktree_set_head_ref(worktree, rebased_branch);
5384 if (err)
5385 goto done;
5387 err = delete_rebase_refs(worktree, repo);
5388 done:
5389 if (fileindex)
5390 got_fileindex_free(fileindex);
5391 free(new_head_commit_id);
5392 unlockerr = lock_worktree(worktree, LOCK_SH);
5393 if (unlockerr && err == NULL)
5394 err = unlockerr;
5395 return err;
5398 const struct got_error *
5399 got_worktree_rebase_abort(struct got_worktree *worktree,
5400 struct got_fileindex *fileindex, struct got_repository *repo,
5401 struct got_reference *new_base_branch,
5402 got_worktree_checkout_cb progress_cb, void *progress_arg)
5404 const struct got_error *err, *unlockerr, *sync_err;
5405 struct got_reference *resolved = NULL;
5406 struct got_object_id *commit_id = NULL;
5407 char *fileindex_path = NULL;
5408 struct revert_file_args rfa;
5409 struct got_object_id *tree_id = NULL;
5411 err = lock_worktree(worktree, LOCK_EX);
5412 if (err)
5413 return err;
5415 err = got_ref_open(&resolved, repo,
5416 got_ref_get_symref_target(new_base_branch), 0);
5417 if (err)
5418 goto done;
5420 err = got_worktree_set_head_ref(worktree, resolved);
5421 if (err)
5422 goto done;
5425 * XXX commits to the base branch could have happened while
5426 * we were busy rebasing; should we store the original commit ID
5427 * when rebase begins and read it back here?
5429 err = got_ref_resolve(&commit_id, repo, resolved);
5430 if (err)
5431 goto done;
5433 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5434 if (err)
5435 goto done;
5437 err = got_object_id_by_path(&tree_id, repo,
5438 worktree->base_commit_id, worktree->path_prefix);
5439 if (err)
5440 goto done;
5442 err = delete_rebase_refs(worktree, repo);
5443 if (err)
5444 goto done;
5446 err = get_fileindex_path(&fileindex_path, worktree);
5447 if (err)
5448 goto done;
5450 rfa.worktree = worktree;
5451 rfa.fileindex = fileindex;
5452 rfa.progress_cb = progress_cb;
5453 rfa.progress_arg = progress_arg;
5454 rfa.patch_cb = NULL;
5455 rfa.patch_arg = NULL;
5456 rfa.repo = repo;
5457 err = worktree_status(worktree, "", fileindex, repo,
5458 revert_file, &rfa, NULL, NULL, 0, 0);
5459 if (err)
5460 goto sync;
5462 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5463 repo, progress_cb, progress_arg, NULL, NULL);
5464 sync:
5465 sync_err = sync_fileindex(fileindex, fileindex_path);
5466 if (sync_err && err == NULL)
5467 err = sync_err;
5468 done:
5469 got_ref_close(resolved);
5470 free(tree_id);
5471 free(commit_id);
5472 if (fileindex)
5473 got_fileindex_free(fileindex);
5474 free(fileindex_path);
5476 unlockerr = lock_worktree(worktree, LOCK_SH);
5477 if (unlockerr && err == NULL)
5478 err = unlockerr;
5479 return err;
5482 const struct got_error *
5483 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5484 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5485 struct got_fileindex **fileindex, struct got_worktree *worktree,
5486 struct got_repository *repo)
5488 const struct got_error *err = NULL;
5489 char *tmp_branch_name = NULL;
5490 char *branch_ref_name = NULL;
5491 char *base_commit_ref_name = NULL;
5492 char *fileindex_path = NULL;
5493 struct check_rebase_ok_arg ok_arg;
5494 struct got_reference *wt_branch = NULL;
5495 struct got_reference *base_commit_ref = NULL;
5497 *tmp_branch = NULL;
5498 *branch_ref = NULL;
5499 *base_commit_id = NULL;
5500 *fileindex = NULL;
5502 err = lock_worktree(worktree, LOCK_EX);
5503 if (err)
5504 return err;
5506 err = open_fileindex(fileindex, &fileindex_path, worktree);
5507 if (err)
5508 goto done;
5510 ok_arg.worktree = worktree;
5511 ok_arg.repo = repo;
5512 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5513 &ok_arg);
5514 if (err)
5515 goto done;
5517 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5518 if (err)
5519 goto done;
5521 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5522 if (err)
5523 goto done;
5525 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5526 worktree);
5527 if (err)
5528 goto done;
5530 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5531 0);
5532 if (err)
5533 goto done;
5535 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5536 if (err)
5537 goto done;
5539 err = got_ref_write(*branch_ref, repo);
5540 if (err)
5541 goto done;
5543 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5544 worktree->base_commit_id);
5545 if (err)
5546 goto done;
5547 err = got_ref_write(base_commit_ref, repo);
5548 if (err)
5549 goto done;
5550 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5551 if (*base_commit_id == NULL) {
5552 err = got_error_from_errno("got_object_id_dup");
5553 goto done;
5556 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5557 worktree->base_commit_id);
5558 if (err)
5559 goto done;
5560 err = got_ref_write(*tmp_branch, repo);
5561 if (err)
5562 goto done;
5564 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5565 if (err)
5566 goto done;
5567 done:
5568 free(fileindex_path);
5569 free(tmp_branch_name);
5570 free(branch_ref_name);
5571 free(base_commit_ref_name);
5572 if (wt_branch)
5573 got_ref_close(wt_branch);
5574 if (err) {
5575 if (*branch_ref) {
5576 got_ref_close(*branch_ref);
5577 *branch_ref = NULL;
5579 if (*tmp_branch) {
5580 got_ref_close(*tmp_branch);
5581 *tmp_branch = NULL;
5583 free(*base_commit_id);
5584 if (*fileindex) {
5585 got_fileindex_free(*fileindex);
5586 *fileindex = NULL;
5588 lock_worktree(worktree, LOCK_SH);
5590 return err;
5593 const struct got_error *
5594 got_worktree_histedit_postpone(struct got_worktree *worktree,
5595 struct got_fileindex *fileindex)
5597 if (fileindex)
5598 got_fileindex_free(fileindex);
5599 return lock_worktree(worktree, LOCK_SH);
5602 const struct got_error *
5603 got_worktree_histedit_in_progress(int *in_progress,
5604 struct got_worktree *worktree)
5606 const struct got_error *err;
5607 char *tmp_branch_name = NULL;
5609 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5610 if (err)
5611 return err;
5613 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5614 free(tmp_branch_name);
5615 return NULL;
5618 const struct got_error *
5619 got_worktree_histedit_continue(struct got_object_id **commit_id,
5620 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5621 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5622 struct got_worktree *worktree, struct got_repository *repo)
5624 const struct got_error *err;
5625 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5626 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5627 struct got_reference *commit_ref = NULL;
5628 struct got_reference *base_commit_ref = NULL;
5629 char *fileindex_path = NULL;
5630 int have_staged_files = 0;
5632 *commit_id = NULL;
5633 *tmp_branch = NULL;
5634 *base_commit_id = NULL;
5635 *fileindex = NULL;
5637 err = lock_worktree(worktree, LOCK_EX);
5638 if (err)
5639 return err;
5641 err = open_fileindex(fileindex, &fileindex_path, worktree);
5642 if (err)
5643 goto done;
5645 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5646 &have_staged_files);
5647 if (err && err->code != GOT_ERR_CANCELLED)
5648 goto done;
5649 if (have_staged_files) {
5650 err = got_error(GOT_ERR_STAGED_PATHS);
5651 goto done;
5654 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5655 if (err)
5656 goto done;
5658 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5659 if (err)
5660 goto done;
5662 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5663 if (err)
5664 goto done;
5666 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5667 worktree);
5668 if (err)
5669 goto done;
5671 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5672 if (err)
5673 goto done;
5675 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5676 if (err)
5677 goto done;
5678 err = got_ref_resolve(commit_id, repo, commit_ref);
5679 if (err)
5680 goto done;
5682 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5683 if (err)
5684 goto done;
5685 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5686 if (err)
5687 goto done;
5689 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5690 if (err)
5691 goto done;
5692 done:
5693 free(commit_ref_name);
5694 free(branch_ref_name);
5695 free(fileindex_path);
5696 if (commit_ref)
5697 got_ref_close(commit_ref);
5698 if (base_commit_ref)
5699 got_ref_close(base_commit_ref);
5700 if (err) {
5701 free(*commit_id);
5702 *commit_id = NULL;
5703 free(*base_commit_id);
5704 *base_commit_id = NULL;
5705 if (*tmp_branch) {
5706 got_ref_close(*tmp_branch);
5707 *tmp_branch = NULL;
5709 if (*fileindex) {
5710 got_fileindex_free(*fileindex);
5711 *fileindex = NULL;
5713 lock_worktree(worktree, LOCK_EX);
5715 return err;
5718 static const struct got_error *
5719 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5723 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5725 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5726 if (err)
5727 goto done;
5728 err = delete_ref(tmp_branch_name, repo);
5729 if (err)
5730 goto done;
5732 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5733 worktree);
5734 if (err)
5735 goto done;
5736 err = delete_ref(base_commit_ref_name, repo);
5737 if (err)
5738 goto done;
5740 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5741 if (err)
5742 goto done;
5743 err = delete_ref(branch_ref_name, repo);
5744 if (err)
5745 goto done;
5747 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5748 if (err)
5749 goto done;
5750 err = delete_ref(commit_ref_name, repo);
5751 if (err)
5752 goto done;
5753 done:
5754 free(tmp_branch_name);
5755 free(base_commit_ref_name);
5756 free(branch_ref_name);
5757 free(commit_ref_name);
5758 return err;
5761 const struct got_error *
5762 got_worktree_histedit_abort(struct got_worktree *worktree,
5763 struct got_fileindex *fileindex, struct got_repository *repo,
5764 struct got_reference *branch, struct got_object_id *base_commit_id,
5765 got_worktree_checkout_cb progress_cb, void *progress_arg)
5767 const struct got_error *err, *unlockerr, *sync_err;
5768 struct got_reference *resolved = NULL;
5769 char *fileindex_path = NULL;
5770 struct got_object_id *tree_id = NULL;
5771 struct revert_file_args rfa;
5773 err = lock_worktree(worktree, LOCK_EX);
5774 if (err)
5775 return err;
5777 err = got_ref_open(&resolved, repo,
5778 got_ref_get_symref_target(branch), 0);
5779 if (err)
5780 goto done;
5782 err = got_worktree_set_head_ref(worktree, resolved);
5783 if (err)
5784 goto done;
5786 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5787 if (err)
5788 goto done;
5790 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5791 worktree->path_prefix);
5792 if (err)
5793 goto done;
5795 err = delete_histedit_refs(worktree, repo);
5796 if (err)
5797 goto done;
5799 err = get_fileindex_path(&fileindex_path, worktree);
5800 if (err)
5801 goto done;
5803 rfa.worktree = worktree;
5804 rfa.fileindex = fileindex;
5805 rfa.progress_cb = progress_cb;
5806 rfa.progress_arg = progress_arg;
5807 rfa.patch_cb = NULL;
5808 rfa.patch_arg = NULL;
5809 rfa.repo = repo;
5810 err = worktree_status(worktree, "", fileindex, repo,
5811 revert_file, &rfa, NULL, NULL, 0, 0);
5812 if (err)
5813 goto sync;
5815 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5816 repo, progress_cb, progress_arg, NULL, NULL);
5817 sync:
5818 sync_err = sync_fileindex(fileindex, fileindex_path);
5819 if (sync_err && err == NULL)
5820 err = sync_err;
5821 done:
5822 got_ref_close(resolved);
5823 free(tree_id);
5824 free(fileindex_path);
5826 unlockerr = lock_worktree(worktree, LOCK_SH);
5827 if (unlockerr && err == NULL)
5828 err = unlockerr;
5829 return err;
5832 const struct got_error *
5833 got_worktree_histedit_complete(struct got_worktree *worktree,
5834 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5835 struct got_reference *edited_branch, struct got_repository *repo)
5837 const struct got_error *err, *unlockerr;
5838 struct got_object_id *new_head_commit_id = NULL;
5839 struct got_reference *resolved = NULL;
5841 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5842 if (err)
5843 return err;
5845 err = got_ref_open(&resolved, repo,
5846 got_ref_get_symref_target(edited_branch), 0);
5847 if (err)
5848 goto done;
5850 err = got_ref_change_ref(resolved, new_head_commit_id);
5851 if (err)
5852 goto done;
5854 err = got_ref_write(resolved, repo);
5855 if (err)
5856 goto done;
5858 err = got_worktree_set_head_ref(worktree, resolved);
5859 if (err)
5860 goto done;
5862 err = delete_histedit_refs(worktree, repo);
5863 done:
5864 if (fileindex)
5865 got_fileindex_free(fileindex);
5866 free(new_head_commit_id);
5867 unlockerr = lock_worktree(worktree, LOCK_SH);
5868 if (unlockerr && err == NULL)
5869 err = unlockerr;
5870 return err;
5873 const struct got_error *
5874 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5875 struct got_object_id *commit_id, struct got_repository *repo)
5877 const struct got_error *err;
5878 char *commit_ref_name;
5880 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5881 if (err)
5882 return err;
5884 err = store_commit_id(commit_ref_name, commit_id, repo);
5885 if (err)
5886 goto done;
5888 err = delete_ref(commit_ref_name, repo);
5889 done:
5890 free(commit_ref_name);
5891 return err;
5894 const struct got_error *
5895 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5896 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5897 struct got_worktree *worktree, const char *refname,
5898 struct got_repository *repo)
5900 const struct got_error *err = NULL;
5901 char *fileindex_path = NULL;
5902 struct check_rebase_ok_arg ok_arg;
5904 *fileindex = NULL;
5905 *branch_ref = NULL;
5906 *base_branch_ref = NULL;
5908 err = lock_worktree(worktree, LOCK_EX);
5909 if (err)
5910 return err;
5912 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5913 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5914 "cannot integrate a branch into itself; "
5915 "update -b or different branch name required");
5916 goto done;
5919 err = open_fileindex(fileindex, &fileindex_path, worktree);
5920 if (err)
5921 goto done;
5923 /* Preconditions are the same as for rebase. */
5924 ok_arg.worktree = worktree;
5925 ok_arg.repo = repo;
5926 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5927 &ok_arg);
5928 if (err)
5929 goto done;
5931 err = got_ref_open(branch_ref, repo, refname, 1);
5932 if (err)
5933 goto done;
5935 err = got_ref_open(base_branch_ref, repo,
5936 got_worktree_get_head_ref_name(worktree), 1);
5937 done:
5938 if (err) {
5939 if (*branch_ref) {
5940 got_ref_close(*branch_ref);
5941 *branch_ref = NULL;
5943 if (*base_branch_ref) {
5944 got_ref_close(*base_branch_ref);
5945 *base_branch_ref = NULL;
5947 if (*fileindex) {
5948 got_fileindex_free(*fileindex);
5949 *fileindex = NULL;
5951 lock_worktree(worktree, LOCK_SH);
5953 return err;
5956 const struct got_error *
5957 got_worktree_integrate_continue(struct got_worktree *worktree,
5958 struct got_fileindex *fileindex, struct got_repository *repo,
5959 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5960 got_worktree_checkout_cb progress_cb, void *progress_arg,
5961 got_cancel_cb cancel_cb, void *cancel_arg)
5963 const struct got_error *err = NULL, *sync_err, *unlockerr;
5964 char *fileindex_path = NULL;
5965 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5967 err = get_fileindex_path(&fileindex_path, worktree);
5968 if (err)
5969 goto done;
5971 err = got_ref_resolve(&commit_id, repo, branch_ref);
5972 if (err)
5973 goto done;
5975 err = got_object_id_by_path(&tree_id, repo, commit_id,
5976 worktree->path_prefix);
5977 if (err)
5978 goto done;
5980 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5981 if (err)
5982 goto done;
5984 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5985 progress_cb, progress_arg, cancel_cb, cancel_arg);
5986 if (err)
5987 goto sync;
5989 err = got_ref_change_ref(base_branch_ref, commit_id);
5990 if (err)
5991 goto sync;
5993 err = got_ref_write(base_branch_ref, repo);
5994 sync:
5995 sync_err = sync_fileindex(fileindex, fileindex_path);
5996 if (sync_err && err == NULL)
5997 err = sync_err;
5999 done:
6000 unlockerr = got_ref_unlock(branch_ref);
6001 if (unlockerr && err == NULL)
6002 err = unlockerr;
6003 got_ref_close(branch_ref);
6005 unlockerr = got_ref_unlock(base_branch_ref);
6006 if (unlockerr && err == NULL)
6007 err = unlockerr;
6008 got_ref_close(base_branch_ref);
6010 got_fileindex_free(fileindex);
6011 free(fileindex_path);
6012 free(tree_id);
6014 unlockerr = lock_worktree(worktree, LOCK_SH);
6015 if (unlockerr && err == NULL)
6016 err = unlockerr;
6017 return err;
6020 const struct got_error *
6021 got_worktree_integrate_abort(struct got_worktree *worktree,
6022 struct got_fileindex *fileindex, struct got_repository *repo,
6023 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6025 const struct got_error *err = NULL, *unlockerr = NULL;
6027 got_fileindex_free(fileindex);
6029 err = lock_worktree(worktree, LOCK_SH);
6031 unlockerr = got_ref_unlock(branch_ref);
6032 if (unlockerr && err == NULL)
6033 err = unlockerr;
6034 got_ref_close(branch_ref);
6036 unlockerr = got_ref_unlock(base_branch_ref);
6037 if (unlockerr && err == NULL)
6038 err = unlockerr;
6039 got_ref_close(base_branch_ref);
6041 return err;
6044 struct check_stage_ok_arg {
6045 struct got_object_id *head_commit_id;
6046 struct got_worktree *worktree;
6047 struct got_fileindex *fileindex;
6048 struct got_repository *repo;
6049 int have_changes;
6052 const struct got_error *
6053 check_stage_ok(void *arg, unsigned char status,
6054 unsigned char staged_status, const char *relpath,
6055 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6056 struct got_object_id *commit_id, int dirfd, const char *de_name)
6058 struct check_stage_ok_arg *a = arg;
6059 const struct got_error *err = NULL;
6060 struct got_fileindex_entry *ie;
6061 struct got_object_id base_commit_id;
6062 struct got_object_id *base_commit_idp = NULL;
6063 char *in_repo_path = NULL, *p;
6065 if (status == GOT_STATUS_UNVERSIONED ||
6066 status == GOT_STATUS_NO_CHANGE)
6067 return NULL;
6068 if (status == GOT_STATUS_NONEXISTENT)
6069 return got_error_set_errno(ENOENT, relpath);
6071 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6072 if (ie == NULL)
6073 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6075 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6076 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6077 relpath) == -1)
6078 return got_error_from_errno("asprintf");
6080 if (got_fileindex_entry_has_commit(ie)) {
6081 memcpy(base_commit_id.sha1, ie->commit_sha1,
6082 SHA1_DIGEST_LENGTH);
6083 base_commit_idp = &base_commit_id;
6086 if (status == GOT_STATUS_CONFLICT) {
6087 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6088 goto done;
6089 } else if (status != GOT_STATUS_ADD &&
6090 status != GOT_STATUS_MODIFY &&
6091 status != GOT_STATUS_DELETE) {
6092 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6093 goto done;
6096 a->have_changes = 1;
6098 p = in_repo_path;
6099 while (p[0] == '/')
6100 p++;
6101 err = check_out_of_date(p, status, staged_status,
6102 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6103 GOT_ERR_STAGE_OUT_OF_DATE);
6104 done:
6105 free(in_repo_path);
6106 return err;
6109 struct stage_path_arg {
6110 struct got_worktree *worktree;
6111 struct got_fileindex *fileindex;
6112 struct got_repository *repo;
6113 got_worktree_status_cb status_cb;
6114 void *status_arg;
6115 got_worktree_patch_cb patch_cb;
6116 void *patch_arg;
6117 int staged_something;
6120 static const struct got_error *
6121 stage_path(void *arg, unsigned char status,
6122 unsigned char staged_status, const char *relpath,
6123 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6124 struct got_object_id *commit_id, int dirfd, const char *de_name)
6126 struct stage_path_arg *a = arg;
6127 const struct got_error *err = NULL;
6128 struct got_fileindex_entry *ie;
6129 char *ondisk_path = NULL, *path_content = NULL;
6130 uint32_t stage;
6131 struct got_object_id *new_staged_blob_id = NULL;
6133 if (status == GOT_STATUS_UNVERSIONED)
6134 return NULL;
6136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6137 if (ie == NULL)
6138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6141 relpath)== -1)
6142 return got_error_from_errno("asprintf");
6144 switch (status) {
6145 case GOT_STATUS_ADD:
6146 case GOT_STATUS_MODIFY:
6147 if (a->patch_cb) {
6148 if (status == GOT_STATUS_ADD) {
6149 int choice = GOT_PATCH_CHOICE_NONE;
6150 err = (*a->patch_cb)(&choice, a->patch_arg,
6151 status, ie->path, NULL, 1, 1);
6152 if (err)
6153 break;
6154 if (choice != GOT_PATCH_CHOICE_YES)
6155 break;
6156 } else {
6157 err = create_patched_content(&path_content, 0,
6158 staged_blob_id ? staged_blob_id : blob_id,
6159 ondisk_path, dirfd, de_name, ie->path,
6160 a->repo, a->patch_cb, a->patch_arg);
6161 if (err || path_content == NULL)
6162 break;
6165 err = got_object_blob_create(&new_staged_blob_id,
6166 path_content ? path_content : ondisk_path, a->repo);
6167 if (err)
6168 break;
6169 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6170 SHA1_DIGEST_LENGTH);
6171 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6172 stage = GOT_FILEIDX_STAGE_ADD;
6173 else
6174 stage = GOT_FILEIDX_STAGE_MODIFY;
6175 got_fileindex_entry_stage_set(ie, stage);
6176 a->staged_something = 1;
6177 if (a->status_cb == NULL)
6178 break;
6179 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6180 get_staged_status(ie), relpath, blob_id,
6181 new_staged_blob_id, NULL, dirfd, de_name);
6182 break;
6183 case GOT_STATUS_DELETE:
6184 if (staged_status == GOT_STATUS_DELETE)
6185 break;
6186 if (a->patch_cb) {
6187 int choice = GOT_PATCH_CHOICE_NONE;
6188 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6189 ie->path, NULL, 1, 1);
6190 if (err)
6191 break;
6192 if (choice == GOT_PATCH_CHOICE_NO)
6193 break;
6194 if (choice != GOT_PATCH_CHOICE_YES) {
6195 err = got_error(GOT_ERR_PATCH_CHOICE);
6196 break;
6199 stage = GOT_FILEIDX_STAGE_DELETE;
6200 got_fileindex_entry_stage_set(ie, stage);
6201 a->staged_something = 1;
6202 if (a->status_cb == NULL)
6203 break;
6204 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6205 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6206 de_name);
6207 break;
6208 case GOT_STATUS_NO_CHANGE:
6209 break;
6210 case GOT_STATUS_CONFLICT:
6211 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6212 break;
6213 case GOT_STATUS_NONEXISTENT:
6214 err = got_error_set_errno(ENOENT, relpath);
6215 break;
6216 default:
6217 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6218 break;
6221 if (path_content && unlink(path_content) == -1 && err == NULL)
6222 err = got_error_from_errno2("unlink", path_content);
6223 free(path_content);
6224 free(ondisk_path);
6225 free(new_staged_blob_id);
6226 return err;
6229 const struct got_error *
6230 got_worktree_stage(struct got_worktree *worktree,
6231 struct got_pathlist_head *paths,
6232 got_worktree_status_cb status_cb, void *status_arg,
6233 got_worktree_patch_cb patch_cb, void *patch_arg,
6234 struct got_repository *repo)
6236 const struct got_error *err = NULL, *sync_err, *unlockerr;
6237 struct got_pathlist_entry *pe;
6238 struct got_fileindex *fileindex = NULL;
6239 char *fileindex_path = NULL;
6240 struct got_reference *head_ref = NULL;
6241 struct got_object_id *head_commit_id = NULL;
6242 struct check_stage_ok_arg oka;
6243 struct stage_path_arg spa;
6245 err = lock_worktree(worktree, LOCK_EX);
6246 if (err)
6247 return err;
6249 err = got_ref_open(&head_ref, repo,
6250 got_worktree_get_head_ref_name(worktree), 0);
6251 if (err)
6252 goto done;
6253 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6254 if (err)
6255 goto done;
6256 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6257 if (err)
6258 goto done;
6260 /* Check pre-conditions before staging anything. */
6261 oka.head_commit_id = head_commit_id;
6262 oka.worktree = worktree;
6263 oka.fileindex = fileindex;
6264 oka.repo = repo;
6265 oka.have_changes = 0;
6266 TAILQ_FOREACH(pe, paths, entry) {
6267 err = worktree_status(worktree, pe->path, fileindex, repo,
6268 check_stage_ok, &oka, NULL, NULL, 0, 0);
6269 if (err)
6270 goto done;
6272 if (!oka.have_changes) {
6273 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6274 goto done;
6277 spa.worktree = worktree;
6278 spa.fileindex = fileindex;
6279 spa.repo = repo;
6280 spa.patch_cb = patch_cb;
6281 spa.patch_arg = patch_arg;
6282 spa.status_cb = status_cb;
6283 spa.status_arg = status_arg;
6284 spa.staged_something = 0;
6285 TAILQ_FOREACH(pe, paths, entry) {
6286 err = worktree_status(worktree, pe->path, fileindex, repo,
6287 stage_path, &spa, NULL, NULL, 0, 0);
6288 if (err)
6289 goto done;
6291 if (!spa.staged_something) {
6292 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6293 goto done;
6296 sync_err = sync_fileindex(fileindex, fileindex_path);
6297 if (sync_err && err == NULL)
6298 err = sync_err;
6299 done:
6300 if (head_ref)
6301 got_ref_close(head_ref);
6302 free(head_commit_id);
6303 free(fileindex_path);
6304 if (fileindex)
6305 got_fileindex_free(fileindex);
6306 unlockerr = lock_worktree(worktree, LOCK_SH);
6307 if (unlockerr && err == NULL)
6308 err = unlockerr;
6309 return err;
6312 struct unstage_path_arg {
6313 struct got_worktree *worktree;
6314 struct got_fileindex *fileindex;
6315 struct got_repository *repo;
6316 got_worktree_checkout_cb progress_cb;
6317 void *progress_arg;
6318 got_worktree_patch_cb patch_cb;
6319 void *patch_arg;
6322 static const struct got_error *
6323 create_unstaged_content(char **path_unstaged_content,
6324 char **path_new_staged_content, struct got_object_id *blob_id,
6325 struct got_object_id *staged_blob_id, const char *relpath,
6326 struct got_repository *repo,
6327 got_worktree_patch_cb patch_cb, void *patch_arg)
6329 const struct got_error *err;
6330 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6331 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6332 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6333 struct stat sb1, sb2;
6334 struct got_diff_changes *changes = NULL;
6335 struct got_diff_state *ds = NULL;
6336 struct got_diff_args *args = NULL;
6337 struct got_diff_change *change;
6338 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6339 int have_content = 0, have_rejected_content = 0;
6341 *path_unstaged_content = NULL;
6342 *path_new_staged_content = NULL;
6344 err = got_object_id_str(&label1, blob_id);
6345 if (err)
6346 return err;
6347 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6348 if (err)
6349 goto done;
6351 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6352 if (err)
6353 goto done;
6355 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6356 if (err)
6357 goto done;
6359 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6360 if (err)
6361 goto done;
6363 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6364 if (err)
6365 goto done;
6367 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6368 if (err)
6369 goto done;
6371 if (stat(path1, &sb1) == -1) {
6372 err = got_error_from_errno2("stat", path1);
6373 goto done;
6376 if (stat(path2, &sb2) == -1) {
6377 err = got_error_from_errno2("stat", path2);
6378 goto done;
6381 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6382 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6383 if (err)
6384 goto done;
6386 err = got_opentemp_named(path_unstaged_content, &outfile,
6387 "got-unstaged-content");
6388 if (err)
6389 goto done;
6390 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6391 "got-new-staged-content");
6392 if (err)
6393 goto done;
6395 if (fseek(f1, 0L, SEEK_SET) == -1) {
6396 err = got_ferror(f1, GOT_ERR_IO);
6397 goto done;
6399 if (fseek(f2, 0L, SEEK_SET) == -1) {
6400 err = got_ferror(f2, GOT_ERR_IO);
6401 goto done;
6403 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6404 int choice;
6405 err = apply_or_reject_change(&choice, change, ++n,
6406 changes->nchanges, ds, args, diff_flags, relpath,
6407 f1, f2, &line_cur1, &line_cur2,
6408 outfile, rejectfile, patch_cb, patch_arg);
6409 if (err)
6410 goto done;
6411 if (choice == GOT_PATCH_CHOICE_YES)
6412 have_content = 1;
6413 else
6414 have_rejected_content = 1;
6415 if (choice == GOT_PATCH_CHOICE_QUIT)
6416 break;
6418 if (have_content || have_rejected_content)
6419 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6420 outfile, rejectfile);
6421 done:
6422 free(label1);
6423 if (blob)
6424 got_object_blob_close(blob);
6425 if (staged_blob)
6426 got_object_blob_close(staged_blob);
6427 if (f1 && fclose(f1) == EOF && err == NULL)
6428 err = got_error_from_errno2("fclose", path1);
6429 if (f2 && fclose(f2) == EOF && err == NULL)
6430 err = got_error_from_errno2("fclose", path2);
6431 if (outfile && fclose(outfile) == EOF && err == NULL)
6432 err = got_error_from_errno2("fclose", *path_unstaged_content);
6433 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6434 err = got_error_from_errno2("fclose", *path_new_staged_content);
6435 if (path1 && unlink(path1) == -1 && err == NULL)
6436 err = got_error_from_errno2("unlink", path1);
6437 if (path2 && unlink(path2) == -1 && err == NULL)
6438 err = got_error_from_errno2("unlink", path2);
6439 if (err || !have_content) {
6440 if (*path_unstaged_content &&
6441 unlink(*path_unstaged_content) == -1 && err == NULL)
6442 err = got_error_from_errno2("unlink",
6443 *path_unstaged_content);
6444 free(*path_unstaged_content);
6445 *path_unstaged_content = NULL;
6447 if (err || !have_rejected_content) {
6448 if (*path_new_staged_content &&
6449 unlink(*path_new_staged_content) == -1 && err == NULL)
6450 err = got_error_from_errno2("unlink",
6451 *path_new_staged_content);
6452 free(*path_new_staged_content);
6453 *path_new_staged_content = NULL;
6455 free(args);
6456 if (ds) {
6457 got_diff_state_free(ds);
6458 free(ds);
6460 if (changes)
6461 got_diff_free_changes(changes);
6462 free(path1);
6463 free(path2);
6464 return err;
6467 static const struct got_error *
6468 unstage_path(void *arg, unsigned char status,
6469 unsigned char staged_status, const char *relpath,
6470 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6471 struct got_object_id *commit_id, int dirfd, const char *de_name)
6473 const struct got_error *err = NULL;
6474 struct unstage_path_arg *a = arg;
6475 struct got_fileindex_entry *ie;
6476 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6477 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6478 char *path_new_staged_content = NULL;
6479 char *id_str = NULL, *label_orig = NULL;
6480 int local_changes_subsumed;
6481 struct stat sb;
6483 if (staged_status != GOT_STATUS_ADD &&
6484 staged_status != GOT_STATUS_MODIFY &&
6485 staged_status != GOT_STATUS_DELETE)
6486 return NULL;
6488 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6489 if (ie == NULL)
6490 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6492 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6493 == -1)
6494 return got_error_from_errno("asprintf");
6496 err = got_object_id_str(&id_str,
6497 commit_id ? commit_id : a->worktree->base_commit_id);
6498 if (err)
6499 goto done;
6500 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6501 id_str) == -1) {
6502 err = got_error_from_errno("asprintf");
6503 goto done;
6506 switch (staged_status) {
6507 case GOT_STATUS_MODIFY:
6508 err = got_object_open_as_blob(&blob_base, a->repo,
6509 blob_id, 8192);
6510 if (err)
6511 break;
6512 /* fall through */
6513 case GOT_STATUS_ADD:
6514 if (a->patch_cb) {
6515 if (staged_status == GOT_STATUS_ADD) {
6516 int choice = GOT_PATCH_CHOICE_NONE;
6517 err = (*a->patch_cb)(&choice, a->patch_arg,
6518 staged_status, ie->path, NULL, 1, 1);
6519 if (err)
6520 break;
6521 if (choice != GOT_PATCH_CHOICE_YES)
6522 break;
6523 } else {
6524 err = create_unstaged_content(
6525 &path_unstaged_content,
6526 &path_new_staged_content, blob_id,
6527 staged_blob_id, ie->path, a->repo,
6528 a->patch_cb, a->patch_arg);
6529 if (err || path_unstaged_content == NULL)
6530 break;
6531 if (path_new_staged_content) {
6532 err = got_object_blob_create(
6533 &staged_blob_id,
6534 path_new_staged_content,
6535 a->repo);
6536 if (err)
6537 break;
6538 memcpy(ie->staged_blob_sha1,
6539 staged_blob_id->sha1,
6540 SHA1_DIGEST_LENGTH);
6542 err = merge_file(&local_changes_subsumed,
6543 a->worktree, blob_base, ondisk_path,
6544 relpath, got_fileindex_perms_to_st(ie),
6545 path_unstaged_content, label_orig,
6546 "unstaged", a->repo, a->progress_cb,
6547 a->progress_arg);
6548 if (err == NULL &&
6549 path_new_staged_content == NULL)
6550 got_fileindex_entry_stage_set(ie,
6551 GOT_FILEIDX_STAGE_NONE);
6552 break; /* Done with this file. */
6555 err = got_object_open_as_blob(&blob_staged, a->repo,
6556 staged_blob_id, 8192);
6557 if (err)
6558 break;
6559 err = merge_blob(&local_changes_subsumed, a->worktree,
6560 blob_base, ondisk_path, relpath,
6561 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6562 commit_id ? commit_id : a->worktree->base_commit_id,
6563 a->repo, a->progress_cb, a->progress_arg);
6564 if (err == NULL)
6565 got_fileindex_entry_stage_set(ie,
6566 GOT_FILEIDX_STAGE_NONE);
6567 break;
6568 case GOT_STATUS_DELETE:
6569 if (a->patch_cb) {
6570 int choice = GOT_PATCH_CHOICE_NONE;
6571 err = (*a->patch_cb)(&choice, a->patch_arg,
6572 staged_status, ie->path, NULL, 1, 1);
6573 if (err)
6574 break;
6575 if (choice == GOT_PATCH_CHOICE_NO)
6576 break;
6577 if (choice != GOT_PATCH_CHOICE_YES) {
6578 err = got_error(GOT_ERR_PATCH_CHOICE);
6579 break;
6582 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6583 err = get_file_status(&status, &sb, ie, ondisk_path,
6584 dirfd, de_name, a->repo);
6585 if (err)
6586 break;
6587 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6588 break;
6590 done:
6591 free(ondisk_path);
6592 if (path_unstaged_content &&
6593 unlink(path_unstaged_content) == -1 && err == NULL)
6594 err = got_error_from_errno2("unlink", path_unstaged_content);
6595 if (path_new_staged_content &&
6596 unlink(path_new_staged_content) == -1 && err == NULL)
6597 err = got_error_from_errno2("unlink", path_new_staged_content);
6598 free(path_unstaged_content);
6599 free(path_new_staged_content);
6600 if (blob_base)
6601 got_object_blob_close(blob_base);
6602 if (blob_staged)
6603 got_object_blob_close(blob_staged);
6604 free(id_str);
6605 free(label_orig);
6606 return err;
6609 const struct got_error *
6610 got_worktree_unstage(struct got_worktree *worktree,
6611 struct got_pathlist_head *paths,
6612 got_worktree_checkout_cb progress_cb, void *progress_arg,
6613 got_worktree_patch_cb patch_cb, void *patch_arg,
6614 struct got_repository *repo)
6616 const struct got_error *err = NULL, *sync_err, *unlockerr;
6617 struct got_pathlist_entry *pe;
6618 struct got_fileindex *fileindex = NULL;
6619 char *fileindex_path = NULL;
6620 struct unstage_path_arg upa;
6622 err = lock_worktree(worktree, LOCK_EX);
6623 if (err)
6624 return err;
6626 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6627 if (err)
6628 goto done;
6630 upa.worktree = worktree;
6631 upa.fileindex = fileindex;
6632 upa.repo = repo;
6633 upa.progress_cb = progress_cb;
6634 upa.progress_arg = progress_arg;
6635 upa.patch_cb = patch_cb;
6636 upa.patch_arg = patch_arg;
6637 TAILQ_FOREACH(pe, paths, entry) {
6638 err = worktree_status(worktree, pe->path, fileindex, repo,
6639 unstage_path, &upa, NULL, NULL, 0, 0);
6640 if (err)
6641 goto done;
6644 sync_err = sync_fileindex(fileindex, fileindex_path);
6645 if (sync_err && err == NULL)
6646 err = sync_err;
6647 done:
6648 free(fileindex_path);
6649 if (fileindex)
6650 got_fileindex_free(fileindex);
6651 unlockerr = lock_worktree(worktree, LOCK_SH);
6652 if (unlockerr && err == NULL)
6653 err = unlockerr;
6654 return err;