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 return err;
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 return err;
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 if (err)
1467 return err;
1468 } else {
1469 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1470 if (err)
1471 return err;
1472 if (status == GOT_STATUS_NO_CHANGE) {
1473 err = remove_ondisk_file(worktree->root_path, ie->path);
1474 if (err)
1475 return err;
1477 got_fileindex_entry_remove(fileindex, ie);
1480 return err;
1483 struct diff_cb_arg {
1484 struct got_fileindex *fileindex;
1485 struct got_worktree *worktree;
1486 struct got_repository *repo;
1487 got_worktree_checkout_cb progress_cb;
1488 void *progress_arg;
1489 got_cancel_cb cancel_cb;
1490 void *cancel_arg;
1493 static const struct got_error *
1494 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1495 struct got_tree_entry *te, const char *parent_path)
1497 struct diff_cb_arg *a = arg;
1499 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1500 return got_error(GOT_ERR_CANCELLED);
1502 return update_blob(a->worktree, a->fileindex, ie, te,
1503 ie->path, a->repo, a->progress_cb, a->progress_arg);
1506 static const struct got_error *
1507 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1509 struct diff_cb_arg *a = arg;
1511 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1512 return got_error(GOT_ERR_CANCELLED);
1514 return delete_blob(a->worktree, a->fileindex, ie,
1515 a->repo, a->progress_cb, a->progress_arg);
1518 static const struct got_error *
1519 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1521 struct diff_cb_arg *a = arg;
1522 const struct got_error *err;
1523 char *path;
1525 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1526 return got_error(GOT_ERR_CANCELLED);
1528 if (got_object_tree_entry_is_submodule(te))
1529 return NULL;
1531 if (asprintf(&path, "%s%s%s", parent_path,
1532 parent_path[0] ? "/" : "", te->name)
1533 == -1)
1534 return got_error_from_errno("asprintf");
1536 if (S_ISDIR(te->mode))
1537 err = add_dir_on_disk(a->worktree, path);
1538 else
1539 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1540 a->repo, a->progress_cb, a->progress_arg);
1542 free(path);
1543 return err;
1546 static const struct got_error *
1547 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1549 const struct got_error *err = NULL;
1550 char *uuidstr = NULL;
1551 uint32_t uuid_status;
1553 *refname = NULL;
1555 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1556 if (uuid_status != uuid_s_ok)
1557 return got_error_uuid(uuid_status, "uuid_to_string");
1559 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1560 == -1) {
1561 err = got_error_from_errno("asprintf");
1562 *refname = NULL;
1564 free(uuidstr);
1565 return err;
1568 const struct got_error *
1569 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1581 static const struct got_error *
1582 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1587 static const struct got_error *
1588 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1590 return get_ref_name(refname, worktree,
1591 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1594 static const struct got_error *
1595 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1597 return get_ref_name(refname, worktree,
1598 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1601 static const struct got_error *
1602 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1604 return get_ref_name(refname, worktree,
1605 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1608 static const struct got_error *
1609 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1611 return get_ref_name(refname, worktree,
1612 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1615 static const struct got_error *
1616 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1618 return get_ref_name(refname, worktree,
1619 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1622 static const struct got_error *
1623 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1625 return get_ref_name(refname, worktree,
1626 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1629 const struct got_error *
1630 got_worktree_get_histedit_script_path(char **path,
1631 struct got_worktree *worktree)
1633 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1634 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1635 *path = NULL;
1636 return got_error_from_errno("asprintf");
1638 return NULL;
1642 * Prevent Git's garbage collector from deleting our base commit by
1643 * setting a reference to our base commit's ID.
1645 static const struct got_error *
1646 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1648 const struct got_error *err = NULL;
1649 struct got_reference *ref = NULL;
1650 char *refname;
1652 err = got_worktree_get_base_ref_name(&refname, worktree);
1653 if (err)
1654 return err;
1656 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1657 if (err)
1658 goto done;
1660 err = got_ref_write(ref, repo);
1661 done:
1662 free(refname);
1663 if (ref)
1664 got_ref_close(ref);
1665 return err;
1668 static const struct got_error *
1669 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1671 const struct got_error *err = NULL;
1673 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1674 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 *fileindex_path = NULL;
1678 return err;
1682 static const struct got_error *
1683 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1684 struct got_worktree *worktree)
1686 const struct got_error *err = NULL;
1687 FILE *index = NULL;
1689 *fileindex_path = NULL;
1690 *fileindex = got_fileindex_alloc();
1691 if (*fileindex == NULL)
1692 return got_error_from_errno("got_fileindex_alloc");
1694 err = get_fileindex_path(fileindex_path, worktree);
1695 if (err)
1696 goto done;
1698 index = fopen(*fileindex_path, "rb");
1699 if (index == NULL) {
1700 if (errno != ENOENT)
1701 err = got_error_from_errno2("fopen", *fileindex_path);
1702 } else {
1703 err = got_fileindex_read(*fileindex, index);
1704 if (fclose(index) != 0 && err == NULL)
1705 err = got_error_from_errno("fclose");
1707 done:
1708 if (err) {
1709 free(*fileindex_path);
1710 *fileindex_path = NULL;
1711 got_fileindex_free(*fileindex);
1712 *fileindex = NULL;
1714 return err;
1717 struct bump_base_commit_id_arg {
1718 struct got_object_id *base_commit_id;
1719 const char *path;
1720 size_t path_len;
1721 const char *entry_name;
1722 got_worktree_checkout_cb progress_cb;
1723 void *progress_arg;
1726 /* Bump base commit ID of all files within an updated part of the work tree. */
1727 static const struct got_error *
1728 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1730 const struct got_error *err;
1731 struct bump_base_commit_id_arg *a = arg;
1733 if (a->entry_name) {
1734 if (strcmp(ie->path, a->path) != 0)
1735 return NULL;
1736 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1737 return NULL;
1739 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1740 SHA1_DIGEST_LENGTH) == 0)
1741 return NULL;
1743 if (a->progress_cb) {
1744 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1745 ie->path);
1746 if (err)
1747 return err;
1749 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1750 return NULL;
1753 static const struct got_error *
1754 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1756 const struct got_error *err = NULL;
1757 char *new_fileindex_path = NULL;
1758 FILE *new_index = NULL;
1759 struct timespec timeout;
1761 err = got_opentemp_named(&new_fileindex_path, &new_index,
1762 fileindex_path);
1763 if (err)
1764 goto done;
1766 err = got_fileindex_write(fileindex, new_index);
1767 if (err)
1768 goto done;
1770 if (rename(new_fileindex_path, fileindex_path) != 0) {
1771 err = got_error_from_errno3("rename", new_fileindex_path,
1772 fileindex_path);
1773 unlink(new_fileindex_path);
1777 * Sleep for a short amount of time to ensure that files modified after
1778 * this program exits have a different time stamp from the one which
1779 * was recorded in the file index.
1781 timeout.tv_sec = 0;
1782 timeout.tv_nsec = 1;
1783 nanosleep(&timeout, NULL);
1784 done:
1785 if (new_index)
1786 fclose(new_index);
1787 free(new_fileindex_path);
1788 return err;
1791 static const struct got_error *
1792 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1793 struct got_object_id **tree_id, const char *wt_relpath,
1794 struct got_worktree *worktree, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 struct got_object_id *id = NULL;
1798 char *in_repo_path = NULL;
1799 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1801 *entry_type = GOT_OBJ_TYPE_ANY;
1802 *tree_relpath = NULL;
1803 *tree_id = NULL;
1805 if (wt_relpath[0] == '\0') {
1806 /* Check out all files within the work tree. */
1807 *entry_type = GOT_OBJ_TYPE_TREE;
1808 *tree_relpath = strdup("");
1809 if (*tree_relpath == NULL) {
1810 err = got_error_from_errno("strdup");
1811 goto done;
1813 err = got_object_id_by_path(tree_id, repo,
1814 worktree->base_commit_id, worktree->path_prefix);
1815 if (err)
1816 goto done;
1817 return NULL;
1820 /* Check out a subset of files in the work tree. */
1822 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1823 is_root_wt ? "" : "/", wt_relpath) == -1) {
1824 err = got_error_from_errno("asprintf");
1825 goto done;
1828 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1829 in_repo_path);
1830 if (err)
1831 goto done;
1833 free(in_repo_path);
1834 in_repo_path = NULL;
1836 err = got_object_get_type(entry_type, repo, id);
1837 if (err)
1838 goto done;
1840 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1841 /* Check out a single file. */
1842 if (strchr(wt_relpath, '/') == NULL) {
1843 /* Check out a single file in work tree's root dir. */
1844 in_repo_path = strdup(worktree->path_prefix);
1845 if (in_repo_path == NULL) {
1846 err = got_error_from_errno("strdup");
1847 goto done;
1849 *tree_relpath = strdup("");
1850 if (*tree_relpath == NULL) {
1851 err = got_error_from_errno("strdup");
1852 goto done;
1854 } else {
1855 /* Check out a single file in a subdirectory. */
1856 err = got_path_dirname(tree_relpath, wt_relpath);
1857 if (err)
1858 return err;
1859 if (asprintf(&in_repo_path, "%s%s%s",
1860 worktree->path_prefix, is_root_wt ? "" : "/",
1861 *tree_relpath) == -1) {
1862 err = got_error_from_errno("asprintf");
1863 goto done;
1866 err = got_object_id_by_path(tree_id, repo,
1867 worktree->base_commit_id, in_repo_path);
1868 } else {
1869 /* Check out all files within a subdirectory. */
1870 *tree_id = got_object_id_dup(id);
1871 if (*tree_id == NULL) {
1872 err = got_error_from_errno("got_object_id_dup");
1873 goto done;
1875 *tree_relpath = strdup(wt_relpath);
1876 if (*tree_relpath == NULL) {
1877 err = got_error_from_errno("strdup");
1878 goto done;
1881 done:
1882 free(id);
1883 free(in_repo_path);
1884 if (err) {
1885 *entry_type = GOT_OBJ_TYPE_ANY;
1886 free(*tree_relpath);
1887 *tree_relpath = NULL;
1888 free(*tree_id);
1889 *tree_id = NULL;
1891 return err;
1894 static const struct got_error *
1895 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1896 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1897 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1898 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1900 const struct got_error *err = NULL;
1901 struct got_commit_object *commit = NULL;
1902 struct got_tree_object *tree = NULL;
1903 struct got_fileindex_diff_tree_cb diff_cb;
1904 struct diff_cb_arg arg;
1906 err = ref_base_commit(worktree, repo);
1907 if (err) {
1908 if (!(err->code == GOT_ERR_ERRNO &&
1909 (errno == EACCES || errno == EROFS)))
1910 goto done;
1911 err = (*progress_cb)(progress_arg,
1912 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1913 if (err)
1914 return err;
1917 err = got_object_open_as_commit(&commit, repo,
1918 worktree->base_commit_id);
1919 if (err)
1920 goto done;
1922 err = got_object_open_as_tree(&tree, repo, tree_id);
1923 if (err)
1924 goto done;
1926 if (entry_name &&
1927 got_object_tree_find_entry(tree, entry_name) == NULL) {
1928 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1929 goto done;
1932 diff_cb.diff_old_new = diff_old_new;
1933 diff_cb.diff_old = diff_old;
1934 diff_cb.diff_new = diff_new;
1935 arg.fileindex = fileindex;
1936 arg.worktree = worktree;
1937 arg.repo = repo;
1938 arg.progress_cb = progress_cb;
1939 arg.progress_arg = progress_arg;
1940 arg.cancel_cb = cancel_cb;
1941 arg.cancel_arg = cancel_arg;
1942 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1943 entry_name, repo, &diff_cb, &arg);
1944 done:
1945 if (tree)
1946 got_object_tree_close(tree);
1947 if (commit)
1948 got_object_commit_close(commit);
1949 return err;
1952 const struct got_error *
1953 got_worktree_checkout_files(struct got_worktree *worktree,
1954 struct got_pathlist_head *paths, struct got_repository *repo,
1955 got_worktree_checkout_cb progress_cb, void *progress_arg,
1956 got_cancel_cb cancel_cb, void *cancel_arg)
1958 const struct got_error *err = NULL, *sync_err, *unlockerr;
1959 struct got_commit_object *commit = NULL;
1960 struct got_tree_object *tree = NULL;
1961 struct got_fileindex *fileindex = NULL;
1962 char *fileindex_path = NULL;
1963 struct got_pathlist_entry *pe;
1964 struct tree_path_data {
1965 SIMPLEQ_ENTRY(tree_path_data) entry;
1966 struct got_object_id *tree_id;
1967 int entry_type;
1968 char *relpath;
1969 char *entry_name;
1970 } *tpd = NULL;
1971 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1973 SIMPLEQ_INIT(&tree_paths);
1975 err = lock_worktree(worktree, LOCK_EX);
1976 if (err)
1977 return err;
1979 /* Map all specified paths to in-repository trees. */
1980 TAILQ_FOREACH(pe, paths, entry) {
1981 tpd = malloc(sizeof(*tpd));
1982 if (tpd == NULL) {
1983 err = got_error_from_errno("malloc");
1984 goto done;
1987 err = find_tree_entry_for_checkout(&tpd->entry_type,
1988 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1989 if (err) {
1990 free(tpd);
1991 goto done;
1994 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1995 err = got_path_basename(&tpd->entry_name, pe->path);
1996 if (err) {
1997 free(tpd->relpath);
1998 free(tpd->tree_id);
1999 free(tpd);
2000 goto done;
2002 } else
2003 tpd->entry_name = NULL;
2005 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2009 * Read the file index.
2010 * Checking out files is supposed to be an idempotent operation.
2011 * If the on-disk file index is incomplete we will try to complete it.
2013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2014 if (err)
2015 goto done;
2017 tpd = SIMPLEQ_FIRST(&tree_paths);
2018 TAILQ_FOREACH(pe, paths, entry) {
2019 struct bump_base_commit_id_arg bbc_arg;
2021 err = checkout_files(worktree, fileindex, tpd->relpath,
2022 tpd->tree_id, tpd->entry_name, repo,
2023 progress_cb, progress_arg, cancel_cb, cancel_arg);
2024 if (err)
2025 break;
2027 bbc_arg.base_commit_id = worktree->base_commit_id;
2028 bbc_arg.entry_name = tpd->entry_name;
2029 bbc_arg.path = pe->path;
2030 bbc_arg.path_len = pe->path_len;
2031 bbc_arg.progress_cb = progress_cb;
2032 bbc_arg.progress_arg = progress_arg;
2033 err = got_fileindex_for_each_entry_safe(fileindex,
2034 bump_base_commit_id, &bbc_arg);
2035 if (err)
2036 break;
2038 tpd = SIMPLEQ_NEXT(tpd, entry);
2040 sync_err = sync_fileindex(fileindex, fileindex_path);
2041 if (sync_err && err == NULL)
2042 err = sync_err;
2043 done:
2044 free(fileindex_path);
2045 if (tree)
2046 got_object_tree_close(tree);
2047 if (commit)
2048 got_object_commit_close(commit);
2049 if (fileindex)
2050 got_fileindex_free(fileindex);
2051 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2052 tpd = SIMPLEQ_FIRST(&tree_paths);
2053 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2054 free(tpd->relpath);
2055 free(tpd->tree_id);
2056 free(tpd);
2058 unlockerr = lock_worktree(worktree, LOCK_SH);
2059 if (unlockerr && err == NULL)
2060 err = unlockerr;
2061 return err;
2064 struct merge_file_cb_arg {
2065 struct got_worktree *worktree;
2066 struct got_fileindex *fileindex;
2067 got_worktree_checkout_cb progress_cb;
2068 void *progress_arg;
2069 got_cancel_cb cancel_cb;
2070 void *cancel_arg;
2071 const char *label_orig;
2072 struct got_object_id *commit_id2;
2075 static const struct got_error *
2076 merge_file_cb(void *arg, struct got_blob_object *blob1,
2077 struct got_blob_object *blob2, struct got_object_id *id1,
2078 struct got_object_id *id2, const char *path1, const char *path2,
2079 mode_t mode1, mode_t mode2, struct got_repository *repo)
2081 static const struct got_error *err = NULL;
2082 struct merge_file_cb_arg *a = arg;
2083 struct got_fileindex_entry *ie;
2084 char *ondisk_path = NULL;
2085 struct stat sb;
2086 unsigned char status;
2087 int local_changes_subsumed;
2089 if (blob1 && blob2) {
2090 ie = got_fileindex_entry_get(a->fileindex, path2,
2091 strlen(path2));
2092 if (ie == NULL)
2093 return (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MISSING, path2);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2097 path2) == -1)
2098 return got_error_from_errno("asprintf");
2100 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2101 repo);
2102 if (err)
2103 goto done;
2105 if (status == GOT_STATUS_DELETE) {
2106 err = (*a->progress_cb)(a->progress_arg,
2107 GOT_STATUS_MERGE, path2);
2108 goto done;
2110 if (status != GOT_STATUS_NO_CHANGE &&
2111 status != GOT_STATUS_MODIFY &&
2112 status != GOT_STATUS_CONFLICT &&
2113 status != GOT_STATUS_ADD) {
2114 err = (*a->progress_cb)(a->progress_arg, status, path2);
2115 goto done;
2118 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2119 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2120 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2121 } else if (blob1) {
2122 ie = got_fileindex_entry_get(a->fileindex, path1,
2123 strlen(path1));
2124 if (ie == NULL)
2125 return (*a->progress_cb)(a->progress_arg,
2126 GOT_STATUS_MISSING, path1);
2128 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2129 path1) == -1)
2130 return got_error_from_errno("asprintf");
2132 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2133 repo);
2134 if (err)
2135 goto done;
2137 switch (status) {
2138 case GOT_STATUS_NO_CHANGE:
2139 err = (*a->progress_cb)(a->progress_arg,
2140 GOT_STATUS_DELETE, path1);
2141 if (err)
2142 goto done;
2143 err = remove_ondisk_file(a->worktree->root_path, path1);
2144 if (err)
2145 goto done;
2146 if (ie)
2147 got_fileindex_entry_mark_deleted_from_disk(ie);
2148 break;
2149 case GOT_STATUS_DELETE:
2150 case GOT_STATUS_MISSING:
2151 err = (*a->progress_cb)(a->progress_arg,
2152 GOT_STATUS_DELETE, path1);
2153 if (err)
2154 goto done;
2155 if (ie)
2156 got_fileindex_entry_mark_deleted_from_disk(ie);
2157 break;
2158 case GOT_STATUS_ADD:
2159 case GOT_STATUS_MODIFY:
2160 case GOT_STATUS_CONFLICT:
2161 err = (*a->progress_cb)(a->progress_arg,
2162 GOT_STATUS_CANNOT_DELETE, path1);
2163 if (err)
2164 goto done;
2165 break;
2166 case GOT_STATUS_OBSTRUCTED:
2167 err = (*a->progress_cb)(a->progress_arg, status, path1);
2168 if (err)
2169 goto done;
2170 break;
2171 default:
2172 break;
2174 } else if (blob2) {
2175 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2176 path2) == -1)
2177 return got_error_from_errno("asprintf");
2178 ie = got_fileindex_entry_get(a->fileindex, path2,
2179 strlen(path2));
2180 if (ie) {
2181 err = get_file_status(&status, &sb, ie, ondisk_path,
2182 -1, NULL, repo);
2183 if (err)
2184 goto done;
2185 if (status != GOT_STATUS_NO_CHANGE &&
2186 status != GOT_STATUS_MODIFY &&
2187 status != GOT_STATUS_CONFLICT &&
2188 status != GOT_STATUS_ADD) {
2189 err = (*a->progress_cb)(a->progress_arg,
2190 status, path2);
2191 goto done;
2193 err = merge_blob(&local_changes_subsumed, a->worktree,
2194 NULL, ondisk_path, path2, sb.st_mode,
2195 a->label_orig, blob2, a->commit_id2, repo,
2196 a->progress_cb,
2197 a->progress_arg);
2198 if (status == GOT_STATUS_DELETE) {
2199 err = update_blob_fileindex_entry(a->worktree,
2200 a->fileindex, ie, ondisk_path, ie->path,
2201 blob2, 0);
2202 if (err)
2203 goto done;
2205 } else {
2206 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2207 err = install_blob(a->worktree, ondisk_path, path2,
2208 /* XXX get this from parent tree! */
2209 GOT_DEFAULT_FILE_MODE,
2210 sb.st_mode, blob2, 0, 0, repo,
2211 a->progress_cb, a->progress_arg);
2212 if (err)
2213 goto done;
2214 err = got_fileindex_entry_alloc(&ie, path2);
2215 if (err)
2216 goto done;
2217 err = got_fileindex_entry_update(ie, ondisk_path,
2218 NULL, NULL, 1);
2219 if (err) {
2220 got_fileindex_entry_free(ie);
2221 goto done;
2223 err = got_fileindex_entry_add(a->fileindex, ie);
2224 if (err) {
2225 got_fileindex_entry_free(ie);
2226 goto done;
2230 done:
2231 free(ondisk_path);
2232 return err;
2235 struct check_merge_ok_arg {
2236 struct got_worktree *worktree;
2237 struct got_repository *repo;
2240 static const struct got_error *
2241 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2243 const struct got_error *err = NULL;
2244 struct check_merge_ok_arg *a = arg;
2245 unsigned char status;
2246 struct stat sb;
2247 char *ondisk_path;
2249 /* Reject merges into a work tree with mixed base commits. */
2250 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2251 SHA1_DIGEST_LENGTH))
2252 return got_error(GOT_ERR_MIXED_COMMITS);
2254 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2255 == -1)
2256 return got_error_from_errno("asprintf");
2258 /* Reject merges into a work tree with conflicted files. */
2259 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2260 if (err)
2261 return err;
2262 if (status == GOT_STATUS_CONFLICT)
2263 return got_error(GOT_ERR_CONFLICTS);
2265 return NULL;
2268 static const struct got_error *
2269 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2270 const char *fileindex_path, struct got_object_id *commit_id1,
2271 struct got_object_id *commit_id2, struct got_repository *repo,
2272 got_worktree_checkout_cb progress_cb, void *progress_arg,
2273 got_cancel_cb cancel_cb, void *cancel_arg)
2275 const struct got_error *err = NULL, *sync_err;
2276 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2277 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2278 struct merge_file_cb_arg arg;
2279 char *label_orig = NULL;
2281 if (commit_id1) {
2282 char *id_str;
2284 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2285 worktree->path_prefix);
2286 if (err)
2287 goto done;
2289 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2290 if (err)
2291 goto done;
2293 err = got_object_id_str(&id_str, commit_id1);
2294 if (err)
2295 goto done;
2297 if (asprintf(&label_orig, "%s: commit %s",
2298 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 free(id_str);
2301 goto done;
2303 free(id_str);
2306 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2307 worktree->path_prefix);
2308 if (err)
2309 goto done;
2311 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2312 if (err)
2313 goto done;
2315 arg.worktree = worktree;
2316 arg.fileindex = fileindex;
2317 arg.progress_cb = progress_cb;
2318 arg.progress_arg = progress_arg;
2319 arg.cancel_cb = cancel_cb;
2320 arg.cancel_arg = cancel_arg;
2321 arg.label_orig = label_orig;
2322 arg.commit_id2 = commit_id2;
2323 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2324 sync_err = sync_fileindex(fileindex, fileindex_path);
2325 if (sync_err && err == NULL)
2326 err = sync_err;
2327 done:
2328 if (tree1)
2329 got_object_tree_close(tree1);
2330 if (tree2)
2331 got_object_tree_close(tree2);
2332 free(label_orig);
2333 return err;
2336 const struct got_error *
2337 got_worktree_merge_files(struct got_worktree *worktree,
2338 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2339 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2340 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2342 const struct got_error *err, *unlockerr;
2343 char *fileindex_path = NULL;
2344 struct got_fileindex *fileindex = NULL;
2345 struct check_merge_ok_arg mok_arg;
2347 err = lock_worktree(worktree, LOCK_EX);
2348 if (err)
2349 return err;
2351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2352 if (err)
2353 goto done;
2355 mok_arg.worktree = worktree;
2356 mok_arg.repo = repo;
2357 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2358 &mok_arg);
2359 if (err)
2360 goto done;
2362 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2363 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2364 done:
2365 if (fileindex)
2366 got_fileindex_free(fileindex);
2367 free(fileindex_path);
2368 unlockerr = lock_worktree(worktree, LOCK_SH);
2369 if (unlockerr && err == NULL)
2370 err = unlockerr;
2371 return err;
2374 struct diff_dir_cb_arg {
2375 struct got_fileindex *fileindex;
2376 struct got_worktree *worktree;
2377 const char *status_path;
2378 size_t status_path_len;
2379 struct got_repository *repo;
2380 got_worktree_status_cb status_cb;
2381 void *status_arg;
2382 got_cancel_cb cancel_cb;
2383 void *cancel_arg;
2384 /* A pathlist containing per-directory pathlists of ignore patterns. */
2385 struct got_pathlist_head ignores;
2386 int report_unchanged;
2389 static const struct got_error *
2390 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2391 int dirfd, const char *de_name,
2392 got_worktree_status_cb status_cb, void *status_arg,
2393 struct got_repository *repo, int report_unchanged)
2395 const struct got_error *err = NULL;
2396 unsigned char status = GOT_STATUS_NO_CHANGE;
2397 unsigned char staged_status = get_staged_status(ie);
2398 struct stat sb;
2399 struct got_object_id blob_id, commit_id, staged_blob_id;
2400 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2401 struct got_object_id *staged_blob_idp = NULL;
2403 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2404 if (err)
2405 return err;
2407 if (status == GOT_STATUS_NO_CHANGE &&
2408 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2409 return NULL;
2411 if (got_fileindex_entry_has_blob(ie)) {
2412 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2413 blob_idp = &blob_id;
2415 if (got_fileindex_entry_has_commit(ie)) {
2416 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2417 commit_idp = &commit_id;
2419 if (staged_status == GOT_STATUS_ADD ||
2420 staged_status == GOT_STATUS_MODIFY) {
2421 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2422 SHA1_DIGEST_LENGTH);
2423 staged_blob_idp = &staged_blob_id;
2426 return (*status_cb)(status_arg, status, staged_status,
2427 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2430 static const struct got_error *
2431 status_old_new(void *arg, struct got_fileindex_entry *ie,
2432 struct dirent *de, const char *parent_path, int dirfd)
2434 const struct got_error *err = NULL;
2435 struct diff_dir_cb_arg *a = arg;
2436 char *abspath;
2438 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2439 return got_error(GOT_ERR_CANCELLED);
2441 if (got_path_cmp(parent_path, a->status_path,
2442 strlen(parent_path), a->status_path_len) != 0 &&
2443 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2444 return NULL;
2446 if (parent_path[0]) {
2447 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2448 parent_path, de->d_name) == -1)
2449 return got_error_from_errno("asprintf");
2450 } else {
2451 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2452 de->d_name) == -1)
2453 return got_error_from_errno("asprintf");
2456 err = report_file_status(ie, abspath, dirfd, de->d_name,
2457 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2458 free(abspath);
2459 return err;
2462 static const struct got_error *
2463 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2465 struct diff_dir_cb_arg *a = arg;
2466 struct got_object_id blob_id, commit_id;
2467 unsigned char status;
2469 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2470 return got_error(GOT_ERR_CANCELLED);
2472 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2473 return NULL;
2475 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2476 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2477 if (got_fileindex_entry_has_file_on_disk(ie))
2478 status = GOT_STATUS_MISSING;
2479 else
2480 status = GOT_STATUS_DELETE;
2481 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2482 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2485 void
2486 free_ignorelist(struct got_pathlist_head *ignorelist)
2488 struct got_pathlist_entry *pe;
2490 TAILQ_FOREACH(pe, ignorelist, entry)
2491 free((char *)pe->path);
2492 got_pathlist_free(ignorelist);
2495 void
2496 free_ignores(struct got_pathlist_head *ignores)
2498 struct got_pathlist_entry *pe;
2500 TAILQ_FOREACH(pe, ignores, entry) {
2501 struct got_pathlist_head *ignorelist = pe->data;
2502 free_ignorelist(ignorelist);
2503 free((char *)pe->path);
2505 got_pathlist_free(ignores);
2508 static const struct got_error *
2509 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2511 const struct got_error *err = NULL;
2512 struct got_pathlist_entry *pe = NULL;
2513 struct got_pathlist_head *ignorelist;
2514 char *line = NULL, *pattern, *dirpath = NULL;
2515 size_t linesize = 0;
2516 ssize_t linelen;
2518 ignorelist = calloc(1, sizeof(*ignorelist));
2519 if (ignorelist == NULL)
2520 return got_error_from_errno("calloc");
2521 TAILQ_INIT(ignorelist);
2523 while ((linelen = getline(&line, &linesize, f)) != -1) {
2524 if (linelen > 0 && line[linelen - 1] == '\n')
2525 line[linelen - 1] = '\0';
2527 /* Git's ignores may contain comments. */
2528 if (line[0] == '#')
2529 continue;
2531 /* Git's negated patterns are not (yet?) supported. */
2532 if (line[0] == '!')
2533 continue;
2535 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2536 line) == -1) {
2537 err = got_error_from_errno("asprintf");
2538 goto done;
2540 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2541 if (err)
2542 goto done;
2544 if (ferror(f)) {
2545 err = got_error_from_errno("getline");
2546 goto done;
2549 dirpath = strdup(path);
2550 if (dirpath == NULL) {
2551 err = got_error_from_errno("strdup");
2552 goto done;
2554 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2555 done:
2556 free(line);
2557 if (err || pe == NULL) {
2558 free(dirpath);
2559 free_ignorelist(ignorelist);
2561 return err;
2564 int
2565 match_ignores(struct got_pathlist_head *ignores, const char *path)
2567 struct got_pathlist_entry *pe;
2569 /* Handle patterns which match in all directories. */
2570 TAILQ_FOREACH(pe, ignores, entry) {
2571 struct got_pathlist_head *ignorelist = pe->data;
2572 struct got_pathlist_entry *pi;
2574 TAILQ_FOREACH(pi, ignorelist, entry) {
2575 const char *p, *pattern = pi->path;
2577 if (strncmp(pattern, "**/", 3) != 0)
2578 continue;
2579 pattern += 3;
2580 p = path;
2581 while (*p) {
2582 if (fnmatch(pattern, p,
2583 FNM_PATHNAME | FNM_LEADING_DIR)) {
2584 /* Retry in next directory. */
2585 while (*p && *p != '/')
2586 p++;
2587 while (*p == '/')
2588 p++;
2589 continue;
2591 return 1;
2597 * The ignores pathlist contains ignore lists from children before
2598 * parents, so we can find the most specific ignorelist by walking
2599 * ignores backwards.
2601 pe = TAILQ_LAST(ignores, got_pathlist_head);
2602 while (pe) {
2603 if (got_path_is_child(path, pe->path, pe->path_len)) {
2604 struct got_pathlist_head *ignorelist = pe->data;
2605 struct got_pathlist_entry *pi;
2606 TAILQ_FOREACH(pi, ignorelist, entry) {
2607 const char *pattern = pi->path;
2608 int flags = FNM_LEADING_DIR;
2609 if (strstr(pattern, "/**/") == NULL)
2610 flags |= FNM_PATHNAME;
2611 if (fnmatch(pattern, path, flags))
2612 continue;
2613 return 1;
2616 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2619 return 0;
2622 static const struct got_error *
2623 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2624 const char *path, int dirfd, const char *ignores_filename)
2626 const struct got_error *err = NULL;
2627 char *ignorespath;
2628 int fd = -1;
2629 FILE *ignoresfile = NULL;
2631 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2632 path[0] ? "/" : "", ignores_filename) == -1)
2633 return got_error_from_errno("asprintf");
2635 if (dirfd != -1) {
2636 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2637 if (fd == -1) {
2638 if (errno != ENOENT && errno != EACCES)
2639 err = got_error_from_errno2("openat",
2640 ignorespath);
2641 } else {
2642 ignoresfile = fdopen(fd, "r");
2643 if (ignoresfile == NULL)
2644 err = got_error_from_errno2("fdopen",
2645 ignorespath);
2646 else {
2647 fd = -1;
2648 err = read_ignores(ignores, path, ignoresfile);
2651 } else {
2652 ignoresfile = fopen(ignorespath, "r");
2653 if (ignoresfile == NULL) {
2654 if (errno != ENOENT && errno != EACCES)
2655 err = got_error_from_errno2("fopen",
2656 ignorespath);
2657 } else
2658 err = read_ignores(ignores, path, ignoresfile);
2661 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2662 err = got_error_from_errno2("fclose", path);
2663 if (fd != -1 && close(fd) == -1 && err == NULL)
2664 err = got_error_from_errno2("close", path);
2665 free(ignorespath);
2666 return err;
2669 static const struct got_error *
2670 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2672 const struct got_error *err = NULL;
2673 struct diff_dir_cb_arg *a = arg;
2674 char *path = NULL;
2676 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2677 return got_error(GOT_ERR_CANCELLED);
2679 /* XXX ignore symlinks for now */
2680 if (de->d_type == DT_LNK)
2681 return NULL;
2683 if (parent_path[0]) {
2684 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2685 return got_error_from_errno("asprintf");
2686 } else {
2687 path = de->d_name;
2690 if (de->d_type == DT_DIR) {
2691 int subdirfd = openat(dirfd, de->d_name,
2692 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2693 if (subdirfd == -1) {
2694 if (errno != ENOENT && errno != EACCES)
2695 err = got_error_from_errno2("openat", path);
2696 } else {
2697 err = add_ignores(&a->ignores, a->worktree->root_path,
2698 path, subdirfd, ".cvsignore");
2699 if (err == NULL)
2700 err = add_ignores(&a->ignores,
2701 a->worktree->root_path, path,
2702 subdirfd, ".gitignore");
2703 if (close(subdirfd) == -1 && err == NULL)
2704 err = got_error_from_errno2("close", path);
2706 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2707 && !match_ignores(&a->ignores, path))
2708 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2709 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2710 if (parent_path[0])
2711 free(path);
2712 return err;
2715 static const struct got_error *
2716 report_single_file_status(const char *path, const char *ondisk_path,
2717 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2718 void *status_arg, struct got_repository *repo, int report_unchanged)
2720 struct got_fileindex_entry *ie;
2721 struct stat sb;
2723 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2724 if (ie)
2725 return report_file_status(ie, ondisk_path, -1, NULL,
2726 status_cb, status_arg, repo, report_unchanged);
2728 if (lstat(ondisk_path, &sb) == -1) {
2729 if (errno != ENOENT)
2730 return got_error_from_errno2("lstat", ondisk_path);
2731 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2732 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2733 return NULL;
2736 if (S_ISREG(sb.st_mode))
2737 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2738 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2740 return NULL;
2743 static const struct got_error *
2744 worktree_status(struct got_worktree *worktree, const char *path,
2745 struct got_fileindex *fileindex, struct got_repository *repo,
2746 got_worktree_status_cb status_cb, void *status_arg,
2747 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2748 int report_unchanged)
2750 const struct got_error *err = NULL;
2751 int fd = -1;
2752 struct got_fileindex_diff_dir_cb fdiff_cb;
2753 struct diff_dir_cb_arg arg;
2754 char *ondisk_path = NULL;
2756 if (asprintf(&ondisk_path, "%s%s%s",
2757 worktree->root_path, path[0] ? "/" : "", path) == -1)
2758 return got_error_from_errno("asprintf");
2760 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2761 if (fd == -1) {
2762 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2763 err = got_error_from_errno2("open", ondisk_path);
2764 else
2765 err = report_single_file_status(path, ondisk_path,
2766 fileindex, status_cb, status_arg, repo,
2767 report_unchanged);
2768 } else {
2769 fdiff_cb.diff_old_new = status_old_new;
2770 fdiff_cb.diff_old = status_old;
2771 fdiff_cb.diff_new = status_new;
2772 arg.fileindex = fileindex;
2773 arg.worktree = worktree;
2774 arg.status_path = path;
2775 arg.status_path_len = strlen(path);
2776 arg.repo = repo;
2777 arg.status_cb = status_cb;
2778 arg.status_arg = status_arg;
2779 arg.cancel_cb = cancel_cb;
2780 arg.cancel_arg = cancel_arg;
2781 arg.report_unchanged = report_unchanged;
2782 TAILQ_INIT(&arg.ignores);
2783 if (!no_ignores) {
2784 err = add_ignores(&arg.ignores, worktree->root_path,
2785 path, fd, ".cvsignore");
2786 if (err == NULL)
2787 err = add_ignores(&arg.ignores,
2788 worktree->root_path, path, fd,
2789 ".gitignore");
2791 if (err == NULL)
2792 err = got_fileindex_diff_dir(fileindex, fd,
2793 worktree->root_path, path, repo, &fdiff_cb, &arg);
2794 free_ignores(&arg.ignores);
2797 if (fd != -1 && close(fd) != 0 && err == NULL)
2798 err = got_error_from_errno("close");
2799 free(ondisk_path);
2800 return err;
2803 const struct got_error *
2804 got_worktree_status(struct got_worktree *worktree,
2805 struct got_pathlist_head *paths, struct got_repository *repo,
2806 got_worktree_status_cb status_cb, void *status_arg,
2807 got_cancel_cb cancel_cb, void *cancel_arg)
2809 const struct got_error *err = NULL;
2810 char *fileindex_path = NULL;
2811 struct got_fileindex *fileindex = NULL;
2812 struct got_pathlist_entry *pe;
2814 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2815 if (err)
2816 return err;
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 err = worktree_status(worktree, pe->path, fileindex, repo,
2820 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2821 if (err)
2822 break;
2824 free(fileindex_path);
2825 got_fileindex_free(fileindex);
2826 return err;
2829 const struct got_error *
2830 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2831 const char *arg)
2833 const struct got_error *err = NULL;
2834 char *resolved, *cwd = NULL, *path = NULL;
2835 size_t len;
2837 *wt_path = NULL;
2839 resolved = realpath(arg, NULL);
2840 if (resolved == NULL) {
2841 if (errno != ENOENT)
2842 return got_error_from_errno2("realpath", arg);
2843 cwd = getcwd(NULL, 0);
2844 if (cwd == NULL)
2845 return got_error_from_errno("getcwd");
2846 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2847 err = got_error_from_errno("asprintf");
2848 goto done;
2852 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2853 strlen(got_worktree_get_root_path(worktree)))) {
2854 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2855 goto done;
2858 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2859 err = got_path_skip_common_ancestor(&path,
2860 got_worktree_get_root_path(worktree), resolved);
2861 if (err)
2862 goto done;
2863 } else {
2864 path = strdup("");
2865 if (path == NULL) {
2866 err = got_error_from_errno("strdup");
2867 goto done;
2871 /* XXX status walk can't deal with trailing slash! */
2872 len = strlen(path);
2873 while (len > 0 && path[len - 1] == '/') {
2874 path[len - 1] = '\0';
2875 len--;
2877 done:
2878 free(resolved);
2879 free(cwd);
2880 if (err == NULL)
2881 *wt_path = path;
2882 else
2883 free(path);
2884 return err;
2887 struct schedule_addition_args {
2888 struct got_worktree *worktree;
2889 struct got_fileindex *fileindex;
2890 got_worktree_checkout_cb progress_cb;
2891 void *progress_arg;
2892 struct got_repository *repo;
2895 static const struct got_error *
2896 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2897 const char *relpath, struct got_object_id *blob_id,
2898 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2899 int dirfd, const char *de_name)
2901 struct schedule_addition_args *a = arg;
2902 const struct got_error *err = NULL;
2903 struct got_fileindex_entry *ie;
2904 struct stat sb;
2905 char *ondisk_path;
2907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2908 relpath) == -1)
2909 return got_error_from_errno("asprintf");
2911 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2912 if (ie) {
2913 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2914 de_name, a->repo);
2915 if (err)
2916 goto done;
2917 /* Re-adding an existing entry is a no-op. */
2918 if (status == GOT_STATUS_ADD)
2919 goto done;
2920 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2921 if (err)
2922 goto done;
2925 if (status != GOT_STATUS_UNVERSIONED) {
2926 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2927 goto done;
2930 err = got_fileindex_entry_alloc(&ie, relpath);
2931 if (err)
2932 goto done;
2933 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2934 if (err) {
2935 got_fileindex_entry_free(ie);
2936 goto done;
2938 err = got_fileindex_entry_add(a->fileindex, ie);
2939 if (err) {
2940 got_fileindex_entry_free(ie);
2941 goto done;
2943 done:
2944 free(ondisk_path);
2945 if (err)
2946 return err;
2947 if (status == GOT_STATUS_ADD)
2948 return NULL;
2949 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2952 const struct got_error *
2953 got_worktree_schedule_add(struct got_worktree *worktree,
2954 struct got_pathlist_head *paths,
2955 got_worktree_checkout_cb progress_cb, void *progress_arg,
2956 struct got_repository *repo, int no_ignores)
2958 struct got_fileindex *fileindex = NULL;
2959 char *fileindex_path = NULL;
2960 const struct got_error *err = NULL, *sync_err, *unlockerr;
2961 struct got_pathlist_entry *pe;
2962 struct schedule_addition_args saa;
2964 err = lock_worktree(worktree, LOCK_EX);
2965 if (err)
2966 return err;
2968 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2969 if (err)
2970 goto done;
2972 saa.worktree = worktree;
2973 saa.fileindex = fileindex;
2974 saa.progress_cb = progress_cb;
2975 saa.progress_arg = progress_arg;
2976 saa.repo = repo;
2978 TAILQ_FOREACH(pe, paths, entry) {
2979 err = worktree_status(worktree, pe->path, fileindex, repo,
2980 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2981 if (err)
2982 break;
2984 sync_err = sync_fileindex(fileindex, fileindex_path);
2985 if (sync_err && err == NULL)
2986 err = sync_err;
2987 done:
2988 free(fileindex_path);
2989 if (fileindex)
2990 got_fileindex_free(fileindex);
2991 unlockerr = lock_worktree(worktree, LOCK_SH);
2992 if (unlockerr && err == NULL)
2993 err = unlockerr;
2994 return err;
2997 struct schedule_deletion_args {
2998 struct got_worktree *worktree;
2999 struct got_fileindex *fileindex;
3000 got_worktree_delete_cb progress_cb;
3001 void *progress_arg;
3002 struct got_repository *repo;
3003 int delete_local_mods;
3004 int keep_on_disk;
3007 static const struct got_error *
3008 schedule_for_deletion(void *arg, unsigned char status,
3009 unsigned char staged_status, const char *relpath,
3010 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3011 struct got_object_id *commit_id, int dirfd, const char *de_name)
3013 struct schedule_deletion_args *a = arg;
3014 const struct got_error *err = NULL;
3015 struct got_fileindex_entry *ie = NULL;
3016 struct stat sb;
3017 char *ondisk_path, *parent = NULL;
3019 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3020 if (ie == NULL)
3021 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3023 staged_status = get_staged_status(ie);
3024 if (staged_status != GOT_STATUS_NO_CHANGE) {
3025 if (staged_status == GOT_STATUS_DELETE)
3026 return NULL;
3027 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3031 relpath) == -1)
3032 return got_error_from_errno("asprintf");
3034 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3035 a->repo);
3036 if (err)
3037 goto done;
3039 if (status != GOT_STATUS_NO_CHANGE) {
3040 if (status == GOT_STATUS_DELETE)
3041 goto done;
3042 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3043 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3044 goto done;
3046 if (status != GOT_STATUS_MODIFY &&
3047 status != GOT_STATUS_MISSING) {
3048 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3049 goto done;
3053 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3054 if (dirfd != -1) {
3055 if (unlinkat(dirfd, de_name, 0) != 0) {
3056 err = got_error_from_errno2("unlinkat",
3057 ondisk_path);
3058 goto done;
3060 } else if (unlink(ondisk_path) != 0) {
3061 err = got_error_from_errno2("unlink", ondisk_path);
3062 goto done;
3065 parent = dirname(ondisk_path);
3067 if (parent == NULL) {
3068 err = got_error_from_errno2("dirname", ondisk_path);
3069 goto done;
3071 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3072 if (rmdir(parent) == -1) {
3073 if (errno != ENOTEMPTY)
3074 err = got_error_from_errno2("rmdir",
3075 parent);
3076 break;
3078 parent = dirname(parent);
3079 if (parent == NULL) {
3080 err = got_error_from_errno2("dirname", parent);
3081 goto done;
3086 got_fileindex_entry_mark_deleted_from_disk(ie);
3087 done:
3088 free(ondisk_path);
3089 if (err)
3090 return err;
3091 if (status == GOT_STATUS_DELETE)
3092 return NULL;
3093 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3094 staged_status, relpath);
3097 const struct got_error *
3098 got_worktree_schedule_delete(struct got_worktree *worktree,
3099 struct got_pathlist_head *paths, int delete_local_mods,
3100 got_worktree_delete_cb progress_cb, void *progress_arg,
3101 struct got_repository *repo, int keep_on_disk)
3103 struct got_fileindex *fileindex = NULL;
3104 char *fileindex_path = NULL;
3105 const struct got_error *err = NULL, *sync_err, *unlockerr;
3106 struct got_pathlist_entry *pe;
3107 struct schedule_deletion_args sda;
3109 err = lock_worktree(worktree, LOCK_EX);
3110 if (err)
3111 return err;
3113 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3114 if (err)
3115 goto done;
3117 sda.worktree = worktree;
3118 sda.fileindex = fileindex;
3119 sda.progress_cb = progress_cb;
3120 sda.progress_arg = progress_arg;
3121 sda.repo = repo;
3122 sda.delete_local_mods = delete_local_mods;
3123 sda.keep_on_disk = keep_on_disk;
3125 TAILQ_FOREACH(pe, paths, entry) {
3126 err = worktree_status(worktree, pe->path, fileindex, repo,
3127 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3128 if (err)
3129 break;
3131 sync_err = sync_fileindex(fileindex, fileindex_path);
3132 if (sync_err && err == NULL)
3133 err = sync_err;
3134 done:
3135 free(fileindex_path);
3136 if (fileindex)
3137 got_fileindex_free(fileindex);
3138 unlockerr = lock_worktree(worktree, LOCK_SH);
3139 if (unlockerr && err == NULL)
3140 err = unlockerr;
3141 return err;
3144 static const struct got_error *
3145 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3147 const struct got_error *err = NULL;
3148 char *line = NULL;
3149 size_t linesize = 0, n;
3150 ssize_t linelen;
3152 linelen = getline(&line, &linesize, infile);
3153 if (linelen == -1) {
3154 if (ferror(infile)) {
3155 err = got_error_from_errno("getline");
3156 goto done;
3158 return NULL;
3160 if (outfile) {
3161 n = fwrite(line, 1, linelen, outfile);
3162 if (n != linelen) {
3163 err = got_ferror(outfile, GOT_ERR_IO);
3164 goto done;
3167 if (rejectfile) {
3168 n = fwrite(line, 1, linelen, rejectfile);
3169 if (n != linelen)
3170 err = got_ferror(outfile, GOT_ERR_IO);
3172 done:
3173 free(line);
3174 return err;
3177 static const struct got_error *
3178 skip_one_line(FILE *f)
3180 char *line = NULL;
3181 size_t linesize = 0;
3182 ssize_t linelen;
3184 linelen = getline(&line, &linesize, f);
3185 if (linelen == -1) {
3186 if (ferror(f))
3187 return got_error_from_errno("getline");
3188 return NULL;
3190 free(line);
3191 return NULL;
3194 static const struct got_error *
3195 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3196 int start_old, int end_old, int start_new, int end_new,
3197 FILE *outfile, FILE *rejectfile)
3199 const struct got_error *err;
3201 /* Copy old file's lines leading up to patch. */
3202 while (!feof(f1) && *line_cur1 < start_old) {
3203 err = copy_one_line(f1, outfile, NULL);
3204 if (err)
3205 return err;
3206 (*line_cur1)++;
3208 /* Skip new file's lines leading up to patch. */
3209 while (!feof(f2) && *line_cur2 < start_new) {
3210 if (rejectfile)
3211 err = copy_one_line(f2, NULL, rejectfile);
3212 else
3213 err = skip_one_line(f2);
3214 if (err)
3215 return err;
3216 (*line_cur2)++;
3218 /* Copy patched lines. */
3219 while (!feof(f2) && *line_cur2 <= end_new) {
3220 err = copy_one_line(f2, outfile, NULL);
3221 if (err)
3222 return err;
3223 (*line_cur2)++;
3225 /* Skip over old file's replaced lines. */
3226 while (!feof(f1) && *line_cur1 <= end_old) {
3227 if (rejectfile)
3228 err = copy_one_line(f1, NULL, rejectfile);
3229 else
3230 err = skip_one_line(f1);
3231 if (err)
3232 return err;
3233 (*line_cur1)++;
3236 return NULL;
3239 static const struct got_error *
3240 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3241 FILE *outfile, FILE *rejectfile)
3243 const struct got_error *err;
3245 if (outfile) {
3246 /* Copy old file's lines until EOF. */
3247 while (!feof(f1)) {
3248 err = copy_one_line(f1, outfile, NULL);
3249 if (err)
3250 return err;
3251 (*line_cur1)++;
3254 if (rejectfile) {
3255 /* Copy new file's lines until EOF. */
3256 while (!feof(f2)) {
3257 err = copy_one_line(f2, NULL, rejectfile);
3258 if (err)
3259 return err;
3260 (*line_cur2)++;
3264 return NULL;
3267 static const struct got_error *
3268 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3269 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3270 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3271 int *line_cur2, FILE *outfile, FILE *rejectfile,
3272 got_worktree_patch_cb patch_cb, void *patch_arg)
3274 const struct got_error *err = NULL;
3275 int start_old = change->cv.a;
3276 int end_old = change->cv.b;
3277 int start_new = change->cv.c;
3278 int end_new = change->cv.d;
3279 long pos1, pos2;
3280 FILE *hunkfile;
3282 *choice = GOT_PATCH_CHOICE_NONE;
3284 hunkfile = got_opentemp();
3285 if (hunkfile == NULL)
3286 return got_error_from_errno("got_opentemp");
3288 pos1 = ftell(f1);
3289 pos2 = ftell(f2);
3291 /* XXX TODO needs error checking */
3292 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3294 if (fseek(f1, pos1, SEEK_SET) == -1) {
3295 err = got_ferror(f1, GOT_ERR_IO);
3296 goto done;
3298 if (fseek(f2, pos2, SEEK_SET) == -1) {
3299 err = got_ferror(f1, GOT_ERR_IO);
3300 goto done;
3302 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3303 err = got_ferror(hunkfile, GOT_ERR_IO);
3304 goto done;
3307 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3308 hunkfile, n, nchanges);
3309 if (err)
3310 goto done;
3312 switch (*choice) {
3313 case GOT_PATCH_CHOICE_YES:
3314 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3315 end_old, start_new, end_new, outfile, rejectfile);
3316 break;
3317 case GOT_PATCH_CHOICE_NO:
3318 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3319 end_old, start_new, end_new, rejectfile, outfile);
3320 break;
3321 case GOT_PATCH_CHOICE_QUIT:
3322 break;
3323 default:
3324 err = got_error(GOT_ERR_PATCH_CHOICE);
3325 break;
3327 done:
3328 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3329 err = got_error_from_errno("fclose");
3330 return err;
3333 struct revert_file_args {
3334 struct got_worktree *worktree;
3335 struct got_fileindex *fileindex;
3336 got_worktree_checkout_cb progress_cb;
3337 void *progress_arg;
3338 got_worktree_patch_cb patch_cb;
3339 void *patch_arg;
3340 struct got_repository *repo;
3343 static const struct got_error *
3344 create_patched_content(char **path_outfile, int reverse_patch,
3345 struct got_object_id *blob_id, const char *path2,
3346 int dirfd2, const char *de_name2,
3347 const char *relpath, struct got_repository *repo,
3348 got_worktree_patch_cb patch_cb, void *patch_arg)
3350 const struct got_error *err;
3351 struct got_blob_object *blob = NULL;
3352 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3353 int fd2 = -1;
3354 char *path1 = NULL, *id_str = NULL;
3355 struct stat sb1, sb2;
3356 struct got_diff_changes *changes = NULL;
3357 struct got_diff_state *ds = NULL;
3358 struct got_diff_args *args = NULL;
3359 struct got_diff_change *change;
3360 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3361 int n = 0;
3363 *path_outfile = NULL;
3365 err = got_object_id_str(&id_str, blob_id);
3366 if (err)
3367 return err;
3369 if (dirfd2 != -1) {
3370 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3371 if (fd2 == -1) {
3372 err = got_error_from_errno2("openat", path2);
3373 goto done;
3375 } else {
3376 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3377 if (fd2 == -1) {
3378 err = got_error_from_errno2("open", path2);
3379 goto done;
3382 if (fstat(fd2, &sb2) == -1) {
3383 err = got_error_from_errno2("fstat", path2);
3384 goto done;
3387 f2 = fdopen(fd2, "r");
3388 if (f2 == NULL) {
3389 err = got_error_from_errno2("fdopen", path2);
3390 goto done;
3392 fd2 = -1;
3394 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3395 if (err)
3396 goto done;
3398 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3399 if (err)
3400 goto done;
3402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3403 if (err)
3404 goto done;
3406 if (stat(path1, &sb1) == -1) {
3407 err = got_error_from_errno2("stat", path1);
3408 goto done;
3411 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3412 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3413 if (err)
3414 goto done;
3416 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3417 if (err)
3418 goto done;
3420 if (fseek(f1, 0L, SEEK_SET) == -1)
3421 return got_ferror(f1, GOT_ERR_IO);
3422 if (fseek(f2, 0L, SEEK_SET) == -1)
3423 return got_ferror(f2, GOT_ERR_IO);
3424 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3425 int choice;
3426 err = apply_or_reject_change(&choice, change, ++n,
3427 changes->nchanges, ds, args, diff_flags, relpath,
3428 f1, f2, &line_cur1, &line_cur2,
3429 reverse_patch ? NULL : outfile,
3430 reverse_patch ? outfile : NULL,
3431 patch_cb, patch_arg);
3432 if (err)
3433 goto done;
3434 if (choice == GOT_PATCH_CHOICE_YES)
3435 have_content = 1;
3436 else if (choice == GOT_PATCH_CHOICE_QUIT)
3437 break;
3439 if (have_content) {
3440 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3441 reverse_patch ? NULL : outfile,
3442 reverse_patch ? outfile : NULL);
3443 if (err)
3444 goto done;
3446 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3447 err = got_error_from_errno2("chmod", path2);
3448 goto done;
3451 done:
3452 free(id_str);
3453 if (blob)
3454 got_object_blob_close(blob);
3455 if (f1 && fclose(f1) == EOF && err == NULL)
3456 err = got_error_from_errno2("fclose", path1);
3457 if (f2 && fclose(f2) == EOF && err == NULL)
3458 err = got_error_from_errno2("fclose", path2);
3459 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3460 err = got_error_from_errno2("close", path2);
3461 if (outfile && fclose(outfile) == EOF && err == NULL)
3462 err = got_error_from_errno2("fclose", *path_outfile);
3463 if (path1 && unlink(path1) == -1 && err == NULL)
3464 err = got_error_from_errno2("unlink", path1);
3465 if (err || !have_content) {
3466 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3467 err = got_error_from_errno2("unlink", *path_outfile);
3468 free(*path_outfile);
3469 *path_outfile = NULL;
3471 free(args);
3472 if (ds) {
3473 got_diff_state_free(ds);
3474 free(ds);
3476 if (changes)
3477 got_diff_free_changes(changes);
3478 free(path1);
3479 return err;
3482 static const struct got_error *
3483 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3484 const char *relpath, struct got_object_id *blob_id,
3485 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3486 int dirfd, const char *de_name)
3488 struct revert_file_args *a = arg;
3489 const struct got_error *err = NULL;
3490 char *parent_path = NULL;
3491 struct got_fileindex_entry *ie;
3492 struct got_tree_object *tree = NULL;
3493 struct got_object_id *tree_id = NULL;
3494 const struct got_tree_entry *te = NULL;
3495 char *tree_path = NULL, *te_name;
3496 char *ondisk_path = NULL, *path_content = NULL;
3497 struct got_blob_object *blob = NULL;
3499 /* Reverting a staged deletion is a no-op. */
3500 if (status == GOT_STATUS_DELETE &&
3501 staged_status != GOT_STATUS_NO_CHANGE)
3502 return NULL;
3504 if (status == GOT_STATUS_UNVERSIONED)
3505 return (*a->progress_cb)(a->progress_arg,
3506 GOT_STATUS_UNVERSIONED, relpath);
3508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3509 if (ie == NULL)
3510 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3512 /* Construct in-repository path of tree which contains this blob. */
3513 err = got_path_dirname(&parent_path, ie->path);
3514 if (err) {
3515 if (err->code != GOT_ERR_BAD_PATH)
3516 goto done;
3517 parent_path = strdup("/");
3518 if (parent_path == NULL) {
3519 err = got_error_from_errno("strdup");
3520 goto done;
3523 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3524 tree_path = strdup(parent_path);
3525 if (tree_path == NULL) {
3526 err = got_error_from_errno("strdup");
3527 goto done;
3529 } else {
3530 if (got_path_is_root_dir(parent_path)) {
3531 tree_path = strdup(a->worktree->path_prefix);
3532 if (tree_path == NULL) {
3533 err = got_error_from_errno("strdup");
3534 goto done;
3536 } else {
3537 if (asprintf(&tree_path, "%s/%s",
3538 a->worktree->path_prefix, parent_path) == -1) {
3539 err = got_error_from_errno("asprintf");
3540 goto done;
3545 err = got_object_id_by_path(&tree_id, a->repo,
3546 a->worktree->base_commit_id, tree_path);
3547 if (err) {
3548 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3549 (status == GOT_STATUS_ADD ||
3550 staged_status == GOT_STATUS_ADD)))
3551 goto done;
3552 } else {
3553 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3554 if (err)
3555 goto done;
3557 te_name = basename(ie->path);
3558 if (te_name == NULL) {
3559 err = got_error_from_errno2("basename", ie->path);
3560 goto done;
3563 te = got_object_tree_find_entry(tree, te_name);
3564 if (te == NULL && status != GOT_STATUS_ADD &&
3565 staged_status != GOT_STATUS_ADD) {
3566 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3567 goto done;
3571 switch (status) {
3572 case GOT_STATUS_ADD:
3573 if (a->patch_cb) {
3574 int choice = GOT_PATCH_CHOICE_NONE;
3575 err = (*a->patch_cb)(&choice, a->patch_arg,
3576 status, ie->path, NULL, 1, 1);
3577 if (err)
3578 goto done;
3579 if (choice != GOT_PATCH_CHOICE_YES)
3580 break;
3582 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3583 ie->path);
3584 if (err)
3585 goto done;
3586 got_fileindex_entry_remove(a->fileindex, ie);
3587 break;
3588 case GOT_STATUS_DELETE:
3589 if (a->patch_cb) {
3590 int choice = GOT_PATCH_CHOICE_NONE;
3591 err = (*a->patch_cb)(&choice, a->patch_arg,
3592 status, ie->path, NULL, 1, 1);
3593 if (err)
3594 goto done;
3595 if (choice != GOT_PATCH_CHOICE_YES)
3596 break;
3598 /* fall through */
3599 case GOT_STATUS_MODIFY:
3600 case GOT_STATUS_MODE_CHANGE:
3601 case GOT_STATUS_CONFLICT:
3602 case GOT_STATUS_MISSING: {
3603 struct got_object_id id;
3604 if (staged_status == GOT_STATUS_ADD ||
3605 staged_status == GOT_STATUS_MODIFY) {
3606 memcpy(id.sha1, ie->staged_blob_sha1,
3607 SHA1_DIGEST_LENGTH);
3608 } else
3609 memcpy(id.sha1, ie->blob_sha1,
3610 SHA1_DIGEST_LENGTH);
3611 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3612 if (err)
3613 goto done;
3615 if (asprintf(&ondisk_path, "%s/%s",
3616 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3617 err = got_error_from_errno("asprintf");
3618 goto done;
3621 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3622 status == GOT_STATUS_CONFLICT)) {
3623 err = create_patched_content(&path_content, 1, &id,
3624 ondisk_path, dirfd, de_name, ie->path, a->repo,
3625 a->patch_cb, a->patch_arg);
3626 if (err || path_content == NULL)
3627 break;
3628 if (rename(path_content, ondisk_path) == -1) {
3629 err = got_error_from_errno3("rename",
3630 path_content, ondisk_path);
3631 goto done;
3633 } else {
3634 err = install_blob(a->worktree, ondisk_path, ie->path,
3635 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3636 got_fileindex_perms_to_st(ie), blob, 0, 1,
3637 a->repo, a->progress_cb, a->progress_arg);
3638 if (err)
3639 goto done;
3640 if (status == GOT_STATUS_DELETE ||
3641 status == GOT_STATUS_MODE_CHANGE) {
3642 err = update_blob_fileindex_entry(a->worktree,
3643 a->fileindex, ie, ondisk_path, ie->path,
3644 blob, 1);
3645 if (err)
3646 goto done;
3649 break;
3651 default:
3652 break;
3654 done:
3655 free(ondisk_path);
3656 free(path_content);
3657 free(parent_path);
3658 free(tree_path);
3659 if (blob)
3660 got_object_blob_close(blob);
3661 if (tree)
3662 got_object_tree_close(tree);
3663 free(tree_id);
3664 return err;
3667 const struct got_error *
3668 got_worktree_revert(struct got_worktree *worktree,
3669 struct got_pathlist_head *paths,
3670 got_worktree_checkout_cb progress_cb, void *progress_arg,
3671 got_worktree_patch_cb patch_cb, void *patch_arg,
3672 struct got_repository *repo)
3674 struct got_fileindex *fileindex = NULL;
3675 char *fileindex_path = NULL;
3676 const struct got_error *err = NULL, *unlockerr = NULL;
3677 const struct got_error *sync_err = NULL;
3678 struct got_pathlist_entry *pe;
3679 struct revert_file_args rfa;
3681 err = lock_worktree(worktree, LOCK_EX);
3682 if (err)
3683 return err;
3685 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3686 if (err)
3687 goto done;
3689 rfa.worktree = worktree;
3690 rfa.fileindex = fileindex;
3691 rfa.progress_cb = progress_cb;
3692 rfa.progress_arg = progress_arg;
3693 rfa.patch_cb = patch_cb;
3694 rfa.patch_arg = patch_arg;
3695 rfa.repo = repo;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 revert_file, &rfa, NULL, NULL, 0, 0);
3699 if (err)
3700 break;
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3704 err = sync_err;
3705 done:
3706 free(fileindex_path);
3707 if (fileindex)
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3711 err = unlockerr;
3712 return err;
3715 static void
3716 free_commitable(struct got_commitable *ct)
3718 free(ct->path);
3719 free(ct->in_repo_path);
3720 free(ct->ondisk_path);
3721 free(ct->blob_id);
3722 free(ct->base_blob_id);
3723 free(ct->staged_blob_id);
3724 free(ct->base_commit_id);
3725 free(ct);
3728 struct collect_commitables_arg {
3729 struct got_pathlist_head *commitable_paths;
3730 struct got_repository *repo;
3731 struct got_worktree *worktree;
3732 int have_staged_files;
3735 static const struct got_error *
3736 collect_commitables(void *arg, unsigned char status,
3737 unsigned char staged_status, const char *relpath,
3738 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3739 struct got_object_id *commit_id, int dirfd, const char *de_name)
3741 struct collect_commitables_arg *a = arg;
3742 const struct got_error *err = NULL;
3743 struct got_commitable *ct = NULL;
3744 struct got_pathlist_entry *new = NULL;
3745 char *parent_path = NULL, *path = NULL;
3746 struct stat sb;
3748 if (a->have_staged_files) {
3749 if (staged_status != GOT_STATUS_MODIFY &&
3750 staged_status != GOT_STATUS_ADD &&
3751 staged_status != GOT_STATUS_DELETE)
3752 return NULL;
3753 } else {
3754 if (status == GOT_STATUS_CONFLICT)
3755 return got_error(GOT_ERR_COMMIT_CONFLICT);
3757 if (status != GOT_STATUS_MODIFY &&
3758 status != GOT_STATUS_MODE_CHANGE &&
3759 status != GOT_STATUS_ADD &&
3760 status != GOT_STATUS_DELETE)
3761 return NULL;
3764 if (asprintf(&path, "/%s", relpath) == -1) {
3765 err = got_error_from_errno("asprintf");
3766 goto done;
3768 if (strcmp(path, "/") == 0) {
3769 parent_path = strdup("");
3770 if (parent_path == NULL)
3771 return got_error_from_errno("strdup");
3772 } else {
3773 err = got_path_dirname(&parent_path, path);
3774 if (err)
3775 return err;
3778 ct = calloc(1, sizeof(*ct));
3779 if (ct == NULL) {
3780 err = got_error_from_errno("calloc");
3781 goto done;
3784 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3785 relpath) == -1) {
3786 err = got_error_from_errno("asprintf");
3787 goto done;
3789 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3790 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3791 } else {
3792 if (dirfd != -1) {
3793 if (fstatat(dirfd, de_name, &sb,
3794 AT_SYMLINK_NOFOLLOW) == -1) {
3795 err = got_error_from_errno2("fstatat",
3796 ct->ondisk_path);
3797 goto done;
3799 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3800 err = got_error_from_errno2("lstat", ct->ondisk_path);
3801 goto done;
3803 ct->mode = sb.st_mode;
3806 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3807 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3808 relpath) == -1) {
3809 err = got_error_from_errno("asprintf");
3810 goto done;
3813 ct->status = status;
3814 ct->staged_status = staged_status;
3815 ct->blob_id = NULL; /* will be filled in when blob gets created */
3816 if (ct->status != GOT_STATUS_ADD &&
3817 ct->staged_status != GOT_STATUS_ADD) {
3818 ct->base_blob_id = got_object_id_dup(blob_id);
3819 if (ct->base_blob_id == NULL) {
3820 err = got_error_from_errno("got_object_id_dup");
3821 goto done;
3823 ct->base_commit_id = got_object_id_dup(commit_id);
3824 if (ct->base_commit_id == NULL) {
3825 err = got_error_from_errno("got_object_id_dup");
3826 goto done;
3829 if (ct->staged_status == GOT_STATUS_ADD ||
3830 ct->staged_status == GOT_STATUS_MODIFY) {
3831 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3832 if (ct->staged_blob_id == NULL) {
3833 err = got_error_from_errno("got_object_id_dup");
3834 goto done;
3837 ct->path = strdup(path);
3838 if (ct->path == NULL) {
3839 err = got_error_from_errno("strdup");
3840 goto done;
3842 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3843 done:
3844 if (ct && (err || new == NULL))
3845 free_commitable(ct);
3846 free(parent_path);
3847 free(path);
3848 return err;
3851 static const struct got_error *write_tree(struct got_object_id **, int *,
3852 struct got_tree_object *, const char *, struct got_pathlist_head *,
3853 got_worktree_status_cb status_cb, void *status_arg,
3854 struct got_repository *);
3856 static const struct got_error *
3857 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
3858 struct got_tree_entry *te, const char *parent_path,
3859 struct got_pathlist_head *commitable_paths,
3860 got_worktree_status_cb status_cb, void *status_arg,
3861 struct got_repository *repo)
3863 const struct got_error *err = NULL;
3864 struct got_tree_object *subtree;
3865 char *subpath;
3867 if (asprintf(&subpath, "%s%s%s", parent_path,
3868 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3869 return got_error_from_errno("asprintf");
3871 err = got_object_open_as_tree(&subtree, repo, &te->id);
3872 if (err)
3873 return err;
3875 err = write_tree(new_subtree_id, nentries, subtree, subpath,
3876 commitable_paths, status_cb, status_arg, repo);
3877 got_object_tree_close(subtree);
3878 free(subpath);
3879 return err;
3882 static const struct got_error *
3883 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3885 const struct got_error *err = NULL;
3886 char *ct_parent_path = NULL;
3888 *match = 0;
3890 if (strchr(ct->in_repo_path, '/') == NULL) {
3891 *match = got_path_is_root_dir(path);
3892 return NULL;
3895 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3896 if (err)
3897 return err;
3898 *match = (strcmp(path, ct_parent_path) == 0);
3899 free(ct_parent_path);
3900 return err;
3903 static mode_t
3904 get_ct_file_mode(struct got_commitable *ct)
3906 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3909 static const struct got_error *
3910 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3911 struct got_tree_entry *te, struct got_commitable *ct)
3913 const struct got_error *err = NULL;
3915 *new_te = NULL;
3917 err = got_object_tree_entry_dup(new_te, te);
3918 if (err)
3919 goto done;
3921 (*new_te)->mode = get_ct_file_mode(ct);
3923 if (ct->staged_status == GOT_STATUS_MODIFY)
3924 memcpy(&(*new_te)->id, ct->staged_blob_id,
3925 sizeof((*new_te)->id));
3926 else
3927 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3928 done:
3929 if (err && *new_te) {
3930 free(*new_te);
3931 *new_te = NULL;
3933 return err;
3936 static const struct got_error *
3937 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3938 struct got_commitable *ct)
3940 const struct got_error *err = NULL;
3941 char *ct_name;
3943 *new_te = NULL;
3945 *new_te = calloc(1, sizeof(**new_te));
3946 if (*new_te == NULL)
3947 return got_error_from_errno("calloc");
3949 ct_name = basename(ct->path);
3950 if (ct_name == NULL) {
3951 err = got_error_from_errno2("basename", ct->path);
3952 goto done;
3954 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3955 sizeof((*new_te)->name)) {
3956 err = got_error(GOT_ERR_NO_SPACE);
3957 goto done;
3960 (*new_te)->mode = get_ct_file_mode(ct);
3962 if (ct->staged_status == GOT_STATUS_ADD)
3963 memcpy(&(*new_te)->id, ct->staged_blob_id,
3964 sizeof((*new_te)->id));
3965 else
3966 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3967 done:
3968 if (err && *new_te) {
3969 free(*new_te);
3970 *new_te = NULL;
3972 return err;
3975 static const struct got_error *
3976 insert_tree_entry(struct got_tree_entry *new_te,
3977 struct got_pathlist_head *paths)
3979 const struct got_error *err = NULL;
3980 struct got_pathlist_entry *new_pe;
3982 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3983 if (err)
3984 return err;
3985 if (new_pe == NULL)
3986 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3987 return NULL;
3990 static const struct got_error *
3991 report_ct_status(struct got_commitable *ct,
3992 got_worktree_status_cb status_cb, void *status_arg)
3994 const char *ct_path = ct->path;
3995 unsigned char status;
3997 while (ct_path[0] == '/')
3998 ct_path++;
4000 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4001 status = ct->staged_status;
4002 else
4003 status = ct->status;
4005 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4006 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4009 static const struct got_error *
4010 match_modified_subtree(int *modified, struct got_tree_entry *te,
4011 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4013 const struct got_error *err = NULL;
4014 struct got_pathlist_entry *pe;
4015 char *te_path;
4017 *modified = 0;
4019 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4020 got_path_is_root_dir(base_tree_path) ? "" : "/",
4021 te->name) == -1)
4022 return got_error_from_errno("asprintf");
4024 TAILQ_FOREACH(pe, commitable_paths, entry) {
4025 struct got_commitable *ct = pe->data;
4026 *modified = got_path_is_child(ct->in_repo_path, te_path,
4027 strlen(te_path));
4028 if (*modified)
4029 break;
4032 free(te_path);
4033 return err;
4036 static const struct got_error *
4037 match_deleted_or_modified_ct(struct got_commitable **ctp,
4038 struct got_tree_entry *te, const char *base_tree_path,
4039 struct got_pathlist_head *commitable_paths)
4041 const struct got_error *err = NULL;
4042 struct got_pathlist_entry *pe;
4044 *ctp = NULL;
4046 TAILQ_FOREACH(pe, commitable_paths, entry) {
4047 struct got_commitable *ct = pe->data;
4048 char *ct_name = NULL;
4049 int path_matches;
4051 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4052 if (ct->status != GOT_STATUS_MODIFY &&
4053 ct->status != GOT_STATUS_MODE_CHANGE &&
4054 ct->status != GOT_STATUS_DELETE)
4055 continue;
4056 } else {
4057 if (ct->staged_status != GOT_STATUS_MODIFY &&
4058 ct->staged_status != GOT_STATUS_DELETE)
4059 continue;
4062 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4063 continue;
4065 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4066 if (err)
4067 return err;
4068 if (!path_matches)
4069 continue;
4071 ct_name = basename(pe->path);
4072 if (ct_name == NULL)
4073 return got_error_from_errno2("basename", pe->path);
4075 if (strcmp(te->name, ct_name) != 0)
4076 continue;
4078 *ctp = ct;
4079 break;
4082 return err;
4085 static const struct got_error *
4086 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4087 const char *child_path, const char *path_base_tree,
4088 struct got_pathlist_head *commitable_paths,
4089 got_worktree_status_cb status_cb, void *status_arg,
4090 struct got_repository *repo)
4092 const struct got_error *err = NULL;
4093 struct got_tree_entry *new_te;
4094 char *subtree_path;
4095 struct got_object_id *id = NULL;
4096 int nentries;
4098 *new_tep = NULL;
4100 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4101 got_path_is_root_dir(path_base_tree) ? "" : "/",
4102 child_path) == -1)
4103 return got_error_from_errno("asprintf");
4105 new_te = calloc(1, sizeof(*new_te));
4106 if (new_te == NULL)
4107 return got_error_from_errno("calloc");
4108 new_te->mode = S_IFDIR;
4110 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4111 sizeof(new_te->name)) {
4112 err = got_error(GOT_ERR_NO_SPACE);
4113 goto done;
4115 err = write_tree(&id, &nentries, NULL, subtree_path,
4116 commitable_paths, status_cb, status_arg, repo);
4117 if (err) {
4118 free(new_te);
4119 goto done;
4121 memcpy(&new_te->id, id, sizeof(new_te->id));
4122 done:
4123 free(id);
4124 free(subtree_path);
4125 if (err == NULL)
4126 *new_tep = new_te;
4127 return err;
4130 static const struct got_error *
4131 write_tree(struct got_object_id **new_tree_id, int *nentries,
4132 struct got_tree_object *base_tree, const char *path_base_tree,
4133 struct got_pathlist_head *commitable_paths,
4134 got_worktree_status_cb status_cb, void *status_arg,
4135 struct got_repository *repo)
4137 const struct got_error *err = NULL;
4138 struct got_pathlist_head paths;
4139 struct got_tree_entry *te, *new_te = NULL;
4140 struct got_pathlist_entry *pe;
4142 TAILQ_INIT(&paths);
4143 *nentries = 0;
4145 /* Insert, and recurse into, newly added entries first. */
4146 TAILQ_FOREACH(pe, commitable_paths, entry) {
4147 struct got_commitable *ct = pe->data;
4148 char *child_path = NULL, *slash;
4150 if ((ct->status != GOT_STATUS_ADD &&
4151 ct->staged_status != GOT_STATUS_ADD) ||
4152 (ct->flags & GOT_COMMITABLE_ADDED))
4153 continue;
4155 if (!got_path_is_child(pe->path, path_base_tree,
4156 strlen(path_base_tree)))
4157 continue;
4159 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4160 pe->path);
4161 if (err)
4162 goto done;
4164 slash = strchr(child_path, '/');
4165 if (slash == NULL) {
4166 err = alloc_added_blob_tree_entry(&new_te, ct);
4167 if (err)
4168 goto done;
4169 err = report_ct_status(ct, status_cb, status_arg);
4170 if (err)
4171 goto done;
4172 ct->flags |= GOT_COMMITABLE_ADDED;
4173 err = insert_tree_entry(new_te, &paths);
4174 if (err)
4175 goto done;
4176 (*nentries)++;
4177 } else {
4178 *slash = '\0'; /* trim trailing path components */
4179 if (base_tree == NULL ||
4180 got_object_tree_find_entry(base_tree, child_path)
4181 == NULL) {
4182 err = make_subtree_for_added_blob(&new_te,
4183 child_path, path_base_tree,
4184 commitable_paths, status_cb, status_arg,
4185 repo);
4186 if (err)
4187 goto done;
4188 err = insert_tree_entry(new_te, &paths);
4189 if (err)
4190 goto done;
4191 (*nentries)++;
4196 if (base_tree) {
4197 int i, nbase_entries;
4198 /* Handle modified and deleted entries. */
4199 nbase_entries = got_object_tree_get_nentries(base_tree);
4200 for (i = 0; i < nbase_entries; i++) {
4201 struct got_commitable *ct = NULL;
4203 te = got_object_tree_get_entry(base_tree, i);
4204 if (got_object_tree_entry_is_submodule(te)) {
4205 /* Entry is a submodule; just copy it. */
4206 err = got_object_tree_entry_dup(&new_te, te);
4207 if (err)
4208 goto done;
4209 err = insert_tree_entry(new_te, &paths);
4210 if (err)
4211 goto done;
4212 (*nentries)++;
4213 continue;
4216 if (S_ISDIR(te->mode)) {
4217 int modified;
4218 err = got_object_tree_entry_dup(&new_te, te);
4219 if (err)
4220 goto done;
4221 err = match_modified_subtree(&modified, te,
4222 path_base_tree, commitable_paths);
4223 if (err)
4224 goto done;
4225 /* Avoid recursion into unmodified subtrees. */
4226 if (modified) {
4227 struct got_object_id *new_id;
4228 int nsubentries;
4229 err = write_subtree(&new_id,
4230 &nsubentries, te,
4231 path_base_tree, commitable_paths,
4232 status_cb, status_arg, repo);
4233 if (err)
4234 goto done;
4235 if (nsubentries == 0) {
4236 /* All entries were deleted. */
4237 free(new_id);
4238 continue;
4240 memcpy(&new_te->id, new_id,
4241 sizeof(new_te->id));
4242 free(new_id);
4244 err = insert_tree_entry(new_te, &paths);
4245 if (err)
4246 goto done;
4247 (*nentries)++;
4248 continue;
4251 err = match_deleted_or_modified_ct(&ct, te,
4252 path_base_tree, commitable_paths);
4253 if (err)
4254 goto done;
4255 if (ct) {
4256 /* NB: Deleted entries get dropped here. */
4257 if (ct->status == GOT_STATUS_MODIFY ||
4258 ct->status == GOT_STATUS_MODE_CHANGE ||
4259 ct->staged_status == GOT_STATUS_MODIFY) {
4260 err = alloc_modified_blob_tree_entry(
4261 &new_te, te, ct);
4262 if (err)
4263 goto done;
4264 err = insert_tree_entry(new_te, &paths);
4265 if (err)
4266 goto done;
4267 (*nentries)++;
4269 err = report_ct_status(ct, status_cb,
4270 status_arg);
4271 if (err)
4272 goto done;
4273 } else {
4274 /* Entry is unchanged; just copy it. */
4275 err = got_object_tree_entry_dup(&new_te, te);
4276 if (err)
4277 goto done;
4278 err = insert_tree_entry(new_te, &paths);
4279 if (err)
4280 goto done;
4281 (*nentries)++;
4286 /* Write new list of entries; deleted entries have been dropped. */
4287 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4288 done:
4289 got_pathlist_free(&paths);
4290 return err;
4293 static const struct got_error *
4294 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4295 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4296 int have_staged_files)
4298 const struct got_error *err = NULL;
4299 struct got_pathlist_entry *pe;
4301 TAILQ_FOREACH(pe, commitable_paths, entry) {
4302 struct got_fileindex_entry *ie;
4303 struct got_commitable *ct = pe->data;
4305 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4306 if (ie) {
4307 if (ct->status == GOT_STATUS_DELETE ||
4308 ct->staged_status == GOT_STATUS_DELETE) {
4309 got_fileindex_entry_remove(fileindex, ie);
4310 } else if (ct->staged_status == GOT_STATUS_ADD ||
4311 ct->staged_status == GOT_STATUS_MODIFY) {
4312 got_fileindex_entry_stage_set(ie,
4313 GOT_FILEIDX_STAGE_NONE);
4314 err = got_fileindex_entry_update(ie,
4315 ct->ondisk_path, ct->staged_blob_id->sha1,
4316 new_base_commit_id->sha1,
4317 !have_staged_files);
4318 } else
4319 err = got_fileindex_entry_update(ie,
4320 ct->ondisk_path, ct->blob_id->sha1,
4321 new_base_commit_id->sha1,
4322 !have_staged_files);
4323 } else {
4324 err = got_fileindex_entry_alloc(&ie, pe->path);
4325 if (err)
4326 break;
4327 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4328 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4329 if (err) {
4330 got_fileindex_entry_free(ie);
4331 break;
4333 err = got_fileindex_entry_add(fileindex, ie);
4334 if (err) {
4335 got_fileindex_entry_free(ie);
4336 break;
4340 return err;
4344 static const struct got_error *
4345 check_out_of_date(const char *in_repo_path, unsigned char status,
4346 unsigned char staged_status, struct got_object_id *base_blob_id,
4347 struct got_object_id *base_commit_id,
4348 struct got_object_id *head_commit_id, struct got_repository *repo,
4349 int ood_errcode)
4351 const struct got_error *err = NULL;
4352 struct got_object_id *id = NULL;
4354 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4355 /* Trivial case: base commit == head commit */
4356 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4357 return NULL;
4359 * Ensure file content which local changes were based
4360 * on matches file content in the branch head.
4362 err = got_object_id_by_path(&id, repo, head_commit_id,
4363 in_repo_path);
4364 if (err) {
4365 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4366 err = got_error(ood_errcode);
4367 goto done;
4368 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4369 err = got_error(ood_errcode);
4370 } else {
4371 /* Require that added files don't exist in the branch head. */
4372 err = got_object_id_by_path(&id, repo, head_commit_id,
4373 in_repo_path);
4374 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4375 goto done;
4376 err = id ? got_error(ood_errcode) : NULL;
4378 done:
4379 free(id);
4380 return err;
4383 const struct got_error *
4384 commit_worktree(struct got_object_id **new_commit_id,
4385 struct got_pathlist_head *commitable_paths,
4386 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4387 const char *author, const char *committer,
4388 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4389 got_worktree_status_cb status_cb, void *status_arg,
4390 struct got_repository *repo)
4392 const struct got_error *err = NULL, *unlockerr = NULL;
4393 struct got_pathlist_entry *pe;
4394 const char *head_ref_name = NULL;
4395 struct got_commit_object *head_commit = NULL;
4396 struct got_reference *head_ref2 = NULL;
4397 struct got_object_id *head_commit_id2 = NULL;
4398 struct got_tree_object *head_tree = NULL;
4399 struct got_object_id *new_tree_id = NULL;
4400 int nentries;
4401 struct got_object_id_queue parent_ids;
4402 struct got_object_qid *pid = NULL;
4403 char *logmsg = NULL;
4405 *new_commit_id = NULL;
4407 SIMPLEQ_INIT(&parent_ids);
4409 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4410 if (err)
4411 goto done;
4413 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4414 if (err)
4415 goto done;
4417 if (commit_msg_cb != NULL) {
4418 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4419 if (err)
4420 goto done;
4423 if (logmsg == NULL || strlen(logmsg) == 0) {
4424 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4425 goto done;
4428 /* Create blobs from added and modified files and record their IDs. */
4429 TAILQ_FOREACH(pe, commitable_paths, entry) {
4430 struct got_commitable *ct = pe->data;
4431 char *ondisk_path;
4433 /* Blobs for staged files already exist. */
4434 if (ct->staged_status == GOT_STATUS_ADD ||
4435 ct->staged_status == GOT_STATUS_MODIFY)
4436 continue;
4438 if (ct->status != GOT_STATUS_ADD &&
4439 ct->status != GOT_STATUS_MODIFY &&
4440 ct->status != GOT_STATUS_MODE_CHANGE)
4441 continue;
4443 if (asprintf(&ondisk_path, "%s/%s",
4444 worktree->root_path, pe->path) == -1) {
4445 err = got_error_from_errno("asprintf");
4446 goto done;
4448 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4449 free(ondisk_path);
4450 if (err)
4451 goto done;
4454 /* Recursively write new tree objects. */
4455 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4456 commitable_paths, status_cb, status_arg, repo);
4457 if (err)
4458 goto done;
4460 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4461 if (err)
4462 goto done;
4463 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4464 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4465 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4466 got_object_qid_free(pid);
4467 if (logmsg != NULL)
4468 free(logmsg);
4469 if (err)
4470 goto done;
4472 /* Check if a concurrent commit to our branch has occurred. */
4473 head_ref_name = got_worktree_get_head_ref_name(worktree);
4474 if (head_ref_name == NULL) {
4475 err = got_error_from_errno("got_worktree_get_head_ref_name");
4476 goto done;
4478 /* Lock the reference here to prevent concurrent modification. */
4479 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4480 if (err)
4481 goto done;
4482 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4483 if (err)
4484 goto done;
4485 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4486 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4487 goto done;
4489 /* Update branch head in repository. */
4490 err = got_ref_change_ref(head_ref2, *new_commit_id);
4491 if (err)
4492 goto done;
4493 err = got_ref_write(head_ref2, repo);
4494 if (err)
4495 goto done;
4497 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4498 if (err)
4499 goto done;
4501 err = ref_base_commit(worktree, repo);
4502 if (err)
4503 goto done;
4504 done:
4505 if (head_tree)
4506 got_object_tree_close(head_tree);
4507 if (head_commit)
4508 got_object_commit_close(head_commit);
4509 free(head_commit_id2);
4510 if (head_ref2) {
4511 unlockerr = got_ref_unlock(head_ref2);
4512 if (unlockerr && err == NULL)
4513 err = unlockerr;
4514 got_ref_close(head_ref2);
4516 return err;
4519 static const struct got_error *
4520 check_path_is_commitable(const char *path,
4521 struct got_pathlist_head *commitable_paths)
4523 struct got_pathlist_entry *cpe = NULL;
4524 size_t path_len = strlen(path);
4526 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4527 struct got_commitable *ct = cpe->data;
4528 const char *ct_path = ct->path;
4530 while (ct_path[0] == '/')
4531 ct_path++;
4533 if (strcmp(path, ct_path) == 0 ||
4534 got_path_is_child(ct_path, path, path_len))
4535 break;
4538 if (cpe == NULL)
4539 return got_error_path(path, GOT_ERR_BAD_PATH);
4541 return NULL;
4544 static const struct got_error *
4545 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4547 int *have_staged_files = arg;
4549 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4550 *have_staged_files = 1;
4551 return got_error(GOT_ERR_CANCELLED);
4554 return NULL;
4557 static const struct got_error *
4558 check_non_staged_files(struct got_fileindex *fileindex,
4559 struct got_pathlist_head *paths)
4561 struct got_pathlist_entry *pe;
4562 struct got_fileindex_entry *ie;
4564 TAILQ_FOREACH(pe, paths, entry) {
4565 if (pe->path[0] == '\0')
4566 continue;
4567 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4568 if (ie == NULL)
4569 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4570 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4571 return got_error_path(pe->path,
4572 GOT_ERR_FILE_NOT_STAGED);
4575 return NULL;
4578 const struct got_error *
4579 got_worktree_commit(struct got_object_id **new_commit_id,
4580 struct got_worktree *worktree, struct got_pathlist_head *paths,
4581 const char *author, const char *committer,
4582 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4583 got_worktree_status_cb status_cb, void *status_arg,
4584 struct got_repository *repo)
4586 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4587 struct got_fileindex *fileindex = NULL;
4588 char *fileindex_path = NULL;
4589 struct got_pathlist_head commitable_paths;
4590 struct collect_commitables_arg cc_arg;
4591 struct got_pathlist_entry *pe;
4592 struct got_reference *head_ref = NULL;
4593 struct got_object_id *head_commit_id = NULL;
4594 int have_staged_files = 0;
4596 *new_commit_id = NULL;
4598 TAILQ_INIT(&commitable_paths);
4600 err = lock_worktree(worktree, LOCK_EX);
4601 if (err)
4602 goto done;
4604 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4605 if (err)
4606 goto done;
4608 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4609 if (err)
4610 goto done;
4612 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4613 if (err)
4614 goto done;
4616 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4617 &have_staged_files);
4618 if (err && err->code != GOT_ERR_CANCELLED)
4619 goto done;
4620 if (have_staged_files) {
4621 err = check_non_staged_files(fileindex, paths);
4622 if (err)
4623 goto done;
4626 cc_arg.commitable_paths = &commitable_paths;
4627 cc_arg.worktree = worktree;
4628 cc_arg.repo = repo;
4629 cc_arg.have_staged_files = have_staged_files;
4630 TAILQ_FOREACH(pe, paths, entry) {
4631 err = worktree_status(worktree, pe->path, fileindex, repo,
4632 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4633 if (err)
4634 goto done;
4637 if (TAILQ_EMPTY(&commitable_paths)) {
4638 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4639 goto done;
4642 TAILQ_FOREACH(pe, paths, entry) {
4643 err = check_path_is_commitable(pe->path, &commitable_paths);
4644 if (err)
4645 goto done;
4648 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4649 struct got_commitable *ct = pe->data;
4650 const char *ct_path = ct->in_repo_path;
4652 while (ct_path[0] == '/')
4653 ct_path++;
4654 err = check_out_of_date(ct_path, ct->status,
4655 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4656 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4657 if (err)
4658 goto done;
4662 err = commit_worktree(new_commit_id, &commitable_paths,
4663 head_commit_id, worktree, author, committer,
4664 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4665 if (err)
4666 goto done;
4668 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4669 fileindex, have_staged_files);
4670 sync_err = sync_fileindex(fileindex, fileindex_path);
4671 if (sync_err && err == NULL)
4672 err = sync_err;
4673 done:
4674 if (fileindex)
4675 got_fileindex_free(fileindex);
4676 free(fileindex_path);
4677 unlockerr = lock_worktree(worktree, LOCK_SH);
4678 if (unlockerr && err == NULL)
4679 err = unlockerr;
4680 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4681 struct got_commitable *ct = pe->data;
4682 free_commitable(ct);
4684 got_pathlist_free(&commitable_paths);
4685 return err;
4688 const char *
4689 got_commitable_get_path(struct got_commitable *ct)
4691 return ct->path;
4694 unsigned int
4695 got_commitable_get_status(struct got_commitable *ct)
4697 return ct->status;
4700 struct check_rebase_ok_arg {
4701 struct got_worktree *worktree;
4702 struct got_repository *repo;
4705 static const struct got_error *
4706 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4708 const struct got_error *err = NULL;
4709 struct check_rebase_ok_arg *a = arg;
4710 unsigned char status;
4711 struct stat sb;
4712 char *ondisk_path;
4714 /* Reject rebase of a work tree with mixed base commits. */
4715 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4716 SHA1_DIGEST_LENGTH))
4717 return got_error(GOT_ERR_MIXED_COMMITS);
4719 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4720 == -1)
4721 return got_error_from_errno("asprintf");
4723 /* Reject rebase of a work tree with modified or staged files. */
4724 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4725 free(ondisk_path);
4726 if (err)
4727 return err;
4729 if (status != GOT_STATUS_NO_CHANGE)
4730 return got_error(GOT_ERR_MODIFIED);
4731 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4732 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4734 return NULL;
4737 const struct got_error *
4738 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4739 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4740 struct got_worktree *worktree, struct got_reference *branch,
4741 struct got_repository *repo)
4743 const struct got_error *err = NULL;
4744 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4745 char *branch_ref_name = NULL;
4746 char *fileindex_path = NULL;
4747 struct check_rebase_ok_arg ok_arg;
4748 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4749 struct got_object_id *wt_branch_tip = NULL;
4751 *new_base_branch_ref = NULL;
4752 *tmp_branch = NULL;
4753 *fileindex = NULL;
4755 err = lock_worktree(worktree, LOCK_EX);
4756 if (err)
4757 return err;
4759 err = open_fileindex(fileindex, &fileindex_path, worktree);
4760 if (err)
4761 goto done;
4763 ok_arg.worktree = worktree;
4764 ok_arg.repo = repo;
4765 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4766 &ok_arg);
4767 if (err)
4768 goto done;
4770 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4771 if (err)
4772 goto done;
4774 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4775 if (err)
4776 goto done;
4778 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4779 if (err)
4780 goto done;
4782 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4783 0);
4784 if (err)
4785 goto done;
4787 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4788 if (err)
4789 goto done;
4790 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4791 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4792 goto done;
4795 err = got_ref_alloc_symref(new_base_branch_ref,
4796 new_base_branch_ref_name, wt_branch);
4797 if (err)
4798 goto done;
4799 err = got_ref_write(*new_base_branch_ref, repo);
4800 if (err)
4801 goto done;
4803 /* TODO Lock original branch's ref while rebasing? */
4805 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4806 if (err)
4807 goto done;
4809 err = got_ref_write(branch_ref, repo);
4810 if (err)
4811 goto done;
4813 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4814 worktree->base_commit_id);
4815 if (err)
4816 goto done;
4817 err = got_ref_write(*tmp_branch, repo);
4818 if (err)
4819 goto done;
4821 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4822 if (err)
4823 goto done;
4824 done:
4825 free(fileindex_path);
4826 free(tmp_branch_name);
4827 free(new_base_branch_ref_name);
4828 free(branch_ref_name);
4829 if (branch_ref)
4830 got_ref_close(branch_ref);
4831 if (wt_branch)
4832 got_ref_close(wt_branch);
4833 free(wt_branch_tip);
4834 if (err) {
4835 if (*new_base_branch_ref) {
4836 got_ref_close(*new_base_branch_ref);
4837 *new_base_branch_ref = NULL;
4839 if (*tmp_branch) {
4840 got_ref_close(*tmp_branch);
4841 *tmp_branch = NULL;
4843 if (*fileindex) {
4844 got_fileindex_free(*fileindex);
4845 *fileindex = NULL;
4847 lock_worktree(worktree, LOCK_SH);
4849 return err;
4852 const struct got_error *
4853 got_worktree_rebase_continue(struct got_object_id **commit_id,
4854 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4855 struct got_reference **branch, struct got_fileindex **fileindex,
4856 struct got_worktree *worktree, struct got_repository *repo)
4858 const struct got_error *err;
4859 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4860 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4861 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4862 char *fileindex_path = NULL;
4863 int have_staged_files = 0;
4865 *commit_id = NULL;
4866 *new_base_branch = NULL;
4867 *tmp_branch = NULL;
4868 *branch = NULL;
4869 *fileindex = NULL;
4871 err = lock_worktree(worktree, LOCK_EX);
4872 if (err)
4873 return err;
4875 err = open_fileindex(fileindex, &fileindex_path, worktree);
4876 if (err)
4877 goto done;
4879 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4880 &have_staged_files);
4881 if (err && err->code != GOT_ERR_CANCELLED)
4882 goto done;
4883 if (have_staged_files) {
4884 err = got_error(GOT_ERR_STAGED_PATHS);
4885 goto done;
4888 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4889 if (err)
4890 goto done;
4892 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4893 if (err)
4894 goto done;
4896 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4897 if (err)
4898 goto done;
4900 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4901 if (err)
4902 goto done;
4904 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4905 if (err)
4906 goto done;
4908 err = got_ref_open(branch, repo,
4909 got_ref_get_symref_target(branch_ref), 0);
4910 if (err)
4911 goto done;
4913 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4914 if (err)
4915 goto done;
4917 err = got_ref_resolve(commit_id, repo, commit_ref);
4918 if (err)
4919 goto done;
4921 err = got_ref_open(new_base_branch, repo,
4922 new_base_branch_ref_name, 0);
4923 if (err)
4924 goto done;
4926 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4927 if (err)
4928 goto done;
4929 done:
4930 free(commit_ref_name);
4931 free(branch_ref_name);
4932 free(fileindex_path);
4933 if (commit_ref)
4934 got_ref_close(commit_ref);
4935 if (branch_ref)
4936 got_ref_close(branch_ref);
4937 if (err) {
4938 free(*commit_id);
4939 *commit_id = NULL;
4940 if (*tmp_branch) {
4941 got_ref_close(*tmp_branch);
4942 *tmp_branch = NULL;
4944 if (*new_base_branch) {
4945 got_ref_close(*new_base_branch);
4946 *new_base_branch = NULL;
4948 if (*branch) {
4949 got_ref_close(*branch);
4950 *branch = NULL;
4952 if (*fileindex) {
4953 got_fileindex_free(*fileindex);
4954 *fileindex = NULL;
4956 lock_worktree(worktree, LOCK_SH);
4958 return err;
4961 const struct got_error *
4962 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4964 const struct got_error *err;
4965 char *tmp_branch_name = NULL;
4967 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4968 if (err)
4969 return err;
4971 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4972 free(tmp_branch_name);
4973 return NULL;
4976 static const struct got_error *
4977 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4978 char **logmsg, void *arg)
4980 *logmsg = arg;
4981 return NULL;
4984 static const struct got_error *
4985 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4986 const char *path, struct got_object_id *blob_id,
4987 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4988 int dirfd, const char *de_name)
4990 return NULL;
4993 struct collect_merged_paths_arg {
4994 got_worktree_checkout_cb progress_cb;
4995 void *progress_arg;
4996 struct got_pathlist_head *merged_paths;
4999 static const struct got_error *
5000 collect_merged_paths(void *arg, unsigned char status, const char *path)
5002 const struct got_error *err;
5003 struct collect_merged_paths_arg *a = arg;
5004 char *p;
5005 struct got_pathlist_entry *new;
5007 err = (*a->progress_cb)(a->progress_arg, status, path);
5008 if (err)
5009 return err;
5011 if (status != GOT_STATUS_MERGE &&
5012 status != GOT_STATUS_ADD &&
5013 status != GOT_STATUS_DELETE &&
5014 status != GOT_STATUS_CONFLICT)
5015 return NULL;
5017 p = strdup(path);
5018 if (p == NULL)
5019 return got_error_from_errno("strdup");
5021 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5022 if (err || new == NULL)
5023 free(p);
5024 return err;
5027 void
5028 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5030 struct got_pathlist_entry *pe;
5032 TAILQ_FOREACH(pe, merged_paths, entry)
5033 free((char *)pe->path);
5035 got_pathlist_free(merged_paths);
5038 static const struct got_error *
5039 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5040 int is_rebase, struct got_repository *repo)
5042 const struct got_error *err;
5043 struct got_reference *commit_ref = NULL;
5045 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5046 if (err) {
5047 if (err->code != GOT_ERR_NOT_REF)
5048 goto done;
5049 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5050 if (err)
5051 goto done;
5052 err = got_ref_write(commit_ref, repo);
5053 if (err)
5054 goto done;
5055 } else if (is_rebase) {
5056 struct got_object_id *stored_id;
5057 int cmp;
5059 err = got_ref_resolve(&stored_id, repo, commit_ref);
5060 if (err)
5061 goto done;
5062 cmp = got_object_id_cmp(commit_id, stored_id);
5063 free(stored_id);
5064 if (cmp != 0) {
5065 err = got_error(GOT_ERR_REBASE_COMMITID);
5066 goto done;
5069 done:
5070 if (commit_ref)
5071 got_ref_close(commit_ref);
5072 return err;
5075 static const struct got_error *
5076 rebase_merge_files(struct got_pathlist_head *merged_paths,
5077 const char *commit_ref_name, struct got_worktree *worktree,
5078 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5079 struct got_object_id *commit_id, struct got_repository *repo,
5080 got_worktree_checkout_cb progress_cb, void *progress_arg,
5081 got_cancel_cb cancel_cb, void *cancel_arg)
5083 const struct got_error *err;
5084 struct got_reference *commit_ref = NULL;
5085 struct collect_merged_paths_arg cmp_arg;
5086 char *fileindex_path;
5088 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5090 err = get_fileindex_path(&fileindex_path, worktree);
5091 if (err)
5092 return err;
5094 cmp_arg.progress_cb = progress_cb;
5095 cmp_arg.progress_arg = progress_arg;
5096 cmp_arg.merged_paths = merged_paths;
5097 err = merge_files(worktree, fileindex, fileindex_path,
5098 parent_commit_id, commit_id, repo, collect_merged_paths,
5099 &cmp_arg, cancel_cb, cancel_arg);
5100 if (commit_ref)
5101 got_ref_close(commit_ref);
5102 return err;
5105 const struct got_error *
5106 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5107 struct got_worktree *worktree, struct got_fileindex *fileindex,
5108 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5109 struct got_repository *repo,
5110 got_worktree_checkout_cb progress_cb, void *progress_arg,
5111 got_cancel_cb cancel_cb, void *cancel_arg)
5113 const struct got_error *err;
5114 char *commit_ref_name;
5116 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5117 if (err)
5118 return err;
5120 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5121 if (err)
5122 goto done;
5124 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5125 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5126 progress_arg, cancel_cb, cancel_arg);
5127 done:
5128 free(commit_ref_name);
5129 return err;
5132 const struct got_error *
5133 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5134 struct got_worktree *worktree, struct got_fileindex *fileindex,
5135 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5136 struct got_repository *repo,
5137 got_worktree_checkout_cb progress_cb, void *progress_arg,
5138 got_cancel_cb cancel_cb, void *cancel_arg)
5140 const struct got_error *err;
5141 char *commit_ref_name;
5143 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5144 if (err)
5145 return err;
5147 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5148 if (err)
5149 goto done;
5151 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5152 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5153 progress_arg, cancel_cb, cancel_arg);
5154 done:
5155 free(commit_ref_name);
5156 return err;
5159 static const struct got_error *
5160 rebase_commit(struct got_object_id **new_commit_id,
5161 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5162 struct got_worktree *worktree, struct got_fileindex *fileindex,
5163 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5164 const char *new_logmsg, struct got_repository *repo)
5166 const struct got_error *err, *sync_err;
5167 struct got_pathlist_head commitable_paths;
5168 struct collect_commitables_arg cc_arg;
5169 char *fileindex_path = NULL;
5170 struct got_reference *head_ref = NULL;
5171 struct got_object_id *head_commit_id = NULL;
5172 char *logmsg = NULL;
5174 TAILQ_INIT(&commitable_paths);
5175 *new_commit_id = NULL;
5177 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5179 err = get_fileindex_path(&fileindex_path, worktree);
5180 if (err)
5181 return err;
5183 cc_arg.commitable_paths = &commitable_paths;
5184 cc_arg.worktree = worktree;
5185 cc_arg.repo = repo;
5186 cc_arg.have_staged_files = 0;
5188 * If possible get the status of individual files directly to
5189 * avoid crawling the entire work tree once per rebased commit.
5190 * TODO: Ideally, merged_paths would contain a list of commitables
5191 * we could use so we could skip worktree_status() entirely.
5193 if (merged_paths) {
5194 struct got_pathlist_entry *pe;
5195 TAILQ_FOREACH(pe, merged_paths, entry) {
5196 err = worktree_status(worktree, pe->path, fileindex,
5197 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5198 0);
5199 if (err)
5200 goto done;
5202 } else {
5203 err = worktree_status(worktree, "", fileindex, repo,
5204 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5205 if (err)
5206 goto done;
5209 if (TAILQ_EMPTY(&commitable_paths)) {
5210 /* No-op change; commit will be elided. */
5211 err = got_ref_delete(commit_ref, repo);
5212 if (err)
5213 goto done;
5214 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5215 goto done;
5218 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5219 if (err)
5220 goto done;
5222 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5223 if (err)
5224 goto done;
5226 if (new_logmsg) {
5227 logmsg = strdup(new_logmsg);
5228 if (logmsg == NULL) {
5229 err = got_error_from_errno("strdup");
5230 goto done;
5232 } else {
5233 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5234 if (err)
5235 goto done;
5238 /* NB: commit_worktree will call free(logmsg) */
5239 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5240 worktree, got_object_commit_get_author(orig_commit),
5241 got_object_commit_get_committer(orig_commit),
5242 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5243 if (err)
5244 goto done;
5246 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5247 if (err)
5248 goto done;
5250 err = got_ref_delete(commit_ref, repo);
5251 if (err)
5252 goto done;
5254 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5255 fileindex, 0);
5256 sync_err = sync_fileindex(fileindex, fileindex_path);
5257 if (sync_err && err == NULL)
5258 err = sync_err;
5259 done:
5260 free(fileindex_path);
5261 free(head_commit_id);
5262 if (head_ref)
5263 got_ref_close(head_ref);
5264 if (err) {
5265 free(*new_commit_id);
5266 *new_commit_id = NULL;
5268 return err;
5271 const struct got_error *
5272 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5273 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5274 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5275 struct got_commit_object *orig_commit,
5276 struct got_object_id *orig_commit_id, struct got_repository *repo)
5278 const struct got_error *err;
5279 char *commit_ref_name;
5280 struct got_reference *commit_ref = NULL;
5281 struct got_object_id *commit_id = NULL;
5283 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5284 if (err)
5285 return err;
5287 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5288 if (err)
5289 goto done;
5290 err = got_ref_resolve(&commit_id, repo, commit_ref);
5291 if (err)
5292 goto done;
5293 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5294 err = got_error(GOT_ERR_REBASE_COMMITID);
5295 goto done;
5298 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5299 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5300 done:
5301 if (commit_ref)
5302 got_ref_close(commit_ref);
5303 free(commit_ref_name);
5304 free(commit_id);
5305 return err;
5308 const struct got_error *
5309 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5310 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5311 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5312 struct got_commit_object *orig_commit,
5313 struct got_object_id *orig_commit_id, const char *new_logmsg,
5314 struct got_repository *repo)
5316 const struct got_error *err;
5317 char *commit_ref_name;
5318 struct got_reference *commit_ref = NULL;
5320 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5321 if (err)
5322 return err;
5324 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5325 if (err)
5326 goto done;
5328 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5329 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5330 done:
5331 if (commit_ref)
5332 got_ref_close(commit_ref);
5333 free(commit_ref_name);
5334 return err;
5337 const struct got_error *
5338 got_worktree_rebase_postpone(struct got_worktree *worktree,
5339 struct got_fileindex *fileindex)
5341 if (fileindex)
5342 got_fileindex_free(fileindex);
5343 return lock_worktree(worktree, LOCK_SH);
5346 static const struct got_error *
5347 delete_ref(const char *name, struct got_repository *repo)
5349 const struct got_error *err;
5350 struct got_reference *ref;
5352 err = got_ref_open(&ref, repo, name, 0);
5353 if (err) {
5354 if (err->code == GOT_ERR_NOT_REF)
5355 return NULL;
5356 return err;
5359 err = got_ref_delete(ref, repo);
5360 got_ref_close(ref);
5361 return err;
5364 static const struct got_error *
5365 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 const struct got_error *err;
5368 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5369 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5372 if (err)
5373 goto done;
5374 err = delete_ref(tmp_branch_name, repo);
5375 if (err)
5376 goto done;
5378 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5379 if (err)
5380 goto done;
5381 err = delete_ref(new_base_branch_ref_name, repo);
5382 if (err)
5383 goto done;
5385 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5386 if (err)
5387 goto done;
5388 err = delete_ref(branch_ref_name, repo);
5389 if (err)
5390 goto done;
5392 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5393 if (err)
5394 goto done;
5395 err = delete_ref(commit_ref_name, repo);
5396 if (err)
5397 goto done;
5399 done:
5400 free(tmp_branch_name);
5401 free(new_base_branch_ref_name);
5402 free(branch_ref_name);
5403 free(commit_ref_name);
5404 return err;
5407 const struct got_error *
5408 got_worktree_rebase_complete(struct got_worktree *worktree,
5409 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5410 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5411 struct got_repository *repo)
5413 const struct got_error *err, *unlockerr;
5414 struct got_object_id *new_head_commit_id = NULL;
5416 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5417 if (err)
5418 return err;
5420 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5421 if (err)
5422 goto done;
5424 err = got_ref_write(rebased_branch, repo);
5425 if (err)
5426 goto done;
5428 err = got_worktree_set_head_ref(worktree, rebased_branch);
5429 if (err)
5430 goto done;
5432 err = delete_rebase_refs(worktree, repo);
5433 done:
5434 if (fileindex)
5435 got_fileindex_free(fileindex);
5436 free(new_head_commit_id);
5437 unlockerr = lock_worktree(worktree, LOCK_SH);
5438 if (unlockerr && err == NULL)
5439 err = unlockerr;
5440 return err;
5443 const struct got_error *
5444 got_worktree_rebase_abort(struct got_worktree *worktree,
5445 struct got_fileindex *fileindex, struct got_repository *repo,
5446 struct got_reference *new_base_branch,
5447 got_worktree_checkout_cb progress_cb, void *progress_arg)
5449 const struct got_error *err, *unlockerr, *sync_err;
5450 struct got_reference *resolved = NULL;
5451 struct got_object_id *commit_id = NULL;
5452 char *fileindex_path = NULL;
5453 struct revert_file_args rfa;
5454 struct got_object_id *tree_id = NULL;
5456 err = lock_worktree(worktree, LOCK_EX);
5457 if (err)
5458 return err;
5460 err = got_ref_open(&resolved, repo,
5461 got_ref_get_symref_target(new_base_branch), 0);
5462 if (err)
5463 goto done;
5465 err = got_worktree_set_head_ref(worktree, resolved);
5466 if (err)
5467 goto done;
5470 * XXX commits to the base branch could have happened while
5471 * we were busy rebasing; should we store the original commit ID
5472 * when rebase begins and read it back here?
5474 err = got_ref_resolve(&commit_id, repo, resolved);
5475 if (err)
5476 goto done;
5478 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5479 if (err)
5480 goto done;
5482 err = got_object_id_by_path(&tree_id, repo,
5483 worktree->base_commit_id, worktree->path_prefix);
5484 if (err)
5485 goto done;
5487 err = delete_rebase_refs(worktree, repo);
5488 if (err)
5489 goto done;
5491 err = get_fileindex_path(&fileindex_path, worktree);
5492 if (err)
5493 goto done;
5495 rfa.worktree = worktree;
5496 rfa.fileindex = fileindex;
5497 rfa.progress_cb = progress_cb;
5498 rfa.progress_arg = progress_arg;
5499 rfa.patch_cb = NULL;
5500 rfa.patch_arg = NULL;
5501 rfa.repo = repo;
5502 err = worktree_status(worktree, "", fileindex, repo,
5503 revert_file, &rfa, NULL, NULL, 0, 0);
5504 if (err)
5505 goto sync;
5507 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5508 repo, progress_cb, progress_arg, NULL, NULL);
5509 sync:
5510 sync_err = sync_fileindex(fileindex, fileindex_path);
5511 if (sync_err && err == NULL)
5512 err = sync_err;
5513 done:
5514 got_ref_close(resolved);
5515 free(tree_id);
5516 free(commit_id);
5517 if (fileindex)
5518 got_fileindex_free(fileindex);
5519 free(fileindex_path);
5521 unlockerr = lock_worktree(worktree, LOCK_SH);
5522 if (unlockerr && err == NULL)
5523 err = unlockerr;
5524 return err;
5527 const struct got_error *
5528 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5529 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5530 struct got_fileindex **fileindex, struct got_worktree *worktree,
5531 struct got_repository *repo)
5533 const struct got_error *err = NULL;
5534 char *tmp_branch_name = NULL;
5535 char *branch_ref_name = NULL;
5536 char *base_commit_ref_name = NULL;
5537 char *fileindex_path = NULL;
5538 struct check_rebase_ok_arg ok_arg;
5539 struct got_reference *wt_branch = NULL;
5540 struct got_reference *base_commit_ref = NULL;
5542 *tmp_branch = NULL;
5543 *branch_ref = NULL;
5544 *base_commit_id = NULL;
5545 *fileindex = NULL;
5547 err = lock_worktree(worktree, LOCK_EX);
5548 if (err)
5549 return err;
5551 err = open_fileindex(fileindex, &fileindex_path, worktree);
5552 if (err)
5553 goto done;
5555 ok_arg.worktree = worktree;
5556 ok_arg.repo = repo;
5557 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5558 &ok_arg);
5559 if (err)
5560 goto done;
5562 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5563 if (err)
5564 goto done;
5566 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5567 if (err)
5568 goto done;
5570 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5571 worktree);
5572 if (err)
5573 goto done;
5575 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5576 0);
5577 if (err)
5578 goto done;
5580 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5581 if (err)
5582 goto done;
5584 err = got_ref_write(*branch_ref, repo);
5585 if (err)
5586 goto done;
5588 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5589 worktree->base_commit_id);
5590 if (err)
5591 goto done;
5592 err = got_ref_write(base_commit_ref, repo);
5593 if (err)
5594 goto done;
5595 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5596 if (*base_commit_id == NULL) {
5597 err = got_error_from_errno("got_object_id_dup");
5598 goto done;
5601 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5602 worktree->base_commit_id);
5603 if (err)
5604 goto done;
5605 err = got_ref_write(*tmp_branch, repo);
5606 if (err)
5607 goto done;
5609 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5610 if (err)
5611 goto done;
5612 done:
5613 free(fileindex_path);
5614 free(tmp_branch_name);
5615 free(branch_ref_name);
5616 free(base_commit_ref_name);
5617 if (wt_branch)
5618 got_ref_close(wt_branch);
5619 if (err) {
5620 if (*branch_ref) {
5621 got_ref_close(*branch_ref);
5622 *branch_ref = NULL;
5624 if (*tmp_branch) {
5625 got_ref_close(*tmp_branch);
5626 *tmp_branch = NULL;
5628 free(*base_commit_id);
5629 if (*fileindex) {
5630 got_fileindex_free(*fileindex);
5631 *fileindex = NULL;
5633 lock_worktree(worktree, LOCK_SH);
5635 return err;
5638 const struct got_error *
5639 got_worktree_histedit_postpone(struct got_worktree *worktree,
5640 struct got_fileindex *fileindex)
5642 if (fileindex)
5643 got_fileindex_free(fileindex);
5644 return lock_worktree(worktree, LOCK_SH);
5647 const struct got_error *
5648 got_worktree_histedit_in_progress(int *in_progress,
5649 struct got_worktree *worktree)
5651 const struct got_error *err;
5652 char *tmp_branch_name = NULL;
5654 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5655 if (err)
5656 return err;
5658 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5659 free(tmp_branch_name);
5660 return NULL;
5663 const struct got_error *
5664 got_worktree_histedit_continue(struct got_object_id **commit_id,
5665 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5666 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5667 struct got_worktree *worktree, struct got_repository *repo)
5669 const struct got_error *err;
5670 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5671 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5672 struct got_reference *commit_ref = NULL;
5673 struct got_reference *base_commit_ref = NULL;
5674 char *fileindex_path = NULL;
5675 int have_staged_files = 0;
5677 *commit_id = NULL;
5678 *tmp_branch = NULL;
5679 *base_commit_id = NULL;
5680 *fileindex = NULL;
5682 err = lock_worktree(worktree, LOCK_EX);
5683 if (err)
5684 return err;
5686 err = open_fileindex(fileindex, &fileindex_path, worktree);
5687 if (err)
5688 goto done;
5690 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5691 &have_staged_files);
5692 if (err && err->code != GOT_ERR_CANCELLED)
5693 goto done;
5694 if (have_staged_files) {
5695 err = got_error(GOT_ERR_STAGED_PATHS);
5696 goto done;
5699 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5700 if (err)
5701 goto done;
5703 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5704 if (err)
5705 goto done;
5707 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5708 if (err)
5709 goto done;
5711 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5712 worktree);
5713 if (err)
5714 goto done;
5716 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5717 if (err)
5718 goto done;
5720 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5721 if (err)
5722 goto done;
5723 err = got_ref_resolve(commit_id, repo, commit_ref);
5724 if (err)
5725 goto done;
5727 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5728 if (err)
5729 goto done;
5730 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5731 if (err)
5732 goto done;
5734 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5735 if (err)
5736 goto done;
5737 done:
5738 free(commit_ref_name);
5739 free(branch_ref_name);
5740 free(fileindex_path);
5741 if (commit_ref)
5742 got_ref_close(commit_ref);
5743 if (base_commit_ref)
5744 got_ref_close(base_commit_ref);
5745 if (err) {
5746 free(*commit_id);
5747 *commit_id = NULL;
5748 free(*base_commit_id);
5749 *base_commit_id = NULL;
5750 if (*tmp_branch) {
5751 got_ref_close(*tmp_branch);
5752 *tmp_branch = NULL;
5754 if (*fileindex) {
5755 got_fileindex_free(*fileindex);
5756 *fileindex = NULL;
5758 lock_worktree(worktree, LOCK_EX);
5760 return err;
5763 static const struct got_error *
5764 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5766 const struct got_error *err;
5767 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5768 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5770 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5771 if (err)
5772 goto done;
5773 err = delete_ref(tmp_branch_name, repo);
5774 if (err)
5775 goto done;
5777 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5778 worktree);
5779 if (err)
5780 goto done;
5781 err = delete_ref(base_commit_ref_name, repo);
5782 if (err)
5783 goto done;
5785 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5786 if (err)
5787 goto done;
5788 err = delete_ref(branch_ref_name, repo);
5789 if (err)
5790 goto done;
5792 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5793 if (err)
5794 goto done;
5795 err = delete_ref(commit_ref_name, repo);
5796 if (err)
5797 goto done;
5798 done:
5799 free(tmp_branch_name);
5800 free(base_commit_ref_name);
5801 free(branch_ref_name);
5802 free(commit_ref_name);
5803 return err;
5806 const struct got_error *
5807 got_worktree_histedit_abort(struct got_worktree *worktree,
5808 struct got_fileindex *fileindex, struct got_repository *repo,
5809 struct got_reference *branch, struct got_object_id *base_commit_id,
5810 got_worktree_checkout_cb progress_cb, void *progress_arg)
5812 const struct got_error *err, *unlockerr, *sync_err;
5813 struct got_reference *resolved = NULL;
5814 char *fileindex_path = NULL;
5815 struct got_object_id *tree_id = NULL;
5816 struct revert_file_args rfa;
5818 err = lock_worktree(worktree, LOCK_EX);
5819 if (err)
5820 return err;
5822 err = got_ref_open(&resolved, repo,
5823 got_ref_get_symref_target(branch), 0);
5824 if (err)
5825 goto done;
5827 err = got_worktree_set_head_ref(worktree, resolved);
5828 if (err)
5829 goto done;
5831 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5832 if (err)
5833 goto done;
5835 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5836 worktree->path_prefix);
5837 if (err)
5838 goto done;
5840 err = delete_histedit_refs(worktree, repo);
5841 if (err)
5842 goto done;
5844 err = get_fileindex_path(&fileindex_path, worktree);
5845 if (err)
5846 goto done;
5848 rfa.worktree = worktree;
5849 rfa.fileindex = fileindex;
5850 rfa.progress_cb = progress_cb;
5851 rfa.progress_arg = progress_arg;
5852 rfa.patch_cb = NULL;
5853 rfa.patch_arg = NULL;
5854 rfa.repo = repo;
5855 err = worktree_status(worktree, "", fileindex, repo,
5856 revert_file, &rfa, NULL, NULL, 0, 0);
5857 if (err)
5858 goto sync;
5860 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5861 repo, progress_cb, progress_arg, NULL, NULL);
5862 sync:
5863 sync_err = sync_fileindex(fileindex, fileindex_path);
5864 if (sync_err && err == NULL)
5865 err = sync_err;
5866 done:
5867 got_ref_close(resolved);
5868 free(tree_id);
5869 free(fileindex_path);
5871 unlockerr = lock_worktree(worktree, LOCK_SH);
5872 if (unlockerr && err == NULL)
5873 err = unlockerr;
5874 return err;
5877 const struct got_error *
5878 got_worktree_histedit_complete(struct got_worktree *worktree,
5879 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5880 struct got_reference *edited_branch, struct got_repository *repo)
5882 const struct got_error *err, *unlockerr;
5883 struct got_object_id *new_head_commit_id = NULL;
5884 struct got_reference *resolved = NULL;
5886 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5887 if (err)
5888 return err;
5890 err = got_ref_open(&resolved, repo,
5891 got_ref_get_symref_target(edited_branch), 0);
5892 if (err)
5893 goto done;
5895 err = got_ref_change_ref(resolved, new_head_commit_id);
5896 if (err)
5897 goto done;
5899 err = got_ref_write(resolved, repo);
5900 if (err)
5901 goto done;
5903 err = got_worktree_set_head_ref(worktree, resolved);
5904 if (err)
5905 goto done;
5907 err = delete_histedit_refs(worktree, repo);
5908 done:
5909 if (fileindex)
5910 got_fileindex_free(fileindex);
5911 free(new_head_commit_id);
5912 unlockerr = lock_worktree(worktree, LOCK_SH);
5913 if (unlockerr && err == NULL)
5914 err = unlockerr;
5915 return err;
5918 const struct got_error *
5919 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5920 struct got_object_id *commit_id, struct got_repository *repo)
5922 const struct got_error *err;
5923 char *commit_ref_name;
5925 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5926 if (err)
5927 return err;
5929 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5930 if (err)
5931 goto done;
5933 err = delete_ref(commit_ref_name, repo);
5934 done:
5935 free(commit_ref_name);
5936 return err;
5939 const struct got_error *
5940 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5941 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5942 struct got_worktree *worktree, const char *refname,
5943 struct got_repository *repo)
5945 const struct got_error *err = NULL;
5946 char *fileindex_path = NULL;
5947 struct check_rebase_ok_arg ok_arg;
5949 *fileindex = NULL;
5950 *branch_ref = NULL;
5951 *base_branch_ref = NULL;
5953 err = lock_worktree(worktree, LOCK_EX);
5954 if (err)
5955 return err;
5957 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5958 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5959 "cannot integrate a branch into itself; "
5960 "update -b or different branch name required");
5961 goto done;
5964 err = open_fileindex(fileindex, &fileindex_path, worktree);
5965 if (err)
5966 goto done;
5968 /* Preconditions are the same as for rebase. */
5969 ok_arg.worktree = worktree;
5970 ok_arg.repo = repo;
5971 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5972 &ok_arg);
5973 if (err)
5974 goto done;
5976 err = got_ref_open(branch_ref, repo, refname, 1);
5977 if (err)
5978 goto done;
5980 err = got_ref_open(base_branch_ref, repo,
5981 got_worktree_get_head_ref_name(worktree), 1);
5982 done:
5983 if (err) {
5984 if (*branch_ref) {
5985 got_ref_close(*branch_ref);
5986 *branch_ref = NULL;
5988 if (*base_branch_ref) {
5989 got_ref_close(*base_branch_ref);
5990 *base_branch_ref = NULL;
5992 if (*fileindex) {
5993 got_fileindex_free(*fileindex);
5994 *fileindex = NULL;
5996 lock_worktree(worktree, LOCK_SH);
5998 return err;
6001 const struct got_error *
6002 got_worktree_integrate_continue(struct got_worktree *worktree,
6003 struct got_fileindex *fileindex, struct got_repository *repo,
6004 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6005 got_worktree_checkout_cb progress_cb, void *progress_arg,
6006 got_cancel_cb cancel_cb, void *cancel_arg)
6008 const struct got_error *err = NULL, *sync_err, *unlockerr;
6009 char *fileindex_path = NULL;
6010 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6012 err = get_fileindex_path(&fileindex_path, worktree);
6013 if (err)
6014 goto done;
6016 err = got_ref_resolve(&commit_id, repo, branch_ref);
6017 if (err)
6018 goto done;
6020 err = got_object_id_by_path(&tree_id, repo, commit_id,
6021 worktree->path_prefix);
6022 if (err)
6023 goto done;
6025 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6026 if (err)
6027 goto done;
6029 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6030 progress_cb, progress_arg, cancel_cb, cancel_arg);
6031 if (err)
6032 goto sync;
6034 err = got_ref_change_ref(base_branch_ref, commit_id);
6035 if (err)
6036 goto sync;
6038 err = got_ref_write(base_branch_ref, repo);
6039 sync:
6040 sync_err = sync_fileindex(fileindex, fileindex_path);
6041 if (sync_err && err == NULL)
6042 err = sync_err;
6044 done:
6045 unlockerr = got_ref_unlock(branch_ref);
6046 if (unlockerr && err == NULL)
6047 err = unlockerr;
6048 got_ref_close(branch_ref);
6050 unlockerr = got_ref_unlock(base_branch_ref);
6051 if (unlockerr && err == NULL)
6052 err = unlockerr;
6053 got_ref_close(base_branch_ref);
6055 got_fileindex_free(fileindex);
6056 free(fileindex_path);
6057 free(tree_id);
6059 unlockerr = lock_worktree(worktree, LOCK_SH);
6060 if (unlockerr && err == NULL)
6061 err = unlockerr;
6062 return err;
6065 const struct got_error *
6066 got_worktree_integrate_abort(struct got_worktree *worktree,
6067 struct got_fileindex *fileindex, struct got_repository *repo,
6068 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6070 const struct got_error *err = NULL, *unlockerr = NULL;
6072 got_fileindex_free(fileindex);
6074 err = lock_worktree(worktree, LOCK_SH);
6076 unlockerr = got_ref_unlock(branch_ref);
6077 if (unlockerr && err == NULL)
6078 err = unlockerr;
6079 got_ref_close(branch_ref);
6081 unlockerr = got_ref_unlock(base_branch_ref);
6082 if (unlockerr && err == NULL)
6083 err = unlockerr;
6084 got_ref_close(base_branch_ref);
6086 return err;
6089 struct check_stage_ok_arg {
6090 struct got_object_id *head_commit_id;
6091 struct got_worktree *worktree;
6092 struct got_fileindex *fileindex;
6093 struct got_repository *repo;
6094 int have_changes;
6097 const struct got_error *
6098 check_stage_ok(void *arg, unsigned char status,
6099 unsigned char staged_status, const char *relpath,
6100 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6101 struct got_object_id *commit_id, int dirfd, const char *de_name)
6103 struct check_stage_ok_arg *a = arg;
6104 const struct got_error *err = NULL;
6105 struct got_fileindex_entry *ie;
6106 struct got_object_id base_commit_id;
6107 struct got_object_id *base_commit_idp = NULL;
6108 char *in_repo_path = NULL, *p;
6110 if (status == GOT_STATUS_UNVERSIONED ||
6111 status == GOT_STATUS_NO_CHANGE)
6112 return NULL;
6113 if (status == GOT_STATUS_NONEXISTENT)
6114 return got_error_set_errno(ENOENT, relpath);
6116 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6117 if (ie == NULL)
6118 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6120 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6121 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6122 relpath) == -1)
6123 return got_error_from_errno("asprintf");
6125 if (got_fileindex_entry_has_commit(ie)) {
6126 memcpy(base_commit_id.sha1, ie->commit_sha1,
6127 SHA1_DIGEST_LENGTH);
6128 base_commit_idp = &base_commit_id;
6131 if (status == GOT_STATUS_CONFLICT) {
6132 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6133 goto done;
6134 } else if (status != GOT_STATUS_ADD &&
6135 status != GOT_STATUS_MODIFY &&
6136 status != GOT_STATUS_DELETE) {
6137 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6138 goto done;
6141 a->have_changes = 1;
6143 p = in_repo_path;
6144 while (p[0] == '/')
6145 p++;
6146 err = check_out_of_date(p, status, staged_status,
6147 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6148 GOT_ERR_STAGE_OUT_OF_DATE);
6149 done:
6150 free(in_repo_path);
6151 return err;
6154 struct stage_path_arg {
6155 struct got_worktree *worktree;
6156 struct got_fileindex *fileindex;
6157 struct got_repository *repo;
6158 got_worktree_status_cb status_cb;
6159 void *status_arg;
6160 got_worktree_patch_cb patch_cb;
6161 void *patch_arg;
6162 int staged_something;
6165 static const struct got_error *
6166 stage_path(void *arg, unsigned char status,
6167 unsigned char staged_status, const char *relpath,
6168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6169 struct got_object_id *commit_id, int dirfd, const char *de_name)
6171 struct stage_path_arg *a = arg;
6172 const struct got_error *err = NULL;
6173 struct got_fileindex_entry *ie;
6174 char *ondisk_path = NULL, *path_content = NULL;
6175 uint32_t stage;
6176 struct got_object_id *new_staged_blob_id = NULL;
6178 if (status == GOT_STATUS_UNVERSIONED)
6179 return NULL;
6181 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6182 if (ie == NULL)
6183 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6185 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6186 relpath)== -1)
6187 return got_error_from_errno("asprintf");
6189 switch (status) {
6190 case GOT_STATUS_ADD:
6191 case GOT_STATUS_MODIFY:
6192 if (a->patch_cb) {
6193 if (status == GOT_STATUS_ADD) {
6194 int choice = GOT_PATCH_CHOICE_NONE;
6195 err = (*a->patch_cb)(&choice, a->patch_arg,
6196 status, ie->path, NULL, 1, 1);
6197 if (err)
6198 break;
6199 if (choice != GOT_PATCH_CHOICE_YES)
6200 break;
6201 } else {
6202 err = create_patched_content(&path_content, 0,
6203 staged_blob_id ? staged_blob_id : blob_id,
6204 ondisk_path, dirfd, de_name, ie->path,
6205 a->repo, a->patch_cb, a->patch_arg);
6206 if (err || path_content == NULL)
6207 break;
6210 err = got_object_blob_create(&new_staged_blob_id,
6211 path_content ? path_content : ondisk_path, a->repo);
6212 if (err)
6213 break;
6214 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6215 SHA1_DIGEST_LENGTH);
6216 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6217 stage = GOT_FILEIDX_STAGE_ADD;
6218 else
6219 stage = GOT_FILEIDX_STAGE_MODIFY;
6220 got_fileindex_entry_stage_set(ie, stage);
6221 a->staged_something = 1;
6222 if (a->status_cb == NULL)
6223 break;
6224 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6225 get_staged_status(ie), relpath, blob_id,
6226 new_staged_blob_id, NULL, dirfd, de_name);
6227 break;
6228 case GOT_STATUS_DELETE:
6229 if (staged_status == GOT_STATUS_DELETE)
6230 break;
6231 if (a->patch_cb) {
6232 int choice = GOT_PATCH_CHOICE_NONE;
6233 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6234 ie->path, NULL, 1, 1);
6235 if (err)
6236 break;
6237 if (choice == GOT_PATCH_CHOICE_NO)
6238 break;
6239 if (choice != GOT_PATCH_CHOICE_YES) {
6240 err = got_error(GOT_ERR_PATCH_CHOICE);
6241 break;
6244 stage = GOT_FILEIDX_STAGE_DELETE;
6245 got_fileindex_entry_stage_set(ie, stage);
6246 a->staged_something = 1;
6247 if (a->status_cb == NULL)
6248 break;
6249 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6250 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6251 de_name);
6252 break;
6253 case GOT_STATUS_NO_CHANGE:
6254 break;
6255 case GOT_STATUS_CONFLICT:
6256 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6257 break;
6258 case GOT_STATUS_NONEXISTENT:
6259 err = got_error_set_errno(ENOENT, relpath);
6260 break;
6261 default:
6262 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6263 break;
6266 if (path_content && unlink(path_content) == -1 && err == NULL)
6267 err = got_error_from_errno2("unlink", path_content);
6268 free(path_content);
6269 free(ondisk_path);
6270 free(new_staged_blob_id);
6271 return err;
6274 const struct got_error *
6275 got_worktree_stage(struct got_worktree *worktree,
6276 struct got_pathlist_head *paths,
6277 got_worktree_status_cb status_cb, void *status_arg,
6278 got_worktree_patch_cb patch_cb, void *patch_arg,
6279 struct got_repository *repo)
6281 const struct got_error *err = NULL, *sync_err, *unlockerr;
6282 struct got_pathlist_entry *pe;
6283 struct got_fileindex *fileindex = NULL;
6284 char *fileindex_path = NULL;
6285 struct got_reference *head_ref = NULL;
6286 struct got_object_id *head_commit_id = NULL;
6287 struct check_stage_ok_arg oka;
6288 struct stage_path_arg spa;
6290 err = lock_worktree(worktree, LOCK_EX);
6291 if (err)
6292 return err;
6294 err = got_ref_open(&head_ref, repo,
6295 got_worktree_get_head_ref_name(worktree), 0);
6296 if (err)
6297 goto done;
6298 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6299 if (err)
6300 goto done;
6301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6302 if (err)
6303 goto done;
6305 /* Check pre-conditions before staging anything. */
6306 oka.head_commit_id = head_commit_id;
6307 oka.worktree = worktree;
6308 oka.fileindex = fileindex;
6309 oka.repo = repo;
6310 oka.have_changes = 0;
6311 TAILQ_FOREACH(pe, paths, entry) {
6312 err = worktree_status(worktree, pe->path, fileindex, repo,
6313 check_stage_ok, &oka, NULL, NULL, 0, 0);
6314 if (err)
6315 goto done;
6317 if (!oka.have_changes) {
6318 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6319 goto done;
6322 spa.worktree = worktree;
6323 spa.fileindex = fileindex;
6324 spa.repo = repo;
6325 spa.patch_cb = patch_cb;
6326 spa.patch_arg = patch_arg;
6327 spa.status_cb = status_cb;
6328 spa.status_arg = status_arg;
6329 spa.staged_something = 0;
6330 TAILQ_FOREACH(pe, paths, entry) {
6331 err = worktree_status(worktree, pe->path, fileindex, repo,
6332 stage_path, &spa, NULL, NULL, 0, 0);
6333 if (err)
6334 goto done;
6336 if (!spa.staged_something) {
6337 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6338 goto done;
6341 sync_err = sync_fileindex(fileindex, fileindex_path);
6342 if (sync_err && err == NULL)
6343 err = sync_err;
6344 done:
6345 if (head_ref)
6346 got_ref_close(head_ref);
6347 free(head_commit_id);
6348 free(fileindex_path);
6349 if (fileindex)
6350 got_fileindex_free(fileindex);
6351 unlockerr = lock_worktree(worktree, LOCK_SH);
6352 if (unlockerr && err == NULL)
6353 err = unlockerr;
6354 return err;
6357 struct unstage_path_arg {
6358 struct got_worktree *worktree;
6359 struct got_fileindex *fileindex;
6360 struct got_repository *repo;
6361 got_worktree_checkout_cb progress_cb;
6362 void *progress_arg;
6363 got_worktree_patch_cb patch_cb;
6364 void *patch_arg;
6367 static const struct got_error *
6368 create_unstaged_content(char **path_unstaged_content,
6369 char **path_new_staged_content, struct got_object_id *blob_id,
6370 struct got_object_id *staged_blob_id, const char *relpath,
6371 struct got_repository *repo,
6372 got_worktree_patch_cb patch_cb, void *patch_arg)
6374 const struct got_error *err;
6375 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6376 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6377 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6378 struct stat sb1, sb2;
6379 struct got_diff_changes *changes = NULL;
6380 struct got_diff_state *ds = NULL;
6381 struct got_diff_args *args = NULL;
6382 struct got_diff_change *change;
6383 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6384 int have_content = 0, have_rejected_content = 0;
6386 *path_unstaged_content = NULL;
6387 *path_new_staged_content = NULL;
6389 err = got_object_id_str(&label1, blob_id);
6390 if (err)
6391 return err;
6392 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6393 if (err)
6394 goto done;
6396 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6397 if (err)
6398 goto done;
6400 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6401 if (err)
6402 goto done;
6404 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6405 if (err)
6406 goto done;
6408 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6409 if (err)
6410 goto done;
6412 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6413 if (err)
6414 goto done;
6416 if (stat(path1, &sb1) == -1) {
6417 err = got_error_from_errno2("stat", path1);
6418 goto done;
6421 if (stat(path2, &sb2) == -1) {
6422 err = got_error_from_errno2("stat", path2);
6423 goto done;
6426 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6427 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6428 if (err)
6429 goto done;
6431 err = got_opentemp_named(path_unstaged_content, &outfile,
6432 "got-unstaged-content");
6433 if (err)
6434 goto done;
6435 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6436 "got-new-staged-content");
6437 if (err)
6438 goto done;
6440 if (fseek(f1, 0L, SEEK_SET) == -1) {
6441 err = got_ferror(f1, GOT_ERR_IO);
6442 goto done;
6444 if (fseek(f2, 0L, SEEK_SET) == -1) {
6445 err = got_ferror(f2, GOT_ERR_IO);
6446 goto done;
6448 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6449 int choice;
6450 err = apply_or_reject_change(&choice, change, ++n,
6451 changes->nchanges, ds, args, diff_flags, relpath,
6452 f1, f2, &line_cur1, &line_cur2,
6453 outfile, rejectfile, patch_cb, patch_arg);
6454 if (err)
6455 goto done;
6456 if (choice == GOT_PATCH_CHOICE_YES)
6457 have_content = 1;
6458 else
6459 have_rejected_content = 1;
6460 if (choice == GOT_PATCH_CHOICE_QUIT)
6461 break;
6463 if (have_content || have_rejected_content)
6464 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6465 outfile, rejectfile);
6466 done:
6467 free(label1);
6468 if (blob)
6469 got_object_blob_close(blob);
6470 if (staged_blob)
6471 got_object_blob_close(staged_blob);
6472 if (f1 && fclose(f1) == EOF && err == NULL)
6473 err = got_error_from_errno2("fclose", path1);
6474 if (f2 && fclose(f2) == EOF && err == NULL)
6475 err = got_error_from_errno2("fclose", path2);
6476 if (outfile && fclose(outfile) == EOF && err == NULL)
6477 err = got_error_from_errno2("fclose", *path_unstaged_content);
6478 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6479 err = got_error_from_errno2("fclose", *path_new_staged_content);
6480 if (path1 && unlink(path1) == -1 && err == NULL)
6481 err = got_error_from_errno2("unlink", path1);
6482 if (path2 && unlink(path2) == -1 && err == NULL)
6483 err = got_error_from_errno2("unlink", path2);
6484 if (err || !have_content) {
6485 if (*path_unstaged_content &&
6486 unlink(*path_unstaged_content) == -1 && err == NULL)
6487 err = got_error_from_errno2("unlink",
6488 *path_unstaged_content);
6489 free(*path_unstaged_content);
6490 *path_unstaged_content = NULL;
6492 if (err || !have_rejected_content) {
6493 if (*path_new_staged_content &&
6494 unlink(*path_new_staged_content) == -1 && err == NULL)
6495 err = got_error_from_errno2("unlink",
6496 *path_new_staged_content);
6497 free(*path_new_staged_content);
6498 *path_new_staged_content = NULL;
6500 free(args);
6501 if (ds) {
6502 got_diff_state_free(ds);
6503 free(ds);
6505 if (changes)
6506 got_diff_free_changes(changes);
6507 free(path1);
6508 free(path2);
6509 return err;
6512 static const struct got_error *
6513 unstage_path(void *arg, unsigned char status,
6514 unsigned char staged_status, const char *relpath,
6515 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6516 struct got_object_id *commit_id, int dirfd, const char *de_name)
6518 const struct got_error *err = NULL;
6519 struct unstage_path_arg *a = arg;
6520 struct got_fileindex_entry *ie;
6521 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6522 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6523 char *path_new_staged_content = NULL;
6524 char *id_str = NULL, *label_orig = NULL;
6525 int local_changes_subsumed;
6526 struct stat sb;
6528 if (staged_status != GOT_STATUS_ADD &&
6529 staged_status != GOT_STATUS_MODIFY &&
6530 staged_status != GOT_STATUS_DELETE)
6531 return NULL;
6533 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6534 if (ie == NULL)
6535 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6537 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6538 == -1)
6539 return got_error_from_errno("asprintf");
6541 err = got_object_id_str(&id_str,
6542 commit_id ? commit_id : a->worktree->base_commit_id);
6543 if (err)
6544 goto done;
6545 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6546 id_str) == -1) {
6547 err = got_error_from_errno("asprintf");
6548 goto done;
6551 switch (staged_status) {
6552 case GOT_STATUS_MODIFY:
6553 err = got_object_open_as_blob(&blob_base, a->repo,
6554 blob_id, 8192);
6555 if (err)
6556 break;
6557 /* fall through */
6558 case GOT_STATUS_ADD:
6559 if (a->patch_cb) {
6560 if (staged_status == GOT_STATUS_ADD) {
6561 int choice = GOT_PATCH_CHOICE_NONE;
6562 err = (*a->patch_cb)(&choice, a->patch_arg,
6563 staged_status, ie->path, NULL, 1, 1);
6564 if (err)
6565 break;
6566 if (choice != GOT_PATCH_CHOICE_YES)
6567 break;
6568 } else {
6569 err = create_unstaged_content(
6570 &path_unstaged_content,
6571 &path_new_staged_content, blob_id,
6572 staged_blob_id, ie->path, a->repo,
6573 a->patch_cb, a->patch_arg);
6574 if (err || path_unstaged_content == NULL)
6575 break;
6576 if (path_new_staged_content) {
6577 err = got_object_blob_create(
6578 &staged_blob_id,
6579 path_new_staged_content,
6580 a->repo);
6581 if (err)
6582 break;
6583 memcpy(ie->staged_blob_sha1,
6584 staged_blob_id->sha1,
6585 SHA1_DIGEST_LENGTH);
6587 err = merge_file(&local_changes_subsumed,
6588 a->worktree, blob_base, ondisk_path,
6589 relpath, got_fileindex_perms_to_st(ie),
6590 path_unstaged_content, label_orig,
6591 "unstaged", a->repo, a->progress_cb,
6592 a->progress_arg);
6593 if (err == NULL &&
6594 path_new_staged_content == NULL)
6595 got_fileindex_entry_stage_set(ie,
6596 GOT_FILEIDX_STAGE_NONE);
6597 break; /* Done with this file. */
6600 err = got_object_open_as_blob(&blob_staged, a->repo,
6601 staged_blob_id, 8192);
6602 if (err)
6603 break;
6604 err = merge_blob(&local_changes_subsumed, a->worktree,
6605 blob_base, ondisk_path, relpath,
6606 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6607 commit_id ? commit_id : a->worktree->base_commit_id,
6608 a->repo, a->progress_cb, a->progress_arg);
6609 if (err == NULL)
6610 got_fileindex_entry_stage_set(ie,
6611 GOT_FILEIDX_STAGE_NONE);
6612 break;
6613 case GOT_STATUS_DELETE:
6614 if (a->patch_cb) {
6615 int choice = GOT_PATCH_CHOICE_NONE;
6616 err = (*a->patch_cb)(&choice, a->patch_arg,
6617 staged_status, ie->path, NULL, 1, 1);
6618 if (err)
6619 break;
6620 if (choice == GOT_PATCH_CHOICE_NO)
6621 break;
6622 if (choice != GOT_PATCH_CHOICE_YES) {
6623 err = got_error(GOT_ERR_PATCH_CHOICE);
6624 break;
6627 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6628 err = get_file_status(&status, &sb, ie, ondisk_path,
6629 dirfd, de_name, a->repo);
6630 if (err)
6631 break;
6632 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6633 break;
6635 done:
6636 free(ondisk_path);
6637 if (path_unstaged_content &&
6638 unlink(path_unstaged_content) == -1 && err == NULL)
6639 err = got_error_from_errno2("unlink", path_unstaged_content);
6640 if (path_new_staged_content &&
6641 unlink(path_new_staged_content) == -1 && err == NULL)
6642 err = got_error_from_errno2("unlink", path_new_staged_content);
6643 free(path_unstaged_content);
6644 free(path_new_staged_content);
6645 if (blob_base)
6646 got_object_blob_close(blob_base);
6647 if (blob_staged)
6648 got_object_blob_close(blob_staged);
6649 free(id_str);
6650 free(label_orig);
6651 return err;
6654 const struct got_error *
6655 got_worktree_unstage(struct got_worktree *worktree,
6656 struct got_pathlist_head *paths,
6657 got_worktree_checkout_cb progress_cb, void *progress_arg,
6658 got_worktree_patch_cb patch_cb, void *patch_arg,
6659 struct got_repository *repo)
6661 const struct got_error *err = NULL, *sync_err, *unlockerr;
6662 struct got_pathlist_entry *pe;
6663 struct got_fileindex *fileindex = NULL;
6664 char *fileindex_path = NULL;
6665 struct unstage_path_arg upa;
6667 err = lock_worktree(worktree, LOCK_EX);
6668 if (err)
6669 return err;
6671 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6672 if (err)
6673 goto done;
6675 upa.worktree = worktree;
6676 upa.fileindex = fileindex;
6677 upa.repo = repo;
6678 upa.progress_cb = progress_cb;
6679 upa.progress_arg = progress_arg;
6680 upa.patch_cb = patch_cb;
6681 upa.patch_arg = patch_arg;
6682 TAILQ_FOREACH(pe, paths, entry) {
6683 err = worktree_status(worktree, pe->path, fileindex, repo,
6684 unstage_path, &upa, NULL, NULL, 0, 0);
6685 if (err)
6686 goto done;
6689 sync_err = sync_fileindex(fileindex, fileindex_path);
6690 if (sync_err && err == NULL)
6691 err = sync_err;
6692 done:
6693 free(fileindex_path);
6694 if (fileindex)
6695 got_fileindex_free(fileindex);
6696 unlockerr = lock_worktree(worktree, LOCK_SH);
6697 if (unlockerr && err == NULL)
6698 err = unlockerr;
6699 return err;