Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("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, uint16_t te_mode, uint16_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;
1747 err = got_opentemp_named(&new_fileindex_path, &new_index,
1748 fileindex_path);
1749 if (err)
1750 goto done;
1752 err = got_fileindex_write(fileindex, new_index);
1753 if (err)
1754 goto done;
1756 if (rename(new_fileindex_path, fileindex_path) != 0) {
1757 err = got_error_from_errno3("rename", new_fileindex_path,
1758 fileindex_path);
1759 unlink(new_fileindex_path);
1761 done:
1762 if (new_index)
1763 fclose(new_index);
1764 free(new_fileindex_path);
1765 return err;
1768 static const struct got_error *
1769 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1770 struct got_object_id **tree_id, const char *wt_relpath,
1771 struct got_worktree *worktree, struct got_repository *repo)
1773 const struct got_error *err = NULL;
1774 struct got_object_id *id = NULL;
1775 char *in_repo_path = NULL;
1776 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1778 *entry_type = GOT_OBJ_TYPE_ANY;
1779 *tree_relpath = NULL;
1780 *tree_id = NULL;
1782 if (wt_relpath[0] == '\0') {
1783 /* Check out all files within the work tree. */
1784 *entry_type = GOT_OBJ_TYPE_TREE;
1785 *tree_relpath = strdup("");
1786 if (*tree_relpath == NULL) {
1787 err = got_error_from_errno("strdup");
1788 goto done;
1790 err = got_object_id_by_path(tree_id, repo,
1791 worktree->base_commit_id, worktree->path_prefix);
1792 if (err)
1793 goto done;
1794 return NULL;
1797 /* Check out a subset of files in the work tree. */
1799 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1800 is_root_wt ? "" : "/", wt_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1805 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1806 in_repo_path);
1807 if (err)
1808 goto done;
1810 free(in_repo_path);
1811 in_repo_path = NULL;
1813 err = got_object_get_type(entry_type, repo, id);
1814 if (err)
1815 goto done;
1817 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1818 /* Check out a single file. */
1819 if (strchr(wt_relpath, '/') == NULL) {
1820 /* Check out a single file in work tree's root dir. */
1821 in_repo_path = strdup(worktree->path_prefix);
1822 if (in_repo_path == NULL) {
1823 err = got_error_from_errno("strdup");
1824 goto done;
1826 *tree_relpath = strdup("");
1827 if (*tree_relpath == NULL) {
1828 err = got_error_from_errno("strdup");
1829 goto done;
1831 } else {
1832 /* Check out a single file in a subdirectory. */
1833 err = got_path_dirname(tree_relpath, wt_relpath);
1834 if (err)
1835 return err;
1836 if (asprintf(&in_repo_path, "%s%s%s",
1837 worktree->path_prefix, is_root_wt ? "" : "/",
1838 *tree_relpath) == -1) {
1839 err = got_error_from_errno("asprintf");
1840 goto done;
1843 err = got_object_id_by_path(tree_id, repo,
1844 worktree->base_commit_id, in_repo_path);
1845 } else {
1846 /* Check out all files within a subdirectory. */
1847 *tree_id = got_object_id_dup(id);
1848 if (*tree_id == NULL) {
1849 err = got_error_from_errno("got_object_id_dup");
1850 goto done;
1852 *tree_relpath = strdup(wt_relpath);
1853 if (*tree_relpath == NULL) {
1854 err = got_error_from_errno("strdup");
1855 goto done;
1858 done:
1859 free(id);
1860 free(in_repo_path);
1861 if (err) {
1862 *entry_type = GOT_OBJ_TYPE_ANY;
1863 free(*tree_relpath);
1864 *tree_relpath = NULL;
1865 free(*tree_id);
1866 *tree_id = NULL;
1868 return err;
1871 static const struct got_error *
1872 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1873 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1874 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1875 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1877 const struct got_error *err = NULL;
1878 struct got_commit_object *commit = NULL;
1879 struct got_tree_object *tree = NULL;
1880 struct got_fileindex_diff_tree_cb diff_cb;
1881 struct diff_cb_arg arg;
1883 err = ref_base_commit(worktree, repo);
1884 if (err)
1885 goto done;
1887 err = got_object_open_as_commit(&commit, repo,
1888 worktree->base_commit_id);
1889 if (err)
1890 goto done;
1892 err = got_object_open_as_tree(&tree, repo, tree_id);
1893 if (err)
1894 goto done;
1896 if (entry_name &&
1897 got_object_tree_find_entry(tree, entry_name) == NULL) {
1898 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1899 goto done;
1902 diff_cb.diff_old_new = diff_old_new;
1903 diff_cb.diff_old = diff_old;
1904 diff_cb.diff_new = diff_new;
1905 arg.fileindex = fileindex;
1906 arg.worktree = worktree;
1907 arg.repo = repo;
1908 arg.progress_cb = progress_cb;
1909 arg.progress_arg = progress_arg;
1910 arg.cancel_cb = cancel_cb;
1911 arg.cancel_arg = cancel_arg;
1912 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1913 entry_name, repo, &diff_cb, &arg);
1914 done:
1915 if (tree)
1916 got_object_tree_close(tree);
1917 if (commit)
1918 got_object_commit_close(commit);
1919 return err;
1922 const struct got_error *
1923 got_worktree_checkout_files(struct got_worktree *worktree,
1924 struct got_pathlist_head *paths, struct got_repository *repo,
1925 got_worktree_checkout_cb progress_cb, void *progress_arg,
1926 got_cancel_cb cancel_cb, void *cancel_arg)
1928 const struct got_error *err = NULL, *sync_err, *unlockerr;
1929 struct got_commit_object *commit = NULL;
1930 struct got_tree_object *tree = NULL;
1931 struct got_fileindex *fileindex = NULL;
1932 char *fileindex_path = NULL;
1933 struct got_pathlist_entry *pe;
1934 struct tree_path_data {
1935 SIMPLEQ_ENTRY(tree_path_data) entry;
1936 struct got_object_id *tree_id;
1937 int entry_type;
1938 char *relpath;
1939 char *entry_name;
1940 } *tpd = NULL;
1941 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1943 SIMPLEQ_INIT(&tree_paths);
1945 err = lock_worktree(worktree, LOCK_EX);
1946 if (err)
1947 return err;
1949 /* Map all specified paths to in-repository trees. */
1950 TAILQ_FOREACH(pe, paths, entry) {
1951 tpd = malloc(sizeof(*tpd));
1952 if (tpd == NULL) {
1953 err = got_error_from_errno("malloc");
1954 goto done;
1957 err = find_tree_entry_for_checkout(&tpd->entry_type,
1958 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1959 if (err) {
1960 free(tpd);
1961 goto done;
1964 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1965 err = got_path_basename(&tpd->entry_name, pe->path);
1966 if (err) {
1967 free(tpd->relpath);
1968 free(tpd->tree_id);
1969 free(tpd);
1970 goto done;
1972 } else
1973 tpd->entry_name = NULL;
1975 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1979 * Read the file index.
1980 * Checking out files is supposed to be an idempotent operation.
1981 * If the on-disk file index is incomplete we will try to complete it.
1983 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1984 if (err)
1985 goto done;
1987 tpd = SIMPLEQ_FIRST(&tree_paths);
1988 TAILQ_FOREACH(pe, paths, entry) {
1989 struct bump_base_commit_id_arg bbc_arg;
1991 err = checkout_files(worktree, fileindex, tpd->relpath,
1992 tpd->tree_id, tpd->entry_name, repo,
1993 progress_cb, progress_arg, cancel_cb, cancel_arg);
1994 if (err)
1995 break;
1997 bbc_arg.base_commit_id = worktree->base_commit_id;
1998 bbc_arg.entry_name = tpd->entry_name;
1999 bbc_arg.path = pe->path;
2000 bbc_arg.path_len = pe->path_len;
2001 bbc_arg.progress_cb = progress_cb;
2002 bbc_arg.progress_arg = progress_arg;
2003 err = got_fileindex_for_each_entry_safe(fileindex,
2004 bump_base_commit_id, &bbc_arg);
2005 if (err)
2006 break;
2008 tpd = SIMPLEQ_NEXT(tpd, entry);
2010 sync_err = sync_fileindex(fileindex, fileindex_path);
2011 if (sync_err && err == NULL)
2012 err = sync_err;
2013 done:
2014 free(fileindex_path);
2015 if (tree)
2016 got_object_tree_close(tree);
2017 if (commit)
2018 got_object_commit_close(commit);
2019 if (fileindex)
2020 got_fileindex_free(fileindex);
2021 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2022 tpd = SIMPLEQ_FIRST(&tree_paths);
2023 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2024 free(tpd->relpath);
2025 free(tpd->tree_id);
2026 free(tpd);
2028 unlockerr = lock_worktree(worktree, LOCK_SH);
2029 if (unlockerr && err == NULL)
2030 err = unlockerr;
2031 return err;
2034 struct merge_file_cb_arg {
2035 struct got_worktree *worktree;
2036 struct got_fileindex *fileindex;
2037 got_worktree_checkout_cb progress_cb;
2038 void *progress_arg;
2039 got_cancel_cb cancel_cb;
2040 void *cancel_arg;
2041 const char *label_orig;
2042 struct got_object_id *commit_id2;
2045 static const struct got_error *
2046 merge_file_cb(void *arg, struct got_blob_object *blob1,
2047 struct got_blob_object *blob2, struct got_object_id *id1,
2048 struct got_object_id *id2, const char *path1, const char *path2,
2049 mode_t mode1, mode_t mode2, struct got_repository *repo)
2051 static const struct got_error *err = NULL;
2052 struct merge_file_cb_arg *a = arg;
2053 struct got_fileindex_entry *ie;
2054 char *ondisk_path = NULL;
2055 struct stat sb;
2056 unsigned char status;
2057 int local_changes_subsumed;
2059 if (blob1 && blob2) {
2060 ie = got_fileindex_entry_get(a->fileindex, path2,
2061 strlen(path2));
2062 if (ie == NULL)
2063 return (*a->progress_cb)(a->progress_arg,
2064 GOT_STATUS_MISSING, path2);
2066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2067 path2) == -1)
2068 return got_error_from_errno("asprintf");
2070 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2071 repo);
2072 if (err)
2073 goto done;
2075 if (status == GOT_STATUS_DELETE) {
2076 err = (*a->progress_cb)(a->progress_arg,
2077 GOT_STATUS_MERGE, path2);
2078 goto done;
2080 if (status != GOT_STATUS_NO_CHANGE &&
2081 status != GOT_STATUS_MODIFY &&
2082 status != GOT_STATUS_CONFLICT &&
2083 status != GOT_STATUS_ADD) {
2084 err = (*a->progress_cb)(a->progress_arg, status, path2);
2085 goto done;
2088 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2089 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2090 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2091 } else if (blob1) {
2092 ie = got_fileindex_entry_get(a->fileindex, path1,
2093 strlen(path1));
2094 if (ie == NULL)
2095 return (*a->progress_cb)(a->progress_arg,
2096 GOT_STATUS_MISSING, path2);
2098 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2099 path1) == -1)
2100 return got_error_from_errno("asprintf");
2102 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2103 repo);
2104 if (err)
2105 goto done;
2107 switch (status) {
2108 case GOT_STATUS_NO_CHANGE:
2109 err = (*a->progress_cb)(a->progress_arg,
2110 GOT_STATUS_DELETE, path1);
2111 if (err)
2112 goto done;
2113 err = remove_ondisk_file(a->worktree->root_path, path1);
2114 if (err)
2115 goto done;
2116 if (ie)
2117 got_fileindex_entry_mark_deleted_from_disk(ie);
2118 break;
2119 case GOT_STATUS_DELETE:
2120 case GOT_STATUS_MISSING:
2121 err = (*a->progress_cb)(a->progress_arg,
2122 GOT_STATUS_DELETE, path1);
2123 if (err)
2124 goto done;
2125 if (ie)
2126 got_fileindex_entry_mark_deleted_from_disk(ie);
2127 break;
2128 case GOT_STATUS_ADD:
2129 case GOT_STATUS_MODIFY:
2130 case GOT_STATUS_CONFLICT:
2131 err = (*a->progress_cb)(a->progress_arg,
2132 GOT_STATUS_CANNOT_DELETE, path1);
2133 if (err)
2134 goto done;
2135 break;
2136 case GOT_STATUS_OBSTRUCTED:
2137 err = (*a->progress_cb)(a->progress_arg, status, path1);
2138 if (err)
2139 goto done;
2140 break;
2141 default:
2142 break;
2144 } else if (blob2) {
2145 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2146 path2) == -1)
2147 return got_error_from_errno("asprintf");
2148 ie = got_fileindex_entry_get(a->fileindex, path2,
2149 strlen(path2));
2150 if (ie) {
2151 err = get_file_status(&status, &sb, ie, ondisk_path,
2152 -1, NULL, repo);
2153 if (err)
2154 goto done;
2155 if (status != GOT_STATUS_NO_CHANGE &&
2156 status != GOT_STATUS_MODIFY &&
2157 status != GOT_STATUS_CONFLICT &&
2158 status != GOT_STATUS_ADD) {
2159 err = (*a->progress_cb)(a->progress_arg,
2160 status, path2);
2161 goto done;
2163 err = merge_blob(&local_changes_subsumed, a->worktree,
2164 NULL, ondisk_path, path2, sb.st_mode,
2165 a->label_orig, blob2, a->commit_id2, repo,
2166 a->progress_cb,
2167 a->progress_arg);
2168 if (status == GOT_STATUS_DELETE) {
2169 err = update_blob_fileindex_entry(a->worktree,
2170 a->fileindex, ie, ondisk_path, ie->path,
2171 blob2, 0);
2172 if (err)
2173 goto done;
2175 } else {
2176 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2177 err = install_blob(a->worktree, ondisk_path, path2,
2178 /* XXX get this from parent tree! */
2179 GOT_DEFAULT_FILE_MODE,
2180 sb.st_mode, blob2, 0, 0, repo,
2181 a->progress_cb, a->progress_arg);
2182 if (err)
2183 goto done;
2184 err = got_fileindex_entry_alloc(&ie,
2185 ondisk_path, path2, NULL, NULL);
2186 if (err)
2187 goto done;
2188 err = got_fileindex_entry_add(a->fileindex, ie);
2189 if (err) {
2190 got_fileindex_entry_free(ie);
2191 goto done;
2195 done:
2196 free(ondisk_path);
2197 return err;
2200 struct check_merge_ok_arg {
2201 struct got_worktree *worktree;
2202 struct got_repository *repo;
2205 static const struct got_error *
2206 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2208 const struct got_error *err = NULL;
2209 struct check_merge_ok_arg *a = arg;
2210 unsigned char status;
2211 struct stat sb;
2212 char *ondisk_path;
2214 /* Reject merges into a work tree with mixed base commits. */
2215 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2216 SHA1_DIGEST_LENGTH))
2217 return got_error(GOT_ERR_MIXED_COMMITS);
2219 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2220 == -1)
2221 return got_error_from_errno("asprintf");
2223 /* Reject merges into a work tree with conflicted files. */
2224 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2225 if (err)
2226 return err;
2227 if (status == GOT_STATUS_CONFLICT)
2228 return got_error(GOT_ERR_CONFLICTS);
2230 return NULL;
2233 static const struct got_error *
2234 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2235 const char *fileindex_path, struct got_object_id *commit_id1,
2236 struct got_object_id *commit_id2, struct got_repository *repo,
2237 got_worktree_checkout_cb progress_cb, void *progress_arg,
2238 got_cancel_cb cancel_cb, void *cancel_arg)
2240 const struct got_error *err = NULL, *sync_err;
2241 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2242 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2243 struct merge_file_cb_arg arg;
2244 char *label_orig = NULL;
2246 if (commit_id1) {
2247 char *id_str;
2249 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2250 worktree->path_prefix);
2251 if (err)
2252 goto done;
2254 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2255 if (err)
2256 goto done;
2258 err = got_object_id_str(&id_str, commit_id1);
2259 if (err)
2260 goto done;
2262 if (asprintf(&label_orig, "%s: commit %s",
2263 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2264 err = got_error_from_errno("asprintf");
2265 free(id_str);
2266 goto done;
2268 free(id_str);
2271 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2272 worktree->path_prefix);
2273 if (err)
2274 goto done;
2276 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2277 if (err)
2278 goto done;
2280 arg.worktree = worktree;
2281 arg.fileindex = fileindex;
2282 arg.progress_cb = progress_cb;
2283 arg.progress_arg = progress_arg;
2284 arg.cancel_cb = cancel_cb;
2285 arg.cancel_arg = cancel_arg;
2286 arg.label_orig = label_orig;
2287 arg.commit_id2 = commit_id2;
2288 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2289 sync_err = sync_fileindex(fileindex, fileindex_path);
2290 if (sync_err && err == NULL)
2291 err = sync_err;
2292 done:
2293 if (tree1)
2294 got_object_tree_close(tree1);
2295 if (tree2)
2296 got_object_tree_close(tree2);
2297 free(label_orig);
2298 return err;
2301 const struct got_error *
2302 got_worktree_merge_files(struct got_worktree *worktree,
2303 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2304 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2305 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2307 const struct got_error *err, *unlockerr;
2308 char *fileindex_path = NULL;
2309 struct got_fileindex *fileindex = NULL;
2310 struct check_merge_ok_arg mok_arg;
2312 err = lock_worktree(worktree, LOCK_EX);
2313 if (err)
2314 return err;
2316 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2317 if (err)
2318 goto done;
2320 mok_arg.worktree = worktree;
2321 mok_arg.repo = repo;
2322 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2323 &mok_arg);
2324 if (err)
2325 goto done;
2327 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2328 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2329 done:
2330 if (fileindex)
2331 got_fileindex_free(fileindex);
2332 free(fileindex_path);
2333 unlockerr = lock_worktree(worktree, LOCK_SH);
2334 if (unlockerr && err == NULL)
2335 err = unlockerr;
2336 return err;
2339 struct diff_dir_cb_arg {
2340 struct got_fileindex *fileindex;
2341 struct got_worktree *worktree;
2342 const char *status_path;
2343 size_t status_path_len;
2344 struct got_repository *repo;
2345 got_worktree_status_cb status_cb;
2346 void *status_arg;
2347 got_cancel_cb cancel_cb;
2348 void *cancel_arg;
2349 /* A pathlist containing per-directory pathlists of ignore patterns. */
2350 struct got_pathlist_head ignores;
2351 int report_unchanged;
2354 static const struct got_error *
2355 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2356 int dirfd, const char *de_name,
2357 got_worktree_status_cb status_cb, void *status_arg,
2358 struct got_repository *repo, int report_unchanged)
2360 const struct got_error *err = NULL;
2361 unsigned char status = GOT_STATUS_NO_CHANGE;
2362 unsigned char staged_status = get_staged_status(ie);
2363 struct stat sb;
2364 struct got_object_id blob_id, commit_id, staged_blob_id;
2365 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2366 struct got_object_id *staged_blob_idp = NULL;
2368 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2369 if (err)
2370 return err;
2372 if (status == GOT_STATUS_NO_CHANGE &&
2373 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2374 return NULL;
2376 if (got_fileindex_entry_has_blob(ie)) {
2377 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2378 blob_idp = &blob_id;
2380 if (got_fileindex_entry_has_commit(ie)) {
2381 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2382 commit_idp = &commit_id;
2384 if (staged_status == GOT_STATUS_ADD ||
2385 staged_status == GOT_STATUS_MODIFY) {
2386 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2387 SHA1_DIGEST_LENGTH);
2388 staged_blob_idp = &staged_blob_id;
2391 return (*status_cb)(status_arg, status, staged_status,
2392 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2395 static const struct got_error *
2396 status_old_new(void *arg, struct got_fileindex_entry *ie,
2397 struct dirent *de, const char *parent_path, int dirfd)
2399 const struct got_error *err = NULL;
2400 struct diff_dir_cb_arg *a = arg;
2401 char *abspath;
2403 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2404 return got_error(GOT_ERR_CANCELLED);
2406 if (got_path_cmp(parent_path, a->status_path,
2407 strlen(parent_path), a->status_path_len) != 0 &&
2408 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2409 return NULL;
2411 if (parent_path[0]) {
2412 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2413 parent_path, de->d_name) == -1)
2414 return got_error_from_errno("asprintf");
2415 } else {
2416 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2417 de->d_name) == -1)
2418 return got_error_from_errno("asprintf");
2421 err = report_file_status(ie, abspath, dirfd, de->d_name,
2422 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2423 free(abspath);
2424 return err;
2427 static const struct got_error *
2428 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2430 struct diff_dir_cb_arg *a = arg;
2431 struct got_object_id blob_id, commit_id;
2432 unsigned char status;
2434 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2435 return got_error(GOT_ERR_CANCELLED);
2437 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2438 return NULL;
2440 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2441 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2442 if (got_fileindex_entry_has_file_on_disk(ie))
2443 status = GOT_STATUS_MISSING;
2444 else
2445 status = GOT_STATUS_DELETE;
2446 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2447 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2450 void
2451 free_ignorelist(struct got_pathlist_head *ignorelist)
2453 struct got_pathlist_entry *pe;
2455 TAILQ_FOREACH(pe, ignorelist, entry)
2456 free((char *)pe->path);
2457 got_pathlist_free(ignorelist);
2460 void
2461 free_ignores(struct got_pathlist_head *ignores)
2463 struct got_pathlist_entry *pe;
2465 TAILQ_FOREACH(pe, ignores, entry) {
2466 struct got_pathlist_head *ignorelist = pe->data;
2467 free_ignorelist(ignorelist);
2468 free((char *)pe->path);
2470 got_pathlist_free(ignores);
2473 static const struct got_error *
2474 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2476 const struct got_error *err = NULL;
2477 struct got_pathlist_entry *pe = NULL;
2478 struct got_pathlist_head *ignorelist;
2479 char *line = NULL, *pattern, *dirpath = NULL;
2480 size_t linesize = 0;
2481 ssize_t linelen;
2483 ignorelist = calloc(1, sizeof(*ignorelist));
2484 if (ignorelist == NULL)
2485 return got_error_from_errno("calloc");
2486 TAILQ_INIT(ignorelist);
2488 while ((linelen = getline(&line, &linesize, f)) != -1) {
2489 if (linelen > 0 && line[linelen - 1] == '\n')
2490 line[linelen - 1] = '\0';
2492 /* Git's ignores may contain comments. */
2493 if (line[0] == '#')
2494 continue;
2496 /* Git's negated patterns are not (yet?) supported. */
2497 if (line[0] == '!')
2498 continue;
2500 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2501 line) == -1) {
2502 err = got_error_from_errno("asprintf");
2503 goto done;
2505 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2506 if (err)
2507 goto done;
2509 if (ferror(f)) {
2510 err = got_error_from_errno("getline");
2511 goto done;
2514 dirpath = strdup(path);
2515 if (dirpath == NULL) {
2516 err = got_error_from_errno("strdup");
2517 goto done;
2519 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2520 done:
2521 free(line);
2522 if (err || pe == NULL) {
2523 free(dirpath);
2524 free_ignorelist(ignorelist);
2526 return err;
2529 int
2530 match_ignores(struct got_pathlist_head *ignores, const char *path)
2532 struct got_pathlist_entry *pe;
2534 /* Handle patterns which match in all directories. */
2535 TAILQ_FOREACH(pe, ignores, entry) {
2536 struct got_pathlist_head *ignorelist = pe->data;
2537 struct got_pathlist_entry *pi;
2539 TAILQ_FOREACH(pi, ignorelist, entry) {
2540 const char *p, *pattern = pi->path;
2542 if (strncmp(pattern, "**/", 3) != 0)
2543 continue;
2544 pattern += 3;
2545 p = path;
2546 while (*p) {
2547 if (fnmatch(pattern, p,
2548 FNM_PATHNAME | FNM_LEADING_DIR)) {
2549 /* Retry in next directory. */
2550 while (*p && *p != '/')
2551 p++;
2552 while (*p == '/')
2553 p++;
2554 continue;
2556 return 1;
2562 * The ignores pathlist contains ignore lists from children before
2563 * parents, so we can find the most specific ignorelist by walking
2564 * ignores backwards.
2566 pe = TAILQ_LAST(ignores, got_pathlist_head);
2567 while (pe) {
2568 if (got_path_is_child(path, pe->path, pe->path_len)) {
2569 struct got_pathlist_head *ignorelist = pe->data;
2570 struct got_pathlist_entry *pi;
2571 TAILQ_FOREACH(pi, ignorelist, entry) {
2572 const char *pattern = pi->path;
2573 int flags = FNM_LEADING_DIR;
2574 if (strstr(pattern, "/**/") == NULL)
2575 flags |= FNM_PATHNAME;
2576 if (fnmatch(pattern, path, flags))
2577 continue;
2578 return 1;
2581 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2584 return 0;
2587 static const struct got_error *
2588 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2589 const char *path, int dirfd, const char *ignores_filename)
2591 const struct got_error *err = NULL;
2592 char *ignorespath;
2593 int fd = -1;
2594 FILE *ignoresfile = NULL;
2596 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2597 path[0] ? "/" : "", ignores_filename) == -1)
2598 return got_error_from_errno("asprintf");
2600 if (dirfd != -1) {
2601 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2602 if (fd == -1) {
2603 if (errno != ENOENT && errno != EACCES)
2604 err = got_error_from_errno2("openat",
2605 ignorespath);
2606 } else {
2607 ignoresfile = fdopen(fd, "r");
2608 if (ignoresfile == NULL)
2609 err = got_error_from_errno2("fdopen",
2610 ignorespath);
2611 else {
2612 fd = -1;
2613 err = read_ignores(ignores, path, ignoresfile);
2616 } else {
2617 ignoresfile = fopen(ignorespath, "r");
2618 if (ignoresfile == NULL) {
2619 if (errno != ENOENT && errno != EACCES)
2620 err = got_error_from_errno2("fopen",
2621 ignorespath);
2622 } else
2623 err = read_ignores(ignores, path, ignoresfile);
2626 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2627 err = got_error_from_errno2("fclose", path);
2628 if (fd != -1 && close(fd) == -1 && err == NULL)
2629 err = got_error_from_errno2("close", path);
2630 free(ignorespath);
2631 return err;
2634 static const struct got_error *
2635 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2637 const struct got_error *err = NULL;
2638 struct diff_dir_cb_arg *a = arg;
2639 char *path = NULL;
2641 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2642 return got_error(GOT_ERR_CANCELLED);
2644 /* XXX ignore symlinks for now */
2645 if (de->d_type == DT_LNK)
2646 return NULL;
2648 if (parent_path[0]) {
2649 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2650 return got_error_from_errno("asprintf");
2651 } else {
2652 path = de->d_name;
2655 if (de->d_type == DT_DIR) {
2656 int subdirfd = openat(dirfd, de->d_name,
2657 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2658 if (subdirfd == -1) {
2659 if (errno != ENOENT && errno != EACCES)
2660 err = got_error_from_errno2("openat", path);
2661 } else {
2662 err = add_ignores(&a->ignores, a->worktree->root_path,
2663 path, subdirfd, ".cvsignore");
2664 if (err == NULL)
2665 err = add_ignores(&a->ignores,
2666 a->worktree->root_path, path,
2667 subdirfd, ".gitignore");
2668 if (close(subdirfd) == -1 && err == NULL)
2669 err = got_error_from_errno2("close", path);
2671 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2672 && !match_ignores(&a->ignores, path))
2673 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2674 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2675 if (parent_path[0])
2676 free(path);
2677 return err;
2680 static const struct got_error *
2681 report_single_file_status(const char *path, const char *ondisk_path,
2682 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2683 void *status_arg, struct got_repository *repo, int report_unchanged)
2685 struct got_fileindex_entry *ie;
2686 struct stat sb;
2688 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2689 if (ie)
2690 return report_file_status(ie, ondisk_path, -1, NULL,
2691 status_cb, status_arg, repo, report_unchanged);
2693 if (lstat(ondisk_path, &sb) == -1) {
2694 if (errno != ENOENT)
2695 return got_error_from_errno2("lstat", ondisk_path);
2696 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2697 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2698 return NULL;
2701 if (S_ISREG(sb.st_mode))
2702 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2703 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2705 return NULL;
2708 static const struct got_error *
2709 worktree_status(struct got_worktree *worktree, const char *path,
2710 struct got_fileindex *fileindex, struct got_repository *repo,
2711 got_worktree_status_cb status_cb, void *status_arg,
2712 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2713 int report_unchanged)
2715 const struct got_error *err = NULL;
2716 int fd = -1;
2717 struct got_fileindex_diff_dir_cb fdiff_cb;
2718 struct diff_dir_cb_arg arg;
2719 char *ondisk_path = NULL;
2721 if (asprintf(&ondisk_path, "%s%s%s",
2722 worktree->root_path, path[0] ? "/" : "", path) == -1)
2723 return got_error_from_errno("asprintf");
2725 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2726 if (fd == -1) {
2727 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2728 err = got_error_from_errno2("open", ondisk_path);
2729 else
2730 err = report_single_file_status(path, ondisk_path,
2731 fileindex, status_cb, status_arg, repo,
2732 report_unchanged);
2733 } else {
2734 fdiff_cb.diff_old_new = status_old_new;
2735 fdiff_cb.diff_old = status_old;
2736 fdiff_cb.diff_new = status_new;
2737 arg.fileindex = fileindex;
2738 arg.worktree = worktree;
2739 arg.status_path = path;
2740 arg.status_path_len = strlen(path);
2741 arg.repo = repo;
2742 arg.status_cb = status_cb;
2743 arg.status_arg = status_arg;
2744 arg.cancel_cb = cancel_cb;
2745 arg.cancel_arg = cancel_arg;
2746 arg.report_unchanged = report_unchanged;
2747 TAILQ_INIT(&arg.ignores);
2748 if (!no_ignores) {
2749 err = add_ignores(&arg.ignores, worktree->root_path,
2750 path, fd, ".cvsignore");
2751 if (err == NULL)
2752 err = add_ignores(&arg.ignores,
2753 worktree->root_path, path, fd,
2754 ".gitignore");
2756 if (err == NULL)
2757 err = got_fileindex_diff_dir(fileindex, fd,
2758 worktree->root_path, path, repo, &fdiff_cb, &arg);
2759 free_ignores(&arg.ignores);
2762 if (fd != -1 && close(fd) != 0 && err == NULL)
2763 err = got_error_from_errno("close");
2764 free(ondisk_path);
2765 return err;
2768 const struct got_error *
2769 got_worktree_status(struct got_worktree *worktree,
2770 struct got_pathlist_head *paths, struct got_repository *repo,
2771 got_worktree_status_cb status_cb, void *status_arg,
2772 got_cancel_cb cancel_cb, void *cancel_arg)
2774 const struct got_error *err = NULL;
2775 char *fileindex_path = NULL;
2776 struct got_fileindex *fileindex = NULL;
2777 struct got_pathlist_entry *pe;
2779 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2780 if (err)
2781 return err;
2783 TAILQ_FOREACH(pe, paths, entry) {
2784 err = worktree_status(worktree, pe->path, fileindex, repo,
2785 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2786 if (err)
2787 break;
2789 free(fileindex_path);
2790 got_fileindex_free(fileindex);
2791 return err;
2794 const struct got_error *
2795 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2796 const char *arg)
2798 const struct got_error *err = NULL;
2799 char *resolved, *cwd = NULL, *path = NULL;
2800 size_t len;
2802 *wt_path = NULL;
2804 resolved = realpath(arg, NULL);
2805 if (resolved == NULL) {
2806 if (errno != ENOENT)
2807 return got_error_from_errno2("realpath", arg);
2808 cwd = getcwd(NULL, 0);
2809 if (cwd == NULL)
2810 return got_error_from_errno("getcwd");
2811 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2812 err = got_error_from_errno("asprintf");
2813 goto done;
2817 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2818 strlen(got_worktree_get_root_path(worktree)))) {
2819 err = got_error(GOT_ERR_BAD_PATH);
2820 goto done;
2823 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2824 err = got_path_skip_common_ancestor(&path,
2825 got_worktree_get_root_path(worktree), resolved);
2826 if (err)
2827 goto done;
2828 } else {
2829 path = strdup("");
2830 if (path == NULL) {
2831 err = got_error_from_errno("strdup");
2832 goto done;
2836 /* XXX status walk can't deal with trailing slash! */
2837 len = strlen(path);
2838 while (len > 0 && path[len - 1] == '/') {
2839 path[len - 1] = '\0';
2840 len--;
2842 done:
2843 free(resolved);
2844 free(cwd);
2845 if (err == NULL)
2846 *wt_path = path;
2847 else
2848 free(path);
2849 return err;
2852 struct schedule_addition_args {
2853 struct got_worktree *worktree;
2854 struct got_fileindex *fileindex;
2855 got_worktree_checkout_cb progress_cb;
2856 void *progress_arg;
2857 struct got_repository *repo;
2860 static const struct got_error *
2861 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2862 const char *relpath, struct got_object_id *blob_id,
2863 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2864 int dirfd, const char *de_name)
2866 struct schedule_addition_args *a = arg;
2867 const struct got_error *err = NULL;
2868 struct got_fileindex_entry *ie;
2869 struct stat sb;
2870 char *ondisk_path;
2872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2873 relpath) == -1)
2874 return got_error_from_errno("asprintf");
2876 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2877 if (ie) {
2878 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2879 de_name, a->repo);
2880 if (err)
2881 goto done;
2882 /* Re-adding an existing entry is a no-op. */
2883 if (status == GOT_STATUS_ADD)
2884 goto done;
2885 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2886 if (err)
2887 goto done;
2890 if (status != GOT_STATUS_UNVERSIONED) {
2891 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2892 goto done;
2895 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2896 if (err)
2897 goto done;
2899 err = got_fileindex_entry_add(a->fileindex, ie);
2900 if (err) {
2901 got_fileindex_entry_free(ie);
2902 goto done;
2904 done:
2905 free(ondisk_path);
2906 if (err)
2907 return err;
2908 if (status == GOT_STATUS_ADD)
2909 return NULL;
2910 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2913 const struct got_error *
2914 got_worktree_schedule_add(struct got_worktree *worktree,
2915 struct got_pathlist_head *paths,
2916 got_worktree_checkout_cb progress_cb, void *progress_arg,
2917 struct got_repository *repo, int no_ignores)
2919 struct got_fileindex *fileindex = NULL;
2920 char *fileindex_path = NULL;
2921 const struct got_error *err = NULL, *sync_err, *unlockerr;
2922 struct got_pathlist_entry *pe;
2923 struct schedule_addition_args saa;
2925 err = lock_worktree(worktree, LOCK_EX);
2926 if (err)
2927 return err;
2929 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2930 if (err)
2931 goto done;
2933 saa.worktree = worktree;
2934 saa.fileindex = fileindex;
2935 saa.progress_cb = progress_cb;
2936 saa.progress_arg = progress_arg;
2937 saa.repo = repo;
2939 TAILQ_FOREACH(pe, paths, entry) {
2940 err = worktree_status(worktree, pe->path, fileindex, repo,
2941 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2942 if (err)
2943 break;
2945 sync_err = sync_fileindex(fileindex, fileindex_path);
2946 if (sync_err && err == NULL)
2947 err = sync_err;
2948 done:
2949 free(fileindex_path);
2950 if (fileindex)
2951 got_fileindex_free(fileindex);
2952 unlockerr = lock_worktree(worktree, LOCK_SH);
2953 if (unlockerr && err == NULL)
2954 err = unlockerr;
2955 return err;
2958 struct schedule_deletion_args {
2959 struct got_worktree *worktree;
2960 struct got_fileindex *fileindex;
2961 got_worktree_delete_cb progress_cb;
2962 void *progress_arg;
2963 struct got_repository *repo;
2964 int delete_local_mods;
2965 int keep_on_disk;
2968 static const struct got_error *
2969 schedule_for_deletion(void *arg, unsigned char status,
2970 unsigned char staged_status, const char *relpath,
2971 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2972 struct got_object_id *commit_id, int dirfd, const char *de_name)
2974 struct schedule_deletion_args *a = arg;
2975 const struct got_error *err = NULL;
2976 struct got_fileindex_entry *ie = NULL;
2977 struct stat sb;
2978 char *ondisk_path;
2980 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2981 if (ie == NULL)
2982 return got_error(GOT_ERR_BAD_PATH);
2984 staged_status = get_staged_status(ie);
2985 if (staged_status != GOT_STATUS_NO_CHANGE) {
2986 if (staged_status == GOT_STATUS_DELETE)
2987 return NULL;
2988 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2991 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2992 relpath) == -1)
2993 return got_error_from_errno("asprintf");
2995 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2996 a->repo);
2997 if (err)
2998 goto done;
3000 if (status != GOT_STATUS_NO_CHANGE) {
3001 if (status == GOT_STATUS_DELETE)
3002 goto done;
3003 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3004 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3005 goto done;
3007 if (status != GOT_STATUS_MODIFY &&
3008 status != GOT_STATUS_MISSING) {
3009 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3010 goto done;
3014 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3015 if (dirfd != -1) {
3016 if (unlinkat(dirfd, de_name, 0) != 0) {
3017 err = got_error_from_errno2("unlinkat",
3018 ondisk_path);
3019 goto done;
3021 } else if (unlink(ondisk_path) != 0) {
3022 err = got_error_from_errno2("unlink", ondisk_path);
3023 goto done;
3027 got_fileindex_entry_mark_deleted_from_disk(ie);
3028 done:
3029 free(ondisk_path);
3030 if (err)
3031 return err;
3032 if (status == GOT_STATUS_DELETE)
3033 return NULL;
3034 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3035 staged_status, relpath);
3038 const struct got_error *
3039 got_worktree_schedule_delete(struct got_worktree *worktree,
3040 struct got_pathlist_head *paths, int delete_local_mods,
3041 got_worktree_delete_cb progress_cb, void *progress_arg,
3042 struct got_repository *repo, int keep_on_disk)
3044 struct got_fileindex *fileindex = NULL;
3045 char *fileindex_path = NULL;
3046 const struct got_error *err = NULL, *sync_err, *unlockerr;
3047 struct got_pathlist_entry *pe;
3048 struct schedule_deletion_args sda;
3050 err = lock_worktree(worktree, LOCK_EX);
3051 if (err)
3052 return err;
3054 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3055 if (err)
3056 goto done;
3058 sda.worktree = worktree;
3059 sda.fileindex = fileindex;
3060 sda.progress_cb = progress_cb;
3061 sda.progress_arg = progress_arg;
3062 sda.repo = repo;
3063 sda.delete_local_mods = delete_local_mods;
3064 sda.keep_on_disk = keep_on_disk;
3066 TAILQ_FOREACH(pe, paths, entry) {
3067 err = worktree_status(worktree, pe->path, fileindex, repo,
3068 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3069 if (err)
3070 break;
3072 sync_err = sync_fileindex(fileindex, fileindex_path);
3073 if (sync_err && err == NULL)
3074 err = sync_err;
3075 done:
3076 free(fileindex_path);
3077 if (fileindex)
3078 got_fileindex_free(fileindex);
3079 unlockerr = lock_worktree(worktree, LOCK_SH);
3080 if (unlockerr && err == NULL)
3081 err = unlockerr;
3082 return err;
3085 static const struct got_error *
3086 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3088 const struct got_error *err = NULL;
3089 char *line = NULL;
3090 size_t linesize = 0, n;
3091 ssize_t linelen;
3093 linelen = getline(&line, &linesize, infile);
3094 if (linelen == -1) {
3095 if (ferror(infile)) {
3096 err = got_error_from_errno("getline");
3097 goto done;
3099 return NULL;
3101 if (outfile) {
3102 n = fwrite(line, 1, linelen, outfile);
3103 if (n != linelen) {
3104 err = got_ferror(outfile, GOT_ERR_IO);
3105 goto done;
3108 if (rejectfile) {
3109 n = fwrite(line, 1, linelen, rejectfile);
3110 if (n != linelen)
3111 err = got_ferror(outfile, GOT_ERR_IO);
3113 done:
3114 free(line);
3115 return err;
3118 static const struct got_error *
3119 skip_one_line(FILE *f)
3121 char *line = NULL;
3122 size_t linesize = 0;
3123 ssize_t linelen;
3125 linelen = getline(&line, &linesize, f);
3126 if (linelen == -1) {
3127 if (ferror(f))
3128 return got_error_from_errno("getline");
3129 return NULL;
3131 free(line);
3132 return NULL;
3135 static const struct got_error *
3136 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3137 int start_old, int end_old, int start_new, int end_new,
3138 FILE *outfile, FILE *rejectfile)
3140 const struct got_error *err;
3142 /* Copy old file's lines leading up to patch. */
3143 while (!feof(f1) && *line_cur1 < start_old) {
3144 err = copy_one_line(f1, outfile, NULL);
3145 if (err)
3146 return err;
3147 (*line_cur1)++;
3149 /* Skip new file's lines leading up to patch. */
3150 while (!feof(f2) && *line_cur2 < start_new) {
3151 if (rejectfile)
3152 err = copy_one_line(f2, NULL, rejectfile);
3153 else
3154 err = skip_one_line(f2);
3155 if (err)
3156 return err;
3157 (*line_cur2)++;
3159 /* Copy patched lines. */
3160 while (!feof(f2) && *line_cur2 <= end_new) {
3161 err = copy_one_line(f2, outfile, NULL);
3162 if (err)
3163 return err;
3164 (*line_cur2)++;
3166 /* Skip over old file's replaced lines. */
3167 while (!feof(f1) && *line_cur1 <= end_old) {
3168 if (rejectfile)
3169 err = copy_one_line(f1, NULL, rejectfile);
3170 else
3171 err = skip_one_line(f1);
3172 if (err)
3173 return err;
3174 (*line_cur1)++;
3177 return NULL;
3180 static const struct got_error *
3181 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3182 FILE *outfile, FILE *rejectfile)
3184 const struct got_error *err;
3186 if (outfile) {
3187 /* Copy old file's lines until EOF. */
3188 while (!feof(f1)) {
3189 err = copy_one_line(f1, outfile, NULL);
3190 if (err)
3191 return err;
3192 (*line_cur1)++;
3195 if (rejectfile) {
3196 /* Copy new file's lines until EOF. */
3197 while (!feof(f2)) {
3198 err = copy_one_line(f2, NULL, rejectfile);
3199 if (err)
3200 return err;
3201 (*line_cur2)++;
3205 return NULL;
3208 static const struct got_error *
3209 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3210 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3211 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3212 int *line_cur2, FILE *outfile, FILE *rejectfile,
3213 got_worktree_patch_cb patch_cb, void *patch_arg)
3215 const struct got_error *err = NULL;
3216 int start_old = change->cv.a;
3217 int end_old = change->cv.b;
3218 int start_new = change->cv.c;
3219 int end_new = change->cv.d;
3220 long pos1, pos2;
3221 FILE *hunkfile;
3223 *choice = GOT_PATCH_CHOICE_NONE;
3225 hunkfile = got_opentemp();
3226 if (hunkfile == NULL)
3227 return got_error_from_errno("got_opentemp");
3229 pos1 = ftell(f1);
3230 pos2 = ftell(f2);
3232 /* XXX TODO needs error checking */
3233 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3235 if (fseek(f1, pos1, SEEK_SET) == -1) {
3236 err = got_ferror(f1, GOT_ERR_IO);
3237 goto done;
3239 if (fseek(f2, pos2, SEEK_SET) == -1) {
3240 err = got_ferror(f1, GOT_ERR_IO);
3241 goto done;
3243 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3244 err = got_ferror(hunkfile, GOT_ERR_IO);
3245 goto done;
3248 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3249 hunkfile, n, nchanges);
3250 if (err)
3251 goto done;
3253 switch (*choice) {
3254 case GOT_PATCH_CHOICE_YES:
3255 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3256 end_old, start_new, end_new, outfile, rejectfile);
3257 break;
3258 case GOT_PATCH_CHOICE_NO:
3259 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3260 end_old, start_new, end_new, rejectfile, outfile);
3261 break;
3262 case GOT_PATCH_CHOICE_QUIT:
3263 break;
3264 default:
3265 err = got_error(GOT_ERR_PATCH_CHOICE);
3266 break;
3268 done:
3269 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3270 err = got_error_from_errno("fclose");
3271 return err;
3274 struct revert_file_args {
3275 struct got_worktree *worktree;
3276 struct got_fileindex *fileindex;
3277 got_worktree_checkout_cb progress_cb;
3278 void *progress_arg;
3279 got_worktree_patch_cb patch_cb;
3280 void *patch_arg;
3281 struct got_repository *repo;
3284 static const struct got_error *
3285 create_patched_content(char **path_outfile, int reverse_patch,
3286 struct got_object_id *blob_id, const char *path2,
3287 int dirfd2, const char *de_name2,
3288 const char *relpath, struct got_repository *repo,
3289 got_worktree_patch_cb patch_cb, void *patch_arg)
3291 const struct got_error *err;
3292 struct got_blob_object *blob = NULL;
3293 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3294 int fd2 = -1;
3295 char *path1 = NULL, *id_str = NULL;
3296 struct stat sb1, sb2;
3297 struct got_diff_changes *changes = NULL;
3298 struct got_diff_state *ds = NULL;
3299 struct got_diff_args *args = NULL;
3300 struct got_diff_change *change;
3301 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3302 int n = 0;
3304 *path_outfile = NULL;
3306 err = got_object_id_str(&id_str, blob_id);
3307 if (err)
3308 return err;
3310 if (dirfd2 != -1) {
3311 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3312 if (fd2 == -1) {
3313 err = got_error_from_errno2("openat", path2);
3314 goto done;
3316 } else {
3317 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3318 if (fd2 == -1) {
3319 err = got_error_from_errno2("open", path2);
3320 goto done;
3323 if (fstat(fd2, &sb2) == -1) {
3324 err = got_error_from_errno2("fstat", path2);
3325 goto done;
3328 f2 = fdopen(fd2, "r");
3329 if (f2 == NULL) {
3330 err = got_error_from_errno2("fdopen", path2);
3331 goto done;
3333 fd2 = -1;
3335 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3336 if (err)
3337 goto done;
3339 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3340 if (err)
3341 goto done;
3343 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3344 if (err)
3345 goto done;
3347 if (stat(path1, &sb1) == -1) {
3348 err = got_error_from_errno2("stat", path1);
3349 goto done;
3352 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3353 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3354 if (err)
3355 goto done;
3357 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3358 if (err)
3359 goto done;
3361 if (fseek(f1, 0L, SEEK_SET) == -1)
3362 return got_ferror(f1, GOT_ERR_IO);
3363 if (fseek(f2, 0L, SEEK_SET) == -1)
3364 return got_ferror(f2, GOT_ERR_IO);
3365 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3366 int choice;
3367 err = apply_or_reject_change(&choice, change, ++n,
3368 changes->nchanges, ds, args, diff_flags, relpath,
3369 f1, f2, &line_cur1, &line_cur2,
3370 reverse_patch ? NULL : outfile,
3371 reverse_patch ? outfile : NULL,
3372 patch_cb, patch_arg);
3373 if (err)
3374 goto done;
3375 if (choice == GOT_PATCH_CHOICE_YES)
3376 have_content = 1;
3377 else if (choice == GOT_PATCH_CHOICE_QUIT)
3378 break;
3380 if (have_content) {
3381 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3382 reverse_patch ? NULL : outfile,
3383 reverse_patch ? outfile : NULL);
3384 if (err)
3385 goto done;
3387 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3388 err = got_error_from_errno2("chmod", path2);
3389 goto done;
3392 done:
3393 free(id_str);
3394 if (blob)
3395 got_object_blob_close(blob);
3396 if (f1 && fclose(f1) == EOF && err == NULL)
3397 err = got_error_from_errno2("fclose", path1);
3398 if (f2 && fclose(f2) == EOF && err == NULL)
3399 err = got_error_from_errno2("fclose", path2);
3400 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3401 err = got_error_from_errno2("close", path2);
3402 if (outfile && fclose(outfile) == EOF && err == NULL)
3403 err = got_error_from_errno2("fclose", *path_outfile);
3404 if (path1 && unlink(path1) == -1 && err == NULL)
3405 err = got_error_from_errno2("unlink", path1);
3406 if (err || !have_content) {
3407 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3408 err = got_error_from_errno2("unlink", *path_outfile);
3409 free(*path_outfile);
3410 *path_outfile = NULL;
3412 free(args);
3413 if (ds) {
3414 got_diff_state_free(ds);
3415 free(ds);
3417 if (changes)
3418 got_diff_free_changes(changes);
3419 free(path1);
3420 return err;
3423 static const struct got_error *
3424 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3425 const char *relpath, struct got_object_id *blob_id,
3426 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3427 int dirfd, const char *de_name)
3429 struct revert_file_args *a = arg;
3430 const struct got_error *err = NULL;
3431 char *parent_path = NULL;
3432 struct got_fileindex_entry *ie;
3433 struct got_tree_object *tree = NULL;
3434 struct got_object_id *tree_id = NULL;
3435 const struct got_tree_entry *te = NULL;
3436 char *tree_path = NULL, *te_name;
3437 char *ondisk_path = NULL, *path_content = NULL;
3438 struct got_blob_object *blob = NULL;
3440 /* Reverting a staged deletion is a no-op. */
3441 if (status == GOT_STATUS_DELETE &&
3442 staged_status != GOT_STATUS_NO_CHANGE)
3443 return NULL;
3445 if (status == GOT_STATUS_UNVERSIONED)
3446 return (*a->progress_cb)(a->progress_arg,
3447 GOT_STATUS_UNVERSIONED, relpath);
3449 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3450 if (ie == NULL)
3451 return got_error(GOT_ERR_BAD_PATH);
3453 /* Construct in-repository path of tree which contains this blob. */
3454 err = got_path_dirname(&parent_path, ie->path);
3455 if (err) {
3456 if (err->code != GOT_ERR_BAD_PATH)
3457 goto done;
3458 parent_path = strdup("/");
3459 if (parent_path == NULL) {
3460 err = got_error_from_errno("strdup");
3461 goto done;
3464 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3465 tree_path = strdup(parent_path);
3466 if (tree_path == NULL) {
3467 err = got_error_from_errno("strdup");
3468 goto done;
3470 } else {
3471 if (got_path_is_root_dir(parent_path)) {
3472 tree_path = strdup(a->worktree->path_prefix);
3473 if (tree_path == NULL) {
3474 err = got_error_from_errno("strdup");
3475 goto done;
3477 } else {
3478 if (asprintf(&tree_path, "%s/%s",
3479 a->worktree->path_prefix, parent_path) == -1) {
3480 err = got_error_from_errno("asprintf");
3481 goto done;
3486 err = got_object_id_by_path(&tree_id, a->repo,
3487 a->worktree->base_commit_id, tree_path);
3488 if (err) {
3489 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3490 (status == GOT_STATUS_ADD ||
3491 staged_status == GOT_STATUS_ADD)))
3492 goto done;
3493 } else {
3494 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3495 if (err)
3496 goto done;
3498 te_name = basename(ie->path);
3499 if (te_name == NULL) {
3500 err = got_error_from_errno2("basename", ie->path);
3501 goto done;
3504 te = got_object_tree_find_entry(tree, te_name);
3505 if (te == NULL && status != GOT_STATUS_ADD &&
3506 staged_status != GOT_STATUS_ADD) {
3507 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3508 goto done;
3512 switch (status) {
3513 case GOT_STATUS_ADD:
3514 if (a->patch_cb) {
3515 int choice = GOT_PATCH_CHOICE_NONE;
3516 err = (*a->patch_cb)(&choice, a->patch_arg,
3517 status, ie->path, NULL, 1, 1);
3518 if (err)
3519 goto done;
3520 if (choice != GOT_PATCH_CHOICE_YES)
3521 break;
3523 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3524 ie->path);
3525 if (err)
3526 goto done;
3527 got_fileindex_entry_remove(a->fileindex, ie);
3528 break;
3529 case GOT_STATUS_DELETE:
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 /* fall through */
3540 case GOT_STATUS_MODIFY:
3541 case GOT_STATUS_MODE_CHANGE:
3542 case GOT_STATUS_CONFLICT:
3543 case GOT_STATUS_MISSING: {
3544 struct got_object_id id;
3545 if (staged_status == GOT_STATUS_ADD ||
3546 staged_status == GOT_STATUS_MODIFY) {
3547 memcpy(id.sha1, ie->staged_blob_sha1,
3548 SHA1_DIGEST_LENGTH);
3549 } else
3550 memcpy(id.sha1, ie->blob_sha1,
3551 SHA1_DIGEST_LENGTH);
3552 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3553 if (err)
3554 goto done;
3556 if (asprintf(&ondisk_path, "%s/%s",
3557 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3558 err = got_error_from_errno("asprintf");
3559 goto done;
3562 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3563 status == GOT_STATUS_CONFLICT)) {
3564 err = create_patched_content(&path_content, 1, &id,
3565 ondisk_path, dirfd, de_name, ie->path, a->repo,
3566 a->patch_cb, a->patch_arg);
3567 if (err || path_content == NULL)
3568 break;
3569 if (rename(path_content, ondisk_path) == -1) {
3570 err = got_error_from_errno3("rename",
3571 path_content, ondisk_path);
3572 goto done;
3574 } else {
3575 err = install_blob(a->worktree, ondisk_path, ie->path,
3576 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3577 got_fileindex_perms_to_st(ie), blob, 0, 1,
3578 a->repo, a->progress_cb, a->progress_arg);
3579 if (err)
3580 goto done;
3581 if (status == GOT_STATUS_DELETE ||
3582 status == GOT_STATUS_MODE_CHANGE) {
3583 err = update_blob_fileindex_entry(a->worktree,
3584 a->fileindex, ie, ondisk_path, ie->path,
3585 blob, 1);
3586 if (err)
3587 goto done;
3590 break;
3592 default:
3593 break;
3595 done:
3596 free(ondisk_path);
3597 free(path_content);
3598 free(parent_path);
3599 free(tree_path);
3600 if (blob)
3601 got_object_blob_close(blob);
3602 if (tree)
3603 got_object_tree_close(tree);
3604 free(tree_id);
3605 return err;
3608 const struct got_error *
3609 got_worktree_revert(struct got_worktree *worktree,
3610 struct got_pathlist_head *paths,
3611 got_worktree_checkout_cb progress_cb, void *progress_arg,
3612 got_worktree_patch_cb patch_cb, void *patch_arg,
3613 struct got_repository *repo)
3615 struct got_fileindex *fileindex = NULL;
3616 char *fileindex_path = NULL;
3617 const struct got_error *err = NULL, *unlockerr = NULL;
3618 const struct got_error *sync_err = NULL;
3619 struct got_pathlist_entry *pe;
3620 struct revert_file_args rfa;
3622 err = lock_worktree(worktree, LOCK_EX);
3623 if (err)
3624 return err;
3626 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3627 if (err)
3628 goto done;
3630 rfa.worktree = worktree;
3631 rfa.fileindex = fileindex;
3632 rfa.progress_cb = progress_cb;
3633 rfa.progress_arg = progress_arg;
3634 rfa.patch_cb = patch_cb;
3635 rfa.patch_arg = patch_arg;
3636 rfa.repo = repo;
3637 TAILQ_FOREACH(pe, paths, entry) {
3638 err = worktree_status(worktree, pe->path, fileindex, repo,
3639 revert_file, &rfa, NULL, NULL, 0, 0);
3640 if (err)
3641 break;
3643 sync_err = sync_fileindex(fileindex, fileindex_path);
3644 if (sync_err && err == NULL)
3645 err = sync_err;
3646 done:
3647 free(fileindex_path);
3648 if (fileindex)
3649 got_fileindex_free(fileindex);
3650 unlockerr = lock_worktree(worktree, LOCK_SH);
3651 if (unlockerr && err == NULL)
3652 err = unlockerr;
3653 return err;
3656 static void
3657 free_commitable(struct got_commitable *ct)
3659 free(ct->path);
3660 free(ct->in_repo_path);
3661 free(ct->ondisk_path);
3662 free(ct->blob_id);
3663 free(ct->base_blob_id);
3664 free(ct->staged_blob_id);
3665 free(ct->base_commit_id);
3666 free(ct);
3669 struct collect_commitables_arg {
3670 struct got_pathlist_head *commitable_paths;
3671 struct got_repository *repo;
3672 struct got_worktree *worktree;
3673 int have_staged_files;
3676 static const struct got_error *
3677 collect_commitables(void *arg, unsigned char status,
3678 unsigned char staged_status, const char *relpath,
3679 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3680 struct got_object_id *commit_id, int dirfd, const char *de_name)
3682 struct collect_commitables_arg *a = arg;
3683 const struct got_error *err = NULL;
3684 struct got_commitable *ct = NULL;
3685 struct got_pathlist_entry *new = NULL;
3686 char *parent_path = NULL, *path = NULL;
3687 struct stat sb;
3689 if (a->have_staged_files) {
3690 if (staged_status != GOT_STATUS_MODIFY &&
3691 staged_status != GOT_STATUS_ADD &&
3692 staged_status != GOT_STATUS_DELETE)
3693 return NULL;
3694 } else {
3695 if (status == GOT_STATUS_CONFLICT)
3696 return got_error(GOT_ERR_COMMIT_CONFLICT);
3698 if (status != GOT_STATUS_MODIFY &&
3699 status != GOT_STATUS_MODE_CHANGE &&
3700 status != GOT_STATUS_ADD &&
3701 status != GOT_STATUS_DELETE)
3702 return NULL;
3705 if (asprintf(&path, "/%s", relpath) == -1) {
3706 err = got_error_from_errno("asprintf");
3707 goto done;
3709 if (strcmp(path, "/") == 0) {
3710 parent_path = strdup("");
3711 if (parent_path == NULL)
3712 return got_error_from_errno("strdup");
3713 } else {
3714 err = got_path_dirname(&parent_path, path);
3715 if (err)
3716 return err;
3719 ct = calloc(1, sizeof(*ct));
3720 if (ct == NULL) {
3721 err = got_error_from_errno("calloc");
3722 goto done;
3725 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3726 relpath) == -1) {
3727 err = got_error_from_errno("asprintf");
3728 goto done;
3730 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3731 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3732 } else {
3733 if (dirfd != -1) {
3734 if (fstatat(dirfd, de_name, &sb,
3735 AT_SYMLINK_NOFOLLOW) == -1) {
3736 err = got_error_from_errno2("fstatat",
3737 ct->ondisk_path);
3738 goto done;
3740 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3741 err = got_error_from_errno2("lstat", ct->ondisk_path);
3742 goto done;
3744 ct->mode = sb.st_mode;
3747 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3748 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3749 relpath) == -1) {
3750 err = got_error_from_errno("asprintf");
3751 goto done;
3754 ct->status = status;
3755 ct->staged_status = staged_status;
3756 ct->blob_id = NULL; /* will be filled in when blob gets created */
3757 if (ct->status != GOT_STATUS_ADD &&
3758 ct->staged_status != GOT_STATUS_ADD) {
3759 ct->base_blob_id = got_object_id_dup(blob_id);
3760 if (ct->base_blob_id == NULL) {
3761 err = got_error_from_errno("got_object_id_dup");
3762 goto done;
3764 ct->base_commit_id = got_object_id_dup(commit_id);
3765 if (ct->base_commit_id == NULL) {
3766 err = got_error_from_errno("got_object_id_dup");
3767 goto done;
3770 if (ct->staged_status == GOT_STATUS_ADD ||
3771 ct->staged_status == GOT_STATUS_MODIFY) {
3772 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3773 if (ct->staged_blob_id == NULL) {
3774 err = got_error_from_errno("got_object_id_dup");
3775 goto done;
3778 ct->path = strdup(path);
3779 if (ct->path == NULL) {
3780 err = got_error_from_errno("strdup");
3781 goto done;
3783 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3784 done:
3785 if (ct && (err || new == NULL))
3786 free_commitable(ct);
3787 free(parent_path);
3788 free(path);
3789 return err;
3792 static const struct got_error *write_tree(struct got_object_id **,
3793 struct got_tree_object *, const char *, struct got_pathlist_head *,
3794 got_worktree_status_cb status_cb, void *status_arg,
3795 struct got_repository *);
3797 static const struct got_error *
3798 write_subtree(struct got_object_id **new_subtree_id,
3799 struct got_tree_entry *te, const char *parent_path,
3800 struct got_pathlist_head *commitable_paths,
3801 got_worktree_status_cb status_cb, void *status_arg,
3802 struct got_repository *repo)
3804 const struct got_error *err = NULL;
3805 struct got_tree_object *subtree;
3806 char *subpath;
3808 if (asprintf(&subpath, "%s%s%s", parent_path,
3809 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3810 return got_error_from_errno("asprintf");
3812 err = got_object_open_as_tree(&subtree, repo, &te->id);
3813 if (err)
3814 return err;
3816 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3817 status_cb, status_arg, repo);
3818 got_object_tree_close(subtree);
3819 free(subpath);
3820 return err;
3823 static const struct got_error *
3824 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3826 const struct got_error *err = NULL;
3827 char *ct_parent_path = NULL;
3829 *match = 0;
3831 if (strchr(ct->in_repo_path, '/') == NULL) {
3832 *match = got_path_is_root_dir(path);
3833 return NULL;
3836 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3837 if (err)
3838 return err;
3839 *match = (strcmp(path, ct_parent_path) == 0);
3840 free(ct_parent_path);
3841 return err;
3844 static mode_t
3845 get_ct_file_mode(struct got_commitable *ct)
3847 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3850 static const struct got_error *
3851 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3852 struct got_tree_entry *te, struct got_commitable *ct)
3854 const struct got_error *err = NULL;
3856 *new_te = NULL;
3858 err = got_object_tree_entry_dup(new_te, te);
3859 if (err)
3860 goto done;
3862 (*new_te)->mode = get_ct_file_mode(ct);
3864 if (ct->staged_status == GOT_STATUS_MODIFY)
3865 memcpy(&(*new_te)->id, ct->staged_blob_id,
3866 sizeof((*new_te)->id));
3867 else
3868 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3869 done:
3870 if (err && *new_te) {
3871 free(*new_te);
3872 *new_te = NULL;
3874 return err;
3877 static const struct got_error *
3878 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3879 struct got_commitable *ct)
3881 const struct got_error *err = NULL;
3882 char *ct_name;
3884 *new_te = NULL;
3886 *new_te = calloc(1, sizeof(**new_te));
3887 if (*new_te == NULL)
3888 return got_error_from_errno("calloc");
3890 ct_name = basename(ct->path);
3891 if (ct_name == NULL) {
3892 err = got_error_from_errno2("basename", ct->path);
3893 goto done;
3895 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3896 sizeof((*new_te)->name)) {
3897 err = got_error(GOT_ERR_NO_SPACE);
3898 goto done;
3901 (*new_te)->mode = get_ct_file_mode(ct);
3903 if (ct->staged_status == GOT_STATUS_ADD)
3904 memcpy(&(*new_te)->id, ct->staged_blob_id,
3905 sizeof((*new_te)->id));
3906 else
3907 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3908 done:
3909 if (err && *new_te) {
3910 free(*new_te);
3911 *new_te = NULL;
3913 return err;
3916 static const struct got_error *
3917 insert_tree_entry(struct got_tree_entry *new_te,
3918 struct got_pathlist_head *paths)
3920 const struct got_error *err = NULL;
3921 struct got_pathlist_entry *new_pe;
3923 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3924 if (err)
3925 return err;
3926 if (new_pe == NULL)
3927 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3928 return NULL;
3931 static const struct got_error *
3932 report_ct_status(struct got_commitable *ct,
3933 got_worktree_status_cb status_cb, void *status_arg)
3935 const char *ct_path = ct->path;
3936 unsigned char status;
3938 while (ct_path[0] == '/')
3939 ct_path++;
3941 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3942 status = ct->staged_status;
3943 else
3944 status = ct->status;
3946 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3947 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3950 static const struct got_error *
3951 match_modified_subtree(int *modified, struct got_tree_entry *te,
3952 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3954 const struct got_error *err = NULL;
3955 struct got_pathlist_entry *pe;
3956 char *te_path;
3958 *modified = 0;
3960 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3961 got_path_is_root_dir(base_tree_path) ? "" : "/",
3962 te->name) == -1)
3963 return got_error_from_errno("asprintf");
3965 TAILQ_FOREACH(pe, commitable_paths, entry) {
3966 struct got_commitable *ct = pe->data;
3967 *modified = got_path_is_child(ct->in_repo_path, te_path,
3968 strlen(te_path));
3969 if (*modified)
3970 break;
3973 free(te_path);
3974 return err;
3977 static const struct got_error *
3978 match_deleted_or_modified_ct(struct got_commitable **ctp,
3979 struct got_tree_entry *te, const char *base_tree_path,
3980 struct got_pathlist_head *commitable_paths)
3982 const struct got_error *err = NULL;
3983 struct got_pathlist_entry *pe;
3985 *ctp = NULL;
3987 TAILQ_FOREACH(pe, commitable_paths, entry) {
3988 struct got_commitable *ct = pe->data;
3989 char *ct_name = NULL;
3990 int path_matches;
3992 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3993 if (ct->status != GOT_STATUS_MODIFY &&
3994 ct->status != GOT_STATUS_MODE_CHANGE &&
3995 ct->status != GOT_STATUS_DELETE)
3996 continue;
3997 } else {
3998 if (ct->staged_status != GOT_STATUS_MODIFY &&
3999 ct->staged_status != GOT_STATUS_DELETE)
4000 continue;
4003 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4004 continue;
4006 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4007 if (err)
4008 return err;
4009 if (!path_matches)
4010 continue;
4012 ct_name = basename(pe->path);
4013 if (ct_name == NULL)
4014 return got_error_from_errno2("basename", pe->path);
4016 if (strcmp(te->name, ct_name) != 0)
4017 continue;
4019 *ctp = ct;
4020 break;
4023 return err;
4026 static const struct got_error *
4027 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4028 const char *child_path, const char *path_base_tree,
4029 struct got_pathlist_head *commitable_paths,
4030 got_worktree_status_cb status_cb, void *status_arg,
4031 struct got_repository *repo)
4033 const struct got_error *err = NULL;
4034 struct got_tree_entry *new_te;
4035 char *subtree_path;
4036 struct got_object_id *id = NULL;
4038 *new_tep = NULL;
4040 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4041 got_path_is_root_dir(path_base_tree) ? "" : "/",
4042 child_path) == -1)
4043 return got_error_from_errno("asprintf");
4045 new_te = calloc(1, sizeof(*new_te));
4046 if (new_te == NULL)
4047 return got_error_from_errno("calloc");
4048 new_te->mode = S_IFDIR;
4050 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4051 sizeof(new_te->name)) {
4052 err = got_error(GOT_ERR_NO_SPACE);
4053 goto done;
4055 err = write_tree(&id, NULL, subtree_path,
4056 commitable_paths, status_cb, status_arg, repo);
4057 if (err) {
4058 free(new_te);
4059 goto done;
4061 memcpy(&new_te->id, id, sizeof(new_te->id));
4062 done:
4063 free(id);
4064 free(subtree_path);
4065 if (err == NULL)
4066 *new_tep = new_te;
4067 return err;
4070 static const struct got_error *
4071 write_tree(struct got_object_id **new_tree_id,
4072 struct got_tree_object *base_tree, const char *path_base_tree,
4073 struct got_pathlist_head *commitable_paths,
4074 got_worktree_status_cb status_cb, void *status_arg,
4075 struct got_repository *repo)
4077 const struct got_error *err = NULL;
4078 struct got_pathlist_head paths;
4079 struct got_tree_entry *te, *new_te = NULL;
4080 struct got_pathlist_entry *pe;
4081 int nentries = 0;
4083 TAILQ_INIT(&paths);
4085 /* Insert, and recurse into, newly added entries first. */
4086 TAILQ_FOREACH(pe, commitable_paths, entry) {
4087 struct got_commitable *ct = pe->data;
4088 char *child_path = NULL, *slash;
4090 if ((ct->status != GOT_STATUS_ADD &&
4091 ct->staged_status != GOT_STATUS_ADD) ||
4092 (ct->flags & GOT_COMMITABLE_ADDED))
4093 continue;
4095 if (!got_path_is_child(pe->path, path_base_tree,
4096 strlen(path_base_tree)))
4097 continue;
4099 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4100 pe->path);
4101 if (err)
4102 goto done;
4104 slash = strchr(child_path, '/');
4105 if (slash == NULL) {
4106 err = alloc_added_blob_tree_entry(&new_te, ct);
4107 if (err)
4108 goto done;
4109 err = report_ct_status(ct, status_cb, status_arg);
4110 if (err)
4111 goto done;
4112 ct->flags |= GOT_COMMITABLE_ADDED;
4113 err = insert_tree_entry(new_te, &paths);
4114 if (err)
4115 goto done;
4116 nentries++;
4117 } else {
4118 *slash = '\0'; /* trim trailing path components */
4119 if (base_tree == NULL ||
4120 got_object_tree_find_entry(base_tree, child_path)
4121 == NULL) {
4122 err = make_subtree_for_added_blob(&new_te,
4123 child_path, path_base_tree,
4124 commitable_paths, status_cb, status_arg,
4125 repo);
4126 if (err)
4127 goto done;
4128 err = insert_tree_entry(new_te, &paths);
4129 if (err)
4130 goto done;
4131 nentries++;
4136 if (base_tree) {
4137 int i, nbase_entries;
4138 /* Handle modified and deleted entries. */
4139 nbase_entries = got_object_tree_get_nentries(base_tree);
4140 for (i = 0; i < nbase_entries; i++) {
4141 struct got_commitable *ct = NULL;
4143 te = got_object_tree_get_entry(base_tree, i);
4144 if (got_object_tree_entry_is_submodule(te)) {
4145 /* Entry is a submodule; just copy it. */
4146 err = got_object_tree_entry_dup(&new_te, te);
4147 if (err)
4148 goto done;
4149 err = insert_tree_entry(new_te, &paths);
4150 if (err)
4151 goto done;
4152 nentries++;
4153 continue;
4156 if (S_ISDIR(te->mode)) {
4157 int modified;
4158 err = got_object_tree_entry_dup(&new_te, te);
4159 if (err)
4160 goto done;
4161 err = match_modified_subtree(&modified, te,
4162 path_base_tree, commitable_paths);
4163 if (err)
4164 goto done;
4165 /* Avoid recursion into unmodified subtrees. */
4166 if (modified) {
4167 struct got_object_id *new_id;
4168 err = write_subtree(&new_id, te,
4169 path_base_tree, commitable_paths,
4170 status_cb, status_arg, repo);
4171 if (err)
4172 goto done;
4173 memcpy(&new_te->id, new_id,
4174 sizeof(new_te->id));
4175 free(new_id);
4177 err = insert_tree_entry(new_te, &paths);
4178 if (err)
4179 goto done;
4180 nentries++;
4181 continue;
4184 err = match_deleted_or_modified_ct(&ct, te,
4185 path_base_tree, commitable_paths);
4186 if (err)
4187 goto done;
4188 if (ct) {
4189 /* NB: Deleted entries get dropped here. */
4190 if (ct->status == GOT_STATUS_MODIFY ||
4191 ct->status == GOT_STATUS_MODE_CHANGE ||
4192 ct->staged_status == GOT_STATUS_MODIFY) {
4193 err = alloc_modified_blob_tree_entry(
4194 &new_te, te, ct);
4195 if (err)
4196 goto done;
4197 err = insert_tree_entry(new_te, &paths);
4198 if (err)
4199 goto done;
4200 nentries++;
4202 err = report_ct_status(ct, status_cb,
4203 status_arg);
4204 if (err)
4205 goto done;
4206 } else {
4207 /* Entry is unchanged; just copy it. */
4208 err = got_object_tree_entry_dup(&new_te, te);
4209 if (err)
4210 goto done;
4211 err = insert_tree_entry(new_te, &paths);
4212 if (err)
4213 goto done;
4214 nentries++;
4219 /* Write new list of entries; deleted entries have been dropped. */
4220 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4221 done:
4222 got_pathlist_free(&paths);
4223 return err;
4226 static const struct got_error *
4227 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4228 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4229 int have_staged_files)
4231 const struct got_error *err = NULL;
4232 struct got_pathlist_entry *pe;
4234 TAILQ_FOREACH(pe, commitable_paths, entry) {
4235 struct got_fileindex_entry *ie;
4236 struct got_commitable *ct = pe->data;
4238 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4239 if (ie) {
4240 if (ct->status == GOT_STATUS_DELETE ||
4241 ct->staged_status == GOT_STATUS_DELETE) {
4242 got_fileindex_entry_remove(fileindex, ie);
4243 got_fileindex_entry_free(ie);
4244 } else if (ct->staged_status == GOT_STATUS_ADD ||
4245 ct->staged_status == GOT_STATUS_MODIFY) {
4246 got_fileindex_entry_stage_set(ie,
4247 GOT_FILEIDX_STAGE_NONE);
4248 err = got_fileindex_entry_update(ie,
4249 ct->ondisk_path, ct->staged_blob_id->sha1,
4250 new_base_commit_id->sha1,
4251 !have_staged_files);
4252 } else
4253 err = got_fileindex_entry_update(ie,
4254 ct->ondisk_path, ct->blob_id->sha1,
4255 new_base_commit_id->sha1,
4256 !have_staged_files);
4257 } else {
4258 err = got_fileindex_entry_alloc(&ie,
4259 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4260 new_base_commit_id->sha1);
4261 if (err)
4262 break;
4263 err = got_fileindex_entry_add(fileindex, ie);
4264 if (err)
4265 break;
4268 return err;
4272 static const struct got_error *
4273 check_out_of_date(const char *in_repo_path, unsigned char status,
4274 unsigned char staged_status, struct got_object_id *base_blob_id,
4275 struct got_object_id *base_commit_id,
4276 struct got_object_id *head_commit_id, struct got_repository *repo,
4277 int ood_errcode)
4279 const struct got_error *err = NULL;
4280 struct got_object_id *id = NULL;
4282 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4283 /* Trivial case: base commit == head commit */
4284 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4285 return NULL;
4287 * Ensure file content which local changes were based
4288 * on matches file content in the branch head.
4290 err = got_object_id_by_path(&id, repo, head_commit_id,
4291 in_repo_path);
4292 if (err) {
4293 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4294 err = got_error(ood_errcode);
4295 goto done;
4296 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4297 err = got_error(ood_errcode);
4298 } else {
4299 /* Require that added files don't exist in the branch head. */
4300 err = got_object_id_by_path(&id, repo, head_commit_id,
4301 in_repo_path);
4302 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4303 goto done;
4304 err = id ? got_error(ood_errcode) : NULL;
4306 done:
4307 free(id);
4308 return err;
4311 const struct got_error *
4312 commit_worktree(struct got_object_id **new_commit_id,
4313 struct got_pathlist_head *commitable_paths,
4314 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4315 const char *author, const char *committer,
4316 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4317 got_worktree_status_cb status_cb, void *status_arg,
4318 struct got_repository *repo)
4320 const struct got_error *err = NULL, *unlockerr = NULL;
4321 struct got_pathlist_entry *pe;
4322 const char *head_ref_name = NULL;
4323 struct got_commit_object *head_commit = NULL;
4324 struct got_reference *head_ref2 = NULL;
4325 struct got_object_id *head_commit_id2 = NULL;
4326 struct got_tree_object *head_tree = NULL;
4327 struct got_object_id *new_tree_id = NULL;
4328 struct got_object_id_queue parent_ids;
4329 struct got_object_qid *pid = NULL;
4330 char *logmsg = NULL;
4332 *new_commit_id = NULL;
4334 SIMPLEQ_INIT(&parent_ids);
4336 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4337 if (err)
4338 goto done;
4340 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4341 if (err)
4342 goto done;
4344 if (commit_msg_cb != NULL) {
4345 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4346 if (err)
4347 goto done;
4350 if (logmsg == NULL || strlen(logmsg) == 0) {
4351 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4352 goto done;
4355 /* Create blobs from added and modified files and record their IDs. */
4356 TAILQ_FOREACH(pe, commitable_paths, entry) {
4357 struct got_commitable *ct = pe->data;
4358 char *ondisk_path;
4360 /* Blobs for staged files already exist. */
4361 if (ct->staged_status == GOT_STATUS_ADD ||
4362 ct->staged_status == GOT_STATUS_MODIFY)
4363 continue;
4365 if (ct->status != GOT_STATUS_ADD &&
4366 ct->status != GOT_STATUS_MODIFY &&
4367 ct->status != GOT_STATUS_MODE_CHANGE)
4368 continue;
4370 if (asprintf(&ondisk_path, "%s/%s",
4371 worktree->root_path, pe->path) == -1) {
4372 err = got_error_from_errno("asprintf");
4373 goto done;
4375 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4376 free(ondisk_path);
4377 if (err)
4378 goto done;
4381 /* Recursively write new tree objects. */
4382 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4383 status_cb, status_arg, repo);
4384 if (err)
4385 goto done;
4387 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4388 if (err)
4389 goto done;
4390 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4391 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4392 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4393 got_object_qid_free(pid);
4394 if (logmsg != NULL)
4395 free(logmsg);
4396 if (err)
4397 goto done;
4399 /* Check if a concurrent commit to our branch has occurred. */
4400 head_ref_name = got_worktree_get_head_ref_name(worktree);
4401 if (head_ref_name == NULL) {
4402 err = got_error_from_errno("got_worktree_get_head_ref_name");
4403 goto done;
4405 /* Lock the reference here to prevent concurrent modification. */
4406 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4407 if (err)
4408 goto done;
4409 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4410 if (err)
4411 goto done;
4412 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4413 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4414 goto done;
4416 /* Update branch head in repository. */
4417 err = got_ref_change_ref(head_ref2, *new_commit_id);
4418 if (err)
4419 goto done;
4420 err = got_ref_write(head_ref2, repo);
4421 if (err)
4422 goto done;
4424 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4425 if (err)
4426 goto done;
4428 err = ref_base_commit(worktree, repo);
4429 if (err)
4430 goto done;
4431 done:
4432 if (head_tree)
4433 got_object_tree_close(head_tree);
4434 if (head_commit)
4435 got_object_commit_close(head_commit);
4436 free(head_commit_id2);
4437 if (head_ref2) {
4438 unlockerr = got_ref_unlock(head_ref2);
4439 if (unlockerr && err == NULL)
4440 err = unlockerr;
4441 got_ref_close(head_ref2);
4443 return err;
4446 static const struct got_error *
4447 check_path_is_commitable(const char *path,
4448 struct got_pathlist_head *commitable_paths)
4450 struct got_pathlist_entry *cpe = NULL;
4451 size_t path_len = strlen(path);
4453 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4454 struct got_commitable *ct = cpe->data;
4455 const char *ct_path = ct->path;
4457 while (ct_path[0] == '/')
4458 ct_path++;
4460 if (strcmp(path, ct_path) == 0 ||
4461 got_path_is_child(ct_path, path, path_len))
4462 break;
4465 if (cpe == NULL)
4466 return got_error_path(path, GOT_ERR_BAD_PATH);
4468 return NULL;
4471 static const struct got_error *
4472 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4474 int *have_staged_files = arg;
4476 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4477 *have_staged_files = 1;
4478 return got_error(GOT_ERR_CANCELLED);
4481 return NULL;
4484 static const struct got_error *
4485 check_non_staged_files(struct got_fileindex *fileindex,
4486 struct got_pathlist_head *paths)
4488 struct got_pathlist_entry *pe;
4489 struct got_fileindex_entry *ie;
4491 TAILQ_FOREACH(pe, paths, entry) {
4492 if (pe->path[0] == '\0')
4493 continue;
4494 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4495 if (ie == NULL)
4496 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4497 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4498 return got_error_path(pe->path,
4499 GOT_ERR_FILE_NOT_STAGED);
4502 return NULL;
4505 const struct got_error *
4506 got_worktree_commit(struct got_object_id **new_commit_id,
4507 struct got_worktree *worktree, struct got_pathlist_head *paths,
4508 const char *author, const char *committer,
4509 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4510 got_worktree_status_cb status_cb, void *status_arg,
4511 struct got_repository *repo)
4513 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4514 struct got_fileindex *fileindex = NULL;
4515 char *fileindex_path = NULL;
4516 struct got_pathlist_head commitable_paths;
4517 struct collect_commitables_arg cc_arg;
4518 struct got_pathlist_entry *pe;
4519 struct got_reference *head_ref = NULL;
4520 struct got_object_id *head_commit_id = NULL;
4521 int have_staged_files = 0;
4523 *new_commit_id = NULL;
4525 TAILQ_INIT(&commitable_paths);
4527 err = lock_worktree(worktree, LOCK_EX);
4528 if (err)
4529 goto done;
4531 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4532 if (err)
4533 goto done;
4535 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4536 if (err)
4537 goto done;
4539 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4540 if (err)
4541 goto done;
4543 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4544 &have_staged_files);
4545 if (err && err->code != GOT_ERR_CANCELLED)
4546 goto done;
4547 if (have_staged_files) {
4548 err = check_non_staged_files(fileindex, paths);
4549 if (err)
4550 goto done;
4553 cc_arg.commitable_paths = &commitable_paths;
4554 cc_arg.worktree = worktree;
4555 cc_arg.repo = repo;
4556 cc_arg.have_staged_files = have_staged_files;
4557 TAILQ_FOREACH(pe, paths, entry) {
4558 err = worktree_status(worktree, pe->path, fileindex, repo,
4559 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4560 if (err)
4561 goto done;
4564 if (TAILQ_EMPTY(&commitable_paths)) {
4565 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4566 goto done;
4569 TAILQ_FOREACH(pe, paths, entry) {
4570 err = check_path_is_commitable(pe->path, &commitable_paths);
4571 if (err)
4572 goto done;
4575 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4576 struct got_commitable *ct = pe->data;
4577 const char *ct_path = ct->in_repo_path;
4579 while (ct_path[0] == '/')
4580 ct_path++;
4581 err = check_out_of_date(ct_path, ct->status,
4582 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4583 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4584 if (err)
4585 goto done;
4589 err = commit_worktree(new_commit_id, &commitable_paths,
4590 head_commit_id, worktree, author, committer,
4591 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4592 if (err)
4593 goto done;
4595 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4596 fileindex, have_staged_files);
4597 sync_err = sync_fileindex(fileindex, fileindex_path);
4598 if (sync_err && err == NULL)
4599 err = sync_err;
4600 done:
4601 if (fileindex)
4602 got_fileindex_free(fileindex);
4603 free(fileindex_path);
4604 unlockerr = lock_worktree(worktree, LOCK_SH);
4605 if (unlockerr && err == NULL)
4606 err = unlockerr;
4607 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4608 struct got_commitable *ct = pe->data;
4609 free_commitable(ct);
4611 got_pathlist_free(&commitable_paths);
4612 return err;
4615 const char *
4616 got_commitable_get_path(struct got_commitable *ct)
4618 return ct->path;
4621 unsigned int
4622 got_commitable_get_status(struct got_commitable *ct)
4624 return ct->status;
4627 struct check_rebase_ok_arg {
4628 struct got_worktree *worktree;
4629 struct got_repository *repo;
4632 static const struct got_error *
4633 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4635 const struct got_error *err = NULL;
4636 struct check_rebase_ok_arg *a = arg;
4637 unsigned char status;
4638 struct stat sb;
4639 char *ondisk_path;
4641 /* Reject rebase of a work tree with mixed base commits. */
4642 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4643 SHA1_DIGEST_LENGTH))
4644 return got_error(GOT_ERR_MIXED_COMMITS);
4646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4647 == -1)
4648 return got_error_from_errno("asprintf");
4650 /* Reject rebase of a work tree with modified or staged files. */
4651 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4652 free(ondisk_path);
4653 if (err)
4654 return err;
4656 if (status != GOT_STATUS_NO_CHANGE)
4657 return got_error(GOT_ERR_MODIFIED);
4658 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4659 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4661 return NULL;
4664 const struct got_error *
4665 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4666 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4667 struct got_worktree *worktree, struct got_reference *branch,
4668 struct got_repository *repo)
4670 const struct got_error *err = NULL;
4671 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4672 char *branch_ref_name = NULL;
4673 char *fileindex_path = NULL;
4674 struct check_rebase_ok_arg ok_arg;
4675 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4677 *new_base_branch_ref = NULL;
4678 *tmp_branch = NULL;
4679 *fileindex = NULL;
4681 err = lock_worktree(worktree, LOCK_EX);
4682 if (err)
4683 return err;
4685 err = open_fileindex(fileindex, &fileindex_path, worktree);
4686 if (err)
4687 goto done;
4689 ok_arg.worktree = worktree;
4690 ok_arg.repo = repo;
4691 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4692 &ok_arg);
4693 if (err)
4694 goto done;
4696 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4697 if (err)
4698 goto done;
4700 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4701 if (err)
4702 goto done;
4704 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4705 if (err)
4706 goto done;
4708 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4709 0);
4710 if (err)
4711 goto done;
4713 err = got_ref_alloc_symref(new_base_branch_ref,
4714 new_base_branch_ref_name, wt_branch);
4715 if (err)
4716 goto done;
4717 err = got_ref_write(*new_base_branch_ref, repo);
4718 if (err)
4719 goto done;
4721 /* TODO Lock original branch's ref while rebasing? */
4723 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4724 if (err)
4725 goto done;
4727 err = got_ref_write(branch_ref, repo);
4728 if (err)
4729 goto done;
4731 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4732 worktree->base_commit_id);
4733 if (err)
4734 goto done;
4735 err = got_ref_write(*tmp_branch, repo);
4736 if (err)
4737 goto done;
4739 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4740 if (err)
4741 goto done;
4742 done:
4743 free(fileindex_path);
4744 free(tmp_branch_name);
4745 free(new_base_branch_ref_name);
4746 free(branch_ref_name);
4747 if (branch_ref)
4748 got_ref_close(branch_ref);
4749 if (wt_branch)
4750 got_ref_close(wt_branch);
4751 if (err) {
4752 if (*new_base_branch_ref) {
4753 got_ref_close(*new_base_branch_ref);
4754 *new_base_branch_ref = NULL;
4756 if (*tmp_branch) {
4757 got_ref_close(*tmp_branch);
4758 *tmp_branch = NULL;
4760 if (*fileindex) {
4761 got_fileindex_free(*fileindex);
4762 *fileindex = NULL;
4764 lock_worktree(worktree, LOCK_SH);
4766 return err;
4769 const struct got_error *
4770 got_worktree_rebase_continue(struct got_object_id **commit_id,
4771 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4772 struct got_reference **branch, struct got_fileindex **fileindex,
4773 struct got_worktree *worktree, struct got_repository *repo)
4775 const struct got_error *err;
4776 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4777 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4778 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4779 char *fileindex_path = NULL;
4780 int have_staged_files = 0;
4782 *commit_id = NULL;
4783 *new_base_branch = NULL;
4784 *tmp_branch = NULL;
4785 *branch = NULL;
4786 *fileindex = NULL;
4788 err = lock_worktree(worktree, LOCK_EX);
4789 if (err)
4790 return err;
4792 err = open_fileindex(fileindex, &fileindex_path, worktree);
4793 if (err)
4794 goto done;
4796 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4797 &have_staged_files);
4798 if (err && err->code != GOT_ERR_CANCELLED)
4799 goto done;
4800 if (have_staged_files) {
4801 err = got_error(GOT_ERR_STAGED_PATHS);
4802 goto done;
4805 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4806 if (err)
4807 goto done;
4809 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4810 if (err)
4811 goto done;
4813 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4814 if (err)
4815 goto done;
4817 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4818 if (err)
4819 goto done;
4821 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4822 if (err)
4823 goto done;
4825 err = got_ref_open(branch, repo,
4826 got_ref_get_symref_target(branch_ref), 0);
4827 if (err)
4828 goto done;
4830 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4831 if (err)
4832 goto done;
4834 err = got_ref_resolve(commit_id, repo, commit_ref);
4835 if (err)
4836 goto done;
4838 err = got_ref_open(new_base_branch, repo,
4839 new_base_branch_ref_name, 0);
4840 if (err)
4841 goto done;
4843 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4844 if (err)
4845 goto done;
4846 done:
4847 free(commit_ref_name);
4848 free(branch_ref_name);
4849 free(fileindex_path);
4850 if (commit_ref)
4851 got_ref_close(commit_ref);
4852 if (branch_ref)
4853 got_ref_close(branch_ref);
4854 if (err) {
4855 free(*commit_id);
4856 *commit_id = NULL;
4857 if (*tmp_branch) {
4858 got_ref_close(*tmp_branch);
4859 *tmp_branch = NULL;
4861 if (*new_base_branch) {
4862 got_ref_close(*new_base_branch);
4863 *new_base_branch = NULL;
4865 if (*branch) {
4866 got_ref_close(*branch);
4867 *branch = NULL;
4869 if (*fileindex) {
4870 got_fileindex_free(*fileindex);
4871 *fileindex = NULL;
4873 lock_worktree(worktree, LOCK_SH);
4875 return err;
4878 const struct got_error *
4879 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4881 const struct got_error *err;
4882 char *tmp_branch_name = NULL;
4884 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4885 if (err)
4886 return err;
4888 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4889 free(tmp_branch_name);
4890 return NULL;
4893 static const struct got_error *
4894 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4895 char **logmsg, void *arg)
4897 *logmsg = arg;
4898 return NULL;
4901 static const struct got_error *
4902 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4903 const char *path, struct got_object_id *blob_id,
4904 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4905 int dirfd, const char *de_name)
4907 return NULL;
4910 struct collect_merged_paths_arg {
4911 got_worktree_checkout_cb progress_cb;
4912 void *progress_arg;
4913 struct got_pathlist_head *merged_paths;
4916 static const struct got_error *
4917 collect_merged_paths(void *arg, unsigned char status, const char *path)
4919 const struct got_error *err;
4920 struct collect_merged_paths_arg *a = arg;
4921 char *p;
4922 struct got_pathlist_entry *new;
4924 err = (*a->progress_cb)(a->progress_arg, status, path);
4925 if (err)
4926 return err;
4928 if (status != GOT_STATUS_MERGE &&
4929 status != GOT_STATUS_ADD &&
4930 status != GOT_STATUS_DELETE &&
4931 status != GOT_STATUS_CONFLICT)
4932 return NULL;
4934 p = strdup(path);
4935 if (p == NULL)
4936 return got_error_from_errno("strdup");
4938 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4939 if (err || new == NULL)
4940 free(p);
4941 return err;
4944 void
4945 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4947 struct got_pathlist_entry *pe;
4949 TAILQ_FOREACH(pe, merged_paths, entry)
4950 free((char *)pe->path);
4952 got_pathlist_free(merged_paths);
4955 static const struct got_error *
4956 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4957 struct got_repository *repo)
4959 const struct got_error *err;
4960 struct got_reference *commit_ref = NULL;
4962 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4963 if (err) {
4964 if (err->code != GOT_ERR_NOT_REF)
4965 goto done;
4966 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4967 if (err)
4968 goto done;
4969 err = got_ref_write(commit_ref, repo);
4970 if (err)
4971 goto done;
4972 } else {
4973 struct got_object_id *stored_id;
4974 int cmp;
4976 err = got_ref_resolve(&stored_id, repo, commit_ref);
4977 if (err)
4978 goto done;
4979 cmp = got_object_id_cmp(commit_id, stored_id);
4980 free(stored_id);
4981 if (cmp != 0) {
4982 err = got_error(GOT_ERR_REBASE_COMMITID);
4983 goto done;
4986 done:
4987 if (commit_ref)
4988 got_ref_close(commit_ref);
4989 return err;
4992 static const struct got_error *
4993 rebase_merge_files(struct got_pathlist_head *merged_paths,
4994 const char *commit_ref_name, struct got_worktree *worktree,
4995 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4996 struct got_object_id *commit_id, struct got_repository *repo,
4997 got_worktree_checkout_cb progress_cb, void *progress_arg,
4998 got_cancel_cb cancel_cb, void *cancel_arg)
5000 const struct got_error *err;
5001 struct got_reference *commit_ref = NULL;
5002 struct collect_merged_paths_arg cmp_arg;
5003 char *fileindex_path;
5005 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5007 err = get_fileindex_path(&fileindex_path, worktree);
5008 if (err)
5009 return err;
5011 cmp_arg.progress_cb = progress_cb;
5012 cmp_arg.progress_arg = progress_arg;
5013 cmp_arg.merged_paths = merged_paths;
5014 err = merge_files(worktree, fileindex, fileindex_path,
5015 parent_commit_id, commit_id, repo, collect_merged_paths,
5016 &cmp_arg, cancel_cb, cancel_arg);
5017 if (commit_ref)
5018 got_ref_close(commit_ref);
5019 return err;
5022 const struct got_error *
5023 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5024 struct got_worktree *worktree, struct got_fileindex *fileindex,
5025 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5026 struct got_repository *repo,
5027 got_worktree_checkout_cb progress_cb, void *progress_arg,
5028 got_cancel_cb cancel_cb, void *cancel_arg)
5030 const struct got_error *err;
5031 char *commit_ref_name;
5033 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5034 if (err)
5035 return err;
5037 err = store_commit_id(commit_ref_name, commit_id, repo);
5038 if (err)
5039 goto done;
5041 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5042 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5043 progress_arg, cancel_cb, cancel_arg);
5044 done:
5045 free(commit_ref_name);
5046 return err;
5049 const struct got_error *
5050 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5051 struct got_worktree *worktree, struct got_fileindex *fileindex,
5052 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5053 struct got_repository *repo,
5054 got_worktree_checkout_cb progress_cb, void *progress_arg,
5055 got_cancel_cb cancel_cb, void *cancel_arg)
5057 const struct got_error *err;
5058 char *commit_ref_name;
5060 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5061 if (err)
5062 return err;
5064 err = store_commit_id(commit_ref_name, commit_id, repo);
5065 if (err)
5066 goto done;
5068 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5069 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5070 progress_arg, cancel_cb, cancel_arg);
5071 done:
5072 free(commit_ref_name);
5073 return err;
5076 static const struct got_error *
5077 rebase_commit(struct got_object_id **new_commit_id,
5078 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5079 struct got_worktree *worktree, struct got_fileindex *fileindex,
5080 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5081 const char *new_logmsg, struct got_repository *repo)
5083 const struct got_error *err, *sync_err;
5084 struct got_pathlist_head commitable_paths;
5085 struct collect_commitables_arg cc_arg;
5086 char *fileindex_path = NULL;
5087 struct got_reference *head_ref = NULL;
5088 struct got_object_id *head_commit_id = NULL;
5089 char *logmsg = NULL;
5091 TAILQ_INIT(&commitable_paths);
5092 *new_commit_id = NULL;
5094 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5096 err = get_fileindex_path(&fileindex_path, worktree);
5097 if (err)
5098 return err;
5100 cc_arg.commitable_paths = &commitable_paths;
5101 cc_arg.worktree = worktree;
5102 cc_arg.repo = repo;
5103 cc_arg.have_staged_files = 0;
5105 * If possible get the status of individual files directly to
5106 * avoid crawling the entire work tree once per rebased commit.
5107 * TODO: Ideally, merged_paths would contain a list of commitables
5108 * we could use so we could skip worktree_status() entirely.
5110 if (merged_paths) {
5111 struct got_pathlist_entry *pe;
5112 if (TAILQ_EMPTY(merged_paths)) {
5113 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5114 goto done;
5116 TAILQ_FOREACH(pe, merged_paths, entry) {
5117 err = worktree_status(worktree, pe->path, fileindex,
5118 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5119 0);
5120 if (err)
5121 goto done;
5123 } else {
5124 err = worktree_status(worktree, "", fileindex, repo,
5125 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5126 if (err)
5127 goto done;
5130 if (TAILQ_EMPTY(&commitable_paths)) {
5131 /* No-op change; commit will be elided. */
5132 err = got_ref_delete(commit_ref, repo);
5133 if (err)
5134 goto done;
5135 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5136 goto done;
5139 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5140 if (err)
5141 goto done;
5143 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5144 if (err)
5145 goto done;
5147 if (new_logmsg) {
5148 logmsg = strdup(new_logmsg);
5149 if (logmsg == NULL) {
5150 err = got_error_from_errno("strdup");
5151 goto done;
5153 } else {
5154 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5155 if (err)
5156 goto done;
5159 /* NB: commit_worktree will call free(logmsg) */
5160 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5161 worktree, got_object_commit_get_author(orig_commit),
5162 got_object_commit_get_committer(orig_commit),
5163 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5164 if (err)
5165 goto done;
5167 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5168 if (err)
5169 goto done;
5171 err = got_ref_delete(commit_ref, repo);
5172 if (err)
5173 goto done;
5175 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5176 fileindex, 0);
5177 sync_err = sync_fileindex(fileindex, fileindex_path);
5178 if (sync_err && err == NULL)
5179 err = sync_err;
5180 done:
5181 free(fileindex_path);
5182 free(head_commit_id);
5183 if (head_ref)
5184 got_ref_close(head_ref);
5185 if (err) {
5186 free(*new_commit_id);
5187 *new_commit_id = NULL;
5189 return err;
5192 const struct got_error *
5193 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5194 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5195 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5196 struct got_commit_object *orig_commit,
5197 struct got_object_id *orig_commit_id, struct got_repository *repo)
5199 const struct got_error *err;
5200 char *commit_ref_name;
5201 struct got_reference *commit_ref = NULL;
5202 struct got_object_id *commit_id = NULL;
5204 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5205 if (err)
5206 return err;
5208 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5209 if (err)
5210 goto done;
5211 err = got_ref_resolve(&commit_id, repo, commit_ref);
5212 if (err)
5213 goto done;
5214 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5215 err = got_error(GOT_ERR_REBASE_COMMITID);
5216 goto done;
5219 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5220 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5221 done:
5222 if (commit_ref)
5223 got_ref_close(commit_ref);
5224 free(commit_ref_name);
5225 free(commit_id);
5226 return err;
5229 const struct got_error *
5230 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5231 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5232 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5233 struct got_commit_object *orig_commit,
5234 struct got_object_id *orig_commit_id, const char *new_logmsg,
5235 struct got_repository *repo)
5237 const struct got_error *err;
5238 char *commit_ref_name;
5239 struct got_reference *commit_ref = NULL;
5240 struct got_object_id *commit_id = NULL;
5242 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5243 if (err)
5244 return err;
5246 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5247 if (err)
5248 goto done;
5249 err = got_ref_resolve(&commit_id, repo, commit_ref);
5250 if (err)
5251 goto done;
5252 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5253 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5254 goto done;
5257 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5258 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5259 done:
5260 if (commit_ref)
5261 got_ref_close(commit_ref);
5262 free(commit_ref_name);
5263 free(commit_id);
5264 return err;
5267 const struct got_error *
5268 got_worktree_rebase_postpone(struct got_worktree *worktree,
5269 struct got_fileindex *fileindex)
5271 if (fileindex)
5272 got_fileindex_free(fileindex);
5273 return lock_worktree(worktree, LOCK_SH);
5276 static const struct got_error *
5277 delete_ref(const char *name, struct got_repository *repo)
5279 const struct got_error *err;
5280 struct got_reference *ref;
5282 err = got_ref_open(&ref, repo, name, 0);
5283 if (err) {
5284 if (err->code == GOT_ERR_NOT_REF)
5285 return NULL;
5286 return err;
5289 err = got_ref_delete(ref, repo);
5290 got_ref_close(ref);
5291 return err;
5294 static const struct got_error *
5295 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5297 const struct got_error *err;
5298 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5299 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5301 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5302 if (err)
5303 goto done;
5304 err = delete_ref(tmp_branch_name, repo);
5305 if (err)
5306 goto done;
5308 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5309 if (err)
5310 goto done;
5311 err = delete_ref(new_base_branch_ref_name, repo);
5312 if (err)
5313 goto done;
5315 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5316 if (err)
5317 goto done;
5318 err = delete_ref(branch_ref_name, repo);
5319 if (err)
5320 goto done;
5322 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5323 if (err)
5324 goto done;
5325 err = delete_ref(commit_ref_name, repo);
5326 if (err)
5327 goto done;
5329 done:
5330 free(tmp_branch_name);
5331 free(new_base_branch_ref_name);
5332 free(branch_ref_name);
5333 free(commit_ref_name);
5334 return err;
5337 const struct got_error *
5338 got_worktree_rebase_complete(struct got_worktree *worktree,
5339 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5340 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5341 struct got_repository *repo)
5343 const struct got_error *err, *unlockerr;
5344 struct got_object_id *new_head_commit_id = NULL;
5346 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5347 if (err)
5348 return err;
5350 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5351 if (err)
5352 goto done;
5354 err = got_ref_write(rebased_branch, repo);
5355 if (err)
5356 goto done;
5358 err = got_worktree_set_head_ref(worktree, rebased_branch);
5359 if (err)
5360 goto done;
5362 err = delete_rebase_refs(worktree, repo);
5363 done:
5364 if (fileindex)
5365 got_fileindex_free(fileindex);
5366 free(new_head_commit_id);
5367 unlockerr = lock_worktree(worktree, LOCK_SH);
5368 if (unlockerr && err == NULL)
5369 err = unlockerr;
5370 return err;
5373 const struct got_error *
5374 got_worktree_rebase_abort(struct got_worktree *worktree,
5375 struct got_fileindex *fileindex, struct got_repository *repo,
5376 struct got_reference *new_base_branch,
5377 got_worktree_checkout_cb progress_cb, void *progress_arg)
5379 const struct got_error *err, *unlockerr, *sync_err;
5380 struct got_reference *resolved = NULL;
5381 struct got_object_id *commit_id = NULL;
5382 char *fileindex_path = NULL;
5383 struct revert_file_args rfa;
5384 struct got_object_id *tree_id = NULL;
5386 err = lock_worktree(worktree, LOCK_EX);
5387 if (err)
5388 return err;
5390 err = got_ref_open(&resolved, repo,
5391 got_ref_get_symref_target(new_base_branch), 0);
5392 if (err)
5393 goto done;
5395 err = got_worktree_set_head_ref(worktree, resolved);
5396 if (err)
5397 goto done;
5400 * XXX commits to the base branch could have happened while
5401 * we were busy rebasing; should we store the original commit ID
5402 * when rebase begins and read it back here?
5404 err = got_ref_resolve(&commit_id, repo, resolved);
5405 if (err)
5406 goto done;
5408 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5409 if (err)
5410 goto done;
5412 err = got_object_id_by_path(&tree_id, repo,
5413 worktree->base_commit_id, worktree->path_prefix);
5414 if (err)
5415 goto done;
5417 err = delete_rebase_refs(worktree, repo);
5418 if (err)
5419 goto done;
5421 err = get_fileindex_path(&fileindex_path, worktree);
5422 if (err)
5423 goto done;
5425 rfa.worktree = worktree;
5426 rfa.fileindex = fileindex;
5427 rfa.progress_cb = progress_cb;
5428 rfa.progress_arg = progress_arg;
5429 rfa.patch_cb = NULL;
5430 rfa.patch_arg = NULL;
5431 rfa.repo = repo;
5432 err = worktree_status(worktree, "", fileindex, repo,
5433 revert_file, &rfa, NULL, NULL, 0, 0);
5434 if (err)
5435 goto sync;
5437 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5438 repo, progress_cb, progress_arg, NULL, NULL);
5439 sync:
5440 sync_err = sync_fileindex(fileindex, fileindex_path);
5441 if (sync_err && err == NULL)
5442 err = sync_err;
5443 done:
5444 got_ref_close(resolved);
5445 free(tree_id);
5446 free(commit_id);
5447 if (fileindex)
5448 got_fileindex_free(fileindex);
5449 free(fileindex_path);
5451 unlockerr = lock_worktree(worktree, LOCK_SH);
5452 if (unlockerr && err == NULL)
5453 err = unlockerr;
5454 return err;
5457 const struct got_error *
5458 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5459 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5460 struct got_fileindex **fileindex, struct got_worktree *worktree,
5461 struct got_repository *repo)
5463 const struct got_error *err = NULL;
5464 char *tmp_branch_name = NULL;
5465 char *branch_ref_name = NULL;
5466 char *base_commit_ref_name = NULL;
5467 char *fileindex_path = NULL;
5468 struct check_rebase_ok_arg ok_arg;
5469 struct got_reference *wt_branch = NULL;
5470 struct got_reference *base_commit_ref = NULL;
5472 *tmp_branch = NULL;
5473 *branch_ref = NULL;
5474 *base_commit_id = NULL;
5475 *fileindex = NULL;
5477 err = lock_worktree(worktree, LOCK_EX);
5478 if (err)
5479 return err;
5481 err = open_fileindex(fileindex, &fileindex_path, worktree);
5482 if (err)
5483 goto done;
5485 ok_arg.worktree = worktree;
5486 ok_arg.repo = repo;
5487 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5488 &ok_arg);
5489 if (err)
5490 goto done;
5492 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5493 if (err)
5494 goto done;
5496 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5497 if (err)
5498 goto done;
5500 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5501 worktree);
5502 if (err)
5503 goto done;
5505 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5506 0);
5507 if (err)
5508 goto done;
5510 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5511 if (err)
5512 goto done;
5514 err = got_ref_write(*branch_ref, repo);
5515 if (err)
5516 goto done;
5518 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5519 worktree->base_commit_id);
5520 if (err)
5521 goto done;
5522 err = got_ref_write(base_commit_ref, repo);
5523 if (err)
5524 goto done;
5525 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5526 if (*base_commit_id == NULL) {
5527 err = got_error_from_errno("got_object_id_dup");
5528 goto done;
5531 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5532 worktree->base_commit_id);
5533 if (err)
5534 goto done;
5535 err = got_ref_write(*tmp_branch, repo);
5536 if (err)
5537 goto done;
5539 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5540 if (err)
5541 goto done;
5542 done:
5543 free(fileindex_path);
5544 free(tmp_branch_name);
5545 free(branch_ref_name);
5546 free(base_commit_ref_name);
5547 if (wt_branch)
5548 got_ref_close(wt_branch);
5549 if (err) {
5550 if (*branch_ref) {
5551 got_ref_close(*branch_ref);
5552 *branch_ref = NULL;
5554 if (*tmp_branch) {
5555 got_ref_close(*tmp_branch);
5556 *tmp_branch = NULL;
5558 free(*base_commit_id);
5559 if (*fileindex) {
5560 got_fileindex_free(*fileindex);
5561 *fileindex = NULL;
5563 lock_worktree(worktree, LOCK_SH);
5565 return err;
5568 const struct got_error *
5569 got_worktree_histedit_postpone(struct got_worktree *worktree,
5570 struct got_fileindex *fileindex)
5572 if (fileindex)
5573 got_fileindex_free(fileindex);
5574 return lock_worktree(worktree, LOCK_SH);
5577 const struct got_error *
5578 got_worktree_histedit_in_progress(int *in_progress,
5579 struct got_worktree *worktree)
5581 const struct got_error *err;
5582 char *tmp_branch_name = NULL;
5584 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5585 if (err)
5586 return err;
5588 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5589 free(tmp_branch_name);
5590 return NULL;
5593 const struct got_error *
5594 got_worktree_histedit_continue(struct got_object_id **commit_id,
5595 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5596 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5597 struct got_worktree *worktree, struct got_repository *repo)
5599 const struct got_error *err;
5600 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5601 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5602 struct got_reference *commit_ref = NULL;
5603 struct got_reference *base_commit_ref = NULL;
5604 char *fileindex_path = NULL;
5605 int have_staged_files = 0;
5607 *commit_id = NULL;
5608 *tmp_branch = NULL;
5609 *base_commit_id = NULL;
5610 *fileindex = NULL;
5612 err = lock_worktree(worktree, LOCK_EX);
5613 if (err)
5614 return err;
5616 err = open_fileindex(fileindex, &fileindex_path, worktree);
5617 if (err)
5618 goto done;
5620 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5621 &have_staged_files);
5622 if (err && err->code != GOT_ERR_CANCELLED)
5623 goto done;
5624 if (have_staged_files) {
5625 err = got_error(GOT_ERR_STAGED_PATHS);
5626 goto done;
5629 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5630 if (err)
5631 goto done;
5633 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5634 if (err)
5635 goto done;
5637 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5638 if (err)
5639 goto done;
5641 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5642 worktree);
5643 if (err)
5644 goto done;
5646 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5647 if (err)
5648 goto done;
5650 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5651 if (err)
5652 goto done;
5653 err = got_ref_resolve(commit_id, repo, commit_ref);
5654 if (err)
5655 goto done;
5657 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5658 if (err)
5659 goto done;
5660 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5661 if (err)
5662 goto done;
5664 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5665 if (err)
5666 goto done;
5667 done:
5668 free(commit_ref_name);
5669 free(branch_ref_name);
5670 free(fileindex_path);
5671 if (commit_ref)
5672 got_ref_close(commit_ref);
5673 if (base_commit_ref)
5674 got_ref_close(base_commit_ref);
5675 if (err) {
5676 free(*commit_id);
5677 *commit_id = NULL;
5678 free(*base_commit_id);
5679 *base_commit_id = NULL;
5680 if (*tmp_branch) {
5681 got_ref_close(*tmp_branch);
5682 *tmp_branch = NULL;
5684 if (*fileindex) {
5685 got_fileindex_free(*fileindex);
5686 *fileindex = NULL;
5688 lock_worktree(worktree, LOCK_EX);
5690 return err;
5693 static const struct got_error *
5694 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5696 const struct got_error *err;
5697 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5698 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5700 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5701 if (err)
5702 goto done;
5703 err = delete_ref(tmp_branch_name, repo);
5704 if (err)
5705 goto done;
5707 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5708 worktree);
5709 if (err)
5710 goto done;
5711 err = delete_ref(base_commit_ref_name, repo);
5712 if (err)
5713 goto done;
5715 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5716 if (err)
5717 goto done;
5718 err = delete_ref(branch_ref_name, repo);
5719 if (err)
5720 goto done;
5722 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5723 if (err)
5724 goto done;
5725 err = delete_ref(commit_ref_name, repo);
5726 if (err)
5727 goto done;
5728 done:
5729 free(tmp_branch_name);
5730 free(base_commit_ref_name);
5731 free(branch_ref_name);
5732 free(commit_ref_name);
5733 return err;
5736 const struct got_error *
5737 got_worktree_histedit_abort(struct got_worktree *worktree,
5738 struct got_fileindex *fileindex, struct got_repository *repo,
5739 struct got_reference *branch, struct got_object_id *base_commit_id,
5740 got_worktree_checkout_cb progress_cb, void *progress_arg)
5742 const struct got_error *err, *unlockerr, *sync_err;
5743 struct got_reference *resolved = NULL;
5744 char *fileindex_path = NULL;
5745 struct got_object_id *tree_id = NULL;
5746 struct revert_file_args rfa;
5748 err = lock_worktree(worktree, LOCK_EX);
5749 if (err)
5750 return err;
5752 err = got_ref_open(&resolved, repo,
5753 got_ref_get_symref_target(branch), 0);
5754 if (err)
5755 goto done;
5757 err = got_worktree_set_head_ref(worktree, resolved);
5758 if (err)
5759 goto done;
5761 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5762 if (err)
5763 goto done;
5765 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5766 worktree->path_prefix);
5767 if (err)
5768 goto done;
5770 err = delete_histedit_refs(worktree, repo);
5771 if (err)
5772 goto done;
5774 err = get_fileindex_path(&fileindex_path, worktree);
5775 if (err)
5776 goto done;
5778 rfa.worktree = worktree;
5779 rfa.fileindex = fileindex;
5780 rfa.progress_cb = progress_cb;
5781 rfa.progress_arg = progress_arg;
5782 rfa.patch_cb = NULL;
5783 rfa.patch_arg = NULL;
5784 rfa.repo = repo;
5785 err = worktree_status(worktree, "", fileindex, repo,
5786 revert_file, &rfa, NULL, NULL, 0, 0);
5787 if (err)
5788 goto sync;
5790 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5791 repo, progress_cb, progress_arg, NULL, NULL);
5792 sync:
5793 sync_err = sync_fileindex(fileindex, fileindex_path);
5794 if (sync_err && err == NULL)
5795 err = sync_err;
5796 done:
5797 got_ref_close(resolved);
5798 free(tree_id);
5799 free(fileindex_path);
5801 unlockerr = lock_worktree(worktree, LOCK_SH);
5802 if (unlockerr && err == NULL)
5803 err = unlockerr;
5804 return err;
5807 const struct got_error *
5808 got_worktree_histedit_complete(struct got_worktree *worktree,
5809 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5810 struct got_reference *edited_branch, struct got_repository *repo)
5812 const struct got_error *err, *unlockerr;
5813 struct got_object_id *new_head_commit_id = NULL;
5814 struct got_reference *resolved = NULL;
5816 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5817 if (err)
5818 return err;
5820 err = got_ref_open(&resolved, repo,
5821 got_ref_get_symref_target(edited_branch), 0);
5822 if (err)
5823 goto done;
5825 err = got_ref_change_ref(resolved, new_head_commit_id);
5826 if (err)
5827 goto done;
5829 err = got_ref_write(resolved, repo);
5830 if (err)
5831 goto done;
5833 err = got_worktree_set_head_ref(worktree, resolved);
5834 if (err)
5835 goto done;
5837 err = delete_histedit_refs(worktree, repo);
5838 done:
5839 if (fileindex)
5840 got_fileindex_free(fileindex);
5841 free(new_head_commit_id);
5842 unlockerr = lock_worktree(worktree, LOCK_SH);
5843 if (unlockerr && err == NULL)
5844 err = unlockerr;
5845 return err;
5848 const struct got_error *
5849 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5850 struct got_object_id *commit_id, struct got_repository *repo)
5852 const struct got_error *err;
5853 char *commit_ref_name;
5855 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5856 if (err)
5857 return err;
5859 err = store_commit_id(commit_ref_name, commit_id, repo);
5860 if (err)
5861 goto done;
5863 err = delete_ref(commit_ref_name, repo);
5864 done:
5865 free(commit_ref_name);
5866 return err;
5869 const struct got_error *
5870 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5871 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5872 struct got_worktree *worktree, const char *refname,
5873 struct got_repository *repo)
5875 const struct got_error *err = NULL;
5876 char *fileindex_path = NULL;
5877 struct check_rebase_ok_arg ok_arg;
5879 *fileindex = NULL;
5880 *branch_ref = NULL;
5881 *base_branch_ref = NULL;
5883 err = lock_worktree(worktree, LOCK_EX);
5884 if (err)
5885 return err;
5887 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5888 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5889 "cannot integrate a branch into itself; "
5890 "update -b or different branch name required");
5891 goto done;
5894 err = open_fileindex(fileindex, &fileindex_path, worktree);
5895 if (err)
5896 goto done;
5898 /* Preconditions are the same as for rebase. */
5899 ok_arg.worktree = worktree;
5900 ok_arg.repo = repo;
5901 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5902 &ok_arg);
5903 if (err)
5904 goto done;
5906 err = got_ref_open(branch_ref, repo, refname, 1);
5907 if (err)
5908 goto done;
5910 err = got_ref_open(base_branch_ref, repo,
5911 got_worktree_get_head_ref_name(worktree), 1);
5912 done:
5913 if (err) {
5914 if (*branch_ref) {
5915 got_ref_close(*branch_ref);
5916 *branch_ref = NULL;
5918 if (*base_branch_ref) {
5919 got_ref_close(*base_branch_ref);
5920 *base_branch_ref = NULL;
5922 if (*fileindex) {
5923 got_fileindex_free(*fileindex);
5924 *fileindex = NULL;
5926 lock_worktree(worktree, LOCK_SH);
5928 return err;
5931 const struct got_error *
5932 got_worktree_integrate_continue(struct got_worktree *worktree,
5933 struct got_fileindex *fileindex, struct got_repository *repo,
5934 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5935 got_worktree_checkout_cb progress_cb, void *progress_arg,
5936 got_cancel_cb cancel_cb, void *cancel_arg)
5938 const struct got_error *err = NULL, *sync_err, *unlockerr;
5939 char *fileindex_path = NULL;
5940 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5942 err = get_fileindex_path(&fileindex_path, worktree);
5943 if (err)
5944 goto done;
5946 err = got_ref_resolve(&commit_id, repo, branch_ref);
5947 if (err)
5948 goto done;
5950 err = got_object_id_by_path(&tree_id, repo, commit_id,
5951 worktree->path_prefix);
5952 if (err)
5953 goto done;
5955 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5956 if (err)
5957 goto done;
5959 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5960 progress_cb, progress_arg, cancel_cb, cancel_arg);
5961 if (err)
5962 goto sync;
5964 err = got_ref_change_ref(base_branch_ref, commit_id);
5965 if (err)
5966 goto sync;
5968 err = got_ref_write(base_branch_ref, repo);
5969 sync:
5970 sync_err = sync_fileindex(fileindex, fileindex_path);
5971 if (sync_err && err == NULL)
5972 err = sync_err;
5974 done:
5975 unlockerr = got_ref_unlock(branch_ref);
5976 if (unlockerr && err == NULL)
5977 err = unlockerr;
5978 got_ref_close(branch_ref);
5980 unlockerr = got_ref_unlock(base_branch_ref);
5981 if (unlockerr && err == NULL)
5982 err = unlockerr;
5983 got_ref_close(base_branch_ref);
5985 got_fileindex_free(fileindex);
5986 free(fileindex_path);
5987 free(tree_id);
5989 unlockerr = lock_worktree(worktree, LOCK_SH);
5990 if (unlockerr && err == NULL)
5991 err = unlockerr;
5992 return err;
5995 const struct got_error *
5996 got_worktree_integrate_abort(struct got_worktree *worktree,
5997 struct got_fileindex *fileindex, struct got_repository *repo,
5998 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6000 const struct got_error *err = NULL, *unlockerr = NULL;
6002 got_fileindex_free(fileindex);
6004 err = lock_worktree(worktree, LOCK_SH);
6006 unlockerr = got_ref_unlock(branch_ref);
6007 if (unlockerr && err == NULL)
6008 err = unlockerr;
6009 got_ref_close(branch_ref);
6011 unlockerr = got_ref_unlock(base_branch_ref);
6012 if (unlockerr && err == NULL)
6013 err = unlockerr;
6014 got_ref_close(base_branch_ref);
6016 return err;
6019 struct check_stage_ok_arg {
6020 struct got_object_id *head_commit_id;
6021 struct got_worktree *worktree;
6022 struct got_fileindex *fileindex;
6023 struct got_repository *repo;
6024 int have_changes;
6027 const struct got_error *
6028 check_stage_ok(void *arg, unsigned char status,
6029 unsigned char staged_status, const char *relpath,
6030 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6031 struct got_object_id *commit_id, int dirfd, const char *de_name)
6033 struct check_stage_ok_arg *a = arg;
6034 const struct got_error *err = NULL;
6035 struct got_fileindex_entry *ie;
6036 struct got_object_id base_commit_id;
6037 struct got_object_id *base_commit_idp = NULL;
6038 char *in_repo_path = NULL, *p;
6040 if (status == GOT_STATUS_UNVERSIONED ||
6041 status == GOT_STATUS_NO_CHANGE)
6042 return NULL;
6043 if (status == GOT_STATUS_NONEXISTENT)
6044 return got_error_set_errno(ENOENT, relpath);
6046 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6047 if (ie == NULL)
6048 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6050 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6051 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6052 relpath) == -1)
6053 return got_error_from_errno("asprintf");
6055 if (got_fileindex_entry_has_commit(ie)) {
6056 memcpy(base_commit_id.sha1, ie->commit_sha1,
6057 SHA1_DIGEST_LENGTH);
6058 base_commit_idp = &base_commit_id;
6061 if (status == GOT_STATUS_CONFLICT) {
6062 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6063 goto done;
6064 } else if (status != GOT_STATUS_ADD &&
6065 status != GOT_STATUS_MODIFY &&
6066 status != GOT_STATUS_DELETE) {
6067 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6068 goto done;
6071 a->have_changes = 1;
6073 p = in_repo_path;
6074 while (p[0] == '/')
6075 p++;
6076 err = check_out_of_date(p, status, staged_status,
6077 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6078 GOT_ERR_STAGE_OUT_OF_DATE);
6079 done:
6080 free(in_repo_path);
6081 return err;
6084 struct stage_path_arg {
6085 struct got_worktree *worktree;
6086 struct got_fileindex *fileindex;
6087 struct got_repository *repo;
6088 got_worktree_status_cb status_cb;
6089 void *status_arg;
6090 got_worktree_patch_cb patch_cb;
6091 void *patch_arg;
6092 int staged_something;
6095 static const struct got_error *
6096 stage_path(void *arg, unsigned char status,
6097 unsigned char staged_status, const char *relpath,
6098 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6099 struct got_object_id *commit_id, int dirfd, const char *de_name)
6101 struct stage_path_arg *a = arg;
6102 const struct got_error *err = NULL;
6103 struct got_fileindex_entry *ie;
6104 char *ondisk_path = NULL, *path_content = NULL;
6105 uint32_t stage;
6106 struct got_object_id *new_staged_blob_id = NULL;
6108 if (status == GOT_STATUS_UNVERSIONED)
6109 return NULL;
6111 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6112 if (ie == NULL)
6113 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6115 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6116 relpath)== -1)
6117 return got_error_from_errno("asprintf");
6119 switch (status) {
6120 case GOT_STATUS_ADD:
6121 case GOT_STATUS_MODIFY:
6122 if (a->patch_cb) {
6123 if (status == GOT_STATUS_ADD) {
6124 int choice = GOT_PATCH_CHOICE_NONE;
6125 err = (*a->patch_cb)(&choice, a->patch_arg,
6126 status, ie->path, NULL, 1, 1);
6127 if (err)
6128 break;
6129 if (choice != GOT_PATCH_CHOICE_YES)
6130 break;
6131 } else {
6132 err = create_patched_content(&path_content, 0,
6133 staged_blob_id ? staged_blob_id : blob_id,
6134 ondisk_path, dirfd, de_name, ie->path,
6135 a->repo, a->patch_cb, a->patch_arg);
6136 if (err || path_content == NULL)
6137 break;
6140 err = got_object_blob_create(&new_staged_blob_id,
6141 path_content ? path_content : ondisk_path, a->repo);
6142 if (err)
6143 break;
6144 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6145 SHA1_DIGEST_LENGTH);
6146 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6147 stage = GOT_FILEIDX_STAGE_ADD;
6148 else
6149 stage = GOT_FILEIDX_STAGE_MODIFY;
6150 got_fileindex_entry_stage_set(ie, stage);
6151 a->staged_something = 1;
6152 if (a->status_cb == NULL)
6153 break;
6154 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6155 get_staged_status(ie), relpath, blob_id,
6156 new_staged_blob_id, NULL, dirfd, de_name);
6157 break;
6158 case GOT_STATUS_DELETE:
6159 if (staged_status == GOT_STATUS_DELETE)
6160 break;
6161 if (a->patch_cb) {
6162 int choice = GOT_PATCH_CHOICE_NONE;
6163 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6164 ie->path, NULL, 1, 1);
6165 if (err)
6166 break;
6167 if (choice == GOT_PATCH_CHOICE_NO)
6168 break;
6169 if (choice != GOT_PATCH_CHOICE_YES) {
6170 err = got_error(GOT_ERR_PATCH_CHOICE);
6171 break;
6174 stage = GOT_FILEIDX_STAGE_DELETE;
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, NULL, NULL, NULL, dirfd,
6181 de_name);
6182 break;
6183 case GOT_STATUS_NO_CHANGE:
6184 break;
6185 case GOT_STATUS_CONFLICT:
6186 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6187 break;
6188 case GOT_STATUS_NONEXISTENT:
6189 err = got_error_set_errno(ENOENT, relpath);
6190 break;
6191 default:
6192 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6193 break;
6196 if (path_content && unlink(path_content) == -1 && err == NULL)
6197 err = got_error_from_errno2("unlink", path_content);
6198 free(path_content);
6199 free(ondisk_path);
6200 free(new_staged_blob_id);
6201 return err;
6204 const struct got_error *
6205 got_worktree_stage(struct got_worktree *worktree,
6206 struct got_pathlist_head *paths,
6207 got_worktree_status_cb status_cb, void *status_arg,
6208 got_worktree_patch_cb patch_cb, void *patch_arg,
6209 struct got_repository *repo)
6211 const struct got_error *err = NULL, *sync_err, *unlockerr;
6212 struct got_pathlist_entry *pe;
6213 struct got_fileindex *fileindex = NULL;
6214 char *fileindex_path = NULL;
6215 struct got_reference *head_ref = NULL;
6216 struct got_object_id *head_commit_id = NULL;
6217 struct check_stage_ok_arg oka;
6218 struct stage_path_arg spa;
6220 err = lock_worktree(worktree, LOCK_EX);
6221 if (err)
6222 return err;
6224 err = got_ref_open(&head_ref, repo,
6225 got_worktree_get_head_ref_name(worktree), 0);
6226 if (err)
6227 goto done;
6228 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6229 if (err)
6230 goto done;
6231 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6232 if (err)
6233 goto done;
6235 /* Check pre-conditions before staging anything. */
6236 oka.head_commit_id = head_commit_id;
6237 oka.worktree = worktree;
6238 oka.fileindex = fileindex;
6239 oka.repo = repo;
6240 oka.have_changes = 0;
6241 TAILQ_FOREACH(pe, paths, entry) {
6242 err = worktree_status(worktree, pe->path, fileindex, repo,
6243 check_stage_ok, &oka, NULL, NULL, 0, 0);
6244 if (err)
6245 goto done;
6247 if (!oka.have_changes) {
6248 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6249 goto done;
6252 spa.worktree = worktree;
6253 spa.fileindex = fileindex;
6254 spa.repo = repo;
6255 spa.patch_cb = patch_cb;
6256 spa.patch_arg = patch_arg;
6257 spa.status_cb = status_cb;
6258 spa.status_arg = status_arg;
6259 spa.staged_something = 0;
6260 TAILQ_FOREACH(pe, paths, entry) {
6261 err = worktree_status(worktree, pe->path, fileindex, repo,
6262 stage_path, &spa, NULL, NULL, 0, 0);
6263 if (err)
6264 goto done;
6266 if (!spa.staged_something) {
6267 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6268 goto done;
6271 sync_err = sync_fileindex(fileindex, fileindex_path);
6272 if (sync_err && err == NULL)
6273 err = sync_err;
6274 done:
6275 if (head_ref)
6276 got_ref_close(head_ref);
6277 free(head_commit_id);
6278 free(fileindex_path);
6279 if (fileindex)
6280 got_fileindex_free(fileindex);
6281 unlockerr = lock_worktree(worktree, LOCK_SH);
6282 if (unlockerr && err == NULL)
6283 err = unlockerr;
6284 return err;
6287 struct unstage_path_arg {
6288 struct got_worktree *worktree;
6289 struct got_fileindex *fileindex;
6290 struct got_repository *repo;
6291 got_worktree_checkout_cb progress_cb;
6292 void *progress_arg;
6293 got_worktree_patch_cb patch_cb;
6294 void *patch_arg;
6297 static const struct got_error *
6298 create_unstaged_content(char **path_unstaged_content,
6299 char **path_new_staged_content, struct got_object_id *blob_id,
6300 struct got_object_id *staged_blob_id, const char *relpath,
6301 struct got_repository *repo,
6302 got_worktree_patch_cb patch_cb, void *patch_arg)
6304 const struct got_error *err;
6305 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6306 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6307 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6308 struct stat sb1, sb2;
6309 struct got_diff_changes *changes = NULL;
6310 struct got_diff_state *ds = NULL;
6311 struct got_diff_args *args = NULL;
6312 struct got_diff_change *change;
6313 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6314 int have_content = 0, have_rejected_content = 0;
6316 *path_unstaged_content = NULL;
6317 *path_new_staged_content = NULL;
6319 err = got_object_id_str(&label1, blob_id);
6320 if (err)
6321 return err;
6322 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6323 if (err)
6324 goto done;
6326 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6327 if (err)
6328 goto done;
6330 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6331 if (err)
6332 goto done;
6334 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6335 if (err)
6336 goto done;
6338 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6339 if (err)
6340 goto done;
6342 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6343 if (err)
6344 goto done;
6346 if (stat(path1, &sb1) == -1) {
6347 err = got_error_from_errno2("stat", path1);
6348 goto done;
6351 if (stat(path2, &sb2) == -1) {
6352 err = got_error_from_errno2("stat", path2);
6353 goto done;
6356 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6357 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6358 if (err)
6359 goto done;
6361 err = got_opentemp_named(path_unstaged_content, &outfile,
6362 "got-unstaged-content");
6363 if (err)
6364 goto done;
6365 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6366 "got-new-staged-content");
6367 if (err)
6368 goto done;
6370 if (fseek(f1, 0L, SEEK_SET) == -1) {
6371 err = got_ferror(f1, GOT_ERR_IO);
6372 goto done;
6374 if (fseek(f2, 0L, SEEK_SET) == -1) {
6375 err = got_ferror(f2, GOT_ERR_IO);
6376 goto done;
6378 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6379 int choice;
6380 err = apply_or_reject_change(&choice, change, ++n,
6381 changes->nchanges, ds, args, diff_flags, relpath,
6382 f1, f2, &line_cur1, &line_cur2,
6383 outfile, rejectfile, patch_cb, patch_arg);
6384 if (err)
6385 goto done;
6386 if (choice == GOT_PATCH_CHOICE_YES)
6387 have_content = 1;
6388 else
6389 have_rejected_content = 1;
6390 if (choice == GOT_PATCH_CHOICE_QUIT)
6391 break;
6393 if (have_content || have_rejected_content)
6394 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6395 outfile, rejectfile);
6396 done:
6397 free(label1);
6398 if (blob)
6399 got_object_blob_close(blob);
6400 if (staged_blob)
6401 got_object_blob_close(staged_blob);
6402 if (f1 && fclose(f1) == EOF && err == NULL)
6403 err = got_error_from_errno2("fclose", path1);
6404 if (f2 && fclose(f2) == EOF && err == NULL)
6405 err = got_error_from_errno2("fclose", path2);
6406 if (outfile && fclose(outfile) == EOF && err == NULL)
6407 err = got_error_from_errno2("fclose", *path_unstaged_content);
6408 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6409 err = got_error_from_errno2("fclose", *path_new_staged_content);
6410 if (path1 && unlink(path1) == -1 && err == NULL)
6411 err = got_error_from_errno2("unlink", path1);
6412 if (path2 && unlink(path2) == -1 && err == NULL)
6413 err = got_error_from_errno2("unlink", path2);
6414 if (err || !have_content) {
6415 if (*path_unstaged_content &&
6416 unlink(*path_unstaged_content) == -1 && err == NULL)
6417 err = got_error_from_errno2("unlink",
6418 *path_unstaged_content);
6419 free(*path_unstaged_content);
6420 *path_unstaged_content = NULL;
6422 if (err || !have_rejected_content) {
6423 if (*path_new_staged_content &&
6424 unlink(*path_new_staged_content) == -1 && err == NULL)
6425 err = got_error_from_errno2("unlink",
6426 *path_new_staged_content);
6427 free(*path_new_staged_content);
6428 *path_new_staged_content = NULL;
6430 free(args);
6431 if (ds) {
6432 got_diff_state_free(ds);
6433 free(ds);
6435 if (changes)
6436 got_diff_free_changes(changes);
6437 free(path1);
6438 free(path2);
6439 return err;
6442 static const struct got_error *
6443 unstage_path(void *arg, unsigned char status,
6444 unsigned char staged_status, const char *relpath,
6445 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6446 struct got_object_id *commit_id, int dirfd, const char *de_name)
6448 const struct got_error *err = NULL;
6449 struct unstage_path_arg *a = arg;
6450 struct got_fileindex_entry *ie;
6451 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6452 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6453 char *path_new_staged_content = NULL;
6454 char *id_str = NULL, *label_orig = NULL;
6455 int local_changes_subsumed;
6456 struct stat sb;
6458 if (staged_status != GOT_STATUS_ADD &&
6459 staged_status != GOT_STATUS_MODIFY &&
6460 staged_status != GOT_STATUS_DELETE)
6461 return NULL;
6463 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6464 if (ie == NULL)
6465 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6467 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6468 == -1)
6469 return got_error_from_errno("asprintf");
6471 err = got_object_id_str(&id_str,
6472 commit_id ? commit_id : a->worktree->base_commit_id);
6473 if (err)
6474 goto done;
6475 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6476 id_str) == -1) {
6477 err = got_error_from_errno("asprintf");
6478 goto done;
6481 switch (staged_status) {
6482 case GOT_STATUS_MODIFY:
6483 err = got_object_open_as_blob(&blob_base, a->repo,
6484 blob_id, 8192);
6485 if (err)
6486 break;
6487 /* fall through */
6488 case GOT_STATUS_ADD:
6489 if (a->patch_cb) {
6490 if (staged_status == GOT_STATUS_ADD) {
6491 int choice = GOT_PATCH_CHOICE_NONE;
6492 err = (*a->patch_cb)(&choice, a->patch_arg,
6493 staged_status, ie->path, NULL, 1, 1);
6494 if (err)
6495 break;
6496 if (choice != GOT_PATCH_CHOICE_YES)
6497 break;
6498 } else {
6499 err = create_unstaged_content(
6500 &path_unstaged_content,
6501 &path_new_staged_content, blob_id,
6502 staged_blob_id, ie->path, a->repo,
6503 a->patch_cb, a->patch_arg);
6504 if (err || path_unstaged_content == NULL)
6505 break;
6506 if (path_new_staged_content) {
6507 err = got_object_blob_create(
6508 &staged_blob_id,
6509 path_new_staged_content,
6510 a->repo);
6511 if (err)
6512 break;
6513 memcpy(ie->staged_blob_sha1,
6514 staged_blob_id->sha1,
6515 SHA1_DIGEST_LENGTH);
6517 err = merge_file(&local_changes_subsumed,
6518 a->worktree, blob_base, ondisk_path,
6519 relpath, got_fileindex_perms_to_st(ie),
6520 path_unstaged_content, label_orig,
6521 "unstaged", a->repo, a->progress_cb,
6522 a->progress_arg);
6523 if (err == NULL &&
6524 path_new_staged_content == NULL)
6525 got_fileindex_entry_stage_set(ie,
6526 GOT_FILEIDX_STAGE_NONE);
6527 break; /* Done with this file. */
6530 err = got_object_open_as_blob(&blob_staged, a->repo,
6531 staged_blob_id, 8192);
6532 if (err)
6533 break;
6534 err = merge_blob(&local_changes_subsumed, a->worktree,
6535 blob_base, ondisk_path, relpath,
6536 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6537 commit_id ? commit_id : a->worktree->base_commit_id,
6538 a->repo, a->progress_cb, a->progress_arg);
6539 if (err == NULL)
6540 got_fileindex_entry_stage_set(ie,
6541 GOT_FILEIDX_STAGE_NONE);
6542 break;
6543 case GOT_STATUS_DELETE:
6544 if (a->patch_cb) {
6545 int choice = GOT_PATCH_CHOICE_NONE;
6546 err = (*a->patch_cb)(&choice, a->patch_arg,
6547 staged_status, ie->path, NULL, 1, 1);
6548 if (err)
6549 break;
6550 if (choice == GOT_PATCH_CHOICE_NO)
6551 break;
6552 if (choice != GOT_PATCH_CHOICE_YES) {
6553 err = got_error(GOT_ERR_PATCH_CHOICE);
6554 break;
6557 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6558 err = get_file_status(&status, &sb, ie, ondisk_path,
6559 dirfd, de_name, a->repo);
6560 if (err)
6561 break;
6562 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6563 break;
6565 done:
6566 free(ondisk_path);
6567 if (path_unstaged_content &&
6568 unlink(path_unstaged_content) == -1 && err == NULL)
6569 err = got_error_from_errno2("unlink", path_unstaged_content);
6570 if (path_new_staged_content &&
6571 unlink(path_new_staged_content) == -1 && err == NULL)
6572 err = got_error_from_errno2("unlink", path_new_staged_content);
6573 free(path_unstaged_content);
6574 free(path_new_staged_content);
6575 if (blob_base)
6576 got_object_blob_close(blob_base);
6577 if (blob_staged)
6578 got_object_blob_close(blob_staged);
6579 free(id_str);
6580 free(label_orig);
6581 return err;
6584 const struct got_error *
6585 got_worktree_unstage(struct got_worktree *worktree,
6586 struct got_pathlist_head *paths,
6587 got_worktree_checkout_cb progress_cb, void *progress_arg,
6588 got_worktree_patch_cb patch_cb, void *patch_arg,
6589 struct got_repository *repo)
6591 const struct got_error *err = NULL, *sync_err, *unlockerr;
6592 struct got_pathlist_entry *pe;
6593 struct got_fileindex *fileindex = NULL;
6594 char *fileindex_path = NULL;
6595 struct unstage_path_arg upa;
6597 err = lock_worktree(worktree, LOCK_EX);
6598 if (err)
6599 return err;
6601 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6602 if (err)
6603 goto done;
6605 upa.worktree = worktree;
6606 upa.fileindex = fileindex;
6607 upa.repo = repo;
6608 upa.progress_cb = progress_cb;
6609 upa.progress_arg = progress_arg;
6610 upa.patch_cb = patch_cb;
6611 upa.patch_arg = patch_arg;
6612 TAILQ_FOREACH(pe, paths, entry) {
6613 err = worktree_status(worktree, pe->path, fileindex, repo,
6614 unstage_path, &upa, NULL, NULL, 0, 0);
6615 if (err)
6616 goto done;
6619 sync_err = sync_fileindex(fileindex, fileindex_path);
6620 if (sync_err && err == NULL)
6621 err = sync_err;
6622 done:
6623 free(fileindex_path);
6624 if (fileindex)
6625 got_fileindex_free(fileindex);
6626 unlockerr = lock_worktree(worktree, LOCK_SH);
6627 if (unlockerr && err == NULL)
6628 err = unlockerr;
6629 return err;