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 (parent == NULL) {
993 err = got_error_from_errno2("dirname", ondisk_path);
994 goto done;
996 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
997 err = got_error_from_errno("asprintf");
998 goto done;
1003 * unveil(2) restricts our view of paths in the filesystem.
1004 * ENOENT will occur if a link target path does not exist or
1005 * if it points outside our unveiled path space.
1007 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1008 if (resolved_path == NULL) {
1009 if (errno != ENOENT)
1010 return got_error_from_errno2("realpath", target_path);
1013 /* Only allow symlinks pointing at paths within the work tree. */
1014 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1015 abspath : target_path), worktree->root_path,
1016 strlen(worktree->root_path))) {
1017 /* install as a regular file */
1018 got_object_blob_rewind(blob);
1019 err = install_blob(worktree, ondisk_path, path,
1020 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1021 restoring_missing_file, reverting_versioned_file,
1022 repo, progress_cb, progress_arg);
1023 goto done;
1026 if (symlink(target_path, ondisk_path) == -1) {
1027 if (errno == EEXIST) {
1028 struct stat sb;
1029 ssize_t elen;
1030 char etarget[PATH_MAX];
1031 if (lstat(ondisk_path, &sb) == -1) {
1032 err = got_error_from_errno2("lstat",
1033 ondisk_path);
1034 goto done;
1036 if (!S_ISLNK(sb.st_mode)) {
1037 err = got_error_path(ondisk_path,
1038 GOT_ERR_FILE_OBSTRUCTED);
1039 goto done;
1041 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1042 if (elen == -1) {
1043 err = got_error_from_errno2("readlink",
1044 ondisk_path);
1045 goto done;
1047 if (elen == target_len &&
1048 memcmp(etarget, target_path, target_len) == 0) {
1049 err = NULL; /* nothing to do */
1050 goto done;
1051 } else {
1052 if (unlink(ondisk_path) == -1) {
1053 err = got_error_from_errno2("unlink",
1054 ondisk_path);
1055 goto done;
1057 if (symlink(target_path, ondisk_path) == -1) {
1058 err = got_error_from_errno3("symlink",
1059 target_path, ondisk_path);
1060 goto done;
1063 err = (*progress_cb)(progress_arg,
1064 GOT_STATUS_UPDATE, path);
1065 goto done;
1069 if (errno == ENOENT) {
1070 char *parent = dirname(ondisk_path);
1071 if (parent == NULL) {
1072 err = got_error_from_errno2("dirname",
1073 ondisk_path);
1074 goto done;
1076 err = add_dir_on_disk(worktree, parent);
1077 if (err)
1078 goto done;
1080 * Retry, and fall through to error handling
1081 * below if this second attempt fails.
1083 if (symlink(target_path, ondisk_path) != -1) {
1084 err = NULL; /* success */
1085 goto done;
1089 /* Handle errors from first or second creation attempt. */
1090 if (errno == ENAMETOOLONG) {
1091 /* bad target path; install as a regular file */
1092 got_object_blob_rewind(blob);
1093 err = install_blob(worktree, ondisk_path, path,
1094 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1095 restoring_missing_file, reverting_versioned_file,
1096 repo, progress_cb, progress_arg);
1097 } else if (errno == ENOTDIR) {
1098 err = got_error_path(ondisk_path,
1099 GOT_ERR_FILE_OBSTRUCTED);
1100 } else {
1101 err = got_error_from_errno3("symlink",
1102 target_path, ondisk_path);
1104 } else
1105 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1106 done:
1107 free(resolved_path);
1108 free(abspath);
1109 return err;
1112 static const struct got_error *
1113 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1114 const char *path, mode_t te_mode, mode_t st_mode,
1115 struct got_blob_object *blob, int restoring_missing_file,
1116 int reverting_versioned_file, struct got_repository *repo,
1117 got_worktree_checkout_cb progress_cb, void *progress_arg)
1119 const struct got_error *err = NULL;
1120 int fd = -1;
1121 size_t len, hdrlen;
1122 int update = 0;
1123 char *tmppath = NULL;
1125 if (S_ISLNK(te_mode))
1126 return install_symlink(worktree, ondisk_path, path, te_mode,
1127 st_mode, blob, restoring_missing_file,
1128 reverting_versioned_file, repo, progress_cb, progress_arg);
1130 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1131 GOT_DEFAULT_FILE_MODE);
1132 if (fd == -1) {
1133 if (errno == ENOENT) {
1134 char *parent = dirname(path);
1135 if (parent == NULL)
1136 return got_error_from_errno2("dirname", path);
1137 err = add_dir_on_disk(worktree, parent);
1138 if (err)
1139 return err;
1140 fd = open(ondisk_path,
1141 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1142 GOT_DEFAULT_FILE_MODE);
1143 if (fd == -1)
1144 return got_error_from_errno2("open",
1145 ondisk_path);
1146 } else if (errno == EEXIST) {
1147 if (!S_ISREG(st_mode)) {
1148 /* TODO file is obstructed; do something */
1149 err = got_error_path(ondisk_path,
1150 GOT_ERR_FILE_OBSTRUCTED);
1151 goto done;
1152 } else {
1153 err = got_opentemp_named_fd(&tmppath, &fd,
1154 ondisk_path);
1155 if (err)
1156 goto done;
1157 update = 1;
1159 } else
1160 return got_error_from_errno2("open", ondisk_path);
1163 if (restoring_missing_file)
1164 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1165 else if (reverting_versioned_file)
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1167 else
1168 err = (*progress_cb)(progress_arg,
1169 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1170 if (err)
1171 goto done;
1173 hdrlen = got_object_blob_get_hdrlen(blob);
1174 do {
1175 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1176 err = got_object_blob_read_block(&len, blob);
1177 if (err)
1178 break;
1179 if (len > 0) {
1180 /* Skip blob object header first time around. */
1181 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1182 if (outlen == -1) {
1183 err = got_error_from_errno("write");
1184 goto done;
1185 } else if (outlen != len - hdrlen) {
1186 err = got_error(GOT_ERR_IO);
1187 goto done;
1189 hdrlen = 0;
1191 } while (len != 0);
1193 if (fsync(fd) != 0) {
1194 err = got_error_from_errno("fsync");
1195 goto done;
1198 if (update) {
1199 if (rename(tmppath, ondisk_path) != 0) {
1200 err = got_error_from_errno3("rename", tmppath,
1201 ondisk_path);
1202 unlink(tmppath);
1203 goto done;
1207 if (chmod(ondisk_path,
1208 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1209 err = got_error_from_errno2("chmod", ondisk_path);
1210 goto done;
1213 done:
1214 if (fd != -1 && close(fd) != 0 && err == NULL)
1215 err = got_error_from_errno("close");
1216 free(tmppath);
1217 return err;
1220 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1221 static const struct got_error *
1222 get_modified_file_content_status(unsigned char *status, FILE *f)
1224 const struct got_error *err = NULL;
1225 const char *markers[3] = {
1226 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1227 GOT_DIFF_CONFLICT_MARKER_SEP,
1228 GOT_DIFF_CONFLICT_MARKER_END
1230 int i = 0;
1231 char *line;
1232 size_t len;
1233 const char delim[3] = {'\0', '\0', '\0'};
1235 while (*status == GOT_STATUS_MODIFY) {
1236 line = fparseln(f, &len, NULL, delim, 0);
1237 if (line == NULL) {
1238 if (feof(f))
1239 break;
1240 err = got_ferror(f, GOT_ERR_IO);
1241 break;
1244 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1245 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1246 == 0)
1247 *status = GOT_STATUS_CONFLICT;
1248 else
1249 i++;
1253 return err;
1256 static int
1257 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1259 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1260 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1263 static int
1264 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1266 return !(ie->ctime_sec == sb->st_ctime &&
1267 ie->ctime_nsec == sb->st_ctimensec &&
1268 ie->mtime_sec == sb->st_mtime &&
1269 ie->mtime_nsec == sb->st_mtimensec &&
1270 ie->size == (sb->st_size & 0xffffffff) &&
1271 !xbit_differs(ie, sb->st_mode));
1274 static unsigned char
1275 get_staged_status(struct got_fileindex_entry *ie)
1277 switch (got_fileindex_entry_stage_get(ie)) {
1278 case GOT_FILEIDX_STAGE_ADD:
1279 return GOT_STATUS_ADD;
1280 case GOT_FILEIDX_STAGE_DELETE:
1281 return GOT_STATUS_DELETE;
1282 case GOT_FILEIDX_STAGE_MODIFY:
1283 return GOT_STATUS_MODIFY;
1284 default:
1285 return GOT_STATUS_NO_CHANGE;
1289 static const struct got_error *
1290 get_symlink_status(unsigned char *status, struct stat *sb,
1291 struct got_fileindex_entry *ie, const char *abspath,
1292 int dirfd, const char *de_name, struct got_blob_object *blob)
1294 const struct got_error *err = NULL;
1295 char target_path[PATH_MAX];
1296 char etarget[PATH_MAX];
1297 ssize_t elen;
1298 size_t len, target_len = 0;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *status = GOT_STATUS_NO_CHANGE;
1304 /* Blob object content specifies the target path of the link. */
1305 do {
1306 err = got_object_blob_read_block(&len, blob);
1307 if (err)
1308 return err;
1309 if (len + target_len >= sizeof(target_path)) {
1311 * Should not happen. The blob contents were OK
1312 * when this symlink was installed.
1314 return got_error(GOT_ERR_NO_SPACE);
1316 if (len > 0) {
1317 /* Skip blob object header first time around. */
1318 memcpy(target_path + target_len, buf + hdrlen,
1319 len - hdrlen);
1320 target_len += len - hdrlen;
1321 hdrlen = 0;
1323 } while (len != 0);
1324 target_path[target_len] = '\0';
1326 if (dirfd != -1) {
1327 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1328 if (elen == -1)
1329 return got_error_from_errno2("readlinkat", abspath);
1330 } else {
1331 elen = readlink(abspath, etarget, sizeof(etarget));
1332 if (elen == -1)
1333 return got_error_from_errno2("readlinkat", abspath);
1336 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1337 *status = GOT_STATUS_MODIFY;
1339 return NULL;
1342 static const struct got_error *
1343 get_file_status(unsigned char *status, struct stat *sb,
1344 struct got_fileindex_entry *ie, const char *abspath,
1345 int dirfd, const char *de_name, struct got_repository *repo)
1347 const struct got_error *err = NULL;
1348 struct got_object_id id;
1349 size_t hdrlen;
1350 int fd = -1;
1351 FILE *f = NULL;
1352 uint8_t fbuf[8192];
1353 struct got_blob_object *blob = NULL;
1354 size_t flen, blen;
1355 unsigned char staged_status = get_staged_status(ie);
1357 *status = GOT_STATUS_NO_CHANGE;
1360 * Whenever the caller provides a directory descriptor and a
1361 * directory entry name for the file, use them! This prevents
1362 * race conditions if filesystem paths change beneath our feet.
1364 if (dirfd != -1) {
1365 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1366 if (errno == ENOENT) {
1367 if (got_fileindex_entry_has_file_on_disk(ie))
1368 *status = GOT_STATUS_MISSING;
1369 else
1370 *status = GOT_STATUS_DELETE;
1371 goto done;
1373 err = got_error_from_errno2("fstatat", abspath);
1374 goto done;
1376 } else {
1377 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1378 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1379 return got_error_from_errno2("open", abspath);
1380 else if (fd == -1 && errno == ELOOP) {
1381 if (lstat(abspath, sb) == -1)
1382 return got_error_from_errno2("lstat", abspath);
1383 } else if (fd == -1 || fstat(fd, sb) == -1) {
1384 if (errno == ENOENT) {
1385 if (got_fileindex_entry_has_file_on_disk(ie))
1386 *status = GOT_STATUS_MISSING;
1387 else
1388 *status = GOT_STATUS_DELETE;
1389 goto done;
1391 err = got_error_from_errno2("fstat", abspath);
1392 goto done;
1396 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1397 *status = GOT_STATUS_OBSTRUCTED;
1398 goto done;
1401 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1402 *status = GOT_STATUS_DELETE;
1403 goto done;
1404 } else if (!got_fileindex_entry_has_blob(ie) &&
1405 staged_status != GOT_STATUS_ADD) {
1406 *status = GOT_STATUS_ADD;
1407 goto done;
1410 if (!stat_info_differs(ie, sb))
1411 goto done;
1413 if (staged_status == GOT_STATUS_MODIFY ||
1414 staged_status == GOT_STATUS_ADD)
1415 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1416 else
1417 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1419 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1420 if (err)
1421 goto done;
1423 if (S_ISLNK(sb->st_mode)) {
1424 /* Staging changes to symlinks is not yet(?) supported. */
1425 if (staged_status != GOT_STATUS_NO_CHANGE) {
1426 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1427 goto done;
1429 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1430 de_name, blob);
1431 goto done;
1435 if (dirfd != -1) {
1436 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1437 if (fd == -1) {
1438 err = got_error_from_errno2("openat", abspath);
1439 goto done;
1443 f = fdopen(fd, "r");
1444 if (f == NULL) {
1445 err = got_error_from_errno2("fdopen", abspath);
1446 goto done;
1448 fd = -1;
1449 hdrlen = got_object_blob_get_hdrlen(blob);
1450 for (;;) {
1451 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1452 err = got_object_blob_read_block(&blen, blob);
1453 if (err)
1454 goto done;
1455 /* Skip length of blob object header first time around. */
1456 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1457 if (flen == 0 && ferror(f)) {
1458 err = got_error_from_errno("fread");
1459 goto done;
1461 if (blen == 0) {
1462 if (flen != 0)
1463 *status = GOT_STATUS_MODIFY;
1464 break;
1465 } else if (flen == 0) {
1466 if (blen != 0)
1467 *status = GOT_STATUS_MODIFY;
1468 break;
1469 } else if (blen - hdrlen == flen) {
1470 /* Skip blob object header first time around. */
1471 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1472 *status = GOT_STATUS_MODIFY;
1473 break;
1475 } else {
1476 *status = GOT_STATUS_MODIFY;
1477 break;
1479 hdrlen = 0;
1482 if (*status == GOT_STATUS_MODIFY) {
1483 rewind(f);
1484 err = get_modified_file_content_status(status, f);
1485 } else if (xbit_differs(ie, sb->st_mode))
1486 *status = GOT_STATUS_MODE_CHANGE;
1487 done:
1488 if (blob)
1489 got_object_blob_close(blob);
1490 if (f != NULL && fclose(f) == EOF && err == NULL)
1491 err = got_error_from_errno2("fclose", abspath);
1492 if (fd != -1 && close(fd) == -1 && err == NULL)
1493 err = got_error_from_errno2("close", abspath);
1494 return err;
1498 * Update timestamps in the file index if a file is unmodified and
1499 * we had to run a full content comparison to find out.
1501 static const struct got_error *
1502 sync_timestamps(char *ondisk_path, unsigned char status,
1503 struct got_fileindex_entry *ie, struct stat *sb)
1505 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1506 return got_fileindex_entry_update(ie, ondisk_path,
1507 ie->blob_sha1, ie->commit_sha1, 1);
1509 return NULL;
1512 static const struct got_error *
1513 update_blob(struct got_worktree *worktree,
1514 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1515 struct got_tree_entry *te, const char *path,
1516 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1517 void *progress_arg)
1519 const struct got_error *err = NULL;
1520 struct got_blob_object *blob = NULL;
1521 char *ondisk_path;
1522 unsigned char status = GOT_STATUS_NO_CHANGE;
1523 struct stat sb;
1525 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1526 return got_error_from_errno("asprintf");
1528 if (ie) {
1529 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1530 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1531 goto done;
1533 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1534 repo);
1535 if (err)
1536 goto done;
1537 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1538 sb.st_mode = got_fileindex_perms_to_st(ie);
1539 } else
1540 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1542 if (status == GOT_STATUS_OBSTRUCTED) {
1543 err = (*progress_cb)(progress_arg, status, path);
1544 goto done;
1546 if (status == GOT_STATUS_CONFLICT) {
1547 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1548 path);
1549 goto done;
1552 if (ie && status != GOT_STATUS_MISSING &&
1553 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1554 if (got_fileindex_entry_has_commit(ie) &&
1555 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1556 SHA1_DIGEST_LENGTH) == 0) {
1557 err = sync_timestamps(ondisk_path, status, ie, &sb);
1558 if (err)
1559 goto done;
1560 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1561 path);
1562 goto done;
1564 if (got_fileindex_entry_has_blob(ie) &&
1565 memcmp(ie->blob_sha1, te->id.sha1,
1566 SHA1_DIGEST_LENGTH) == 0) {
1567 err = sync_timestamps(ondisk_path, status, ie, &sb);
1568 goto done;
1572 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1573 if (err)
1574 goto done;
1576 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1577 int update_timestamps;
1578 struct got_blob_object *blob2 = NULL;
1579 char *label_orig = NULL;
1580 if (got_fileindex_entry_has_blob(ie)) {
1581 struct got_object_id id2;
1582 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1583 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1584 if (err)
1585 goto done;
1587 if (got_fileindex_entry_has_commit(ie)) {
1588 char id_str[SHA1_DIGEST_STRING_LENGTH];
1589 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1590 sizeof(id_str)) == NULL) {
1591 err = got_error_path(id_str,
1592 GOT_ERR_BAD_OBJ_ID_STR);
1593 goto done;
1595 if (asprintf(&label_orig, "%s: commit %s",
1596 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1597 err = got_error_from_errno("asprintf");
1598 goto done;
1601 err = merge_blob(&update_timestamps, worktree, blob2,
1602 ondisk_path, path, sb.st_mode, label_orig, blob,
1603 worktree->base_commit_id, repo,
1604 progress_cb, progress_arg);
1605 free(label_orig);
1606 if (blob2)
1607 got_object_blob_close(blob2);
1608 if (err)
1609 goto done;
1611 * Do not update timestamps of files with local changes.
1612 * Otherwise, a future status walk would treat them as
1613 * unmodified files again.
1615 err = got_fileindex_entry_update(ie, ondisk_path,
1616 blob->id.sha1, worktree->base_commit_id->sha1,
1617 update_timestamps);
1618 } else if (status == GOT_STATUS_MODE_CHANGE) {
1619 err = got_fileindex_entry_update(ie, ondisk_path,
1620 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1621 } else if (status == GOT_STATUS_DELETE) {
1622 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1623 if (err)
1624 goto done;
1625 err = got_fileindex_entry_update(ie, ondisk_path,
1626 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1627 if (err)
1628 goto done;
1629 } else {
1630 err = install_blob(worktree, ondisk_path, path, te->mode,
1631 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1632 repo, progress_cb, progress_arg);
1633 if (err)
1634 goto done;
1635 if (ie) {
1636 err = got_fileindex_entry_update(ie, ondisk_path,
1637 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1638 } else {
1639 err = create_fileindex_entry(fileindex,
1640 worktree->base_commit_id, ondisk_path, path,
1641 &blob->id);
1643 if (err)
1644 goto done;
1646 got_object_blob_close(blob);
1647 done:
1648 free(ondisk_path);
1649 return err;
1652 static const struct got_error *
1653 remove_ondisk_file(const char *root_path, const char *path)
1655 const struct got_error *err = NULL;
1656 char *ondisk_path = NULL;
1658 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1659 return got_error_from_errno("asprintf");
1661 if (unlink(ondisk_path) == -1) {
1662 if (errno != ENOENT)
1663 err = got_error_from_errno2("unlink", ondisk_path);
1664 } else {
1665 char *parent = dirname(ondisk_path);
1666 while (parent && strcmp(parent, root_path) != 0) {
1667 if (rmdir(parent) == -1) {
1668 if (errno != ENOTEMPTY)
1669 err = got_error_from_errno2("rmdir",
1670 parent);
1671 break;
1673 parent = dirname(parent);
1676 free(ondisk_path);
1677 return err;
1680 static const struct got_error *
1681 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1682 struct got_fileindex_entry *ie, struct got_repository *repo,
1683 got_worktree_checkout_cb progress_cb, void *progress_arg)
1685 const struct got_error *err = NULL;
1686 unsigned char status;
1687 struct stat sb;
1688 char *ondisk_path;
1690 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1691 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1693 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1694 == -1)
1695 return got_error_from_errno("asprintf");
1697 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1698 if (err)
1699 goto done;
1701 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1702 status == GOT_STATUS_ADD) {
1703 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1704 if (err)
1705 goto done;
1707 * Preserve the working file and change the deleted blob's
1708 * entry into a schedule-add entry.
1710 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1711 0);
1712 } else {
1713 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1714 if (err)
1715 goto done;
1716 if (status == GOT_STATUS_NO_CHANGE) {
1717 err = remove_ondisk_file(worktree->root_path, ie->path);
1718 if (err)
1719 goto done;
1721 got_fileindex_entry_remove(fileindex, ie);
1723 done:
1724 free(ondisk_path);
1725 return err;
1728 struct diff_cb_arg {
1729 struct got_fileindex *fileindex;
1730 struct got_worktree *worktree;
1731 struct got_repository *repo;
1732 got_worktree_checkout_cb progress_cb;
1733 void *progress_arg;
1734 got_cancel_cb cancel_cb;
1735 void *cancel_arg;
1738 static const struct got_error *
1739 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1740 struct got_tree_entry *te, const char *parent_path)
1742 struct diff_cb_arg *a = arg;
1744 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1745 return got_error(GOT_ERR_CANCELLED);
1747 return update_blob(a->worktree, a->fileindex, ie, te,
1748 ie->path, a->repo, a->progress_cb, a->progress_arg);
1751 static const struct got_error *
1752 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1754 struct diff_cb_arg *a = arg;
1756 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1757 return got_error(GOT_ERR_CANCELLED);
1759 return delete_blob(a->worktree, a->fileindex, ie,
1760 a->repo, a->progress_cb, a->progress_arg);
1763 static const struct got_error *
1764 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1766 struct diff_cb_arg *a = arg;
1767 const struct got_error *err;
1768 char *path;
1770 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1771 return got_error(GOT_ERR_CANCELLED);
1773 if (got_object_tree_entry_is_submodule(te))
1774 return NULL;
1776 if (asprintf(&path, "%s%s%s", parent_path,
1777 parent_path[0] ? "/" : "", te->name)
1778 == -1)
1779 return got_error_from_errno("asprintf");
1781 if (S_ISDIR(te->mode))
1782 err = add_dir_on_disk(a->worktree, path);
1783 else
1784 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1785 a->repo, a->progress_cb, a->progress_arg);
1787 free(path);
1788 return err;
1791 static const struct got_error *
1792 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1794 const struct got_error *err = NULL;
1795 char *uuidstr = NULL;
1796 uint32_t uuid_status;
1798 *refname = NULL;
1800 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1801 if (uuid_status != uuid_s_ok)
1802 return got_error_uuid(uuid_status, "uuid_to_string");
1804 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1805 == -1) {
1806 err = got_error_from_errno("asprintf");
1807 *refname = NULL;
1809 free(uuidstr);
1810 return err;
1813 const struct got_error *
1814 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1816 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1819 static const struct got_error *
1820 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1822 return get_ref_name(refname, worktree,
1823 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1826 static const struct got_error *
1827 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1829 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1832 static const struct got_error *
1833 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1835 return get_ref_name(refname, worktree,
1836 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1839 static const struct got_error *
1840 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1842 return get_ref_name(refname, worktree,
1843 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1846 static const struct got_error *
1847 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1849 return get_ref_name(refname, worktree,
1850 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1853 static const struct got_error *
1854 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1856 return get_ref_name(refname, worktree,
1857 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1860 static const struct got_error *
1861 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1863 return get_ref_name(refname, worktree,
1864 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1867 static const struct got_error *
1868 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1870 return get_ref_name(refname, worktree,
1871 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1874 const struct got_error *
1875 got_worktree_get_histedit_script_path(char **path,
1876 struct got_worktree *worktree)
1878 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1879 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1880 *path = NULL;
1881 return got_error_from_errno("asprintf");
1883 return NULL;
1887 * Prevent Git's garbage collector from deleting our base commit by
1888 * setting a reference to our base commit's ID.
1890 static const struct got_error *
1891 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1893 const struct got_error *err = NULL;
1894 struct got_reference *ref = NULL;
1895 char *refname;
1897 err = got_worktree_get_base_ref_name(&refname, worktree);
1898 if (err)
1899 return err;
1901 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1902 if (err)
1903 goto done;
1905 err = got_ref_write(ref, repo);
1906 done:
1907 free(refname);
1908 if (ref)
1909 got_ref_close(ref);
1910 return err;
1913 static const struct got_error *
1914 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1916 const struct got_error *err = NULL;
1918 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1919 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1920 err = got_error_from_errno("asprintf");
1921 *fileindex_path = NULL;
1923 return err;
1927 static const struct got_error *
1928 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1929 struct got_worktree *worktree)
1931 const struct got_error *err = NULL;
1932 FILE *index = NULL;
1934 *fileindex_path = NULL;
1935 *fileindex = got_fileindex_alloc();
1936 if (*fileindex == NULL)
1937 return got_error_from_errno("got_fileindex_alloc");
1939 err = get_fileindex_path(fileindex_path, worktree);
1940 if (err)
1941 goto done;
1943 index = fopen(*fileindex_path, "rb");
1944 if (index == NULL) {
1945 if (errno != ENOENT)
1946 err = got_error_from_errno2("fopen", *fileindex_path);
1947 } else {
1948 err = got_fileindex_read(*fileindex, index);
1949 if (fclose(index) != 0 && err == NULL)
1950 err = got_error_from_errno("fclose");
1952 done:
1953 if (err) {
1954 free(*fileindex_path);
1955 *fileindex_path = NULL;
1956 got_fileindex_free(*fileindex);
1957 *fileindex = NULL;
1959 return err;
1962 struct bump_base_commit_id_arg {
1963 struct got_object_id *base_commit_id;
1964 const char *path;
1965 size_t path_len;
1966 const char *entry_name;
1967 got_worktree_checkout_cb progress_cb;
1968 void *progress_arg;
1971 /* Bump base commit ID of all files within an updated part of the work tree. */
1972 static const struct got_error *
1973 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1975 const struct got_error *err;
1976 struct bump_base_commit_id_arg *a = arg;
1978 if (a->entry_name) {
1979 if (strcmp(ie->path, a->path) != 0)
1980 return NULL;
1981 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1982 return NULL;
1984 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1985 SHA1_DIGEST_LENGTH) == 0)
1986 return NULL;
1988 if (a->progress_cb) {
1989 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1990 ie->path);
1991 if (err)
1992 return err;
1994 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1995 return NULL;
1998 static const struct got_error *
1999 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2001 const struct got_error *err = NULL;
2002 char *new_fileindex_path = NULL;
2003 FILE *new_index = NULL;
2004 struct timespec timeout;
2006 err = got_opentemp_named(&new_fileindex_path, &new_index,
2007 fileindex_path);
2008 if (err)
2009 goto done;
2011 err = got_fileindex_write(fileindex, new_index);
2012 if (err)
2013 goto done;
2015 if (rename(new_fileindex_path, fileindex_path) != 0) {
2016 err = got_error_from_errno3("rename", new_fileindex_path,
2017 fileindex_path);
2018 unlink(new_fileindex_path);
2022 * Sleep for a short amount of time to ensure that files modified after
2023 * this program exits have a different time stamp from the one which
2024 * was recorded in the file index.
2026 timeout.tv_sec = 0;
2027 timeout.tv_nsec = 1;
2028 nanosleep(&timeout, NULL);
2029 done:
2030 if (new_index)
2031 fclose(new_index);
2032 free(new_fileindex_path);
2033 return err;
2036 static const struct got_error *
2037 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2038 struct got_object_id **tree_id, const char *wt_relpath,
2039 struct got_worktree *worktree, struct got_repository *repo)
2041 const struct got_error *err = NULL;
2042 struct got_object_id *id = NULL;
2043 char *in_repo_path = NULL;
2044 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2046 *entry_type = GOT_OBJ_TYPE_ANY;
2047 *tree_relpath = NULL;
2048 *tree_id = NULL;
2050 if (wt_relpath[0] == '\0') {
2051 /* Check out all files within the work tree. */
2052 *entry_type = GOT_OBJ_TYPE_TREE;
2053 *tree_relpath = strdup("");
2054 if (*tree_relpath == NULL) {
2055 err = got_error_from_errno("strdup");
2056 goto done;
2058 err = got_object_id_by_path(tree_id, repo,
2059 worktree->base_commit_id, worktree->path_prefix);
2060 if (err)
2061 goto done;
2062 return NULL;
2065 /* Check out a subset of files in the work tree. */
2067 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2068 is_root_wt ? "" : "/", wt_relpath) == -1) {
2069 err = got_error_from_errno("asprintf");
2070 goto done;
2073 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2074 in_repo_path);
2075 if (err)
2076 goto done;
2078 free(in_repo_path);
2079 in_repo_path = NULL;
2081 err = got_object_get_type(entry_type, repo, id);
2082 if (err)
2083 goto done;
2085 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2086 /* Check out a single file. */
2087 if (strchr(wt_relpath, '/') == NULL) {
2088 /* Check out a single file in work tree's root dir. */
2089 in_repo_path = strdup(worktree->path_prefix);
2090 if (in_repo_path == NULL) {
2091 err = got_error_from_errno("strdup");
2092 goto done;
2094 *tree_relpath = strdup("");
2095 if (*tree_relpath == NULL) {
2096 err = got_error_from_errno("strdup");
2097 goto done;
2099 } else {
2100 /* Check out a single file in a subdirectory. */
2101 err = got_path_dirname(tree_relpath, wt_relpath);
2102 if (err)
2103 return err;
2104 if (asprintf(&in_repo_path, "%s%s%s",
2105 worktree->path_prefix, is_root_wt ? "" : "/",
2106 *tree_relpath) == -1) {
2107 err = got_error_from_errno("asprintf");
2108 goto done;
2111 err = got_object_id_by_path(tree_id, repo,
2112 worktree->base_commit_id, in_repo_path);
2113 } else {
2114 /* Check out all files within a subdirectory. */
2115 *tree_id = got_object_id_dup(id);
2116 if (*tree_id == NULL) {
2117 err = got_error_from_errno("got_object_id_dup");
2118 goto done;
2120 *tree_relpath = strdup(wt_relpath);
2121 if (*tree_relpath == NULL) {
2122 err = got_error_from_errno("strdup");
2123 goto done;
2126 done:
2127 free(id);
2128 free(in_repo_path);
2129 if (err) {
2130 *entry_type = GOT_OBJ_TYPE_ANY;
2131 free(*tree_relpath);
2132 *tree_relpath = NULL;
2133 free(*tree_id);
2134 *tree_id = NULL;
2136 return err;
2139 static const struct got_error *
2140 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2142 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2143 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2145 const struct got_error *err = NULL;
2146 struct got_commit_object *commit = NULL;
2147 struct got_tree_object *tree = NULL;
2148 struct got_fileindex_diff_tree_cb diff_cb;
2149 struct diff_cb_arg arg;
2151 err = ref_base_commit(worktree, repo);
2152 if (err) {
2153 if (!(err->code == GOT_ERR_ERRNO &&
2154 (errno == EACCES || errno == EROFS)))
2155 goto done;
2156 err = (*progress_cb)(progress_arg,
2157 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2158 if (err)
2159 return err;
2162 err = got_object_open_as_commit(&commit, repo,
2163 worktree->base_commit_id);
2164 if (err)
2165 goto done;
2167 err = got_object_open_as_tree(&tree, repo, tree_id);
2168 if (err)
2169 goto done;
2171 if (entry_name &&
2172 got_object_tree_find_entry(tree, entry_name) == NULL) {
2173 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2174 goto done;
2177 diff_cb.diff_old_new = diff_old_new;
2178 diff_cb.diff_old = diff_old;
2179 diff_cb.diff_new = diff_new;
2180 arg.fileindex = fileindex;
2181 arg.worktree = worktree;
2182 arg.repo = repo;
2183 arg.progress_cb = progress_cb;
2184 arg.progress_arg = progress_arg;
2185 arg.cancel_cb = cancel_cb;
2186 arg.cancel_arg = cancel_arg;
2187 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2188 entry_name, repo, &diff_cb, &arg);
2189 done:
2190 if (tree)
2191 got_object_tree_close(tree);
2192 if (commit)
2193 got_object_commit_close(commit);
2194 return err;
2197 const struct got_error *
2198 got_worktree_checkout_files(struct got_worktree *worktree,
2199 struct got_pathlist_head *paths, struct got_repository *repo,
2200 got_worktree_checkout_cb progress_cb, void *progress_arg,
2201 got_cancel_cb cancel_cb, void *cancel_arg)
2203 const struct got_error *err = NULL, *sync_err, *unlockerr;
2204 struct got_commit_object *commit = NULL;
2205 struct got_tree_object *tree = NULL;
2206 struct got_fileindex *fileindex = NULL;
2207 char *fileindex_path = NULL;
2208 struct got_pathlist_entry *pe;
2209 struct tree_path_data {
2210 SIMPLEQ_ENTRY(tree_path_data) entry;
2211 struct got_object_id *tree_id;
2212 int entry_type;
2213 char *relpath;
2214 char *entry_name;
2215 } *tpd = NULL;
2216 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2218 SIMPLEQ_INIT(&tree_paths);
2220 err = lock_worktree(worktree, LOCK_EX);
2221 if (err)
2222 return err;
2224 /* Map all specified paths to in-repository trees. */
2225 TAILQ_FOREACH(pe, paths, entry) {
2226 tpd = malloc(sizeof(*tpd));
2227 if (tpd == NULL) {
2228 err = got_error_from_errno("malloc");
2229 goto done;
2232 err = find_tree_entry_for_checkout(&tpd->entry_type,
2233 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2234 if (err) {
2235 free(tpd);
2236 goto done;
2239 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2240 err = got_path_basename(&tpd->entry_name, pe->path);
2241 if (err) {
2242 free(tpd->relpath);
2243 free(tpd->tree_id);
2244 free(tpd);
2245 goto done;
2247 } else
2248 tpd->entry_name = NULL;
2250 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2254 * Read the file index.
2255 * Checking out files is supposed to be an idempotent operation.
2256 * If the on-disk file index is incomplete we will try to complete it.
2258 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2259 if (err)
2260 goto done;
2262 tpd = SIMPLEQ_FIRST(&tree_paths);
2263 TAILQ_FOREACH(pe, paths, entry) {
2264 struct bump_base_commit_id_arg bbc_arg;
2266 err = checkout_files(worktree, fileindex, tpd->relpath,
2267 tpd->tree_id, tpd->entry_name, repo,
2268 progress_cb, progress_arg, cancel_cb, cancel_arg);
2269 if (err)
2270 break;
2272 bbc_arg.base_commit_id = worktree->base_commit_id;
2273 bbc_arg.entry_name = tpd->entry_name;
2274 bbc_arg.path = pe->path;
2275 bbc_arg.path_len = pe->path_len;
2276 bbc_arg.progress_cb = progress_cb;
2277 bbc_arg.progress_arg = progress_arg;
2278 err = got_fileindex_for_each_entry_safe(fileindex,
2279 bump_base_commit_id, &bbc_arg);
2280 if (err)
2281 break;
2283 tpd = SIMPLEQ_NEXT(tpd, entry);
2285 sync_err = sync_fileindex(fileindex, fileindex_path);
2286 if (sync_err && err == NULL)
2287 err = sync_err;
2288 done:
2289 free(fileindex_path);
2290 if (tree)
2291 got_object_tree_close(tree);
2292 if (commit)
2293 got_object_commit_close(commit);
2294 if (fileindex)
2295 got_fileindex_free(fileindex);
2296 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2297 tpd = SIMPLEQ_FIRST(&tree_paths);
2298 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2299 free(tpd->relpath);
2300 free(tpd->tree_id);
2301 free(tpd);
2303 unlockerr = lock_worktree(worktree, LOCK_SH);
2304 if (unlockerr && err == NULL)
2305 err = unlockerr;
2306 return err;
2309 struct merge_file_cb_arg {
2310 struct got_worktree *worktree;
2311 struct got_fileindex *fileindex;
2312 got_worktree_checkout_cb progress_cb;
2313 void *progress_arg;
2314 got_cancel_cb cancel_cb;
2315 void *cancel_arg;
2316 const char *label_orig;
2317 struct got_object_id *commit_id2;
2320 static const struct got_error *
2321 merge_file_cb(void *arg, struct got_blob_object *blob1,
2322 struct got_blob_object *blob2, struct got_object_id *id1,
2323 struct got_object_id *id2, const char *path1, const char *path2,
2324 mode_t mode1, mode_t mode2, struct got_repository *repo)
2326 static const struct got_error *err = NULL;
2327 struct merge_file_cb_arg *a = arg;
2328 struct got_fileindex_entry *ie;
2329 char *ondisk_path = NULL;
2330 struct stat sb;
2331 unsigned char status;
2332 int local_changes_subsumed;
2334 if (blob1 && blob2) {
2335 ie = got_fileindex_entry_get(a->fileindex, path2,
2336 strlen(path2));
2337 if (ie == NULL)
2338 return (*a->progress_cb)(a->progress_arg,
2339 GOT_STATUS_MISSING, path2);
2341 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2342 path2) == -1)
2343 return got_error_from_errno("asprintf");
2345 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2346 repo);
2347 if (err)
2348 goto done;
2350 if (status == GOT_STATUS_DELETE) {
2351 err = (*a->progress_cb)(a->progress_arg,
2352 GOT_STATUS_MERGE, path2);
2353 goto done;
2355 if (status != GOT_STATUS_NO_CHANGE &&
2356 status != GOT_STATUS_MODIFY &&
2357 status != GOT_STATUS_CONFLICT &&
2358 status != GOT_STATUS_ADD) {
2359 err = (*a->progress_cb)(a->progress_arg, status, path2);
2360 goto done;
2363 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2364 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2365 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2366 } else if (blob1) {
2367 ie = got_fileindex_entry_get(a->fileindex, path1,
2368 strlen(path1));
2369 if (ie == NULL)
2370 return (*a->progress_cb)(a->progress_arg,
2371 GOT_STATUS_MISSING, path1);
2373 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2374 path1) == -1)
2375 return got_error_from_errno("asprintf");
2377 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2378 repo);
2379 if (err)
2380 goto done;
2382 switch (status) {
2383 case GOT_STATUS_NO_CHANGE:
2384 err = (*a->progress_cb)(a->progress_arg,
2385 GOT_STATUS_DELETE, path1);
2386 if (err)
2387 goto done;
2388 err = remove_ondisk_file(a->worktree->root_path, path1);
2389 if (err)
2390 goto done;
2391 if (ie)
2392 got_fileindex_entry_mark_deleted_from_disk(ie);
2393 break;
2394 case GOT_STATUS_DELETE:
2395 case GOT_STATUS_MISSING:
2396 err = (*a->progress_cb)(a->progress_arg,
2397 GOT_STATUS_DELETE, path1);
2398 if (err)
2399 goto done;
2400 if (ie)
2401 got_fileindex_entry_mark_deleted_from_disk(ie);
2402 break;
2403 case GOT_STATUS_ADD:
2404 case GOT_STATUS_MODIFY:
2405 case GOT_STATUS_CONFLICT:
2406 err = (*a->progress_cb)(a->progress_arg,
2407 GOT_STATUS_CANNOT_DELETE, path1);
2408 if (err)
2409 goto done;
2410 break;
2411 case GOT_STATUS_OBSTRUCTED:
2412 err = (*a->progress_cb)(a->progress_arg, status, path1);
2413 if (err)
2414 goto done;
2415 break;
2416 default:
2417 break;
2419 } else if (blob2) {
2420 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2421 path2) == -1)
2422 return got_error_from_errno("asprintf");
2423 ie = got_fileindex_entry_get(a->fileindex, path2,
2424 strlen(path2));
2425 if (ie) {
2426 err = get_file_status(&status, &sb, ie, ondisk_path,
2427 -1, NULL, repo);
2428 if (err)
2429 goto done;
2430 if (status != GOT_STATUS_NO_CHANGE &&
2431 status != GOT_STATUS_MODIFY &&
2432 status != GOT_STATUS_CONFLICT &&
2433 status != GOT_STATUS_ADD) {
2434 err = (*a->progress_cb)(a->progress_arg,
2435 status, path2);
2436 goto done;
2438 err = merge_blob(&local_changes_subsumed, a->worktree,
2439 NULL, ondisk_path, path2, sb.st_mode,
2440 a->label_orig, blob2, a->commit_id2, repo,
2441 a->progress_cb,
2442 a->progress_arg);
2443 if (status == GOT_STATUS_DELETE) {
2444 err = got_fileindex_entry_update(ie,
2445 ondisk_path, blob2->id.sha1,
2446 a->worktree->base_commit_id->sha1, 0);
2447 if (err)
2448 goto done;
2450 } else {
2451 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2452 err = install_blob(a->worktree, ondisk_path, path2,
2453 /* XXX get this from parent tree! */
2454 GOT_DEFAULT_FILE_MODE,
2455 sb.st_mode, blob2, 0, 0, repo,
2456 a->progress_cb, a->progress_arg);
2457 if (err)
2458 goto done;
2459 err = got_fileindex_entry_alloc(&ie, path2);
2460 if (err)
2461 goto done;
2462 err = got_fileindex_entry_update(ie, ondisk_path,
2463 NULL, NULL, 1);
2464 if (err) {
2465 got_fileindex_entry_free(ie);
2466 goto done;
2468 err = got_fileindex_entry_add(a->fileindex, ie);
2469 if (err) {
2470 got_fileindex_entry_free(ie);
2471 goto done;
2475 done:
2476 free(ondisk_path);
2477 return err;
2480 struct check_merge_ok_arg {
2481 struct got_worktree *worktree;
2482 struct got_repository *repo;
2485 static const struct got_error *
2486 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2488 const struct got_error *err = NULL;
2489 struct check_merge_ok_arg *a = arg;
2490 unsigned char status;
2491 struct stat sb;
2492 char *ondisk_path;
2494 /* Reject merges into a work tree with mixed base commits. */
2495 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2496 SHA1_DIGEST_LENGTH))
2497 return got_error(GOT_ERR_MIXED_COMMITS);
2499 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2500 == -1)
2501 return got_error_from_errno("asprintf");
2503 /* Reject merges into a work tree with conflicted files. */
2504 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2505 if (err)
2506 return err;
2507 if (status == GOT_STATUS_CONFLICT)
2508 return got_error(GOT_ERR_CONFLICTS);
2510 return NULL;
2513 static const struct got_error *
2514 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2515 const char *fileindex_path, struct got_object_id *commit_id1,
2516 struct got_object_id *commit_id2, struct got_repository *repo,
2517 got_worktree_checkout_cb progress_cb, void *progress_arg,
2518 got_cancel_cb cancel_cb, void *cancel_arg)
2520 const struct got_error *err = NULL, *sync_err;
2521 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2522 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2523 struct merge_file_cb_arg arg;
2524 char *label_orig = NULL;
2526 if (commit_id1) {
2527 char *id_str;
2529 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2530 worktree->path_prefix);
2531 if (err)
2532 goto done;
2534 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2535 if (err)
2536 goto done;
2538 err = got_object_id_str(&id_str, commit_id1);
2539 if (err)
2540 goto done;
2542 if (asprintf(&label_orig, "%s: commit %s",
2543 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2544 err = got_error_from_errno("asprintf");
2545 free(id_str);
2546 goto done;
2548 free(id_str);
2551 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2552 worktree->path_prefix);
2553 if (err)
2554 goto done;
2556 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2557 if (err)
2558 goto done;
2560 arg.worktree = worktree;
2561 arg.fileindex = fileindex;
2562 arg.progress_cb = progress_cb;
2563 arg.progress_arg = progress_arg;
2564 arg.cancel_cb = cancel_cb;
2565 arg.cancel_arg = cancel_arg;
2566 arg.label_orig = label_orig;
2567 arg.commit_id2 = commit_id2;
2568 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2569 sync_err = sync_fileindex(fileindex, fileindex_path);
2570 if (sync_err && err == NULL)
2571 err = sync_err;
2572 done:
2573 if (tree1)
2574 got_object_tree_close(tree1);
2575 if (tree2)
2576 got_object_tree_close(tree2);
2577 free(label_orig);
2578 return err;
2581 const struct got_error *
2582 got_worktree_merge_files(struct got_worktree *worktree,
2583 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2584 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2585 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2587 const struct got_error *err, *unlockerr;
2588 char *fileindex_path = NULL;
2589 struct got_fileindex *fileindex = NULL;
2590 struct check_merge_ok_arg mok_arg;
2592 err = lock_worktree(worktree, LOCK_EX);
2593 if (err)
2594 return err;
2596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2597 if (err)
2598 goto done;
2600 mok_arg.worktree = worktree;
2601 mok_arg.repo = repo;
2602 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2603 &mok_arg);
2604 if (err)
2605 goto done;
2607 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2608 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2609 done:
2610 if (fileindex)
2611 got_fileindex_free(fileindex);
2612 free(fileindex_path);
2613 unlockerr = lock_worktree(worktree, LOCK_SH);
2614 if (unlockerr && err == NULL)
2615 err = unlockerr;
2616 return err;
2619 struct diff_dir_cb_arg {
2620 struct got_fileindex *fileindex;
2621 struct got_worktree *worktree;
2622 const char *status_path;
2623 size_t status_path_len;
2624 struct got_repository *repo;
2625 got_worktree_status_cb status_cb;
2626 void *status_arg;
2627 got_cancel_cb cancel_cb;
2628 void *cancel_arg;
2629 /* A pathlist containing per-directory pathlists of ignore patterns. */
2630 struct got_pathlist_head ignores;
2631 int report_unchanged;
2632 int no_ignores;
2635 static const struct got_error *
2636 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2637 int dirfd, const char *de_name,
2638 got_worktree_status_cb status_cb, void *status_arg,
2639 struct got_repository *repo, int report_unchanged)
2641 const struct got_error *err = NULL;
2642 unsigned char status = GOT_STATUS_NO_CHANGE;
2643 unsigned char staged_status = get_staged_status(ie);
2644 struct stat sb;
2645 struct got_object_id blob_id, commit_id, staged_blob_id;
2646 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2647 struct got_object_id *staged_blob_idp = NULL;
2649 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2650 if (err)
2651 return err;
2653 if (status == GOT_STATUS_NO_CHANGE &&
2654 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2655 return NULL;
2657 if (got_fileindex_entry_has_blob(ie)) {
2658 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2659 blob_idp = &blob_id;
2661 if (got_fileindex_entry_has_commit(ie)) {
2662 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2663 commit_idp = &commit_id;
2665 if (staged_status == GOT_STATUS_ADD ||
2666 staged_status == GOT_STATUS_MODIFY) {
2667 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2668 SHA1_DIGEST_LENGTH);
2669 staged_blob_idp = &staged_blob_id;
2672 return (*status_cb)(status_arg, status, staged_status,
2673 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2676 static const struct got_error *
2677 status_old_new(void *arg, struct got_fileindex_entry *ie,
2678 struct dirent *de, const char *parent_path, int dirfd)
2680 const struct got_error *err = NULL;
2681 struct diff_dir_cb_arg *a = arg;
2682 char *abspath;
2684 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2685 return got_error(GOT_ERR_CANCELLED);
2687 if (got_path_cmp(parent_path, a->status_path,
2688 strlen(parent_path), a->status_path_len) != 0 &&
2689 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2690 return NULL;
2692 if (parent_path[0]) {
2693 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2694 parent_path, de->d_name) == -1)
2695 return got_error_from_errno("asprintf");
2696 } else {
2697 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2698 de->d_name) == -1)
2699 return got_error_from_errno("asprintf");
2702 err = report_file_status(ie, abspath, dirfd, de->d_name,
2703 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2704 free(abspath);
2705 return err;
2708 static const struct got_error *
2709 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2711 struct diff_dir_cb_arg *a = arg;
2712 struct got_object_id blob_id, commit_id;
2713 unsigned char status;
2715 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2716 return got_error(GOT_ERR_CANCELLED);
2718 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2719 return NULL;
2721 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2722 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2723 if (got_fileindex_entry_has_file_on_disk(ie))
2724 status = GOT_STATUS_MISSING;
2725 else
2726 status = GOT_STATUS_DELETE;
2727 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2728 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2731 void
2732 free_ignorelist(struct got_pathlist_head *ignorelist)
2734 struct got_pathlist_entry *pe;
2736 TAILQ_FOREACH(pe, ignorelist, entry)
2737 free((char *)pe->path);
2738 got_pathlist_free(ignorelist);
2741 void
2742 free_ignores(struct got_pathlist_head *ignores)
2744 struct got_pathlist_entry *pe;
2746 TAILQ_FOREACH(pe, ignores, entry) {
2747 struct got_pathlist_head *ignorelist = pe->data;
2748 free_ignorelist(ignorelist);
2749 free((char *)pe->path);
2751 got_pathlist_free(ignores);
2754 static const struct got_error *
2755 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2757 const struct got_error *err = NULL;
2758 struct got_pathlist_entry *pe = NULL;
2759 struct got_pathlist_head *ignorelist;
2760 char *line = NULL, *pattern, *dirpath = NULL;
2761 size_t linesize = 0;
2762 ssize_t linelen;
2764 ignorelist = calloc(1, sizeof(*ignorelist));
2765 if (ignorelist == NULL)
2766 return got_error_from_errno("calloc");
2767 TAILQ_INIT(ignorelist);
2769 while ((linelen = getline(&line, &linesize, f)) != -1) {
2770 if (linelen > 0 && line[linelen - 1] == '\n')
2771 line[linelen - 1] = '\0';
2773 /* Git's ignores may contain comments. */
2774 if (line[0] == '#')
2775 continue;
2777 /* Git's negated patterns are not (yet?) supported. */
2778 if (line[0] == '!')
2779 continue;
2781 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2782 line) == -1) {
2783 err = got_error_from_errno("asprintf");
2784 goto done;
2786 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2787 if (err)
2788 goto done;
2790 if (ferror(f)) {
2791 err = got_error_from_errno("getline");
2792 goto done;
2795 dirpath = strdup(path);
2796 if (dirpath == NULL) {
2797 err = got_error_from_errno("strdup");
2798 goto done;
2800 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2801 done:
2802 free(line);
2803 if (err || pe == NULL) {
2804 free(dirpath);
2805 free_ignorelist(ignorelist);
2807 return err;
2810 int
2811 match_ignores(struct got_pathlist_head *ignores, const char *path)
2813 struct got_pathlist_entry *pe;
2815 /* Handle patterns which match in all directories. */
2816 TAILQ_FOREACH(pe, ignores, entry) {
2817 struct got_pathlist_head *ignorelist = pe->data;
2818 struct got_pathlist_entry *pi;
2820 TAILQ_FOREACH(pi, ignorelist, entry) {
2821 const char *p, *pattern = pi->path;
2823 if (strncmp(pattern, "**/", 3) != 0)
2824 continue;
2825 pattern += 3;
2826 p = path;
2827 while (*p) {
2828 if (fnmatch(pattern, p,
2829 FNM_PATHNAME | FNM_LEADING_DIR)) {
2830 /* Retry in next directory. */
2831 while (*p && *p != '/')
2832 p++;
2833 while (*p == '/')
2834 p++;
2835 continue;
2837 return 1;
2843 * The ignores pathlist contains ignore lists from children before
2844 * parents, so we can find the most specific ignorelist by walking
2845 * ignores backwards.
2847 pe = TAILQ_LAST(ignores, got_pathlist_head);
2848 while (pe) {
2849 if (got_path_is_child(path, pe->path, pe->path_len)) {
2850 struct got_pathlist_head *ignorelist = pe->data;
2851 struct got_pathlist_entry *pi;
2852 TAILQ_FOREACH(pi, ignorelist, entry) {
2853 const char *pattern = pi->path;
2854 int flags = FNM_LEADING_DIR;
2855 if (strstr(pattern, "/**/") == NULL)
2856 flags |= FNM_PATHNAME;
2857 if (fnmatch(pattern, path, flags))
2858 continue;
2859 return 1;
2862 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2865 return 0;
2868 static const struct got_error *
2869 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2870 const char *path, int dirfd, const char *ignores_filename)
2872 const struct got_error *err = NULL;
2873 char *ignorespath;
2874 int fd = -1;
2875 FILE *ignoresfile = NULL;
2877 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2878 path[0] ? "/" : "", ignores_filename) == -1)
2879 return got_error_from_errno("asprintf");
2881 if (dirfd != -1) {
2882 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2883 if (fd == -1) {
2884 if (errno != ENOENT && errno != EACCES)
2885 err = got_error_from_errno2("openat",
2886 ignorespath);
2887 } else {
2888 ignoresfile = fdopen(fd, "r");
2889 if (ignoresfile == NULL)
2890 err = got_error_from_errno2("fdopen",
2891 ignorespath);
2892 else {
2893 fd = -1;
2894 err = read_ignores(ignores, path, ignoresfile);
2897 } else {
2898 ignoresfile = fopen(ignorespath, "r");
2899 if (ignoresfile == NULL) {
2900 if (errno != ENOENT && errno != EACCES)
2901 err = got_error_from_errno2("fopen",
2902 ignorespath);
2903 } else
2904 err = read_ignores(ignores, path, ignoresfile);
2907 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2908 err = got_error_from_errno2("fclose", path);
2909 if (fd != -1 && close(fd) == -1 && err == NULL)
2910 err = got_error_from_errno2("close", path);
2911 free(ignorespath);
2912 return err;
2915 static const struct got_error *
2916 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2918 const struct got_error *err = NULL;
2919 struct diff_dir_cb_arg *a = arg;
2920 char *path = NULL;
2922 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2923 return got_error(GOT_ERR_CANCELLED);
2925 if (parent_path[0]) {
2926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2927 return got_error_from_errno("asprintf");
2928 } else {
2929 path = de->d_name;
2932 if (de->d_type != DT_DIR &&
2933 got_path_is_child(path, a->status_path, a->status_path_len)
2934 && !match_ignores(&a->ignores, path))
2935 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2936 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2937 if (parent_path[0])
2938 free(path);
2939 return err;
2942 static const struct got_error *
2943 status_traverse(void *arg, const char *path, int dirfd)
2945 const struct got_error *err = NULL;
2946 struct diff_dir_cb_arg *a = arg;
2948 if (a->no_ignores)
2949 return NULL;
2951 err = add_ignores(&a->ignores, a->worktree->root_path,
2952 path, dirfd, ".cvsignore");
2953 if (err)
2954 return err;
2956 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2957 dirfd, ".gitignore");
2959 return err;
2962 static const struct got_error *
2963 report_single_file_status(const char *path, const char *ondisk_path,
2964 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2965 void *status_arg, struct got_repository *repo, int report_unchanged)
2967 struct got_fileindex_entry *ie;
2968 struct stat sb;
2970 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2971 if (ie)
2972 return report_file_status(ie, ondisk_path, -1, NULL,
2973 status_cb, status_arg, repo, report_unchanged);
2975 if (lstat(ondisk_path, &sb) == -1) {
2976 if (errno != ENOENT)
2977 return got_error_from_errno2("lstat", ondisk_path);
2978 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2979 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2980 return NULL;
2983 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2984 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2985 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2987 return NULL;
2990 static const struct got_error *
2991 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2992 const char *root_path, const char *path)
2994 const struct got_error *err;
2995 char *parent_path, *next_parent_path = NULL;
2997 err = add_ignores(ignores, root_path, "", -1,
2998 ".cvsignore");
2999 if (err)
3000 return err;
3002 err = add_ignores(ignores, root_path, "", -1,
3003 ".gitignore");
3004 if (err)
3005 return err;
3007 err = got_path_dirname(&parent_path, path);
3008 if (err) {
3009 if (err->code == GOT_ERR_BAD_PATH)
3010 return NULL; /* cannot traverse parent */
3011 return err;
3013 for (;;) {
3014 err = add_ignores(ignores, root_path, parent_path, -1,
3015 ".cvsignore");
3016 if (err)
3017 break;
3018 err = add_ignores(ignores, root_path, parent_path, -1,
3019 ".gitignore");
3020 if (err)
3021 break;
3022 err = got_path_dirname(&next_parent_path, parent_path);
3023 if (err) {
3024 if (err->code == GOT_ERR_BAD_PATH)
3025 err = NULL; /* traversed everything */
3026 break;
3028 free(parent_path);
3029 parent_path = next_parent_path;
3030 next_parent_path = NULL;
3033 free(parent_path);
3034 free(next_parent_path);
3035 return err;
3038 static const struct got_error *
3039 worktree_status(struct got_worktree *worktree, const char *path,
3040 struct got_fileindex *fileindex, struct got_repository *repo,
3041 got_worktree_status_cb status_cb, void *status_arg,
3042 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3043 int report_unchanged)
3045 const struct got_error *err = NULL;
3046 int fd = -1;
3047 struct got_fileindex_diff_dir_cb fdiff_cb;
3048 struct diff_dir_cb_arg arg;
3049 char *ondisk_path = NULL;
3051 TAILQ_INIT(&arg.ignores);
3053 if (asprintf(&ondisk_path, "%s%s%s",
3054 worktree->root_path, path[0] ? "/" : "", path) == -1)
3055 return got_error_from_errno("asprintf");
3057 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3058 if (fd == -1) {
3059 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3060 errno != ELOOP)
3061 err = got_error_from_errno2("open", ondisk_path);
3062 else
3063 err = report_single_file_status(path, ondisk_path,
3064 fileindex, status_cb, status_arg, repo,
3065 report_unchanged);
3066 } else {
3067 fdiff_cb.diff_old_new = status_old_new;
3068 fdiff_cb.diff_old = status_old;
3069 fdiff_cb.diff_new = status_new;
3070 fdiff_cb.diff_traverse = status_traverse;
3071 arg.fileindex = fileindex;
3072 arg.worktree = worktree;
3073 arg.status_path = path;
3074 arg.status_path_len = strlen(path);
3075 arg.repo = repo;
3076 arg.status_cb = status_cb;
3077 arg.status_arg = status_arg;
3078 arg.cancel_cb = cancel_cb;
3079 arg.cancel_arg = cancel_arg;
3080 arg.report_unchanged = report_unchanged;
3081 arg.no_ignores = no_ignores;
3082 if (!no_ignores) {
3083 err = add_ignores_from_parent_paths(&arg.ignores,
3084 worktree->root_path, path);
3085 if (err)
3086 goto done;
3088 err = got_fileindex_diff_dir(fileindex, fd,
3089 worktree->root_path, path, repo, &fdiff_cb, &arg);
3091 done:
3092 free_ignores(&arg.ignores);
3093 if (fd != -1 && close(fd) != 0 && err == NULL)
3094 err = got_error_from_errno("close");
3095 free(ondisk_path);
3096 return err;
3099 const struct got_error *
3100 got_worktree_status(struct got_worktree *worktree,
3101 struct got_pathlist_head *paths, struct got_repository *repo,
3102 got_worktree_status_cb status_cb, void *status_arg,
3103 got_cancel_cb cancel_cb, void *cancel_arg)
3105 const struct got_error *err = NULL;
3106 char *fileindex_path = NULL;
3107 struct got_fileindex *fileindex = NULL;
3108 struct got_pathlist_entry *pe;
3110 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3111 if (err)
3112 return err;
3114 TAILQ_FOREACH(pe, paths, entry) {
3115 err = worktree_status(worktree, pe->path, fileindex, repo,
3116 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3117 if (err)
3118 break;
3120 free(fileindex_path);
3121 got_fileindex_free(fileindex);
3122 return err;
3125 const struct got_error *
3126 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3127 const char *arg)
3129 const struct got_error *err = NULL;
3130 char *resolved = NULL, *cwd = NULL, *path = NULL;
3131 size_t len;
3132 struct stat sb;
3134 *wt_path = NULL;
3136 cwd = getcwd(NULL, 0);
3137 if (cwd == NULL)
3138 return got_error_from_errno("getcwd");
3140 if (lstat(arg, &sb) == -1) {
3141 if (errno != ENOENT) {
3142 err = got_error_from_errno2("lstat", arg);
3143 goto done;
3146 if (S_ISLNK(sb.st_mode)) {
3148 * We cannot use realpath(3) with symlinks since we want to
3149 * operate on the symlink itself.
3150 * But we can make the path absolute, assuming it is relative
3151 * to the current working directory, and then canonicalize it.
3153 char *abspath = NULL;
3154 char canonpath[PATH_MAX];
3155 if (!got_path_is_absolute(arg)) {
3156 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3157 err = got_error_from_errno("asprintf");
3158 goto done;
3162 err = got_canonpath(abspath ? abspath : arg, canonpath,
3163 sizeof(canonpath));
3164 if (err)
3165 goto done;
3166 resolved = strdup(canonpath);
3167 if (resolved == NULL) {
3168 err = got_error_from_errno("strdup");
3169 goto done;
3171 } else {
3172 resolved = realpath(arg, NULL);
3173 if (resolved == NULL) {
3174 if (errno != ENOENT) {
3175 err = got_error_from_errno2("realpath", arg);
3176 goto done;
3178 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3179 err = got_error_from_errno("asprintf");
3180 goto done;
3185 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3186 strlen(got_worktree_get_root_path(worktree)))) {
3187 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3188 goto done;
3191 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3192 err = got_path_skip_common_ancestor(&path,
3193 got_worktree_get_root_path(worktree), resolved);
3194 if (err)
3195 goto done;
3196 } else {
3197 path = strdup("");
3198 if (path == NULL) {
3199 err = got_error_from_errno("strdup");
3200 goto done;
3204 /* XXX status walk can't deal with trailing slash! */
3205 len = strlen(path);
3206 while (len > 0 && path[len - 1] == '/') {
3207 path[len - 1] = '\0';
3208 len--;
3210 done:
3211 free(resolved);
3212 free(cwd);
3213 if (err == NULL)
3214 *wt_path = path;
3215 else
3216 free(path);
3217 return err;
3220 struct schedule_addition_args {
3221 struct got_worktree *worktree;
3222 struct got_fileindex *fileindex;
3223 got_worktree_checkout_cb progress_cb;
3224 void *progress_arg;
3225 struct got_repository *repo;
3228 static const struct got_error *
3229 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3230 const char *relpath, struct got_object_id *blob_id,
3231 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3232 int dirfd, const char *de_name)
3234 struct schedule_addition_args *a = arg;
3235 const struct got_error *err = NULL;
3236 struct got_fileindex_entry *ie;
3237 struct stat sb;
3238 char *ondisk_path;
3240 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3241 relpath) == -1)
3242 return got_error_from_errno("asprintf");
3244 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3245 if (ie) {
3246 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3247 de_name, a->repo);
3248 if (err)
3249 goto done;
3250 /* Re-adding an existing entry is a no-op. */
3251 if (status == GOT_STATUS_ADD)
3252 goto done;
3253 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3254 if (err)
3255 goto done;
3258 if (status != GOT_STATUS_UNVERSIONED) {
3259 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3260 goto done;
3263 err = got_fileindex_entry_alloc(&ie, relpath);
3264 if (err)
3265 goto done;
3266 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3267 if (err) {
3268 got_fileindex_entry_free(ie);
3269 goto done;
3271 err = got_fileindex_entry_add(a->fileindex, ie);
3272 if (err) {
3273 got_fileindex_entry_free(ie);
3274 goto done;
3276 done:
3277 free(ondisk_path);
3278 if (err)
3279 return err;
3280 if (status == GOT_STATUS_ADD)
3281 return NULL;
3282 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3285 const struct got_error *
3286 got_worktree_schedule_add(struct got_worktree *worktree,
3287 struct got_pathlist_head *paths,
3288 got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 struct got_repository *repo, int no_ignores)
3291 struct got_fileindex *fileindex = NULL;
3292 char *fileindex_path = NULL;
3293 const struct got_error *err = NULL, *sync_err, *unlockerr;
3294 struct got_pathlist_entry *pe;
3295 struct schedule_addition_args saa;
3297 err = lock_worktree(worktree, LOCK_EX);
3298 if (err)
3299 return err;
3301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3302 if (err)
3303 goto done;
3305 saa.worktree = worktree;
3306 saa.fileindex = fileindex;
3307 saa.progress_cb = progress_cb;
3308 saa.progress_arg = progress_arg;
3309 saa.repo = repo;
3311 TAILQ_FOREACH(pe, paths, entry) {
3312 err = worktree_status(worktree, pe->path, fileindex, repo,
3313 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3314 if (err)
3315 break;
3317 sync_err = sync_fileindex(fileindex, fileindex_path);
3318 if (sync_err && err == NULL)
3319 err = sync_err;
3320 done:
3321 free(fileindex_path);
3322 if (fileindex)
3323 got_fileindex_free(fileindex);
3324 unlockerr = lock_worktree(worktree, LOCK_SH);
3325 if (unlockerr && err == NULL)
3326 err = unlockerr;
3327 return err;
3330 struct schedule_deletion_args {
3331 struct got_worktree *worktree;
3332 struct got_fileindex *fileindex;
3333 got_worktree_delete_cb progress_cb;
3334 void *progress_arg;
3335 struct got_repository *repo;
3336 int delete_local_mods;
3337 int keep_on_disk;
3340 static const struct got_error *
3341 schedule_for_deletion(void *arg, unsigned char status,
3342 unsigned char staged_status, const char *relpath,
3343 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3344 struct got_object_id *commit_id, int dirfd, const char *de_name)
3346 struct schedule_deletion_args *a = arg;
3347 const struct got_error *err = NULL;
3348 struct got_fileindex_entry *ie = NULL;
3349 struct stat sb;
3350 char *ondisk_path, *parent = NULL;
3352 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3353 if (ie == NULL)
3354 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3356 staged_status = get_staged_status(ie);
3357 if (staged_status != GOT_STATUS_NO_CHANGE) {
3358 if (staged_status == GOT_STATUS_DELETE)
3359 return NULL;
3360 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3363 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3364 relpath) == -1)
3365 return got_error_from_errno("asprintf");
3367 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3368 a->repo);
3369 if (err)
3370 goto done;
3372 if (status != GOT_STATUS_NO_CHANGE) {
3373 if (status == GOT_STATUS_DELETE)
3374 goto done;
3375 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3376 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3377 goto done;
3379 if (status != GOT_STATUS_MODIFY &&
3380 status != GOT_STATUS_MISSING) {
3381 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3382 goto done;
3386 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3387 if (dirfd != -1) {
3388 if (unlinkat(dirfd, de_name, 0) != 0) {
3389 err = got_error_from_errno2("unlinkat",
3390 ondisk_path);
3391 goto done;
3393 } else if (unlink(ondisk_path) != 0) {
3394 err = got_error_from_errno2("unlink", ondisk_path);
3395 goto done;
3398 parent = dirname(ondisk_path);
3400 if (parent == NULL) {
3401 err = got_error_from_errno2("dirname", ondisk_path);
3402 goto done;
3404 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3405 if (rmdir(parent) == -1) {
3406 if (errno != ENOTEMPTY)
3407 err = got_error_from_errno2("rmdir",
3408 parent);
3409 break;
3411 parent = dirname(parent);
3412 if (parent == NULL) {
3413 err = got_error_from_errno2("dirname", parent);
3414 goto done;
3419 got_fileindex_entry_mark_deleted_from_disk(ie);
3420 done:
3421 free(ondisk_path);
3422 if (err)
3423 return err;
3424 if (status == GOT_STATUS_DELETE)
3425 return NULL;
3426 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3427 staged_status, relpath);
3430 const struct got_error *
3431 got_worktree_schedule_delete(struct got_worktree *worktree,
3432 struct got_pathlist_head *paths, int delete_local_mods,
3433 got_worktree_delete_cb progress_cb, void *progress_arg,
3434 struct got_repository *repo, int keep_on_disk)
3436 struct got_fileindex *fileindex = NULL;
3437 char *fileindex_path = NULL;
3438 const struct got_error *err = NULL, *sync_err, *unlockerr;
3439 struct got_pathlist_entry *pe;
3440 struct schedule_deletion_args sda;
3442 err = lock_worktree(worktree, LOCK_EX);
3443 if (err)
3444 return err;
3446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3447 if (err)
3448 goto done;
3450 sda.worktree = worktree;
3451 sda.fileindex = fileindex;
3452 sda.progress_cb = progress_cb;
3453 sda.progress_arg = progress_arg;
3454 sda.repo = repo;
3455 sda.delete_local_mods = delete_local_mods;
3456 sda.keep_on_disk = keep_on_disk;
3458 TAILQ_FOREACH(pe, paths, entry) {
3459 err = worktree_status(worktree, pe->path, fileindex, repo,
3460 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3461 if (err)
3462 break;
3464 sync_err = sync_fileindex(fileindex, fileindex_path);
3465 if (sync_err && err == NULL)
3466 err = sync_err;
3467 done:
3468 free(fileindex_path);
3469 if (fileindex)
3470 got_fileindex_free(fileindex);
3471 unlockerr = lock_worktree(worktree, LOCK_SH);
3472 if (unlockerr && err == NULL)
3473 err = unlockerr;
3474 return err;
3477 static const struct got_error *
3478 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3480 const struct got_error *err = NULL;
3481 char *line = NULL;
3482 size_t linesize = 0, n;
3483 ssize_t linelen;
3485 linelen = getline(&line, &linesize, infile);
3486 if (linelen == -1) {
3487 if (ferror(infile)) {
3488 err = got_error_from_errno("getline");
3489 goto done;
3491 return NULL;
3493 if (outfile) {
3494 n = fwrite(line, 1, linelen, outfile);
3495 if (n != linelen) {
3496 err = got_ferror(outfile, GOT_ERR_IO);
3497 goto done;
3500 if (rejectfile) {
3501 n = fwrite(line, 1, linelen, rejectfile);
3502 if (n != linelen)
3503 err = got_ferror(outfile, GOT_ERR_IO);
3505 done:
3506 free(line);
3507 return err;
3510 static const struct got_error *
3511 skip_one_line(FILE *f)
3513 char *line = NULL;
3514 size_t linesize = 0;
3515 ssize_t linelen;
3517 linelen = getline(&line, &linesize, f);
3518 if (linelen == -1) {
3519 if (ferror(f))
3520 return got_error_from_errno("getline");
3521 return NULL;
3523 free(line);
3524 return NULL;
3527 static const struct got_error *
3528 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3529 int start_old, int end_old, int start_new, int end_new,
3530 FILE *outfile, FILE *rejectfile)
3532 const struct got_error *err;
3534 /* Copy old file's lines leading up to patch. */
3535 while (!feof(f1) && *line_cur1 < start_old) {
3536 err = copy_one_line(f1, outfile, NULL);
3537 if (err)
3538 return err;
3539 (*line_cur1)++;
3541 /* Skip new file's lines leading up to patch. */
3542 while (!feof(f2) && *line_cur2 < start_new) {
3543 if (rejectfile)
3544 err = copy_one_line(f2, NULL, rejectfile);
3545 else
3546 err = skip_one_line(f2);
3547 if (err)
3548 return err;
3549 (*line_cur2)++;
3551 /* Copy patched lines. */
3552 while (!feof(f2) && *line_cur2 <= end_new) {
3553 err = copy_one_line(f2, outfile, NULL);
3554 if (err)
3555 return err;
3556 (*line_cur2)++;
3558 /* Skip over old file's replaced lines. */
3559 while (!feof(f1) && *line_cur1 <= end_old) {
3560 if (rejectfile)
3561 err = copy_one_line(f1, NULL, rejectfile);
3562 else
3563 err = skip_one_line(f1);
3564 if (err)
3565 return err;
3566 (*line_cur1)++;
3569 return NULL;
3572 static const struct got_error *
3573 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3574 FILE *outfile, FILE *rejectfile)
3576 const struct got_error *err;
3578 if (outfile) {
3579 /* Copy old file's lines until EOF. */
3580 while (!feof(f1)) {
3581 err = copy_one_line(f1, outfile, NULL);
3582 if (err)
3583 return err;
3584 (*line_cur1)++;
3587 if (rejectfile) {
3588 /* Copy new file's lines until EOF. */
3589 while (!feof(f2)) {
3590 err = copy_one_line(f2, NULL, rejectfile);
3591 if (err)
3592 return err;
3593 (*line_cur2)++;
3597 return NULL;
3600 static const struct got_error *
3601 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3602 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3603 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3604 int *line_cur2, FILE *outfile, FILE *rejectfile,
3605 got_worktree_patch_cb patch_cb, void *patch_arg)
3607 const struct got_error *err = NULL;
3608 int start_old = change->cv.a;
3609 int end_old = change->cv.b;
3610 int start_new = change->cv.c;
3611 int end_new = change->cv.d;
3612 long pos1, pos2;
3613 FILE *hunkfile;
3615 *choice = GOT_PATCH_CHOICE_NONE;
3617 hunkfile = got_opentemp();
3618 if (hunkfile == NULL)
3619 return got_error_from_errno("got_opentemp");
3621 pos1 = ftell(f1);
3622 pos2 = ftell(f2);
3624 /* XXX TODO needs error checking */
3625 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3627 if (fseek(f1, pos1, SEEK_SET) == -1) {
3628 err = got_ferror(f1, GOT_ERR_IO);
3629 goto done;
3631 if (fseek(f2, pos2, SEEK_SET) == -1) {
3632 err = got_ferror(f1, GOT_ERR_IO);
3633 goto done;
3635 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3636 err = got_ferror(hunkfile, GOT_ERR_IO);
3637 goto done;
3640 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3641 hunkfile, n, nchanges);
3642 if (err)
3643 goto done;
3645 switch (*choice) {
3646 case GOT_PATCH_CHOICE_YES:
3647 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3648 end_old, start_new, end_new, outfile, rejectfile);
3649 break;
3650 case GOT_PATCH_CHOICE_NO:
3651 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3652 end_old, start_new, end_new, rejectfile, outfile);
3653 break;
3654 case GOT_PATCH_CHOICE_QUIT:
3655 break;
3656 default:
3657 err = got_error(GOT_ERR_PATCH_CHOICE);
3658 break;
3660 done:
3661 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3662 err = got_error_from_errno("fclose");
3663 return err;
3666 struct revert_file_args {
3667 struct got_worktree *worktree;
3668 struct got_fileindex *fileindex;
3669 got_worktree_checkout_cb progress_cb;
3670 void *progress_arg;
3671 got_worktree_patch_cb patch_cb;
3672 void *patch_arg;
3673 struct got_repository *repo;
3676 static const struct got_error *
3677 create_patched_content(char **path_outfile, int reverse_patch,
3678 struct got_object_id *blob_id, const char *path2,
3679 int dirfd2, const char *de_name2,
3680 const char *relpath, struct got_repository *repo,
3681 got_worktree_patch_cb patch_cb, void *patch_arg)
3683 const struct got_error *err;
3684 struct got_blob_object *blob = NULL;
3685 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3686 int fd2 = -1;
3687 char *path1 = NULL, *id_str = NULL;
3688 struct stat sb1, sb2;
3689 struct got_diff_changes *changes = NULL;
3690 struct got_diff_state *ds = NULL;
3691 struct got_diff_args *args = NULL;
3692 struct got_diff_change *change;
3693 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3694 int n = 0;
3696 *path_outfile = NULL;
3698 err = got_object_id_str(&id_str, blob_id);
3699 if (err)
3700 return err;
3702 if (dirfd2 != -1) {
3703 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3704 if (fd2 == -1) {
3705 err = got_error_from_errno2("openat", path2);
3706 goto done;
3708 } else {
3709 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3710 if (fd2 == -1) {
3711 err = got_error_from_errno2("open", path2);
3712 goto done;
3715 if (fstat(fd2, &sb2) == -1) {
3716 err = got_error_from_errno2("fstat", path2);
3717 goto done;
3720 f2 = fdopen(fd2, "r");
3721 if (f2 == NULL) {
3722 err = got_error_from_errno2("fdopen", path2);
3723 goto done;
3725 fd2 = -1;
3727 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3728 if (err)
3729 goto done;
3731 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3732 if (err)
3733 goto done;
3735 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3736 if (err)
3737 goto done;
3739 if (stat(path1, &sb1) == -1) {
3740 err = got_error_from_errno2("stat", path1);
3741 goto done;
3744 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3745 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3746 if (err)
3747 goto done;
3749 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3750 if (err)
3751 goto done;
3753 if (fseek(f1, 0L, SEEK_SET) == -1)
3754 return got_ferror(f1, GOT_ERR_IO);
3755 if (fseek(f2, 0L, SEEK_SET) == -1)
3756 return got_ferror(f2, GOT_ERR_IO);
3757 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3758 int choice;
3759 err = apply_or_reject_change(&choice, change, ++n,
3760 changes->nchanges, ds, args, diff_flags, relpath,
3761 f1, f2, &line_cur1, &line_cur2,
3762 reverse_patch ? NULL : outfile,
3763 reverse_patch ? outfile : NULL,
3764 patch_cb, patch_arg);
3765 if (err)
3766 goto done;
3767 if (choice == GOT_PATCH_CHOICE_YES)
3768 have_content = 1;
3769 else if (choice == GOT_PATCH_CHOICE_QUIT)
3770 break;
3772 if (have_content) {
3773 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3774 reverse_patch ? NULL : outfile,
3775 reverse_patch ? outfile : NULL);
3776 if (err)
3777 goto done;
3779 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3780 err = got_error_from_errno2("chmod", path2);
3781 goto done;
3784 done:
3785 free(id_str);
3786 if (blob)
3787 got_object_blob_close(blob);
3788 if (f1 && fclose(f1) == EOF && err == NULL)
3789 err = got_error_from_errno2("fclose", path1);
3790 if (f2 && fclose(f2) == EOF && err == NULL)
3791 err = got_error_from_errno2("fclose", path2);
3792 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3793 err = got_error_from_errno2("close", path2);
3794 if (outfile && fclose(outfile) == EOF && err == NULL)
3795 err = got_error_from_errno2("fclose", *path_outfile);
3796 if (path1 && unlink(path1) == -1 && err == NULL)
3797 err = got_error_from_errno2("unlink", path1);
3798 if (err || !have_content) {
3799 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3800 err = got_error_from_errno2("unlink", *path_outfile);
3801 free(*path_outfile);
3802 *path_outfile = NULL;
3804 free(args);
3805 if (ds) {
3806 got_diff_state_free(ds);
3807 free(ds);
3809 if (changes)
3810 got_diff_free_changes(changes);
3811 free(path1);
3812 return err;
3815 static const struct got_error *
3816 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3817 const char *relpath, struct got_object_id *blob_id,
3818 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3819 int dirfd, const char *de_name)
3821 struct revert_file_args *a = arg;
3822 const struct got_error *err = NULL;
3823 char *parent_path = NULL;
3824 struct got_fileindex_entry *ie;
3825 struct got_tree_object *tree = NULL;
3826 struct got_object_id *tree_id = NULL;
3827 const struct got_tree_entry *te = NULL;
3828 char *tree_path = NULL, *te_name;
3829 char *ondisk_path = NULL, *path_content = NULL;
3830 struct got_blob_object *blob = NULL;
3832 /* Reverting a staged deletion is a no-op. */
3833 if (status == GOT_STATUS_DELETE &&
3834 staged_status != GOT_STATUS_NO_CHANGE)
3835 return NULL;
3837 if (status == GOT_STATUS_UNVERSIONED)
3838 return (*a->progress_cb)(a->progress_arg,
3839 GOT_STATUS_UNVERSIONED, relpath);
3841 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3842 if (ie == NULL)
3843 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3845 /* Construct in-repository path of tree which contains this blob. */
3846 err = got_path_dirname(&parent_path, ie->path);
3847 if (err) {
3848 if (err->code != GOT_ERR_BAD_PATH)
3849 goto done;
3850 parent_path = strdup("/");
3851 if (parent_path == NULL) {
3852 err = got_error_from_errno("strdup");
3853 goto done;
3856 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3857 tree_path = strdup(parent_path);
3858 if (tree_path == NULL) {
3859 err = got_error_from_errno("strdup");
3860 goto done;
3862 } else {
3863 if (got_path_is_root_dir(parent_path)) {
3864 tree_path = strdup(a->worktree->path_prefix);
3865 if (tree_path == NULL) {
3866 err = got_error_from_errno("strdup");
3867 goto done;
3869 } else {
3870 if (asprintf(&tree_path, "%s/%s",
3871 a->worktree->path_prefix, parent_path) == -1) {
3872 err = got_error_from_errno("asprintf");
3873 goto done;
3878 err = got_object_id_by_path(&tree_id, a->repo,
3879 a->worktree->base_commit_id, tree_path);
3880 if (err) {
3881 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3882 (status == GOT_STATUS_ADD ||
3883 staged_status == GOT_STATUS_ADD)))
3884 goto done;
3885 } else {
3886 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3887 if (err)
3888 goto done;
3890 te_name = basename(ie->path);
3891 if (te_name == NULL) {
3892 err = got_error_from_errno2("basename", ie->path);
3893 goto done;
3896 te = got_object_tree_find_entry(tree, te_name);
3897 if (te == NULL && status != GOT_STATUS_ADD &&
3898 staged_status != GOT_STATUS_ADD) {
3899 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3900 goto done;
3904 switch (status) {
3905 case GOT_STATUS_ADD:
3906 if (a->patch_cb) {
3907 int choice = GOT_PATCH_CHOICE_NONE;
3908 err = (*a->patch_cb)(&choice, a->patch_arg,
3909 status, ie->path, NULL, 1, 1);
3910 if (err)
3911 goto done;
3912 if (choice != GOT_PATCH_CHOICE_YES)
3913 break;
3915 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3916 ie->path);
3917 if (err)
3918 goto done;
3919 got_fileindex_entry_remove(a->fileindex, ie);
3920 break;
3921 case GOT_STATUS_DELETE:
3922 if (a->patch_cb) {
3923 int choice = GOT_PATCH_CHOICE_NONE;
3924 err = (*a->patch_cb)(&choice, a->patch_arg,
3925 status, ie->path, NULL, 1, 1);
3926 if (err)
3927 goto done;
3928 if (choice != GOT_PATCH_CHOICE_YES)
3929 break;
3931 /* fall through */
3932 case GOT_STATUS_MODIFY:
3933 case GOT_STATUS_MODE_CHANGE:
3934 case GOT_STATUS_CONFLICT:
3935 case GOT_STATUS_MISSING: {
3936 struct got_object_id id;
3937 if (staged_status == GOT_STATUS_ADD ||
3938 staged_status == GOT_STATUS_MODIFY) {
3939 memcpy(id.sha1, ie->staged_blob_sha1,
3940 SHA1_DIGEST_LENGTH);
3941 } else
3942 memcpy(id.sha1, ie->blob_sha1,
3943 SHA1_DIGEST_LENGTH);
3944 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3945 if (err)
3946 goto done;
3948 if (asprintf(&ondisk_path, "%s/%s",
3949 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3950 err = got_error_from_errno("asprintf");
3951 goto done;
3954 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3955 status == GOT_STATUS_CONFLICT)) {
3956 err = create_patched_content(&path_content, 1, &id,
3957 ondisk_path, dirfd, de_name, ie->path, a->repo,
3958 a->patch_cb, a->patch_arg);
3959 if (err || path_content == NULL)
3960 break;
3961 if (rename(path_content, ondisk_path) == -1) {
3962 err = got_error_from_errno3("rename",
3963 path_content, ondisk_path);
3964 goto done;
3966 } else {
3967 err = install_blob(a->worktree, ondisk_path, ie->path,
3968 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3969 got_fileindex_perms_to_st(ie), blob, 0, 1,
3970 a->repo, a->progress_cb, a->progress_arg);
3971 if (err)
3972 goto done;
3973 if (status == GOT_STATUS_DELETE ||
3974 status == GOT_STATUS_MODE_CHANGE) {
3975 err = got_fileindex_entry_update(ie,
3976 ondisk_path, blob->id.sha1,
3977 a->worktree->base_commit_id->sha1, 1);
3978 if (err)
3979 goto done;
3982 break;
3984 default:
3985 break;
3987 done:
3988 free(ondisk_path);
3989 free(path_content);
3990 free(parent_path);
3991 free(tree_path);
3992 if (blob)
3993 got_object_blob_close(blob);
3994 if (tree)
3995 got_object_tree_close(tree);
3996 free(tree_id);
3997 return err;
4000 const struct got_error *
4001 got_worktree_revert(struct got_worktree *worktree,
4002 struct got_pathlist_head *paths,
4003 got_worktree_checkout_cb progress_cb, void *progress_arg,
4004 got_worktree_patch_cb patch_cb, void *patch_arg,
4005 struct got_repository *repo)
4007 struct got_fileindex *fileindex = NULL;
4008 char *fileindex_path = NULL;
4009 const struct got_error *err = NULL, *unlockerr = NULL;
4010 const struct got_error *sync_err = NULL;
4011 struct got_pathlist_entry *pe;
4012 struct revert_file_args rfa;
4014 err = lock_worktree(worktree, LOCK_EX);
4015 if (err)
4016 return err;
4018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 if (err)
4020 goto done;
4022 rfa.worktree = worktree;
4023 rfa.fileindex = fileindex;
4024 rfa.progress_cb = progress_cb;
4025 rfa.progress_arg = progress_arg;
4026 rfa.patch_cb = patch_cb;
4027 rfa.patch_arg = patch_arg;
4028 rfa.repo = repo;
4029 TAILQ_FOREACH(pe, paths, entry) {
4030 err = worktree_status(worktree, pe->path, fileindex, repo,
4031 revert_file, &rfa, NULL, NULL, 0, 0);
4032 if (err)
4033 break;
4035 sync_err = sync_fileindex(fileindex, fileindex_path);
4036 if (sync_err && err == NULL)
4037 err = sync_err;
4038 done:
4039 free(fileindex_path);
4040 if (fileindex)
4041 got_fileindex_free(fileindex);
4042 unlockerr = lock_worktree(worktree, LOCK_SH);
4043 if (unlockerr && err == NULL)
4044 err = unlockerr;
4045 return err;
4048 static void
4049 free_commitable(struct got_commitable *ct)
4051 free(ct->path);
4052 free(ct->in_repo_path);
4053 free(ct->ondisk_path);
4054 free(ct->blob_id);
4055 free(ct->base_blob_id);
4056 free(ct->staged_blob_id);
4057 free(ct->base_commit_id);
4058 free(ct);
4061 struct collect_commitables_arg {
4062 struct got_pathlist_head *commitable_paths;
4063 struct got_repository *repo;
4064 struct got_worktree *worktree;
4065 int have_staged_files;
4068 static const struct got_error *
4069 collect_commitables(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct collect_commitables_arg *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_commitable *ct = NULL;
4077 struct got_pathlist_entry *new = NULL;
4078 char *parent_path = NULL, *path = NULL;
4079 struct stat sb;
4081 if (a->have_staged_files) {
4082 if (staged_status != GOT_STATUS_MODIFY &&
4083 staged_status != GOT_STATUS_ADD &&
4084 staged_status != GOT_STATUS_DELETE)
4085 return NULL;
4086 } else {
4087 if (status == GOT_STATUS_CONFLICT)
4088 return got_error(GOT_ERR_COMMIT_CONFLICT);
4090 if (status != GOT_STATUS_MODIFY &&
4091 status != GOT_STATUS_MODE_CHANGE &&
4092 status != GOT_STATUS_ADD &&
4093 status != GOT_STATUS_DELETE)
4094 return NULL;
4097 if (asprintf(&path, "/%s", relpath) == -1) {
4098 err = got_error_from_errno("asprintf");
4099 goto done;
4101 if (strcmp(path, "/") == 0) {
4102 parent_path = strdup("");
4103 if (parent_path == NULL)
4104 return got_error_from_errno("strdup");
4105 } else {
4106 err = got_path_dirname(&parent_path, path);
4107 if (err)
4108 return err;
4111 ct = calloc(1, sizeof(*ct));
4112 if (ct == NULL) {
4113 err = got_error_from_errno("calloc");
4114 goto done;
4117 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4118 relpath) == -1) {
4119 err = got_error_from_errno("asprintf");
4120 goto done;
4122 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4123 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4124 } else {
4125 if (dirfd != -1) {
4126 if (fstatat(dirfd, de_name, &sb,
4127 AT_SYMLINK_NOFOLLOW) == -1) {
4128 err = got_error_from_errno2("fstatat",
4129 ct->ondisk_path);
4130 goto done;
4132 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4133 err = got_error_from_errno2("lstat", ct->ondisk_path);
4134 goto done;
4136 ct->mode = sb.st_mode;
4139 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4140 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4141 relpath) == -1) {
4142 err = got_error_from_errno("asprintf");
4143 goto done;
4146 ct->status = status;
4147 ct->staged_status = staged_status;
4148 ct->blob_id = NULL; /* will be filled in when blob gets created */
4149 if (ct->status != GOT_STATUS_ADD &&
4150 ct->staged_status != GOT_STATUS_ADD) {
4151 ct->base_blob_id = got_object_id_dup(blob_id);
4152 if (ct->base_blob_id == NULL) {
4153 err = got_error_from_errno("got_object_id_dup");
4154 goto done;
4156 ct->base_commit_id = got_object_id_dup(commit_id);
4157 if (ct->base_commit_id == NULL) {
4158 err = got_error_from_errno("got_object_id_dup");
4159 goto done;
4162 if (ct->staged_status == GOT_STATUS_ADD ||
4163 ct->staged_status == GOT_STATUS_MODIFY) {
4164 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4165 if (ct->staged_blob_id == NULL) {
4166 err = got_error_from_errno("got_object_id_dup");
4167 goto done;
4170 ct->path = strdup(path);
4171 if (ct->path == NULL) {
4172 err = got_error_from_errno("strdup");
4173 goto done;
4175 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4176 done:
4177 if (ct && (err || new == NULL))
4178 free_commitable(ct);
4179 free(parent_path);
4180 free(path);
4181 return err;
4184 static const struct got_error *write_tree(struct got_object_id **, int *,
4185 struct got_tree_object *, const char *, struct got_pathlist_head *,
4186 got_worktree_status_cb status_cb, void *status_arg,
4187 struct got_repository *);
4189 static const struct got_error *
4190 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4191 struct got_tree_entry *te, const char *parent_path,
4192 struct got_pathlist_head *commitable_paths,
4193 got_worktree_status_cb status_cb, void *status_arg,
4194 struct got_repository *repo)
4196 const struct got_error *err = NULL;
4197 struct got_tree_object *subtree;
4198 char *subpath;
4200 if (asprintf(&subpath, "%s%s%s", parent_path,
4201 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4202 return got_error_from_errno("asprintf");
4204 err = got_object_open_as_tree(&subtree, repo, &te->id);
4205 if (err)
4206 return err;
4208 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4209 commitable_paths, status_cb, status_arg, repo);
4210 got_object_tree_close(subtree);
4211 free(subpath);
4212 return err;
4215 static const struct got_error *
4216 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4218 const struct got_error *err = NULL;
4219 char *ct_parent_path = NULL;
4221 *match = 0;
4223 if (strchr(ct->in_repo_path, '/') == NULL) {
4224 *match = got_path_is_root_dir(path);
4225 return NULL;
4228 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4229 if (err)
4230 return err;
4231 *match = (strcmp(path, ct_parent_path) == 0);
4232 free(ct_parent_path);
4233 return err;
4236 static mode_t
4237 get_ct_file_mode(struct got_commitable *ct)
4239 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4242 static const struct got_error *
4243 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4244 struct got_tree_entry *te, struct got_commitable *ct)
4246 const struct got_error *err = NULL;
4248 *new_te = NULL;
4250 err = got_object_tree_entry_dup(new_te, te);
4251 if (err)
4252 goto done;
4254 (*new_te)->mode = get_ct_file_mode(ct);
4256 if (ct->staged_status == GOT_STATUS_MODIFY)
4257 memcpy(&(*new_te)->id, ct->staged_blob_id,
4258 sizeof((*new_te)->id));
4259 else
4260 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4261 done:
4262 if (err && *new_te) {
4263 free(*new_te);
4264 *new_te = NULL;
4266 return err;
4269 static const struct got_error *
4270 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4271 struct got_commitable *ct)
4273 const struct got_error *err = NULL;
4274 char *ct_name;
4276 *new_te = NULL;
4278 *new_te = calloc(1, sizeof(**new_te));
4279 if (*new_te == NULL)
4280 return got_error_from_errno("calloc");
4282 ct_name = basename(ct->path);
4283 if (ct_name == NULL) {
4284 err = got_error_from_errno2("basename", ct->path);
4285 goto done;
4287 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4288 sizeof((*new_te)->name)) {
4289 err = got_error(GOT_ERR_NO_SPACE);
4290 goto done;
4293 (*new_te)->mode = get_ct_file_mode(ct);
4295 if (ct->staged_status == GOT_STATUS_ADD)
4296 memcpy(&(*new_te)->id, ct->staged_blob_id,
4297 sizeof((*new_te)->id));
4298 else
4299 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4300 done:
4301 if (err && *new_te) {
4302 free(*new_te);
4303 *new_te = NULL;
4305 return err;
4308 static const struct got_error *
4309 insert_tree_entry(struct got_tree_entry *new_te,
4310 struct got_pathlist_head *paths)
4312 const struct got_error *err = NULL;
4313 struct got_pathlist_entry *new_pe;
4315 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4316 if (err)
4317 return err;
4318 if (new_pe == NULL)
4319 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4320 return NULL;
4323 static const struct got_error *
4324 report_ct_status(struct got_commitable *ct,
4325 got_worktree_status_cb status_cb, void *status_arg)
4327 const char *ct_path = ct->path;
4328 unsigned char status;
4330 while (ct_path[0] == '/')
4331 ct_path++;
4333 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4334 status = ct->staged_status;
4335 else
4336 status = ct->status;
4338 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4339 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4342 static const struct got_error *
4343 match_modified_subtree(int *modified, struct got_tree_entry *te,
4344 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4346 const struct got_error *err = NULL;
4347 struct got_pathlist_entry *pe;
4348 char *te_path;
4350 *modified = 0;
4352 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4353 got_path_is_root_dir(base_tree_path) ? "" : "/",
4354 te->name) == -1)
4355 return got_error_from_errno("asprintf");
4357 TAILQ_FOREACH(pe, commitable_paths, entry) {
4358 struct got_commitable *ct = pe->data;
4359 *modified = got_path_is_child(ct->in_repo_path, te_path,
4360 strlen(te_path));
4361 if (*modified)
4362 break;
4365 free(te_path);
4366 return err;
4369 static const struct got_error *
4370 match_deleted_or_modified_ct(struct got_commitable **ctp,
4371 struct got_tree_entry *te, const char *base_tree_path,
4372 struct got_pathlist_head *commitable_paths)
4374 const struct got_error *err = NULL;
4375 struct got_pathlist_entry *pe;
4377 *ctp = NULL;
4379 TAILQ_FOREACH(pe, commitable_paths, entry) {
4380 struct got_commitable *ct = pe->data;
4381 char *ct_name = NULL;
4382 int path_matches;
4384 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4385 if (ct->status != GOT_STATUS_MODIFY &&
4386 ct->status != GOT_STATUS_MODE_CHANGE &&
4387 ct->status != GOT_STATUS_DELETE)
4388 continue;
4389 } else {
4390 if (ct->staged_status != GOT_STATUS_MODIFY &&
4391 ct->staged_status != GOT_STATUS_DELETE)
4392 continue;
4395 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4396 continue;
4398 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4399 if (err)
4400 return err;
4401 if (!path_matches)
4402 continue;
4404 ct_name = basename(pe->path);
4405 if (ct_name == NULL)
4406 return got_error_from_errno2("basename", pe->path);
4408 if (strcmp(te->name, ct_name) != 0)
4409 continue;
4411 *ctp = ct;
4412 break;
4415 return err;
4418 static const struct got_error *
4419 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4420 const char *child_path, const char *path_base_tree,
4421 struct got_pathlist_head *commitable_paths,
4422 got_worktree_status_cb status_cb, void *status_arg,
4423 struct got_repository *repo)
4425 const struct got_error *err = NULL;
4426 struct got_tree_entry *new_te;
4427 char *subtree_path;
4428 struct got_object_id *id = NULL;
4429 int nentries;
4431 *new_tep = NULL;
4433 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4434 got_path_is_root_dir(path_base_tree) ? "" : "/",
4435 child_path) == -1)
4436 return got_error_from_errno("asprintf");
4438 new_te = calloc(1, sizeof(*new_te));
4439 if (new_te == NULL)
4440 return got_error_from_errno("calloc");
4441 new_te->mode = S_IFDIR;
4443 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4444 sizeof(new_te->name)) {
4445 err = got_error(GOT_ERR_NO_SPACE);
4446 goto done;
4448 err = write_tree(&id, &nentries, NULL, subtree_path,
4449 commitable_paths, status_cb, status_arg, repo);
4450 if (err) {
4451 free(new_te);
4452 goto done;
4454 memcpy(&new_te->id, id, sizeof(new_te->id));
4455 done:
4456 free(id);
4457 free(subtree_path);
4458 if (err == NULL)
4459 *new_tep = new_te;
4460 return err;
4463 static const struct got_error *
4464 write_tree(struct got_object_id **new_tree_id, int *nentries,
4465 struct got_tree_object *base_tree, const char *path_base_tree,
4466 struct got_pathlist_head *commitable_paths,
4467 got_worktree_status_cb status_cb, void *status_arg,
4468 struct got_repository *repo)
4470 const struct got_error *err = NULL;
4471 struct got_pathlist_head paths;
4472 struct got_tree_entry *te, *new_te = NULL;
4473 struct got_pathlist_entry *pe;
4475 TAILQ_INIT(&paths);
4476 *nentries = 0;
4478 /* Insert, and recurse into, newly added entries first. */
4479 TAILQ_FOREACH(pe, commitable_paths, entry) {
4480 struct got_commitable *ct = pe->data;
4481 char *child_path = NULL, *slash;
4483 if ((ct->status != GOT_STATUS_ADD &&
4484 ct->staged_status != GOT_STATUS_ADD) ||
4485 (ct->flags & GOT_COMMITABLE_ADDED))
4486 continue;
4488 if (!got_path_is_child(pe->path, path_base_tree,
4489 strlen(path_base_tree)))
4490 continue;
4492 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4493 pe->path);
4494 if (err)
4495 goto done;
4497 slash = strchr(child_path, '/');
4498 if (slash == NULL) {
4499 err = alloc_added_blob_tree_entry(&new_te, ct);
4500 if (err)
4501 goto done;
4502 err = report_ct_status(ct, status_cb, status_arg);
4503 if (err)
4504 goto done;
4505 ct->flags |= GOT_COMMITABLE_ADDED;
4506 err = insert_tree_entry(new_te, &paths);
4507 if (err)
4508 goto done;
4509 (*nentries)++;
4510 } else {
4511 *slash = '\0'; /* trim trailing path components */
4512 if (base_tree == NULL ||
4513 got_object_tree_find_entry(base_tree, child_path)
4514 == NULL) {
4515 err = make_subtree_for_added_blob(&new_te,
4516 child_path, path_base_tree,
4517 commitable_paths, status_cb, status_arg,
4518 repo);
4519 if (err)
4520 goto done;
4521 err = insert_tree_entry(new_te, &paths);
4522 if (err)
4523 goto done;
4524 (*nentries)++;
4529 if (base_tree) {
4530 int i, nbase_entries;
4531 /* Handle modified and deleted entries. */
4532 nbase_entries = got_object_tree_get_nentries(base_tree);
4533 for (i = 0; i < nbase_entries; i++) {
4534 struct got_commitable *ct = NULL;
4536 te = got_object_tree_get_entry(base_tree, i);
4537 if (got_object_tree_entry_is_submodule(te)) {
4538 /* Entry is a submodule; just copy it. */
4539 err = got_object_tree_entry_dup(&new_te, te);
4540 if (err)
4541 goto done;
4542 err = insert_tree_entry(new_te, &paths);
4543 if (err)
4544 goto done;
4545 (*nentries)++;
4546 continue;
4549 if (S_ISDIR(te->mode)) {
4550 int modified;
4551 err = got_object_tree_entry_dup(&new_te, te);
4552 if (err)
4553 goto done;
4554 err = match_modified_subtree(&modified, te,
4555 path_base_tree, commitable_paths);
4556 if (err)
4557 goto done;
4558 /* Avoid recursion into unmodified subtrees. */
4559 if (modified) {
4560 struct got_object_id *new_id;
4561 int nsubentries;
4562 err = write_subtree(&new_id,
4563 &nsubentries, te,
4564 path_base_tree, commitable_paths,
4565 status_cb, status_arg, repo);
4566 if (err)
4567 goto done;
4568 if (nsubentries == 0) {
4569 /* All entries were deleted. */
4570 free(new_id);
4571 continue;
4573 memcpy(&new_te->id, new_id,
4574 sizeof(new_te->id));
4575 free(new_id);
4577 err = insert_tree_entry(new_te, &paths);
4578 if (err)
4579 goto done;
4580 (*nentries)++;
4581 continue;
4584 err = match_deleted_or_modified_ct(&ct, te,
4585 path_base_tree, commitable_paths);
4586 if (err)
4587 goto done;
4588 if (ct) {
4589 /* NB: Deleted entries get dropped here. */
4590 if (ct->status == GOT_STATUS_MODIFY ||
4591 ct->status == GOT_STATUS_MODE_CHANGE ||
4592 ct->staged_status == GOT_STATUS_MODIFY) {
4593 err = alloc_modified_blob_tree_entry(
4594 &new_te, te, ct);
4595 if (err)
4596 goto done;
4597 err = insert_tree_entry(new_te, &paths);
4598 if (err)
4599 goto done;
4600 (*nentries)++;
4602 err = report_ct_status(ct, status_cb,
4603 status_arg);
4604 if (err)
4605 goto done;
4606 } else {
4607 /* Entry is unchanged; just copy it. */
4608 err = got_object_tree_entry_dup(&new_te, te);
4609 if (err)
4610 goto done;
4611 err = insert_tree_entry(new_te, &paths);
4612 if (err)
4613 goto done;
4614 (*nentries)++;
4619 /* Write new list of entries; deleted entries have been dropped. */
4620 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4621 done:
4622 got_pathlist_free(&paths);
4623 return err;
4626 static const struct got_error *
4627 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4628 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4629 int have_staged_files)
4631 const struct got_error *err = NULL;
4632 struct got_pathlist_entry *pe;
4634 TAILQ_FOREACH(pe, commitable_paths, entry) {
4635 struct got_fileindex_entry *ie;
4636 struct got_commitable *ct = pe->data;
4638 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4639 if (ie) {
4640 if (ct->status == GOT_STATUS_DELETE ||
4641 ct->staged_status == GOT_STATUS_DELETE) {
4642 got_fileindex_entry_remove(fileindex, ie);
4643 } else if (ct->staged_status == GOT_STATUS_ADD ||
4644 ct->staged_status == GOT_STATUS_MODIFY) {
4645 got_fileindex_entry_stage_set(ie,
4646 GOT_FILEIDX_STAGE_NONE);
4647 err = got_fileindex_entry_update(ie,
4648 ct->ondisk_path, ct->staged_blob_id->sha1,
4649 new_base_commit_id->sha1,
4650 !have_staged_files);
4651 } else
4652 err = got_fileindex_entry_update(ie,
4653 ct->ondisk_path, ct->blob_id->sha1,
4654 new_base_commit_id->sha1,
4655 !have_staged_files);
4656 } else {
4657 err = got_fileindex_entry_alloc(&ie, pe->path);
4658 if (err)
4659 break;
4660 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4661 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4662 if (err) {
4663 got_fileindex_entry_free(ie);
4664 break;
4666 err = got_fileindex_entry_add(fileindex, ie);
4667 if (err) {
4668 got_fileindex_entry_free(ie);
4669 break;
4673 return err;
4677 static const struct got_error *
4678 check_out_of_date(const char *in_repo_path, unsigned char status,
4679 unsigned char staged_status, struct got_object_id *base_blob_id,
4680 struct got_object_id *base_commit_id,
4681 struct got_object_id *head_commit_id, struct got_repository *repo,
4682 int ood_errcode)
4684 const struct got_error *err = NULL;
4685 struct got_object_id *id = NULL;
4687 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4688 /* Trivial case: base commit == head commit */
4689 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4690 return NULL;
4692 * Ensure file content which local changes were based
4693 * on matches file content in the branch head.
4695 err = got_object_id_by_path(&id, repo, head_commit_id,
4696 in_repo_path);
4697 if (err) {
4698 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4699 err = got_error(ood_errcode);
4700 goto done;
4701 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4702 err = got_error(ood_errcode);
4703 } else {
4704 /* Require that added files don't exist in the branch head. */
4705 err = got_object_id_by_path(&id, repo, head_commit_id,
4706 in_repo_path);
4707 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4708 goto done;
4709 err = id ? got_error(ood_errcode) : NULL;
4711 done:
4712 free(id);
4713 return err;
4716 const struct got_error *
4717 commit_worktree(struct got_object_id **new_commit_id,
4718 struct got_pathlist_head *commitable_paths,
4719 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4720 const char *author, const char *committer,
4721 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4722 got_worktree_status_cb status_cb, void *status_arg,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL, *unlockerr = NULL;
4726 struct got_pathlist_entry *pe;
4727 const char *head_ref_name = NULL;
4728 struct got_commit_object *head_commit = NULL;
4729 struct got_reference *head_ref2 = NULL;
4730 struct got_object_id *head_commit_id2 = NULL;
4731 struct got_tree_object *head_tree = NULL;
4732 struct got_object_id *new_tree_id = NULL;
4733 int nentries;
4734 struct got_object_id_queue parent_ids;
4735 struct got_object_qid *pid = NULL;
4736 char *logmsg = NULL;
4738 *new_commit_id = NULL;
4740 SIMPLEQ_INIT(&parent_ids);
4742 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4743 if (err)
4744 goto done;
4746 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4747 if (err)
4748 goto done;
4750 if (commit_msg_cb != NULL) {
4751 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4752 if (err)
4753 goto done;
4756 if (logmsg == NULL || strlen(logmsg) == 0) {
4757 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4758 goto done;
4761 /* Create blobs from added and modified files and record their IDs. */
4762 TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 struct got_commitable *ct = pe->data;
4764 char *ondisk_path;
4766 /* Blobs for staged files already exist. */
4767 if (ct->staged_status == GOT_STATUS_ADD ||
4768 ct->staged_status == GOT_STATUS_MODIFY)
4769 continue;
4771 if (ct->status != GOT_STATUS_ADD &&
4772 ct->status != GOT_STATUS_MODIFY &&
4773 ct->status != GOT_STATUS_MODE_CHANGE)
4774 continue;
4776 if (asprintf(&ondisk_path, "%s/%s",
4777 worktree->root_path, pe->path) == -1) {
4778 err = got_error_from_errno("asprintf");
4779 goto done;
4781 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4782 free(ondisk_path);
4783 if (err)
4784 goto done;
4787 /* Recursively write new tree objects. */
4788 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4789 commitable_paths, status_cb, status_arg, repo);
4790 if (err)
4791 goto done;
4793 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4794 if (err)
4795 goto done;
4796 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4797 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4798 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4799 got_object_qid_free(pid);
4800 if (logmsg != NULL)
4801 free(logmsg);
4802 if (err)
4803 goto done;
4805 /* Check if a concurrent commit to our branch has occurred. */
4806 head_ref_name = got_worktree_get_head_ref_name(worktree);
4807 if (head_ref_name == NULL) {
4808 err = got_error_from_errno("got_worktree_get_head_ref_name");
4809 goto done;
4811 /* Lock the reference here to prevent concurrent modification. */
4812 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4813 if (err)
4814 goto done;
4815 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4816 if (err)
4817 goto done;
4818 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4819 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4820 goto done;
4822 /* Update branch head in repository. */
4823 err = got_ref_change_ref(head_ref2, *new_commit_id);
4824 if (err)
4825 goto done;
4826 err = got_ref_write(head_ref2, repo);
4827 if (err)
4828 goto done;
4830 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4831 if (err)
4832 goto done;
4834 err = ref_base_commit(worktree, repo);
4835 if (err)
4836 goto done;
4837 done:
4838 if (head_tree)
4839 got_object_tree_close(head_tree);
4840 if (head_commit)
4841 got_object_commit_close(head_commit);
4842 free(head_commit_id2);
4843 if (head_ref2) {
4844 unlockerr = got_ref_unlock(head_ref2);
4845 if (unlockerr && err == NULL)
4846 err = unlockerr;
4847 got_ref_close(head_ref2);
4849 return err;
4852 static const struct got_error *
4853 check_path_is_commitable(const char *path,
4854 struct got_pathlist_head *commitable_paths)
4856 struct got_pathlist_entry *cpe = NULL;
4857 size_t path_len = strlen(path);
4859 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4860 struct got_commitable *ct = cpe->data;
4861 const char *ct_path = ct->path;
4863 while (ct_path[0] == '/')
4864 ct_path++;
4866 if (strcmp(path, ct_path) == 0 ||
4867 got_path_is_child(ct_path, path, path_len))
4868 break;
4871 if (cpe == NULL)
4872 return got_error_path(path, GOT_ERR_BAD_PATH);
4874 return NULL;
4877 static const struct got_error *
4878 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4880 int *have_staged_files = arg;
4882 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4883 *have_staged_files = 1;
4884 return got_error(GOT_ERR_CANCELLED);
4887 return NULL;
4890 static const struct got_error *
4891 check_non_staged_files(struct got_fileindex *fileindex,
4892 struct got_pathlist_head *paths)
4894 struct got_pathlist_entry *pe;
4895 struct got_fileindex_entry *ie;
4897 TAILQ_FOREACH(pe, paths, entry) {
4898 if (pe->path[0] == '\0')
4899 continue;
4900 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4901 if (ie == NULL)
4902 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4903 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4904 return got_error_path(pe->path,
4905 GOT_ERR_FILE_NOT_STAGED);
4908 return NULL;
4911 const struct got_error *
4912 got_worktree_commit(struct got_object_id **new_commit_id,
4913 struct got_worktree *worktree, struct got_pathlist_head *paths,
4914 const char *author, const char *committer,
4915 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4916 got_worktree_status_cb status_cb, void *status_arg,
4917 struct got_repository *repo)
4919 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4920 struct got_fileindex *fileindex = NULL;
4921 char *fileindex_path = NULL;
4922 struct got_pathlist_head commitable_paths;
4923 struct collect_commitables_arg cc_arg;
4924 struct got_pathlist_entry *pe;
4925 struct got_reference *head_ref = NULL;
4926 struct got_object_id *head_commit_id = NULL;
4927 int have_staged_files = 0;
4929 *new_commit_id = NULL;
4931 TAILQ_INIT(&commitable_paths);
4933 err = lock_worktree(worktree, LOCK_EX);
4934 if (err)
4935 goto done;
4937 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4938 if (err)
4939 goto done;
4941 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4942 if (err)
4943 goto done;
4945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4946 if (err)
4947 goto done;
4949 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4950 &have_staged_files);
4951 if (err && err->code != GOT_ERR_CANCELLED)
4952 goto done;
4953 if (have_staged_files) {
4954 err = check_non_staged_files(fileindex, paths);
4955 if (err)
4956 goto done;
4959 cc_arg.commitable_paths = &commitable_paths;
4960 cc_arg.worktree = worktree;
4961 cc_arg.repo = repo;
4962 cc_arg.have_staged_files = have_staged_files;
4963 TAILQ_FOREACH(pe, paths, entry) {
4964 err = worktree_status(worktree, pe->path, fileindex, repo,
4965 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4966 if (err)
4967 goto done;
4970 if (TAILQ_EMPTY(&commitable_paths)) {
4971 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4972 goto done;
4975 TAILQ_FOREACH(pe, paths, entry) {
4976 err = check_path_is_commitable(pe->path, &commitable_paths);
4977 if (err)
4978 goto done;
4981 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4982 struct got_commitable *ct = pe->data;
4983 const char *ct_path = ct->in_repo_path;
4985 while (ct_path[0] == '/')
4986 ct_path++;
4987 err = check_out_of_date(ct_path, ct->status,
4988 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4989 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4990 if (err)
4991 goto done;
4995 err = commit_worktree(new_commit_id, &commitable_paths,
4996 head_commit_id, worktree, author, committer,
4997 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4998 if (err)
4999 goto done;
5001 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5002 fileindex, have_staged_files);
5003 sync_err = sync_fileindex(fileindex, fileindex_path);
5004 if (sync_err && err == NULL)
5005 err = sync_err;
5006 done:
5007 if (fileindex)
5008 got_fileindex_free(fileindex);
5009 free(fileindex_path);
5010 unlockerr = lock_worktree(worktree, LOCK_SH);
5011 if (unlockerr && err == NULL)
5012 err = unlockerr;
5013 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5014 struct got_commitable *ct = pe->data;
5015 free_commitable(ct);
5017 got_pathlist_free(&commitable_paths);
5018 return err;
5021 const char *
5022 got_commitable_get_path(struct got_commitable *ct)
5024 return ct->path;
5027 unsigned int
5028 got_commitable_get_status(struct got_commitable *ct)
5030 return ct->status;
5033 struct check_rebase_ok_arg {
5034 struct got_worktree *worktree;
5035 struct got_repository *repo;
5038 static const struct got_error *
5039 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5041 const struct got_error *err = NULL;
5042 struct check_rebase_ok_arg *a = arg;
5043 unsigned char status;
5044 struct stat sb;
5045 char *ondisk_path;
5047 /* Reject rebase of a work tree with mixed base commits. */
5048 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5049 SHA1_DIGEST_LENGTH))
5050 return got_error(GOT_ERR_MIXED_COMMITS);
5052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5053 == -1)
5054 return got_error_from_errno("asprintf");
5056 /* Reject rebase of a work tree with modified or staged files. */
5057 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5058 free(ondisk_path);
5059 if (err)
5060 return err;
5062 if (status != GOT_STATUS_NO_CHANGE)
5063 return got_error(GOT_ERR_MODIFIED);
5064 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5065 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5067 return NULL;
5070 const struct got_error *
5071 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5072 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5073 struct got_worktree *worktree, struct got_reference *branch,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5078 char *branch_ref_name = NULL;
5079 char *fileindex_path = NULL;
5080 struct check_rebase_ok_arg ok_arg;
5081 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5082 struct got_object_id *wt_branch_tip = NULL;
5084 *new_base_branch_ref = NULL;
5085 *tmp_branch = NULL;
5086 *fileindex = NULL;
5088 err = lock_worktree(worktree, LOCK_EX);
5089 if (err)
5090 return err;
5092 err = open_fileindex(fileindex, &fileindex_path, worktree);
5093 if (err)
5094 goto done;
5096 ok_arg.worktree = worktree;
5097 ok_arg.repo = repo;
5098 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5099 &ok_arg);
5100 if (err)
5101 goto done;
5103 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5104 if (err)
5105 goto done;
5107 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5108 if (err)
5109 goto done;
5111 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5112 if (err)
5113 goto done;
5115 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5116 0);
5117 if (err)
5118 goto done;
5120 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5121 if (err)
5122 goto done;
5123 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5124 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5125 goto done;
5128 err = got_ref_alloc_symref(new_base_branch_ref,
5129 new_base_branch_ref_name, wt_branch);
5130 if (err)
5131 goto done;
5132 err = got_ref_write(*new_base_branch_ref, repo);
5133 if (err)
5134 goto done;
5136 /* TODO Lock original branch's ref while rebasing? */
5138 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5139 if (err)
5140 goto done;
5142 err = got_ref_write(branch_ref, repo);
5143 if (err)
5144 goto done;
5146 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5147 worktree->base_commit_id);
5148 if (err)
5149 goto done;
5150 err = got_ref_write(*tmp_branch, repo);
5151 if (err)
5152 goto done;
5154 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5155 if (err)
5156 goto done;
5157 done:
5158 free(fileindex_path);
5159 free(tmp_branch_name);
5160 free(new_base_branch_ref_name);
5161 free(branch_ref_name);
5162 if (branch_ref)
5163 got_ref_close(branch_ref);
5164 if (wt_branch)
5165 got_ref_close(wt_branch);
5166 free(wt_branch_tip);
5167 if (err) {
5168 if (*new_base_branch_ref) {
5169 got_ref_close(*new_base_branch_ref);
5170 *new_base_branch_ref = NULL;
5172 if (*tmp_branch) {
5173 got_ref_close(*tmp_branch);
5174 *tmp_branch = NULL;
5176 if (*fileindex) {
5177 got_fileindex_free(*fileindex);
5178 *fileindex = NULL;
5180 lock_worktree(worktree, LOCK_SH);
5182 return err;
5185 const struct got_error *
5186 got_worktree_rebase_continue(struct got_object_id **commit_id,
5187 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5188 struct got_reference **branch, struct got_fileindex **fileindex,
5189 struct got_worktree *worktree, struct got_repository *repo)
5191 const struct got_error *err;
5192 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5193 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5194 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5195 char *fileindex_path = NULL;
5196 int have_staged_files = 0;
5198 *commit_id = NULL;
5199 *new_base_branch = NULL;
5200 *tmp_branch = NULL;
5201 *branch = NULL;
5202 *fileindex = NULL;
5204 err = lock_worktree(worktree, LOCK_EX);
5205 if (err)
5206 return err;
5208 err = open_fileindex(fileindex, &fileindex_path, worktree);
5209 if (err)
5210 goto done;
5212 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5213 &have_staged_files);
5214 if (err && err->code != GOT_ERR_CANCELLED)
5215 goto done;
5216 if (have_staged_files) {
5217 err = got_error(GOT_ERR_STAGED_PATHS);
5218 goto done;
5221 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5222 if (err)
5223 goto done;
5225 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5226 if (err)
5227 goto done;
5229 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5230 if (err)
5231 goto done;
5233 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5234 if (err)
5235 goto done;
5237 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5238 if (err)
5239 goto done;
5241 err = got_ref_open(branch, repo,
5242 got_ref_get_symref_target(branch_ref), 0);
5243 if (err)
5244 goto done;
5246 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5247 if (err)
5248 goto done;
5250 err = got_ref_resolve(commit_id, repo, commit_ref);
5251 if (err)
5252 goto done;
5254 err = got_ref_open(new_base_branch, repo,
5255 new_base_branch_ref_name, 0);
5256 if (err)
5257 goto done;
5259 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5260 if (err)
5261 goto done;
5262 done:
5263 free(commit_ref_name);
5264 free(branch_ref_name);
5265 free(fileindex_path);
5266 if (commit_ref)
5267 got_ref_close(commit_ref);
5268 if (branch_ref)
5269 got_ref_close(branch_ref);
5270 if (err) {
5271 free(*commit_id);
5272 *commit_id = NULL;
5273 if (*tmp_branch) {
5274 got_ref_close(*tmp_branch);
5275 *tmp_branch = NULL;
5277 if (*new_base_branch) {
5278 got_ref_close(*new_base_branch);
5279 *new_base_branch = NULL;
5281 if (*branch) {
5282 got_ref_close(*branch);
5283 *branch = NULL;
5285 if (*fileindex) {
5286 got_fileindex_free(*fileindex);
5287 *fileindex = NULL;
5289 lock_worktree(worktree, LOCK_SH);
5291 return err;
5294 const struct got_error *
5295 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5297 const struct got_error *err;
5298 char *tmp_branch_name = NULL;
5300 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5301 if (err)
5302 return err;
5304 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5305 free(tmp_branch_name);
5306 return NULL;
5309 static const struct got_error *
5310 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5311 char **logmsg, void *arg)
5313 *logmsg = arg;
5314 return NULL;
5317 static const struct got_error *
5318 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5319 const char *path, struct got_object_id *blob_id,
5320 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5321 int dirfd, const char *de_name)
5323 return NULL;
5326 struct collect_merged_paths_arg {
5327 got_worktree_checkout_cb progress_cb;
5328 void *progress_arg;
5329 struct got_pathlist_head *merged_paths;
5332 static const struct got_error *
5333 collect_merged_paths(void *arg, unsigned char status, const char *path)
5335 const struct got_error *err;
5336 struct collect_merged_paths_arg *a = arg;
5337 char *p;
5338 struct got_pathlist_entry *new;
5340 err = (*a->progress_cb)(a->progress_arg, status, path);
5341 if (err)
5342 return err;
5344 if (status != GOT_STATUS_MERGE &&
5345 status != GOT_STATUS_ADD &&
5346 status != GOT_STATUS_DELETE &&
5347 status != GOT_STATUS_CONFLICT)
5348 return NULL;
5350 p = strdup(path);
5351 if (p == NULL)
5352 return got_error_from_errno("strdup");
5354 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5355 if (err || new == NULL)
5356 free(p);
5357 return err;
5360 void
5361 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5363 struct got_pathlist_entry *pe;
5365 TAILQ_FOREACH(pe, merged_paths, entry)
5366 free((char *)pe->path);
5368 got_pathlist_free(merged_paths);
5371 static const struct got_error *
5372 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5373 int is_rebase, struct got_repository *repo)
5375 const struct got_error *err;
5376 struct got_reference *commit_ref = NULL;
5378 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5379 if (err) {
5380 if (err->code != GOT_ERR_NOT_REF)
5381 goto done;
5382 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5383 if (err)
5384 goto done;
5385 err = got_ref_write(commit_ref, repo);
5386 if (err)
5387 goto done;
5388 } else if (is_rebase) {
5389 struct got_object_id *stored_id;
5390 int cmp;
5392 err = got_ref_resolve(&stored_id, repo, commit_ref);
5393 if (err)
5394 goto done;
5395 cmp = got_object_id_cmp(commit_id, stored_id);
5396 free(stored_id);
5397 if (cmp != 0) {
5398 err = got_error(GOT_ERR_REBASE_COMMITID);
5399 goto done;
5402 done:
5403 if (commit_ref)
5404 got_ref_close(commit_ref);
5405 return err;
5408 static const struct got_error *
5409 rebase_merge_files(struct got_pathlist_head *merged_paths,
5410 const char *commit_ref_name, struct got_worktree *worktree,
5411 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5412 struct got_object_id *commit_id, struct got_repository *repo,
5413 got_worktree_checkout_cb progress_cb, void *progress_arg,
5414 got_cancel_cb cancel_cb, void *cancel_arg)
5416 const struct got_error *err;
5417 struct got_reference *commit_ref = NULL;
5418 struct collect_merged_paths_arg cmp_arg;
5419 char *fileindex_path;
5421 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5423 err = get_fileindex_path(&fileindex_path, worktree);
5424 if (err)
5425 return err;
5427 cmp_arg.progress_cb = progress_cb;
5428 cmp_arg.progress_arg = progress_arg;
5429 cmp_arg.merged_paths = merged_paths;
5430 err = merge_files(worktree, fileindex, fileindex_path,
5431 parent_commit_id, commit_id, repo, collect_merged_paths,
5432 &cmp_arg, cancel_cb, cancel_arg);
5433 if (commit_ref)
5434 got_ref_close(commit_ref);
5435 return err;
5438 const struct got_error *
5439 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5440 struct got_worktree *worktree, struct got_fileindex *fileindex,
5441 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5442 struct got_repository *repo,
5443 got_worktree_checkout_cb progress_cb, void *progress_arg,
5444 got_cancel_cb cancel_cb, void *cancel_arg)
5446 const struct got_error *err;
5447 char *commit_ref_name;
5449 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5450 if (err)
5451 return err;
5453 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5454 if (err)
5455 goto done;
5457 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5458 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5459 progress_arg, cancel_cb, cancel_arg);
5460 done:
5461 free(commit_ref_name);
5462 return err;
5465 const struct got_error *
5466 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5467 struct got_worktree *worktree, struct got_fileindex *fileindex,
5468 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5469 struct got_repository *repo,
5470 got_worktree_checkout_cb progress_cb, void *progress_arg,
5471 got_cancel_cb cancel_cb, void *cancel_arg)
5473 const struct got_error *err;
5474 char *commit_ref_name;
5476 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5477 if (err)
5478 return err;
5480 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5481 if (err)
5482 goto done;
5484 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5485 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5486 progress_arg, cancel_cb, cancel_arg);
5487 done:
5488 free(commit_ref_name);
5489 return err;
5492 static const struct got_error *
5493 rebase_commit(struct got_object_id **new_commit_id,
5494 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5495 struct got_worktree *worktree, struct got_fileindex *fileindex,
5496 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5497 const char *new_logmsg, struct got_repository *repo)
5499 const struct got_error *err, *sync_err;
5500 struct got_pathlist_head commitable_paths;
5501 struct collect_commitables_arg cc_arg;
5502 char *fileindex_path = NULL;
5503 struct got_reference *head_ref = NULL;
5504 struct got_object_id *head_commit_id = NULL;
5505 char *logmsg = NULL;
5507 TAILQ_INIT(&commitable_paths);
5508 *new_commit_id = NULL;
5510 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5512 err = get_fileindex_path(&fileindex_path, worktree);
5513 if (err)
5514 return err;
5516 cc_arg.commitable_paths = &commitable_paths;
5517 cc_arg.worktree = worktree;
5518 cc_arg.repo = repo;
5519 cc_arg.have_staged_files = 0;
5521 * If possible get the status of individual files directly to
5522 * avoid crawling the entire work tree once per rebased commit.
5523 * TODO: Ideally, merged_paths would contain a list of commitables
5524 * we could use so we could skip worktree_status() entirely.
5526 if (merged_paths) {
5527 struct got_pathlist_entry *pe;
5528 TAILQ_FOREACH(pe, merged_paths, entry) {
5529 err = worktree_status(worktree, pe->path, fileindex,
5530 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5531 0);
5532 if (err)
5533 goto done;
5535 } else {
5536 err = worktree_status(worktree, "", fileindex, repo,
5537 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5538 if (err)
5539 goto done;
5542 if (TAILQ_EMPTY(&commitable_paths)) {
5543 /* No-op change; commit will be elided. */
5544 err = got_ref_delete(commit_ref, repo);
5545 if (err)
5546 goto done;
5547 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5548 goto done;
5551 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5552 if (err)
5553 goto done;
5555 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5556 if (err)
5557 goto done;
5559 if (new_logmsg) {
5560 logmsg = strdup(new_logmsg);
5561 if (logmsg == NULL) {
5562 err = got_error_from_errno("strdup");
5563 goto done;
5565 } else {
5566 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5567 if (err)
5568 goto done;
5571 /* NB: commit_worktree will call free(logmsg) */
5572 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5573 worktree, got_object_commit_get_author(orig_commit),
5574 got_object_commit_get_committer(orig_commit),
5575 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5576 if (err)
5577 goto done;
5579 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5580 if (err)
5581 goto done;
5583 err = got_ref_delete(commit_ref, repo);
5584 if (err)
5585 goto done;
5587 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5588 fileindex, 0);
5589 sync_err = sync_fileindex(fileindex, fileindex_path);
5590 if (sync_err && err == NULL)
5591 err = sync_err;
5592 done:
5593 free(fileindex_path);
5594 free(head_commit_id);
5595 if (head_ref)
5596 got_ref_close(head_ref);
5597 if (err) {
5598 free(*new_commit_id);
5599 *new_commit_id = NULL;
5601 return err;
5604 const struct got_error *
5605 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5606 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5607 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5608 struct got_commit_object *orig_commit,
5609 struct got_object_id *orig_commit_id, struct got_repository *repo)
5611 const struct got_error *err;
5612 char *commit_ref_name;
5613 struct got_reference *commit_ref = NULL;
5614 struct got_object_id *commit_id = NULL;
5616 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5617 if (err)
5618 return err;
5620 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5621 if (err)
5622 goto done;
5623 err = got_ref_resolve(&commit_id, repo, commit_ref);
5624 if (err)
5625 goto done;
5626 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5627 err = got_error(GOT_ERR_REBASE_COMMITID);
5628 goto done;
5631 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5632 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5633 done:
5634 if (commit_ref)
5635 got_ref_close(commit_ref);
5636 free(commit_ref_name);
5637 free(commit_id);
5638 return err;
5641 const struct got_error *
5642 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5643 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5644 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5645 struct got_commit_object *orig_commit,
5646 struct got_object_id *orig_commit_id, const char *new_logmsg,
5647 struct got_repository *repo)
5649 const struct got_error *err;
5650 char *commit_ref_name;
5651 struct got_reference *commit_ref = NULL;
5653 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5654 if (err)
5655 return err;
5657 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5658 if (err)
5659 goto done;
5661 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5662 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5663 done:
5664 if (commit_ref)
5665 got_ref_close(commit_ref);
5666 free(commit_ref_name);
5667 return err;
5670 const struct got_error *
5671 got_worktree_rebase_postpone(struct got_worktree *worktree,
5672 struct got_fileindex *fileindex)
5674 if (fileindex)
5675 got_fileindex_free(fileindex);
5676 return lock_worktree(worktree, LOCK_SH);
5679 static const struct got_error *
5680 delete_ref(const char *name, struct got_repository *repo)
5682 const struct got_error *err;
5683 struct got_reference *ref;
5685 err = got_ref_open(&ref, repo, name, 0);
5686 if (err) {
5687 if (err->code == GOT_ERR_NOT_REF)
5688 return NULL;
5689 return err;
5692 err = got_ref_delete(ref, repo);
5693 got_ref_close(ref);
5694 return err;
5697 static const struct got_error *
5698 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5700 const struct got_error *err;
5701 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5702 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5704 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5705 if (err)
5706 goto done;
5707 err = delete_ref(tmp_branch_name, repo);
5708 if (err)
5709 goto done;
5711 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5712 if (err)
5713 goto done;
5714 err = delete_ref(new_base_branch_ref_name, repo);
5715 if (err)
5716 goto done;
5718 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5719 if (err)
5720 goto done;
5721 err = delete_ref(branch_ref_name, repo);
5722 if (err)
5723 goto done;
5725 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5726 if (err)
5727 goto done;
5728 err = delete_ref(commit_ref_name, repo);
5729 if (err)
5730 goto done;
5732 done:
5733 free(tmp_branch_name);
5734 free(new_base_branch_ref_name);
5735 free(branch_ref_name);
5736 free(commit_ref_name);
5737 return err;
5740 const struct got_error *
5741 got_worktree_rebase_complete(struct got_worktree *worktree,
5742 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5743 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5744 struct got_repository *repo)
5746 const struct got_error *err, *unlockerr;
5747 struct got_object_id *new_head_commit_id = NULL;
5749 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5750 if (err)
5751 return err;
5753 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5754 if (err)
5755 goto done;
5757 err = got_ref_write(rebased_branch, repo);
5758 if (err)
5759 goto done;
5761 err = got_worktree_set_head_ref(worktree, rebased_branch);
5762 if (err)
5763 goto done;
5765 err = delete_rebase_refs(worktree, repo);
5766 done:
5767 if (fileindex)
5768 got_fileindex_free(fileindex);
5769 free(new_head_commit_id);
5770 unlockerr = lock_worktree(worktree, LOCK_SH);
5771 if (unlockerr && err == NULL)
5772 err = unlockerr;
5773 return err;
5776 const struct got_error *
5777 got_worktree_rebase_abort(struct got_worktree *worktree,
5778 struct got_fileindex *fileindex, struct got_repository *repo,
5779 struct got_reference *new_base_branch,
5780 got_worktree_checkout_cb progress_cb, void *progress_arg)
5782 const struct got_error *err, *unlockerr, *sync_err;
5783 struct got_reference *resolved = NULL;
5784 struct got_object_id *commit_id = NULL;
5785 char *fileindex_path = NULL;
5786 struct revert_file_args rfa;
5787 struct got_object_id *tree_id = NULL;
5789 err = lock_worktree(worktree, LOCK_EX);
5790 if (err)
5791 return err;
5793 err = got_ref_open(&resolved, repo,
5794 got_ref_get_symref_target(new_base_branch), 0);
5795 if (err)
5796 goto done;
5798 err = got_worktree_set_head_ref(worktree, resolved);
5799 if (err)
5800 goto done;
5803 * XXX commits to the base branch could have happened while
5804 * we were busy rebasing; should we store the original commit ID
5805 * when rebase begins and read it back here?
5807 err = got_ref_resolve(&commit_id, repo, resolved);
5808 if (err)
5809 goto done;
5811 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5812 if (err)
5813 goto done;
5815 err = got_object_id_by_path(&tree_id, repo,
5816 worktree->base_commit_id, worktree->path_prefix);
5817 if (err)
5818 goto done;
5820 err = delete_rebase_refs(worktree, repo);
5821 if (err)
5822 goto done;
5824 err = get_fileindex_path(&fileindex_path, worktree);
5825 if (err)
5826 goto done;
5828 rfa.worktree = worktree;
5829 rfa.fileindex = fileindex;
5830 rfa.progress_cb = progress_cb;
5831 rfa.progress_arg = progress_arg;
5832 rfa.patch_cb = NULL;
5833 rfa.patch_arg = NULL;
5834 rfa.repo = repo;
5835 err = worktree_status(worktree, "", fileindex, repo,
5836 revert_file, &rfa, NULL, NULL, 0, 0);
5837 if (err)
5838 goto sync;
5840 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5841 repo, progress_cb, progress_arg, NULL, NULL);
5842 sync:
5843 sync_err = sync_fileindex(fileindex, fileindex_path);
5844 if (sync_err && err == NULL)
5845 err = sync_err;
5846 done:
5847 got_ref_close(resolved);
5848 free(tree_id);
5849 free(commit_id);
5850 if (fileindex)
5851 got_fileindex_free(fileindex);
5852 free(fileindex_path);
5854 unlockerr = lock_worktree(worktree, LOCK_SH);
5855 if (unlockerr && err == NULL)
5856 err = unlockerr;
5857 return err;
5860 const struct got_error *
5861 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5862 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5863 struct got_fileindex **fileindex, struct got_worktree *worktree,
5864 struct got_repository *repo)
5866 const struct got_error *err = NULL;
5867 char *tmp_branch_name = NULL;
5868 char *branch_ref_name = NULL;
5869 char *base_commit_ref_name = NULL;
5870 char *fileindex_path = NULL;
5871 struct check_rebase_ok_arg ok_arg;
5872 struct got_reference *wt_branch = NULL;
5873 struct got_reference *base_commit_ref = NULL;
5875 *tmp_branch = NULL;
5876 *branch_ref = NULL;
5877 *base_commit_id = NULL;
5878 *fileindex = NULL;
5880 err = lock_worktree(worktree, LOCK_EX);
5881 if (err)
5882 return err;
5884 err = open_fileindex(fileindex, &fileindex_path, worktree);
5885 if (err)
5886 goto done;
5888 ok_arg.worktree = worktree;
5889 ok_arg.repo = repo;
5890 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5891 &ok_arg);
5892 if (err)
5893 goto done;
5895 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5896 if (err)
5897 goto done;
5899 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5900 if (err)
5901 goto done;
5903 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5904 worktree);
5905 if (err)
5906 goto done;
5908 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5909 0);
5910 if (err)
5911 goto done;
5913 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5914 if (err)
5915 goto done;
5917 err = got_ref_write(*branch_ref, repo);
5918 if (err)
5919 goto done;
5921 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5922 worktree->base_commit_id);
5923 if (err)
5924 goto done;
5925 err = got_ref_write(base_commit_ref, repo);
5926 if (err)
5927 goto done;
5928 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5929 if (*base_commit_id == NULL) {
5930 err = got_error_from_errno("got_object_id_dup");
5931 goto done;
5934 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5935 worktree->base_commit_id);
5936 if (err)
5937 goto done;
5938 err = got_ref_write(*tmp_branch, repo);
5939 if (err)
5940 goto done;
5942 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5943 if (err)
5944 goto done;
5945 done:
5946 free(fileindex_path);
5947 free(tmp_branch_name);
5948 free(branch_ref_name);
5949 free(base_commit_ref_name);
5950 if (wt_branch)
5951 got_ref_close(wt_branch);
5952 if (err) {
5953 if (*branch_ref) {
5954 got_ref_close(*branch_ref);
5955 *branch_ref = NULL;
5957 if (*tmp_branch) {
5958 got_ref_close(*tmp_branch);
5959 *tmp_branch = NULL;
5961 free(*base_commit_id);
5962 if (*fileindex) {
5963 got_fileindex_free(*fileindex);
5964 *fileindex = NULL;
5966 lock_worktree(worktree, LOCK_SH);
5968 return err;
5971 const struct got_error *
5972 got_worktree_histedit_postpone(struct got_worktree *worktree,
5973 struct got_fileindex *fileindex)
5975 if (fileindex)
5976 got_fileindex_free(fileindex);
5977 return lock_worktree(worktree, LOCK_SH);
5980 const struct got_error *
5981 got_worktree_histedit_in_progress(int *in_progress,
5982 struct got_worktree *worktree)
5984 const struct got_error *err;
5985 char *tmp_branch_name = NULL;
5987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5988 if (err)
5989 return err;
5991 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5992 free(tmp_branch_name);
5993 return NULL;
5996 const struct got_error *
5997 got_worktree_histedit_continue(struct got_object_id **commit_id,
5998 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5999 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_repository *repo)
6002 const struct got_error *err;
6003 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 struct got_reference *commit_ref = NULL;
6006 struct got_reference *base_commit_ref = NULL;
6007 char *fileindex_path = NULL;
6008 int have_staged_files = 0;
6010 *commit_id = NULL;
6011 *tmp_branch = NULL;
6012 *base_commit_id = NULL;
6013 *fileindex = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6016 if (err)
6017 return err;
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6020 if (err)
6021 goto done;
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 &have_staged_files);
6025 if (err && err->code != GOT_ERR_CANCELLED)
6026 goto done;
6027 if (have_staged_files) {
6028 err = got_error(GOT_ERR_STAGED_PATHS);
6029 goto done;
6032 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6033 if (err)
6034 goto done;
6036 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6041 if (err)
6042 goto done;
6044 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6045 worktree);
6046 if (err)
6047 goto done;
6049 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6050 if (err)
6051 goto done;
6053 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6054 if (err)
6055 goto done;
6056 err = got_ref_resolve(commit_id, repo, commit_ref);
6057 if (err)
6058 goto done;
6060 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6061 if (err)
6062 goto done;
6063 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6064 if (err)
6065 goto done;
6067 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6068 if (err)
6069 goto done;
6070 done:
6071 free(commit_ref_name);
6072 free(branch_ref_name);
6073 free(fileindex_path);
6074 if (commit_ref)
6075 got_ref_close(commit_ref);
6076 if (base_commit_ref)
6077 got_ref_close(base_commit_ref);
6078 if (err) {
6079 free(*commit_id);
6080 *commit_id = NULL;
6081 free(*base_commit_id);
6082 *base_commit_id = NULL;
6083 if (*tmp_branch) {
6084 got_ref_close(*tmp_branch);
6085 *tmp_branch = NULL;
6087 if (*fileindex) {
6088 got_fileindex_free(*fileindex);
6089 *fileindex = NULL;
6091 lock_worktree(worktree, LOCK_EX);
6093 return err;
6096 static const struct got_error *
6097 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6099 const struct got_error *err;
6100 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6101 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6103 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6104 if (err)
6105 goto done;
6106 err = delete_ref(tmp_branch_name, repo);
6107 if (err)
6108 goto done;
6110 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6111 worktree);
6112 if (err)
6113 goto done;
6114 err = delete_ref(base_commit_ref_name, repo);
6115 if (err)
6116 goto done;
6118 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6119 if (err)
6120 goto done;
6121 err = delete_ref(branch_ref_name, repo);
6122 if (err)
6123 goto done;
6125 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6126 if (err)
6127 goto done;
6128 err = delete_ref(commit_ref_name, repo);
6129 if (err)
6130 goto done;
6131 done:
6132 free(tmp_branch_name);
6133 free(base_commit_ref_name);
6134 free(branch_ref_name);
6135 free(commit_ref_name);
6136 return err;
6139 const struct got_error *
6140 got_worktree_histedit_abort(struct got_worktree *worktree,
6141 struct got_fileindex *fileindex, struct got_repository *repo,
6142 struct got_reference *branch, struct got_object_id *base_commit_id,
6143 got_worktree_checkout_cb progress_cb, void *progress_arg)
6145 const struct got_error *err, *unlockerr, *sync_err;
6146 struct got_reference *resolved = NULL;
6147 char *fileindex_path = NULL;
6148 struct got_object_id *tree_id = NULL;
6149 struct revert_file_args rfa;
6151 err = lock_worktree(worktree, LOCK_EX);
6152 if (err)
6153 return err;
6155 err = got_ref_open(&resolved, repo,
6156 got_ref_get_symref_target(branch), 0);
6157 if (err)
6158 goto done;
6160 err = got_worktree_set_head_ref(worktree, resolved);
6161 if (err)
6162 goto done;
6164 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6165 if (err)
6166 goto done;
6168 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6169 worktree->path_prefix);
6170 if (err)
6171 goto done;
6173 err = delete_histedit_refs(worktree, repo);
6174 if (err)
6175 goto done;
6177 err = get_fileindex_path(&fileindex_path, worktree);
6178 if (err)
6179 goto done;
6181 rfa.worktree = worktree;
6182 rfa.fileindex = fileindex;
6183 rfa.progress_cb = progress_cb;
6184 rfa.progress_arg = progress_arg;
6185 rfa.patch_cb = NULL;
6186 rfa.patch_arg = NULL;
6187 rfa.repo = repo;
6188 err = worktree_status(worktree, "", fileindex, repo,
6189 revert_file, &rfa, NULL, NULL, 0, 0);
6190 if (err)
6191 goto sync;
6193 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6194 repo, progress_cb, progress_arg, NULL, NULL);
6195 sync:
6196 sync_err = sync_fileindex(fileindex, fileindex_path);
6197 if (sync_err && err == NULL)
6198 err = sync_err;
6199 done:
6200 got_ref_close(resolved);
6201 free(tree_id);
6202 free(fileindex_path);
6204 unlockerr = lock_worktree(worktree, LOCK_SH);
6205 if (unlockerr && err == NULL)
6206 err = unlockerr;
6207 return err;
6210 const struct got_error *
6211 got_worktree_histedit_complete(struct got_worktree *worktree,
6212 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6213 struct got_reference *edited_branch, struct got_repository *repo)
6215 const struct got_error *err, *unlockerr;
6216 struct got_object_id *new_head_commit_id = NULL;
6217 struct got_reference *resolved = NULL;
6219 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6220 if (err)
6221 return err;
6223 err = got_ref_open(&resolved, repo,
6224 got_ref_get_symref_target(edited_branch), 0);
6225 if (err)
6226 goto done;
6228 err = got_ref_change_ref(resolved, new_head_commit_id);
6229 if (err)
6230 goto done;
6232 err = got_ref_write(resolved, repo);
6233 if (err)
6234 goto done;
6236 err = got_worktree_set_head_ref(worktree, resolved);
6237 if (err)
6238 goto done;
6240 err = delete_histedit_refs(worktree, repo);
6241 done:
6242 if (fileindex)
6243 got_fileindex_free(fileindex);
6244 free(new_head_commit_id);
6245 unlockerr = lock_worktree(worktree, LOCK_SH);
6246 if (unlockerr && err == NULL)
6247 err = unlockerr;
6248 return err;
6251 const struct got_error *
6252 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6253 struct got_object_id *commit_id, struct got_repository *repo)
6255 const struct got_error *err;
6256 char *commit_ref_name;
6258 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6259 if (err)
6260 return err;
6262 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6263 if (err)
6264 goto done;
6266 err = delete_ref(commit_ref_name, repo);
6267 done:
6268 free(commit_ref_name);
6269 return err;
6272 const struct got_error *
6273 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6274 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6275 struct got_worktree *worktree, const char *refname,
6276 struct got_repository *repo)
6278 const struct got_error *err = NULL;
6279 char *fileindex_path = NULL;
6280 struct check_rebase_ok_arg ok_arg;
6282 *fileindex = NULL;
6283 *branch_ref = NULL;
6284 *base_branch_ref = NULL;
6286 err = lock_worktree(worktree, LOCK_EX);
6287 if (err)
6288 return err;
6290 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6291 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6292 "cannot integrate a branch into itself; "
6293 "update -b or different branch name required");
6294 goto done;
6297 err = open_fileindex(fileindex, &fileindex_path, worktree);
6298 if (err)
6299 goto done;
6301 /* Preconditions are the same as for rebase. */
6302 ok_arg.worktree = worktree;
6303 ok_arg.repo = repo;
6304 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6305 &ok_arg);
6306 if (err)
6307 goto done;
6309 err = got_ref_open(branch_ref, repo, refname, 1);
6310 if (err)
6311 goto done;
6313 err = got_ref_open(base_branch_ref, repo,
6314 got_worktree_get_head_ref_name(worktree), 1);
6315 done:
6316 if (err) {
6317 if (*branch_ref) {
6318 got_ref_close(*branch_ref);
6319 *branch_ref = NULL;
6321 if (*base_branch_ref) {
6322 got_ref_close(*base_branch_ref);
6323 *base_branch_ref = NULL;
6325 if (*fileindex) {
6326 got_fileindex_free(*fileindex);
6327 *fileindex = NULL;
6329 lock_worktree(worktree, LOCK_SH);
6331 return err;
6334 const struct got_error *
6335 got_worktree_integrate_continue(struct got_worktree *worktree,
6336 struct got_fileindex *fileindex, struct got_repository *repo,
6337 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6338 got_worktree_checkout_cb progress_cb, void *progress_arg,
6339 got_cancel_cb cancel_cb, void *cancel_arg)
6341 const struct got_error *err = NULL, *sync_err, *unlockerr;
6342 char *fileindex_path = NULL;
6343 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6345 err = get_fileindex_path(&fileindex_path, worktree);
6346 if (err)
6347 goto done;
6349 err = got_ref_resolve(&commit_id, repo, branch_ref);
6350 if (err)
6351 goto done;
6353 err = got_object_id_by_path(&tree_id, repo, commit_id,
6354 worktree->path_prefix);
6355 if (err)
6356 goto done;
6358 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6359 if (err)
6360 goto done;
6362 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6363 progress_cb, progress_arg, cancel_cb, cancel_arg);
6364 if (err)
6365 goto sync;
6367 err = got_ref_change_ref(base_branch_ref, commit_id);
6368 if (err)
6369 goto sync;
6371 err = got_ref_write(base_branch_ref, repo);
6372 sync:
6373 sync_err = sync_fileindex(fileindex, fileindex_path);
6374 if (sync_err && err == NULL)
6375 err = sync_err;
6377 done:
6378 unlockerr = got_ref_unlock(branch_ref);
6379 if (unlockerr && err == NULL)
6380 err = unlockerr;
6381 got_ref_close(branch_ref);
6383 unlockerr = got_ref_unlock(base_branch_ref);
6384 if (unlockerr && err == NULL)
6385 err = unlockerr;
6386 got_ref_close(base_branch_ref);
6388 got_fileindex_free(fileindex);
6389 free(fileindex_path);
6390 free(tree_id);
6392 unlockerr = lock_worktree(worktree, LOCK_SH);
6393 if (unlockerr && err == NULL)
6394 err = unlockerr;
6395 return err;
6398 const struct got_error *
6399 got_worktree_integrate_abort(struct got_worktree *worktree,
6400 struct got_fileindex *fileindex, struct got_repository *repo,
6401 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6403 const struct got_error *err = NULL, *unlockerr = NULL;
6405 got_fileindex_free(fileindex);
6407 err = lock_worktree(worktree, LOCK_SH);
6409 unlockerr = got_ref_unlock(branch_ref);
6410 if (unlockerr && err == NULL)
6411 err = unlockerr;
6412 got_ref_close(branch_ref);
6414 unlockerr = got_ref_unlock(base_branch_ref);
6415 if (unlockerr && err == NULL)
6416 err = unlockerr;
6417 got_ref_close(base_branch_ref);
6419 return err;
6422 struct check_stage_ok_arg {
6423 struct got_object_id *head_commit_id;
6424 struct got_worktree *worktree;
6425 struct got_fileindex *fileindex;
6426 struct got_repository *repo;
6427 int have_changes;
6430 const struct got_error *
6431 check_stage_ok(void *arg, unsigned char status,
6432 unsigned char staged_status, const char *relpath,
6433 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6434 struct got_object_id *commit_id, int dirfd, const char *de_name)
6436 struct check_stage_ok_arg *a = arg;
6437 const struct got_error *err = NULL;
6438 struct got_fileindex_entry *ie;
6439 struct got_object_id base_commit_id;
6440 struct got_object_id *base_commit_idp = NULL;
6441 char *in_repo_path = NULL, *p;
6443 if (status == GOT_STATUS_UNVERSIONED ||
6444 status == GOT_STATUS_NO_CHANGE)
6445 return NULL;
6446 if (status == GOT_STATUS_NONEXISTENT)
6447 return got_error_set_errno(ENOENT, relpath);
6449 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6450 if (ie == NULL)
6451 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6453 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6454 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6455 relpath) == -1)
6456 return got_error_from_errno("asprintf");
6458 if (got_fileindex_entry_has_commit(ie)) {
6459 memcpy(base_commit_id.sha1, ie->commit_sha1,
6460 SHA1_DIGEST_LENGTH);
6461 base_commit_idp = &base_commit_id;
6464 if (status == GOT_STATUS_CONFLICT) {
6465 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6466 goto done;
6467 } else if (status != GOT_STATUS_ADD &&
6468 status != GOT_STATUS_MODIFY &&
6469 status != GOT_STATUS_DELETE) {
6470 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6471 goto done;
6474 a->have_changes = 1;
6476 p = in_repo_path;
6477 while (p[0] == '/')
6478 p++;
6479 err = check_out_of_date(p, status, staged_status,
6480 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6481 GOT_ERR_STAGE_OUT_OF_DATE);
6482 done:
6483 free(in_repo_path);
6484 return err;
6487 struct stage_path_arg {
6488 struct got_worktree *worktree;
6489 struct got_fileindex *fileindex;
6490 struct got_repository *repo;
6491 got_worktree_status_cb status_cb;
6492 void *status_arg;
6493 got_worktree_patch_cb patch_cb;
6494 void *patch_arg;
6495 int staged_something;
6498 static const struct got_error *
6499 stage_path(void *arg, unsigned char status,
6500 unsigned char staged_status, const char *relpath,
6501 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6502 struct got_object_id *commit_id, int dirfd, const char *de_name)
6504 struct stage_path_arg *a = arg;
6505 const struct got_error *err = NULL;
6506 struct got_fileindex_entry *ie;
6507 char *ondisk_path = NULL, *path_content = NULL;
6508 uint32_t stage;
6509 struct got_object_id *new_staged_blob_id = NULL;
6511 if (status == GOT_STATUS_UNVERSIONED)
6512 return NULL;
6514 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6515 if (ie == NULL)
6516 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6518 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6519 relpath)== -1)
6520 return got_error_from_errno("asprintf");
6522 switch (status) {
6523 case GOT_STATUS_ADD:
6524 case GOT_STATUS_MODIFY:
6525 if (a->patch_cb) {
6526 if (status == GOT_STATUS_ADD) {
6527 int choice = GOT_PATCH_CHOICE_NONE;
6528 err = (*a->patch_cb)(&choice, a->patch_arg,
6529 status, ie->path, NULL, 1, 1);
6530 if (err)
6531 break;
6532 if (choice != GOT_PATCH_CHOICE_YES)
6533 break;
6534 } else {
6535 err = create_patched_content(&path_content, 0,
6536 staged_blob_id ? staged_blob_id : blob_id,
6537 ondisk_path, dirfd, de_name, ie->path,
6538 a->repo, a->patch_cb, a->patch_arg);
6539 if (err || path_content == NULL)
6540 break;
6543 err = got_object_blob_create(&new_staged_blob_id,
6544 path_content ? path_content : ondisk_path, a->repo);
6545 if (err)
6546 break;
6547 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6548 SHA1_DIGEST_LENGTH);
6549 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6550 stage = GOT_FILEIDX_STAGE_ADD;
6551 else
6552 stage = GOT_FILEIDX_STAGE_MODIFY;
6553 got_fileindex_entry_stage_set(ie, stage);
6554 a->staged_something = 1;
6555 if (a->status_cb == NULL)
6556 break;
6557 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6558 get_staged_status(ie), relpath, blob_id,
6559 new_staged_blob_id, NULL, dirfd, de_name);
6560 break;
6561 case GOT_STATUS_DELETE:
6562 if (staged_status == GOT_STATUS_DELETE)
6563 break;
6564 if (a->patch_cb) {
6565 int choice = GOT_PATCH_CHOICE_NONE;
6566 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6567 ie->path, NULL, 1, 1);
6568 if (err)
6569 break;
6570 if (choice == GOT_PATCH_CHOICE_NO)
6571 break;
6572 if (choice != GOT_PATCH_CHOICE_YES) {
6573 err = got_error(GOT_ERR_PATCH_CHOICE);
6574 break;
6577 stage = GOT_FILEIDX_STAGE_DELETE;
6578 got_fileindex_entry_stage_set(ie, stage);
6579 a->staged_something = 1;
6580 if (a->status_cb == NULL)
6581 break;
6582 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6583 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6584 de_name);
6585 break;
6586 case GOT_STATUS_NO_CHANGE:
6587 break;
6588 case GOT_STATUS_CONFLICT:
6589 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6590 break;
6591 case GOT_STATUS_NONEXISTENT:
6592 err = got_error_set_errno(ENOENT, relpath);
6593 break;
6594 default:
6595 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6596 break;
6599 if (path_content && unlink(path_content) == -1 && err == NULL)
6600 err = got_error_from_errno2("unlink", path_content);
6601 free(path_content);
6602 free(ondisk_path);
6603 free(new_staged_blob_id);
6604 return err;
6607 const struct got_error *
6608 got_worktree_stage(struct got_worktree *worktree,
6609 struct got_pathlist_head *paths,
6610 got_worktree_status_cb status_cb, void *status_arg,
6611 got_worktree_patch_cb patch_cb, void *patch_arg,
6612 struct got_repository *repo)
6614 const struct got_error *err = NULL, *sync_err, *unlockerr;
6615 struct got_pathlist_entry *pe;
6616 struct got_fileindex *fileindex = NULL;
6617 char *fileindex_path = NULL;
6618 struct got_reference *head_ref = NULL;
6619 struct got_object_id *head_commit_id = NULL;
6620 struct check_stage_ok_arg oka;
6621 struct stage_path_arg spa;
6623 err = lock_worktree(worktree, LOCK_EX);
6624 if (err)
6625 return err;
6627 err = got_ref_open(&head_ref, repo,
6628 got_worktree_get_head_ref_name(worktree), 0);
6629 if (err)
6630 goto done;
6631 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6632 if (err)
6633 goto done;
6634 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6635 if (err)
6636 goto done;
6638 /* Check pre-conditions before staging anything. */
6639 oka.head_commit_id = head_commit_id;
6640 oka.worktree = worktree;
6641 oka.fileindex = fileindex;
6642 oka.repo = repo;
6643 oka.have_changes = 0;
6644 TAILQ_FOREACH(pe, paths, entry) {
6645 err = worktree_status(worktree, pe->path, fileindex, repo,
6646 check_stage_ok, &oka, NULL, NULL, 0, 0);
6647 if (err)
6648 goto done;
6650 if (!oka.have_changes) {
6651 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6652 goto done;
6655 spa.worktree = worktree;
6656 spa.fileindex = fileindex;
6657 spa.repo = repo;
6658 spa.patch_cb = patch_cb;
6659 spa.patch_arg = patch_arg;
6660 spa.status_cb = status_cb;
6661 spa.status_arg = status_arg;
6662 spa.staged_something = 0;
6663 TAILQ_FOREACH(pe, paths, entry) {
6664 err = worktree_status(worktree, pe->path, fileindex, repo,
6665 stage_path, &spa, NULL, NULL, 0, 0);
6666 if (err)
6667 goto done;
6669 if (!spa.staged_something) {
6670 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6671 goto done;
6674 sync_err = sync_fileindex(fileindex, fileindex_path);
6675 if (sync_err && err == NULL)
6676 err = sync_err;
6677 done:
6678 if (head_ref)
6679 got_ref_close(head_ref);
6680 free(head_commit_id);
6681 free(fileindex_path);
6682 if (fileindex)
6683 got_fileindex_free(fileindex);
6684 unlockerr = lock_worktree(worktree, LOCK_SH);
6685 if (unlockerr && err == NULL)
6686 err = unlockerr;
6687 return err;
6690 struct unstage_path_arg {
6691 struct got_worktree *worktree;
6692 struct got_fileindex *fileindex;
6693 struct got_repository *repo;
6694 got_worktree_checkout_cb progress_cb;
6695 void *progress_arg;
6696 got_worktree_patch_cb patch_cb;
6697 void *patch_arg;
6700 static const struct got_error *
6701 create_unstaged_content(char **path_unstaged_content,
6702 char **path_new_staged_content, struct got_object_id *blob_id,
6703 struct got_object_id *staged_blob_id, const char *relpath,
6704 struct got_repository *repo,
6705 got_worktree_patch_cb patch_cb, void *patch_arg)
6707 const struct got_error *err;
6708 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6709 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6710 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6711 struct stat sb1, sb2;
6712 struct got_diff_changes *changes = NULL;
6713 struct got_diff_state *ds = NULL;
6714 struct got_diff_args *args = NULL;
6715 struct got_diff_change *change;
6716 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6717 int have_content = 0, have_rejected_content = 0;
6719 *path_unstaged_content = NULL;
6720 *path_new_staged_content = NULL;
6722 err = got_object_id_str(&label1, blob_id);
6723 if (err)
6724 return err;
6725 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6726 if (err)
6727 goto done;
6729 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6730 if (err)
6731 goto done;
6733 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6734 if (err)
6735 goto done;
6737 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6738 if (err)
6739 goto done;
6741 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6742 if (err)
6743 goto done;
6745 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6746 if (err)
6747 goto done;
6749 if (stat(path1, &sb1) == -1) {
6750 err = got_error_from_errno2("stat", path1);
6751 goto done;
6754 if (stat(path2, &sb2) == -1) {
6755 err = got_error_from_errno2("stat", path2);
6756 goto done;
6759 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6760 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6761 if (err)
6762 goto done;
6764 err = got_opentemp_named(path_unstaged_content, &outfile,
6765 "got-unstaged-content");
6766 if (err)
6767 goto done;
6768 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6769 "got-new-staged-content");
6770 if (err)
6771 goto done;
6773 if (fseek(f1, 0L, SEEK_SET) == -1) {
6774 err = got_ferror(f1, GOT_ERR_IO);
6775 goto done;
6777 if (fseek(f2, 0L, SEEK_SET) == -1) {
6778 err = got_ferror(f2, GOT_ERR_IO);
6779 goto done;
6781 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6782 int choice;
6783 err = apply_or_reject_change(&choice, change, ++n,
6784 changes->nchanges, ds, args, diff_flags, relpath,
6785 f1, f2, &line_cur1, &line_cur2,
6786 outfile, rejectfile, patch_cb, patch_arg);
6787 if (err)
6788 goto done;
6789 if (choice == GOT_PATCH_CHOICE_YES)
6790 have_content = 1;
6791 else
6792 have_rejected_content = 1;
6793 if (choice == GOT_PATCH_CHOICE_QUIT)
6794 break;
6796 if (have_content || have_rejected_content)
6797 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6798 outfile, rejectfile);
6799 done:
6800 free(label1);
6801 if (blob)
6802 got_object_blob_close(blob);
6803 if (staged_blob)
6804 got_object_blob_close(staged_blob);
6805 if (f1 && fclose(f1) == EOF && err == NULL)
6806 err = got_error_from_errno2("fclose", path1);
6807 if (f2 && fclose(f2) == EOF && err == NULL)
6808 err = got_error_from_errno2("fclose", path2);
6809 if (outfile && fclose(outfile) == EOF && err == NULL)
6810 err = got_error_from_errno2("fclose", *path_unstaged_content);
6811 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6812 err = got_error_from_errno2("fclose", *path_new_staged_content);
6813 if (path1 && unlink(path1) == -1 && err == NULL)
6814 err = got_error_from_errno2("unlink", path1);
6815 if (path2 && unlink(path2) == -1 && err == NULL)
6816 err = got_error_from_errno2("unlink", path2);
6817 if (err || !have_content) {
6818 if (*path_unstaged_content &&
6819 unlink(*path_unstaged_content) == -1 && err == NULL)
6820 err = got_error_from_errno2("unlink",
6821 *path_unstaged_content);
6822 free(*path_unstaged_content);
6823 *path_unstaged_content = NULL;
6825 if (err || !have_rejected_content) {
6826 if (*path_new_staged_content &&
6827 unlink(*path_new_staged_content) == -1 && err == NULL)
6828 err = got_error_from_errno2("unlink",
6829 *path_new_staged_content);
6830 free(*path_new_staged_content);
6831 *path_new_staged_content = NULL;
6833 free(args);
6834 if (ds) {
6835 got_diff_state_free(ds);
6836 free(ds);
6838 if (changes)
6839 got_diff_free_changes(changes);
6840 free(path1);
6841 free(path2);
6842 return err;
6845 static const struct got_error *
6846 unstage_path(void *arg, unsigned char status,
6847 unsigned char staged_status, const char *relpath,
6848 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6849 struct got_object_id *commit_id, int dirfd, const char *de_name)
6851 const struct got_error *err = NULL;
6852 struct unstage_path_arg *a = arg;
6853 struct got_fileindex_entry *ie;
6854 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6855 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6856 char *path_new_staged_content = NULL;
6857 char *id_str = NULL, *label_orig = NULL;
6858 int local_changes_subsumed;
6859 struct stat sb;
6861 if (staged_status != GOT_STATUS_ADD &&
6862 staged_status != GOT_STATUS_MODIFY &&
6863 staged_status != GOT_STATUS_DELETE)
6864 return NULL;
6866 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6867 if (ie == NULL)
6868 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6871 == -1)
6872 return got_error_from_errno("asprintf");
6874 err = got_object_id_str(&id_str,
6875 commit_id ? commit_id : a->worktree->base_commit_id);
6876 if (err)
6877 goto done;
6878 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6879 id_str) == -1) {
6880 err = got_error_from_errno("asprintf");
6881 goto done;
6884 switch (staged_status) {
6885 case GOT_STATUS_MODIFY:
6886 err = got_object_open_as_blob(&blob_base, a->repo,
6887 blob_id, 8192);
6888 if (err)
6889 break;
6890 /* fall through */
6891 case GOT_STATUS_ADD:
6892 if (a->patch_cb) {
6893 if (staged_status == GOT_STATUS_ADD) {
6894 int choice = GOT_PATCH_CHOICE_NONE;
6895 err = (*a->patch_cb)(&choice, a->patch_arg,
6896 staged_status, ie->path, NULL, 1, 1);
6897 if (err)
6898 break;
6899 if (choice != GOT_PATCH_CHOICE_YES)
6900 break;
6901 } else {
6902 err = create_unstaged_content(
6903 &path_unstaged_content,
6904 &path_new_staged_content, blob_id,
6905 staged_blob_id, ie->path, a->repo,
6906 a->patch_cb, a->patch_arg);
6907 if (err || path_unstaged_content == NULL)
6908 break;
6909 if (path_new_staged_content) {
6910 err = got_object_blob_create(
6911 &staged_blob_id,
6912 path_new_staged_content,
6913 a->repo);
6914 if (err)
6915 break;
6916 memcpy(ie->staged_blob_sha1,
6917 staged_blob_id->sha1,
6918 SHA1_DIGEST_LENGTH);
6920 err = merge_file(&local_changes_subsumed,
6921 a->worktree, blob_base, ondisk_path,
6922 relpath, got_fileindex_perms_to_st(ie),
6923 path_unstaged_content, label_orig,
6924 "unstaged", a->repo, a->progress_cb,
6925 a->progress_arg);
6926 if (err == NULL &&
6927 path_new_staged_content == NULL)
6928 got_fileindex_entry_stage_set(ie,
6929 GOT_FILEIDX_STAGE_NONE);
6930 break; /* Done with this file. */
6933 err = got_object_open_as_blob(&blob_staged, a->repo,
6934 staged_blob_id, 8192);
6935 if (err)
6936 break;
6937 err = merge_blob(&local_changes_subsumed, a->worktree,
6938 blob_base, ondisk_path, relpath,
6939 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6940 commit_id ? commit_id : a->worktree->base_commit_id,
6941 a->repo, a->progress_cb, a->progress_arg);
6942 if (err == NULL)
6943 got_fileindex_entry_stage_set(ie,
6944 GOT_FILEIDX_STAGE_NONE);
6945 break;
6946 case GOT_STATUS_DELETE:
6947 if (a->patch_cb) {
6948 int choice = GOT_PATCH_CHOICE_NONE;
6949 err = (*a->patch_cb)(&choice, a->patch_arg,
6950 staged_status, ie->path, NULL, 1, 1);
6951 if (err)
6952 break;
6953 if (choice == GOT_PATCH_CHOICE_NO)
6954 break;
6955 if (choice != GOT_PATCH_CHOICE_YES) {
6956 err = got_error(GOT_ERR_PATCH_CHOICE);
6957 break;
6960 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6961 err = get_file_status(&status, &sb, ie, ondisk_path,
6962 dirfd, de_name, a->repo);
6963 if (err)
6964 break;
6965 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6966 break;
6968 done:
6969 free(ondisk_path);
6970 if (path_unstaged_content &&
6971 unlink(path_unstaged_content) == -1 && err == NULL)
6972 err = got_error_from_errno2("unlink", path_unstaged_content);
6973 if (path_new_staged_content &&
6974 unlink(path_new_staged_content) == -1 && err == NULL)
6975 err = got_error_from_errno2("unlink", path_new_staged_content);
6976 free(path_unstaged_content);
6977 free(path_new_staged_content);
6978 if (blob_base)
6979 got_object_blob_close(blob_base);
6980 if (blob_staged)
6981 got_object_blob_close(blob_staged);
6982 free(id_str);
6983 free(label_orig);
6984 return err;
6987 const struct got_error *
6988 got_worktree_unstage(struct got_worktree *worktree,
6989 struct got_pathlist_head *paths,
6990 got_worktree_checkout_cb progress_cb, void *progress_arg,
6991 got_worktree_patch_cb patch_cb, void *patch_arg,
6992 struct got_repository *repo)
6994 const struct got_error *err = NULL, *sync_err, *unlockerr;
6995 struct got_pathlist_entry *pe;
6996 struct got_fileindex *fileindex = NULL;
6997 char *fileindex_path = NULL;
6998 struct unstage_path_arg upa;
7000 err = lock_worktree(worktree, LOCK_EX);
7001 if (err)
7002 return err;
7004 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7005 if (err)
7006 goto done;
7008 upa.worktree = worktree;
7009 upa.fileindex = fileindex;
7010 upa.repo = repo;
7011 upa.progress_cb = progress_cb;
7012 upa.progress_arg = progress_arg;
7013 upa.patch_cb = patch_cb;
7014 upa.patch_arg = patch_arg;
7015 TAILQ_FOREACH(pe, paths, entry) {
7016 err = worktree_status(worktree, pe->path, fileindex, repo,
7017 unstage_path, &upa, NULL, NULL, 0, 0);
7018 if (err)
7019 goto done;
7022 sync_err = sync_fileindex(fileindex, fileindex_path);
7023 if (sync_err && err == NULL)
7024 err = sync_err;
7025 done:
7026 free(fileindex_path);
7027 if (fileindex)
7028 got_fileindex_free(fileindex);
7029 unlockerr = lock_worktree(worktree, LOCK_SH);
7030 if (unlockerr && err == NULL)
7031 err = unlockerr;
7032 return err;