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 /* forward declaration */
939 static const struct got_error *
940 install_blob(struct got_worktree *worktree, const char *ondisk_path,
941 const char *path, mode_t te_mode, mode_t st_mode,
942 struct got_blob_object *blob, int restoring_missing_file,
943 int reverting_versioned_file, struct got_repository *repo,
944 got_worktree_checkout_cb progress_cb, void *progress_arg);
946 static const struct got_error *
947 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
954 char target_path[PATH_MAX];
955 size_t len, target_len = 0;
956 char *resolved_path = NULL, *abspath = NULL;
957 const uint8_t *buf = got_object_blob_get_read_buf(blob);
958 size_t hdrlen = got_object_blob_get_hdrlen(blob);
960 /*
961 * Blob object content specifies the target path of the link.
962 * If a symbolic link cannot be installed we instead create
963 * a regular file which contains the link target path stored
964 * in the blob object.
965 */
966 do {
967 err = got_object_blob_read_block(&len, blob);
968 if (len + target_len >= sizeof(target_path)) {
969 /* Path too long; install as a regular file. */
970 got_object_blob_rewind(blob);
971 return install_blob(worktree, ondisk_path, path,
972 GOT_DEFAULT_FILE_MODE, st_mode, blob,
973 restoring_missing_file, reverting_versioned_file,
974 repo, progress_cb, progress_arg);
976 if (len > 0) {
977 /* Skip blob object header first time around. */
978 memcpy(target_path + target_len, buf + hdrlen,
979 len - hdrlen);
980 target_len += len - hdrlen;
981 hdrlen = 0;
983 } while (len != 0);
984 target_path[target_len] = '\0';
986 /*
987 * Relative symlink target lookup should begin at the directory
988 * in which the blob object is being installed.
989 */
990 if (!got_path_is_absolute(target_path)) {
991 char *parent = dirname(ondisk_path);
992 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
993 err = got_error_from_errno("asprintf");
994 goto done;
998 /*
999 * unveil(2) restricts our view of paths in the filesystem.
1000 * ENOENT will occur if a link target path does not exist or
1001 * if it points outside our unveiled path space.
1003 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1004 if (resolved_path == NULL) {
1005 if (errno != ENOENT)
1006 return got_error_from_errno2("realpath", target_path);
1009 /* Only allow symlinks pointing at paths within the work tree. */
1010 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1011 abspath : target_path), worktree->root_path,
1012 strlen(worktree->root_path))) {
1013 /* install as a regular file */
1014 got_object_blob_rewind(blob);
1015 err = install_blob(worktree, ondisk_path, path,
1016 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1017 restoring_missing_file, reverting_versioned_file,
1018 repo, progress_cb, progress_arg);
1019 goto done;
1022 if (symlink(target_path, ondisk_path) == -1) {
1023 if (errno == EEXIST) {
1024 struct stat sb;
1025 ssize_t elen;
1026 char etarget[PATH_MAX];
1027 if (lstat(ondisk_path, &sb) == -1) {
1028 err = got_error_from_errno2("lstat",
1029 ondisk_path);
1030 goto done;
1032 if (!S_ISLNK(sb.st_mode)) {
1033 err = got_error_path(ondisk_path,
1034 GOT_ERR_FILE_OBSTRUCTED);
1035 goto done;
1037 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1038 if (elen == -1) {
1039 err = got_error_from_errno2("readlink",
1040 ondisk_path);
1041 goto done;
1043 if (elen == target_len &&
1044 memcmp(etarget, target_path, target_len) == 0) {
1045 err = NULL; /* nothing to do */
1046 goto done;
1047 } else {
1048 if (unlink(ondisk_path) == -1) {
1049 err = got_error_from_errno2("unlink",
1050 ondisk_path);
1051 goto done;
1053 if (symlink(target_path, ondisk_path) == -1) {
1054 err = got_error_from_errno3("symlink",
1055 target_path, ondisk_path);
1056 goto done;
1059 err = (*progress_cb)(progress_arg,
1060 GOT_STATUS_UPDATE, path);
1061 goto done;
1065 if (errno == ENOENT) {
1066 char *parent = dirname(ondisk_path);
1067 if (parent == NULL) {
1068 err = got_error_from_errno2("dirname",
1069 ondisk_path);
1070 goto done;
1072 err = add_dir_on_disk(worktree, parent);
1073 if (err)
1074 goto done;
1076 * Retry, and fall through to error handling
1077 * below if this second attempt fails.
1079 if (symlink(target_path, ondisk_path) != -1) {
1080 err = NULL; /* success */
1081 goto done;
1085 /* Handle errors from first or second creation attempt. */
1086 if (errno == ENAMETOOLONG) {
1087 /* bad target path; install as a regular file */
1088 got_object_blob_rewind(blob);
1089 err = install_blob(worktree, ondisk_path, path,
1090 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1091 restoring_missing_file, reverting_versioned_file,
1092 repo, progress_cb, progress_arg);
1093 } else if (errno == ENOTDIR) {
1094 err = got_error_path(ondisk_path,
1095 GOT_ERR_FILE_OBSTRUCTED);
1096 } else {
1097 err = got_error_from_errno3("symlink",
1098 target_path, ondisk_path);
1100 } else
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1102 done:
1103 free(resolved_path);
1104 free(abspath);
1105 return err;
1108 static const struct got_error *
1109 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1110 const char *path, mode_t te_mode, mode_t st_mode,
1111 struct got_blob_object *blob, int restoring_missing_file,
1112 int reverting_versioned_file, struct got_repository *repo,
1113 got_worktree_checkout_cb progress_cb, void *progress_arg)
1115 const struct got_error *err = NULL;
1116 int fd = -1;
1117 size_t len, hdrlen;
1118 int update = 0;
1119 char *tmppath = NULL;
1121 if (S_ISLNK(te_mode))
1122 return install_symlink(worktree, ondisk_path, path, te_mode,
1123 st_mode, blob, restoring_missing_file,
1124 reverting_versioned_file, repo, progress_cb, progress_arg);
1126 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1127 GOT_DEFAULT_FILE_MODE);
1128 if (fd == -1) {
1129 if (errno == ENOENT) {
1130 char *parent = dirname(path);
1131 if (parent == NULL)
1132 return got_error_from_errno2("dirname", path);
1133 err = add_dir_on_disk(worktree, parent);
1134 if (err)
1135 return err;
1136 fd = open(ondisk_path,
1137 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1138 GOT_DEFAULT_FILE_MODE);
1139 if (fd == -1)
1140 return got_error_from_errno2("open",
1141 ondisk_path);
1142 } else if (errno == EEXIST) {
1143 if (!S_ISREG(st_mode)) {
1144 /* TODO file is obstructed; do something */
1145 err = got_error_path(ondisk_path,
1146 GOT_ERR_FILE_OBSTRUCTED);
1147 goto done;
1148 } else {
1149 err = got_opentemp_named_fd(&tmppath, &fd,
1150 ondisk_path);
1151 if (err)
1152 goto done;
1153 update = 1;
1155 } else
1156 return got_error_from_errno2("open", ondisk_path);
1159 if (restoring_missing_file)
1160 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1161 else if (reverting_versioned_file)
1162 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1163 else
1164 err = (*progress_cb)(progress_arg,
1165 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1166 if (err)
1167 goto done;
1169 hdrlen = got_object_blob_get_hdrlen(blob);
1170 do {
1171 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1172 err = got_object_blob_read_block(&len, blob);
1173 if (err)
1174 break;
1175 if (len > 0) {
1176 /* Skip blob object header first time around. */
1177 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1178 if (outlen == -1) {
1179 err = got_error_from_errno("write");
1180 goto done;
1181 } else if (outlen != len - hdrlen) {
1182 err = got_error(GOT_ERR_IO);
1183 goto done;
1185 hdrlen = 0;
1187 } while (len != 0);
1189 if (fsync(fd) != 0) {
1190 err = got_error_from_errno("fsync");
1191 goto done;
1194 if (update) {
1195 if (rename(tmppath, ondisk_path) != 0) {
1196 err = got_error_from_errno3("rename", tmppath,
1197 ondisk_path);
1198 unlink(tmppath);
1199 goto done;
1203 if (chmod(ondisk_path,
1204 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1205 err = got_error_from_errno2("chmod", ondisk_path);
1206 goto done;
1209 done:
1210 if (fd != -1 && close(fd) != 0 && err == NULL)
1211 err = got_error_from_errno("close");
1212 free(tmppath);
1213 return err;
1216 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1217 static const struct got_error *
1218 get_modified_file_content_status(unsigned char *status, FILE *f)
1220 const struct got_error *err = NULL;
1221 const char *markers[3] = {
1222 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1223 GOT_DIFF_CONFLICT_MARKER_SEP,
1224 GOT_DIFF_CONFLICT_MARKER_END
1226 int i = 0;
1227 char *line;
1228 size_t len;
1229 const char delim[3] = {'\0', '\0', '\0'};
1231 while (*status == GOT_STATUS_MODIFY) {
1232 line = fparseln(f, &len, NULL, delim, 0);
1233 if (line == NULL) {
1234 if (feof(f))
1235 break;
1236 err = got_ferror(f, GOT_ERR_IO);
1237 break;
1240 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1241 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1242 == 0)
1243 *status = GOT_STATUS_CONFLICT;
1244 else
1245 i++;
1249 return err;
1252 static int
1253 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1255 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1256 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1259 static int
1260 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1262 return !(ie->ctime_sec == sb->st_ctime &&
1263 ie->ctime_nsec == sb->st_ctimensec &&
1264 ie->mtime_sec == sb->st_mtime &&
1265 ie->mtime_nsec == sb->st_mtimensec &&
1266 ie->size == (sb->st_size & 0xffffffff) &&
1267 !xbit_differs(ie, sb->st_mode));
1270 static unsigned char
1271 get_staged_status(struct got_fileindex_entry *ie)
1273 switch (got_fileindex_entry_stage_get(ie)) {
1274 case GOT_FILEIDX_STAGE_ADD:
1275 return GOT_STATUS_ADD;
1276 case GOT_FILEIDX_STAGE_DELETE:
1277 return GOT_STATUS_DELETE;
1278 case GOT_FILEIDX_STAGE_MODIFY:
1279 return GOT_STATUS_MODIFY;
1280 default:
1281 return GOT_STATUS_NO_CHANGE;
1285 static const struct got_error *
1286 get_symlink_status(unsigned char *status, struct stat *sb,
1287 struct got_fileindex_entry *ie, const char *abspath,
1288 int dirfd, const char *de_name, struct got_blob_object *blob)
1290 const struct got_error *err = NULL;
1291 char target_path[PATH_MAX];
1292 char etarget[PATH_MAX];
1293 ssize_t elen;
1294 size_t len, target_len = 0;
1295 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1296 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1298 *status = GOT_STATUS_NO_CHANGE;
1300 /* Blob object content specifies the target path of the link. */
1301 do {
1302 err = got_object_blob_read_block(&len, blob);
1303 if (err)
1304 return err;
1305 if (len + target_len >= sizeof(target_path)) {
1307 * Should not happen. The blob contents were OK
1308 * when this symlink was installed.
1310 return got_error(GOT_ERR_NO_SPACE);
1312 if (len > 0) {
1313 /* Skip blob object header first time around. */
1314 memcpy(target_path + target_len, buf + hdrlen,
1315 len - hdrlen);
1316 target_len += len - hdrlen;
1317 hdrlen = 0;
1319 } while (len != 0);
1320 target_path[target_len] = '\0';
1322 if (dirfd != -1) {
1323 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1324 if (elen == -1)
1325 return got_error_from_errno2("readlinkat", abspath);
1326 } else {
1327 elen = readlink(abspath, etarget, sizeof(etarget));
1328 if (elen == -1)
1329 return got_error_from_errno2("readlinkat", abspath);
1332 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1333 *status = GOT_STATUS_MODIFY;
1335 return NULL;
1338 static const struct got_error *
1339 get_file_status(unsigned char *status, struct stat *sb,
1340 struct got_fileindex_entry *ie, const char *abspath,
1341 int dirfd, const char *de_name, struct got_repository *repo)
1343 const struct got_error *err = NULL;
1344 struct got_object_id id;
1345 size_t hdrlen;
1346 int fd = -1;
1347 FILE *f = NULL;
1348 uint8_t fbuf[8192];
1349 struct got_blob_object *blob = NULL;
1350 size_t flen, blen;
1351 unsigned char staged_status = get_staged_status(ie);
1353 *status = GOT_STATUS_NO_CHANGE;
1356 * Whenever the caller provides a directory descriptor and a
1357 * directory entry name for the file, use them! This prevents
1358 * race conditions if filesystem paths change beneath our feet.
1360 if (dirfd != -1) {
1361 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1362 if (errno == ENOENT) {
1363 if (got_fileindex_entry_has_file_on_disk(ie))
1364 *status = GOT_STATUS_MISSING;
1365 else
1366 *status = GOT_STATUS_DELETE;
1367 goto done;
1369 err = got_error_from_errno2("fstatat", abspath);
1370 goto done;
1372 } else {
1373 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1374 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1375 return got_error_from_errno2("open", abspath);
1376 else if (fd == -1 && errno == ELOOP) {
1377 if (lstat(abspath, sb) == -1)
1378 return got_error_from_errno2("lstat", abspath);
1379 } else if (fd == -1 || fstat(fd, sb) == -1) {
1380 if (errno == ENOENT) {
1381 if (got_fileindex_entry_has_file_on_disk(ie))
1382 *status = GOT_STATUS_MISSING;
1383 else
1384 *status = GOT_STATUS_DELETE;
1385 goto done;
1387 err = got_error_from_errno2("fstat", abspath);
1388 goto done;
1392 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1393 *status = GOT_STATUS_OBSTRUCTED;
1394 goto done;
1397 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1398 *status = GOT_STATUS_DELETE;
1399 goto done;
1400 } else if (!got_fileindex_entry_has_blob(ie) &&
1401 staged_status != GOT_STATUS_ADD) {
1402 *status = GOT_STATUS_ADD;
1403 goto done;
1406 if (!stat_info_differs(ie, sb))
1407 goto done;
1409 if (staged_status == GOT_STATUS_MODIFY ||
1410 staged_status == GOT_STATUS_ADD)
1411 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1412 else
1413 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1415 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1416 if (err)
1417 goto done;
1419 if (S_ISLNK(sb->st_mode)) {
1420 /* Staging changes to symlinks is not yet(?) supported. */
1421 if (staged_status != GOT_STATUS_NO_CHANGE) {
1422 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1423 goto done;
1425 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1426 de_name, blob);
1427 goto done;
1431 if (dirfd != -1) {
1432 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1433 if (fd == -1) {
1434 err = got_error_from_errno2("openat", abspath);
1435 goto done;
1439 f = fdopen(fd, "r");
1440 if (f == NULL) {
1441 err = got_error_from_errno2("fdopen", abspath);
1442 goto done;
1444 fd = -1;
1445 hdrlen = got_object_blob_get_hdrlen(blob);
1446 for (;;) {
1447 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1448 err = got_object_blob_read_block(&blen, blob);
1449 if (err)
1450 goto done;
1451 /* Skip length of blob object header first time around. */
1452 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1453 if (flen == 0 && ferror(f)) {
1454 err = got_error_from_errno("fread");
1455 goto done;
1457 if (blen == 0) {
1458 if (flen != 0)
1459 *status = GOT_STATUS_MODIFY;
1460 break;
1461 } else if (flen == 0) {
1462 if (blen != 0)
1463 *status = GOT_STATUS_MODIFY;
1464 break;
1465 } else if (blen - hdrlen == flen) {
1466 /* Skip blob object header first time around. */
1467 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1468 *status = GOT_STATUS_MODIFY;
1469 break;
1471 } else {
1472 *status = GOT_STATUS_MODIFY;
1473 break;
1475 hdrlen = 0;
1478 if (*status == GOT_STATUS_MODIFY) {
1479 rewind(f);
1480 err = get_modified_file_content_status(status, f);
1481 } else if (xbit_differs(ie, sb->st_mode))
1482 *status = GOT_STATUS_MODE_CHANGE;
1483 done:
1484 if (blob)
1485 got_object_blob_close(blob);
1486 if (f != NULL && fclose(f) == EOF && err == NULL)
1487 err = got_error_from_errno2("fclose", abspath);
1488 if (fd != -1 && close(fd) == -1 && err == NULL)
1489 err = got_error_from_errno2("close", abspath);
1490 return err;
1494 * Update timestamps in the file index if a file is unmodified and
1495 * we had to run a full content comparison to find out.
1497 static const struct got_error *
1498 sync_timestamps(char *ondisk_path, unsigned char status,
1499 struct got_fileindex_entry *ie, struct stat *sb)
1501 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1502 return got_fileindex_entry_update(ie, ondisk_path,
1503 ie->blob_sha1, ie->commit_sha1, 1);
1505 return NULL;
1508 static const struct got_error *
1509 update_blob(struct got_worktree *worktree,
1510 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1511 struct got_tree_entry *te, const char *path,
1512 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1513 void *progress_arg)
1515 const struct got_error *err = NULL;
1516 struct got_blob_object *blob = NULL;
1517 char *ondisk_path;
1518 unsigned char status = GOT_STATUS_NO_CHANGE;
1519 struct stat sb;
1521 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1522 return got_error_from_errno("asprintf");
1524 if (ie) {
1525 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1526 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1527 goto done;
1529 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1530 repo);
1531 if (err)
1532 goto done;
1533 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1534 sb.st_mode = got_fileindex_perms_to_st(ie);
1535 } else
1536 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1538 if (status == GOT_STATUS_OBSTRUCTED) {
1539 err = (*progress_cb)(progress_arg, status, path);
1540 goto done;
1542 if (status == GOT_STATUS_CONFLICT) {
1543 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1544 path);
1545 goto done;
1548 if (ie && status != GOT_STATUS_MISSING &&
1549 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1550 if (got_fileindex_entry_has_commit(ie) &&
1551 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1552 SHA1_DIGEST_LENGTH) == 0) {
1553 err = sync_timestamps(ondisk_path, status, ie, &sb);
1554 if (err)
1555 goto done;
1556 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1557 path);
1558 goto done;
1560 if (got_fileindex_entry_has_blob(ie) &&
1561 memcmp(ie->blob_sha1, te->id.sha1,
1562 SHA1_DIGEST_LENGTH) == 0) {
1563 err = sync_timestamps(ondisk_path, status, ie, &sb);
1564 goto done;
1568 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1569 if (err)
1570 goto done;
1572 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1573 int update_timestamps;
1574 struct got_blob_object *blob2 = NULL;
1575 char *label_orig = NULL;
1576 if (got_fileindex_entry_has_blob(ie)) {
1577 struct got_object_id id2;
1578 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1579 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1580 if (err)
1581 goto done;
1583 if (got_fileindex_entry_has_commit(ie)) {
1584 char id_str[SHA1_DIGEST_STRING_LENGTH];
1585 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1586 sizeof(id_str)) == NULL) {
1587 err = got_error_path(id_str,
1588 GOT_ERR_BAD_OBJ_ID_STR);
1589 goto done;
1591 if (asprintf(&label_orig, "%s: commit %s",
1592 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1593 err = got_error_from_errno("asprintf");
1594 goto done;
1597 err = merge_blob(&update_timestamps, worktree, blob2,
1598 ondisk_path, path, sb.st_mode, label_orig, blob,
1599 worktree->base_commit_id, repo,
1600 progress_cb, progress_arg);
1601 free(label_orig);
1602 if (blob2)
1603 got_object_blob_close(blob2);
1604 if (err)
1605 goto done;
1607 * Do not update timestamps of files with local changes.
1608 * Otherwise, a future status walk would treat them as
1609 * unmodified files again.
1611 err = got_fileindex_entry_update(ie, ondisk_path,
1612 blob->id.sha1, worktree->base_commit_id->sha1,
1613 update_timestamps);
1614 } else if (status == GOT_STATUS_MODE_CHANGE) {
1615 err = got_fileindex_entry_update(ie, ondisk_path,
1616 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1617 } else if (status == GOT_STATUS_DELETE) {
1618 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1619 if (err)
1620 goto done;
1621 err = got_fileindex_entry_update(ie, ondisk_path,
1622 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1623 if (err)
1624 goto done;
1625 } else {
1626 err = install_blob(worktree, ondisk_path, path, te->mode,
1627 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1628 repo, progress_cb, progress_arg);
1629 if (err)
1630 goto done;
1631 if (ie) {
1632 err = got_fileindex_entry_update(ie, ondisk_path,
1633 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1634 } else {
1635 err = create_fileindex_entry(fileindex,
1636 worktree->base_commit_id, ondisk_path, path,
1637 &blob->id);
1639 if (err)
1640 goto done;
1642 got_object_blob_close(blob);
1643 done:
1644 free(ondisk_path);
1645 return err;
1648 static const struct got_error *
1649 remove_ondisk_file(const char *root_path, const char *path)
1651 const struct got_error *err = NULL;
1652 char *ondisk_path = NULL;
1654 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1655 return got_error_from_errno("asprintf");
1657 if (unlink(ondisk_path) == -1) {
1658 if (errno != ENOENT)
1659 err = got_error_from_errno2("unlink", ondisk_path);
1660 } else {
1661 char *parent = dirname(ondisk_path);
1662 while (parent && strcmp(parent, root_path) != 0) {
1663 if (rmdir(parent) == -1) {
1664 if (errno != ENOTEMPTY)
1665 err = got_error_from_errno2("rmdir",
1666 parent);
1667 break;
1669 parent = dirname(parent);
1672 free(ondisk_path);
1673 return err;
1676 static const struct got_error *
1677 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1678 struct got_fileindex_entry *ie, struct got_repository *repo,
1679 got_worktree_checkout_cb progress_cb, void *progress_arg)
1681 const struct got_error *err = NULL;
1682 unsigned char status;
1683 struct stat sb;
1684 char *ondisk_path;
1686 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1687 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1689 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1690 == -1)
1691 return got_error_from_errno("asprintf");
1693 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1694 if (err)
1695 goto done;
1697 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1698 status == GOT_STATUS_ADD) {
1699 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1700 if (err)
1701 goto done;
1703 * Preserve the working file and change the deleted blob's
1704 * entry into a schedule-add entry.
1706 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1707 0);
1708 } else {
1709 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1710 if (err)
1711 goto done;
1712 if (status == GOT_STATUS_NO_CHANGE) {
1713 err = remove_ondisk_file(worktree->root_path, ie->path);
1714 if (err)
1715 goto done;
1717 got_fileindex_entry_remove(fileindex, ie);
1719 done:
1720 free(ondisk_path);
1721 return err;
1724 struct diff_cb_arg {
1725 struct got_fileindex *fileindex;
1726 struct got_worktree *worktree;
1727 struct got_repository *repo;
1728 got_worktree_checkout_cb progress_cb;
1729 void *progress_arg;
1730 got_cancel_cb cancel_cb;
1731 void *cancel_arg;
1734 static const struct got_error *
1735 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1736 struct got_tree_entry *te, const char *parent_path)
1738 struct diff_cb_arg *a = arg;
1740 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1741 return got_error(GOT_ERR_CANCELLED);
1743 return update_blob(a->worktree, a->fileindex, ie, te,
1744 ie->path, a->repo, a->progress_cb, a->progress_arg);
1747 static const struct got_error *
1748 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1750 struct diff_cb_arg *a = arg;
1752 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1753 return got_error(GOT_ERR_CANCELLED);
1755 return delete_blob(a->worktree, a->fileindex, ie,
1756 a->repo, a->progress_cb, a->progress_arg);
1759 static const struct got_error *
1760 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1762 struct diff_cb_arg *a = arg;
1763 const struct got_error *err;
1764 char *path;
1766 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1767 return got_error(GOT_ERR_CANCELLED);
1769 if (got_object_tree_entry_is_submodule(te))
1770 return NULL;
1772 if (asprintf(&path, "%s%s%s", parent_path,
1773 parent_path[0] ? "/" : "", te->name)
1774 == -1)
1775 return got_error_from_errno("asprintf");
1777 if (S_ISDIR(te->mode))
1778 err = add_dir_on_disk(a->worktree, path);
1779 else
1780 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1781 a->repo, a->progress_cb, a->progress_arg);
1783 free(path);
1784 return err;
1787 static const struct got_error *
1788 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1790 const struct got_error *err = NULL;
1791 char *uuidstr = NULL;
1792 uint32_t uuid_status;
1794 *refname = NULL;
1796 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1797 if (uuid_status != uuid_s_ok)
1798 return got_error_uuid(uuid_status, "uuid_to_string");
1800 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1801 == -1) {
1802 err = got_error_from_errno("asprintf");
1803 *refname = NULL;
1805 free(uuidstr);
1806 return err;
1809 const struct got_error *
1810 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1812 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1815 static const struct got_error *
1816 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1818 return get_ref_name(refname, worktree,
1819 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1822 static const struct got_error *
1823 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1825 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1828 static const struct got_error *
1829 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1831 return get_ref_name(refname, worktree,
1832 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1835 static const struct got_error *
1836 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1838 return get_ref_name(refname, worktree,
1839 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1842 static const struct got_error *
1843 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1845 return get_ref_name(refname, worktree,
1846 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1849 static const struct got_error *
1850 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1852 return get_ref_name(refname, worktree,
1853 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1856 static const struct got_error *
1857 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1859 return get_ref_name(refname, worktree,
1860 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1863 static const struct got_error *
1864 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1866 return get_ref_name(refname, worktree,
1867 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1870 const struct got_error *
1871 got_worktree_get_histedit_script_path(char **path,
1872 struct got_worktree *worktree)
1874 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1875 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1876 *path = NULL;
1877 return got_error_from_errno("asprintf");
1879 return NULL;
1883 * Prevent Git's garbage collector from deleting our base commit by
1884 * setting a reference to our base commit's ID.
1886 static const struct got_error *
1887 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1889 const struct got_error *err = NULL;
1890 struct got_reference *ref = NULL;
1891 char *refname;
1893 err = got_worktree_get_base_ref_name(&refname, worktree);
1894 if (err)
1895 return err;
1897 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1898 if (err)
1899 goto done;
1901 err = got_ref_write(ref, repo);
1902 done:
1903 free(refname);
1904 if (ref)
1905 got_ref_close(ref);
1906 return err;
1909 static const struct got_error *
1910 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1912 const struct got_error *err = NULL;
1914 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1915 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1916 err = got_error_from_errno("asprintf");
1917 *fileindex_path = NULL;
1919 return err;
1923 static const struct got_error *
1924 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1925 struct got_worktree *worktree)
1927 const struct got_error *err = NULL;
1928 FILE *index = NULL;
1930 *fileindex_path = NULL;
1931 *fileindex = got_fileindex_alloc();
1932 if (*fileindex == NULL)
1933 return got_error_from_errno("got_fileindex_alloc");
1935 err = get_fileindex_path(fileindex_path, worktree);
1936 if (err)
1937 goto done;
1939 index = fopen(*fileindex_path, "rb");
1940 if (index == NULL) {
1941 if (errno != ENOENT)
1942 err = got_error_from_errno2("fopen", *fileindex_path);
1943 } else {
1944 err = got_fileindex_read(*fileindex, index);
1945 if (fclose(index) != 0 && err == NULL)
1946 err = got_error_from_errno("fclose");
1948 done:
1949 if (err) {
1950 free(*fileindex_path);
1951 *fileindex_path = NULL;
1952 got_fileindex_free(*fileindex);
1953 *fileindex = NULL;
1955 return err;
1958 struct bump_base_commit_id_arg {
1959 struct got_object_id *base_commit_id;
1960 const char *path;
1961 size_t path_len;
1962 const char *entry_name;
1963 got_worktree_checkout_cb progress_cb;
1964 void *progress_arg;
1967 /* Bump base commit ID of all files within an updated part of the work tree. */
1968 static const struct got_error *
1969 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1971 const struct got_error *err;
1972 struct bump_base_commit_id_arg *a = arg;
1974 if (a->entry_name) {
1975 if (strcmp(ie->path, a->path) != 0)
1976 return NULL;
1977 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1978 return NULL;
1980 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1981 SHA1_DIGEST_LENGTH) == 0)
1982 return NULL;
1984 if (a->progress_cb) {
1985 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1986 ie->path);
1987 if (err)
1988 return err;
1990 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1991 return NULL;
1994 static const struct got_error *
1995 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1997 const struct got_error *err = NULL;
1998 char *new_fileindex_path = NULL;
1999 FILE *new_index = NULL;
2000 struct timespec timeout;
2002 err = got_opentemp_named(&new_fileindex_path, &new_index,
2003 fileindex_path);
2004 if (err)
2005 goto done;
2007 err = got_fileindex_write(fileindex, new_index);
2008 if (err)
2009 goto done;
2011 if (rename(new_fileindex_path, fileindex_path) != 0) {
2012 err = got_error_from_errno3("rename", new_fileindex_path,
2013 fileindex_path);
2014 unlink(new_fileindex_path);
2018 * Sleep for a short amount of time to ensure that files modified after
2019 * this program exits have a different time stamp from the one which
2020 * was recorded in the file index.
2022 timeout.tv_sec = 0;
2023 timeout.tv_nsec = 1;
2024 nanosleep(&timeout, NULL);
2025 done:
2026 if (new_index)
2027 fclose(new_index);
2028 free(new_fileindex_path);
2029 return err;
2032 static const struct got_error *
2033 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2034 struct got_object_id **tree_id, const char *wt_relpath,
2035 struct got_worktree *worktree, struct got_repository *repo)
2037 const struct got_error *err = NULL;
2038 struct got_object_id *id = NULL;
2039 char *in_repo_path = NULL;
2040 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2042 *entry_type = GOT_OBJ_TYPE_ANY;
2043 *tree_relpath = NULL;
2044 *tree_id = NULL;
2046 if (wt_relpath[0] == '\0') {
2047 /* Check out all files within the work tree. */
2048 *entry_type = GOT_OBJ_TYPE_TREE;
2049 *tree_relpath = strdup("");
2050 if (*tree_relpath == NULL) {
2051 err = got_error_from_errno("strdup");
2052 goto done;
2054 err = got_object_id_by_path(tree_id, repo,
2055 worktree->base_commit_id, worktree->path_prefix);
2056 if (err)
2057 goto done;
2058 return NULL;
2061 /* Check out a subset of files in the work tree. */
2063 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2064 is_root_wt ? "" : "/", wt_relpath) == -1) {
2065 err = got_error_from_errno("asprintf");
2066 goto done;
2069 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2070 in_repo_path);
2071 if (err)
2072 goto done;
2074 free(in_repo_path);
2075 in_repo_path = NULL;
2077 err = got_object_get_type(entry_type, repo, id);
2078 if (err)
2079 goto done;
2081 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2082 /* Check out a single file. */
2083 if (strchr(wt_relpath, '/') == NULL) {
2084 /* Check out a single file in work tree's root dir. */
2085 in_repo_path = strdup(worktree->path_prefix);
2086 if (in_repo_path == NULL) {
2087 err = got_error_from_errno("strdup");
2088 goto done;
2090 *tree_relpath = strdup("");
2091 if (*tree_relpath == NULL) {
2092 err = got_error_from_errno("strdup");
2093 goto done;
2095 } else {
2096 /* Check out a single file in a subdirectory. */
2097 err = got_path_dirname(tree_relpath, wt_relpath);
2098 if (err)
2099 return err;
2100 if (asprintf(&in_repo_path, "%s%s%s",
2101 worktree->path_prefix, is_root_wt ? "" : "/",
2102 *tree_relpath) == -1) {
2103 err = got_error_from_errno("asprintf");
2104 goto done;
2107 err = got_object_id_by_path(tree_id, repo,
2108 worktree->base_commit_id, in_repo_path);
2109 } else {
2110 /* Check out all files within a subdirectory. */
2111 *tree_id = got_object_id_dup(id);
2112 if (*tree_id == NULL) {
2113 err = got_error_from_errno("got_object_id_dup");
2114 goto done;
2116 *tree_relpath = strdup(wt_relpath);
2117 if (*tree_relpath == NULL) {
2118 err = got_error_from_errno("strdup");
2119 goto done;
2122 done:
2123 free(id);
2124 free(in_repo_path);
2125 if (err) {
2126 *entry_type = GOT_OBJ_TYPE_ANY;
2127 free(*tree_relpath);
2128 *tree_relpath = NULL;
2129 free(*tree_id);
2130 *tree_id = NULL;
2132 return err;
2135 static const struct got_error *
2136 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2137 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2138 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2139 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2141 const struct got_error *err = NULL;
2142 struct got_commit_object *commit = NULL;
2143 struct got_tree_object *tree = NULL;
2144 struct got_fileindex_diff_tree_cb diff_cb;
2145 struct diff_cb_arg arg;
2147 err = ref_base_commit(worktree, repo);
2148 if (err) {
2149 if (!(err->code == GOT_ERR_ERRNO &&
2150 (errno == EACCES || errno == EROFS)))
2151 goto done;
2152 err = (*progress_cb)(progress_arg,
2153 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2154 if (err)
2155 return err;
2158 err = got_object_open_as_commit(&commit, repo,
2159 worktree->base_commit_id);
2160 if (err)
2161 goto done;
2163 err = got_object_open_as_tree(&tree, repo, tree_id);
2164 if (err)
2165 goto done;
2167 if (entry_name &&
2168 got_object_tree_find_entry(tree, entry_name) == NULL) {
2169 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2170 goto done;
2173 diff_cb.diff_old_new = diff_old_new;
2174 diff_cb.diff_old = diff_old;
2175 diff_cb.diff_new = diff_new;
2176 arg.fileindex = fileindex;
2177 arg.worktree = worktree;
2178 arg.repo = repo;
2179 arg.progress_cb = progress_cb;
2180 arg.progress_arg = progress_arg;
2181 arg.cancel_cb = cancel_cb;
2182 arg.cancel_arg = cancel_arg;
2183 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2184 entry_name, repo, &diff_cb, &arg);
2185 done:
2186 if (tree)
2187 got_object_tree_close(tree);
2188 if (commit)
2189 got_object_commit_close(commit);
2190 return err;
2193 const struct got_error *
2194 got_worktree_checkout_files(struct got_worktree *worktree,
2195 struct got_pathlist_head *paths, struct got_repository *repo,
2196 got_worktree_checkout_cb progress_cb, void *progress_arg,
2197 got_cancel_cb cancel_cb, void *cancel_arg)
2199 const struct got_error *err = NULL, *sync_err, *unlockerr;
2200 struct got_commit_object *commit = NULL;
2201 struct got_tree_object *tree = NULL;
2202 struct got_fileindex *fileindex = NULL;
2203 char *fileindex_path = NULL;
2204 struct got_pathlist_entry *pe;
2205 struct tree_path_data {
2206 SIMPLEQ_ENTRY(tree_path_data) entry;
2207 struct got_object_id *tree_id;
2208 int entry_type;
2209 char *relpath;
2210 char *entry_name;
2211 } *tpd = NULL;
2212 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2214 SIMPLEQ_INIT(&tree_paths);
2216 err = lock_worktree(worktree, LOCK_EX);
2217 if (err)
2218 return err;
2220 /* Map all specified paths to in-repository trees. */
2221 TAILQ_FOREACH(pe, paths, entry) {
2222 tpd = malloc(sizeof(*tpd));
2223 if (tpd == NULL) {
2224 err = got_error_from_errno("malloc");
2225 goto done;
2228 err = find_tree_entry_for_checkout(&tpd->entry_type,
2229 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2230 if (err) {
2231 free(tpd);
2232 goto done;
2235 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2236 err = got_path_basename(&tpd->entry_name, pe->path);
2237 if (err) {
2238 free(tpd->relpath);
2239 free(tpd->tree_id);
2240 free(tpd);
2241 goto done;
2243 } else
2244 tpd->entry_name = NULL;
2246 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2250 * Read the file index.
2251 * Checking out files is supposed to be an idempotent operation.
2252 * If the on-disk file index is incomplete we will try to complete it.
2254 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2255 if (err)
2256 goto done;
2258 tpd = SIMPLEQ_FIRST(&tree_paths);
2259 TAILQ_FOREACH(pe, paths, entry) {
2260 struct bump_base_commit_id_arg bbc_arg;
2262 err = checkout_files(worktree, fileindex, tpd->relpath,
2263 tpd->tree_id, tpd->entry_name, repo,
2264 progress_cb, progress_arg, cancel_cb, cancel_arg);
2265 if (err)
2266 break;
2268 bbc_arg.base_commit_id = worktree->base_commit_id;
2269 bbc_arg.entry_name = tpd->entry_name;
2270 bbc_arg.path = pe->path;
2271 bbc_arg.path_len = pe->path_len;
2272 bbc_arg.progress_cb = progress_cb;
2273 bbc_arg.progress_arg = progress_arg;
2274 err = got_fileindex_for_each_entry_safe(fileindex,
2275 bump_base_commit_id, &bbc_arg);
2276 if (err)
2277 break;
2279 tpd = SIMPLEQ_NEXT(tpd, entry);
2281 sync_err = sync_fileindex(fileindex, fileindex_path);
2282 if (sync_err && err == NULL)
2283 err = sync_err;
2284 done:
2285 free(fileindex_path);
2286 if (tree)
2287 got_object_tree_close(tree);
2288 if (commit)
2289 got_object_commit_close(commit);
2290 if (fileindex)
2291 got_fileindex_free(fileindex);
2292 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2293 tpd = SIMPLEQ_FIRST(&tree_paths);
2294 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2295 free(tpd->relpath);
2296 free(tpd->tree_id);
2297 free(tpd);
2299 unlockerr = lock_worktree(worktree, LOCK_SH);
2300 if (unlockerr && err == NULL)
2301 err = unlockerr;
2302 return err;
2305 struct merge_file_cb_arg {
2306 struct got_worktree *worktree;
2307 struct got_fileindex *fileindex;
2308 got_worktree_checkout_cb progress_cb;
2309 void *progress_arg;
2310 got_cancel_cb cancel_cb;
2311 void *cancel_arg;
2312 const char *label_orig;
2313 struct got_object_id *commit_id2;
2316 static const struct got_error *
2317 merge_file_cb(void *arg, struct got_blob_object *blob1,
2318 struct got_blob_object *blob2, struct got_object_id *id1,
2319 struct got_object_id *id2, const char *path1, const char *path2,
2320 mode_t mode1, mode_t mode2, struct got_repository *repo)
2322 static const struct got_error *err = NULL;
2323 struct merge_file_cb_arg *a = arg;
2324 struct got_fileindex_entry *ie;
2325 char *ondisk_path = NULL;
2326 struct stat sb;
2327 unsigned char status;
2328 int local_changes_subsumed;
2330 if (blob1 && blob2) {
2331 ie = got_fileindex_entry_get(a->fileindex, path2,
2332 strlen(path2));
2333 if (ie == NULL)
2334 return (*a->progress_cb)(a->progress_arg,
2335 GOT_STATUS_MISSING, path2);
2337 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2338 path2) == -1)
2339 return got_error_from_errno("asprintf");
2341 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2342 repo);
2343 if (err)
2344 goto done;
2346 if (status == GOT_STATUS_DELETE) {
2347 err = (*a->progress_cb)(a->progress_arg,
2348 GOT_STATUS_MERGE, path2);
2349 goto done;
2351 if (status != GOT_STATUS_NO_CHANGE &&
2352 status != GOT_STATUS_MODIFY &&
2353 status != GOT_STATUS_CONFLICT &&
2354 status != GOT_STATUS_ADD) {
2355 err = (*a->progress_cb)(a->progress_arg, status, path2);
2356 goto done;
2359 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2360 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2361 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2362 } else if (blob1) {
2363 ie = got_fileindex_entry_get(a->fileindex, path1,
2364 strlen(path1));
2365 if (ie == NULL)
2366 return (*a->progress_cb)(a->progress_arg,
2367 GOT_STATUS_MISSING, path1);
2369 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2370 path1) == -1)
2371 return got_error_from_errno("asprintf");
2373 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2374 repo);
2375 if (err)
2376 goto done;
2378 switch (status) {
2379 case GOT_STATUS_NO_CHANGE:
2380 err = (*a->progress_cb)(a->progress_arg,
2381 GOT_STATUS_DELETE, path1);
2382 if (err)
2383 goto done;
2384 err = remove_ondisk_file(a->worktree->root_path, path1);
2385 if (err)
2386 goto done;
2387 if (ie)
2388 got_fileindex_entry_mark_deleted_from_disk(ie);
2389 break;
2390 case GOT_STATUS_DELETE:
2391 case GOT_STATUS_MISSING:
2392 err = (*a->progress_cb)(a->progress_arg,
2393 GOT_STATUS_DELETE, path1);
2394 if (err)
2395 goto done;
2396 if (ie)
2397 got_fileindex_entry_mark_deleted_from_disk(ie);
2398 break;
2399 case GOT_STATUS_ADD:
2400 case GOT_STATUS_MODIFY:
2401 case GOT_STATUS_CONFLICT:
2402 err = (*a->progress_cb)(a->progress_arg,
2403 GOT_STATUS_CANNOT_DELETE, path1);
2404 if (err)
2405 goto done;
2406 break;
2407 case GOT_STATUS_OBSTRUCTED:
2408 err = (*a->progress_cb)(a->progress_arg, status, path1);
2409 if (err)
2410 goto done;
2411 break;
2412 default:
2413 break;
2415 } else if (blob2) {
2416 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2417 path2) == -1)
2418 return got_error_from_errno("asprintf");
2419 ie = got_fileindex_entry_get(a->fileindex, path2,
2420 strlen(path2));
2421 if (ie) {
2422 err = get_file_status(&status, &sb, ie, ondisk_path,
2423 -1, NULL, repo);
2424 if (err)
2425 goto done;
2426 if (status != GOT_STATUS_NO_CHANGE &&
2427 status != GOT_STATUS_MODIFY &&
2428 status != GOT_STATUS_CONFLICT &&
2429 status != GOT_STATUS_ADD) {
2430 err = (*a->progress_cb)(a->progress_arg,
2431 status, path2);
2432 goto done;
2434 err = merge_blob(&local_changes_subsumed, a->worktree,
2435 NULL, ondisk_path, path2, sb.st_mode,
2436 a->label_orig, blob2, a->commit_id2, repo,
2437 a->progress_cb,
2438 a->progress_arg);
2439 if (status == GOT_STATUS_DELETE) {
2440 err = got_fileindex_entry_update(ie,
2441 ondisk_path, blob2->id.sha1,
2442 a->worktree->base_commit_id->sha1, 0);
2443 if (err)
2444 goto done;
2446 } else {
2447 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2448 err = install_blob(a->worktree, ondisk_path, path2,
2449 /* XXX get this from parent tree! */
2450 GOT_DEFAULT_FILE_MODE,
2451 sb.st_mode, blob2, 0, 0, repo,
2452 a->progress_cb, a->progress_arg);
2453 if (err)
2454 goto done;
2455 err = got_fileindex_entry_alloc(&ie, path2);
2456 if (err)
2457 goto done;
2458 err = got_fileindex_entry_update(ie, ondisk_path,
2459 NULL, NULL, 1);
2460 if (err) {
2461 got_fileindex_entry_free(ie);
2462 goto done;
2464 err = got_fileindex_entry_add(a->fileindex, ie);
2465 if (err) {
2466 got_fileindex_entry_free(ie);
2467 goto done;
2471 done:
2472 free(ondisk_path);
2473 return err;
2476 struct check_merge_ok_arg {
2477 struct got_worktree *worktree;
2478 struct got_repository *repo;
2481 static const struct got_error *
2482 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2484 const struct got_error *err = NULL;
2485 struct check_merge_ok_arg *a = arg;
2486 unsigned char status;
2487 struct stat sb;
2488 char *ondisk_path;
2490 /* Reject merges into a work tree with mixed base commits. */
2491 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2492 SHA1_DIGEST_LENGTH))
2493 return got_error(GOT_ERR_MIXED_COMMITS);
2495 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2496 == -1)
2497 return got_error_from_errno("asprintf");
2499 /* Reject merges into a work tree with conflicted files. */
2500 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2501 if (err)
2502 return err;
2503 if (status == GOT_STATUS_CONFLICT)
2504 return got_error(GOT_ERR_CONFLICTS);
2506 return NULL;
2509 static const struct got_error *
2510 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2511 const char *fileindex_path, struct got_object_id *commit_id1,
2512 struct got_object_id *commit_id2, struct got_repository *repo,
2513 got_worktree_checkout_cb progress_cb, void *progress_arg,
2514 got_cancel_cb cancel_cb, void *cancel_arg)
2516 const struct got_error *err = NULL, *sync_err;
2517 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2518 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2519 struct merge_file_cb_arg arg;
2520 char *label_orig = NULL;
2522 if (commit_id1) {
2523 char *id_str;
2525 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2526 worktree->path_prefix);
2527 if (err)
2528 goto done;
2530 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2531 if (err)
2532 goto done;
2534 err = got_object_id_str(&id_str, commit_id1);
2535 if (err)
2536 goto done;
2538 if (asprintf(&label_orig, "%s: commit %s",
2539 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2540 err = got_error_from_errno("asprintf");
2541 free(id_str);
2542 goto done;
2544 free(id_str);
2547 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2548 worktree->path_prefix);
2549 if (err)
2550 goto done;
2552 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2553 if (err)
2554 goto done;
2556 arg.worktree = worktree;
2557 arg.fileindex = fileindex;
2558 arg.progress_cb = progress_cb;
2559 arg.progress_arg = progress_arg;
2560 arg.cancel_cb = cancel_cb;
2561 arg.cancel_arg = cancel_arg;
2562 arg.label_orig = label_orig;
2563 arg.commit_id2 = commit_id2;
2564 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2565 sync_err = sync_fileindex(fileindex, fileindex_path);
2566 if (sync_err && err == NULL)
2567 err = sync_err;
2568 done:
2569 if (tree1)
2570 got_object_tree_close(tree1);
2571 if (tree2)
2572 got_object_tree_close(tree2);
2573 free(label_orig);
2574 return err;
2577 const struct got_error *
2578 got_worktree_merge_files(struct got_worktree *worktree,
2579 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2580 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2581 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2583 const struct got_error *err, *unlockerr;
2584 char *fileindex_path = NULL;
2585 struct got_fileindex *fileindex = NULL;
2586 struct check_merge_ok_arg mok_arg;
2588 err = lock_worktree(worktree, LOCK_EX);
2589 if (err)
2590 return err;
2592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2593 if (err)
2594 goto done;
2596 mok_arg.worktree = worktree;
2597 mok_arg.repo = repo;
2598 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2599 &mok_arg);
2600 if (err)
2601 goto done;
2603 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2604 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2605 done:
2606 if (fileindex)
2607 got_fileindex_free(fileindex);
2608 free(fileindex_path);
2609 unlockerr = lock_worktree(worktree, LOCK_SH);
2610 if (unlockerr && err == NULL)
2611 err = unlockerr;
2612 return err;
2615 struct diff_dir_cb_arg {
2616 struct got_fileindex *fileindex;
2617 struct got_worktree *worktree;
2618 const char *status_path;
2619 size_t status_path_len;
2620 struct got_repository *repo;
2621 got_worktree_status_cb status_cb;
2622 void *status_arg;
2623 got_cancel_cb cancel_cb;
2624 void *cancel_arg;
2625 /* A pathlist containing per-directory pathlists of ignore patterns. */
2626 struct got_pathlist_head ignores;
2627 int report_unchanged;
2628 int no_ignores;
2631 static const struct got_error *
2632 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2633 int dirfd, const char *de_name,
2634 got_worktree_status_cb status_cb, void *status_arg,
2635 struct got_repository *repo, int report_unchanged)
2637 const struct got_error *err = NULL;
2638 unsigned char status = GOT_STATUS_NO_CHANGE;
2639 unsigned char staged_status = get_staged_status(ie);
2640 struct stat sb;
2641 struct got_object_id blob_id, commit_id, staged_blob_id;
2642 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2643 struct got_object_id *staged_blob_idp = NULL;
2645 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2646 if (err)
2647 return err;
2649 if (status == GOT_STATUS_NO_CHANGE &&
2650 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2651 return NULL;
2653 if (got_fileindex_entry_has_blob(ie)) {
2654 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2655 blob_idp = &blob_id;
2657 if (got_fileindex_entry_has_commit(ie)) {
2658 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2659 commit_idp = &commit_id;
2661 if (staged_status == GOT_STATUS_ADD ||
2662 staged_status == GOT_STATUS_MODIFY) {
2663 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2664 SHA1_DIGEST_LENGTH);
2665 staged_blob_idp = &staged_blob_id;
2668 return (*status_cb)(status_arg, status, staged_status,
2669 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2672 static const struct got_error *
2673 status_old_new(void *arg, struct got_fileindex_entry *ie,
2674 struct dirent *de, const char *parent_path, int dirfd)
2676 const struct got_error *err = NULL;
2677 struct diff_dir_cb_arg *a = arg;
2678 char *abspath;
2680 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2681 return got_error(GOT_ERR_CANCELLED);
2683 if (got_path_cmp(parent_path, a->status_path,
2684 strlen(parent_path), a->status_path_len) != 0 &&
2685 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2686 return NULL;
2688 if (parent_path[0]) {
2689 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2690 parent_path, de->d_name) == -1)
2691 return got_error_from_errno("asprintf");
2692 } else {
2693 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2694 de->d_name) == -1)
2695 return got_error_from_errno("asprintf");
2698 err = report_file_status(ie, abspath, dirfd, de->d_name,
2699 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2700 free(abspath);
2701 return err;
2704 static const struct got_error *
2705 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2707 struct diff_dir_cb_arg *a = arg;
2708 struct got_object_id blob_id, commit_id;
2709 unsigned char status;
2711 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2712 return got_error(GOT_ERR_CANCELLED);
2714 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2715 return NULL;
2717 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2718 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2719 if (got_fileindex_entry_has_file_on_disk(ie))
2720 status = GOT_STATUS_MISSING;
2721 else
2722 status = GOT_STATUS_DELETE;
2723 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2724 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2727 void
2728 free_ignorelist(struct got_pathlist_head *ignorelist)
2730 struct got_pathlist_entry *pe;
2732 TAILQ_FOREACH(pe, ignorelist, entry)
2733 free((char *)pe->path);
2734 got_pathlist_free(ignorelist);
2737 void
2738 free_ignores(struct got_pathlist_head *ignores)
2740 struct got_pathlist_entry *pe;
2742 TAILQ_FOREACH(pe, ignores, entry) {
2743 struct got_pathlist_head *ignorelist = pe->data;
2744 free_ignorelist(ignorelist);
2745 free((char *)pe->path);
2747 got_pathlist_free(ignores);
2750 static const struct got_error *
2751 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2753 const struct got_error *err = NULL;
2754 struct got_pathlist_entry *pe = NULL;
2755 struct got_pathlist_head *ignorelist;
2756 char *line = NULL, *pattern, *dirpath = NULL;
2757 size_t linesize = 0;
2758 ssize_t linelen;
2760 ignorelist = calloc(1, sizeof(*ignorelist));
2761 if (ignorelist == NULL)
2762 return got_error_from_errno("calloc");
2763 TAILQ_INIT(ignorelist);
2765 while ((linelen = getline(&line, &linesize, f)) != -1) {
2766 if (linelen > 0 && line[linelen - 1] == '\n')
2767 line[linelen - 1] = '\0';
2769 /* Git's ignores may contain comments. */
2770 if (line[0] == '#')
2771 continue;
2773 /* Git's negated patterns are not (yet?) supported. */
2774 if (line[0] == '!')
2775 continue;
2777 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2778 line) == -1) {
2779 err = got_error_from_errno("asprintf");
2780 goto done;
2782 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2783 if (err)
2784 goto done;
2786 if (ferror(f)) {
2787 err = got_error_from_errno("getline");
2788 goto done;
2791 dirpath = strdup(path);
2792 if (dirpath == NULL) {
2793 err = got_error_from_errno("strdup");
2794 goto done;
2796 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2797 done:
2798 free(line);
2799 if (err || pe == NULL) {
2800 free(dirpath);
2801 free_ignorelist(ignorelist);
2803 return err;
2806 int
2807 match_ignores(struct got_pathlist_head *ignores, const char *path)
2809 struct got_pathlist_entry *pe;
2811 /* Handle patterns which match in all directories. */
2812 TAILQ_FOREACH(pe, ignores, entry) {
2813 struct got_pathlist_head *ignorelist = pe->data;
2814 struct got_pathlist_entry *pi;
2816 TAILQ_FOREACH(pi, ignorelist, entry) {
2817 const char *p, *pattern = pi->path;
2819 if (strncmp(pattern, "**/", 3) != 0)
2820 continue;
2821 pattern += 3;
2822 p = path;
2823 while (*p) {
2824 if (fnmatch(pattern, p,
2825 FNM_PATHNAME | FNM_LEADING_DIR)) {
2826 /* Retry in next directory. */
2827 while (*p && *p != '/')
2828 p++;
2829 while (*p == '/')
2830 p++;
2831 continue;
2833 return 1;
2839 * The ignores pathlist contains ignore lists from children before
2840 * parents, so we can find the most specific ignorelist by walking
2841 * ignores backwards.
2843 pe = TAILQ_LAST(ignores, got_pathlist_head);
2844 while (pe) {
2845 if (got_path_is_child(path, pe->path, pe->path_len)) {
2846 struct got_pathlist_head *ignorelist = pe->data;
2847 struct got_pathlist_entry *pi;
2848 TAILQ_FOREACH(pi, ignorelist, entry) {
2849 const char *pattern = pi->path;
2850 int flags = FNM_LEADING_DIR;
2851 if (strstr(pattern, "/**/") == NULL)
2852 flags |= FNM_PATHNAME;
2853 if (fnmatch(pattern, path, flags))
2854 continue;
2855 return 1;
2858 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2861 return 0;
2864 static const struct got_error *
2865 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2866 const char *path, int dirfd, const char *ignores_filename)
2868 const struct got_error *err = NULL;
2869 char *ignorespath;
2870 int fd = -1;
2871 FILE *ignoresfile = NULL;
2873 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2874 path[0] ? "/" : "", ignores_filename) == -1)
2875 return got_error_from_errno("asprintf");
2877 if (dirfd != -1) {
2878 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2879 if (fd == -1) {
2880 if (errno != ENOENT && errno != EACCES)
2881 err = got_error_from_errno2("openat",
2882 ignorespath);
2883 } else {
2884 ignoresfile = fdopen(fd, "r");
2885 if (ignoresfile == NULL)
2886 err = got_error_from_errno2("fdopen",
2887 ignorespath);
2888 else {
2889 fd = -1;
2890 err = read_ignores(ignores, path, ignoresfile);
2893 } else {
2894 ignoresfile = fopen(ignorespath, "r");
2895 if (ignoresfile == NULL) {
2896 if (errno != ENOENT && errno != EACCES)
2897 err = got_error_from_errno2("fopen",
2898 ignorespath);
2899 } else
2900 err = read_ignores(ignores, path, ignoresfile);
2903 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2904 err = got_error_from_errno2("fclose", path);
2905 if (fd != -1 && close(fd) == -1 && err == NULL)
2906 err = got_error_from_errno2("close", path);
2907 free(ignorespath);
2908 return err;
2911 static const struct got_error *
2912 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2914 const struct got_error *err = NULL;
2915 struct diff_dir_cb_arg *a = arg;
2916 char *path = NULL;
2918 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2919 return got_error(GOT_ERR_CANCELLED);
2921 if (parent_path[0]) {
2922 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2923 return got_error_from_errno("asprintf");
2924 } else {
2925 path = de->d_name;
2928 if (de->d_type != DT_DIR &&
2929 got_path_is_child(path, a->status_path, a->status_path_len)
2930 && !match_ignores(&a->ignores, path))
2931 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2932 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2933 if (parent_path[0])
2934 free(path);
2935 return err;
2938 static const struct got_error *
2939 status_traverse(void *arg, const char *path, int dirfd)
2941 const struct got_error *err = NULL;
2942 struct diff_dir_cb_arg *a = arg;
2944 if (a->no_ignores)
2945 return NULL;
2947 err = add_ignores(&a->ignores, a->worktree->root_path,
2948 path, dirfd, ".cvsignore");
2949 if (err)
2950 return err;
2952 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2953 dirfd, ".gitignore");
2955 return err;
2958 static const struct got_error *
2959 report_single_file_status(const char *path, const char *ondisk_path,
2960 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2961 void *status_arg, struct got_repository *repo, int report_unchanged)
2963 struct got_fileindex_entry *ie;
2964 struct stat sb;
2966 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2967 if (ie)
2968 return report_file_status(ie, ondisk_path, -1, NULL,
2969 status_cb, status_arg, repo, report_unchanged);
2971 if (lstat(ondisk_path, &sb) == -1) {
2972 if (errno != ENOENT)
2973 return got_error_from_errno2("lstat", ondisk_path);
2974 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2975 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2976 return NULL;
2979 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2980 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2981 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2983 return NULL;
2986 static const struct got_error *
2987 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2988 const char *root_path, const char *path)
2990 const struct got_error *err;
2991 char *parent_path, *next_parent_path = NULL;
2993 err = add_ignores(ignores, root_path, "", -1,
2994 ".cvsignore");
2995 if (err)
2996 return err;
2998 err = add_ignores(ignores, root_path, "", -1,
2999 ".gitignore");
3000 if (err)
3001 return err;
3003 err = got_path_dirname(&parent_path, path);
3004 if (err) {
3005 if (err->code == GOT_ERR_BAD_PATH)
3006 return NULL; /* cannot traverse parent */
3007 return err;
3009 for (;;) {
3010 err = add_ignores(ignores, root_path, parent_path, -1,
3011 ".cvsignore");
3012 if (err)
3013 break;
3014 err = add_ignores(ignores, root_path, parent_path, -1,
3015 ".gitignore");
3016 if (err)
3017 break;
3018 err = got_path_dirname(&next_parent_path, parent_path);
3019 if (err) {
3020 if (err->code == GOT_ERR_BAD_PATH)
3021 err = NULL; /* traversed everything */
3022 break;
3024 free(parent_path);
3025 parent_path = next_parent_path;
3026 next_parent_path = NULL;
3029 free(parent_path);
3030 free(next_parent_path);
3031 return err;
3034 static const struct got_error *
3035 worktree_status(struct got_worktree *worktree, const char *path,
3036 struct got_fileindex *fileindex, struct got_repository *repo,
3037 got_worktree_status_cb status_cb, void *status_arg,
3038 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3039 int report_unchanged)
3041 const struct got_error *err = NULL;
3042 int fd = -1;
3043 struct got_fileindex_diff_dir_cb fdiff_cb;
3044 struct diff_dir_cb_arg arg;
3045 char *ondisk_path = NULL;
3047 TAILQ_INIT(&arg.ignores);
3049 if (asprintf(&ondisk_path, "%s%s%s",
3050 worktree->root_path, path[0] ? "/" : "", path) == -1)
3051 return got_error_from_errno("asprintf");
3053 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3054 if (fd == -1) {
3055 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3056 errno != ELOOP)
3057 err = got_error_from_errno2("open", ondisk_path);
3058 else
3059 err = report_single_file_status(path, ondisk_path,
3060 fileindex, status_cb, status_arg, repo,
3061 report_unchanged);
3062 } else {
3063 fdiff_cb.diff_old_new = status_old_new;
3064 fdiff_cb.diff_old = status_old;
3065 fdiff_cb.diff_new = status_new;
3066 fdiff_cb.diff_traverse = status_traverse;
3067 arg.fileindex = fileindex;
3068 arg.worktree = worktree;
3069 arg.status_path = path;
3070 arg.status_path_len = strlen(path);
3071 arg.repo = repo;
3072 arg.status_cb = status_cb;
3073 arg.status_arg = status_arg;
3074 arg.cancel_cb = cancel_cb;
3075 arg.cancel_arg = cancel_arg;
3076 arg.report_unchanged = report_unchanged;
3077 arg.no_ignores = no_ignores;
3078 if (!no_ignores) {
3079 err = add_ignores_from_parent_paths(&arg.ignores,
3080 worktree->root_path, path);
3081 if (err)
3082 goto done;
3084 err = got_fileindex_diff_dir(fileindex, fd,
3085 worktree->root_path, path, repo, &fdiff_cb, &arg);
3087 done:
3088 free_ignores(&arg.ignores);
3089 if (fd != -1 && close(fd) != 0 && err == NULL)
3090 err = got_error_from_errno("close");
3091 free(ondisk_path);
3092 return err;
3095 const struct got_error *
3096 got_worktree_status(struct got_worktree *worktree,
3097 struct got_pathlist_head *paths, struct got_repository *repo,
3098 got_worktree_status_cb status_cb, void *status_arg,
3099 got_cancel_cb cancel_cb, void *cancel_arg)
3101 const struct got_error *err = NULL;
3102 char *fileindex_path = NULL;
3103 struct got_fileindex *fileindex = NULL;
3104 struct got_pathlist_entry *pe;
3106 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3107 if (err)
3108 return err;
3110 TAILQ_FOREACH(pe, paths, entry) {
3111 err = worktree_status(worktree, pe->path, fileindex, repo,
3112 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3113 if (err)
3114 break;
3116 free(fileindex_path);
3117 got_fileindex_free(fileindex);
3118 return err;
3121 const struct got_error *
3122 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3123 const char *arg)
3125 const struct got_error *err = NULL;
3126 char *resolved = NULL, *cwd = NULL, *path = NULL;
3127 size_t len;
3128 struct stat sb;
3130 *wt_path = NULL;
3132 cwd = getcwd(NULL, 0);
3133 if (cwd == NULL)
3134 return got_error_from_errno("getcwd");
3136 if (lstat(arg, &sb) == -1) {
3137 if (errno != ENOENT) {
3138 err = got_error_from_errno2("lstat", arg);
3139 goto done;
3142 if (S_ISLNK(sb.st_mode)) {
3144 * We cannot use realpath(3) with symlinks since we want to
3145 * operate on the symlink itself.
3146 * But we can make the path absolute, assuming it is relative
3147 * to the current working directory, and then canonicalize it.
3149 char *abspath = NULL;
3150 char canonpath[PATH_MAX];
3151 if (!got_path_is_absolute(arg)) {
3152 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3153 err = got_error_from_errno("asprintf");
3154 goto done;
3158 err = got_canonpath(abspath ? abspath : arg, canonpath,
3159 sizeof(canonpath));
3160 if (err)
3161 goto done;
3162 resolved = strdup(canonpath);
3163 if (resolved == NULL) {
3164 err = got_error_from_errno("strdup");
3165 goto done;
3167 } else {
3168 resolved = realpath(arg, NULL);
3169 if (resolved == NULL) {
3170 if (errno != ENOENT) {
3171 err = got_error_from_errno2("realpath", arg);
3172 goto done;
3174 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3175 err = got_error_from_errno("asprintf");
3176 goto done;
3181 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3182 strlen(got_worktree_get_root_path(worktree)))) {
3183 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3184 goto done;
3187 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3188 err = got_path_skip_common_ancestor(&path,
3189 got_worktree_get_root_path(worktree), resolved);
3190 if (err)
3191 goto done;
3192 } else {
3193 path = strdup("");
3194 if (path == NULL) {
3195 err = got_error_from_errno("strdup");
3196 goto done;
3200 /* XXX status walk can't deal with trailing slash! */
3201 len = strlen(path);
3202 while (len > 0 && path[len - 1] == '/') {
3203 path[len - 1] = '\0';
3204 len--;
3206 done:
3207 free(resolved);
3208 free(cwd);
3209 if (err == NULL)
3210 *wt_path = path;
3211 else
3212 free(path);
3213 return err;
3216 struct schedule_addition_args {
3217 struct got_worktree *worktree;
3218 struct got_fileindex *fileindex;
3219 got_worktree_checkout_cb progress_cb;
3220 void *progress_arg;
3221 struct got_repository *repo;
3224 static const struct got_error *
3225 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3226 const char *relpath, struct got_object_id *blob_id,
3227 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3228 int dirfd, const char *de_name)
3230 struct schedule_addition_args *a = arg;
3231 const struct got_error *err = NULL;
3232 struct got_fileindex_entry *ie;
3233 struct stat sb;
3234 char *ondisk_path;
3236 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3237 relpath) == -1)
3238 return got_error_from_errno("asprintf");
3240 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3241 if (ie) {
3242 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3243 de_name, a->repo);
3244 if (err)
3245 goto done;
3246 /* Re-adding an existing entry is a no-op. */
3247 if (status == GOT_STATUS_ADD)
3248 goto done;
3249 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3250 if (err)
3251 goto done;
3254 if (status != GOT_STATUS_UNVERSIONED) {
3255 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3256 goto done;
3259 err = got_fileindex_entry_alloc(&ie, relpath);
3260 if (err)
3261 goto done;
3262 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3263 if (err) {
3264 got_fileindex_entry_free(ie);
3265 goto done;
3267 err = got_fileindex_entry_add(a->fileindex, ie);
3268 if (err) {
3269 got_fileindex_entry_free(ie);
3270 goto done;
3272 done:
3273 free(ondisk_path);
3274 if (err)
3275 return err;
3276 if (status == GOT_STATUS_ADD)
3277 return NULL;
3278 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3281 const struct got_error *
3282 got_worktree_schedule_add(struct got_worktree *worktree,
3283 struct got_pathlist_head *paths,
3284 got_worktree_checkout_cb progress_cb, void *progress_arg,
3285 struct got_repository *repo, int no_ignores)
3287 struct got_fileindex *fileindex = NULL;
3288 char *fileindex_path = NULL;
3289 const struct got_error *err = NULL, *sync_err, *unlockerr;
3290 struct got_pathlist_entry *pe;
3291 struct schedule_addition_args saa;
3293 err = lock_worktree(worktree, LOCK_EX);
3294 if (err)
3295 return err;
3297 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3298 if (err)
3299 goto done;
3301 saa.worktree = worktree;
3302 saa.fileindex = fileindex;
3303 saa.progress_cb = progress_cb;
3304 saa.progress_arg = progress_arg;
3305 saa.repo = repo;
3307 TAILQ_FOREACH(pe, paths, entry) {
3308 err = worktree_status(worktree, pe->path, fileindex, repo,
3309 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3310 if (err)
3311 break;
3313 sync_err = sync_fileindex(fileindex, fileindex_path);
3314 if (sync_err && err == NULL)
3315 err = sync_err;
3316 done:
3317 free(fileindex_path);
3318 if (fileindex)
3319 got_fileindex_free(fileindex);
3320 unlockerr = lock_worktree(worktree, LOCK_SH);
3321 if (unlockerr && err == NULL)
3322 err = unlockerr;
3323 return err;
3326 struct schedule_deletion_args {
3327 struct got_worktree *worktree;
3328 struct got_fileindex *fileindex;
3329 got_worktree_delete_cb progress_cb;
3330 void *progress_arg;
3331 struct got_repository *repo;
3332 int delete_local_mods;
3333 int keep_on_disk;
3336 static const struct got_error *
3337 schedule_for_deletion(void *arg, unsigned char status,
3338 unsigned char staged_status, const char *relpath,
3339 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3340 struct got_object_id *commit_id, int dirfd, const char *de_name)
3342 struct schedule_deletion_args *a = arg;
3343 const struct got_error *err = NULL;
3344 struct got_fileindex_entry *ie = NULL;
3345 struct stat sb;
3346 char *ondisk_path, *parent = NULL;
3348 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3349 if (ie == NULL)
3350 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3352 staged_status = get_staged_status(ie);
3353 if (staged_status != GOT_STATUS_NO_CHANGE) {
3354 if (staged_status == GOT_STATUS_DELETE)
3355 return NULL;
3356 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3359 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3360 relpath) == -1)
3361 return got_error_from_errno("asprintf");
3363 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3364 a->repo);
3365 if (err)
3366 goto done;
3368 if (status != GOT_STATUS_NO_CHANGE) {
3369 if (status == GOT_STATUS_DELETE)
3370 goto done;
3371 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3372 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3373 goto done;
3375 if (status != GOT_STATUS_MODIFY &&
3376 status != GOT_STATUS_MISSING) {
3377 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3378 goto done;
3382 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3383 if (dirfd != -1) {
3384 if (unlinkat(dirfd, de_name, 0) != 0) {
3385 err = got_error_from_errno2("unlinkat",
3386 ondisk_path);
3387 goto done;
3389 } else if (unlink(ondisk_path) != 0) {
3390 err = got_error_from_errno2("unlink", ondisk_path);
3391 goto done;
3394 parent = dirname(ondisk_path);
3396 if (parent == NULL) {
3397 err = got_error_from_errno2("dirname", ondisk_path);
3398 goto done;
3400 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3401 if (rmdir(parent) == -1) {
3402 if (errno != ENOTEMPTY)
3403 err = got_error_from_errno2("rmdir",
3404 parent);
3405 break;
3407 parent = dirname(parent);
3408 if (parent == NULL) {
3409 err = got_error_from_errno2("dirname", parent);
3410 goto done;
3415 got_fileindex_entry_mark_deleted_from_disk(ie);
3416 done:
3417 free(ondisk_path);
3418 if (err)
3419 return err;
3420 if (status == GOT_STATUS_DELETE)
3421 return NULL;
3422 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3423 staged_status, relpath);
3426 const struct got_error *
3427 got_worktree_schedule_delete(struct got_worktree *worktree,
3428 struct got_pathlist_head *paths, int delete_local_mods,
3429 got_worktree_delete_cb progress_cb, void *progress_arg,
3430 struct got_repository *repo, int keep_on_disk)
3432 struct got_fileindex *fileindex = NULL;
3433 char *fileindex_path = NULL;
3434 const struct got_error *err = NULL, *sync_err, *unlockerr;
3435 struct got_pathlist_entry *pe;
3436 struct schedule_deletion_args sda;
3438 err = lock_worktree(worktree, LOCK_EX);
3439 if (err)
3440 return err;
3442 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3443 if (err)
3444 goto done;
3446 sda.worktree = worktree;
3447 sda.fileindex = fileindex;
3448 sda.progress_cb = progress_cb;
3449 sda.progress_arg = progress_arg;
3450 sda.repo = repo;
3451 sda.delete_local_mods = delete_local_mods;
3452 sda.keep_on_disk = keep_on_disk;
3454 TAILQ_FOREACH(pe, paths, entry) {
3455 err = worktree_status(worktree, pe->path, fileindex, repo,
3456 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3457 if (err)
3458 break;
3460 sync_err = sync_fileindex(fileindex, fileindex_path);
3461 if (sync_err && err == NULL)
3462 err = sync_err;
3463 done:
3464 free(fileindex_path);
3465 if (fileindex)
3466 got_fileindex_free(fileindex);
3467 unlockerr = lock_worktree(worktree, LOCK_SH);
3468 if (unlockerr && err == NULL)
3469 err = unlockerr;
3470 return err;
3473 static const struct got_error *
3474 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3476 const struct got_error *err = NULL;
3477 char *line = NULL;
3478 size_t linesize = 0, n;
3479 ssize_t linelen;
3481 linelen = getline(&line, &linesize, infile);
3482 if (linelen == -1) {
3483 if (ferror(infile)) {
3484 err = got_error_from_errno("getline");
3485 goto done;
3487 return NULL;
3489 if (outfile) {
3490 n = fwrite(line, 1, linelen, outfile);
3491 if (n != linelen) {
3492 err = got_ferror(outfile, GOT_ERR_IO);
3493 goto done;
3496 if (rejectfile) {
3497 n = fwrite(line, 1, linelen, rejectfile);
3498 if (n != linelen)
3499 err = got_ferror(outfile, GOT_ERR_IO);
3501 done:
3502 free(line);
3503 return err;
3506 static const struct got_error *
3507 skip_one_line(FILE *f)
3509 char *line = NULL;
3510 size_t linesize = 0;
3511 ssize_t linelen;
3513 linelen = getline(&line, &linesize, f);
3514 if (linelen == -1) {
3515 if (ferror(f))
3516 return got_error_from_errno("getline");
3517 return NULL;
3519 free(line);
3520 return NULL;
3523 static const struct got_error *
3524 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3525 int start_old, int end_old, int start_new, int end_new,
3526 FILE *outfile, FILE *rejectfile)
3528 const struct got_error *err;
3530 /* Copy old file's lines leading up to patch. */
3531 while (!feof(f1) && *line_cur1 < start_old) {
3532 err = copy_one_line(f1, outfile, NULL);
3533 if (err)
3534 return err;
3535 (*line_cur1)++;
3537 /* Skip new file's lines leading up to patch. */
3538 while (!feof(f2) && *line_cur2 < start_new) {
3539 if (rejectfile)
3540 err = copy_one_line(f2, NULL, rejectfile);
3541 else
3542 err = skip_one_line(f2);
3543 if (err)
3544 return err;
3545 (*line_cur2)++;
3547 /* Copy patched lines. */
3548 while (!feof(f2) && *line_cur2 <= end_new) {
3549 err = copy_one_line(f2, outfile, NULL);
3550 if (err)
3551 return err;
3552 (*line_cur2)++;
3554 /* Skip over old file's replaced lines. */
3555 while (!feof(f1) && *line_cur1 <= end_old) {
3556 if (rejectfile)
3557 err = copy_one_line(f1, NULL, rejectfile);
3558 else
3559 err = skip_one_line(f1);
3560 if (err)
3561 return err;
3562 (*line_cur1)++;
3565 return NULL;
3568 static const struct got_error *
3569 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3570 FILE *outfile, FILE *rejectfile)
3572 const struct got_error *err;
3574 if (outfile) {
3575 /* Copy old file's lines until EOF. */
3576 while (!feof(f1)) {
3577 err = copy_one_line(f1, outfile, NULL);
3578 if (err)
3579 return err;
3580 (*line_cur1)++;
3583 if (rejectfile) {
3584 /* Copy new file's lines until EOF. */
3585 while (!feof(f2)) {
3586 err = copy_one_line(f2, NULL, rejectfile);
3587 if (err)
3588 return err;
3589 (*line_cur2)++;
3593 return NULL;
3596 static const struct got_error *
3597 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3598 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3599 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3600 int *line_cur2, FILE *outfile, FILE *rejectfile,
3601 got_worktree_patch_cb patch_cb, void *patch_arg)
3603 const struct got_error *err = NULL;
3604 int start_old = change->cv.a;
3605 int end_old = change->cv.b;
3606 int start_new = change->cv.c;
3607 int end_new = change->cv.d;
3608 long pos1, pos2;
3609 FILE *hunkfile;
3611 *choice = GOT_PATCH_CHOICE_NONE;
3613 hunkfile = got_opentemp();
3614 if (hunkfile == NULL)
3615 return got_error_from_errno("got_opentemp");
3617 pos1 = ftell(f1);
3618 pos2 = ftell(f2);
3620 /* XXX TODO needs error checking */
3621 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3623 if (fseek(f1, pos1, SEEK_SET) == -1) {
3624 err = got_ferror(f1, GOT_ERR_IO);
3625 goto done;
3627 if (fseek(f2, pos2, SEEK_SET) == -1) {
3628 err = got_ferror(f1, GOT_ERR_IO);
3629 goto done;
3631 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3632 err = got_ferror(hunkfile, GOT_ERR_IO);
3633 goto done;
3636 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3637 hunkfile, n, nchanges);
3638 if (err)
3639 goto done;
3641 switch (*choice) {
3642 case GOT_PATCH_CHOICE_YES:
3643 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3644 end_old, start_new, end_new, outfile, rejectfile);
3645 break;
3646 case GOT_PATCH_CHOICE_NO:
3647 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3648 end_old, start_new, end_new, rejectfile, outfile);
3649 break;
3650 case GOT_PATCH_CHOICE_QUIT:
3651 break;
3652 default:
3653 err = got_error(GOT_ERR_PATCH_CHOICE);
3654 break;
3656 done:
3657 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3658 err = got_error_from_errno("fclose");
3659 return err;
3662 struct revert_file_args {
3663 struct got_worktree *worktree;
3664 struct got_fileindex *fileindex;
3665 got_worktree_checkout_cb progress_cb;
3666 void *progress_arg;
3667 got_worktree_patch_cb patch_cb;
3668 void *patch_arg;
3669 struct got_repository *repo;
3672 static const struct got_error *
3673 create_patched_content(char **path_outfile, int reverse_patch,
3674 struct got_object_id *blob_id, const char *path2,
3675 int dirfd2, const char *de_name2,
3676 const char *relpath, struct got_repository *repo,
3677 got_worktree_patch_cb patch_cb, void *patch_arg)
3679 const struct got_error *err;
3680 struct got_blob_object *blob = NULL;
3681 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3682 int fd2 = -1;
3683 char *path1 = NULL, *id_str = NULL;
3684 struct stat sb1, sb2;
3685 struct got_diff_changes *changes = NULL;
3686 struct got_diff_state *ds = NULL;
3687 struct got_diff_args *args = NULL;
3688 struct got_diff_change *change;
3689 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3690 int n = 0;
3692 *path_outfile = NULL;
3694 err = got_object_id_str(&id_str, blob_id);
3695 if (err)
3696 return err;
3698 if (dirfd2 != -1) {
3699 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3700 if (fd2 == -1) {
3701 err = got_error_from_errno2("openat", path2);
3702 goto done;
3704 } else {
3705 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3706 if (fd2 == -1) {
3707 err = got_error_from_errno2("open", path2);
3708 goto done;
3711 if (fstat(fd2, &sb2) == -1) {
3712 err = got_error_from_errno2("fstat", path2);
3713 goto done;
3716 f2 = fdopen(fd2, "r");
3717 if (f2 == NULL) {
3718 err = got_error_from_errno2("fdopen", path2);
3719 goto done;
3721 fd2 = -1;
3723 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3724 if (err)
3725 goto done;
3727 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3728 if (err)
3729 goto done;
3731 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3732 if (err)
3733 goto done;
3735 if (stat(path1, &sb1) == -1) {
3736 err = got_error_from_errno2("stat", path1);
3737 goto done;
3740 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3741 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3742 if (err)
3743 goto done;
3745 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3746 if (err)
3747 goto done;
3749 if (fseek(f1, 0L, SEEK_SET) == -1)
3750 return got_ferror(f1, GOT_ERR_IO);
3751 if (fseek(f2, 0L, SEEK_SET) == -1)
3752 return got_ferror(f2, GOT_ERR_IO);
3753 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3754 int choice;
3755 err = apply_or_reject_change(&choice, change, ++n,
3756 changes->nchanges, ds, args, diff_flags, relpath,
3757 f1, f2, &line_cur1, &line_cur2,
3758 reverse_patch ? NULL : outfile,
3759 reverse_patch ? outfile : NULL,
3760 patch_cb, patch_arg);
3761 if (err)
3762 goto done;
3763 if (choice == GOT_PATCH_CHOICE_YES)
3764 have_content = 1;
3765 else if (choice == GOT_PATCH_CHOICE_QUIT)
3766 break;
3768 if (have_content) {
3769 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3770 reverse_patch ? NULL : outfile,
3771 reverse_patch ? outfile : NULL);
3772 if (err)
3773 goto done;
3775 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3776 err = got_error_from_errno2("chmod", path2);
3777 goto done;
3780 done:
3781 free(id_str);
3782 if (blob)
3783 got_object_blob_close(blob);
3784 if (f1 && fclose(f1) == EOF && err == NULL)
3785 err = got_error_from_errno2("fclose", path1);
3786 if (f2 && fclose(f2) == EOF && err == NULL)
3787 err = got_error_from_errno2("fclose", path2);
3788 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3789 err = got_error_from_errno2("close", path2);
3790 if (outfile && fclose(outfile) == EOF && err == NULL)
3791 err = got_error_from_errno2("fclose", *path_outfile);
3792 if (path1 && unlink(path1) == -1 && err == NULL)
3793 err = got_error_from_errno2("unlink", path1);
3794 if (err || !have_content) {
3795 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3796 err = got_error_from_errno2("unlink", *path_outfile);
3797 free(*path_outfile);
3798 *path_outfile = NULL;
3800 free(args);
3801 if (ds) {
3802 got_diff_state_free(ds);
3803 free(ds);
3805 if (changes)
3806 got_diff_free_changes(changes);
3807 free(path1);
3808 return err;
3811 static const struct got_error *
3812 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3813 const char *relpath, struct got_object_id *blob_id,
3814 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3815 int dirfd, const char *de_name)
3817 struct revert_file_args *a = arg;
3818 const struct got_error *err = NULL;
3819 char *parent_path = NULL;
3820 struct got_fileindex_entry *ie;
3821 struct got_tree_object *tree = NULL;
3822 struct got_object_id *tree_id = NULL;
3823 const struct got_tree_entry *te = NULL;
3824 char *tree_path = NULL, *te_name;
3825 char *ondisk_path = NULL, *path_content = NULL;
3826 struct got_blob_object *blob = NULL;
3828 /* Reverting a staged deletion is a no-op. */
3829 if (status == GOT_STATUS_DELETE &&
3830 staged_status != GOT_STATUS_NO_CHANGE)
3831 return NULL;
3833 if (status == GOT_STATUS_UNVERSIONED)
3834 return (*a->progress_cb)(a->progress_arg,
3835 GOT_STATUS_UNVERSIONED, relpath);
3837 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3838 if (ie == NULL)
3839 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3841 /* Construct in-repository path of tree which contains this blob. */
3842 err = got_path_dirname(&parent_path, ie->path);
3843 if (err) {
3844 if (err->code != GOT_ERR_BAD_PATH)
3845 goto done;
3846 parent_path = strdup("/");
3847 if (parent_path == NULL) {
3848 err = got_error_from_errno("strdup");
3849 goto done;
3852 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3853 tree_path = strdup(parent_path);
3854 if (tree_path == NULL) {
3855 err = got_error_from_errno("strdup");
3856 goto done;
3858 } else {
3859 if (got_path_is_root_dir(parent_path)) {
3860 tree_path = strdup(a->worktree->path_prefix);
3861 if (tree_path == NULL) {
3862 err = got_error_from_errno("strdup");
3863 goto done;
3865 } else {
3866 if (asprintf(&tree_path, "%s/%s",
3867 a->worktree->path_prefix, parent_path) == -1) {
3868 err = got_error_from_errno("asprintf");
3869 goto done;
3874 err = got_object_id_by_path(&tree_id, a->repo,
3875 a->worktree->base_commit_id, tree_path);
3876 if (err) {
3877 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3878 (status == GOT_STATUS_ADD ||
3879 staged_status == GOT_STATUS_ADD)))
3880 goto done;
3881 } else {
3882 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3883 if (err)
3884 goto done;
3886 te_name = basename(ie->path);
3887 if (te_name == NULL) {
3888 err = got_error_from_errno2("basename", ie->path);
3889 goto done;
3892 te = got_object_tree_find_entry(tree, te_name);
3893 if (te == NULL && status != GOT_STATUS_ADD &&
3894 staged_status != GOT_STATUS_ADD) {
3895 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3896 goto done;
3900 switch (status) {
3901 case GOT_STATUS_ADD:
3902 if (a->patch_cb) {
3903 int choice = GOT_PATCH_CHOICE_NONE;
3904 err = (*a->patch_cb)(&choice, a->patch_arg,
3905 status, ie->path, NULL, 1, 1);
3906 if (err)
3907 goto done;
3908 if (choice != GOT_PATCH_CHOICE_YES)
3909 break;
3911 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3912 ie->path);
3913 if (err)
3914 goto done;
3915 got_fileindex_entry_remove(a->fileindex, ie);
3916 break;
3917 case GOT_STATUS_DELETE:
3918 if (a->patch_cb) {
3919 int choice = GOT_PATCH_CHOICE_NONE;
3920 err = (*a->patch_cb)(&choice, a->patch_arg,
3921 status, ie->path, NULL, 1, 1);
3922 if (err)
3923 goto done;
3924 if (choice != GOT_PATCH_CHOICE_YES)
3925 break;
3927 /* fall through */
3928 case GOT_STATUS_MODIFY:
3929 case GOT_STATUS_MODE_CHANGE:
3930 case GOT_STATUS_CONFLICT:
3931 case GOT_STATUS_MISSING: {
3932 struct got_object_id id;
3933 if (staged_status == GOT_STATUS_ADD ||
3934 staged_status == GOT_STATUS_MODIFY) {
3935 memcpy(id.sha1, ie->staged_blob_sha1,
3936 SHA1_DIGEST_LENGTH);
3937 } else
3938 memcpy(id.sha1, ie->blob_sha1,
3939 SHA1_DIGEST_LENGTH);
3940 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3941 if (err)
3942 goto done;
3944 if (asprintf(&ondisk_path, "%s/%s",
3945 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3946 err = got_error_from_errno("asprintf");
3947 goto done;
3950 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3951 status == GOT_STATUS_CONFLICT)) {
3952 err = create_patched_content(&path_content, 1, &id,
3953 ondisk_path, dirfd, de_name, ie->path, a->repo,
3954 a->patch_cb, a->patch_arg);
3955 if (err || path_content == NULL)
3956 break;
3957 if (rename(path_content, ondisk_path) == -1) {
3958 err = got_error_from_errno3("rename",
3959 path_content, ondisk_path);
3960 goto done;
3962 } else {
3963 err = install_blob(a->worktree, ondisk_path, ie->path,
3964 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3965 got_fileindex_perms_to_st(ie), blob, 0, 1,
3966 a->repo, a->progress_cb, a->progress_arg);
3967 if (err)
3968 goto done;
3969 if (status == GOT_STATUS_DELETE ||
3970 status == GOT_STATUS_MODE_CHANGE) {
3971 err = got_fileindex_entry_update(ie,
3972 ondisk_path, blob->id.sha1,
3973 a->worktree->base_commit_id->sha1, 1);
3974 if (err)
3975 goto done;
3978 break;
3980 default:
3981 break;
3983 done:
3984 free(ondisk_path);
3985 free(path_content);
3986 free(parent_path);
3987 free(tree_path);
3988 if (blob)
3989 got_object_blob_close(blob);
3990 if (tree)
3991 got_object_tree_close(tree);
3992 free(tree_id);
3993 return err;
3996 const struct got_error *
3997 got_worktree_revert(struct got_worktree *worktree,
3998 struct got_pathlist_head *paths,
3999 got_worktree_checkout_cb progress_cb, void *progress_arg,
4000 got_worktree_patch_cb patch_cb, void *patch_arg,
4001 struct got_repository *repo)
4003 struct got_fileindex *fileindex = NULL;
4004 char *fileindex_path = NULL;
4005 const struct got_error *err = NULL, *unlockerr = NULL;
4006 const struct got_error *sync_err = NULL;
4007 struct got_pathlist_entry *pe;
4008 struct revert_file_args rfa;
4010 err = lock_worktree(worktree, LOCK_EX);
4011 if (err)
4012 return err;
4014 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4015 if (err)
4016 goto done;
4018 rfa.worktree = worktree;
4019 rfa.fileindex = fileindex;
4020 rfa.progress_cb = progress_cb;
4021 rfa.progress_arg = progress_arg;
4022 rfa.patch_cb = patch_cb;
4023 rfa.patch_arg = patch_arg;
4024 rfa.repo = repo;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 revert_file, &rfa, NULL, NULL, 0, 0);
4028 if (err)
4029 break;
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4033 err = sync_err;
4034 done:
4035 free(fileindex_path);
4036 if (fileindex)
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4040 err = unlockerr;
4041 return err;
4044 static void
4045 free_commitable(struct got_commitable *ct)
4047 free(ct->path);
4048 free(ct->in_repo_path);
4049 free(ct->ondisk_path);
4050 free(ct->blob_id);
4051 free(ct->base_blob_id);
4052 free(ct->staged_blob_id);
4053 free(ct->base_commit_id);
4054 free(ct);
4057 struct collect_commitables_arg {
4058 struct got_pathlist_head *commitable_paths;
4059 struct got_repository *repo;
4060 struct got_worktree *worktree;
4061 int have_staged_files;
4064 static const struct got_error *
4065 collect_commitables(void *arg, unsigned char status,
4066 unsigned char staged_status, const char *relpath,
4067 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4068 struct got_object_id *commit_id, int dirfd, const char *de_name)
4070 struct collect_commitables_arg *a = arg;
4071 const struct got_error *err = NULL;
4072 struct got_commitable *ct = NULL;
4073 struct got_pathlist_entry *new = NULL;
4074 char *parent_path = NULL, *path = NULL;
4075 struct stat sb;
4077 if (a->have_staged_files) {
4078 if (staged_status != GOT_STATUS_MODIFY &&
4079 staged_status != GOT_STATUS_ADD &&
4080 staged_status != GOT_STATUS_DELETE)
4081 return NULL;
4082 } else {
4083 if (status == GOT_STATUS_CONFLICT)
4084 return got_error(GOT_ERR_COMMIT_CONFLICT);
4086 if (status != GOT_STATUS_MODIFY &&
4087 status != GOT_STATUS_MODE_CHANGE &&
4088 status != GOT_STATUS_ADD &&
4089 status != GOT_STATUS_DELETE)
4090 return NULL;
4093 if (asprintf(&path, "/%s", relpath) == -1) {
4094 err = got_error_from_errno("asprintf");
4095 goto done;
4097 if (strcmp(path, "/") == 0) {
4098 parent_path = strdup("");
4099 if (parent_path == NULL)
4100 return got_error_from_errno("strdup");
4101 } else {
4102 err = got_path_dirname(&parent_path, path);
4103 if (err)
4104 return err;
4107 ct = calloc(1, sizeof(*ct));
4108 if (ct == NULL) {
4109 err = got_error_from_errno("calloc");
4110 goto done;
4113 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4114 relpath) == -1) {
4115 err = got_error_from_errno("asprintf");
4116 goto done;
4118 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4119 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4120 } else {
4121 if (dirfd != -1) {
4122 if (fstatat(dirfd, de_name, &sb,
4123 AT_SYMLINK_NOFOLLOW) == -1) {
4124 err = got_error_from_errno2("fstatat",
4125 ct->ondisk_path);
4126 goto done;
4128 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4129 err = got_error_from_errno2("lstat", ct->ondisk_path);
4130 goto done;
4132 ct->mode = sb.st_mode;
4135 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4136 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4137 relpath) == -1) {
4138 err = got_error_from_errno("asprintf");
4139 goto done;
4142 ct->status = status;
4143 ct->staged_status = staged_status;
4144 ct->blob_id = NULL; /* will be filled in when blob gets created */
4145 if (ct->status != GOT_STATUS_ADD &&
4146 ct->staged_status != GOT_STATUS_ADD) {
4147 ct->base_blob_id = got_object_id_dup(blob_id);
4148 if (ct->base_blob_id == NULL) {
4149 err = got_error_from_errno("got_object_id_dup");
4150 goto done;
4152 ct->base_commit_id = got_object_id_dup(commit_id);
4153 if (ct->base_commit_id == NULL) {
4154 err = got_error_from_errno("got_object_id_dup");
4155 goto done;
4158 if (ct->staged_status == GOT_STATUS_ADD ||
4159 ct->staged_status == GOT_STATUS_MODIFY) {
4160 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4161 if (ct->staged_blob_id == NULL) {
4162 err = got_error_from_errno("got_object_id_dup");
4163 goto done;
4166 ct->path = strdup(path);
4167 if (ct->path == NULL) {
4168 err = got_error_from_errno("strdup");
4169 goto done;
4171 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4172 done:
4173 if (ct && (err || new == NULL))
4174 free_commitable(ct);
4175 free(parent_path);
4176 free(path);
4177 return err;
4180 static const struct got_error *write_tree(struct got_object_id **, int *,
4181 struct got_tree_object *, const char *, struct got_pathlist_head *,
4182 got_worktree_status_cb status_cb, void *status_arg,
4183 struct got_repository *);
4185 static const struct got_error *
4186 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4187 struct got_tree_entry *te, const char *parent_path,
4188 struct got_pathlist_head *commitable_paths,
4189 got_worktree_status_cb status_cb, void *status_arg,
4190 struct got_repository *repo)
4192 const struct got_error *err = NULL;
4193 struct got_tree_object *subtree;
4194 char *subpath;
4196 if (asprintf(&subpath, "%s%s%s", parent_path,
4197 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4198 return got_error_from_errno("asprintf");
4200 err = got_object_open_as_tree(&subtree, repo, &te->id);
4201 if (err)
4202 return err;
4204 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4205 commitable_paths, status_cb, status_arg, repo);
4206 got_object_tree_close(subtree);
4207 free(subpath);
4208 return err;
4211 static const struct got_error *
4212 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4214 const struct got_error *err = NULL;
4215 char *ct_parent_path = NULL;
4217 *match = 0;
4219 if (strchr(ct->in_repo_path, '/') == NULL) {
4220 *match = got_path_is_root_dir(path);
4221 return NULL;
4224 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4225 if (err)
4226 return err;
4227 *match = (strcmp(path, ct_parent_path) == 0);
4228 free(ct_parent_path);
4229 return err;
4232 static mode_t
4233 get_ct_file_mode(struct got_commitable *ct)
4235 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4238 static const struct got_error *
4239 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4240 struct got_tree_entry *te, struct got_commitable *ct)
4242 const struct got_error *err = NULL;
4244 *new_te = NULL;
4246 err = got_object_tree_entry_dup(new_te, te);
4247 if (err)
4248 goto done;
4250 (*new_te)->mode = get_ct_file_mode(ct);
4252 if (ct->staged_status == GOT_STATUS_MODIFY)
4253 memcpy(&(*new_te)->id, ct->staged_blob_id,
4254 sizeof((*new_te)->id));
4255 else
4256 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4257 done:
4258 if (err && *new_te) {
4259 free(*new_te);
4260 *new_te = NULL;
4262 return err;
4265 static const struct got_error *
4266 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4267 struct got_commitable *ct)
4269 const struct got_error *err = NULL;
4270 char *ct_name;
4272 *new_te = NULL;
4274 *new_te = calloc(1, sizeof(**new_te));
4275 if (*new_te == NULL)
4276 return got_error_from_errno("calloc");
4278 ct_name = basename(ct->path);
4279 if (ct_name == NULL) {
4280 err = got_error_from_errno2("basename", ct->path);
4281 goto done;
4283 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4284 sizeof((*new_te)->name)) {
4285 err = got_error(GOT_ERR_NO_SPACE);
4286 goto done;
4289 (*new_te)->mode = get_ct_file_mode(ct);
4291 if (ct->staged_status == GOT_STATUS_ADD)
4292 memcpy(&(*new_te)->id, ct->staged_blob_id,
4293 sizeof((*new_te)->id));
4294 else
4295 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4296 done:
4297 if (err && *new_te) {
4298 free(*new_te);
4299 *new_te = NULL;
4301 return err;
4304 static const struct got_error *
4305 insert_tree_entry(struct got_tree_entry *new_te,
4306 struct got_pathlist_head *paths)
4308 const struct got_error *err = NULL;
4309 struct got_pathlist_entry *new_pe;
4311 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4312 if (err)
4313 return err;
4314 if (new_pe == NULL)
4315 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4316 return NULL;
4319 static const struct got_error *
4320 report_ct_status(struct got_commitable *ct,
4321 got_worktree_status_cb status_cb, void *status_arg)
4323 const char *ct_path = ct->path;
4324 unsigned char status;
4326 while (ct_path[0] == '/')
4327 ct_path++;
4329 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4330 status = ct->staged_status;
4331 else
4332 status = ct->status;
4334 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4335 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4338 static const struct got_error *
4339 match_modified_subtree(int *modified, struct got_tree_entry *te,
4340 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4342 const struct got_error *err = NULL;
4343 struct got_pathlist_entry *pe;
4344 char *te_path;
4346 *modified = 0;
4348 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4349 got_path_is_root_dir(base_tree_path) ? "" : "/",
4350 te->name) == -1)
4351 return got_error_from_errno("asprintf");
4353 TAILQ_FOREACH(pe, commitable_paths, entry) {
4354 struct got_commitable *ct = pe->data;
4355 *modified = got_path_is_child(ct->in_repo_path, te_path,
4356 strlen(te_path));
4357 if (*modified)
4358 break;
4361 free(te_path);
4362 return err;
4365 static const struct got_error *
4366 match_deleted_or_modified_ct(struct got_commitable **ctp,
4367 struct got_tree_entry *te, const char *base_tree_path,
4368 struct got_pathlist_head *commitable_paths)
4370 const struct got_error *err = NULL;
4371 struct got_pathlist_entry *pe;
4373 *ctp = NULL;
4375 TAILQ_FOREACH(pe, commitable_paths, entry) {
4376 struct got_commitable *ct = pe->data;
4377 char *ct_name = NULL;
4378 int path_matches;
4380 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4381 if (ct->status != GOT_STATUS_MODIFY &&
4382 ct->status != GOT_STATUS_MODE_CHANGE &&
4383 ct->status != GOT_STATUS_DELETE)
4384 continue;
4385 } else {
4386 if (ct->staged_status != GOT_STATUS_MODIFY &&
4387 ct->staged_status != GOT_STATUS_DELETE)
4388 continue;
4391 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4392 continue;
4394 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4395 if (err)
4396 return err;
4397 if (!path_matches)
4398 continue;
4400 ct_name = basename(pe->path);
4401 if (ct_name == NULL)
4402 return got_error_from_errno2("basename", pe->path);
4404 if (strcmp(te->name, ct_name) != 0)
4405 continue;
4407 *ctp = ct;
4408 break;
4411 return err;
4414 static const struct got_error *
4415 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4416 const char *child_path, const char *path_base_tree,
4417 struct got_pathlist_head *commitable_paths,
4418 got_worktree_status_cb status_cb, void *status_arg,
4419 struct got_repository *repo)
4421 const struct got_error *err = NULL;
4422 struct got_tree_entry *new_te;
4423 char *subtree_path;
4424 struct got_object_id *id = NULL;
4425 int nentries;
4427 *new_tep = NULL;
4429 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4430 got_path_is_root_dir(path_base_tree) ? "" : "/",
4431 child_path) == -1)
4432 return got_error_from_errno("asprintf");
4434 new_te = calloc(1, sizeof(*new_te));
4435 if (new_te == NULL)
4436 return got_error_from_errno("calloc");
4437 new_te->mode = S_IFDIR;
4439 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4440 sizeof(new_te->name)) {
4441 err = got_error(GOT_ERR_NO_SPACE);
4442 goto done;
4444 err = write_tree(&id, &nentries, NULL, subtree_path,
4445 commitable_paths, status_cb, status_arg, repo);
4446 if (err) {
4447 free(new_te);
4448 goto done;
4450 memcpy(&new_te->id, id, sizeof(new_te->id));
4451 done:
4452 free(id);
4453 free(subtree_path);
4454 if (err == NULL)
4455 *new_tep = new_te;
4456 return err;
4459 static const struct got_error *
4460 write_tree(struct got_object_id **new_tree_id, int *nentries,
4461 struct got_tree_object *base_tree, const char *path_base_tree,
4462 struct got_pathlist_head *commitable_paths,
4463 got_worktree_status_cb status_cb, void *status_arg,
4464 struct got_repository *repo)
4466 const struct got_error *err = NULL;
4467 struct got_pathlist_head paths;
4468 struct got_tree_entry *te, *new_te = NULL;
4469 struct got_pathlist_entry *pe;
4471 TAILQ_INIT(&paths);
4472 *nentries = 0;
4474 /* Insert, and recurse into, newly added entries first. */
4475 TAILQ_FOREACH(pe, commitable_paths, entry) {
4476 struct got_commitable *ct = pe->data;
4477 char *child_path = NULL, *slash;
4479 if ((ct->status != GOT_STATUS_ADD &&
4480 ct->staged_status != GOT_STATUS_ADD) ||
4481 (ct->flags & GOT_COMMITABLE_ADDED))
4482 continue;
4484 if (!got_path_is_child(pe->path, path_base_tree,
4485 strlen(path_base_tree)))
4486 continue;
4488 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4489 pe->path);
4490 if (err)
4491 goto done;
4493 slash = strchr(child_path, '/');
4494 if (slash == NULL) {
4495 err = alloc_added_blob_tree_entry(&new_te, ct);
4496 if (err)
4497 goto done;
4498 err = report_ct_status(ct, status_cb, status_arg);
4499 if (err)
4500 goto done;
4501 ct->flags |= GOT_COMMITABLE_ADDED;
4502 err = insert_tree_entry(new_te, &paths);
4503 if (err)
4504 goto done;
4505 (*nentries)++;
4506 } else {
4507 *slash = '\0'; /* trim trailing path components */
4508 if (base_tree == NULL ||
4509 got_object_tree_find_entry(base_tree, child_path)
4510 == NULL) {
4511 err = make_subtree_for_added_blob(&new_te,
4512 child_path, path_base_tree,
4513 commitable_paths, status_cb, status_arg,
4514 repo);
4515 if (err)
4516 goto done;
4517 err = insert_tree_entry(new_te, &paths);
4518 if (err)
4519 goto done;
4520 (*nentries)++;
4525 if (base_tree) {
4526 int i, nbase_entries;
4527 /* Handle modified and deleted entries. */
4528 nbase_entries = got_object_tree_get_nentries(base_tree);
4529 for (i = 0; i < nbase_entries; i++) {
4530 struct got_commitable *ct = NULL;
4532 te = got_object_tree_get_entry(base_tree, i);
4533 if (got_object_tree_entry_is_submodule(te)) {
4534 /* Entry is a submodule; just copy it. */
4535 err = got_object_tree_entry_dup(&new_te, te);
4536 if (err)
4537 goto done;
4538 err = insert_tree_entry(new_te, &paths);
4539 if (err)
4540 goto done;
4541 (*nentries)++;
4542 continue;
4545 if (S_ISDIR(te->mode)) {
4546 int modified;
4547 err = got_object_tree_entry_dup(&new_te, te);
4548 if (err)
4549 goto done;
4550 err = match_modified_subtree(&modified, te,
4551 path_base_tree, commitable_paths);
4552 if (err)
4553 goto done;
4554 /* Avoid recursion into unmodified subtrees. */
4555 if (modified) {
4556 struct got_object_id *new_id;
4557 int nsubentries;
4558 err = write_subtree(&new_id,
4559 &nsubentries, te,
4560 path_base_tree, commitable_paths,
4561 status_cb, status_arg, repo);
4562 if (err)
4563 goto done;
4564 if (nsubentries == 0) {
4565 /* All entries were deleted. */
4566 free(new_id);
4567 continue;
4569 memcpy(&new_te->id, new_id,
4570 sizeof(new_te->id));
4571 free(new_id);
4573 err = insert_tree_entry(new_te, &paths);
4574 if (err)
4575 goto done;
4576 (*nentries)++;
4577 continue;
4580 err = match_deleted_or_modified_ct(&ct, te,
4581 path_base_tree, commitable_paths);
4582 if (err)
4583 goto done;
4584 if (ct) {
4585 /* NB: Deleted entries get dropped here. */
4586 if (ct->status == GOT_STATUS_MODIFY ||
4587 ct->status == GOT_STATUS_MODE_CHANGE ||
4588 ct->staged_status == GOT_STATUS_MODIFY) {
4589 err = alloc_modified_blob_tree_entry(
4590 &new_te, te, ct);
4591 if (err)
4592 goto done;
4593 err = insert_tree_entry(new_te, &paths);
4594 if (err)
4595 goto done;
4596 (*nentries)++;
4598 err = report_ct_status(ct, status_cb,
4599 status_arg);
4600 if (err)
4601 goto done;
4602 } else {
4603 /* Entry is unchanged; just copy it. */
4604 err = got_object_tree_entry_dup(&new_te, te);
4605 if (err)
4606 goto done;
4607 err = insert_tree_entry(new_te, &paths);
4608 if (err)
4609 goto done;
4610 (*nentries)++;
4615 /* Write new list of entries; deleted entries have been dropped. */
4616 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4617 done:
4618 got_pathlist_free(&paths);
4619 return err;
4622 static const struct got_error *
4623 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4624 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4625 int have_staged_files)
4627 const struct got_error *err = NULL;
4628 struct got_pathlist_entry *pe;
4630 TAILQ_FOREACH(pe, commitable_paths, entry) {
4631 struct got_fileindex_entry *ie;
4632 struct got_commitable *ct = pe->data;
4634 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4635 if (ie) {
4636 if (ct->status == GOT_STATUS_DELETE ||
4637 ct->staged_status == GOT_STATUS_DELETE) {
4638 got_fileindex_entry_remove(fileindex, ie);
4639 } else if (ct->staged_status == GOT_STATUS_ADD ||
4640 ct->staged_status == GOT_STATUS_MODIFY) {
4641 got_fileindex_entry_stage_set(ie,
4642 GOT_FILEIDX_STAGE_NONE);
4643 err = got_fileindex_entry_update(ie,
4644 ct->ondisk_path, ct->staged_blob_id->sha1,
4645 new_base_commit_id->sha1,
4646 !have_staged_files);
4647 } else
4648 err = got_fileindex_entry_update(ie,
4649 ct->ondisk_path, ct->blob_id->sha1,
4650 new_base_commit_id->sha1,
4651 !have_staged_files);
4652 } else {
4653 err = got_fileindex_entry_alloc(&ie, pe->path);
4654 if (err)
4655 break;
4656 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4657 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4658 if (err) {
4659 got_fileindex_entry_free(ie);
4660 break;
4662 err = got_fileindex_entry_add(fileindex, ie);
4663 if (err) {
4664 got_fileindex_entry_free(ie);
4665 break;
4669 return err;
4673 static const struct got_error *
4674 check_out_of_date(const char *in_repo_path, unsigned char status,
4675 unsigned char staged_status, struct got_object_id *base_blob_id,
4676 struct got_object_id *base_commit_id,
4677 struct got_object_id *head_commit_id, struct got_repository *repo,
4678 int ood_errcode)
4680 const struct got_error *err = NULL;
4681 struct got_object_id *id = NULL;
4683 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4684 /* Trivial case: base commit == head commit */
4685 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4686 return NULL;
4688 * Ensure file content which local changes were based
4689 * on matches file content in the branch head.
4691 err = got_object_id_by_path(&id, repo, head_commit_id,
4692 in_repo_path);
4693 if (err) {
4694 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4695 err = got_error(ood_errcode);
4696 goto done;
4697 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4698 err = got_error(ood_errcode);
4699 } else {
4700 /* Require that added files don't exist in the branch head. */
4701 err = got_object_id_by_path(&id, repo, head_commit_id,
4702 in_repo_path);
4703 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4704 goto done;
4705 err = id ? got_error(ood_errcode) : NULL;
4707 done:
4708 free(id);
4709 return err;
4712 const struct got_error *
4713 commit_worktree(struct got_object_id **new_commit_id,
4714 struct got_pathlist_head *commitable_paths,
4715 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4716 const char *author, const char *committer,
4717 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4718 got_worktree_status_cb status_cb, void *status_arg,
4719 struct got_repository *repo)
4721 const struct got_error *err = NULL, *unlockerr = NULL;
4722 struct got_pathlist_entry *pe;
4723 const char *head_ref_name = NULL;
4724 struct got_commit_object *head_commit = NULL;
4725 struct got_reference *head_ref2 = NULL;
4726 struct got_object_id *head_commit_id2 = NULL;
4727 struct got_tree_object *head_tree = NULL;
4728 struct got_object_id *new_tree_id = NULL;
4729 int nentries;
4730 struct got_object_id_queue parent_ids;
4731 struct got_object_qid *pid = NULL;
4732 char *logmsg = NULL;
4734 *new_commit_id = NULL;
4736 SIMPLEQ_INIT(&parent_ids);
4738 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4739 if (err)
4740 goto done;
4742 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4743 if (err)
4744 goto done;
4746 if (commit_msg_cb != NULL) {
4747 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4748 if (err)
4749 goto done;
4752 if (logmsg == NULL || strlen(logmsg) == 0) {
4753 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4754 goto done;
4757 /* Create blobs from added and modified files and record their IDs. */
4758 TAILQ_FOREACH(pe, commitable_paths, entry) {
4759 struct got_commitable *ct = pe->data;
4760 char *ondisk_path;
4762 /* Blobs for staged files already exist. */
4763 if (ct->staged_status == GOT_STATUS_ADD ||
4764 ct->staged_status == GOT_STATUS_MODIFY)
4765 continue;
4767 if (ct->status != GOT_STATUS_ADD &&
4768 ct->status != GOT_STATUS_MODIFY &&
4769 ct->status != GOT_STATUS_MODE_CHANGE)
4770 continue;
4772 if (asprintf(&ondisk_path, "%s/%s",
4773 worktree->root_path, pe->path) == -1) {
4774 err = got_error_from_errno("asprintf");
4775 goto done;
4777 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4778 free(ondisk_path);
4779 if (err)
4780 goto done;
4783 /* Recursively write new tree objects. */
4784 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4785 commitable_paths, status_cb, status_arg, repo);
4786 if (err)
4787 goto done;
4789 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4790 if (err)
4791 goto done;
4792 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4793 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4794 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4795 got_object_qid_free(pid);
4796 if (logmsg != NULL)
4797 free(logmsg);
4798 if (err)
4799 goto done;
4801 /* Check if a concurrent commit to our branch has occurred. */
4802 head_ref_name = got_worktree_get_head_ref_name(worktree);
4803 if (head_ref_name == NULL) {
4804 err = got_error_from_errno("got_worktree_get_head_ref_name");
4805 goto done;
4807 /* Lock the reference here to prevent concurrent modification. */
4808 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4809 if (err)
4810 goto done;
4811 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4812 if (err)
4813 goto done;
4814 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4815 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4816 goto done;
4818 /* Update branch head in repository. */
4819 err = got_ref_change_ref(head_ref2, *new_commit_id);
4820 if (err)
4821 goto done;
4822 err = got_ref_write(head_ref2, repo);
4823 if (err)
4824 goto done;
4826 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4827 if (err)
4828 goto done;
4830 err = ref_base_commit(worktree, repo);
4831 if (err)
4832 goto done;
4833 done:
4834 if (head_tree)
4835 got_object_tree_close(head_tree);
4836 if (head_commit)
4837 got_object_commit_close(head_commit);
4838 free(head_commit_id2);
4839 if (head_ref2) {
4840 unlockerr = got_ref_unlock(head_ref2);
4841 if (unlockerr && err == NULL)
4842 err = unlockerr;
4843 got_ref_close(head_ref2);
4845 return err;
4848 static const struct got_error *
4849 check_path_is_commitable(const char *path,
4850 struct got_pathlist_head *commitable_paths)
4852 struct got_pathlist_entry *cpe = NULL;
4853 size_t path_len = strlen(path);
4855 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4856 struct got_commitable *ct = cpe->data;
4857 const char *ct_path = ct->path;
4859 while (ct_path[0] == '/')
4860 ct_path++;
4862 if (strcmp(path, ct_path) == 0 ||
4863 got_path_is_child(ct_path, path, path_len))
4864 break;
4867 if (cpe == NULL)
4868 return got_error_path(path, GOT_ERR_BAD_PATH);
4870 return NULL;
4873 static const struct got_error *
4874 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4876 int *have_staged_files = arg;
4878 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4879 *have_staged_files = 1;
4880 return got_error(GOT_ERR_CANCELLED);
4883 return NULL;
4886 static const struct got_error *
4887 check_non_staged_files(struct got_fileindex *fileindex,
4888 struct got_pathlist_head *paths)
4890 struct got_pathlist_entry *pe;
4891 struct got_fileindex_entry *ie;
4893 TAILQ_FOREACH(pe, paths, entry) {
4894 if (pe->path[0] == '\0')
4895 continue;
4896 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4897 if (ie == NULL)
4898 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4899 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4900 return got_error_path(pe->path,
4901 GOT_ERR_FILE_NOT_STAGED);
4904 return NULL;
4907 const struct got_error *
4908 got_worktree_commit(struct got_object_id **new_commit_id,
4909 struct got_worktree *worktree, struct got_pathlist_head *paths,
4910 const char *author, const char *committer,
4911 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4912 got_worktree_status_cb status_cb, void *status_arg,
4913 struct got_repository *repo)
4915 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4916 struct got_fileindex *fileindex = NULL;
4917 char *fileindex_path = NULL;
4918 struct got_pathlist_head commitable_paths;
4919 struct collect_commitables_arg cc_arg;
4920 struct got_pathlist_entry *pe;
4921 struct got_reference *head_ref = NULL;
4922 struct got_object_id *head_commit_id = NULL;
4923 int have_staged_files = 0;
4925 *new_commit_id = NULL;
4927 TAILQ_INIT(&commitable_paths);
4929 err = lock_worktree(worktree, LOCK_EX);
4930 if (err)
4931 goto done;
4933 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4934 if (err)
4935 goto done;
4937 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4938 if (err)
4939 goto done;
4941 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4942 if (err)
4943 goto done;
4945 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4946 &have_staged_files);
4947 if (err && err->code != GOT_ERR_CANCELLED)
4948 goto done;
4949 if (have_staged_files) {
4950 err = check_non_staged_files(fileindex, paths);
4951 if (err)
4952 goto done;
4955 cc_arg.commitable_paths = &commitable_paths;
4956 cc_arg.worktree = worktree;
4957 cc_arg.repo = repo;
4958 cc_arg.have_staged_files = have_staged_files;
4959 TAILQ_FOREACH(pe, paths, entry) {
4960 err = worktree_status(worktree, pe->path, fileindex, repo,
4961 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4962 if (err)
4963 goto done;
4966 if (TAILQ_EMPTY(&commitable_paths)) {
4967 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4968 goto done;
4971 TAILQ_FOREACH(pe, paths, entry) {
4972 err = check_path_is_commitable(pe->path, &commitable_paths);
4973 if (err)
4974 goto done;
4977 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4978 struct got_commitable *ct = pe->data;
4979 const char *ct_path = ct->in_repo_path;
4981 while (ct_path[0] == '/')
4982 ct_path++;
4983 err = check_out_of_date(ct_path, ct->status,
4984 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4985 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4986 if (err)
4987 goto done;
4991 err = commit_worktree(new_commit_id, &commitable_paths,
4992 head_commit_id, worktree, author, committer,
4993 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4994 if (err)
4995 goto done;
4997 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4998 fileindex, have_staged_files);
4999 sync_err = sync_fileindex(fileindex, fileindex_path);
5000 if (sync_err && err == NULL)
5001 err = sync_err;
5002 done:
5003 if (fileindex)
5004 got_fileindex_free(fileindex);
5005 free(fileindex_path);
5006 unlockerr = lock_worktree(worktree, LOCK_SH);
5007 if (unlockerr && err == NULL)
5008 err = unlockerr;
5009 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5010 struct got_commitable *ct = pe->data;
5011 free_commitable(ct);
5013 got_pathlist_free(&commitable_paths);
5014 return err;
5017 const char *
5018 got_commitable_get_path(struct got_commitable *ct)
5020 return ct->path;
5023 unsigned int
5024 got_commitable_get_status(struct got_commitable *ct)
5026 return ct->status;
5029 struct check_rebase_ok_arg {
5030 struct got_worktree *worktree;
5031 struct got_repository *repo;
5034 static const struct got_error *
5035 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5037 const struct got_error *err = NULL;
5038 struct check_rebase_ok_arg *a = arg;
5039 unsigned char status;
5040 struct stat sb;
5041 char *ondisk_path;
5043 /* Reject rebase of a work tree with mixed base commits. */
5044 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5045 SHA1_DIGEST_LENGTH))
5046 return got_error(GOT_ERR_MIXED_COMMITS);
5048 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5049 == -1)
5050 return got_error_from_errno("asprintf");
5052 /* Reject rebase of a work tree with modified or staged files. */
5053 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5054 free(ondisk_path);
5055 if (err)
5056 return err;
5058 if (status != GOT_STATUS_NO_CHANGE)
5059 return got_error(GOT_ERR_MODIFIED);
5060 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5061 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5063 return NULL;
5066 const struct got_error *
5067 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5068 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5069 struct got_worktree *worktree, struct got_reference *branch,
5070 struct got_repository *repo)
5072 const struct got_error *err = NULL;
5073 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5074 char *branch_ref_name = NULL;
5075 char *fileindex_path = NULL;
5076 struct check_rebase_ok_arg ok_arg;
5077 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5078 struct got_object_id *wt_branch_tip = NULL;
5080 *new_base_branch_ref = NULL;
5081 *tmp_branch = NULL;
5082 *fileindex = NULL;
5084 err = lock_worktree(worktree, LOCK_EX);
5085 if (err)
5086 return err;
5088 err = open_fileindex(fileindex, &fileindex_path, worktree);
5089 if (err)
5090 goto done;
5092 ok_arg.worktree = worktree;
5093 ok_arg.repo = repo;
5094 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5095 &ok_arg);
5096 if (err)
5097 goto done;
5099 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5100 if (err)
5101 goto done;
5103 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5104 if (err)
5105 goto done;
5107 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5108 if (err)
5109 goto done;
5111 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5112 0);
5113 if (err)
5114 goto done;
5116 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5117 if (err)
5118 goto done;
5119 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5120 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5121 goto done;
5124 err = got_ref_alloc_symref(new_base_branch_ref,
5125 new_base_branch_ref_name, wt_branch);
5126 if (err)
5127 goto done;
5128 err = got_ref_write(*new_base_branch_ref, repo);
5129 if (err)
5130 goto done;
5132 /* TODO Lock original branch's ref while rebasing? */
5134 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5135 if (err)
5136 goto done;
5138 err = got_ref_write(branch_ref, repo);
5139 if (err)
5140 goto done;
5142 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5143 worktree->base_commit_id);
5144 if (err)
5145 goto done;
5146 err = got_ref_write(*tmp_branch, repo);
5147 if (err)
5148 goto done;
5150 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5151 if (err)
5152 goto done;
5153 done:
5154 free(fileindex_path);
5155 free(tmp_branch_name);
5156 free(new_base_branch_ref_name);
5157 free(branch_ref_name);
5158 if (branch_ref)
5159 got_ref_close(branch_ref);
5160 if (wt_branch)
5161 got_ref_close(wt_branch);
5162 free(wt_branch_tip);
5163 if (err) {
5164 if (*new_base_branch_ref) {
5165 got_ref_close(*new_base_branch_ref);
5166 *new_base_branch_ref = NULL;
5168 if (*tmp_branch) {
5169 got_ref_close(*tmp_branch);
5170 *tmp_branch = NULL;
5172 if (*fileindex) {
5173 got_fileindex_free(*fileindex);
5174 *fileindex = NULL;
5176 lock_worktree(worktree, LOCK_SH);
5178 return err;
5181 const struct got_error *
5182 got_worktree_rebase_continue(struct got_object_id **commit_id,
5183 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5184 struct got_reference **branch, struct got_fileindex **fileindex,
5185 struct got_worktree *worktree, struct got_repository *repo)
5187 const struct got_error *err;
5188 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5189 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5190 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5191 char *fileindex_path = NULL;
5192 int have_staged_files = 0;
5194 *commit_id = NULL;
5195 *new_base_branch = NULL;
5196 *tmp_branch = NULL;
5197 *branch = NULL;
5198 *fileindex = NULL;
5200 err = lock_worktree(worktree, LOCK_EX);
5201 if (err)
5202 return err;
5204 err = open_fileindex(fileindex, &fileindex_path, worktree);
5205 if (err)
5206 goto done;
5208 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5209 &have_staged_files);
5210 if (err && err->code != GOT_ERR_CANCELLED)
5211 goto done;
5212 if (have_staged_files) {
5213 err = got_error(GOT_ERR_STAGED_PATHS);
5214 goto done;
5217 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5218 if (err)
5219 goto done;
5221 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5222 if (err)
5223 goto done;
5225 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5226 if (err)
5227 goto done;
5229 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5230 if (err)
5231 goto done;
5233 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5234 if (err)
5235 goto done;
5237 err = got_ref_open(branch, repo,
5238 got_ref_get_symref_target(branch_ref), 0);
5239 if (err)
5240 goto done;
5242 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5243 if (err)
5244 goto done;
5246 err = got_ref_resolve(commit_id, repo, commit_ref);
5247 if (err)
5248 goto done;
5250 err = got_ref_open(new_base_branch, repo,
5251 new_base_branch_ref_name, 0);
5252 if (err)
5253 goto done;
5255 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5256 if (err)
5257 goto done;
5258 done:
5259 free(commit_ref_name);
5260 free(branch_ref_name);
5261 free(fileindex_path);
5262 if (commit_ref)
5263 got_ref_close(commit_ref);
5264 if (branch_ref)
5265 got_ref_close(branch_ref);
5266 if (err) {
5267 free(*commit_id);
5268 *commit_id = NULL;
5269 if (*tmp_branch) {
5270 got_ref_close(*tmp_branch);
5271 *tmp_branch = NULL;
5273 if (*new_base_branch) {
5274 got_ref_close(*new_base_branch);
5275 *new_base_branch = NULL;
5277 if (*branch) {
5278 got_ref_close(*branch);
5279 *branch = NULL;
5281 if (*fileindex) {
5282 got_fileindex_free(*fileindex);
5283 *fileindex = NULL;
5285 lock_worktree(worktree, LOCK_SH);
5287 return err;
5290 const struct got_error *
5291 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5293 const struct got_error *err;
5294 char *tmp_branch_name = NULL;
5296 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5297 if (err)
5298 return err;
5300 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5301 free(tmp_branch_name);
5302 return NULL;
5305 static const struct got_error *
5306 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5307 char **logmsg, void *arg)
5309 *logmsg = arg;
5310 return NULL;
5313 static const struct got_error *
5314 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5315 const char *path, struct got_object_id *blob_id,
5316 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5317 int dirfd, const char *de_name)
5319 return NULL;
5322 struct collect_merged_paths_arg {
5323 got_worktree_checkout_cb progress_cb;
5324 void *progress_arg;
5325 struct got_pathlist_head *merged_paths;
5328 static const struct got_error *
5329 collect_merged_paths(void *arg, unsigned char status, const char *path)
5331 const struct got_error *err;
5332 struct collect_merged_paths_arg *a = arg;
5333 char *p;
5334 struct got_pathlist_entry *new;
5336 err = (*a->progress_cb)(a->progress_arg, status, path);
5337 if (err)
5338 return err;
5340 if (status != GOT_STATUS_MERGE &&
5341 status != GOT_STATUS_ADD &&
5342 status != GOT_STATUS_DELETE &&
5343 status != GOT_STATUS_CONFLICT)
5344 return NULL;
5346 p = strdup(path);
5347 if (p == NULL)
5348 return got_error_from_errno("strdup");
5350 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5351 if (err || new == NULL)
5352 free(p);
5353 return err;
5356 void
5357 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5359 struct got_pathlist_entry *pe;
5361 TAILQ_FOREACH(pe, merged_paths, entry)
5362 free((char *)pe->path);
5364 got_pathlist_free(merged_paths);
5367 static const struct got_error *
5368 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5369 int is_rebase, struct got_repository *repo)
5371 const struct got_error *err;
5372 struct got_reference *commit_ref = NULL;
5374 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5375 if (err) {
5376 if (err->code != GOT_ERR_NOT_REF)
5377 goto done;
5378 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5379 if (err)
5380 goto done;
5381 err = got_ref_write(commit_ref, repo);
5382 if (err)
5383 goto done;
5384 } else if (is_rebase) {
5385 struct got_object_id *stored_id;
5386 int cmp;
5388 err = got_ref_resolve(&stored_id, repo, commit_ref);
5389 if (err)
5390 goto done;
5391 cmp = got_object_id_cmp(commit_id, stored_id);
5392 free(stored_id);
5393 if (cmp != 0) {
5394 err = got_error(GOT_ERR_REBASE_COMMITID);
5395 goto done;
5398 done:
5399 if (commit_ref)
5400 got_ref_close(commit_ref);
5401 return err;
5404 static const struct got_error *
5405 rebase_merge_files(struct got_pathlist_head *merged_paths,
5406 const char *commit_ref_name, struct got_worktree *worktree,
5407 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5408 struct got_object_id *commit_id, struct got_repository *repo,
5409 got_worktree_checkout_cb progress_cb, void *progress_arg,
5410 got_cancel_cb cancel_cb, void *cancel_arg)
5412 const struct got_error *err;
5413 struct got_reference *commit_ref = NULL;
5414 struct collect_merged_paths_arg cmp_arg;
5415 char *fileindex_path;
5417 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5419 err = get_fileindex_path(&fileindex_path, worktree);
5420 if (err)
5421 return err;
5423 cmp_arg.progress_cb = progress_cb;
5424 cmp_arg.progress_arg = progress_arg;
5425 cmp_arg.merged_paths = merged_paths;
5426 err = merge_files(worktree, fileindex, fileindex_path,
5427 parent_commit_id, commit_id, repo, collect_merged_paths,
5428 &cmp_arg, cancel_cb, cancel_arg);
5429 if (commit_ref)
5430 got_ref_close(commit_ref);
5431 return err;
5434 const struct got_error *
5435 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5436 struct got_worktree *worktree, struct got_fileindex *fileindex,
5437 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5438 struct got_repository *repo,
5439 got_worktree_checkout_cb progress_cb, void *progress_arg,
5440 got_cancel_cb cancel_cb, void *cancel_arg)
5442 const struct got_error *err;
5443 char *commit_ref_name;
5445 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5446 if (err)
5447 return err;
5449 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5450 if (err)
5451 goto done;
5453 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5454 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5455 progress_arg, cancel_cb, cancel_arg);
5456 done:
5457 free(commit_ref_name);
5458 return err;
5461 const struct got_error *
5462 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5463 struct got_worktree *worktree, struct got_fileindex *fileindex,
5464 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5465 struct got_repository *repo,
5466 got_worktree_checkout_cb progress_cb, void *progress_arg,
5467 got_cancel_cb cancel_cb, void *cancel_arg)
5469 const struct got_error *err;
5470 char *commit_ref_name;
5472 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5473 if (err)
5474 return err;
5476 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5477 if (err)
5478 goto done;
5480 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5481 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5482 progress_arg, cancel_cb, cancel_arg);
5483 done:
5484 free(commit_ref_name);
5485 return err;
5488 static const struct got_error *
5489 rebase_commit(struct got_object_id **new_commit_id,
5490 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5491 struct got_worktree *worktree, struct got_fileindex *fileindex,
5492 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5493 const char *new_logmsg, struct got_repository *repo)
5495 const struct got_error *err, *sync_err;
5496 struct got_pathlist_head commitable_paths;
5497 struct collect_commitables_arg cc_arg;
5498 char *fileindex_path = NULL;
5499 struct got_reference *head_ref = NULL;
5500 struct got_object_id *head_commit_id = NULL;
5501 char *logmsg = NULL;
5503 TAILQ_INIT(&commitable_paths);
5504 *new_commit_id = NULL;
5506 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5508 err = get_fileindex_path(&fileindex_path, worktree);
5509 if (err)
5510 return err;
5512 cc_arg.commitable_paths = &commitable_paths;
5513 cc_arg.worktree = worktree;
5514 cc_arg.repo = repo;
5515 cc_arg.have_staged_files = 0;
5517 * If possible get the status of individual files directly to
5518 * avoid crawling the entire work tree once per rebased commit.
5519 * TODO: Ideally, merged_paths would contain a list of commitables
5520 * we could use so we could skip worktree_status() entirely.
5522 if (merged_paths) {
5523 struct got_pathlist_entry *pe;
5524 TAILQ_FOREACH(pe, merged_paths, entry) {
5525 err = worktree_status(worktree, pe->path, fileindex,
5526 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5527 0);
5528 if (err)
5529 goto done;
5531 } else {
5532 err = worktree_status(worktree, "", fileindex, repo,
5533 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5534 if (err)
5535 goto done;
5538 if (TAILQ_EMPTY(&commitable_paths)) {
5539 /* No-op change; commit will be elided. */
5540 err = got_ref_delete(commit_ref, repo);
5541 if (err)
5542 goto done;
5543 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5544 goto done;
5547 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5548 if (err)
5549 goto done;
5551 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5552 if (err)
5553 goto done;
5555 if (new_logmsg) {
5556 logmsg = strdup(new_logmsg);
5557 if (logmsg == NULL) {
5558 err = got_error_from_errno("strdup");
5559 goto done;
5561 } else {
5562 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5563 if (err)
5564 goto done;
5567 /* NB: commit_worktree will call free(logmsg) */
5568 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5569 worktree, got_object_commit_get_author(orig_commit),
5570 got_object_commit_get_committer(orig_commit),
5571 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5572 if (err)
5573 goto done;
5575 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5576 if (err)
5577 goto done;
5579 err = got_ref_delete(commit_ref, repo);
5580 if (err)
5581 goto done;
5583 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5584 fileindex, 0);
5585 sync_err = sync_fileindex(fileindex, fileindex_path);
5586 if (sync_err && err == NULL)
5587 err = sync_err;
5588 done:
5589 free(fileindex_path);
5590 free(head_commit_id);
5591 if (head_ref)
5592 got_ref_close(head_ref);
5593 if (err) {
5594 free(*new_commit_id);
5595 *new_commit_id = NULL;
5597 return err;
5600 const struct got_error *
5601 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5602 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5603 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5604 struct got_commit_object *orig_commit,
5605 struct got_object_id *orig_commit_id, struct got_repository *repo)
5607 const struct got_error *err;
5608 char *commit_ref_name;
5609 struct got_reference *commit_ref = NULL;
5610 struct got_object_id *commit_id = NULL;
5612 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5613 if (err)
5614 return err;
5616 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5617 if (err)
5618 goto done;
5619 err = got_ref_resolve(&commit_id, repo, commit_ref);
5620 if (err)
5621 goto done;
5622 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5623 err = got_error(GOT_ERR_REBASE_COMMITID);
5624 goto done;
5627 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5628 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5629 done:
5630 if (commit_ref)
5631 got_ref_close(commit_ref);
5632 free(commit_ref_name);
5633 free(commit_id);
5634 return err;
5637 const struct got_error *
5638 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5639 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5640 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5641 struct got_commit_object *orig_commit,
5642 struct got_object_id *orig_commit_id, const char *new_logmsg,
5643 struct got_repository *repo)
5645 const struct got_error *err;
5646 char *commit_ref_name;
5647 struct got_reference *commit_ref = NULL;
5649 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5650 if (err)
5651 return err;
5653 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5654 if (err)
5655 goto done;
5657 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5658 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5659 done:
5660 if (commit_ref)
5661 got_ref_close(commit_ref);
5662 free(commit_ref_name);
5663 return err;
5666 const struct got_error *
5667 got_worktree_rebase_postpone(struct got_worktree *worktree,
5668 struct got_fileindex *fileindex)
5670 if (fileindex)
5671 got_fileindex_free(fileindex);
5672 return lock_worktree(worktree, LOCK_SH);
5675 static const struct got_error *
5676 delete_ref(const char *name, struct got_repository *repo)
5678 const struct got_error *err;
5679 struct got_reference *ref;
5681 err = got_ref_open(&ref, repo, name, 0);
5682 if (err) {
5683 if (err->code == GOT_ERR_NOT_REF)
5684 return NULL;
5685 return err;
5688 err = got_ref_delete(ref, repo);
5689 got_ref_close(ref);
5690 return err;
5693 static const struct got_error *
5694 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5696 const struct got_error *err;
5697 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5698 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5700 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5701 if (err)
5702 goto done;
5703 err = delete_ref(tmp_branch_name, repo);
5704 if (err)
5705 goto done;
5707 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5708 if (err)
5709 goto done;
5710 err = delete_ref(new_base_branch_ref_name, repo);
5711 if (err)
5712 goto done;
5714 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5715 if (err)
5716 goto done;
5717 err = delete_ref(branch_ref_name, repo);
5718 if (err)
5719 goto done;
5721 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5722 if (err)
5723 goto done;
5724 err = delete_ref(commit_ref_name, repo);
5725 if (err)
5726 goto done;
5728 done:
5729 free(tmp_branch_name);
5730 free(new_base_branch_ref_name);
5731 free(branch_ref_name);
5732 free(commit_ref_name);
5733 return err;
5736 const struct got_error *
5737 got_worktree_rebase_complete(struct got_worktree *worktree,
5738 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5739 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5740 struct got_repository *repo)
5742 const struct got_error *err, *unlockerr;
5743 struct got_object_id *new_head_commit_id = NULL;
5745 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5746 if (err)
5747 return err;
5749 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5750 if (err)
5751 goto done;
5753 err = got_ref_write(rebased_branch, repo);
5754 if (err)
5755 goto done;
5757 err = got_worktree_set_head_ref(worktree, rebased_branch);
5758 if (err)
5759 goto done;
5761 err = delete_rebase_refs(worktree, repo);
5762 done:
5763 if (fileindex)
5764 got_fileindex_free(fileindex);
5765 free(new_head_commit_id);
5766 unlockerr = lock_worktree(worktree, LOCK_SH);
5767 if (unlockerr && err == NULL)
5768 err = unlockerr;
5769 return err;
5772 const struct got_error *
5773 got_worktree_rebase_abort(struct got_worktree *worktree,
5774 struct got_fileindex *fileindex, struct got_repository *repo,
5775 struct got_reference *new_base_branch,
5776 got_worktree_checkout_cb progress_cb, void *progress_arg)
5778 const struct got_error *err, *unlockerr, *sync_err;
5779 struct got_reference *resolved = NULL;
5780 struct got_object_id *commit_id = NULL;
5781 char *fileindex_path = NULL;
5782 struct revert_file_args rfa;
5783 struct got_object_id *tree_id = NULL;
5785 err = lock_worktree(worktree, LOCK_EX);
5786 if (err)
5787 return err;
5789 err = got_ref_open(&resolved, repo,
5790 got_ref_get_symref_target(new_base_branch), 0);
5791 if (err)
5792 goto done;
5794 err = got_worktree_set_head_ref(worktree, resolved);
5795 if (err)
5796 goto done;
5799 * XXX commits to the base branch could have happened while
5800 * we were busy rebasing; should we store the original commit ID
5801 * when rebase begins and read it back here?
5803 err = got_ref_resolve(&commit_id, repo, resolved);
5804 if (err)
5805 goto done;
5807 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5808 if (err)
5809 goto done;
5811 err = got_object_id_by_path(&tree_id, repo,
5812 worktree->base_commit_id, worktree->path_prefix);
5813 if (err)
5814 goto done;
5816 err = delete_rebase_refs(worktree, repo);
5817 if (err)
5818 goto done;
5820 err = get_fileindex_path(&fileindex_path, worktree);
5821 if (err)
5822 goto done;
5824 rfa.worktree = worktree;
5825 rfa.fileindex = fileindex;
5826 rfa.progress_cb = progress_cb;
5827 rfa.progress_arg = progress_arg;
5828 rfa.patch_cb = NULL;
5829 rfa.patch_arg = NULL;
5830 rfa.repo = repo;
5831 err = worktree_status(worktree, "", fileindex, repo,
5832 revert_file, &rfa, NULL, NULL, 0, 0);
5833 if (err)
5834 goto sync;
5836 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5837 repo, progress_cb, progress_arg, NULL, NULL);
5838 sync:
5839 sync_err = sync_fileindex(fileindex, fileindex_path);
5840 if (sync_err && err == NULL)
5841 err = sync_err;
5842 done:
5843 got_ref_close(resolved);
5844 free(tree_id);
5845 free(commit_id);
5846 if (fileindex)
5847 got_fileindex_free(fileindex);
5848 free(fileindex_path);
5850 unlockerr = lock_worktree(worktree, LOCK_SH);
5851 if (unlockerr && err == NULL)
5852 err = unlockerr;
5853 return err;
5856 const struct got_error *
5857 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5858 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5859 struct got_fileindex **fileindex, struct got_worktree *worktree,
5860 struct got_repository *repo)
5862 const struct got_error *err = NULL;
5863 char *tmp_branch_name = NULL;
5864 char *branch_ref_name = NULL;
5865 char *base_commit_ref_name = NULL;
5866 char *fileindex_path = NULL;
5867 struct check_rebase_ok_arg ok_arg;
5868 struct got_reference *wt_branch = NULL;
5869 struct got_reference *base_commit_ref = NULL;
5871 *tmp_branch = NULL;
5872 *branch_ref = NULL;
5873 *base_commit_id = NULL;
5874 *fileindex = NULL;
5876 err = lock_worktree(worktree, LOCK_EX);
5877 if (err)
5878 return err;
5880 err = open_fileindex(fileindex, &fileindex_path, worktree);
5881 if (err)
5882 goto done;
5884 ok_arg.worktree = worktree;
5885 ok_arg.repo = repo;
5886 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5887 &ok_arg);
5888 if (err)
5889 goto done;
5891 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5892 if (err)
5893 goto done;
5895 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5896 if (err)
5897 goto done;
5899 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5900 worktree);
5901 if (err)
5902 goto done;
5904 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5905 0);
5906 if (err)
5907 goto done;
5909 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5910 if (err)
5911 goto done;
5913 err = got_ref_write(*branch_ref, repo);
5914 if (err)
5915 goto done;
5917 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5918 worktree->base_commit_id);
5919 if (err)
5920 goto done;
5921 err = got_ref_write(base_commit_ref, repo);
5922 if (err)
5923 goto done;
5924 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5925 if (*base_commit_id == NULL) {
5926 err = got_error_from_errno("got_object_id_dup");
5927 goto done;
5930 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5931 worktree->base_commit_id);
5932 if (err)
5933 goto done;
5934 err = got_ref_write(*tmp_branch, repo);
5935 if (err)
5936 goto done;
5938 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5939 if (err)
5940 goto done;
5941 done:
5942 free(fileindex_path);
5943 free(tmp_branch_name);
5944 free(branch_ref_name);
5945 free(base_commit_ref_name);
5946 if (wt_branch)
5947 got_ref_close(wt_branch);
5948 if (err) {
5949 if (*branch_ref) {
5950 got_ref_close(*branch_ref);
5951 *branch_ref = NULL;
5953 if (*tmp_branch) {
5954 got_ref_close(*tmp_branch);
5955 *tmp_branch = NULL;
5957 free(*base_commit_id);
5958 if (*fileindex) {
5959 got_fileindex_free(*fileindex);
5960 *fileindex = NULL;
5962 lock_worktree(worktree, LOCK_SH);
5964 return err;
5967 const struct got_error *
5968 got_worktree_histedit_postpone(struct got_worktree *worktree,
5969 struct got_fileindex *fileindex)
5971 if (fileindex)
5972 got_fileindex_free(fileindex);
5973 return lock_worktree(worktree, LOCK_SH);
5976 const struct got_error *
5977 got_worktree_histedit_in_progress(int *in_progress,
5978 struct got_worktree *worktree)
5980 const struct got_error *err;
5981 char *tmp_branch_name = NULL;
5983 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5984 if (err)
5985 return err;
5987 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5988 free(tmp_branch_name);
5989 return NULL;
5992 const struct got_error *
5993 got_worktree_histedit_continue(struct got_object_id **commit_id,
5994 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5995 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5996 struct got_worktree *worktree, struct got_repository *repo)
5998 const struct got_error *err;
5999 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6000 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6001 struct got_reference *commit_ref = NULL;
6002 struct got_reference *base_commit_ref = NULL;
6003 char *fileindex_path = NULL;
6004 int have_staged_files = 0;
6006 *commit_id = NULL;
6007 *tmp_branch = NULL;
6008 *base_commit_id = NULL;
6009 *fileindex = NULL;
6011 err = lock_worktree(worktree, LOCK_EX);
6012 if (err)
6013 return err;
6015 err = open_fileindex(fileindex, &fileindex_path, worktree);
6016 if (err)
6017 goto done;
6019 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6020 &have_staged_files);
6021 if (err && err->code != GOT_ERR_CANCELLED)
6022 goto done;
6023 if (have_staged_files) {
6024 err = got_error(GOT_ERR_STAGED_PATHS);
6025 goto done;
6028 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6029 if (err)
6030 goto done;
6032 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6033 if (err)
6034 goto done;
6036 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6041 worktree);
6042 if (err)
6043 goto done;
6045 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6046 if (err)
6047 goto done;
6049 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6050 if (err)
6051 goto done;
6052 err = got_ref_resolve(commit_id, repo, commit_ref);
6053 if (err)
6054 goto done;
6056 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6057 if (err)
6058 goto done;
6059 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6060 if (err)
6061 goto done;
6063 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6064 if (err)
6065 goto done;
6066 done:
6067 free(commit_ref_name);
6068 free(branch_ref_name);
6069 free(fileindex_path);
6070 if (commit_ref)
6071 got_ref_close(commit_ref);
6072 if (base_commit_ref)
6073 got_ref_close(base_commit_ref);
6074 if (err) {
6075 free(*commit_id);
6076 *commit_id = NULL;
6077 free(*base_commit_id);
6078 *base_commit_id = NULL;
6079 if (*tmp_branch) {
6080 got_ref_close(*tmp_branch);
6081 *tmp_branch = NULL;
6083 if (*fileindex) {
6084 got_fileindex_free(*fileindex);
6085 *fileindex = NULL;
6087 lock_worktree(worktree, LOCK_EX);
6089 return err;
6092 static const struct got_error *
6093 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6095 const struct got_error *err;
6096 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6097 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6099 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6100 if (err)
6101 goto done;
6102 err = delete_ref(tmp_branch_name, repo);
6103 if (err)
6104 goto done;
6106 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6107 worktree);
6108 if (err)
6109 goto done;
6110 err = delete_ref(base_commit_ref_name, repo);
6111 if (err)
6112 goto done;
6114 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6115 if (err)
6116 goto done;
6117 err = delete_ref(branch_ref_name, repo);
6118 if (err)
6119 goto done;
6121 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6122 if (err)
6123 goto done;
6124 err = delete_ref(commit_ref_name, repo);
6125 if (err)
6126 goto done;
6127 done:
6128 free(tmp_branch_name);
6129 free(base_commit_ref_name);
6130 free(branch_ref_name);
6131 free(commit_ref_name);
6132 return err;
6135 const struct got_error *
6136 got_worktree_histedit_abort(struct got_worktree *worktree,
6137 struct got_fileindex *fileindex, struct got_repository *repo,
6138 struct got_reference *branch, struct got_object_id *base_commit_id,
6139 got_worktree_checkout_cb progress_cb, void *progress_arg)
6141 const struct got_error *err, *unlockerr, *sync_err;
6142 struct got_reference *resolved = NULL;
6143 char *fileindex_path = NULL;
6144 struct got_object_id *tree_id = NULL;
6145 struct revert_file_args rfa;
6147 err = lock_worktree(worktree, LOCK_EX);
6148 if (err)
6149 return err;
6151 err = got_ref_open(&resolved, repo,
6152 got_ref_get_symref_target(branch), 0);
6153 if (err)
6154 goto done;
6156 err = got_worktree_set_head_ref(worktree, resolved);
6157 if (err)
6158 goto done;
6160 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6161 if (err)
6162 goto done;
6164 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6165 worktree->path_prefix);
6166 if (err)
6167 goto done;
6169 err = delete_histedit_refs(worktree, repo);
6170 if (err)
6171 goto done;
6173 err = get_fileindex_path(&fileindex_path, worktree);
6174 if (err)
6175 goto done;
6177 rfa.worktree = worktree;
6178 rfa.fileindex = fileindex;
6179 rfa.progress_cb = progress_cb;
6180 rfa.progress_arg = progress_arg;
6181 rfa.patch_cb = NULL;
6182 rfa.patch_arg = NULL;
6183 rfa.repo = repo;
6184 err = worktree_status(worktree, "", fileindex, repo,
6185 revert_file, &rfa, NULL, NULL, 0, 0);
6186 if (err)
6187 goto sync;
6189 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6190 repo, progress_cb, progress_arg, NULL, NULL);
6191 sync:
6192 sync_err = sync_fileindex(fileindex, fileindex_path);
6193 if (sync_err && err == NULL)
6194 err = sync_err;
6195 done:
6196 got_ref_close(resolved);
6197 free(tree_id);
6198 free(fileindex_path);
6200 unlockerr = lock_worktree(worktree, LOCK_SH);
6201 if (unlockerr && err == NULL)
6202 err = unlockerr;
6203 return err;
6206 const struct got_error *
6207 got_worktree_histedit_complete(struct got_worktree *worktree,
6208 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6209 struct got_reference *edited_branch, struct got_repository *repo)
6211 const struct got_error *err, *unlockerr;
6212 struct got_object_id *new_head_commit_id = NULL;
6213 struct got_reference *resolved = NULL;
6215 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6216 if (err)
6217 return err;
6219 err = got_ref_open(&resolved, repo,
6220 got_ref_get_symref_target(edited_branch), 0);
6221 if (err)
6222 goto done;
6224 err = got_ref_change_ref(resolved, new_head_commit_id);
6225 if (err)
6226 goto done;
6228 err = got_ref_write(resolved, repo);
6229 if (err)
6230 goto done;
6232 err = got_worktree_set_head_ref(worktree, resolved);
6233 if (err)
6234 goto done;
6236 err = delete_histedit_refs(worktree, repo);
6237 done:
6238 if (fileindex)
6239 got_fileindex_free(fileindex);
6240 free(new_head_commit_id);
6241 unlockerr = lock_worktree(worktree, LOCK_SH);
6242 if (unlockerr && err == NULL)
6243 err = unlockerr;
6244 return err;
6247 const struct got_error *
6248 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6249 struct got_object_id *commit_id, struct got_repository *repo)
6251 const struct got_error *err;
6252 char *commit_ref_name;
6254 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6255 if (err)
6256 return err;
6258 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6259 if (err)
6260 goto done;
6262 err = delete_ref(commit_ref_name, repo);
6263 done:
6264 free(commit_ref_name);
6265 return err;
6268 const struct got_error *
6269 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6270 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6271 struct got_worktree *worktree, const char *refname,
6272 struct got_repository *repo)
6274 const struct got_error *err = NULL;
6275 char *fileindex_path = NULL;
6276 struct check_rebase_ok_arg ok_arg;
6278 *fileindex = NULL;
6279 *branch_ref = NULL;
6280 *base_branch_ref = NULL;
6282 err = lock_worktree(worktree, LOCK_EX);
6283 if (err)
6284 return err;
6286 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6287 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6288 "cannot integrate a branch into itself; "
6289 "update -b or different branch name required");
6290 goto done;
6293 err = open_fileindex(fileindex, &fileindex_path, worktree);
6294 if (err)
6295 goto done;
6297 /* Preconditions are the same as for rebase. */
6298 ok_arg.worktree = worktree;
6299 ok_arg.repo = repo;
6300 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6301 &ok_arg);
6302 if (err)
6303 goto done;
6305 err = got_ref_open(branch_ref, repo, refname, 1);
6306 if (err)
6307 goto done;
6309 err = got_ref_open(base_branch_ref, repo,
6310 got_worktree_get_head_ref_name(worktree), 1);
6311 done:
6312 if (err) {
6313 if (*branch_ref) {
6314 got_ref_close(*branch_ref);
6315 *branch_ref = NULL;
6317 if (*base_branch_ref) {
6318 got_ref_close(*base_branch_ref);
6319 *base_branch_ref = NULL;
6321 if (*fileindex) {
6322 got_fileindex_free(*fileindex);
6323 *fileindex = NULL;
6325 lock_worktree(worktree, LOCK_SH);
6327 return err;
6330 const struct got_error *
6331 got_worktree_integrate_continue(struct got_worktree *worktree,
6332 struct got_fileindex *fileindex, struct got_repository *repo,
6333 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6334 got_worktree_checkout_cb progress_cb, void *progress_arg,
6335 got_cancel_cb cancel_cb, void *cancel_arg)
6337 const struct got_error *err = NULL, *sync_err, *unlockerr;
6338 char *fileindex_path = NULL;
6339 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6341 err = get_fileindex_path(&fileindex_path, worktree);
6342 if (err)
6343 goto done;
6345 err = got_ref_resolve(&commit_id, repo, branch_ref);
6346 if (err)
6347 goto done;
6349 err = got_object_id_by_path(&tree_id, repo, commit_id,
6350 worktree->path_prefix);
6351 if (err)
6352 goto done;
6354 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6355 if (err)
6356 goto done;
6358 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6359 progress_cb, progress_arg, cancel_cb, cancel_arg);
6360 if (err)
6361 goto sync;
6363 err = got_ref_change_ref(base_branch_ref, commit_id);
6364 if (err)
6365 goto sync;
6367 err = got_ref_write(base_branch_ref, repo);
6368 sync:
6369 sync_err = sync_fileindex(fileindex, fileindex_path);
6370 if (sync_err && err == NULL)
6371 err = sync_err;
6373 done:
6374 unlockerr = got_ref_unlock(branch_ref);
6375 if (unlockerr && err == NULL)
6376 err = unlockerr;
6377 got_ref_close(branch_ref);
6379 unlockerr = got_ref_unlock(base_branch_ref);
6380 if (unlockerr && err == NULL)
6381 err = unlockerr;
6382 got_ref_close(base_branch_ref);
6384 got_fileindex_free(fileindex);
6385 free(fileindex_path);
6386 free(tree_id);
6388 unlockerr = lock_worktree(worktree, LOCK_SH);
6389 if (unlockerr && err == NULL)
6390 err = unlockerr;
6391 return err;
6394 const struct got_error *
6395 got_worktree_integrate_abort(struct got_worktree *worktree,
6396 struct got_fileindex *fileindex, struct got_repository *repo,
6397 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6399 const struct got_error *err = NULL, *unlockerr = NULL;
6401 got_fileindex_free(fileindex);
6403 err = lock_worktree(worktree, LOCK_SH);
6405 unlockerr = got_ref_unlock(branch_ref);
6406 if (unlockerr && err == NULL)
6407 err = unlockerr;
6408 got_ref_close(branch_ref);
6410 unlockerr = got_ref_unlock(base_branch_ref);
6411 if (unlockerr && err == NULL)
6412 err = unlockerr;
6413 got_ref_close(base_branch_ref);
6415 return err;
6418 struct check_stage_ok_arg {
6419 struct got_object_id *head_commit_id;
6420 struct got_worktree *worktree;
6421 struct got_fileindex *fileindex;
6422 struct got_repository *repo;
6423 int have_changes;
6426 const struct got_error *
6427 check_stage_ok(void *arg, unsigned char status,
6428 unsigned char staged_status, const char *relpath,
6429 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6430 struct got_object_id *commit_id, int dirfd, const char *de_name)
6432 struct check_stage_ok_arg *a = arg;
6433 const struct got_error *err = NULL;
6434 struct got_fileindex_entry *ie;
6435 struct got_object_id base_commit_id;
6436 struct got_object_id *base_commit_idp = NULL;
6437 char *in_repo_path = NULL, *p;
6439 if (status == GOT_STATUS_UNVERSIONED ||
6440 status == GOT_STATUS_NO_CHANGE)
6441 return NULL;
6442 if (status == GOT_STATUS_NONEXISTENT)
6443 return got_error_set_errno(ENOENT, relpath);
6445 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6446 if (ie == NULL)
6447 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6449 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6450 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6451 relpath) == -1)
6452 return got_error_from_errno("asprintf");
6454 if (got_fileindex_entry_has_commit(ie)) {
6455 memcpy(base_commit_id.sha1, ie->commit_sha1,
6456 SHA1_DIGEST_LENGTH);
6457 base_commit_idp = &base_commit_id;
6460 if (status == GOT_STATUS_CONFLICT) {
6461 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6462 goto done;
6463 } else if (status != GOT_STATUS_ADD &&
6464 status != GOT_STATUS_MODIFY &&
6465 status != GOT_STATUS_DELETE) {
6466 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6467 goto done;
6470 a->have_changes = 1;
6472 p = in_repo_path;
6473 while (p[0] == '/')
6474 p++;
6475 err = check_out_of_date(p, status, staged_status,
6476 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6477 GOT_ERR_STAGE_OUT_OF_DATE);
6478 done:
6479 free(in_repo_path);
6480 return err;
6483 struct stage_path_arg {
6484 struct got_worktree *worktree;
6485 struct got_fileindex *fileindex;
6486 struct got_repository *repo;
6487 got_worktree_status_cb status_cb;
6488 void *status_arg;
6489 got_worktree_patch_cb patch_cb;
6490 void *patch_arg;
6491 int staged_something;
6494 static const struct got_error *
6495 stage_path(void *arg, unsigned char status,
6496 unsigned char staged_status, const char *relpath,
6497 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6498 struct got_object_id *commit_id, int dirfd, const char *de_name)
6500 struct stage_path_arg *a = arg;
6501 const struct got_error *err = NULL;
6502 struct got_fileindex_entry *ie;
6503 char *ondisk_path = NULL, *path_content = NULL;
6504 uint32_t stage;
6505 struct got_object_id *new_staged_blob_id = NULL;
6507 if (status == GOT_STATUS_UNVERSIONED)
6508 return NULL;
6510 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6511 if (ie == NULL)
6512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6515 relpath)== -1)
6516 return got_error_from_errno("asprintf");
6518 switch (status) {
6519 case GOT_STATUS_ADD:
6520 case GOT_STATUS_MODIFY:
6521 if (a->patch_cb) {
6522 if (status == GOT_STATUS_ADD) {
6523 int choice = GOT_PATCH_CHOICE_NONE;
6524 err = (*a->patch_cb)(&choice, a->patch_arg,
6525 status, ie->path, NULL, 1, 1);
6526 if (err)
6527 break;
6528 if (choice != GOT_PATCH_CHOICE_YES)
6529 break;
6530 } else {
6531 err = create_patched_content(&path_content, 0,
6532 staged_blob_id ? staged_blob_id : blob_id,
6533 ondisk_path, dirfd, de_name, ie->path,
6534 a->repo, a->patch_cb, a->patch_arg);
6535 if (err || path_content == NULL)
6536 break;
6539 err = got_object_blob_create(&new_staged_blob_id,
6540 path_content ? path_content : ondisk_path, a->repo);
6541 if (err)
6542 break;
6543 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6544 SHA1_DIGEST_LENGTH);
6545 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6546 stage = GOT_FILEIDX_STAGE_ADD;
6547 else
6548 stage = GOT_FILEIDX_STAGE_MODIFY;
6549 got_fileindex_entry_stage_set(ie, stage);
6550 a->staged_something = 1;
6551 if (a->status_cb == NULL)
6552 break;
6553 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6554 get_staged_status(ie), relpath, blob_id,
6555 new_staged_blob_id, NULL, dirfd, de_name);
6556 break;
6557 case GOT_STATUS_DELETE:
6558 if (staged_status == GOT_STATUS_DELETE)
6559 break;
6560 if (a->patch_cb) {
6561 int choice = GOT_PATCH_CHOICE_NONE;
6562 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6563 ie->path, NULL, 1, 1);
6564 if (err)
6565 break;
6566 if (choice == GOT_PATCH_CHOICE_NO)
6567 break;
6568 if (choice != GOT_PATCH_CHOICE_YES) {
6569 err = got_error(GOT_ERR_PATCH_CHOICE);
6570 break;
6573 stage = GOT_FILEIDX_STAGE_DELETE;
6574 got_fileindex_entry_stage_set(ie, stage);
6575 a->staged_something = 1;
6576 if (a->status_cb == NULL)
6577 break;
6578 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6579 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6580 de_name);
6581 break;
6582 case GOT_STATUS_NO_CHANGE:
6583 break;
6584 case GOT_STATUS_CONFLICT:
6585 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6586 break;
6587 case GOT_STATUS_NONEXISTENT:
6588 err = got_error_set_errno(ENOENT, relpath);
6589 break;
6590 default:
6591 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6592 break;
6595 if (path_content && unlink(path_content) == -1 && err == NULL)
6596 err = got_error_from_errno2("unlink", path_content);
6597 free(path_content);
6598 free(ondisk_path);
6599 free(new_staged_blob_id);
6600 return err;
6603 const struct got_error *
6604 got_worktree_stage(struct got_worktree *worktree,
6605 struct got_pathlist_head *paths,
6606 got_worktree_status_cb status_cb, void *status_arg,
6607 got_worktree_patch_cb patch_cb, void *patch_arg,
6608 struct got_repository *repo)
6610 const struct got_error *err = NULL, *sync_err, *unlockerr;
6611 struct got_pathlist_entry *pe;
6612 struct got_fileindex *fileindex = NULL;
6613 char *fileindex_path = NULL;
6614 struct got_reference *head_ref = NULL;
6615 struct got_object_id *head_commit_id = NULL;
6616 struct check_stage_ok_arg oka;
6617 struct stage_path_arg spa;
6619 err = lock_worktree(worktree, LOCK_EX);
6620 if (err)
6621 return err;
6623 err = got_ref_open(&head_ref, repo,
6624 got_worktree_get_head_ref_name(worktree), 0);
6625 if (err)
6626 goto done;
6627 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6628 if (err)
6629 goto done;
6630 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6631 if (err)
6632 goto done;
6634 /* Check pre-conditions before staging anything. */
6635 oka.head_commit_id = head_commit_id;
6636 oka.worktree = worktree;
6637 oka.fileindex = fileindex;
6638 oka.repo = repo;
6639 oka.have_changes = 0;
6640 TAILQ_FOREACH(pe, paths, entry) {
6641 err = worktree_status(worktree, pe->path, fileindex, repo,
6642 check_stage_ok, &oka, NULL, NULL, 0, 0);
6643 if (err)
6644 goto done;
6646 if (!oka.have_changes) {
6647 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6648 goto done;
6651 spa.worktree = worktree;
6652 spa.fileindex = fileindex;
6653 spa.repo = repo;
6654 spa.patch_cb = patch_cb;
6655 spa.patch_arg = patch_arg;
6656 spa.status_cb = status_cb;
6657 spa.status_arg = status_arg;
6658 spa.staged_something = 0;
6659 TAILQ_FOREACH(pe, paths, entry) {
6660 err = worktree_status(worktree, pe->path, fileindex, repo,
6661 stage_path, &spa, NULL, NULL, 0, 0);
6662 if (err)
6663 goto done;
6665 if (!spa.staged_something) {
6666 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6667 goto done;
6670 sync_err = sync_fileindex(fileindex, fileindex_path);
6671 if (sync_err && err == NULL)
6672 err = sync_err;
6673 done:
6674 if (head_ref)
6675 got_ref_close(head_ref);
6676 free(head_commit_id);
6677 free(fileindex_path);
6678 if (fileindex)
6679 got_fileindex_free(fileindex);
6680 unlockerr = lock_worktree(worktree, LOCK_SH);
6681 if (unlockerr && err == NULL)
6682 err = unlockerr;
6683 return err;
6686 struct unstage_path_arg {
6687 struct got_worktree *worktree;
6688 struct got_fileindex *fileindex;
6689 struct got_repository *repo;
6690 got_worktree_checkout_cb progress_cb;
6691 void *progress_arg;
6692 got_worktree_patch_cb patch_cb;
6693 void *patch_arg;
6696 static const struct got_error *
6697 create_unstaged_content(char **path_unstaged_content,
6698 char **path_new_staged_content, struct got_object_id *blob_id,
6699 struct got_object_id *staged_blob_id, const char *relpath,
6700 struct got_repository *repo,
6701 got_worktree_patch_cb patch_cb, void *patch_arg)
6703 const struct got_error *err;
6704 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6705 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6706 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6707 struct stat sb1, sb2;
6708 struct got_diff_changes *changes = NULL;
6709 struct got_diff_state *ds = NULL;
6710 struct got_diff_args *args = NULL;
6711 struct got_diff_change *change;
6712 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6713 int have_content = 0, have_rejected_content = 0;
6715 *path_unstaged_content = NULL;
6716 *path_new_staged_content = NULL;
6718 err = got_object_id_str(&label1, blob_id);
6719 if (err)
6720 return err;
6721 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6722 if (err)
6723 goto done;
6725 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6726 if (err)
6727 goto done;
6729 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6730 if (err)
6731 goto done;
6733 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6734 if (err)
6735 goto done;
6737 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6738 if (err)
6739 goto done;
6741 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6742 if (err)
6743 goto done;
6745 if (stat(path1, &sb1) == -1) {
6746 err = got_error_from_errno2("stat", path1);
6747 goto done;
6750 if (stat(path2, &sb2) == -1) {
6751 err = got_error_from_errno2("stat", path2);
6752 goto done;
6755 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6756 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6757 if (err)
6758 goto done;
6760 err = got_opentemp_named(path_unstaged_content, &outfile,
6761 "got-unstaged-content");
6762 if (err)
6763 goto done;
6764 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6765 "got-new-staged-content");
6766 if (err)
6767 goto done;
6769 if (fseek(f1, 0L, SEEK_SET) == -1) {
6770 err = got_ferror(f1, GOT_ERR_IO);
6771 goto done;
6773 if (fseek(f2, 0L, SEEK_SET) == -1) {
6774 err = got_ferror(f2, GOT_ERR_IO);
6775 goto done;
6777 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6778 int choice;
6779 err = apply_or_reject_change(&choice, change, ++n,
6780 changes->nchanges, ds, args, diff_flags, relpath,
6781 f1, f2, &line_cur1, &line_cur2,
6782 outfile, rejectfile, patch_cb, patch_arg);
6783 if (err)
6784 goto done;
6785 if (choice == GOT_PATCH_CHOICE_YES)
6786 have_content = 1;
6787 else
6788 have_rejected_content = 1;
6789 if (choice == GOT_PATCH_CHOICE_QUIT)
6790 break;
6792 if (have_content || have_rejected_content)
6793 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6794 outfile, rejectfile);
6795 done:
6796 free(label1);
6797 if (blob)
6798 got_object_blob_close(blob);
6799 if (staged_blob)
6800 got_object_blob_close(staged_blob);
6801 if (f1 && fclose(f1) == EOF && err == NULL)
6802 err = got_error_from_errno2("fclose", path1);
6803 if (f2 && fclose(f2) == EOF && err == NULL)
6804 err = got_error_from_errno2("fclose", path2);
6805 if (outfile && fclose(outfile) == EOF && err == NULL)
6806 err = got_error_from_errno2("fclose", *path_unstaged_content);
6807 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6808 err = got_error_from_errno2("fclose", *path_new_staged_content);
6809 if (path1 && unlink(path1) == -1 && err == NULL)
6810 err = got_error_from_errno2("unlink", path1);
6811 if (path2 && unlink(path2) == -1 && err == NULL)
6812 err = got_error_from_errno2("unlink", path2);
6813 if (err || !have_content) {
6814 if (*path_unstaged_content &&
6815 unlink(*path_unstaged_content) == -1 && err == NULL)
6816 err = got_error_from_errno2("unlink",
6817 *path_unstaged_content);
6818 free(*path_unstaged_content);
6819 *path_unstaged_content = NULL;
6821 if (err || !have_rejected_content) {
6822 if (*path_new_staged_content &&
6823 unlink(*path_new_staged_content) == -1 && err == NULL)
6824 err = got_error_from_errno2("unlink",
6825 *path_new_staged_content);
6826 free(*path_new_staged_content);
6827 *path_new_staged_content = NULL;
6829 free(args);
6830 if (ds) {
6831 got_diff_state_free(ds);
6832 free(ds);
6834 if (changes)
6835 got_diff_free_changes(changes);
6836 free(path1);
6837 free(path2);
6838 return err;
6841 static const struct got_error *
6842 unstage_path(void *arg, unsigned char status,
6843 unsigned char staged_status, const char *relpath,
6844 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6845 struct got_object_id *commit_id, int dirfd, const char *de_name)
6847 const struct got_error *err = NULL;
6848 struct unstage_path_arg *a = arg;
6849 struct got_fileindex_entry *ie;
6850 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6851 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6852 char *path_new_staged_content = NULL;
6853 char *id_str = NULL, *label_orig = NULL;
6854 int local_changes_subsumed;
6855 struct stat sb;
6857 if (staged_status != GOT_STATUS_ADD &&
6858 staged_status != GOT_STATUS_MODIFY &&
6859 staged_status != GOT_STATUS_DELETE)
6860 return NULL;
6862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6863 if (ie == NULL)
6864 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6866 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6867 == -1)
6868 return got_error_from_errno("asprintf");
6870 err = got_object_id_str(&id_str,
6871 commit_id ? commit_id : a->worktree->base_commit_id);
6872 if (err)
6873 goto done;
6874 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6875 id_str) == -1) {
6876 err = got_error_from_errno("asprintf");
6877 goto done;
6880 switch (staged_status) {
6881 case GOT_STATUS_MODIFY:
6882 err = got_object_open_as_blob(&blob_base, a->repo,
6883 blob_id, 8192);
6884 if (err)
6885 break;
6886 /* fall through */
6887 case GOT_STATUS_ADD:
6888 if (a->patch_cb) {
6889 if (staged_status == GOT_STATUS_ADD) {
6890 int choice = GOT_PATCH_CHOICE_NONE;
6891 err = (*a->patch_cb)(&choice, a->patch_arg,
6892 staged_status, ie->path, NULL, 1, 1);
6893 if (err)
6894 break;
6895 if (choice != GOT_PATCH_CHOICE_YES)
6896 break;
6897 } else {
6898 err = create_unstaged_content(
6899 &path_unstaged_content,
6900 &path_new_staged_content, blob_id,
6901 staged_blob_id, ie->path, a->repo,
6902 a->patch_cb, a->patch_arg);
6903 if (err || path_unstaged_content == NULL)
6904 break;
6905 if (path_new_staged_content) {
6906 err = got_object_blob_create(
6907 &staged_blob_id,
6908 path_new_staged_content,
6909 a->repo);
6910 if (err)
6911 break;
6912 memcpy(ie->staged_blob_sha1,
6913 staged_blob_id->sha1,
6914 SHA1_DIGEST_LENGTH);
6916 err = merge_file(&local_changes_subsumed,
6917 a->worktree, blob_base, ondisk_path,
6918 relpath, got_fileindex_perms_to_st(ie),
6919 path_unstaged_content, label_orig,
6920 "unstaged", a->repo, a->progress_cb,
6921 a->progress_arg);
6922 if (err == NULL &&
6923 path_new_staged_content == NULL)
6924 got_fileindex_entry_stage_set(ie,
6925 GOT_FILEIDX_STAGE_NONE);
6926 break; /* Done with this file. */
6929 err = got_object_open_as_blob(&blob_staged, a->repo,
6930 staged_blob_id, 8192);
6931 if (err)
6932 break;
6933 err = merge_blob(&local_changes_subsumed, a->worktree,
6934 blob_base, ondisk_path, relpath,
6935 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6936 commit_id ? commit_id : a->worktree->base_commit_id,
6937 a->repo, a->progress_cb, a->progress_arg);
6938 if (err == NULL)
6939 got_fileindex_entry_stage_set(ie,
6940 GOT_FILEIDX_STAGE_NONE);
6941 break;
6942 case GOT_STATUS_DELETE:
6943 if (a->patch_cb) {
6944 int choice = GOT_PATCH_CHOICE_NONE;
6945 err = (*a->patch_cb)(&choice, a->patch_arg,
6946 staged_status, ie->path, NULL, 1, 1);
6947 if (err)
6948 break;
6949 if (choice == GOT_PATCH_CHOICE_NO)
6950 break;
6951 if (choice != GOT_PATCH_CHOICE_YES) {
6952 err = got_error(GOT_ERR_PATCH_CHOICE);
6953 break;
6956 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6957 err = get_file_status(&status, &sb, ie, ondisk_path,
6958 dirfd, de_name, a->repo);
6959 if (err)
6960 break;
6961 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6962 break;
6964 done:
6965 free(ondisk_path);
6966 if (path_unstaged_content &&
6967 unlink(path_unstaged_content) == -1 && err == NULL)
6968 err = got_error_from_errno2("unlink", path_unstaged_content);
6969 if (path_new_staged_content &&
6970 unlink(path_new_staged_content) == -1 && err == NULL)
6971 err = got_error_from_errno2("unlink", path_new_staged_content);
6972 free(path_unstaged_content);
6973 free(path_new_staged_content);
6974 if (blob_base)
6975 got_object_blob_close(blob_base);
6976 if (blob_staged)
6977 got_object_blob_close(blob_staged);
6978 free(id_str);
6979 free(label_orig);
6980 return err;
6983 const struct got_error *
6984 got_worktree_unstage(struct got_worktree *worktree,
6985 struct got_pathlist_head *paths,
6986 got_worktree_checkout_cb progress_cb, void *progress_arg,
6987 got_worktree_patch_cb patch_cb, void *patch_arg,
6988 struct got_repository *repo)
6990 const struct got_error *err = NULL, *sync_err, *unlockerr;
6991 struct got_pathlist_entry *pe;
6992 struct got_fileindex *fileindex = NULL;
6993 char *fileindex_path = NULL;
6994 struct unstage_path_arg upa;
6996 err = lock_worktree(worktree, LOCK_EX);
6997 if (err)
6998 return err;
7000 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7001 if (err)
7002 goto done;
7004 upa.worktree = worktree;
7005 upa.fileindex = fileindex;
7006 upa.repo = repo;
7007 upa.progress_cb = progress_cb;
7008 upa.progress_arg = progress_arg;
7009 upa.patch_cb = patch_cb;
7010 upa.patch_arg = patch_arg;
7011 TAILQ_FOREACH(pe, paths, entry) {
7012 err = worktree_status(worktree, pe->path, fileindex, repo,
7013 unstage_path, &upa, NULL, NULL, 0, 0);
7014 if (err)
7015 goto done;
7018 sync_err = sync_fileindex(fileindex, fileindex_path);
7019 if (sync_err && err == NULL)
7020 err = sync_err;
7021 done:
7022 free(fileindex_path);
7023 if (fileindex)
7024 got_fileindex_free(fileindex);
7025 unlockerr = lock_worktree(worktree, LOCK_SH);
7026 if (unlockerr && err == NULL)
7027 err = unlockerr;
7028 return err;