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_path(abspath, 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 create_fileindex_entry(struct got_fileindex *fileindex,
899 struct got_object_id *base_commit_id, const char *ondisk_path,
900 const char *path, struct got_object_id *blob_id)
902 const struct got_error *err = NULL;
903 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, path);
906 if (err)
907 return err;
909 err = got_fileindex_entry_update(new_ie, ondisk_path,
910 blob_id->sha1, base_commit_id->sha1, 1);
911 if (err)
912 goto done;
914 err = got_fileindex_entry_add(fileindex, new_ie);
915 done:
916 if (err)
917 got_fileindex_entry_free(new_ie);
918 return err;
921 static mode_t
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
926 if (executable) {
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
929 xbits |= S_IXGRP;
930 if (st_mode & S_IROTH)
931 xbits |= S_IXOTH;
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 static const struct got_error *
939 install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 const char *path, mode_t te_mode, mode_t st_mode,
941 struct got_blob_object *blob, int restoring_missing_file,
942 int reverting_versioned_file, struct got_repository *repo,
943 got_worktree_checkout_cb progress_cb, void *progress_arg)
945 const struct got_error *err = NULL;
946 int fd = -1;
947 size_t len, hdrlen;
948 int update = 0;
949 char *tmppath = NULL;
951 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 GOT_DEFAULT_FILE_MODE);
953 if (fd == -1) {
954 if (errno == ENOENT) {
955 char *parent = dirname(path);
956 if (parent == NULL)
957 return got_error_from_errno2("dirname", path);
958 err = add_dir_on_disk(worktree, parent);
959 if (err)
960 return err;
961 fd = open(ondisk_path,
962 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 GOT_DEFAULT_FILE_MODE);
964 if (fd == -1)
965 return got_error_from_errno2("open",
966 ondisk_path);
967 } else if (errno == EEXIST) {
968 if (!S_ISREG(st_mode)) {
969 /* TODO file is obstructed; do something */
970 err = got_error_path(ondisk_path,
971 GOT_ERR_FILE_OBSTRUCTED);
972 goto done;
973 } else {
974 err = got_opentemp_named_fd(&tmppath, &fd,
975 ondisk_path);
976 if (err)
977 goto done;
978 update = 1;
980 } else
981 return got_error_from_errno2("open", ondisk_path);
984 if (restoring_missing_file)
985 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
986 else if (reverting_versioned_file)
987 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
988 else
989 err = (*progress_cb)(progress_arg,
990 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
991 if (err)
992 goto done;
994 hdrlen = got_object_blob_get_hdrlen(blob);
995 do {
996 const uint8_t *buf = got_object_blob_get_read_buf(blob);
997 err = got_object_blob_read_block(&len, blob);
998 if (err)
999 break;
1000 if (len > 0) {
1001 /* Skip blob object header first time around. */
1002 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1003 if (outlen == -1) {
1004 err = got_error_from_errno("write");
1005 goto done;
1006 } else if (outlen != len - hdrlen) {
1007 err = got_error(GOT_ERR_IO);
1008 goto done;
1010 hdrlen = 0;
1012 } while (len != 0);
1014 if (fsync(fd) != 0) {
1015 err = got_error_from_errno("fsync");
1016 goto done;
1019 if (update) {
1020 if (rename(tmppath, ondisk_path) != 0) {
1021 err = got_error_from_errno3("rename", tmppath,
1022 ondisk_path);
1023 unlink(tmppath);
1024 goto done;
1028 if (chmod(ondisk_path,
1029 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1030 err = got_error_from_errno2("chmod", ondisk_path);
1031 goto done;
1034 done:
1035 if (fd != -1 && close(fd) != 0 && err == NULL)
1036 err = got_error_from_errno("close");
1037 free(tmppath);
1038 return err;
1041 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1042 static const struct got_error *
1043 get_modified_file_content_status(unsigned char *status, FILE *f)
1045 const struct got_error *err = NULL;
1046 const char *markers[3] = {
1047 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1048 GOT_DIFF_CONFLICT_MARKER_SEP,
1049 GOT_DIFF_CONFLICT_MARKER_END
1051 int i = 0;
1052 char *line;
1053 size_t len;
1054 const char delim[3] = {'\0', '\0', '\0'};
1056 while (*status == GOT_STATUS_MODIFY) {
1057 line = fparseln(f, &len, NULL, delim, 0);
1058 if (line == NULL) {
1059 if (feof(f))
1060 break;
1061 err = got_ferror(f, GOT_ERR_IO);
1062 break;
1065 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1066 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1067 == 0)
1068 *status = GOT_STATUS_CONFLICT;
1069 else
1070 i++;
1074 return err;
1077 static int
1078 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1080 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1081 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1084 static int
1085 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1087 return !(ie->ctime_sec == sb->st_ctime &&
1088 ie->ctime_nsec == sb->st_ctimensec &&
1089 ie->mtime_sec == sb->st_mtime &&
1090 ie->mtime_nsec == sb->st_mtimensec &&
1091 ie->size == (sb->st_size & 0xffffffff) &&
1092 !xbit_differs(ie, sb->st_mode));
1095 static unsigned char
1096 get_staged_status(struct got_fileindex_entry *ie)
1098 switch (got_fileindex_entry_stage_get(ie)) {
1099 case GOT_FILEIDX_STAGE_ADD:
1100 return GOT_STATUS_ADD;
1101 case GOT_FILEIDX_STAGE_DELETE:
1102 return GOT_STATUS_DELETE;
1103 case GOT_FILEIDX_STAGE_MODIFY:
1104 return GOT_STATUS_MODIFY;
1105 default:
1106 return GOT_STATUS_NO_CHANGE;
1110 static const struct got_error *
1111 get_file_status(unsigned char *status, struct stat *sb,
1112 struct got_fileindex_entry *ie, const char *abspath,
1113 int dirfd, const char *de_name, struct got_repository *repo)
1115 const struct got_error *err = NULL;
1116 struct got_object_id id;
1117 size_t hdrlen;
1118 int fd = -1;
1119 FILE *f = NULL;
1120 uint8_t fbuf[8192];
1121 struct got_blob_object *blob = NULL;
1122 size_t flen, blen;
1123 unsigned char staged_status = get_staged_status(ie);
1125 *status = GOT_STATUS_NO_CHANGE;
1128 * Whenever the caller provides a directory descriptor and a
1129 * directory entry name for the file, use them! This prevents
1130 * race conditions if filesystem paths change beneath our feet.
1132 if (dirfd != -1) {
1133 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1134 if (errno == ENOENT) {
1135 if (got_fileindex_entry_has_file_on_disk(ie))
1136 *status = GOT_STATUS_MISSING;
1137 else
1138 *status = GOT_STATUS_DELETE;
1139 goto done;
1141 err = got_error_from_errno2("fstatat", abspath);
1142 goto done;
1144 } else {
1145 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1146 if (fd == -1 && errno != ENOENT)
1147 return got_error_from_errno2("open", abspath);
1148 if (fd == -1 || fstat(fd, sb) == -1) {
1149 if (errno == ENOENT) {
1150 if (got_fileindex_entry_has_file_on_disk(ie))
1151 *status = GOT_STATUS_MISSING;
1152 else
1153 *status = GOT_STATUS_DELETE;
1154 goto done;
1156 err = got_error_from_errno2("fstat", abspath);
1157 goto done;
1161 if (!S_ISREG(sb->st_mode)) {
1162 *status = GOT_STATUS_OBSTRUCTED;
1163 goto done;
1166 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1167 *status = GOT_STATUS_DELETE;
1168 goto done;
1169 } else if (!got_fileindex_entry_has_blob(ie) &&
1170 staged_status != GOT_STATUS_ADD) {
1171 *status = GOT_STATUS_ADD;
1172 goto done;
1175 if (!stat_info_differs(ie, sb))
1176 goto done;
1178 if (staged_status == GOT_STATUS_MODIFY ||
1179 staged_status == GOT_STATUS_ADD)
1180 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1181 else
1182 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1184 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1185 if (err)
1186 goto done;
1188 if (dirfd != -1) {
1189 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1190 if (fd == -1) {
1191 err = got_error_from_errno2("openat", abspath);
1192 goto done;
1196 f = fdopen(fd, "r");
1197 if (f == NULL) {
1198 err = got_error_from_errno2("fdopen", abspath);
1199 goto done;
1201 fd = -1;
1202 hdrlen = got_object_blob_get_hdrlen(blob);
1203 for (;;) {
1204 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1205 err = got_object_blob_read_block(&blen, blob);
1206 if (err)
1207 goto done;
1208 /* Skip length of blob object header first time around. */
1209 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1210 if (flen == 0 && ferror(f)) {
1211 err = got_error_from_errno("fread");
1212 goto done;
1214 if (blen == 0) {
1215 if (flen != 0)
1216 *status = GOT_STATUS_MODIFY;
1217 break;
1218 } else if (flen == 0) {
1219 if (blen != 0)
1220 *status = GOT_STATUS_MODIFY;
1221 break;
1222 } else if (blen - hdrlen == flen) {
1223 /* Skip blob object header first time around. */
1224 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1225 *status = GOT_STATUS_MODIFY;
1226 break;
1228 } else {
1229 *status = GOT_STATUS_MODIFY;
1230 break;
1232 hdrlen = 0;
1235 if (*status == GOT_STATUS_MODIFY) {
1236 rewind(f);
1237 err = get_modified_file_content_status(status, f);
1238 } else if (xbit_differs(ie, sb->st_mode))
1239 *status = GOT_STATUS_MODE_CHANGE;
1240 done:
1241 if (blob)
1242 got_object_blob_close(blob);
1243 if (f != NULL && fclose(f) == EOF && err == NULL)
1244 err = got_error_from_errno2("fclose", abspath);
1245 if (fd != -1 && close(fd) == -1 && err == NULL)
1246 err = got_error_from_errno2("close", abspath);
1247 return err;
1251 * Update timestamps in the file index if a file is unmodified and
1252 * we had to run a full content comparison to find out.
1254 static const struct got_error *
1255 sync_timestamps(char *ondisk_path, unsigned char status,
1256 struct got_fileindex_entry *ie, struct stat *sb)
1258 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1259 return got_fileindex_entry_update(ie, ondisk_path,
1260 ie->blob_sha1, ie->commit_sha1, 1);
1262 return NULL;
1265 static const struct got_error *
1266 update_blob(struct got_worktree *worktree,
1267 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1268 struct got_tree_entry *te, const char *path,
1269 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1270 void *progress_arg)
1272 const struct got_error *err = NULL;
1273 struct got_blob_object *blob = NULL;
1274 char *ondisk_path;
1275 unsigned char status = GOT_STATUS_NO_CHANGE;
1276 struct stat sb;
1278 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1279 return got_error_from_errno("asprintf");
1281 if (ie) {
1282 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1283 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1284 goto done;
1286 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1287 repo);
1288 if (err)
1289 goto done;
1290 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1291 sb.st_mode = got_fileindex_perms_to_st(ie);
1292 } else
1293 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1295 if (status == GOT_STATUS_OBSTRUCTED) {
1296 err = (*progress_cb)(progress_arg, status, path);
1297 goto done;
1299 if (status == GOT_STATUS_CONFLICT) {
1300 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1301 path);
1302 goto done;
1305 if (ie && status != GOT_STATUS_MISSING &&
1306 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1307 if (got_fileindex_entry_has_commit(ie) &&
1308 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1309 SHA1_DIGEST_LENGTH) == 0) {
1310 err = sync_timestamps(ondisk_path, status, ie, &sb);
1311 if (err)
1312 goto done;
1313 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1314 path);
1315 goto done;
1317 if (got_fileindex_entry_has_blob(ie) &&
1318 memcmp(ie->blob_sha1, te->id.sha1,
1319 SHA1_DIGEST_LENGTH) == 0) {
1320 err = sync_timestamps(ondisk_path, status, ie, &sb);
1321 goto done;
1325 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1326 if (err)
1327 goto done;
1329 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1330 int update_timestamps;
1331 struct got_blob_object *blob2 = NULL;
1332 char *label_orig = NULL;
1333 if (got_fileindex_entry_has_blob(ie)) {
1334 struct got_object_id id2;
1335 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1336 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1337 if (err)
1338 goto done;
1340 if (got_fileindex_entry_has_commit(ie)) {
1341 char id_str[SHA1_DIGEST_STRING_LENGTH];
1342 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1343 sizeof(id_str)) == NULL) {
1344 err = got_error_path(id_str,
1345 GOT_ERR_BAD_OBJ_ID_STR);
1346 goto done;
1348 if (asprintf(&label_orig, "%s: commit %s",
1349 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1350 err = got_error_from_errno("asprintf");
1351 goto done;
1354 err = merge_blob(&update_timestamps, worktree, blob2,
1355 ondisk_path, path, sb.st_mode, label_orig, blob,
1356 worktree->base_commit_id, repo,
1357 progress_cb, progress_arg);
1358 free(label_orig);
1359 if (blob2)
1360 got_object_blob_close(blob2);
1361 if (err)
1362 goto done;
1364 * Do not update timestamps of files with local changes.
1365 * Otherwise, a future status walk would treat them as
1366 * unmodified files again.
1368 err = got_fileindex_entry_update(ie, ondisk_path,
1369 blob->id.sha1, worktree->base_commit_id->sha1,
1370 update_timestamps);
1371 } else if (status == GOT_STATUS_MODE_CHANGE) {
1372 err = got_fileindex_entry_update(ie, ondisk_path,
1373 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1374 } else if (status == GOT_STATUS_DELETE) {
1375 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1376 if (err)
1377 goto done;
1378 err = got_fileindex_entry_update(ie, ondisk_path,
1379 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1380 if (err)
1381 goto done;
1382 } else {
1383 err = install_blob(worktree, ondisk_path, path, te->mode,
1384 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1385 repo, progress_cb, progress_arg);
1386 if (err)
1387 goto done;
1388 if (ie) {
1389 err = got_fileindex_entry_update(ie, ondisk_path,
1390 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1391 } else {
1392 err = create_fileindex_entry(fileindex,
1393 worktree->base_commit_id, ondisk_path, path,
1394 &blob->id);
1396 if (err)
1397 goto done;
1399 got_object_blob_close(blob);
1400 done:
1401 free(ondisk_path);
1402 return err;
1405 static const struct got_error *
1406 remove_ondisk_file(const char *root_path, const char *path)
1408 const struct got_error *err = NULL;
1409 char *ondisk_path = NULL;
1411 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1412 return got_error_from_errno("asprintf");
1414 if (unlink(ondisk_path) == -1) {
1415 if (errno != ENOENT)
1416 err = got_error_from_errno2("unlink", ondisk_path);
1417 } else {
1418 char *parent = dirname(ondisk_path);
1419 while (parent && strcmp(parent, root_path) != 0) {
1420 if (rmdir(parent) == -1) {
1421 if (errno != ENOTEMPTY)
1422 err = got_error_from_errno2("rmdir",
1423 parent);
1424 break;
1426 parent = dirname(parent);
1429 free(ondisk_path);
1430 return err;
1433 static const struct got_error *
1434 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1435 struct got_fileindex_entry *ie, struct got_repository *repo,
1436 got_worktree_checkout_cb progress_cb, void *progress_arg)
1438 const struct got_error *err = NULL;
1439 unsigned char status;
1440 struct stat sb;
1441 char *ondisk_path;
1443 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1444 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1446 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1447 == -1)
1448 return got_error_from_errno("asprintf");
1450 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1451 if (err)
1452 goto done;
1454 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1455 status == GOT_STATUS_ADD) {
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1457 if (err)
1458 goto done;
1460 * Preserve the working file and change the deleted blob's
1461 * entry into a schedule-add entry.
1463 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1464 0);
1465 } else {
1466 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1467 if (err)
1468 goto done;
1469 if (status == GOT_STATUS_NO_CHANGE) {
1470 err = remove_ondisk_file(worktree->root_path, ie->path);
1471 if (err)
1472 goto done;
1474 got_fileindex_entry_remove(fileindex, ie);
1476 done:
1477 free(ondisk_path);
1478 return err;
1481 struct diff_cb_arg {
1482 struct got_fileindex *fileindex;
1483 struct got_worktree *worktree;
1484 struct got_repository *repo;
1485 got_worktree_checkout_cb progress_cb;
1486 void *progress_arg;
1487 got_cancel_cb cancel_cb;
1488 void *cancel_arg;
1491 static const struct got_error *
1492 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1493 struct got_tree_entry *te, const char *parent_path)
1495 struct diff_cb_arg *a = arg;
1497 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1498 return got_error(GOT_ERR_CANCELLED);
1500 return update_blob(a->worktree, a->fileindex, ie, te,
1501 ie->path, a->repo, a->progress_cb, a->progress_arg);
1504 static const struct got_error *
1505 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1507 struct diff_cb_arg *a = arg;
1509 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1510 return got_error(GOT_ERR_CANCELLED);
1512 return delete_blob(a->worktree, a->fileindex, ie,
1513 a->repo, a->progress_cb, a->progress_arg);
1516 static const struct got_error *
1517 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1519 struct diff_cb_arg *a = arg;
1520 const struct got_error *err;
1521 char *path;
1523 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1524 return got_error(GOT_ERR_CANCELLED);
1526 if (got_object_tree_entry_is_submodule(te))
1527 return NULL;
1529 if (asprintf(&path, "%s%s%s", parent_path,
1530 parent_path[0] ? "/" : "", te->name)
1531 == -1)
1532 return got_error_from_errno("asprintf");
1534 if (S_ISDIR(te->mode))
1535 err = add_dir_on_disk(a->worktree, path);
1536 else
1537 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1538 a->repo, a->progress_cb, a->progress_arg);
1540 free(path);
1541 return err;
1544 static const struct got_error *
1545 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1547 const struct got_error *err = NULL;
1548 char *uuidstr = NULL;
1549 uint32_t uuid_status;
1551 *refname = NULL;
1553 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1554 if (uuid_status != uuid_s_ok)
1555 return got_error_uuid(uuid_status, "uuid_to_string");
1557 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1558 == -1) {
1559 err = got_error_from_errno("asprintf");
1560 *refname = NULL;
1562 free(uuidstr);
1563 return err;
1566 const struct got_error *
1567 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1569 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1572 static const struct got_error *
1573 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1575 return get_ref_name(refname, worktree,
1576 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1579 static const struct got_error *
1580 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1582 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1585 static const struct got_error *
1586 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1588 return get_ref_name(refname, worktree,
1589 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1592 static const struct got_error *
1593 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1595 return get_ref_name(refname, worktree,
1596 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1599 static const struct got_error *
1600 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1602 return get_ref_name(refname, worktree,
1603 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1606 static const struct got_error *
1607 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1609 return get_ref_name(refname, worktree,
1610 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1613 static const struct got_error *
1614 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1616 return get_ref_name(refname, worktree,
1617 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1620 static const struct got_error *
1621 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1623 return get_ref_name(refname, worktree,
1624 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1627 const struct got_error *
1628 got_worktree_get_histedit_script_path(char **path,
1629 struct got_worktree *worktree)
1631 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1632 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1633 *path = NULL;
1634 return got_error_from_errno("asprintf");
1636 return NULL;
1640 * Prevent Git's garbage collector from deleting our base commit by
1641 * setting a reference to our base commit's ID.
1643 static const struct got_error *
1644 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1646 const struct got_error *err = NULL;
1647 struct got_reference *ref = NULL;
1648 char *refname;
1650 err = got_worktree_get_base_ref_name(&refname, worktree);
1651 if (err)
1652 return err;
1654 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1655 if (err)
1656 goto done;
1658 err = got_ref_write(ref, repo);
1659 done:
1660 free(refname);
1661 if (ref)
1662 got_ref_close(ref);
1663 return err;
1666 static const struct got_error *
1667 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1669 const struct got_error *err = NULL;
1671 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1672 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1673 err = got_error_from_errno("asprintf");
1674 *fileindex_path = NULL;
1676 return err;
1680 static const struct got_error *
1681 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1682 struct got_worktree *worktree)
1684 const struct got_error *err = NULL;
1685 FILE *index = NULL;
1687 *fileindex_path = NULL;
1688 *fileindex = got_fileindex_alloc();
1689 if (*fileindex == NULL)
1690 return got_error_from_errno("got_fileindex_alloc");
1692 err = get_fileindex_path(fileindex_path, worktree);
1693 if (err)
1694 goto done;
1696 index = fopen(*fileindex_path, "rb");
1697 if (index == NULL) {
1698 if (errno != ENOENT)
1699 err = got_error_from_errno2("fopen", *fileindex_path);
1700 } else {
1701 err = got_fileindex_read(*fileindex, index);
1702 if (fclose(index) != 0 && err == NULL)
1703 err = got_error_from_errno("fclose");
1705 done:
1706 if (err) {
1707 free(*fileindex_path);
1708 *fileindex_path = NULL;
1709 got_fileindex_free(*fileindex);
1710 *fileindex = NULL;
1712 return err;
1715 struct bump_base_commit_id_arg {
1716 struct got_object_id *base_commit_id;
1717 const char *path;
1718 size_t path_len;
1719 const char *entry_name;
1720 got_worktree_checkout_cb progress_cb;
1721 void *progress_arg;
1724 /* Bump base commit ID of all files within an updated part of the work tree. */
1725 static const struct got_error *
1726 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1728 const struct got_error *err;
1729 struct bump_base_commit_id_arg *a = arg;
1731 if (a->entry_name) {
1732 if (strcmp(ie->path, a->path) != 0)
1733 return NULL;
1734 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1735 return NULL;
1737 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1738 SHA1_DIGEST_LENGTH) == 0)
1739 return NULL;
1741 if (a->progress_cb) {
1742 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1743 ie->path);
1744 if (err)
1745 return err;
1747 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1748 return NULL;
1751 static const struct got_error *
1752 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1754 const struct got_error *err = NULL;
1755 char *new_fileindex_path = NULL;
1756 FILE *new_index = NULL;
1757 struct timespec timeout;
1759 err = got_opentemp_named(&new_fileindex_path, &new_index,
1760 fileindex_path);
1761 if (err)
1762 goto done;
1764 err = got_fileindex_write(fileindex, new_index);
1765 if (err)
1766 goto done;
1768 if (rename(new_fileindex_path, fileindex_path) != 0) {
1769 err = got_error_from_errno3("rename", new_fileindex_path,
1770 fileindex_path);
1771 unlink(new_fileindex_path);
1775 * Sleep for a short amount of time to ensure that files modified after
1776 * this program exits have a different time stamp from the one which
1777 * was recorded in the file index.
1779 timeout.tv_sec = 0;
1780 timeout.tv_nsec = 1;
1781 nanosleep(&timeout, NULL);
1782 done:
1783 if (new_index)
1784 fclose(new_index);
1785 free(new_fileindex_path);
1786 return err;
1789 static const struct got_error *
1790 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1791 struct got_object_id **tree_id, const char *wt_relpath,
1792 struct got_worktree *worktree, struct got_repository *repo)
1794 const struct got_error *err = NULL;
1795 struct got_object_id *id = NULL;
1796 char *in_repo_path = NULL;
1797 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1799 *entry_type = GOT_OBJ_TYPE_ANY;
1800 *tree_relpath = NULL;
1801 *tree_id = NULL;
1803 if (wt_relpath[0] == '\0') {
1804 /* Check out all files within the work tree. */
1805 *entry_type = GOT_OBJ_TYPE_TREE;
1806 *tree_relpath = strdup("");
1807 if (*tree_relpath == NULL) {
1808 err = got_error_from_errno("strdup");
1809 goto done;
1811 err = got_object_id_by_path(tree_id, repo,
1812 worktree->base_commit_id, worktree->path_prefix);
1813 if (err)
1814 goto done;
1815 return NULL;
1818 /* Check out a subset of files in the work tree. */
1820 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1821 is_root_wt ? "" : "/", wt_relpath) == -1) {
1822 err = got_error_from_errno("asprintf");
1823 goto done;
1826 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1827 in_repo_path);
1828 if (err)
1829 goto done;
1831 free(in_repo_path);
1832 in_repo_path = NULL;
1834 err = got_object_get_type(entry_type, repo, id);
1835 if (err)
1836 goto done;
1838 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1839 /* Check out a single file. */
1840 if (strchr(wt_relpath, '/') == NULL) {
1841 /* Check out a single file in work tree's root dir. */
1842 in_repo_path = strdup(worktree->path_prefix);
1843 if (in_repo_path == NULL) {
1844 err = got_error_from_errno("strdup");
1845 goto done;
1847 *tree_relpath = strdup("");
1848 if (*tree_relpath == NULL) {
1849 err = got_error_from_errno("strdup");
1850 goto done;
1852 } else {
1853 /* Check out a single file in a subdirectory. */
1854 err = got_path_dirname(tree_relpath, wt_relpath);
1855 if (err)
1856 return err;
1857 if (asprintf(&in_repo_path, "%s%s%s",
1858 worktree->path_prefix, is_root_wt ? "" : "/",
1859 *tree_relpath) == -1) {
1860 err = got_error_from_errno("asprintf");
1861 goto done;
1864 err = got_object_id_by_path(tree_id, repo,
1865 worktree->base_commit_id, in_repo_path);
1866 } else {
1867 /* Check out all files within a subdirectory. */
1868 *tree_id = got_object_id_dup(id);
1869 if (*tree_id == NULL) {
1870 err = got_error_from_errno("got_object_id_dup");
1871 goto done;
1873 *tree_relpath = strdup(wt_relpath);
1874 if (*tree_relpath == NULL) {
1875 err = got_error_from_errno("strdup");
1876 goto done;
1879 done:
1880 free(id);
1881 free(in_repo_path);
1882 if (err) {
1883 *entry_type = GOT_OBJ_TYPE_ANY;
1884 free(*tree_relpath);
1885 *tree_relpath = NULL;
1886 free(*tree_id);
1887 *tree_id = NULL;
1889 return err;
1892 static const struct got_error *
1893 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1894 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1895 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1896 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1898 const struct got_error *err = NULL;
1899 struct got_commit_object *commit = NULL;
1900 struct got_tree_object *tree = NULL;
1901 struct got_fileindex_diff_tree_cb diff_cb;
1902 struct diff_cb_arg arg;
1904 err = ref_base_commit(worktree, repo);
1905 if (err) {
1906 if (!(err->code == GOT_ERR_ERRNO &&
1907 (errno == EACCES || errno == EROFS)))
1908 goto done;
1909 err = (*progress_cb)(progress_arg,
1910 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1911 if (err)
1912 return err;
1915 err = got_object_open_as_commit(&commit, repo,
1916 worktree->base_commit_id);
1917 if (err)
1918 goto done;
1920 err = got_object_open_as_tree(&tree, repo, tree_id);
1921 if (err)
1922 goto done;
1924 if (entry_name &&
1925 got_object_tree_find_entry(tree, entry_name) == NULL) {
1926 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1927 goto done;
1930 diff_cb.diff_old_new = diff_old_new;
1931 diff_cb.diff_old = diff_old;
1932 diff_cb.diff_new = diff_new;
1933 arg.fileindex = fileindex;
1934 arg.worktree = worktree;
1935 arg.repo = repo;
1936 arg.progress_cb = progress_cb;
1937 arg.progress_arg = progress_arg;
1938 arg.cancel_cb = cancel_cb;
1939 arg.cancel_arg = cancel_arg;
1940 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1941 entry_name, repo, &diff_cb, &arg);
1942 done:
1943 if (tree)
1944 got_object_tree_close(tree);
1945 if (commit)
1946 got_object_commit_close(commit);
1947 return err;
1950 const struct got_error *
1951 got_worktree_checkout_files(struct got_worktree *worktree,
1952 struct got_pathlist_head *paths, struct got_repository *repo,
1953 got_worktree_checkout_cb progress_cb, void *progress_arg,
1954 got_cancel_cb cancel_cb, void *cancel_arg)
1956 const struct got_error *err = NULL, *sync_err, *unlockerr;
1957 struct got_commit_object *commit = NULL;
1958 struct got_tree_object *tree = NULL;
1959 struct got_fileindex *fileindex = NULL;
1960 char *fileindex_path = NULL;
1961 struct got_pathlist_entry *pe;
1962 struct tree_path_data {
1963 SIMPLEQ_ENTRY(tree_path_data) entry;
1964 struct got_object_id *tree_id;
1965 int entry_type;
1966 char *relpath;
1967 char *entry_name;
1968 } *tpd = NULL;
1969 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1971 SIMPLEQ_INIT(&tree_paths);
1973 err = lock_worktree(worktree, LOCK_EX);
1974 if (err)
1975 return err;
1977 /* Map all specified paths to in-repository trees. */
1978 TAILQ_FOREACH(pe, paths, entry) {
1979 tpd = malloc(sizeof(*tpd));
1980 if (tpd == NULL) {
1981 err = got_error_from_errno("malloc");
1982 goto done;
1985 err = find_tree_entry_for_checkout(&tpd->entry_type,
1986 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1987 if (err) {
1988 free(tpd);
1989 goto done;
1992 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1993 err = got_path_basename(&tpd->entry_name, pe->path);
1994 if (err) {
1995 free(tpd->relpath);
1996 free(tpd->tree_id);
1997 free(tpd);
1998 goto done;
2000 } else
2001 tpd->entry_name = NULL;
2003 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2007 * Read the file index.
2008 * Checking out files is supposed to be an idempotent operation.
2009 * If the on-disk file index is incomplete we will try to complete it.
2011 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2012 if (err)
2013 goto done;
2015 tpd = SIMPLEQ_FIRST(&tree_paths);
2016 TAILQ_FOREACH(pe, paths, entry) {
2017 struct bump_base_commit_id_arg bbc_arg;
2019 err = checkout_files(worktree, fileindex, tpd->relpath,
2020 tpd->tree_id, tpd->entry_name, repo,
2021 progress_cb, progress_arg, cancel_cb, cancel_arg);
2022 if (err)
2023 break;
2025 bbc_arg.base_commit_id = worktree->base_commit_id;
2026 bbc_arg.entry_name = tpd->entry_name;
2027 bbc_arg.path = pe->path;
2028 bbc_arg.path_len = pe->path_len;
2029 bbc_arg.progress_cb = progress_cb;
2030 bbc_arg.progress_arg = progress_arg;
2031 err = got_fileindex_for_each_entry_safe(fileindex,
2032 bump_base_commit_id, &bbc_arg);
2033 if (err)
2034 break;
2036 tpd = SIMPLEQ_NEXT(tpd, entry);
2038 sync_err = sync_fileindex(fileindex, fileindex_path);
2039 if (sync_err && err == NULL)
2040 err = sync_err;
2041 done:
2042 free(fileindex_path);
2043 if (tree)
2044 got_object_tree_close(tree);
2045 if (commit)
2046 got_object_commit_close(commit);
2047 if (fileindex)
2048 got_fileindex_free(fileindex);
2049 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2050 tpd = SIMPLEQ_FIRST(&tree_paths);
2051 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2052 free(tpd->relpath);
2053 free(tpd->tree_id);
2054 free(tpd);
2056 unlockerr = lock_worktree(worktree, LOCK_SH);
2057 if (unlockerr && err == NULL)
2058 err = unlockerr;
2059 return err;
2062 struct merge_file_cb_arg {
2063 struct got_worktree *worktree;
2064 struct got_fileindex *fileindex;
2065 got_worktree_checkout_cb progress_cb;
2066 void *progress_arg;
2067 got_cancel_cb cancel_cb;
2068 void *cancel_arg;
2069 const char *label_orig;
2070 struct got_object_id *commit_id2;
2073 static const struct got_error *
2074 merge_file_cb(void *arg, struct got_blob_object *blob1,
2075 struct got_blob_object *blob2, struct got_object_id *id1,
2076 struct got_object_id *id2, const char *path1, const char *path2,
2077 mode_t mode1, mode_t mode2, struct got_repository *repo)
2079 static const struct got_error *err = NULL;
2080 struct merge_file_cb_arg *a = arg;
2081 struct got_fileindex_entry *ie;
2082 char *ondisk_path = NULL;
2083 struct stat sb;
2084 unsigned char status;
2085 int local_changes_subsumed;
2087 if (blob1 && blob2) {
2088 ie = got_fileindex_entry_get(a->fileindex, path2,
2089 strlen(path2));
2090 if (ie == NULL)
2091 return (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_MISSING, path2);
2094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2095 path2) == -1)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2099 repo);
2100 if (err)
2101 goto done;
2103 if (status == GOT_STATUS_DELETE) {
2104 err = (*a->progress_cb)(a->progress_arg,
2105 GOT_STATUS_MERGE, path2);
2106 goto done;
2108 if (status != GOT_STATUS_NO_CHANGE &&
2109 status != GOT_STATUS_MODIFY &&
2110 status != GOT_STATUS_CONFLICT &&
2111 status != GOT_STATUS_ADD) {
2112 err = (*a->progress_cb)(a->progress_arg, status, path2);
2113 goto done;
2116 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2117 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2118 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2119 } else if (blob1) {
2120 ie = got_fileindex_entry_get(a->fileindex, path1,
2121 strlen(path1));
2122 if (ie == NULL)
2123 return (*a->progress_cb)(a->progress_arg,
2124 GOT_STATUS_MISSING, path1);
2126 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2127 path1) == -1)
2128 return got_error_from_errno("asprintf");
2130 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2131 repo);
2132 if (err)
2133 goto done;
2135 switch (status) {
2136 case GOT_STATUS_NO_CHANGE:
2137 err = (*a->progress_cb)(a->progress_arg,
2138 GOT_STATUS_DELETE, path1);
2139 if (err)
2140 goto done;
2141 err = remove_ondisk_file(a->worktree->root_path, path1);
2142 if (err)
2143 goto done;
2144 if (ie)
2145 got_fileindex_entry_mark_deleted_from_disk(ie);
2146 break;
2147 case GOT_STATUS_DELETE:
2148 case GOT_STATUS_MISSING:
2149 err = (*a->progress_cb)(a->progress_arg,
2150 GOT_STATUS_DELETE, path1);
2151 if (err)
2152 goto done;
2153 if (ie)
2154 got_fileindex_entry_mark_deleted_from_disk(ie);
2155 break;
2156 case GOT_STATUS_ADD:
2157 case GOT_STATUS_MODIFY:
2158 case GOT_STATUS_CONFLICT:
2159 err = (*a->progress_cb)(a->progress_arg,
2160 GOT_STATUS_CANNOT_DELETE, path1);
2161 if (err)
2162 goto done;
2163 break;
2164 case GOT_STATUS_OBSTRUCTED:
2165 err = (*a->progress_cb)(a->progress_arg, status, path1);
2166 if (err)
2167 goto done;
2168 break;
2169 default:
2170 break;
2172 } else if (blob2) {
2173 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2174 path2) == -1)
2175 return got_error_from_errno("asprintf");
2176 ie = got_fileindex_entry_get(a->fileindex, path2,
2177 strlen(path2));
2178 if (ie) {
2179 err = get_file_status(&status, &sb, ie, ondisk_path,
2180 -1, NULL, repo);
2181 if (err)
2182 goto done;
2183 if (status != GOT_STATUS_NO_CHANGE &&
2184 status != GOT_STATUS_MODIFY &&
2185 status != GOT_STATUS_CONFLICT &&
2186 status != GOT_STATUS_ADD) {
2187 err = (*a->progress_cb)(a->progress_arg,
2188 status, path2);
2189 goto done;
2191 err = merge_blob(&local_changes_subsumed, a->worktree,
2192 NULL, ondisk_path, path2, sb.st_mode,
2193 a->label_orig, blob2, a->commit_id2, repo,
2194 a->progress_cb,
2195 a->progress_arg);
2196 if (status == GOT_STATUS_DELETE) {
2197 err = got_fileindex_entry_update(ie,
2198 ondisk_path, blob2->id.sha1,
2199 a->worktree->base_commit_id->sha1, 0);
2200 if (err)
2201 goto done;
2203 } else {
2204 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2205 err = install_blob(a->worktree, ondisk_path, path2,
2206 /* XXX get this from parent tree! */
2207 GOT_DEFAULT_FILE_MODE,
2208 sb.st_mode, blob2, 0, 0, repo,
2209 a->progress_cb, a->progress_arg);
2210 if (err)
2211 goto done;
2212 err = got_fileindex_entry_alloc(&ie, path2);
2213 if (err)
2214 goto done;
2215 err = got_fileindex_entry_update(ie, ondisk_path,
2216 NULL, NULL, 1);
2217 if (err) {
2218 got_fileindex_entry_free(ie);
2219 goto done;
2221 err = got_fileindex_entry_add(a->fileindex, ie);
2222 if (err) {
2223 got_fileindex_entry_free(ie);
2224 goto done;
2228 done:
2229 free(ondisk_path);
2230 return err;
2233 struct check_merge_ok_arg {
2234 struct got_worktree *worktree;
2235 struct got_repository *repo;
2238 static const struct got_error *
2239 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2241 const struct got_error *err = NULL;
2242 struct check_merge_ok_arg *a = arg;
2243 unsigned char status;
2244 struct stat sb;
2245 char *ondisk_path;
2247 /* Reject merges into a work tree with mixed base commits. */
2248 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2249 SHA1_DIGEST_LENGTH))
2250 return got_error(GOT_ERR_MIXED_COMMITS);
2252 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2253 == -1)
2254 return got_error_from_errno("asprintf");
2256 /* Reject merges into a work tree with conflicted files. */
2257 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2258 if (err)
2259 return err;
2260 if (status == GOT_STATUS_CONFLICT)
2261 return got_error(GOT_ERR_CONFLICTS);
2263 return NULL;
2266 static const struct got_error *
2267 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2268 const char *fileindex_path, struct got_object_id *commit_id1,
2269 struct got_object_id *commit_id2, struct got_repository *repo,
2270 got_worktree_checkout_cb progress_cb, void *progress_arg,
2271 got_cancel_cb cancel_cb, void *cancel_arg)
2273 const struct got_error *err = NULL, *sync_err;
2274 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2275 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2276 struct merge_file_cb_arg arg;
2277 char *label_orig = NULL;
2279 if (commit_id1) {
2280 char *id_str;
2282 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2283 worktree->path_prefix);
2284 if (err)
2285 goto done;
2287 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2288 if (err)
2289 goto done;
2291 err = got_object_id_str(&id_str, commit_id1);
2292 if (err)
2293 goto done;
2295 if (asprintf(&label_orig, "%s: commit %s",
2296 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2297 err = got_error_from_errno("asprintf");
2298 free(id_str);
2299 goto done;
2301 free(id_str);
2304 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2305 worktree->path_prefix);
2306 if (err)
2307 goto done;
2309 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2310 if (err)
2311 goto done;
2313 arg.worktree = worktree;
2314 arg.fileindex = fileindex;
2315 arg.progress_cb = progress_cb;
2316 arg.progress_arg = progress_arg;
2317 arg.cancel_cb = cancel_cb;
2318 arg.cancel_arg = cancel_arg;
2319 arg.label_orig = label_orig;
2320 arg.commit_id2 = commit_id2;
2321 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2322 sync_err = sync_fileindex(fileindex, fileindex_path);
2323 if (sync_err && err == NULL)
2324 err = sync_err;
2325 done:
2326 if (tree1)
2327 got_object_tree_close(tree1);
2328 if (tree2)
2329 got_object_tree_close(tree2);
2330 free(label_orig);
2331 return err;
2334 const struct got_error *
2335 got_worktree_merge_files(struct got_worktree *worktree,
2336 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2337 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2338 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2340 const struct got_error *err, *unlockerr;
2341 char *fileindex_path = NULL;
2342 struct got_fileindex *fileindex = NULL;
2343 struct check_merge_ok_arg mok_arg;
2345 err = lock_worktree(worktree, LOCK_EX);
2346 if (err)
2347 return err;
2349 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2350 if (err)
2351 goto done;
2353 mok_arg.worktree = worktree;
2354 mok_arg.repo = repo;
2355 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2356 &mok_arg);
2357 if (err)
2358 goto done;
2360 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2361 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2362 done:
2363 if (fileindex)
2364 got_fileindex_free(fileindex);
2365 free(fileindex_path);
2366 unlockerr = lock_worktree(worktree, LOCK_SH);
2367 if (unlockerr && err == NULL)
2368 err = unlockerr;
2369 return err;
2372 struct diff_dir_cb_arg {
2373 struct got_fileindex *fileindex;
2374 struct got_worktree *worktree;
2375 const char *status_path;
2376 size_t status_path_len;
2377 struct got_repository *repo;
2378 got_worktree_status_cb status_cb;
2379 void *status_arg;
2380 got_cancel_cb cancel_cb;
2381 void *cancel_arg;
2382 /* A pathlist containing per-directory pathlists of ignore patterns. */
2383 struct got_pathlist_head ignores;
2384 int report_unchanged;
2385 int no_ignores;
2388 static const struct got_error *
2389 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2390 int dirfd, const char *de_name,
2391 got_worktree_status_cb status_cb, void *status_arg,
2392 struct got_repository *repo, int report_unchanged)
2394 const struct got_error *err = NULL;
2395 unsigned char status = GOT_STATUS_NO_CHANGE;
2396 unsigned char staged_status = get_staged_status(ie);
2397 struct stat sb;
2398 struct got_object_id blob_id, commit_id, staged_blob_id;
2399 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2400 struct got_object_id *staged_blob_idp = NULL;
2402 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2403 if (err)
2404 return err;
2406 if (status == GOT_STATUS_NO_CHANGE &&
2407 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2408 return NULL;
2410 if (got_fileindex_entry_has_blob(ie)) {
2411 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2412 blob_idp = &blob_id;
2414 if (got_fileindex_entry_has_commit(ie)) {
2415 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2416 commit_idp = &commit_id;
2418 if (staged_status == GOT_STATUS_ADD ||
2419 staged_status == GOT_STATUS_MODIFY) {
2420 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2421 SHA1_DIGEST_LENGTH);
2422 staged_blob_idp = &staged_blob_id;
2425 return (*status_cb)(status_arg, status, staged_status,
2426 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2429 static const struct got_error *
2430 status_old_new(void *arg, struct got_fileindex_entry *ie,
2431 struct dirent *de, const char *parent_path, int dirfd)
2433 const struct got_error *err = NULL;
2434 struct diff_dir_cb_arg *a = arg;
2435 char *abspath;
2437 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2438 return got_error(GOT_ERR_CANCELLED);
2440 if (got_path_cmp(parent_path, a->status_path,
2441 strlen(parent_path), a->status_path_len) != 0 &&
2442 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2443 return NULL;
2445 if (parent_path[0]) {
2446 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2447 parent_path, de->d_name) == -1)
2448 return got_error_from_errno("asprintf");
2449 } else {
2450 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2451 de->d_name) == -1)
2452 return got_error_from_errno("asprintf");
2455 err = report_file_status(ie, abspath, dirfd, de->d_name,
2456 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2457 free(abspath);
2458 return err;
2461 static const struct got_error *
2462 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2464 struct diff_dir_cb_arg *a = arg;
2465 struct got_object_id blob_id, commit_id;
2466 unsigned char status;
2468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2469 return got_error(GOT_ERR_CANCELLED);
2471 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2472 return NULL;
2474 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2475 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2476 if (got_fileindex_entry_has_file_on_disk(ie))
2477 status = GOT_STATUS_MISSING;
2478 else
2479 status = GOT_STATUS_DELETE;
2480 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2481 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2484 void
2485 free_ignorelist(struct got_pathlist_head *ignorelist)
2487 struct got_pathlist_entry *pe;
2489 TAILQ_FOREACH(pe, ignorelist, entry)
2490 free((char *)pe->path);
2491 got_pathlist_free(ignorelist);
2494 void
2495 free_ignores(struct got_pathlist_head *ignores)
2497 struct got_pathlist_entry *pe;
2499 TAILQ_FOREACH(pe, ignores, entry) {
2500 struct got_pathlist_head *ignorelist = pe->data;
2501 free_ignorelist(ignorelist);
2502 free((char *)pe->path);
2504 got_pathlist_free(ignores);
2507 static const struct got_error *
2508 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2510 const struct got_error *err = NULL;
2511 struct got_pathlist_entry *pe = NULL;
2512 struct got_pathlist_head *ignorelist;
2513 char *line = NULL, *pattern, *dirpath = NULL;
2514 size_t linesize = 0;
2515 ssize_t linelen;
2517 ignorelist = calloc(1, sizeof(*ignorelist));
2518 if (ignorelist == NULL)
2519 return got_error_from_errno("calloc");
2520 TAILQ_INIT(ignorelist);
2522 while ((linelen = getline(&line, &linesize, f)) != -1) {
2523 if (linelen > 0 && line[linelen - 1] == '\n')
2524 line[linelen - 1] = '\0';
2526 /* Git's ignores may contain comments. */
2527 if (line[0] == '#')
2528 continue;
2530 /* Git's negated patterns are not (yet?) supported. */
2531 if (line[0] == '!')
2532 continue;
2534 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2535 line) == -1) {
2536 err = got_error_from_errno("asprintf");
2537 goto done;
2539 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2540 if (err)
2541 goto done;
2543 if (ferror(f)) {
2544 err = got_error_from_errno("getline");
2545 goto done;
2548 dirpath = strdup(path);
2549 if (dirpath == NULL) {
2550 err = got_error_from_errno("strdup");
2551 goto done;
2553 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2554 done:
2555 free(line);
2556 if (err || pe == NULL) {
2557 free(dirpath);
2558 free_ignorelist(ignorelist);
2560 return err;
2563 int
2564 match_ignores(struct got_pathlist_head *ignores, const char *path)
2566 struct got_pathlist_entry *pe;
2568 /* Handle patterns which match in all directories. */
2569 TAILQ_FOREACH(pe, ignores, entry) {
2570 struct got_pathlist_head *ignorelist = pe->data;
2571 struct got_pathlist_entry *pi;
2573 TAILQ_FOREACH(pi, ignorelist, entry) {
2574 const char *p, *pattern = pi->path;
2576 if (strncmp(pattern, "**/", 3) != 0)
2577 continue;
2578 pattern += 3;
2579 p = path;
2580 while (*p) {
2581 if (fnmatch(pattern, p,
2582 FNM_PATHNAME | FNM_LEADING_DIR)) {
2583 /* Retry in next directory. */
2584 while (*p && *p != '/')
2585 p++;
2586 while (*p == '/')
2587 p++;
2588 continue;
2590 return 1;
2596 * The ignores pathlist contains ignore lists from children before
2597 * parents, so we can find the most specific ignorelist by walking
2598 * ignores backwards.
2600 pe = TAILQ_LAST(ignores, got_pathlist_head);
2601 while (pe) {
2602 if (got_path_is_child(path, pe->path, pe->path_len)) {
2603 struct got_pathlist_head *ignorelist = pe->data;
2604 struct got_pathlist_entry *pi;
2605 TAILQ_FOREACH(pi, ignorelist, entry) {
2606 const char *pattern = pi->path;
2607 int flags = FNM_LEADING_DIR;
2608 if (strstr(pattern, "/**/") == NULL)
2609 flags |= FNM_PATHNAME;
2610 if (fnmatch(pattern, path, flags))
2611 continue;
2612 return 1;
2615 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2618 return 0;
2621 static const struct got_error *
2622 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2623 const char *path, int dirfd, const char *ignores_filename)
2625 const struct got_error *err = NULL;
2626 char *ignorespath;
2627 int fd = -1;
2628 FILE *ignoresfile = NULL;
2630 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2631 path[0] ? "/" : "", ignores_filename) == -1)
2632 return got_error_from_errno("asprintf");
2634 if (dirfd != -1) {
2635 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2636 if (fd == -1) {
2637 if (errno != ENOENT && errno != EACCES)
2638 err = got_error_from_errno2("openat",
2639 ignorespath);
2640 } else {
2641 ignoresfile = fdopen(fd, "r");
2642 if (ignoresfile == NULL)
2643 err = got_error_from_errno2("fdopen",
2644 ignorespath);
2645 else {
2646 fd = -1;
2647 err = read_ignores(ignores, path, ignoresfile);
2650 } else {
2651 ignoresfile = fopen(ignorespath, "r");
2652 if (ignoresfile == NULL) {
2653 if (errno != ENOENT && errno != EACCES)
2654 err = got_error_from_errno2("fopen",
2655 ignorespath);
2656 } else
2657 err = read_ignores(ignores, path, ignoresfile);
2660 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2661 err = got_error_from_errno2("fclose", path);
2662 if (fd != -1 && close(fd) == -1 && err == NULL)
2663 err = got_error_from_errno2("close", path);
2664 free(ignorespath);
2665 return err;
2668 static const struct got_error *
2669 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2671 const struct got_error *err = NULL;
2672 struct diff_dir_cb_arg *a = arg;
2673 char *path = NULL;
2675 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2676 return got_error(GOT_ERR_CANCELLED);
2678 /* XXX ignore symlinks for now */
2679 if (de->d_type == DT_LNK)
2680 return NULL;
2682 if (parent_path[0]) {
2683 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2684 return got_error_from_errno("asprintf");
2685 } else {
2686 path = de->d_name;
2689 if (de->d_type != DT_DIR &&
2690 got_path_is_child(path, a->status_path, a->status_path_len)
2691 && !match_ignores(&a->ignores, path))
2692 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2693 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2694 if (parent_path[0])
2695 free(path);
2696 return err;
2699 static const struct got_error *
2700 status_traverse(void *arg, const char *path, int dirfd)
2702 const struct got_error *err = NULL;
2703 struct diff_dir_cb_arg *a = arg;
2705 if (a->no_ignores)
2706 return NULL;
2708 err = add_ignores(&a->ignores, a->worktree->root_path,
2709 path, dirfd, ".cvsignore");
2710 if (err)
2711 return err;
2713 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2714 dirfd, ".gitignore");
2716 return err;
2719 static const struct got_error *
2720 report_single_file_status(const char *path, const char *ondisk_path,
2721 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2722 void *status_arg, struct got_repository *repo, int report_unchanged)
2724 struct got_fileindex_entry *ie;
2725 struct stat sb;
2727 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2728 if (ie)
2729 return report_file_status(ie, ondisk_path, -1, NULL,
2730 status_cb, status_arg, repo, report_unchanged);
2732 if (lstat(ondisk_path, &sb) == -1) {
2733 if (errno != ENOENT)
2734 return got_error_from_errno2("lstat", ondisk_path);
2735 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2736 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2737 return NULL;
2740 if (S_ISREG(sb.st_mode))
2741 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2742 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2744 return NULL;
2747 static const struct got_error *
2748 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2749 const char *root_path, const char *path)
2751 const struct got_error *err;
2752 char *parent_path, *next_parent_path = NULL;
2754 err = add_ignores(ignores, root_path, "", -1,
2755 ".cvsignore");
2756 if (err)
2757 return err;
2759 err = add_ignores(ignores, root_path, "", -1,
2760 ".gitignore");
2761 if (err)
2762 return err;
2764 err = got_path_dirname(&parent_path, path);
2765 if (err) {
2766 if (err->code == GOT_ERR_BAD_PATH)
2767 return NULL; /* cannot traverse parent */
2768 return err;
2770 for (;;) {
2771 err = add_ignores(ignores, root_path, parent_path, -1,
2772 ".cvsignore");
2773 if (err)
2774 break;
2775 err = add_ignores(ignores, root_path, parent_path, -1,
2776 ".gitignore");
2777 if (err)
2778 break;
2779 err = got_path_dirname(&next_parent_path, parent_path);
2780 if (err) {
2781 if (err->code == GOT_ERR_BAD_PATH)
2782 err = NULL; /* traversed everything */
2783 break;
2785 free(parent_path);
2786 parent_path = next_parent_path;
2787 next_parent_path = NULL;
2790 free(parent_path);
2791 free(next_parent_path);
2792 return err;
2795 static const struct got_error *
2796 worktree_status(struct got_worktree *worktree, const char *path,
2797 struct got_fileindex *fileindex, struct got_repository *repo,
2798 got_worktree_status_cb status_cb, void *status_arg,
2799 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2800 int report_unchanged)
2802 const struct got_error *err = NULL;
2803 int fd = -1;
2804 struct got_fileindex_diff_dir_cb fdiff_cb;
2805 struct diff_dir_cb_arg arg;
2806 char *ondisk_path = NULL;
2808 TAILQ_INIT(&arg.ignores);
2810 if (asprintf(&ondisk_path, "%s%s%s",
2811 worktree->root_path, path[0] ? "/" : "", path) == -1)
2812 return got_error_from_errno("asprintf");
2814 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2815 if (fd == -1) {
2816 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2817 err = got_error_from_errno2("open", ondisk_path);
2818 else
2819 err = report_single_file_status(path, ondisk_path,
2820 fileindex, status_cb, status_arg, repo,
2821 report_unchanged);
2822 } else {
2823 fdiff_cb.diff_old_new = status_old_new;
2824 fdiff_cb.diff_old = status_old;
2825 fdiff_cb.diff_new = status_new;
2826 fdiff_cb.diff_traverse = status_traverse;
2827 arg.fileindex = fileindex;
2828 arg.worktree = worktree;
2829 arg.status_path = path;
2830 arg.status_path_len = strlen(path);
2831 arg.repo = repo;
2832 arg.status_cb = status_cb;
2833 arg.status_arg = status_arg;
2834 arg.cancel_cb = cancel_cb;
2835 arg.cancel_arg = cancel_arg;
2836 arg.report_unchanged = report_unchanged;
2837 arg.no_ignores = no_ignores;
2838 if (!no_ignores) {
2839 err = add_ignores_from_parent_paths(&arg.ignores,
2840 worktree->root_path, path);
2841 if (err)
2842 goto done;
2844 err = got_fileindex_diff_dir(fileindex, fd,
2845 worktree->root_path, path, repo, &fdiff_cb, &arg);
2847 done:
2848 free_ignores(&arg.ignores);
2849 if (fd != -1 && close(fd) != 0 && err == NULL)
2850 err = got_error_from_errno("close");
2851 free(ondisk_path);
2852 return err;
2855 const struct got_error *
2856 got_worktree_status(struct got_worktree *worktree,
2857 struct got_pathlist_head *paths, struct got_repository *repo,
2858 got_worktree_status_cb status_cb, void *status_arg,
2859 got_cancel_cb cancel_cb, void *cancel_arg)
2861 const struct got_error *err = NULL;
2862 char *fileindex_path = NULL;
2863 struct got_fileindex *fileindex = NULL;
2864 struct got_pathlist_entry *pe;
2866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2867 if (err)
2868 return err;
2870 TAILQ_FOREACH(pe, paths, entry) {
2871 err = worktree_status(worktree, pe->path, fileindex, repo,
2872 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2873 if (err)
2874 break;
2876 free(fileindex_path);
2877 got_fileindex_free(fileindex);
2878 return err;
2881 const struct got_error *
2882 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2883 const char *arg)
2885 const struct got_error *err = NULL;
2886 char *resolved, *cwd = NULL, *path = NULL;
2887 size_t len;
2889 *wt_path = NULL;
2891 resolved = realpath(arg, NULL);
2892 if (resolved == NULL) {
2893 if (errno != ENOENT)
2894 return got_error_from_errno2("realpath", arg);
2895 cwd = getcwd(NULL, 0);
2896 if (cwd == NULL)
2897 return got_error_from_errno("getcwd");
2898 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2899 err = got_error_from_errno("asprintf");
2900 goto done;
2904 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2905 strlen(got_worktree_get_root_path(worktree)))) {
2906 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2907 goto done;
2910 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2911 err = got_path_skip_common_ancestor(&path,
2912 got_worktree_get_root_path(worktree), resolved);
2913 if (err)
2914 goto done;
2915 } else {
2916 path = strdup("");
2917 if (path == NULL) {
2918 err = got_error_from_errno("strdup");
2919 goto done;
2923 /* XXX status walk can't deal with trailing slash! */
2924 len = strlen(path);
2925 while (len > 0 && path[len - 1] == '/') {
2926 path[len - 1] = '\0';
2927 len--;
2929 done:
2930 free(resolved);
2931 free(cwd);
2932 if (err == NULL)
2933 *wt_path = path;
2934 else
2935 free(path);
2936 return err;
2939 struct schedule_addition_args {
2940 struct got_worktree *worktree;
2941 struct got_fileindex *fileindex;
2942 got_worktree_checkout_cb progress_cb;
2943 void *progress_arg;
2944 struct got_repository *repo;
2947 static const struct got_error *
2948 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2949 const char *relpath, struct got_object_id *blob_id,
2950 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2951 int dirfd, const char *de_name)
2953 struct schedule_addition_args *a = arg;
2954 const struct got_error *err = NULL;
2955 struct got_fileindex_entry *ie;
2956 struct stat sb;
2957 char *ondisk_path;
2959 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2960 relpath) == -1)
2961 return got_error_from_errno("asprintf");
2963 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2964 if (ie) {
2965 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2966 de_name, a->repo);
2967 if (err)
2968 goto done;
2969 /* Re-adding an existing entry is a no-op. */
2970 if (status == GOT_STATUS_ADD)
2971 goto done;
2972 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2973 if (err)
2974 goto done;
2977 if (status != GOT_STATUS_UNVERSIONED) {
2978 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2979 goto done;
2982 err = got_fileindex_entry_alloc(&ie, relpath);
2983 if (err)
2984 goto done;
2985 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2986 if (err) {
2987 got_fileindex_entry_free(ie);
2988 goto done;
2990 err = got_fileindex_entry_add(a->fileindex, ie);
2991 if (err) {
2992 got_fileindex_entry_free(ie);
2993 goto done;
2995 done:
2996 free(ondisk_path);
2997 if (err)
2998 return err;
2999 if (status == GOT_STATUS_ADD)
3000 return NULL;
3001 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3004 const struct got_error *
3005 got_worktree_schedule_add(struct got_worktree *worktree,
3006 struct got_pathlist_head *paths,
3007 got_worktree_checkout_cb progress_cb, void *progress_arg,
3008 struct got_repository *repo, int no_ignores)
3010 struct got_fileindex *fileindex = NULL;
3011 char *fileindex_path = NULL;
3012 const struct got_error *err = NULL, *sync_err, *unlockerr;
3013 struct got_pathlist_entry *pe;
3014 struct schedule_addition_args saa;
3016 err = lock_worktree(worktree, LOCK_EX);
3017 if (err)
3018 return err;
3020 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3021 if (err)
3022 goto done;
3024 saa.worktree = worktree;
3025 saa.fileindex = fileindex;
3026 saa.progress_cb = progress_cb;
3027 saa.progress_arg = progress_arg;
3028 saa.repo = repo;
3030 TAILQ_FOREACH(pe, paths, entry) {
3031 err = worktree_status(worktree, pe->path, fileindex, repo,
3032 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3033 if (err)
3034 break;
3036 sync_err = sync_fileindex(fileindex, fileindex_path);
3037 if (sync_err && err == NULL)
3038 err = sync_err;
3039 done:
3040 free(fileindex_path);
3041 if (fileindex)
3042 got_fileindex_free(fileindex);
3043 unlockerr = lock_worktree(worktree, LOCK_SH);
3044 if (unlockerr && err == NULL)
3045 err = unlockerr;
3046 return err;
3049 struct schedule_deletion_args {
3050 struct got_worktree *worktree;
3051 struct got_fileindex *fileindex;
3052 got_worktree_delete_cb progress_cb;
3053 void *progress_arg;
3054 struct got_repository *repo;
3055 int delete_local_mods;
3056 int keep_on_disk;
3059 static const struct got_error *
3060 schedule_for_deletion(void *arg, unsigned char status,
3061 unsigned char staged_status, const char *relpath,
3062 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3063 struct got_object_id *commit_id, int dirfd, const char *de_name)
3065 struct schedule_deletion_args *a = arg;
3066 const struct got_error *err = NULL;
3067 struct got_fileindex_entry *ie = NULL;
3068 struct stat sb;
3069 char *ondisk_path, *parent = NULL;
3071 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3072 if (ie == NULL)
3073 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3075 staged_status = get_staged_status(ie);
3076 if (staged_status != GOT_STATUS_NO_CHANGE) {
3077 if (staged_status == GOT_STATUS_DELETE)
3078 return NULL;
3079 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3082 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3083 relpath) == -1)
3084 return got_error_from_errno("asprintf");
3086 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3087 a->repo);
3088 if (err)
3089 goto done;
3091 if (status != GOT_STATUS_NO_CHANGE) {
3092 if (status == GOT_STATUS_DELETE)
3093 goto done;
3094 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3095 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3096 goto done;
3098 if (status != GOT_STATUS_MODIFY &&
3099 status != GOT_STATUS_MISSING) {
3100 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3101 goto done;
3105 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3106 if (dirfd != -1) {
3107 if (unlinkat(dirfd, de_name, 0) != 0) {
3108 err = got_error_from_errno2("unlinkat",
3109 ondisk_path);
3110 goto done;
3112 } else if (unlink(ondisk_path) != 0) {
3113 err = got_error_from_errno2("unlink", ondisk_path);
3114 goto done;
3117 parent = dirname(ondisk_path);
3119 if (parent == NULL) {
3120 err = got_error_from_errno2("dirname", ondisk_path);
3121 goto done;
3123 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3124 if (rmdir(parent) == -1) {
3125 if (errno != ENOTEMPTY)
3126 err = got_error_from_errno2("rmdir",
3127 parent);
3128 break;
3130 parent = dirname(parent);
3131 if (parent == NULL) {
3132 err = got_error_from_errno2("dirname", parent);
3133 goto done;
3138 got_fileindex_entry_mark_deleted_from_disk(ie);
3139 done:
3140 free(ondisk_path);
3141 if (err)
3142 return err;
3143 if (status == GOT_STATUS_DELETE)
3144 return NULL;
3145 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3146 staged_status, relpath);
3149 const struct got_error *
3150 got_worktree_schedule_delete(struct got_worktree *worktree,
3151 struct got_pathlist_head *paths, int delete_local_mods,
3152 got_worktree_delete_cb progress_cb, void *progress_arg,
3153 struct got_repository *repo, int keep_on_disk)
3155 struct got_fileindex *fileindex = NULL;
3156 char *fileindex_path = NULL;
3157 const struct got_error *err = NULL, *sync_err, *unlockerr;
3158 struct got_pathlist_entry *pe;
3159 struct schedule_deletion_args sda;
3161 err = lock_worktree(worktree, LOCK_EX);
3162 if (err)
3163 return err;
3165 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3166 if (err)
3167 goto done;
3169 sda.worktree = worktree;
3170 sda.fileindex = fileindex;
3171 sda.progress_cb = progress_cb;
3172 sda.progress_arg = progress_arg;
3173 sda.repo = repo;
3174 sda.delete_local_mods = delete_local_mods;
3175 sda.keep_on_disk = keep_on_disk;
3177 TAILQ_FOREACH(pe, paths, entry) {
3178 err = worktree_status(worktree, pe->path, fileindex, repo,
3179 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3180 if (err)
3181 break;
3183 sync_err = sync_fileindex(fileindex, fileindex_path);
3184 if (sync_err && err == NULL)
3185 err = sync_err;
3186 done:
3187 free(fileindex_path);
3188 if (fileindex)
3189 got_fileindex_free(fileindex);
3190 unlockerr = lock_worktree(worktree, LOCK_SH);
3191 if (unlockerr && err == NULL)
3192 err = unlockerr;
3193 return err;
3196 static const struct got_error *
3197 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3199 const struct got_error *err = NULL;
3200 char *line = NULL;
3201 size_t linesize = 0, n;
3202 ssize_t linelen;
3204 linelen = getline(&line, &linesize, infile);
3205 if (linelen == -1) {
3206 if (ferror(infile)) {
3207 err = got_error_from_errno("getline");
3208 goto done;
3210 return NULL;
3212 if (outfile) {
3213 n = fwrite(line, 1, linelen, outfile);
3214 if (n != linelen) {
3215 err = got_ferror(outfile, GOT_ERR_IO);
3216 goto done;
3219 if (rejectfile) {
3220 n = fwrite(line, 1, linelen, rejectfile);
3221 if (n != linelen)
3222 err = got_ferror(outfile, GOT_ERR_IO);
3224 done:
3225 free(line);
3226 return err;
3229 static const struct got_error *
3230 skip_one_line(FILE *f)
3232 char *line = NULL;
3233 size_t linesize = 0;
3234 ssize_t linelen;
3236 linelen = getline(&line, &linesize, f);
3237 if (linelen == -1) {
3238 if (ferror(f))
3239 return got_error_from_errno("getline");
3240 return NULL;
3242 free(line);
3243 return NULL;
3246 static const struct got_error *
3247 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3248 int start_old, int end_old, int start_new, int end_new,
3249 FILE *outfile, FILE *rejectfile)
3251 const struct got_error *err;
3253 /* Copy old file's lines leading up to patch. */
3254 while (!feof(f1) && *line_cur1 < start_old) {
3255 err = copy_one_line(f1, outfile, NULL);
3256 if (err)
3257 return err;
3258 (*line_cur1)++;
3260 /* Skip new file's lines leading up to patch. */
3261 while (!feof(f2) && *line_cur2 < start_new) {
3262 if (rejectfile)
3263 err = copy_one_line(f2, NULL, rejectfile);
3264 else
3265 err = skip_one_line(f2);
3266 if (err)
3267 return err;
3268 (*line_cur2)++;
3270 /* Copy patched lines. */
3271 while (!feof(f2) && *line_cur2 <= end_new) {
3272 err = copy_one_line(f2, outfile, NULL);
3273 if (err)
3274 return err;
3275 (*line_cur2)++;
3277 /* Skip over old file's replaced lines. */
3278 while (!feof(f1) && *line_cur1 <= end_old) {
3279 if (rejectfile)
3280 err = copy_one_line(f1, NULL, rejectfile);
3281 else
3282 err = skip_one_line(f1);
3283 if (err)
3284 return err;
3285 (*line_cur1)++;
3288 return NULL;
3291 static const struct got_error *
3292 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3293 FILE *outfile, FILE *rejectfile)
3295 const struct got_error *err;
3297 if (outfile) {
3298 /* Copy old file's lines until EOF. */
3299 while (!feof(f1)) {
3300 err = copy_one_line(f1, outfile, NULL);
3301 if (err)
3302 return err;
3303 (*line_cur1)++;
3306 if (rejectfile) {
3307 /* Copy new file's lines until EOF. */
3308 while (!feof(f2)) {
3309 err = copy_one_line(f2, NULL, rejectfile);
3310 if (err)
3311 return err;
3312 (*line_cur2)++;
3316 return NULL;
3319 static const struct got_error *
3320 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3321 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3322 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3323 int *line_cur2, FILE *outfile, FILE *rejectfile,
3324 got_worktree_patch_cb patch_cb, void *patch_arg)
3326 const struct got_error *err = NULL;
3327 int start_old = change->cv.a;
3328 int end_old = change->cv.b;
3329 int start_new = change->cv.c;
3330 int end_new = change->cv.d;
3331 long pos1, pos2;
3332 FILE *hunkfile;
3334 *choice = GOT_PATCH_CHOICE_NONE;
3336 hunkfile = got_opentemp();
3337 if (hunkfile == NULL)
3338 return got_error_from_errno("got_opentemp");
3340 pos1 = ftell(f1);
3341 pos2 = ftell(f2);
3343 /* XXX TODO needs error checking */
3344 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3346 if (fseek(f1, pos1, SEEK_SET) == -1) {
3347 err = got_ferror(f1, GOT_ERR_IO);
3348 goto done;
3350 if (fseek(f2, pos2, SEEK_SET) == -1) {
3351 err = got_ferror(f1, GOT_ERR_IO);
3352 goto done;
3354 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3355 err = got_ferror(hunkfile, GOT_ERR_IO);
3356 goto done;
3359 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3360 hunkfile, n, nchanges);
3361 if (err)
3362 goto done;
3364 switch (*choice) {
3365 case GOT_PATCH_CHOICE_YES:
3366 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3367 end_old, start_new, end_new, outfile, rejectfile);
3368 break;
3369 case GOT_PATCH_CHOICE_NO:
3370 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3371 end_old, start_new, end_new, rejectfile, outfile);
3372 break;
3373 case GOT_PATCH_CHOICE_QUIT:
3374 break;
3375 default:
3376 err = got_error(GOT_ERR_PATCH_CHOICE);
3377 break;
3379 done:
3380 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3381 err = got_error_from_errno("fclose");
3382 return err;
3385 struct revert_file_args {
3386 struct got_worktree *worktree;
3387 struct got_fileindex *fileindex;
3388 got_worktree_checkout_cb progress_cb;
3389 void *progress_arg;
3390 got_worktree_patch_cb patch_cb;
3391 void *patch_arg;
3392 struct got_repository *repo;
3395 static const struct got_error *
3396 create_patched_content(char **path_outfile, int reverse_patch,
3397 struct got_object_id *blob_id, const char *path2,
3398 int dirfd2, const char *de_name2,
3399 const char *relpath, struct got_repository *repo,
3400 got_worktree_patch_cb patch_cb, void *patch_arg)
3402 const struct got_error *err;
3403 struct got_blob_object *blob = NULL;
3404 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3405 int fd2 = -1;
3406 char *path1 = NULL, *id_str = NULL;
3407 struct stat sb1, sb2;
3408 struct got_diff_changes *changes = NULL;
3409 struct got_diff_state *ds = NULL;
3410 struct got_diff_args *args = NULL;
3411 struct got_diff_change *change;
3412 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3413 int n = 0;
3415 *path_outfile = NULL;
3417 err = got_object_id_str(&id_str, blob_id);
3418 if (err)
3419 return err;
3421 if (dirfd2 != -1) {
3422 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3423 if (fd2 == -1) {
3424 err = got_error_from_errno2("openat", path2);
3425 goto done;
3427 } else {
3428 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3429 if (fd2 == -1) {
3430 err = got_error_from_errno2("open", path2);
3431 goto done;
3434 if (fstat(fd2, &sb2) == -1) {
3435 err = got_error_from_errno2("fstat", path2);
3436 goto done;
3439 f2 = fdopen(fd2, "r");
3440 if (f2 == NULL) {
3441 err = got_error_from_errno2("fdopen", path2);
3442 goto done;
3444 fd2 = -1;
3446 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3447 if (err)
3448 goto done;
3450 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3451 if (err)
3452 goto done;
3454 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3455 if (err)
3456 goto done;
3458 if (stat(path1, &sb1) == -1) {
3459 err = got_error_from_errno2("stat", path1);
3460 goto done;
3463 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3464 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3465 if (err)
3466 goto done;
3468 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3469 if (err)
3470 goto done;
3472 if (fseek(f1, 0L, SEEK_SET) == -1)
3473 return got_ferror(f1, GOT_ERR_IO);
3474 if (fseek(f2, 0L, SEEK_SET) == -1)
3475 return got_ferror(f2, GOT_ERR_IO);
3476 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3477 int choice;
3478 err = apply_or_reject_change(&choice, change, ++n,
3479 changes->nchanges, ds, args, diff_flags, relpath,
3480 f1, f2, &line_cur1, &line_cur2,
3481 reverse_patch ? NULL : outfile,
3482 reverse_patch ? outfile : NULL,
3483 patch_cb, patch_arg);
3484 if (err)
3485 goto done;
3486 if (choice == GOT_PATCH_CHOICE_YES)
3487 have_content = 1;
3488 else if (choice == GOT_PATCH_CHOICE_QUIT)
3489 break;
3491 if (have_content) {
3492 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3493 reverse_patch ? NULL : outfile,
3494 reverse_patch ? outfile : NULL);
3495 if (err)
3496 goto done;
3498 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3499 err = got_error_from_errno2("chmod", path2);
3500 goto done;
3503 done:
3504 free(id_str);
3505 if (blob)
3506 got_object_blob_close(blob);
3507 if (f1 && fclose(f1) == EOF && err == NULL)
3508 err = got_error_from_errno2("fclose", path1);
3509 if (f2 && fclose(f2) == EOF && err == NULL)
3510 err = got_error_from_errno2("fclose", path2);
3511 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3512 err = got_error_from_errno2("close", path2);
3513 if (outfile && fclose(outfile) == EOF && err == NULL)
3514 err = got_error_from_errno2("fclose", *path_outfile);
3515 if (path1 && unlink(path1) == -1 && err == NULL)
3516 err = got_error_from_errno2("unlink", path1);
3517 if (err || !have_content) {
3518 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3519 err = got_error_from_errno2("unlink", *path_outfile);
3520 free(*path_outfile);
3521 *path_outfile = NULL;
3523 free(args);
3524 if (ds) {
3525 got_diff_state_free(ds);
3526 free(ds);
3528 if (changes)
3529 got_diff_free_changes(changes);
3530 free(path1);
3531 return err;
3534 static const struct got_error *
3535 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3536 const char *relpath, struct got_object_id *blob_id,
3537 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3538 int dirfd, const char *de_name)
3540 struct revert_file_args *a = arg;
3541 const struct got_error *err = NULL;
3542 char *parent_path = NULL;
3543 struct got_fileindex_entry *ie;
3544 struct got_tree_object *tree = NULL;
3545 struct got_object_id *tree_id = NULL;
3546 const struct got_tree_entry *te = NULL;
3547 char *tree_path = NULL, *te_name;
3548 char *ondisk_path = NULL, *path_content = NULL;
3549 struct got_blob_object *blob = NULL;
3551 /* Reverting a staged deletion is a no-op. */
3552 if (status == GOT_STATUS_DELETE &&
3553 staged_status != GOT_STATUS_NO_CHANGE)
3554 return NULL;
3556 if (status == GOT_STATUS_UNVERSIONED)
3557 return (*a->progress_cb)(a->progress_arg,
3558 GOT_STATUS_UNVERSIONED, relpath);
3560 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3561 if (ie == NULL)
3562 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3564 /* Construct in-repository path of tree which contains this blob. */
3565 err = got_path_dirname(&parent_path, ie->path);
3566 if (err) {
3567 if (err->code != GOT_ERR_BAD_PATH)
3568 goto done;
3569 parent_path = strdup("/");
3570 if (parent_path == NULL) {
3571 err = got_error_from_errno("strdup");
3572 goto done;
3575 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3576 tree_path = strdup(parent_path);
3577 if (tree_path == NULL) {
3578 err = got_error_from_errno("strdup");
3579 goto done;
3581 } else {
3582 if (got_path_is_root_dir(parent_path)) {
3583 tree_path = strdup(a->worktree->path_prefix);
3584 if (tree_path == NULL) {
3585 err = got_error_from_errno("strdup");
3586 goto done;
3588 } else {
3589 if (asprintf(&tree_path, "%s/%s",
3590 a->worktree->path_prefix, parent_path) == -1) {
3591 err = got_error_from_errno("asprintf");
3592 goto done;
3597 err = got_object_id_by_path(&tree_id, a->repo,
3598 a->worktree->base_commit_id, tree_path);
3599 if (err) {
3600 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3601 (status == GOT_STATUS_ADD ||
3602 staged_status == GOT_STATUS_ADD)))
3603 goto done;
3604 } else {
3605 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3606 if (err)
3607 goto done;
3609 te_name = basename(ie->path);
3610 if (te_name == NULL) {
3611 err = got_error_from_errno2("basename", ie->path);
3612 goto done;
3615 te = got_object_tree_find_entry(tree, te_name);
3616 if (te == NULL && status != GOT_STATUS_ADD &&
3617 staged_status != GOT_STATUS_ADD) {
3618 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3619 goto done;
3623 switch (status) {
3624 case GOT_STATUS_ADD:
3625 if (a->patch_cb) {
3626 int choice = GOT_PATCH_CHOICE_NONE;
3627 err = (*a->patch_cb)(&choice, a->patch_arg,
3628 status, ie->path, NULL, 1, 1);
3629 if (err)
3630 goto done;
3631 if (choice != GOT_PATCH_CHOICE_YES)
3632 break;
3634 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3635 ie->path);
3636 if (err)
3637 goto done;
3638 got_fileindex_entry_remove(a->fileindex, ie);
3639 break;
3640 case GOT_STATUS_DELETE:
3641 if (a->patch_cb) {
3642 int choice = GOT_PATCH_CHOICE_NONE;
3643 err = (*a->patch_cb)(&choice, a->patch_arg,
3644 status, ie->path, NULL, 1, 1);
3645 if (err)
3646 goto done;
3647 if (choice != GOT_PATCH_CHOICE_YES)
3648 break;
3650 /* fall through */
3651 case GOT_STATUS_MODIFY:
3652 case GOT_STATUS_MODE_CHANGE:
3653 case GOT_STATUS_CONFLICT:
3654 case GOT_STATUS_MISSING: {
3655 struct got_object_id id;
3656 if (staged_status == GOT_STATUS_ADD ||
3657 staged_status == GOT_STATUS_MODIFY) {
3658 memcpy(id.sha1, ie->staged_blob_sha1,
3659 SHA1_DIGEST_LENGTH);
3660 } else
3661 memcpy(id.sha1, ie->blob_sha1,
3662 SHA1_DIGEST_LENGTH);
3663 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3664 if (err)
3665 goto done;
3667 if (asprintf(&ondisk_path, "%s/%s",
3668 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3669 err = got_error_from_errno("asprintf");
3670 goto done;
3673 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3674 status == GOT_STATUS_CONFLICT)) {
3675 err = create_patched_content(&path_content, 1, &id,
3676 ondisk_path, dirfd, de_name, ie->path, a->repo,
3677 a->patch_cb, a->patch_arg);
3678 if (err || path_content == NULL)
3679 break;
3680 if (rename(path_content, ondisk_path) == -1) {
3681 err = got_error_from_errno3("rename",
3682 path_content, ondisk_path);
3683 goto done;
3685 } else {
3686 err = install_blob(a->worktree, ondisk_path, ie->path,
3687 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3688 got_fileindex_perms_to_st(ie), blob, 0, 1,
3689 a->repo, a->progress_cb, a->progress_arg);
3690 if (err)
3691 goto done;
3692 if (status == GOT_STATUS_DELETE ||
3693 status == GOT_STATUS_MODE_CHANGE) {
3694 err = got_fileindex_entry_update(ie,
3695 ondisk_path, blob->id.sha1,
3696 a->worktree->base_commit_id->sha1, 1);
3697 if (err)
3698 goto done;
3701 break;
3703 default:
3704 break;
3706 done:
3707 free(ondisk_path);
3708 free(path_content);
3709 free(parent_path);
3710 free(tree_path);
3711 if (blob)
3712 got_object_blob_close(blob);
3713 if (tree)
3714 got_object_tree_close(tree);
3715 free(tree_id);
3716 return err;
3719 const struct got_error *
3720 got_worktree_revert(struct got_worktree *worktree,
3721 struct got_pathlist_head *paths,
3722 got_worktree_checkout_cb progress_cb, void *progress_arg,
3723 got_worktree_patch_cb patch_cb, void *patch_arg,
3724 struct got_repository *repo)
3726 struct got_fileindex *fileindex = NULL;
3727 char *fileindex_path = NULL;
3728 const struct got_error *err = NULL, *unlockerr = NULL;
3729 const struct got_error *sync_err = NULL;
3730 struct got_pathlist_entry *pe;
3731 struct revert_file_args rfa;
3733 err = lock_worktree(worktree, LOCK_EX);
3734 if (err)
3735 return err;
3737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3738 if (err)
3739 goto done;
3741 rfa.worktree = worktree;
3742 rfa.fileindex = fileindex;
3743 rfa.progress_cb = progress_cb;
3744 rfa.progress_arg = progress_arg;
3745 rfa.patch_cb = patch_cb;
3746 rfa.patch_arg = patch_arg;
3747 rfa.repo = repo;
3748 TAILQ_FOREACH(pe, paths, entry) {
3749 err = worktree_status(worktree, pe->path, fileindex, repo,
3750 revert_file, &rfa, NULL, NULL, 0, 0);
3751 if (err)
3752 break;
3754 sync_err = sync_fileindex(fileindex, fileindex_path);
3755 if (sync_err && err == NULL)
3756 err = sync_err;
3757 done:
3758 free(fileindex_path);
3759 if (fileindex)
3760 got_fileindex_free(fileindex);
3761 unlockerr = lock_worktree(worktree, LOCK_SH);
3762 if (unlockerr && err == NULL)
3763 err = unlockerr;
3764 return err;
3767 static void
3768 free_commitable(struct got_commitable *ct)
3770 free(ct->path);
3771 free(ct->in_repo_path);
3772 free(ct->ondisk_path);
3773 free(ct->blob_id);
3774 free(ct->base_blob_id);
3775 free(ct->staged_blob_id);
3776 free(ct->base_commit_id);
3777 free(ct);
3780 struct collect_commitables_arg {
3781 struct got_pathlist_head *commitable_paths;
3782 struct got_repository *repo;
3783 struct got_worktree *worktree;
3784 int have_staged_files;
3787 static const struct got_error *
3788 collect_commitables(void *arg, unsigned char status,
3789 unsigned char staged_status, const char *relpath,
3790 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3791 struct got_object_id *commit_id, int dirfd, const char *de_name)
3793 struct collect_commitables_arg *a = arg;
3794 const struct got_error *err = NULL;
3795 struct got_commitable *ct = NULL;
3796 struct got_pathlist_entry *new = NULL;
3797 char *parent_path = NULL, *path = NULL;
3798 struct stat sb;
3800 if (a->have_staged_files) {
3801 if (staged_status != GOT_STATUS_MODIFY &&
3802 staged_status != GOT_STATUS_ADD &&
3803 staged_status != GOT_STATUS_DELETE)
3804 return NULL;
3805 } else {
3806 if (status == GOT_STATUS_CONFLICT)
3807 return got_error(GOT_ERR_COMMIT_CONFLICT);
3809 if (status != GOT_STATUS_MODIFY &&
3810 status != GOT_STATUS_MODE_CHANGE &&
3811 status != GOT_STATUS_ADD &&
3812 status != GOT_STATUS_DELETE)
3813 return NULL;
3816 if (asprintf(&path, "/%s", relpath) == -1) {
3817 err = got_error_from_errno("asprintf");
3818 goto done;
3820 if (strcmp(path, "/") == 0) {
3821 parent_path = strdup("");
3822 if (parent_path == NULL)
3823 return got_error_from_errno("strdup");
3824 } else {
3825 err = got_path_dirname(&parent_path, path);
3826 if (err)
3827 return err;
3830 ct = calloc(1, sizeof(*ct));
3831 if (ct == NULL) {
3832 err = got_error_from_errno("calloc");
3833 goto done;
3836 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3837 relpath) == -1) {
3838 err = got_error_from_errno("asprintf");
3839 goto done;
3841 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3842 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3843 } else {
3844 if (dirfd != -1) {
3845 if (fstatat(dirfd, de_name, &sb,
3846 AT_SYMLINK_NOFOLLOW) == -1) {
3847 err = got_error_from_errno2("fstatat",
3848 ct->ondisk_path);
3849 goto done;
3851 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3852 err = got_error_from_errno2("lstat", ct->ondisk_path);
3853 goto done;
3855 ct->mode = sb.st_mode;
3858 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3859 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3860 relpath) == -1) {
3861 err = got_error_from_errno("asprintf");
3862 goto done;
3865 ct->status = status;
3866 ct->staged_status = staged_status;
3867 ct->blob_id = NULL; /* will be filled in when blob gets created */
3868 if (ct->status != GOT_STATUS_ADD &&
3869 ct->staged_status != GOT_STATUS_ADD) {
3870 ct->base_blob_id = got_object_id_dup(blob_id);
3871 if (ct->base_blob_id == NULL) {
3872 err = got_error_from_errno("got_object_id_dup");
3873 goto done;
3875 ct->base_commit_id = got_object_id_dup(commit_id);
3876 if (ct->base_commit_id == NULL) {
3877 err = got_error_from_errno("got_object_id_dup");
3878 goto done;
3881 if (ct->staged_status == GOT_STATUS_ADD ||
3882 ct->staged_status == GOT_STATUS_MODIFY) {
3883 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3884 if (ct->staged_blob_id == NULL) {
3885 err = got_error_from_errno("got_object_id_dup");
3886 goto done;
3889 ct->path = strdup(path);
3890 if (ct->path == NULL) {
3891 err = got_error_from_errno("strdup");
3892 goto done;
3894 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3895 done:
3896 if (ct && (err || new == NULL))
3897 free_commitable(ct);
3898 free(parent_path);
3899 free(path);
3900 return err;
3903 static const struct got_error *write_tree(struct got_object_id **, int *,
3904 struct got_tree_object *, const char *, struct got_pathlist_head *,
3905 got_worktree_status_cb status_cb, void *status_arg,
3906 struct got_repository *);
3908 static const struct got_error *
3909 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
3910 struct got_tree_entry *te, const char *parent_path,
3911 struct got_pathlist_head *commitable_paths,
3912 got_worktree_status_cb status_cb, void *status_arg,
3913 struct got_repository *repo)
3915 const struct got_error *err = NULL;
3916 struct got_tree_object *subtree;
3917 char *subpath;
3919 if (asprintf(&subpath, "%s%s%s", parent_path,
3920 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3921 return got_error_from_errno("asprintf");
3923 err = got_object_open_as_tree(&subtree, repo, &te->id);
3924 if (err)
3925 return err;
3927 err = write_tree(new_subtree_id, nentries, subtree, subpath,
3928 commitable_paths, status_cb, status_arg, repo);
3929 got_object_tree_close(subtree);
3930 free(subpath);
3931 return err;
3934 static const struct got_error *
3935 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3937 const struct got_error *err = NULL;
3938 char *ct_parent_path = NULL;
3940 *match = 0;
3942 if (strchr(ct->in_repo_path, '/') == NULL) {
3943 *match = got_path_is_root_dir(path);
3944 return NULL;
3947 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3948 if (err)
3949 return err;
3950 *match = (strcmp(path, ct_parent_path) == 0);
3951 free(ct_parent_path);
3952 return err;
3955 static mode_t
3956 get_ct_file_mode(struct got_commitable *ct)
3958 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3961 static const struct got_error *
3962 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3963 struct got_tree_entry *te, struct got_commitable *ct)
3965 const struct got_error *err = NULL;
3967 *new_te = NULL;
3969 err = got_object_tree_entry_dup(new_te, te);
3970 if (err)
3971 goto done;
3973 (*new_te)->mode = get_ct_file_mode(ct);
3975 if (ct->staged_status == GOT_STATUS_MODIFY)
3976 memcpy(&(*new_te)->id, ct->staged_blob_id,
3977 sizeof((*new_te)->id));
3978 else
3979 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3980 done:
3981 if (err && *new_te) {
3982 free(*new_te);
3983 *new_te = NULL;
3985 return err;
3988 static const struct got_error *
3989 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3990 struct got_commitable *ct)
3992 const struct got_error *err = NULL;
3993 char *ct_name;
3995 *new_te = NULL;
3997 *new_te = calloc(1, sizeof(**new_te));
3998 if (*new_te == NULL)
3999 return got_error_from_errno("calloc");
4001 ct_name = basename(ct->path);
4002 if (ct_name == NULL) {
4003 err = got_error_from_errno2("basename", ct->path);
4004 goto done;
4006 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4007 sizeof((*new_te)->name)) {
4008 err = got_error(GOT_ERR_NO_SPACE);
4009 goto done;
4012 (*new_te)->mode = get_ct_file_mode(ct);
4014 if (ct->staged_status == GOT_STATUS_ADD)
4015 memcpy(&(*new_te)->id, ct->staged_blob_id,
4016 sizeof((*new_te)->id));
4017 else
4018 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4019 done:
4020 if (err && *new_te) {
4021 free(*new_te);
4022 *new_te = NULL;
4024 return err;
4027 static const struct got_error *
4028 insert_tree_entry(struct got_tree_entry *new_te,
4029 struct got_pathlist_head *paths)
4031 const struct got_error *err = NULL;
4032 struct got_pathlist_entry *new_pe;
4034 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4035 if (err)
4036 return err;
4037 if (new_pe == NULL)
4038 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4039 return NULL;
4042 static const struct got_error *
4043 report_ct_status(struct got_commitable *ct,
4044 got_worktree_status_cb status_cb, void *status_arg)
4046 const char *ct_path = ct->path;
4047 unsigned char status;
4049 while (ct_path[0] == '/')
4050 ct_path++;
4052 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4053 status = ct->staged_status;
4054 else
4055 status = ct->status;
4057 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4058 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4061 static const struct got_error *
4062 match_modified_subtree(int *modified, struct got_tree_entry *te,
4063 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4065 const struct got_error *err = NULL;
4066 struct got_pathlist_entry *pe;
4067 char *te_path;
4069 *modified = 0;
4071 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4072 got_path_is_root_dir(base_tree_path) ? "" : "/",
4073 te->name) == -1)
4074 return got_error_from_errno("asprintf");
4076 TAILQ_FOREACH(pe, commitable_paths, entry) {
4077 struct got_commitable *ct = pe->data;
4078 *modified = got_path_is_child(ct->in_repo_path, te_path,
4079 strlen(te_path));
4080 if (*modified)
4081 break;
4084 free(te_path);
4085 return err;
4088 static const struct got_error *
4089 match_deleted_or_modified_ct(struct got_commitable **ctp,
4090 struct got_tree_entry *te, const char *base_tree_path,
4091 struct got_pathlist_head *commitable_paths)
4093 const struct got_error *err = NULL;
4094 struct got_pathlist_entry *pe;
4096 *ctp = NULL;
4098 TAILQ_FOREACH(pe, commitable_paths, entry) {
4099 struct got_commitable *ct = pe->data;
4100 char *ct_name = NULL;
4101 int path_matches;
4103 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4104 if (ct->status != GOT_STATUS_MODIFY &&
4105 ct->status != GOT_STATUS_MODE_CHANGE &&
4106 ct->status != GOT_STATUS_DELETE)
4107 continue;
4108 } else {
4109 if (ct->staged_status != GOT_STATUS_MODIFY &&
4110 ct->staged_status != GOT_STATUS_DELETE)
4111 continue;
4114 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4115 continue;
4117 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4118 if (err)
4119 return err;
4120 if (!path_matches)
4121 continue;
4123 ct_name = basename(pe->path);
4124 if (ct_name == NULL)
4125 return got_error_from_errno2("basename", pe->path);
4127 if (strcmp(te->name, ct_name) != 0)
4128 continue;
4130 *ctp = ct;
4131 break;
4134 return err;
4137 static const struct got_error *
4138 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4139 const char *child_path, const char *path_base_tree,
4140 struct got_pathlist_head *commitable_paths,
4141 got_worktree_status_cb status_cb, void *status_arg,
4142 struct got_repository *repo)
4144 const struct got_error *err = NULL;
4145 struct got_tree_entry *new_te;
4146 char *subtree_path;
4147 struct got_object_id *id = NULL;
4148 int nentries;
4150 *new_tep = NULL;
4152 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4153 got_path_is_root_dir(path_base_tree) ? "" : "/",
4154 child_path) == -1)
4155 return got_error_from_errno("asprintf");
4157 new_te = calloc(1, sizeof(*new_te));
4158 if (new_te == NULL)
4159 return got_error_from_errno("calloc");
4160 new_te->mode = S_IFDIR;
4162 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4163 sizeof(new_te->name)) {
4164 err = got_error(GOT_ERR_NO_SPACE);
4165 goto done;
4167 err = write_tree(&id, &nentries, NULL, subtree_path,
4168 commitable_paths, status_cb, status_arg, repo);
4169 if (err) {
4170 free(new_te);
4171 goto done;
4173 memcpy(&new_te->id, id, sizeof(new_te->id));
4174 done:
4175 free(id);
4176 free(subtree_path);
4177 if (err == NULL)
4178 *new_tep = new_te;
4179 return err;
4182 static const struct got_error *
4183 write_tree(struct got_object_id **new_tree_id, int *nentries,
4184 struct got_tree_object *base_tree, const char *path_base_tree,
4185 struct got_pathlist_head *commitable_paths,
4186 got_worktree_status_cb status_cb, void *status_arg,
4187 struct got_repository *repo)
4189 const struct got_error *err = NULL;
4190 struct got_pathlist_head paths;
4191 struct got_tree_entry *te, *new_te = NULL;
4192 struct got_pathlist_entry *pe;
4194 TAILQ_INIT(&paths);
4195 *nentries = 0;
4197 /* Insert, and recurse into, newly added entries first. */
4198 TAILQ_FOREACH(pe, commitable_paths, entry) {
4199 struct got_commitable *ct = pe->data;
4200 char *child_path = NULL, *slash;
4202 if ((ct->status != GOT_STATUS_ADD &&
4203 ct->staged_status != GOT_STATUS_ADD) ||
4204 (ct->flags & GOT_COMMITABLE_ADDED))
4205 continue;
4207 if (!got_path_is_child(pe->path, path_base_tree,
4208 strlen(path_base_tree)))
4209 continue;
4211 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4212 pe->path);
4213 if (err)
4214 goto done;
4216 slash = strchr(child_path, '/');
4217 if (slash == NULL) {
4218 err = alloc_added_blob_tree_entry(&new_te, ct);
4219 if (err)
4220 goto done;
4221 err = report_ct_status(ct, status_cb, status_arg);
4222 if (err)
4223 goto done;
4224 ct->flags |= GOT_COMMITABLE_ADDED;
4225 err = insert_tree_entry(new_te, &paths);
4226 if (err)
4227 goto done;
4228 (*nentries)++;
4229 } else {
4230 *slash = '\0'; /* trim trailing path components */
4231 if (base_tree == NULL ||
4232 got_object_tree_find_entry(base_tree, child_path)
4233 == NULL) {
4234 err = make_subtree_for_added_blob(&new_te,
4235 child_path, path_base_tree,
4236 commitable_paths, status_cb, status_arg,
4237 repo);
4238 if (err)
4239 goto done;
4240 err = insert_tree_entry(new_te, &paths);
4241 if (err)
4242 goto done;
4243 (*nentries)++;
4248 if (base_tree) {
4249 int i, nbase_entries;
4250 /* Handle modified and deleted entries. */
4251 nbase_entries = got_object_tree_get_nentries(base_tree);
4252 for (i = 0; i < nbase_entries; i++) {
4253 struct got_commitable *ct = NULL;
4255 te = got_object_tree_get_entry(base_tree, i);
4256 if (got_object_tree_entry_is_submodule(te)) {
4257 /* Entry is a submodule; just copy it. */
4258 err = got_object_tree_entry_dup(&new_te, te);
4259 if (err)
4260 goto done;
4261 err = insert_tree_entry(new_te, &paths);
4262 if (err)
4263 goto done;
4264 (*nentries)++;
4265 continue;
4268 if (S_ISDIR(te->mode)) {
4269 int modified;
4270 err = got_object_tree_entry_dup(&new_te, te);
4271 if (err)
4272 goto done;
4273 err = match_modified_subtree(&modified, te,
4274 path_base_tree, commitable_paths);
4275 if (err)
4276 goto done;
4277 /* Avoid recursion into unmodified subtrees. */
4278 if (modified) {
4279 struct got_object_id *new_id;
4280 int nsubentries;
4281 err = write_subtree(&new_id,
4282 &nsubentries, te,
4283 path_base_tree, commitable_paths,
4284 status_cb, status_arg, repo);
4285 if (err)
4286 goto done;
4287 if (nsubentries == 0) {
4288 /* All entries were deleted. */
4289 free(new_id);
4290 continue;
4292 memcpy(&new_te->id, new_id,
4293 sizeof(new_te->id));
4294 free(new_id);
4296 err = insert_tree_entry(new_te, &paths);
4297 if (err)
4298 goto done;
4299 (*nentries)++;
4300 continue;
4303 err = match_deleted_or_modified_ct(&ct, te,
4304 path_base_tree, commitable_paths);
4305 if (err)
4306 goto done;
4307 if (ct) {
4308 /* NB: Deleted entries get dropped here. */
4309 if (ct->status == GOT_STATUS_MODIFY ||
4310 ct->status == GOT_STATUS_MODE_CHANGE ||
4311 ct->staged_status == GOT_STATUS_MODIFY) {
4312 err = alloc_modified_blob_tree_entry(
4313 &new_te, te, ct);
4314 if (err)
4315 goto done;
4316 err = insert_tree_entry(new_te, &paths);
4317 if (err)
4318 goto done;
4319 (*nentries)++;
4321 err = report_ct_status(ct, status_cb,
4322 status_arg);
4323 if (err)
4324 goto done;
4325 } else {
4326 /* Entry is unchanged; just copy it. */
4327 err = got_object_tree_entry_dup(&new_te, te);
4328 if (err)
4329 goto done;
4330 err = insert_tree_entry(new_te, &paths);
4331 if (err)
4332 goto done;
4333 (*nentries)++;
4338 /* Write new list of entries; deleted entries have been dropped. */
4339 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4340 done:
4341 got_pathlist_free(&paths);
4342 return err;
4345 static const struct got_error *
4346 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4347 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4348 int have_staged_files)
4350 const struct got_error *err = NULL;
4351 struct got_pathlist_entry *pe;
4353 TAILQ_FOREACH(pe, commitable_paths, entry) {
4354 struct got_fileindex_entry *ie;
4355 struct got_commitable *ct = pe->data;
4357 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4358 if (ie) {
4359 if (ct->status == GOT_STATUS_DELETE ||
4360 ct->staged_status == GOT_STATUS_DELETE) {
4361 got_fileindex_entry_remove(fileindex, ie);
4362 } else if (ct->staged_status == GOT_STATUS_ADD ||
4363 ct->staged_status == GOT_STATUS_MODIFY) {
4364 got_fileindex_entry_stage_set(ie,
4365 GOT_FILEIDX_STAGE_NONE);
4366 err = got_fileindex_entry_update(ie,
4367 ct->ondisk_path, ct->staged_blob_id->sha1,
4368 new_base_commit_id->sha1,
4369 !have_staged_files);
4370 } else
4371 err = got_fileindex_entry_update(ie,
4372 ct->ondisk_path, ct->blob_id->sha1,
4373 new_base_commit_id->sha1,
4374 !have_staged_files);
4375 } else {
4376 err = got_fileindex_entry_alloc(&ie, pe->path);
4377 if (err)
4378 break;
4379 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4380 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4381 if (err) {
4382 got_fileindex_entry_free(ie);
4383 break;
4385 err = got_fileindex_entry_add(fileindex, ie);
4386 if (err) {
4387 got_fileindex_entry_free(ie);
4388 break;
4392 return err;
4396 static const struct got_error *
4397 check_out_of_date(const char *in_repo_path, unsigned char status,
4398 unsigned char staged_status, struct got_object_id *base_blob_id,
4399 struct got_object_id *base_commit_id,
4400 struct got_object_id *head_commit_id, struct got_repository *repo,
4401 int ood_errcode)
4403 const struct got_error *err = NULL;
4404 struct got_object_id *id = NULL;
4406 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4407 /* Trivial case: base commit == head commit */
4408 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4409 return NULL;
4411 * Ensure file content which local changes were based
4412 * on matches file content in the branch head.
4414 err = got_object_id_by_path(&id, repo, head_commit_id,
4415 in_repo_path);
4416 if (err) {
4417 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4418 err = got_error(ood_errcode);
4419 goto done;
4420 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4421 err = got_error(ood_errcode);
4422 } else {
4423 /* Require that added files don't exist in the branch head. */
4424 err = got_object_id_by_path(&id, repo, head_commit_id,
4425 in_repo_path);
4426 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4427 goto done;
4428 err = id ? got_error(ood_errcode) : NULL;
4430 done:
4431 free(id);
4432 return err;
4435 const struct got_error *
4436 commit_worktree(struct got_object_id **new_commit_id,
4437 struct got_pathlist_head *commitable_paths,
4438 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4439 const char *author, const char *committer,
4440 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4441 got_worktree_status_cb status_cb, void *status_arg,
4442 struct got_repository *repo)
4444 const struct got_error *err = NULL, *unlockerr = NULL;
4445 struct got_pathlist_entry *pe;
4446 const char *head_ref_name = NULL;
4447 struct got_commit_object *head_commit = NULL;
4448 struct got_reference *head_ref2 = NULL;
4449 struct got_object_id *head_commit_id2 = NULL;
4450 struct got_tree_object *head_tree = NULL;
4451 struct got_object_id *new_tree_id = NULL;
4452 int nentries;
4453 struct got_object_id_queue parent_ids;
4454 struct got_object_qid *pid = NULL;
4455 char *logmsg = NULL;
4457 *new_commit_id = NULL;
4459 SIMPLEQ_INIT(&parent_ids);
4461 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4462 if (err)
4463 goto done;
4465 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4466 if (err)
4467 goto done;
4469 if (commit_msg_cb != NULL) {
4470 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4471 if (err)
4472 goto done;
4475 if (logmsg == NULL || strlen(logmsg) == 0) {
4476 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4477 goto done;
4480 /* Create blobs from added and modified files and record their IDs. */
4481 TAILQ_FOREACH(pe, commitable_paths, entry) {
4482 struct got_commitable *ct = pe->data;
4483 char *ondisk_path;
4485 /* Blobs for staged files already exist. */
4486 if (ct->staged_status == GOT_STATUS_ADD ||
4487 ct->staged_status == GOT_STATUS_MODIFY)
4488 continue;
4490 if (ct->status != GOT_STATUS_ADD &&
4491 ct->status != GOT_STATUS_MODIFY &&
4492 ct->status != GOT_STATUS_MODE_CHANGE)
4493 continue;
4495 if (asprintf(&ondisk_path, "%s/%s",
4496 worktree->root_path, pe->path) == -1) {
4497 err = got_error_from_errno("asprintf");
4498 goto done;
4500 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4501 free(ondisk_path);
4502 if (err)
4503 goto done;
4506 /* Recursively write new tree objects. */
4507 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4508 commitable_paths, status_cb, status_arg, repo);
4509 if (err)
4510 goto done;
4512 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4513 if (err)
4514 goto done;
4515 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4516 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4517 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4518 got_object_qid_free(pid);
4519 if (logmsg != NULL)
4520 free(logmsg);
4521 if (err)
4522 goto done;
4524 /* Check if a concurrent commit to our branch has occurred. */
4525 head_ref_name = got_worktree_get_head_ref_name(worktree);
4526 if (head_ref_name == NULL) {
4527 err = got_error_from_errno("got_worktree_get_head_ref_name");
4528 goto done;
4530 /* Lock the reference here to prevent concurrent modification. */
4531 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4532 if (err)
4533 goto done;
4534 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4535 if (err)
4536 goto done;
4537 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4538 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4539 goto done;
4541 /* Update branch head in repository. */
4542 err = got_ref_change_ref(head_ref2, *new_commit_id);
4543 if (err)
4544 goto done;
4545 err = got_ref_write(head_ref2, repo);
4546 if (err)
4547 goto done;
4549 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4550 if (err)
4551 goto done;
4553 err = ref_base_commit(worktree, repo);
4554 if (err)
4555 goto done;
4556 done:
4557 if (head_tree)
4558 got_object_tree_close(head_tree);
4559 if (head_commit)
4560 got_object_commit_close(head_commit);
4561 free(head_commit_id2);
4562 if (head_ref2) {
4563 unlockerr = got_ref_unlock(head_ref2);
4564 if (unlockerr && err == NULL)
4565 err = unlockerr;
4566 got_ref_close(head_ref2);
4568 return err;
4571 static const struct got_error *
4572 check_path_is_commitable(const char *path,
4573 struct got_pathlist_head *commitable_paths)
4575 struct got_pathlist_entry *cpe = NULL;
4576 size_t path_len = strlen(path);
4578 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4579 struct got_commitable *ct = cpe->data;
4580 const char *ct_path = ct->path;
4582 while (ct_path[0] == '/')
4583 ct_path++;
4585 if (strcmp(path, ct_path) == 0 ||
4586 got_path_is_child(ct_path, path, path_len))
4587 break;
4590 if (cpe == NULL)
4591 return got_error_path(path, GOT_ERR_BAD_PATH);
4593 return NULL;
4596 static const struct got_error *
4597 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4599 int *have_staged_files = arg;
4601 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4602 *have_staged_files = 1;
4603 return got_error(GOT_ERR_CANCELLED);
4606 return NULL;
4609 static const struct got_error *
4610 check_non_staged_files(struct got_fileindex *fileindex,
4611 struct got_pathlist_head *paths)
4613 struct got_pathlist_entry *pe;
4614 struct got_fileindex_entry *ie;
4616 TAILQ_FOREACH(pe, paths, entry) {
4617 if (pe->path[0] == '\0')
4618 continue;
4619 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4620 if (ie == NULL)
4621 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4622 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4623 return got_error_path(pe->path,
4624 GOT_ERR_FILE_NOT_STAGED);
4627 return NULL;
4630 const struct got_error *
4631 got_worktree_commit(struct got_object_id **new_commit_id,
4632 struct got_worktree *worktree, struct got_pathlist_head *paths,
4633 const char *author, const char *committer,
4634 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4635 got_worktree_status_cb status_cb, void *status_arg,
4636 struct got_repository *repo)
4638 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4639 struct got_fileindex *fileindex = NULL;
4640 char *fileindex_path = NULL;
4641 struct got_pathlist_head commitable_paths;
4642 struct collect_commitables_arg cc_arg;
4643 struct got_pathlist_entry *pe;
4644 struct got_reference *head_ref = NULL;
4645 struct got_object_id *head_commit_id = NULL;
4646 int have_staged_files = 0;
4648 *new_commit_id = NULL;
4650 TAILQ_INIT(&commitable_paths);
4652 err = lock_worktree(worktree, LOCK_EX);
4653 if (err)
4654 goto done;
4656 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4657 if (err)
4658 goto done;
4660 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4661 if (err)
4662 goto done;
4664 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4665 if (err)
4666 goto done;
4668 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4669 &have_staged_files);
4670 if (err && err->code != GOT_ERR_CANCELLED)
4671 goto done;
4672 if (have_staged_files) {
4673 err = check_non_staged_files(fileindex, paths);
4674 if (err)
4675 goto done;
4678 cc_arg.commitable_paths = &commitable_paths;
4679 cc_arg.worktree = worktree;
4680 cc_arg.repo = repo;
4681 cc_arg.have_staged_files = have_staged_files;
4682 TAILQ_FOREACH(pe, paths, entry) {
4683 err = worktree_status(worktree, pe->path, fileindex, repo,
4684 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4685 if (err)
4686 goto done;
4689 if (TAILQ_EMPTY(&commitable_paths)) {
4690 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4691 goto done;
4694 TAILQ_FOREACH(pe, paths, entry) {
4695 err = check_path_is_commitable(pe->path, &commitable_paths);
4696 if (err)
4697 goto done;
4700 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4701 struct got_commitable *ct = pe->data;
4702 const char *ct_path = ct->in_repo_path;
4704 while (ct_path[0] == '/')
4705 ct_path++;
4706 err = check_out_of_date(ct_path, ct->status,
4707 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4708 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4709 if (err)
4710 goto done;
4714 err = commit_worktree(new_commit_id, &commitable_paths,
4715 head_commit_id, worktree, author, committer,
4716 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4717 if (err)
4718 goto done;
4720 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4721 fileindex, have_staged_files);
4722 sync_err = sync_fileindex(fileindex, fileindex_path);
4723 if (sync_err && err == NULL)
4724 err = sync_err;
4725 done:
4726 if (fileindex)
4727 got_fileindex_free(fileindex);
4728 free(fileindex_path);
4729 unlockerr = lock_worktree(worktree, LOCK_SH);
4730 if (unlockerr && err == NULL)
4731 err = unlockerr;
4732 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4733 struct got_commitable *ct = pe->data;
4734 free_commitable(ct);
4736 got_pathlist_free(&commitable_paths);
4737 return err;
4740 const char *
4741 got_commitable_get_path(struct got_commitable *ct)
4743 return ct->path;
4746 unsigned int
4747 got_commitable_get_status(struct got_commitable *ct)
4749 return ct->status;
4752 struct check_rebase_ok_arg {
4753 struct got_worktree *worktree;
4754 struct got_repository *repo;
4757 static const struct got_error *
4758 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4760 const struct got_error *err = NULL;
4761 struct check_rebase_ok_arg *a = arg;
4762 unsigned char status;
4763 struct stat sb;
4764 char *ondisk_path;
4766 /* Reject rebase of a work tree with mixed base commits. */
4767 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4768 SHA1_DIGEST_LENGTH))
4769 return got_error(GOT_ERR_MIXED_COMMITS);
4771 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4772 == -1)
4773 return got_error_from_errno("asprintf");
4775 /* Reject rebase of a work tree with modified or staged files. */
4776 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4777 free(ondisk_path);
4778 if (err)
4779 return err;
4781 if (status != GOT_STATUS_NO_CHANGE)
4782 return got_error(GOT_ERR_MODIFIED);
4783 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4784 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4786 return NULL;
4789 const struct got_error *
4790 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4791 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4792 struct got_worktree *worktree, struct got_reference *branch,
4793 struct got_repository *repo)
4795 const struct got_error *err = NULL;
4796 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4797 char *branch_ref_name = NULL;
4798 char *fileindex_path = NULL;
4799 struct check_rebase_ok_arg ok_arg;
4800 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4801 struct got_object_id *wt_branch_tip = NULL;
4803 *new_base_branch_ref = NULL;
4804 *tmp_branch = NULL;
4805 *fileindex = NULL;
4807 err = lock_worktree(worktree, LOCK_EX);
4808 if (err)
4809 return err;
4811 err = open_fileindex(fileindex, &fileindex_path, worktree);
4812 if (err)
4813 goto done;
4815 ok_arg.worktree = worktree;
4816 ok_arg.repo = repo;
4817 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4818 &ok_arg);
4819 if (err)
4820 goto done;
4822 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4823 if (err)
4824 goto done;
4826 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4827 if (err)
4828 goto done;
4830 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4831 if (err)
4832 goto done;
4834 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4835 0);
4836 if (err)
4837 goto done;
4839 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4840 if (err)
4841 goto done;
4842 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4843 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4844 goto done;
4847 err = got_ref_alloc_symref(new_base_branch_ref,
4848 new_base_branch_ref_name, wt_branch);
4849 if (err)
4850 goto done;
4851 err = got_ref_write(*new_base_branch_ref, repo);
4852 if (err)
4853 goto done;
4855 /* TODO Lock original branch's ref while rebasing? */
4857 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4858 if (err)
4859 goto done;
4861 err = got_ref_write(branch_ref, repo);
4862 if (err)
4863 goto done;
4865 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4866 worktree->base_commit_id);
4867 if (err)
4868 goto done;
4869 err = got_ref_write(*tmp_branch, repo);
4870 if (err)
4871 goto done;
4873 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4874 if (err)
4875 goto done;
4876 done:
4877 free(fileindex_path);
4878 free(tmp_branch_name);
4879 free(new_base_branch_ref_name);
4880 free(branch_ref_name);
4881 if (branch_ref)
4882 got_ref_close(branch_ref);
4883 if (wt_branch)
4884 got_ref_close(wt_branch);
4885 free(wt_branch_tip);
4886 if (err) {
4887 if (*new_base_branch_ref) {
4888 got_ref_close(*new_base_branch_ref);
4889 *new_base_branch_ref = NULL;
4891 if (*tmp_branch) {
4892 got_ref_close(*tmp_branch);
4893 *tmp_branch = NULL;
4895 if (*fileindex) {
4896 got_fileindex_free(*fileindex);
4897 *fileindex = NULL;
4899 lock_worktree(worktree, LOCK_SH);
4901 return err;
4904 const struct got_error *
4905 got_worktree_rebase_continue(struct got_object_id **commit_id,
4906 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4907 struct got_reference **branch, struct got_fileindex **fileindex,
4908 struct got_worktree *worktree, struct got_repository *repo)
4910 const struct got_error *err;
4911 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4912 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4913 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4914 char *fileindex_path = NULL;
4915 int have_staged_files = 0;
4917 *commit_id = NULL;
4918 *new_base_branch = NULL;
4919 *tmp_branch = NULL;
4920 *branch = NULL;
4921 *fileindex = NULL;
4923 err = lock_worktree(worktree, LOCK_EX);
4924 if (err)
4925 return err;
4927 err = open_fileindex(fileindex, &fileindex_path, worktree);
4928 if (err)
4929 goto done;
4931 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4932 &have_staged_files);
4933 if (err && err->code != GOT_ERR_CANCELLED)
4934 goto done;
4935 if (have_staged_files) {
4936 err = got_error(GOT_ERR_STAGED_PATHS);
4937 goto done;
4940 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4941 if (err)
4942 goto done;
4944 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4945 if (err)
4946 goto done;
4948 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4949 if (err)
4950 goto done;
4952 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4953 if (err)
4954 goto done;
4956 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4957 if (err)
4958 goto done;
4960 err = got_ref_open(branch, repo,
4961 got_ref_get_symref_target(branch_ref), 0);
4962 if (err)
4963 goto done;
4965 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4966 if (err)
4967 goto done;
4969 err = got_ref_resolve(commit_id, repo, commit_ref);
4970 if (err)
4971 goto done;
4973 err = got_ref_open(new_base_branch, repo,
4974 new_base_branch_ref_name, 0);
4975 if (err)
4976 goto done;
4978 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4979 if (err)
4980 goto done;
4981 done:
4982 free(commit_ref_name);
4983 free(branch_ref_name);
4984 free(fileindex_path);
4985 if (commit_ref)
4986 got_ref_close(commit_ref);
4987 if (branch_ref)
4988 got_ref_close(branch_ref);
4989 if (err) {
4990 free(*commit_id);
4991 *commit_id = NULL;
4992 if (*tmp_branch) {
4993 got_ref_close(*tmp_branch);
4994 *tmp_branch = NULL;
4996 if (*new_base_branch) {
4997 got_ref_close(*new_base_branch);
4998 *new_base_branch = NULL;
5000 if (*branch) {
5001 got_ref_close(*branch);
5002 *branch = NULL;
5004 if (*fileindex) {
5005 got_fileindex_free(*fileindex);
5006 *fileindex = NULL;
5008 lock_worktree(worktree, LOCK_SH);
5010 return err;
5013 const struct got_error *
5014 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5016 const struct got_error *err;
5017 char *tmp_branch_name = NULL;
5019 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5020 if (err)
5021 return err;
5023 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5024 free(tmp_branch_name);
5025 return NULL;
5028 static const struct got_error *
5029 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5030 char **logmsg, void *arg)
5032 *logmsg = arg;
5033 return NULL;
5036 static const struct got_error *
5037 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5038 const char *path, struct got_object_id *blob_id,
5039 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5040 int dirfd, const char *de_name)
5042 return NULL;
5045 struct collect_merged_paths_arg {
5046 got_worktree_checkout_cb progress_cb;
5047 void *progress_arg;
5048 struct got_pathlist_head *merged_paths;
5051 static const struct got_error *
5052 collect_merged_paths(void *arg, unsigned char status, const char *path)
5054 const struct got_error *err;
5055 struct collect_merged_paths_arg *a = arg;
5056 char *p;
5057 struct got_pathlist_entry *new;
5059 err = (*a->progress_cb)(a->progress_arg, status, path);
5060 if (err)
5061 return err;
5063 if (status != GOT_STATUS_MERGE &&
5064 status != GOT_STATUS_ADD &&
5065 status != GOT_STATUS_DELETE &&
5066 status != GOT_STATUS_CONFLICT)
5067 return NULL;
5069 p = strdup(path);
5070 if (p == NULL)
5071 return got_error_from_errno("strdup");
5073 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5074 if (err || new == NULL)
5075 free(p);
5076 return err;
5079 void
5080 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5082 struct got_pathlist_entry *pe;
5084 TAILQ_FOREACH(pe, merged_paths, entry)
5085 free((char *)pe->path);
5087 got_pathlist_free(merged_paths);
5090 static const struct got_error *
5091 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5092 int is_rebase, struct got_repository *repo)
5094 const struct got_error *err;
5095 struct got_reference *commit_ref = NULL;
5097 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5098 if (err) {
5099 if (err->code != GOT_ERR_NOT_REF)
5100 goto done;
5101 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5102 if (err)
5103 goto done;
5104 err = got_ref_write(commit_ref, repo);
5105 if (err)
5106 goto done;
5107 } else if (is_rebase) {
5108 struct got_object_id *stored_id;
5109 int cmp;
5111 err = got_ref_resolve(&stored_id, repo, commit_ref);
5112 if (err)
5113 goto done;
5114 cmp = got_object_id_cmp(commit_id, stored_id);
5115 free(stored_id);
5116 if (cmp != 0) {
5117 err = got_error(GOT_ERR_REBASE_COMMITID);
5118 goto done;
5121 done:
5122 if (commit_ref)
5123 got_ref_close(commit_ref);
5124 return err;
5127 static const struct got_error *
5128 rebase_merge_files(struct got_pathlist_head *merged_paths,
5129 const char *commit_ref_name, struct got_worktree *worktree,
5130 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5131 struct got_object_id *commit_id, struct got_repository *repo,
5132 got_worktree_checkout_cb progress_cb, void *progress_arg,
5133 got_cancel_cb cancel_cb, void *cancel_arg)
5135 const struct got_error *err;
5136 struct got_reference *commit_ref = NULL;
5137 struct collect_merged_paths_arg cmp_arg;
5138 char *fileindex_path;
5140 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5142 err = get_fileindex_path(&fileindex_path, worktree);
5143 if (err)
5144 return err;
5146 cmp_arg.progress_cb = progress_cb;
5147 cmp_arg.progress_arg = progress_arg;
5148 cmp_arg.merged_paths = merged_paths;
5149 err = merge_files(worktree, fileindex, fileindex_path,
5150 parent_commit_id, commit_id, repo, collect_merged_paths,
5151 &cmp_arg, cancel_cb, cancel_arg);
5152 if (commit_ref)
5153 got_ref_close(commit_ref);
5154 return err;
5157 const struct got_error *
5158 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5159 struct got_worktree *worktree, struct got_fileindex *fileindex,
5160 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5161 struct got_repository *repo,
5162 got_worktree_checkout_cb progress_cb, void *progress_arg,
5163 got_cancel_cb cancel_cb, void *cancel_arg)
5165 const struct got_error *err;
5166 char *commit_ref_name;
5168 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5169 if (err)
5170 return err;
5172 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5173 if (err)
5174 goto done;
5176 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5177 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5178 progress_arg, cancel_cb, cancel_arg);
5179 done:
5180 free(commit_ref_name);
5181 return err;
5184 const struct got_error *
5185 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5186 struct got_worktree *worktree, struct got_fileindex *fileindex,
5187 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5188 struct got_repository *repo,
5189 got_worktree_checkout_cb progress_cb, void *progress_arg,
5190 got_cancel_cb cancel_cb, void *cancel_arg)
5192 const struct got_error *err;
5193 char *commit_ref_name;
5195 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5196 if (err)
5197 return err;
5199 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5200 if (err)
5201 goto done;
5203 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5204 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5205 progress_arg, cancel_cb, cancel_arg);
5206 done:
5207 free(commit_ref_name);
5208 return err;
5211 static const struct got_error *
5212 rebase_commit(struct got_object_id **new_commit_id,
5213 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5214 struct got_worktree *worktree, struct got_fileindex *fileindex,
5215 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5216 const char *new_logmsg, struct got_repository *repo)
5218 const struct got_error *err, *sync_err;
5219 struct got_pathlist_head commitable_paths;
5220 struct collect_commitables_arg cc_arg;
5221 char *fileindex_path = NULL;
5222 struct got_reference *head_ref = NULL;
5223 struct got_object_id *head_commit_id = NULL;
5224 char *logmsg = NULL;
5226 TAILQ_INIT(&commitable_paths);
5227 *new_commit_id = NULL;
5229 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5231 err = get_fileindex_path(&fileindex_path, worktree);
5232 if (err)
5233 return err;
5235 cc_arg.commitable_paths = &commitable_paths;
5236 cc_arg.worktree = worktree;
5237 cc_arg.repo = repo;
5238 cc_arg.have_staged_files = 0;
5240 * If possible get the status of individual files directly to
5241 * avoid crawling the entire work tree once per rebased commit.
5242 * TODO: Ideally, merged_paths would contain a list of commitables
5243 * we could use so we could skip worktree_status() entirely.
5245 if (merged_paths) {
5246 struct got_pathlist_entry *pe;
5247 TAILQ_FOREACH(pe, merged_paths, entry) {
5248 err = worktree_status(worktree, pe->path, fileindex,
5249 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5250 0);
5251 if (err)
5252 goto done;
5254 } else {
5255 err = worktree_status(worktree, "", fileindex, repo,
5256 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5257 if (err)
5258 goto done;
5261 if (TAILQ_EMPTY(&commitable_paths)) {
5262 /* No-op change; commit will be elided. */
5263 err = got_ref_delete(commit_ref, repo);
5264 if (err)
5265 goto done;
5266 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5267 goto done;
5270 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5271 if (err)
5272 goto done;
5274 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5275 if (err)
5276 goto done;
5278 if (new_logmsg) {
5279 logmsg = strdup(new_logmsg);
5280 if (logmsg == NULL) {
5281 err = got_error_from_errno("strdup");
5282 goto done;
5284 } else {
5285 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5286 if (err)
5287 goto done;
5290 /* NB: commit_worktree will call free(logmsg) */
5291 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5292 worktree, got_object_commit_get_author(orig_commit),
5293 got_object_commit_get_committer(orig_commit),
5294 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5295 if (err)
5296 goto done;
5298 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5299 if (err)
5300 goto done;
5302 err = got_ref_delete(commit_ref, repo);
5303 if (err)
5304 goto done;
5306 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5307 fileindex, 0);
5308 sync_err = sync_fileindex(fileindex, fileindex_path);
5309 if (sync_err && err == NULL)
5310 err = sync_err;
5311 done:
5312 free(fileindex_path);
5313 free(head_commit_id);
5314 if (head_ref)
5315 got_ref_close(head_ref);
5316 if (err) {
5317 free(*new_commit_id);
5318 *new_commit_id = NULL;
5320 return err;
5323 const struct got_error *
5324 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5325 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5326 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5327 struct got_commit_object *orig_commit,
5328 struct got_object_id *orig_commit_id, struct got_repository *repo)
5330 const struct got_error *err;
5331 char *commit_ref_name;
5332 struct got_reference *commit_ref = NULL;
5333 struct got_object_id *commit_id = NULL;
5335 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5336 if (err)
5337 return err;
5339 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5340 if (err)
5341 goto done;
5342 err = got_ref_resolve(&commit_id, repo, commit_ref);
5343 if (err)
5344 goto done;
5345 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5346 err = got_error(GOT_ERR_REBASE_COMMITID);
5347 goto done;
5350 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5351 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5352 done:
5353 if (commit_ref)
5354 got_ref_close(commit_ref);
5355 free(commit_ref_name);
5356 free(commit_id);
5357 return err;
5360 const struct got_error *
5361 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5362 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5363 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5364 struct got_commit_object *orig_commit,
5365 struct got_object_id *orig_commit_id, const char *new_logmsg,
5366 struct got_repository *repo)
5368 const struct got_error *err;
5369 char *commit_ref_name;
5370 struct got_reference *commit_ref = NULL;
5372 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5373 if (err)
5374 return err;
5376 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5377 if (err)
5378 goto done;
5380 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5381 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5382 done:
5383 if (commit_ref)
5384 got_ref_close(commit_ref);
5385 free(commit_ref_name);
5386 return err;
5389 const struct got_error *
5390 got_worktree_rebase_postpone(struct got_worktree *worktree,
5391 struct got_fileindex *fileindex)
5393 if (fileindex)
5394 got_fileindex_free(fileindex);
5395 return lock_worktree(worktree, LOCK_SH);
5398 static const struct got_error *
5399 delete_ref(const char *name, struct got_repository *repo)
5401 const struct got_error *err;
5402 struct got_reference *ref;
5404 err = got_ref_open(&ref, repo, name, 0);
5405 if (err) {
5406 if (err->code == GOT_ERR_NOT_REF)
5407 return NULL;
5408 return err;
5411 err = got_ref_delete(ref, repo);
5412 got_ref_close(ref);
5413 return err;
5416 static const struct got_error *
5417 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5419 const struct got_error *err;
5420 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5421 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5423 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5424 if (err)
5425 goto done;
5426 err = delete_ref(tmp_branch_name, repo);
5427 if (err)
5428 goto done;
5430 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5431 if (err)
5432 goto done;
5433 err = delete_ref(new_base_branch_ref_name, repo);
5434 if (err)
5435 goto done;
5437 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5438 if (err)
5439 goto done;
5440 err = delete_ref(branch_ref_name, repo);
5441 if (err)
5442 goto done;
5444 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5445 if (err)
5446 goto done;
5447 err = delete_ref(commit_ref_name, repo);
5448 if (err)
5449 goto done;
5451 done:
5452 free(tmp_branch_name);
5453 free(new_base_branch_ref_name);
5454 free(branch_ref_name);
5455 free(commit_ref_name);
5456 return err;
5459 const struct got_error *
5460 got_worktree_rebase_complete(struct got_worktree *worktree,
5461 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5462 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5463 struct got_repository *repo)
5465 const struct got_error *err, *unlockerr;
5466 struct got_object_id *new_head_commit_id = NULL;
5468 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5469 if (err)
5470 return err;
5472 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5473 if (err)
5474 goto done;
5476 err = got_ref_write(rebased_branch, repo);
5477 if (err)
5478 goto done;
5480 err = got_worktree_set_head_ref(worktree, rebased_branch);
5481 if (err)
5482 goto done;
5484 err = delete_rebase_refs(worktree, repo);
5485 done:
5486 if (fileindex)
5487 got_fileindex_free(fileindex);
5488 free(new_head_commit_id);
5489 unlockerr = lock_worktree(worktree, LOCK_SH);
5490 if (unlockerr && err == NULL)
5491 err = unlockerr;
5492 return err;
5495 const struct got_error *
5496 got_worktree_rebase_abort(struct got_worktree *worktree,
5497 struct got_fileindex *fileindex, struct got_repository *repo,
5498 struct got_reference *new_base_branch,
5499 got_worktree_checkout_cb progress_cb, void *progress_arg)
5501 const struct got_error *err, *unlockerr, *sync_err;
5502 struct got_reference *resolved = NULL;
5503 struct got_object_id *commit_id = NULL;
5504 char *fileindex_path = NULL;
5505 struct revert_file_args rfa;
5506 struct got_object_id *tree_id = NULL;
5508 err = lock_worktree(worktree, LOCK_EX);
5509 if (err)
5510 return err;
5512 err = got_ref_open(&resolved, repo,
5513 got_ref_get_symref_target(new_base_branch), 0);
5514 if (err)
5515 goto done;
5517 err = got_worktree_set_head_ref(worktree, resolved);
5518 if (err)
5519 goto done;
5522 * XXX commits to the base branch could have happened while
5523 * we were busy rebasing; should we store the original commit ID
5524 * when rebase begins and read it back here?
5526 err = got_ref_resolve(&commit_id, repo, resolved);
5527 if (err)
5528 goto done;
5530 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5531 if (err)
5532 goto done;
5534 err = got_object_id_by_path(&tree_id, repo,
5535 worktree->base_commit_id, worktree->path_prefix);
5536 if (err)
5537 goto done;
5539 err = delete_rebase_refs(worktree, repo);
5540 if (err)
5541 goto done;
5543 err = get_fileindex_path(&fileindex_path, worktree);
5544 if (err)
5545 goto done;
5547 rfa.worktree = worktree;
5548 rfa.fileindex = fileindex;
5549 rfa.progress_cb = progress_cb;
5550 rfa.progress_arg = progress_arg;
5551 rfa.patch_cb = NULL;
5552 rfa.patch_arg = NULL;
5553 rfa.repo = repo;
5554 err = worktree_status(worktree, "", fileindex, repo,
5555 revert_file, &rfa, NULL, NULL, 0, 0);
5556 if (err)
5557 goto sync;
5559 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5560 repo, progress_cb, progress_arg, NULL, NULL);
5561 sync:
5562 sync_err = sync_fileindex(fileindex, fileindex_path);
5563 if (sync_err && err == NULL)
5564 err = sync_err;
5565 done:
5566 got_ref_close(resolved);
5567 free(tree_id);
5568 free(commit_id);
5569 if (fileindex)
5570 got_fileindex_free(fileindex);
5571 free(fileindex_path);
5573 unlockerr = lock_worktree(worktree, LOCK_SH);
5574 if (unlockerr && err == NULL)
5575 err = unlockerr;
5576 return err;
5579 const struct got_error *
5580 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5581 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5582 struct got_fileindex **fileindex, struct got_worktree *worktree,
5583 struct got_repository *repo)
5585 const struct got_error *err = NULL;
5586 char *tmp_branch_name = NULL;
5587 char *branch_ref_name = NULL;
5588 char *base_commit_ref_name = NULL;
5589 char *fileindex_path = NULL;
5590 struct check_rebase_ok_arg ok_arg;
5591 struct got_reference *wt_branch = NULL;
5592 struct got_reference *base_commit_ref = NULL;
5594 *tmp_branch = NULL;
5595 *branch_ref = NULL;
5596 *base_commit_id = NULL;
5597 *fileindex = NULL;
5599 err = lock_worktree(worktree, LOCK_EX);
5600 if (err)
5601 return err;
5603 err = open_fileindex(fileindex, &fileindex_path, worktree);
5604 if (err)
5605 goto done;
5607 ok_arg.worktree = worktree;
5608 ok_arg.repo = repo;
5609 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5610 &ok_arg);
5611 if (err)
5612 goto done;
5614 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5615 if (err)
5616 goto done;
5618 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5619 if (err)
5620 goto done;
5622 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5623 worktree);
5624 if (err)
5625 goto done;
5627 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5628 0);
5629 if (err)
5630 goto done;
5632 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5633 if (err)
5634 goto done;
5636 err = got_ref_write(*branch_ref, repo);
5637 if (err)
5638 goto done;
5640 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5641 worktree->base_commit_id);
5642 if (err)
5643 goto done;
5644 err = got_ref_write(base_commit_ref, repo);
5645 if (err)
5646 goto done;
5647 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5648 if (*base_commit_id == NULL) {
5649 err = got_error_from_errno("got_object_id_dup");
5650 goto done;
5653 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5654 worktree->base_commit_id);
5655 if (err)
5656 goto done;
5657 err = got_ref_write(*tmp_branch, repo);
5658 if (err)
5659 goto done;
5661 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5662 if (err)
5663 goto done;
5664 done:
5665 free(fileindex_path);
5666 free(tmp_branch_name);
5667 free(branch_ref_name);
5668 free(base_commit_ref_name);
5669 if (wt_branch)
5670 got_ref_close(wt_branch);
5671 if (err) {
5672 if (*branch_ref) {
5673 got_ref_close(*branch_ref);
5674 *branch_ref = NULL;
5676 if (*tmp_branch) {
5677 got_ref_close(*tmp_branch);
5678 *tmp_branch = NULL;
5680 free(*base_commit_id);
5681 if (*fileindex) {
5682 got_fileindex_free(*fileindex);
5683 *fileindex = NULL;
5685 lock_worktree(worktree, LOCK_SH);
5687 return err;
5690 const struct got_error *
5691 got_worktree_histedit_postpone(struct got_worktree *worktree,
5692 struct got_fileindex *fileindex)
5694 if (fileindex)
5695 got_fileindex_free(fileindex);
5696 return lock_worktree(worktree, LOCK_SH);
5699 const struct got_error *
5700 got_worktree_histedit_in_progress(int *in_progress,
5701 struct got_worktree *worktree)
5703 const struct got_error *err;
5704 char *tmp_branch_name = NULL;
5706 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5707 if (err)
5708 return err;
5710 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5711 free(tmp_branch_name);
5712 return NULL;
5715 const struct got_error *
5716 got_worktree_histedit_continue(struct got_object_id **commit_id,
5717 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5718 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5719 struct got_worktree *worktree, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5723 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5724 struct got_reference *commit_ref = NULL;
5725 struct got_reference *base_commit_ref = NULL;
5726 char *fileindex_path = NULL;
5727 int have_staged_files = 0;
5729 *commit_id = NULL;
5730 *tmp_branch = NULL;
5731 *base_commit_id = NULL;
5732 *fileindex = NULL;
5734 err = lock_worktree(worktree, LOCK_EX);
5735 if (err)
5736 return err;
5738 err = open_fileindex(fileindex, &fileindex_path, worktree);
5739 if (err)
5740 goto done;
5742 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5743 &have_staged_files);
5744 if (err && err->code != GOT_ERR_CANCELLED)
5745 goto done;
5746 if (have_staged_files) {
5747 err = got_error(GOT_ERR_STAGED_PATHS);
5748 goto done;
5751 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5752 if (err)
5753 goto done;
5755 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5756 if (err)
5757 goto done;
5759 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5760 if (err)
5761 goto done;
5763 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5764 worktree);
5765 if (err)
5766 goto done;
5768 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5769 if (err)
5770 goto done;
5772 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5773 if (err)
5774 goto done;
5775 err = got_ref_resolve(commit_id, repo, commit_ref);
5776 if (err)
5777 goto done;
5779 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5780 if (err)
5781 goto done;
5782 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5783 if (err)
5784 goto done;
5786 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5787 if (err)
5788 goto done;
5789 done:
5790 free(commit_ref_name);
5791 free(branch_ref_name);
5792 free(fileindex_path);
5793 if (commit_ref)
5794 got_ref_close(commit_ref);
5795 if (base_commit_ref)
5796 got_ref_close(base_commit_ref);
5797 if (err) {
5798 free(*commit_id);
5799 *commit_id = NULL;
5800 free(*base_commit_id);
5801 *base_commit_id = NULL;
5802 if (*tmp_branch) {
5803 got_ref_close(*tmp_branch);
5804 *tmp_branch = NULL;
5806 if (*fileindex) {
5807 got_fileindex_free(*fileindex);
5808 *fileindex = NULL;
5810 lock_worktree(worktree, LOCK_EX);
5812 return err;
5815 static const struct got_error *
5816 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5818 const struct got_error *err;
5819 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5820 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5822 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5823 if (err)
5824 goto done;
5825 err = delete_ref(tmp_branch_name, repo);
5826 if (err)
5827 goto done;
5829 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5830 worktree);
5831 if (err)
5832 goto done;
5833 err = delete_ref(base_commit_ref_name, repo);
5834 if (err)
5835 goto done;
5837 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5838 if (err)
5839 goto done;
5840 err = delete_ref(branch_ref_name, repo);
5841 if (err)
5842 goto done;
5844 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5845 if (err)
5846 goto done;
5847 err = delete_ref(commit_ref_name, repo);
5848 if (err)
5849 goto done;
5850 done:
5851 free(tmp_branch_name);
5852 free(base_commit_ref_name);
5853 free(branch_ref_name);
5854 free(commit_ref_name);
5855 return err;
5858 const struct got_error *
5859 got_worktree_histedit_abort(struct got_worktree *worktree,
5860 struct got_fileindex *fileindex, struct got_repository *repo,
5861 struct got_reference *branch, struct got_object_id *base_commit_id,
5862 got_worktree_checkout_cb progress_cb, void *progress_arg)
5864 const struct got_error *err, *unlockerr, *sync_err;
5865 struct got_reference *resolved = NULL;
5866 char *fileindex_path = NULL;
5867 struct got_object_id *tree_id = NULL;
5868 struct revert_file_args rfa;
5870 err = lock_worktree(worktree, LOCK_EX);
5871 if (err)
5872 return err;
5874 err = got_ref_open(&resolved, repo,
5875 got_ref_get_symref_target(branch), 0);
5876 if (err)
5877 goto done;
5879 err = got_worktree_set_head_ref(worktree, resolved);
5880 if (err)
5881 goto done;
5883 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5884 if (err)
5885 goto done;
5887 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5888 worktree->path_prefix);
5889 if (err)
5890 goto done;
5892 err = delete_histedit_refs(worktree, repo);
5893 if (err)
5894 goto done;
5896 err = get_fileindex_path(&fileindex_path, worktree);
5897 if (err)
5898 goto done;
5900 rfa.worktree = worktree;
5901 rfa.fileindex = fileindex;
5902 rfa.progress_cb = progress_cb;
5903 rfa.progress_arg = progress_arg;
5904 rfa.patch_cb = NULL;
5905 rfa.patch_arg = NULL;
5906 rfa.repo = repo;
5907 err = worktree_status(worktree, "", fileindex, repo,
5908 revert_file, &rfa, NULL, NULL, 0, 0);
5909 if (err)
5910 goto sync;
5912 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5913 repo, progress_cb, progress_arg, NULL, NULL);
5914 sync:
5915 sync_err = sync_fileindex(fileindex, fileindex_path);
5916 if (sync_err && err == NULL)
5917 err = sync_err;
5918 done:
5919 got_ref_close(resolved);
5920 free(tree_id);
5921 free(fileindex_path);
5923 unlockerr = lock_worktree(worktree, LOCK_SH);
5924 if (unlockerr && err == NULL)
5925 err = unlockerr;
5926 return err;
5929 const struct got_error *
5930 got_worktree_histedit_complete(struct got_worktree *worktree,
5931 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5932 struct got_reference *edited_branch, struct got_repository *repo)
5934 const struct got_error *err, *unlockerr;
5935 struct got_object_id *new_head_commit_id = NULL;
5936 struct got_reference *resolved = NULL;
5938 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5939 if (err)
5940 return err;
5942 err = got_ref_open(&resolved, repo,
5943 got_ref_get_symref_target(edited_branch), 0);
5944 if (err)
5945 goto done;
5947 err = got_ref_change_ref(resolved, new_head_commit_id);
5948 if (err)
5949 goto done;
5951 err = got_ref_write(resolved, repo);
5952 if (err)
5953 goto done;
5955 err = got_worktree_set_head_ref(worktree, resolved);
5956 if (err)
5957 goto done;
5959 err = delete_histedit_refs(worktree, repo);
5960 done:
5961 if (fileindex)
5962 got_fileindex_free(fileindex);
5963 free(new_head_commit_id);
5964 unlockerr = lock_worktree(worktree, LOCK_SH);
5965 if (unlockerr && err == NULL)
5966 err = unlockerr;
5967 return err;
5970 const struct got_error *
5971 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5972 struct got_object_id *commit_id, struct got_repository *repo)
5974 const struct got_error *err;
5975 char *commit_ref_name;
5977 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5978 if (err)
5979 return err;
5981 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5982 if (err)
5983 goto done;
5985 err = delete_ref(commit_ref_name, repo);
5986 done:
5987 free(commit_ref_name);
5988 return err;
5991 const struct got_error *
5992 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5993 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5994 struct got_worktree *worktree, const char *refname,
5995 struct got_repository *repo)
5997 const struct got_error *err = NULL;
5998 char *fileindex_path = NULL;
5999 struct check_rebase_ok_arg ok_arg;
6001 *fileindex = NULL;
6002 *branch_ref = NULL;
6003 *base_branch_ref = NULL;
6005 err = lock_worktree(worktree, LOCK_EX);
6006 if (err)
6007 return err;
6009 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6010 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6011 "cannot integrate a branch into itself; "
6012 "update -b or different branch name required");
6013 goto done;
6016 err = open_fileindex(fileindex, &fileindex_path, worktree);
6017 if (err)
6018 goto done;
6020 /* Preconditions are the same as for rebase. */
6021 ok_arg.worktree = worktree;
6022 ok_arg.repo = repo;
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6024 &ok_arg);
6025 if (err)
6026 goto done;
6028 err = got_ref_open(branch_ref, repo, refname, 1);
6029 if (err)
6030 goto done;
6032 err = got_ref_open(base_branch_ref, repo,
6033 got_worktree_get_head_ref_name(worktree), 1);
6034 done:
6035 if (err) {
6036 if (*branch_ref) {
6037 got_ref_close(*branch_ref);
6038 *branch_ref = NULL;
6040 if (*base_branch_ref) {
6041 got_ref_close(*base_branch_ref);
6042 *base_branch_ref = NULL;
6044 if (*fileindex) {
6045 got_fileindex_free(*fileindex);
6046 *fileindex = NULL;
6048 lock_worktree(worktree, LOCK_SH);
6050 return err;
6053 const struct got_error *
6054 got_worktree_integrate_continue(struct got_worktree *worktree,
6055 struct got_fileindex *fileindex, struct got_repository *repo,
6056 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6057 got_worktree_checkout_cb progress_cb, void *progress_arg,
6058 got_cancel_cb cancel_cb, void *cancel_arg)
6060 const struct got_error *err = NULL, *sync_err, *unlockerr;
6061 char *fileindex_path = NULL;
6062 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6064 err = get_fileindex_path(&fileindex_path, worktree);
6065 if (err)
6066 goto done;
6068 err = got_ref_resolve(&commit_id, repo, branch_ref);
6069 if (err)
6070 goto done;
6072 err = got_object_id_by_path(&tree_id, repo, commit_id,
6073 worktree->path_prefix);
6074 if (err)
6075 goto done;
6077 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6078 if (err)
6079 goto done;
6081 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6082 progress_cb, progress_arg, cancel_cb, cancel_arg);
6083 if (err)
6084 goto sync;
6086 err = got_ref_change_ref(base_branch_ref, commit_id);
6087 if (err)
6088 goto sync;
6090 err = got_ref_write(base_branch_ref, repo);
6091 sync:
6092 sync_err = sync_fileindex(fileindex, fileindex_path);
6093 if (sync_err && err == NULL)
6094 err = sync_err;
6096 done:
6097 unlockerr = got_ref_unlock(branch_ref);
6098 if (unlockerr && err == NULL)
6099 err = unlockerr;
6100 got_ref_close(branch_ref);
6102 unlockerr = got_ref_unlock(base_branch_ref);
6103 if (unlockerr && err == NULL)
6104 err = unlockerr;
6105 got_ref_close(base_branch_ref);
6107 got_fileindex_free(fileindex);
6108 free(fileindex_path);
6109 free(tree_id);
6111 unlockerr = lock_worktree(worktree, LOCK_SH);
6112 if (unlockerr && err == NULL)
6113 err = unlockerr;
6114 return err;
6117 const struct got_error *
6118 got_worktree_integrate_abort(struct got_worktree *worktree,
6119 struct got_fileindex *fileindex, struct got_repository *repo,
6120 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6122 const struct got_error *err = NULL, *unlockerr = NULL;
6124 got_fileindex_free(fileindex);
6126 err = lock_worktree(worktree, LOCK_SH);
6128 unlockerr = got_ref_unlock(branch_ref);
6129 if (unlockerr && err == NULL)
6130 err = unlockerr;
6131 got_ref_close(branch_ref);
6133 unlockerr = got_ref_unlock(base_branch_ref);
6134 if (unlockerr && err == NULL)
6135 err = unlockerr;
6136 got_ref_close(base_branch_ref);
6138 return err;
6141 struct check_stage_ok_arg {
6142 struct got_object_id *head_commit_id;
6143 struct got_worktree *worktree;
6144 struct got_fileindex *fileindex;
6145 struct got_repository *repo;
6146 int have_changes;
6149 const struct got_error *
6150 check_stage_ok(void *arg, unsigned char status,
6151 unsigned char staged_status, const char *relpath,
6152 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6153 struct got_object_id *commit_id, int dirfd, const char *de_name)
6155 struct check_stage_ok_arg *a = arg;
6156 const struct got_error *err = NULL;
6157 struct got_fileindex_entry *ie;
6158 struct got_object_id base_commit_id;
6159 struct got_object_id *base_commit_idp = NULL;
6160 char *in_repo_path = NULL, *p;
6162 if (status == GOT_STATUS_UNVERSIONED ||
6163 status == GOT_STATUS_NO_CHANGE)
6164 return NULL;
6165 if (status == GOT_STATUS_NONEXISTENT)
6166 return got_error_set_errno(ENOENT, relpath);
6168 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6169 if (ie == NULL)
6170 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6172 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6173 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6174 relpath) == -1)
6175 return got_error_from_errno("asprintf");
6177 if (got_fileindex_entry_has_commit(ie)) {
6178 memcpy(base_commit_id.sha1, ie->commit_sha1,
6179 SHA1_DIGEST_LENGTH);
6180 base_commit_idp = &base_commit_id;
6183 if (status == GOT_STATUS_CONFLICT) {
6184 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6185 goto done;
6186 } else if (status != GOT_STATUS_ADD &&
6187 status != GOT_STATUS_MODIFY &&
6188 status != GOT_STATUS_DELETE) {
6189 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6190 goto done;
6193 a->have_changes = 1;
6195 p = in_repo_path;
6196 while (p[0] == '/')
6197 p++;
6198 err = check_out_of_date(p, status, staged_status,
6199 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6200 GOT_ERR_STAGE_OUT_OF_DATE);
6201 done:
6202 free(in_repo_path);
6203 return err;
6206 struct stage_path_arg {
6207 struct got_worktree *worktree;
6208 struct got_fileindex *fileindex;
6209 struct got_repository *repo;
6210 got_worktree_status_cb status_cb;
6211 void *status_arg;
6212 got_worktree_patch_cb patch_cb;
6213 void *patch_arg;
6214 int staged_something;
6217 static const struct got_error *
6218 stage_path(void *arg, unsigned char status,
6219 unsigned char staged_status, const char *relpath,
6220 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6221 struct got_object_id *commit_id, int dirfd, const char *de_name)
6223 struct stage_path_arg *a = arg;
6224 const struct got_error *err = NULL;
6225 struct got_fileindex_entry *ie;
6226 char *ondisk_path = NULL, *path_content = NULL;
6227 uint32_t stage;
6228 struct got_object_id *new_staged_blob_id = NULL;
6230 if (status == GOT_STATUS_UNVERSIONED)
6231 return NULL;
6233 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6234 if (ie == NULL)
6235 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6237 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6238 relpath)== -1)
6239 return got_error_from_errno("asprintf");
6241 switch (status) {
6242 case GOT_STATUS_ADD:
6243 case GOT_STATUS_MODIFY:
6244 if (a->patch_cb) {
6245 if (status == GOT_STATUS_ADD) {
6246 int choice = GOT_PATCH_CHOICE_NONE;
6247 err = (*a->patch_cb)(&choice, a->patch_arg,
6248 status, ie->path, NULL, 1, 1);
6249 if (err)
6250 break;
6251 if (choice != GOT_PATCH_CHOICE_YES)
6252 break;
6253 } else {
6254 err = create_patched_content(&path_content, 0,
6255 staged_blob_id ? staged_blob_id : blob_id,
6256 ondisk_path, dirfd, de_name, ie->path,
6257 a->repo, a->patch_cb, a->patch_arg);
6258 if (err || path_content == NULL)
6259 break;
6262 err = got_object_blob_create(&new_staged_blob_id,
6263 path_content ? path_content : ondisk_path, a->repo);
6264 if (err)
6265 break;
6266 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6267 SHA1_DIGEST_LENGTH);
6268 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6269 stage = GOT_FILEIDX_STAGE_ADD;
6270 else
6271 stage = GOT_FILEIDX_STAGE_MODIFY;
6272 got_fileindex_entry_stage_set(ie, stage);
6273 a->staged_something = 1;
6274 if (a->status_cb == NULL)
6275 break;
6276 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6277 get_staged_status(ie), relpath, blob_id,
6278 new_staged_blob_id, NULL, dirfd, de_name);
6279 break;
6280 case GOT_STATUS_DELETE:
6281 if (staged_status == GOT_STATUS_DELETE)
6282 break;
6283 if (a->patch_cb) {
6284 int choice = GOT_PATCH_CHOICE_NONE;
6285 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6286 ie->path, NULL, 1, 1);
6287 if (err)
6288 break;
6289 if (choice == GOT_PATCH_CHOICE_NO)
6290 break;
6291 if (choice != GOT_PATCH_CHOICE_YES) {
6292 err = got_error(GOT_ERR_PATCH_CHOICE);
6293 break;
6296 stage = GOT_FILEIDX_STAGE_DELETE;
6297 got_fileindex_entry_stage_set(ie, stage);
6298 a->staged_something = 1;
6299 if (a->status_cb == NULL)
6300 break;
6301 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6302 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6303 de_name);
6304 break;
6305 case GOT_STATUS_NO_CHANGE:
6306 break;
6307 case GOT_STATUS_CONFLICT:
6308 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6309 break;
6310 case GOT_STATUS_NONEXISTENT:
6311 err = got_error_set_errno(ENOENT, relpath);
6312 break;
6313 default:
6314 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6315 break;
6318 if (path_content && unlink(path_content) == -1 && err == NULL)
6319 err = got_error_from_errno2("unlink", path_content);
6320 free(path_content);
6321 free(ondisk_path);
6322 free(new_staged_blob_id);
6323 return err;
6326 const struct got_error *
6327 got_worktree_stage(struct got_worktree *worktree,
6328 struct got_pathlist_head *paths,
6329 got_worktree_status_cb status_cb, void *status_arg,
6330 got_worktree_patch_cb patch_cb, void *patch_arg,
6331 struct got_repository *repo)
6333 const struct got_error *err = NULL, *sync_err, *unlockerr;
6334 struct got_pathlist_entry *pe;
6335 struct got_fileindex *fileindex = NULL;
6336 char *fileindex_path = NULL;
6337 struct got_reference *head_ref = NULL;
6338 struct got_object_id *head_commit_id = NULL;
6339 struct check_stage_ok_arg oka;
6340 struct stage_path_arg spa;
6342 err = lock_worktree(worktree, LOCK_EX);
6343 if (err)
6344 return err;
6346 err = got_ref_open(&head_ref, repo,
6347 got_worktree_get_head_ref_name(worktree), 0);
6348 if (err)
6349 goto done;
6350 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6351 if (err)
6352 goto done;
6353 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6354 if (err)
6355 goto done;
6357 /* Check pre-conditions before staging anything. */
6358 oka.head_commit_id = head_commit_id;
6359 oka.worktree = worktree;
6360 oka.fileindex = fileindex;
6361 oka.repo = repo;
6362 oka.have_changes = 0;
6363 TAILQ_FOREACH(pe, paths, entry) {
6364 err = worktree_status(worktree, pe->path, fileindex, repo,
6365 check_stage_ok, &oka, NULL, NULL, 0, 0);
6366 if (err)
6367 goto done;
6369 if (!oka.have_changes) {
6370 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6371 goto done;
6374 spa.worktree = worktree;
6375 spa.fileindex = fileindex;
6376 spa.repo = repo;
6377 spa.patch_cb = patch_cb;
6378 spa.patch_arg = patch_arg;
6379 spa.status_cb = status_cb;
6380 spa.status_arg = status_arg;
6381 spa.staged_something = 0;
6382 TAILQ_FOREACH(pe, paths, entry) {
6383 err = worktree_status(worktree, pe->path, fileindex, repo,
6384 stage_path, &spa, NULL, NULL, 0, 0);
6385 if (err)
6386 goto done;
6388 if (!spa.staged_something) {
6389 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6390 goto done;
6393 sync_err = sync_fileindex(fileindex, fileindex_path);
6394 if (sync_err && err == NULL)
6395 err = sync_err;
6396 done:
6397 if (head_ref)
6398 got_ref_close(head_ref);
6399 free(head_commit_id);
6400 free(fileindex_path);
6401 if (fileindex)
6402 got_fileindex_free(fileindex);
6403 unlockerr = lock_worktree(worktree, LOCK_SH);
6404 if (unlockerr && err == NULL)
6405 err = unlockerr;
6406 return err;
6409 struct unstage_path_arg {
6410 struct got_worktree *worktree;
6411 struct got_fileindex *fileindex;
6412 struct got_repository *repo;
6413 got_worktree_checkout_cb progress_cb;
6414 void *progress_arg;
6415 got_worktree_patch_cb patch_cb;
6416 void *patch_arg;
6419 static const struct got_error *
6420 create_unstaged_content(char **path_unstaged_content,
6421 char **path_new_staged_content, struct got_object_id *blob_id,
6422 struct got_object_id *staged_blob_id, const char *relpath,
6423 struct got_repository *repo,
6424 got_worktree_patch_cb patch_cb, void *patch_arg)
6426 const struct got_error *err;
6427 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6428 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6429 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6430 struct stat sb1, sb2;
6431 struct got_diff_changes *changes = NULL;
6432 struct got_diff_state *ds = NULL;
6433 struct got_diff_args *args = NULL;
6434 struct got_diff_change *change;
6435 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6436 int have_content = 0, have_rejected_content = 0;
6438 *path_unstaged_content = NULL;
6439 *path_new_staged_content = NULL;
6441 err = got_object_id_str(&label1, blob_id);
6442 if (err)
6443 return err;
6444 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6445 if (err)
6446 goto done;
6448 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6449 if (err)
6450 goto done;
6452 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6453 if (err)
6454 goto done;
6456 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6457 if (err)
6458 goto done;
6460 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6461 if (err)
6462 goto done;
6464 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6465 if (err)
6466 goto done;
6468 if (stat(path1, &sb1) == -1) {
6469 err = got_error_from_errno2("stat", path1);
6470 goto done;
6473 if (stat(path2, &sb2) == -1) {
6474 err = got_error_from_errno2("stat", path2);
6475 goto done;
6478 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6479 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6480 if (err)
6481 goto done;
6483 err = got_opentemp_named(path_unstaged_content, &outfile,
6484 "got-unstaged-content");
6485 if (err)
6486 goto done;
6487 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6488 "got-new-staged-content");
6489 if (err)
6490 goto done;
6492 if (fseek(f1, 0L, SEEK_SET) == -1) {
6493 err = got_ferror(f1, GOT_ERR_IO);
6494 goto done;
6496 if (fseek(f2, 0L, SEEK_SET) == -1) {
6497 err = got_ferror(f2, GOT_ERR_IO);
6498 goto done;
6500 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6501 int choice;
6502 err = apply_or_reject_change(&choice, change, ++n,
6503 changes->nchanges, ds, args, diff_flags, relpath,
6504 f1, f2, &line_cur1, &line_cur2,
6505 outfile, rejectfile, patch_cb, patch_arg);
6506 if (err)
6507 goto done;
6508 if (choice == GOT_PATCH_CHOICE_YES)
6509 have_content = 1;
6510 else
6511 have_rejected_content = 1;
6512 if (choice == GOT_PATCH_CHOICE_QUIT)
6513 break;
6515 if (have_content || have_rejected_content)
6516 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6517 outfile, rejectfile);
6518 done:
6519 free(label1);
6520 if (blob)
6521 got_object_blob_close(blob);
6522 if (staged_blob)
6523 got_object_blob_close(staged_blob);
6524 if (f1 && fclose(f1) == EOF && err == NULL)
6525 err = got_error_from_errno2("fclose", path1);
6526 if (f2 && fclose(f2) == EOF && err == NULL)
6527 err = got_error_from_errno2("fclose", path2);
6528 if (outfile && fclose(outfile) == EOF && err == NULL)
6529 err = got_error_from_errno2("fclose", *path_unstaged_content);
6530 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6531 err = got_error_from_errno2("fclose", *path_new_staged_content);
6532 if (path1 && unlink(path1) == -1 && err == NULL)
6533 err = got_error_from_errno2("unlink", path1);
6534 if (path2 && unlink(path2) == -1 && err == NULL)
6535 err = got_error_from_errno2("unlink", path2);
6536 if (err || !have_content) {
6537 if (*path_unstaged_content &&
6538 unlink(*path_unstaged_content) == -1 && err == NULL)
6539 err = got_error_from_errno2("unlink",
6540 *path_unstaged_content);
6541 free(*path_unstaged_content);
6542 *path_unstaged_content = NULL;
6544 if (err || !have_rejected_content) {
6545 if (*path_new_staged_content &&
6546 unlink(*path_new_staged_content) == -1 && err == NULL)
6547 err = got_error_from_errno2("unlink",
6548 *path_new_staged_content);
6549 free(*path_new_staged_content);
6550 *path_new_staged_content = NULL;
6552 free(args);
6553 if (ds) {
6554 got_diff_state_free(ds);
6555 free(ds);
6557 if (changes)
6558 got_diff_free_changes(changes);
6559 free(path1);
6560 free(path2);
6561 return err;
6564 static const struct got_error *
6565 unstage_path(void *arg, unsigned char status,
6566 unsigned char staged_status, const char *relpath,
6567 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6568 struct got_object_id *commit_id, int dirfd, const char *de_name)
6570 const struct got_error *err = NULL;
6571 struct unstage_path_arg *a = arg;
6572 struct got_fileindex_entry *ie;
6573 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6574 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6575 char *path_new_staged_content = NULL;
6576 char *id_str = NULL, *label_orig = NULL;
6577 int local_changes_subsumed;
6578 struct stat sb;
6580 if (staged_status != GOT_STATUS_ADD &&
6581 staged_status != GOT_STATUS_MODIFY &&
6582 staged_status != GOT_STATUS_DELETE)
6583 return NULL;
6585 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6586 if (ie == NULL)
6587 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6589 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6590 == -1)
6591 return got_error_from_errno("asprintf");
6593 err = got_object_id_str(&id_str,
6594 commit_id ? commit_id : a->worktree->base_commit_id);
6595 if (err)
6596 goto done;
6597 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6598 id_str) == -1) {
6599 err = got_error_from_errno("asprintf");
6600 goto done;
6603 switch (staged_status) {
6604 case GOT_STATUS_MODIFY:
6605 err = got_object_open_as_blob(&blob_base, a->repo,
6606 blob_id, 8192);
6607 if (err)
6608 break;
6609 /* fall through */
6610 case GOT_STATUS_ADD:
6611 if (a->patch_cb) {
6612 if (staged_status == GOT_STATUS_ADD) {
6613 int choice = GOT_PATCH_CHOICE_NONE;
6614 err = (*a->patch_cb)(&choice, a->patch_arg,
6615 staged_status, ie->path, NULL, 1, 1);
6616 if (err)
6617 break;
6618 if (choice != GOT_PATCH_CHOICE_YES)
6619 break;
6620 } else {
6621 err = create_unstaged_content(
6622 &path_unstaged_content,
6623 &path_new_staged_content, blob_id,
6624 staged_blob_id, ie->path, a->repo,
6625 a->patch_cb, a->patch_arg);
6626 if (err || path_unstaged_content == NULL)
6627 break;
6628 if (path_new_staged_content) {
6629 err = got_object_blob_create(
6630 &staged_blob_id,
6631 path_new_staged_content,
6632 a->repo);
6633 if (err)
6634 break;
6635 memcpy(ie->staged_blob_sha1,
6636 staged_blob_id->sha1,
6637 SHA1_DIGEST_LENGTH);
6639 err = merge_file(&local_changes_subsumed,
6640 a->worktree, blob_base, ondisk_path,
6641 relpath, got_fileindex_perms_to_st(ie),
6642 path_unstaged_content, label_orig,
6643 "unstaged", a->repo, a->progress_cb,
6644 a->progress_arg);
6645 if (err == NULL &&
6646 path_new_staged_content == NULL)
6647 got_fileindex_entry_stage_set(ie,
6648 GOT_FILEIDX_STAGE_NONE);
6649 break; /* Done with this file. */
6652 err = got_object_open_as_blob(&blob_staged, a->repo,
6653 staged_blob_id, 8192);
6654 if (err)
6655 break;
6656 err = merge_blob(&local_changes_subsumed, a->worktree,
6657 blob_base, ondisk_path, relpath,
6658 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6659 commit_id ? commit_id : a->worktree->base_commit_id,
6660 a->repo, a->progress_cb, a->progress_arg);
6661 if (err == NULL)
6662 got_fileindex_entry_stage_set(ie,
6663 GOT_FILEIDX_STAGE_NONE);
6664 break;
6665 case GOT_STATUS_DELETE:
6666 if (a->patch_cb) {
6667 int choice = GOT_PATCH_CHOICE_NONE;
6668 err = (*a->patch_cb)(&choice, a->patch_arg,
6669 staged_status, ie->path, NULL, 1, 1);
6670 if (err)
6671 break;
6672 if (choice == GOT_PATCH_CHOICE_NO)
6673 break;
6674 if (choice != GOT_PATCH_CHOICE_YES) {
6675 err = got_error(GOT_ERR_PATCH_CHOICE);
6676 break;
6679 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6680 err = get_file_status(&status, &sb, ie, ondisk_path,
6681 dirfd, de_name, a->repo);
6682 if (err)
6683 break;
6684 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6685 break;
6687 done:
6688 free(ondisk_path);
6689 if (path_unstaged_content &&
6690 unlink(path_unstaged_content) == -1 && err == NULL)
6691 err = got_error_from_errno2("unlink", path_unstaged_content);
6692 if (path_new_staged_content &&
6693 unlink(path_new_staged_content) == -1 && err == NULL)
6694 err = got_error_from_errno2("unlink", path_new_staged_content);
6695 free(path_unstaged_content);
6696 free(path_new_staged_content);
6697 if (blob_base)
6698 got_object_blob_close(blob_base);
6699 if (blob_staged)
6700 got_object_blob_close(blob_staged);
6701 free(id_str);
6702 free(label_orig);
6703 return err;
6706 const struct got_error *
6707 got_worktree_unstage(struct got_worktree *worktree,
6708 struct got_pathlist_head *paths,
6709 got_worktree_checkout_cb progress_cb, void *progress_arg,
6710 got_worktree_patch_cb patch_cb, void *patch_arg,
6711 struct got_repository *repo)
6713 const struct got_error *err = NULL, *sync_err, *unlockerr;
6714 struct got_pathlist_entry *pe;
6715 struct got_fileindex *fileindex = NULL;
6716 char *fileindex_path = NULL;
6717 struct unstage_path_arg upa;
6719 err = lock_worktree(worktree, LOCK_EX);
6720 if (err)
6721 return err;
6723 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6724 if (err)
6725 goto done;
6727 upa.worktree = worktree;
6728 upa.fileindex = fileindex;
6729 upa.repo = repo;
6730 upa.progress_cb = progress_cb;
6731 upa.progress_arg = progress_arg;
6732 upa.patch_cb = patch_cb;
6733 upa.patch_arg = patch_arg;
6734 TAILQ_FOREACH(pe, paths, entry) {
6735 err = worktree_status(worktree, pe->path, fileindex, repo,
6736 unstage_path, &upa, NULL, NULL, 0, 0);
6737 if (err)
6738 goto done;
6741 sync_err = sync_fileindex(fileindex, fileindex_path);
6742 if (sync_err && err == NULL)
6743 err = sync_err;
6744 done:
6745 free(fileindex_path);
6746 if (fileindex)
6747 got_fileindex_free(fileindex);
6748 unlockerr = lock_worktree(worktree, LOCK_SH);
6749 if (unlockerr && err == NULL)
6750 err = unlockerr;
6751 return err;