Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 int dirfd, const char *de_name, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 int fd = -1;
1116 FILE *f = NULL;
1117 uint8_t fbuf[8192];
1118 struct got_blob_object *blob = NULL;
1119 size_t flen, blen;
1120 unsigned char staged_status = get_staged_status(ie);
1122 *status = GOT_STATUS_NO_CHANGE;
1125 * Whenever the caller provides a directory descriptor and a
1126 * directory entry name for the file, use them! This prevents
1127 * race conditions if filesystem paths change beneath our feet.
1129 if (dirfd != -1) {
1130 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1131 if (errno == ENOENT) {
1132 if (got_fileindex_entry_has_file_on_disk(ie))
1133 *status = GOT_STATUS_MISSING;
1134 else
1135 *status = GOT_STATUS_DELETE;
1136 goto done;
1138 err = got_error_from_errno2("fstatat", abspath);
1139 goto done;
1141 } else {
1142 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1143 if (fd == -1 && errno != ENOENT)
1144 return got_error_from_errno2("open", abspath);
1145 if (fd == -1 || fstat(fd, sb) == -1) {
1146 if (errno == ENOENT) {
1147 if (got_fileindex_entry_has_file_on_disk(ie))
1148 *status = GOT_STATUS_MISSING;
1149 else
1150 *status = GOT_STATUS_DELETE;
1151 goto done;
1153 err = got_error_from_errno2("fstat", abspath);
1154 goto done;
1158 if (!S_ISREG(sb->st_mode)) {
1159 *status = GOT_STATUS_OBSTRUCTED;
1160 goto done;
1163 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1164 *status = GOT_STATUS_DELETE;
1165 goto done;
1166 } else if (!got_fileindex_entry_has_blob(ie) &&
1167 staged_status != GOT_STATUS_ADD) {
1168 *status = GOT_STATUS_ADD;
1169 goto done;
1172 if (!stat_info_differs(ie, sb))
1173 goto done;
1175 if (staged_status == GOT_STATUS_MODIFY ||
1176 staged_status == GOT_STATUS_ADD)
1177 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1178 else
1179 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1181 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1182 if (err)
1183 goto done;
1185 if (dirfd != -1) {
1186 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1187 if (fd == -1) {
1188 err = got_error_from_errno2("openat", abspath);
1189 goto done;
1193 f = fdopen(fd, "r");
1194 if (f == NULL) {
1195 err = got_error_from_errno2("fopen", abspath);
1196 goto done;
1198 fd = -1;
1199 hdrlen = got_object_blob_get_hdrlen(blob);
1200 for (;;) {
1201 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1202 err = got_object_blob_read_block(&blen, blob);
1203 if (err)
1204 goto done;
1205 /* Skip length of blob object header first time around. */
1206 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1207 if (flen == 0 && ferror(f)) {
1208 err = got_error_from_errno("fread");
1209 goto done;
1211 if (blen == 0) {
1212 if (flen != 0)
1213 *status = GOT_STATUS_MODIFY;
1214 break;
1215 } else if (flen == 0) {
1216 if (blen != 0)
1217 *status = GOT_STATUS_MODIFY;
1218 break;
1219 } else if (blen - hdrlen == flen) {
1220 /* Skip blob object header first time around. */
1221 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1222 *status = GOT_STATUS_MODIFY;
1223 break;
1225 } else {
1226 *status = GOT_STATUS_MODIFY;
1227 break;
1229 hdrlen = 0;
1232 if (*status == GOT_STATUS_MODIFY) {
1233 rewind(f);
1234 err = get_modified_file_content_status(status, f);
1235 } else if (xbit_differs(ie, sb->st_mode))
1236 *status = GOT_STATUS_MODE_CHANGE;
1237 done:
1238 if (blob)
1239 got_object_blob_close(blob);
1240 if (f != NULL && fclose(f) == EOF && err == NULL)
1241 err = got_error_from_errno2("fclose", abspath);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", abspath);
1244 return err;
1248 * Update timestamps in the file index if a file is unmodified and
1249 * we had to run a full content comparison to find out.
1251 static const struct got_error *
1252 sync_timestamps(char *ondisk_path, unsigned char status,
1253 struct got_fileindex_entry *ie, struct stat *sb)
1255 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1256 return got_fileindex_entry_update(ie, ondisk_path,
1257 ie->blob_sha1, ie->commit_sha1, 1);
1259 return NULL;
1262 static const struct got_error *
1263 update_blob(struct got_worktree *worktree,
1264 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1265 struct got_tree_entry *te, const char *path,
1266 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1267 void *progress_arg)
1269 const struct got_error *err = NULL;
1270 struct got_blob_object *blob = NULL;
1271 char *ondisk_path;
1272 unsigned char status = GOT_STATUS_NO_CHANGE;
1273 struct stat sb;
1275 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1276 return got_error_from_errno("asprintf");
1278 if (ie) {
1279 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1280 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1281 goto done;
1283 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1284 repo);
1285 if (err)
1286 goto done;
1287 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1288 sb.st_mode = got_fileindex_perms_to_st(ie);
1289 } else
1290 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1292 if (status == GOT_STATUS_OBSTRUCTED) {
1293 err = (*progress_cb)(progress_arg, status, path);
1294 goto done;
1297 if (ie && status != GOT_STATUS_MISSING &&
1298 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1299 if (got_fileindex_entry_has_commit(ie) &&
1300 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1301 SHA1_DIGEST_LENGTH) == 0) {
1302 err = sync_timestamps(ondisk_path, status, ie, &sb);
1303 if (err)
1304 goto done;
1305 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1306 path);
1307 goto done;
1309 if (got_fileindex_entry_has_blob(ie) &&
1310 memcmp(ie->blob_sha1, te->id.sha1,
1311 SHA1_DIGEST_LENGTH) == 0) {
1312 err = sync_timestamps(ondisk_path, status, ie, &sb);
1313 goto done;
1317 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1318 if (err)
1319 goto done;
1321 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1322 int update_timestamps;
1323 struct got_blob_object *blob2 = NULL;
1324 char *label_orig = NULL;
1325 if (got_fileindex_entry_has_blob(ie)) {
1326 struct got_object_id id2;
1327 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1328 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1329 if (err)
1330 goto done;
1332 if (got_fileindex_entry_has_commit(ie)) {
1333 char id_str[SHA1_DIGEST_STRING_LENGTH];
1334 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1335 sizeof(id_str)) == NULL) {
1336 err = got_error_path(id_str,
1337 GOT_ERR_BAD_OBJ_ID_STR);
1338 goto done;
1340 if (asprintf(&label_orig, "%s: commit %s",
1341 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1342 err = got_error_from_errno("asprintf");
1343 goto done;
1346 err = merge_blob(&update_timestamps, worktree, blob2,
1347 ondisk_path, path, sb.st_mode, label_orig, blob,
1348 worktree->base_commit_id, repo,
1349 progress_cb, progress_arg);
1350 free(label_orig);
1351 if (blob2)
1352 got_object_blob_close(blob2);
1353 if (err)
1354 goto done;
1356 * Do not update timestamps of files with local changes.
1357 * Otherwise, a future status walk would treat them as
1358 * unmodified files again.
1360 err = got_fileindex_entry_update(ie, ondisk_path,
1361 blob->id.sha1, worktree->base_commit_id->sha1,
1362 update_timestamps);
1363 } else if (status == GOT_STATUS_MODE_CHANGE) {
1364 err = got_fileindex_entry_update(ie, ondisk_path,
1365 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1366 } else if (status == GOT_STATUS_DELETE) {
1367 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1368 if (err)
1369 goto done;
1370 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1371 ondisk_path, path, blob, 0);
1372 if (err)
1373 goto done;
1374 } else {
1375 err = install_blob(worktree, ondisk_path, path, te->mode,
1376 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1377 repo, progress_cb, progress_arg);
1378 if (err)
1379 goto done;
1380 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1381 ondisk_path, path, blob, 1);
1382 if (err)
1383 goto done;
1385 got_object_blob_close(blob);
1386 done:
1387 free(ondisk_path);
1388 return err;
1391 static const struct got_error *
1392 remove_ondisk_file(const char *root_path, const char *path)
1394 const struct got_error *err = NULL;
1395 char *ondisk_path = NULL;
1397 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1398 return got_error_from_errno("asprintf");
1400 if (unlink(ondisk_path) == -1) {
1401 if (errno != ENOENT)
1402 err = got_error_from_errno2("unlink", ondisk_path);
1403 } else {
1404 char *parent = dirname(ondisk_path);
1405 while (parent && strcmp(parent, root_path) != 0) {
1406 if (rmdir(parent) == -1) {
1407 if (errno != ENOTEMPTY)
1408 err = got_error_from_errno2("rmdir",
1409 parent);
1410 break;
1412 parent = dirname(parent);
1415 free(ondisk_path);
1416 return err;
1419 static const struct got_error *
1420 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1421 struct got_fileindex_entry *ie, struct got_repository *repo,
1422 got_worktree_checkout_cb progress_cb, void *progress_arg)
1424 const struct got_error *err = NULL;
1425 unsigned char status;
1426 struct stat sb;
1427 char *ondisk_path;
1429 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1430 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1432 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1433 == -1)
1434 return got_error_from_errno("asprintf");
1436 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1437 if (err)
1438 return err;
1440 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1441 status == GOT_STATUS_ADD) {
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1443 if (err)
1444 return err;
1446 * Preserve the working file and change the deleted blob's
1447 * entry into a schedule-add entry.
1449 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1450 0);
1451 if (err)
1452 return err;
1453 } else {
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1455 if (err)
1456 return err;
1457 if (status == GOT_STATUS_NO_CHANGE) {
1458 err = remove_ondisk_file(worktree->root_path, ie->path);
1459 if (err)
1460 return err;
1462 got_fileindex_entry_remove(fileindex, ie);
1465 return err;
1468 struct diff_cb_arg {
1469 struct got_fileindex *fileindex;
1470 struct got_worktree *worktree;
1471 struct got_repository *repo;
1472 got_worktree_checkout_cb progress_cb;
1473 void *progress_arg;
1474 got_cancel_cb cancel_cb;
1475 void *cancel_arg;
1478 static const struct got_error *
1479 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1480 struct got_tree_entry *te, const char *parent_path)
1482 struct diff_cb_arg *a = arg;
1484 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1485 return got_error(GOT_ERR_CANCELLED);
1487 return update_blob(a->worktree, a->fileindex, ie, te,
1488 ie->path, a->repo, a->progress_cb, a->progress_arg);
1491 static const struct got_error *
1492 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1494 struct diff_cb_arg *a = arg;
1496 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1497 return got_error(GOT_ERR_CANCELLED);
1499 return delete_blob(a->worktree, a->fileindex, ie,
1500 a->repo, a->progress_cb, a->progress_arg);
1503 static const struct got_error *
1504 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1506 struct diff_cb_arg *a = arg;
1507 const struct got_error *err;
1508 char *path;
1510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1511 return got_error(GOT_ERR_CANCELLED);
1513 if (got_object_tree_entry_is_submodule(te))
1514 return NULL;
1516 if (asprintf(&path, "%s%s%s", parent_path,
1517 parent_path[0] ? "/" : "", te->name)
1518 == -1)
1519 return got_error_from_errno("asprintf");
1521 if (S_ISDIR(te->mode))
1522 err = add_dir_on_disk(a->worktree, path);
1523 else
1524 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1525 a->repo, a->progress_cb, a->progress_arg);
1527 free(path);
1528 return err;
1531 static const struct got_error *
1532 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1534 const struct got_error *err = NULL;
1535 char *uuidstr = NULL;
1536 uint32_t uuid_status;
1538 *refname = NULL;
1540 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1541 if (uuid_status != uuid_s_ok)
1542 return got_error_uuid(uuid_status, "uuid_to_string");
1544 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1545 == -1) {
1546 err = got_error_from_errno("asprintf");
1547 *refname = NULL;
1549 free(uuidstr);
1550 return err;
1553 const struct got_error *
1554 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1556 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1559 static const struct got_error *
1560 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1562 return get_ref_name(refname, worktree,
1563 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1566 static const struct got_error *
1567 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1569 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1572 static const struct got_error *
1573 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1575 return get_ref_name(refname, worktree,
1576 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1579 static const struct got_error *
1580 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1582 return get_ref_name(refname, worktree,
1583 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1586 static const struct got_error *
1587 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1589 return get_ref_name(refname, worktree,
1590 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1593 static const struct got_error *
1594 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1596 return get_ref_name(refname, worktree,
1597 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1600 static const struct got_error *
1601 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1603 return get_ref_name(refname, worktree,
1604 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1607 static const struct got_error *
1608 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1610 return get_ref_name(refname, worktree,
1611 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1614 const struct got_error *
1615 got_worktree_get_histedit_script_path(char **path,
1616 struct got_worktree *worktree)
1618 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1619 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1620 *path = NULL;
1621 return got_error_from_errno("asprintf");
1623 return NULL;
1627 * Prevent Git's garbage collector from deleting our base commit by
1628 * setting a reference to our base commit's ID.
1630 static const struct got_error *
1631 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_reference *ref = NULL;
1635 char *refname;
1637 err = got_worktree_get_base_ref_name(&refname, worktree);
1638 if (err)
1639 return err;
1641 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1642 if (err)
1643 goto done;
1645 err = got_ref_write(ref, repo);
1646 done:
1647 free(refname);
1648 if (ref)
1649 got_ref_close(ref);
1650 return err;
1653 static const struct got_error *
1654 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1656 const struct got_error *err = NULL;
1658 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1659 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1660 err = got_error_from_errno("asprintf");
1661 *fileindex_path = NULL;
1663 return err;
1667 static const struct got_error *
1668 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1669 struct got_worktree *worktree)
1671 const struct got_error *err = NULL;
1672 FILE *index = NULL;
1674 *fileindex_path = NULL;
1675 *fileindex = got_fileindex_alloc();
1676 if (*fileindex == NULL)
1677 return got_error_from_errno("got_fileindex_alloc");
1679 err = get_fileindex_path(fileindex_path, worktree);
1680 if (err)
1681 goto done;
1683 index = fopen(*fileindex_path, "rb");
1684 if (index == NULL) {
1685 if (errno != ENOENT)
1686 err = got_error_from_errno2("fopen", *fileindex_path);
1687 } else {
1688 err = got_fileindex_read(*fileindex, index);
1689 if (fclose(index) != 0 && err == NULL)
1690 err = got_error_from_errno("fclose");
1692 done:
1693 if (err) {
1694 free(*fileindex_path);
1695 *fileindex_path = NULL;
1696 got_fileindex_free(*fileindex);
1697 *fileindex = NULL;
1699 return err;
1702 struct bump_base_commit_id_arg {
1703 struct got_object_id *base_commit_id;
1704 const char *path;
1705 size_t path_len;
1706 const char *entry_name;
1707 got_worktree_checkout_cb progress_cb;
1708 void *progress_arg;
1711 /* Bump base commit ID of all files within an updated part of the work tree. */
1712 static const struct got_error *
1713 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1715 const struct got_error *err;
1716 struct bump_base_commit_id_arg *a = arg;
1718 if (a->entry_name) {
1719 if (strcmp(ie->path, a->path) != 0)
1720 return NULL;
1721 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1722 return NULL;
1724 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1725 SHA1_DIGEST_LENGTH) == 0)
1726 return NULL;
1728 if (a->progress_cb) {
1729 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1730 ie->path);
1731 if (err)
1732 return err;
1734 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1735 return NULL;
1738 static const struct got_error *
1739 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1741 const struct got_error *err = NULL;
1742 char *new_fileindex_path = NULL;
1743 FILE *new_index = NULL;
1745 err = got_opentemp_named(&new_fileindex_path, &new_index,
1746 fileindex_path);
1747 if (err)
1748 goto done;
1750 err = got_fileindex_write(fileindex, new_index);
1751 if (err)
1752 goto done;
1754 if (rename(new_fileindex_path, fileindex_path) != 0) {
1755 err = got_error_from_errno3("rename", new_fileindex_path,
1756 fileindex_path);
1757 unlink(new_fileindex_path);
1759 done:
1760 if (new_index)
1761 fclose(new_index);
1762 free(new_fileindex_path);
1763 return err;
1766 static const struct got_error *
1767 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1768 struct got_object_id **tree_id, const char *wt_relpath,
1769 struct got_worktree *worktree, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_object_id *id = NULL;
1773 char *in_repo_path = NULL;
1774 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 *tree_relpath = NULL;
1778 *tree_id = NULL;
1780 if (wt_relpath[0] == '\0') {
1781 /* Check out all files within the work tree. */
1782 *entry_type = GOT_OBJ_TYPE_TREE;
1783 *tree_relpath = strdup("");
1784 if (*tree_relpath == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 err = got_object_id_by_path(tree_id, repo,
1789 worktree->base_commit_id, worktree->path_prefix);
1790 if (err)
1791 goto done;
1792 return NULL;
1795 /* Check out a subset of files in the work tree. */
1797 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1798 is_root_wt ? "" : "/", wt_relpath) == -1) {
1799 err = got_error_from_errno("asprintf");
1800 goto done;
1803 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1804 in_repo_path);
1805 if (err)
1806 goto done;
1808 free(in_repo_path);
1809 in_repo_path = NULL;
1811 err = got_object_get_type(entry_type, repo, id);
1812 if (err)
1813 goto done;
1815 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1816 /* Check out a single file. */
1817 if (strchr(wt_relpath, '/') == NULL) {
1818 /* Check out a single file in work tree's root dir. */
1819 in_repo_path = strdup(worktree->path_prefix);
1820 if (in_repo_path == NULL) {
1821 err = got_error_from_errno("strdup");
1822 goto done;
1824 *tree_relpath = strdup("");
1825 if (*tree_relpath == NULL) {
1826 err = got_error_from_errno("strdup");
1827 goto done;
1829 } else {
1830 /* Check out a single file in a subdirectory. */
1831 err = got_path_dirname(tree_relpath, wt_relpath);
1832 if (err)
1833 return err;
1834 if (asprintf(&in_repo_path, "%s%s%s",
1835 worktree->path_prefix, is_root_wt ? "" : "/",
1836 *tree_relpath) == -1) {
1837 err = got_error_from_errno("asprintf");
1838 goto done;
1841 err = got_object_id_by_path(tree_id, repo,
1842 worktree->base_commit_id, in_repo_path);
1843 } else {
1844 /* Check out all files within a subdirectory. */
1845 *tree_id = got_object_id_dup(id);
1846 if (*tree_id == NULL) {
1847 err = got_error_from_errno("got_object_id_dup");
1848 goto done;
1850 *tree_relpath = strdup(wt_relpath);
1851 if (*tree_relpath == NULL) {
1852 err = got_error_from_errno("strdup");
1853 goto done;
1856 done:
1857 free(id);
1858 free(in_repo_path);
1859 if (err) {
1860 *entry_type = GOT_OBJ_TYPE_ANY;
1861 free(*tree_relpath);
1862 *tree_relpath = NULL;
1863 free(*tree_id);
1864 *tree_id = NULL;
1866 return err;
1869 static const struct got_error *
1870 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1871 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1873 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1875 const struct got_error *err = NULL;
1876 struct got_commit_object *commit = NULL;
1877 struct got_tree_object *tree = NULL;
1878 struct got_fileindex_diff_tree_cb diff_cb;
1879 struct diff_cb_arg arg;
1881 err = ref_base_commit(worktree, repo);
1882 if (err)
1883 goto done;
1885 err = got_object_open_as_commit(&commit, repo,
1886 worktree->base_commit_id);
1887 if (err)
1888 goto done;
1890 err = got_object_open_as_tree(&tree, repo, tree_id);
1891 if (err)
1892 goto done;
1894 if (entry_name &&
1895 got_object_tree_find_entry(tree, entry_name) == NULL) {
1896 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1897 goto done;
1900 diff_cb.diff_old_new = diff_old_new;
1901 diff_cb.diff_old = diff_old;
1902 diff_cb.diff_new = diff_new;
1903 arg.fileindex = fileindex;
1904 arg.worktree = worktree;
1905 arg.repo = repo;
1906 arg.progress_cb = progress_cb;
1907 arg.progress_arg = progress_arg;
1908 arg.cancel_cb = cancel_cb;
1909 arg.cancel_arg = cancel_arg;
1910 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1911 entry_name, repo, &diff_cb, &arg);
1912 done:
1913 if (tree)
1914 got_object_tree_close(tree);
1915 if (commit)
1916 got_object_commit_close(commit);
1917 return err;
1920 const struct got_error *
1921 got_worktree_checkout_files(struct got_worktree *worktree,
1922 struct got_pathlist_head *paths, struct got_repository *repo,
1923 got_worktree_checkout_cb progress_cb, void *progress_arg,
1924 got_cancel_cb cancel_cb, void *cancel_arg)
1926 const struct got_error *err = NULL, *sync_err, *unlockerr;
1927 struct got_commit_object *commit = NULL;
1928 struct got_tree_object *tree = NULL;
1929 struct got_fileindex *fileindex = NULL;
1930 char *fileindex_path = NULL;
1931 struct got_pathlist_entry *pe;
1932 struct tree_path_data {
1933 SIMPLEQ_ENTRY(tree_path_data) entry;
1934 struct got_object_id *tree_id;
1935 int entry_type;
1936 char *relpath;
1937 char *entry_name;
1938 } *tpd = NULL;
1939 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1941 SIMPLEQ_INIT(&tree_paths);
1943 err = lock_worktree(worktree, LOCK_EX);
1944 if (err)
1945 return err;
1947 /* Map all specified paths to in-repository trees. */
1948 TAILQ_FOREACH(pe, paths, entry) {
1949 tpd = malloc(sizeof(*tpd));
1950 if (tpd == NULL) {
1951 err = got_error_from_errno("malloc");
1952 goto done;
1955 err = find_tree_entry_for_checkout(&tpd->entry_type,
1956 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1957 if (err) {
1958 free(tpd);
1959 goto done;
1962 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1963 err = got_path_basename(&tpd->entry_name, pe->path);
1964 if (err) {
1965 free(tpd->relpath);
1966 free(tpd->tree_id);
1967 free(tpd);
1968 goto done;
1970 } else
1971 tpd->entry_name = NULL;
1973 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1977 * Read the file index.
1978 * Checking out files is supposed to be an idempotent operation.
1979 * If the on-disk file index is incomplete we will try to complete it.
1981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1982 if (err)
1983 goto done;
1985 tpd = SIMPLEQ_FIRST(&tree_paths);
1986 TAILQ_FOREACH(pe, paths, entry) {
1987 struct bump_base_commit_id_arg bbc_arg;
1989 err = checkout_files(worktree, fileindex, tpd->relpath,
1990 tpd->tree_id, tpd->entry_name, repo,
1991 progress_cb, progress_arg, cancel_cb, cancel_arg);
1992 if (err)
1993 break;
1995 bbc_arg.base_commit_id = worktree->base_commit_id;
1996 bbc_arg.entry_name = tpd->entry_name;
1997 bbc_arg.path = pe->path;
1998 bbc_arg.path_len = pe->path_len;
1999 bbc_arg.progress_cb = progress_cb;
2000 bbc_arg.progress_arg = progress_arg;
2001 err = got_fileindex_for_each_entry_safe(fileindex,
2002 bump_base_commit_id, &bbc_arg);
2003 if (err)
2004 break;
2006 tpd = SIMPLEQ_NEXT(tpd, entry);
2008 sync_err = sync_fileindex(fileindex, fileindex_path);
2009 if (sync_err && err == NULL)
2010 err = sync_err;
2011 done:
2012 free(fileindex_path);
2013 if (tree)
2014 got_object_tree_close(tree);
2015 if (commit)
2016 got_object_commit_close(commit);
2017 if (fileindex)
2018 got_fileindex_free(fileindex);
2019 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2020 tpd = SIMPLEQ_FIRST(&tree_paths);
2021 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2022 free(tpd->relpath);
2023 free(tpd->tree_id);
2024 free(tpd);
2026 unlockerr = lock_worktree(worktree, LOCK_SH);
2027 if (unlockerr && err == NULL)
2028 err = unlockerr;
2029 return err;
2032 struct merge_file_cb_arg {
2033 struct got_worktree *worktree;
2034 struct got_fileindex *fileindex;
2035 got_worktree_checkout_cb progress_cb;
2036 void *progress_arg;
2037 got_cancel_cb cancel_cb;
2038 void *cancel_arg;
2039 const char *label_orig;
2040 struct got_object_id *commit_id2;
2043 static const struct got_error *
2044 merge_file_cb(void *arg, struct got_blob_object *blob1,
2045 struct got_blob_object *blob2, struct got_object_id *id1,
2046 struct got_object_id *id2, const char *path1, const char *path2,
2047 mode_t mode1, mode_t mode2, struct got_repository *repo)
2049 static const struct got_error *err = NULL;
2050 struct merge_file_cb_arg *a = arg;
2051 struct got_fileindex_entry *ie;
2052 char *ondisk_path = NULL;
2053 struct stat sb;
2054 unsigned char status;
2055 int local_changes_subsumed;
2057 if (blob1 && blob2) {
2058 ie = got_fileindex_entry_get(a->fileindex, path2,
2059 strlen(path2));
2060 if (ie == NULL)
2061 return (*a->progress_cb)(a->progress_arg,
2062 GOT_STATUS_MISSING, path2);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2065 path2) == -1)
2066 return got_error_from_errno("asprintf");
2068 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2069 repo);
2070 if (err)
2071 goto done;
2073 if (status == GOT_STATUS_DELETE) {
2074 err = (*a->progress_cb)(a->progress_arg,
2075 GOT_STATUS_MERGE, path2);
2076 goto done;
2078 if (status != GOT_STATUS_NO_CHANGE &&
2079 status != GOT_STATUS_MODIFY &&
2080 status != GOT_STATUS_CONFLICT &&
2081 status != GOT_STATUS_ADD) {
2082 err = (*a->progress_cb)(a->progress_arg, status, path2);
2083 goto done;
2086 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2087 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2088 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2089 } else if (blob1) {
2090 ie = got_fileindex_entry_get(a->fileindex, path1,
2091 strlen(path1));
2092 if (ie == NULL)
2093 return (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MISSING, path2);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2097 path1) == -1)
2098 return got_error_from_errno("asprintf");
2100 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2101 repo);
2102 if (err)
2103 goto done;
2105 switch (status) {
2106 case GOT_STATUS_NO_CHANGE:
2107 err = (*a->progress_cb)(a->progress_arg,
2108 GOT_STATUS_DELETE, path1);
2109 if (err)
2110 goto done;
2111 err = remove_ondisk_file(a->worktree->root_path, path1);
2112 if (err)
2113 goto done;
2114 if (ie)
2115 got_fileindex_entry_mark_deleted_from_disk(ie);
2116 break;
2117 case GOT_STATUS_DELETE:
2118 case GOT_STATUS_MISSING:
2119 err = (*a->progress_cb)(a->progress_arg,
2120 GOT_STATUS_DELETE, path1);
2121 if (err)
2122 goto done;
2123 if (ie)
2124 got_fileindex_entry_mark_deleted_from_disk(ie);
2125 break;
2126 case GOT_STATUS_ADD:
2127 case GOT_STATUS_MODIFY:
2128 case GOT_STATUS_CONFLICT:
2129 err = (*a->progress_cb)(a->progress_arg,
2130 GOT_STATUS_CANNOT_DELETE, path1);
2131 if (err)
2132 goto done;
2133 break;
2134 case GOT_STATUS_OBSTRUCTED:
2135 err = (*a->progress_cb)(a->progress_arg, status, path1);
2136 if (err)
2137 goto done;
2138 break;
2139 default:
2140 break;
2142 } else if (blob2) {
2143 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2144 path2) == -1)
2145 return got_error_from_errno("asprintf");
2146 ie = got_fileindex_entry_get(a->fileindex, path2,
2147 strlen(path2));
2148 if (ie) {
2149 err = get_file_status(&status, &sb, ie, ondisk_path,
2150 -1, NULL, repo);
2151 if (err)
2152 goto done;
2153 if (status != GOT_STATUS_NO_CHANGE &&
2154 status != GOT_STATUS_MODIFY &&
2155 status != GOT_STATUS_CONFLICT &&
2156 status != GOT_STATUS_ADD) {
2157 err = (*a->progress_cb)(a->progress_arg,
2158 status, path2);
2159 goto done;
2161 err = merge_blob(&local_changes_subsumed, a->worktree,
2162 NULL, ondisk_path, path2, sb.st_mode,
2163 a->label_orig, blob2, a->commit_id2, repo,
2164 a->progress_cb,
2165 a->progress_arg);
2166 if (status == GOT_STATUS_DELETE) {
2167 err = update_blob_fileindex_entry(a->worktree,
2168 a->fileindex, ie, ondisk_path, ie->path,
2169 blob2, 0);
2170 if (err)
2171 goto done;
2173 } else {
2174 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2175 err = install_blob(a->worktree, ondisk_path, path2,
2176 /* XXX get this from parent tree! */
2177 GOT_DEFAULT_FILE_MODE,
2178 sb.st_mode, blob2, 0, 0, repo,
2179 a->progress_cb, a->progress_arg);
2180 if (err)
2181 goto done;
2182 err = got_fileindex_entry_alloc(&ie,
2183 ondisk_path, path2, NULL, NULL);
2184 if (err)
2185 goto done;
2186 err = got_fileindex_entry_add(a->fileindex, ie);
2187 if (err) {
2188 got_fileindex_entry_free(ie);
2189 goto done;
2193 done:
2194 free(ondisk_path);
2195 return err;
2198 struct check_merge_ok_arg {
2199 struct got_worktree *worktree;
2200 struct got_repository *repo;
2203 static const struct got_error *
2204 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2206 const struct got_error *err = NULL;
2207 struct check_merge_ok_arg *a = arg;
2208 unsigned char status;
2209 struct stat sb;
2210 char *ondisk_path;
2212 /* Reject merges into a work tree with mixed base commits. */
2213 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2214 SHA1_DIGEST_LENGTH))
2215 return got_error(GOT_ERR_MIXED_COMMITS);
2217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2218 == -1)
2219 return got_error_from_errno("asprintf");
2221 /* Reject merges into a work tree with conflicted files. */
2222 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2223 if (err)
2224 return err;
2225 if (status == GOT_STATUS_CONFLICT)
2226 return got_error(GOT_ERR_CONFLICTS);
2228 return NULL;
2231 static const struct got_error *
2232 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2233 const char *fileindex_path, struct got_object_id *commit_id1,
2234 struct got_object_id *commit_id2, struct got_repository *repo,
2235 got_worktree_checkout_cb progress_cb, void *progress_arg,
2236 got_cancel_cb cancel_cb, void *cancel_arg)
2238 const struct got_error *err = NULL, *sync_err;
2239 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2240 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2241 struct merge_file_cb_arg arg;
2242 char *label_orig = NULL;
2244 if (commit_id1) {
2245 char *id_str;
2247 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2248 worktree->path_prefix);
2249 if (err)
2250 goto done;
2252 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2253 if (err)
2254 goto done;
2256 err = got_object_id_str(&id_str, commit_id1);
2257 if (err)
2258 goto done;
2260 if (asprintf(&label_orig, "%s: commit %s",
2261 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2262 err = got_error_from_errno("asprintf");
2263 free(id_str);
2264 goto done;
2266 free(id_str);
2269 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2270 worktree->path_prefix);
2271 if (err)
2272 goto done;
2274 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2275 if (err)
2276 goto done;
2278 arg.worktree = worktree;
2279 arg.fileindex = fileindex;
2280 arg.progress_cb = progress_cb;
2281 arg.progress_arg = progress_arg;
2282 arg.cancel_cb = cancel_cb;
2283 arg.cancel_arg = cancel_arg;
2284 arg.label_orig = label_orig;
2285 arg.commit_id2 = commit_id2;
2286 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2287 sync_err = sync_fileindex(fileindex, fileindex_path);
2288 if (sync_err && err == NULL)
2289 err = sync_err;
2290 done:
2291 if (tree1)
2292 got_object_tree_close(tree1);
2293 if (tree2)
2294 got_object_tree_close(tree2);
2295 free(label_orig);
2296 return err;
2299 const struct got_error *
2300 got_worktree_merge_files(struct got_worktree *worktree,
2301 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2302 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2303 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2305 const struct got_error *err, *unlockerr;
2306 char *fileindex_path = NULL;
2307 struct got_fileindex *fileindex = NULL;
2308 struct check_merge_ok_arg mok_arg;
2310 err = lock_worktree(worktree, LOCK_EX);
2311 if (err)
2312 return err;
2314 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2315 if (err)
2316 goto done;
2318 mok_arg.worktree = worktree;
2319 mok_arg.repo = repo;
2320 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2321 &mok_arg);
2322 if (err)
2323 goto done;
2325 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2326 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2327 done:
2328 if (fileindex)
2329 got_fileindex_free(fileindex);
2330 free(fileindex_path);
2331 unlockerr = lock_worktree(worktree, LOCK_SH);
2332 if (unlockerr && err == NULL)
2333 err = unlockerr;
2334 return err;
2337 struct diff_dir_cb_arg {
2338 struct got_fileindex *fileindex;
2339 struct got_worktree *worktree;
2340 const char *status_path;
2341 size_t status_path_len;
2342 struct got_repository *repo;
2343 got_worktree_status_cb status_cb;
2344 void *status_arg;
2345 got_cancel_cb cancel_cb;
2346 void *cancel_arg;
2347 /* A pathlist containing per-directory pathlists of ignore patterns. */
2348 struct got_pathlist_head ignores;
2349 int report_unchanged;
2352 static const struct got_error *
2353 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2354 int dirfd, const char *de_name,
2355 got_worktree_status_cb status_cb, void *status_arg,
2356 struct got_repository *repo, int report_unchanged)
2358 const struct got_error *err = NULL;
2359 unsigned char status = GOT_STATUS_NO_CHANGE;
2360 unsigned char staged_status = get_staged_status(ie);
2361 struct stat sb;
2362 struct got_object_id blob_id, commit_id, staged_blob_id;
2363 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2364 struct got_object_id *staged_blob_idp = NULL;
2366 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2367 if (err)
2368 return err;
2370 if (status == GOT_STATUS_NO_CHANGE &&
2371 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2372 return NULL;
2374 if (got_fileindex_entry_has_blob(ie)) {
2375 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2376 blob_idp = &blob_id;
2378 if (got_fileindex_entry_has_commit(ie)) {
2379 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2380 commit_idp = &commit_id;
2382 if (staged_status == GOT_STATUS_ADD ||
2383 staged_status == GOT_STATUS_MODIFY) {
2384 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2385 SHA1_DIGEST_LENGTH);
2386 staged_blob_idp = &staged_blob_id;
2389 return (*status_cb)(status_arg, status, staged_status,
2390 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2393 static const struct got_error *
2394 status_old_new(void *arg, struct got_fileindex_entry *ie,
2395 struct dirent *de, const char *parent_path, int dirfd)
2397 const struct got_error *err = NULL;
2398 struct diff_dir_cb_arg *a = arg;
2399 char *abspath;
2401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2402 return got_error(GOT_ERR_CANCELLED);
2404 if (got_path_cmp(parent_path, a->status_path,
2405 strlen(parent_path), a->status_path_len) != 0 &&
2406 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2407 return NULL;
2409 if (parent_path[0]) {
2410 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2411 parent_path, de->d_name) == -1)
2412 return got_error_from_errno("asprintf");
2413 } else {
2414 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2415 de->d_name) == -1)
2416 return got_error_from_errno("asprintf");
2419 err = report_file_status(ie, abspath, dirfd, de->d_name,
2420 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2421 free(abspath);
2422 return err;
2425 static const struct got_error *
2426 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2428 struct diff_dir_cb_arg *a = arg;
2429 struct got_object_id blob_id, commit_id;
2430 unsigned char status;
2432 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2433 return got_error(GOT_ERR_CANCELLED);
2435 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2436 return NULL;
2438 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2439 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2440 if (got_fileindex_entry_has_file_on_disk(ie))
2441 status = GOT_STATUS_MISSING;
2442 else
2443 status = GOT_STATUS_DELETE;
2444 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2445 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2448 void
2449 free_ignorelist(struct got_pathlist_head *ignorelist)
2451 struct got_pathlist_entry *pe;
2453 TAILQ_FOREACH(pe, ignorelist, entry)
2454 free((char *)pe->path);
2455 got_pathlist_free(ignorelist);
2458 void
2459 free_ignores(struct got_pathlist_head *ignores)
2461 struct got_pathlist_entry *pe;
2463 TAILQ_FOREACH(pe, ignores, entry) {
2464 struct got_pathlist_head *ignorelist = pe->data;
2465 free_ignorelist(ignorelist);
2466 free((char *)pe->path);
2468 got_pathlist_free(ignores);
2471 static const struct got_error *
2472 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2474 const struct got_error *err = NULL;
2475 struct got_pathlist_entry *pe = NULL;
2476 struct got_pathlist_head *ignorelist;
2477 char *line = NULL, *pattern, *dirpath = NULL;
2478 size_t linesize = 0;
2479 ssize_t linelen;
2481 ignorelist = calloc(1, sizeof(*ignorelist));
2482 if (ignorelist == NULL)
2483 return got_error_from_errno("calloc");
2484 TAILQ_INIT(ignorelist);
2486 while ((linelen = getline(&line, &linesize, f)) != -1) {
2487 if (linelen > 0 && line[linelen - 1] == '\n')
2488 line[linelen - 1] = '\0';
2490 /* Git's ignores may contain comments. */
2491 if (line[0] == '#')
2492 continue;
2494 /* Git's negated patterns are not (yet?) supported. */
2495 if (line[0] == '!')
2496 continue;
2498 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2499 line) == -1) {
2500 err = got_error_from_errno("asprintf");
2501 goto done;
2503 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2504 if (err)
2505 goto done;
2507 if (ferror(f)) {
2508 err = got_error_from_errno("getline");
2509 goto done;
2512 dirpath = strdup(path);
2513 if (dirpath == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2518 done:
2519 free(line);
2520 if (err || pe == NULL) {
2521 free(dirpath);
2522 free_ignorelist(ignorelist);
2524 return err;
2527 int
2528 match_ignores(struct got_pathlist_head *ignores, const char *path)
2530 struct got_pathlist_entry *pe;
2532 /* Handle patterns which match in all directories. */
2533 TAILQ_FOREACH(pe, ignores, entry) {
2534 struct got_pathlist_head *ignorelist = pe->data;
2535 struct got_pathlist_entry *pi;
2537 TAILQ_FOREACH(pi, ignorelist, entry) {
2538 const char *p, *pattern = pi->path;
2540 if (strncmp(pattern, "**/", 3) != 0)
2541 continue;
2542 pattern += 3;
2543 p = path;
2544 while (*p) {
2545 if (fnmatch(pattern, p,
2546 FNM_PATHNAME | FNM_LEADING_DIR)) {
2547 /* Retry in next directory. */
2548 while (*p && *p != '/')
2549 p++;
2550 while (*p == '/')
2551 p++;
2552 continue;
2554 return 1;
2560 * The ignores pathlist contains ignore lists from children before
2561 * parents, so we can find the most specific ignorelist by walking
2562 * ignores backwards.
2564 pe = TAILQ_LAST(ignores, got_pathlist_head);
2565 while (pe) {
2566 if (got_path_is_child(path, pe->path, pe->path_len)) {
2567 struct got_pathlist_head *ignorelist = pe->data;
2568 struct got_pathlist_entry *pi;
2569 TAILQ_FOREACH(pi, ignorelist, entry) {
2570 const char *pattern = pi->path;
2571 int flags = FNM_LEADING_DIR;
2572 if (strstr(pattern, "/**/") == NULL)
2573 flags |= FNM_PATHNAME;
2574 if (fnmatch(pattern, path, flags))
2575 continue;
2576 return 1;
2579 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2582 return 0;
2585 static const struct got_error *
2586 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2587 const char *path, int dirfd, const char *ignores_filename)
2589 const struct got_error *err = NULL;
2590 char *ignorespath;
2591 int fd = -1;
2592 FILE *ignoresfile = NULL;
2594 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2595 path[0] ? "/" : "", ignores_filename) == -1)
2596 return got_error_from_errno("asprintf");
2598 if (dirfd != -1) {
2599 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2600 if (fd == -1) {
2601 if (errno != ENOENT && errno != EACCES)
2602 err = got_error_from_errno2("openat",
2603 ignorespath);
2604 } else {
2605 ignoresfile = fdopen(fd, "r");
2606 if (ignoresfile == NULL)
2607 err = got_error_from_errno2("fdopen",
2608 ignorespath);
2609 else {
2610 fd = -1;
2611 err = read_ignores(ignores, path, ignoresfile);
2614 } else {
2615 ignoresfile = fopen(ignorespath, "r");
2616 if (ignoresfile == NULL) {
2617 if (errno != ENOENT && errno != EACCES)
2618 err = got_error_from_errno2("fopen",
2619 ignorespath);
2620 } else
2621 err = read_ignores(ignores, path, ignoresfile);
2624 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2625 err = got_error_from_errno2("fclose", path);
2626 if (fd != -1 && close(fd) == -1 && err == NULL)
2627 err = got_error_from_errno2("close", path);
2628 free(ignorespath);
2629 return err;
2632 static const struct got_error *
2633 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2635 const struct got_error *err = NULL;
2636 struct diff_dir_cb_arg *a = arg;
2637 char *path = NULL;
2639 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2640 return got_error(GOT_ERR_CANCELLED);
2642 /* XXX ignore symlinks for now */
2643 if (de->d_type == DT_LNK)
2644 return NULL;
2646 if (parent_path[0]) {
2647 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2648 return got_error_from_errno("asprintf");
2649 } else {
2650 path = de->d_name;
2653 if (de->d_type == DT_DIR) {
2654 int subdirfd;
2655 subdirfd = openat(dirfd, de->d_name,
2656 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2657 if (subdirfd != -1) {
2658 err = add_ignores(&a->ignores, a->worktree->root_path,
2659 path, subdirfd, ".cvsignore");
2660 if (err == NULL)
2661 err = add_ignores(&a->ignores,
2662 a->worktree->root_path, path,
2663 subdirfd, ".gitignore");
2664 if (close(subdirfd) == -1 && err == NULL)
2665 err = got_error_from_errno2("close", path);
2667 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2668 && !match_ignores(&a->ignores, path))
2669 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2670 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2671 if (parent_path[0])
2672 free(path);
2673 return err;
2676 static const struct got_error *
2677 report_single_file_status(const char *path, const char *ondisk_path,
2678 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2679 void *status_arg, struct got_repository *repo, int report_unchanged)
2681 struct got_fileindex_entry *ie;
2682 struct stat sb;
2684 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2685 if (ie)
2686 return report_file_status(ie, ondisk_path, -1, NULL,
2687 status_cb, status_arg, repo, report_unchanged);
2689 if (lstat(ondisk_path, &sb) == -1) {
2690 if (errno != ENOENT)
2691 return got_error_from_errno2("lstat", ondisk_path);
2692 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2693 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2694 return NULL;
2697 if (S_ISREG(sb.st_mode))
2698 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2699 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2701 return NULL;
2704 static const struct got_error *
2705 worktree_status(struct got_worktree *worktree, const char *path,
2706 struct got_fileindex *fileindex, struct got_repository *repo,
2707 got_worktree_status_cb status_cb, void *status_arg,
2708 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2709 int report_unchanged)
2711 const struct got_error *err = NULL;
2712 int fd = -1;
2713 struct got_fileindex_diff_dir_cb fdiff_cb;
2714 struct diff_dir_cb_arg arg;
2715 char *ondisk_path = NULL;
2717 if (asprintf(&ondisk_path, "%s%s%s",
2718 worktree->root_path, path[0] ? "/" : "", path) == -1)
2719 return got_error_from_errno("asprintf");
2721 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2722 if (fd == -1) {
2723 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2724 err = got_error_from_errno2("open", ondisk_path);
2725 else
2726 err = report_single_file_status(path, ondisk_path,
2727 fileindex, status_cb, status_arg, repo,
2728 report_unchanged);
2729 } else {
2730 fdiff_cb.diff_old_new = status_old_new;
2731 fdiff_cb.diff_old = status_old;
2732 fdiff_cb.diff_new = status_new;
2733 arg.fileindex = fileindex;
2734 arg.worktree = worktree;
2735 arg.status_path = path;
2736 arg.status_path_len = strlen(path);
2737 arg.repo = repo;
2738 arg.status_cb = status_cb;
2739 arg.status_arg = status_arg;
2740 arg.cancel_cb = cancel_cb;
2741 arg.cancel_arg = cancel_arg;
2742 arg.report_unchanged = report_unchanged;
2743 TAILQ_INIT(&arg.ignores);
2744 if (!no_ignores) {
2745 err = add_ignores(&arg.ignores, worktree->root_path,
2746 path, fd, ".cvsignore");
2747 if (err == NULL)
2748 err = add_ignores(&arg.ignores,
2749 worktree->root_path, path, fd,
2750 ".gitignore");
2752 if (err == NULL)
2753 err = got_fileindex_diff_dir(fileindex, fd,
2754 worktree->root_path, path, repo, &fdiff_cb, &arg);
2755 free_ignores(&arg.ignores);
2758 if (fd != -1 && close(fd) != 0 && err == NULL)
2759 err = got_error_from_errno("close");
2760 free(ondisk_path);
2761 return err;
2764 const struct got_error *
2765 got_worktree_status(struct got_worktree *worktree,
2766 struct got_pathlist_head *paths, struct got_repository *repo,
2767 got_worktree_status_cb status_cb, void *status_arg,
2768 got_cancel_cb cancel_cb, void *cancel_arg)
2770 const struct got_error *err = NULL;
2771 char *fileindex_path = NULL;
2772 struct got_fileindex *fileindex = NULL;
2773 struct got_pathlist_entry *pe;
2775 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2776 if (err)
2777 return err;
2779 TAILQ_FOREACH(pe, paths, entry) {
2780 err = worktree_status(worktree, pe->path, fileindex, repo,
2781 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2782 if (err)
2783 break;
2785 free(fileindex_path);
2786 got_fileindex_free(fileindex);
2787 return err;
2790 const struct got_error *
2791 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2792 const char *arg)
2794 const struct got_error *err = NULL;
2795 char *resolved, *cwd = NULL, *path = NULL;
2796 size_t len;
2798 *wt_path = NULL;
2800 resolved = realpath(arg, NULL);
2801 if (resolved == NULL) {
2802 if (errno != ENOENT)
2803 return got_error_from_errno2("realpath", arg);
2804 cwd = getcwd(NULL, 0);
2805 if (cwd == NULL)
2806 return got_error_from_errno("getcwd");
2807 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2808 err = got_error_from_errno("asprintf");
2809 goto done;
2813 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2814 strlen(got_worktree_get_root_path(worktree)))) {
2815 err = got_error(GOT_ERR_BAD_PATH);
2816 goto done;
2819 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2820 err = got_path_skip_common_ancestor(&path,
2821 got_worktree_get_root_path(worktree), resolved);
2822 if (err)
2823 goto done;
2824 } else {
2825 path = strdup("");
2826 if (path == NULL) {
2827 err = got_error_from_errno("strdup");
2828 goto done;
2832 /* XXX status walk can't deal with trailing slash! */
2833 len = strlen(path);
2834 while (len > 0 && path[len - 1] == '/') {
2835 path[len - 1] = '\0';
2836 len--;
2838 done:
2839 free(resolved);
2840 free(cwd);
2841 if (err == NULL)
2842 *wt_path = path;
2843 else
2844 free(path);
2845 return err;
2848 struct schedule_addition_args {
2849 struct got_worktree *worktree;
2850 struct got_fileindex *fileindex;
2851 got_worktree_checkout_cb progress_cb;
2852 void *progress_arg;
2853 struct got_repository *repo;
2856 static const struct got_error *
2857 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2858 const char *relpath, struct got_object_id *blob_id,
2859 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2860 int dirfd, const char *de_name)
2862 struct schedule_addition_args *a = arg;
2863 const struct got_error *err = NULL;
2864 struct got_fileindex_entry *ie;
2865 struct stat sb;
2866 char *ondisk_path;
2868 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2869 relpath) == -1)
2870 return got_error_from_errno("asprintf");
2872 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2873 if (ie) {
2874 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2875 de_name, a->repo);
2876 if (err)
2877 goto done;
2878 /* Re-adding an existing entry is a no-op. */
2879 if (status == GOT_STATUS_ADD)
2880 goto done;
2881 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2882 if (err)
2883 goto done;
2886 if (status != GOT_STATUS_UNVERSIONED) {
2887 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2888 goto done;
2891 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2892 if (err)
2893 goto done;
2895 err = got_fileindex_entry_add(a->fileindex, ie);
2896 if (err) {
2897 got_fileindex_entry_free(ie);
2898 goto done;
2900 done:
2901 free(ondisk_path);
2902 if (err)
2903 return err;
2904 if (status == GOT_STATUS_ADD)
2905 return NULL;
2906 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2909 const struct got_error *
2910 got_worktree_schedule_add(struct got_worktree *worktree,
2911 struct got_pathlist_head *paths,
2912 got_worktree_checkout_cb progress_cb, void *progress_arg,
2913 struct got_repository *repo, int no_ignores)
2915 struct got_fileindex *fileindex = NULL;
2916 char *fileindex_path = NULL;
2917 const struct got_error *err = NULL, *sync_err, *unlockerr;
2918 struct got_pathlist_entry *pe;
2919 struct schedule_addition_args saa;
2921 err = lock_worktree(worktree, LOCK_EX);
2922 if (err)
2923 return err;
2925 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2926 if (err)
2927 goto done;
2929 saa.worktree = worktree;
2930 saa.fileindex = fileindex;
2931 saa.progress_cb = progress_cb;
2932 saa.progress_arg = progress_arg;
2933 saa.repo = repo;
2935 TAILQ_FOREACH(pe, paths, entry) {
2936 err = worktree_status(worktree, pe->path, fileindex, repo,
2937 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2938 if (err)
2939 break;
2941 sync_err = sync_fileindex(fileindex, fileindex_path);
2942 if (sync_err && err == NULL)
2943 err = sync_err;
2944 done:
2945 free(fileindex_path);
2946 if (fileindex)
2947 got_fileindex_free(fileindex);
2948 unlockerr = lock_worktree(worktree, LOCK_SH);
2949 if (unlockerr && err == NULL)
2950 err = unlockerr;
2951 return err;
2954 struct schedule_deletion_args {
2955 struct got_worktree *worktree;
2956 struct got_fileindex *fileindex;
2957 got_worktree_delete_cb progress_cb;
2958 void *progress_arg;
2959 struct got_repository *repo;
2960 int delete_local_mods;
2961 int keep_on_disk;
2964 static const struct got_error *
2965 schedule_for_deletion(void *arg, unsigned char status,
2966 unsigned char staged_status, const char *relpath,
2967 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2968 struct got_object_id *commit_id, int dirfd, const char *de_name)
2970 struct schedule_deletion_args *a = arg;
2971 const struct got_error *err = NULL;
2972 struct got_fileindex_entry *ie = NULL;
2973 struct stat sb;
2974 char *ondisk_path;
2976 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2977 if (ie == NULL)
2978 return got_error(GOT_ERR_BAD_PATH);
2980 staged_status = get_staged_status(ie);
2981 if (staged_status != GOT_STATUS_NO_CHANGE) {
2982 if (staged_status == GOT_STATUS_DELETE)
2983 return NULL;
2984 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2987 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2988 relpath) == -1)
2989 return got_error_from_errno("asprintf");
2991 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2992 a->repo);
2993 if (err)
2994 goto done;
2996 if (status != GOT_STATUS_NO_CHANGE) {
2997 if (status == GOT_STATUS_DELETE)
2998 goto done;
2999 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3000 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3001 goto done;
3003 if (status != GOT_STATUS_MODIFY &&
3004 status != GOT_STATUS_MISSING) {
3005 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3006 goto done;
3010 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3011 if (dirfd != -1) {
3012 if (unlinkat(dirfd, de_name, 0) != 0) {
3013 err = got_error_from_errno2("unlinkat",
3014 ondisk_path);
3015 goto done;
3017 } else if (unlink(ondisk_path) != 0) {
3018 err = got_error_from_errno2("unlink", ondisk_path);
3019 goto done;
3023 got_fileindex_entry_mark_deleted_from_disk(ie);
3024 done:
3025 free(ondisk_path);
3026 if (err)
3027 return err;
3028 if (status == GOT_STATUS_DELETE)
3029 return NULL;
3030 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3031 staged_status, relpath);
3034 const struct got_error *
3035 got_worktree_schedule_delete(struct got_worktree *worktree,
3036 struct got_pathlist_head *paths, int delete_local_mods,
3037 got_worktree_delete_cb progress_cb, void *progress_arg,
3038 struct got_repository *repo, int keep_on_disk)
3040 struct got_fileindex *fileindex = NULL;
3041 char *fileindex_path = NULL;
3042 const struct got_error *err = NULL, *sync_err, *unlockerr;
3043 struct got_pathlist_entry *pe;
3044 struct schedule_deletion_args sda;
3046 err = lock_worktree(worktree, LOCK_EX);
3047 if (err)
3048 return err;
3050 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3051 if (err)
3052 goto done;
3054 sda.worktree = worktree;
3055 sda.fileindex = fileindex;
3056 sda.progress_cb = progress_cb;
3057 sda.progress_arg = progress_arg;
3058 sda.repo = repo;
3059 sda.delete_local_mods = delete_local_mods;
3060 sda.keep_on_disk = keep_on_disk;
3062 TAILQ_FOREACH(pe, paths, entry) {
3063 err = worktree_status(worktree, pe->path, fileindex, repo,
3064 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3065 if (err)
3066 break;
3068 sync_err = sync_fileindex(fileindex, fileindex_path);
3069 if (sync_err && err == NULL)
3070 err = sync_err;
3071 done:
3072 free(fileindex_path);
3073 if (fileindex)
3074 got_fileindex_free(fileindex);
3075 unlockerr = lock_worktree(worktree, LOCK_SH);
3076 if (unlockerr && err == NULL)
3077 err = unlockerr;
3078 return err;
3081 static const struct got_error *
3082 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3084 const struct got_error *err = NULL;
3085 char *line = NULL;
3086 size_t linesize = 0, n;
3087 ssize_t linelen;
3089 linelen = getline(&line, &linesize, infile);
3090 if (linelen == -1) {
3091 if (ferror(infile)) {
3092 err = got_error_from_errno("getline");
3093 goto done;
3095 return NULL;
3097 if (outfile) {
3098 n = fwrite(line, 1, linelen, outfile);
3099 if (n != linelen) {
3100 err = got_ferror(outfile, GOT_ERR_IO);
3101 goto done;
3104 if (rejectfile) {
3105 n = fwrite(line, 1, linelen, rejectfile);
3106 if (n != linelen)
3107 err = got_ferror(outfile, GOT_ERR_IO);
3109 done:
3110 free(line);
3111 return err;
3114 static const struct got_error *
3115 skip_one_line(FILE *f)
3117 char *line = NULL;
3118 size_t linesize = 0;
3119 ssize_t linelen;
3121 linelen = getline(&line, &linesize, f);
3122 if (linelen == -1) {
3123 if (ferror(f))
3124 return got_error_from_errno("getline");
3125 return NULL;
3127 free(line);
3128 return NULL;
3131 static const struct got_error *
3132 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3133 int start_old, int end_old, int start_new, int end_new,
3134 FILE *outfile, FILE *rejectfile)
3136 const struct got_error *err;
3138 /* Copy old file's lines leading up to patch. */
3139 while (!feof(f1) && *line_cur1 < start_old) {
3140 err = copy_one_line(f1, outfile, NULL);
3141 if (err)
3142 return err;
3143 (*line_cur1)++;
3145 /* Skip new file's lines leading up to patch. */
3146 while (!feof(f2) && *line_cur2 < start_new) {
3147 if (rejectfile)
3148 err = copy_one_line(f2, NULL, rejectfile);
3149 else
3150 err = skip_one_line(f2);
3151 if (err)
3152 return err;
3153 (*line_cur2)++;
3155 /* Copy patched lines. */
3156 while (!feof(f2) && *line_cur2 <= end_new) {
3157 err = copy_one_line(f2, outfile, NULL);
3158 if (err)
3159 return err;
3160 (*line_cur2)++;
3162 /* Skip over old file's replaced lines. */
3163 while (!feof(f1) && *line_cur1 <= end_old) {
3164 if (rejectfile)
3165 err = copy_one_line(f1, NULL, rejectfile);
3166 else
3167 err = skip_one_line(f1);
3168 if (err)
3169 return err;
3170 (*line_cur1)++;
3173 return NULL;
3176 static const struct got_error *
3177 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3178 FILE *outfile, FILE *rejectfile)
3180 const struct got_error *err;
3182 if (outfile) {
3183 /* Copy old file's lines until EOF. */
3184 while (!feof(f1)) {
3185 err = copy_one_line(f1, outfile, NULL);
3186 if (err)
3187 return err;
3188 (*line_cur1)++;
3191 if (rejectfile) {
3192 /* Copy new file's lines until EOF. */
3193 while (!feof(f2)) {
3194 err = copy_one_line(f2, NULL, rejectfile);
3195 if (err)
3196 return err;
3197 (*line_cur2)++;
3201 return NULL;
3204 static const struct got_error *
3205 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3206 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3207 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3208 int *line_cur2, FILE *outfile, FILE *rejectfile,
3209 got_worktree_patch_cb patch_cb, void *patch_arg)
3211 const struct got_error *err = NULL;
3212 int start_old = change->cv.a;
3213 int end_old = change->cv.b;
3214 int start_new = change->cv.c;
3215 int end_new = change->cv.d;
3216 long pos1, pos2;
3217 FILE *hunkfile;
3219 *choice = GOT_PATCH_CHOICE_NONE;
3221 hunkfile = got_opentemp();
3222 if (hunkfile == NULL)
3223 return got_error_from_errno("got_opentemp");
3225 pos1 = ftell(f1);
3226 pos2 = ftell(f2);
3228 /* XXX TODO needs error checking */
3229 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3231 if (fseek(f1, pos1, SEEK_SET) == -1) {
3232 err = got_ferror(f1, GOT_ERR_IO);
3233 goto done;
3235 if (fseek(f2, pos2, SEEK_SET) == -1) {
3236 err = got_ferror(f1, GOT_ERR_IO);
3237 goto done;
3239 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3240 err = got_ferror(hunkfile, GOT_ERR_IO);
3241 goto done;
3244 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3245 hunkfile, n, nchanges);
3246 if (err)
3247 goto done;
3249 switch (*choice) {
3250 case GOT_PATCH_CHOICE_YES:
3251 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3252 end_old, start_new, end_new, outfile, rejectfile);
3253 break;
3254 case GOT_PATCH_CHOICE_NO:
3255 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3256 end_old, start_new, end_new, rejectfile, outfile);
3257 break;
3258 case GOT_PATCH_CHOICE_QUIT:
3259 break;
3260 default:
3261 err = got_error(GOT_ERR_PATCH_CHOICE);
3262 break;
3264 done:
3265 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3266 err = got_error_from_errno("fclose");
3267 return err;
3270 struct revert_file_args {
3271 struct got_worktree *worktree;
3272 struct got_fileindex *fileindex;
3273 got_worktree_checkout_cb progress_cb;
3274 void *progress_arg;
3275 got_worktree_patch_cb patch_cb;
3276 void *patch_arg;
3277 struct got_repository *repo;
3280 static const struct got_error *
3281 create_patched_content(char **path_outfile, int reverse_patch,
3282 struct got_object_id *blob_id, const char *path2,
3283 int dirfd2, const char *de_name2,
3284 const char *relpath, struct got_repository *repo,
3285 got_worktree_patch_cb patch_cb, void *patch_arg)
3287 const struct got_error *err;
3288 struct got_blob_object *blob = NULL;
3289 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3290 int fd2 = -1;
3291 char *path1 = NULL, *id_str = NULL;
3292 struct stat sb1, sb2;
3293 struct got_diff_changes *changes = NULL;
3294 struct got_diff_state *ds = NULL;
3295 struct got_diff_args *args = NULL;
3296 struct got_diff_change *change;
3297 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3298 int n = 0;
3300 *path_outfile = NULL;
3302 err = got_object_id_str(&id_str, blob_id);
3303 if (err)
3304 return err;
3306 if (dirfd2 != -1) {
3307 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3308 if (fd2 == -1) {
3309 err = got_error_from_errno2("openat", path2);
3310 goto done;
3312 } else {
3313 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3314 if (fd2 == -1) {
3315 err = got_error_from_errno2("open", path2);
3316 goto done;
3319 if (fstat(fd2, &sb2) == -1) {
3320 err = got_error_from_errno2("fstat", path2);
3321 goto done;
3324 f2 = fdopen(fd2, "r");
3325 if (f2 == NULL) {
3326 err = got_error_from_errno2("fopen", path2);
3327 goto done;
3329 fd2 = -1;
3331 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3332 if (err)
3333 goto done;
3335 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3336 if (err)
3337 goto done;
3339 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3340 if (err)
3341 goto done;
3343 if (stat(path1, &sb1) == -1) {
3344 err = got_error_from_errno2("stat", path1);
3345 goto done;
3348 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3349 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3350 if (err)
3351 goto done;
3353 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3354 if (err)
3355 goto done;
3357 if (fseek(f1, 0L, SEEK_SET) == -1)
3358 return got_ferror(f1, GOT_ERR_IO);
3359 if (fseek(f2, 0L, SEEK_SET) == -1)
3360 return got_ferror(f2, GOT_ERR_IO);
3361 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3362 int choice;
3363 err = apply_or_reject_change(&choice, change, ++n,
3364 changes->nchanges, ds, args, diff_flags, relpath,
3365 f1, f2, &line_cur1, &line_cur2,
3366 reverse_patch ? NULL : outfile,
3367 reverse_patch ? outfile : NULL,
3368 patch_cb, patch_arg);
3369 if (err)
3370 goto done;
3371 if (choice == GOT_PATCH_CHOICE_YES)
3372 have_content = 1;
3373 else if (choice == GOT_PATCH_CHOICE_QUIT)
3374 break;
3376 if (have_content) {
3377 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3378 reverse_patch ? NULL : outfile,
3379 reverse_patch ? outfile : NULL);
3380 if (err)
3381 goto done;
3383 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3384 err = got_error_from_errno2("chmod", path2);
3385 goto done;
3388 done:
3389 free(id_str);
3390 if (blob)
3391 got_object_blob_close(blob);
3392 if (f1 && fclose(f1) == EOF && err == NULL)
3393 err = got_error_from_errno2("fclose", path1);
3394 if (f2 && fclose(f2) == EOF && err == NULL)
3395 err = got_error_from_errno2("fclose", path2);
3396 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3397 err = got_error_from_errno2("close", path2);
3398 if (outfile && fclose(outfile) == EOF && err == NULL)
3399 err = got_error_from_errno2("fclose", *path_outfile);
3400 if (path1 && unlink(path1) == -1 && err == NULL)
3401 err = got_error_from_errno2("unlink", path1);
3402 if (err || !have_content) {
3403 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3404 err = got_error_from_errno2("unlink", *path_outfile);
3405 free(*path_outfile);
3406 *path_outfile = NULL;
3408 free(args);
3409 if (ds) {
3410 got_diff_state_free(ds);
3411 free(ds);
3413 if (changes)
3414 got_diff_free_changes(changes);
3415 free(path1);
3416 return err;
3419 static const struct got_error *
3420 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3421 const char *relpath, struct got_object_id *blob_id,
3422 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3423 int dirfd, const char *de_name)
3425 struct revert_file_args *a = arg;
3426 const struct got_error *err = NULL;
3427 char *parent_path = NULL;
3428 struct got_fileindex_entry *ie;
3429 struct got_tree_object *tree = NULL;
3430 struct got_object_id *tree_id = NULL;
3431 const struct got_tree_entry *te = NULL;
3432 char *tree_path = NULL, *te_name;
3433 char *ondisk_path = NULL, *path_content = NULL;
3434 struct got_blob_object *blob = NULL;
3436 /* Reverting a staged deletion is a no-op. */
3437 if (status == GOT_STATUS_DELETE &&
3438 staged_status != GOT_STATUS_NO_CHANGE)
3439 return NULL;
3441 if (status == GOT_STATUS_UNVERSIONED)
3442 return (*a->progress_cb)(a->progress_arg,
3443 GOT_STATUS_UNVERSIONED, relpath);
3445 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3446 if (ie == NULL)
3447 return got_error(GOT_ERR_BAD_PATH);
3449 /* Construct in-repository path of tree which contains this blob. */
3450 err = got_path_dirname(&parent_path, ie->path);
3451 if (err) {
3452 if (err->code != GOT_ERR_BAD_PATH)
3453 goto done;
3454 parent_path = strdup("/");
3455 if (parent_path == NULL) {
3456 err = got_error_from_errno("strdup");
3457 goto done;
3460 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3461 tree_path = strdup(parent_path);
3462 if (tree_path == NULL) {
3463 err = got_error_from_errno("strdup");
3464 goto done;
3466 } else {
3467 if (got_path_is_root_dir(parent_path)) {
3468 tree_path = strdup(a->worktree->path_prefix);
3469 if (tree_path == NULL) {
3470 err = got_error_from_errno("strdup");
3471 goto done;
3473 } else {
3474 if (asprintf(&tree_path, "%s/%s",
3475 a->worktree->path_prefix, parent_path) == -1) {
3476 err = got_error_from_errno("asprintf");
3477 goto done;
3482 err = got_object_id_by_path(&tree_id, a->repo,
3483 a->worktree->base_commit_id, tree_path);
3484 if (err) {
3485 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3486 (status == GOT_STATUS_ADD ||
3487 staged_status == GOT_STATUS_ADD)))
3488 goto done;
3489 } else {
3490 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3491 if (err)
3492 goto done;
3494 te_name = basename(ie->path);
3495 if (te_name == NULL) {
3496 err = got_error_from_errno2("basename", ie->path);
3497 goto done;
3500 te = got_object_tree_find_entry(tree, te_name);
3501 if (te == NULL && status != GOT_STATUS_ADD &&
3502 staged_status != GOT_STATUS_ADD) {
3503 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3504 goto done;
3508 switch (status) {
3509 case GOT_STATUS_ADD:
3510 if (a->patch_cb) {
3511 int choice = GOT_PATCH_CHOICE_NONE;
3512 err = (*a->patch_cb)(&choice, a->patch_arg,
3513 status, ie->path, NULL, 1, 1);
3514 if (err)
3515 goto done;
3516 if (choice != GOT_PATCH_CHOICE_YES)
3517 break;
3519 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3520 ie->path);
3521 if (err)
3522 goto done;
3523 got_fileindex_entry_remove(a->fileindex, ie);
3524 break;
3525 case GOT_STATUS_DELETE:
3526 if (a->patch_cb) {
3527 int choice = GOT_PATCH_CHOICE_NONE;
3528 err = (*a->patch_cb)(&choice, a->patch_arg,
3529 status, ie->path, NULL, 1, 1);
3530 if (err)
3531 goto done;
3532 if (choice != GOT_PATCH_CHOICE_YES)
3533 break;
3535 /* fall through */
3536 case GOT_STATUS_MODIFY:
3537 case GOT_STATUS_MODE_CHANGE:
3538 case GOT_STATUS_CONFLICT:
3539 case GOT_STATUS_MISSING: {
3540 struct got_object_id id;
3541 if (staged_status == GOT_STATUS_ADD ||
3542 staged_status == GOT_STATUS_MODIFY) {
3543 memcpy(id.sha1, ie->staged_blob_sha1,
3544 SHA1_DIGEST_LENGTH);
3545 } else
3546 memcpy(id.sha1, ie->blob_sha1,
3547 SHA1_DIGEST_LENGTH);
3548 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3549 if (err)
3550 goto done;
3552 if (asprintf(&ondisk_path, "%s/%s",
3553 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3554 err = got_error_from_errno("asprintf");
3555 goto done;
3558 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3559 status == GOT_STATUS_CONFLICT)) {
3560 err = create_patched_content(&path_content, 1, &id,
3561 ondisk_path, dirfd, de_name, ie->path, a->repo,
3562 a->patch_cb, a->patch_arg);
3563 if (err || path_content == NULL)
3564 break;
3565 if (rename(path_content, ondisk_path) == -1) {
3566 err = got_error_from_errno3("rename",
3567 path_content, ondisk_path);
3568 goto done;
3570 } else {
3571 err = install_blob(a->worktree, ondisk_path, ie->path,
3572 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3573 got_fileindex_perms_to_st(ie), blob, 0, 1,
3574 a->repo, a->progress_cb, a->progress_arg);
3575 if (err)
3576 goto done;
3577 if (status == GOT_STATUS_DELETE ||
3578 status == GOT_STATUS_MODE_CHANGE) {
3579 err = update_blob_fileindex_entry(a->worktree,
3580 a->fileindex, ie, ondisk_path, ie->path,
3581 blob, 1);
3582 if (err)
3583 goto done;
3586 break;
3588 default:
3589 break;
3591 done:
3592 free(ondisk_path);
3593 free(path_content);
3594 free(parent_path);
3595 free(tree_path);
3596 if (blob)
3597 got_object_blob_close(blob);
3598 if (tree)
3599 got_object_tree_close(tree);
3600 free(tree_id);
3601 return err;
3604 const struct got_error *
3605 got_worktree_revert(struct got_worktree *worktree,
3606 struct got_pathlist_head *paths,
3607 got_worktree_checkout_cb progress_cb, void *progress_arg,
3608 got_worktree_patch_cb patch_cb, void *patch_arg,
3609 struct got_repository *repo)
3611 struct got_fileindex *fileindex = NULL;
3612 char *fileindex_path = NULL;
3613 const struct got_error *err = NULL, *unlockerr = NULL;
3614 const struct got_error *sync_err = NULL;
3615 struct got_pathlist_entry *pe;
3616 struct revert_file_args rfa;
3618 err = lock_worktree(worktree, LOCK_EX);
3619 if (err)
3620 return err;
3622 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3623 if (err)
3624 goto done;
3626 rfa.worktree = worktree;
3627 rfa.fileindex = fileindex;
3628 rfa.progress_cb = progress_cb;
3629 rfa.progress_arg = progress_arg;
3630 rfa.patch_cb = patch_cb;
3631 rfa.patch_arg = patch_arg;
3632 rfa.repo = repo;
3633 TAILQ_FOREACH(pe, paths, entry) {
3634 err = worktree_status(worktree, pe->path, fileindex, repo,
3635 revert_file, &rfa, NULL, NULL, 0, 0);
3636 if (err)
3637 break;
3639 sync_err = sync_fileindex(fileindex, fileindex_path);
3640 if (sync_err && err == NULL)
3641 err = sync_err;
3642 done:
3643 free(fileindex_path);
3644 if (fileindex)
3645 got_fileindex_free(fileindex);
3646 unlockerr = lock_worktree(worktree, LOCK_SH);
3647 if (unlockerr && err == NULL)
3648 err = unlockerr;
3649 return err;
3652 static void
3653 free_commitable(struct got_commitable *ct)
3655 free(ct->path);
3656 free(ct->in_repo_path);
3657 free(ct->ondisk_path);
3658 free(ct->blob_id);
3659 free(ct->base_blob_id);
3660 free(ct->staged_blob_id);
3661 free(ct->base_commit_id);
3662 free(ct);
3665 struct collect_commitables_arg {
3666 struct got_pathlist_head *commitable_paths;
3667 struct got_repository *repo;
3668 struct got_worktree *worktree;
3669 int have_staged_files;
3672 static const struct got_error *
3673 collect_commitables(void *arg, unsigned char status,
3674 unsigned char staged_status, const char *relpath,
3675 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3676 struct got_object_id *commit_id, int dirfd, const char *de_name)
3678 struct collect_commitables_arg *a = arg;
3679 const struct got_error *err = NULL;
3680 struct got_commitable *ct = NULL;
3681 struct got_pathlist_entry *new = NULL;
3682 char *parent_path = NULL, *path = NULL;
3683 struct stat sb;
3685 if (a->have_staged_files) {
3686 if (staged_status != GOT_STATUS_MODIFY &&
3687 staged_status != GOT_STATUS_ADD &&
3688 staged_status != GOT_STATUS_DELETE)
3689 return NULL;
3690 } else {
3691 if (status == GOT_STATUS_CONFLICT)
3692 return got_error(GOT_ERR_COMMIT_CONFLICT);
3694 if (status != GOT_STATUS_MODIFY &&
3695 status != GOT_STATUS_MODE_CHANGE &&
3696 status != GOT_STATUS_ADD &&
3697 status != GOT_STATUS_DELETE)
3698 return NULL;
3701 if (asprintf(&path, "/%s", relpath) == -1) {
3702 err = got_error_from_errno("asprintf");
3703 goto done;
3705 if (strcmp(path, "/") == 0) {
3706 parent_path = strdup("");
3707 if (parent_path == NULL)
3708 return got_error_from_errno("strdup");
3709 } else {
3710 err = got_path_dirname(&parent_path, path);
3711 if (err)
3712 return err;
3715 ct = calloc(1, sizeof(*ct));
3716 if (ct == NULL) {
3717 err = got_error_from_errno("calloc");
3718 goto done;
3721 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3722 relpath) == -1) {
3723 err = got_error_from_errno("asprintf");
3724 goto done;
3726 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3727 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3728 } else {
3729 if (dirfd != -1) {
3730 if (fstatat(dirfd, de_name, &sb,
3731 AT_SYMLINK_NOFOLLOW) == -1) {
3732 err = got_error_from_errno2("fstatat",
3733 ct->ondisk_path);
3734 goto done;
3736 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3737 err = got_error_from_errno2("lstat", ct->ondisk_path);
3738 goto done;
3740 ct->mode = sb.st_mode;
3743 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3744 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3745 relpath) == -1) {
3746 err = got_error_from_errno("asprintf");
3747 goto done;
3750 ct->status = status;
3751 ct->staged_status = staged_status;
3752 ct->blob_id = NULL; /* will be filled in when blob gets created */
3753 if (ct->status != GOT_STATUS_ADD &&
3754 ct->staged_status != GOT_STATUS_ADD) {
3755 ct->base_blob_id = got_object_id_dup(blob_id);
3756 if (ct->base_blob_id == NULL) {
3757 err = got_error_from_errno("got_object_id_dup");
3758 goto done;
3760 ct->base_commit_id = got_object_id_dup(commit_id);
3761 if (ct->base_commit_id == NULL) {
3762 err = got_error_from_errno("got_object_id_dup");
3763 goto done;
3766 if (ct->staged_status == GOT_STATUS_ADD ||
3767 ct->staged_status == GOT_STATUS_MODIFY) {
3768 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3769 if (ct->staged_blob_id == NULL) {
3770 err = got_error_from_errno("got_object_id_dup");
3771 goto done;
3774 ct->path = strdup(path);
3775 if (ct->path == NULL) {
3776 err = got_error_from_errno("strdup");
3777 goto done;
3779 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3780 done:
3781 if (ct && (err || new == NULL))
3782 free_commitable(ct);
3783 free(parent_path);
3784 free(path);
3785 return err;
3788 static const struct got_error *write_tree(struct got_object_id **,
3789 struct got_tree_object *, const char *, struct got_pathlist_head *,
3790 got_worktree_status_cb status_cb, void *status_arg,
3791 struct got_repository *);
3793 static const struct got_error *
3794 write_subtree(struct got_object_id **new_subtree_id,
3795 struct got_tree_entry *te, const char *parent_path,
3796 struct got_pathlist_head *commitable_paths,
3797 got_worktree_status_cb status_cb, void *status_arg,
3798 struct got_repository *repo)
3800 const struct got_error *err = NULL;
3801 struct got_tree_object *subtree;
3802 char *subpath;
3804 if (asprintf(&subpath, "%s%s%s", parent_path,
3805 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3806 return got_error_from_errno("asprintf");
3808 err = got_object_open_as_tree(&subtree, repo, &te->id);
3809 if (err)
3810 return err;
3812 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3813 status_cb, status_arg, repo);
3814 got_object_tree_close(subtree);
3815 free(subpath);
3816 return err;
3819 static const struct got_error *
3820 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3822 const struct got_error *err = NULL;
3823 char *ct_parent_path = NULL;
3825 *match = 0;
3827 if (strchr(ct->in_repo_path, '/') == NULL) {
3828 *match = got_path_is_root_dir(path);
3829 return NULL;
3832 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3833 if (err)
3834 return err;
3835 *match = (strcmp(path, ct_parent_path) == 0);
3836 free(ct_parent_path);
3837 return err;
3840 static mode_t
3841 get_ct_file_mode(struct got_commitable *ct)
3843 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3846 static const struct got_error *
3847 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3848 struct got_tree_entry *te, struct got_commitable *ct)
3850 const struct got_error *err = NULL;
3852 *new_te = NULL;
3854 err = got_object_tree_entry_dup(new_te, te);
3855 if (err)
3856 goto done;
3858 (*new_te)->mode = get_ct_file_mode(ct);
3860 if (ct->staged_status == GOT_STATUS_MODIFY)
3861 memcpy(&(*new_te)->id, ct->staged_blob_id,
3862 sizeof((*new_te)->id));
3863 else
3864 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3865 done:
3866 if (err && *new_te) {
3867 free(*new_te);
3868 *new_te = NULL;
3870 return err;
3873 static const struct got_error *
3874 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3875 struct got_commitable *ct)
3877 const struct got_error *err = NULL;
3878 char *ct_name;
3880 *new_te = NULL;
3882 *new_te = calloc(1, sizeof(**new_te));
3883 if (*new_te == NULL)
3884 return got_error_from_errno("calloc");
3886 ct_name = basename(ct->path);
3887 if (ct_name == NULL) {
3888 err = got_error_from_errno2("basename", ct->path);
3889 goto done;
3891 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3892 sizeof((*new_te)->name)) {
3893 err = got_error(GOT_ERR_NO_SPACE);
3894 goto done;
3897 (*new_te)->mode = get_ct_file_mode(ct);
3899 if (ct->staged_status == GOT_STATUS_ADD)
3900 memcpy(&(*new_te)->id, ct->staged_blob_id,
3901 sizeof((*new_te)->id));
3902 else
3903 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3904 done:
3905 if (err && *new_te) {
3906 free(*new_te);
3907 *new_te = NULL;
3909 return err;
3912 static const struct got_error *
3913 insert_tree_entry(struct got_tree_entry *new_te,
3914 struct got_pathlist_head *paths)
3916 const struct got_error *err = NULL;
3917 struct got_pathlist_entry *new_pe;
3919 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3920 if (err)
3921 return err;
3922 if (new_pe == NULL)
3923 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3924 return NULL;
3927 static const struct got_error *
3928 report_ct_status(struct got_commitable *ct,
3929 got_worktree_status_cb status_cb, void *status_arg)
3931 const char *ct_path = ct->path;
3932 unsigned char status;
3934 while (ct_path[0] == '/')
3935 ct_path++;
3937 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3938 status = ct->staged_status;
3939 else
3940 status = ct->status;
3942 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3943 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3946 static const struct got_error *
3947 match_modified_subtree(int *modified, struct got_tree_entry *te,
3948 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3950 const struct got_error *err = NULL;
3951 struct got_pathlist_entry *pe;
3952 char *te_path;
3954 *modified = 0;
3956 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3957 got_path_is_root_dir(base_tree_path) ? "" : "/",
3958 te->name) == -1)
3959 return got_error_from_errno("asprintf");
3961 TAILQ_FOREACH(pe, commitable_paths, entry) {
3962 struct got_commitable *ct = pe->data;
3963 *modified = got_path_is_child(ct->in_repo_path, te_path,
3964 strlen(te_path));
3965 if (*modified)
3966 break;
3969 free(te_path);
3970 return err;
3973 static const struct got_error *
3974 match_deleted_or_modified_ct(struct got_commitable **ctp,
3975 struct got_tree_entry *te, const char *base_tree_path,
3976 struct got_pathlist_head *commitable_paths)
3978 const struct got_error *err = NULL;
3979 struct got_pathlist_entry *pe;
3981 *ctp = NULL;
3983 TAILQ_FOREACH(pe, commitable_paths, entry) {
3984 struct got_commitable *ct = pe->data;
3985 char *ct_name = NULL;
3986 int path_matches;
3988 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3989 if (ct->status != GOT_STATUS_MODIFY &&
3990 ct->status != GOT_STATUS_MODE_CHANGE &&
3991 ct->status != GOT_STATUS_DELETE)
3992 continue;
3993 } else {
3994 if (ct->staged_status != GOT_STATUS_MODIFY &&
3995 ct->staged_status != GOT_STATUS_DELETE)
3996 continue;
3999 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4000 continue;
4002 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4003 if (err)
4004 return err;
4005 if (!path_matches)
4006 continue;
4008 ct_name = basename(pe->path);
4009 if (ct_name == NULL)
4010 return got_error_from_errno2("basename", pe->path);
4012 if (strcmp(te->name, ct_name) != 0)
4013 continue;
4015 *ctp = ct;
4016 break;
4019 return err;
4022 static const struct got_error *
4023 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4024 const char *child_path, const char *path_base_tree,
4025 struct got_pathlist_head *commitable_paths,
4026 got_worktree_status_cb status_cb, void *status_arg,
4027 struct got_repository *repo)
4029 const struct got_error *err = NULL;
4030 struct got_tree_entry *new_te;
4031 char *subtree_path;
4032 struct got_object_id *id = NULL;
4034 *new_tep = NULL;
4036 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4037 got_path_is_root_dir(path_base_tree) ? "" : "/",
4038 child_path) == -1)
4039 return got_error_from_errno("asprintf");
4041 new_te = calloc(1, sizeof(*new_te));
4042 if (new_te == NULL)
4043 return got_error_from_errno("calloc");
4044 new_te->mode = S_IFDIR;
4046 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4047 sizeof(new_te->name)) {
4048 err = got_error(GOT_ERR_NO_SPACE);
4049 goto done;
4051 err = write_tree(&id, NULL, subtree_path,
4052 commitable_paths, status_cb, status_arg, repo);
4053 if (err) {
4054 free(new_te);
4055 goto done;
4057 memcpy(&new_te->id, id, sizeof(new_te->id));
4058 done:
4059 free(id);
4060 free(subtree_path);
4061 if (err == NULL)
4062 *new_tep = new_te;
4063 return err;
4066 static const struct got_error *
4067 write_tree(struct got_object_id **new_tree_id,
4068 struct got_tree_object *base_tree, const char *path_base_tree,
4069 struct got_pathlist_head *commitable_paths,
4070 got_worktree_status_cb status_cb, void *status_arg,
4071 struct got_repository *repo)
4073 const struct got_error *err = NULL;
4074 struct got_pathlist_head paths;
4075 struct got_tree_entry *te, *new_te = NULL;
4076 struct got_pathlist_entry *pe;
4077 int nentries = 0;
4079 TAILQ_INIT(&paths);
4081 /* Insert, and recurse into, newly added entries first. */
4082 TAILQ_FOREACH(pe, commitable_paths, entry) {
4083 struct got_commitable *ct = pe->data;
4084 char *child_path = NULL, *slash;
4086 if ((ct->status != GOT_STATUS_ADD &&
4087 ct->staged_status != GOT_STATUS_ADD) ||
4088 (ct->flags & GOT_COMMITABLE_ADDED))
4089 continue;
4091 if (!got_path_is_child(pe->path, path_base_tree,
4092 strlen(path_base_tree)))
4093 continue;
4095 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4096 pe->path);
4097 if (err)
4098 goto done;
4100 slash = strchr(child_path, '/');
4101 if (slash == NULL) {
4102 err = alloc_added_blob_tree_entry(&new_te, ct);
4103 if (err)
4104 goto done;
4105 err = report_ct_status(ct, status_cb, status_arg);
4106 if (err)
4107 goto done;
4108 ct->flags |= GOT_COMMITABLE_ADDED;
4109 err = insert_tree_entry(new_te, &paths);
4110 if (err)
4111 goto done;
4112 nentries++;
4113 } else {
4114 *slash = '\0'; /* trim trailing path components */
4115 if (base_tree == NULL ||
4116 got_object_tree_find_entry(base_tree, child_path)
4117 == NULL) {
4118 err = make_subtree_for_added_blob(&new_te,
4119 child_path, path_base_tree,
4120 commitable_paths, status_cb, status_arg,
4121 repo);
4122 if (err)
4123 goto done;
4124 err = insert_tree_entry(new_te, &paths);
4125 if (err)
4126 goto done;
4127 nentries++;
4132 if (base_tree) {
4133 int i, nbase_entries;
4134 /* Handle modified and deleted entries. */
4135 nbase_entries = got_object_tree_get_nentries(base_tree);
4136 for (i = 0; i < nbase_entries; i++) {
4137 struct got_commitable *ct = NULL;
4139 te = got_object_tree_get_entry(base_tree, i);
4140 if (got_object_tree_entry_is_submodule(te)) {
4141 /* Entry is a submodule; just copy it. */
4142 err = got_object_tree_entry_dup(&new_te, te);
4143 if (err)
4144 goto done;
4145 err = insert_tree_entry(new_te, &paths);
4146 if (err)
4147 goto done;
4148 nentries++;
4149 continue;
4152 if (S_ISDIR(te->mode)) {
4153 int modified;
4154 err = got_object_tree_entry_dup(&new_te, te);
4155 if (err)
4156 goto done;
4157 err = match_modified_subtree(&modified, te,
4158 path_base_tree, commitable_paths);
4159 if (err)
4160 goto done;
4161 /* Avoid recursion into unmodified subtrees. */
4162 if (modified) {
4163 struct got_object_id *new_id;
4164 err = write_subtree(&new_id, te,
4165 path_base_tree, commitable_paths,
4166 status_cb, status_arg, repo);
4167 if (err)
4168 goto done;
4169 memcpy(&new_te->id, new_id,
4170 sizeof(new_te->id));
4171 free(new_id);
4173 err = insert_tree_entry(new_te, &paths);
4174 if (err)
4175 goto done;
4176 nentries++;
4177 continue;
4180 err = match_deleted_or_modified_ct(&ct, te,
4181 path_base_tree, commitable_paths);
4182 if (err)
4183 goto done;
4184 if (ct) {
4185 /* NB: Deleted entries get dropped here. */
4186 if (ct->status == GOT_STATUS_MODIFY ||
4187 ct->status == GOT_STATUS_MODE_CHANGE ||
4188 ct->staged_status == GOT_STATUS_MODIFY) {
4189 err = alloc_modified_blob_tree_entry(
4190 &new_te, te, ct);
4191 if (err)
4192 goto done;
4193 err = insert_tree_entry(new_te, &paths);
4194 if (err)
4195 goto done;
4196 nentries++;
4198 err = report_ct_status(ct, status_cb,
4199 status_arg);
4200 if (err)
4201 goto done;
4202 } else {
4203 /* Entry is unchanged; just copy it. */
4204 err = got_object_tree_entry_dup(&new_te, te);
4205 if (err)
4206 goto done;
4207 err = insert_tree_entry(new_te, &paths);
4208 if (err)
4209 goto done;
4210 nentries++;
4215 /* Write new list of entries; deleted entries have been dropped. */
4216 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4217 done:
4218 got_pathlist_free(&paths);
4219 return err;
4222 static const struct got_error *
4223 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4224 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4225 int have_staged_files)
4227 const struct got_error *err = NULL;
4228 struct got_pathlist_entry *pe;
4230 TAILQ_FOREACH(pe, commitable_paths, entry) {
4231 struct got_fileindex_entry *ie;
4232 struct got_commitable *ct = pe->data;
4234 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4235 if (ie) {
4236 if (ct->status == GOT_STATUS_DELETE ||
4237 ct->staged_status == GOT_STATUS_DELETE) {
4238 got_fileindex_entry_remove(fileindex, ie);
4239 got_fileindex_entry_free(ie);
4240 } else if (ct->staged_status == GOT_STATUS_ADD ||
4241 ct->staged_status == GOT_STATUS_MODIFY) {
4242 got_fileindex_entry_stage_set(ie,
4243 GOT_FILEIDX_STAGE_NONE);
4244 err = got_fileindex_entry_update(ie,
4245 ct->ondisk_path, ct->staged_blob_id->sha1,
4246 new_base_commit_id->sha1,
4247 !have_staged_files);
4248 } else
4249 err = got_fileindex_entry_update(ie,
4250 ct->ondisk_path, ct->blob_id->sha1,
4251 new_base_commit_id->sha1,
4252 !have_staged_files);
4253 } else {
4254 err = got_fileindex_entry_alloc(&ie,
4255 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4256 new_base_commit_id->sha1);
4257 if (err)
4258 break;
4259 err = got_fileindex_entry_add(fileindex, ie);
4260 if (err)
4261 break;
4264 return err;
4268 static const struct got_error *
4269 check_out_of_date(const char *in_repo_path, unsigned char status,
4270 unsigned char staged_status, struct got_object_id *base_blob_id,
4271 struct got_object_id *base_commit_id,
4272 struct got_object_id *head_commit_id, struct got_repository *repo,
4273 int ood_errcode)
4275 const struct got_error *err = NULL;
4276 struct got_object_id *id = NULL;
4278 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4279 /* Trivial case: base commit == head commit */
4280 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4281 return NULL;
4283 * Ensure file content which local changes were based
4284 * on matches file content in the branch head.
4286 err = got_object_id_by_path(&id, repo, head_commit_id,
4287 in_repo_path);
4288 if (err) {
4289 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4290 err = got_error(ood_errcode);
4291 goto done;
4292 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4293 err = got_error(ood_errcode);
4294 } else {
4295 /* Require that added files don't exist in the branch head. */
4296 err = got_object_id_by_path(&id, repo, head_commit_id,
4297 in_repo_path);
4298 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4299 goto done;
4300 err = id ? got_error(ood_errcode) : NULL;
4302 done:
4303 free(id);
4304 return err;
4307 const struct got_error *
4308 commit_worktree(struct got_object_id **new_commit_id,
4309 struct got_pathlist_head *commitable_paths,
4310 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4311 const char *author, const char *committer,
4312 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4313 got_worktree_status_cb status_cb, void *status_arg,
4314 struct got_repository *repo)
4316 const struct got_error *err = NULL, *unlockerr = NULL;
4317 struct got_pathlist_entry *pe;
4318 const char *head_ref_name = NULL;
4319 struct got_commit_object *head_commit = NULL;
4320 struct got_reference *head_ref2 = NULL;
4321 struct got_object_id *head_commit_id2 = NULL;
4322 struct got_tree_object *head_tree = NULL;
4323 struct got_object_id *new_tree_id = NULL;
4324 struct got_object_id_queue parent_ids;
4325 struct got_object_qid *pid = NULL;
4326 char *logmsg = NULL;
4328 *new_commit_id = NULL;
4330 SIMPLEQ_INIT(&parent_ids);
4332 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4333 if (err)
4334 goto done;
4336 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4337 if (err)
4338 goto done;
4340 if (commit_msg_cb != NULL) {
4341 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4342 if (err)
4343 goto done;
4346 if (logmsg == NULL || strlen(logmsg) == 0) {
4347 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4348 goto done;
4351 /* Create blobs from added and modified files and record their IDs. */
4352 TAILQ_FOREACH(pe, commitable_paths, entry) {
4353 struct got_commitable *ct = pe->data;
4354 char *ondisk_path;
4356 /* Blobs for staged files already exist. */
4357 if (ct->staged_status == GOT_STATUS_ADD ||
4358 ct->staged_status == GOT_STATUS_MODIFY)
4359 continue;
4361 if (ct->status != GOT_STATUS_ADD &&
4362 ct->status != GOT_STATUS_MODIFY &&
4363 ct->status != GOT_STATUS_MODE_CHANGE)
4364 continue;
4366 if (asprintf(&ondisk_path, "%s/%s",
4367 worktree->root_path, pe->path) == -1) {
4368 err = got_error_from_errno("asprintf");
4369 goto done;
4371 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4372 free(ondisk_path);
4373 if (err)
4374 goto done;
4377 /* Recursively write new tree objects. */
4378 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4379 status_cb, status_arg, repo);
4380 if (err)
4381 goto done;
4383 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4384 if (err)
4385 goto done;
4386 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4387 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4388 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4389 got_object_qid_free(pid);
4390 if (logmsg != NULL)
4391 free(logmsg);
4392 if (err)
4393 goto done;
4395 /* Check if a concurrent commit to our branch has occurred. */
4396 head_ref_name = got_worktree_get_head_ref_name(worktree);
4397 if (head_ref_name == NULL) {
4398 err = got_error_from_errno("got_worktree_get_head_ref_name");
4399 goto done;
4401 /* Lock the reference here to prevent concurrent modification. */
4402 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4403 if (err)
4404 goto done;
4405 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4406 if (err)
4407 goto done;
4408 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4409 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4410 goto done;
4412 /* Update branch head in repository. */
4413 err = got_ref_change_ref(head_ref2, *new_commit_id);
4414 if (err)
4415 goto done;
4416 err = got_ref_write(head_ref2, repo);
4417 if (err)
4418 goto done;
4420 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4421 if (err)
4422 goto done;
4424 err = ref_base_commit(worktree, repo);
4425 if (err)
4426 goto done;
4427 done:
4428 if (head_tree)
4429 got_object_tree_close(head_tree);
4430 if (head_commit)
4431 got_object_commit_close(head_commit);
4432 free(head_commit_id2);
4433 if (head_ref2) {
4434 unlockerr = got_ref_unlock(head_ref2);
4435 if (unlockerr && err == NULL)
4436 err = unlockerr;
4437 got_ref_close(head_ref2);
4439 return err;
4442 static const struct got_error *
4443 check_path_is_commitable(const char *path,
4444 struct got_pathlist_head *commitable_paths)
4446 struct got_pathlist_entry *cpe = NULL;
4447 size_t path_len = strlen(path);
4449 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4450 struct got_commitable *ct = cpe->data;
4451 const char *ct_path = ct->path;
4453 while (ct_path[0] == '/')
4454 ct_path++;
4456 if (strcmp(path, ct_path) == 0 ||
4457 got_path_is_child(ct_path, path, path_len))
4458 break;
4461 if (cpe == NULL)
4462 return got_error_path(path, GOT_ERR_BAD_PATH);
4464 return NULL;
4467 static const struct got_error *
4468 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4470 int *have_staged_files = arg;
4472 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4473 *have_staged_files = 1;
4474 return got_error(GOT_ERR_CANCELLED);
4477 return NULL;
4480 static const struct got_error *
4481 check_non_staged_files(struct got_fileindex *fileindex,
4482 struct got_pathlist_head *paths)
4484 struct got_pathlist_entry *pe;
4485 struct got_fileindex_entry *ie;
4487 TAILQ_FOREACH(pe, paths, entry) {
4488 if (pe->path[0] == '\0')
4489 continue;
4490 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4491 if (ie == NULL)
4492 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4493 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4494 return got_error_path(pe->path,
4495 GOT_ERR_FILE_NOT_STAGED);
4498 return NULL;
4501 const struct got_error *
4502 got_worktree_commit(struct got_object_id **new_commit_id,
4503 struct got_worktree *worktree, struct got_pathlist_head *paths,
4504 const char *author, const char *committer,
4505 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4506 got_worktree_status_cb status_cb, void *status_arg,
4507 struct got_repository *repo)
4509 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4510 struct got_fileindex *fileindex = NULL;
4511 char *fileindex_path = NULL;
4512 struct got_pathlist_head commitable_paths;
4513 struct collect_commitables_arg cc_arg;
4514 struct got_pathlist_entry *pe;
4515 struct got_reference *head_ref = NULL;
4516 struct got_object_id *head_commit_id = NULL;
4517 int have_staged_files = 0;
4519 *new_commit_id = NULL;
4521 TAILQ_INIT(&commitable_paths);
4523 err = lock_worktree(worktree, LOCK_EX);
4524 if (err)
4525 goto done;
4527 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4528 if (err)
4529 goto done;
4531 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4532 if (err)
4533 goto done;
4535 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4536 if (err)
4537 goto done;
4539 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4540 &have_staged_files);
4541 if (err && err->code != GOT_ERR_CANCELLED)
4542 goto done;
4543 if (have_staged_files) {
4544 err = check_non_staged_files(fileindex, paths);
4545 if (err)
4546 goto done;
4549 cc_arg.commitable_paths = &commitable_paths;
4550 cc_arg.worktree = worktree;
4551 cc_arg.repo = repo;
4552 cc_arg.have_staged_files = have_staged_files;
4553 TAILQ_FOREACH(pe, paths, entry) {
4554 err = worktree_status(worktree, pe->path, fileindex, repo,
4555 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4556 if (err)
4557 goto done;
4560 if (TAILQ_EMPTY(&commitable_paths)) {
4561 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4562 goto done;
4565 TAILQ_FOREACH(pe, paths, entry) {
4566 err = check_path_is_commitable(pe->path, &commitable_paths);
4567 if (err)
4568 goto done;
4571 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4572 struct got_commitable *ct = pe->data;
4573 const char *ct_path = ct->in_repo_path;
4575 while (ct_path[0] == '/')
4576 ct_path++;
4577 err = check_out_of_date(ct_path, ct->status,
4578 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4579 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4580 if (err)
4581 goto done;
4585 err = commit_worktree(new_commit_id, &commitable_paths,
4586 head_commit_id, worktree, author, committer,
4587 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4588 if (err)
4589 goto done;
4591 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4592 fileindex, have_staged_files);
4593 sync_err = sync_fileindex(fileindex, fileindex_path);
4594 if (sync_err && err == NULL)
4595 err = sync_err;
4596 done:
4597 if (fileindex)
4598 got_fileindex_free(fileindex);
4599 free(fileindex_path);
4600 unlockerr = lock_worktree(worktree, LOCK_SH);
4601 if (unlockerr && err == NULL)
4602 err = unlockerr;
4603 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4604 struct got_commitable *ct = pe->data;
4605 free_commitable(ct);
4607 got_pathlist_free(&commitable_paths);
4608 return err;
4611 const char *
4612 got_commitable_get_path(struct got_commitable *ct)
4614 return ct->path;
4617 unsigned int
4618 got_commitable_get_status(struct got_commitable *ct)
4620 return ct->status;
4623 struct check_rebase_ok_arg {
4624 struct got_worktree *worktree;
4625 struct got_repository *repo;
4628 static const struct got_error *
4629 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4631 const struct got_error *err = NULL;
4632 struct check_rebase_ok_arg *a = arg;
4633 unsigned char status;
4634 struct stat sb;
4635 char *ondisk_path;
4637 /* Reject rebase of a work tree with mixed base commits. */
4638 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4639 SHA1_DIGEST_LENGTH))
4640 return got_error(GOT_ERR_MIXED_COMMITS);
4642 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4643 == -1)
4644 return got_error_from_errno("asprintf");
4646 /* Reject rebase of a work tree with modified or staged files. */
4647 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4648 free(ondisk_path);
4649 if (err)
4650 return err;
4652 if (status != GOT_STATUS_NO_CHANGE)
4653 return got_error(GOT_ERR_MODIFIED);
4654 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4655 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4657 return NULL;
4660 const struct got_error *
4661 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4662 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4663 struct got_worktree *worktree, struct got_reference *branch,
4664 struct got_repository *repo)
4666 const struct got_error *err = NULL;
4667 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4668 char *branch_ref_name = NULL;
4669 char *fileindex_path = NULL;
4670 struct check_rebase_ok_arg ok_arg;
4671 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4673 *new_base_branch_ref = NULL;
4674 *tmp_branch = NULL;
4675 *fileindex = NULL;
4677 err = lock_worktree(worktree, LOCK_EX);
4678 if (err)
4679 return err;
4681 err = open_fileindex(fileindex, &fileindex_path, worktree);
4682 if (err)
4683 goto done;
4685 ok_arg.worktree = worktree;
4686 ok_arg.repo = repo;
4687 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4688 &ok_arg);
4689 if (err)
4690 goto done;
4692 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4693 if (err)
4694 goto done;
4696 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4697 if (err)
4698 goto done;
4700 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4701 if (err)
4702 goto done;
4704 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4705 0);
4706 if (err)
4707 goto done;
4709 err = got_ref_alloc_symref(new_base_branch_ref,
4710 new_base_branch_ref_name, wt_branch);
4711 if (err)
4712 goto done;
4713 err = got_ref_write(*new_base_branch_ref, repo);
4714 if (err)
4715 goto done;
4717 /* TODO Lock original branch's ref while rebasing? */
4719 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4720 if (err)
4721 goto done;
4723 err = got_ref_write(branch_ref, repo);
4724 if (err)
4725 goto done;
4727 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4728 worktree->base_commit_id);
4729 if (err)
4730 goto done;
4731 err = got_ref_write(*tmp_branch, repo);
4732 if (err)
4733 goto done;
4735 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4736 if (err)
4737 goto done;
4738 done:
4739 free(fileindex_path);
4740 free(tmp_branch_name);
4741 free(new_base_branch_ref_name);
4742 free(branch_ref_name);
4743 if (branch_ref)
4744 got_ref_close(branch_ref);
4745 if (wt_branch)
4746 got_ref_close(wt_branch);
4747 if (err) {
4748 if (*new_base_branch_ref) {
4749 got_ref_close(*new_base_branch_ref);
4750 *new_base_branch_ref = NULL;
4752 if (*tmp_branch) {
4753 got_ref_close(*tmp_branch);
4754 *tmp_branch = NULL;
4756 if (*fileindex) {
4757 got_fileindex_free(*fileindex);
4758 *fileindex = NULL;
4760 lock_worktree(worktree, LOCK_SH);
4762 return err;
4765 const struct got_error *
4766 got_worktree_rebase_continue(struct got_object_id **commit_id,
4767 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4768 struct got_reference **branch, struct got_fileindex **fileindex,
4769 struct got_worktree *worktree, struct got_repository *repo)
4771 const struct got_error *err;
4772 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4773 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4774 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4775 char *fileindex_path = NULL;
4776 int have_staged_files = 0;
4778 *commit_id = NULL;
4779 *new_base_branch = NULL;
4780 *tmp_branch = NULL;
4781 *branch = NULL;
4782 *fileindex = NULL;
4784 err = lock_worktree(worktree, LOCK_EX);
4785 if (err)
4786 return err;
4788 err = open_fileindex(fileindex, &fileindex_path, worktree);
4789 if (err)
4790 goto done;
4792 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4793 &have_staged_files);
4794 if (err && err->code != GOT_ERR_CANCELLED)
4795 goto done;
4796 if (have_staged_files) {
4797 err = got_error(GOT_ERR_STAGED_PATHS);
4798 goto done;
4801 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4802 if (err)
4803 goto done;
4805 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4806 if (err)
4807 goto done;
4809 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4810 if (err)
4811 goto done;
4813 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4814 if (err)
4815 goto done;
4817 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4818 if (err)
4819 goto done;
4821 err = got_ref_open(branch, repo,
4822 got_ref_get_symref_target(branch_ref), 0);
4823 if (err)
4824 goto done;
4826 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4827 if (err)
4828 goto done;
4830 err = got_ref_resolve(commit_id, repo, commit_ref);
4831 if (err)
4832 goto done;
4834 err = got_ref_open(new_base_branch, repo,
4835 new_base_branch_ref_name, 0);
4836 if (err)
4837 goto done;
4839 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4840 if (err)
4841 goto done;
4842 done:
4843 free(commit_ref_name);
4844 free(branch_ref_name);
4845 free(fileindex_path);
4846 if (commit_ref)
4847 got_ref_close(commit_ref);
4848 if (branch_ref)
4849 got_ref_close(branch_ref);
4850 if (err) {
4851 free(*commit_id);
4852 *commit_id = NULL;
4853 if (*tmp_branch) {
4854 got_ref_close(*tmp_branch);
4855 *tmp_branch = NULL;
4857 if (*new_base_branch) {
4858 got_ref_close(*new_base_branch);
4859 *new_base_branch = NULL;
4861 if (*branch) {
4862 got_ref_close(*branch);
4863 *branch = NULL;
4865 if (*fileindex) {
4866 got_fileindex_free(*fileindex);
4867 *fileindex = NULL;
4869 lock_worktree(worktree, LOCK_SH);
4871 return err;
4874 const struct got_error *
4875 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4877 const struct got_error *err;
4878 char *tmp_branch_name = NULL;
4880 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4881 if (err)
4882 return err;
4884 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4885 free(tmp_branch_name);
4886 return NULL;
4889 static const struct got_error *
4890 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4891 char **logmsg, void *arg)
4893 *logmsg = arg;
4894 return NULL;
4897 static const struct got_error *
4898 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4899 const char *path, struct got_object_id *blob_id,
4900 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4901 int dirfd, const char *de_name)
4903 return NULL;
4906 struct collect_merged_paths_arg {
4907 got_worktree_checkout_cb progress_cb;
4908 void *progress_arg;
4909 struct got_pathlist_head *merged_paths;
4912 static const struct got_error *
4913 collect_merged_paths(void *arg, unsigned char status, const char *path)
4915 const struct got_error *err;
4916 struct collect_merged_paths_arg *a = arg;
4917 char *p;
4918 struct got_pathlist_entry *new;
4920 err = (*a->progress_cb)(a->progress_arg, status, path);
4921 if (err)
4922 return err;
4924 if (status != GOT_STATUS_MERGE &&
4925 status != GOT_STATUS_ADD &&
4926 status != GOT_STATUS_DELETE &&
4927 status != GOT_STATUS_CONFLICT)
4928 return NULL;
4930 p = strdup(path);
4931 if (p == NULL)
4932 return got_error_from_errno("strdup");
4934 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4935 if (err || new == NULL)
4936 free(p);
4937 return err;
4940 void
4941 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4943 struct got_pathlist_entry *pe;
4945 TAILQ_FOREACH(pe, merged_paths, entry)
4946 free((char *)pe->path);
4948 got_pathlist_free(merged_paths);
4951 static const struct got_error *
4952 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4953 struct got_repository *repo)
4955 const struct got_error *err;
4956 struct got_reference *commit_ref = NULL;
4958 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4959 if (err) {
4960 if (err->code != GOT_ERR_NOT_REF)
4961 goto done;
4962 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4963 if (err)
4964 goto done;
4965 err = got_ref_write(commit_ref, repo);
4966 if (err)
4967 goto done;
4968 } else {
4969 struct got_object_id *stored_id;
4970 int cmp;
4972 err = got_ref_resolve(&stored_id, repo, commit_ref);
4973 if (err)
4974 goto done;
4975 cmp = got_object_id_cmp(commit_id, stored_id);
4976 free(stored_id);
4977 if (cmp != 0) {
4978 err = got_error(GOT_ERR_REBASE_COMMITID);
4979 goto done;
4982 done:
4983 if (commit_ref)
4984 got_ref_close(commit_ref);
4985 return err;
4988 static const struct got_error *
4989 rebase_merge_files(struct got_pathlist_head *merged_paths,
4990 const char *commit_ref_name, struct got_worktree *worktree,
4991 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4992 struct got_object_id *commit_id, struct got_repository *repo,
4993 got_worktree_checkout_cb progress_cb, void *progress_arg,
4994 got_cancel_cb cancel_cb, void *cancel_arg)
4996 const struct got_error *err;
4997 struct got_reference *commit_ref = NULL;
4998 struct collect_merged_paths_arg cmp_arg;
4999 char *fileindex_path;
5001 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5003 err = get_fileindex_path(&fileindex_path, worktree);
5004 if (err)
5005 return err;
5007 cmp_arg.progress_cb = progress_cb;
5008 cmp_arg.progress_arg = progress_arg;
5009 cmp_arg.merged_paths = merged_paths;
5010 err = merge_files(worktree, fileindex, fileindex_path,
5011 parent_commit_id, commit_id, repo, collect_merged_paths,
5012 &cmp_arg, cancel_cb, cancel_arg);
5013 if (commit_ref)
5014 got_ref_close(commit_ref);
5015 return err;
5018 const struct got_error *
5019 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5020 struct got_worktree *worktree, struct got_fileindex *fileindex,
5021 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5022 struct got_repository *repo,
5023 got_worktree_checkout_cb progress_cb, void *progress_arg,
5024 got_cancel_cb cancel_cb, void *cancel_arg)
5026 const struct got_error *err;
5027 char *commit_ref_name;
5029 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5030 if (err)
5031 return err;
5033 err = store_commit_id(commit_ref_name, commit_id, repo);
5034 if (err)
5035 goto done;
5037 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5038 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5039 progress_arg, cancel_cb, cancel_arg);
5040 done:
5041 free(commit_ref_name);
5042 return err;
5045 const struct got_error *
5046 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5047 struct got_worktree *worktree, struct got_fileindex *fileindex,
5048 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5049 struct got_repository *repo,
5050 got_worktree_checkout_cb progress_cb, void *progress_arg,
5051 got_cancel_cb cancel_cb, void *cancel_arg)
5053 const struct got_error *err;
5054 char *commit_ref_name;
5056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5057 if (err)
5058 return err;
5060 err = store_commit_id(commit_ref_name, commit_id, repo);
5061 if (err)
5062 goto done;
5064 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5065 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5066 progress_arg, cancel_cb, cancel_arg);
5067 done:
5068 free(commit_ref_name);
5069 return err;
5072 static const struct got_error *
5073 rebase_commit(struct got_object_id **new_commit_id,
5074 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5075 struct got_worktree *worktree, struct got_fileindex *fileindex,
5076 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5077 const char *new_logmsg, struct got_repository *repo)
5079 const struct got_error *err, *sync_err;
5080 struct got_pathlist_head commitable_paths;
5081 struct collect_commitables_arg cc_arg;
5082 char *fileindex_path = NULL;
5083 struct got_reference *head_ref = NULL;
5084 struct got_object_id *head_commit_id = NULL;
5085 char *logmsg = NULL;
5087 TAILQ_INIT(&commitable_paths);
5088 *new_commit_id = NULL;
5090 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5092 err = get_fileindex_path(&fileindex_path, worktree);
5093 if (err)
5094 return err;
5096 cc_arg.commitable_paths = &commitable_paths;
5097 cc_arg.worktree = worktree;
5098 cc_arg.repo = repo;
5099 cc_arg.have_staged_files = 0;
5101 * If possible get the status of individual files directly to
5102 * avoid crawling the entire work tree once per rebased commit.
5103 * TODO: Ideally, merged_paths would contain a list of commitables
5104 * we could use so we could skip worktree_status() entirely.
5106 if (merged_paths) {
5107 struct got_pathlist_entry *pe;
5108 if (TAILQ_EMPTY(merged_paths)) {
5109 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5110 goto done;
5112 TAILQ_FOREACH(pe, merged_paths, entry) {
5113 err = worktree_status(worktree, pe->path, fileindex,
5114 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5115 0);
5116 if (err)
5117 goto done;
5119 } else {
5120 err = worktree_status(worktree, "", fileindex, repo,
5121 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5122 if (err)
5123 goto done;
5126 if (TAILQ_EMPTY(&commitable_paths)) {
5127 /* No-op change; commit will be elided. */
5128 err = got_ref_delete(commit_ref, repo);
5129 if (err)
5130 goto done;
5131 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5132 goto done;
5135 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5136 if (err)
5137 goto done;
5139 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5140 if (err)
5141 goto done;
5143 if (new_logmsg) {
5144 logmsg = strdup(new_logmsg);
5145 if (logmsg == NULL) {
5146 err = got_error_from_errno("strdup");
5147 goto done;
5149 } else {
5150 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5151 if (err)
5152 goto done;
5155 /* NB: commit_worktree will call free(logmsg) */
5156 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5157 worktree, got_object_commit_get_author(orig_commit),
5158 got_object_commit_get_committer(orig_commit),
5159 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5160 if (err)
5161 goto done;
5163 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5164 if (err)
5165 goto done;
5167 err = got_ref_delete(commit_ref, repo);
5168 if (err)
5169 goto done;
5171 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5172 fileindex, 0);
5173 sync_err = sync_fileindex(fileindex, fileindex_path);
5174 if (sync_err && err == NULL)
5175 err = sync_err;
5176 done:
5177 free(fileindex_path);
5178 free(head_commit_id);
5179 if (head_ref)
5180 got_ref_close(head_ref);
5181 if (err) {
5182 free(*new_commit_id);
5183 *new_commit_id = NULL;
5185 return err;
5188 const struct got_error *
5189 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5190 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5191 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5192 struct got_commit_object *orig_commit,
5193 struct got_object_id *orig_commit_id, struct got_repository *repo)
5195 const struct got_error *err;
5196 char *commit_ref_name;
5197 struct got_reference *commit_ref = NULL;
5198 struct got_object_id *commit_id = NULL;
5200 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5201 if (err)
5202 return err;
5204 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5205 if (err)
5206 goto done;
5207 err = got_ref_resolve(&commit_id, repo, commit_ref);
5208 if (err)
5209 goto done;
5210 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5211 err = got_error(GOT_ERR_REBASE_COMMITID);
5212 goto done;
5215 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5216 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5217 done:
5218 if (commit_ref)
5219 got_ref_close(commit_ref);
5220 free(commit_ref_name);
5221 free(commit_id);
5222 return err;
5225 const struct got_error *
5226 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5227 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5228 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5229 struct got_commit_object *orig_commit,
5230 struct got_object_id *orig_commit_id, const char *new_logmsg,
5231 struct got_repository *repo)
5233 const struct got_error *err;
5234 char *commit_ref_name;
5235 struct got_reference *commit_ref = NULL;
5236 struct got_object_id *commit_id = NULL;
5238 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5239 if (err)
5240 return err;
5242 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5243 if (err)
5244 goto done;
5245 err = got_ref_resolve(&commit_id, repo, commit_ref);
5246 if (err)
5247 goto done;
5248 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5249 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5250 goto done;
5253 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5254 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5255 done:
5256 if (commit_ref)
5257 got_ref_close(commit_ref);
5258 free(commit_ref_name);
5259 free(commit_id);
5260 return err;
5263 const struct got_error *
5264 got_worktree_rebase_postpone(struct got_worktree *worktree,
5265 struct got_fileindex *fileindex)
5267 if (fileindex)
5268 got_fileindex_free(fileindex);
5269 return lock_worktree(worktree, LOCK_SH);
5272 static const struct got_error *
5273 delete_ref(const char *name, struct got_repository *repo)
5275 const struct got_error *err;
5276 struct got_reference *ref;
5278 err = got_ref_open(&ref, repo, name, 0);
5279 if (err) {
5280 if (err->code == GOT_ERR_NOT_REF)
5281 return NULL;
5282 return err;
5285 err = got_ref_delete(ref, repo);
5286 got_ref_close(ref);
5287 return err;
5290 static const struct got_error *
5291 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5293 const struct got_error *err;
5294 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5295 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5297 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5298 if (err)
5299 goto done;
5300 err = delete_ref(tmp_branch_name, repo);
5301 if (err)
5302 goto done;
5304 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5305 if (err)
5306 goto done;
5307 err = delete_ref(new_base_branch_ref_name, repo);
5308 if (err)
5309 goto done;
5311 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5312 if (err)
5313 goto done;
5314 err = delete_ref(branch_ref_name, repo);
5315 if (err)
5316 goto done;
5318 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5319 if (err)
5320 goto done;
5321 err = delete_ref(commit_ref_name, repo);
5322 if (err)
5323 goto done;
5325 done:
5326 free(tmp_branch_name);
5327 free(new_base_branch_ref_name);
5328 free(branch_ref_name);
5329 free(commit_ref_name);
5330 return err;
5333 const struct got_error *
5334 got_worktree_rebase_complete(struct got_worktree *worktree,
5335 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5336 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5337 struct got_repository *repo)
5339 const struct got_error *err, *unlockerr;
5340 struct got_object_id *new_head_commit_id = NULL;
5342 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5343 if (err)
5344 return err;
5346 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5347 if (err)
5348 goto done;
5350 err = got_ref_write(rebased_branch, repo);
5351 if (err)
5352 goto done;
5354 err = got_worktree_set_head_ref(worktree, rebased_branch);
5355 if (err)
5356 goto done;
5358 err = delete_rebase_refs(worktree, repo);
5359 done:
5360 if (fileindex)
5361 got_fileindex_free(fileindex);
5362 free(new_head_commit_id);
5363 unlockerr = lock_worktree(worktree, LOCK_SH);
5364 if (unlockerr && err == NULL)
5365 err = unlockerr;
5366 return err;
5369 const struct got_error *
5370 got_worktree_rebase_abort(struct got_worktree *worktree,
5371 struct got_fileindex *fileindex, struct got_repository *repo,
5372 struct got_reference *new_base_branch,
5373 got_worktree_checkout_cb progress_cb, void *progress_arg)
5375 const struct got_error *err, *unlockerr, *sync_err;
5376 struct got_reference *resolved = NULL;
5377 struct got_object_id *commit_id = NULL;
5378 char *fileindex_path = NULL;
5379 struct revert_file_args rfa;
5380 struct got_object_id *tree_id = NULL;
5382 err = lock_worktree(worktree, LOCK_EX);
5383 if (err)
5384 return err;
5386 err = got_ref_open(&resolved, repo,
5387 got_ref_get_symref_target(new_base_branch), 0);
5388 if (err)
5389 goto done;
5391 err = got_worktree_set_head_ref(worktree, resolved);
5392 if (err)
5393 goto done;
5396 * XXX commits to the base branch could have happened while
5397 * we were busy rebasing; should we store the original commit ID
5398 * when rebase begins and read it back here?
5400 err = got_ref_resolve(&commit_id, repo, resolved);
5401 if (err)
5402 goto done;
5404 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5405 if (err)
5406 goto done;
5408 err = got_object_id_by_path(&tree_id, repo,
5409 worktree->base_commit_id, worktree->path_prefix);
5410 if (err)
5411 goto done;
5413 err = delete_rebase_refs(worktree, repo);
5414 if (err)
5415 goto done;
5417 err = get_fileindex_path(&fileindex_path, worktree);
5418 if (err)
5419 goto done;
5421 rfa.worktree = worktree;
5422 rfa.fileindex = fileindex;
5423 rfa.progress_cb = progress_cb;
5424 rfa.progress_arg = progress_arg;
5425 rfa.patch_cb = NULL;
5426 rfa.patch_arg = NULL;
5427 rfa.repo = repo;
5428 err = worktree_status(worktree, "", fileindex, repo,
5429 revert_file, &rfa, NULL, NULL, 0, 0);
5430 if (err)
5431 goto sync;
5433 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5434 repo, progress_cb, progress_arg, NULL, NULL);
5435 sync:
5436 sync_err = sync_fileindex(fileindex, fileindex_path);
5437 if (sync_err && err == NULL)
5438 err = sync_err;
5439 done:
5440 got_ref_close(resolved);
5441 free(tree_id);
5442 free(commit_id);
5443 if (fileindex)
5444 got_fileindex_free(fileindex);
5445 free(fileindex_path);
5447 unlockerr = lock_worktree(worktree, LOCK_SH);
5448 if (unlockerr && err == NULL)
5449 err = unlockerr;
5450 return err;
5453 const struct got_error *
5454 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5455 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5456 struct got_fileindex **fileindex, struct got_worktree *worktree,
5457 struct got_repository *repo)
5459 const struct got_error *err = NULL;
5460 char *tmp_branch_name = NULL;
5461 char *branch_ref_name = NULL;
5462 char *base_commit_ref_name = NULL;
5463 char *fileindex_path = NULL;
5464 struct check_rebase_ok_arg ok_arg;
5465 struct got_reference *wt_branch = NULL;
5466 struct got_reference *base_commit_ref = NULL;
5468 *tmp_branch = NULL;
5469 *branch_ref = NULL;
5470 *base_commit_id = NULL;
5471 *fileindex = NULL;
5473 err = lock_worktree(worktree, LOCK_EX);
5474 if (err)
5475 return err;
5477 err = open_fileindex(fileindex, &fileindex_path, worktree);
5478 if (err)
5479 goto done;
5481 ok_arg.worktree = worktree;
5482 ok_arg.repo = repo;
5483 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5484 &ok_arg);
5485 if (err)
5486 goto done;
5488 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5489 if (err)
5490 goto done;
5492 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5493 if (err)
5494 goto done;
5496 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5497 worktree);
5498 if (err)
5499 goto done;
5501 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5502 0);
5503 if (err)
5504 goto done;
5506 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5507 if (err)
5508 goto done;
5510 err = got_ref_write(*branch_ref, repo);
5511 if (err)
5512 goto done;
5514 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5515 worktree->base_commit_id);
5516 if (err)
5517 goto done;
5518 err = got_ref_write(base_commit_ref, repo);
5519 if (err)
5520 goto done;
5521 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5522 if (*base_commit_id == NULL) {
5523 err = got_error_from_errno("got_object_id_dup");
5524 goto done;
5527 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5528 worktree->base_commit_id);
5529 if (err)
5530 goto done;
5531 err = got_ref_write(*tmp_branch, repo);
5532 if (err)
5533 goto done;
5535 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5536 if (err)
5537 goto done;
5538 done:
5539 free(fileindex_path);
5540 free(tmp_branch_name);
5541 free(branch_ref_name);
5542 free(base_commit_ref_name);
5543 if (wt_branch)
5544 got_ref_close(wt_branch);
5545 if (err) {
5546 if (*branch_ref) {
5547 got_ref_close(*branch_ref);
5548 *branch_ref = NULL;
5550 if (*tmp_branch) {
5551 got_ref_close(*tmp_branch);
5552 *tmp_branch = NULL;
5554 free(*base_commit_id);
5555 if (*fileindex) {
5556 got_fileindex_free(*fileindex);
5557 *fileindex = NULL;
5559 lock_worktree(worktree, LOCK_SH);
5561 return err;
5564 const struct got_error *
5565 got_worktree_histedit_postpone(struct got_worktree *worktree,
5566 struct got_fileindex *fileindex)
5568 if (fileindex)
5569 got_fileindex_free(fileindex);
5570 return lock_worktree(worktree, LOCK_SH);
5573 const struct got_error *
5574 got_worktree_histedit_in_progress(int *in_progress,
5575 struct got_worktree *worktree)
5577 const struct got_error *err;
5578 char *tmp_branch_name = NULL;
5580 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5581 if (err)
5582 return err;
5584 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5585 free(tmp_branch_name);
5586 return NULL;
5589 const struct got_error *
5590 got_worktree_histedit_continue(struct got_object_id **commit_id,
5591 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5592 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5593 struct got_worktree *worktree, struct got_repository *repo)
5595 const struct got_error *err;
5596 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5597 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5598 struct got_reference *commit_ref = NULL;
5599 struct got_reference *base_commit_ref = NULL;
5600 char *fileindex_path = NULL;
5601 int have_staged_files = 0;
5603 *commit_id = NULL;
5604 *tmp_branch = NULL;
5605 *base_commit_id = NULL;
5606 *fileindex = NULL;
5608 err = lock_worktree(worktree, LOCK_EX);
5609 if (err)
5610 return err;
5612 err = open_fileindex(fileindex, &fileindex_path, worktree);
5613 if (err)
5614 goto done;
5616 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5617 &have_staged_files);
5618 if (err && err->code != GOT_ERR_CANCELLED)
5619 goto done;
5620 if (have_staged_files) {
5621 err = got_error(GOT_ERR_STAGED_PATHS);
5622 goto done;
5625 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5626 if (err)
5627 goto done;
5629 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5630 if (err)
5631 goto done;
5633 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5634 if (err)
5635 goto done;
5637 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5638 worktree);
5639 if (err)
5640 goto done;
5642 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5643 if (err)
5644 goto done;
5646 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5647 if (err)
5648 goto done;
5649 err = got_ref_resolve(commit_id, repo, commit_ref);
5650 if (err)
5651 goto done;
5653 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5654 if (err)
5655 goto done;
5656 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5657 if (err)
5658 goto done;
5660 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5661 if (err)
5662 goto done;
5663 done:
5664 free(commit_ref_name);
5665 free(branch_ref_name);
5666 free(fileindex_path);
5667 if (commit_ref)
5668 got_ref_close(commit_ref);
5669 if (base_commit_ref)
5670 got_ref_close(base_commit_ref);
5671 if (err) {
5672 free(*commit_id);
5673 *commit_id = NULL;
5674 free(*base_commit_id);
5675 *base_commit_id = NULL;
5676 if (*tmp_branch) {
5677 got_ref_close(*tmp_branch);
5678 *tmp_branch = NULL;
5680 if (*fileindex) {
5681 got_fileindex_free(*fileindex);
5682 *fileindex = NULL;
5684 lock_worktree(worktree, LOCK_EX);
5686 return err;
5689 static const struct got_error *
5690 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5692 const struct got_error *err;
5693 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5694 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5696 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5697 if (err)
5698 goto done;
5699 err = delete_ref(tmp_branch_name, repo);
5700 if (err)
5701 goto done;
5703 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5704 worktree);
5705 if (err)
5706 goto done;
5707 err = delete_ref(base_commit_ref_name, repo);
5708 if (err)
5709 goto done;
5711 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5712 if (err)
5713 goto done;
5714 err = delete_ref(branch_ref_name, repo);
5715 if (err)
5716 goto done;
5718 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5719 if (err)
5720 goto done;
5721 err = delete_ref(commit_ref_name, repo);
5722 if (err)
5723 goto done;
5724 done:
5725 free(tmp_branch_name);
5726 free(base_commit_ref_name);
5727 free(branch_ref_name);
5728 free(commit_ref_name);
5729 return err;
5732 const struct got_error *
5733 got_worktree_histedit_abort(struct got_worktree *worktree,
5734 struct got_fileindex *fileindex, struct got_repository *repo,
5735 struct got_reference *branch, struct got_object_id *base_commit_id,
5736 got_worktree_checkout_cb progress_cb, void *progress_arg)
5738 const struct got_error *err, *unlockerr, *sync_err;
5739 struct got_reference *resolved = NULL;
5740 char *fileindex_path = NULL;
5741 struct got_object_id *tree_id = NULL;
5742 struct revert_file_args rfa;
5744 err = lock_worktree(worktree, LOCK_EX);
5745 if (err)
5746 return err;
5748 err = got_ref_open(&resolved, repo,
5749 got_ref_get_symref_target(branch), 0);
5750 if (err)
5751 goto done;
5753 err = got_worktree_set_head_ref(worktree, resolved);
5754 if (err)
5755 goto done;
5757 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5758 if (err)
5759 goto done;
5761 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5762 worktree->path_prefix);
5763 if (err)
5764 goto done;
5766 err = delete_histedit_refs(worktree, repo);
5767 if (err)
5768 goto done;
5770 err = get_fileindex_path(&fileindex_path, worktree);
5771 if (err)
5772 goto done;
5774 rfa.worktree = worktree;
5775 rfa.fileindex = fileindex;
5776 rfa.progress_cb = progress_cb;
5777 rfa.progress_arg = progress_arg;
5778 rfa.patch_cb = NULL;
5779 rfa.patch_arg = NULL;
5780 rfa.repo = repo;
5781 err = worktree_status(worktree, "", fileindex, repo,
5782 revert_file, &rfa, NULL, NULL, 0, 0);
5783 if (err)
5784 goto sync;
5786 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5787 repo, progress_cb, progress_arg, NULL, NULL);
5788 sync:
5789 sync_err = sync_fileindex(fileindex, fileindex_path);
5790 if (sync_err && err == NULL)
5791 err = sync_err;
5792 done:
5793 got_ref_close(resolved);
5794 free(tree_id);
5795 free(fileindex_path);
5797 unlockerr = lock_worktree(worktree, LOCK_SH);
5798 if (unlockerr && err == NULL)
5799 err = unlockerr;
5800 return err;
5803 const struct got_error *
5804 got_worktree_histedit_complete(struct got_worktree *worktree,
5805 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5806 struct got_reference *edited_branch, struct got_repository *repo)
5808 const struct got_error *err, *unlockerr;
5809 struct got_object_id *new_head_commit_id = NULL;
5810 struct got_reference *resolved = NULL;
5812 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5813 if (err)
5814 return err;
5816 err = got_ref_open(&resolved, repo,
5817 got_ref_get_symref_target(edited_branch), 0);
5818 if (err)
5819 goto done;
5821 err = got_ref_change_ref(resolved, new_head_commit_id);
5822 if (err)
5823 goto done;
5825 err = got_ref_write(resolved, repo);
5826 if (err)
5827 goto done;
5829 err = got_worktree_set_head_ref(worktree, resolved);
5830 if (err)
5831 goto done;
5833 err = delete_histedit_refs(worktree, repo);
5834 done:
5835 if (fileindex)
5836 got_fileindex_free(fileindex);
5837 free(new_head_commit_id);
5838 unlockerr = lock_worktree(worktree, LOCK_SH);
5839 if (unlockerr && err == NULL)
5840 err = unlockerr;
5841 return err;
5844 const struct got_error *
5845 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5846 struct got_object_id *commit_id, struct got_repository *repo)
5848 const struct got_error *err;
5849 char *commit_ref_name;
5851 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5852 if (err)
5853 return err;
5855 err = store_commit_id(commit_ref_name, commit_id, repo);
5856 if (err)
5857 goto done;
5859 err = delete_ref(commit_ref_name, repo);
5860 done:
5861 free(commit_ref_name);
5862 return err;
5865 const struct got_error *
5866 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5867 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5868 struct got_worktree *worktree, const char *refname,
5869 struct got_repository *repo)
5871 const struct got_error *err = NULL;
5872 char *fileindex_path = NULL;
5873 struct check_rebase_ok_arg ok_arg;
5875 *fileindex = NULL;
5876 *branch_ref = NULL;
5877 *base_branch_ref = NULL;
5879 err = lock_worktree(worktree, LOCK_EX);
5880 if (err)
5881 return err;
5883 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5884 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5885 "cannot integrate a branch into itself; "
5886 "update -b or different branch name required");
5887 goto done;
5890 err = open_fileindex(fileindex, &fileindex_path, worktree);
5891 if (err)
5892 goto done;
5894 /* Preconditions are the same as for rebase. */
5895 ok_arg.worktree = worktree;
5896 ok_arg.repo = repo;
5897 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5898 &ok_arg);
5899 if (err)
5900 goto done;
5902 err = got_ref_open(branch_ref, repo, refname, 1);
5903 if (err)
5904 goto done;
5906 err = got_ref_open(base_branch_ref, repo,
5907 got_worktree_get_head_ref_name(worktree), 1);
5908 done:
5909 if (err) {
5910 if (*branch_ref) {
5911 got_ref_close(*branch_ref);
5912 *branch_ref = NULL;
5914 if (*base_branch_ref) {
5915 got_ref_close(*base_branch_ref);
5916 *base_branch_ref = NULL;
5918 if (*fileindex) {
5919 got_fileindex_free(*fileindex);
5920 *fileindex = NULL;
5922 lock_worktree(worktree, LOCK_SH);
5924 return err;
5927 const struct got_error *
5928 got_worktree_integrate_continue(struct got_worktree *worktree,
5929 struct got_fileindex *fileindex, struct got_repository *repo,
5930 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5931 got_worktree_checkout_cb progress_cb, void *progress_arg,
5932 got_cancel_cb cancel_cb, void *cancel_arg)
5934 const struct got_error *err = NULL, *sync_err, *unlockerr;
5935 char *fileindex_path = NULL;
5936 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5938 err = get_fileindex_path(&fileindex_path, worktree);
5939 if (err)
5940 goto done;
5942 err = got_ref_resolve(&commit_id, repo, branch_ref);
5943 if (err)
5944 goto done;
5946 err = got_object_id_by_path(&tree_id, repo, commit_id,
5947 worktree->path_prefix);
5948 if (err)
5949 goto done;
5951 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5952 if (err)
5953 goto done;
5955 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5956 progress_cb, progress_arg, cancel_cb, cancel_arg);
5957 if (err)
5958 goto sync;
5960 err = got_ref_change_ref(base_branch_ref, commit_id);
5961 if (err)
5962 goto sync;
5964 err = got_ref_write(base_branch_ref, repo);
5965 sync:
5966 sync_err = sync_fileindex(fileindex, fileindex_path);
5967 if (sync_err && err == NULL)
5968 err = sync_err;
5970 done:
5971 unlockerr = got_ref_unlock(branch_ref);
5972 if (unlockerr && err == NULL)
5973 err = unlockerr;
5974 got_ref_close(branch_ref);
5976 unlockerr = got_ref_unlock(base_branch_ref);
5977 if (unlockerr && err == NULL)
5978 err = unlockerr;
5979 got_ref_close(base_branch_ref);
5981 got_fileindex_free(fileindex);
5982 free(fileindex_path);
5983 free(tree_id);
5985 unlockerr = lock_worktree(worktree, LOCK_SH);
5986 if (unlockerr && err == NULL)
5987 err = unlockerr;
5988 return err;
5991 const struct got_error *
5992 got_worktree_integrate_abort(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex, struct got_repository *repo,
5994 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5996 const struct got_error *err = NULL, *unlockerr = NULL;
5998 got_fileindex_free(fileindex);
6000 err = lock_worktree(worktree, LOCK_SH);
6002 unlockerr = got_ref_unlock(branch_ref);
6003 if (unlockerr && err == NULL)
6004 err = unlockerr;
6005 got_ref_close(branch_ref);
6007 unlockerr = got_ref_unlock(base_branch_ref);
6008 if (unlockerr && err == NULL)
6009 err = unlockerr;
6010 got_ref_close(base_branch_ref);
6012 return err;
6015 struct check_stage_ok_arg {
6016 struct got_object_id *head_commit_id;
6017 struct got_worktree *worktree;
6018 struct got_fileindex *fileindex;
6019 struct got_repository *repo;
6020 int have_changes;
6023 const struct got_error *
6024 check_stage_ok(void *arg, unsigned char status,
6025 unsigned char staged_status, const char *relpath,
6026 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6027 struct got_object_id *commit_id, int dirfd, const char *de_name)
6029 struct check_stage_ok_arg *a = arg;
6030 const struct got_error *err = NULL;
6031 struct got_fileindex_entry *ie;
6032 struct got_object_id base_commit_id;
6033 struct got_object_id *base_commit_idp = NULL;
6034 char *in_repo_path = NULL, *p;
6036 if (status == GOT_STATUS_UNVERSIONED ||
6037 status == GOT_STATUS_NO_CHANGE)
6038 return NULL;
6039 if (status == GOT_STATUS_NONEXISTENT)
6040 return got_error_set_errno(ENOENT, relpath);
6042 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6043 if (ie == NULL)
6044 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6046 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6047 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6048 relpath) == -1)
6049 return got_error_from_errno("asprintf");
6051 if (got_fileindex_entry_has_commit(ie)) {
6052 memcpy(base_commit_id.sha1, ie->commit_sha1,
6053 SHA1_DIGEST_LENGTH);
6054 base_commit_idp = &base_commit_id;
6057 if (status == GOT_STATUS_CONFLICT) {
6058 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6059 goto done;
6060 } else if (status != GOT_STATUS_ADD &&
6061 status != GOT_STATUS_MODIFY &&
6062 status != GOT_STATUS_DELETE) {
6063 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6064 goto done;
6067 a->have_changes = 1;
6069 p = in_repo_path;
6070 while (p[0] == '/')
6071 p++;
6072 err = check_out_of_date(p, status, staged_status,
6073 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6074 GOT_ERR_STAGE_OUT_OF_DATE);
6075 done:
6076 free(in_repo_path);
6077 return err;
6080 struct stage_path_arg {
6081 struct got_worktree *worktree;
6082 struct got_fileindex *fileindex;
6083 struct got_repository *repo;
6084 got_worktree_status_cb status_cb;
6085 void *status_arg;
6086 got_worktree_patch_cb patch_cb;
6087 void *patch_arg;
6088 int staged_something;
6091 static const struct got_error *
6092 stage_path(void *arg, unsigned char status,
6093 unsigned char staged_status, const char *relpath,
6094 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6095 struct got_object_id *commit_id, int dirfd, const char *de_name)
6097 struct stage_path_arg *a = arg;
6098 const struct got_error *err = NULL;
6099 struct got_fileindex_entry *ie;
6100 char *ondisk_path = NULL, *path_content = NULL;
6101 uint32_t stage;
6102 struct got_object_id *new_staged_blob_id = NULL;
6104 if (status == GOT_STATUS_UNVERSIONED)
6105 return NULL;
6107 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6108 if (ie == NULL)
6109 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6111 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6112 relpath)== -1)
6113 return got_error_from_errno("asprintf");
6115 switch (status) {
6116 case GOT_STATUS_ADD:
6117 case GOT_STATUS_MODIFY:
6118 if (a->patch_cb) {
6119 if (status == GOT_STATUS_ADD) {
6120 int choice = GOT_PATCH_CHOICE_NONE;
6121 err = (*a->patch_cb)(&choice, a->patch_arg,
6122 status, ie->path, NULL, 1, 1);
6123 if (err)
6124 break;
6125 if (choice != GOT_PATCH_CHOICE_YES)
6126 break;
6127 } else {
6128 err = create_patched_content(&path_content, 0,
6129 staged_blob_id ? staged_blob_id : blob_id,
6130 ondisk_path, dirfd, de_name, ie->path,
6131 a->repo, a->patch_cb, a->patch_arg);
6132 if (err || path_content == NULL)
6133 break;
6136 err = got_object_blob_create(&new_staged_blob_id,
6137 path_content ? path_content : ondisk_path, a->repo);
6138 if (err)
6139 break;
6140 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6141 SHA1_DIGEST_LENGTH);
6142 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6143 stage = GOT_FILEIDX_STAGE_ADD;
6144 else
6145 stage = GOT_FILEIDX_STAGE_MODIFY;
6146 got_fileindex_entry_stage_set(ie, stage);
6147 a->staged_something = 1;
6148 if (a->status_cb == NULL)
6149 break;
6150 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6151 get_staged_status(ie), relpath, blob_id,
6152 new_staged_blob_id, NULL, dirfd, de_name);
6153 break;
6154 case GOT_STATUS_DELETE:
6155 if (staged_status == GOT_STATUS_DELETE)
6156 break;
6157 if (a->patch_cb) {
6158 int choice = GOT_PATCH_CHOICE_NONE;
6159 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6160 ie->path, NULL, 1, 1);
6161 if (err)
6162 break;
6163 if (choice == GOT_PATCH_CHOICE_NO)
6164 break;
6165 if (choice != GOT_PATCH_CHOICE_YES) {
6166 err = got_error(GOT_ERR_PATCH_CHOICE);
6167 break;
6170 stage = GOT_FILEIDX_STAGE_DELETE;
6171 got_fileindex_entry_stage_set(ie, stage);
6172 a->staged_something = 1;
6173 if (a->status_cb == NULL)
6174 break;
6175 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6176 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6177 de_name);
6178 break;
6179 case GOT_STATUS_NO_CHANGE:
6180 break;
6181 case GOT_STATUS_CONFLICT:
6182 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6183 break;
6184 case GOT_STATUS_NONEXISTENT:
6185 err = got_error_set_errno(ENOENT, relpath);
6186 break;
6187 default:
6188 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6189 break;
6192 if (path_content && unlink(path_content) == -1 && err == NULL)
6193 err = got_error_from_errno2("unlink", path_content);
6194 free(path_content);
6195 free(ondisk_path);
6196 free(new_staged_blob_id);
6197 return err;
6200 const struct got_error *
6201 got_worktree_stage(struct got_worktree *worktree,
6202 struct got_pathlist_head *paths,
6203 got_worktree_status_cb status_cb, void *status_arg,
6204 got_worktree_patch_cb patch_cb, void *patch_arg,
6205 struct got_repository *repo)
6207 const struct got_error *err = NULL, *sync_err, *unlockerr;
6208 struct got_pathlist_entry *pe;
6209 struct got_fileindex *fileindex = NULL;
6210 char *fileindex_path = NULL;
6211 struct got_reference *head_ref = NULL;
6212 struct got_object_id *head_commit_id = NULL;
6213 struct check_stage_ok_arg oka;
6214 struct stage_path_arg spa;
6216 err = lock_worktree(worktree, LOCK_EX);
6217 if (err)
6218 return err;
6220 err = got_ref_open(&head_ref, repo,
6221 got_worktree_get_head_ref_name(worktree), 0);
6222 if (err)
6223 goto done;
6224 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6225 if (err)
6226 goto done;
6227 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6228 if (err)
6229 goto done;
6231 /* Check pre-conditions before staging anything. */
6232 oka.head_commit_id = head_commit_id;
6233 oka.worktree = worktree;
6234 oka.fileindex = fileindex;
6235 oka.repo = repo;
6236 oka.have_changes = 0;
6237 TAILQ_FOREACH(pe, paths, entry) {
6238 err = worktree_status(worktree, pe->path, fileindex, repo,
6239 check_stage_ok, &oka, NULL, NULL, 0, 0);
6240 if (err)
6241 goto done;
6243 if (!oka.have_changes) {
6244 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6245 goto done;
6248 spa.worktree = worktree;
6249 spa.fileindex = fileindex;
6250 spa.repo = repo;
6251 spa.patch_cb = patch_cb;
6252 spa.patch_arg = patch_arg;
6253 spa.status_cb = status_cb;
6254 spa.status_arg = status_arg;
6255 spa.staged_something = 0;
6256 TAILQ_FOREACH(pe, paths, entry) {
6257 err = worktree_status(worktree, pe->path, fileindex, repo,
6258 stage_path, &spa, NULL, NULL, 0, 0);
6259 if (err)
6260 goto done;
6262 if (!spa.staged_something) {
6263 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6264 goto done;
6267 sync_err = sync_fileindex(fileindex, fileindex_path);
6268 if (sync_err && err == NULL)
6269 err = sync_err;
6270 done:
6271 if (head_ref)
6272 got_ref_close(head_ref);
6273 free(head_commit_id);
6274 free(fileindex_path);
6275 if (fileindex)
6276 got_fileindex_free(fileindex);
6277 unlockerr = lock_worktree(worktree, LOCK_SH);
6278 if (unlockerr && err == NULL)
6279 err = unlockerr;
6280 return err;
6283 struct unstage_path_arg {
6284 struct got_worktree *worktree;
6285 struct got_fileindex *fileindex;
6286 struct got_repository *repo;
6287 got_worktree_checkout_cb progress_cb;
6288 void *progress_arg;
6289 got_worktree_patch_cb patch_cb;
6290 void *patch_arg;
6293 static const struct got_error *
6294 create_unstaged_content(char **path_unstaged_content,
6295 char **path_new_staged_content, struct got_object_id *blob_id,
6296 struct got_object_id *staged_blob_id, const char *relpath,
6297 struct got_repository *repo,
6298 got_worktree_patch_cb patch_cb, void *patch_arg)
6300 const struct got_error *err;
6301 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6302 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6303 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6304 struct stat sb1, sb2;
6305 struct got_diff_changes *changes = NULL;
6306 struct got_diff_state *ds = NULL;
6307 struct got_diff_args *args = NULL;
6308 struct got_diff_change *change;
6309 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6310 int have_content = 0, have_rejected_content = 0;
6312 *path_unstaged_content = NULL;
6313 *path_new_staged_content = NULL;
6315 err = got_object_id_str(&label1, blob_id);
6316 if (err)
6317 return err;
6318 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6319 if (err)
6320 goto done;
6322 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6323 if (err)
6324 goto done;
6326 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6327 if (err)
6328 goto done;
6330 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6331 if (err)
6332 goto done;
6334 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6335 if (err)
6336 goto done;
6338 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6339 if (err)
6340 goto done;
6342 if (stat(path1, &sb1) == -1) {
6343 err = got_error_from_errno2("stat", path1);
6344 goto done;
6347 if (stat(path2, &sb2) == -1) {
6348 err = got_error_from_errno2("stat", path2);
6349 goto done;
6352 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6353 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6354 if (err)
6355 goto done;
6357 err = got_opentemp_named(path_unstaged_content, &outfile,
6358 "got-unstaged-content");
6359 if (err)
6360 goto done;
6361 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6362 "got-new-staged-content");
6363 if (err)
6364 goto done;
6366 if (fseek(f1, 0L, SEEK_SET) == -1) {
6367 err = got_ferror(f1, GOT_ERR_IO);
6368 goto done;
6370 if (fseek(f2, 0L, SEEK_SET) == -1) {
6371 err = got_ferror(f2, GOT_ERR_IO);
6372 goto done;
6374 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6375 int choice;
6376 err = apply_or_reject_change(&choice, change, ++n,
6377 changes->nchanges, ds, args, diff_flags, relpath,
6378 f1, f2, &line_cur1, &line_cur2,
6379 outfile, rejectfile, patch_cb, patch_arg);
6380 if (err)
6381 goto done;
6382 if (choice == GOT_PATCH_CHOICE_YES)
6383 have_content = 1;
6384 else
6385 have_rejected_content = 1;
6386 if (choice == GOT_PATCH_CHOICE_QUIT)
6387 break;
6389 if (have_content || have_rejected_content)
6390 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6391 outfile, rejectfile);
6392 done:
6393 free(label1);
6394 if (blob)
6395 got_object_blob_close(blob);
6396 if (staged_blob)
6397 got_object_blob_close(staged_blob);
6398 if (f1 && fclose(f1) == EOF && err == NULL)
6399 err = got_error_from_errno2("fclose", path1);
6400 if (f2 && fclose(f2) == EOF && err == NULL)
6401 err = got_error_from_errno2("fclose", path2);
6402 if (outfile && fclose(outfile) == EOF && err == NULL)
6403 err = got_error_from_errno2("fclose", *path_unstaged_content);
6404 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6405 err = got_error_from_errno2("fclose", *path_new_staged_content);
6406 if (path1 && unlink(path1) == -1 && err == NULL)
6407 err = got_error_from_errno2("unlink", path1);
6408 if (path2 && unlink(path2) == -1 && err == NULL)
6409 err = got_error_from_errno2("unlink", path2);
6410 if (err || !have_content) {
6411 if (*path_unstaged_content &&
6412 unlink(*path_unstaged_content) == -1 && err == NULL)
6413 err = got_error_from_errno2("unlink",
6414 *path_unstaged_content);
6415 free(*path_unstaged_content);
6416 *path_unstaged_content = NULL;
6418 if (err || !have_rejected_content) {
6419 if (*path_new_staged_content &&
6420 unlink(*path_new_staged_content) == -1 && err == NULL)
6421 err = got_error_from_errno2("unlink",
6422 *path_new_staged_content);
6423 free(*path_new_staged_content);
6424 *path_new_staged_content = NULL;
6426 free(args);
6427 if (ds) {
6428 got_diff_state_free(ds);
6429 free(ds);
6431 if (changes)
6432 got_diff_free_changes(changes);
6433 free(path1);
6434 free(path2);
6435 return err;
6438 static const struct got_error *
6439 unstage_path(void *arg, unsigned char status,
6440 unsigned char staged_status, const char *relpath,
6441 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6442 struct got_object_id *commit_id, int dirfd, const char *de_name)
6444 const struct got_error *err = NULL;
6445 struct unstage_path_arg *a = arg;
6446 struct got_fileindex_entry *ie;
6447 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6448 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6449 char *path_new_staged_content = NULL;
6450 char *id_str = NULL, *label_orig = NULL;
6451 int local_changes_subsumed;
6452 struct stat sb;
6454 if (staged_status != GOT_STATUS_ADD &&
6455 staged_status != GOT_STATUS_MODIFY &&
6456 staged_status != GOT_STATUS_DELETE)
6457 return NULL;
6459 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6460 if (ie == NULL)
6461 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6463 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6464 == -1)
6465 return got_error_from_errno("asprintf");
6467 err = got_object_id_str(&id_str,
6468 commit_id ? commit_id : a->worktree->base_commit_id);
6469 if (err)
6470 goto done;
6471 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6472 id_str) == -1) {
6473 err = got_error_from_errno("asprintf");
6474 goto done;
6477 switch (staged_status) {
6478 case GOT_STATUS_MODIFY:
6479 err = got_object_open_as_blob(&blob_base, a->repo,
6480 blob_id, 8192);
6481 if (err)
6482 break;
6483 /* fall through */
6484 case GOT_STATUS_ADD:
6485 if (a->patch_cb) {
6486 if (staged_status == GOT_STATUS_ADD) {
6487 int choice = GOT_PATCH_CHOICE_NONE;
6488 err = (*a->patch_cb)(&choice, a->patch_arg,
6489 staged_status, ie->path, NULL, 1, 1);
6490 if (err)
6491 break;
6492 if (choice != GOT_PATCH_CHOICE_YES)
6493 break;
6494 } else {
6495 err = create_unstaged_content(
6496 &path_unstaged_content,
6497 &path_new_staged_content, blob_id,
6498 staged_blob_id, ie->path, a->repo,
6499 a->patch_cb, a->patch_arg);
6500 if (err || path_unstaged_content == NULL)
6501 break;
6502 if (path_new_staged_content) {
6503 err = got_object_blob_create(
6504 &staged_blob_id,
6505 path_new_staged_content,
6506 a->repo);
6507 if (err)
6508 break;
6509 memcpy(ie->staged_blob_sha1,
6510 staged_blob_id->sha1,
6511 SHA1_DIGEST_LENGTH);
6513 err = merge_file(&local_changes_subsumed,
6514 a->worktree, blob_base, ondisk_path,
6515 relpath, got_fileindex_perms_to_st(ie),
6516 path_unstaged_content, label_orig,
6517 "unstaged", a->repo, a->progress_cb,
6518 a->progress_arg);
6519 if (err == NULL &&
6520 path_new_staged_content == NULL)
6521 got_fileindex_entry_stage_set(ie,
6522 GOT_FILEIDX_STAGE_NONE);
6523 break; /* Done with this file. */
6526 err = got_object_open_as_blob(&blob_staged, a->repo,
6527 staged_blob_id, 8192);
6528 if (err)
6529 break;
6530 err = merge_blob(&local_changes_subsumed, a->worktree,
6531 blob_base, ondisk_path, relpath,
6532 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6533 commit_id ? commit_id : a->worktree->base_commit_id,
6534 a->repo, a->progress_cb, a->progress_arg);
6535 if (err == NULL)
6536 got_fileindex_entry_stage_set(ie,
6537 GOT_FILEIDX_STAGE_NONE);
6538 break;
6539 case GOT_STATUS_DELETE:
6540 if (a->patch_cb) {
6541 int choice = GOT_PATCH_CHOICE_NONE;
6542 err = (*a->patch_cb)(&choice, a->patch_arg,
6543 staged_status, ie->path, NULL, 1, 1);
6544 if (err)
6545 break;
6546 if (choice == GOT_PATCH_CHOICE_NO)
6547 break;
6548 if (choice != GOT_PATCH_CHOICE_YES) {
6549 err = got_error(GOT_ERR_PATCH_CHOICE);
6550 break;
6553 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6554 err = get_file_status(&status, &sb, ie, ondisk_path,
6555 dirfd, de_name, a->repo);
6556 if (err)
6557 break;
6558 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6559 break;
6561 done:
6562 free(ondisk_path);
6563 if (path_unstaged_content &&
6564 unlink(path_unstaged_content) == -1 && err == NULL)
6565 err = got_error_from_errno2("unlink", path_unstaged_content);
6566 if (path_new_staged_content &&
6567 unlink(path_new_staged_content) == -1 && err == NULL)
6568 err = got_error_from_errno2("unlink", path_new_staged_content);
6569 free(path_unstaged_content);
6570 free(path_new_staged_content);
6571 if (blob_base)
6572 got_object_blob_close(blob_base);
6573 if (blob_staged)
6574 got_object_blob_close(blob_staged);
6575 free(id_str);
6576 free(label_orig);
6577 return err;
6580 const struct got_error *
6581 got_worktree_unstage(struct got_worktree *worktree,
6582 struct got_pathlist_head *paths,
6583 got_worktree_checkout_cb progress_cb, void *progress_arg,
6584 got_worktree_patch_cb patch_cb, void *patch_arg,
6585 struct got_repository *repo)
6587 const struct got_error *err = NULL, *sync_err, *unlockerr;
6588 struct got_pathlist_entry *pe;
6589 struct got_fileindex *fileindex = NULL;
6590 char *fileindex_path = NULL;
6591 struct unstage_path_arg upa;
6593 err = lock_worktree(worktree, LOCK_EX);
6594 if (err)
6595 return err;
6597 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6598 if (err)
6599 goto done;
6601 upa.worktree = worktree;
6602 upa.fileindex = fileindex;
6603 upa.repo = repo;
6604 upa.progress_cb = progress_cb;
6605 upa.progress_arg = progress_arg;
6606 upa.patch_cb = patch_cb;
6607 upa.patch_arg = patch_arg;
6608 TAILQ_FOREACH(pe, paths, entry) {
6609 err = worktree_status(worktree, pe->path, fileindex, repo,
6610 unstage_path, &upa, NULL, NULL, 0, 0);
6611 if (err)
6612 goto done;
6615 sync_err = sync_fileindex(fileindex, fileindex_path);
6616 if (sync_err && err == NULL)
6617 err = sync_err;
6618 done:
6619 free(fileindex_path);
6620 if (fileindex)
6621 got_fileindex_free(fileindex);
6622 unlockerr = lock_worktree(worktree, LOCK_SH);
6623 if (unlockerr && err == NULL)
6624 err = unlockerr;
6625 return err;