Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 /*
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
837 */
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
854 if (parent == NULL)
855 return got_error_from_errno2("dirname", ondisk_path);
857 free(base_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
860 base_path = NULL;
861 goto done;
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 if (err)
866 goto done;
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 blob_deriv);
869 if (err)
870 goto done;
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (err)
874 goto done;
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
878 goto done;
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
884 done:
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
887 free(base_path);
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
892 free(id_str);
893 free(label_deriv);
894 return err;
897 static const struct got_error *
898 update_blob_fileindex_entry(struct got_worktree *worktree,
899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 int update_timestamps)
903 const struct got_error *err = NULL;
905 if (ie == NULL)
906 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
907 if (ie)
908 err = got_fileindex_entry_update(ie, ondisk_path,
909 blob->id.sha1, worktree->base_commit_id->sha1,
910 update_timestamps);
911 else {
912 struct got_fileindex_entry *new_ie;
913 err = got_fileindex_entry_alloc(&new_ie, path);
914 if (err)
915 return err;
916 err = got_fileindex_entry_update(new_ie, ondisk_path,
917 blob->id.sha1, worktree->base_commit_id->sha1, 1);
918 if (err) {
919 got_fileindex_entry_free(new_ie);
920 return err;
922 err = got_fileindex_entry_add(fileindex, new_ie);
923 if (err)
924 got_fileindex_entry_free(new_ie);
926 return err;
929 static mode_t
930 get_ondisk_perms(int executable, mode_t st_mode)
932 mode_t xbits = S_IXUSR;
934 if (executable) {
935 /* Map read bits to execute bits. */
936 if (st_mode & S_IRGRP)
937 xbits |= S_IXGRP;
938 if (st_mode & S_IROTH)
939 xbits |= S_IXOTH;
940 return st_mode | xbits;
943 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
946 static const struct got_error *
947 install_blob(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
954 int fd = -1;
955 size_t len, hdrlen;
956 int update = 0;
957 char *tmppath = NULL;
959 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
960 GOT_DEFAULT_FILE_MODE);
961 if (fd == -1) {
962 if (errno == ENOENT) {
963 char *parent = dirname(path);
964 if (parent == NULL)
965 return got_error_from_errno2("dirname", path);
966 err = add_dir_on_disk(worktree, parent);
967 if (err)
968 return err;
969 fd = open(ondisk_path,
970 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
971 GOT_DEFAULT_FILE_MODE);
972 if (fd == -1)
973 return got_error_from_errno2("open",
974 ondisk_path);
975 } else if (errno == EEXIST) {
976 if (!S_ISREG(st_mode)) {
977 /* TODO file is obstructed; do something */
978 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
979 goto done;
980 } else {
981 err = got_opentemp_named_fd(&tmppath, &fd,
982 ondisk_path);
983 if (err)
984 goto done;
985 update = 1;
987 } else
988 return got_error_from_errno2("open", ondisk_path);
991 if (restoring_missing_file)
992 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
993 else if (reverting_versioned_file)
994 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
995 else
996 err = (*progress_cb)(progress_arg,
997 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
998 if (err)
999 goto done;
1001 hdrlen = got_object_blob_get_hdrlen(blob);
1002 do {
1003 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1004 err = got_object_blob_read_block(&len, blob);
1005 if (err)
1006 break;
1007 if (len > 0) {
1008 /* Skip blob object header first time around. */
1009 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1010 if (outlen == -1) {
1011 err = got_error_from_errno("write");
1012 goto done;
1013 } else if (outlen != len - hdrlen) {
1014 err = got_error(GOT_ERR_IO);
1015 goto done;
1017 hdrlen = 0;
1019 } while (len != 0);
1021 if (fsync(fd) != 0) {
1022 err = got_error_from_errno("fsync");
1023 goto done;
1026 if (update) {
1027 if (rename(tmppath, ondisk_path) != 0) {
1028 err = got_error_from_errno3("rename", tmppath,
1029 ondisk_path);
1030 unlink(tmppath);
1031 goto done;
1035 if (chmod(ondisk_path,
1036 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1037 err = got_error_from_errno2("chmod", ondisk_path);
1038 goto done;
1041 done:
1042 if (fd != -1 && close(fd) != 0 && err == NULL)
1043 err = got_error_from_errno("close");
1044 free(tmppath);
1045 return err;
1048 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1049 static const struct got_error *
1050 get_modified_file_content_status(unsigned char *status, FILE *f)
1052 const struct got_error *err = NULL;
1053 const char *markers[3] = {
1054 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1055 GOT_DIFF_CONFLICT_MARKER_SEP,
1056 GOT_DIFF_CONFLICT_MARKER_END
1058 int i = 0;
1059 char *line;
1060 size_t len;
1061 const char delim[3] = {'\0', '\0', '\0'};
1063 while (*status == GOT_STATUS_MODIFY) {
1064 line = fparseln(f, &len, NULL, delim, 0);
1065 if (line == NULL) {
1066 if (feof(f))
1067 break;
1068 err = got_ferror(f, GOT_ERR_IO);
1069 break;
1072 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1073 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1074 == 0)
1075 *status = GOT_STATUS_CONFLICT;
1076 else
1077 i++;
1081 return err;
1084 static int
1085 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1087 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1088 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1091 static int
1092 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1094 return !(ie->ctime_sec == sb->st_ctime &&
1095 ie->ctime_nsec == sb->st_ctimensec &&
1096 ie->mtime_sec == sb->st_mtime &&
1097 ie->mtime_nsec == sb->st_mtimensec &&
1098 ie->size == (sb->st_size & 0xffffffff) &&
1099 !xbit_differs(ie, sb->st_mode));
1102 static unsigned char
1103 get_staged_status(struct got_fileindex_entry *ie)
1105 switch (got_fileindex_entry_stage_get(ie)) {
1106 case GOT_FILEIDX_STAGE_ADD:
1107 return GOT_STATUS_ADD;
1108 case GOT_FILEIDX_STAGE_DELETE:
1109 return GOT_STATUS_DELETE;
1110 case GOT_FILEIDX_STAGE_MODIFY:
1111 return GOT_STATUS_MODIFY;
1112 default:
1113 return GOT_STATUS_NO_CHANGE;
1117 static const struct got_error *
1118 get_file_status(unsigned char *status, struct stat *sb,
1119 struct got_fileindex_entry *ie, const char *abspath,
1120 int dirfd, const char *de_name, struct got_repository *repo)
1122 const struct got_error *err = NULL;
1123 struct got_object_id id;
1124 size_t hdrlen;
1125 int fd = -1;
1126 FILE *f = NULL;
1127 uint8_t fbuf[8192];
1128 struct got_blob_object *blob = NULL;
1129 size_t flen, blen;
1130 unsigned char staged_status = get_staged_status(ie);
1132 *status = GOT_STATUS_NO_CHANGE;
1135 * Whenever the caller provides a directory descriptor and a
1136 * directory entry name for the file, use them! This prevents
1137 * race conditions if filesystem paths change beneath our feet.
1139 if (dirfd != -1) {
1140 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1141 if (errno == ENOENT) {
1142 if (got_fileindex_entry_has_file_on_disk(ie))
1143 *status = GOT_STATUS_MISSING;
1144 else
1145 *status = GOT_STATUS_DELETE;
1146 goto done;
1148 err = got_error_from_errno2("fstatat", abspath);
1149 goto done;
1151 } else {
1152 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1153 if (fd == -1 && errno != ENOENT)
1154 return got_error_from_errno2("open", abspath);
1155 if (fd == -1 || fstat(fd, sb) == -1) {
1156 if (errno == ENOENT) {
1157 if (got_fileindex_entry_has_file_on_disk(ie))
1158 *status = GOT_STATUS_MISSING;
1159 else
1160 *status = GOT_STATUS_DELETE;
1161 goto done;
1163 err = got_error_from_errno2("fstat", abspath);
1164 goto done;
1168 if (!S_ISREG(sb->st_mode)) {
1169 *status = GOT_STATUS_OBSTRUCTED;
1170 goto done;
1173 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1174 *status = GOT_STATUS_DELETE;
1175 goto done;
1176 } else if (!got_fileindex_entry_has_blob(ie) &&
1177 staged_status != GOT_STATUS_ADD) {
1178 *status = GOT_STATUS_ADD;
1179 goto done;
1182 if (!stat_info_differs(ie, sb))
1183 goto done;
1185 if (staged_status == GOT_STATUS_MODIFY ||
1186 staged_status == GOT_STATUS_ADD)
1187 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1188 else
1189 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1191 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1192 if (err)
1193 goto done;
1195 if (dirfd != -1) {
1196 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1197 if (fd == -1) {
1198 err = got_error_from_errno2("openat", abspath);
1199 goto done;
1203 f = fdopen(fd, "r");
1204 if (f == NULL) {
1205 err = got_error_from_errno2("fdopen", abspath);
1206 goto done;
1208 fd = -1;
1209 hdrlen = got_object_blob_get_hdrlen(blob);
1210 for (;;) {
1211 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1212 err = got_object_blob_read_block(&blen, blob);
1213 if (err)
1214 goto done;
1215 /* Skip length of blob object header first time around. */
1216 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1217 if (flen == 0 && ferror(f)) {
1218 err = got_error_from_errno("fread");
1219 goto done;
1221 if (blen == 0) {
1222 if (flen != 0)
1223 *status = GOT_STATUS_MODIFY;
1224 break;
1225 } else if (flen == 0) {
1226 if (blen != 0)
1227 *status = GOT_STATUS_MODIFY;
1228 break;
1229 } else if (blen - hdrlen == flen) {
1230 /* Skip blob object header first time around. */
1231 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1232 *status = GOT_STATUS_MODIFY;
1233 break;
1235 } else {
1236 *status = GOT_STATUS_MODIFY;
1237 break;
1239 hdrlen = 0;
1242 if (*status == GOT_STATUS_MODIFY) {
1243 rewind(f);
1244 err = get_modified_file_content_status(status, f);
1245 } else if (xbit_differs(ie, sb->st_mode))
1246 *status = GOT_STATUS_MODE_CHANGE;
1247 done:
1248 if (blob)
1249 got_object_blob_close(blob);
1250 if (f != NULL && fclose(f) == EOF && err == NULL)
1251 err = got_error_from_errno2("fclose", abspath);
1252 if (fd != -1 && close(fd) == -1 && err == NULL)
1253 err = got_error_from_errno2("close", abspath);
1254 return err;
1258 * Update timestamps in the file index if a file is unmodified and
1259 * we had to run a full content comparison to find out.
1261 static const struct got_error *
1262 sync_timestamps(char *ondisk_path, unsigned char status,
1263 struct got_fileindex_entry *ie, struct stat *sb)
1265 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1266 return got_fileindex_entry_update(ie, ondisk_path,
1267 ie->blob_sha1, ie->commit_sha1, 1);
1269 return NULL;
1272 static const struct got_error *
1273 update_blob(struct got_worktree *worktree,
1274 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1275 struct got_tree_entry *te, const char *path,
1276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1277 void *progress_arg)
1279 const struct got_error *err = NULL;
1280 struct got_blob_object *blob = NULL;
1281 char *ondisk_path;
1282 unsigned char status = GOT_STATUS_NO_CHANGE;
1283 struct stat sb;
1285 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1286 return got_error_from_errno("asprintf");
1288 if (ie) {
1289 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1290 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1291 goto done;
1293 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1294 repo);
1295 if (err)
1296 goto done;
1297 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1298 sb.st_mode = got_fileindex_perms_to_st(ie);
1299 } else
1300 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1302 if (status == GOT_STATUS_OBSTRUCTED) {
1303 err = (*progress_cb)(progress_arg, status, path);
1304 goto done;
1306 if (status == GOT_STATUS_CONFLICT) {
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1308 path);
1309 goto done;
1312 if (ie && status != GOT_STATUS_MISSING &&
1313 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1314 if (got_fileindex_entry_has_commit(ie) &&
1315 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1316 SHA1_DIGEST_LENGTH) == 0) {
1317 err = sync_timestamps(ondisk_path, status, ie, &sb);
1318 if (err)
1319 goto done;
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1321 path);
1322 goto done;
1324 if (got_fileindex_entry_has_blob(ie) &&
1325 memcmp(ie->blob_sha1, te->id.sha1,
1326 SHA1_DIGEST_LENGTH) == 0) {
1327 err = sync_timestamps(ondisk_path, status, ie, &sb);
1328 goto done;
1332 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1333 if (err)
1334 goto done;
1336 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1337 int update_timestamps;
1338 struct got_blob_object *blob2 = NULL;
1339 char *label_orig = NULL;
1340 if (got_fileindex_entry_has_blob(ie)) {
1341 struct got_object_id id2;
1342 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1343 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1344 if (err)
1345 goto done;
1347 if (got_fileindex_entry_has_commit(ie)) {
1348 char id_str[SHA1_DIGEST_STRING_LENGTH];
1349 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1350 sizeof(id_str)) == NULL) {
1351 err = got_error_path(id_str,
1352 GOT_ERR_BAD_OBJ_ID_STR);
1353 goto done;
1355 if (asprintf(&label_orig, "%s: commit %s",
1356 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1357 err = got_error_from_errno("asprintf");
1358 goto done;
1361 err = merge_blob(&update_timestamps, worktree, blob2,
1362 ondisk_path, path, sb.st_mode, label_orig, blob,
1363 worktree->base_commit_id, repo,
1364 progress_cb, progress_arg);
1365 free(label_orig);
1366 if (blob2)
1367 got_object_blob_close(blob2);
1368 if (err)
1369 goto done;
1371 * Do not update timestamps of files with local changes.
1372 * Otherwise, a future status walk would treat them as
1373 * unmodified files again.
1375 err = got_fileindex_entry_update(ie, ondisk_path,
1376 blob->id.sha1, worktree->base_commit_id->sha1,
1377 update_timestamps);
1378 } else if (status == GOT_STATUS_MODE_CHANGE) {
1379 err = got_fileindex_entry_update(ie, ondisk_path,
1380 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1381 } else if (status == GOT_STATUS_DELETE) {
1382 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1383 if (err)
1384 goto done;
1385 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1386 ondisk_path, path, blob, 0);
1387 if (err)
1388 goto done;
1389 } else {
1390 err = install_blob(worktree, ondisk_path, path, te->mode,
1391 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1392 repo, progress_cb, progress_arg);
1393 if (err)
1394 goto done;
1395 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1396 ondisk_path, path, blob, 1);
1397 if (err)
1398 goto done;
1400 got_object_blob_close(blob);
1401 done:
1402 free(ondisk_path);
1403 return err;
1406 static const struct got_error *
1407 remove_ondisk_file(const char *root_path, const char *path)
1409 const struct got_error *err = NULL;
1410 char *ondisk_path = NULL;
1412 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1413 return got_error_from_errno("asprintf");
1415 if (unlink(ondisk_path) == -1) {
1416 if (errno != ENOENT)
1417 err = got_error_from_errno2("unlink", ondisk_path);
1418 } else {
1419 char *parent = dirname(ondisk_path);
1420 while (parent && strcmp(parent, root_path) != 0) {
1421 if (rmdir(parent) == -1) {
1422 if (errno != ENOTEMPTY)
1423 err = got_error_from_errno2("rmdir",
1424 parent);
1425 break;
1427 parent = dirname(parent);
1430 free(ondisk_path);
1431 return err;
1434 static const struct got_error *
1435 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1436 struct got_fileindex_entry *ie, struct got_repository *repo,
1437 got_worktree_checkout_cb progress_cb, void *progress_arg)
1439 const struct got_error *err = NULL;
1440 unsigned char status;
1441 struct stat sb;
1442 char *ondisk_path;
1444 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1445 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1447 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1448 == -1)
1449 return got_error_from_errno("asprintf");
1451 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1452 if (err)
1453 goto done;
1455 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1456 status == GOT_STATUS_ADD) {
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1458 if (err)
1459 goto done;
1461 * Preserve the working file and change the deleted blob's
1462 * entry into a schedule-add entry.
1464 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1465 0);
1466 } else {
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1468 if (err)
1469 goto done;
1470 if (status == GOT_STATUS_NO_CHANGE) {
1471 err = remove_ondisk_file(worktree->root_path, ie->path);
1472 if (err)
1473 goto done;
1475 got_fileindex_entry_remove(fileindex, ie);
1477 done:
1478 free(ondisk_path);
1479 return err;
1482 struct diff_cb_arg {
1483 struct got_fileindex *fileindex;
1484 struct got_worktree *worktree;
1485 struct got_repository *repo;
1486 got_worktree_checkout_cb progress_cb;
1487 void *progress_arg;
1488 got_cancel_cb cancel_cb;
1489 void *cancel_arg;
1492 static const struct got_error *
1493 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1494 struct got_tree_entry *te, const char *parent_path)
1496 struct diff_cb_arg *a = arg;
1498 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 return got_error(GOT_ERR_CANCELLED);
1501 return update_blob(a->worktree, a->fileindex, ie, te,
1502 ie->path, a->repo, a->progress_cb, a->progress_arg);
1505 static const struct got_error *
1506 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1508 struct diff_cb_arg *a = arg;
1510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1511 return got_error(GOT_ERR_CANCELLED);
1513 return delete_blob(a->worktree, a->fileindex, ie,
1514 a->repo, a->progress_cb, a->progress_arg);
1517 static const struct got_error *
1518 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1520 struct diff_cb_arg *a = arg;
1521 const struct got_error *err;
1522 char *path;
1524 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1525 return got_error(GOT_ERR_CANCELLED);
1527 if (got_object_tree_entry_is_submodule(te))
1528 return NULL;
1530 if (asprintf(&path, "%s%s%s", parent_path,
1531 parent_path[0] ? "/" : "", te->name)
1532 == -1)
1533 return got_error_from_errno("asprintf");
1535 if (S_ISDIR(te->mode))
1536 err = add_dir_on_disk(a->worktree, path);
1537 else
1538 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1539 a->repo, a->progress_cb, a->progress_arg);
1541 free(path);
1542 return err;
1545 static const struct got_error *
1546 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1548 const struct got_error *err = NULL;
1549 char *uuidstr = NULL;
1550 uint32_t uuid_status;
1552 *refname = NULL;
1554 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1555 if (uuid_status != uuid_s_ok)
1556 return got_error_uuid(uuid_status, "uuid_to_string");
1558 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1559 == -1) {
1560 err = got_error_from_errno("asprintf");
1561 *refname = NULL;
1563 free(uuidstr);
1564 return err;
1567 const struct got_error *
1568 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1570 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1573 static const struct got_error *
1574 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1576 return get_ref_name(refname, worktree,
1577 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1580 static const struct got_error *
1581 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1583 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1586 static const struct got_error *
1587 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1589 return get_ref_name(refname, worktree,
1590 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1593 static const struct got_error *
1594 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1596 return get_ref_name(refname, worktree,
1597 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1600 static const struct got_error *
1601 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1603 return get_ref_name(refname, worktree,
1604 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1607 static const struct got_error *
1608 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1610 return get_ref_name(refname, worktree,
1611 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1614 static const struct got_error *
1615 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1617 return get_ref_name(refname, worktree,
1618 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1621 static const struct got_error *
1622 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1624 return get_ref_name(refname, worktree,
1625 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1628 const struct got_error *
1629 got_worktree_get_histedit_script_path(char **path,
1630 struct got_worktree *worktree)
1632 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1633 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1634 *path = NULL;
1635 return got_error_from_errno("asprintf");
1637 return NULL;
1641 * Prevent Git's garbage collector from deleting our base commit by
1642 * setting a reference to our base commit's ID.
1644 static const struct got_error *
1645 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 struct got_reference *ref = NULL;
1649 char *refname;
1651 err = got_worktree_get_base_ref_name(&refname, worktree);
1652 if (err)
1653 return err;
1655 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1656 if (err)
1657 goto done;
1659 err = got_ref_write(ref, repo);
1660 done:
1661 free(refname);
1662 if (ref)
1663 got_ref_close(ref);
1664 return err;
1667 static const struct got_error *
1668 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1670 const struct got_error *err = NULL;
1672 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1673 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 *fileindex_path = NULL;
1677 return err;
1681 static const struct got_error *
1682 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1683 struct got_worktree *worktree)
1685 const struct got_error *err = NULL;
1686 FILE *index = NULL;
1688 *fileindex_path = NULL;
1689 *fileindex = got_fileindex_alloc();
1690 if (*fileindex == NULL)
1691 return got_error_from_errno("got_fileindex_alloc");
1693 err = get_fileindex_path(fileindex_path, worktree);
1694 if (err)
1695 goto done;
1697 index = fopen(*fileindex_path, "rb");
1698 if (index == NULL) {
1699 if (errno != ENOENT)
1700 err = got_error_from_errno2("fopen", *fileindex_path);
1701 } else {
1702 err = got_fileindex_read(*fileindex, index);
1703 if (fclose(index) != 0 && err == NULL)
1704 err = got_error_from_errno("fclose");
1706 done:
1707 if (err) {
1708 free(*fileindex_path);
1709 *fileindex_path = NULL;
1710 got_fileindex_free(*fileindex);
1711 *fileindex = NULL;
1713 return err;
1716 struct bump_base_commit_id_arg {
1717 struct got_object_id *base_commit_id;
1718 const char *path;
1719 size_t path_len;
1720 const char *entry_name;
1721 got_worktree_checkout_cb progress_cb;
1722 void *progress_arg;
1725 /* Bump base commit ID of all files within an updated part of the work tree. */
1726 static const struct got_error *
1727 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1729 const struct got_error *err;
1730 struct bump_base_commit_id_arg *a = arg;
1732 if (a->entry_name) {
1733 if (strcmp(ie->path, a->path) != 0)
1734 return NULL;
1735 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1736 return NULL;
1738 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1739 SHA1_DIGEST_LENGTH) == 0)
1740 return NULL;
1742 if (a->progress_cb) {
1743 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1744 ie->path);
1745 if (err)
1746 return err;
1748 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1749 return NULL;
1752 static const struct got_error *
1753 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1755 const struct got_error *err = NULL;
1756 char *new_fileindex_path = NULL;
1757 FILE *new_index = NULL;
1758 struct timespec timeout;
1760 err = got_opentemp_named(&new_fileindex_path, &new_index,
1761 fileindex_path);
1762 if (err)
1763 goto done;
1765 err = got_fileindex_write(fileindex, new_index);
1766 if (err)
1767 goto done;
1769 if (rename(new_fileindex_path, fileindex_path) != 0) {
1770 err = got_error_from_errno3("rename", new_fileindex_path,
1771 fileindex_path);
1772 unlink(new_fileindex_path);
1776 * Sleep for a short amount of time to ensure that files modified after
1777 * this program exits have a different time stamp from the one which
1778 * was recorded in the file index.
1780 timeout.tv_sec = 0;
1781 timeout.tv_nsec = 1;
1782 nanosleep(&timeout, NULL);
1783 done:
1784 if (new_index)
1785 fclose(new_index);
1786 free(new_fileindex_path);
1787 return err;
1790 static const struct got_error *
1791 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1792 struct got_object_id **tree_id, const char *wt_relpath,
1793 struct got_worktree *worktree, struct got_repository *repo)
1795 const struct got_error *err = NULL;
1796 struct got_object_id *id = NULL;
1797 char *in_repo_path = NULL;
1798 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1800 *entry_type = GOT_OBJ_TYPE_ANY;
1801 *tree_relpath = NULL;
1802 *tree_id = NULL;
1804 if (wt_relpath[0] == '\0') {
1805 /* Check out all files within the work tree. */
1806 *entry_type = GOT_OBJ_TYPE_TREE;
1807 *tree_relpath = strdup("");
1808 if (*tree_relpath == NULL) {
1809 err = got_error_from_errno("strdup");
1810 goto done;
1812 err = got_object_id_by_path(tree_id, repo,
1813 worktree->base_commit_id, worktree->path_prefix);
1814 if (err)
1815 goto done;
1816 return NULL;
1819 /* Check out a subset of files in the work tree. */
1821 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1822 is_root_wt ? "" : "/", wt_relpath) == -1) {
1823 err = got_error_from_errno("asprintf");
1824 goto done;
1827 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1828 in_repo_path);
1829 if (err)
1830 goto done;
1832 free(in_repo_path);
1833 in_repo_path = NULL;
1835 err = got_object_get_type(entry_type, repo, id);
1836 if (err)
1837 goto done;
1839 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1840 /* Check out a single file. */
1841 if (strchr(wt_relpath, '/') == NULL) {
1842 /* Check out a single file in work tree's root dir. */
1843 in_repo_path = strdup(worktree->path_prefix);
1844 if (in_repo_path == NULL) {
1845 err = got_error_from_errno("strdup");
1846 goto done;
1848 *tree_relpath = strdup("");
1849 if (*tree_relpath == NULL) {
1850 err = got_error_from_errno("strdup");
1851 goto done;
1853 } else {
1854 /* Check out a single file in a subdirectory. */
1855 err = got_path_dirname(tree_relpath, wt_relpath);
1856 if (err)
1857 return err;
1858 if (asprintf(&in_repo_path, "%s%s%s",
1859 worktree->path_prefix, is_root_wt ? "" : "/",
1860 *tree_relpath) == -1) {
1861 err = got_error_from_errno("asprintf");
1862 goto done;
1865 err = got_object_id_by_path(tree_id, repo,
1866 worktree->base_commit_id, in_repo_path);
1867 } else {
1868 /* Check out all files within a subdirectory. */
1869 *tree_id = got_object_id_dup(id);
1870 if (*tree_id == NULL) {
1871 err = got_error_from_errno("got_object_id_dup");
1872 goto done;
1874 *tree_relpath = strdup(wt_relpath);
1875 if (*tree_relpath == NULL) {
1876 err = got_error_from_errno("strdup");
1877 goto done;
1880 done:
1881 free(id);
1882 free(in_repo_path);
1883 if (err) {
1884 *entry_type = GOT_OBJ_TYPE_ANY;
1885 free(*tree_relpath);
1886 *tree_relpath = NULL;
1887 free(*tree_id);
1888 *tree_id = NULL;
1890 return err;
1893 static const struct got_error *
1894 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1895 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1896 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1897 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1899 const struct got_error *err = NULL;
1900 struct got_commit_object *commit = NULL;
1901 struct got_tree_object *tree = NULL;
1902 struct got_fileindex_diff_tree_cb diff_cb;
1903 struct diff_cb_arg arg;
1905 err = ref_base_commit(worktree, repo);
1906 if (err) {
1907 if (!(err->code == GOT_ERR_ERRNO &&
1908 (errno == EACCES || errno == EROFS)))
1909 goto done;
1910 err = (*progress_cb)(progress_arg,
1911 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1912 if (err)
1913 return err;
1916 err = got_object_open_as_commit(&commit, repo,
1917 worktree->base_commit_id);
1918 if (err)
1919 goto done;
1921 err = got_object_open_as_tree(&tree, repo, tree_id);
1922 if (err)
1923 goto done;
1925 if (entry_name &&
1926 got_object_tree_find_entry(tree, entry_name) == NULL) {
1927 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1928 goto done;
1931 diff_cb.diff_old_new = diff_old_new;
1932 diff_cb.diff_old = diff_old;
1933 diff_cb.diff_new = diff_new;
1934 arg.fileindex = fileindex;
1935 arg.worktree = worktree;
1936 arg.repo = repo;
1937 arg.progress_cb = progress_cb;
1938 arg.progress_arg = progress_arg;
1939 arg.cancel_cb = cancel_cb;
1940 arg.cancel_arg = cancel_arg;
1941 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1942 entry_name, repo, &diff_cb, &arg);
1943 done:
1944 if (tree)
1945 got_object_tree_close(tree);
1946 if (commit)
1947 got_object_commit_close(commit);
1948 return err;
1951 const struct got_error *
1952 got_worktree_checkout_files(struct got_worktree *worktree,
1953 struct got_pathlist_head *paths, struct got_repository *repo,
1954 got_worktree_checkout_cb progress_cb, void *progress_arg,
1955 got_cancel_cb cancel_cb, void *cancel_arg)
1957 const struct got_error *err = NULL, *sync_err, *unlockerr;
1958 struct got_commit_object *commit = NULL;
1959 struct got_tree_object *tree = NULL;
1960 struct got_fileindex *fileindex = NULL;
1961 char *fileindex_path = NULL;
1962 struct got_pathlist_entry *pe;
1963 struct tree_path_data {
1964 SIMPLEQ_ENTRY(tree_path_data) entry;
1965 struct got_object_id *tree_id;
1966 int entry_type;
1967 char *relpath;
1968 char *entry_name;
1969 } *tpd = NULL;
1970 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1972 SIMPLEQ_INIT(&tree_paths);
1974 err = lock_worktree(worktree, LOCK_EX);
1975 if (err)
1976 return err;
1978 /* Map all specified paths to in-repository trees. */
1979 TAILQ_FOREACH(pe, paths, entry) {
1980 tpd = malloc(sizeof(*tpd));
1981 if (tpd == NULL) {
1982 err = got_error_from_errno("malloc");
1983 goto done;
1986 err = find_tree_entry_for_checkout(&tpd->entry_type,
1987 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1988 if (err) {
1989 free(tpd);
1990 goto done;
1993 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1994 err = got_path_basename(&tpd->entry_name, pe->path);
1995 if (err) {
1996 free(tpd->relpath);
1997 free(tpd->tree_id);
1998 free(tpd);
1999 goto done;
2001 } else
2002 tpd->entry_name = NULL;
2004 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2008 * Read the file index.
2009 * Checking out files is supposed to be an idempotent operation.
2010 * If the on-disk file index is incomplete we will try to complete it.
2012 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2013 if (err)
2014 goto done;
2016 tpd = SIMPLEQ_FIRST(&tree_paths);
2017 TAILQ_FOREACH(pe, paths, entry) {
2018 struct bump_base_commit_id_arg bbc_arg;
2020 err = checkout_files(worktree, fileindex, tpd->relpath,
2021 tpd->tree_id, tpd->entry_name, repo,
2022 progress_cb, progress_arg, cancel_cb, cancel_arg);
2023 if (err)
2024 break;
2026 bbc_arg.base_commit_id = worktree->base_commit_id;
2027 bbc_arg.entry_name = tpd->entry_name;
2028 bbc_arg.path = pe->path;
2029 bbc_arg.path_len = pe->path_len;
2030 bbc_arg.progress_cb = progress_cb;
2031 bbc_arg.progress_arg = progress_arg;
2032 err = got_fileindex_for_each_entry_safe(fileindex,
2033 bump_base_commit_id, &bbc_arg);
2034 if (err)
2035 break;
2037 tpd = SIMPLEQ_NEXT(tpd, entry);
2039 sync_err = sync_fileindex(fileindex, fileindex_path);
2040 if (sync_err && err == NULL)
2041 err = sync_err;
2042 done:
2043 free(fileindex_path);
2044 if (tree)
2045 got_object_tree_close(tree);
2046 if (commit)
2047 got_object_commit_close(commit);
2048 if (fileindex)
2049 got_fileindex_free(fileindex);
2050 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2051 tpd = SIMPLEQ_FIRST(&tree_paths);
2052 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2053 free(tpd->relpath);
2054 free(tpd->tree_id);
2055 free(tpd);
2057 unlockerr = lock_worktree(worktree, LOCK_SH);
2058 if (unlockerr && err == NULL)
2059 err = unlockerr;
2060 return err;
2063 struct merge_file_cb_arg {
2064 struct got_worktree *worktree;
2065 struct got_fileindex *fileindex;
2066 got_worktree_checkout_cb progress_cb;
2067 void *progress_arg;
2068 got_cancel_cb cancel_cb;
2069 void *cancel_arg;
2070 const char *label_orig;
2071 struct got_object_id *commit_id2;
2074 static const struct got_error *
2075 merge_file_cb(void *arg, struct got_blob_object *blob1,
2076 struct got_blob_object *blob2, struct got_object_id *id1,
2077 struct got_object_id *id2, const char *path1, const char *path2,
2078 mode_t mode1, mode_t mode2, struct got_repository *repo)
2080 static const struct got_error *err = NULL;
2081 struct merge_file_cb_arg *a = arg;
2082 struct got_fileindex_entry *ie;
2083 char *ondisk_path = NULL;
2084 struct stat sb;
2085 unsigned char status;
2086 int local_changes_subsumed;
2088 if (blob1 && blob2) {
2089 ie = got_fileindex_entry_get(a->fileindex, path2,
2090 strlen(path2));
2091 if (ie == NULL)
2092 return (*a->progress_cb)(a->progress_arg,
2093 GOT_STATUS_MISSING, path2);
2095 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2096 path2) == -1)
2097 return got_error_from_errno("asprintf");
2099 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2100 repo);
2101 if (err)
2102 goto done;
2104 if (status == GOT_STATUS_DELETE) {
2105 err = (*a->progress_cb)(a->progress_arg,
2106 GOT_STATUS_MERGE, path2);
2107 goto done;
2109 if (status != GOT_STATUS_NO_CHANGE &&
2110 status != GOT_STATUS_MODIFY &&
2111 status != GOT_STATUS_CONFLICT &&
2112 status != GOT_STATUS_ADD) {
2113 err = (*a->progress_cb)(a->progress_arg, status, path2);
2114 goto done;
2117 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2118 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2119 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2120 } else if (blob1) {
2121 ie = got_fileindex_entry_get(a->fileindex, path1,
2122 strlen(path1));
2123 if (ie == NULL)
2124 return (*a->progress_cb)(a->progress_arg,
2125 GOT_STATUS_MISSING, path1);
2127 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2128 path1) == -1)
2129 return got_error_from_errno("asprintf");
2131 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2132 repo);
2133 if (err)
2134 goto done;
2136 switch (status) {
2137 case GOT_STATUS_NO_CHANGE:
2138 err = (*a->progress_cb)(a->progress_arg,
2139 GOT_STATUS_DELETE, path1);
2140 if (err)
2141 goto done;
2142 err = remove_ondisk_file(a->worktree->root_path, path1);
2143 if (err)
2144 goto done;
2145 if (ie)
2146 got_fileindex_entry_mark_deleted_from_disk(ie);
2147 break;
2148 case GOT_STATUS_DELETE:
2149 case GOT_STATUS_MISSING:
2150 err = (*a->progress_cb)(a->progress_arg,
2151 GOT_STATUS_DELETE, path1);
2152 if (err)
2153 goto done;
2154 if (ie)
2155 got_fileindex_entry_mark_deleted_from_disk(ie);
2156 break;
2157 case GOT_STATUS_ADD:
2158 case GOT_STATUS_MODIFY:
2159 case GOT_STATUS_CONFLICT:
2160 err = (*a->progress_cb)(a->progress_arg,
2161 GOT_STATUS_CANNOT_DELETE, path1);
2162 if (err)
2163 goto done;
2164 break;
2165 case GOT_STATUS_OBSTRUCTED:
2166 err = (*a->progress_cb)(a->progress_arg, status, path1);
2167 if (err)
2168 goto done;
2169 break;
2170 default:
2171 break;
2173 } else if (blob2) {
2174 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2175 path2) == -1)
2176 return got_error_from_errno("asprintf");
2177 ie = got_fileindex_entry_get(a->fileindex, path2,
2178 strlen(path2));
2179 if (ie) {
2180 err = get_file_status(&status, &sb, ie, ondisk_path,
2181 -1, NULL, repo);
2182 if (err)
2183 goto done;
2184 if (status != GOT_STATUS_NO_CHANGE &&
2185 status != GOT_STATUS_MODIFY &&
2186 status != GOT_STATUS_CONFLICT &&
2187 status != GOT_STATUS_ADD) {
2188 err = (*a->progress_cb)(a->progress_arg,
2189 status, path2);
2190 goto done;
2192 err = merge_blob(&local_changes_subsumed, a->worktree,
2193 NULL, ondisk_path, path2, sb.st_mode,
2194 a->label_orig, blob2, a->commit_id2, repo,
2195 a->progress_cb,
2196 a->progress_arg);
2197 if (status == GOT_STATUS_DELETE) {
2198 err = update_blob_fileindex_entry(a->worktree,
2199 a->fileindex, ie, ondisk_path, ie->path,
2200 blob2, 0);
2201 if (err)
2202 goto done;
2204 } else {
2205 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2206 err = install_blob(a->worktree, ondisk_path, path2,
2207 /* XXX get this from parent tree! */
2208 GOT_DEFAULT_FILE_MODE,
2209 sb.st_mode, blob2, 0, 0, repo,
2210 a->progress_cb, a->progress_arg);
2211 if (err)
2212 goto done;
2213 err = got_fileindex_entry_alloc(&ie, path2);
2214 if (err)
2215 goto done;
2216 err = got_fileindex_entry_update(ie, ondisk_path,
2217 NULL, NULL, 1);
2218 if (err) {
2219 got_fileindex_entry_free(ie);
2220 goto done;
2222 err = got_fileindex_entry_add(a->fileindex, ie);
2223 if (err) {
2224 got_fileindex_entry_free(ie);
2225 goto done;
2229 done:
2230 free(ondisk_path);
2231 return err;
2234 struct check_merge_ok_arg {
2235 struct got_worktree *worktree;
2236 struct got_repository *repo;
2239 static const struct got_error *
2240 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2242 const struct got_error *err = NULL;
2243 struct check_merge_ok_arg *a = arg;
2244 unsigned char status;
2245 struct stat sb;
2246 char *ondisk_path;
2248 /* Reject merges into a work tree with mixed base commits. */
2249 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2250 SHA1_DIGEST_LENGTH))
2251 return got_error(GOT_ERR_MIXED_COMMITS);
2253 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2254 == -1)
2255 return got_error_from_errno("asprintf");
2257 /* Reject merges into a work tree with conflicted files. */
2258 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2259 if (err)
2260 return err;
2261 if (status == GOT_STATUS_CONFLICT)
2262 return got_error(GOT_ERR_CONFLICTS);
2264 return NULL;
2267 static const struct got_error *
2268 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2269 const char *fileindex_path, struct got_object_id *commit_id1,
2270 struct got_object_id *commit_id2, struct got_repository *repo,
2271 got_worktree_checkout_cb progress_cb, void *progress_arg,
2272 got_cancel_cb cancel_cb, void *cancel_arg)
2274 const struct got_error *err = NULL, *sync_err;
2275 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2276 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2277 struct merge_file_cb_arg arg;
2278 char *label_orig = NULL;
2280 if (commit_id1) {
2281 char *id_str;
2283 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2284 worktree->path_prefix);
2285 if (err)
2286 goto done;
2288 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2289 if (err)
2290 goto done;
2292 err = got_object_id_str(&id_str, commit_id1);
2293 if (err)
2294 goto done;
2296 if (asprintf(&label_orig, "%s: commit %s",
2297 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2298 err = got_error_from_errno("asprintf");
2299 free(id_str);
2300 goto done;
2302 free(id_str);
2305 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2306 worktree->path_prefix);
2307 if (err)
2308 goto done;
2310 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2311 if (err)
2312 goto done;
2314 arg.worktree = worktree;
2315 arg.fileindex = fileindex;
2316 arg.progress_cb = progress_cb;
2317 arg.progress_arg = progress_arg;
2318 arg.cancel_cb = cancel_cb;
2319 arg.cancel_arg = cancel_arg;
2320 arg.label_orig = label_orig;
2321 arg.commit_id2 = commit_id2;
2322 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2323 sync_err = sync_fileindex(fileindex, fileindex_path);
2324 if (sync_err && err == NULL)
2325 err = sync_err;
2326 done:
2327 if (tree1)
2328 got_object_tree_close(tree1);
2329 if (tree2)
2330 got_object_tree_close(tree2);
2331 free(label_orig);
2332 return err;
2335 const struct got_error *
2336 got_worktree_merge_files(struct got_worktree *worktree,
2337 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2338 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2339 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2341 const struct got_error *err, *unlockerr;
2342 char *fileindex_path = NULL;
2343 struct got_fileindex *fileindex = NULL;
2344 struct check_merge_ok_arg mok_arg;
2346 err = lock_worktree(worktree, LOCK_EX);
2347 if (err)
2348 return err;
2350 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2351 if (err)
2352 goto done;
2354 mok_arg.worktree = worktree;
2355 mok_arg.repo = repo;
2356 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2357 &mok_arg);
2358 if (err)
2359 goto done;
2361 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2362 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2363 done:
2364 if (fileindex)
2365 got_fileindex_free(fileindex);
2366 free(fileindex_path);
2367 unlockerr = lock_worktree(worktree, LOCK_SH);
2368 if (unlockerr && err == NULL)
2369 err = unlockerr;
2370 return err;
2373 struct diff_dir_cb_arg {
2374 struct got_fileindex *fileindex;
2375 struct got_worktree *worktree;
2376 const char *status_path;
2377 size_t status_path_len;
2378 struct got_repository *repo;
2379 got_worktree_status_cb status_cb;
2380 void *status_arg;
2381 got_cancel_cb cancel_cb;
2382 void *cancel_arg;
2383 /* A pathlist containing per-directory pathlists of ignore patterns. */
2384 struct got_pathlist_head ignores;
2385 int report_unchanged;
2388 static const struct got_error *
2389 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2390 int dirfd, const char *de_name,
2391 got_worktree_status_cb status_cb, void *status_arg,
2392 struct got_repository *repo, int report_unchanged)
2394 const struct got_error *err = NULL;
2395 unsigned char status = GOT_STATUS_NO_CHANGE;
2396 unsigned char staged_status = get_staged_status(ie);
2397 struct stat sb;
2398 struct got_object_id blob_id, commit_id, staged_blob_id;
2399 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2400 struct got_object_id *staged_blob_idp = NULL;
2402 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2403 if (err)
2404 return err;
2406 if (status == GOT_STATUS_NO_CHANGE &&
2407 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2408 return NULL;
2410 if (got_fileindex_entry_has_blob(ie)) {
2411 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2412 blob_idp = &blob_id;
2414 if (got_fileindex_entry_has_commit(ie)) {
2415 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2416 commit_idp = &commit_id;
2418 if (staged_status == GOT_STATUS_ADD ||
2419 staged_status == GOT_STATUS_MODIFY) {
2420 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2421 SHA1_DIGEST_LENGTH);
2422 staged_blob_idp = &staged_blob_id;
2425 return (*status_cb)(status_arg, status, staged_status,
2426 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2429 static const struct got_error *
2430 status_old_new(void *arg, struct got_fileindex_entry *ie,
2431 struct dirent *de, const char *parent_path, int dirfd)
2433 const struct got_error *err = NULL;
2434 struct diff_dir_cb_arg *a = arg;
2435 char *abspath;
2437 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2438 return got_error(GOT_ERR_CANCELLED);
2440 if (got_path_cmp(parent_path, a->status_path,
2441 strlen(parent_path), a->status_path_len) != 0 &&
2442 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2443 return NULL;
2445 if (parent_path[0]) {
2446 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2447 parent_path, de->d_name) == -1)
2448 return got_error_from_errno("asprintf");
2449 } else {
2450 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2451 de->d_name) == -1)
2452 return got_error_from_errno("asprintf");
2455 err = report_file_status(ie, abspath, dirfd, de->d_name,
2456 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2457 free(abspath);
2458 return err;
2461 static const struct got_error *
2462 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2464 struct diff_dir_cb_arg *a = arg;
2465 struct got_object_id blob_id, commit_id;
2466 unsigned char status;
2468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2469 return got_error(GOT_ERR_CANCELLED);
2471 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2472 return NULL;
2474 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2475 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2476 if (got_fileindex_entry_has_file_on_disk(ie))
2477 status = GOT_STATUS_MISSING;
2478 else
2479 status = GOT_STATUS_DELETE;
2480 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2481 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2484 void
2485 free_ignorelist(struct got_pathlist_head *ignorelist)
2487 struct got_pathlist_entry *pe;
2489 TAILQ_FOREACH(pe, ignorelist, entry)
2490 free((char *)pe->path);
2491 got_pathlist_free(ignorelist);
2494 void
2495 free_ignores(struct got_pathlist_head *ignores)
2497 struct got_pathlist_entry *pe;
2499 TAILQ_FOREACH(pe, ignores, entry) {
2500 struct got_pathlist_head *ignorelist = pe->data;
2501 free_ignorelist(ignorelist);
2502 free((char *)pe->path);
2504 got_pathlist_free(ignores);
2507 static const struct got_error *
2508 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2510 const struct got_error *err = NULL;
2511 struct got_pathlist_entry *pe = NULL;
2512 struct got_pathlist_head *ignorelist;
2513 char *line = NULL, *pattern, *dirpath = NULL;
2514 size_t linesize = 0;
2515 ssize_t linelen;
2517 ignorelist = calloc(1, sizeof(*ignorelist));
2518 if (ignorelist == NULL)
2519 return got_error_from_errno("calloc");
2520 TAILQ_INIT(ignorelist);
2522 while ((linelen = getline(&line, &linesize, f)) != -1) {
2523 if (linelen > 0 && line[linelen - 1] == '\n')
2524 line[linelen - 1] = '\0';
2526 /* Git's ignores may contain comments. */
2527 if (line[0] == '#')
2528 continue;
2530 /* Git's negated patterns are not (yet?) supported. */
2531 if (line[0] == '!')
2532 continue;
2534 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2535 line) == -1) {
2536 err = got_error_from_errno("asprintf");
2537 goto done;
2539 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2540 if (err)
2541 goto done;
2543 if (ferror(f)) {
2544 err = got_error_from_errno("getline");
2545 goto done;
2548 dirpath = strdup(path);
2549 if (dirpath == NULL) {
2550 err = got_error_from_errno("strdup");
2551 goto done;
2553 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2554 done:
2555 free(line);
2556 if (err || pe == NULL) {
2557 free(dirpath);
2558 free_ignorelist(ignorelist);
2560 return err;
2563 int
2564 match_ignores(struct got_pathlist_head *ignores, const char *path)
2566 struct got_pathlist_entry *pe;
2568 /* Handle patterns which match in all directories. */
2569 TAILQ_FOREACH(pe, ignores, entry) {
2570 struct got_pathlist_head *ignorelist = pe->data;
2571 struct got_pathlist_entry *pi;
2573 TAILQ_FOREACH(pi, ignorelist, entry) {
2574 const char *p, *pattern = pi->path;
2576 if (strncmp(pattern, "**/", 3) != 0)
2577 continue;
2578 pattern += 3;
2579 p = path;
2580 while (*p) {
2581 if (fnmatch(pattern, p,
2582 FNM_PATHNAME | FNM_LEADING_DIR)) {
2583 /* Retry in next directory. */
2584 while (*p && *p != '/')
2585 p++;
2586 while (*p == '/')
2587 p++;
2588 continue;
2590 return 1;
2596 * The ignores pathlist contains ignore lists from children before
2597 * parents, so we can find the most specific ignorelist by walking
2598 * ignores backwards.
2600 pe = TAILQ_LAST(ignores, got_pathlist_head);
2601 while (pe) {
2602 if (got_path_is_child(path, pe->path, pe->path_len)) {
2603 struct got_pathlist_head *ignorelist = pe->data;
2604 struct got_pathlist_entry *pi;
2605 TAILQ_FOREACH(pi, ignorelist, entry) {
2606 const char *pattern = pi->path;
2607 int flags = FNM_LEADING_DIR;
2608 if (strstr(pattern, "/**/") == NULL)
2609 flags |= FNM_PATHNAME;
2610 if (fnmatch(pattern, path, flags))
2611 continue;
2612 return 1;
2615 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2618 return 0;
2621 static const struct got_error *
2622 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2623 const char *path, int dirfd, const char *ignores_filename)
2625 const struct got_error *err = NULL;
2626 char *ignorespath;
2627 int fd = -1;
2628 FILE *ignoresfile = NULL;
2630 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2631 path[0] ? "/" : "", ignores_filename) == -1)
2632 return got_error_from_errno("asprintf");
2634 if (dirfd != -1) {
2635 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2636 if (fd == -1) {
2637 if (errno != ENOENT && errno != EACCES)
2638 err = got_error_from_errno2("openat",
2639 ignorespath);
2640 } else {
2641 ignoresfile = fdopen(fd, "r");
2642 if (ignoresfile == NULL)
2643 err = got_error_from_errno2("fdopen",
2644 ignorespath);
2645 else {
2646 fd = -1;
2647 err = read_ignores(ignores, path, ignoresfile);
2650 } else {
2651 ignoresfile = fopen(ignorespath, "r");
2652 if (ignoresfile == NULL) {
2653 if (errno != ENOENT && errno != EACCES)
2654 err = got_error_from_errno2("fopen",
2655 ignorespath);
2656 } else
2657 err = read_ignores(ignores, path, ignoresfile);
2660 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2661 err = got_error_from_errno2("fclose", path);
2662 if (fd != -1 && close(fd) == -1 && err == NULL)
2663 err = got_error_from_errno2("close", path);
2664 free(ignorespath);
2665 return err;
2668 static const struct got_error *
2669 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2671 const struct got_error *err = NULL;
2672 struct diff_dir_cb_arg *a = arg;
2673 char *path = NULL;
2675 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2676 return got_error(GOT_ERR_CANCELLED);
2678 /* XXX ignore symlinks for now */
2679 if (de->d_type == DT_LNK)
2680 return NULL;
2682 if (parent_path[0]) {
2683 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2684 return got_error_from_errno("asprintf");
2685 } else {
2686 path = de->d_name;
2689 if (de->d_type == DT_DIR) {
2690 int subdirfd = openat(dirfd, de->d_name,
2691 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2692 if (subdirfd == -1) {
2693 if (errno != ENOENT && errno != EACCES)
2694 err = got_error_from_errno2("openat", path);
2695 } else {
2696 err = add_ignores(&a->ignores, a->worktree->root_path,
2697 path, subdirfd, ".cvsignore");
2698 if (err == NULL)
2699 err = add_ignores(&a->ignores,
2700 a->worktree->root_path, path,
2701 subdirfd, ".gitignore");
2702 if (close(subdirfd) == -1 && err == NULL)
2703 err = got_error_from_errno2("close", path);
2705 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2706 && !match_ignores(&a->ignores, path))
2707 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2708 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2709 if (parent_path[0])
2710 free(path);
2711 return err;
2714 static const struct got_error *
2715 report_single_file_status(const char *path, const char *ondisk_path,
2716 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2717 void *status_arg, struct got_repository *repo, int report_unchanged)
2719 struct got_fileindex_entry *ie;
2720 struct stat sb;
2722 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2723 if (ie)
2724 return report_file_status(ie, ondisk_path, -1, NULL,
2725 status_cb, status_arg, repo, report_unchanged);
2727 if (lstat(ondisk_path, &sb) == -1) {
2728 if (errno != ENOENT)
2729 return got_error_from_errno2("lstat", ondisk_path);
2730 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2731 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2732 return NULL;
2735 if (S_ISREG(sb.st_mode))
2736 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2737 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2739 return NULL;
2742 static const struct got_error *
2743 worktree_status(struct got_worktree *worktree, const char *path,
2744 struct got_fileindex *fileindex, struct got_repository *repo,
2745 got_worktree_status_cb status_cb, void *status_arg,
2746 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2747 int report_unchanged)
2749 const struct got_error *err = NULL;
2750 int fd = -1;
2751 struct got_fileindex_diff_dir_cb fdiff_cb;
2752 struct diff_dir_cb_arg arg;
2753 char *ondisk_path = NULL;
2755 if (asprintf(&ondisk_path, "%s%s%s",
2756 worktree->root_path, path[0] ? "/" : "", path) == -1)
2757 return got_error_from_errno("asprintf");
2759 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2760 if (fd == -1) {
2761 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2762 err = got_error_from_errno2("open", ondisk_path);
2763 else
2764 err = report_single_file_status(path, ondisk_path,
2765 fileindex, status_cb, status_arg, repo,
2766 report_unchanged);
2767 } else {
2768 fdiff_cb.diff_old_new = status_old_new;
2769 fdiff_cb.diff_old = status_old;
2770 fdiff_cb.diff_new = status_new;
2771 arg.fileindex = fileindex;
2772 arg.worktree = worktree;
2773 arg.status_path = path;
2774 arg.status_path_len = strlen(path);
2775 arg.repo = repo;
2776 arg.status_cb = status_cb;
2777 arg.status_arg = status_arg;
2778 arg.cancel_cb = cancel_cb;
2779 arg.cancel_arg = cancel_arg;
2780 arg.report_unchanged = report_unchanged;
2781 TAILQ_INIT(&arg.ignores);
2782 if (!no_ignores) {
2783 err = add_ignores(&arg.ignores, worktree->root_path,
2784 path, fd, ".cvsignore");
2785 if (err == NULL)
2786 err = add_ignores(&arg.ignores,
2787 worktree->root_path, path, fd,
2788 ".gitignore");
2790 if (err == NULL)
2791 err = got_fileindex_diff_dir(fileindex, fd,
2792 worktree->root_path, path, repo, &fdiff_cb, &arg);
2793 free_ignores(&arg.ignores);
2796 if (fd != -1 && close(fd) != 0 && err == NULL)
2797 err = got_error_from_errno("close");
2798 free(ondisk_path);
2799 return err;
2802 const struct got_error *
2803 got_worktree_status(struct got_worktree *worktree,
2804 struct got_pathlist_head *paths, struct got_repository *repo,
2805 got_worktree_status_cb status_cb, void *status_arg,
2806 got_cancel_cb cancel_cb, void *cancel_arg)
2808 const struct got_error *err = NULL;
2809 char *fileindex_path = NULL;
2810 struct got_fileindex *fileindex = NULL;
2811 struct got_pathlist_entry *pe;
2813 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2814 if (err)
2815 return err;
2817 TAILQ_FOREACH(pe, paths, entry) {
2818 err = worktree_status(worktree, pe->path, fileindex, repo,
2819 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2820 if (err)
2821 break;
2823 free(fileindex_path);
2824 got_fileindex_free(fileindex);
2825 return err;
2828 const struct got_error *
2829 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2830 const char *arg)
2832 const struct got_error *err = NULL;
2833 char *resolved, *cwd = NULL, *path = NULL;
2834 size_t len;
2836 *wt_path = NULL;
2838 resolved = realpath(arg, NULL);
2839 if (resolved == NULL) {
2840 if (errno != ENOENT)
2841 return got_error_from_errno2("realpath", arg);
2842 cwd = getcwd(NULL, 0);
2843 if (cwd == NULL)
2844 return got_error_from_errno("getcwd");
2845 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2846 err = got_error_from_errno("asprintf");
2847 goto done;
2851 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2852 strlen(got_worktree_get_root_path(worktree)))) {
2853 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2854 goto done;
2857 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2858 err = got_path_skip_common_ancestor(&path,
2859 got_worktree_get_root_path(worktree), resolved);
2860 if (err)
2861 goto done;
2862 } else {
2863 path = strdup("");
2864 if (path == NULL) {
2865 err = got_error_from_errno("strdup");
2866 goto done;
2870 /* XXX status walk can't deal with trailing slash! */
2871 len = strlen(path);
2872 while (len > 0 && path[len - 1] == '/') {
2873 path[len - 1] = '\0';
2874 len--;
2876 done:
2877 free(resolved);
2878 free(cwd);
2879 if (err == NULL)
2880 *wt_path = path;
2881 else
2882 free(path);
2883 return err;
2886 struct schedule_addition_args {
2887 struct got_worktree *worktree;
2888 struct got_fileindex *fileindex;
2889 got_worktree_checkout_cb progress_cb;
2890 void *progress_arg;
2891 struct got_repository *repo;
2894 static const struct got_error *
2895 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2896 const char *relpath, struct got_object_id *blob_id,
2897 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2898 int dirfd, const char *de_name)
2900 struct schedule_addition_args *a = arg;
2901 const struct got_error *err = NULL;
2902 struct got_fileindex_entry *ie;
2903 struct stat sb;
2904 char *ondisk_path;
2906 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2907 relpath) == -1)
2908 return got_error_from_errno("asprintf");
2910 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2911 if (ie) {
2912 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2913 de_name, a->repo);
2914 if (err)
2915 goto done;
2916 /* Re-adding an existing entry is a no-op. */
2917 if (status == GOT_STATUS_ADD)
2918 goto done;
2919 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2920 if (err)
2921 goto done;
2924 if (status != GOT_STATUS_UNVERSIONED) {
2925 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2926 goto done;
2929 err = got_fileindex_entry_alloc(&ie, relpath);
2930 if (err)
2931 goto done;
2932 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2933 if (err) {
2934 got_fileindex_entry_free(ie);
2935 goto done;
2937 err = got_fileindex_entry_add(a->fileindex, ie);
2938 if (err) {
2939 got_fileindex_entry_free(ie);
2940 goto done;
2942 done:
2943 free(ondisk_path);
2944 if (err)
2945 return err;
2946 if (status == GOT_STATUS_ADD)
2947 return NULL;
2948 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2951 const struct got_error *
2952 got_worktree_schedule_add(struct got_worktree *worktree,
2953 struct got_pathlist_head *paths,
2954 got_worktree_checkout_cb progress_cb, void *progress_arg,
2955 struct got_repository *repo, int no_ignores)
2957 struct got_fileindex *fileindex = NULL;
2958 char *fileindex_path = NULL;
2959 const struct got_error *err = NULL, *sync_err, *unlockerr;
2960 struct got_pathlist_entry *pe;
2961 struct schedule_addition_args saa;
2963 err = lock_worktree(worktree, LOCK_EX);
2964 if (err)
2965 return err;
2967 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2968 if (err)
2969 goto done;
2971 saa.worktree = worktree;
2972 saa.fileindex = fileindex;
2973 saa.progress_cb = progress_cb;
2974 saa.progress_arg = progress_arg;
2975 saa.repo = repo;
2977 TAILQ_FOREACH(pe, paths, entry) {
2978 err = worktree_status(worktree, pe->path, fileindex, repo,
2979 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2980 if (err)
2981 break;
2983 sync_err = sync_fileindex(fileindex, fileindex_path);
2984 if (sync_err && err == NULL)
2985 err = sync_err;
2986 done:
2987 free(fileindex_path);
2988 if (fileindex)
2989 got_fileindex_free(fileindex);
2990 unlockerr = lock_worktree(worktree, LOCK_SH);
2991 if (unlockerr && err == NULL)
2992 err = unlockerr;
2993 return err;
2996 struct schedule_deletion_args {
2997 struct got_worktree *worktree;
2998 struct got_fileindex *fileindex;
2999 got_worktree_delete_cb progress_cb;
3000 void *progress_arg;
3001 struct got_repository *repo;
3002 int delete_local_mods;
3003 int keep_on_disk;
3006 static const struct got_error *
3007 schedule_for_deletion(void *arg, unsigned char status,
3008 unsigned char staged_status, const char *relpath,
3009 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3010 struct got_object_id *commit_id, int dirfd, const char *de_name)
3012 struct schedule_deletion_args *a = arg;
3013 const struct got_error *err = NULL;
3014 struct got_fileindex_entry *ie = NULL;
3015 struct stat sb;
3016 char *ondisk_path, *parent = NULL;
3018 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3019 if (ie == NULL)
3020 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3022 staged_status = get_staged_status(ie);
3023 if (staged_status != GOT_STATUS_NO_CHANGE) {
3024 if (staged_status == GOT_STATUS_DELETE)
3025 return NULL;
3026 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3029 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3030 relpath) == -1)
3031 return got_error_from_errno("asprintf");
3033 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3034 a->repo);
3035 if (err)
3036 goto done;
3038 if (status != GOT_STATUS_NO_CHANGE) {
3039 if (status == GOT_STATUS_DELETE)
3040 goto done;
3041 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3042 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3043 goto done;
3045 if (status != GOT_STATUS_MODIFY &&
3046 status != GOT_STATUS_MISSING) {
3047 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3048 goto done;
3052 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3053 if (dirfd != -1) {
3054 if (unlinkat(dirfd, de_name, 0) != 0) {
3055 err = got_error_from_errno2("unlinkat",
3056 ondisk_path);
3057 goto done;
3059 } else if (unlink(ondisk_path) != 0) {
3060 err = got_error_from_errno2("unlink", ondisk_path);
3061 goto done;
3064 parent = dirname(ondisk_path);
3066 if (parent == NULL) {
3067 err = got_error_from_errno2("dirname", ondisk_path);
3068 goto done;
3070 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3071 if (rmdir(parent) == -1) {
3072 if (errno != ENOTEMPTY)
3073 err = got_error_from_errno2("rmdir",
3074 parent);
3075 break;
3077 parent = dirname(parent);
3078 if (parent == NULL) {
3079 err = got_error_from_errno2("dirname", parent);
3080 goto done;
3085 got_fileindex_entry_mark_deleted_from_disk(ie);
3086 done:
3087 free(ondisk_path);
3088 if (err)
3089 return err;
3090 if (status == GOT_STATUS_DELETE)
3091 return NULL;
3092 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3093 staged_status, relpath);
3096 const struct got_error *
3097 got_worktree_schedule_delete(struct got_worktree *worktree,
3098 struct got_pathlist_head *paths, int delete_local_mods,
3099 got_worktree_delete_cb progress_cb, void *progress_arg,
3100 struct got_repository *repo, int keep_on_disk)
3102 struct got_fileindex *fileindex = NULL;
3103 char *fileindex_path = NULL;
3104 const struct got_error *err = NULL, *sync_err, *unlockerr;
3105 struct got_pathlist_entry *pe;
3106 struct schedule_deletion_args sda;
3108 err = lock_worktree(worktree, LOCK_EX);
3109 if (err)
3110 return err;
3112 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3113 if (err)
3114 goto done;
3116 sda.worktree = worktree;
3117 sda.fileindex = fileindex;
3118 sda.progress_cb = progress_cb;
3119 sda.progress_arg = progress_arg;
3120 sda.repo = repo;
3121 sda.delete_local_mods = delete_local_mods;
3122 sda.keep_on_disk = keep_on_disk;
3124 TAILQ_FOREACH(pe, paths, entry) {
3125 err = worktree_status(worktree, pe->path, fileindex, repo,
3126 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3127 if (err)
3128 break;
3130 sync_err = sync_fileindex(fileindex, fileindex_path);
3131 if (sync_err && err == NULL)
3132 err = sync_err;
3133 done:
3134 free(fileindex_path);
3135 if (fileindex)
3136 got_fileindex_free(fileindex);
3137 unlockerr = lock_worktree(worktree, LOCK_SH);
3138 if (unlockerr && err == NULL)
3139 err = unlockerr;
3140 return err;
3143 static const struct got_error *
3144 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3146 const struct got_error *err = NULL;
3147 char *line = NULL;
3148 size_t linesize = 0, n;
3149 ssize_t linelen;
3151 linelen = getline(&line, &linesize, infile);
3152 if (linelen == -1) {
3153 if (ferror(infile)) {
3154 err = got_error_from_errno("getline");
3155 goto done;
3157 return NULL;
3159 if (outfile) {
3160 n = fwrite(line, 1, linelen, outfile);
3161 if (n != linelen) {
3162 err = got_ferror(outfile, GOT_ERR_IO);
3163 goto done;
3166 if (rejectfile) {
3167 n = fwrite(line, 1, linelen, rejectfile);
3168 if (n != linelen)
3169 err = got_ferror(outfile, GOT_ERR_IO);
3171 done:
3172 free(line);
3173 return err;
3176 static const struct got_error *
3177 skip_one_line(FILE *f)
3179 char *line = NULL;
3180 size_t linesize = 0;
3181 ssize_t linelen;
3183 linelen = getline(&line, &linesize, f);
3184 if (linelen == -1) {
3185 if (ferror(f))
3186 return got_error_from_errno("getline");
3187 return NULL;
3189 free(line);
3190 return NULL;
3193 static const struct got_error *
3194 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3195 int start_old, int end_old, int start_new, int end_new,
3196 FILE *outfile, FILE *rejectfile)
3198 const struct got_error *err;
3200 /* Copy old file's lines leading up to patch. */
3201 while (!feof(f1) && *line_cur1 < start_old) {
3202 err = copy_one_line(f1, outfile, NULL);
3203 if (err)
3204 return err;
3205 (*line_cur1)++;
3207 /* Skip new file's lines leading up to patch. */
3208 while (!feof(f2) && *line_cur2 < start_new) {
3209 if (rejectfile)
3210 err = copy_one_line(f2, NULL, rejectfile);
3211 else
3212 err = skip_one_line(f2);
3213 if (err)
3214 return err;
3215 (*line_cur2)++;
3217 /* Copy patched lines. */
3218 while (!feof(f2) && *line_cur2 <= end_new) {
3219 err = copy_one_line(f2, outfile, NULL);
3220 if (err)
3221 return err;
3222 (*line_cur2)++;
3224 /* Skip over old file's replaced lines. */
3225 while (!feof(f1) && *line_cur1 <= end_old) {
3226 if (rejectfile)
3227 err = copy_one_line(f1, NULL, rejectfile);
3228 else
3229 err = skip_one_line(f1);
3230 if (err)
3231 return err;
3232 (*line_cur1)++;
3235 return NULL;
3238 static const struct got_error *
3239 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3240 FILE *outfile, FILE *rejectfile)
3242 const struct got_error *err;
3244 if (outfile) {
3245 /* Copy old file's lines until EOF. */
3246 while (!feof(f1)) {
3247 err = copy_one_line(f1, outfile, NULL);
3248 if (err)
3249 return err;
3250 (*line_cur1)++;
3253 if (rejectfile) {
3254 /* Copy new file's lines until EOF. */
3255 while (!feof(f2)) {
3256 err = copy_one_line(f2, NULL, rejectfile);
3257 if (err)
3258 return err;
3259 (*line_cur2)++;
3263 return NULL;
3266 static const struct got_error *
3267 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3268 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3269 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3270 int *line_cur2, FILE *outfile, FILE *rejectfile,
3271 got_worktree_patch_cb patch_cb, void *patch_arg)
3273 const struct got_error *err = NULL;
3274 int start_old = change->cv.a;
3275 int end_old = change->cv.b;
3276 int start_new = change->cv.c;
3277 int end_new = change->cv.d;
3278 long pos1, pos2;
3279 FILE *hunkfile;
3281 *choice = GOT_PATCH_CHOICE_NONE;
3283 hunkfile = got_opentemp();
3284 if (hunkfile == NULL)
3285 return got_error_from_errno("got_opentemp");
3287 pos1 = ftell(f1);
3288 pos2 = ftell(f2);
3290 /* XXX TODO needs error checking */
3291 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3293 if (fseek(f1, pos1, SEEK_SET) == -1) {
3294 err = got_ferror(f1, GOT_ERR_IO);
3295 goto done;
3297 if (fseek(f2, pos2, SEEK_SET) == -1) {
3298 err = got_ferror(f1, GOT_ERR_IO);
3299 goto done;
3301 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3302 err = got_ferror(hunkfile, GOT_ERR_IO);
3303 goto done;
3306 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3307 hunkfile, n, nchanges);
3308 if (err)
3309 goto done;
3311 switch (*choice) {
3312 case GOT_PATCH_CHOICE_YES:
3313 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3314 end_old, start_new, end_new, outfile, rejectfile);
3315 break;
3316 case GOT_PATCH_CHOICE_NO:
3317 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3318 end_old, start_new, end_new, rejectfile, outfile);
3319 break;
3320 case GOT_PATCH_CHOICE_QUIT:
3321 break;
3322 default:
3323 err = got_error(GOT_ERR_PATCH_CHOICE);
3324 break;
3326 done:
3327 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3328 err = got_error_from_errno("fclose");
3329 return err;
3332 struct revert_file_args {
3333 struct got_worktree *worktree;
3334 struct got_fileindex *fileindex;
3335 got_worktree_checkout_cb progress_cb;
3336 void *progress_arg;
3337 got_worktree_patch_cb patch_cb;
3338 void *patch_arg;
3339 struct got_repository *repo;
3342 static const struct got_error *
3343 create_patched_content(char **path_outfile, int reverse_patch,
3344 struct got_object_id *blob_id, const char *path2,
3345 int dirfd2, const char *de_name2,
3346 const char *relpath, struct got_repository *repo,
3347 got_worktree_patch_cb patch_cb, void *patch_arg)
3349 const struct got_error *err;
3350 struct got_blob_object *blob = NULL;
3351 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3352 int fd2 = -1;
3353 char *path1 = NULL, *id_str = NULL;
3354 struct stat sb1, sb2;
3355 struct got_diff_changes *changes = NULL;
3356 struct got_diff_state *ds = NULL;
3357 struct got_diff_args *args = NULL;
3358 struct got_diff_change *change;
3359 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3360 int n = 0;
3362 *path_outfile = NULL;
3364 err = got_object_id_str(&id_str, blob_id);
3365 if (err)
3366 return err;
3368 if (dirfd2 != -1) {
3369 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3370 if (fd2 == -1) {
3371 err = got_error_from_errno2("openat", path2);
3372 goto done;
3374 } else {
3375 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3376 if (fd2 == -1) {
3377 err = got_error_from_errno2("open", path2);
3378 goto done;
3381 if (fstat(fd2, &sb2) == -1) {
3382 err = got_error_from_errno2("fstat", path2);
3383 goto done;
3386 f2 = fdopen(fd2, "r");
3387 if (f2 == NULL) {
3388 err = got_error_from_errno2("fdopen", path2);
3389 goto done;
3391 fd2 = -1;
3393 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3394 if (err)
3395 goto done;
3397 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3398 if (err)
3399 goto done;
3401 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3402 if (err)
3403 goto done;
3405 if (stat(path1, &sb1) == -1) {
3406 err = got_error_from_errno2("stat", path1);
3407 goto done;
3410 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3411 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3412 if (err)
3413 goto done;
3415 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3416 if (err)
3417 goto done;
3419 if (fseek(f1, 0L, SEEK_SET) == -1)
3420 return got_ferror(f1, GOT_ERR_IO);
3421 if (fseek(f2, 0L, SEEK_SET) == -1)
3422 return got_ferror(f2, GOT_ERR_IO);
3423 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3424 int choice;
3425 err = apply_or_reject_change(&choice, change, ++n,
3426 changes->nchanges, ds, args, diff_flags, relpath,
3427 f1, f2, &line_cur1, &line_cur2,
3428 reverse_patch ? NULL : outfile,
3429 reverse_patch ? outfile : NULL,
3430 patch_cb, patch_arg);
3431 if (err)
3432 goto done;
3433 if (choice == GOT_PATCH_CHOICE_YES)
3434 have_content = 1;
3435 else if (choice == GOT_PATCH_CHOICE_QUIT)
3436 break;
3438 if (have_content) {
3439 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3440 reverse_patch ? NULL : outfile,
3441 reverse_patch ? outfile : NULL);
3442 if (err)
3443 goto done;
3445 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3446 err = got_error_from_errno2("chmod", path2);
3447 goto done;
3450 done:
3451 free(id_str);
3452 if (blob)
3453 got_object_blob_close(blob);
3454 if (f1 && fclose(f1) == EOF && err == NULL)
3455 err = got_error_from_errno2("fclose", path1);
3456 if (f2 && fclose(f2) == EOF && err == NULL)
3457 err = got_error_from_errno2("fclose", path2);
3458 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3459 err = got_error_from_errno2("close", path2);
3460 if (outfile && fclose(outfile) == EOF && err == NULL)
3461 err = got_error_from_errno2("fclose", *path_outfile);
3462 if (path1 && unlink(path1) == -1 && err == NULL)
3463 err = got_error_from_errno2("unlink", path1);
3464 if (err || !have_content) {
3465 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3466 err = got_error_from_errno2("unlink", *path_outfile);
3467 free(*path_outfile);
3468 *path_outfile = NULL;
3470 free(args);
3471 if (ds) {
3472 got_diff_state_free(ds);
3473 free(ds);
3475 if (changes)
3476 got_diff_free_changes(changes);
3477 free(path1);
3478 return err;
3481 static const struct got_error *
3482 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3483 const char *relpath, struct got_object_id *blob_id,
3484 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3485 int dirfd, const char *de_name)
3487 struct revert_file_args *a = arg;
3488 const struct got_error *err = NULL;
3489 char *parent_path = NULL;
3490 struct got_fileindex_entry *ie;
3491 struct got_tree_object *tree = NULL;
3492 struct got_object_id *tree_id = NULL;
3493 const struct got_tree_entry *te = NULL;
3494 char *tree_path = NULL, *te_name;
3495 char *ondisk_path = NULL, *path_content = NULL;
3496 struct got_blob_object *blob = NULL;
3498 /* Reverting a staged deletion is a no-op. */
3499 if (status == GOT_STATUS_DELETE &&
3500 staged_status != GOT_STATUS_NO_CHANGE)
3501 return NULL;
3503 if (status == GOT_STATUS_UNVERSIONED)
3504 return (*a->progress_cb)(a->progress_arg,
3505 GOT_STATUS_UNVERSIONED, relpath);
3507 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3508 if (ie == NULL)
3509 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3511 /* Construct in-repository path of tree which contains this blob. */
3512 err = got_path_dirname(&parent_path, ie->path);
3513 if (err) {
3514 if (err->code != GOT_ERR_BAD_PATH)
3515 goto done;
3516 parent_path = strdup("/");
3517 if (parent_path == NULL) {
3518 err = got_error_from_errno("strdup");
3519 goto done;
3522 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3523 tree_path = strdup(parent_path);
3524 if (tree_path == NULL) {
3525 err = got_error_from_errno("strdup");
3526 goto done;
3528 } else {
3529 if (got_path_is_root_dir(parent_path)) {
3530 tree_path = strdup(a->worktree->path_prefix);
3531 if (tree_path == NULL) {
3532 err = got_error_from_errno("strdup");
3533 goto done;
3535 } else {
3536 if (asprintf(&tree_path, "%s/%s",
3537 a->worktree->path_prefix, parent_path) == -1) {
3538 err = got_error_from_errno("asprintf");
3539 goto done;
3544 err = got_object_id_by_path(&tree_id, a->repo,
3545 a->worktree->base_commit_id, tree_path);
3546 if (err) {
3547 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3548 (status == GOT_STATUS_ADD ||
3549 staged_status == GOT_STATUS_ADD)))
3550 goto done;
3551 } else {
3552 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3553 if (err)
3554 goto done;
3556 te_name = basename(ie->path);
3557 if (te_name == NULL) {
3558 err = got_error_from_errno2("basename", ie->path);
3559 goto done;
3562 te = got_object_tree_find_entry(tree, te_name);
3563 if (te == NULL && status != GOT_STATUS_ADD &&
3564 staged_status != GOT_STATUS_ADD) {
3565 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3566 goto done;
3570 switch (status) {
3571 case GOT_STATUS_ADD:
3572 if (a->patch_cb) {
3573 int choice = GOT_PATCH_CHOICE_NONE;
3574 err = (*a->patch_cb)(&choice, a->patch_arg,
3575 status, ie->path, NULL, 1, 1);
3576 if (err)
3577 goto done;
3578 if (choice != GOT_PATCH_CHOICE_YES)
3579 break;
3581 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3582 ie->path);
3583 if (err)
3584 goto done;
3585 got_fileindex_entry_remove(a->fileindex, ie);
3586 break;
3587 case GOT_STATUS_DELETE:
3588 if (a->patch_cb) {
3589 int choice = GOT_PATCH_CHOICE_NONE;
3590 err = (*a->patch_cb)(&choice, a->patch_arg,
3591 status, ie->path, NULL, 1, 1);
3592 if (err)
3593 goto done;
3594 if (choice != GOT_PATCH_CHOICE_YES)
3595 break;
3597 /* fall through */
3598 case GOT_STATUS_MODIFY:
3599 case GOT_STATUS_MODE_CHANGE:
3600 case GOT_STATUS_CONFLICT:
3601 case GOT_STATUS_MISSING: {
3602 struct got_object_id id;
3603 if (staged_status == GOT_STATUS_ADD ||
3604 staged_status == GOT_STATUS_MODIFY) {
3605 memcpy(id.sha1, ie->staged_blob_sha1,
3606 SHA1_DIGEST_LENGTH);
3607 } else
3608 memcpy(id.sha1, ie->blob_sha1,
3609 SHA1_DIGEST_LENGTH);
3610 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3611 if (err)
3612 goto done;
3614 if (asprintf(&ondisk_path, "%s/%s",
3615 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3616 err = got_error_from_errno("asprintf");
3617 goto done;
3620 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3621 status == GOT_STATUS_CONFLICT)) {
3622 err = create_patched_content(&path_content, 1, &id,
3623 ondisk_path, dirfd, de_name, ie->path, a->repo,
3624 a->patch_cb, a->patch_arg);
3625 if (err || path_content == NULL)
3626 break;
3627 if (rename(path_content, ondisk_path) == -1) {
3628 err = got_error_from_errno3("rename",
3629 path_content, ondisk_path);
3630 goto done;
3632 } else {
3633 err = install_blob(a->worktree, ondisk_path, ie->path,
3634 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3635 got_fileindex_perms_to_st(ie), blob, 0, 1,
3636 a->repo, a->progress_cb, a->progress_arg);
3637 if (err)
3638 goto done;
3639 if (status == GOT_STATUS_DELETE ||
3640 status == GOT_STATUS_MODE_CHANGE) {
3641 err = update_blob_fileindex_entry(a->worktree,
3642 a->fileindex, ie, ondisk_path, ie->path,
3643 blob, 1);
3644 if (err)
3645 goto done;
3648 break;
3650 default:
3651 break;
3653 done:
3654 free(ondisk_path);
3655 free(path_content);
3656 free(parent_path);
3657 free(tree_path);
3658 if (blob)
3659 got_object_blob_close(blob);
3660 if (tree)
3661 got_object_tree_close(tree);
3662 free(tree_id);
3663 return err;
3666 const struct got_error *
3667 got_worktree_revert(struct got_worktree *worktree,
3668 struct got_pathlist_head *paths,
3669 got_worktree_checkout_cb progress_cb, void *progress_arg,
3670 got_worktree_patch_cb patch_cb, void *patch_arg,
3671 struct got_repository *repo)
3673 struct got_fileindex *fileindex = NULL;
3674 char *fileindex_path = NULL;
3675 const struct got_error *err = NULL, *unlockerr = NULL;
3676 const struct got_error *sync_err = NULL;
3677 struct got_pathlist_entry *pe;
3678 struct revert_file_args rfa;
3680 err = lock_worktree(worktree, LOCK_EX);
3681 if (err)
3682 return err;
3684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3685 if (err)
3686 goto done;
3688 rfa.worktree = worktree;
3689 rfa.fileindex = fileindex;
3690 rfa.progress_cb = progress_cb;
3691 rfa.progress_arg = progress_arg;
3692 rfa.patch_cb = patch_cb;
3693 rfa.patch_arg = patch_arg;
3694 rfa.repo = repo;
3695 TAILQ_FOREACH(pe, paths, entry) {
3696 err = worktree_status(worktree, pe->path, fileindex, repo,
3697 revert_file, &rfa, NULL, NULL, 0, 0);
3698 if (err)
3699 break;
3701 sync_err = sync_fileindex(fileindex, fileindex_path);
3702 if (sync_err && err == NULL)
3703 err = sync_err;
3704 done:
3705 free(fileindex_path);
3706 if (fileindex)
3707 got_fileindex_free(fileindex);
3708 unlockerr = lock_worktree(worktree, LOCK_SH);
3709 if (unlockerr && err == NULL)
3710 err = unlockerr;
3711 return err;
3714 static void
3715 free_commitable(struct got_commitable *ct)
3717 free(ct->path);
3718 free(ct->in_repo_path);
3719 free(ct->ondisk_path);
3720 free(ct->blob_id);
3721 free(ct->base_blob_id);
3722 free(ct->staged_blob_id);
3723 free(ct->base_commit_id);
3724 free(ct);
3727 struct collect_commitables_arg {
3728 struct got_pathlist_head *commitable_paths;
3729 struct got_repository *repo;
3730 struct got_worktree *worktree;
3731 int have_staged_files;
3734 static const struct got_error *
3735 collect_commitables(void *arg, unsigned char status,
3736 unsigned char staged_status, const char *relpath,
3737 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3738 struct got_object_id *commit_id, int dirfd, const char *de_name)
3740 struct collect_commitables_arg *a = arg;
3741 const struct got_error *err = NULL;
3742 struct got_commitable *ct = NULL;
3743 struct got_pathlist_entry *new = NULL;
3744 char *parent_path = NULL, *path = NULL;
3745 struct stat sb;
3747 if (a->have_staged_files) {
3748 if (staged_status != GOT_STATUS_MODIFY &&
3749 staged_status != GOT_STATUS_ADD &&
3750 staged_status != GOT_STATUS_DELETE)
3751 return NULL;
3752 } else {
3753 if (status == GOT_STATUS_CONFLICT)
3754 return got_error(GOT_ERR_COMMIT_CONFLICT);
3756 if (status != GOT_STATUS_MODIFY &&
3757 status != GOT_STATUS_MODE_CHANGE &&
3758 status != GOT_STATUS_ADD &&
3759 status != GOT_STATUS_DELETE)
3760 return NULL;
3763 if (asprintf(&path, "/%s", relpath) == -1) {
3764 err = got_error_from_errno("asprintf");
3765 goto done;
3767 if (strcmp(path, "/") == 0) {
3768 parent_path = strdup("");
3769 if (parent_path == NULL)
3770 return got_error_from_errno("strdup");
3771 } else {
3772 err = got_path_dirname(&parent_path, path);
3773 if (err)
3774 return err;
3777 ct = calloc(1, sizeof(*ct));
3778 if (ct == NULL) {
3779 err = got_error_from_errno("calloc");
3780 goto done;
3783 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3784 relpath) == -1) {
3785 err = got_error_from_errno("asprintf");
3786 goto done;
3788 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3789 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3790 } else {
3791 if (dirfd != -1) {
3792 if (fstatat(dirfd, de_name, &sb,
3793 AT_SYMLINK_NOFOLLOW) == -1) {
3794 err = got_error_from_errno2("fstatat",
3795 ct->ondisk_path);
3796 goto done;
3798 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3799 err = got_error_from_errno2("lstat", ct->ondisk_path);
3800 goto done;
3802 ct->mode = sb.st_mode;
3805 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3806 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3807 relpath) == -1) {
3808 err = got_error_from_errno("asprintf");
3809 goto done;
3812 ct->status = status;
3813 ct->staged_status = staged_status;
3814 ct->blob_id = NULL; /* will be filled in when blob gets created */
3815 if (ct->status != GOT_STATUS_ADD &&
3816 ct->staged_status != GOT_STATUS_ADD) {
3817 ct->base_blob_id = got_object_id_dup(blob_id);
3818 if (ct->base_blob_id == NULL) {
3819 err = got_error_from_errno("got_object_id_dup");
3820 goto done;
3822 ct->base_commit_id = got_object_id_dup(commit_id);
3823 if (ct->base_commit_id == NULL) {
3824 err = got_error_from_errno("got_object_id_dup");
3825 goto done;
3828 if (ct->staged_status == GOT_STATUS_ADD ||
3829 ct->staged_status == GOT_STATUS_MODIFY) {
3830 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3831 if (ct->staged_blob_id == NULL) {
3832 err = got_error_from_errno("got_object_id_dup");
3833 goto done;
3836 ct->path = strdup(path);
3837 if (ct->path == NULL) {
3838 err = got_error_from_errno("strdup");
3839 goto done;
3841 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3842 done:
3843 if (ct && (err || new == NULL))
3844 free_commitable(ct);
3845 free(parent_path);
3846 free(path);
3847 return err;
3850 static const struct got_error *write_tree(struct got_object_id **, int *,
3851 struct got_tree_object *, const char *, struct got_pathlist_head *,
3852 got_worktree_status_cb status_cb, void *status_arg,
3853 struct got_repository *);
3855 static const struct got_error *
3856 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
3857 struct got_tree_entry *te, const char *parent_path,
3858 struct got_pathlist_head *commitable_paths,
3859 got_worktree_status_cb status_cb, void *status_arg,
3860 struct got_repository *repo)
3862 const struct got_error *err = NULL;
3863 struct got_tree_object *subtree;
3864 char *subpath;
3866 if (asprintf(&subpath, "%s%s%s", parent_path,
3867 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3868 return got_error_from_errno("asprintf");
3870 err = got_object_open_as_tree(&subtree, repo, &te->id);
3871 if (err)
3872 return err;
3874 err = write_tree(new_subtree_id, nentries, subtree, subpath,
3875 commitable_paths, status_cb, status_arg, repo);
3876 got_object_tree_close(subtree);
3877 free(subpath);
3878 return err;
3881 static const struct got_error *
3882 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3884 const struct got_error *err = NULL;
3885 char *ct_parent_path = NULL;
3887 *match = 0;
3889 if (strchr(ct->in_repo_path, '/') == NULL) {
3890 *match = got_path_is_root_dir(path);
3891 return NULL;
3894 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3895 if (err)
3896 return err;
3897 *match = (strcmp(path, ct_parent_path) == 0);
3898 free(ct_parent_path);
3899 return err;
3902 static mode_t
3903 get_ct_file_mode(struct got_commitable *ct)
3905 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3908 static const struct got_error *
3909 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3910 struct got_tree_entry *te, struct got_commitable *ct)
3912 const struct got_error *err = NULL;
3914 *new_te = NULL;
3916 err = got_object_tree_entry_dup(new_te, te);
3917 if (err)
3918 goto done;
3920 (*new_te)->mode = get_ct_file_mode(ct);
3922 if (ct->staged_status == GOT_STATUS_MODIFY)
3923 memcpy(&(*new_te)->id, ct->staged_blob_id,
3924 sizeof((*new_te)->id));
3925 else
3926 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3927 done:
3928 if (err && *new_te) {
3929 free(*new_te);
3930 *new_te = NULL;
3932 return err;
3935 static const struct got_error *
3936 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3937 struct got_commitable *ct)
3939 const struct got_error *err = NULL;
3940 char *ct_name;
3942 *new_te = NULL;
3944 *new_te = calloc(1, sizeof(**new_te));
3945 if (*new_te == NULL)
3946 return got_error_from_errno("calloc");
3948 ct_name = basename(ct->path);
3949 if (ct_name == NULL) {
3950 err = got_error_from_errno2("basename", ct->path);
3951 goto done;
3953 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3954 sizeof((*new_te)->name)) {
3955 err = got_error(GOT_ERR_NO_SPACE);
3956 goto done;
3959 (*new_te)->mode = get_ct_file_mode(ct);
3961 if (ct->staged_status == GOT_STATUS_ADD)
3962 memcpy(&(*new_te)->id, ct->staged_blob_id,
3963 sizeof((*new_te)->id));
3964 else
3965 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3966 done:
3967 if (err && *new_te) {
3968 free(*new_te);
3969 *new_te = NULL;
3971 return err;
3974 static const struct got_error *
3975 insert_tree_entry(struct got_tree_entry *new_te,
3976 struct got_pathlist_head *paths)
3978 const struct got_error *err = NULL;
3979 struct got_pathlist_entry *new_pe;
3981 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3982 if (err)
3983 return err;
3984 if (new_pe == NULL)
3985 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3986 return NULL;
3989 static const struct got_error *
3990 report_ct_status(struct got_commitable *ct,
3991 got_worktree_status_cb status_cb, void *status_arg)
3993 const char *ct_path = ct->path;
3994 unsigned char status;
3996 while (ct_path[0] == '/')
3997 ct_path++;
3999 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4000 status = ct->staged_status;
4001 else
4002 status = ct->status;
4004 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4005 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4008 static const struct got_error *
4009 match_modified_subtree(int *modified, struct got_tree_entry *te,
4010 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4012 const struct got_error *err = NULL;
4013 struct got_pathlist_entry *pe;
4014 char *te_path;
4016 *modified = 0;
4018 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4019 got_path_is_root_dir(base_tree_path) ? "" : "/",
4020 te->name) == -1)
4021 return got_error_from_errno("asprintf");
4023 TAILQ_FOREACH(pe, commitable_paths, entry) {
4024 struct got_commitable *ct = pe->data;
4025 *modified = got_path_is_child(ct->in_repo_path, te_path,
4026 strlen(te_path));
4027 if (*modified)
4028 break;
4031 free(te_path);
4032 return err;
4035 static const struct got_error *
4036 match_deleted_or_modified_ct(struct got_commitable **ctp,
4037 struct got_tree_entry *te, const char *base_tree_path,
4038 struct got_pathlist_head *commitable_paths)
4040 const struct got_error *err = NULL;
4041 struct got_pathlist_entry *pe;
4043 *ctp = NULL;
4045 TAILQ_FOREACH(pe, commitable_paths, entry) {
4046 struct got_commitable *ct = pe->data;
4047 char *ct_name = NULL;
4048 int path_matches;
4050 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4051 if (ct->status != GOT_STATUS_MODIFY &&
4052 ct->status != GOT_STATUS_MODE_CHANGE &&
4053 ct->status != GOT_STATUS_DELETE)
4054 continue;
4055 } else {
4056 if (ct->staged_status != GOT_STATUS_MODIFY &&
4057 ct->staged_status != GOT_STATUS_DELETE)
4058 continue;
4061 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4062 continue;
4064 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4065 if (err)
4066 return err;
4067 if (!path_matches)
4068 continue;
4070 ct_name = basename(pe->path);
4071 if (ct_name == NULL)
4072 return got_error_from_errno2("basename", pe->path);
4074 if (strcmp(te->name, ct_name) != 0)
4075 continue;
4077 *ctp = ct;
4078 break;
4081 return err;
4084 static const struct got_error *
4085 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4086 const char *child_path, const char *path_base_tree,
4087 struct got_pathlist_head *commitable_paths,
4088 got_worktree_status_cb status_cb, void *status_arg,
4089 struct got_repository *repo)
4091 const struct got_error *err = NULL;
4092 struct got_tree_entry *new_te;
4093 char *subtree_path;
4094 struct got_object_id *id = NULL;
4095 int nentries;
4097 *new_tep = NULL;
4099 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4100 got_path_is_root_dir(path_base_tree) ? "" : "/",
4101 child_path) == -1)
4102 return got_error_from_errno("asprintf");
4104 new_te = calloc(1, sizeof(*new_te));
4105 if (new_te == NULL)
4106 return got_error_from_errno("calloc");
4107 new_te->mode = S_IFDIR;
4109 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4110 sizeof(new_te->name)) {
4111 err = got_error(GOT_ERR_NO_SPACE);
4112 goto done;
4114 err = write_tree(&id, &nentries, NULL, subtree_path,
4115 commitable_paths, status_cb, status_arg, repo);
4116 if (err) {
4117 free(new_te);
4118 goto done;
4120 memcpy(&new_te->id, id, sizeof(new_te->id));
4121 done:
4122 free(id);
4123 free(subtree_path);
4124 if (err == NULL)
4125 *new_tep = new_te;
4126 return err;
4129 static const struct got_error *
4130 write_tree(struct got_object_id **new_tree_id, int *nentries,
4131 struct got_tree_object *base_tree, const char *path_base_tree,
4132 struct got_pathlist_head *commitable_paths,
4133 got_worktree_status_cb status_cb, void *status_arg,
4134 struct got_repository *repo)
4136 const struct got_error *err = NULL;
4137 struct got_pathlist_head paths;
4138 struct got_tree_entry *te, *new_te = NULL;
4139 struct got_pathlist_entry *pe;
4141 TAILQ_INIT(&paths);
4142 *nentries = 0;
4144 /* Insert, and recurse into, newly added entries first. */
4145 TAILQ_FOREACH(pe, commitable_paths, entry) {
4146 struct got_commitable *ct = pe->data;
4147 char *child_path = NULL, *slash;
4149 if ((ct->status != GOT_STATUS_ADD &&
4150 ct->staged_status != GOT_STATUS_ADD) ||
4151 (ct->flags & GOT_COMMITABLE_ADDED))
4152 continue;
4154 if (!got_path_is_child(pe->path, path_base_tree,
4155 strlen(path_base_tree)))
4156 continue;
4158 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4159 pe->path);
4160 if (err)
4161 goto done;
4163 slash = strchr(child_path, '/');
4164 if (slash == NULL) {
4165 err = alloc_added_blob_tree_entry(&new_te, ct);
4166 if (err)
4167 goto done;
4168 err = report_ct_status(ct, status_cb, status_arg);
4169 if (err)
4170 goto done;
4171 ct->flags |= GOT_COMMITABLE_ADDED;
4172 err = insert_tree_entry(new_te, &paths);
4173 if (err)
4174 goto done;
4175 (*nentries)++;
4176 } else {
4177 *slash = '\0'; /* trim trailing path components */
4178 if (base_tree == NULL ||
4179 got_object_tree_find_entry(base_tree, child_path)
4180 == NULL) {
4181 err = make_subtree_for_added_blob(&new_te,
4182 child_path, path_base_tree,
4183 commitable_paths, status_cb, status_arg,
4184 repo);
4185 if (err)
4186 goto done;
4187 err = insert_tree_entry(new_te, &paths);
4188 if (err)
4189 goto done;
4190 (*nentries)++;
4195 if (base_tree) {
4196 int i, nbase_entries;
4197 /* Handle modified and deleted entries. */
4198 nbase_entries = got_object_tree_get_nentries(base_tree);
4199 for (i = 0; i < nbase_entries; i++) {
4200 struct got_commitable *ct = NULL;
4202 te = got_object_tree_get_entry(base_tree, i);
4203 if (got_object_tree_entry_is_submodule(te)) {
4204 /* Entry is a submodule; just copy it. */
4205 err = got_object_tree_entry_dup(&new_te, te);
4206 if (err)
4207 goto done;
4208 err = insert_tree_entry(new_te, &paths);
4209 if (err)
4210 goto done;
4211 (*nentries)++;
4212 continue;
4215 if (S_ISDIR(te->mode)) {
4216 int modified;
4217 err = got_object_tree_entry_dup(&new_te, te);
4218 if (err)
4219 goto done;
4220 err = match_modified_subtree(&modified, te,
4221 path_base_tree, commitable_paths);
4222 if (err)
4223 goto done;
4224 /* Avoid recursion into unmodified subtrees. */
4225 if (modified) {
4226 struct got_object_id *new_id;
4227 int nsubentries;
4228 err = write_subtree(&new_id,
4229 &nsubentries, te,
4230 path_base_tree, commitable_paths,
4231 status_cb, status_arg, repo);
4232 if (err)
4233 goto done;
4234 if (nsubentries == 0) {
4235 /* All entries were deleted. */
4236 free(new_id);
4237 continue;
4239 memcpy(&new_te->id, new_id,
4240 sizeof(new_te->id));
4241 free(new_id);
4243 err = insert_tree_entry(new_te, &paths);
4244 if (err)
4245 goto done;
4246 (*nentries)++;
4247 continue;
4250 err = match_deleted_or_modified_ct(&ct, te,
4251 path_base_tree, commitable_paths);
4252 if (err)
4253 goto done;
4254 if (ct) {
4255 /* NB: Deleted entries get dropped here. */
4256 if (ct->status == GOT_STATUS_MODIFY ||
4257 ct->status == GOT_STATUS_MODE_CHANGE ||
4258 ct->staged_status == GOT_STATUS_MODIFY) {
4259 err = alloc_modified_blob_tree_entry(
4260 &new_te, te, ct);
4261 if (err)
4262 goto done;
4263 err = insert_tree_entry(new_te, &paths);
4264 if (err)
4265 goto done;
4266 (*nentries)++;
4268 err = report_ct_status(ct, status_cb,
4269 status_arg);
4270 if (err)
4271 goto done;
4272 } else {
4273 /* Entry is unchanged; just copy it. */
4274 err = got_object_tree_entry_dup(&new_te, te);
4275 if (err)
4276 goto done;
4277 err = insert_tree_entry(new_te, &paths);
4278 if (err)
4279 goto done;
4280 (*nentries)++;
4285 /* Write new list of entries; deleted entries have been dropped. */
4286 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4287 done:
4288 got_pathlist_free(&paths);
4289 return err;
4292 static const struct got_error *
4293 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4294 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4295 int have_staged_files)
4297 const struct got_error *err = NULL;
4298 struct got_pathlist_entry *pe;
4300 TAILQ_FOREACH(pe, commitable_paths, entry) {
4301 struct got_fileindex_entry *ie;
4302 struct got_commitable *ct = pe->data;
4304 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4305 if (ie) {
4306 if (ct->status == GOT_STATUS_DELETE ||
4307 ct->staged_status == GOT_STATUS_DELETE) {
4308 got_fileindex_entry_remove(fileindex, ie);
4309 } else if (ct->staged_status == GOT_STATUS_ADD ||
4310 ct->staged_status == GOT_STATUS_MODIFY) {
4311 got_fileindex_entry_stage_set(ie,
4312 GOT_FILEIDX_STAGE_NONE);
4313 err = got_fileindex_entry_update(ie,
4314 ct->ondisk_path, ct->staged_blob_id->sha1,
4315 new_base_commit_id->sha1,
4316 !have_staged_files);
4317 } else
4318 err = got_fileindex_entry_update(ie,
4319 ct->ondisk_path, ct->blob_id->sha1,
4320 new_base_commit_id->sha1,
4321 !have_staged_files);
4322 } else {
4323 err = got_fileindex_entry_alloc(&ie, pe->path);
4324 if (err)
4325 break;
4326 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4327 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4328 if (err) {
4329 got_fileindex_entry_free(ie);
4330 break;
4332 err = got_fileindex_entry_add(fileindex, ie);
4333 if (err) {
4334 got_fileindex_entry_free(ie);
4335 break;
4339 return err;
4343 static const struct got_error *
4344 check_out_of_date(const char *in_repo_path, unsigned char status,
4345 unsigned char staged_status, struct got_object_id *base_blob_id,
4346 struct got_object_id *base_commit_id,
4347 struct got_object_id *head_commit_id, struct got_repository *repo,
4348 int ood_errcode)
4350 const struct got_error *err = NULL;
4351 struct got_object_id *id = NULL;
4353 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4354 /* Trivial case: base commit == head commit */
4355 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4356 return NULL;
4358 * Ensure file content which local changes were based
4359 * on matches file content in the branch head.
4361 err = got_object_id_by_path(&id, repo, head_commit_id,
4362 in_repo_path);
4363 if (err) {
4364 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4365 err = got_error(ood_errcode);
4366 goto done;
4367 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4368 err = got_error(ood_errcode);
4369 } else {
4370 /* Require that added files don't exist in the branch head. */
4371 err = got_object_id_by_path(&id, repo, head_commit_id,
4372 in_repo_path);
4373 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4374 goto done;
4375 err = id ? got_error(ood_errcode) : NULL;
4377 done:
4378 free(id);
4379 return err;
4382 const struct got_error *
4383 commit_worktree(struct got_object_id **new_commit_id,
4384 struct got_pathlist_head *commitable_paths,
4385 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4386 const char *author, const char *committer,
4387 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4388 got_worktree_status_cb status_cb, void *status_arg,
4389 struct got_repository *repo)
4391 const struct got_error *err = NULL, *unlockerr = NULL;
4392 struct got_pathlist_entry *pe;
4393 const char *head_ref_name = NULL;
4394 struct got_commit_object *head_commit = NULL;
4395 struct got_reference *head_ref2 = NULL;
4396 struct got_object_id *head_commit_id2 = NULL;
4397 struct got_tree_object *head_tree = NULL;
4398 struct got_object_id *new_tree_id = NULL;
4399 int nentries;
4400 struct got_object_id_queue parent_ids;
4401 struct got_object_qid *pid = NULL;
4402 char *logmsg = NULL;
4404 *new_commit_id = NULL;
4406 SIMPLEQ_INIT(&parent_ids);
4408 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4409 if (err)
4410 goto done;
4412 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4413 if (err)
4414 goto done;
4416 if (commit_msg_cb != NULL) {
4417 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4418 if (err)
4419 goto done;
4422 if (logmsg == NULL || strlen(logmsg) == 0) {
4423 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4424 goto done;
4427 /* Create blobs from added and modified files and record their IDs. */
4428 TAILQ_FOREACH(pe, commitable_paths, entry) {
4429 struct got_commitable *ct = pe->data;
4430 char *ondisk_path;
4432 /* Blobs for staged files already exist. */
4433 if (ct->staged_status == GOT_STATUS_ADD ||
4434 ct->staged_status == GOT_STATUS_MODIFY)
4435 continue;
4437 if (ct->status != GOT_STATUS_ADD &&
4438 ct->status != GOT_STATUS_MODIFY &&
4439 ct->status != GOT_STATUS_MODE_CHANGE)
4440 continue;
4442 if (asprintf(&ondisk_path, "%s/%s",
4443 worktree->root_path, pe->path) == -1) {
4444 err = got_error_from_errno("asprintf");
4445 goto done;
4447 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4448 free(ondisk_path);
4449 if (err)
4450 goto done;
4453 /* Recursively write new tree objects. */
4454 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4455 commitable_paths, status_cb, status_arg, repo);
4456 if (err)
4457 goto done;
4459 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4460 if (err)
4461 goto done;
4462 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4463 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4464 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4465 got_object_qid_free(pid);
4466 if (logmsg != NULL)
4467 free(logmsg);
4468 if (err)
4469 goto done;
4471 /* Check if a concurrent commit to our branch has occurred. */
4472 head_ref_name = got_worktree_get_head_ref_name(worktree);
4473 if (head_ref_name == NULL) {
4474 err = got_error_from_errno("got_worktree_get_head_ref_name");
4475 goto done;
4477 /* Lock the reference here to prevent concurrent modification. */
4478 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4479 if (err)
4480 goto done;
4481 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4482 if (err)
4483 goto done;
4484 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4485 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4486 goto done;
4488 /* Update branch head in repository. */
4489 err = got_ref_change_ref(head_ref2, *new_commit_id);
4490 if (err)
4491 goto done;
4492 err = got_ref_write(head_ref2, repo);
4493 if (err)
4494 goto done;
4496 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4497 if (err)
4498 goto done;
4500 err = ref_base_commit(worktree, repo);
4501 if (err)
4502 goto done;
4503 done:
4504 if (head_tree)
4505 got_object_tree_close(head_tree);
4506 if (head_commit)
4507 got_object_commit_close(head_commit);
4508 free(head_commit_id2);
4509 if (head_ref2) {
4510 unlockerr = got_ref_unlock(head_ref2);
4511 if (unlockerr && err == NULL)
4512 err = unlockerr;
4513 got_ref_close(head_ref2);
4515 return err;
4518 static const struct got_error *
4519 check_path_is_commitable(const char *path,
4520 struct got_pathlist_head *commitable_paths)
4522 struct got_pathlist_entry *cpe = NULL;
4523 size_t path_len = strlen(path);
4525 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4526 struct got_commitable *ct = cpe->data;
4527 const char *ct_path = ct->path;
4529 while (ct_path[0] == '/')
4530 ct_path++;
4532 if (strcmp(path, ct_path) == 0 ||
4533 got_path_is_child(ct_path, path, path_len))
4534 break;
4537 if (cpe == NULL)
4538 return got_error_path(path, GOT_ERR_BAD_PATH);
4540 return NULL;
4543 static const struct got_error *
4544 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4546 int *have_staged_files = arg;
4548 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4549 *have_staged_files = 1;
4550 return got_error(GOT_ERR_CANCELLED);
4553 return NULL;
4556 static const struct got_error *
4557 check_non_staged_files(struct got_fileindex *fileindex,
4558 struct got_pathlist_head *paths)
4560 struct got_pathlist_entry *pe;
4561 struct got_fileindex_entry *ie;
4563 TAILQ_FOREACH(pe, paths, entry) {
4564 if (pe->path[0] == '\0')
4565 continue;
4566 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4567 if (ie == NULL)
4568 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4569 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4570 return got_error_path(pe->path,
4571 GOT_ERR_FILE_NOT_STAGED);
4574 return NULL;
4577 const struct got_error *
4578 got_worktree_commit(struct got_object_id **new_commit_id,
4579 struct got_worktree *worktree, struct got_pathlist_head *paths,
4580 const char *author, const char *committer,
4581 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4582 got_worktree_status_cb status_cb, void *status_arg,
4583 struct got_repository *repo)
4585 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4586 struct got_fileindex *fileindex = NULL;
4587 char *fileindex_path = NULL;
4588 struct got_pathlist_head commitable_paths;
4589 struct collect_commitables_arg cc_arg;
4590 struct got_pathlist_entry *pe;
4591 struct got_reference *head_ref = NULL;
4592 struct got_object_id *head_commit_id = NULL;
4593 int have_staged_files = 0;
4595 *new_commit_id = NULL;
4597 TAILQ_INIT(&commitable_paths);
4599 err = lock_worktree(worktree, LOCK_EX);
4600 if (err)
4601 goto done;
4603 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4604 if (err)
4605 goto done;
4607 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4608 if (err)
4609 goto done;
4611 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4612 if (err)
4613 goto done;
4615 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4616 &have_staged_files);
4617 if (err && err->code != GOT_ERR_CANCELLED)
4618 goto done;
4619 if (have_staged_files) {
4620 err = check_non_staged_files(fileindex, paths);
4621 if (err)
4622 goto done;
4625 cc_arg.commitable_paths = &commitable_paths;
4626 cc_arg.worktree = worktree;
4627 cc_arg.repo = repo;
4628 cc_arg.have_staged_files = have_staged_files;
4629 TAILQ_FOREACH(pe, paths, entry) {
4630 err = worktree_status(worktree, pe->path, fileindex, repo,
4631 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4632 if (err)
4633 goto done;
4636 if (TAILQ_EMPTY(&commitable_paths)) {
4637 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4638 goto done;
4641 TAILQ_FOREACH(pe, paths, entry) {
4642 err = check_path_is_commitable(pe->path, &commitable_paths);
4643 if (err)
4644 goto done;
4647 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4648 struct got_commitable *ct = pe->data;
4649 const char *ct_path = ct->in_repo_path;
4651 while (ct_path[0] == '/')
4652 ct_path++;
4653 err = check_out_of_date(ct_path, ct->status,
4654 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4655 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4656 if (err)
4657 goto done;
4661 err = commit_worktree(new_commit_id, &commitable_paths,
4662 head_commit_id, worktree, author, committer,
4663 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4664 if (err)
4665 goto done;
4667 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4668 fileindex, have_staged_files);
4669 sync_err = sync_fileindex(fileindex, fileindex_path);
4670 if (sync_err && err == NULL)
4671 err = sync_err;
4672 done:
4673 if (fileindex)
4674 got_fileindex_free(fileindex);
4675 free(fileindex_path);
4676 unlockerr = lock_worktree(worktree, LOCK_SH);
4677 if (unlockerr && err == NULL)
4678 err = unlockerr;
4679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4680 struct got_commitable *ct = pe->data;
4681 free_commitable(ct);
4683 got_pathlist_free(&commitable_paths);
4684 return err;
4687 const char *
4688 got_commitable_get_path(struct got_commitable *ct)
4690 return ct->path;
4693 unsigned int
4694 got_commitable_get_status(struct got_commitable *ct)
4696 return ct->status;
4699 struct check_rebase_ok_arg {
4700 struct got_worktree *worktree;
4701 struct got_repository *repo;
4704 static const struct got_error *
4705 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4707 const struct got_error *err = NULL;
4708 struct check_rebase_ok_arg *a = arg;
4709 unsigned char status;
4710 struct stat sb;
4711 char *ondisk_path;
4713 /* Reject rebase of a work tree with mixed base commits. */
4714 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4715 SHA1_DIGEST_LENGTH))
4716 return got_error(GOT_ERR_MIXED_COMMITS);
4718 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4719 == -1)
4720 return got_error_from_errno("asprintf");
4722 /* Reject rebase of a work tree with modified or staged files. */
4723 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4724 free(ondisk_path);
4725 if (err)
4726 return err;
4728 if (status != GOT_STATUS_NO_CHANGE)
4729 return got_error(GOT_ERR_MODIFIED);
4730 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4731 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4733 return NULL;
4736 const struct got_error *
4737 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4738 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4739 struct got_worktree *worktree, struct got_reference *branch,
4740 struct got_repository *repo)
4742 const struct got_error *err = NULL;
4743 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4744 char *branch_ref_name = NULL;
4745 char *fileindex_path = NULL;
4746 struct check_rebase_ok_arg ok_arg;
4747 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4748 struct got_object_id *wt_branch_tip = NULL;
4750 *new_base_branch_ref = NULL;
4751 *tmp_branch = NULL;
4752 *fileindex = NULL;
4754 err = lock_worktree(worktree, LOCK_EX);
4755 if (err)
4756 return err;
4758 err = open_fileindex(fileindex, &fileindex_path, worktree);
4759 if (err)
4760 goto done;
4762 ok_arg.worktree = worktree;
4763 ok_arg.repo = repo;
4764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4765 &ok_arg);
4766 if (err)
4767 goto done;
4769 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4770 if (err)
4771 goto done;
4773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4774 if (err)
4775 goto done;
4777 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4778 if (err)
4779 goto done;
4781 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4782 0);
4783 if (err)
4784 goto done;
4786 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4787 if (err)
4788 goto done;
4789 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4790 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4791 goto done;
4794 err = got_ref_alloc_symref(new_base_branch_ref,
4795 new_base_branch_ref_name, wt_branch);
4796 if (err)
4797 goto done;
4798 err = got_ref_write(*new_base_branch_ref, repo);
4799 if (err)
4800 goto done;
4802 /* TODO Lock original branch's ref while rebasing? */
4804 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4805 if (err)
4806 goto done;
4808 err = got_ref_write(branch_ref, repo);
4809 if (err)
4810 goto done;
4812 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4813 worktree->base_commit_id);
4814 if (err)
4815 goto done;
4816 err = got_ref_write(*tmp_branch, repo);
4817 if (err)
4818 goto done;
4820 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4821 if (err)
4822 goto done;
4823 done:
4824 free(fileindex_path);
4825 free(tmp_branch_name);
4826 free(new_base_branch_ref_name);
4827 free(branch_ref_name);
4828 if (branch_ref)
4829 got_ref_close(branch_ref);
4830 if (wt_branch)
4831 got_ref_close(wt_branch);
4832 free(wt_branch_tip);
4833 if (err) {
4834 if (*new_base_branch_ref) {
4835 got_ref_close(*new_base_branch_ref);
4836 *new_base_branch_ref = NULL;
4838 if (*tmp_branch) {
4839 got_ref_close(*tmp_branch);
4840 *tmp_branch = NULL;
4842 if (*fileindex) {
4843 got_fileindex_free(*fileindex);
4844 *fileindex = NULL;
4846 lock_worktree(worktree, LOCK_SH);
4848 return err;
4851 const struct got_error *
4852 got_worktree_rebase_continue(struct got_object_id **commit_id,
4853 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4854 struct got_reference **branch, struct got_fileindex **fileindex,
4855 struct got_worktree *worktree, struct got_repository *repo)
4857 const struct got_error *err;
4858 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4859 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4860 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4861 char *fileindex_path = NULL;
4862 int have_staged_files = 0;
4864 *commit_id = NULL;
4865 *new_base_branch = NULL;
4866 *tmp_branch = NULL;
4867 *branch = NULL;
4868 *fileindex = NULL;
4870 err = lock_worktree(worktree, LOCK_EX);
4871 if (err)
4872 return err;
4874 err = open_fileindex(fileindex, &fileindex_path, worktree);
4875 if (err)
4876 goto done;
4878 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4879 &have_staged_files);
4880 if (err && err->code != GOT_ERR_CANCELLED)
4881 goto done;
4882 if (have_staged_files) {
4883 err = got_error(GOT_ERR_STAGED_PATHS);
4884 goto done;
4887 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4888 if (err)
4889 goto done;
4891 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4892 if (err)
4893 goto done;
4895 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4896 if (err)
4897 goto done;
4899 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4900 if (err)
4901 goto done;
4903 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4904 if (err)
4905 goto done;
4907 err = got_ref_open(branch, repo,
4908 got_ref_get_symref_target(branch_ref), 0);
4909 if (err)
4910 goto done;
4912 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4913 if (err)
4914 goto done;
4916 err = got_ref_resolve(commit_id, repo, commit_ref);
4917 if (err)
4918 goto done;
4920 err = got_ref_open(new_base_branch, repo,
4921 new_base_branch_ref_name, 0);
4922 if (err)
4923 goto done;
4925 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4926 if (err)
4927 goto done;
4928 done:
4929 free(commit_ref_name);
4930 free(branch_ref_name);
4931 free(fileindex_path);
4932 if (commit_ref)
4933 got_ref_close(commit_ref);
4934 if (branch_ref)
4935 got_ref_close(branch_ref);
4936 if (err) {
4937 free(*commit_id);
4938 *commit_id = NULL;
4939 if (*tmp_branch) {
4940 got_ref_close(*tmp_branch);
4941 *tmp_branch = NULL;
4943 if (*new_base_branch) {
4944 got_ref_close(*new_base_branch);
4945 *new_base_branch = NULL;
4947 if (*branch) {
4948 got_ref_close(*branch);
4949 *branch = NULL;
4951 if (*fileindex) {
4952 got_fileindex_free(*fileindex);
4953 *fileindex = NULL;
4955 lock_worktree(worktree, LOCK_SH);
4957 return err;
4960 const struct got_error *
4961 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4963 const struct got_error *err;
4964 char *tmp_branch_name = NULL;
4966 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4967 if (err)
4968 return err;
4970 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4971 free(tmp_branch_name);
4972 return NULL;
4975 static const struct got_error *
4976 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4977 char **logmsg, void *arg)
4979 *logmsg = arg;
4980 return NULL;
4983 static const struct got_error *
4984 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4985 const char *path, struct got_object_id *blob_id,
4986 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4987 int dirfd, const char *de_name)
4989 return NULL;
4992 struct collect_merged_paths_arg {
4993 got_worktree_checkout_cb progress_cb;
4994 void *progress_arg;
4995 struct got_pathlist_head *merged_paths;
4998 static const struct got_error *
4999 collect_merged_paths(void *arg, unsigned char status, const char *path)
5001 const struct got_error *err;
5002 struct collect_merged_paths_arg *a = arg;
5003 char *p;
5004 struct got_pathlist_entry *new;
5006 err = (*a->progress_cb)(a->progress_arg, status, path);
5007 if (err)
5008 return err;
5010 if (status != GOT_STATUS_MERGE &&
5011 status != GOT_STATUS_ADD &&
5012 status != GOT_STATUS_DELETE &&
5013 status != GOT_STATUS_CONFLICT)
5014 return NULL;
5016 p = strdup(path);
5017 if (p == NULL)
5018 return got_error_from_errno("strdup");
5020 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5021 if (err || new == NULL)
5022 free(p);
5023 return err;
5026 void
5027 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5029 struct got_pathlist_entry *pe;
5031 TAILQ_FOREACH(pe, merged_paths, entry)
5032 free((char *)pe->path);
5034 got_pathlist_free(merged_paths);
5037 static const struct got_error *
5038 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5039 int is_rebase, struct got_repository *repo)
5041 const struct got_error *err;
5042 struct got_reference *commit_ref = NULL;
5044 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5045 if (err) {
5046 if (err->code != GOT_ERR_NOT_REF)
5047 goto done;
5048 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5049 if (err)
5050 goto done;
5051 err = got_ref_write(commit_ref, repo);
5052 if (err)
5053 goto done;
5054 } else if (is_rebase) {
5055 struct got_object_id *stored_id;
5056 int cmp;
5058 err = got_ref_resolve(&stored_id, repo, commit_ref);
5059 if (err)
5060 goto done;
5061 cmp = got_object_id_cmp(commit_id, stored_id);
5062 free(stored_id);
5063 if (cmp != 0) {
5064 err = got_error(GOT_ERR_REBASE_COMMITID);
5065 goto done;
5068 done:
5069 if (commit_ref)
5070 got_ref_close(commit_ref);
5071 return err;
5074 static const struct got_error *
5075 rebase_merge_files(struct got_pathlist_head *merged_paths,
5076 const char *commit_ref_name, struct got_worktree *worktree,
5077 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5078 struct got_object_id *commit_id, struct got_repository *repo,
5079 got_worktree_checkout_cb progress_cb, void *progress_arg,
5080 got_cancel_cb cancel_cb, void *cancel_arg)
5082 const struct got_error *err;
5083 struct got_reference *commit_ref = NULL;
5084 struct collect_merged_paths_arg cmp_arg;
5085 char *fileindex_path;
5087 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5089 err = get_fileindex_path(&fileindex_path, worktree);
5090 if (err)
5091 return err;
5093 cmp_arg.progress_cb = progress_cb;
5094 cmp_arg.progress_arg = progress_arg;
5095 cmp_arg.merged_paths = merged_paths;
5096 err = merge_files(worktree, fileindex, fileindex_path,
5097 parent_commit_id, commit_id, repo, collect_merged_paths,
5098 &cmp_arg, cancel_cb, cancel_arg);
5099 if (commit_ref)
5100 got_ref_close(commit_ref);
5101 return err;
5104 const struct got_error *
5105 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5106 struct got_worktree *worktree, struct got_fileindex *fileindex,
5107 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5108 struct got_repository *repo,
5109 got_worktree_checkout_cb progress_cb, void *progress_arg,
5110 got_cancel_cb cancel_cb, void *cancel_arg)
5112 const struct got_error *err;
5113 char *commit_ref_name;
5115 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5116 if (err)
5117 return err;
5119 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5120 if (err)
5121 goto done;
5123 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5124 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5125 progress_arg, cancel_cb, cancel_arg);
5126 done:
5127 free(commit_ref_name);
5128 return err;
5131 const struct got_error *
5132 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5133 struct got_worktree *worktree, struct got_fileindex *fileindex,
5134 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5135 struct got_repository *repo,
5136 got_worktree_checkout_cb progress_cb, void *progress_arg,
5137 got_cancel_cb cancel_cb, void *cancel_arg)
5139 const struct got_error *err;
5140 char *commit_ref_name;
5142 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5143 if (err)
5144 return err;
5146 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5147 if (err)
5148 goto done;
5150 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5151 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5152 progress_arg, cancel_cb, cancel_arg);
5153 done:
5154 free(commit_ref_name);
5155 return err;
5158 static const struct got_error *
5159 rebase_commit(struct got_object_id **new_commit_id,
5160 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5161 struct got_worktree *worktree, struct got_fileindex *fileindex,
5162 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5163 const char *new_logmsg, struct got_repository *repo)
5165 const struct got_error *err, *sync_err;
5166 struct got_pathlist_head commitable_paths;
5167 struct collect_commitables_arg cc_arg;
5168 char *fileindex_path = NULL;
5169 struct got_reference *head_ref = NULL;
5170 struct got_object_id *head_commit_id = NULL;
5171 char *logmsg = NULL;
5173 TAILQ_INIT(&commitable_paths);
5174 *new_commit_id = NULL;
5176 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5178 err = get_fileindex_path(&fileindex_path, worktree);
5179 if (err)
5180 return err;
5182 cc_arg.commitable_paths = &commitable_paths;
5183 cc_arg.worktree = worktree;
5184 cc_arg.repo = repo;
5185 cc_arg.have_staged_files = 0;
5187 * If possible get the status of individual files directly to
5188 * avoid crawling the entire work tree once per rebased commit.
5189 * TODO: Ideally, merged_paths would contain a list of commitables
5190 * we could use so we could skip worktree_status() entirely.
5192 if (merged_paths) {
5193 struct got_pathlist_entry *pe;
5194 TAILQ_FOREACH(pe, merged_paths, entry) {
5195 err = worktree_status(worktree, pe->path, fileindex,
5196 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5197 0);
5198 if (err)
5199 goto done;
5201 } else {
5202 err = worktree_status(worktree, "", fileindex, repo,
5203 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5204 if (err)
5205 goto done;
5208 if (TAILQ_EMPTY(&commitable_paths)) {
5209 /* No-op change; commit will be elided. */
5210 err = got_ref_delete(commit_ref, repo);
5211 if (err)
5212 goto done;
5213 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5214 goto done;
5217 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5218 if (err)
5219 goto done;
5221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5222 if (err)
5223 goto done;
5225 if (new_logmsg) {
5226 logmsg = strdup(new_logmsg);
5227 if (logmsg == NULL) {
5228 err = got_error_from_errno("strdup");
5229 goto done;
5231 } else {
5232 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5233 if (err)
5234 goto done;
5237 /* NB: commit_worktree will call free(logmsg) */
5238 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5239 worktree, got_object_commit_get_author(orig_commit),
5240 got_object_commit_get_committer(orig_commit),
5241 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5242 if (err)
5243 goto done;
5245 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5246 if (err)
5247 goto done;
5249 err = got_ref_delete(commit_ref, repo);
5250 if (err)
5251 goto done;
5253 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5254 fileindex, 0);
5255 sync_err = sync_fileindex(fileindex, fileindex_path);
5256 if (sync_err && err == NULL)
5257 err = sync_err;
5258 done:
5259 free(fileindex_path);
5260 free(head_commit_id);
5261 if (head_ref)
5262 got_ref_close(head_ref);
5263 if (err) {
5264 free(*new_commit_id);
5265 *new_commit_id = NULL;
5267 return err;
5270 const struct got_error *
5271 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5272 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5273 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5274 struct got_commit_object *orig_commit,
5275 struct got_object_id *orig_commit_id, struct got_repository *repo)
5277 const struct got_error *err;
5278 char *commit_ref_name;
5279 struct got_reference *commit_ref = NULL;
5280 struct got_object_id *commit_id = NULL;
5282 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5283 if (err)
5284 return err;
5286 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5287 if (err)
5288 goto done;
5289 err = got_ref_resolve(&commit_id, repo, commit_ref);
5290 if (err)
5291 goto done;
5292 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5293 err = got_error(GOT_ERR_REBASE_COMMITID);
5294 goto done;
5297 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5298 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5299 done:
5300 if (commit_ref)
5301 got_ref_close(commit_ref);
5302 free(commit_ref_name);
5303 free(commit_id);
5304 return err;
5307 const struct got_error *
5308 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5309 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5310 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5311 struct got_commit_object *orig_commit,
5312 struct got_object_id *orig_commit_id, const char *new_logmsg,
5313 struct got_repository *repo)
5315 const struct got_error *err;
5316 char *commit_ref_name;
5317 struct got_reference *commit_ref = NULL;
5319 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5320 if (err)
5321 return err;
5323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5324 if (err)
5325 goto done;
5327 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5328 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5329 done:
5330 if (commit_ref)
5331 got_ref_close(commit_ref);
5332 free(commit_ref_name);
5333 return err;
5336 const struct got_error *
5337 got_worktree_rebase_postpone(struct got_worktree *worktree,
5338 struct got_fileindex *fileindex)
5340 if (fileindex)
5341 got_fileindex_free(fileindex);
5342 return lock_worktree(worktree, LOCK_SH);
5345 static const struct got_error *
5346 delete_ref(const char *name, struct got_repository *repo)
5348 const struct got_error *err;
5349 struct got_reference *ref;
5351 err = got_ref_open(&ref, repo, name, 0);
5352 if (err) {
5353 if (err->code == GOT_ERR_NOT_REF)
5354 return NULL;
5355 return err;
5358 err = got_ref_delete(ref, repo);
5359 got_ref_close(ref);
5360 return err;
5363 static const struct got_error *
5364 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5366 const struct got_error *err;
5367 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5368 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5370 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5371 if (err)
5372 goto done;
5373 err = delete_ref(tmp_branch_name, repo);
5374 if (err)
5375 goto done;
5377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5378 if (err)
5379 goto done;
5380 err = delete_ref(new_base_branch_ref_name, repo);
5381 if (err)
5382 goto done;
5384 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5385 if (err)
5386 goto done;
5387 err = delete_ref(branch_ref_name, repo);
5388 if (err)
5389 goto done;
5391 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5392 if (err)
5393 goto done;
5394 err = delete_ref(commit_ref_name, repo);
5395 if (err)
5396 goto done;
5398 done:
5399 free(tmp_branch_name);
5400 free(new_base_branch_ref_name);
5401 free(branch_ref_name);
5402 free(commit_ref_name);
5403 return err;
5406 const struct got_error *
5407 got_worktree_rebase_complete(struct got_worktree *worktree,
5408 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5409 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5410 struct got_repository *repo)
5412 const struct got_error *err, *unlockerr;
5413 struct got_object_id *new_head_commit_id = NULL;
5415 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5416 if (err)
5417 return err;
5419 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5420 if (err)
5421 goto done;
5423 err = got_ref_write(rebased_branch, repo);
5424 if (err)
5425 goto done;
5427 err = got_worktree_set_head_ref(worktree, rebased_branch);
5428 if (err)
5429 goto done;
5431 err = delete_rebase_refs(worktree, repo);
5432 done:
5433 if (fileindex)
5434 got_fileindex_free(fileindex);
5435 free(new_head_commit_id);
5436 unlockerr = lock_worktree(worktree, LOCK_SH);
5437 if (unlockerr && err == NULL)
5438 err = unlockerr;
5439 return err;
5442 const struct got_error *
5443 got_worktree_rebase_abort(struct got_worktree *worktree,
5444 struct got_fileindex *fileindex, struct got_repository *repo,
5445 struct got_reference *new_base_branch,
5446 got_worktree_checkout_cb progress_cb, void *progress_arg)
5448 const struct got_error *err, *unlockerr, *sync_err;
5449 struct got_reference *resolved = NULL;
5450 struct got_object_id *commit_id = NULL;
5451 char *fileindex_path = NULL;
5452 struct revert_file_args rfa;
5453 struct got_object_id *tree_id = NULL;
5455 err = lock_worktree(worktree, LOCK_EX);
5456 if (err)
5457 return err;
5459 err = got_ref_open(&resolved, repo,
5460 got_ref_get_symref_target(new_base_branch), 0);
5461 if (err)
5462 goto done;
5464 err = got_worktree_set_head_ref(worktree, resolved);
5465 if (err)
5466 goto done;
5469 * XXX commits to the base branch could have happened while
5470 * we were busy rebasing; should we store the original commit ID
5471 * when rebase begins and read it back here?
5473 err = got_ref_resolve(&commit_id, repo, resolved);
5474 if (err)
5475 goto done;
5477 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5478 if (err)
5479 goto done;
5481 err = got_object_id_by_path(&tree_id, repo,
5482 worktree->base_commit_id, worktree->path_prefix);
5483 if (err)
5484 goto done;
5486 err = delete_rebase_refs(worktree, repo);
5487 if (err)
5488 goto done;
5490 err = get_fileindex_path(&fileindex_path, worktree);
5491 if (err)
5492 goto done;
5494 rfa.worktree = worktree;
5495 rfa.fileindex = fileindex;
5496 rfa.progress_cb = progress_cb;
5497 rfa.progress_arg = progress_arg;
5498 rfa.patch_cb = NULL;
5499 rfa.patch_arg = NULL;
5500 rfa.repo = repo;
5501 err = worktree_status(worktree, "", fileindex, repo,
5502 revert_file, &rfa, NULL, NULL, 0, 0);
5503 if (err)
5504 goto sync;
5506 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5507 repo, progress_cb, progress_arg, NULL, NULL);
5508 sync:
5509 sync_err = sync_fileindex(fileindex, fileindex_path);
5510 if (sync_err && err == NULL)
5511 err = sync_err;
5512 done:
5513 got_ref_close(resolved);
5514 free(tree_id);
5515 free(commit_id);
5516 if (fileindex)
5517 got_fileindex_free(fileindex);
5518 free(fileindex_path);
5520 unlockerr = lock_worktree(worktree, LOCK_SH);
5521 if (unlockerr && err == NULL)
5522 err = unlockerr;
5523 return err;
5526 const struct got_error *
5527 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5528 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5529 struct got_fileindex **fileindex, struct got_worktree *worktree,
5530 struct got_repository *repo)
5532 const struct got_error *err = NULL;
5533 char *tmp_branch_name = NULL;
5534 char *branch_ref_name = NULL;
5535 char *base_commit_ref_name = NULL;
5536 char *fileindex_path = NULL;
5537 struct check_rebase_ok_arg ok_arg;
5538 struct got_reference *wt_branch = NULL;
5539 struct got_reference *base_commit_ref = NULL;
5541 *tmp_branch = NULL;
5542 *branch_ref = NULL;
5543 *base_commit_id = NULL;
5544 *fileindex = NULL;
5546 err = lock_worktree(worktree, LOCK_EX);
5547 if (err)
5548 return err;
5550 err = open_fileindex(fileindex, &fileindex_path, worktree);
5551 if (err)
5552 goto done;
5554 ok_arg.worktree = worktree;
5555 ok_arg.repo = repo;
5556 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5557 &ok_arg);
5558 if (err)
5559 goto done;
5561 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5562 if (err)
5563 goto done;
5565 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5566 if (err)
5567 goto done;
5569 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5570 worktree);
5571 if (err)
5572 goto done;
5574 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5575 0);
5576 if (err)
5577 goto done;
5579 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5580 if (err)
5581 goto done;
5583 err = got_ref_write(*branch_ref, repo);
5584 if (err)
5585 goto done;
5587 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5588 worktree->base_commit_id);
5589 if (err)
5590 goto done;
5591 err = got_ref_write(base_commit_ref, repo);
5592 if (err)
5593 goto done;
5594 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5595 if (*base_commit_id == NULL) {
5596 err = got_error_from_errno("got_object_id_dup");
5597 goto done;
5600 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5601 worktree->base_commit_id);
5602 if (err)
5603 goto done;
5604 err = got_ref_write(*tmp_branch, repo);
5605 if (err)
5606 goto done;
5608 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5609 if (err)
5610 goto done;
5611 done:
5612 free(fileindex_path);
5613 free(tmp_branch_name);
5614 free(branch_ref_name);
5615 free(base_commit_ref_name);
5616 if (wt_branch)
5617 got_ref_close(wt_branch);
5618 if (err) {
5619 if (*branch_ref) {
5620 got_ref_close(*branch_ref);
5621 *branch_ref = NULL;
5623 if (*tmp_branch) {
5624 got_ref_close(*tmp_branch);
5625 *tmp_branch = NULL;
5627 free(*base_commit_id);
5628 if (*fileindex) {
5629 got_fileindex_free(*fileindex);
5630 *fileindex = NULL;
5632 lock_worktree(worktree, LOCK_SH);
5634 return err;
5637 const struct got_error *
5638 got_worktree_histedit_postpone(struct got_worktree *worktree,
5639 struct got_fileindex *fileindex)
5641 if (fileindex)
5642 got_fileindex_free(fileindex);
5643 return lock_worktree(worktree, LOCK_SH);
5646 const struct got_error *
5647 got_worktree_histedit_in_progress(int *in_progress,
5648 struct got_worktree *worktree)
5650 const struct got_error *err;
5651 char *tmp_branch_name = NULL;
5653 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5654 if (err)
5655 return err;
5657 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5658 free(tmp_branch_name);
5659 return NULL;
5662 const struct got_error *
5663 got_worktree_histedit_continue(struct got_object_id **commit_id,
5664 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5665 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5666 struct got_worktree *worktree, struct got_repository *repo)
5668 const struct got_error *err;
5669 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5670 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5671 struct got_reference *commit_ref = NULL;
5672 struct got_reference *base_commit_ref = NULL;
5673 char *fileindex_path = NULL;
5674 int have_staged_files = 0;
5676 *commit_id = NULL;
5677 *tmp_branch = NULL;
5678 *base_commit_id = NULL;
5679 *fileindex = NULL;
5681 err = lock_worktree(worktree, LOCK_EX);
5682 if (err)
5683 return err;
5685 err = open_fileindex(fileindex, &fileindex_path, worktree);
5686 if (err)
5687 goto done;
5689 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5690 &have_staged_files);
5691 if (err && err->code != GOT_ERR_CANCELLED)
5692 goto done;
5693 if (have_staged_files) {
5694 err = got_error(GOT_ERR_STAGED_PATHS);
5695 goto done;
5698 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5699 if (err)
5700 goto done;
5702 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5703 if (err)
5704 goto done;
5706 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5707 if (err)
5708 goto done;
5710 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5711 worktree);
5712 if (err)
5713 goto done;
5715 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5716 if (err)
5717 goto done;
5719 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5720 if (err)
5721 goto done;
5722 err = got_ref_resolve(commit_id, repo, commit_ref);
5723 if (err)
5724 goto done;
5726 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5727 if (err)
5728 goto done;
5729 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5730 if (err)
5731 goto done;
5733 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5734 if (err)
5735 goto done;
5736 done:
5737 free(commit_ref_name);
5738 free(branch_ref_name);
5739 free(fileindex_path);
5740 if (commit_ref)
5741 got_ref_close(commit_ref);
5742 if (base_commit_ref)
5743 got_ref_close(base_commit_ref);
5744 if (err) {
5745 free(*commit_id);
5746 *commit_id = NULL;
5747 free(*base_commit_id);
5748 *base_commit_id = NULL;
5749 if (*tmp_branch) {
5750 got_ref_close(*tmp_branch);
5751 *tmp_branch = NULL;
5753 if (*fileindex) {
5754 got_fileindex_free(*fileindex);
5755 *fileindex = NULL;
5757 lock_worktree(worktree, LOCK_EX);
5759 return err;
5762 static const struct got_error *
5763 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5765 const struct got_error *err;
5766 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5767 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5769 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5770 if (err)
5771 goto done;
5772 err = delete_ref(tmp_branch_name, repo);
5773 if (err)
5774 goto done;
5776 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5777 worktree);
5778 if (err)
5779 goto done;
5780 err = delete_ref(base_commit_ref_name, repo);
5781 if (err)
5782 goto done;
5784 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5785 if (err)
5786 goto done;
5787 err = delete_ref(branch_ref_name, repo);
5788 if (err)
5789 goto done;
5791 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5792 if (err)
5793 goto done;
5794 err = delete_ref(commit_ref_name, repo);
5795 if (err)
5796 goto done;
5797 done:
5798 free(tmp_branch_name);
5799 free(base_commit_ref_name);
5800 free(branch_ref_name);
5801 free(commit_ref_name);
5802 return err;
5805 const struct got_error *
5806 got_worktree_histedit_abort(struct got_worktree *worktree,
5807 struct got_fileindex *fileindex, struct got_repository *repo,
5808 struct got_reference *branch, struct got_object_id *base_commit_id,
5809 got_worktree_checkout_cb progress_cb, void *progress_arg)
5811 const struct got_error *err, *unlockerr, *sync_err;
5812 struct got_reference *resolved = NULL;
5813 char *fileindex_path = NULL;
5814 struct got_object_id *tree_id = NULL;
5815 struct revert_file_args rfa;
5817 err = lock_worktree(worktree, LOCK_EX);
5818 if (err)
5819 return err;
5821 err = got_ref_open(&resolved, repo,
5822 got_ref_get_symref_target(branch), 0);
5823 if (err)
5824 goto done;
5826 err = got_worktree_set_head_ref(worktree, resolved);
5827 if (err)
5828 goto done;
5830 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5831 if (err)
5832 goto done;
5834 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5835 worktree->path_prefix);
5836 if (err)
5837 goto done;
5839 err = delete_histedit_refs(worktree, repo);
5840 if (err)
5841 goto done;
5843 err = get_fileindex_path(&fileindex_path, worktree);
5844 if (err)
5845 goto done;
5847 rfa.worktree = worktree;
5848 rfa.fileindex = fileindex;
5849 rfa.progress_cb = progress_cb;
5850 rfa.progress_arg = progress_arg;
5851 rfa.patch_cb = NULL;
5852 rfa.patch_arg = NULL;
5853 rfa.repo = repo;
5854 err = worktree_status(worktree, "", fileindex, repo,
5855 revert_file, &rfa, NULL, NULL, 0, 0);
5856 if (err)
5857 goto sync;
5859 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5860 repo, progress_cb, progress_arg, NULL, NULL);
5861 sync:
5862 sync_err = sync_fileindex(fileindex, fileindex_path);
5863 if (sync_err && err == NULL)
5864 err = sync_err;
5865 done:
5866 got_ref_close(resolved);
5867 free(tree_id);
5868 free(fileindex_path);
5870 unlockerr = lock_worktree(worktree, LOCK_SH);
5871 if (unlockerr && err == NULL)
5872 err = unlockerr;
5873 return err;
5876 const struct got_error *
5877 got_worktree_histedit_complete(struct got_worktree *worktree,
5878 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5879 struct got_reference *edited_branch, struct got_repository *repo)
5881 const struct got_error *err, *unlockerr;
5882 struct got_object_id *new_head_commit_id = NULL;
5883 struct got_reference *resolved = NULL;
5885 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5886 if (err)
5887 return err;
5889 err = got_ref_open(&resolved, repo,
5890 got_ref_get_symref_target(edited_branch), 0);
5891 if (err)
5892 goto done;
5894 err = got_ref_change_ref(resolved, new_head_commit_id);
5895 if (err)
5896 goto done;
5898 err = got_ref_write(resolved, repo);
5899 if (err)
5900 goto done;
5902 err = got_worktree_set_head_ref(worktree, resolved);
5903 if (err)
5904 goto done;
5906 err = delete_histedit_refs(worktree, repo);
5907 done:
5908 if (fileindex)
5909 got_fileindex_free(fileindex);
5910 free(new_head_commit_id);
5911 unlockerr = lock_worktree(worktree, LOCK_SH);
5912 if (unlockerr && err == NULL)
5913 err = unlockerr;
5914 return err;
5917 const struct got_error *
5918 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5919 struct got_object_id *commit_id, struct got_repository *repo)
5921 const struct got_error *err;
5922 char *commit_ref_name;
5924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5925 if (err)
5926 return err;
5928 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5929 if (err)
5930 goto done;
5932 err = delete_ref(commit_ref_name, repo);
5933 done:
5934 free(commit_ref_name);
5935 return err;
5938 const struct got_error *
5939 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5940 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5941 struct got_worktree *worktree, const char *refname,
5942 struct got_repository *repo)
5944 const struct got_error *err = NULL;
5945 char *fileindex_path = NULL;
5946 struct check_rebase_ok_arg ok_arg;
5948 *fileindex = NULL;
5949 *branch_ref = NULL;
5950 *base_branch_ref = NULL;
5952 err = lock_worktree(worktree, LOCK_EX);
5953 if (err)
5954 return err;
5956 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5957 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5958 "cannot integrate a branch into itself; "
5959 "update -b or different branch name required");
5960 goto done;
5963 err = open_fileindex(fileindex, &fileindex_path, worktree);
5964 if (err)
5965 goto done;
5967 /* Preconditions are the same as for rebase. */
5968 ok_arg.worktree = worktree;
5969 ok_arg.repo = repo;
5970 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5971 &ok_arg);
5972 if (err)
5973 goto done;
5975 err = got_ref_open(branch_ref, repo, refname, 1);
5976 if (err)
5977 goto done;
5979 err = got_ref_open(base_branch_ref, repo,
5980 got_worktree_get_head_ref_name(worktree), 1);
5981 done:
5982 if (err) {
5983 if (*branch_ref) {
5984 got_ref_close(*branch_ref);
5985 *branch_ref = NULL;
5987 if (*base_branch_ref) {
5988 got_ref_close(*base_branch_ref);
5989 *base_branch_ref = NULL;
5991 if (*fileindex) {
5992 got_fileindex_free(*fileindex);
5993 *fileindex = NULL;
5995 lock_worktree(worktree, LOCK_SH);
5997 return err;
6000 const struct got_error *
6001 got_worktree_integrate_continue(struct got_worktree *worktree,
6002 struct got_fileindex *fileindex, struct got_repository *repo,
6003 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6004 got_worktree_checkout_cb progress_cb, void *progress_arg,
6005 got_cancel_cb cancel_cb, void *cancel_arg)
6007 const struct got_error *err = NULL, *sync_err, *unlockerr;
6008 char *fileindex_path = NULL;
6009 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6011 err = get_fileindex_path(&fileindex_path, worktree);
6012 if (err)
6013 goto done;
6015 err = got_ref_resolve(&commit_id, repo, branch_ref);
6016 if (err)
6017 goto done;
6019 err = got_object_id_by_path(&tree_id, repo, commit_id,
6020 worktree->path_prefix);
6021 if (err)
6022 goto done;
6024 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6025 if (err)
6026 goto done;
6028 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6029 progress_cb, progress_arg, cancel_cb, cancel_arg);
6030 if (err)
6031 goto sync;
6033 err = got_ref_change_ref(base_branch_ref, commit_id);
6034 if (err)
6035 goto sync;
6037 err = got_ref_write(base_branch_ref, repo);
6038 sync:
6039 sync_err = sync_fileindex(fileindex, fileindex_path);
6040 if (sync_err && err == NULL)
6041 err = sync_err;
6043 done:
6044 unlockerr = got_ref_unlock(branch_ref);
6045 if (unlockerr && err == NULL)
6046 err = unlockerr;
6047 got_ref_close(branch_ref);
6049 unlockerr = got_ref_unlock(base_branch_ref);
6050 if (unlockerr && err == NULL)
6051 err = unlockerr;
6052 got_ref_close(base_branch_ref);
6054 got_fileindex_free(fileindex);
6055 free(fileindex_path);
6056 free(tree_id);
6058 unlockerr = lock_worktree(worktree, LOCK_SH);
6059 if (unlockerr && err == NULL)
6060 err = unlockerr;
6061 return err;
6064 const struct got_error *
6065 got_worktree_integrate_abort(struct got_worktree *worktree,
6066 struct got_fileindex *fileindex, struct got_repository *repo,
6067 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6069 const struct got_error *err = NULL, *unlockerr = NULL;
6071 got_fileindex_free(fileindex);
6073 err = lock_worktree(worktree, LOCK_SH);
6075 unlockerr = got_ref_unlock(branch_ref);
6076 if (unlockerr && err == NULL)
6077 err = unlockerr;
6078 got_ref_close(branch_ref);
6080 unlockerr = got_ref_unlock(base_branch_ref);
6081 if (unlockerr && err == NULL)
6082 err = unlockerr;
6083 got_ref_close(base_branch_ref);
6085 return err;
6088 struct check_stage_ok_arg {
6089 struct got_object_id *head_commit_id;
6090 struct got_worktree *worktree;
6091 struct got_fileindex *fileindex;
6092 struct got_repository *repo;
6093 int have_changes;
6096 const struct got_error *
6097 check_stage_ok(void *arg, unsigned char status,
6098 unsigned char staged_status, const char *relpath,
6099 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6100 struct got_object_id *commit_id, int dirfd, const char *de_name)
6102 struct check_stage_ok_arg *a = arg;
6103 const struct got_error *err = NULL;
6104 struct got_fileindex_entry *ie;
6105 struct got_object_id base_commit_id;
6106 struct got_object_id *base_commit_idp = NULL;
6107 char *in_repo_path = NULL, *p;
6109 if (status == GOT_STATUS_UNVERSIONED ||
6110 status == GOT_STATUS_NO_CHANGE)
6111 return NULL;
6112 if (status == GOT_STATUS_NONEXISTENT)
6113 return got_error_set_errno(ENOENT, relpath);
6115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6116 if (ie == NULL)
6117 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6119 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6120 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6121 relpath) == -1)
6122 return got_error_from_errno("asprintf");
6124 if (got_fileindex_entry_has_commit(ie)) {
6125 memcpy(base_commit_id.sha1, ie->commit_sha1,
6126 SHA1_DIGEST_LENGTH);
6127 base_commit_idp = &base_commit_id;
6130 if (status == GOT_STATUS_CONFLICT) {
6131 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6132 goto done;
6133 } else if (status != GOT_STATUS_ADD &&
6134 status != GOT_STATUS_MODIFY &&
6135 status != GOT_STATUS_DELETE) {
6136 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6137 goto done;
6140 a->have_changes = 1;
6142 p = in_repo_path;
6143 while (p[0] == '/')
6144 p++;
6145 err = check_out_of_date(p, status, staged_status,
6146 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6147 GOT_ERR_STAGE_OUT_OF_DATE);
6148 done:
6149 free(in_repo_path);
6150 return err;
6153 struct stage_path_arg {
6154 struct got_worktree *worktree;
6155 struct got_fileindex *fileindex;
6156 struct got_repository *repo;
6157 got_worktree_status_cb status_cb;
6158 void *status_arg;
6159 got_worktree_patch_cb patch_cb;
6160 void *patch_arg;
6161 int staged_something;
6164 static const struct got_error *
6165 stage_path(void *arg, unsigned char status,
6166 unsigned char staged_status, const char *relpath,
6167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6168 struct got_object_id *commit_id, int dirfd, const char *de_name)
6170 struct stage_path_arg *a = arg;
6171 const struct got_error *err = NULL;
6172 struct got_fileindex_entry *ie;
6173 char *ondisk_path = NULL, *path_content = NULL;
6174 uint32_t stage;
6175 struct got_object_id *new_staged_blob_id = NULL;
6177 if (status == GOT_STATUS_UNVERSIONED)
6178 return NULL;
6180 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6181 if (ie == NULL)
6182 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6184 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6185 relpath)== -1)
6186 return got_error_from_errno("asprintf");
6188 switch (status) {
6189 case GOT_STATUS_ADD:
6190 case GOT_STATUS_MODIFY:
6191 if (a->patch_cb) {
6192 if (status == GOT_STATUS_ADD) {
6193 int choice = GOT_PATCH_CHOICE_NONE;
6194 err = (*a->patch_cb)(&choice, a->patch_arg,
6195 status, ie->path, NULL, 1, 1);
6196 if (err)
6197 break;
6198 if (choice != GOT_PATCH_CHOICE_YES)
6199 break;
6200 } else {
6201 err = create_patched_content(&path_content, 0,
6202 staged_blob_id ? staged_blob_id : blob_id,
6203 ondisk_path, dirfd, de_name, ie->path,
6204 a->repo, a->patch_cb, a->patch_arg);
6205 if (err || path_content == NULL)
6206 break;
6209 err = got_object_blob_create(&new_staged_blob_id,
6210 path_content ? path_content : ondisk_path, a->repo);
6211 if (err)
6212 break;
6213 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6214 SHA1_DIGEST_LENGTH);
6215 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6216 stage = GOT_FILEIDX_STAGE_ADD;
6217 else
6218 stage = GOT_FILEIDX_STAGE_MODIFY;
6219 got_fileindex_entry_stage_set(ie, stage);
6220 a->staged_something = 1;
6221 if (a->status_cb == NULL)
6222 break;
6223 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6224 get_staged_status(ie), relpath, blob_id,
6225 new_staged_blob_id, NULL, dirfd, de_name);
6226 break;
6227 case GOT_STATUS_DELETE:
6228 if (staged_status == GOT_STATUS_DELETE)
6229 break;
6230 if (a->patch_cb) {
6231 int choice = GOT_PATCH_CHOICE_NONE;
6232 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6233 ie->path, NULL, 1, 1);
6234 if (err)
6235 break;
6236 if (choice == GOT_PATCH_CHOICE_NO)
6237 break;
6238 if (choice != GOT_PATCH_CHOICE_YES) {
6239 err = got_error(GOT_ERR_PATCH_CHOICE);
6240 break;
6243 stage = GOT_FILEIDX_STAGE_DELETE;
6244 got_fileindex_entry_stage_set(ie, stage);
6245 a->staged_something = 1;
6246 if (a->status_cb == NULL)
6247 break;
6248 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6249 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6250 de_name);
6251 break;
6252 case GOT_STATUS_NO_CHANGE:
6253 break;
6254 case GOT_STATUS_CONFLICT:
6255 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6256 break;
6257 case GOT_STATUS_NONEXISTENT:
6258 err = got_error_set_errno(ENOENT, relpath);
6259 break;
6260 default:
6261 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6262 break;
6265 if (path_content && unlink(path_content) == -1 && err == NULL)
6266 err = got_error_from_errno2("unlink", path_content);
6267 free(path_content);
6268 free(ondisk_path);
6269 free(new_staged_blob_id);
6270 return err;
6273 const struct got_error *
6274 got_worktree_stage(struct got_worktree *worktree,
6275 struct got_pathlist_head *paths,
6276 got_worktree_status_cb status_cb, void *status_arg,
6277 got_worktree_patch_cb patch_cb, void *patch_arg,
6278 struct got_repository *repo)
6280 const struct got_error *err = NULL, *sync_err, *unlockerr;
6281 struct got_pathlist_entry *pe;
6282 struct got_fileindex *fileindex = NULL;
6283 char *fileindex_path = NULL;
6284 struct got_reference *head_ref = NULL;
6285 struct got_object_id *head_commit_id = NULL;
6286 struct check_stage_ok_arg oka;
6287 struct stage_path_arg spa;
6289 err = lock_worktree(worktree, LOCK_EX);
6290 if (err)
6291 return err;
6293 err = got_ref_open(&head_ref, repo,
6294 got_worktree_get_head_ref_name(worktree), 0);
6295 if (err)
6296 goto done;
6297 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6298 if (err)
6299 goto done;
6300 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6301 if (err)
6302 goto done;
6304 /* Check pre-conditions before staging anything. */
6305 oka.head_commit_id = head_commit_id;
6306 oka.worktree = worktree;
6307 oka.fileindex = fileindex;
6308 oka.repo = repo;
6309 oka.have_changes = 0;
6310 TAILQ_FOREACH(pe, paths, entry) {
6311 err = worktree_status(worktree, pe->path, fileindex, repo,
6312 check_stage_ok, &oka, NULL, NULL, 0, 0);
6313 if (err)
6314 goto done;
6316 if (!oka.have_changes) {
6317 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6318 goto done;
6321 spa.worktree = worktree;
6322 spa.fileindex = fileindex;
6323 spa.repo = repo;
6324 spa.patch_cb = patch_cb;
6325 spa.patch_arg = patch_arg;
6326 spa.status_cb = status_cb;
6327 spa.status_arg = status_arg;
6328 spa.staged_something = 0;
6329 TAILQ_FOREACH(pe, paths, entry) {
6330 err = worktree_status(worktree, pe->path, fileindex, repo,
6331 stage_path, &spa, NULL, NULL, 0, 0);
6332 if (err)
6333 goto done;
6335 if (!spa.staged_something) {
6336 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6337 goto done;
6340 sync_err = sync_fileindex(fileindex, fileindex_path);
6341 if (sync_err && err == NULL)
6342 err = sync_err;
6343 done:
6344 if (head_ref)
6345 got_ref_close(head_ref);
6346 free(head_commit_id);
6347 free(fileindex_path);
6348 if (fileindex)
6349 got_fileindex_free(fileindex);
6350 unlockerr = lock_worktree(worktree, LOCK_SH);
6351 if (unlockerr && err == NULL)
6352 err = unlockerr;
6353 return err;
6356 struct unstage_path_arg {
6357 struct got_worktree *worktree;
6358 struct got_fileindex *fileindex;
6359 struct got_repository *repo;
6360 got_worktree_checkout_cb progress_cb;
6361 void *progress_arg;
6362 got_worktree_patch_cb patch_cb;
6363 void *patch_arg;
6366 static const struct got_error *
6367 create_unstaged_content(char **path_unstaged_content,
6368 char **path_new_staged_content, struct got_object_id *blob_id,
6369 struct got_object_id *staged_blob_id, const char *relpath,
6370 struct got_repository *repo,
6371 got_worktree_patch_cb patch_cb, void *patch_arg)
6373 const struct got_error *err;
6374 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6375 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6376 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6377 struct stat sb1, sb2;
6378 struct got_diff_changes *changes = NULL;
6379 struct got_diff_state *ds = NULL;
6380 struct got_diff_args *args = NULL;
6381 struct got_diff_change *change;
6382 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6383 int have_content = 0, have_rejected_content = 0;
6385 *path_unstaged_content = NULL;
6386 *path_new_staged_content = NULL;
6388 err = got_object_id_str(&label1, blob_id);
6389 if (err)
6390 return err;
6391 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6392 if (err)
6393 goto done;
6395 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6396 if (err)
6397 goto done;
6399 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6400 if (err)
6401 goto done;
6403 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6404 if (err)
6405 goto done;
6407 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6408 if (err)
6409 goto done;
6411 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6412 if (err)
6413 goto done;
6415 if (stat(path1, &sb1) == -1) {
6416 err = got_error_from_errno2("stat", path1);
6417 goto done;
6420 if (stat(path2, &sb2) == -1) {
6421 err = got_error_from_errno2("stat", path2);
6422 goto done;
6425 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6426 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6427 if (err)
6428 goto done;
6430 err = got_opentemp_named(path_unstaged_content, &outfile,
6431 "got-unstaged-content");
6432 if (err)
6433 goto done;
6434 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6435 "got-new-staged-content");
6436 if (err)
6437 goto done;
6439 if (fseek(f1, 0L, SEEK_SET) == -1) {
6440 err = got_ferror(f1, GOT_ERR_IO);
6441 goto done;
6443 if (fseek(f2, 0L, SEEK_SET) == -1) {
6444 err = got_ferror(f2, GOT_ERR_IO);
6445 goto done;
6447 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6448 int choice;
6449 err = apply_or_reject_change(&choice, change, ++n,
6450 changes->nchanges, ds, args, diff_flags, relpath,
6451 f1, f2, &line_cur1, &line_cur2,
6452 outfile, rejectfile, patch_cb, patch_arg);
6453 if (err)
6454 goto done;
6455 if (choice == GOT_PATCH_CHOICE_YES)
6456 have_content = 1;
6457 else
6458 have_rejected_content = 1;
6459 if (choice == GOT_PATCH_CHOICE_QUIT)
6460 break;
6462 if (have_content || have_rejected_content)
6463 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6464 outfile, rejectfile);
6465 done:
6466 free(label1);
6467 if (blob)
6468 got_object_blob_close(blob);
6469 if (staged_blob)
6470 got_object_blob_close(staged_blob);
6471 if (f1 && fclose(f1) == EOF && err == NULL)
6472 err = got_error_from_errno2("fclose", path1);
6473 if (f2 && fclose(f2) == EOF && err == NULL)
6474 err = got_error_from_errno2("fclose", path2);
6475 if (outfile && fclose(outfile) == EOF && err == NULL)
6476 err = got_error_from_errno2("fclose", *path_unstaged_content);
6477 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6478 err = got_error_from_errno2("fclose", *path_new_staged_content);
6479 if (path1 && unlink(path1) == -1 && err == NULL)
6480 err = got_error_from_errno2("unlink", path1);
6481 if (path2 && unlink(path2) == -1 && err == NULL)
6482 err = got_error_from_errno2("unlink", path2);
6483 if (err || !have_content) {
6484 if (*path_unstaged_content &&
6485 unlink(*path_unstaged_content) == -1 && err == NULL)
6486 err = got_error_from_errno2("unlink",
6487 *path_unstaged_content);
6488 free(*path_unstaged_content);
6489 *path_unstaged_content = NULL;
6491 if (err || !have_rejected_content) {
6492 if (*path_new_staged_content &&
6493 unlink(*path_new_staged_content) == -1 && err == NULL)
6494 err = got_error_from_errno2("unlink",
6495 *path_new_staged_content);
6496 free(*path_new_staged_content);
6497 *path_new_staged_content = NULL;
6499 free(args);
6500 if (ds) {
6501 got_diff_state_free(ds);
6502 free(ds);
6504 if (changes)
6505 got_diff_free_changes(changes);
6506 free(path1);
6507 free(path2);
6508 return err;
6511 static const struct got_error *
6512 unstage_path(void *arg, unsigned char status,
6513 unsigned char staged_status, const char *relpath,
6514 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6515 struct got_object_id *commit_id, int dirfd, const char *de_name)
6517 const struct got_error *err = NULL;
6518 struct unstage_path_arg *a = arg;
6519 struct got_fileindex_entry *ie;
6520 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6521 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6522 char *path_new_staged_content = NULL;
6523 char *id_str = NULL, *label_orig = NULL;
6524 int local_changes_subsumed;
6525 struct stat sb;
6527 if (staged_status != GOT_STATUS_ADD &&
6528 staged_status != GOT_STATUS_MODIFY &&
6529 staged_status != GOT_STATUS_DELETE)
6530 return NULL;
6532 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6533 if (ie == NULL)
6534 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6536 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6537 == -1)
6538 return got_error_from_errno("asprintf");
6540 err = got_object_id_str(&id_str,
6541 commit_id ? commit_id : a->worktree->base_commit_id);
6542 if (err)
6543 goto done;
6544 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6545 id_str) == -1) {
6546 err = got_error_from_errno("asprintf");
6547 goto done;
6550 switch (staged_status) {
6551 case GOT_STATUS_MODIFY:
6552 err = got_object_open_as_blob(&blob_base, a->repo,
6553 blob_id, 8192);
6554 if (err)
6555 break;
6556 /* fall through */
6557 case GOT_STATUS_ADD:
6558 if (a->patch_cb) {
6559 if (staged_status == GOT_STATUS_ADD) {
6560 int choice = GOT_PATCH_CHOICE_NONE;
6561 err = (*a->patch_cb)(&choice, a->patch_arg,
6562 staged_status, ie->path, NULL, 1, 1);
6563 if (err)
6564 break;
6565 if (choice != GOT_PATCH_CHOICE_YES)
6566 break;
6567 } else {
6568 err = create_unstaged_content(
6569 &path_unstaged_content,
6570 &path_new_staged_content, blob_id,
6571 staged_blob_id, ie->path, a->repo,
6572 a->patch_cb, a->patch_arg);
6573 if (err || path_unstaged_content == NULL)
6574 break;
6575 if (path_new_staged_content) {
6576 err = got_object_blob_create(
6577 &staged_blob_id,
6578 path_new_staged_content,
6579 a->repo);
6580 if (err)
6581 break;
6582 memcpy(ie->staged_blob_sha1,
6583 staged_blob_id->sha1,
6584 SHA1_DIGEST_LENGTH);
6586 err = merge_file(&local_changes_subsumed,
6587 a->worktree, blob_base, ondisk_path,
6588 relpath, got_fileindex_perms_to_st(ie),
6589 path_unstaged_content, label_orig,
6590 "unstaged", a->repo, a->progress_cb,
6591 a->progress_arg);
6592 if (err == NULL &&
6593 path_new_staged_content == NULL)
6594 got_fileindex_entry_stage_set(ie,
6595 GOT_FILEIDX_STAGE_NONE);
6596 break; /* Done with this file. */
6599 err = got_object_open_as_blob(&blob_staged, a->repo,
6600 staged_blob_id, 8192);
6601 if (err)
6602 break;
6603 err = merge_blob(&local_changes_subsumed, a->worktree,
6604 blob_base, ondisk_path, relpath,
6605 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6606 commit_id ? commit_id : a->worktree->base_commit_id,
6607 a->repo, a->progress_cb, a->progress_arg);
6608 if (err == NULL)
6609 got_fileindex_entry_stage_set(ie,
6610 GOT_FILEIDX_STAGE_NONE);
6611 break;
6612 case GOT_STATUS_DELETE:
6613 if (a->patch_cb) {
6614 int choice = GOT_PATCH_CHOICE_NONE;
6615 err = (*a->patch_cb)(&choice, a->patch_arg,
6616 staged_status, ie->path, NULL, 1, 1);
6617 if (err)
6618 break;
6619 if (choice == GOT_PATCH_CHOICE_NO)
6620 break;
6621 if (choice != GOT_PATCH_CHOICE_YES) {
6622 err = got_error(GOT_ERR_PATCH_CHOICE);
6623 break;
6626 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6627 err = get_file_status(&status, &sb, ie, ondisk_path,
6628 dirfd, de_name, a->repo);
6629 if (err)
6630 break;
6631 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6632 break;
6634 done:
6635 free(ondisk_path);
6636 if (path_unstaged_content &&
6637 unlink(path_unstaged_content) == -1 && err == NULL)
6638 err = got_error_from_errno2("unlink", path_unstaged_content);
6639 if (path_new_staged_content &&
6640 unlink(path_new_staged_content) == -1 && err == NULL)
6641 err = got_error_from_errno2("unlink", path_new_staged_content);
6642 free(path_unstaged_content);
6643 free(path_new_staged_content);
6644 if (blob_base)
6645 got_object_blob_close(blob_base);
6646 if (blob_staged)
6647 got_object_blob_close(blob_staged);
6648 free(id_str);
6649 free(label_orig);
6650 return err;
6653 const struct got_error *
6654 got_worktree_unstage(struct got_worktree *worktree,
6655 struct got_pathlist_head *paths,
6656 got_worktree_checkout_cb progress_cb, void *progress_arg,
6657 got_worktree_patch_cb patch_cb, void *patch_arg,
6658 struct got_repository *repo)
6660 const struct got_error *err = NULL, *sync_err, *unlockerr;
6661 struct got_pathlist_entry *pe;
6662 struct got_fileindex *fileindex = NULL;
6663 char *fileindex_path = NULL;
6664 struct unstage_path_arg upa;
6666 err = lock_worktree(worktree, LOCK_EX);
6667 if (err)
6668 return err;
6670 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6671 if (err)
6672 goto done;
6674 upa.worktree = worktree;
6675 upa.fileindex = fileindex;
6676 upa.repo = repo;
6677 upa.progress_cb = progress_cb;
6678 upa.progress_arg = progress_arg;
6679 upa.patch_cb = patch_cb;
6680 upa.patch_arg = patch_arg;
6681 TAILQ_FOREACH(pe, paths, entry) {
6682 err = worktree_status(worktree, pe->path, fileindex, repo,
6683 unstage_path, &upa, NULL, NULL, 0, 0);
6684 if (err)
6685 goto done;
6688 sync_err = sync_fileindex(fileindex, fileindex_path);
6689 if (sync_err && err == NULL)
6690 err = sync_err;
6691 done:
6692 free(fileindex_path);
6693 if (fileindex)
6694 got_fileindex_free(fileindex);
6695 unlockerr = lock_worktree(worktree, LOCK_SH);
6696 if (unlockerr && err == NULL)
6697 err = unlockerr;
6698 return err;