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;
1307 if (ie && status != GOT_STATUS_MISSING &&
1308 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1309 if (got_fileindex_entry_has_commit(ie) &&
1310 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1311 SHA1_DIGEST_LENGTH) == 0) {
1312 err = sync_timestamps(ondisk_path, status, ie, &sb);
1313 if (err)
1314 goto done;
1315 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1316 path);
1317 goto done;
1319 if (got_fileindex_entry_has_blob(ie) &&
1320 memcmp(ie->blob_sha1, te->id.sha1,
1321 SHA1_DIGEST_LENGTH) == 0) {
1322 err = sync_timestamps(ondisk_path, status, ie, &sb);
1323 goto done;
1327 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1328 if (err)
1329 goto done;
1331 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1332 int update_timestamps;
1333 struct got_blob_object *blob2 = NULL;
1334 char *label_orig = NULL;
1335 if (got_fileindex_entry_has_blob(ie)) {
1336 struct got_object_id id2;
1337 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1338 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1339 if (err)
1340 goto done;
1342 if (got_fileindex_entry_has_commit(ie)) {
1343 char id_str[SHA1_DIGEST_STRING_LENGTH];
1344 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1345 sizeof(id_str)) == NULL) {
1346 err = got_error_path(id_str,
1347 GOT_ERR_BAD_OBJ_ID_STR);
1348 goto done;
1350 if (asprintf(&label_orig, "%s: commit %s",
1351 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1352 err = got_error_from_errno("asprintf");
1353 goto done;
1356 err = merge_blob(&update_timestamps, worktree, blob2,
1357 ondisk_path, path, sb.st_mode, label_orig, blob,
1358 worktree->base_commit_id, repo,
1359 progress_cb, progress_arg);
1360 free(label_orig);
1361 if (blob2)
1362 got_object_blob_close(blob2);
1363 if (err)
1364 goto done;
1366 * Do not update timestamps of files with local changes.
1367 * Otherwise, a future status walk would treat them as
1368 * unmodified files again.
1370 err = got_fileindex_entry_update(ie, ondisk_path,
1371 blob->id.sha1, worktree->base_commit_id->sha1,
1372 update_timestamps);
1373 } else if (status == GOT_STATUS_MODE_CHANGE) {
1374 err = got_fileindex_entry_update(ie, ondisk_path,
1375 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1376 } else if (status == GOT_STATUS_DELETE) {
1377 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1378 if (err)
1379 goto done;
1380 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1381 ondisk_path, path, blob, 0);
1382 if (err)
1383 goto done;
1384 } else {
1385 err = install_blob(worktree, ondisk_path, path, te->mode,
1386 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1387 repo, progress_cb, progress_arg);
1388 if (err)
1389 goto done;
1390 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1391 ondisk_path, path, blob, 1);
1392 if (err)
1393 goto done;
1395 got_object_blob_close(blob);
1396 done:
1397 free(ondisk_path);
1398 return err;
1401 static const struct got_error *
1402 remove_ondisk_file(const char *root_path, const char *path)
1404 const struct got_error *err = NULL;
1405 char *ondisk_path = NULL;
1407 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1408 return got_error_from_errno("asprintf");
1410 if (unlink(ondisk_path) == -1) {
1411 if (errno != ENOENT)
1412 err = got_error_from_errno2("unlink", ondisk_path);
1413 } else {
1414 char *parent = dirname(ondisk_path);
1415 while (parent && strcmp(parent, root_path) != 0) {
1416 if (rmdir(parent) == -1) {
1417 if (errno != ENOTEMPTY)
1418 err = got_error_from_errno2("rmdir",
1419 parent);
1420 break;
1422 parent = dirname(parent);
1425 free(ondisk_path);
1426 return err;
1429 static const struct got_error *
1430 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1431 struct got_fileindex_entry *ie, struct got_repository *repo,
1432 got_worktree_checkout_cb progress_cb, void *progress_arg)
1434 const struct got_error *err = NULL;
1435 unsigned char status;
1436 struct stat sb;
1437 char *ondisk_path;
1439 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1440 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1442 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1443 == -1)
1444 return got_error_from_errno("asprintf");
1446 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1447 if (err)
1448 return err;
1450 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1451 status == GOT_STATUS_ADD) {
1452 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1453 if (err)
1454 return err;
1456 * Preserve the working file and change the deleted blob's
1457 * entry into a schedule-add entry.
1459 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1460 0);
1461 if (err)
1462 return err;
1463 } else {
1464 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1465 if (err)
1466 return err;
1467 if (status == GOT_STATUS_NO_CHANGE) {
1468 err = remove_ondisk_file(worktree->root_path, ie->path);
1469 if (err)
1470 return err;
1472 got_fileindex_entry_remove(fileindex, ie);
1475 return err;
1478 struct diff_cb_arg {
1479 struct got_fileindex *fileindex;
1480 struct got_worktree *worktree;
1481 struct got_repository *repo;
1482 got_worktree_checkout_cb progress_cb;
1483 void *progress_arg;
1484 got_cancel_cb cancel_cb;
1485 void *cancel_arg;
1488 static const struct got_error *
1489 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1490 struct got_tree_entry *te, const char *parent_path)
1492 struct diff_cb_arg *a = arg;
1494 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1495 return got_error(GOT_ERR_CANCELLED);
1497 return update_blob(a->worktree, a->fileindex, ie, te,
1498 ie->path, a->repo, a->progress_cb, a->progress_arg);
1501 static const struct got_error *
1502 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1504 struct diff_cb_arg *a = arg;
1506 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1507 return got_error(GOT_ERR_CANCELLED);
1509 return delete_blob(a->worktree, a->fileindex, ie,
1510 a->repo, a->progress_cb, a->progress_arg);
1513 static const struct got_error *
1514 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1516 struct diff_cb_arg *a = arg;
1517 const struct got_error *err;
1518 char *path;
1520 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1521 return got_error(GOT_ERR_CANCELLED);
1523 if (got_object_tree_entry_is_submodule(te))
1524 return NULL;
1526 if (asprintf(&path, "%s%s%s", parent_path,
1527 parent_path[0] ? "/" : "", te->name)
1528 == -1)
1529 return got_error_from_errno("asprintf");
1531 if (S_ISDIR(te->mode))
1532 err = add_dir_on_disk(a->worktree, path);
1533 else
1534 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1535 a->repo, a->progress_cb, a->progress_arg);
1537 free(path);
1538 return err;
1541 static const struct got_error *
1542 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1544 const struct got_error *err = NULL;
1545 char *uuidstr = NULL;
1546 uint32_t uuid_status;
1548 *refname = NULL;
1550 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1551 if (uuid_status != uuid_s_ok)
1552 return got_error_uuid(uuid_status, "uuid_to_string");
1554 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1555 == -1) {
1556 err = got_error_from_errno("asprintf");
1557 *refname = NULL;
1559 free(uuidstr);
1560 return err;
1563 const struct got_error *
1564 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1569 static const struct got_error *
1570 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1572 return get_ref_name(refname, worktree,
1573 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1576 static const struct got_error *
1577 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1579 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1582 static const struct got_error *
1583 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1585 return get_ref_name(refname, worktree,
1586 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1589 static const struct got_error *
1590 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1592 return get_ref_name(refname, worktree,
1593 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1596 static const struct got_error *
1597 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1599 return get_ref_name(refname, worktree,
1600 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1603 static const struct got_error *
1604 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1606 return get_ref_name(refname, worktree,
1607 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1610 static const struct got_error *
1611 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1613 return get_ref_name(refname, worktree,
1614 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1617 static const struct got_error *
1618 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1620 return get_ref_name(refname, worktree,
1621 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1624 const struct got_error *
1625 got_worktree_get_histedit_script_path(char **path,
1626 struct got_worktree *worktree)
1628 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1629 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1630 *path = NULL;
1631 return got_error_from_errno("asprintf");
1633 return NULL;
1637 * Prevent Git's garbage collector from deleting our base commit by
1638 * setting a reference to our base commit's ID.
1640 static const struct got_error *
1641 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_reference *ref = NULL;
1645 char *refname;
1647 err = got_worktree_get_base_ref_name(&refname, worktree);
1648 if (err)
1649 return err;
1651 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1652 if (err)
1653 goto done;
1655 err = got_ref_write(ref, repo);
1656 done:
1657 free(refname);
1658 if (ref)
1659 got_ref_close(ref);
1660 return err;
1663 static const struct got_error *
1664 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1666 const struct got_error *err = NULL;
1668 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1669 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 *fileindex_path = NULL;
1673 return err;
1677 static const struct got_error *
1678 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1679 struct got_worktree *worktree)
1681 const struct got_error *err = NULL;
1682 FILE *index = NULL;
1684 *fileindex_path = NULL;
1685 *fileindex = got_fileindex_alloc();
1686 if (*fileindex == NULL)
1687 return got_error_from_errno("got_fileindex_alloc");
1689 err = get_fileindex_path(fileindex_path, worktree);
1690 if (err)
1691 goto done;
1693 index = fopen(*fileindex_path, "rb");
1694 if (index == NULL) {
1695 if (errno != ENOENT)
1696 err = got_error_from_errno2("fopen", *fileindex_path);
1697 } else {
1698 err = got_fileindex_read(*fileindex, index);
1699 if (fclose(index) != 0 && err == NULL)
1700 err = got_error_from_errno("fclose");
1702 done:
1703 if (err) {
1704 free(*fileindex_path);
1705 *fileindex_path = NULL;
1706 got_fileindex_free(*fileindex);
1707 *fileindex = NULL;
1709 return err;
1712 struct bump_base_commit_id_arg {
1713 struct got_object_id *base_commit_id;
1714 const char *path;
1715 size_t path_len;
1716 const char *entry_name;
1717 got_worktree_checkout_cb progress_cb;
1718 void *progress_arg;
1721 /* Bump base commit ID of all files within an updated part of the work tree. */
1722 static const struct got_error *
1723 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1725 const struct got_error *err;
1726 struct bump_base_commit_id_arg *a = arg;
1728 if (a->entry_name) {
1729 if (strcmp(ie->path, a->path) != 0)
1730 return NULL;
1731 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1732 return NULL;
1734 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1735 SHA1_DIGEST_LENGTH) == 0)
1736 return NULL;
1738 if (a->progress_cb) {
1739 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1740 ie->path);
1741 if (err)
1742 return err;
1744 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1745 return NULL;
1748 static const struct got_error *
1749 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1751 const struct got_error *err = NULL;
1752 char *new_fileindex_path = NULL;
1753 FILE *new_index = NULL;
1754 struct timespec timeout;
1756 err = got_opentemp_named(&new_fileindex_path, &new_index,
1757 fileindex_path);
1758 if (err)
1759 goto done;
1761 err = got_fileindex_write(fileindex, new_index);
1762 if (err)
1763 goto done;
1765 if (rename(new_fileindex_path, fileindex_path) != 0) {
1766 err = got_error_from_errno3("rename", new_fileindex_path,
1767 fileindex_path);
1768 unlink(new_fileindex_path);
1772 * Sleep for a short amount of time to ensure that files modified after
1773 * this program exits have a different time stamp from the one which
1774 * was recorded in the file index.
1776 timeout.tv_sec = 0;
1777 timeout.tv_nsec = 1;
1778 nanosleep(&timeout, NULL);
1779 done:
1780 if (new_index)
1781 fclose(new_index);
1782 free(new_fileindex_path);
1783 return err;
1786 static const struct got_error *
1787 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1788 struct got_object_id **tree_id, const char *wt_relpath,
1789 struct got_worktree *worktree, struct got_repository *repo)
1791 const struct got_error *err = NULL;
1792 struct got_object_id *id = NULL;
1793 char *in_repo_path = NULL;
1794 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1796 *entry_type = GOT_OBJ_TYPE_ANY;
1797 *tree_relpath = NULL;
1798 *tree_id = NULL;
1800 if (wt_relpath[0] == '\0') {
1801 /* Check out all files within the work tree. */
1802 *entry_type = GOT_OBJ_TYPE_TREE;
1803 *tree_relpath = strdup("");
1804 if (*tree_relpath == NULL) {
1805 err = got_error_from_errno("strdup");
1806 goto done;
1808 err = got_object_id_by_path(tree_id, repo,
1809 worktree->base_commit_id, worktree->path_prefix);
1810 if (err)
1811 goto done;
1812 return NULL;
1815 /* Check out a subset of files in the work tree. */
1817 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1818 is_root_wt ? "" : "/", wt_relpath) == -1) {
1819 err = got_error_from_errno("asprintf");
1820 goto done;
1823 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1824 in_repo_path);
1825 if (err)
1826 goto done;
1828 free(in_repo_path);
1829 in_repo_path = NULL;
1831 err = got_object_get_type(entry_type, repo, id);
1832 if (err)
1833 goto done;
1835 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1836 /* Check out a single file. */
1837 if (strchr(wt_relpath, '/') == NULL) {
1838 /* Check out a single file in work tree's root dir. */
1839 in_repo_path = strdup(worktree->path_prefix);
1840 if (in_repo_path == NULL) {
1841 err = got_error_from_errno("strdup");
1842 goto done;
1844 *tree_relpath = strdup("");
1845 if (*tree_relpath == NULL) {
1846 err = got_error_from_errno("strdup");
1847 goto done;
1849 } else {
1850 /* Check out a single file in a subdirectory. */
1851 err = got_path_dirname(tree_relpath, wt_relpath);
1852 if (err)
1853 return err;
1854 if (asprintf(&in_repo_path, "%s%s%s",
1855 worktree->path_prefix, is_root_wt ? "" : "/",
1856 *tree_relpath) == -1) {
1857 err = got_error_from_errno("asprintf");
1858 goto done;
1861 err = got_object_id_by_path(tree_id, repo,
1862 worktree->base_commit_id, in_repo_path);
1863 } else {
1864 /* Check out all files within a subdirectory. */
1865 *tree_id = got_object_id_dup(id);
1866 if (*tree_id == NULL) {
1867 err = got_error_from_errno("got_object_id_dup");
1868 goto done;
1870 *tree_relpath = strdup(wt_relpath);
1871 if (*tree_relpath == NULL) {
1872 err = got_error_from_errno("strdup");
1873 goto done;
1876 done:
1877 free(id);
1878 free(in_repo_path);
1879 if (err) {
1880 *entry_type = GOT_OBJ_TYPE_ANY;
1881 free(*tree_relpath);
1882 *tree_relpath = NULL;
1883 free(*tree_id);
1884 *tree_id = NULL;
1886 return err;
1889 static const struct got_error *
1890 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1891 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1895 const struct got_error *err = NULL;
1896 struct got_commit_object *commit = NULL;
1897 struct got_tree_object *tree = NULL;
1898 struct got_fileindex_diff_tree_cb diff_cb;
1899 struct diff_cb_arg arg;
1901 err = ref_base_commit(worktree, repo);
1902 if (err) {
1903 if (!(err->code == GOT_ERR_ERRNO &&
1904 (errno == EACCES || errno == EROFS)))
1905 goto done;
1906 err = (*progress_cb)(progress_arg,
1907 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1908 if (err)
1909 return err;
1912 err = got_object_open_as_commit(&commit, repo,
1913 worktree->base_commit_id);
1914 if (err)
1915 goto done;
1917 err = got_object_open_as_tree(&tree, repo, tree_id);
1918 if (err)
1919 goto done;
1921 if (entry_name &&
1922 got_object_tree_find_entry(tree, entry_name) == NULL) {
1923 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1924 goto done;
1927 diff_cb.diff_old_new = diff_old_new;
1928 diff_cb.diff_old = diff_old;
1929 diff_cb.diff_new = diff_new;
1930 arg.fileindex = fileindex;
1931 arg.worktree = worktree;
1932 arg.repo = repo;
1933 arg.progress_cb = progress_cb;
1934 arg.progress_arg = progress_arg;
1935 arg.cancel_cb = cancel_cb;
1936 arg.cancel_arg = cancel_arg;
1937 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1938 entry_name, repo, &diff_cb, &arg);
1939 done:
1940 if (tree)
1941 got_object_tree_close(tree);
1942 if (commit)
1943 got_object_commit_close(commit);
1944 return err;
1947 const struct got_error *
1948 got_worktree_checkout_files(struct got_worktree *worktree,
1949 struct got_pathlist_head *paths, struct got_repository *repo,
1950 got_worktree_checkout_cb progress_cb, void *progress_arg,
1951 got_cancel_cb cancel_cb, void *cancel_arg)
1953 const struct got_error *err = NULL, *sync_err, *unlockerr;
1954 struct got_commit_object *commit = NULL;
1955 struct got_tree_object *tree = NULL;
1956 struct got_fileindex *fileindex = NULL;
1957 char *fileindex_path = NULL;
1958 struct got_pathlist_entry *pe;
1959 struct tree_path_data {
1960 SIMPLEQ_ENTRY(tree_path_data) entry;
1961 struct got_object_id *tree_id;
1962 int entry_type;
1963 char *relpath;
1964 char *entry_name;
1965 } *tpd = NULL;
1966 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1968 SIMPLEQ_INIT(&tree_paths);
1970 err = lock_worktree(worktree, LOCK_EX);
1971 if (err)
1972 return err;
1974 /* Map all specified paths to in-repository trees. */
1975 TAILQ_FOREACH(pe, paths, entry) {
1976 tpd = malloc(sizeof(*tpd));
1977 if (tpd == NULL) {
1978 err = got_error_from_errno("malloc");
1979 goto done;
1982 err = find_tree_entry_for_checkout(&tpd->entry_type,
1983 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1984 if (err) {
1985 free(tpd);
1986 goto done;
1989 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1990 err = got_path_basename(&tpd->entry_name, pe->path);
1991 if (err) {
1992 free(tpd->relpath);
1993 free(tpd->tree_id);
1994 free(tpd);
1995 goto done;
1997 } else
1998 tpd->entry_name = NULL;
2000 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2004 * Read the file index.
2005 * Checking out files is supposed to be an idempotent operation.
2006 * If the on-disk file index is incomplete we will try to complete it.
2008 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2009 if (err)
2010 goto done;
2012 tpd = SIMPLEQ_FIRST(&tree_paths);
2013 TAILQ_FOREACH(pe, paths, entry) {
2014 struct bump_base_commit_id_arg bbc_arg;
2016 err = checkout_files(worktree, fileindex, tpd->relpath,
2017 tpd->tree_id, tpd->entry_name, repo,
2018 progress_cb, progress_arg, cancel_cb, cancel_arg);
2019 if (err)
2020 break;
2022 bbc_arg.base_commit_id = worktree->base_commit_id;
2023 bbc_arg.entry_name = tpd->entry_name;
2024 bbc_arg.path = pe->path;
2025 bbc_arg.path_len = pe->path_len;
2026 bbc_arg.progress_cb = progress_cb;
2027 bbc_arg.progress_arg = progress_arg;
2028 err = got_fileindex_for_each_entry_safe(fileindex,
2029 bump_base_commit_id, &bbc_arg);
2030 if (err)
2031 break;
2033 tpd = SIMPLEQ_NEXT(tpd, entry);
2035 sync_err = sync_fileindex(fileindex, fileindex_path);
2036 if (sync_err && err == NULL)
2037 err = sync_err;
2038 done:
2039 free(fileindex_path);
2040 if (tree)
2041 got_object_tree_close(tree);
2042 if (commit)
2043 got_object_commit_close(commit);
2044 if (fileindex)
2045 got_fileindex_free(fileindex);
2046 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2047 tpd = SIMPLEQ_FIRST(&tree_paths);
2048 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2049 free(tpd->relpath);
2050 free(tpd->tree_id);
2051 free(tpd);
2053 unlockerr = lock_worktree(worktree, LOCK_SH);
2054 if (unlockerr && err == NULL)
2055 err = unlockerr;
2056 return err;
2059 struct merge_file_cb_arg {
2060 struct got_worktree *worktree;
2061 struct got_fileindex *fileindex;
2062 got_worktree_checkout_cb progress_cb;
2063 void *progress_arg;
2064 got_cancel_cb cancel_cb;
2065 void *cancel_arg;
2066 const char *label_orig;
2067 struct got_object_id *commit_id2;
2070 static const struct got_error *
2071 merge_file_cb(void *arg, struct got_blob_object *blob1,
2072 struct got_blob_object *blob2, struct got_object_id *id1,
2073 struct got_object_id *id2, const char *path1, const char *path2,
2074 mode_t mode1, mode_t mode2, struct got_repository *repo)
2076 static const struct got_error *err = NULL;
2077 struct merge_file_cb_arg *a = arg;
2078 struct got_fileindex_entry *ie;
2079 char *ondisk_path = NULL;
2080 struct stat sb;
2081 unsigned char status;
2082 int local_changes_subsumed;
2084 if (blob1 && blob2) {
2085 ie = got_fileindex_entry_get(a->fileindex, path2,
2086 strlen(path2));
2087 if (ie == NULL)
2088 return (*a->progress_cb)(a->progress_arg,
2089 GOT_STATUS_MISSING, path2);
2091 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2092 path2) == -1)
2093 return got_error_from_errno("asprintf");
2095 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2096 repo);
2097 if (err)
2098 goto done;
2100 if (status == GOT_STATUS_DELETE) {
2101 err = (*a->progress_cb)(a->progress_arg,
2102 GOT_STATUS_MERGE, path2);
2103 goto done;
2105 if (status != GOT_STATUS_NO_CHANGE &&
2106 status != GOT_STATUS_MODIFY &&
2107 status != GOT_STATUS_CONFLICT &&
2108 status != GOT_STATUS_ADD) {
2109 err = (*a->progress_cb)(a->progress_arg, status, path2);
2110 goto done;
2113 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2114 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2115 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2116 } else if (blob1) {
2117 ie = got_fileindex_entry_get(a->fileindex, path1,
2118 strlen(path1));
2119 if (ie == NULL)
2120 return (*a->progress_cb)(a->progress_arg,
2121 GOT_STATUS_MISSING, path1);
2123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2124 path1) == -1)
2125 return got_error_from_errno("asprintf");
2127 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2128 repo);
2129 if (err)
2130 goto done;
2132 switch (status) {
2133 case GOT_STATUS_NO_CHANGE:
2134 err = (*a->progress_cb)(a->progress_arg,
2135 GOT_STATUS_DELETE, path1);
2136 if (err)
2137 goto done;
2138 err = remove_ondisk_file(a->worktree->root_path, path1);
2139 if (err)
2140 goto done;
2141 if (ie)
2142 got_fileindex_entry_mark_deleted_from_disk(ie);
2143 break;
2144 case GOT_STATUS_DELETE:
2145 case GOT_STATUS_MISSING:
2146 err = (*a->progress_cb)(a->progress_arg,
2147 GOT_STATUS_DELETE, path1);
2148 if (err)
2149 goto done;
2150 if (ie)
2151 got_fileindex_entry_mark_deleted_from_disk(ie);
2152 break;
2153 case GOT_STATUS_ADD:
2154 case GOT_STATUS_MODIFY:
2155 case GOT_STATUS_CONFLICT:
2156 err = (*a->progress_cb)(a->progress_arg,
2157 GOT_STATUS_CANNOT_DELETE, path1);
2158 if (err)
2159 goto done;
2160 break;
2161 case GOT_STATUS_OBSTRUCTED:
2162 err = (*a->progress_cb)(a->progress_arg, status, path1);
2163 if (err)
2164 goto done;
2165 break;
2166 default:
2167 break;
2169 } else if (blob2) {
2170 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2171 path2) == -1)
2172 return got_error_from_errno("asprintf");
2173 ie = got_fileindex_entry_get(a->fileindex, path2,
2174 strlen(path2));
2175 if (ie) {
2176 err = get_file_status(&status, &sb, ie, ondisk_path,
2177 -1, NULL, repo);
2178 if (err)
2179 goto done;
2180 if (status != GOT_STATUS_NO_CHANGE &&
2181 status != GOT_STATUS_MODIFY &&
2182 status != GOT_STATUS_CONFLICT &&
2183 status != GOT_STATUS_ADD) {
2184 err = (*a->progress_cb)(a->progress_arg,
2185 status, path2);
2186 goto done;
2188 err = merge_blob(&local_changes_subsumed, a->worktree,
2189 NULL, ondisk_path, path2, sb.st_mode,
2190 a->label_orig, blob2, a->commit_id2, repo,
2191 a->progress_cb,
2192 a->progress_arg);
2193 if (status == GOT_STATUS_DELETE) {
2194 err = update_blob_fileindex_entry(a->worktree,
2195 a->fileindex, ie, ondisk_path, ie->path,
2196 blob2, 0);
2197 if (err)
2198 goto done;
2200 } else {
2201 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2202 err = install_blob(a->worktree, ondisk_path, path2,
2203 /* XXX get this from parent tree! */
2204 GOT_DEFAULT_FILE_MODE,
2205 sb.st_mode, blob2, 0, 0, repo,
2206 a->progress_cb, a->progress_arg);
2207 if (err)
2208 goto done;
2209 err = got_fileindex_entry_alloc(&ie, path2);
2210 if (err)
2211 goto done;
2212 err = got_fileindex_entry_update(ie, ondisk_path,
2213 NULL, NULL, 1);
2214 if (err) {
2215 got_fileindex_entry_free(ie);
2216 goto done;
2218 err = got_fileindex_entry_add(a->fileindex, ie);
2219 if (err) {
2220 got_fileindex_entry_free(ie);
2221 goto done;
2225 done:
2226 free(ondisk_path);
2227 return err;
2230 struct check_merge_ok_arg {
2231 struct got_worktree *worktree;
2232 struct got_repository *repo;
2235 static const struct got_error *
2236 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2238 const struct got_error *err = NULL;
2239 struct check_merge_ok_arg *a = arg;
2240 unsigned char status;
2241 struct stat sb;
2242 char *ondisk_path;
2244 /* Reject merges into a work tree with mixed base commits. */
2245 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2246 SHA1_DIGEST_LENGTH))
2247 return got_error(GOT_ERR_MIXED_COMMITS);
2249 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2250 == -1)
2251 return got_error_from_errno("asprintf");
2253 /* Reject merges into a work tree with conflicted files. */
2254 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2255 if (err)
2256 return err;
2257 if (status == GOT_STATUS_CONFLICT)
2258 return got_error(GOT_ERR_CONFLICTS);
2260 return NULL;
2263 static const struct got_error *
2264 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2265 const char *fileindex_path, struct got_object_id *commit_id1,
2266 struct got_object_id *commit_id2, struct got_repository *repo,
2267 got_worktree_checkout_cb progress_cb, void *progress_arg,
2268 got_cancel_cb cancel_cb, void *cancel_arg)
2270 const struct got_error *err = NULL, *sync_err;
2271 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2272 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2273 struct merge_file_cb_arg arg;
2274 char *label_orig = NULL;
2276 if (commit_id1) {
2277 char *id_str;
2279 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2280 worktree->path_prefix);
2281 if (err)
2282 goto done;
2284 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2285 if (err)
2286 goto done;
2288 err = got_object_id_str(&id_str, commit_id1);
2289 if (err)
2290 goto done;
2292 if (asprintf(&label_orig, "%s: commit %s",
2293 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2294 err = got_error_from_errno("asprintf");
2295 free(id_str);
2296 goto done;
2298 free(id_str);
2301 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2302 worktree->path_prefix);
2303 if (err)
2304 goto done;
2306 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2307 if (err)
2308 goto done;
2310 arg.worktree = worktree;
2311 arg.fileindex = fileindex;
2312 arg.progress_cb = progress_cb;
2313 arg.progress_arg = progress_arg;
2314 arg.cancel_cb = cancel_cb;
2315 arg.cancel_arg = cancel_arg;
2316 arg.label_orig = label_orig;
2317 arg.commit_id2 = commit_id2;
2318 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2319 sync_err = sync_fileindex(fileindex, fileindex_path);
2320 if (sync_err && err == NULL)
2321 err = sync_err;
2322 done:
2323 if (tree1)
2324 got_object_tree_close(tree1);
2325 if (tree2)
2326 got_object_tree_close(tree2);
2327 free(label_orig);
2328 return err;
2331 const struct got_error *
2332 got_worktree_merge_files(struct got_worktree *worktree,
2333 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2334 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2335 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2337 const struct got_error *err, *unlockerr;
2338 char *fileindex_path = NULL;
2339 struct got_fileindex *fileindex = NULL;
2340 struct check_merge_ok_arg mok_arg;
2342 err = lock_worktree(worktree, LOCK_EX);
2343 if (err)
2344 return err;
2346 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2347 if (err)
2348 goto done;
2350 mok_arg.worktree = worktree;
2351 mok_arg.repo = repo;
2352 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2353 &mok_arg);
2354 if (err)
2355 goto done;
2357 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2358 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2359 done:
2360 if (fileindex)
2361 got_fileindex_free(fileindex);
2362 free(fileindex_path);
2363 unlockerr = lock_worktree(worktree, LOCK_SH);
2364 if (unlockerr && err == NULL)
2365 err = unlockerr;
2366 return err;
2369 struct diff_dir_cb_arg {
2370 struct got_fileindex *fileindex;
2371 struct got_worktree *worktree;
2372 const char *status_path;
2373 size_t status_path_len;
2374 struct got_repository *repo;
2375 got_worktree_status_cb status_cb;
2376 void *status_arg;
2377 got_cancel_cb cancel_cb;
2378 void *cancel_arg;
2379 /* A pathlist containing per-directory pathlists of ignore patterns. */
2380 struct got_pathlist_head ignores;
2381 int report_unchanged;
2384 static const struct got_error *
2385 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2386 int dirfd, const char *de_name,
2387 got_worktree_status_cb status_cb, void *status_arg,
2388 struct got_repository *repo, int report_unchanged)
2390 const struct got_error *err = NULL;
2391 unsigned char status = GOT_STATUS_NO_CHANGE;
2392 unsigned char staged_status = get_staged_status(ie);
2393 struct stat sb;
2394 struct got_object_id blob_id, commit_id, staged_blob_id;
2395 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2396 struct got_object_id *staged_blob_idp = NULL;
2398 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2399 if (err)
2400 return err;
2402 if (status == GOT_STATUS_NO_CHANGE &&
2403 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2404 return NULL;
2406 if (got_fileindex_entry_has_blob(ie)) {
2407 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2408 blob_idp = &blob_id;
2410 if (got_fileindex_entry_has_commit(ie)) {
2411 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2412 commit_idp = &commit_id;
2414 if (staged_status == GOT_STATUS_ADD ||
2415 staged_status == GOT_STATUS_MODIFY) {
2416 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2417 SHA1_DIGEST_LENGTH);
2418 staged_blob_idp = &staged_blob_id;
2421 return (*status_cb)(status_arg, status, staged_status,
2422 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2425 static const struct got_error *
2426 status_old_new(void *arg, struct got_fileindex_entry *ie,
2427 struct dirent *de, const char *parent_path, int dirfd)
2429 const struct got_error *err = NULL;
2430 struct diff_dir_cb_arg *a = arg;
2431 char *abspath;
2433 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2434 return got_error(GOT_ERR_CANCELLED);
2436 if (got_path_cmp(parent_path, a->status_path,
2437 strlen(parent_path), a->status_path_len) != 0 &&
2438 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2439 return NULL;
2441 if (parent_path[0]) {
2442 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2443 parent_path, de->d_name) == -1)
2444 return got_error_from_errno("asprintf");
2445 } else {
2446 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2447 de->d_name) == -1)
2448 return got_error_from_errno("asprintf");
2451 err = report_file_status(ie, abspath, dirfd, de->d_name,
2452 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2453 free(abspath);
2454 return err;
2457 static const struct got_error *
2458 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2460 struct diff_dir_cb_arg *a = arg;
2461 struct got_object_id blob_id, commit_id;
2462 unsigned char status;
2464 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2465 return got_error(GOT_ERR_CANCELLED);
2467 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2468 return NULL;
2470 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2471 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2472 if (got_fileindex_entry_has_file_on_disk(ie))
2473 status = GOT_STATUS_MISSING;
2474 else
2475 status = GOT_STATUS_DELETE;
2476 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2477 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2480 void
2481 free_ignorelist(struct got_pathlist_head *ignorelist)
2483 struct got_pathlist_entry *pe;
2485 TAILQ_FOREACH(pe, ignorelist, entry)
2486 free((char *)pe->path);
2487 got_pathlist_free(ignorelist);
2490 void
2491 free_ignores(struct got_pathlist_head *ignores)
2493 struct got_pathlist_entry *pe;
2495 TAILQ_FOREACH(pe, ignores, entry) {
2496 struct got_pathlist_head *ignorelist = pe->data;
2497 free_ignorelist(ignorelist);
2498 free((char *)pe->path);
2500 got_pathlist_free(ignores);
2503 static const struct got_error *
2504 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2506 const struct got_error *err = NULL;
2507 struct got_pathlist_entry *pe = NULL;
2508 struct got_pathlist_head *ignorelist;
2509 char *line = NULL, *pattern, *dirpath = NULL;
2510 size_t linesize = 0;
2511 ssize_t linelen;
2513 ignorelist = calloc(1, sizeof(*ignorelist));
2514 if (ignorelist == NULL)
2515 return got_error_from_errno("calloc");
2516 TAILQ_INIT(ignorelist);
2518 while ((linelen = getline(&line, &linesize, f)) != -1) {
2519 if (linelen > 0 && line[linelen - 1] == '\n')
2520 line[linelen - 1] = '\0';
2522 /* Git's ignores may contain comments. */
2523 if (line[0] == '#')
2524 continue;
2526 /* Git's negated patterns are not (yet?) supported. */
2527 if (line[0] == '!')
2528 continue;
2530 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2531 line) == -1) {
2532 err = got_error_from_errno("asprintf");
2533 goto done;
2535 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2536 if (err)
2537 goto done;
2539 if (ferror(f)) {
2540 err = got_error_from_errno("getline");
2541 goto done;
2544 dirpath = strdup(path);
2545 if (dirpath == NULL) {
2546 err = got_error_from_errno("strdup");
2547 goto done;
2549 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2550 done:
2551 free(line);
2552 if (err || pe == NULL) {
2553 free(dirpath);
2554 free_ignorelist(ignorelist);
2556 return err;
2559 int
2560 match_ignores(struct got_pathlist_head *ignores, const char *path)
2562 struct got_pathlist_entry *pe;
2564 /* Handle patterns which match in all directories. */
2565 TAILQ_FOREACH(pe, ignores, entry) {
2566 struct got_pathlist_head *ignorelist = pe->data;
2567 struct got_pathlist_entry *pi;
2569 TAILQ_FOREACH(pi, ignorelist, entry) {
2570 const char *p, *pattern = pi->path;
2572 if (strncmp(pattern, "**/", 3) != 0)
2573 continue;
2574 pattern += 3;
2575 p = path;
2576 while (*p) {
2577 if (fnmatch(pattern, p,
2578 FNM_PATHNAME | FNM_LEADING_DIR)) {
2579 /* Retry in next directory. */
2580 while (*p && *p != '/')
2581 p++;
2582 while (*p == '/')
2583 p++;
2584 continue;
2586 return 1;
2592 * The ignores pathlist contains ignore lists from children before
2593 * parents, so we can find the most specific ignorelist by walking
2594 * ignores backwards.
2596 pe = TAILQ_LAST(ignores, got_pathlist_head);
2597 while (pe) {
2598 if (got_path_is_child(path, pe->path, pe->path_len)) {
2599 struct got_pathlist_head *ignorelist = pe->data;
2600 struct got_pathlist_entry *pi;
2601 TAILQ_FOREACH(pi, ignorelist, entry) {
2602 const char *pattern = pi->path;
2603 int flags = FNM_LEADING_DIR;
2604 if (strstr(pattern, "/**/") == NULL)
2605 flags |= FNM_PATHNAME;
2606 if (fnmatch(pattern, path, flags))
2607 continue;
2608 return 1;
2611 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2614 return 0;
2617 static const struct got_error *
2618 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2619 const char *path, int dirfd, const char *ignores_filename)
2621 const struct got_error *err = NULL;
2622 char *ignorespath;
2623 int fd = -1;
2624 FILE *ignoresfile = NULL;
2626 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2627 path[0] ? "/" : "", ignores_filename) == -1)
2628 return got_error_from_errno("asprintf");
2630 if (dirfd != -1) {
2631 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2632 if (fd == -1) {
2633 if (errno != ENOENT && errno != EACCES)
2634 err = got_error_from_errno2("openat",
2635 ignorespath);
2636 } else {
2637 ignoresfile = fdopen(fd, "r");
2638 if (ignoresfile == NULL)
2639 err = got_error_from_errno2("fdopen",
2640 ignorespath);
2641 else {
2642 fd = -1;
2643 err = read_ignores(ignores, path, ignoresfile);
2646 } else {
2647 ignoresfile = fopen(ignorespath, "r");
2648 if (ignoresfile == NULL) {
2649 if (errno != ENOENT && errno != EACCES)
2650 err = got_error_from_errno2("fopen",
2651 ignorespath);
2652 } else
2653 err = read_ignores(ignores, path, ignoresfile);
2656 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2657 err = got_error_from_errno2("fclose", path);
2658 if (fd != -1 && close(fd) == -1 && err == NULL)
2659 err = got_error_from_errno2("close", path);
2660 free(ignorespath);
2661 return err;
2664 static const struct got_error *
2665 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2667 const struct got_error *err = NULL;
2668 struct diff_dir_cb_arg *a = arg;
2669 char *path = NULL;
2671 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2672 return got_error(GOT_ERR_CANCELLED);
2674 /* XXX ignore symlinks for now */
2675 if (de->d_type == DT_LNK)
2676 return NULL;
2678 if (parent_path[0]) {
2679 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2680 return got_error_from_errno("asprintf");
2681 } else {
2682 path = de->d_name;
2685 if (de->d_type == DT_DIR) {
2686 int subdirfd = openat(dirfd, de->d_name,
2687 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2688 if (subdirfd == -1) {
2689 if (errno != ENOENT && errno != EACCES)
2690 err = got_error_from_errno2("openat", path);
2691 } else {
2692 err = add_ignores(&a->ignores, a->worktree->root_path,
2693 path, subdirfd, ".cvsignore");
2694 if (err == NULL)
2695 err = add_ignores(&a->ignores,
2696 a->worktree->root_path, path,
2697 subdirfd, ".gitignore");
2698 if (close(subdirfd) == -1 && err == NULL)
2699 err = got_error_from_errno2("close", path);
2701 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2702 && !match_ignores(&a->ignores, path))
2703 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2704 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2705 if (parent_path[0])
2706 free(path);
2707 return err;
2710 static const struct got_error *
2711 report_single_file_status(const char *path, const char *ondisk_path,
2712 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2713 void *status_arg, struct got_repository *repo, int report_unchanged)
2715 struct got_fileindex_entry *ie;
2716 struct stat sb;
2718 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2719 if (ie)
2720 return report_file_status(ie, ondisk_path, -1, NULL,
2721 status_cb, status_arg, repo, report_unchanged);
2723 if (lstat(ondisk_path, &sb) == -1) {
2724 if (errno != ENOENT)
2725 return got_error_from_errno2("lstat", ondisk_path);
2726 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2727 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2728 return NULL;
2731 if (S_ISREG(sb.st_mode))
2732 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2733 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2735 return NULL;
2738 static const struct got_error *
2739 worktree_status(struct got_worktree *worktree, const char *path,
2740 struct got_fileindex *fileindex, struct got_repository *repo,
2741 got_worktree_status_cb status_cb, void *status_arg,
2742 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2743 int report_unchanged)
2745 const struct got_error *err = NULL;
2746 int fd = -1;
2747 struct got_fileindex_diff_dir_cb fdiff_cb;
2748 struct diff_dir_cb_arg arg;
2749 char *ondisk_path = NULL;
2751 if (asprintf(&ondisk_path, "%s%s%s",
2752 worktree->root_path, path[0] ? "/" : "", path) == -1)
2753 return got_error_from_errno("asprintf");
2755 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2756 if (fd == -1) {
2757 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2758 err = got_error_from_errno2("open", ondisk_path);
2759 else
2760 err = report_single_file_status(path, ondisk_path,
2761 fileindex, status_cb, status_arg, repo,
2762 report_unchanged);
2763 } else {
2764 fdiff_cb.diff_old_new = status_old_new;
2765 fdiff_cb.diff_old = status_old;
2766 fdiff_cb.diff_new = status_new;
2767 arg.fileindex = fileindex;
2768 arg.worktree = worktree;
2769 arg.status_path = path;
2770 arg.status_path_len = strlen(path);
2771 arg.repo = repo;
2772 arg.status_cb = status_cb;
2773 arg.status_arg = status_arg;
2774 arg.cancel_cb = cancel_cb;
2775 arg.cancel_arg = cancel_arg;
2776 arg.report_unchanged = report_unchanged;
2777 TAILQ_INIT(&arg.ignores);
2778 if (!no_ignores) {
2779 err = add_ignores(&arg.ignores, worktree->root_path,
2780 path, fd, ".cvsignore");
2781 if (err == NULL)
2782 err = add_ignores(&arg.ignores,
2783 worktree->root_path, path, fd,
2784 ".gitignore");
2786 if (err == NULL)
2787 err = got_fileindex_diff_dir(fileindex, fd,
2788 worktree->root_path, path, repo, &fdiff_cb, &arg);
2789 free_ignores(&arg.ignores);
2792 if (fd != -1 && close(fd) != 0 && err == NULL)
2793 err = got_error_from_errno("close");
2794 free(ondisk_path);
2795 return err;
2798 const struct got_error *
2799 got_worktree_status(struct got_worktree *worktree,
2800 struct got_pathlist_head *paths, struct got_repository *repo,
2801 got_worktree_status_cb status_cb, void *status_arg,
2802 got_cancel_cb cancel_cb, void *cancel_arg)
2804 const struct got_error *err = NULL;
2805 char *fileindex_path = NULL;
2806 struct got_fileindex *fileindex = NULL;
2807 struct got_pathlist_entry *pe;
2809 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2810 if (err)
2811 return err;
2813 TAILQ_FOREACH(pe, paths, entry) {
2814 err = worktree_status(worktree, pe->path, fileindex, repo,
2815 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2816 if (err)
2817 break;
2819 free(fileindex_path);
2820 got_fileindex_free(fileindex);
2821 return err;
2824 const struct got_error *
2825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2826 const char *arg)
2828 const struct got_error *err = NULL;
2829 char *resolved, *cwd = NULL, *path = NULL;
2830 size_t len;
2832 *wt_path = NULL;
2834 resolved = realpath(arg, NULL);
2835 if (resolved == NULL) {
2836 if (errno != ENOENT)
2837 return got_error_from_errno2("realpath", arg);
2838 cwd = getcwd(NULL, 0);
2839 if (cwd == NULL)
2840 return got_error_from_errno("getcwd");
2841 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2842 err = got_error_from_errno("asprintf");
2843 goto done;
2847 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2848 strlen(got_worktree_get_root_path(worktree)))) {
2849 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2850 goto done;
2853 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2854 err = got_path_skip_common_ancestor(&path,
2855 got_worktree_get_root_path(worktree), resolved);
2856 if (err)
2857 goto done;
2858 } else {
2859 path = strdup("");
2860 if (path == NULL) {
2861 err = got_error_from_errno("strdup");
2862 goto done;
2866 /* XXX status walk can't deal with trailing slash! */
2867 len = strlen(path);
2868 while (len > 0 && path[len - 1] == '/') {
2869 path[len - 1] = '\0';
2870 len--;
2872 done:
2873 free(resolved);
2874 free(cwd);
2875 if (err == NULL)
2876 *wt_path = path;
2877 else
2878 free(path);
2879 return err;
2882 struct schedule_addition_args {
2883 struct got_worktree *worktree;
2884 struct got_fileindex *fileindex;
2885 got_worktree_checkout_cb progress_cb;
2886 void *progress_arg;
2887 struct got_repository *repo;
2890 static const struct got_error *
2891 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2892 const char *relpath, struct got_object_id *blob_id,
2893 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2894 int dirfd, const char *de_name)
2896 struct schedule_addition_args *a = arg;
2897 const struct got_error *err = NULL;
2898 struct got_fileindex_entry *ie;
2899 struct stat sb;
2900 char *ondisk_path;
2902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2903 relpath) == -1)
2904 return got_error_from_errno("asprintf");
2906 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2907 if (ie) {
2908 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2909 de_name, a->repo);
2910 if (err)
2911 goto done;
2912 /* Re-adding an existing entry is a no-op. */
2913 if (status == GOT_STATUS_ADD)
2914 goto done;
2915 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2916 if (err)
2917 goto done;
2920 if (status != GOT_STATUS_UNVERSIONED) {
2921 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2922 goto done;
2925 err = got_fileindex_entry_alloc(&ie, relpath);
2926 if (err)
2927 goto done;
2928 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2929 if (err) {
2930 got_fileindex_entry_free(ie);
2931 goto done;
2933 err = got_fileindex_entry_add(a->fileindex, ie);
2934 if (err) {
2935 got_fileindex_entry_free(ie);
2936 goto done;
2938 done:
2939 free(ondisk_path);
2940 if (err)
2941 return err;
2942 if (status == GOT_STATUS_ADD)
2943 return NULL;
2944 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2947 const struct got_error *
2948 got_worktree_schedule_add(struct got_worktree *worktree,
2949 struct got_pathlist_head *paths,
2950 got_worktree_checkout_cb progress_cb, void *progress_arg,
2951 struct got_repository *repo, int no_ignores)
2953 struct got_fileindex *fileindex = NULL;
2954 char *fileindex_path = NULL;
2955 const struct got_error *err = NULL, *sync_err, *unlockerr;
2956 struct got_pathlist_entry *pe;
2957 struct schedule_addition_args saa;
2959 err = lock_worktree(worktree, LOCK_EX);
2960 if (err)
2961 return err;
2963 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2964 if (err)
2965 goto done;
2967 saa.worktree = worktree;
2968 saa.fileindex = fileindex;
2969 saa.progress_cb = progress_cb;
2970 saa.progress_arg = progress_arg;
2971 saa.repo = repo;
2973 TAILQ_FOREACH(pe, paths, entry) {
2974 err = worktree_status(worktree, pe->path, fileindex, repo,
2975 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2976 if (err)
2977 break;
2979 sync_err = sync_fileindex(fileindex, fileindex_path);
2980 if (sync_err && err == NULL)
2981 err = sync_err;
2982 done:
2983 free(fileindex_path);
2984 if (fileindex)
2985 got_fileindex_free(fileindex);
2986 unlockerr = lock_worktree(worktree, LOCK_SH);
2987 if (unlockerr && err == NULL)
2988 err = unlockerr;
2989 return err;
2992 struct schedule_deletion_args {
2993 struct got_worktree *worktree;
2994 struct got_fileindex *fileindex;
2995 got_worktree_delete_cb progress_cb;
2996 void *progress_arg;
2997 struct got_repository *repo;
2998 int delete_local_mods;
2999 int keep_on_disk;
3002 static const struct got_error *
3003 schedule_for_deletion(void *arg, unsigned char status,
3004 unsigned char staged_status, const char *relpath,
3005 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3006 struct got_object_id *commit_id, int dirfd, const char *de_name)
3008 struct schedule_deletion_args *a = arg;
3009 const struct got_error *err = NULL;
3010 struct got_fileindex_entry *ie = NULL;
3011 struct stat sb;
3012 char *ondisk_path, *parent = NULL;
3014 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3015 if (ie == NULL)
3016 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3018 staged_status = get_staged_status(ie);
3019 if (staged_status != GOT_STATUS_NO_CHANGE) {
3020 if (staged_status == GOT_STATUS_DELETE)
3021 return NULL;
3022 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3025 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3026 relpath) == -1)
3027 return got_error_from_errno("asprintf");
3029 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3030 a->repo);
3031 if (err)
3032 goto done;
3034 if (status != GOT_STATUS_NO_CHANGE) {
3035 if (status == GOT_STATUS_DELETE)
3036 goto done;
3037 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3038 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3039 goto done;
3041 if (status != GOT_STATUS_MODIFY &&
3042 status != GOT_STATUS_MISSING) {
3043 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3044 goto done;
3048 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3049 if (dirfd != -1) {
3050 if (unlinkat(dirfd, de_name, 0) != 0) {
3051 err = got_error_from_errno2("unlinkat",
3052 ondisk_path);
3053 goto done;
3055 } else if (unlink(ondisk_path) != 0) {
3056 err = got_error_from_errno2("unlink", ondisk_path);
3057 goto done;
3060 parent = dirname(ondisk_path);
3062 if (parent == NULL) {
3063 err = got_error_from_errno2("dirname", ondisk_path);
3064 goto done;
3066 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3067 if (rmdir(parent) == -1) {
3068 if (errno != ENOTEMPTY)
3069 err = got_error_from_errno2("rmdir",
3070 parent);
3071 break;
3073 parent = dirname(parent);
3074 if (parent == NULL) {
3075 err = got_error_from_errno2("dirname", parent);
3076 goto done;
3081 got_fileindex_entry_mark_deleted_from_disk(ie);
3082 done:
3083 free(ondisk_path);
3084 if (err)
3085 return err;
3086 if (status == GOT_STATUS_DELETE)
3087 return NULL;
3088 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3089 staged_status, relpath);
3092 const struct got_error *
3093 got_worktree_schedule_delete(struct got_worktree *worktree,
3094 struct got_pathlist_head *paths, int delete_local_mods,
3095 got_worktree_delete_cb progress_cb, void *progress_arg,
3096 struct got_repository *repo, int keep_on_disk)
3098 struct got_fileindex *fileindex = NULL;
3099 char *fileindex_path = NULL;
3100 const struct got_error *err = NULL, *sync_err, *unlockerr;
3101 struct got_pathlist_entry *pe;
3102 struct schedule_deletion_args sda;
3104 err = lock_worktree(worktree, LOCK_EX);
3105 if (err)
3106 return err;
3108 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3109 if (err)
3110 goto done;
3112 sda.worktree = worktree;
3113 sda.fileindex = fileindex;
3114 sda.progress_cb = progress_cb;
3115 sda.progress_arg = progress_arg;
3116 sda.repo = repo;
3117 sda.delete_local_mods = delete_local_mods;
3118 sda.keep_on_disk = keep_on_disk;
3120 TAILQ_FOREACH(pe, paths, entry) {
3121 err = worktree_status(worktree, pe->path, fileindex, repo,
3122 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3123 if (err)
3124 break;
3126 sync_err = sync_fileindex(fileindex, fileindex_path);
3127 if (sync_err && err == NULL)
3128 err = sync_err;
3129 done:
3130 free(fileindex_path);
3131 if (fileindex)
3132 got_fileindex_free(fileindex);
3133 unlockerr = lock_worktree(worktree, LOCK_SH);
3134 if (unlockerr && err == NULL)
3135 err = unlockerr;
3136 return err;
3139 static const struct got_error *
3140 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3142 const struct got_error *err = NULL;
3143 char *line = NULL;
3144 size_t linesize = 0, n;
3145 ssize_t linelen;
3147 linelen = getline(&line, &linesize, infile);
3148 if (linelen == -1) {
3149 if (ferror(infile)) {
3150 err = got_error_from_errno("getline");
3151 goto done;
3153 return NULL;
3155 if (outfile) {
3156 n = fwrite(line, 1, linelen, outfile);
3157 if (n != linelen) {
3158 err = got_ferror(outfile, GOT_ERR_IO);
3159 goto done;
3162 if (rejectfile) {
3163 n = fwrite(line, 1, linelen, rejectfile);
3164 if (n != linelen)
3165 err = got_ferror(outfile, GOT_ERR_IO);
3167 done:
3168 free(line);
3169 return err;
3172 static const struct got_error *
3173 skip_one_line(FILE *f)
3175 char *line = NULL;
3176 size_t linesize = 0;
3177 ssize_t linelen;
3179 linelen = getline(&line, &linesize, f);
3180 if (linelen == -1) {
3181 if (ferror(f))
3182 return got_error_from_errno("getline");
3183 return NULL;
3185 free(line);
3186 return NULL;
3189 static const struct got_error *
3190 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3191 int start_old, int end_old, int start_new, int end_new,
3192 FILE *outfile, FILE *rejectfile)
3194 const struct got_error *err;
3196 /* Copy old file's lines leading up to patch. */
3197 while (!feof(f1) && *line_cur1 < start_old) {
3198 err = copy_one_line(f1, outfile, NULL);
3199 if (err)
3200 return err;
3201 (*line_cur1)++;
3203 /* Skip new file's lines leading up to patch. */
3204 while (!feof(f2) && *line_cur2 < start_new) {
3205 if (rejectfile)
3206 err = copy_one_line(f2, NULL, rejectfile);
3207 else
3208 err = skip_one_line(f2);
3209 if (err)
3210 return err;
3211 (*line_cur2)++;
3213 /* Copy patched lines. */
3214 while (!feof(f2) && *line_cur2 <= end_new) {
3215 err = copy_one_line(f2, outfile, NULL);
3216 if (err)
3217 return err;
3218 (*line_cur2)++;
3220 /* Skip over old file's replaced lines. */
3221 while (!feof(f1) && *line_cur1 <= end_old) {
3222 if (rejectfile)
3223 err = copy_one_line(f1, NULL, rejectfile);
3224 else
3225 err = skip_one_line(f1);
3226 if (err)
3227 return err;
3228 (*line_cur1)++;
3231 return NULL;
3234 static const struct got_error *
3235 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3236 FILE *outfile, FILE *rejectfile)
3238 const struct got_error *err;
3240 if (outfile) {
3241 /* Copy old file's lines until EOF. */
3242 while (!feof(f1)) {
3243 err = copy_one_line(f1, outfile, NULL);
3244 if (err)
3245 return err;
3246 (*line_cur1)++;
3249 if (rejectfile) {
3250 /* Copy new file's lines until EOF. */
3251 while (!feof(f2)) {
3252 err = copy_one_line(f2, NULL, rejectfile);
3253 if (err)
3254 return err;
3255 (*line_cur2)++;
3259 return NULL;
3262 static const struct got_error *
3263 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3264 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3265 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3266 int *line_cur2, FILE *outfile, FILE *rejectfile,
3267 got_worktree_patch_cb patch_cb, void *patch_arg)
3269 const struct got_error *err = NULL;
3270 int start_old = change->cv.a;
3271 int end_old = change->cv.b;
3272 int start_new = change->cv.c;
3273 int end_new = change->cv.d;
3274 long pos1, pos2;
3275 FILE *hunkfile;
3277 *choice = GOT_PATCH_CHOICE_NONE;
3279 hunkfile = got_opentemp();
3280 if (hunkfile == NULL)
3281 return got_error_from_errno("got_opentemp");
3283 pos1 = ftell(f1);
3284 pos2 = ftell(f2);
3286 /* XXX TODO needs error checking */
3287 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3289 if (fseek(f1, pos1, SEEK_SET) == -1) {
3290 err = got_ferror(f1, GOT_ERR_IO);
3291 goto done;
3293 if (fseek(f2, pos2, SEEK_SET) == -1) {
3294 err = got_ferror(f1, GOT_ERR_IO);
3295 goto done;
3297 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3298 err = got_ferror(hunkfile, GOT_ERR_IO);
3299 goto done;
3302 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3303 hunkfile, n, nchanges);
3304 if (err)
3305 goto done;
3307 switch (*choice) {
3308 case GOT_PATCH_CHOICE_YES:
3309 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3310 end_old, start_new, end_new, outfile, rejectfile);
3311 break;
3312 case GOT_PATCH_CHOICE_NO:
3313 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3314 end_old, start_new, end_new, rejectfile, outfile);
3315 break;
3316 case GOT_PATCH_CHOICE_QUIT:
3317 break;
3318 default:
3319 err = got_error(GOT_ERR_PATCH_CHOICE);
3320 break;
3322 done:
3323 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3324 err = got_error_from_errno("fclose");
3325 return err;
3328 struct revert_file_args {
3329 struct got_worktree *worktree;
3330 struct got_fileindex *fileindex;
3331 got_worktree_checkout_cb progress_cb;
3332 void *progress_arg;
3333 got_worktree_patch_cb patch_cb;
3334 void *patch_arg;
3335 struct got_repository *repo;
3338 static const struct got_error *
3339 create_patched_content(char **path_outfile, int reverse_patch,
3340 struct got_object_id *blob_id, const char *path2,
3341 int dirfd2, const char *de_name2,
3342 const char *relpath, struct got_repository *repo,
3343 got_worktree_patch_cb patch_cb, void *patch_arg)
3345 const struct got_error *err;
3346 struct got_blob_object *blob = NULL;
3347 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3348 int fd2 = -1;
3349 char *path1 = NULL, *id_str = NULL;
3350 struct stat sb1, sb2;
3351 struct got_diff_changes *changes = NULL;
3352 struct got_diff_state *ds = NULL;
3353 struct got_diff_args *args = NULL;
3354 struct got_diff_change *change;
3355 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3356 int n = 0;
3358 *path_outfile = NULL;
3360 err = got_object_id_str(&id_str, blob_id);
3361 if (err)
3362 return err;
3364 if (dirfd2 != -1) {
3365 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3366 if (fd2 == -1) {
3367 err = got_error_from_errno2("openat", path2);
3368 goto done;
3370 } else {
3371 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3372 if (fd2 == -1) {
3373 err = got_error_from_errno2("open", path2);
3374 goto done;
3377 if (fstat(fd2, &sb2) == -1) {
3378 err = got_error_from_errno2("fstat", path2);
3379 goto done;
3382 f2 = fdopen(fd2, "r");
3383 if (f2 == NULL) {
3384 err = got_error_from_errno2("fdopen", path2);
3385 goto done;
3387 fd2 = -1;
3389 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3390 if (err)
3391 goto done;
3393 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3394 if (err)
3395 goto done;
3397 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3398 if (err)
3399 goto done;
3401 if (stat(path1, &sb1) == -1) {
3402 err = got_error_from_errno2("stat", path1);
3403 goto done;
3406 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3407 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3408 if (err)
3409 goto done;
3411 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3412 if (err)
3413 goto done;
3415 if (fseek(f1, 0L, SEEK_SET) == -1)
3416 return got_ferror(f1, GOT_ERR_IO);
3417 if (fseek(f2, 0L, SEEK_SET) == -1)
3418 return got_ferror(f2, GOT_ERR_IO);
3419 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3420 int choice;
3421 err = apply_or_reject_change(&choice, change, ++n,
3422 changes->nchanges, ds, args, diff_flags, relpath,
3423 f1, f2, &line_cur1, &line_cur2,
3424 reverse_patch ? NULL : outfile,
3425 reverse_patch ? outfile : NULL,
3426 patch_cb, patch_arg);
3427 if (err)
3428 goto done;
3429 if (choice == GOT_PATCH_CHOICE_YES)
3430 have_content = 1;
3431 else if (choice == GOT_PATCH_CHOICE_QUIT)
3432 break;
3434 if (have_content) {
3435 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3436 reverse_patch ? NULL : outfile,
3437 reverse_patch ? outfile : NULL);
3438 if (err)
3439 goto done;
3441 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3442 err = got_error_from_errno2("chmod", path2);
3443 goto done;
3446 done:
3447 free(id_str);
3448 if (blob)
3449 got_object_blob_close(blob);
3450 if (f1 && fclose(f1) == EOF && err == NULL)
3451 err = got_error_from_errno2("fclose", path1);
3452 if (f2 && fclose(f2) == EOF && err == NULL)
3453 err = got_error_from_errno2("fclose", path2);
3454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3455 err = got_error_from_errno2("close", path2);
3456 if (outfile && fclose(outfile) == EOF && err == NULL)
3457 err = got_error_from_errno2("fclose", *path_outfile);
3458 if (path1 && unlink(path1) == -1 && err == NULL)
3459 err = got_error_from_errno2("unlink", path1);
3460 if (err || !have_content) {
3461 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3462 err = got_error_from_errno2("unlink", *path_outfile);
3463 free(*path_outfile);
3464 *path_outfile = NULL;
3466 free(args);
3467 if (ds) {
3468 got_diff_state_free(ds);
3469 free(ds);
3471 if (changes)
3472 got_diff_free_changes(changes);
3473 free(path1);
3474 return err;
3477 static const struct got_error *
3478 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3479 const char *relpath, struct got_object_id *blob_id,
3480 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3481 int dirfd, const char *de_name)
3483 struct revert_file_args *a = arg;
3484 const struct got_error *err = NULL;
3485 char *parent_path = NULL;
3486 struct got_fileindex_entry *ie;
3487 struct got_tree_object *tree = NULL;
3488 struct got_object_id *tree_id = NULL;
3489 const struct got_tree_entry *te = NULL;
3490 char *tree_path = NULL, *te_name;
3491 char *ondisk_path = NULL, *path_content = NULL;
3492 struct got_blob_object *blob = NULL;
3494 /* Reverting a staged deletion is a no-op. */
3495 if (status == GOT_STATUS_DELETE &&
3496 staged_status != GOT_STATUS_NO_CHANGE)
3497 return NULL;
3499 if (status == GOT_STATUS_UNVERSIONED)
3500 return (*a->progress_cb)(a->progress_arg,
3501 GOT_STATUS_UNVERSIONED, relpath);
3503 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3504 if (ie == NULL)
3505 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3507 /* Construct in-repository path of tree which contains this blob. */
3508 err = got_path_dirname(&parent_path, ie->path);
3509 if (err) {
3510 if (err->code != GOT_ERR_BAD_PATH)
3511 goto done;
3512 parent_path = strdup("/");
3513 if (parent_path == NULL) {
3514 err = got_error_from_errno("strdup");
3515 goto done;
3518 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3519 tree_path = strdup(parent_path);
3520 if (tree_path == NULL) {
3521 err = got_error_from_errno("strdup");
3522 goto done;
3524 } else {
3525 if (got_path_is_root_dir(parent_path)) {
3526 tree_path = strdup(a->worktree->path_prefix);
3527 if (tree_path == NULL) {
3528 err = got_error_from_errno("strdup");
3529 goto done;
3531 } else {
3532 if (asprintf(&tree_path, "%s/%s",
3533 a->worktree->path_prefix, parent_path) == -1) {
3534 err = got_error_from_errno("asprintf");
3535 goto done;
3540 err = got_object_id_by_path(&tree_id, a->repo,
3541 a->worktree->base_commit_id, tree_path);
3542 if (err) {
3543 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3544 (status == GOT_STATUS_ADD ||
3545 staged_status == GOT_STATUS_ADD)))
3546 goto done;
3547 } else {
3548 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3549 if (err)
3550 goto done;
3552 te_name = basename(ie->path);
3553 if (te_name == NULL) {
3554 err = got_error_from_errno2("basename", ie->path);
3555 goto done;
3558 te = got_object_tree_find_entry(tree, te_name);
3559 if (te == NULL && status != GOT_STATUS_ADD &&
3560 staged_status != GOT_STATUS_ADD) {
3561 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3562 goto done;
3566 switch (status) {
3567 case GOT_STATUS_ADD:
3568 if (a->patch_cb) {
3569 int choice = GOT_PATCH_CHOICE_NONE;
3570 err = (*a->patch_cb)(&choice, a->patch_arg,
3571 status, ie->path, NULL, 1, 1);
3572 if (err)
3573 goto done;
3574 if (choice != GOT_PATCH_CHOICE_YES)
3575 break;
3577 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3578 ie->path);
3579 if (err)
3580 goto done;
3581 got_fileindex_entry_remove(a->fileindex, ie);
3582 break;
3583 case GOT_STATUS_DELETE:
3584 if (a->patch_cb) {
3585 int choice = GOT_PATCH_CHOICE_NONE;
3586 err = (*a->patch_cb)(&choice, a->patch_arg,
3587 status, ie->path, NULL, 1, 1);
3588 if (err)
3589 goto done;
3590 if (choice != GOT_PATCH_CHOICE_YES)
3591 break;
3593 /* fall through */
3594 case GOT_STATUS_MODIFY:
3595 case GOT_STATUS_MODE_CHANGE:
3596 case GOT_STATUS_CONFLICT:
3597 case GOT_STATUS_MISSING: {
3598 struct got_object_id id;
3599 if (staged_status == GOT_STATUS_ADD ||
3600 staged_status == GOT_STATUS_MODIFY) {
3601 memcpy(id.sha1, ie->staged_blob_sha1,
3602 SHA1_DIGEST_LENGTH);
3603 } else
3604 memcpy(id.sha1, ie->blob_sha1,
3605 SHA1_DIGEST_LENGTH);
3606 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3607 if (err)
3608 goto done;
3610 if (asprintf(&ondisk_path, "%s/%s",
3611 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3612 err = got_error_from_errno("asprintf");
3613 goto done;
3616 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3617 status == GOT_STATUS_CONFLICT)) {
3618 err = create_patched_content(&path_content, 1, &id,
3619 ondisk_path, dirfd, de_name, ie->path, a->repo,
3620 a->patch_cb, a->patch_arg);
3621 if (err || path_content == NULL)
3622 break;
3623 if (rename(path_content, ondisk_path) == -1) {
3624 err = got_error_from_errno3("rename",
3625 path_content, ondisk_path);
3626 goto done;
3628 } else {
3629 err = install_blob(a->worktree, ondisk_path, ie->path,
3630 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3631 got_fileindex_perms_to_st(ie), blob, 0, 1,
3632 a->repo, a->progress_cb, a->progress_arg);
3633 if (err)
3634 goto done;
3635 if (status == GOT_STATUS_DELETE ||
3636 status == GOT_STATUS_MODE_CHANGE) {
3637 err = update_blob_fileindex_entry(a->worktree,
3638 a->fileindex, ie, ondisk_path, ie->path,
3639 blob, 1);
3640 if (err)
3641 goto done;
3644 break;
3646 default:
3647 break;
3649 done:
3650 free(ondisk_path);
3651 free(path_content);
3652 free(parent_path);
3653 free(tree_path);
3654 if (blob)
3655 got_object_blob_close(blob);
3656 if (tree)
3657 got_object_tree_close(tree);
3658 free(tree_id);
3659 return err;
3662 const struct got_error *
3663 got_worktree_revert(struct got_worktree *worktree,
3664 struct got_pathlist_head *paths,
3665 got_worktree_checkout_cb progress_cb, void *progress_arg,
3666 got_worktree_patch_cb patch_cb, void *patch_arg,
3667 struct got_repository *repo)
3669 struct got_fileindex *fileindex = NULL;
3670 char *fileindex_path = NULL;
3671 const struct got_error *err = NULL, *unlockerr = NULL;
3672 const struct got_error *sync_err = NULL;
3673 struct got_pathlist_entry *pe;
3674 struct revert_file_args rfa;
3676 err = lock_worktree(worktree, LOCK_EX);
3677 if (err)
3678 return err;
3680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3681 if (err)
3682 goto done;
3684 rfa.worktree = worktree;
3685 rfa.fileindex = fileindex;
3686 rfa.progress_cb = progress_cb;
3687 rfa.progress_arg = progress_arg;
3688 rfa.patch_cb = patch_cb;
3689 rfa.patch_arg = patch_arg;
3690 rfa.repo = repo;
3691 TAILQ_FOREACH(pe, paths, entry) {
3692 err = worktree_status(worktree, pe->path, fileindex, repo,
3693 revert_file, &rfa, NULL, NULL, 0, 0);
3694 if (err)
3695 break;
3697 sync_err = sync_fileindex(fileindex, fileindex_path);
3698 if (sync_err && err == NULL)
3699 err = sync_err;
3700 done:
3701 free(fileindex_path);
3702 if (fileindex)
3703 got_fileindex_free(fileindex);
3704 unlockerr = lock_worktree(worktree, LOCK_SH);
3705 if (unlockerr && err == NULL)
3706 err = unlockerr;
3707 return err;
3710 static void
3711 free_commitable(struct got_commitable *ct)
3713 free(ct->path);
3714 free(ct->in_repo_path);
3715 free(ct->ondisk_path);
3716 free(ct->blob_id);
3717 free(ct->base_blob_id);
3718 free(ct->staged_blob_id);
3719 free(ct->base_commit_id);
3720 free(ct);
3723 struct collect_commitables_arg {
3724 struct got_pathlist_head *commitable_paths;
3725 struct got_repository *repo;
3726 struct got_worktree *worktree;
3727 int have_staged_files;
3730 static const struct got_error *
3731 collect_commitables(void *arg, unsigned char status,
3732 unsigned char staged_status, const char *relpath,
3733 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3734 struct got_object_id *commit_id, int dirfd, const char *de_name)
3736 struct collect_commitables_arg *a = arg;
3737 const struct got_error *err = NULL;
3738 struct got_commitable *ct = NULL;
3739 struct got_pathlist_entry *new = NULL;
3740 char *parent_path = NULL, *path = NULL;
3741 struct stat sb;
3743 if (a->have_staged_files) {
3744 if (staged_status != GOT_STATUS_MODIFY &&
3745 staged_status != GOT_STATUS_ADD &&
3746 staged_status != GOT_STATUS_DELETE)
3747 return NULL;
3748 } else {
3749 if (status == GOT_STATUS_CONFLICT)
3750 return got_error(GOT_ERR_COMMIT_CONFLICT);
3752 if (status != GOT_STATUS_MODIFY &&
3753 status != GOT_STATUS_MODE_CHANGE &&
3754 status != GOT_STATUS_ADD &&
3755 status != GOT_STATUS_DELETE)
3756 return NULL;
3759 if (asprintf(&path, "/%s", relpath) == -1) {
3760 err = got_error_from_errno("asprintf");
3761 goto done;
3763 if (strcmp(path, "/") == 0) {
3764 parent_path = strdup("");
3765 if (parent_path == NULL)
3766 return got_error_from_errno("strdup");
3767 } else {
3768 err = got_path_dirname(&parent_path, path);
3769 if (err)
3770 return err;
3773 ct = calloc(1, sizeof(*ct));
3774 if (ct == NULL) {
3775 err = got_error_from_errno("calloc");
3776 goto done;
3779 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3780 relpath) == -1) {
3781 err = got_error_from_errno("asprintf");
3782 goto done;
3784 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3785 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3786 } else {
3787 if (dirfd != -1) {
3788 if (fstatat(dirfd, de_name, &sb,
3789 AT_SYMLINK_NOFOLLOW) == -1) {
3790 err = got_error_from_errno2("fstatat",
3791 ct->ondisk_path);
3792 goto done;
3794 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3795 err = got_error_from_errno2("lstat", ct->ondisk_path);
3796 goto done;
3798 ct->mode = sb.st_mode;
3801 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3802 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3803 relpath) == -1) {
3804 err = got_error_from_errno("asprintf");
3805 goto done;
3808 ct->status = status;
3809 ct->staged_status = staged_status;
3810 ct->blob_id = NULL; /* will be filled in when blob gets created */
3811 if (ct->status != GOT_STATUS_ADD &&
3812 ct->staged_status != GOT_STATUS_ADD) {
3813 ct->base_blob_id = got_object_id_dup(blob_id);
3814 if (ct->base_blob_id == NULL) {
3815 err = got_error_from_errno("got_object_id_dup");
3816 goto done;
3818 ct->base_commit_id = got_object_id_dup(commit_id);
3819 if (ct->base_commit_id == NULL) {
3820 err = got_error_from_errno("got_object_id_dup");
3821 goto done;
3824 if (ct->staged_status == GOT_STATUS_ADD ||
3825 ct->staged_status == GOT_STATUS_MODIFY) {
3826 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3827 if (ct->staged_blob_id == NULL) {
3828 err = got_error_from_errno("got_object_id_dup");
3829 goto done;
3832 ct->path = strdup(path);
3833 if (ct->path == NULL) {
3834 err = got_error_from_errno("strdup");
3835 goto done;
3837 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3838 done:
3839 if (ct && (err || new == NULL))
3840 free_commitable(ct);
3841 free(parent_path);
3842 free(path);
3843 return err;
3846 static const struct got_error *write_tree(struct got_object_id **,
3847 struct got_tree_object *, const char *, struct got_pathlist_head *,
3848 got_worktree_status_cb status_cb, void *status_arg,
3849 struct got_repository *);
3851 static const struct got_error *
3852 write_subtree(struct got_object_id **new_subtree_id,
3853 struct got_tree_entry *te, const char *parent_path,
3854 struct got_pathlist_head *commitable_paths,
3855 got_worktree_status_cb status_cb, void *status_arg,
3856 struct got_repository *repo)
3858 const struct got_error *err = NULL;
3859 struct got_tree_object *subtree;
3860 char *subpath;
3862 if (asprintf(&subpath, "%s%s%s", parent_path,
3863 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3864 return got_error_from_errno("asprintf");
3866 err = got_object_open_as_tree(&subtree, repo, &te->id);
3867 if (err)
3868 return err;
3870 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3871 status_cb, status_arg, repo);
3872 got_object_tree_close(subtree);
3873 free(subpath);
3874 return err;
3877 static const struct got_error *
3878 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3880 const struct got_error *err = NULL;
3881 char *ct_parent_path = NULL;
3883 *match = 0;
3885 if (strchr(ct->in_repo_path, '/') == NULL) {
3886 *match = got_path_is_root_dir(path);
3887 return NULL;
3890 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3891 if (err)
3892 return err;
3893 *match = (strcmp(path, ct_parent_path) == 0);
3894 free(ct_parent_path);
3895 return err;
3898 static mode_t
3899 get_ct_file_mode(struct got_commitable *ct)
3901 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3904 static const struct got_error *
3905 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3906 struct got_tree_entry *te, struct got_commitable *ct)
3908 const struct got_error *err = NULL;
3910 *new_te = NULL;
3912 err = got_object_tree_entry_dup(new_te, te);
3913 if (err)
3914 goto done;
3916 (*new_te)->mode = get_ct_file_mode(ct);
3918 if (ct->staged_status == GOT_STATUS_MODIFY)
3919 memcpy(&(*new_te)->id, ct->staged_blob_id,
3920 sizeof((*new_te)->id));
3921 else
3922 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3923 done:
3924 if (err && *new_te) {
3925 free(*new_te);
3926 *new_te = NULL;
3928 return err;
3931 static const struct got_error *
3932 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3933 struct got_commitable *ct)
3935 const struct got_error *err = NULL;
3936 char *ct_name;
3938 *new_te = NULL;
3940 *new_te = calloc(1, sizeof(**new_te));
3941 if (*new_te == NULL)
3942 return got_error_from_errno("calloc");
3944 ct_name = basename(ct->path);
3945 if (ct_name == NULL) {
3946 err = got_error_from_errno2("basename", ct->path);
3947 goto done;
3949 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3950 sizeof((*new_te)->name)) {
3951 err = got_error(GOT_ERR_NO_SPACE);
3952 goto done;
3955 (*new_te)->mode = get_ct_file_mode(ct);
3957 if (ct->staged_status == GOT_STATUS_ADD)
3958 memcpy(&(*new_te)->id, ct->staged_blob_id,
3959 sizeof((*new_te)->id));
3960 else
3961 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3962 done:
3963 if (err && *new_te) {
3964 free(*new_te);
3965 *new_te = NULL;
3967 return err;
3970 static const struct got_error *
3971 insert_tree_entry(struct got_tree_entry *new_te,
3972 struct got_pathlist_head *paths)
3974 const struct got_error *err = NULL;
3975 struct got_pathlist_entry *new_pe;
3977 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3978 if (err)
3979 return err;
3980 if (new_pe == NULL)
3981 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3982 return NULL;
3985 static const struct got_error *
3986 report_ct_status(struct got_commitable *ct,
3987 got_worktree_status_cb status_cb, void *status_arg)
3989 const char *ct_path = ct->path;
3990 unsigned char status;
3992 while (ct_path[0] == '/')
3993 ct_path++;
3995 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3996 status = ct->staged_status;
3997 else
3998 status = ct->status;
4000 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4001 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4004 static const struct got_error *
4005 match_modified_subtree(int *modified, struct got_tree_entry *te,
4006 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4008 const struct got_error *err = NULL;
4009 struct got_pathlist_entry *pe;
4010 char *te_path;
4012 *modified = 0;
4014 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4015 got_path_is_root_dir(base_tree_path) ? "" : "/",
4016 te->name) == -1)
4017 return got_error_from_errno("asprintf");
4019 TAILQ_FOREACH(pe, commitable_paths, entry) {
4020 struct got_commitable *ct = pe->data;
4021 *modified = got_path_is_child(ct->in_repo_path, te_path,
4022 strlen(te_path));
4023 if (*modified)
4024 break;
4027 free(te_path);
4028 return err;
4031 static const struct got_error *
4032 match_deleted_or_modified_ct(struct got_commitable **ctp,
4033 struct got_tree_entry *te, const char *base_tree_path,
4034 struct got_pathlist_head *commitable_paths)
4036 const struct got_error *err = NULL;
4037 struct got_pathlist_entry *pe;
4039 *ctp = NULL;
4041 TAILQ_FOREACH(pe, commitable_paths, entry) {
4042 struct got_commitable *ct = pe->data;
4043 char *ct_name = NULL;
4044 int path_matches;
4046 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4047 if (ct->status != GOT_STATUS_MODIFY &&
4048 ct->status != GOT_STATUS_MODE_CHANGE &&
4049 ct->status != GOT_STATUS_DELETE)
4050 continue;
4051 } else {
4052 if (ct->staged_status != GOT_STATUS_MODIFY &&
4053 ct->staged_status != GOT_STATUS_DELETE)
4054 continue;
4057 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4058 continue;
4060 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4061 if (err)
4062 return err;
4063 if (!path_matches)
4064 continue;
4066 ct_name = basename(pe->path);
4067 if (ct_name == NULL)
4068 return got_error_from_errno2("basename", pe->path);
4070 if (strcmp(te->name, ct_name) != 0)
4071 continue;
4073 *ctp = ct;
4074 break;
4077 return err;
4080 static const struct got_error *
4081 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4082 const char *child_path, const char *path_base_tree,
4083 struct got_pathlist_head *commitable_paths,
4084 got_worktree_status_cb status_cb, void *status_arg,
4085 struct got_repository *repo)
4087 const struct got_error *err = NULL;
4088 struct got_tree_entry *new_te;
4089 char *subtree_path;
4090 struct got_object_id *id = NULL;
4092 *new_tep = NULL;
4094 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4095 got_path_is_root_dir(path_base_tree) ? "" : "/",
4096 child_path) == -1)
4097 return got_error_from_errno("asprintf");
4099 new_te = calloc(1, sizeof(*new_te));
4100 if (new_te == NULL)
4101 return got_error_from_errno("calloc");
4102 new_te->mode = S_IFDIR;
4104 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4105 sizeof(new_te->name)) {
4106 err = got_error(GOT_ERR_NO_SPACE);
4107 goto done;
4109 err = write_tree(&id, NULL, subtree_path,
4110 commitable_paths, status_cb, status_arg, repo);
4111 if (err) {
4112 free(new_te);
4113 goto done;
4115 memcpy(&new_te->id, id, sizeof(new_te->id));
4116 done:
4117 free(id);
4118 free(subtree_path);
4119 if (err == NULL)
4120 *new_tep = new_te;
4121 return err;
4124 static const struct got_error *
4125 write_tree(struct got_object_id **new_tree_id,
4126 struct got_tree_object *base_tree, const char *path_base_tree,
4127 struct got_pathlist_head *commitable_paths,
4128 got_worktree_status_cb status_cb, void *status_arg,
4129 struct got_repository *repo)
4131 const struct got_error *err = NULL;
4132 struct got_pathlist_head paths;
4133 struct got_tree_entry *te, *new_te = NULL;
4134 struct got_pathlist_entry *pe;
4135 int nentries = 0;
4137 TAILQ_INIT(&paths);
4139 /* Insert, and recurse into, newly added entries first. */
4140 TAILQ_FOREACH(pe, commitable_paths, entry) {
4141 struct got_commitable *ct = pe->data;
4142 char *child_path = NULL, *slash;
4144 if ((ct->status != GOT_STATUS_ADD &&
4145 ct->staged_status != GOT_STATUS_ADD) ||
4146 (ct->flags & GOT_COMMITABLE_ADDED))
4147 continue;
4149 if (!got_path_is_child(pe->path, path_base_tree,
4150 strlen(path_base_tree)))
4151 continue;
4153 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4154 pe->path);
4155 if (err)
4156 goto done;
4158 slash = strchr(child_path, '/');
4159 if (slash == NULL) {
4160 err = alloc_added_blob_tree_entry(&new_te, ct);
4161 if (err)
4162 goto done;
4163 err = report_ct_status(ct, status_cb, status_arg);
4164 if (err)
4165 goto done;
4166 ct->flags |= GOT_COMMITABLE_ADDED;
4167 err = insert_tree_entry(new_te, &paths);
4168 if (err)
4169 goto done;
4170 nentries++;
4171 } else {
4172 *slash = '\0'; /* trim trailing path components */
4173 if (base_tree == NULL ||
4174 got_object_tree_find_entry(base_tree, child_path)
4175 == NULL) {
4176 err = make_subtree_for_added_blob(&new_te,
4177 child_path, path_base_tree,
4178 commitable_paths, status_cb, status_arg,
4179 repo);
4180 if (err)
4181 goto done;
4182 err = insert_tree_entry(new_te, &paths);
4183 if (err)
4184 goto done;
4185 nentries++;
4190 if (base_tree) {
4191 int i, nbase_entries;
4192 /* Handle modified and deleted entries. */
4193 nbase_entries = got_object_tree_get_nentries(base_tree);
4194 for (i = 0; i < nbase_entries; i++) {
4195 struct got_commitable *ct = NULL;
4197 te = got_object_tree_get_entry(base_tree, i);
4198 if (got_object_tree_entry_is_submodule(te)) {
4199 /* Entry is a submodule; just copy it. */
4200 err = got_object_tree_entry_dup(&new_te, te);
4201 if (err)
4202 goto done;
4203 err = insert_tree_entry(new_te, &paths);
4204 if (err)
4205 goto done;
4206 nentries++;
4207 continue;
4210 if (S_ISDIR(te->mode)) {
4211 int modified;
4212 err = got_object_tree_entry_dup(&new_te, te);
4213 if (err)
4214 goto done;
4215 err = match_modified_subtree(&modified, te,
4216 path_base_tree, commitable_paths);
4217 if (err)
4218 goto done;
4219 /* Avoid recursion into unmodified subtrees. */
4220 if (modified) {
4221 struct got_object_id *new_id;
4222 err = write_subtree(&new_id, te,
4223 path_base_tree, commitable_paths,
4224 status_cb, status_arg, repo);
4225 if (err)
4226 goto done;
4227 memcpy(&new_te->id, new_id,
4228 sizeof(new_te->id));
4229 free(new_id);
4231 err = insert_tree_entry(new_te, &paths);
4232 if (err)
4233 goto done;
4234 nentries++;
4235 continue;
4238 err = match_deleted_or_modified_ct(&ct, te,
4239 path_base_tree, commitable_paths);
4240 if (err)
4241 goto done;
4242 if (ct) {
4243 /* NB: Deleted entries get dropped here. */
4244 if (ct->status == GOT_STATUS_MODIFY ||
4245 ct->status == GOT_STATUS_MODE_CHANGE ||
4246 ct->staged_status == GOT_STATUS_MODIFY) {
4247 err = alloc_modified_blob_tree_entry(
4248 &new_te, te, ct);
4249 if (err)
4250 goto done;
4251 err = insert_tree_entry(new_te, &paths);
4252 if (err)
4253 goto done;
4254 nentries++;
4256 err = report_ct_status(ct, status_cb,
4257 status_arg);
4258 if (err)
4259 goto done;
4260 } else {
4261 /* Entry is unchanged; just copy it. */
4262 err = got_object_tree_entry_dup(&new_te, te);
4263 if (err)
4264 goto done;
4265 err = insert_tree_entry(new_te, &paths);
4266 if (err)
4267 goto done;
4268 nentries++;
4273 /* Write new list of entries; deleted entries have been dropped. */
4274 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4275 done:
4276 got_pathlist_free(&paths);
4277 return err;
4280 static const struct got_error *
4281 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4282 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4283 int have_staged_files)
4285 const struct got_error *err = NULL;
4286 struct got_pathlist_entry *pe;
4288 TAILQ_FOREACH(pe, commitable_paths, entry) {
4289 struct got_fileindex_entry *ie;
4290 struct got_commitable *ct = pe->data;
4292 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4293 if (ie) {
4294 if (ct->status == GOT_STATUS_DELETE ||
4295 ct->staged_status == GOT_STATUS_DELETE) {
4296 got_fileindex_entry_remove(fileindex, ie);
4297 } else if (ct->staged_status == GOT_STATUS_ADD ||
4298 ct->staged_status == GOT_STATUS_MODIFY) {
4299 got_fileindex_entry_stage_set(ie,
4300 GOT_FILEIDX_STAGE_NONE);
4301 err = got_fileindex_entry_update(ie,
4302 ct->ondisk_path, ct->staged_blob_id->sha1,
4303 new_base_commit_id->sha1,
4304 !have_staged_files);
4305 } else
4306 err = got_fileindex_entry_update(ie,
4307 ct->ondisk_path, ct->blob_id->sha1,
4308 new_base_commit_id->sha1,
4309 !have_staged_files);
4310 } else {
4311 err = got_fileindex_entry_alloc(&ie, pe->path);
4312 if (err)
4313 break;
4314 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4315 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4316 if (err) {
4317 got_fileindex_entry_free(ie);
4318 break;
4320 err = got_fileindex_entry_add(fileindex, ie);
4321 if (err) {
4322 got_fileindex_entry_free(ie);
4323 break;
4327 return err;
4331 static const struct got_error *
4332 check_out_of_date(const char *in_repo_path, unsigned char status,
4333 unsigned char staged_status, struct got_object_id *base_blob_id,
4334 struct got_object_id *base_commit_id,
4335 struct got_object_id *head_commit_id, struct got_repository *repo,
4336 int ood_errcode)
4338 const struct got_error *err = NULL;
4339 struct got_object_id *id = NULL;
4341 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4342 /* Trivial case: base commit == head commit */
4343 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4344 return NULL;
4346 * Ensure file content which local changes were based
4347 * on matches file content in the branch head.
4349 err = got_object_id_by_path(&id, repo, head_commit_id,
4350 in_repo_path);
4351 if (err) {
4352 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4353 err = got_error(ood_errcode);
4354 goto done;
4355 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4356 err = got_error(ood_errcode);
4357 } else {
4358 /* Require that added files don't exist in the branch head. */
4359 err = got_object_id_by_path(&id, repo, head_commit_id,
4360 in_repo_path);
4361 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4362 goto done;
4363 err = id ? got_error(ood_errcode) : NULL;
4365 done:
4366 free(id);
4367 return err;
4370 const struct got_error *
4371 commit_worktree(struct got_object_id **new_commit_id,
4372 struct got_pathlist_head *commitable_paths,
4373 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4374 const char *author, const char *committer,
4375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4376 got_worktree_status_cb status_cb, void *status_arg,
4377 struct got_repository *repo)
4379 const struct got_error *err = NULL, *unlockerr = NULL;
4380 struct got_pathlist_entry *pe;
4381 const char *head_ref_name = NULL;
4382 struct got_commit_object *head_commit = NULL;
4383 struct got_reference *head_ref2 = NULL;
4384 struct got_object_id *head_commit_id2 = NULL;
4385 struct got_tree_object *head_tree = NULL;
4386 struct got_object_id *new_tree_id = NULL;
4387 struct got_object_id_queue parent_ids;
4388 struct got_object_qid *pid = NULL;
4389 char *logmsg = NULL;
4391 *new_commit_id = NULL;
4393 SIMPLEQ_INIT(&parent_ids);
4395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4396 if (err)
4397 goto done;
4399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4400 if (err)
4401 goto done;
4403 if (commit_msg_cb != NULL) {
4404 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4405 if (err)
4406 goto done;
4409 if (logmsg == NULL || strlen(logmsg) == 0) {
4410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4411 goto done;
4414 /* Create blobs from added and modified files and record their IDs. */
4415 TAILQ_FOREACH(pe, commitable_paths, entry) {
4416 struct got_commitable *ct = pe->data;
4417 char *ondisk_path;
4419 /* Blobs for staged files already exist. */
4420 if (ct->staged_status == GOT_STATUS_ADD ||
4421 ct->staged_status == GOT_STATUS_MODIFY)
4422 continue;
4424 if (ct->status != GOT_STATUS_ADD &&
4425 ct->status != GOT_STATUS_MODIFY &&
4426 ct->status != GOT_STATUS_MODE_CHANGE)
4427 continue;
4429 if (asprintf(&ondisk_path, "%s/%s",
4430 worktree->root_path, pe->path) == -1) {
4431 err = got_error_from_errno("asprintf");
4432 goto done;
4434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4435 free(ondisk_path);
4436 if (err)
4437 goto done;
4440 /* Recursively write new tree objects. */
4441 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4442 status_cb, status_arg, repo);
4443 if (err)
4444 goto done;
4446 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4447 if (err)
4448 goto done;
4449 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4450 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4451 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4452 got_object_qid_free(pid);
4453 if (logmsg != NULL)
4454 free(logmsg);
4455 if (err)
4456 goto done;
4458 /* Check if a concurrent commit to our branch has occurred. */
4459 head_ref_name = got_worktree_get_head_ref_name(worktree);
4460 if (head_ref_name == NULL) {
4461 err = got_error_from_errno("got_worktree_get_head_ref_name");
4462 goto done;
4464 /* Lock the reference here to prevent concurrent modification. */
4465 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4466 if (err)
4467 goto done;
4468 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4469 if (err)
4470 goto done;
4471 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4472 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4473 goto done;
4475 /* Update branch head in repository. */
4476 err = got_ref_change_ref(head_ref2, *new_commit_id);
4477 if (err)
4478 goto done;
4479 err = got_ref_write(head_ref2, repo);
4480 if (err)
4481 goto done;
4483 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4484 if (err)
4485 goto done;
4487 err = ref_base_commit(worktree, repo);
4488 if (err)
4489 goto done;
4490 done:
4491 if (head_tree)
4492 got_object_tree_close(head_tree);
4493 if (head_commit)
4494 got_object_commit_close(head_commit);
4495 free(head_commit_id2);
4496 if (head_ref2) {
4497 unlockerr = got_ref_unlock(head_ref2);
4498 if (unlockerr && err == NULL)
4499 err = unlockerr;
4500 got_ref_close(head_ref2);
4502 return err;
4505 static const struct got_error *
4506 check_path_is_commitable(const char *path,
4507 struct got_pathlist_head *commitable_paths)
4509 struct got_pathlist_entry *cpe = NULL;
4510 size_t path_len = strlen(path);
4512 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4513 struct got_commitable *ct = cpe->data;
4514 const char *ct_path = ct->path;
4516 while (ct_path[0] == '/')
4517 ct_path++;
4519 if (strcmp(path, ct_path) == 0 ||
4520 got_path_is_child(ct_path, path, path_len))
4521 break;
4524 if (cpe == NULL)
4525 return got_error_path(path, GOT_ERR_BAD_PATH);
4527 return NULL;
4530 static const struct got_error *
4531 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4533 int *have_staged_files = arg;
4535 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4536 *have_staged_files = 1;
4537 return got_error(GOT_ERR_CANCELLED);
4540 return NULL;
4543 static const struct got_error *
4544 check_non_staged_files(struct got_fileindex *fileindex,
4545 struct got_pathlist_head *paths)
4547 struct got_pathlist_entry *pe;
4548 struct got_fileindex_entry *ie;
4550 TAILQ_FOREACH(pe, paths, entry) {
4551 if (pe->path[0] == '\0')
4552 continue;
4553 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4554 if (ie == NULL)
4555 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4556 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4557 return got_error_path(pe->path,
4558 GOT_ERR_FILE_NOT_STAGED);
4561 return NULL;
4564 const struct got_error *
4565 got_worktree_commit(struct got_object_id **new_commit_id,
4566 struct got_worktree *worktree, struct got_pathlist_head *paths,
4567 const char *author, const char *committer,
4568 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4569 got_worktree_status_cb status_cb, void *status_arg,
4570 struct got_repository *repo)
4572 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4573 struct got_fileindex *fileindex = NULL;
4574 char *fileindex_path = NULL;
4575 struct got_pathlist_head commitable_paths;
4576 struct collect_commitables_arg cc_arg;
4577 struct got_pathlist_entry *pe;
4578 struct got_reference *head_ref = NULL;
4579 struct got_object_id *head_commit_id = NULL;
4580 int have_staged_files = 0;
4582 *new_commit_id = NULL;
4584 TAILQ_INIT(&commitable_paths);
4586 err = lock_worktree(worktree, LOCK_EX);
4587 if (err)
4588 goto done;
4590 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4591 if (err)
4592 goto done;
4594 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4595 if (err)
4596 goto done;
4598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4599 if (err)
4600 goto done;
4602 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4603 &have_staged_files);
4604 if (err && err->code != GOT_ERR_CANCELLED)
4605 goto done;
4606 if (have_staged_files) {
4607 err = check_non_staged_files(fileindex, paths);
4608 if (err)
4609 goto done;
4612 cc_arg.commitable_paths = &commitable_paths;
4613 cc_arg.worktree = worktree;
4614 cc_arg.repo = repo;
4615 cc_arg.have_staged_files = have_staged_files;
4616 TAILQ_FOREACH(pe, paths, entry) {
4617 err = worktree_status(worktree, pe->path, fileindex, repo,
4618 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4619 if (err)
4620 goto done;
4623 if (TAILQ_EMPTY(&commitable_paths)) {
4624 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4625 goto done;
4628 TAILQ_FOREACH(pe, paths, entry) {
4629 err = check_path_is_commitable(pe->path, &commitable_paths);
4630 if (err)
4631 goto done;
4634 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4635 struct got_commitable *ct = pe->data;
4636 const char *ct_path = ct->in_repo_path;
4638 while (ct_path[0] == '/')
4639 ct_path++;
4640 err = check_out_of_date(ct_path, ct->status,
4641 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4642 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4643 if (err)
4644 goto done;
4648 err = commit_worktree(new_commit_id, &commitable_paths,
4649 head_commit_id, worktree, author, committer,
4650 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4651 if (err)
4652 goto done;
4654 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4655 fileindex, have_staged_files);
4656 sync_err = sync_fileindex(fileindex, fileindex_path);
4657 if (sync_err && err == NULL)
4658 err = sync_err;
4659 done:
4660 if (fileindex)
4661 got_fileindex_free(fileindex);
4662 free(fileindex_path);
4663 unlockerr = lock_worktree(worktree, LOCK_SH);
4664 if (unlockerr && err == NULL)
4665 err = unlockerr;
4666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4667 struct got_commitable *ct = pe->data;
4668 free_commitable(ct);
4670 got_pathlist_free(&commitable_paths);
4671 return err;
4674 const char *
4675 got_commitable_get_path(struct got_commitable *ct)
4677 return ct->path;
4680 unsigned int
4681 got_commitable_get_status(struct got_commitable *ct)
4683 return ct->status;
4686 struct check_rebase_ok_arg {
4687 struct got_worktree *worktree;
4688 struct got_repository *repo;
4691 static const struct got_error *
4692 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4694 const struct got_error *err = NULL;
4695 struct check_rebase_ok_arg *a = arg;
4696 unsigned char status;
4697 struct stat sb;
4698 char *ondisk_path;
4700 /* Reject rebase of a work tree with mixed base commits. */
4701 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4702 SHA1_DIGEST_LENGTH))
4703 return got_error(GOT_ERR_MIXED_COMMITS);
4705 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4706 == -1)
4707 return got_error_from_errno("asprintf");
4709 /* Reject rebase of a work tree with modified or staged files. */
4710 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4711 free(ondisk_path);
4712 if (err)
4713 return err;
4715 if (status != GOT_STATUS_NO_CHANGE)
4716 return got_error(GOT_ERR_MODIFIED);
4717 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4718 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4720 return NULL;
4723 const struct got_error *
4724 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4725 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4726 struct got_worktree *worktree, struct got_reference *branch,
4727 struct got_repository *repo)
4729 const struct got_error *err = NULL;
4730 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4731 char *branch_ref_name = NULL;
4732 char *fileindex_path = NULL;
4733 struct check_rebase_ok_arg ok_arg;
4734 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4735 struct got_object_id *wt_branch_tip = NULL;
4737 *new_base_branch_ref = NULL;
4738 *tmp_branch = NULL;
4739 *fileindex = NULL;
4741 err = lock_worktree(worktree, LOCK_EX);
4742 if (err)
4743 return err;
4745 err = open_fileindex(fileindex, &fileindex_path, worktree);
4746 if (err)
4747 goto done;
4749 ok_arg.worktree = worktree;
4750 ok_arg.repo = repo;
4751 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4752 &ok_arg);
4753 if (err)
4754 goto done;
4756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4757 if (err)
4758 goto done;
4760 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4761 if (err)
4762 goto done;
4764 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4765 if (err)
4766 goto done;
4768 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4769 0);
4770 if (err)
4771 goto done;
4773 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4774 if (err)
4775 goto done;
4776 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4777 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4778 goto done;
4781 err = got_ref_alloc_symref(new_base_branch_ref,
4782 new_base_branch_ref_name, wt_branch);
4783 if (err)
4784 goto done;
4785 err = got_ref_write(*new_base_branch_ref, repo);
4786 if (err)
4787 goto done;
4789 /* TODO Lock original branch's ref while rebasing? */
4791 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4792 if (err)
4793 goto done;
4795 err = got_ref_write(branch_ref, repo);
4796 if (err)
4797 goto done;
4799 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4800 worktree->base_commit_id);
4801 if (err)
4802 goto done;
4803 err = got_ref_write(*tmp_branch, repo);
4804 if (err)
4805 goto done;
4807 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4808 if (err)
4809 goto done;
4810 done:
4811 free(fileindex_path);
4812 free(tmp_branch_name);
4813 free(new_base_branch_ref_name);
4814 free(branch_ref_name);
4815 if (branch_ref)
4816 got_ref_close(branch_ref);
4817 if (wt_branch)
4818 got_ref_close(wt_branch);
4819 free(wt_branch_tip);
4820 if (err) {
4821 if (*new_base_branch_ref) {
4822 got_ref_close(*new_base_branch_ref);
4823 *new_base_branch_ref = NULL;
4825 if (*tmp_branch) {
4826 got_ref_close(*tmp_branch);
4827 *tmp_branch = NULL;
4829 if (*fileindex) {
4830 got_fileindex_free(*fileindex);
4831 *fileindex = NULL;
4833 lock_worktree(worktree, LOCK_SH);
4835 return err;
4838 const struct got_error *
4839 got_worktree_rebase_continue(struct got_object_id **commit_id,
4840 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4841 struct got_reference **branch, struct got_fileindex **fileindex,
4842 struct got_worktree *worktree, struct got_repository *repo)
4844 const struct got_error *err;
4845 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4846 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4847 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4848 char *fileindex_path = NULL;
4849 int have_staged_files = 0;
4851 *commit_id = NULL;
4852 *new_base_branch = NULL;
4853 *tmp_branch = NULL;
4854 *branch = NULL;
4855 *fileindex = NULL;
4857 err = lock_worktree(worktree, LOCK_EX);
4858 if (err)
4859 return err;
4861 err = open_fileindex(fileindex, &fileindex_path, worktree);
4862 if (err)
4863 goto done;
4865 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4866 &have_staged_files);
4867 if (err && err->code != GOT_ERR_CANCELLED)
4868 goto done;
4869 if (have_staged_files) {
4870 err = got_error(GOT_ERR_STAGED_PATHS);
4871 goto done;
4874 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4875 if (err)
4876 goto done;
4878 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4879 if (err)
4880 goto done;
4882 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4883 if (err)
4884 goto done;
4886 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4887 if (err)
4888 goto done;
4890 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4891 if (err)
4892 goto done;
4894 err = got_ref_open(branch, repo,
4895 got_ref_get_symref_target(branch_ref), 0);
4896 if (err)
4897 goto done;
4899 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4900 if (err)
4901 goto done;
4903 err = got_ref_resolve(commit_id, repo, commit_ref);
4904 if (err)
4905 goto done;
4907 err = got_ref_open(new_base_branch, repo,
4908 new_base_branch_ref_name, 0);
4909 if (err)
4910 goto done;
4912 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4913 if (err)
4914 goto done;
4915 done:
4916 free(commit_ref_name);
4917 free(branch_ref_name);
4918 free(fileindex_path);
4919 if (commit_ref)
4920 got_ref_close(commit_ref);
4921 if (branch_ref)
4922 got_ref_close(branch_ref);
4923 if (err) {
4924 free(*commit_id);
4925 *commit_id = NULL;
4926 if (*tmp_branch) {
4927 got_ref_close(*tmp_branch);
4928 *tmp_branch = NULL;
4930 if (*new_base_branch) {
4931 got_ref_close(*new_base_branch);
4932 *new_base_branch = NULL;
4934 if (*branch) {
4935 got_ref_close(*branch);
4936 *branch = NULL;
4938 if (*fileindex) {
4939 got_fileindex_free(*fileindex);
4940 *fileindex = NULL;
4942 lock_worktree(worktree, LOCK_SH);
4944 return err;
4947 const struct got_error *
4948 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4950 const struct got_error *err;
4951 char *tmp_branch_name = NULL;
4953 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4954 if (err)
4955 return err;
4957 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4958 free(tmp_branch_name);
4959 return NULL;
4962 static const struct got_error *
4963 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4964 char **logmsg, void *arg)
4966 *logmsg = arg;
4967 return NULL;
4970 static const struct got_error *
4971 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4972 const char *path, struct got_object_id *blob_id,
4973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4974 int dirfd, const char *de_name)
4976 return NULL;
4979 struct collect_merged_paths_arg {
4980 got_worktree_checkout_cb progress_cb;
4981 void *progress_arg;
4982 struct got_pathlist_head *merged_paths;
4985 static const struct got_error *
4986 collect_merged_paths(void *arg, unsigned char status, const char *path)
4988 const struct got_error *err;
4989 struct collect_merged_paths_arg *a = arg;
4990 char *p;
4991 struct got_pathlist_entry *new;
4993 err = (*a->progress_cb)(a->progress_arg, status, path);
4994 if (err)
4995 return err;
4997 if (status != GOT_STATUS_MERGE &&
4998 status != GOT_STATUS_ADD &&
4999 status != GOT_STATUS_DELETE &&
5000 status != GOT_STATUS_CONFLICT)
5001 return NULL;
5003 p = strdup(path);
5004 if (p == NULL)
5005 return got_error_from_errno("strdup");
5007 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5008 if (err || new == NULL)
5009 free(p);
5010 return err;
5013 void
5014 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5016 struct got_pathlist_entry *pe;
5018 TAILQ_FOREACH(pe, merged_paths, entry)
5019 free((char *)pe->path);
5021 got_pathlist_free(merged_paths);
5024 static const struct got_error *
5025 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5026 int is_rebase, struct got_repository *repo)
5028 const struct got_error *err;
5029 struct got_reference *commit_ref = NULL;
5031 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5032 if (err) {
5033 if (err->code != GOT_ERR_NOT_REF)
5034 goto done;
5035 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5036 if (err)
5037 goto done;
5038 err = got_ref_write(commit_ref, repo);
5039 if (err)
5040 goto done;
5041 } else if (is_rebase) {
5042 struct got_object_id *stored_id;
5043 int cmp;
5045 err = got_ref_resolve(&stored_id, repo, commit_ref);
5046 if (err)
5047 goto done;
5048 cmp = got_object_id_cmp(commit_id, stored_id);
5049 free(stored_id);
5050 if (cmp != 0) {
5051 err = got_error(GOT_ERR_REBASE_COMMITID);
5052 goto done;
5055 done:
5056 if (commit_ref)
5057 got_ref_close(commit_ref);
5058 return err;
5061 static const struct got_error *
5062 rebase_merge_files(struct got_pathlist_head *merged_paths,
5063 const char *commit_ref_name, struct got_worktree *worktree,
5064 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5065 struct got_object_id *commit_id, struct got_repository *repo,
5066 got_worktree_checkout_cb progress_cb, void *progress_arg,
5067 got_cancel_cb cancel_cb, void *cancel_arg)
5069 const struct got_error *err;
5070 struct got_reference *commit_ref = NULL;
5071 struct collect_merged_paths_arg cmp_arg;
5072 char *fileindex_path;
5074 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5076 err = get_fileindex_path(&fileindex_path, worktree);
5077 if (err)
5078 return err;
5080 cmp_arg.progress_cb = progress_cb;
5081 cmp_arg.progress_arg = progress_arg;
5082 cmp_arg.merged_paths = merged_paths;
5083 err = merge_files(worktree, fileindex, fileindex_path,
5084 parent_commit_id, commit_id, repo, collect_merged_paths,
5085 &cmp_arg, cancel_cb, cancel_arg);
5086 if (commit_ref)
5087 got_ref_close(commit_ref);
5088 return err;
5091 const struct got_error *
5092 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5093 struct got_worktree *worktree, struct got_fileindex *fileindex,
5094 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5095 struct got_repository *repo,
5096 got_worktree_checkout_cb progress_cb, void *progress_arg,
5097 got_cancel_cb cancel_cb, void *cancel_arg)
5099 const struct got_error *err;
5100 char *commit_ref_name;
5102 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5103 if (err)
5104 return err;
5106 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5107 if (err)
5108 goto done;
5110 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5111 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5112 progress_arg, cancel_cb, cancel_arg);
5113 done:
5114 free(commit_ref_name);
5115 return err;
5118 const struct got_error *
5119 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5120 struct got_worktree *worktree, struct got_fileindex *fileindex,
5121 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5122 struct got_repository *repo,
5123 got_worktree_checkout_cb progress_cb, void *progress_arg,
5124 got_cancel_cb cancel_cb, void *cancel_arg)
5126 const struct got_error *err;
5127 char *commit_ref_name;
5129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5130 if (err)
5131 return err;
5133 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5134 if (err)
5135 goto done;
5137 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5138 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5139 progress_arg, cancel_cb, cancel_arg);
5140 done:
5141 free(commit_ref_name);
5142 return err;
5145 static const struct got_error *
5146 rebase_commit(struct got_object_id **new_commit_id,
5147 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5148 struct got_worktree *worktree, struct got_fileindex *fileindex,
5149 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5150 const char *new_logmsg, struct got_repository *repo)
5152 const struct got_error *err, *sync_err;
5153 struct got_pathlist_head commitable_paths;
5154 struct collect_commitables_arg cc_arg;
5155 char *fileindex_path = NULL;
5156 struct got_reference *head_ref = NULL;
5157 struct got_object_id *head_commit_id = NULL;
5158 char *logmsg = NULL;
5160 TAILQ_INIT(&commitable_paths);
5161 *new_commit_id = NULL;
5163 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5165 err = get_fileindex_path(&fileindex_path, worktree);
5166 if (err)
5167 return err;
5169 cc_arg.commitable_paths = &commitable_paths;
5170 cc_arg.worktree = worktree;
5171 cc_arg.repo = repo;
5172 cc_arg.have_staged_files = 0;
5174 * If possible get the status of individual files directly to
5175 * avoid crawling the entire work tree once per rebased commit.
5176 * TODO: Ideally, merged_paths would contain a list of commitables
5177 * we could use so we could skip worktree_status() entirely.
5179 if (merged_paths) {
5180 struct got_pathlist_entry *pe;
5181 if (TAILQ_EMPTY(merged_paths)) {
5182 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5183 goto done;
5185 TAILQ_FOREACH(pe, merged_paths, entry) {
5186 err = worktree_status(worktree, pe->path, fileindex,
5187 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5188 0);
5189 if (err)
5190 goto done;
5192 } else {
5193 err = worktree_status(worktree, "", fileindex, repo,
5194 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5195 if (err)
5196 goto done;
5199 if (TAILQ_EMPTY(&commitable_paths)) {
5200 /* No-op change; commit will be elided. */
5201 err = got_ref_delete(commit_ref, repo);
5202 if (err)
5203 goto done;
5204 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5205 goto done;
5208 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5209 if (err)
5210 goto done;
5212 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5213 if (err)
5214 goto done;
5216 if (new_logmsg) {
5217 logmsg = strdup(new_logmsg);
5218 if (logmsg == NULL) {
5219 err = got_error_from_errno("strdup");
5220 goto done;
5222 } else {
5223 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5224 if (err)
5225 goto done;
5228 /* NB: commit_worktree will call free(logmsg) */
5229 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5230 worktree, got_object_commit_get_author(orig_commit),
5231 got_object_commit_get_committer(orig_commit),
5232 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5233 if (err)
5234 goto done;
5236 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5237 if (err)
5238 goto done;
5240 err = got_ref_delete(commit_ref, repo);
5241 if (err)
5242 goto done;
5244 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5245 fileindex, 0);
5246 sync_err = sync_fileindex(fileindex, fileindex_path);
5247 if (sync_err && err == NULL)
5248 err = sync_err;
5249 done:
5250 free(fileindex_path);
5251 free(head_commit_id);
5252 if (head_ref)
5253 got_ref_close(head_ref);
5254 if (err) {
5255 free(*new_commit_id);
5256 *new_commit_id = NULL;
5258 return err;
5261 const struct got_error *
5262 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5263 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5264 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5265 struct got_commit_object *orig_commit,
5266 struct got_object_id *orig_commit_id, struct got_repository *repo)
5268 const struct got_error *err;
5269 char *commit_ref_name;
5270 struct got_reference *commit_ref = NULL;
5271 struct got_object_id *commit_id = NULL;
5273 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5274 if (err)
5275 return err;
5277 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5278 if (err)
5279 goto done;
5280 err = got_ref_resolve(&commit_id, repo, commit_ref);
5281 if (err)
5282 goto done;
5283 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5284 err = got_error(GOT_ERR_REBASE_COMMITID);
5285 goto done;
5288 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5289 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5290 done:
5291 if (commit_ref)
5292 got_ref_close(commit_ref);
5293 free(commit_ref_name);
5294 free(commit_id);
5295 return err;
5298 const struct got_error *
5299 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5300 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5301 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5302 struct got_commit_object *orig_commit,
5303 struct got_object_id *orig_commit_id, const char *new_logmsg,
5304 struct got_repository *repo)
5306 const struct got_error *err;
5307 char *commit_ref_name;
5308 struct got_reference *commit_ref = NULL;
5310 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5311 if (err)
5312 return err;
5314 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5315 if (err)
5316 goto done;
5318 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5319 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5320 done:
5321 if (commit_ref)
5322 got_ref_close(commit_ref);
5323 free(commit_ref_name);
5324 return err;
5327 const struct got_error *
5328 got_worktree_rebase_postpone(struct got_worktree *worktree,
5329 struct got_fileindex *fileindex)
5331 if (fileindex)
5332 got_fileindex_free(fileindex);
5333 return lock_worktree(worktree, LOCK_SH);
5336 static const struct got_error *
5337 delete_ref(const char *name, struct got_repository *repo)
5339 const struct got_error *err;
5340 struct got_reference *ref;
5342 err = got_ref_open(&ref, repo, name, 0);
5343 if (err) {
5344 if (err->code == GOT_ERR_NOT_REF)
5345 return NULL;
5346 return err;
5349 err = got_ref_delete(ref, repo);
5350 got_ref_close(ref);
5351 return err;
5354 static const struct got_error *
5355 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5357 const struct got_error *err;
5358 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5359 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5361 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5362 if (err)
5363 goto done;
5364 err = delete_ref(tmp_branch_name, repo);
5365 if (err)
5366 goto done;
5368 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5369 if (err)
5370 goto done;
5371 err = delete_ref(new_base_branch_ref_name, repo);
5372 if (err)
5373 goto done;
5375 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5376 if (err)
5377 goto done;
5378 err = delete_ref(branch_ref_name, repo);
5379 if (err)
5380 goto done;
5382 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5383 if (err)
5384 goto done;
5385 err = delete_ref(commit_ref_name, repo);
5386 if (err)
5387 goto done;
5389 done:
5390 free(tmp_branch_name);
5391 free(new_base_branch_ref_name);
5392 free(branch_ref_name);
5393 free(commit_ref_name);
5394 return err;
5397 const struct got_error *
5398 got_worktree_rebase_complete(struct got_worktree *worktree,
5399 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5400 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5401 struct got_repository *repo)
5403 const struct got_error *err, *unlockerr;
5404 struct got_object_id *new_head_commit_id = NULL;
5406 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5407 if (err)
5408 return err;
5410 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5411 if (err)
5412 goto done;
5414 err = got_ref_write(rebased_branch, repo);
5415 if (err)
5416 goto done;
5418 err = got_worktree_set_head_ref(worktree, rebased_branch);
5419 if (err)
5420 goto done;
5422 err = delete_rebase_refs(worktree, repo);
5423 done:
5424 if (fileindex)
5425 got_fileindex_free(fileindex);
5426 free(new_head_commit_id);
5427 unlockerr = lock_worktree(worktree, LOCK_SH);
5428 if (unlockerr && err == NULL)
5429 err = unlockerr;
5430 return err;
5433 const struct got_error *
5434 got_worktree_rebase_abort(struct got_worktree *worktree,
5435 struct got_fileindex *fileindex, struct got_repository *repo,
5436 struct got_reference *new_base_branch,
5437 got_worktree_checkout_cb progress_cb, void *progress_arg)
5439 const struct got_error *err, *unlockerr, *sync_err;
5440 struct got_reference *resolved = NULL;
5441 struct got_object_id *commit_id = NULL;
5442 char *fileindex_path = NULL;
5443 struct revert_file_args rfa;
5444 struct got_object_id *tree_id = NULL;
5446 err = lock_worktree(worktree, LOCK_EX);
5447 if (err)
5448 return err;
5450 err = got_ref_open(&resolved, repo,
5451 got_ref_get_symref_target(new_base_branch), 0);
5452 if (err)
5453 goto done;
5455 err = got_worktree_set_head_ref(worktree, resolved);
5456 if (err)
5457 goto done;
5460 * XXX commits to the base branch could have happened while
5461 * we were busy rebasing; should we store the original commit ID
5462 * when rebase begins and read it back here?
5464 err = got_ref_resolve(&commit_id, repo, resolved);
5465 if (err)
5466 goto done;
5468 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5469 if (err)
5470 goto done;
5472 err = got_object_id_by_path(&tree_id, repo,
5473 worktree->base_commit_id, worktree->path_prefix);
5474 if (err)
5475 goto done;
5477 err = delete_rebase_refs(worktree, repo);
5478 if (err)
5479 goto done;
5481 err = get_fileindex_path(&fileindex_path, worktree);
5482 if (err)
5483 goto done;
5485 rfa.worktree = worktree;
5486 rfa.fileindex = fileindex;
5487 rfa.progress_cb = progress_cb;
5488 rfa.progress_arg = progress_arg;
5489 rfa.patch_cb = NULL;
5490 rfa.patch_arg = NULL;
5491 rfa.repo = repo;
5492 err = worktree_status(worktree, "", fileindex, repo,
5493 revert_file, &rfa, NULL, NULL, 0, 0);
5494 if (err)
5495 goto sync;
5497 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5498 repo, progress_cb, progress_arg, NULL, NULL);
5499 sync:
5500 sync_err = sync_fileindex(fileindex, fileindex_path);
5501 if (sync_err && err == NULL)
5502 err = sync_err;
5503 done:
5504 got_ref_close(resolved);
5505 free(tree_id);
5506 free(commit_id);
5507 if (fileindex)
5508 got_fileindex_free(fileindex);
5509 free(fileindex_path);
5511 unlockerr = lock_worktree(worktree, LOCK_SH);
5512 if (unlockerr && err == NULL)
5513 err = unlockerr;
5514 return err;
5517 const struct got_error *
5518 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5519 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5520 struct got_fileindex **fileindex, struct got_worktree *worktree,
5521 struct got_repository *repo)
5523 const struct got_error *err = NULL;
5524 char *tmp_branch_name = NULL;
5525 char *branch_ref_name = NULL;
5526 char *base_commit_ref_name = NULL;
5527 char *fileindex_path = NULL;
5528 struct check_rebase_ok_arg ok_arg;
5529 struct got_reference *wt_branch = NULL;
5530 struct got_reference *base_commit_ref = NULL;
5532 *tmp_branch = NULL;
5533 *branch_ref = NULL;
5534 *base_commit_id = NULL;
5535 *fileindex = NULL;
5537 err = lock_worktree(worktree, LOCK_EX);
5538 if (err)
5539 return err;
5541 err = open_fileindex(fileindex, &fileindex_path, worktree);
5542 if (err)
5543 goto done;
5545 ok_arg.worktree = worktree;
5546 ok_arg.repo = repo;
5547 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5548 &ok_arg);
5549 if (err)
5550 goto done;
5552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5553 if (err)
5554 goto done;
5556 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5557 if (err)
5558 goto done;
5560 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5561 worktree);
5562 if (err)
5563 goto done;
5565 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5566 0);
5567 if (err)
5568 goto done;
5570 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5571 if (err)
5572 goto done;
5574 err = got_ref_write(*branch_ref, repo);
5575 if (err)
5576 goto done;
5578 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5579 worktree->base_commit_id);
5580 if (err)
5581 goto done;
5582 err = got_ref_write(base_commit_ref, repo);
5583 if (err)
5584 goto done;
5585 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5586 if (*base_commit_id == NULL) {
5587 err = got_error_from_errno("got_object_id_dup");
5588 goto done;
5591 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5592 worktree->base_commit_id);
5593 if (err)
5594 goto done;
5595 err = got_ref_write(*tmp_branch, repo);
5596 if (err)
5597 goto done;
5599 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5600 if (err)
5601 goto done;
5602 done:
5603 free(fileindex_path);
5604 free(tmp_branch_name);
5605 free(branch_ref_name);
5606 free(base_commit_ref_name);
5607 if (wt_branch)
5608 got_ref_close(wt_branch);
5609 if (err) {
5610 if (*branch_ref) {
5611 got_ref_close(*branch_ref);
5612 *branch_ref = NULL;
5614 if (*tmp_branch) {
5615 got_ref_close(*tmp_branch);
5616 *tmp_branch = NULL;
5618 free(*base_commit_id);
5619 if (*fileindex) {
5620 got_fileindex_free(*fileindex);
5621 *fileindex = NULL;
5623 lock_worktree(worktree, LOCK_SH);
5625 return err;
5628 const struct got_error *
5629 got_worktree_histedit_postpone(struct got_worktree *worktree,
5630 struct got_fileindex *fileindex)
5632 if (fileindex)
5633 got_fileindex_free(fileindex);
5634 return lock_worktree(worktree, LOCK_SH);
5637 const struct got_error *
5638 got_worktree_histedit_in_progress(int *in_progress,
5639 struct got_worktree *worktree)
5641 const struct got_error *err;
5642 char *tmp_branch_name = NULL;
5644 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5645 if (err)
5646 return err;
5648 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5649 free(tmp_branch_name);
5650 return NULL;
5653 const struct got_error *
5654 got_worktree_histedit_continue(struct got_object_id **commit_id,
5655 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5656 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5657 struct got_worktree *worktree, struct got_repository *repo)
5659 const struct got_error *err;
5660 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5661 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5662 struct got_reference *commit_ref = NULL;
5663 struct got_reference *base_commit_ref = NULL;
5664 char *fileindex_path = NULL;
5665 int have_staged_files = 0;
5667 *commit_id = NULL;
5668 *tmp_branch = NULL;
5669 *base_commit_id = NULL;
5670 *fileindex = NULL;
5672 err = lock_worktree(worktree, LOCK_EX);
5673 if (err)
5674 return err;
5676 err = open_fileindex(fileindex, &fileindex_path, worktree);
5677 if (err)
5678 goto done;
5680 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5681 &have_staged_files);
5682 if (err && err->code != GOT_ERR_CANCELLED)
5683 goto done;
5684 if (have_staged_files) {
5685 err = got_error(GOT_ERR_STAGED_PATHS);
5686 goto done;
5689 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5690 if (err)
5691 goto done;
5693 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5694 if (err)
5695 goto done;
5697 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5698 if (err)
5699 goto done;
5701 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5702 worktree);
5703 if (err)
5704 goto done;
5706 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5707 if (err)
5708 goto done;
5710 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5711 if (err)
5712 goto done;
5713 err = got_ref_resolve(commit_id, repo, commit_ref);
5714 if (err)
5715 goto done;
5717 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5718 if (err)
5719 goto done;
5720 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5721 if (err)
5722 goto done;
5724 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5725 if (err)
5726 goto done;
5727 done:
5728 free(commit_ref_name);
5729 free(branch_ref_name);
5730 free(fileindex_path);
5731 if (commit_ref)
5732 got_ref_close(commit_ref);
5733 if (base_commit_ref)
5734 got_ref_close(base_commit_ref);
5735 if (err) {
5736 free(*commit_id);
5737 *commit_id = NULL;
5738 free(*base_commit_id);
5739 *base_commit_id = NULL;
5740 if (*tmp_branch) {
5741 got_ref_close(*tmp_branch);
5742 *tmp_branch = NULL;
5744 if (*fileindex) {
5745 got_fileindex_free(*fileindex);
5746 *fileindex = NULL;
5748 lock_worktree(worktree, LOCK_EX);
5750 return err;
5753 static const struct got_error *
5754 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5756 const struct got_error *err;
5757 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5758 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5760 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5761 if (err)
5762 goto done;
5763 err = delete_ref(tmp_branch_name, repo);
5764 if (err)
5765 goto done;
5767 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5768 worktree);
5769 if (err)
5770 goto done;
5771 err = delete_ref(base_commit_ref_name, repo);
5772 if (err)
5773 goto done;
5775 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5776 if (err)
5777 goto done;
5778 err = delete_ref(branch_ref_name, repo);
5779 if (err)
5780 goto done;
5782 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5783 if (err)
5784 goto done;
5785 err = delete_ref(commit_ref_name, repo);
5786 if (err)
5787 goto done;
5788 done:
5789 free(tmp_branch_name);
5790 free(base_commit_ref_name);
5791 free(branch_ref_name);
5792 free(commit_ref_name);
5793 return err;
5796 const struct got_error *
5797 got_worktree_histedit_abort(struct got_worktree *worktree,
5798 struct got_fileindex *fileindex, struct got_repository *repo,
5799 struct got_reference *branch, struct got_object_id *base_commit_id,
5800 got_worktree_checkout_cb progress_cb, void *progress_arg)
5802 const struct got_error *err, *unlockerr, *sync_err;
5803 struct got_reference *resolved = NULL;
5804 char *fileindex_path = NULL;
5805 struct got_object_id *tree_id = NULL;
5806 struct revert_file_args rfa;
5808 err = lock_worktree(worktree, LOCK_EX);
5809 if (err)
5810 return err;
5812 err = got_ref_open(&resolved, repo,
5813 got_ref_get_symref_target(branch), 0);
5814 if (err)
5815 goto done;
5817 err = got_worktree_set_head_ref(worktree, resolved);
5818 if (err)
5819 goto done;
5821 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5822 if (err)
5823 goto done;
5825 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5826 worktree->path_prefix);
5827 if (err)
5828 goto done;
5830 err = delete_histedit_refs(worktree, repo);
5831 if (err)
5832 goto done;
5834 err = get_fileindex_path(&fileindex_path, worktree);
5835 if (err)
5836 goto done;
5838 rfa.worktree = worktree;
5839 rfa.fileindex = fileindex;
5840 rfa.progress_cb = progress_cb;
5841 rfa.progress_arg = progress_arg;
5842 rfa.patch_cb = NULL;
5843 rfa.patch_arg = NULL;
5844 rfa.repo = repo;
5845 err = worktree_status(worktree, "", fileindex, repo,
5846 revert_file, &rfa, NULL, NULL, 0, 0);
5847 if (err)
5848 goto sync;
5850 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5851 repo, progress_cb, progress_arg, NULL, NULL);
5852 sync:
5853 sync_err = sync_fileindex(fileindex, fileindex_path);
5854 if (sync_err && err == NULL)
5855 err = sync_err;
5856 done:
5857 got_ref_close(resolved);
5858 free(tree_id);
5859 free(fileindex_path);
5861 unlockerr = lock_worktree(worktree, LOCK_SH);
5862 if (unlockerr && err == NULL)
5863 err = unlockerr;
5864 return err;
5867 const struct got_error *
5868 got_worktree_histedit_complete(struct got_worktree *worktree,
5869 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5870 struct got_reference *edited_branch, struct got_repository *repo)
5872 const struct got_error *err, *unlockerr;
5873 struct got_object_id *new_head_commit_id = NULL;
5874 struct got_reference *resolved = NULL;
5876 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5877 if (err)
5878 return err;
5880 err = got_ref_open(&resolved, repo,
5881 got_ref_get_symref_target(edited_branch), 0);
5882 if (err)
5883 goto done;
5885 err = got_ref_change_ref(resolved, new_head_commit_id);
5886 if (err)
5887 goto done;
5889 err = got_ref_write(resolved, repo);
5890 if (err)
5891 goto done;
5893 err = got_worktree_set_head_ref(worktree, resolved);
5894 if (err)
5895 goto done;
5897 err = delete_histedit_refs(worktree, repo);
5898 done:
5899 if (fileindex)
5900 got_fileindex_free(fileindex);
5901 free(new_head_commit_id);
5902 unlockerr = lock_worktree(worktree, LOCK_SH);
5903 if (unlockerr && err == NULL)
5904 err = unlockerr;
5905 return err;
5908 const struct got_error *
5909 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5910 struct got_object_id *commit_id, struct got_repository *repo)
5912 const struct got_error *err;
5913 char *commit_ref_name;
5915 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5916 if (err)
5917 return err;
5919 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5920 if (err)
5921 goto done;
5923 err = delete_ref(commit_ref_name, repo);
5924 done:
5925 free(commit_ref_name);
5926 return err;
5929 const struct got_error *
5930 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5931 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5932 struct got_worktree *worktree, const char *refname,
5933 struct got_repository *repo)
5935 const struct got_error *err = NULL;
5936 char *fileindex_path = NULL;
5937 struct check_rebase_ok_arg ok_arg;
5939 *fileindex = NULL;
5940 *branch_ref = NULL;
5941 *base_branch_ref = NULL;
5943 err = lock_worktree(worktree, LOCK_EX);
5944 if (err)
5945 return err;
5947 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5948 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5949 "cannot integrate a branch into itself; "
5950 "update -b or different branch name required");
5951 goto done;
5954 err = open_fileindex(fileindex, &fileindex_path, worktree);
5955 if (err)
5956 goto done;
5958 /* Preconditions are the same as for rebase. */
5959 ok_arg.worktree = worktree;
5960 ok_arg.repo = repo;
5961 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5962 &ok_arg);
5963 if (err)
5964 goto done;
5966 err = got_ref_open(branch_ref, repo, refname, 1);
5967 if (err)
5968 goto done;
5970 err = got_ref_open(base_branch_ref, repo,
5971 got_worktree_get_head_ref_name(worktree), 1);
5972 done:
5973 if (err) {
5974 if (*branch_ref) {
5975 got_ref_close(*branch_ref);
5976 *branch_ref = NULL;
5978 if (*base_branch_ref) {
5979 got_ref_close(*base_branch_ref);
5980 *base_branch_ref = NULL;
5982 if (*fileindex) {
5983 got_fileindex_free(*fileindex);
5984 *fileindex = NULL;
5986 lock_worktree(worktree, LOCK_SH);
5988 return err;
5991 const struct got_error *
5992 got_worktree_integrate_continue(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex, struct got_repository *repo,
5994 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5995 got_worktree_checkout_cb progress_cb, void *progress_arg,
5996 got_cancel_cb cancel_cb, void *cancel_arg)
5998 const struct got_error *err = NULL, *sync_err, *unlockerr;
5999 char *fileindex_path = NULL;
6000 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6002 err = get_fileindex_path(&fileindex_path, worktree);
6003 if (err)
6004 goto done;
6006 err = got_ref_resolve(&commit_id, repo, branch_ref);
6007 if (err)
6008 goto done;
6010 err = got_object_id_by_path(&tree_id, repo, commit_id,
6011 worktree->path_prefix);
6012 if (err)
6013 goto done;
6015 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6016 if (err)
6017 goto done;
6019 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6020 progress_cb, progress_arg, cancel_cb, cancel_arg);
6021 if (err)
6022 goto sync;
6024 err = got_ref_change_ref(base_branch_ref, commit_id);
6025 if (err)
6026 goto sync;
6028 err = got_ref_write(base_branch_ref, repo);
6029 sync:
6030 sync_err = sync_fileindex(fileindex, fileindex_path);
6031 if (sync_err && err == NULL)
6032 err = sync_err;
6034 done:
6035 unlockerr = got_ref_unlock(branch_ref);
6036 if (unlockerr && err == NULL)
6037 err = unlockerr;
6038 got_ref_close(branch_ref);
6040 unlockerr = got_ref_unlock(base_branch_ref);
6041 if (unlockerr && err == NULL)
6042 err = unlockerr;
6043 got_ref_close(base_branch_ref);
6045 got_fileindex_free(fileindex);
6046 free(fileindex_path);
6047 free(tree_id);
6049 unlockerr = lock_worktree(worktree, LOCK_SH);
6050 if (unlockerr && err == NULL)
6051 err = unlockerr;
6052 return err;
6055 const struct got_error *
6056 got_worktree_integrate_abort(struct got_worktree *worktree,
6057 struct got_fileindex *fileindex, struct got_repository *repo,
6058 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6060 const struct got_error *err = NULL, *unlockerr = NULL;
6062 got_fileindex_free(fileindex);
6064 err = lock_worktree(worktree, LOCK_SH);
6066 unlockerr = got_ref_unlock(branch_ref);
6067 if (unlockerr && err == NULL)
6068 err = unlockerr;
6069 got_ref_close(branch_ref);
6071 unlockerr = got_ref_unlock(base_branch_ref);
6072 if (unlockerr && err == NULL)
6073 err = unlockerr;
6074 got_ref_close(base_branch_ref);
6076 return err;
6079 struct check_stage_ok_arg {
6080 struct got_object_id *head_commit_id;
6081 struct got_worktree *worktree;
6082 struct got_fileindex *fileindex;
6083 struct got_repository *repo;
6084 int have_changes;
6087 const struct got_error *
6088 check_stage_ok(void *arg, unsigned char status,
6089 unsigned char staged_status, const char *relpath,
6090 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6091 struct got_object_id *commit_id, int dirfd, const char *de_name)
6093 struct check_stage_ok_arg *a = arg;
6094 const struct got_error *err = NULL;
6095 struct got_fileindex_entry *ie;
6096 struct got_object_id base_commit_id;
6097 struct got_object_id *base_commit_idp = NULL;
6098 char *in_repo_path = NULL, *p;
6100 if (status == GOT_STATUS_UNVERSIONED ||
6101 status == GOT_STATUS_NO_CHANGE)
6102 return NULL;
6103 if (status == GOT_STATUS_NONEXISTENT)
6104 return got_error_set_errno(ENOENT, relpath);
6106 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6107 if (ie == NULL)
6108 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6110 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6111 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6112 relpath) == -1)
6113 return got_error_from_errno("asprintf");
6115 if (got_fileindex_entry_has_commit(ie)) {
6116 memcpy(base_commit_id.sha1, ie->commit_sha1,
6117 SHA1_DIGEST_LENGTH);
6118 base_commit_idp = &base_commit_id;
6121 if (status == GOT_STATUS_CONFLICT) {
6122 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6123 goto done;
6124 } else if (status != GOT_STATUS_ADD &&
6125 status != GOT_STATUS_MODIFY &&
6126 status != GOT_STATUS_DELETE) {
6127 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6128 goto done;
6131 a->have_changes = 1;
6133 p = in_repo_path;
6134 while (p[0] == '/')
6135 p++;
6136 err = check_out_of_date(p, status, staged_status,
6137 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6138 GOT_ERR_STAGE_OUT_OF_DATE);
6139 done:
6140 free(in_repo_path);
6141 return err;
6144 struct stage_path_arg {
6145 struct got_worktree *worktree;
6146 struct got_fileindex *fileindex;
6147 struct got_repository *repo;
6148 got_worktree_status_cb status_cb;
6149 void *status_arg;
6150 got_worktree_patch_cb patch_cb;
6151 void *patch_arg;
6152 int staged_something;
6155 static const struct got_error *
6156 stage_path(void *arg, unsigned char status,
6157 unsigned char staged_status, const char *relpath,
6158 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6159 struct got_object_id *commit_id, int dirfd, const char *de_name)
6161 struct stage_path_arg *a = arg;
6162 const struct got_error *err = NULL;
6163 struct got_fileindex_entry *ie;
6164 char *ondisk_path = NULL, *path_content = NULL;
6165 uint32_t stage;
6166 struct got_object_id *new_staged_blob_id = NULL;
6168 if (status == GOT_STATUS_UNVERSIONED)
6169 return NULL;
6171 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6172 if (ie == NULL)
6173 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6175 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6176 relpath)== -1)
6177 return got_error_from_errno("asprintf");
6179 switch (status) {
6180 case GOT_STATUS_ADD:
6181 case GOT_STATUS_MODIFY:
6182 if (a->patch_cb) {
6183 if (status == GOT_STATUS_ADD) {
6184 int choice = GOT_PATCH_CHOICE_NONE;
6185 err = (*a->patch_cb)(&choice, a->patch_arg,
6186 status, ie->path, NULL, 1, 1);
6187 if (err)
6188 break;
6189 if (choice != GOT_PATCH_CHOICE_YES)
6190 break;
6191 } else {
6192 err = create_patched_content(&path_content, 0,
6193 staged_blob_id ? staged_blob_id : blob_id,
6194 ondisk_path, dirfd, de_name, ie->path,
6195 a->repo, a->patch_cb, a->patch_arg);
6196 if (err || path_content == NULL)
6197 break;
6200 err = got_object_blob_create(&new_staged_blob_id,
6201 path_content ? path_content : ondisk_path, a->repo);
6202 if (err)
6203 break;
6204 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6205 SHA1_DIGEST_LENGTH);
6206 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6207 stage = GOT_FILEIDX_STAGE_ADD;
6208 else
6209 stage = GOT_FILEIDX_STAGE_MODIFY;
6210 got_fileindex_entry_stage_set(ie, stage);
6211 a->staged_something = 1;
6212 if (a->status_cb == NULL)
6213 break;
6214 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6215 get_staged_status(ie), relpath, blob_id,
6216 new_staged_blob_id, NULL, dirfd, de_name);
6217 break;
6218 case GOT_STATUS_DELETE:
6219 if (staged_status == GOT_STATUS_DELETE)
6220 break;
6221 if (a->patch_cb) {
6222 int choice = GOT_PATCH_CHOICE_NONE;
6223 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6224 ie->path, NULL, 1, 1);
6225 if (err)
6226 break;
6227 if (choice == GOT_PATCH_CHOICE_NO)
6228 break;
6229 if (choice != GOT_PATCH_CHOICE_YES) {
6230 err = got_error(GOT_ERR_PATCH_CHOICE);
6231 break;
6234 stage = GOT_FILEIDX_STAGE_DELETE;
6235 got_fileindex_entry_stage_set(ie, stage);
6236 a->staged_something = 1;
6237 if (a->status_cb == NULL)
6238 break;
6239 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6240 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6241 de_name);
6242 break;
6243 case GOT_STATUS_NO_CHANGE:
6244 break;
6245 case GOT_STATUS_CONFLICT:
6246 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6247 break;
6248 case GOT_STATUS_NONEXISTENT:
6249 err = got_error_set_errno(ENOENT, relpath);
6250 break;
6251 default:
6252 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6253 break;
6256 if (path_content && unlink(path_content) == -1 && err == NULL)
6257 err = got_error_from_errno2("unlink", path_content);
6258 free(path_content);
6259 free(ondisk_path);
6260 free(new_staged_blob_id);
6261 return err;
6264 const struct got_error *
6265 got_worktree_stage(struct got_worktree *worktree,
6266 struct got_pathlist_head *paths,
6267 got_worktree_status_cb status_cb, void *status_arg,
6268 got_worktree_patch_cb patch_cb, void *patch_arg,
6269 struct got_repository *repo)
6271 const struct got_error *err = NULL, *sync_err, *unlockerr;
6272 struct got_pathlist_entry *pe;
6273 struct got_fileindex *fileindex = NULL;
6274 char *fileindex_path = NULL;
6275 struct got_reference *head_ref = NULL;
6276 struct got_object_id *head_commit_id = NULL;
6277 struct check_stage_ok_arg oka;
6278 struct stage_path_arg spa;
6280 err = lock_worktree(worktree, LOCK_EX);
6281 if (err)
6282 return err;
6284 err = got_ref_open(&head_ref, repo,
6285 got_worktree_get_head_ref_name(worktree), 0);
6286 if (err)
6287 goto done;
6288 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6289 if (err)
6290 goto done;
6291 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6292 if (err)
6293 goto done;
6295 /* Check pre-conditions before staging anything. */
6296 oka.head_commit_id = head_commit_id;
6297 oka.worktree = worktree;
6298 oka.fileindex = fileindex;
6299 oka.repo = repo;
6300 oka.have_changes = 0;
6301 TAILQ_FOREACH(pe, paths, entry) {
6302 err = worktree_status(worktree, pe->path, fileindex, repo,
6303 check_stage_ok, &oka, NULL, NULL, 0, 0);
6304 if (err)
6305 goto done;
6307 if (!oka.have_changes) {
6308 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6309 goto done;
6312 spa.worktree = worktree;
6313 spa.fileindex = fileindex;
6314 spa.repo = repo;
6315 spa.patch_cb = patch_cb;
6316 spa.patch_arg = patch_arg;
6317 spa.status_cb = status_cb;
6318 spa.status_arg = status_arg;
6319 spa.staged_something = 0;
6320 TAILQ_FOREACH(pe, paths, entry) {
6321 err = worktree_status(worktree, pe->path, fileindex, repo,
6322 stage_path, &spa, NULL, NULL, 0, 0);
6323 if (err)
6324 goto done;
6326 if (!spa.staged_something) {
6327 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6328 goto done;
6331 sync_err = sync_fileindex(fileindex, fileindex_path);
6332 if (sync_err && err == NULL)
6333 err = sync_err;
6334 done:
6335 if (head_ref)
6336 got_ref_close(head_ref);
6337 free(head_commit_id);
6338 free(fileindex_path);
6339 if (fileindex)
6340 got_fileindex_free(fileindex);
6341 unlockerr = lock_worktree(worktree, LOCK_SH);
6342 if (unlockerr && err == NULL)
6343 err = unlockerr;
6344 return err;
6347 struct unstage_path_arg {
6348 struct got_worktree *worktree;
6349 struct got_fileindex *fileindex;
6350 struct got_repository *repo;
6351 got_worktree_checkout_cb progress_cb;
6352 void *progress_arg;
6353 got_worktree_patch_cb patch_cb;
6354 void *patch_arg;
6357 static const struct got_error *
6358 create_unstaged_content(char **path_unstaged_content,
6359 char **path_new_staged_content, struct got_object_id *blob_id,
6360 struct got_object_id *staged_blob_id, const char *relpath,
6361 struct got_repository *repo,
6362 got_worktree_patch_cb patch_cb, void *patch_arg)
6364 const struct got_error *err;
6365 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6366 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6367 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6368 struct stat sb1, sb2;
6369 struct got_diff_changes *changes = NULL;
6370 struct got_diff_state *ds = NULL;
6371 struct got_diff_args *args = NULL;
6372 struct got_diff_change *change;
6373 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6374 int have_content = 0, have_rejected_content = 0;
6376 *path_unstaged_content = NULL;
6377 *path_new_staged_content = NULL;
6379 err = got_object_id_str(&label1, blob_id);
6380 if (err)
6381 return err;
6382 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6383 if (err)
6384 goto done;
6386 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6387 if (err)
6388 goto done;
6390 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6391 if (err)
6392 goto done;
6394 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6395 if (err)
6396 goto done;
6398 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6399 if (err)
6400 goto done;
6402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6403 if (err)
6404 goto done;
6406 if (stat(path1, &sb1) == -1) {
6407 err = got_error_from_errno2("stat", path1);
6408 goto done;
6411 if (stat(path2, &sb2) == -1) {
6412 err = got_error_from_errno2("stat", path2);
6413 goto done;
6416 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6417 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6418 if (err)
6419 goto done;
6421 err = got_opentemp_named(path_unstaged_content, &outfile,
6422 "got-unstaged-content");
6423 if (err)
6424 goto done;
6425 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6426 "got-new-staged-content");
6427 if (err)
6428 goto done;
6430 if (fseek(f1, 0L, SEEK_SET) == -1) {
6431 err = got_ferror(f1, GOT_ERR_IO);
6432 goto done;
6434 if (fseek(f2, 0L, SEEK_SET) == -1) {
6435 err = got_ferror(f2, GOT_ERR_IO);
6436 goto done;
6438 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6439 int choice;
6440 err = apply_or_reject_change(&choice, change, ++n,
6441 changes->nchanges, ds, args, diff_flags, relpath,
6442 f1, f2, &line_cur1, &line_cur2,
6443 outfile, rejectfile, patch_cb, patch_arg);
6444 if (err)
6445 goto done;
6446 if (choice == GOT_PATCH_CHOICE_YES)
6447 have_content = 1;
6448 else
6449 have_rejected_content = 1;
6450 if (choice == GOT_PATCH_CHOICE_QUIT)
6451 break;
6453 if (have_content || have_rejected_content)
6454 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6455 outfile, rejectfile);
6456 done:
6457 free(label1);
6458 if (blob)
6459 got_object_blob_close(blob);
6460 if (staged_blob)
6461 got_object_blob_close(staged_blob);
6462 if (f1 && fclose(f1) == EOF && err == NULL)
6463 err = got_error_from_errno2("fclose", path1);
6464 if (f2 && fclose(f2) == EOF && err == NULL)
6465 err = got_error_from_errno2("fclose", path2);
6466 if (outfile && fclose(outfile) == EOF && err == NULL)
6467 err = got_error_from_errno2("fclose", *path_unstaged_content);
6468 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6469 err = got_error_from_errno2("fclose", *path_new_staged_content);
6470 if (path1 && unlink(path1) == -1 && err == NULL)
6471 err = got_error_from_errno2("unlink", path1);
6472 if (path2 && unlink(path2) == -1 && err == NULL)
6473 err = got_error_from_errno2("unlink", path2);
6474 if (err || !have_content) {
6475 if (*path_unstaged_content &&
6476 unlink(*path_unstaged_content) == -1 && err == NULL)
6477 err = got_error_from_errno2("unlink",
6478 *path_unstaged_content);
6479 free(*path_unstaged_content);
6480 *path_unstaged_content = NULL;
6482 if (err || !have_rejected_content) {
6483 if (*path_new_staged_content &&
6484 unlink(*path_new_staged_content) == -1 && err == NULL)
6485 err = got_error_from_errno2("unlink",
6486 *path_new_staged_content);
6487 free(*path_new_staged_content);
6488 *path_new_staged_content = NULL;
6490 free(args);
6491 if (ds) {
6492 got_diff_state_free(ds);
6493 free(ds);
6495 if (changes)
6496 got_diff_free_changes(changes);
6497 free(path1);
6498 free(path2);
6499 return err;
6502 static const struct got_error *
6503 unstage_path(void *arg, unsigned char status,
6504 unsigned char staged_status, const char *relpath,
6505 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6506 struct got_object_id *commit_id, int dirfd, const char *de_name)
6508 const struct got_error *err = NULL;
6509 struct unstage_path_arg *a = arg;
6510 struct got_fileindex_entry *ie;
6511 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6512 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6513 char *path_new_staged_content = NULL;
6514 char *id_str = NULL, *label_orig = NULL;
6515 int local_changes_subsumed;
6516 struct stat sb;
6518 if (staged_status != GOT_STATUS_ADD &&
6519 staged_status != GOT_STATUS_MODIFY &&
6520 staged_status != GOT_STATUS_DELETE)
6521 return NULL;
6523 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6524 if (ie == NULL)
6525 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6527 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6528 == -1)
6529 return got_error_from_errno("asprintf");
6531 err = got_object_id_str(&id_str,
6532 commit_id ? commit_id : a->worktree->base_commit_id);
6533 if (err)
6534 goto done;
6535 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6536 id_str) == -1) {
6537 err = got_error_from_errno("asprintf");
6538 goto done;
6541 switch (staged_status) {
6542 case GOT_STATUS_MODIFY:
6543 err = got_object_open_as_blob(&blob_base, a->repo,
6544 blob_id, 8192);
6545 if (err)
6546 break;
6547 /* fall through */
6548 case GOT_STATUS_ADD:
6549 if (a->patch_cb) {
6550 if (staged_status == GOT_STATUS_ADD) {
6551 int choice = GOT_PATCH_CHOICE_NONE;
6552 err = (*a->patch_cb)(&choice, a->patch_arg,
6553 staged_status, ie->path, NULL, 1, 1);
6554 if (err)
6555 break;
6556 if (choice != GOT_PATCH_CHOICE_YES)
6557 break;
6558 } else {
6559 err = create_unstaged_content(
6560 &path_unstaged_content,
6561 &path_new_staged_content, blob_id,
6562 staged_blob_id, ie->path, a->repo,
6563 a->patch_cb, a->patch_arg);
6564 if (err || path_unstaged_content == NULL)
6565 break;
6566 if (path_new_staged_content) {
6567 err = got_object_blob_create(
6568 &staged_blob_id,
6569 path_new_staged_content,
6570 a->repo);
6571 if (err)
6572 break;
6573 memcpy(ie->staged_blob_sha1,
6574 staged_blob_id->sha1,
6575 SHA1_DIGEST_LENGTH);
6577 err = merge_file(&local_changes_subsumed,
6578 a->worktree, blob_base, ondisk_path,
6579 relpath, got_fileindex_perms_to_st(ie),
6580 path_unstaged_content, label_orig,
6581 "unstaged", a->repo, a->progress_cb,
6582 a->progress_arg);
6583 if (err == NULL &&
6584 path_new_staged_content == NULL)
6585 got_fileindex_entry_stage_set(ie,
6586 GOT_FILEIDX_STAGE_NONE);
6587 break; /* Done with this file. */
6590 err = got_object_open_as_blob(&blob_staged, a->repo,
6591 staged_blob_id, 8192);
6592 if (err)
6593 break;
6594 err = merge_blob(&local_changes_subsumed, a->worktree,
6595 blob_base, ondisk_path, relpath,
6596 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6597 commit_id ? commit_id : a->worktree->base_commit_id,
6598 a->repo, a->progress_cb, a->progress_arg);
6599 if (err == NULL)
6600 got_fileindex_entry_stage_set(ie,
6601 GOT_FILEIDX_STAGE_NONE);
6602 break;
6603 case GOT_STATUS_DELETE:
6604 if (a->patch_cb) {
6605 int choice = GOT_PATCH_CHOICE_NONE;
6606 err = (*a->patch_cb)(&choice, a->patch_arg,
6607 staged_status, ie->path, NULL, 1, 1);
6608 if (err)
6609 break;
6610 if (choice == GOT_PATCH_CHOICE_NO)
6611 break;
6612 if (choice != GOT_PATCH_CHOICE_YES) {
6613 err = got_error(GOT_ERR_PATCH_CHOICE);
6614 break;
6617 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6618 err = get_file_status(&status, &sb, ie, ondisk_path,
6619 dirfd, de_name, a->repo);
6620 if (err)
6621 break;
6622 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6623 break;
6625 done:
6626 free(ondisk_path);
6627 if (path_unstaged_content &&
6628 unlink(path_unstaged_content) == -1 && err == NULL)
6629 err = got_error_from_errno2("unlink", path_unstaged_content);
6630 if (path_new_staged_content &&
6631 unlink(path_new_staged_content) == -1 && err == NULL)
6632 err = got_error_from_errno2("unlink", path_new_staged_content);
6633 free(path_unstaged_content);
6634 free(path_new_staged_content);
6635 if (blob_base)
6636 got_object_blob_close(blob_base);
6637 if (blob_staged)
6638 got_object_blob_close(blob_staged);
6639 free(id_str);
6640 free(label_orig);
6641 return err;
6644 const struct got_error *
6645 got_worktree_unstage(struct got_worktree *worktree,
6646 struct got_pathlist_head *paths,
6647 got_worktree_checkout_cb progress_cb, void *progress_arg,
6648 got_worktree_patch_cb patch_cb, void *patch_arg,
6649 struct got_repository *repo)
6651 const struct got_error *err = NULL, *sync_err, *unlockerr;
6652 struct got_pathlist_entry *pe;
6653 struct got_fileindex *fileindex = NULL;
6654 char *fileindex_path = NULL;
6655 struct unstage_path_arg upa;
6657 err = lock_worktree(worktree, LOCK_EX);
6658 if (err)
6659 return err;
6661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6662 if (err)
6663 goto done;
6665 upa.worktree = worktree;
6666 upa.fileindex = fileindex;
6667 upa.repo = repo;
6668 upa.progress_cb = progress_cb;
6669 upa.progress_arg = progress_arg;
6670 upa.patch_cb = patch_cb;
6671 upa.patch_arg = patch_arg;
6672 TAILQ_FOREACH(pe, paths, entry) {
6673 err = worktree_status(worktree, pe->path, fileindex, repo,
6674 unstage_path, &upa, NULL, NULL, 0, 0);
6675 if (err)
6676 goto done;
6679 sync_err = sync_fileindex(fileindex, fileindex_path);
6680 if (sync_err && err == NULL)
6681 err = sync_err;
6682 done:
6683 free(fileindex_path);
6684 if (fileindex)
6685 got_fileindex_free(fileindex);
6686 unlockerr = lock_worktree(worktree, LOCK_SH);
6687 if (unlockerr && err == NULL)
6688 err = unlockerr;
6689 return err;